Skip to content

Commit cbab4ef

Browse files
committed
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass
1 parent 06edeee commit cbab4ef

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class BLEManagerImpl final : public BLEManager,
227227
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
228228
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
229229
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
230+
kBleDeinitAndMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */
230231
};
231232

232233
enum

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ CHIP_ERROR BLEManagerImpl::_Init()
244244

245245
void BLEManagerImpl::_Shutdown()
246246
{
247+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
248+
{
249+
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
250+
return;
251+
}
252+
247253
CancelBleAdvTimeoutTimer();
248254

249255
BleLayer::Shutdown();
@@ -712,13 +718,15 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
712718
return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
713719
}
714720
}
721+
715722
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
716723
{
717724
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
718725
{
719726
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
720727
}
721728
}
729+
722730
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
723731
{
724732
CancelBleAdvTimeoutTimer();
@@ -729,6 +737,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
729737
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
730738
}
731739
}
740+
732741
void BLEManagerImpl::DriveBLEState(void)
733742
{
734743
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -739,6 +748,11 @@ void BLEManagerImpl::DriveBLEState(void)
739748
mFlags.Set(Flags::kAsyncInitCompleted);
740749
}
741750

751+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
752+
{
753+
return;
754+
}
755+
742756
// Initializes the ESP BLE layer if needed.
743757
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
744758
{
@@ -844,7 +858,7 @@ void BLEManagerImpl::DriveBLEState(void)
844858
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
845859
{
846860
DeinitESPBleLayer();
847-
mFlags.ClearAll();
861+
mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased);
848862
}
849863

850864
exit:

0 commit comments

Comments
 (0)