Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32]: Fixed the crash due to bypassing the ble_hs_is_enabled check. #37354

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,20 @@ class BLEManagerImpl final : public BLEManager,

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

enum
Expand Down
21 changes: 17 additions & 4 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,12 @@ CHIP_ERROR BLEManagerImpl::_Init()

void BLEManagerImpl::_Shutdown()
{
if (mFlags.Has(Flags::kBleDeinitializedMemReleased))
{
ChipLogProgress(DeviceLayer, "Ble already deinitialized, returning from ShutDown flow");
return;
}

CancelBleAdvTimeoutTimer();

BleLayer::Shutdown();
Expand Down Expand Up @@ -729,6 +735,7 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
}
}

void BLEManagerImpl::DriveBLEState(void)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand All @@ -739,6 +746,11 @@ void BLEManagerImpl::DriveBLEState(void)
mFlags.Set(Flags::kAsyncInitCompleted);
}

if (mFlags.Has(Flags::kBleDeinitializedMemReleased))
{
return;
}

// Initializes the ESP BLE layer if needed.
if (mServiceMode == ConnectivityManager::kCHIPoBLEServiceMode_Enabled && !mFlags.Has(Flags::kESPBLELayerInitialized))
{
Expand Down Expand Up @@ -844,7 +856,7 @@ void BLEManagerImpl::DriveBLEState(void)
if (mServiceMode != ConnectivityManager::kCHIPoBLEServiceMode_Enabled && mFlags.Has(Flags::kGATTServiceStarted))
{
DeinitESPBleLayer();
mFlags.ClearAll();
mFlags.ClearAll().Set(Flags::kBleDeinitializedMemReleased);
}

exit:
Expand Down Expand Up @@ -975,20 +987,21 @@ void BLEManagerImpl::DeinitESPBleLayer()
{
VerifyOrReturn(DeinitBLE() == CHIP_NO_ERROR);
#ifdef CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING
BLEManagerImpl::ClaimBLEMemory(nullptr, nullptr);
BLEManagerImpl::ClaimBLEMemory(nullptr, this);
#endif /* CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING */
}

void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void *)
void BLEManagerImpl::ClaimBLEMemory(System::Layer *, void * context)
{
auto * sInstance = static_cast<BLEManagerImpl *>(context);
TaskHandle_t handle = xTaskGetHandle("nimble_host");
if (handle)
{
ChipLogDetail(DeviceLayer, "Schedule ble memory reclaiming since nimble host is still running");

// Rescheduling it for later, 2 seconds is an arbitrary value, keeping it a bit more so that
// we dont have to reschedule it again
SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, nullptr);
SystemLayer().StartTimer(System::Clock::Seconds32(2), ClaimBLEMemory, context);
}
else
{
Expand Down
Loading