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] Replaced FreeRTOS timer with CHIP timers in nimble BLEManagerImpl. #34050

Merged
merged 3 commits into from
Jun 25, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class BLEManagerImpl final : public BLEManager,
CHIP_ERROR StartAdvertising(void);
void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs);
void CancelBleAdvTimeoutTimer(void);
static void BleAdvTimeoutHandler(TimerHandle_t xTimer);
static void BleAdvTimeoutHandler(System::Layer *, void *);

#ifdef CONFIG_BT_BLUEDROID_ENABLED
void HandleGATTControlEvent(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t * param);
Expand Down
39 changes: 8 additions & 31 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ namespace Internal {

namespace {

TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
static constexpr uint16_t kNewConnectionScanTimeout = 60;
static constexpr uint16_t kConnectTimeout = 20;
Expand Down Expand Up @@ -209,16 +208,6 @@ CHIP_ERROR BLEManagerImpl::_Init()
{
CHIP_ERROR err;

// Create FreeRTOS sw timer for BLE timeouts and interval change.
sbleAdvTimeoutTimer = xTimerCreate("BleAdvTimer", // Just a text name, not used by the RTOS kernel
1, // == default timer period
false, // no timer reload (==one-shot)
(void *) this, // init timer id = ble obj context
BleAdvTimeoutHandler // timer callback handler
);

VerifyOrReturnError(sbleAdvTimeoutTimer != nullptr, CHIP_ERROR_NO_MEMORY);

// Initialize the Chip BleLayer.
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
err = BleLayer::Init(this, this, this, &DeviceLayer::SystemLayer());
Expand Down Expand Up @@ -253,9 +242,7 @@ CHIP_ERROR BLEManagerImpl::_Init()

void BLEManagerImpl::_Shutdown()
{
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);
xTimerDelete(sbleAdvTimeoutTimer, portMAX_DELAY);
sbleAdvTimeoutTimer = nullptr;
CancelBleAdvTimeoutTimer();

BleLayer::Shutdown();

Expand Down Expand Up @@ -286,7 +273,7 @@ CHIP_ERROR BLEManagerImpl::_SetAdvertisingEnabled(bool val)
return err;
}

void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
void BLEManagerImpl::BleAdvTimeoutHandler(System::Layer *, void *)
{
if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled))
{
Expand All @@ -298,7 +285,6 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
BLEMgrImpl().mFlags.Clear(Flags::kExtAdvertisingEnabled);
BLEMgrImpl().StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS);
#endif
PlatformMgr().ScheduleWork(DriveBLEState, 0);
}
#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING
else
Expand All @@ -308,9 +294,9 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer)
BLEMgrImpl().mFlags.Set(Flags::kExtAdvertisingEnabled);
BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising);
BLEMgrImpl().mFlags.Set(Flags::kAdvertisingRefreshNeeded, 1);
PlatformMgr().ScheduleWork(DriveBLEState, 0);
}
#endif
PlatformMgr().ScheduleWork(DriveBLEState, 0);
}

CHIP_ERROR BLEManagerImpl::_SetAdvertisingMode(BLEAdvertisingMode mode)
Expand Down Expand Up @@ -719,26 +705,17 @@ CHIP_ERROR BLEManagerImpl::MapBLEError(int bleErr)
}
void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)
{
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);

if (xTimerStop(sbleAdvTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
if (SystemLayer().IsTimerActive(BleAdvTimeoutHandler, nullptr))
{
ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer");
SystemLayer().CancelTimer(BleAdvTimeoutHandler, nullptr);
}
}
void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs)
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved
{
VerifyOrReturn(sbleAdvTimeoutTimer != nullptr);

if (xTimerIsTimerActive(sbleAdvTimeoutTimer))
{
CancelBleAdvTimeoutTimer();
}
CancelBleAdvTimeoutTimer();

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