some fixes in macsniff.cpp

This commit is contained in:
Klaus K Wilting 2018-04-02 14:34:16 +02:00
parent cbecfb4463
commit 60b1d31f6a
4 changed files with 39 additions and 43 deletions

View File

@ -186,7 +186,8 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts.
byte 11: BLE scan cycle duration in seconds (0..255) byte 11: BLE scan cycle duration in seconds (0..255)
byte 12: BLE scan mode (1=on, 0=0ff) byte 12: BLE scan mode (1=on, 0=0ff)
byte 13: Wifi antenna switch (0=internal, 1=external) byte 13: Wifi antenna switch (0=internal, 1=external)
bytes 14-23: Software version (ASCII format) byte 14: RGB LED luminosity (0..100 %)
bytes 15-24: Software version (ASCII format)
0x81 get device uptime 0x81 get device uptime

View File

@ -4,16 +4,16 @@
#include "globals.h" #include "globals.h"
#ifdef BLECOUNTER #ifdef BLECOUNTER
#include <BLEDevice.h> #include <BLEDevice.h>
#include <BLEUtils.h> #include <BLEUtils.h>
#include <BLEScan.h> #include <BLEScan.h>
#include <BLEAdvertisedDevice.h> #include <BLEAdvertisedDevice.h>
#endif #endif
#ifdef VENDORFILTER #ifdef VENDORFILTER
#include <array> #include <array>
#include <algorithm> #include <algorithm>
#include "vendor_array.h" #include "vendor_array.h"
#endif #endif
// Local logging tag // Local logging tag
@ -27,8 +27,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
char counter [6]; // uint16_t -> 2 byte -> 5 decimals + '0' terminator -> 6 chars char counter [6]; // uint16_t -> 2 byte -> 5 decimals + '0' terminator -> 6 chars
char macbuf [21]; // uint64_t -> 8 byte -> 20 decimals + '0' terminator -> 21 chars char macbuf [21]; // uint64_t -> 8 byte -> 20 decimals + '0' terminator -> 21 chars
char typebuff[8] ; char typebuff[8];
bool added = false;
uint64_t addr2int; uint64_t addr2int;
uint32_t vendor2int; uint32_t vendor2int;
uint16_t hashedmac; uint16_t hashedmac;
@ -38,9 +37,9 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
( (uint64_t)paddr[3] << 24 ) | ( (uint64_t)paddr[4] << 32 ) | ( (uint64_t)paddr[5] << 40 ); ( (uint64_t)paddr[3] << 24 ) | ( (uint64_t)paddr[4] << 32 ) | ( (uint64_t)paddr[5] << 40 );
#ifdef VENDORFILTER #ifdef VENDORFILTER
vendor2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[1] << 8 ) | ( (uint32_t)paddr[0] << 16 ); vendor2int = ( (uint32_t)paddr[2] ) | ( (uint32_t)paddr[1] << 8 ) | ( (uint32_t)paddr[0] << 16 );
// No vendor filter for BLE // No vendor filter for BLE
if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) { if ( (sniff_type==MAC_SNIFF_BLE) || std::find(vendors.begin(), vendors.end(), vendor2int) != vendors.end() ) {
#endif #endif
// salt and hash MAC, and if new unique one, store identifier in container and increment counter on display // salt and hash MAC, and if new unique one, store identifier in container and increment counter on display
@ -49,25 +48,21 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
addr2int |= (uint64_t) salt << 48; // prepend 16-bit salt to 48-bit MAC addr2int |= (uint64_t) salt << 48; // prepend 16-bit salt to 48-bit MAC
snprintf(macbuf, 21, "%llx", addr2int); // convert unsigned 64-bit salted MAC to 16 digit hex string snprintf(macbuf, 21, "%llx", addr2int); // convert unsigned 64-bit salted MAC to 16 digit hex string
hashedmac = rokkit(macbuf, 5); // hash MAC string, use 5 chars to fit hash in uint16_t container hashedmac = rokkit(macbuf, 5); // hash MAC string, use 5 chars to fit hash in uint16_t container
newmac = macs.insert(hashedmac); // store hashed MAC only if first time seen newmac = macs.insert(hashedmac); // add hashed MAC to total container if new unique
if (sniff_type == MAC_SNIFF_WIFI ) { if (sniff_type == MAC_SNIFF_WIFI ) {
newmac = wifis.insert(hashedmac); // store hashed MAC if new unique newmac = wifis.insert(hashedmac); // add hashed MAC to wifi container if new unique
strcpy(typebuff, "WiFi"); strcpy(typebuff, "WiFi");
} else if (sniff_type == MAC_SNIFF_BLE ) { } else if (sniff_type == MAC_SNIFF_BLE ) {
newmac = bles.insert(hashedmac); // store hashed MAC if new unique newmac = bles.insert(hashedmac); // add hashed MAC to BLE container if new unique
strcpy(typebuff, "BLE "); strcpy(typebuff, "BLE ");
} }
if (newmac.second) { // first time seen this WIFI/BLE MAC if (newmac.second) { // first time seen this WIFI or BLE MAC
// Insert to global counter
macs.insert(hashedmac);
added = true;
snprintf(counter, 6, "%i", macs.size()); // convert 16-bit MAC counter to decimal counter value snprintf(counter, 6, "%i", macs.size()); // convert 16-bit MAC counter to decimal counter value
//itoa(macs.size(), counter, 10); // base 10 decimal counter value
u8x8.draw2x2String(0, 0, counter); u8x8.draw2x2String(0, 0, counter);
ESP_LOGI(TAG, "%s RSSI %04d -> Hash %04x -> #%05i", typebuff, rssi, hashedmac, macs.size()); ESP_LOGI(TAG, "%s RSSI %04d -> Hash %04x -> #%05i", typebuff, rssi, hashedmac, macs.size());
} else { } else { // already seen WIFI or BLE MAC
ESP_LOGI(TAG, "%s RSSI %04d -> already seen", typebuff, rssi); ESP_LOGI(TAG, "%s RSSI %04d -> already seen", typebuff, rssi);
} }
@ -78,8 +73,8 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
} }
#endif #endif
// True if MAC (WiFi/BLE was new) // True if MAC WiFi/BLE was new
return added; return newmac.second;
} }
#ifdef BLECOUNTER #ifdef BLECOUNTER

View File

@ -49,11 +49,11 @@ int macnum = 0, salt;
uint64_t uptimecounter = 0; uint64_t uptimecounter = 0;
bool joinstate = false; bool joinstate = false;
std::set<uint16_t> macs; // associative container holds filtered total unique MAC adress hashes (Wifi + BLE) std::set<uint16_t> macs; // associative container holds total of unique MAC adress hashes (Wifi + BLE)
std::set<uint16_t> wifis; // associative container holds filtered Wifi MAC adress hashes std::set<uint16_t> wifis; // associative container holds unique Wifi MAC adress hashes
#ifdef BLECOUNTER #ifdef BLECOUNTER
std::set<uint16_t> bles; // associative container holds filtered BLE MAC adresses hashes std::set<uint16_t> bles; // associative container holds unique BLE MAC adresses hashes
int scanTime; int scanTime;
#endif #endif
@ -77,7 +77,7 @@ void saveConfig(void);
void loadConfig(void); void loadConfig(void);
#ifdef HAS_LED #ifdef HAS_LED
void set_onboard_led(int st); void set_onboard_led(int st);
#endif #endif
/* begin LMIC specific parts ------------------------------------------------------------ */ /* begin LMIC specific parts ------------------------------------------------------------ */
@ -250,7 +250,18 @@ void wifi_sniffer_loop(void * pvParameters) {
int nloop=0, lorawait=0; int nloop=0, lorawait=0;
while (true) { while (true) {
nloop++; nloop++; // acutal number of wifi loops
// execute BLE count if BLE function is enabled
#ifdef BLECOUNTER
// Once 2 full Wifi Channels scan, do a BLE scan
if (nloop % (WIFI_CHANNEL_MAX*2) == 0 ) {
// execute BLE count if BLE function is enabled
if (cfg.blescan)
BLECount();
}
#endif
vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS); vTaskDelay(cfg.wifichancycle*10 / portTICK_PERIOD_MS);
yield(); yield();
wifi_sniffer_set_channel(channel); wifi_sniffer_set_channel(channel);
@ -264,16 +275,6 @@ void wifi_sniffer_loop(void * pvParameters) {
u8x8.setCursor(0,4); u8x8.setCursor(0,4);
u8x8.printf("MAC#: %-5i", wifis.size()); u8x8.printf("MAC#: %-5i", wifis.size());
// execute BLE count if BLE function is enabled
#ifdef BLECOUNTER
// Once 2 full Wifi Channels scan, do a BLE scan
if (nloop % (WIFI_CHANNEL_MAX*2) == 0 ) {
// execute BLE count if BLE function is enabled
if (cfg.blescan)
BLECount();
}
#endif
// duration of one wifi scan loop reached? then send data and begin new scan cycle // duration of one wifi scan loop reached? then send data and begin new scan cycle
if( nloop >= ((100 / cfg.wifichancycle) * (cfg.wifiscancycle * 2)) ) { if( nloop >= ((100 / cfg.wifichancycle) * (cfg.wifiscancycle * 2)) ) {
u8x8.setPowerSave(!cfg.screenon); // set display on if enabled u8x8.setPowerSave(!cfg.screenon); // set display on if enabled
@ -290,8 +291,7 @@ void wifi_sniffer_loop(void * pvParameters) {
wifis.clear(); // clear Wifi macs couner wifis.clear(); // clear Wifi macs couner
#ifdef BLECOUNTER #ifdef BLECOUNTER
bles.clear(); // clear BLE macs counter bles.clear(); // clear BLE macs counter
#endif #endif
salt = random(65536); // get new 16bit random for salting hashes salt = random(65536); // get new 16bit random for salting hashes
u8x8.clearLine(0); u8x8.clearLine(1); // clear Display counter u8x8.clearLine(0); u8x8.clearLine(1); // clear Display counter
} }
@ -457,7 +457,7 @@ void setup() {
antenna_init(); antenna_init();
#endif #endif
// initialize salt value using esp_random() called by random in arduino-esp32 core // initialize salt value using esp_random() called by random() in arduino-esp32 core
salt = random(65536); // get new 16bit random for salting hashes salt = random(65536); // get new 16bit random for salting hashes
// initialize display // initialize display

View File

@ -1,5 +1,5 @@
// program version // program version
#define PROGVERSION "1.2.71" // use max 10 chars here! #define PROGVERSION "1.2.8" // use max 10 chars here!
#define PROGNAME "PAXCNT" #define PROGNAME "PAXCNT"
// Verbose enables serial output // Verbose enables serial output