Skip to content

Commit 9e0cfa9

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

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:
@@ -975,20 +987,21 @@ void BLEManagerImpl::DeinitESPBleLayer()
975987
{
976988
VerifyOrReturn(DeinitBLE() == CHIP_NO_ERROR);
977989
#ifdef CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
978-
BLEManagerImpl::ClaimBLEMemory(nullptr, nullptr);
990+
BLEManagerImpl::ClaimBLEMemory(nullptr, this);
979991
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
980992
}
981993

982-
void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void *)
994+
void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void * context)
983995
{
996+
auto * sInstance = static_cast<BLEManagerImpl *>(context);
984997
TaskHandle_t handle = xTaskGetHandle("nimble_host");
985998
if (handle)
986999
{
9871000
ChipLogDetail(DeviceLayer, "Schedule ble memory reclaiming since nimble host is still running");
9881001

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

0 commit comments

Comments
 (0)