Skip to content

Commit

Permalink
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
shripad621git committed Feb 3, 2025
1 parent 3044eeb commit 6e88c9c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
27 changes: 14 additions & 13 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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))
{
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -973,19 +985,20 @@ 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<BLEManagerImpl *>(context);
TaskHandle_t handle = xTaskGetHandle("nimble_host");
if (handle)
{
ChipLogDetail(DeviceLayer, "Schedule ble memory reclaiming since nimble host is still running");

// 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
{
Expand Down

0 comments on commit 6e88c9c

Please sign in to comment.