diff --git a/src/platform/silabs/BLEManagerImpl.h b/src/platform/silabs/BLEManagerImpl.h index 12b3ba121886af..6d628217f91c96 100644 --- a/src/platform/silabs/BLEManagerImpl.h +++ b/src/platform/silabs/BLEManagerImpl.h @@ -30,6 +30,8 @@ #define BLE_MAX_CONNECTION_INTERVAL_MS 45 // 45 msec #define BLE_SLAVE_LATENCY_MS 0 #define BLE_TIMEOUT_MS 400 +#define BLE_DEFAULT_TIMER_PERIOD_MS (1) +#define BLE_SEND_INDICATION_TIMER_PERIOD_MS (400) // Time kept to support all WiFi chips BLE (RS9116/ SiWx917 NCP/SOC) #endif // (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE) #include "FreeRTOS.h" #include "timers.h" @@ -198,6 +200,9 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla #if (SLI_SI91X_ENABLE_BLE || RSI_BLE_ENABLE) void HandleRXCharWrite(rsi_ble_event_write_t * evt); + void StartBleSendIndicationTimeoutTimer(uint32_t aTimeoutInMs); + void CancelBleSendIndicationTimeoutTimer(void); + static void BleSendIndicationTimeoutHandler(TimerHandle_t xTimer); #else void HandleRXCharWrite(volatile sl_bt_msg_t * evt); #endif diff --git a/src/platform/silabs/rs911x/BLEManagerImpl.cpp b/src/platform/silabs/rs911x/BLEManagerImpl.cpp index a32d46b30f2715..d86a2d9a2b649c 100644 --- a/src/platform/silabs/rs911x/BLEManagerImpl.cpp +++ b/src/platform/silabs/rs911x/BLEManagerImpl.cpp @@ -246,9 +246,8 @@ namespace { #define BLE_CONFIG_MIN_CE_LENGTH (0) // Leave to min value #define BLE_CONFIG_MAX_CE_LENGTH (0xFFFF) // Leave to max value -#define BLE_DEFAULT_TIMER_PERIOD_MS (1) - TimerHandle_t sbleAdvTimeoutTimer; // FreeRTOS sw timer. +TimerHandle_t sbleSendIndicationTimeoutTimer; // FreeRTOS sw timer. const uint8_t UUID_CHIPoBLEService[] = { 0xFB, 0x34, 0x9B, 0x5F, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0xF6, 0xFF, 0x00, 0x00 }; @@ -293,6 +292,13 @@ CHIP_ERROR BLEManagerImpl::_Init() BleAdvTimeoutHandler // timer callback handler ); + sbleSendIndicationTimeoutTimer = xTimerCreate("SendIndicationTimer", // 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 + BleSendIndicationTimeoutHandler // timer callback handler + ); + mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART); mFlags.Set(Flags::kFastAdvertisingEnabled, true); PlatformMgr().ScheduleWork(DriveBLEState, 0); @@ -471,6 +477,9 @@ bool BLEManagerImpl::SendIndication(BLE_CONNECTION_OBJECT conId, const ChipBleUU int32_t status = 0; status = rsi_ble_indicate_value(event_msg.resp_enh_conn.dev_addr, event_msg.rsi_ble_measurement_hndl, (data->DataLength()), data->Start()); + + StartBleSendIndicationTimeoutTimer(BLE_SEND_INDICATION_TIMER_PERIOD_MS); + if (status != RSI_SUCCESS) { ChipLogProgress(DeviceLayer, "indication failed with error code %lx ", status); @@ -925,12 +934,19 @@ void BLEManagerImpl::HandleTxConfirmationEvent(BLE_CONNECTION_OBJECT conId) event.Type = DeviceEventType::kCHIPoBLEIndicateConfirm; event.CHIPoBLEIndicateConfirm.ConId = conId; PlatformMgr().PostEventOrDie(&event); + CancelBleSendIndicationTimeoutTimer(); } -// TODO:: Need to Implement + void BLEManagerImpl::HandleSoftTimerEvent(void) { - // TODO:: Need to Implement + uint8_t connHandle = 1; + ChipLogProgress(DeviceLayer, "BLEManagerImpl::HandleSoftTimerEvent CHIPOBLE_PROTOCOL_ABORT"); + ChipDeviceEvent event; + event.Type = DeviceEventType::kCHIPoBLEConnectionError; + event.CHIPoBLEConnectionError.ConId = connHandle; + event.CHIPoBLEConnectionError.Reason = BLE_ERROR_CHIPOBLE_PROTOCOL_ABORT; + PlatformMgr().PostEventOrDie(&event); } bool BLEManagerImpl::RemoveConnection(uint8_t connectionHandle) @@ -1101,6 +1117,36 @@ void BLEManagerImpl::StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs) } } + +void BLEManagerImpl::BleSendIndicationTimeoutHandler(TimerHandle_t xTimer) +{ + sInstance.HandleSoftTimerEvent(); +} + +void BLEManagerImpl::CancelBleSendIndicationTimeoutTimer(void) +{ + if (xTimerStop(sbleSendIndicationTimeoutTimer, pdMS_TO_TICKS(0)) == pdFAIL) + { + ChipLogError(DeviceLayer, "Failed to stop BledAdv timeout timer"); + } +} + +void BLEManagerImpl::StartBleSendIndicationTimeoutTimer(uint32_t aTimeoutInMs) +{ + if (xTimerIsTimerActive(sbleSendIndicationTimeoutTimer)) + { + 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(sbleSendIndicationTimeoutTimer, pdMS_TO_TICKS(aTimeoutInMs), pdMS_TO_TICKS(BLE_CONFIG_TIMEOUT)) != pdPASS) + { + ChipLogError(DeviceLayer, "Failed to start BledAdv timeout timer"); + } +} + void BLEManagerImpl::DriveBLEState(intptr_t arg) { sInstance.DriveBLEState();