diff --git a/CHANGELOG.md b/CHANGELOG.md index c8eec63..fed734d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [1.1.0] - 2022-10-29 +- Adapt Wifi country settings to ESP IDF v4.4 Wi-Fi API +- Keep backward compatibility to 1.0.1 + ## [1.0.1] - 2022-04-14 - Count randomized MACs only (Wifi and BLE) - fix use LSB format to interprete BLE MACS diff --git a/lib/libpax/blescan.cpp b/lib/libpax/blescan.cpp index 81cdffb..42a1d4d 100644 --- a/lib/libpax/blescan.cpp +++ b/lib/libpax/blescan.cpp @@ -12,8 +12,10 @@ #define BLESCANINTERVAL 80 // [illiseconds] #endif -// local Tag for logging -static const char TAG[] = "bluetooth"; +#ifndef TAG +#define TAG __FILE__ +#endif + int initialized_ble = 0; int ble_rssi_threshold = 0; @@ -282,7 +284,6 @@ void stop_BLE_scan(void) { ESP_ERROR_CHECK(esp_bt_controller_disable()); ESP_ERROR_CHECK(esp_bt_controller_deinit()); #endif - ESP_ERROR_CHECK(esp_coex_preference_set(ESP_COEX_PREFER_WIFI)); ESP_LOGI(TAG, "Bluetooth scanner stopped"); initialized_ble = 0; } diff --git a/lib/libpax/libpax.h b/lib/libpax/libpax.h index ce68fd6..8ef4b79 100644 --- a/lib/libpax/libpax.h +++ b/lib/libpax/libpax.h @@ -6,7 +6,7 @@ #include "wifiscan.h" #define CONFIG_MAJOR_VERSION 1 -#define CONFIG_MINOR_VERSION 0 +#define CONFIG_MINOR_VERSION 1 /* Memory payload structure for persiting configurations @@ -18,9 +18,9 @@ struct libpax_config_storage_t { uint8_t reserved_start[2]; struct libpax_config_t config; // Added for structure alignment - uint8_t pad; - // reserved for future use - uint8_t reserved_end[25]; + uint8_t pad[2]; + // reserved for future use + uint8_t reserved_end[21]; uint8_t checksum[4]; }; diff --git a/lib/libpax/libpax_api.cpp b/lib/libpax/libpax_api.cpp index 3e8c318..8c96683 100644 --- a/lib/libpax/libpax_api.cpp +++ b/lib/libpax/libpax_api.cpp @@ -95,7 +95,7 @@ void libpax_default_config(struct libpax_config_t* configuration) { memset(configuration, 0, sizeof(struct libpax_config_t)); configuration->blecounter = 0; configuration->wificounter = 1; - configuration->wifi_my_country = 1; + strcpy(configuration->wifi_my_country_str, "01"); configuration->wifi_channel_map = 0b100010100100100; configuration->wifi_channel_switch_interval = 50; configuration->wifi_rssi_threshold = 0; @@ -130,6 +130,10 @@ int libpax_update_config(struct libpax_config_t* configuration) { if (result == 0) { memcpy(¤t_config, configuration, sizeof(struct libpax_config_t)); + // this if to keep v1.0.1 backward compatibility + if (strcmp(current_config.wifi_my_country_str, "")) { + strcpy(current_config.wifi_my_country_str, current_config.wifi_my_country ? "DE" : "01"); + } config_set = 1; } return result; @@ -172,26 +176,26 @@ int libpax_counter_start() { ESP_LOGE("configuration", "Configuration was not yet set."); return -1; } - if (current_config.wificounter) { - set_wifi_country(current_config.wifi_my_country); - set_wifi_channels(current_config.wifi_channel_map); - set_wifi_rssi_filter(current_config.wifi_rssi_threshold); - wifi_sniffer_init(current_config.wifi_channel_switch_interval); - } - if (current_config.wificounter && current_config.blecounter) { - esp_wifi_set_ps(WIFI_PS_MIN_MODEM); - } else { - esp_wifi_set_ps(WIFI_PS_NONE); - } - if (current_config.blecounter) { + // turn on BT before Wifi, since the ESP32 API coexistence configuration option depends + // on the Bluetooth configuration option + if (current_config.blecounter) { set_BLE_rssi_filter(current_config.ble_rssi_threshold); start_BLE_scan(current_config.blescantime, current_config.blescanwindow, current_config.blescaninterval); } + if (current_config.wificounter) { + wifi_sniffer_init(current_config.wifi_channel_switch_interval); + set_wifi_country(current_config.wifi_my_country_str); + set_wifi_channels(current_config.wifi_channel_map); + set_wifi_rssi_filter(current_config.wifi_rssi_threshold); + } return 0; } int libpax_counter_stop() { + if (PaxReportTimer == NULL) { + return -1; + } wifi_sniffer_stop(); stop_BLE_scan(); xTimerStop(PaxReportTimer, 0); diff --git a/lib/libpax/libpax_api.h b/lib/libpax/libpax_api.h index 5d72e4c..a19e8f2 100644 --- a/lib/libpax/libpax_api.h +++ b/lib/libpax/libpax_api.h @@ -39,6 +39,7 @@ struct libpax_config_t { uint32_t blescantime; // [seconds] scan duration, 0 means infinite [default] uint16_t blescanwindow; // [milliseconds] scan window, see below, 3 ... 10240, default 80ms uint16_t blescaninterval; // [illiseconds] scan interval, see below, 3 ... 10240, default 80ms = 100% duty cycle + char wifi_my_country_str[3]; // set country code for WiFi RF settings, e.g. "01", "DE", etc. }; diff --git a/lib/libpax/wifiscan.cpp b/lib/libpax/wifiscan.cpp index 61d8e01..ae6491b 100644 --- a/lib/libpax/wifiscan.cpp +++ b/lib/libpax/wifiscan.cpp @@ -32,49 +32,50 @@ Which in turn is based of Ɓukasz Marcin Podkalicki's ESP32/016 WiFi Sniffer #include "globals.h" #include -#include "wifiscan.h" #include "libpax.h" +#include "wifiscan.h" TimerHandle_t WifiChanTimer; int initialized_wifi = 0; int wifi_rssi_threshold = 0; uint16_t channels_map = WIFI_CHANNEL_ALL; - -#define WIFI_CHANNEL_MAX 13 -// default values for country configuration -static wifi_country_t wifi_country = {"EU", 1, - WIFI_CHANNEL_MAX, 100, - WIFI_COUNTRY_POLICY_MANUAL}; +static wifi_country_t country; void wifi_noop_sniffer(void* buff, wifi_promiscuous_pkt_type_t type) {} // using IRAM_ATTR here to speed up callback function -static IRAM_ATTR void -wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) { +static IRAM_ATTR void wifi_sniffer_packet_handler( + void* buff, wifi_promiscuous_pkt_type_t type) { const wifi_promiscuous_pkt_t* ppkt = (wifi_promiscuous_pkt_t*)buff; const wifi_ieee80211_packet_t* ipkt = (wifi_ieee80211_packet_t*)ppkt->payload; const wifi_ieee80211_mac_hdr_t* hdr = &ipkt->hdr; if ((wifi_rssi_threshold) && (ppkt->rx_ctrl.rssi < wifi_rssi_threshold)) // rssi is negative value - return; - else - mac_add((uint8_t *)hdr->addr2, MAC_SNIFF_WIFI); + return; + else + mac_add((uint8_t*)hdr->addr2, MAC_SNIFF_WIFI); } // Software-timer driven Wifi channel rotation callback function void switchWifiChannel(TimerHandle_t xTimer) { configASSERT(xTimer); - do { channel = - (channel % WIFI_CHANNEL_MAX) + 1; // rotate channels in bitmap + do { + channel = (channel % country.nchan) + 1; // rotate channels in bitmap } while (!(channels_map >> (channel - 1) & 1)); esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); } +void set_wifi_country(const char* country_code) { + ESP_ERROR_CHECK(esp_wifi_set_country_code(country_code, true)); + ESP_ERROR_CHECK(esp_wifi_get_country(&country)); +} + +// Keep this a while for compatibility with 1.0.1 void set_wifi_country(uint8_t cc) { - switch(cc) { - case 1: - memcpy(wifi_country.cc, "EU", sizeof("EU")); + switch (cc) { + case 1: + set_wifi_country("DE"); break; } } @@ -88,56 +89,45 @@ void set_wifi_rssi_filter(int set_rssi_threshold) { } void wifi_sniffer_init(uint16_t wifi_channel_switch_interval) { - #ifdef LIBPAX_WIFI +#ifdef LIBPAX_WIFI wifi_init_config_t wificfg = WIFI_INIT_CONFIG_DEFAULT(); wificfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM wificfg.wifi_task_core_id = 0; // we want wifi task running on core 0 - // wifi_promiscuous_filter_t filter = { - // .filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // only MGMT frames - // .filter_mask = WIFI_PROMIS_FILTER_MASK_ALL}; // we use all frames - + // filter management and data frames to the sniffer wifi_promiscuous_filter_t filter = {.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT | WIFI_PROMIS_FILTER_MASK_DATA}; ESP_ERROR_CHECK(esp_wifi_init(&wificfg)); // configure Wifi with cfg ESP_ERROR_CHECK( - esp_wifi_set_country(&wifi_country)); // set locales for RF and channels - ESP_ERROR_CHECK( - esp_wifi_set_storage(WIFI_STORAGE_RAM)); // we don't need NVRAM - ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL)); - ESP_ERROR_CHECK( - esp_wifi_set_promiscuous_filter(&filter)); // set frame filter + esp_wifi_set_promiscuous_filter(&filter)); // enable frame filtering ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); - ESP_ERROR_CHECK(esp_wifi_start()); // for esp_wifi v3.3 ESP_ERROR_CHECK( - esp_wifi_set_promiscuous(true)); // now switch on monitor mode + esp_wifi_set_promiscuous(true)); // start sniffer mode // setup wifi channel rotation timer - if(wifi_channel_switch_interval > 0) { - WifiChanTimer = xTimerCreate("WifiChannelTimer", pdMS_TO_TICKS(wifi_channel_switch_interval * 10), - pdTRUE, (void*)0, switchWifiChannel); + if (wifi_channel_switch_interval > 0) { + WifiChanTimer = xTimerCreate( + "WifiChannelTimer", pdMS_TO_TICKS(wifi_channel_switch_interval * 10), + pdTRUE, (void*)0, switchWifiChannel); assert(WifiChanTimer); xTimerStart(WifiChanTimer, 0); } - ESP_ERROR_CHECK(esp_wifi_start()); - esp_wifi_set_promiscuous(true); + initialized_wifi = 1; - #endif +#endif } void wifi_sniffer_stop() { - #ifdef LIBPAX_WIFI - if(initialized_wifi) { - if(WifiChanTimer) xTimerStop(WifiChanTimer, 0); +#ifdef LIBPAX_WIFI + if (initialized_wifi) { + if (WifiChanTimer) xTimerStop(WifiChanTimer, 0); ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_noop_sniffer)); ESP_ERROR_CHECK( esp_wifi_set_promiscuous(false)); // now switch off monitor mode - ESP_ERROR_CHECK(esp_wifi_stop()); esp_wifi_deinit(); - ESP_ERROR_CHECK(esp_coex_preference_set(ESP_COEX_PREFER_BT)); initialized_wifi = 0; } - #endif +#endif } diff --git a/lib/libpax/wifiscan.h b/lib/libpax/wifiscan.h index 8239109..285acdc 100644 --- a/lib/libpax/wifiscan.h +++ b/lib/libpax/wifiscan.h @@ -18,7 +18,8 @@ typedef struct { uint8_t payload[0]; // network data ended with 4 bytes csum (CRC32) } wifi_ieee80211_packet_t; -void set_wifi_country(uint8_t country_code); +void set_wifi_country(const char* country_code); +void set_wifi_country(uint8_t cc); void set_wifi_channels(uint16_t channels_map); void set_wifi_rssi_filter(int set_rssi_threshold); diff --git a/library.json b/library.json index 1681ebf..bb919a2 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "libpax", - "version":"1.0.1", + "version":"1.1.0", "keywords": "libpax", "description": "Library for PAX counting using BLE and WiFi", "repository": diff --git a/platformio.ini b/platformio.ini index 6d50a7a..73461ec 100644 --- a/platformio.ini +++ b/platformio.ini @@ -5,7 +5,7 @@ default_envs = espidf [env] board = ttgo-t-beam monitor_speed = 115200 -platform = espressif32@3.5.0 +platform = espressif32@5.2.0 [env:espidf] framework = espidf diff --git a/test/libpax_test_cases.cpp b/test/libpax_test_cases.cpp index fcbe663..547a967 100644 --- a/test/libpax_test_cases.cpp +++ b/test/libpax_test_cases.cpp @@ -122,7 +122,7 @@ void test_callback() { TEST_ASSERT_EQUAL(0, err_code); printf("libpax should be running\n"); - vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(6000)); + vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(6010)); TEST_ASSERT_EQUAL(6, time_called_back); }