diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index 888b6e01228d8c..6de5269640521c 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -28,10 +28,13 @@ #include "Globals.h" #include "LEDWidget.h" #include "WiFiWidget.h" +#include "esp_bt.h" #include "esp_check.h" #include "esp_err.h" #include "esp_heap_caps.h" #include "esp_log.h" +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" #include "route_hook/esp_route_hook.h" #include #include @@ -114,9 +117,27 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ ESP_LOGI(TAG, "CHIPoBLE disconnected"); break; - case DeviceEventType::kCommissioningComplete: + case DeviceEventType::kCommissioningComplete: { ESP_LOGI(TAG, "Commissioning complete"); - break; +#if CONFIG_BT_NIMBLE_ENABLED && CONFIG_DEINIT_BLE_ON_COMMISSIONING_COMPLETE + int ret = nimble_port_stop(); + if (ret == 0) + { + nimble_port_deinit(); + esp_err_t err = esp_nimble_hci_and_controller_deinit(); + err += esp_bt_mem_release(ESP_BT_MODE_BLE); + if (err == ESP_OK) + { + ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); + } + } + else + { + ESP_LOGW(TAG, "nimble_port_stop() failed"); + } +#endif + } + break; case DeviceEventType::kInterfaceIpAddressChanged: if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) || diff --git a/examples/all-clusters-app/esp32/main/Kconfig.projbuild b/examples/all-clusters-app/esp32/main/Kconfig.projbuild index b305e3c8863d6a..5bd4461042836f 100644 --- a/examples/all-clusters-app/esp32/main/Kconfig.projbuild +++ b/examples/all-clusters-app/esp32/main/Kconfig.projbuild @@ -111,6 +111,11 @@ menu "Demo" help Each board has a status led, define its pin number. + config DEINIT_BLE_ON_COMMISSIONING_COMPLETE + bool "Disable and DeInit BLE on commissioning complete" + default y + help + Disable and deinit BLE and reclaim all its memory, once the commissioning is successful and Commissioning complete event is received in the application. endmenu menu "PW RPC Debug channel" diff --git a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp index f1697ec10da1e6..a9187fe053d91e 100644 --- a/examples/lighting-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/lighting-app/esp32/main/DeviceCallbacks.cpp @@ -26,9 +26,12 @@ #include "DeviceCallbacks.h" #include "LEDWidget.h" +#include "esp_bt.h" #include "esp_err.h" #include "esp_heap_caps.h" #include "esp_log.h" +#include "esp_nimble_hci.h" +#include "nimble/nimble_port.h" #include "route_hook/esp_route_hook.h" #include #include @@ -66,9 +69,27 @@ void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_ ESP_LOGI(TAG, "CHIPoBLE disconnected"); break; - case DeviceEventType::kCommissioningComplete: + case DeviceEventType::kCommissioningComplete: { ESP_LOGI(TAG, "Commissioning complete"); - break; +#if CONFIG_BT_NIMBLE_ENABLED && CONFIG_DEINIT_BLE_ON_COMMISSIONING_COMPLETE + int ret = nimble_port_stop(); + if (ret == 0) + { + nimble_port_deinit(); + esp_err_t err = esp_nimble_hci_and_controller_deinit(); + err += esp_bt_mem_release(ESP_BT_MODE_BLE); + if (err == ESP_OK) + { + ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); + } + } + else + { + ESP_LOGW(TAG, "nimble_port_stop() failed"); + } +#endif + } + break; case DeviceEventType::kInterfaceIpAddressChanged: if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) || diff --git a/examples/lighting-app/esp32/main/Kconfig.projbuild b/examples/lighting-app/esp32/main/Kconfig.projbuild index 839c53f3997d5c..7aa1ecd9be7ab7 100644 --- a/examples/lighting-app/esp32/main/Kconfig.projbuild +++ b/examples/lighting-app/esp32/main/Kconfig.projbuild @@ -106,4 +106,9 @@ menu "Demo" default 4 if RENDEZVOUS_MODE_THREAD default 8 if RENDEZVOUS_MODE_ETHERNET + config DEINIT_BLE_ON_COMMISSIONING_COMPLETE + bool "Disable and DeInit BLE on commissioning complete" + default y + help + Disable and deinit BLE and reclaim all its memory, once the commissioning is successful and Commissioning complete event is received in the application. endmenu