Skip to content

Commit

Permalink
Refactor BLEManagerImpl::_OnPlatformEvent for improved code readabili…
Browse files Browse the repository at this point in the history
…ty and maintainability
  • Loading branch information
rosahay-silabs committed Jul 5, 2024
1 parent 48a20c5 commit 9f1b180
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/platform/silabs/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla

#if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE)
protected:
static void OnSendIndicationTimeout(System::Layer * aLayer, void * appState);
static void OnSendIndicationTimeout(TimerHandle_t xTimer);
#endif
};

Expand Down
30 changes: 21 additions & 9 deletions src/platform/silabs/rs911x/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ namespace {
#define BLE_CONFIG_MAX_CE_LENGTH (0xFFFF) // Leave to max value

TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer.
TimerHandle_t sbleIndicationTimeoutTimer;

const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80,
0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 };
Expand Down Expand Up @@ -270,11 +271,17 @@ CHIP_ERROR BLEManagerImpl::_Init()
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;

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

mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
Expand All @@ -285,7 +292,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
return err;
}

void BLEManagerImpl::OnSendIndicationTimeout(System::Layer * aLayer, void * appState)
void BLEManagerImpl::OnSendIndicationTimeout(TimerHandle_t xTimer)
{
// TODO: change the connection handle with the ble device ID
uint8_t connHandle = 1;
Expand Down Expand Up @@ -475,8 +482,10 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUU
}

// start timer for the indication Confirmation Event
DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Milliseconds32(BLE_SEND_INDICATION_TIMER_PERIOD_MS),
OnSendIndicationTimeout, this);
if (xTimerStart(sbleIndicationTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
{
ChipLogError(DeviceLayer, "Failed to start indication timer");
}
return true;
}

Expand Down Expand Up @@ -924,7 +933,10 @@ void BLEManagerImpl::HandleRXCharWrite(rsi_ble_event_write_t * evt)
void BLEManagerImpl::HandleTxConfirmationEvent(BLE_CONNECTION_OBJECT conId)
{
// stop the indication confirmation timer
DeviceLayer::SystemLayer().CancelTimer(OnSendIndicationTimeout, this);
if (xTimerStop(sbleIndicationTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL)
{
ChipLogError(DeviceLayer, "Failed to stop indication timer");
}
ChipDeviceEvent event;
event.Type = DeviceEventType::kCHIPoBLEIndicateConfirm;
event.CHIPoBLEIndicateConfirm.ConId = conId;
Expand Down

0 comments on commit 9f1b180

Please sign in to comment.