completely reworked BLE scan

This commit is contained in:
Klaus K Wilting 2018-04-14 20:22:58 +02:00
parent 116197f632
commit c57e7b75f4
7 changed files with 90 additions and 146 deletions

View File

@ -228,13 +228,13 @@ under this Licence:
Most source files in this repository are made available under the Eclipse Public License v1.0. The examples which use a more liberal license. Some of the AES code is available under the LGPL. Refer to each individual source file for more details."
------------------------------------------------------------------------------------------------
blecount.cpp
blescan.cpp
Parts of blecount.cpp were derived or taken from
Parts of blescan.cpp were derived or taken from
nkolban esp32 snippets
BLE Scan
https://github.com/nkolban/esp32-snippets/tree/master/cpp_utils/tests/BLETests/Arduino/BLE_scan
BLE GAP DUMP
https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner
under this Licence:

View File

@ -26,15 +26,11 @@ monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2
ESP32 BLE Arduino@0.4.9
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dheltec_wifi_lora_32
@ -53,15 +49,11 @@ upload_speed = 921600
;upload_port = COM15
lib_deps =
U8g2
ESP32 BLE Arduino@0.4.9
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-Dttgov1
-D_lmic_config_h_
@ -79,15 +71,11 @@ monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2
ESP32 BLE Arduino@0.4.9
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dttgov2
@ -104,16 +92,12 @@ monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2@>2.21.7
ESP32 BLE Arduino@0.4.9
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-Dlopy
-D_lmic_config_h_
@ -130,16 +114,12 @@ monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2@>2.21.7
ESP32 BLE Arduino@0.4.9
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dlopy4
@ -156,16 +136,12 @@ monitor_baud = 115200
upload_speed = 256000
lib_deps =
U8g2
ESP32 BLE Arduino@0.4.9
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dlolin32lite_lora
@ -182,16 +158,12 @@ monitor_baud = 115200
upload_speed = 921600
lib_deps =
U8g2
ESP32 BLE Arduino@0.4.9
SmartLeds
build_flags =
;set log level, we need build_flag for this, otherwise we can't use ESP_LOGx in arduino framework
-DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_VERBOSE
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_NONE
;needed for ESP32 BLE Ardunio v0.4.9
-fexceptions
-std=c++11
;override lora settings from LMiC library in lmic/config.h and use main.h instead
-D_lmic_config_h_
-Dlolin32_lora

View File

@ -1,3 +1,5 @@
#ifdef BLECOUNTER
/* code snippets taken from
https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner
*/
@ -10,6 +12,9 @@ https://github.com/nkolban/esp32-snippets/tree/master/BLE/scanner
#include <esp_bt_main.h>
#include <esp_gap_ble_api.h>
// defined in macsniff.cpp
bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type);
#define BT_BD_ADDR_STR "%02x:%02x:%02x:%02x:%02x:%02x"
#define BT_BD_ADDR_HEX(addr) addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]
@ -21,7 +26,7 @@ static const char *bt_dev_type_to_string(esp_bt_dev_type_t type);
static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
// local Tag for logging
static const char *TAG = "paxcnt";
static const char *TAG = "bt_loop";
static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
@ -74,6 +79,32 @@ static void gap_callback_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa
ESP_LOGI(TAG, "num_resps : %d", p->scan_rst.num_resps);
/* to be done here:
#ifdef VENDORFILTER
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
https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/src/BLEAdvertisedDevice.cpp
http://www.libelium.com/products/meshlium/smartphone-detection/
https://www.question-defense.com/2013/01/12/bluetooth-cod-bluetooth-class-of-deviceclass-of-service-explained
https://www.bluetooth.com/specifications/assigned-numbers/baseband
"The Class of Device (CoD) in case of Bluetooth which allows us to differentiate the type of
device (smartphone, handsfree, computer, LAN/network AP). With this parameter we can
differentiate among pedestrians and vehicles."
#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);
if ( p->scan_rst.search_evt == ESP_GAP_SEARCH_INQ_CMPL_EVT)
{
// Scan is done.
@ -311,3 +342,5 @@ end:
vTaskDelete(NULL);
} // bt_loop
#endif // BLECOUNTER

View File

@ -2,13 +2,6 @@
// Basic Config
#include "globals.h"
#ifdef BLECOUNTER
#include <BLEDevice.h>
//#include <BLEUtils.h>
//#include <BLEScan.h>
//#include <BLEAdvertisedDevice.h>
#endif
#ifdef VENDORFILTER
#include <array>
#include <algorithm>
@ -20,7 +13,6 @@ static const char *TAG = "macsniff";
static wifi_country_t wifi_country = {.cc=WIFI_MY_COUNTRY, .schan=WIFI_CHANNEL_MIN, .nchan=WIFI_CHANNEL_MAX, .policy=WIFI_COUNTRY_POLICY_MANUAL};
uint16_t currentScanDevice = 0;
uint16_t salt;
uint16_t salt_reset(void) {
@ -57,16 +49,26 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
// Insert only if it was not found on global count
if (added) {
char buff[16];
snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value
u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE
if (sniff_type == MAC_SNIFF_WIFI ) {
rgb_set_color(COLOR_GREEN);
wifis.insert(hashedmac); // add hashed MAC to wifi container if new unique
wifis.insert(hashedmac); // add hashed MAC to wifi container
u8x8.setCursor(0,4);
u8x8.printf("WIFI: %-4d", (int) wifis.size());
}
#ifdef BLECOUNTER
else if (sniff_type == MAC_SNIFF_BLE ) {
rgb_set_color(COLOR_MAGENTA);
bles.insert(hashedmac); // add hashed MAC to BLE container if new unique
bles.insert(hashedmac); // add hashed MAC to BLE container
u8x8.setCursor(0,3);
u8x8.printf("BLE: %-4d", (int) bles.size());
}
#endif
// Not sure user will have time to see the LED
// TBD do light off further in the code
rgb_set_color(COLOR_NONE);
@ -94,65 +96,6 @@ bool mac_add(uint8_t *paddr, int8_t rssi, bool sniff_type) {
return added; // function returns bool if a new and unique Wifi or BLE mac was counted (true) or not (false)
}
#ifdef BLECOUNTER
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
int lastcount = (int) macs.size();
uint8_t *p = (uint8_t *) advertisedDevice.getAddress().getNative();
/* to be done here:
#ifdef VENDORFILTER
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
https://github.com/nkolban/ESP32_BLE_Arduino/blob/master/src/BLEAdvertisedDevice.cpp
http://www.libelium.com/products/meshlium/smartphone-detection/
https://www.question-defense.com/2013/01/12/bluetooth-cod-bluetooth-class-of-deviceclass-of-service-explained
https://www.bluetooth.com/specifications/assigned-numbers/baseband
"The Class of Device (CoD) in case of Bluetooth which allows us to differentiate the type of
device (smartphone, handsfree, computer, LAN/network AP). With this parameter we can
differentiate among pedestrians and vehicles."
#endif
*/
// Current devices seen on this scan session
currentScanDevice++;
u8x8.setCursor(11,3);
u8x8.printf("%-4d", currentScanDevice);
// add this device and show new count total if it was not previously added
if ( mac_add(p, advertisedDevice.getRSSI(), MAC_SNIFF_BLE) ) {
char buff[16];
snprintf(buff, sizeof(buff), "PAX:%-4d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value
u8x8.draw2x2String(0, 0, buff); // display number on unique macs total Wifi + BLE
}
}
};
void BLECount() {
ESP_LOGI(TAG, "BLE scan started");
currentScanDevice = 0; // Set 0 seen device on this scan session
u8x8.drawString(0,3,"Scanning->");
BLEDevice::init(""); // we don't want to be seen by a name
BLEScanResults foundDevices; // instance for getting count
BLEScan* pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(false); // An active scan would mean that we will wish unneeeded scan responses
pBLEScan->setWindow(BLESCANWINDOW);
pBLEScan->setInterval(BLESCANINTERVAL);
pBLEScan->start(cfg.blescantime); // note: this is a blocking call
ESP_LOGI(TAG, "BLE scan done, seen %d device(s)", foundDevices.getCount());
}
#endif
void wifi_sniffer_init(void) {
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
cfg.nvs_enable = 0; // we don't need any wifi settings from NVRAM

View File

@ -19,10 +19,7 @@ typedef struct {
uint8_t payload[0]; /* network data ended with 4 bytes csum (CRC32) */
} wifi_ieee80211_packet_t;
extern uint16_t currentScanDevice;
uint16_t salt_reset(void);
void BLECount();
void wifi_sniffer_init(void);
void wifi_sniffer_set_channel(uint8_t channel);
void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type);

View File

@ -215,9 +215,7 @@ void lorawan_loop(void * pvParameters) {
void antenna_select(const int8_t _ant);
#endif
#ifdef BLECOUNTER
void BLECount(void);
#else
#ifndef BLECOUNTER
bool btstop = btStop();
#endif
@ -277,18 +275,6 @@ void sniffer_loop(void * pvParameters) {
snprintf(buff, sizeof(buff), "PAX:%d", (int) macs.size()); // convert 16-bit MAC counter to decimal counter value
u8x8.draw2x2String(0, 0, buff); // display number on unique macs total
#ifdef BLECOUNTER
// We just state out of BLE scanning
u8x8.setCursor(0,3);
if (currentScanDevice) {
u8x8.printf("BLE: %-4d %-4d", (int) bles.size(), currentScanDevice);
} else {
u8x8.printf("BLE: %-4d", (int) bles.size());
}
#endif
u8x8.setCursor(0,4);
u8x8.printf("WIFI: %-4d", (int) wifis.size());
u8x8.setCursor(11,4);
u8x8.printf("ch:%02i", channel);
u8x8.setCursor(0,5);
@ -340,12 +326,14 @@ void sniffer_loop(void * pvParameters) {
} // end of send data cycle
else {
/*
#ifdef BLECOUNTER
if (nloop % (WIFI_CHANNEL_MAX * cfg.blescancycle) == 0 ) // once after cfg.blescancycle Wifi scans, do a BLE scan
if (cfg.blescan) { // execute BLE count if BLE function is enabled
BLECount(); // start BLE scan, this is a blocking call
}
#endif
*/
} // end of channel rotation loop
} // end of infinite wifi scan loop
}
@ -493,6 +481,13 @@ void setup() {
// initialize display
init_display(PROGNAME, PROGVERSION);
u8x8.setPowerSave(!cfg.screenon); // set display off if disabled
u8x8.draw2x2String(0, 0, "PAX:0");
u8x8.setCursor(0,4);
u8x8.printf("WIFI: 0");
#ifdef BLECOUNTER
u8x8.setCursor(0,3);
u8x8.printf("BLE: 0");
#endif
u8x8.setCursor(0,5);
u8x8.printf(!cfg.rssilimit ? "RLIM: off" : "RLIM: %d", cfg.rssilimit);
u8x8.drawString(0,6,"Join Wait ");
@ -516,17 +511,21 @@ salt_reset(); // get new 16bit for salting hashes
xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 0);
ESP_LOGI(TAG, "Starting Wifi task on core 0");
xTaskCreatePinnedToCore(wifi_sniffer_loop, "wifisniffer", 4096, ( void * ) 1, 1, NULL, 0);
#ifdef BLECOUNTER
ESP_LOGI(TAG, "Starting Bluetooth task on core 0");
xTaskCreatePinnedToCore(bt_loop, "btscan", 2048, NULL, 5, NULL, 0);
#endif
// to come here: code for switching off core 1
#else // run wifi task on core 0 and lora task on core 1 and bt task on core 1
ESP_LOGI(TAG, "Starting Lora task on core 1");
xTaskCreatePinnedToCore(lorawan_loop, "loratask", 2048, ( void * ) 1, ( 5 | portPRIVILEGE_BIT ), NULL, 1);
ESP_LOGI(TAG, "Starting Wifi task on core 0");
xTaskCreatePinnedToCore(sniffer_loop, "wifisniffer", 4096, ( void * ) 1, 1, NULL, 0);
#ifdef BLECOUNTER
ESP_LOGI(TAG, "Starting Bluetooth task on core 1");
xTaskCreatePinnedToCore(bt_loop, "btscan", 2048, NULL, 5, NULL, 1);
#endif
#endif
// Finally: kickoff first sendjob and join, then send initial payload "0000"
uint8_t mydata[] = "0000";

View File

@ -1,7 +1,7 @@
#pragma once
// program version - note: increment version after modifications to configData_t struct!!
#define PROGVERSION "1.2.95" // use max 10 chars here!
#define PROGVERSION "1.2.96" // use max 10 chars here!
#define PROGNAME "PAXCNT"
// Verbose enables serial output
@ -9,7 +9,7 @@
// set this to include BLE counting and vendor filter functions
#define VENDORFILTER 1 // comment out if you want to count things, not people
//#define BLECOUNTER 1 // comment out if you don't want BLE count
#define BLECOUNTER 1 // comment out if you don't want BLE count
// BLE scan parameters
#define BLESCANCYCLE 2 // BLE scan once after each <BLECYCLE> wifi scans