From 875ad77967b862f179b51f288f6036f9ae246f56 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 30 Mar 2018 20:41:08 +0200 Subject: [PATCH] wifisniffer.cpp: code improvements --- platformio.ini | 4 ++-- src/globals.h | 3 ++- src/main.cpp | 9 +++++---- src/main.h | 2 +- src/rcommand.cpp | 3 ++- src/wifisniffer.cpp | 18 ++++++++---------- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/platformio.ini b/platformio.ini index 88ba9fc3..26c735d5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -10,9 +10,9 @@ ; ---> SELECT TARGET PLATFORM HERE! <--- [platformio] -env_default = heltec_wifi_lora_32 +;env_default = heltec_wifi_lora_32 ;env_default = ttgov1 -;env_default = ttgov2 +env_default = ttgov2 ;env_default = lopy ;env_default = lopy4 ;env_default = lolin32lite_lora diff --git a/src/globals.h b/src/globals.h index 8d6ed3fa..9ac43b8f 100644 --- a/src/globals.h +++ b/src/globals.h @@ -35,7 +35,8 @@ extern uint64_t uptimecounter; extern osjob_t sendjob; extern int macnum, blenum, countermode, screensaver, adrmode, lorasf, txpower, rlim; extern bool joinstate; -extern std::set > macs; +//extern std::set > macs; +extern std::set macs; #ifdef HAS_DISPLAY extern HAS_DISPLAY u8x8; diff --git a/src/main.cpp b/src/main.cpp index c1db254a..68d16700 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -49,7 +49,7 @@ int macnum = 0, blenum = 0; uint64_t uptimecounter = 0; bool joinstate = false; -std::set > macs; // storage holds MAC frames +std::set macs; // storage holds MAC frames // this variable will be changed in the ISR, and read in main loop static volatile bool ButtonTriggered = false; @@ -208,7 +208,7 @@ void wifi_sniffer_loop(void * pvParameters) { // execute BLE count if BLE function is enabled #ifdef BLECOUNTER - if ( cfg.blescan ) + if (cfg.blescan) BLECount(); #endif @@ -220,8 +220,9 @@ void wifi_sniffer_loop(void * pvParameters) { yield(); // clear counter if not in cumulative counter mode - if ( cfg.countermode != 1 ) { - macs.erase(macs.begin(), macs.end()); // clear RAM + if (cfg.countermode != 1) { + //macs.erase(macs.begin(), macs.end()); // clear RAM + macs.clear(); // clear macs container macnum = 0; u8x8.clearLine(0); u8x8.clearLine(1); // clear Display counter } diff --git a/src/main.h b/src/main.h index a32c075b..610c9bcc 100644 --- a/src/main.h +++ b/src/main.h @@ -1,5 +1,5 @@ // program version -#define PROGVERSION "1.2.5" // use max 10 chars here! +#define PROGVERSION "1.2.51" // use max 10 chars here! #define PROGNAME "PAXCNT" // Verbose enables serial output diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 46fe8022..de62bbde 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -67,7 +67,8 @@ void set_reset(int val) { break; case 1: // reset MAC counter ESP_LOGI(TAG, "Remote command: reset MAC counter"); - macs.erase(macs.begin(), macs.end()); // clear RAM + //macs.erase(macs.begin(), macs.end()); // clear RAM + macs.clear(); // clear macs container macnum = 0; u8x8.clearLine(0); u8x8.clearLine(1); // clear Display counter u8x8.clearLine(5); diff --git a/src/wifisniffer.cpp b/src/wifisniffer.cpp index f2ee2d17..3fffdee0 100644 --- a/src/wifisniffer.cpp +++ b/src/wifisniffer.cpp @@ -38,13 +38,11 @@ extern void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t void wifi_sniffer_init(void) { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); cfg.nvs_enable = 0; // we don't want wifi settings from NVRAM + wifi_promiscuous_filter_t filter = {.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_country(&wifi_country)); ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM) ); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL) ); - //ESP_ERROR_CHECK(esp_wifi_start()); // not sure if we need this in this application? - //ESP_ERROR_CHECK(esp_wifi_set_max_tx_power(-128)); // we don't need to TX, so we use lowest power level to save energy - wifi_promiscuous_filter_t filter = {.filter_mask = WIFI_PROMIS_FILTER_MASK_MGMT}; // we need only MGMT frames ESP_ERROR_CHECK(esp_wifi_set_promiscuous_filter(&filter)); // set MAC frame filter ESP_ERROR_CHECK(esp_wifi_set_promiscuous_rx_cb(&wifi_sniffer_packet_handler)); ESP_ERROR_CHECK(esp_wifi_set_promiscuous(true)); @@ -59,6 +57,7 @@ void wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) { const wifi_ieee80211_packet_t *ipkt = (wifi_ieee80211_packet_t *)ppkt->payload; const wifi_ieee80211_mac_hdr_t *hdr = &ipkt->hdr; char counter [10]; + std::pair::iterator, bool> newmac; if (( cfg.rssilimit == 0 ) || (ppkt->rx_ctrl.rssi > cfg.rssilimit )) { // rssi is negative value uint64_t addr2int = ( (uint64_t)hdr->addr2[0] ) | ( (uint64_t)hdr->addr2[1] << 8 ) | ( (uint64_t)hdr->addr2[2] << 16 ) | \ @@ -69,19 +68,18 @@ void wifi_sniffer_packet_handler(void* buff, wifi_promiscuous_pkt_type_t type) { if ( std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) { #endif - macs.insert(addr2int); - - // INFO: RSSI when adding MAC + + // INFO: RSSI when found MAC in range ESP_LOGI(TAG, "WiFi RSSI: %02d", ppkt->rx_ctrl.rssi); // if new unique MAC logged increment counter on display - if ( macs.size() > macnum ) { - macnum = macs.size(); + newmac = macs.insert(addr2int); + if (newmac.second) { + macnum++; itoa(macnum, counter, 10); u8x8.draw2x2String(0, 0, counter); ESP_LOGI(TAG, "MAC counter: %4i", macnum); - } - + } #ifdef VENDORFILTER }