Skip to content

Commit b673e41

Browse files
[nrfconnect] Fix BLE random static address generation (project-chip#13218)
1 parent 026d755 commit b673e41

File tree

6 files changed

+30
-53
lines changed

6 files changed

+30
-53
lines changed

config/nrfconnect/app/overlay-bt_private_addresses.conf

-22
This file was deleted.

examples/lighting-app/nrfconnect/CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ if (EXISTS boards/${BOARD}.conf)
3030
list(APPEND CONF_FILE boards/${BOARD}.conf)
3131
endif()
3232

33-
# TODO: temporary fix to remove after solving static addressing problem on nrf5340
34-
if(BOARD STREQUAL "nrf5340dk_nrf5340_cpuapp")
35-
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
36-
endif()
37-
3833
option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
3934
if(BUILD_WITH_DFU)
4035
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-ota_requestor.conf)

examples/lock-app/nrfconnect/CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
2929
list(APPEND CONF_FILE boards/${BOARD}.conf)
3030
endif()
3131

32-
# TODO: temporary fix to remove after solving static addressing problem on nrf5340
33-
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
34-
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
35-
endif()
36-
3732
option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
3833
if(BUILD_WITH_DFU)
3934
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")

examples/pump-app/nrfconnect/CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
2929
list(APPEND CONF_FILE boards/${BOARD}.conf)
3030
endif()
3131

32-
# TODO: temporary fix to remove after solving static addressing problem on nrf5340
33-
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
34-
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
35-
endif()
36-
3732
option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
3833
if(BUILD_WITH_DFU)
3934
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")

examples/pump-controller-app/nrfconnect/CMakeLists.txt

-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
2929
list(APPEND CONF_FILE boards/${BOARD}.conf)
3030
endif()
3131

32-
# TODO: temporary fix to remove after solving static addressing problem on nrf5340
33-
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
34-
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
35-
endif()
36-
3732
option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
3833
if(BUILD_WITH_DFU)
3934
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")

src/platform/Zephyr/BLEManagerImpl.cpp

+30-11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <bluetooth/addr.h>
3636
#include <bluetooth/gatt.h>
3737
#include <logging/log.h>
38+
#include <random/rand32.h>
3839
#include <sys/byteorder.h>
3940
#include <sys/util.h>
4041

@@ -85,22 +86,40 @@ struct bt_gatt_service sChipoBleService = BT_GATT_SERVICE(sChipoBleAttributes);
8586
// This value should be adjusted accordingly if the service declaration changes.
8687
constexpr int kCHIPoBLE_CCC_AttributeIndex = 3;
8788

88-
void InitRandomStaticAddress()
89+
CHIP_ERROR InitRandomStaticAddress()
8990
{
90-
#if !CONFIG_BT_PRIVACY
91-
// When the BT privacy feature is disabled, generate a random static address once per boot.
92-
// This must be done before bt_enable() has been called.
91+
// Generate a random static address for the default identity.
92+
// This must be done before bt_enable() as after that updating the default identity is not possible.
93+
int error = 0;
9394
bt_addr_le_t addr;
9495

95-
int error = bt_addr_le_create_static(&addr);
96-
VerifyOrReturn(error == 0, ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error));
96+
#if CONFIG_BT_HOST_CRYPTO
97+
// When CONFIG_BT_HOST_CRYPTO is enabled, bt_addr_le_create_static() depends on HCI transport
98+
// which is not yet started at this point, so use a different method for generating the address
99+
addr.type = BT_ADDR_LE_RANDOM;
100+
error = sys_csrand_get(addr.a.val, sizeof(addr.a.val));
101+
BT_ADDR_SET_STATIC(&addr.a);
102+
#else
103+
error = bt_addr_le_create_static(&addr);
104+
#endif
105+
106+
if (error)
107+
{
108+
ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error);
109+
return System::MapErrorZephyr(error);
110+
}
97111

98112
error = bt_id_create(&addr, nullptr);
99-
VerifyOrReturn(error == 0, ChipLogError(DeviceLayer, "Failed to create BLE identity: %d", error));
100113

101-
ChipLogProgress(DeviceLayer, "BLE address was set to %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4],
102-
addr.a.val[3], addr.a.val[2], addr.a.val[1], addr.a.val[0]);
103-
#endif
114+
if (error)
115+
{
116+
ChipLogError(DeviceLayer, "Failed to create BLE identity: %d", error);
117+
return System::MapErrorZephyr(error);
118+
}
119+
120+
ChipLogProgress(DeviceLayer, "BLE address: %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4], addr.a.val[3],
121+
addr.a.val[2], addr.a.val[1], addr.a.val[0]);
122+
return CHIP_NO_ERROR;
104123
}
105124

106125
} // unnamed namespace
@@ -116,7 +135,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
116135

117136
memset(mSubscribedConns, 0, sizeof(mSubscribedConns));
118137

119-
InitRandomStaticAddress();
138+
ReturnErrorOnFailure(InitRandomStaticAddress());
120139
int err = bt_enable(NULL);
121140
VerifyOrReturnError(err == 0, MapErrorZephyr(err));
122141

0 commit comments

Comments
 (0)