From 6e88c9cba014b5597cb9eb80a822521b70450a13 Mon Sep 17 00:00:00 2001 From: shripad621git Date: Fri, 31 Jan 2025 15:03:17 +0530 Subject: [PATCH] [ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass --- src/platform/ESP32/BLEManagerImpl.h | 27 ++++++++++---------- src/platform/ESP32/nimble/BLEManagerImpl.cpp | 21 ++++++++++++--- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/platform/ESP32/BLEManagerImpl.h b/src/platform/ESP32/BLEManagerImpl.h index 2a489420a055a1..fe7f7e949c78fb 100644 --- a/src/platform/ESP32/BLEManagerImpl.h +++ b/src/platform/ESP32/BLEManagerImpl.h @@ -214,19 +214,20 @@ class BLEManagerImpl final : public BLEManager, enum class Flags : uint16_t { - kAsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */ - kESPBLELayerInitialized = 0x0002, /**< The ESP BLE layer has been initialized. */ - kAppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the ESP BLE layer. */ - kAttrsRegistered = 0x0008, /**< The CHIPoBLE GATT attributes have been registered with the ESP BLE layer. */ - kGATTServiceStarted = 0x0010, /**< The CHIPoBLE GATT service has been started. */ - kAdvertisingConfigured = 0x0020, /**< CHIPoBLE advertising has been configured in the ESP BLE layer. */ - kAdvertising = 0x0040, /**< The system is currently CHIPoBLE advertising. */ - kControlOpInProgress = 0x0080, /**< An async control operation has been issued to the ESP BLE layer. */ - kAdvertisingEnabled = 0x0100, /**< The application has enabled CHIPoBLE advertising. */ - kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */ - kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */ - kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */ - kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */ + kAsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */ + kESPBLELayerInitialized = 0x0002, /**< The ESP BLE layer has been initialized. */ + kAppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the ESP BLE layer. */ + kAttrsRegistered = 0x0008, /**< The CHIPoBLE GATT attributes have been registered with the ESP BLE layer. */ + kGATTServiceStarted = 0x0010, /**< The CHIPoBLE GATT service has been started. */ + kAdvertisingConfigured = 0x0020, /**< CHIPoBLE advertising has been configured in the ESP BLE layer. */ + kAdvertising = 0x0040, /**< The system is currently CHIPoBLE advertising. */ + kControlOpInProgress = 0x0080, /**< An async control operation has been issued to the ESP BLE layer. */ + kAdvertisingEnabled = 0x0100, /**< The application has enabled CHIPoBLE advertising. */ + kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */ + kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */ + kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */ + kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */ + kBleDeinitializedMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */ }; enum diff --git a/src/platform/ESP32/nimble/BLEManagerImpl.cpp b/src/platform/ESP32/nimble/BLEManagerImpl.cpp index 0a2ddb176332b2..6c878461472e48 100644 --- a/src/platform/ESP32/nimble/BLEManagerImpl.cpp +++ b/src/platform/ESP32/nimble/BLEManagerImpl.cpp @@ -244,6 +244,12 @@ CHIP_ERROR BLEManagerImpl::_Init() void BLEManagerImpl::_Shutdown() { + if (mFlags.Has(Flags::kBleDeinitializedMemReleased)) + { + ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow"); + return; + } + CancelBleAdvTimeoutTimer(); BleLayer::Shutdown(); @@ -729,6 +735,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs) ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer"); } } + void BLEManagerImpl::DriveBLEState(void) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -739,6 +746,11 @@ void BLEManagerImpl::DriveBLEState(void) mFlags.Set(Flags::kAsyncInitCompleted); } + if (mFlags.Has(Flags::kBleDeinitializedMemReleased)) + { + return; + } + // Initializes the ESP BLE layer if needed. if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized)) { @@ -844,7 +856,7 @@ void BLEManagerImpl::DriveBLEState(void) if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted)) { DeinitESPBleLayer(); - mFlags.ClearAll(); + mFlags.ClearAll().Set(Flags::kBleDeinitializedMemReleased); } exit: @@ -973,11 +985,12 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void) void BLEManagerImpl::DeinitESPBleLayer() { VerifyOrReturn(DeinitBLE() == CHIP_NO_ERROR); - BLEManagerImpl::ClaimBLEMemory(nullptr, nullptr); + BLEManagerImpl::ClaimBLEMemory(nullptr, this); } -void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void *) +void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void * context) { + auto * sInstance = static_cast(context); TaskHandle_t handle = xTaskGetHandle("nimble_host"); if (handle) { @@ -985,7 +998,7 @@ void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void *) // Rescheduling it for later, 2 seconds is an arbitrary value, keeping it a bit more so that // we dont have to reschedule it again - SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, nullptr); + SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, context); } else {