From b288106f83e587b77275dff7fba8136a148e66cc Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:33:17 +0200 Subject: [PATCH 01/26] Update sdcard.h correcting the SD-card-pins --- include/sdcard.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/include/sdcard.h b/include/sdcard.h index e6bc62fb..fa358a71 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -5,7 +5,7 @@ #include #include -#ifdef HAS_SDCARD +#if (HAS_SDCARD) #if HAS_SDCARD == 1 #include //#include @@ -15,6 +15,11 @@ #error HAS_SDCARD unknown card reader value, must be either 1 or 2 #endif #endif +// Pins for SD-card +#define SDCARD_CS (13) +#define SDCARD_MOSI (15) +#define SDCARD_MISO (2) +#define SDCARD_SCLK (14) #ifdef HAS_SDS011 #include "sds011read.h" @@ -38,9 +43,13 @@ #define SDCARD_FILE_NAME "/paxcount.%02d" #define SDCARD_FILE_HEADER "date, time, wifi, bluet" - -bool sdcard_init(void); -void sdcardWriteData(uint16_t, uint16_t); -static void createFile(void); - +#define SDCARD_FILE_NAME "paxcount.%02d" +#define SDCARD_FILE_HEADER "date, time, wifi, bluet" +#if (COUNT_CWA) +#define SDCARD_FILE_HEADER_CWA ",cwa" #endif + +bool sdcard_init( void ); +void sdcardWriteData( uint16_t, uint16_t, uint16_t = 0); + +#endif // _SDCARD_H From 5007373f589675247b19ef37f96aac5a418db6e8 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:34:13 +0200 Subject: [PATCH 02/26] Update main.h added: CWA-handling --- include/main.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/main.h b/include/main.h index bc63ff4e..3e346af7 100644 --- a/include/main.h +++ b/include/main.h @@ -20,5 +20,6 @@ #include "sensor.h" #include "lorawan.h" #include "timekeeper.h" +#include "corona.h" -#endif \ No newline at end of file +#endif From e7b7fb1144c0d8a585c4befefe949c1a6d6a633a Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:35:45 +0200 Subject: [PATCH 03/26] Update senddata.h added: CWA-handling --- include/senddata.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/senddata.h b/include/senddata.h index fd31f884..f1a43867 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -8,6 +8,7 @@ #include "lorawan.h" #include "display.h" #include "sdcard.h" +#include "corona.h" extern Ticker sendcycler; From dcc26a6c9ec7b3bd7a9c7e749d4603e49d182d0d Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:36:41 +0200 Subject: [PATCH 04/26] Update macsniff.h --- include/macsniff.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/macsniff.h b/include/macsniff.h index a04b7cc8..574048b7 100644 --- a/include/macsniff.h +++ b/include/macsniff.h @@ -12,10 +12,11 @@ #define MAC_SNIFF_WIFI 0 #define MAC_SNIFF_BLE 1 +#define MAC_SNIFF_BLE_CWA 2 uint16_t get_salt(void); uint64_t macConvert(uint8_t *paddr); bool 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 \ No newline at end of file +#endif From d36579c8d090845448cb5bdf64ea30a062397e4e Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:48:19 +0200 Subject: [PATCH 05/26] Update paxcounter.conf added: CWA-handling --- src/paxcounter.conf | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/paxcounter.conf b/src/paxcounter.conf index c98cc7b9..b220126e 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -24,6 +24,15 @@ #define BLESCANWINDOW 80 // [milliseconds] scan window, see below, 3 .. 10240, default 80ms #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle +#define COUNT_CWA 1 // count found copies of the Corona Warn App (CWA) + // uncomment this line if you do not want to count these apps + +// for additional sensors (added by some user) +#define HAS_SENSOR_1 1 // needed because we use SENSOR1_DATA for the CWA-data +#define HAS_SENSOR_2 0 // not used +#define HAS_SENSOR_3 0 // not used +#define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) // to simplify things + /* Note: guide for setting bluetooth parameters * * |< Scan Window > |< Scan Window > | ... |< Scan Window > | From add5b8ca2d5619f11f30c7676cd1cbdfb395c97c Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:49:17 +0200 Subject: [PATCH 06/26] Update ttgov21new.h corrected: SD-card --- src/hal/ttgov21new.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hal/ttgov21new.h b/src/hal/ttgov21new.h index ca2a9cc6..4b08748d 100644 --- a/src/hal/ttgov21new.h +++ b/src/hal/ttgov21new.h @@ -12,11 +12,11 @@ // This settings are for boards labeled v1.6 on pcb, NOT for v1.5 or older */ -#define HAS_LORA 1 // comment out if device shall not send data via LoRa +#define HAS_LORA 1 // comment out if device shall not send data via LoRa #define CFG_sx1276_radio 1 // HPD13A LoRa SoC // enable only if you want to store a local paxcount table on the device -#define HAS_SDCARD 2 // this board has an SD-card-reader/writer +#define HAS_SDCARD 1 // this board has an SD-card-reader/writer #define HAS_DISPLAY 1 #define HAS_LED (25) // green on board LED From 2a2ee35c59c5ba32e2a18c98411667a97c748184 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:50:28 +0200 Subject: [PATCH 07/26] Create coona.cpp added: CWA-handling --- src/coona.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/coona.cpp diff --git a/src/coona.cpp b/src/coona.cpp new file mode 100644 index 00000000..44020d84 --- /dev/null +++ b/src/coona.cpp @@ -0,0 +1,85 @@ +// routines for counting the number of available Corona Warn Apps (CWA) + +// copied from https://github.com/kmetz/BLEExposureNotificationBeeper +// (c) by Kaspar Metz +// modified for use in the Paxcounter by AQ + +#if (COUNT_CWA) + +// Local logging tag +static const char TAG[] = __FILE__; + +#define BT_BD_ADDR_HEX(addr) \ + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] + +#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 + +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 + for (auto const ¬ifier : cwaSeenNotifiers) { + ESP_LOGI(TAG, "CWA forget <%X>", notifier.first); +// } + } +//#endif + /* clear everything, + * otherwise we will count the same device again + * as in the next cycle it will get a differetn hash-value + */ + cwaSeenNotifiers.clear(); +} + +/* return the numbers of active CWAs found + */ +uint16_t cwa_report(void) { + return cwaSeenNotifiers.size(); +} + +bool cwa_init(void) +{ + ESP_LOGD(TAG, "init of BLE-scanner for CWA"); + 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 <= 0 ) + return false; // YES -> return + + bool added = false; + ESP_LOGD(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", + BT_BD_ADDR_HEX(paddr)); + + ESP_LOGD(TAG, "hasehd mac = %X, count = %d (total=%d)", hashedmac, cwaSeenNotifiers.count(hashedmac), cwaSeenNotifiers.size()); + added = !(cwaSeenNotifiers.count(hashedmac) > 0); + + // Count only if MAC was not yet seen + if (added) { + ESP_LOGD(TAG, "added device with active CWA"); + } + + cwaSeenNotifiers[hashedmac] = millis(); // last seen at .... + + // True if MAC WiFi/BLE was new + return added; +} + +#endif From ad288b70586992ac414c51baa564f75914887b7b Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:52:07 +0200 Subject: [PATCH 08/26] Update sdcard.cpp added: CWA-handling --- src/sdcard.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 94e14eaa..29f65f3a 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -9,6 +9,8 @@ static const char TAG[] = __FILE__; static bool useSDCard; +static void createFile(void); + File fileSDCard; bool sdcard_init() { @@ -19,9 +21,7 @@ bool sdcard_init() { // https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/sdmmc_host.html #if HAS_SDCARD == 1 // use SD SPI host driver - useSDCard = SD.begin(SDCARD_CS, SDCARD_MOSI, SDCARD_MISO, SDCARD_SCLK); - //SPI.begin(SDCARD_SCLK, SDCARD_MSO, SDCARD_MOSI, SDCARD_CS); //delay(10); //useSDCard = SD.begin(SDCARD_CS, SPI, 40000000, "/sd"); @@ -38,7 +38,7 @@ bool sdcard_init() { return useSDCard; } -void sdcardWriteData(uint16_t noWifi, uint16_t noBle) { +void sdcardWriteData(uint16_t noWifi, uint16_t noBle, __attribute__((unused)) uint16_t noBleCWA) { static int counterWrites = 0; char tempBuffer[12 + 1]; time_t t = now(); @@ -56,6 +56,10 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle) { fileSDCard.print(tempBuffer); sprintf(tempBuffer, "%d,%d", noWifi, noBle); fileSDCard.print(tempBuffer); +#if (COUNT_CWA) + sprintf(tempBuffer, ",%d", noBleCWA); + fileSDCard.print(tempBuffer); +#endif #if (HAS_SDS011) sds011_store(&sds); sprintf(tempBuffer, ",%5.1f,%4.1f", sds.pm10, sds.pm25); @@ -98,6 +102,9 @@ void createFile(void) { if (fileSDCard) { ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); fileSDCard.print(SDCARD_FILE_HEADER); +#if (COUNT_CWA) + fileSDCard.print(SDCARD_FILE_HEADER_CWA); // for Corona-data (CWA) +#endif #if (HAS_SDS011) fileSDCard.print(SDCARD_FILE_HEADER_SDS011); #endif From e9800db481a46158a211cf030517905d1a8f220a Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:53:49 +0200 Subject: [PATCH 09/26] Update macsniff.cpp added: CWA-handling --- src/macsniff.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/macsniff.cpp b/src/macsniff.cpp index a64e49b1..3b6ee5d5 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -10,7 +10,9 @@ // Local logging tag static const char TAG[] = __FILE__; -uint16_t salt; +// used here and in corona.cpp +uint16_t salt = -1; +uint16_t hashedmac = -1; // temporary buffer for generated hash value uint16_t get_salt(void) { salt = (uint16_t)random(65536); // get new 16bit random for salting hashes @@ -51,7 +53,6 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { char buff[10]; // temporary buffer for printf bool added = false; int8_t beaconID; // beacon number in test monitor mode - uint16_t hashedmac; // temporary buffer for generated hash value uint32_t *mac; // temporary buffer for shortened MAC // only last 3 MAC Address bytes are used for MAC address anonymization @@ -111,18 +112,25 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { payload.addAlarm(rssi, beaconID); SendPayload(BEACONPORT, prio_high); } - }; + }; } // added // Log scan result ESP_LOGV(TAG, "%s %s RSSI %ddBi -> salted MAC %s -> Hash %04X -> WiFi:%d " - "BLTH:%d -> " - "%d Bytes left", + "BLTH:%d " +#if (COUNT_CWA) + "(CWA:%d)" +#endif + "-> %d Bytes left", added ? "new " : "known", sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", rssi, buff, - hashedmac, macs_wifi, macs_ble, getFreeRAM()); + hashedmac, macs_wifi, macs_ble, +#if (COUNT_CWA) + cwa_report(), +#endif + getFreeRAM()); #if (VENDORFILTER) } else { From c15d8cb893da028ce1ed496f203c229fe44173d6 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:56:21 +0200 Subject: [PATCH 10/26] Update main.cpp added: CWA-handling --- src/main.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index ed0bd9ee..ff0fa2cd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -312,9 +312,25 @@ void setup() { // initialize sensors #if (HAS_SENSORS) - strcat_P(features, " SENS"); +#if (HAS_SENSOR_1) +#if (COUNT_CWA) + ESP_LOGI(TAG, "init CWA-counter"); + if ( cwa_init() ) + strcat_P(features, " CWA"); +#else + strcat_P(features, " SENS(1)"); sensor_init(); #endif +#endif +#if (HAS_SENSOR_2) + strcat_P(features, " SENS(2)"); + sensor_init(); +#endif +#if (HAS_SENSOR_3) + strcat_P(features, " SENS(3)"); + sensor_init(); +#endif +#endif // initialize LoRa #if (HAS_LORA) @@ -336,7 +352,7 @@ void setup() { assert(mqtt_init() == ESP_OK); #endif -#ifdef HAS_SDCARD +#if (HAS_SDCARD) if (sdcard_init()) strcat_P(features, " SD"); #endif From b8fc516a01c6297db6c443e63fece98a12965d99 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:57:45 +0200 Subject: [PATCH 11/26] Update senddata.cpp added: CWA-handling --- src/senddata.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/senddata.cpp b/src/senddata.cpp index d6067131..34e9000b 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -55,8 +55,14 @@ void SendPayload(uint8_t port, sendprio_t prio) { #endif // write data to sdcard, if present -#ifdef HAS_SDCARD - sdcardWriteData(macs_wifi, macs_ble); +#if (HAS_SDCARD) + if ( port == COUNTERPORT ) { + sdcardWriteData(macs_wifi, macs_ble +#if (COUNT_CWA) + , cwa_report() +#endif + ); + } #endif } // SendPayload @@ -143,22 +149,31 @@ void sendData() { #endif #if (HAS_SENSORS) +#if (HAS_SENSOR_1) case SENSOR1_DATA: payload.reset(); payload.addSensor(sensor_read(1)); SendPayload(SENSOR1PORT, prio_normal); +#if (COUNT_CWA) + cwa_clear(); +#endif break; +#endif +#if (HAS_SENSOR_2) case SENSOR2_DATA: payload.reset(); payload.addSensor(sensor_read(2)); SendPayload(SENSOR2PORT, prio_normal); break; +#endif +#if (HAS_SENSOR_3) case SENSOR3_DATA: payload.reset(); payload.addSensor(sensor_read(3)); SendPayload(SENSOR3PORT, prio_normal); break; #endif +#endif #if (defined BAT_MEASURE_ADC || defined HAS_PMU) case BATT_DATA: From 1f388d64302d2eb8925e55c5237f73ea1bffc2b5 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 11:59:00 +0200 Subject: [PATCH 12/26] Update blecsan.cpp added: CWA-handling --- src/blecsan.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/blecsan.cpp b/src/blecsan.cpp index d305680e..0806776c 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -6,6 +6,9 @@ #define BT_BD_ADDR_HEX(addr) \ addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] +// checking for CWAs we need this magic bytes: +static const char cwaMagicBytes[] = "\x03\x03\x6F\xfd"; + // local Tag for logging static const char TAG[] = "bluetooth"; @@ -158,10 +161,19 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, // 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); +#if (COUNT_CWA) + // we can call the cwa-functions now + // because we use the hashed max-value + // done in mac_add() + + // check for CWA-signature + if ( 0 == strncmp( (const char*)p->scan_rst.ble_adv, cwaMagicBytes , 4) ) { + cwa_mac_add( p->scan_rst.bda); + } +#endif + + /* to be improved in vendorfilter if: - - - // you can search for elements in the payload using the // function esp_ble_resolve_adv_data() // From 98302a95302a2ca4bf80d5536c65b950e967029b Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 12:01:00 +0200 Subject: [PATCH 13/26] Update display.cpp added: CWA-handling --- src/display.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 162fa6b3..579956b9 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -265,7 +265,12 @@ void dp_drawPage(time_t t, bool nextpage) { else dp_printf("WIFI:off"); if (cfg.blescan) - dp_printf(" BLTH:%-5d", macs_ble); +#if !(COUNT_CWA) + dp_printf("BLTH:%-5d", macs_ble); +#else + dp_printf("BLTH:%-5d", macs_ble); + dp_printf("(CWA:%d)", cwa_report()); +#endif else dp_printf(" BLTH:off"); #elif ((WIFICOUNTER) && (!BLECOUNTER)) @@ -274,9 +279,12 @@ void dp_drawPage(time_t t, bool nextpage) { else dp_printf("WIFI:off"); #elif ((!WIFICOUNTER) && (BLECOUNTER)) - if (cfg.blescan) + if (cfg.blescan) { dp_printf("BLTH:%-5d", macs_ble); - else +#if (COUNT_CWA) + dp_printf("(CWA:%d)", cwa_report()); +#endif + } else dp_printf("BLTH:off"); #else dp_printf("Sniffer disabled"); @@ -736,4 +744,4 @@ void dp_plotCurve(uint16_t count, bool reset) { dp_drawPixel(plotbuf, col, row, 1); } -#endif // HAS_DISPLAY \ No newline at end of file +#endif // HAS_DISPLAY From 9c1d03d8868293b1715aeb0b1b72473d62608a54 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 12:02:21 +0200 Subject: [PATCH 14/26] Update sensor.cpp added: CWA-handling --- src/sensor.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/sensor.cpp b/src/sensor.cpp index abed9b75..230718ec 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -2,6 +2,14 @@ #include "globals.h" #include "sensor.h" +#if (COUNT_CWA) +#include "payload.h" +#include "corona.h" +#include "macsniff.h" + +extern PayloadConvert payload; +#endif + // Local logging tag static const char TAG[] = __FILE__; @@ -47,10 +55,14 @@ uint8_t *sensor_read(uint8_t sensor) { case 1: // insert user specific sensor data frames here */ +#if (COUNT_CWA) + payload.addCount( cwa_report(), MAC_SNIFF_BLE_CWA); +#else buf[0] = length; buf[1] = 0x01; buf[2] = 0x02; buf[3] = 0x03; +#endif break; case 2: @@ -71,4 +83,4 @@ uint8_t *sensor_read(uint8_t sensor) { } return buf; -} \ No newline at end of file +} From f1bf16bd74b37be55a04c25c1e2a930a6ea3dd69 Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:26:30 +0200 Subject: [PATCH 15/26] Create corona.h added: CWA-handling --- include/corona.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 include/corona.h diff --git a/include/corona.h b/include/corona.h new file mode 100644 index 00000000..fcbf556d --- /dev/null +++ b/include/corona.h @@ -0,0 +1,16 @@ +#ifndef _CORONA_h +#define _CORONA_H + +// inspired by https://github.com/kmetz/BLEExposureNotificationBeeper +// (c) by Kaspar Metz +// modified for use in the Paxcounter by AQ + +#include "globals.h" +#include + +bool cwa_init(void); +bool cwa_mac_add(uint8_t *); +void cwa_clear(void); +uint16_t cwa_report(void); + +#endif From ec54f3e2e3bbfee7aead30fe9b6e3860d534a63e Mon Sep 17 00:00:00 2001 From: August Quint <49277349+AugustQu@users.noreply.github.com> Date: Tue, 1 Sep 2020 16:30:27 +0200 Subject: [PATCH 16/26] Update paxcounter.conf set to standard-values --- src/paxcounter.conf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/paxcounter.conf b/src/paxcounter.conf index b220126e..60bfc361 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -24,11 +24,11 @@ #define BLESCANWINDOW 80 // [milliseconds] scan window, see below, 3 .. 10240, default 80ms #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle -#define COUNT_CWA 1 // count found copies of the Corona Warn App (CWA) - // uncomment this line if you do not want to count these apps +#define COUNT_CWA 0 // count found copies of the Corona Warn App (CWA) + // set to 0 if you do not want to count these apps // for additional sensors (added by some user) -#define HAS_SENSOR_1 1 // needed because we use SENSOR1_DATA for the CWA-data +#define HAS_SENSOR_1 0 // set to 1 if you want to count CWAs #define HAS_SENSOR_2 0 // not used #define HAS_SENSOR_3 0 // not used #define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) // to simplify things From 2c15b29255baee7956d8722da53909a8d2277c72 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 2 Sep 2020 11:20:43 +0200 Subject: [PATCH 17/26] bugfix timesync LORASERVER (issue #625) --- src/timesync.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/timesync.cpp b/src/timesync.cpp index d0bc4c5d..088ff59f 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -191,7 +191,7 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) { // flag: length of buffer // Store the instant the time request of the node was received on the gateway - timesync_store(osticks2ms(os_getTime(), timesync_rx); + timesync_store(osticks2ms(os_getTime()), timesync_rx); // parse pUserData: // p type meaning From c014824667e86da6796b549a4302bd25267aad0b Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 2 Sep 2020 11:21:56 +0200 Subject: [PATCH 18/26] CWA detection (experimental) --- include/sdcard.h | 7 +--- platformio.ini | 14 ++++---- src/blecsan.cpp | 21 ++++++----- src/coona.cpp | 85 --------------------------------------------- src/corona.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++ src/display.cpp | 16 ++++----- src/macsniff.cpp | 22 ++++++------ src/main.cpp | 2 +- src/paxcounter.conf | 13 +++---- src/sdcard.cpp | 4 +-- src/senddata.cpp | 4 +-- src/sensor.cpp | 4 +-- 12 files changed, 131 insertions(+), 142 deletions(-) delete mode 100644 src/coona.cpp create mode 100644 src/corona.cpp diff --git a/include/sdcard.h b/include/sdcard.h index fa358a71..d95e8336 100644 --- a/include/sdcard.h +++ b/include/sdcard.h @@ -15,11 +15,6 @@ #error HAS_SDCARD unknown card reader value, must be either 1 or 2 #endif #endif -// Pins for SD-card -#define SDCARD_CS (13) -#define SDCARD_MOSI (15) -#define SDCARD_MISO (2) -#define SDCARD_SCLK (14) #ifdef HAS_SDS011 #include "sds011read.h" @@ -45,7 +40,7 @@ #define SDCARD_FILE_HEADER "date, time, wifi, bluet" #define SDCARD_FILE_NAME "paxcount.%02d" #define SDCARD_FILE_HEADER "date, time, wifi, bluet" -#if (COUNT_CWA) +#if (COUNT_ENS) #define SDCARD_FILE_HEADER_CWA ",cwa" #endif diff --git a/platformio.ini b/platformio.ini index 246d5734..96c0a9ab 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,16 +37,16 @@ halfile = generic.h [platformio] ; upload firmware to board with usb cable -;default_envs = usb +default_envs = usb ; upload firmware to a jfrog bintray repository ;default_envs = ota ; use latest versions of libraries -default_envs = dev +;default_envs = dev description = Paxcounter is a device for metering passenger flows in realtime. It counts how many mobile devices are around. [common] ; for release_version use max. 10 chars total, use any decimal format like "a.b.c" -release_version = 1.9.996 +release_version = 2.0.1 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose debug_level = 4 @@ -61,19 +61,19 @@ display_library = ; set by build.py and taken from hal file lib_deps_lora = MCCI LoRaWAN LMIC library@3.2.0 ; MCCI LMIC by Terrill Moore lib_deps_display = - OneBitDisplay@1.5.0 + OneBitDisplay@1.7.1 QRCode@0.0.1 BitBang_I2C@2.1.1 - TFT_eSPI@>=2.2.8 + TFT_eSPI@>=2.2.18 lib_deps_ledmatrix = Ultrathin_LED_Matrix@>=1.0.0 lib_deps_rgbled = - SmartLeds@>=1.2.0 + SmartLeds@>=1.2.1 lib_deps_gps = 1655@>=1.0.2 ; #1655 TinyGPSPlus by Mikal Hart lib_deps_sensors = Adafruit Unified Sensor@>=1.1.4 - Adafruit BME280 Library@>=2.0.2 + Adafruit BME280 Library@>=2.1.0 Adafruit BMP085 Library@>=1.1.0 BSEC Software Library@1.5.1474 https://github.com/ricki-z/SDS011.git diff --git a/src/blecsan.cpp b/src/blecsan.cpp index 0806776c..10dcc900 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -6,8 +6,10 @@ #define BT_BD_ADDR_HEX(addr) \ addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] -// checking for CWAs we need this magic bytes: -static const char cwaMagicBytes[] = "\x03\x03\x6F\xfd"; +// UUID of Exposure Notification Service (ENS) +// see +// https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf +static const char ensMagicBytes[] = "\x03\x03\x6F\xfd"; // local Tag for logging static const char TAG[] = "bluetooth"; @@ -161,18 +163,15 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, // 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); -#if (COUNT_CWA) - // we can call the cwa-functions now - // because we use the hashed max-value - // done in mac_add() +#if (COUNT_ENS) + // we can call the ens functions now using hashed max-value in mac_add() - // check for CWA-signature - if ( 0 == strncmp( (const char*)p->scan_rst.ble_adv, cwaMagicBytes , 4) ) { - cwa_mac_add( p->scan_rst.bda); - } + // check for ens signature + if (0 == strncmp((const char *)p->scan_rst.ble_adv, ensMagicBytes, 4)) { + cwa_mac_add(p->scan_rst.bda); + } #endif - /* to be improved in vendorfilter if: // you can search for elements in the payload using the // function esp_ble_resolve_adv_data() diff --git a/src/coona.cpp b/src/coona.cpp deleted file mode 100644 index 44020d84..00000000 --- a/src/coona.cpp +++ /dev/null @@ -1,85 +0,0 @@ -// routines for counting the number of available Corona Warn Apps (CWA) - -// copied from https://github.com/kmetz/BLEExposureNotificationBeeper -// (c) by Kaspar Metz -// modified for use in the Paxcounter by AQ - -#if (COUNT_CWA) - -// Local logging tag -static const char TAG[] = __FILE__; - -#define BT_BD_ADDR_HEX(addr) \ - addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] - -#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 - -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 - for (auto const ¬ifier : cwaSeenNotifiers) { - ESP_LOGI(TAG, "CWA forget <%X>", notifier.first); -// } - } -//#endif - /* clear everything, - * otherwise we will count the same device again - * as in the next cycle it will get a differetn hash-value - */ - cwaSeenNotifiers.clear(); -} - -/* return the numbers of active CWAs found - */ -uint16_t cwa_report(void) { - return cwaSeenNotifiers.size(); -} - -bool cwa_init(void) -{ - ESP_LOGD(TAG, "init of BLE-scanner for CWA"); - 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 <= 0 ) - return false; // YES -> return - - bool added = false; - ESP_LOGD(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x", - BT_BD_ADDR_HEX(paddr)); - - ESP_LOGD(TAG, "hasehd mac = %X, count = %d (total=%d)", hashedmac, cwaSeenNotifiers.count(hashedmac), cwaSeenNotifiers.size()); - added = !(cwaSeenNotifiers.count(hashedmac) > 0); - - // Count only if MAC was not yet seen - if (added) { - ESP_LOGD(TAG, "added device with active CWA"); - } - - cwaSeenNotifiers[hashedmac] = millis(); // last seen at .... - - // True if MAC WiFi/BLE was new - return added; -} - -#endif diff --git a/src/corona.cpp b/src/corona.cpp new file mode 100644 index 00000000..0ef136a2 --- /dev/null +++ b/src/corona.cpp @@ -0,0 +1,81 @@ +// routines for counting the number of devices which advertise Exposure +// Notification Service e.g. "Corona Warn App" in Germany + +// copied from https://github.com/kmetz/BLEExposureNotificationBeeper +// (c) by Kaspar Metz +// modified for use in the Paxcounter by AQ + +#if (COUNT_ENS) + +// Local logging tag +static const char TAG[] = __FILE__; + +#define BT_BD_ADDR_HEX(addr) \ + addr[0], addr[1], addr[2], addr[3], addr[4], addr[5] + +#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 + +// array of timestamps for seen notifiers +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 + 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 + // next cycle it likely will advertise with a different hash-value + cwaSeenNotifiers.clear(); +} + +// return the total number of devices seen advertising ENS +uint16_t cwa_report(void) { return cwaSeenNotifiers.size(); } + +bool cwa_init(void) { + ESP_LOGD(TAG, "init BLE-scanner for ENS"); + 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 + + 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) { + ESP_LOGD(TAG, "added device with active ENS"); + } + + cwaSeenNotifiers[hashedmac] = millis(); // last seen at .... + + // True if MAC WiFi/BLE was new + return added; +} + +#endif diff --git a/src/display.cpp b/src/display.cpp index 579956b9..84b21952 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -265,11 +265,10 @@ void dp_drawPage(time_t t, bool nextpage) { else dp_printf("WIFI:off"); if (cfg.blescan) -#if !(COUNT_CWA) +#if (!COUNT_ENS) dp_printf("BLTH:%-5d", macs_ble); #else - dp_printf("BLTH:%-5d", macs_ble); - dp_printf("(CWA:%d)", cwa_report()); + dp_printf(" CWA:%-5d", cwa_report()); #endif else dp_printf(" BLTH:off"); @@ -281,10 +280,10 @@ void dp_drawPage(time_t t, bool nextpage) { #elif ((!WIFICOUNTER) && (BLECOUNTER)) if (cfg.blescan) { dp_printf("BLTH:%-5d", macs_ble); -#if (COUNT_CWA) +#if (COUNT_ENS) dp_printf("(CWA:%d)", cwa_report()); -#endif - } else +#endif + } else dp_printf("BLTH:off"); #else dp_printf("Sniffer disabled"); @@ -357,7 +356,7 @@ void dp_drawPage(time_t t, bool nextpage) { #if (HAS_LORA) - // 3|NtwkID:000000 TXpw:aa + // 3|NetID:000000 PWR:aa // 4|DevAdd:00000000 DR:0 // 5|CHMsk:0000 Nonce:0000 // 6|CUp:000000 CDn:000000 @@ -365,8 +364,7 @@ void dp_drawPage(time_t t, bool nextpage) { dp_setFont(MY_FONT_SMALL); dp_setTextCursor(0, 3); - dp_printf("NetwID:%06X TXpw:%-2d", LMIC.netid & 0x001FFFFF, - LMIC.radio_txpow); + dp_printf("NetID:%06X PWR:%-2d", LMIC.netid & 0x001FFFFF, LMIC.radio_txpow); dp_println(); dp_printf("DevAdd:%08X DR:%1d", LMIC.devaddr, LMIC.datarate); dp_println(); diff --git a/src/macsniff.cpp b/src/macsniff.cpp index 3b6ee5d5..6b1df845 100644 --- a/src/macsniff.cpp +++ b/src/macsniff.cpp @@ -11,8 +11,8 @@ static const char TAG[] = __FILE__; // used here and in corona.cpp -uint16_t salt = -1; -uint16_t hashedmac = -1; // temporary buffer for generated hash value +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 @@ -52,8 +52,8 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { 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 + int8_t beaconID; // beacon number in test monitor mode + uint32_t *mac; // temporary buffer for shortened MAC // only last 3 MAC Address bytes are used for MAC address anonymization // but since it's uint32 we take 4 bytes to avoid 1st value to be 0. @@ -112,22 +112,22 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) { payload.addAlarm(rssi, beaconID); SendPayload(BEACONPORT, prio_high); } - }; + }; } // added // Log scan result - ESP_LOGV(TAG, + ESP_LOGD(TAG, "%s %s RSSI %ddBi -> salted MAC %s -> Hash %04X -> WiFi:%d " "BLTH:%d " -#if (COUNT_CWA) - "(CWA:%d)" -#endif +#if (COUNT_ENS) + "(CWA:%d)" +#endif "-> %d Bytes left", added ? "new " : "known", sniff_type == MAC_SNIFF_WIFI ? "WiFi" : "BLTH", rssi, buff, - hashedmac, macs_wifi, macs_ble, -#if (COUNT_CWA) + hashedmac, macs_wifi, macs_ble, +#if (COUNT_ENS) cwa_report(), #endif getFreeRAM()); diff --git a/src/main.cpp b/src/main.cpp index ff0fa2cd..0018c53f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -313,7 +313,7 @@ void setup() { // initialize sensors #if (HAS_SENSORS) #if (HAS_SENSOR_1) -#if (COUNT_CWA) +#if (COUNT_ENS) ESP_LOGI(TAG, "init CWA-counter"); if ( cwa_init() ) strcat_P(features, " CWA"); diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 60bfc361..bc235dfa 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -16,7 +16,7 @@ // Set this to include BLE counting and vendor filter functions, or to switch off WIFI counting #define VENDORFILTER 1 // set to 0 if you want to count things, not people -#define BLECOUNTER 0 // set it to 1 if you want to use BLE count, at expense of power & memory +#define BLECOUNTER 1 // set it to 1 if you want to use BLE count, at expense of power & memory #define WIFICOUNTER 1 // set it to 0 if you want to switch off WIFI count // BLE scan parameters @@ -24,11 +24,12 @@ #define BLESCANWINDOW 80 // [milliseconds] scan window, see below, 3 .. 10240, default 80ms #define BLESCANINTERVAL 80 // [illiseconds] scan interval, see below, 3 .. 10240, default 80ms = 100% duty cycle -#define COUNT_CWA 0 // count found copies of the Corona Warn App (CWA) - // set to 0 if you do not want to count these apps +// 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 // for additional sensors (added by some user) -#define HAS_SENSOR_1 0 // set to 1 if you want to count CWAs +#define HAS_SENSOR_1 1 // set to 1 if you want to count CWAs #define HAS_SENSOR_2 0 // not used #define HAS_SENSOR_3 0 // not used #define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) // to simplify things @@ -82,8 +83,8 @@ #define RESPONSE_TIMEOUT_MS 60000 // firmware binary server connection timeout [milliseconds] // settings for syncing time of node with a time source (network / gps / rtc / timeserver) -#define TIME_SYNC_LORAWAN 1 // set to 1 to use LORA network as time source, 0 means off [default = 1] -#define TIME_SYNC_LORASERVER 0 // set to 1 to use LORA timeserver as time source, 0 means off [default = 0] +#define TIME_SYNC_LORAWAN 0 // set to 1 to use LORA network as time source, 0 means off [default = 1] +#define TIME_SYNC_LORASERVER 1 // set to 1 to use LORA timeserver as time source, 0 means off [default = 0] #define TIME_SYNC_INTERVAL 60 // sync time attempt each .. minutes from time source [default = 60], 0 means off #define TIME_SYNC_INTERVAL_RETRY 10 // retry time sync after lost sync each .. minutes [default = 10], 0 means off #define TIME_SYNC_SAMPLES 1 // number of time requests for averaging, max. 255 diff --git a/src/sdcard.cpp b/src/sdcard.cpp index 29f65f3a..57fc5b81 100644 --- a/src/sdcard.cpp +++ b/src/sdcard.cpp @@ -56,7 +56,7 @@ void sdcardWriteData(uint16_t noWifi, uint16_t noBle, __attribute__((unused)) ui fileSDCard.print(tempBuffer); sprintf(tempBuffer, "%d,%d", noWifi, noBle); fileSDCard.print(tempBuffer); -#if (COUNT_CWA) +#if (COUNT_ENS) sprintf(tempBuffer, ",%d", noBleCWA); fileSDCard.print(tempBuffer); #endif @@ -102,7 +102,7 @@ void createFile(void) { if (fileSDCard) { ESP_LOGD(TAG, "SD: name opened: <%s>", bufferFilename); fileSDCard.print(SDCARD_FILE_HEADER); -#if (COUNT_CWA) +#if (COUNT_ENS) fileSDCard.print(SDCARD_FILE_HEADER_CWA); // for Corona-data (CWA) #endif #if (HAS_SDS011) diff --git a/src/senddata.cpp b/src/senddata.cpp index 34e9000b..13b42927 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -58,7 +58,7 @@ void SendPayload(uint8_t port, sendprio_t prio) { #if (HAS_SDCARD) if ( port == COUNTERPORT ) { sdcardWriteData(macs_wifi, macs_ble -#if (COUNT_CWA) +#if (COUNT_ENS) , cwa_report() #endif ); @@ -154,7 +154,7 @@ void sendData() { payload.reset(); payload.addSensor(sensor_read(1)); SendPayload(SENSOR1PORT, prio_normal); -#if (COUNT_CWA) +#if (COUNT_ENS) cwa_clear(); #endif break; diff --git a/src/sensor.cpp b/src/sensor.cpp index 230718ec..baebf9d1 100644 --- a/src/sensor.cpp +++ b/src/sensor.cpp @@ -2,7 +2,7 @@ #include "globals.h" #include "sensor.h" -#if (COUNT_CWA) +#if (COUNT_ENS) #include "payload.h" #include "corona.h" #include "macsniff.h" @@ -55,7 +55,7 @@ uint8_t *sensor_read(uint8_t sensor) { case 1: // insert user specific sensor data frames here */ -#if (COUNT_CWA) +#if (COUNT_ENS) payload.addCount( cwa_report(), MAC_SNIFF_BLE_CWA); #else buf[0] = length; From 2f0b3c8ef25d3fb2a51c1b857be1e0adc493d28d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Wed, 2 Sep 2020 18:55:06 +0200 Subject: [PATCH 19/26] CWA scanner minor modifications --- include/corona.h | 2 +- include/macsniff.h | 2 +- src/blecsan.cpp | 12 +++++------- src/corona.cpp | 29 +++++++++-------------------- src/macsniff.cpp | 17 ++++++++--------- src/paxcounter.conf | 2 +- 6 files changed, 25 insertions(+), 39 deletions(-) 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 From 0f6e08e7e18b554167d0e214418c4d811f0f5d3f Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Thu, 3 Sep 2020 16:37:51 +0200 Subject: [PATCH 20/26] CWA code edits --- include/corona.h | 2 +- src/blecsan.cpp | 9 +++++---- src/corona.cpp | 23 ++++------------------- 3 files changed, 10 insertions(+), 24 deletions(-) diff --git a/include/corona.h b/include/corona.h index a11990d5..9af0d314 100644 --- a/include/corona.h +++ b/include/corona.h @@ -9,7 +9,7 @@ #include bool cwa_init(void); -bool cwa_mac_add(uint16_t hashedmac); +void cwa_mac_add(uint16_t hashedmac); void cwa_clear(void); uint16_t cwa_report(void); diff --git a/src/blecsan.cpp b/src/blecsan.cpp index bfc8adc8..73110fdd 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -160,14 +160,15 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event, } #endif - // 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); + // 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) // check for ens signature - if (0 == strncmp((const char *)p->scan_rst.ble_adv, ensMagicBytes, 4)) { + if (0 == strncmp((const char *)p->scan_rst.ble_adv, ensMagicBytes, 4)) cwa_mac_add(hashedmac); - } #endif /* to be improved in vendorfilter if: diff --git a/src/corona.cpp b/src/corona.cpp index 00619191..632bff1e 100644 --- a/src/corona.cpp +++ b/src/corona.cpp @@ -15,10 +15,10 @@ static const char TAG[] = __FILE__; #include "corona.h" -// When to forget old senders. +// When to forget old senders ** currently not used ** #define FORGET_AFTER_MINUTES 2 -// array of timestamps for seen notifiers +// array of timestamps for seen notifiers: hash -> timestamp[ms] static std::map cwaSeenNotifiers; // Remove notifiers last seen over FORGET_AFTER_MINUTES ago. @@ -48,23 +48,8 @@ bool cwa_init(void) { return true; } -// 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, "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"); - } - - // True if ENS MAC was new - return added; +void cwa_mac_add(uint16_t hashedmac) { + cwaSeenNotifiers[hashedmac] = millis(); // hash last seen at .... } #endif From ae106406a1b6d559b4f6282606931b527c9df995 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Fri, 4 Sep 2020 20:37:47 +0200 Subject: [PATCH 21/26] CWA settings --- platformio.ini | 2 +- src/paxcounter.conf | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/platformio.ini b/platformio.ini index 96c0a9ab..5a08fc61 100644 --- a/platformio.ini +++ b/platformio.ini @@ -49,7 +49,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I release_version = 2.0.1 ; DEBUG LEVEL: For production run set to 0, otherwise device will leak RAM while running! ; 0=None, 1=Error, 2=Warn, 3=Info, 4=Debug, 5=Verbose -debug_level = 4 +debug_level = 3 extra_scripts = pre:build.py otakeyfile = ota.conf lorakeyfile = loraconf.h diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 9f683134..9ced804c 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -15,9 +15,9 @@ #define COUNTERMODE 0 // 0=cyclic, 1=cumulative, 2=cyclic confirmed // Set this to include BLE counting and vendor filter functions, or to switch off WIFI counting -#define VENDORFILTER 1 // set to 0 if you want to count things, not people +#define VENDORFILTER 0 // set to 0 if you want to all devices, not filtering smartphone OUIs #define BLECOUNTER 1 // set it to 1 if you want to use BLE count, at expense of power & memory -#define WIFICOUNTER 1 // set it to 0 if you want to switch off WIFI count +#define WIFICOUNTER 0 // set it to 0 if you want to switch off WIFI count // BLE scan parameters #define BLESCANTIME 0 // [seconds] scan duration, 0 means infinite [default], see note below @@ -29,7 +29,7 @@ // 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 +#define HAS_SENSOR_1 1 // set to 1 if you want to transmit CWA counter #define HAS_SENSOR_2 0 // not used #define HAS_SENSOR_3 0 // not used #define HAS_SENSORS (HAS_SENSOR_1 || HAS_SENSOR_2 || HAS_SENSOR_3) // to simplify things From 370c9bd16196ddbf65fe9f6d5f1b4b6f57770cd6 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 5 Sep 2020 14:24:59 +0200 Subject: [PATCH 22/26] readme.md: ENS feature added --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c5fcec72..9d84d4d7 100644 --- a/README.md +++ b/README.md @@ -200,12 +200,24 @@ Paxcounter can keep it's time-of-day synced with an external time source. Set *# Paxcounter can be used to sync a wall clock which has a DCF77 or IF482 time telegram input. Set *#define HAS_IF482* or *#define HAS_DCF77* in board's hal file to setup clock controller. Use case of this function is to integrate paxcounter and clock. Accurary of the synthetic DCF77 signal depends on accuracy of on board's time base, see above. # mobile PaxCounter via https://opensensemap.org/ + This describes how to set up a mobile PaxCounter: -Follow all steps so far for preparing the device, use the packed payload format. In paxcounter.conf set PAYLOAD_OPENSENSEBOX to 1. Register a new sensbox on https://opensensemap.org/. +Follow all steps so far for preparing the device, use the packed payload format. In [paxcounter.conf](src/paxcounter.conf) set PAYLOAD_OPENSENSEBOX to 1. Register a new sensbox on https://opensensemap.org/. There in the sensor configuration select "TheThingsNetwork" and set Decoding Profil to "LoRa serialization", enter your TTN Application and Device Id. Decoding option has to be [{"decoder":"latLng"},{"decoder":"uint16","sensor_id":"yoursensorid"}] +# Covid-19 Exposure Notification System beacon detection (Germany: "Corona Warn App counter") + +Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this gives an indication how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best resulta withs this funcion, use following settings in [paxcounter.conf](src/paxcounter.conf): + + #define COUNT_ENS 1 // enable ENS monitoring function + #define VENDORFILTER 0 // disable OUI filter (scans ALL device MACs) + #define BLECOUNTER 1 // enable bluetooth sniffing + #define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed) + #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server + # SD-card + Data can be stored on an SD-card if one is availabe. Simply choose the file in src/hal and add the following lines to your hal-file: #define HAS_SDCARD 1 // SD-card-reader/writer, using SPI interface @@ -559,4 +571,4 @@ Thanks to - [terrillmoore](https://github.com/mcci-catena) for maintaining the LMIC for arduino LoRaWAN stack - [sbamueller](https://github.com/sbamueller) for writing the tutorial in Make Magazine - [Stefan](https://github.com/nerdyscout) for paxcounter opensensebox integration -- [August Quint](https://github.com/AugustQu) for adding SD card data logger and SDS011 support +- [August Quint](https://github.com/AugustQu) for adding SD card data logger, SDS011 and ENS support From 26b68963535b0090fe8413a7815aa99979cf4808 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 5 Sep 2020 14:25:14 +0200 Subject: [PATCH 23/26] display.cpp: small mask fixes --- src/display.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/display.cpp b/src/display.cpp index 84b21952..6e181dd9 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -356,17 +356,17 @@ void dp_drawPage(time_t t, bool nextpage) { #if (HAS_LORA) - // 3|NetID:000000 PWR:aa - // 4|DevAdd:00000000 DR:0 + // 3|Net:000000 Pwr:aa + // 4|Dev:00000000 DR:0 // 5|CHMsk:0000 Nonce:0000 - // 6|CUp:000000 CDn:000000 + // 6|fUp:000000 fDn:000000 // 7|SNR:-0000 RSSI:-0000 dp_setFont(MY_FONT_SMALL); dp_setTextCursor(0, 3); - dp_printf("NetID:%06X PWR:%-2d", LMIC.netid & 0x001FFFFF, LMIC.radio_txpow); + dp_printf("Net:%06X Pwr:%-2d", LMIC.netid & 0x001FFFFF, LMIC.radio_txpow); dp_println(); - dp_printf("DevAdd:%08X DR:%1d", LMIC.devaddr, LMIC.datarate); + dp_printf("Dev:%08X DR:%1d", LMIC.devaddr, LMIC.datarate); dp_println(); dp_printf("ChMsk:%04X Nonce:%04X", LMIC.channelMap, LMIC.devNonce); dp_println(); From 55062923d3bafafdc0f160cee474b977c346e73a Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 5 Sep 2020 14:25:55 +0200 Subject: [PATCH 24/26] platformio.ini: versions edited --- platformio.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platformio.ini b/platformio.ini index 5a08fc61..c76d2b7d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -61,7 +61,7 @@ display_library = ; set by build.py and taken from hal file lib_deps_lora = MCCI LoRaWAN LMIC library@3.2.0 ; MCCI LMIC by Terrill Moore lib_deps_display = - OneBitDisplay@1.7.1 + OneBitDisplay@1.5.0 QRCode@0.0.1 BitBang_I2C@2.1.1 TFT_eSPI@>=2.2.18 From c1727a5b514c641076bd9023974c608ccf144325 Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 5 Sep 2020 14:27:57 +0200 Subject: [PATCH 25/26] readme.md typos --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9d84d4d7..d7a52804 100644 --- a/README.md +++ b/README.md @@ -210,11 +210,11 @@ There in the sensor configuration select "TheThingsNetwork" and set Decoding Pro Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this gives an indication how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best resulta withs this funcion, use following settings in [paxcounter.conf](src/paxcounter.conf): - #define COUNT_ENS 1 // enable ENS monitoring function - #define VENDORFILTER 0 // disable OUI filter (scans ALL device MACs) - #define BLECOUNTER 1 // enable bluetooth sniffing - #define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed) - #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server + #define COUNT_ENS 1 // enable ENS monitoring function + #define VENDORFILTER 0 // disable OUI filter (scans ALL device MACs) + #define BLECOUNTER 1 // enable bluetooth sniffing + #define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed) + #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server # SD-card From 39a54556fccebc8dee56e2875baeb3155557339d Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 5 Sep 2020 14:28:49 +0200 Subject: [PATCH 26/26] readme.md typos --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index d7a52804..4ee4f7e4 100644 --- a/README.md +++ b/README.md @@ -211,10 +211,10 @@ There in the sensor configuration select "TheThingsNetwork" and set Decoding Pro Bluetooth low energy service UUID 0xFD6F, used by Google/Apple COVID-19 Exposure Notification System, can be monitored and counted. By comparing with the total number of observed devices this gives an indication how many people staying in proximity are using Apps for tracing COVID-19 exposures, e.g. in Germany the "Corona Warn App". To achive best resulta withs this funcion, use following settings in [paxcounter.conf](src/paxcounter.conf): #define COUNT_ENS 1 // enable ENS monitoring function - #define VENDORFILTER 0 // disable OUI filter (scans ALL device MACs) + #define VENDORFILTER 0 // disable OUI filter (scans ALL device MACs) #define BLECOUNTER 1 // enable bluetooth sniffing #define WIFICOUNTER 0 // disable wifi sniffing (improves BLE scan speed) - #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server + #define HAS_SENSOR_1 1 // optional: transmit ENS counter data to server # SD-card