From 4addf73fb3ca32c8253cfee14da96b5ef0cdbc52 Mon Sep 17 00:00:00 2001 From: Mathieu Kardous Date: Wed, 17 Apr 2024 14:20:34 -0500 Subject: [PATCH 1/3] Change BLE address from random resolvable address to static address --- src/platform/silabs/efr32/BLEManagerImpl.cpp | 26 +++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/src/platform/silabs/efr32/BLEManagerImpl.cpp b/src/platform/silabs/efr32/BLEManagerImpl.cpp index 64bab16f2e249a..1e893b99719854 100644 --- a/src/platform/silabs/efr32/BLEManagerImpl.cpp +++ b/src/platform/silabs/efr32/BLEManagerImpl.cpp @@ -39,7 +39,9 @@ extern "C" { #include "sl_bt_stack_config.h" #include "sl_bt_stack_init.h" #include "timers.h" +#include #include +#include #include #include #include @@ -106,6 +108,8 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0 const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F, 0x9D, 0x12 } }; +bd_addr randomizedAddr = { 0 }; + } // namespace BLEManagerImpl BLEManagerImpl::sInstance; @@ -132,6 +136,19 @@ CHIP_ERROR BLEManagerImpl::_Init() mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART); mFlags.Set(Flags::kFastAdvertisingEnabled, true); + + if (std::all_of(randomizedAddr.addr, randomizedAddr.addr + (sizeof(randomizedAddr.addr) / sizeof(uint8_t)), + [](uint8_t i) { return i == 0; })) + { + // Since random address configured, generate one + // Copy random value to address. We don't care of the ordering since it's a random value. + uint64_t random = Crypto::GetRandU64(); + memcpy(&randomizedAddr, &random, sizeof(randomizedAddr)); + + // Set MSB to 11 to properly - BLE Static Device Address requirement + randomizedAddr.addr[5] |= 0xC0; + } + PlatformMgr().ScheduleWork(DriveBLEState, 0); exit: @@ -484,14 +501,13 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void) ChipLogError(DeviceLayer, "sl_bt_advertiser_create_set() failed: %s", ErrorStr(err)); }); - bd_addr randomizedAddr = {}; - ret = sl_bt_advertiser_set_random_address(advertising_set_handle, sl_bt_gap_random_resolvable_address, randomizedAddr, - &randomizedAddr); + ret = + sl_bt_advertiser_set_random_address(advertising_set_handle, sl_bt_gap_static_address, randomizedAddr, &randomizedAddr); VerifyOrExit(ret == SL_STATUS_OK, { err = MapBLEError(ret); ChipLogError(DeviceLayer, "sl_bt_advertiser_set_random_address() failed: %s", ErrorStr(err)); }); - ChipLogDetail(DeviceLayer, "BLE Resolvable private random address %02X:%02X:%02X:%02X:%02X:%02X", randomizedAddr.addr[5], + ChipLogDetail(DeviceLayer, "BLE Static Device Address %02X:%02X:%02X:%02X:%02X:%02X", randomizedAddr.addr[5], randomizedAddr.addr[4], randomizedAddr.addr[3], randomizedAddr.addr[2], randomizedAddr.addr[1], randomizedAddr.addr[0]); } @@ -625,6 +641,8 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void) mFlags.Set(Flags::kFastAdvertisingEnabled, true); ret = sl_bt_advertiser_stop(advertising_set_handle); + sl_bt_advertiser_clear_random_address(advertising_set_handle); + sl_bt_advertiser_delete_set(advertising_set_handle); advertising_set_handle = 0xff; err = MapBLEError(ret); From 2f5f8bdbdbc4aba62854e54f49921619b547d288 Mon Sep 17 00:00:00 2001 From: Mathieu Kardous Date: Wed, 17 Apr 2024 14:25:17 -0500 Subject: [PATCH 2/3] add comment --- src/platform/silabs/efr32/BLEManagerImpl.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/platform/silabs/efr32/BLEManagerImpl.cpp b/src/platform/silabs/efr32/BLEManagerImpl.cpp index 1e893b99719854..38536120a820f3 100644 --- a/src/platform/silabs/efr32/BLEManagerImpl.cpp +++ b/src/platform/silabs/efr32/BLEManagerImpl.cpp @@ -137,15 +137,17 @@ CHIP_ERROR BLEManagerImpl::_Init() mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART); mFlags.Set(Flags::kFastAdvertisingEnabled, true); + // Check that an address was not already configured at boot. + // This covers the init-shutdown-init case to comply with the BLE address change at boot only requirement if (std::all_of(randomizedAddr.addr, randomizedAddr.addr + (sizeof(randomizedAddr.addr) / sizeof(uint8_t)), [](uint8_t i) { return i == 0; })) { - // Since random address configured, generate one - // Copy random value to address. We don't care of the ordering since it's a random value. + // Since a random address is not configured, configure one uint64_t random = Crypto::GetRandU64(); + // Copy random value to address. We don't care of the ordering since it's a random value. memcpy(&randomizedAddr, &random, sizeof(randomizedAddr)); - // Set MSB to 11 to properly - BLE Static Device Address requirement + // Set two MSBs to 11 to properly the address - BLE Static Device Address requirement randomizedAddr.addr[5] |= 0xC0; } From 21402549bec78e824f8fac8672ff9ad545e5d994 Mon Sep 17 00:00:00 2001 From: Mathieu Kardous Date: Wed, 17 Apr 2024 16:12:48 -0500 Subject: [PATCH 3/3] Replace lambda function with memcmp --- src/platform/silabs/efr32/BLEManagerImpl.cpp | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/platform/silabs/efr32/BLEManagerImpl.cpp b/src/platform/silabs/efr32/BLEManagerImpl.cpp index 38536120a820f3..1f82f475c8bb4f 100644 --- a/src/platform/silabs/efr32/BLEManagerImpl.cpp +++ b/src/platform/silabs/efr32/BLEManagerImpl.cpp @@ -39,9 +39,9 @@ extern "C" { #include "sl_bt_stack_config.h" #include "sl_bt_stack_init.h" #include "timers.h" -#include #include #include +#include #include #include #include @@ -116,11 +116,8 @@ BLEManagerImpl BLEManagerImpl::sInstance; CHIP_ERROR BLEManagerImpl::_Init() { - CHIP_ERROR err; - // Initialize the CHIP BleLayer. - err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer()); - SuccessOrExit(err); + ReturnErrorOnFailure(BleLayer::Init(this, this, &DeviceLayer::SystemLayer())); memset(mBleConnections, 0, sizeof(mBleConnections)); memset(mIndConfId, kUnusedIndex, sizeof(mIndConfId)); @@ -139,8 +136,8 @@ CHIP_ERROR BLEManagerImpl::_Init() // Check that an address was not already configured at boot. // This covers the init-shutdown-init case to comply with the BLE address change at boot only requirement - if (std::all_of(randomizedAddr.addr, randomizedAddr.addr + (sizeof(randomizedAddr.addr) / sizeof(uint8_t)), - [](uint8_t i) { return i == 0; })) + bd_addr temp = { 0 }; + if (memcmp(&randomizedAddr, &temp, sizeof(bd_addr)) == 0) { // Since a random address is not configured, configure one uint64_t random = Crypto::GetRandU64(); @@ -152,9 +149,7 @@ CHIP_ERROR BLEManagerImpl::_Init() } PlatformMgr().ScheduleWork(DriveBLEState, 0); - -exit: - return err; + return CHIP_NO_ERROR; } uint16_t BLEManagerImpl::_NumConnections(void)