Skip to content

Commit 6e88c9c

Browse files
committed
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass
1 parent 3044eeb commit 6e88c9c

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/platform/ESP32/BLEManagerImpl.h

+14-13
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,20 @@ class BLEManagerImpl final : public BLEManager,
214214

215215
enum class Flags : uint16_t
216216
{
217-
kAsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
218-
kESPBLELayerInitialized = 0x0002, /**< The ESP BLE layer has been initialized. */
219-
kAppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the ESP BLE layer. */
220-
kAttrsRegistered = 0x0008, /**< The CHIPoBLE GATT attributes have been registered with the ESP BLE layer. */
221-
kGATTServiceStarted = 0x0010, /**< The CHIPoBLE GATT service has been started. */
222-
kAdvertisingConfigured = 0x0020, /**< CHIPoBLE advertising has been configured in the ESP BLE layer. */
223-
kAdvertising = 0x0040, /**< The system is currently CHIPoBLE advertising. */
224-
kControlOpInProgress = 0x0080, /**< An async control operation has been issued to the ESP BLE layer. */
225-
kAdvertisingEnabled = 0x0100, /**< The application has enabled CHIPoBLE advertising. */
226-
kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */
227-
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
228-
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
229-
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
217+
kAsyncInitCompleted = 0x0001, /**< One-time asynchronous initialization actions have been performed. */
218+
kESPBLELayerInitialized = 0x0002, /**< The ESP BLE layer has been initialized. */
219+
kAppRegistered = 0x0004, /**< The CHIPoBLE application has been registered with the ESP BLE layer. */
220+
kAttrsRegistered = 0x0008, /**< The CHIPoBLE GATT attributes have been registered with the ESP BLE layer. */
221+
kGATTServiceStarted = 0x0010, /**< The CHIPoBLE GATT service has been started. */
222+
kAdvertisingConfigured = 0x0020, /**< CHIPoBLE advertising has been configured in the ESP BLE layer. */
223+
kAdvertising = 0x0040, /**< The system is currently CHIPoBLE advertising. */
224+
kControlOpInProgress = 0x0080, /**< An async control operation has been issued to the ESP BLE layer. */
225+
kAdvertisingEnabled = 0x0100, /**< The application has enabled CHIPoBLE advertising. */
226+
kFastAdvertisingEnabled = 0x0200, /**< The application has enabled fast advertising. */
227+
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
228+
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
229+
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
230+
kBleDeinitializedMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */
230231
};
231232

232233
enum

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+17-4
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::kBleDeinitializedMemReleased))
248+
{
249+
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
250+
return;
251+
}
252+
247253
CancelBleAdvTimeoutTimer();
248254

249255
BleLayer::Shutdown();
@@ -729,6 +735,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
729735
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
730736
}
731737
}
738+
732739
void BLEManagerImpl::DriveBLEState(void)
733740
{
734741
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -739,6 +746,11 @@ void BLEManagerImpl::DriveBLEState(void)
739746
mFlags.Set(Flags::kAsyncInitCompleted);
740747
}
741748

749+
if (mFlags.Has(Flags::kBleDeinitializedMemReleased))
750+
{
751+
return;
752+
}
753+
742754
// Initializes the ESP BLE layer if needed.
743755
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
744756
{
@@ -844,7 +856,7 @@ void BLEManagerImpl::DriveBLEState(void)
844856
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
845857
{
846858
DeinitESPBleLayer();
847-
mFlags.ClearAll();
859+
mFlags.ClearAll().Set(Flags::kBleDeinitializedMemReleased);
848860
}
849861

850862
exit:
@@ -973,19 +985,20 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)
973985
void BLEManagerImpl::DeinitESPBleLayer()
974986
{
975987
VerifyOrReturn(DeinitBLE() == CHIP_NO_ERROR);
976-
BLEManagerImpl::ClaimBLEMemory(nullptr, nullptr);
988+
BLEManagerImpl::ClaimBLEMemory(nullptr, this);
977989
}
978990

979-
void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void *)
991+
void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void * context)
980992
{
993+
auto * sInstance = static_cast<BLEManagerImpl *>(context);
981994
TaskHandle_t handle = xTaskGetHandle("nimble_host");
982995
if (handle)
983996
{
984997
ChipLogDetail(DeviceLayer, "Schedule ble memory reclaiming since nimble host is still running");
985998

986999
// Rescheduling it for later, 2 seconds is an arbitrary value, keeping it a bit more so that
9871000
// we dont have to reschedule it again
988-
SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, nullptr);
1001+
SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, context);
9891002
}
9901003
else
9911004
{

0 commit comments

Comments
 (0)