Skip to content

Commit c5b37f0

Browse files
committed
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass (project-chip#37354)
1 parent bbe86c2 commit c5b37f0

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
@@ -233,6 +233,7 @@ class BLEManagerImpl final : public BLEManager,
233233
kUseCustomDeviceName = 0x0400, /**< The application has configured a custom BLE device name. */
234234
kAdvertisingRefreshNeeded = 0x0800, /**< The advertising configuration/state in ESP BLE layer needs to be updated. */
235235
kExtAdvertisingEnabled = 0x1000, /**< The application has enabled Extended BLE announcement. */
236+
kBleDeinitAndMemReleased = 0x2000, /**< The ble is deinitialized and memory is reclaimed. */
236237
};
237238

238239
enum

src/platform/ESP32/nimble/BLEManagerImpl.cpp

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

247247
void BLEManagerImpl::_Shutdown()
248248
{
249+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
250+
{
251+
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
252+
return;
253+
}
254+
249255
CancelBleAdvTimeoutTimer();
250256

251257
BleLayer::Shutdown();
@@ -725,13 +731,15 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
725731
return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
726732
}
727733
}
734+
728735
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
729736
{
730737
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
731738
{
732739
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
733740
}
734741
}
742+
735743
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
736744
{
737745
CancelBleAdvTimeoutTimer();
@@ -742,6 +750,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
742750
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
743751
}
744752
}
753+
745754
void BLEManagerImpl::DriveBLEState(void)
746755
{
747756
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -752,6 +761,11 @@ void BLEManagerImpl::DriveBLEState(void)
752761
mFlags.Set(Flags::kAsyncInitCompleted);
753762
}
754763

764+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
765+
{
766+
return;
767+
}
768+
755769
// Initializes the ESP BLE layer if needed.
756770
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
757771
{
@@ -857,7 +871,7 @@ void BLEManagerImpl::DriveBLEState(void)
857871
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
858872
{
859873
DeinitESPBleLayer();
860-
mFlags.ClearAll();
874+
mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased);
861875
}
862876

863877
exit:

0 commit comments

Comments
 (0)