Skip to content

Commit 850302d

Browse files
pimpalemaheshshubhamdp
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 f4363a0 commit 850302d

File tree

2 files changed

+10
-33
lines changed

2 files changed

+10
-33
lines changed

src/platform/ESP32/BLEManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ class BLEManagerImpl final : public BLEManager,
302302
CHIP_ERROR StartAdvertising(void);
303303
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
304304
void CancelBleAdvTimeoutTimer(void);
305-
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
305+
static void BleAdvTimeoutHandler(System::Layer *, void *);
306306

307307
#if CONFIG_BT_BLUEDROID_ENABLED
308308
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

+9-32
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ namespace Internal {
7676

7777
namespace {
7878

79-
TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
80-
#if CONFIG_ENABLE_ESP32_BLE_CONTROLLER
79+
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
8180
static constexpr uint16_t kNewConnectionScanTimeout = 60;
8281
static constexpr uint16_t kConnectTimeout = 20;
8382
#endif
@@ -212,16 +211,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
212211
{
213212
CHIP_ERROR err;
214213

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

258247
void BLEManagerImpl::_Shutdown()
259248
{
260-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
261-
xTimerDelete(sbleAdvTimeoutTimer, portMAX_DELAY);
262-
sbleAdvTimeoutTimer = nullptr;
249+
CancelBleAdvTimeoutTimer();
263250

264251
BleLayer::Shutdown();
265252

@@ -294,7 +281,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
294281
return err;
295282
}
296283

297-
void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
284+
void BLEManagerImpl::BleAdvTimeoutHandler(System::Layer *, void *)
298285
{
299286
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
300287
{
@@ -306,7 +293,6 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
306293
BLEMgrImpl().mFlags.Clear(Flags::kExtAdvertisingEnabled);
307294
BLEMgrImpl().StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
308295
#endif
309-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
310296
}
311297
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
312298
else
@@ -316,9 +302,9 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
316302
BLEMgrImpl().mFlags.Set(Flags::kExtAdvertisingEnabled);
317303
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
318304
BLEMgrImpl().mFlags.Set(Flags::kAdvertisingRefreshNeeded, 1);
319-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
320305
}
321306
#endif
307+
PlatformMgr().ScheduleWork(DriveBLEState, 0);
322308
}
323309

324310
CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
@@ -729,26 +715,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
729715
}
730716
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
731717
{
732-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
733-
734-
if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
718+
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
735719
{
736-
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
720+
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
737721
}
738722
}
739723
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
740724
{
741-
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
742-
743-
if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
744-
{
745-
CancelBleAdvTimeoutTimer();
746-
}
725+
CancelBleAdvTimeoutTimer();
747726

748-
// timer is not active, change its period to required value (== restart).
749-
// FreeRTOS- Block for a maximum of 100 ticks if the change period command
750-
// cannot immediately be sent to the timer command queue.
751-
if (xTimerChangePeriod(sbleAdvTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(100)) != pdPASS)
727+
CHIP_ERROR err = SystemLayer().StartTimer(System::Clock::Milliseconds32(aTimeoutInMs), BleAdvTimeoutHandler, nullptr);
728+
if ((err != CHIP_NO_ERROR))
752729
{
753730
ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer");
754731
}

0 commit comments

Comments
 (0)