Skip to content

Commit a37e2ad

Browse files
committed
Add checks for timer handler
1 parent 7b6d6c3 commit a37e2ad

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+18-9
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
211211
{
212212
CHIP_ERROR err;
213213

214-
// Initialize the Chip BleLayer.
215-
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
216-
err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
217-
#else
218-
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
219-
#endif
220-
SuccessOrExit(err);
221-
222214
// Create FreeRTOS sw timer for BLE timeouts and interval change.
223215
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // Just a text name, not used by the RTOS kernel
224216
1, // == default timer period
@@ -227,6 +219,16 @@ CHIP_ERROR BLEManagerImpl::_Init()
227219
BleAdvTimeoutHandler // timer callback handler
228220
);
229221

222+
VerifyOrReturnError(sbleAdvTimeoutTimer != nullptr, CHIP_ERROR_NO_MEMORY);
223+
224+
// Initialize the Chip BleLayer.
225+
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
226+
err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
227+
#else
228+
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
229+
#endif
230+
SuccessOrExit(err);
231+
230232
mRXCharAttrHandle = 0;
231233
#if CHIP_ENABLE_ADDITIONAL_DATA_ADVERTISING
232234
mC3CharAttrHandle = 0;
@@ -254,8 +256,11 @@ CHIP_ERROR BLEManagerImpl::_Init()
254256

255257
void BLEManagerImpl::_Shutdown()
256258
{
257-
BleLayer::Shutdown();
259+
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
258260
xTimerDelete(sbleAdvTimeoutTimer, portMAX_DELAY);
261+
sbleAdvTimeoutTimer = nullptr;
262+
263+
BleLayer::Shutdown();
259264

260265
// selectively setting kGATTServiceStarted flag, in order to notify the state machine to stop the CHIPoBLE GATT service
261266
mFlags.ClearAll().Set(Flags::kGATTServiceStarted);
@@ -723,13 +728,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
723728
}
724729
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
725730
{
731+
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
732+
726733
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
727734
{
728735
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
729736
}
730737
}
731738
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
732739
{
740+
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
741+
733742
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
734743
{
735744
CancelBleAdvTimeoutTimer();

0 commit comments

Comments
 (0)