diff --git a/src/blecsan.cpp b/src/blecsan.cpp index fdc96630..9ca85b2a 100644 --- a/src/blecsan.cpp +++ b/src/blecsan.cpp @@ -224,11 +224,17 @@ esp_err_t register_ble_functionality(void) return ESP_OK ; } +void stop_BLEscan(void){ + ESP_LOGI(TAG, "Shutting BT Down ..."); + esp_ble_gap_register_callback(NULL); + esp_bluedroid_disable(); + esp_bluedroid_deinit(); + esp_bt_controller_disable(); + esp_bt_controller_deinit(); +} -// Main start code running in its own Xtask -void bt_loop(void * pvParameters) -{ - configASSERT( ( ( uint32_t ) pvParameters ) == 1 ); // FreeRTOS check +void start_BLEscan(void){ + ESP_LOGI(TAG, "Initializing bluetooth scanner ..."); esp_err_t status; @@ -277,15 +283,9 @@ void bt_loop(void * pvParameters) goto end; } - while(1) - { - vTaskDelay(10/portTICK_PERIOD_MS); // reset watchdog - } - end: - ESP_LOGI(TAG, "Terminating BT logging task"); - vTaskDelete(NULL); - -} // bt_loop +ESP_LOGI(TAG, "Bluetooth scanner initialization finished"); + +} // start_BLEscan #endif // BLECOUNTER \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 7497274f..f099049b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -583,17 +583,17 @@ wifi_sniffer_init(); // setup wifi in monitor mode and start MAC counting // note: do this *after* wifi has started, since gets it's seed from RF noise reset_salt(); // get new 16bit for salting hashes -// run wifi task on core 0 and lora task on core 1 and bt task on core 0 +// run wifi channel switching task on core 0 and lora lmic task on core 1 (arduino main loop runs 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", 2048, ( void * ) 1, 1, NULL, 0); +// start BLE scan callback if BLE function is enabled in NVRAM configuration #ifdef BLECOUNTER - if (cfg.blescan) { // start BLE task only if BLE function is enabled in NVRAM configuration - ESP_LOGI(TAG, "Starting Bluetooth task on core 0"); - xTaskCreatePinnedToCore(bt_loop, "btscan", 4096, ( void * ) 1, 1, NULL, 0); + if (cfg.blescan) { + start_BLEscan(); } #endif diff --git a/src/main.h b/src/main.h index f8047656..197924ec 100644 --- a/src/main.h +++ b/src/main.h @@ -41,4 +41,7 @@ void wifi_sniffer_set_channel(uint8_t channel); void wifi_sniffer_packet_handler(void *buff, wifi_promiscuous_pkt_type_t type); // defined in blescan.cpp -void bt_loop(void *ignore); +#ifdef BLECOUNTER + void start_BLEscan(void); + void stop_BLEscan(void); +#endif \ No newline at end of file diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 4cfb4a4a..d18f3248 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -7,13 +7,13 @@ // 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, saves power & memory // BLE scan parameters #define BLESTACKSIZE 8192 // stack size for esp_bt_controller #define BLESCANTIME 0 // [seconds] scan duration, 0 means infinite [default], see note below #define BLESCANWINDOW 10 // [milliseconds] scan window, see below, 3 .. 10240, default 10 -#define BLESCANINTERVAL 10 // [milliseconds] how long to wait between scans, 3 .. 10240, default 10 +#define BLESCANINTERVAL 10 // [milliseconds] scan interval, see below, 3 .. 10240, default 10 /* Note: guide for setting bluetooth parameters * @@ -25,6 +25,7 @@ * Scan window sets how much of the interval should be occupied by scanning. Should be >= BLESCANINTERVAL. * Scan interval is how long scanning should be done on each channel. BLE uses 3 channels for advertising. * -> Adjust these values with power consumption in mind if power is limited. +* -> Scan interval can be changed during runtime by remote comammand. */ // WiFi scan parameters diff --git a/src/rcommand.cpp b/src/rcommand.cpp index 9cc533c0..1920abf5 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -9,12 +9,6 @@ #include #include -// Bluetooth specific includes -#ifdef BLECOUNTER - #include // needed for switching interval parameter by rcommand - esp_err_t register_ble_functionality(void); // defined in blescan.cpp -#endif - // Local logging tag static const char *TAG = "rcommand"; @@ -97,25 +91,14 @@ void set_wifichancycle(uint8_t val) { void set_blescantime(uint8_t val) { cfg.blescantime = val; - ESP_LOGI(TAG, "Remote command: set BLE scan time to %d seconds", cfg.blescantime); + ESP_LOGI(TAG, "Remote command: set BLE scan time to %.1f seconds", cfg.blescantime/float(100)); #ifdef BLECOUNTER - - // stop scan - ESP_LOGI(TAG, "Stopping BLE scan"); - if (esp_ble_gap_stop_scanning() != ESP_OK) - ESP_LOGE(TAG, "Stopping BLE scan failed"); - - // modify parameters - ESP_LOGI(TAG, "Re-register BLE functionality"); - if (register_ble_functionality() != ESP_OK) - ESP_LOGE(TAG, "Re-register BLE functionality failed"); - - // restart scan - ESP_LOGI(TAG, "Restarting BLE scan"); - if (esp_ble_gap_start_scanning(BLESCANTIME) != ESP_OK) - ESP_LOGE(TAG, "Restarting BLE scan failed"); - - + // stop & restart BLE scan task to apply new parameter + if (cfg.blescan) + { + stop_BLEscan(); + start_BLEscan(); + } #endif }; @@ -167,14 +150,20 @@ void set_loraadr(uint8_t val) { }; void set_blescan(uint8_t val) { - ESP_LOGI(TAG, "Remote command: set BLE scan mode to %s", val ? "on" : "off"); + ESP_LOGI(TAG, "Remote command: set BLE scanner to %s", val ? "on" : "off"); switch (val) { case 0: cfg.blescan = 0; macs_ble = 0; // clear BLE counter + #ifdef BLECOUNTER + stop_BLEscan(); + #endif break; default: cfg.blescan = 1; + #ifdef BLECOUNTER + start_BLEscan(); + #endif break; } };