Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 879db8a

Browse files
committedFeb 26, 2025·
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass
1 parent 06edeee commit 879db8a

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
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

+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)
Please sign in to comment.