blescan.cpp: avoid ble stack crash after deinit

This commit is contained in:
cyberman54 2020-12-18 18:39:28 +01:00
parent 49a0c703c1
commit 284d02cb87

View File

@ -214,8 +214,19 @@ IRAM_ATTR void gap_callback_handler(esp_gap_ble_cb_event_t event,
} // switch } // switch
} // gap_callback_handler } // gap_callback_handler
esp_err_t register_ble_callback(void) { esp_err_t register_ble_callback(bool unregister = false) {
ESP_LOGI(TAG, "Register GAP callback");
if (unregister) {
ESP_LOGI(TAG, "Unregister GAP callback...");
ESP_ERROR_CHECK(esp_ble_gap_stop_scanning());
ESP_ERROR_CHECK(esp_ble_gap_register_callback(NULL));
}
else {
ESP_LOGI(TAG, "Register GAP callback...");
// This function is called when gap event occurs, 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 // register the scan callback function to the gap module
@ -224,25 +235,32 @@ esp_err_t register_ble_callback(void) {
static esp_ble_scan_params_t ble_scan_params = { static esp_ble_scan_params_t ble_scan_params = {
.scan_type = BLE_SCAN_TYPE_PASSIVE, .scan_type = BLE_SCAN_TYPE_PASSIVE,
.own_addr_type = BLE_ADDR_TYPE_RANDOM, .own_addr_type = BLE_ADDR_TYPE_RANDOM,
#if (MACFILTER)
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR,
// ADV_IND, ADV_NONCONN_IND, ADV_SCAN_IND packets are used for broadcasting
// data in broadcast applications (e.g., Beacons), so we don't want them in
// macfilter mode
#else
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL, .scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
#endif
/*
#if (MACFILTER)
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_WLIST_PRA_DIR,
// ADV_IND, ADV_NONCONN_IND, ADV_SCAN_IND packets are used for
broadcasting
// data in broadcast applications (e.g., Beacons), so we don't want
them in
// macfilter mode
#else
.scan_filter_policy = BLE_SCAN_FILTER_ALLOW_ALL,
#endif
*/
.scan_interval = .scan_interval =
(uint16_t)(cfg.blescantime * 10 / 0.625), // Time = N * 0.625 msec (uint16_t)(cfg.blescantime * 10 / 0.625), // Time = N * 0.625 msec
.scan_window = (uint16_t)(BLESCANWINDOW / 0.625) // Time = N * 0.625 msec .scan_window =
(uint16_t)(BLESCANWINDOW / 0.625) // Time = N * 0.625 msec
}; };
ESP_LOGI(TAG, "Set GAP scan parameters"); ESP_LOGI(TAG, "Set GAP scan parameters");
// This function is called to set scan parameters. // This function is called to set scan parameters.
ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ble_scan_params)); ESP_ERROR_CHECK(esp_ble_gap_set_scan_params(&ble_scan_params));
}
return ESP_OK; return ESP_OK;
@ -251,35 +269,30 @@ esp_err_t register_ble_callback(void) {
void start_BLEscan(void) { void start_BLEscan(void) {
#if (BLECOUNTER) #if (BLECOUNTER)
ESP_LOGI(TAG, "Initializing bluetooth scanner ..."); ESP_LOGI(TAG, "Initializing bluetooth scanner ...");
// Initialize BT controller to allocate task and other resource. // Initialize BT controller to allocate task and other resource.
if (btStart()) { // enable bt_controller
ESP_ERROR_CHECK(esp_coex_preference_set(ESP_COEX_PREFER_BT)); ESP_ERROR_CHECK(esp_coex_preference_set(ESP_COEX_PREFER_BT));
if (!btStart()) { // enable bt_controller ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
// Register callback function for capturing bluetooth packets
ESP_ERROR_CHECK(register_ble_callback(true));
ESP_LOGI(TAG, "Bluetooth scanner started");
#endif // BLECOUNTER
} else {
ESP_LOGE(TAG, "Bluetooth controller start failed. Resetting device"); ESP_LOGE(TAG, "Bluetooth controller start failed. Resetting device");
do_reset(true); do_reset(true);
} }
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());
// Register callback function for capturing bluetooth packets
ESP_ERROR_CHECK(register_ble_callback());
ESP_LOGI(TAG, "Bluetooth scanner started");
#endif // BLECOUNTER
} // start_BLEscan } // start_BLEscan
void stop_BLEscan(void) { void stop_BLEscan(void) {
#if (BLECOUNTER) #if (BLECOUNTER)
ESP_LOGI(TAG, "Shutting down bluetooth scanner ..."); ESP_LOGI(TAG, "Shutting down bluetooth scanner ...");
ESP_ERROR_CHECK(register_ble_callback(false)); // unregister capture function
ESP_LOGD(TAG, "unregister GAP callback...");
ESP_ERROR_CHECK(esp_ble_gap_register_callback(NULL));
ESP_LOGD(TAG, "bluedroid disable..."); ESP_LOGD(TAG, "bluedroid disable...");
ESP_ERROR_CHECK(esp_bluedroid_disable()); ESP_ERROR_CHECK(esp_bluedroid_disable());
ESP_LOGD(TAG, "bluedroid deinit..."); ESP_LOGD(TAG, "bluedroid deinit...");
ESP_ERROR_CHECK(esp_bluedroid_deinit()); ESP_ERROR_CHECK(esp_bluedroid_deinit());
if (!btStop()) { // disable bt_controller if (!btStop()) { // disable bt_controller
ESP_LOGE(TAG, "Bluetooth controller stop failed. Resetting device"); ESP_LOGE(TAG, "Bluetooth controller stop failed. Resetting device");
do_reset(true); do_reset(true);