From 854bbf308124c30947af65bcdf4136d5c92feb83 Mon Sep 17 00:00:00 2001 From: cnanoleaf <133412959+cnanoleaf@users.noreply.github.com> Date: Fri, 19 Jan 2024 22:49:07 -0500 Subject: [PATCH] [nrf fromtree][TCR] Extended Announcement additions (#31532) * additions to support changes as discussed in connectedhomeip-spec #7825 * add Silabs platform example implementation * add extension for ExtendedAnnouncementFlag + Silabs platform usage * Restyled by clang-format * adjust based on review comments - 1 * Restyled by clang-format * add missing include guard for default case * Revert "Restyled by clang-format" This reverts commit 6d3f9e107de9a8421524437f44c2113730e52e54. * fix extra bracket causing a bug + restyle changes * Restyled by clang-format * adjust based on review comments - 2 * Restyled by clang-format --------- Cherry picked from: ca17912 Co-authored-by: Restyled.io --- src/app/server/CommissioningWindowManager.h | 5 ++ src/ble/CHIPBleServiceData.h | 14 +++++ src/include/platform/CHIPDeviceConfig.h | 55 ++++++++++++++++++++ src/platform/silabs/BLEManagerImpl.h | 1 + src/platform/silabs/efr32/BLEManagerImpl.cpp | 38 ++++++++++++++ 5 files changed, 113 insertions(+) diff --git a/src/app/server/CommissioningWindowManager.h b/src/app/server/CommissioningWindowManager.h index df9b1dcd5e..26a2f9ceb1 100644 --- a/src/app/server/CommissioningWindowManager.h +++ b/src/app/server/CommissioningWindowManager.h @@ -59,8 +59,13 @@ class CommissioningWindowManager : public Messaging::UnsolicitedMessageHandler, static constexpr System::Clock::Seconds16 MaxCommissioningTimeout() { +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + // Specification section 2.3.1 - Extended Announcement Duration up to 48h + return System::Clock::Seconds16(60 * 60 * 48); +#else // Specification section 5.4.2.3. Announcement Duration says 15 minutes. return System::Clock::Seconds16(15 * 60); +#endif } System::Clock::Seconds16 MinCommissioningTimeout() const diff --git a/src/ble/CHIPBleServiceData.h b/src/ble/CHIPBleServiceData.h index 965ae74307..6fbee69b7c 100644 --- a/src/ble/CHIPBleServiceData.h +++ b/src/ble/CHIPBleServiceData.h @@ -47,6 +47,7 @@ struct ChipBLEDeviceIdentificationInfo { constexpr static uint16_t kDiscriminatorMask = 0xfff; constexpr static uint8_t kAdditionalDataFlagMask = 0x1; + constexpr static uint8_t kExtendedAnnouncementFlagMask = 0x2; constexpr static uint8_t kAdvertisementVersionMask = 0xf0; constexpr static uint8_t kAdvertisementVersionShiftBits = 4u; @@ -112,6 +113,19 @@ struct ChipBLEDeviceIdentificationInfo AdditionalDataFlag &= static_cast(~kAdditionalDataFlagMask); } } + + void SetExtendedAnnouncementFlag(bool flag) + { + if (flag) + { + AdditionalDataFlag |= kExtendedAnnouncementFlagMask; + } + else + { + AdditionalDataFlag &= static_cast(~kExtendedAnnouncementFlagMask); + } + } + } __attribute__((packed)); } /* namespace Ble */ diff --git a/src/include/platform/CHIPDeviceConfig.h b/src/include/platform/CHIPDeviceConfig.h index e29c001613..b992b4c659 100644 --- a/src/include/platform/CHIPDeviceConfig.h +++ b/src/include/platform/CHIPDeviceConfig.h @@ -595,6 +595,61 @@ #define CHIP_DEVICE_CONFIG_BLE_ADVERTISING_INTERVAL_CHANGE_TIME 30000 #endif +/** + * CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + * + * Optional configuration to enable Extended Announcement Duration up to 48h. + * Should be used together with extending CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS past 15 minutes. + * Disabled by default. + */ + +#ifndef CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING +#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING 0 +#endif + +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + +/** + * CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS + * + * The amount of time in miliseconds after which BLE advertisement should be switched from the slow + * advertising to the extended advertising, counting from the moment of advertisement commencement. + * + * Defaults to 900000 ms. + */ +#ifndef CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS +#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS (15 * 60 * 1000) +#endif + +/** + * CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN + * + * The minimum interval (in units of 0.625ms) at which the device will send BLE advertisements while + * in the extended advertising mode. The minimum interval shall not be smaller than the default value + * and should not be equal to the CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX. + * + * Defaults to 1920 (1200 ms). + */ +#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN 1920 + +/** + * CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX + * + * The maximum interval (in units of 0.625ms) at which the device will send BLE advertisements while + * in the extended advertising mode. The maximum interval should be greater and not equal to the + * CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN. + * + * Defaults to 1936 (1210 ms). + */ +#ifndef CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX +#define CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX 1936 +#endif + +static_assert(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN < CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX, + "Max Extended Advertising Interval cannot be smaller or equal to the Min Extended Advertising Interval"); + +#endif + // -------------------- Service Provisioning Configuration -------------------- /** diff --git a/src/platform/silabs/BLEManagerImpl.h b/src/platform/silabs/BLEManagerImpl.h index b13f9fb290..8d9a986e50 100644 --- a/src/platform/silabs/BLEManagerImpl.h +++ b/src/platform/silabs/BLEManagerImpl.h @@ -152,6 +152,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla kRestartAdvertising = 0x0008, kEFRBLEStackInitialized = 0x0010, kDeviceNameSet = 0x0020, + kExtAdvertisingEnabled = 0x0040, }; enum diff --git a/src/platform/silabs/efr32/BLEManagerImpl.cpp b/src/platform/silabs/efr32/BLEManagerImpl.cpp index f8130df8b8..3095636ee4 100644 --- a/src/platform/silabs/efr32/BLEManagerImpl.cpp +++ b/src/platform/silabs/efr32/BLEManagerImpl.cpp @@ -457,6 +457,17 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) advData[index++] = CHIP_ADV_DATA_TYPE_SERVICE_DATA; // AD type : Service Data advData[index++] = ShortUUID_CHIPoBLEService[0]; // AD value advData[index++] = ShortUUID_CHIPoBLEService[1]; + +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + // Check for extended advertisement interval and redact VID/PID if past the initial period. + if (mFlags.Has(Flags::kExtAdvertisingEnabled)) + { + mDeviceIdInfo.SetVendorId(0); + mDeviceIdInfo.SetProductId(0); + mDeviceIdInfo.SetExtendedAnnouncementFlag(true); + } +#endif + memcpy(&advData[index], (void *) &mDeviceIdInfo, mDeviceIdInfoLength); // AD value index += mDeviceIdInfoLength; @@ -553,8 +564,21 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void) } else { +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + if (!mFlags.Has(Flags::kExtAdvertisingEnabled)) + { + interval_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN; + interval_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX; + } + else + { + interval_min = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MIN; + interval_max = CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_MAX; + } +#else interval_min = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN; interval_max = CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX; +#endif } ret = sl_bt_advertiser_set_timing(advertising_set_handle, interval_min, interval_max, 0, 0); @@ -952,8 +976,22 @@ void BLEManagerImpl::BleAdvTimeoutHandler(TimerHandle_t xTimer) if (BLEMgrImpl().mFlags.Has(Flags::kFastAdvertisingEnabled)) { ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start slow advertissment"); + BLEMgrImpl().mFlags.Set(Flags::kAdvertising); + BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising); +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + BLEMgrImpl().mFlags.Clear(Flags::kExtAdvertisingEnabled); + BLEMgrImpl().StartBleAdvTimeoutTimer(CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING_INTERVAL_CHANGE_TIME_MS); +#endif + } +#if CHIP_DEVICE_CONFIG_BLE_EXT_ADVERTISING + else + { + ChipLogDetail(DeviceLayer, "bleAdv Timeout : Start extended advertisment"); + BLEMgrImpl().mFlags.Set(Flags::kAdvertising); + BLEMgrImpl().mFlags.Set(Flags::kExtAdvertisingEnabled); BLEMgr().SetAdvertisingMode(BLEAdvertisingMode::kSlowAdvertising); } +#endif } void BLEManagerImpl::CancelBleAdvTimeoutTimer(void)