Skip to content

Commit 2336b95

Browse files
shripad621gitgmarcosb
authored andcommitted
[ESP32]: Fixed the crash due to ble_hs_is_enabled check bypass (project-chip#37354)
1 parent b7e829d commit 2336b95

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
@@ -247,6 +247,12 @@ CHIP_ERROR BLEManagerImpl::_Init()
247247

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

252258
BleLayer::Shutdown();
@@ -715,13 +721,15 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
715721
return CHIP_ERROR(ChipError::Range::kPlatform, CHIP_DEVICE_CONFIG_ESP32_BLE_ERROR_MIN + bleErr);
716722
}
717723
}
724+
718725
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
719726
{
720727
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
721728
{
722729
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
723730
}
724731
}
732+
725733
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
726734
{
727735
CancelBleAdvTimeoutTimer();
@@ -732,6 +740,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
732740
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
733741
}
734742
}
743+
735744
void BLEManagerImpl::DriveBLEState(void)
736745
{
737746
CHIP_ERROR err = CHIP_NO_ERROR;
@@ -742,6 +751,11 @@ void BLEManagerImpl::DriveBLEState(void)
742751
mFlags.Set(Flags::kAsyncInitCompleted);
743752
}
744753

754+
if (mFlags.Has(Flags::kBleDeinitAndMemReleased))
755+
{
756+
return;
757+
}
758+
745759
// Initializes the ESP BLE layer if needed.
746760
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
747761
{
@@ -847,7 +861,7 @@ void BLEManagerImpl::DriveBLEState(void)
847861
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
848862
{
849863
DeinitESPBleLayer();
850-
mFlags.ClearAll();
864+
mFlags.ClearAll().Set(Flags::kBleDeinitAndMemReleased);
851865
}
852866

853867
exit:

0 commit comments

Comments
 (0)