From 956a24feebe2406d3d127041396c053b8e970cd6 Mon Sep 17 00:00:00 2001 From: Mathieu Kardous Date: Tue, 16 Apr 2024 13:15:51 +0000 Subject: [PATCH] Pull request #1759: [Cherry-Pick] Add adv change event to silabs platforms (#32987) Merge in WMN_TOOLS/matter from cherry-pick/advertising_change_event to RC_2.3.0-1.3 Squashed commit of the following: commit 095bd4988dabf92837a81829bf2d8b1d9414200c Author: mkardous-silabs <84793247+mkardous-silabs@users.noreply.github.com> Date: Mon Apr 15 11:51:14 2024 -0500 Add adv change event to silabs platforms (#32987) --- src/platform/silabs/efr32/BLEManagerImpl.cpp | 44 +++++++++++++------ src/platform/silabs/rs911x/BLEManagerImpl.cpp | 32 ++++++++++---- 2 files changed, 54 insertions(+), 22 deletions(-) diff --git a/src/platform/silabs/efr32/BLEManagerImpl.cpp b/src/platform/silabs/efr32/BLEManagerImpl.cpp index 44c90cc460e638..1fd5f7720c7682 100644 --- a/src/platform/silabs/efr32/BLEManagerImpl.cpp +++ b/src/platform/silabs/efr32/BLEManagerImpl.cpp @@ -535,11 +535,12 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) CHIP_ERROR BLEManagerImpl::StartAdvertising(void) { - CHIP_ERROR err; - sl_status_t ret; - uint32_t interval_min; - uint32_t interval_max; + CHIP_ERROR err = CHIP_NO_ERROR; + sl_status_t ret = SL_STATUS_OK; + uint32_t interval_min = 0; + uint32_t interval_max = 0; uint16_t numConnectionss = NumConnections(); + bool postAdvChangeEvent = false; uint8_t connectableAdv = (numConnectionss < kMaxConnections) ? sl_bt_advertiser_connectable_scannable : sl_bt_advertiser_scannable_non_connectable; @@ -551,6 +552,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) else { ChipLogDetail(DeviceLayer, "Start BLE advertisement"); + postAdvChangeEvent = true; } err = ConfigureAdvertisingData(); @@ -589,18 +591,26 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) SuccessOrExit(err); sl_bt_advertiser_configure(advertising_set_handle, 1); + ret = sl_bt_legacy_advertiser_start(advertising_set_handle, connectableAdv); + err = MapBLEError(ret); + SuccessOrExit(err); - if (SL_STATUS_OK == ret) + if (mFlags.Has(Flags::kFastAdvertisingEnabled)) { - if (mFlags.Has(Flags::kFastAdvertisingEnabled)) - { - StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME); - } - mFlags.Set(Flags::kAdvertising); + StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME); } + mFlags.Set(Flags::kAdvertising); - err = MapBLEError(ret); + if (postAdvChangeEvent) + { + // Post CHIPoBLEAdvertisingChange event. + ChipDeviceEvent advChange; + advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; + advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started; + + ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange)); + } exit: return err; @@ -609,10 +619,11 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) CHIP_ERROR BLEManagerImpl::StopAdvertising(void) { CHIP_ERROR err = CHIP_NO_ERROR; - sl_status_t ret; if (mFlags.Has(Flags::kAdvertising)) { + sl_status_t ret = SL_STATUS_OK; + mFlags.Clear(Flags::kAdvertising).Clear(Flags::kRestartAdvertising); mFlags.Set(Flags::kFastAdvertisingEnabled, true); @@ -620,12 +631,17 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void) sl_bt_advertiser_delete_set(advertising_set_handle); advertising_set_handle = 0xff; err = MapBLEError(ret); - SuccessOrExit(err); + VerifyOrReturnError(err == CHIP_NO_ERROR, err); CancelBleAdvTimeoutTimer(); + + // Post CHIPoBLEAdvertisingChange event. + ChipDeviceEvent advChange; + advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; + advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped; + err = PlatformMgr().PostEvent(&advChange); } -exit: return err; } diff --git a/src/platform/silabs/rs911x/BLEManagerImpl.cpp b/src/platform/silabs/rs911x/BLEManagerImpl.cpp index 53fd807519cf43..4ce7ef6973b553 100644 --- a/src/platform/silabs/rs911x/BLEManagerImpl.cpp +++ b/src/platform/silabs/rs911x/BLEManagerImpl.cpp @@ -678,8 +678,9 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) CHIP_ERROR BLEManagerImpl::StartAdvertising(void) { - CHIP_ERROR err = CHIP_NO_ERROR; - int32_t status = 0; + CHIP_ERROR err = CHIP_NO_ERROR; + int32_t status = 0; + bool postAdvChangeEvent = false; ChipLogProgress(DeviceLayer, "StartAdvertising start"); @@ -695,6 +696,7 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) else { ChipLogDetail(DeviceLayer, "Start BLE advertisement"); + postAdvChangeEvent = true; } if (!(mFlags.Has(Flags::kAdvertising))) @@ -719,6 +721,16 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME); } mFlags.Set(Flags::kAdvertising); + + if (postAdvChangeEvent) + { + // Post CHIPoBLEAdvertisingChange event. + ChipDeviceEvent advChange; + advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; + advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Started; + + ReturnErrorOnFailure(PlatformMgr().PostEvent(&advChange)); + } } else { @@ -726,8 +738,9 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) } exit: + // TODO: Add MapBLEError to return the correct error code ChipLogError(DeviceLayer, "StartAdvertising() End error: %s", ErrorStr(err)); - return CHIP_NO_ERROR; // err; + return err; } int32_t BLEManagerImpl::SendBLEAdvertisementCommand(void) @@ -756,7 +769,6 @@ int32_t BLEManagerImpl::SendBLEAdvertisementCommand(void) return rsi_ble_start_advertising_with_values(&ble_adv); } -// TODO:: Implementation need to be done. CHIP_ERROR BLEManagerImpl::StopAdvertising(void) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -772,12 +784,16 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void) mFlags.Set(Flags::kFastAdvertisingEnabled, true); advertising_set_handle = 0xff; CancelBleAdvTimeoutTimer(); - } - else - { - ChipLogProgress(DeviceLayer, "advertising failed to stop, with status = 0x%lx", status); + + // Post CHIPoBLEAdvertisingChange event. + ChipDeviceEvent advChange; + advChange.Type = DeviceEventType::kCHIPoBLEAdvertisingChange; + advChange.CHIPoBLEAdvertisingChange.Result = kActivity_Stopped; + err = PlatformMgr().PostEvent(&advChange); } } + + // TODO: Add MapBLEError to return the correct error code return err; }