From 38c5f6722ce5a0c51c28d5c18d686d9e7030b968 Mon Sep 17 00:00:00 2001 From: Rohit Jadhav <69809379+jadhavrohit924@users.noreply.github.com> Date: Tue, 22 Mar 2022 11:04:54 +0530 Subject: [PATCH] [ESP32] Added config option to disable and de-init BLE post commissioning (#16483) * [ESP32] Added config option to disable and de-init BLE post commissioning * Fixed pipline failure * Implemented config option for all-cluster and lighting app * Address review comment --- .../esp32/main/DeviceCallbacks.cpp | 25 +++++++++++++++++-- .../esp32/main/Kconfig.projbuild | 5 ++++ .../esp32/main/DeviceCallbacks.cpp | 25 +++++++++++++++++-- .../lighting-app/esp32/main/Kconfig.projbuild | 5 ++++ 4 files changed, 56 insertions(+), 4 deletions(-) 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