Skip to content

Commit 511c974

Browse files
[Silabs] Change BLE address from random resolvable address to random static address (#33035)
* Change BLE address from random resolvable address to static address * add comment * Replace lambda function with memcmp
1 parent c6fbf7c commit 511c974

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

src/platform/silabs/efr32/BLEManagerImpl.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ extern "C" {
4040
#include "sl_bt_stack_init.h"
4141
#include "timers.h"
4242
#include <ble/CHIPBleServiceData.h>
43+
#include <crypto/RandUtils.h>
44+
#include <cstring>
4345
#include <lib/support/CodeUtils.h>
4446
#include <lib/support/logging/CHIPLogging.h>
4547
#include <platform/CommissionableDataProvider.h>
@@ -106,17 +108,16 @@ const ChipBleUUID ChipUUID_CHIPoBLEChar_RX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0
106108
const ChipBleUUID ChipUUID_CHIPoBLEChar_TX = { { 0x18, 0xEE, 0x2E, 0xF5, 0x26, 0x3D, 0x45, 0x59, 0x95, 0x9F, 0x4F, 0x9C, 0x42, 0x9F,
107109
0x9D, 0x12 } };
108110

111+
bd_addr randomizedAddr = { 0 };
112+
109113
} // namespace
110114

111115
BLEManagerImpl BLEManagerImpl::sInstance;
112116

113117
CHIP_ERROR BLEManagerImpl::_Init()
114118
{
115-
CHIP_ERROR err;
116-
117119
// Initialize the CHIP BleLayer.
118-
err = BleLayer::Init(this, this, &DeviceLayer::SystemLayer());
119-
SuccessOrExit(err);
120+
ReturnErrorOnFailure(BleLayer::Init(this, this, &DeviceLayer::SystemLayer()));
120121

121122
memset(mBleConnections, 0, sizeof(mBleConnections));
122123
memset(mIndConfId, kUnusedIndex, sizeof(mIndConfId));
@@ -132,10 +133,23 @@ CHIP_ERROR BLEManagerImpl::_Init()
132133

133134
mFlags.ClearAll().Set(Flags::kAdvertisingEnabled, CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART);
134135
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
135-
PlatformMgr().ScheduleWork(DriveBLEState, 0);
136136

137-
exit:
138-
return err;
137+
// Check that an address was not already configured at boot.
138+
// This covers the init-shutdown-init case to comply with the BLE address change at boot only requirement
139+
bd_addr temp = { 0 };
140+
if (memcmp(&randomizedAddr, &temp, sizeof(bd_addr)) == 0)
141+
{
142+
// Since a random address is not configured, configure one
143+
uint64_t random = Crypto::GetRandU64();
144+
// Copy random value to address. We don't care of the ordering since it's a random value.
145+
memcpy(&randomizedAddr, &random, sizeof(randomizedAddr));
146+
147+
// Set two MSBs to 11 to properly the address - BLE Static Device Address requirement
148+
randomizedAddr.addr[5] |= 0xC0;
149+
}
150+
151+
PlatformMgr().ScheduleWork(DriveBLEState, 0);
152+
return CHIP_NO_ERROR;
139153
}
140154

141155
uint16_t BLEManagerImpl::_NumConnections(void)
@@ -484,14 +498,13 @@ CHIP_ERROR BLEManagerImpl::ConfigureAdvertisingData(void)
484498
ChipLogError(DeviceLayer, "sl_bt_advertiser_create_set() failed: %s", ErrorStr(err));
485499
});
486500

487-
bd_addr randomizedAddr = {};
488-
ret = sl_bt_advertiser_set_random_address(advertising_set_handle, sl_bt_gap_random_resolvable_address, randomizedAddr,
489-
&randomizedAddr);
501+
ret =
502+
sl_bt_advertiser_set_random_address(advertising_set_handle, sl_bt_gap_static_address, randomizedAddr, &randomizedAddr);
490503
VerifyOrExit(ret == SL_STATUS_OK, {
491504
err = MapBLEError(ret);
492505
ChipLogError(DeviceLayer, "sl_bt_advertiser_set_random_address() failed: %s", ErrorStr(err));
493506
});
494-
ChipLogDetail(DeviceLayer, "BLE Resolvable private random address %02X:%02X:%02X:%02X:%02X:%02X", randomizedAddr.addr[5],
507+
ChipLogDetail(DeviceLayer, "BLE Static Device Address %02X:%02X:%02X:%02X:%02X:%02X", randomizedAddr.addr[5],
495508
randomizedAddr.addr[4], randomizedAddr.addr[3], randomizedAddr.addr[2], randomizedAddr.addr[1],
496509
randomizedAddr.addr[0]);
497510
}
@@ -625,6 +638,8 @@ CHIP_ERROR BLEManagerImpl::StopAdvertising(void)
625638
mFlags.Set(Flags::kFastAdvertisingEnabled, true);
626639

627640
ret = sl_bt_advertiser_stop(advertising_set_handle);
641+
sl_bt_advertiser_clear_random_address(advertising_set_handle);
642+
628643
sl_bt_advertiser_delete_set(advertising_set_handle);
629644
advertising_set_handle = 0xff;
630645
err = MapBLEError(ret);

0 commit comments

Comments
 (0)