Skip to content

Commit

Permalink
Merge pull request #673 from cyberman54/development
Browse files Browse the repository at this point in the history
v2.0.4
  • Loading branch information
cyberman54 authored Nov 10, 2020
2 parents 270b5e4 + 7ccfb42 commit 7f20c19
Show file tree
Hide file tree
Showing 15 changed files with 226 additions and 200 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Depending on board hardware following features are supported:
- OLED Display (shows detailed status)
- RGB LED (shows colorized status)
- Button (short press: flip display page / long press: send alarm message)
- Silicon unique ID
- Battery voltage monitoring (analog read / AXP192 / IP5306)
- GPS (Generic serial NMEA, or Quectel L76 I2C)
- Environmental sensors (Bosch BMP180/BME280/BME680 I2C; SDS011 serial)
Expand Down
18 changes: 11 additions & 7 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
#include "mallocator.h"
#include <bsec.h>

// sniffing types
#define MAC_SNIFF_WIFI 0
#define MAC_SNIFF_BLE 1

// bits in payloadmask for filtering payload data
#define GPS_DATA (0x01)
#define ALARM_DATA (0x02)
Expand Down Expand Up @@ -67,9 +63,9 @@

enum sendprio_t { prio_low, prio_normal, prio_high };
enum timesource_t { _gps, _rtc, _lora, _unsynced };

enum snifftype_t { MAC_SNIFF_WIFI, MAC_SNIFF_BLE, MAC_SNIFF_BLE_ENS };
enum runmode_t {
RUNMODE_POWERCYCLE = 0,
RUNMODE_POWERCYCLE,
RUNMODE_NORMAL,
RUNMODE_WAKEUP,
RUNMODE_UPDATE
Expand Down Expand Up @@ -114,6 +110,13 @@ typedef struct {
uint8_t Message[PAYLOAD_BUFFER_SIZE];
} MessageBuffer_t;

// Struct for MAC processing queue
typedef struct {
uint8_t mac[6];
int8_t rssi;
snifftype_t sniff_type;
} MacBuffer_t;

typedef struct {
int32_t latitude;
int32_t longitude;
Expand Down Expand Up @@ -145,13 +148,14 @@ extern std::array<uint64_t, 0xff> beacons;
extern configData_t cfg; // current device configuration
extern char lmic_event_msg[LMIC_EVENTMSG_LEN]; // display buffer
extern uint8_t volatile channel; // wifi channel rotation counter
extern uint8_t volatile rf_load; // RF traffic indicator
extern uint8_t batt_level; // display value
extern uint16_t volatile macs_wifi, macs_ble; // display values
extern bool volatile TimePulseTick; // 1sec pps flag set by GPS or RTC
extern timesource_t timeSource;
extern hw_timer_t *displayIRQ, *matrixDisplayIRQ, *ppsIRQ;
extern SemaphoreHandle_t I2Caccess;
extern TaskHandle_t irqHandlerTask, ClockTask;
extern TaskHandle_t irqHandlerTask, ClockTask, macProcessTask;
extern TimerHandle_t WifiChanTimer;
extern Timezone myTZ;
extern RTC_DATA_ATTR runmode_t RTC_runmode;
Expand Down
9 changes: 4 additions & 5 deletions include/macsniff.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@
#include "corona.h"
#endif

#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);
uint16_t mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type);
esp_err_t macQueueInit(void);
void mac_process(void *pvParameters);
void IRAM_ATTR mac_add(uint8_t *paddr, int8_t rssi, snifftype_t sniff_type);
uint16_t mac_analyze(MacBuffer_t MacBuffer);
void printKey(const char *name, const uint8_t *key, uint8_t len, bool lsb);

#endif
2 changes: 1 addition & 1 deletion platformio_orig.ini
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ description = Paxcounter is a device for metering passenger flows in realtime. I

[common]
; for release_version use max. 10 chars total, use any decimal format like "a.b.c"
release_version = 2.0.3
release_version = 2.0.4
; 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 = 3
Expand Down
56 changes: 28 additions & 28 deletions src/blecsan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@
#define BT_BD_ADDR_HEX(addr) \
addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]

// UUID of Exposure Notification Service (ENS)
// see
// https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
static const char ensMagicBytes[] = "\x16\x6f\xfd";

// local Tag for logging
static const char TAG[] = "bluetooth";

#ifdef VERBOSE
const char *bt_addr_t_to_string(esp_ble_addr_type_t type) {
switch (type) {
case BLE_ADDR_TYPE_PUBLIC:
Expand Down Expand Up @@ -110,15 +106,24 @@ const char *btsig_gap_type(uint32_t gap_type) {
return "Unknown type";
}
} // btsig_gap_type
#endif

// using IRAM_:ATTR here to speed up callback function
// 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;

#if (COUNT_ENS)
// UUID of Exposure Notification Service (ENS)
// https://blog.google/documents/70/Exposure_Notification_-_Bluetooth_Specification_v1.2.2.pdf
static const char ensMagicBytes[] = "\x16\x6f\xfd";
#endif

#ifdef VERBOSE
ESP_LOGV(TAG, "BT payload rcvd -> type: 0x%.2x -> %s", *p->scan_rst.ble_adv,
btsig_gap_type(*p->scan_rst.ble_adv));
#endif

switch (event) {
case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT:
Expand All @@ -138,42 +143,37 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event,
if (p->scan_rst.search_evt ==
ESP_GAP_SEARCH_INQ_RES_EVT) // Inquiry result for a peer device
{ // evaluate sniffed packet
#ifdef VERBOSE
ESP_LOGV(TAG, "Device address (bda): %02x:%02x:%02x:%02x:%02x:%02x",
BT_BD_ADDR_HEX(p->scan_rst.bda));
ESP_LOGV(TAG, "Addr_type : %s",
bt_addr_t_to_string(p->scan_rst.ble_addr_type));
ESP_LOGV(TAG, "RSSI : %d", p->scan_rst.rssi);

if ((cfg.rssilimit) &&
(p->scan_rst.rssi < cfg.rssilimit)) { // rssi is negative value
ESP_LOGI(TAG, "BLTH RSSI %d -> ignoring (limit: %d)", p->scan_rst.rssi,
cfg.rssilimit);
break;
}
#endif

#if (VENDORFILTER)
if ((p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RANDOM) ||
(p->scan_rst.ble_addr_type == BLE_ADDR_TYPE_RPA_RANDOM)) {
#ifdef VERBOSE
ESP_LOGV(TAG, "BT device filtered");
#endif
break;
}
#endif

// hash and add this device and show new count total if it was not
// previously added

#if (COUNT_ENS)
uint16_t hashedmac =
#endif

mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
// add this device mac to processing queue

#if (COUNT_ENS)
// check for ens signature
if (cfg.enscount) {
// check for ens signature
if (NULL != strstr((const char *)p->scan_rst.ble_adv, ensMagicBytes))
cwa_mac_add(hashedmac);
if (strstr((const char *)p->scan_rst.ble_adv, ensMagicBytes) != NULL)
mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi,
MAC_SNIFF_BLE_ENS);
else
mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
}
#else
mac_add((uint8_t *)p->scan_rst.bda, p->scan_rst.rssi, MAC_SNIFF_BLE);
#endif

/* to be improved in vendorfilter if:
Expand All @@ -186,8 +186,8 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event,
// uint8_t *data = esp_ble_resolve_adv_data(p->scan_rst.ble_adv,
ESP_BLE_AD_TYPE_NAME_CMPL, &len);
filter BLE devices using their advertisements to get filter alternative to
vendor OUI if vendorfiltering is on, we ...
filter BLE devices using their advertisements to get filter alternative
to vendor OUI if vendorfiltering is on, we ...
- want to count: mobile phones and tablets
- don't want to count: beacons, peripherals (earphones, headsets,
printers), cars and machines see
Expand All @@ -211,13 +211,13 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event,

default:
break;
}
} // switch
} // gap_callback_handler

esp_err_t register_ble_callback(void) {
ESP_LOGI(TAG, "Register GAP callback");

// This function is called to occur gap event, such as scan result.
// This function is called when gap event occurs, such as scan result.
// register the scan callback function to the gap module
ESP_ERROR_CHECK(esp_ble_gap_register_callback(&gap_callback_handler));

Expand Down
19 changes: 7 additions & 12 deletions src/corona.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,13 @@ static std::map<uint16_t, unsigned long> cwaSeenNotifiers;

// Remove notifiers last seen over FORGET_AFTER_MINUTES ago.
void cwa_clear() {
/*
#ifdef SOME_FORM_OF_DEBUG
ESP_LOGD(TAG, "CWA: forget old notifier: %d", cwaSeenNotifiers.size());
for (auto const &notifier : cwaSeenNotifiers) {
ESP_LOGD(TAG, "CWA forget <%X>", notifier.first);
// }
}
#endif
*/

#ifdef VERBOSE
ESP_LOGV(TAG, "CWA: forget old notifier: %d", cwaSeenNotifiers.size());
for (auto const &notifier : cwaSeenNotifiers) {
ESP_LOGD(TAG, "CWA forget <%04X>", 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();
Expand Down
3 changes: 3 additions & 0 deletions src/cyclic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void doHousekeeping() {
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(irqHandlerTask),
eTaskGetState(irqHandlerTask));
ESP_LOGD(TAG, "MACprocessor %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(macProcessTask),
eTaskGetState(macProcessTask));
#if (HAS_LORA)
ESP_LOGD(TAG, "LMiCtask %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(lmicTask), eTaskGetState(lmicTask));
Expand Down
1 change: 1 addition & 0 deletions src/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ void dp_drawPage(time_t t, bool nextpage) {
dp_printf(" ");
#endif
dp_printf(" ch:%02d", channel);
// dp_printf(" due:%02d", rf_load);
dp_println();

// line 5: RSSI limiter + free memory
Expand Down
4 changes: 0 additions & 4 deletions src/hal/generic.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,4 @@
#define LORA_IO1 (33)
#define LORA_IO2 LMIC_UNUSED_PIN

// I2C config for Microchip 24AA02E64 DEVEUI unique address
#define MCP_24AA02E64_I2C_ADDRESS 0x50 // I2C address for the 24AA02E64
#define MCP_24AA02E64_MAC_ADDRESS 0xF8 // Memory adress of unique deveui 64 bits

#endif
42 changes: 0 additions & 42 deletions src/lorawan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,51 +162,9 @@ void os_getDevEui(u1_t *buf) {
} else {
gen_lora_deveui(buf); // generate DEVEUI from device's MAC
}

// Get MCP 24AA02E64 hardware DEVEUI (override default settings if found)
#ifdef MCP_24AA02E64_I2C_ADDRESS
get_hard_deveui(buf);
RevBytes(buf, 8); // swap bytes to LSB format
#endif
#endif
}

void get_hard_deveui(uint8_t *pdeveui) {
// read DEVEUI from Microchip 24AA02E64 2Kb serial eeprom if present
#ifdef MCP_24AA02E64_I2C_ADDRESS

uint8_t i2c_ret;

// Init this just in case, no more to 100KHz
Wire.begin(SDA, SCL, 100000);
Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS);
Wire.write(MCP_24AA02E64_MAC_ADDRESS);
i2c_ret = Wire.endTransmission();

// check if device was seen on i2c bus
if (i2c_ret == 0) {
char deveui[32] = "";
uint8_t data;

Wire.beginTransmission(MCP_24AA02E64_I2C_ADDRESS);
Wire.write(MCP_24AA02E64_MAC_ADDRESS);
Wire.endTransmission();

Wire.requestFrom(MCP_24AA02E64_I2C_ADDRESS, 8);
while (Wire.available()) {
data = Wire.read();
sprintf(deveui + strlen(deveui), "%02X ", data);
*pdeveui++ = data;
}
ESP_LOGI(TAG, "Serial EEPROM found, read DEVEUI %s", deveui);
} else
ESP_LOGI(TAG, "Could not read DEVEUI from serial EEPROM");

// Set back to 400KHz to speed up OLED
Wire.setClock(400000);
#endif // MCP 24AA02E64
}

#if (VERBOSE)

// Display OTAA keys
Expand Down
Loading

0 comments on commit 7f20c19

Please sign in to comment.