Skip to content

Commit

Permalink
Merge pull request #20 from cyberman54/master
Browse files Browse the repository at this point in the history
v1.1.0
  • Loading branch information
oliverbrandmueller authored Dec 19, 2022
2 parents ba8d899 + 5296800 commit fe98131
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions lib/libpax/blescan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/libpax/libpax.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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];
};

Expand Down
30 changes: 17 additions & 13 deletions lib/libpax/libpax_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,6 +130,10 @@ int libpax_update_config(struct libpax_config_t* configuration) {

if (result == 0) {
memcpy(&current_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;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/libpax/libpax_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
};


Expand Down
74 changes: 32 additions & 42 deletions lib/libpax/wifiscan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,49 +32,50 @@ Which in turn is based of Łukasz Marcin Podkalicki's ESP32/016 WiFi Sniffer
#include "globals.h"

#include <string.h>
#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;
}
}
Expand All @@ -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
}
3 changes: 2 additions & 1 deletion lib/libpax/wifiscan.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -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":
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/libpax_test_cases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit fe98131

Please sign in to comment.