From 1511538beaf56078fc055a1f25d6ac227ecb0d38 Mon Sep 17 00:00:00 2001 From: Hrishikesh Dhayagude Date: Wed, 26 Apr 2023 00:43:23 +0530 Subject: [PATCH] ESP32: Move the BLE deinit after BLE connection is closed, post commissioning (#26183) --- .../esp32/common/CommonDeviceCallbacks.cpp | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index 4a112aef9ed7f0..c4409a59cc20f2 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -33,6 +33,7 @@ #include "esp_heap_caps.h" #include "esp_log.h" #include +#include #include #include #if CONFIG_ENABLE_OTA_REQUESTOR @@ -61,6 +62,43 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i case DeviceEventType::kCHIPoBLEConnectionClosed: ESP_LOGI(TAG, "CHIPoBLE disconnected"); +#if CONFIG_BT_ENABLED +#if CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING + + if (chip::Server::GetInstance().GetFabricTable().FabricCount() > 0) + { + esp_err_t err = ESP_OK; +#if CONFIG_BT_NIMBLE_ENABLED + if (ble_hs_is_enabled()) + { + int ret = nimble_port_stop(); + if (ret == 0) + { + nimble_port_deinit(); +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) + err = esp_nimble_hci_and_controller_deinit(); +#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) +#endif // CONFIG_BT_NIMBLE_ENABLED + +#if CONFIG_IDF_TARGET_ESP32 + err += esp_bt_mem_release(ESP_BT_MODE_BTDM); +#elif CONFIG_IDF_TARGET_ESP32C2 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2 + err += esp_bt_mem_release(ESP_BT_MODE_BLE); +#endif + if (err == ESP_OK) + { + ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); + } + } + else + { + ESP_LOGW(TAG, "nimble_port_stop() failed"); + } + } + else { ESP_LOGI(TAG, "BLE already deinited"); } + } +#endif // CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING +#endif // CONFIG_BT_ENABLED break; case DeviceEventType::kDnssdInitialized: @@ -76,34 +114,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i case DeviceEventType::kCommissioningComplete: { ESP_LOGI(TAG, "Commissioning complete"); -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE && CONFIG_BT_NIMBLE_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING - - if (ble_hs_is_enabled()) - { - int ret = nimble_port_stop(); - esp_err_t err = ESP_OK; - if (ret == 0) - { - nimble_port_deinit(); -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - err = esp_nimble_hci_and_controller_deinit(); -#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - err += esp_bt_mem_release(ESP_BT_MODE_BTDM); - if (err == ESP_OK) - { - ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); - } - } - else - { - ESP_LOGW(TAG, "nimble_port_stop() failed"); - } - } - else - { - ESP_LOGI(TAG, "BLE already deinited"); - } -#endif } break;