diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index eddb61524c0a74..0830c49a478cc3 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -16,18 +16,7 @@ * limitations under the License. */ #include "CommonDeviceCallbacks.h" - -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE -#if CONFIG_BT_ENABLED -#include "esp_bt.h" -#if CONFIG_BT_NIMBLE_ENABLED -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) -#include "esp_nimble_hci.h" -#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) -#include "nimble/nimble_port.h" -#endif // CONFIG_BT_NIMBLE_ENABLED -#endif // CONFIG_BT_ENABLED -#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#include "Esp32AppServer.h" #include "esp_err.h" #include "esp_heap_caps.h" @@ -62,48 +51,7 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i case DeviceEventType::kCHIPoBLEConnectionClosed: ESP_LOGI(TAG, "CHIPoBLE disconnected"); - -#if CONFIG_BT_ENABLED && 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()) - { - ESP_LOGI(TAG, "BLE already deinited"); - break; - } - if (nimble_port_stop() != 0) - { - ESP_LOGE(TAG, "nimble_port_stop() failed"); - break; - } - vTaskDelay(100); - nimble_port_deinit(); - -#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) - err = esp_nimble_hci_and_controller_deinit(); -#endif -#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_LOGE(TAG, "BLE deinit failed"); - } - else - { - ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); - } - } -#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */ - + Esp32AppServer::DeInitBLEIfCommissioned(); break; case DeviceEventType::kDnssdInitialized: diff --git a/examples/platform/esp32/common/Esp32AppServer.cpp b/examples/platform/esp32/common/Esp32AppServer.cpp index db787d09867b68..8baafcb458a2d7 100644 --- a/examples/platform/esp32/common/Esp32AppServer.cpp +++ b/examples/platform/esp32/common/Esp32AppServer.cpp @@ -27,6 +27,19 @@ #if CONFIG_ENABLE_ICD_SERVER #include #endif + +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#if CONFIG_BT_ENABLED +#include "esp_bt.h" +#if CONFIG_BT_NIMBLE_ENABLED +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) +#include "esp_nimble_hci.h" +#endif // ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) +#include "nimble/nimble_port.h" +#endif // CONFIG_BT_NIMBLE_ENABLED +#endif // CONFIG_BT_ENABLED +#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + #include using namespace chip; @@ -100,6 +113,50 @@ static size_t hex_string_to_binary(const char * hex_string, uint8_t * buf, size_ } #endif // CONFIG_TEST_EVENT_TRIGGER_ENABLED +void Esp32AppServer::DeInitBLEIfCommissioned(void) +{ +#if CONFIG_BT_ENABLED && 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()) + { + ESP_LOGI(TAG, "BLE already deinited"); + return; + } + if (nimble_port_stop() != 0) + { + ESP_LOGE(TAG, "nimble_port_stop() failed"); + return; + } + vTaskDelay(100); + nimble_port_deinit(); + +#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) + err = esp_nimble_hci_and_controller_deinit(); +#endif +#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_LOGE(TAG, "BLE deinit failed"); + } + else + { + ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); + } + } +#endif /* CONFIG_BT_ENABLED && CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */ +} + void Esp32AppServer::Init(AppDelegate * sAppDelegate) { // Init ZCL Data Model and CHIP App Server @@ -136,4 +193,5 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate) chip::app::DnssdServer::Instance().StartServer(); } #endif + DeInitBLEIfCommissioned(); } diff --git a/examples/platform/esp32/common/Esp32AppServer.h b/examples/platform/esp32/common/Esp32AppServer.h index 7b1598cca5a57e..b09aa1fef972f0 100644 --- a/examples/platform/esp32/common/Esp32AppServer.h +++ b/examples/platform/esp32/common/Esp32AppServer.h @@ -22,5 +22,6 @@ #include namespace Esp32AppServer { +void DeInitBLEIfCommissioned(void); void Init(AppDelegate * context = nullptr); } // namespace Esp32AppServer diff --git a/src/platform/ESP32/OpenthreadLauncher.c b/src/platform/ESP32/OpenthreadLauncher.c index 7183767016bb89..4eaa674e4d8c48 100644 --- a/src/platform/ESP32/OpenthreadLauncher.c +++ b/src/platform/ESP32/OpenthreadLauncher.c @@ -55,9 +55,18 @@ static void ot_task_worker(void * context) vTaskDelete(NULL); } -void set_openthread_platform_config(esp_openthread_platform_config_t * config) +esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config) { - s_platform_config = config; + if (!s_platform_config) + { + s_platform_config = (esp_openthread_platform_config_t *) malloc(sizeof(esp_openthread_platform_config_t)); + if (!s_platform_config) + { + return ESP_ERR_NO_MEM; + } + } + memcpy(s_platform_config, config, sizeof(esp_openthread_platform_config_t)); + return ESP_OK; } esp_err_t openthread_init_stack(void) @@ -77,6 +86,8 @@ esp_err_t openthread_init_stack(void) ESP_ERROR_CHECK(esp_openthread_init(s_platform_config)); // Initialize the esp_netif bindings openthread_netif = init_openthread_netif(s_platform_config); + free(s_platform_config); + s_platform_config = NULL; return ESP_OK; } diff --git a/src/platform/ESP32/OpenthreadLauncher.h b/src/platform/ESP32/OpenthreadLauncher.h index 6817a4dc5afb80..a7f4ef02747eb2 100644 --- a/src/platform/ESP32/OpenthreadLauncher.h +++ b/src/platform/ESP32/OpenthreadLauncher.h @@ -24,7 +24,7 @@ extern "C" { #endif -void set_openthread_platform_config(esp_openthread_platform_config_t * config); +esp_err_t set_openthread_platform_config(esp_openthread_platform_config_t * config); esp_err_t openthread_init_stack(void); esp_err_t openthread_launch_task(void); diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 6a9a047ab84db1..d62e5530823af0 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -211,21 +211,6 @@ void HandleIncomingBleConnection(BLEEndPoint * bleEP) CHIP_ERROR BLEManagerImpl::_Init() { -#if CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING -#if CHIP_DEVICE_CONFIG_ENABLE_THREAD - if (ConnectivityMgr().IsThreadProvisioned()) - { - ESP_LOGI(TAG, "Thread credentials already provisioned, not initializing BLE"); -#else - if (ConnectivityMgr().IsWiFiStationProvisioned()) - { - ESP_LOGI(TAG, "WiFi station already provisioned, not initializing BLE"); -#endif /* CHIP_DEVICE_CONFIG_ENABLE_THREAD */ - esp_bt_mem_release(ESP_BT_MODE_BTDM); - return CHIP_NO_ERROR; - } -#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */ - CHIP_ERROR err; // Initialize the Chip BleLayer.