diff --git a/include/corona.h b/include/corona.h index fcbf556d..a11990d5 100644 --- a/include/corona.h +++ b/include/corona.h @@ -9,7 +9,7 @@ #include bool cwa_init(void); -bool cwa_mac_add(uint8_t *); +bool cwa_mac_add(uint16_t hashedmac); void cwa_clear(void); uint16_t cwa_report(void); diff --git a/include/macsniff.h b/include/macsniff.h index 574048b7..e216af62 100644 --- a/include/macsniff.h +++ b/include/macsniff.h @@ -16,7 +16,7 @@ uint16_t get_salt(void); uint64_t macConvert(uint8_t *paddr); -bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); +uint16_t mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type); void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb); #endif diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 10dcc900..bfc8adc8 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -114,7 +114,9 @@ const char *btsig_gap_type(uint32_t gap_type) { // using IRAM_:ATTR here to speed up callback function IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) { + esp_ble_gap_cb_param_t *p = (esp_ble_gap_cb_param_t *)param; + uint16_t hashedmac = 0; ESP_LOGV(TAG, "BT payload rcvd -> type: 0x%.2x -> %s", *p->scan_rst.ble_adv, btsig_gap_type(*p->scan_rst.ble_adv)); @@ -151,24 +153,20 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, } #if (VENDORFILTER) - if ((p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RANDOM) || (p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_RANDOM)) { ESP_LOGV(TAG, "BT device filtered"); break; } - #endif - // add this device and show new count total if it was not previously added - mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE); + // hash and add this device and show new count total if it was not previously added + hashedmac = mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE); #if (COUNT_ENS) - // we can call the ens functions now using hashed max-value in mac_add() - // check for ens signature if (0 == strncmp((const char *)p->scan_rst.ble_adv, ensMagicBytes, 4)) { - cwa_mac_add(p->scan_rst.bda); + cwa_mac_add(hashedmac); } #endif diff --git a/src/corona.cpp b/src/corona.cpp index 0ef136a2..00619191 100644 --- a/src/corona.cpp +++ b/src/corona.cpp @@ -15,10 +15,6 @@ static const char TAG[] = __FILE__; #include "corona.h" -// taken from macsniff.cpp -extern uint16_t salt; -extern uint16_t hashedmac; - // When to forget old senders. #define FORGET_AFTER_MINUTES 2 @@ -27,15 +23,16 @@ static std::map cwaSeenNotifiers; // Remove notifiers last seen over FORGET_AFTER_MINUTES ago. void cwa_clear() { - ESP_LOGD(TAG, "CWA: forget old notifier: %d", cwaSeenNotifiers.size()); - /* - #ifdef SOME_FORM_OF_DEBUG + + #ifdef SOME_FORM_OF_DEBUG + ESP_LOGD(TAG, "CWA: forget old notifier: %d", cwaSeenNotifiers.size()); for (auto const ¬ifier : cwaSeenNotifiers) { ESP_LOGD(TAG, "CWA forget <%X>", notifier.first); // } } #endif + */ // clear everything, otherwise we would count the same device again, as in the @@ -51,30 +48,22 @@ bool cwa_init(void) { return true; } -// similar to mac_add(), found in macsniff.cpp -// for comments pls. look into this function -bool cwa_mac_add(uint8_t *paddr) { - - // are we too early? - if (!hashedmac) - return false; // YES -> return +// similar to mac_add(), found in macsniff.cpp, for comments look into this function +bool cwa_mac_add(uint16_t hashedmac) { bool added = false; - ESP_LOGD(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", - BT_BD_ADDR_HEX(paddr)); - + ESP_LOGD(TAG, "hashed ENS mac = %X, ENS count = %d (total=%d)", hashedmac, cwaSeenNotifiers.count(hashedmac), cwaSeenNotifiers.size()); added = !(cwaSeenNotifiers.count(hashedmac) > 0); // Count only if this ENS MAC was not yet seen if (added) { + cwaSeenNotifiers[hashedmac] = millis(); // last seen at .... ESP_LOGD(TAG, "added device with active ENS"); } - cwaSeenNotifiers[hashedmac] = millis(); // last seen at .... - - // True if MAC WiFi/BLE was new + // True if ENS MAC was new return added; } diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 6b1df845..4ba08575 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -10,9 +10,7 @@ // Local logging tag static const char TAG[] = __FILE__; -// used here and in corona.cpp uint16_t salt = 0; -uint16_t hashedmac = 0; // temporary buffer for generated hash value uint16_t get_salt(void) { salt = (uint16_t)random(65536); // get new 16bit random for salting hashes @@ -45,12 +43,13 @@ uint64_t macConvert(uint8_t *paddr) { return (__builtin_bswap64(*mac) >> 16); } -bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { +uint16_t mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { - if (!salt) // ensure we have salt (appears after radio is turned on) - return false; + if (salt == 0) // ensure we have salt (appears after radio is turned on) + return 0; - char buff[10]; // temporary buffer for printf + uint16_t hashedmac = 0; // temporary buffer for generated hash value + char buff[10]; // temporary buffer for printf bool added = false; int8_t beaconID; // beacon number in test monitor mode uint32_t *mac; // temporary buffer for shortened MAC @@ -140,7 +139,7 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { } #endif - // True if MAC WiFi/BLE was new - return added; // function returns bool if a new and unique Wifi or BLE mac was - // counted (true) or not (false) + // if a new and unique Wifi or BLE mac was counted, returs hash of this mac, + // else 0 + return hashedmac; } diff --git a/src/paxcounter.conf b/src/paxcounter.conf index bc235dfa..9f683134 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -26,7 +26,7 @@ // Corona Exposure Notification Service(ENS) counter #define COUNT_ENS 1 // count found number of devices which advertise Exposure Notification Service - // set to 0 if you do not want to count these devices + // set to 0 if you do not want to enable this function // for additional sensors (added by some user) #define HAS_SENSOR_1 1 // set to 1 if you want to count CWAs