Skip to content

Commit 594b555

Browse files
pimpalemaheshaustina-csa
authored andcommitted
[ESP32] Replaced FreeRTOS timer with CHIP timers in nimble BLEManagerImpl. (project-chip#34050)
* Replace free RTOS timer with ChipTimer * Replaced FreeRTOS timer with CHIP timer
1 parent 7b2bbaf commit 594b555

File tree

2 files changed

+9
-32
lines changed

2 files changed

+9
-32
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class BLEManagerImpl final : public BLEManager,
296296
CHIP_ERROR StartAdvertising(void);
297297
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
298298
void CancelBleAdvTimeoutTimer(void);
299-
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
299+
static void BleAdvTimeoutHandler(System::Layer *, void *);
300300

301301
#ifdef CONFIG_BT_BLUEDROID_ENABLED
302302
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);

src/platform/ESP32/nimble/BLEManagerImpl.cpp

+8-31
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ namespace Internal {
8282

8383
namespace {
8484

85-
TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
8685
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
8786
static constexpr uint16_t kNewConnectionScanTimeout = 60;
8887
static constexpr uint16_t kConnectTimeout = 20;
@@ -209,16 +208,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
209208
{
210209
CHIP_ERROR err;
211210

212-
// Create FreeRTOS sw timer for BLE timeouts and interval change.
213-
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // Just a text name, not used by the RTOS kernel
214-
1, // == default timer period
215-
false, // no timer reload (==one-shot)
216-
(void *) this, // init timer id = ble obj context
217-
BleAdvTimeoutHandler // timer callback handler
218-
);
219-
220-
VerifyOrReturnError(sbleAdvTimeoutTimer != nullptr, CHIP_ERROR_NO_MEMORY);
221-
222211
// Initialize the Chip BleLayer.
223212
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
224213
err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
@@ -253,9 +242,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
253242

254243
void BLEManagerImpl::_Shutdown()
255244
{
256-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
257-
xTimerDelete(sbleAdvTimeoutTimer, portMAX_DELAY);
258-
sbleAdvTimeoutTimer = nullptr;
245+
CancelBleAdvTimeoutTimer();
259246

260247
BleLayer::Shutdown();
261248

@@ -286,7 +273,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
286273
return err;
287274
}
288275

289-
void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
276+
void BLEManagerImpl::BleAdvTimeoutHandler(System::Layer *, void *)
290277
{
291278
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
292279
{
@@ -298,7 +285,6 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
298285
BLEMgrImpl().mFlags.Clear(Flags::kExtAdvertisingEnabled);
299286
BLEMgrImpl().StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
300287
#endif
301-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
302288
}
303289
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
304290
else
@@ -308,9 +294,9 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
308294
BLEMgrImpl().mFlags.Set(Flags::kExtAdvertisingEnabled);
309295
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
310296
BLEMgrImpl().mFlags.Set(Flags::kAdvertisingRefreshNeeded, 1);
311-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
312297
}
313298
#endif
299+
PlatformMgr().ScheduleWork(DriveBLEState, 0);
314300
}
315301

316302
CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
@@ -719,26 +705,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
719705
}
720706
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
721707
{
722-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
723-
724-
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
708+
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
725709
{
726-
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
710+
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
727711
}
728712
}
729713
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
730714
{
731-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
732-
733-
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
734-
{
735-
CancelBleAdvTimeoutTimer();
736-
}
715+
CancelBleAdvTimeoutTimer();
737716

738-
// timer is not active, change its period to required value (== restart).
739-
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
740-
// cannot immediately be sent to the timer command queue.
741-
if (xTimerChangePeriod(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
717+
CHIP_ERROR err = SystemLayer().StartTimer(System::Clock::Milliseconds32(aTimeoutInMs), BleAdvTimeoutHandler, nullptr);
718+
if ((err != CHIP_NO_ERROR))
742719
{
743720
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
744721
}

0 commit comments

Comments
 (0)