Skip to content

Commit

Permalink
[nrfconnect] Fix BLE random static address generation (#13218)
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian-Nordic authored and pull[bot] committed Apr 13, 2022
1 parent 3472e36 commit 1040720
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 53 deletions.
22 changes: 0 additions & 22 deletions config/nrfconnect/app/overlay-bt_private_addresses.conf

This file was deleted.

5 changes: 0 additions & 5 deletions examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@ if (EXISTS boards/${BOARD}.conf)
list(APPEND CONF_FILE boards/${BOARD}.conf)
endif()

# TODO: temporary fix to remove after solving static addressing problem on nrf5340
if(BOARD STREQUAL "nrf5340dk_nrf5340_cpuapp")
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
endif()

option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
if(BUILD_WITH_DFU)
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-ota_requestor.conf)
Expand Down
5 changes: 0 additions & 5 deletions examples/lock-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
list(APPEND CONF_FILE boards/${BOARD}.conf)
endif()

# TODO: temporary fix to remove after solving static addressing problem on nrf5340
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
endif()

option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
if(BUILD_WITH_DFU)
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
Expand Down
5 changes: 0 additions & 5 deletions examples/pump-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
list(APPEND CONF_FILE boards/${BOARD}.conf)
endif()

# TODO: temporary fix to remove after solving static addressing problem on nrf5340
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
endif()

option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
if(BUILD_WITH_DFU)
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
Expand Down
5 changes: 0 additions & 5 deletions examples/pump-controller-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ if (EXISTS boards/${BOARD}.conf)
list(APPEND CONF_FILE boards/${BOARD}.conf)
endif()

# TODO: temporary fix to remove after solving static addressing problem on nrf5340
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
endif()

option(BUILD_WITH_DFU "Build target with Device Firmware Upgrade support" OFF)
if(BUILD_WITH_DFU)
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
Expand Down
41 changes: 30 additions & 11 deletions src/platform/Zephyr/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include <bluetooth/addr.h>
#include <bluetooth/gatt.h>
#include <logging/log.h>
#include <random/rand32.h>
#include <sys/byteorder.h>
#include <sys/util.h>

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

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

int error = bt_addr_le_create_static(&addr);
VerifyOrReturn(error == 0, ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error));
#if CONFIG_BT_HOST_CRYPTO
// When CONFIG_BT_HOST_CRYPTO is enabled, bt_addr_le_create_static() depends on HCI transport
// which is not yet started at this point, so use a different method for generating the address
addr.type = BT_ADDR_LE_RANDOM;
error = sys_csrand_get(addr.a.val, sizeof(addr.a.val));
BT_ADDR_SET_STATIC(&addr.a);
#else
error = bt_addr_le_create_static(&addr);
#endif

if (error)
{
ChipLogError(DeviceLayer, "Failed to create BLE address: %d", error);
return System::MapErrorZephyr(error);
}

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

ChipLogProgress(DeviceLayer, "BLE address was set to %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4],
addr.a.val[3], addr.a.val[2], addr.a.val[1], addr.a.val[0]);
#endif
if (error)
{
ChipLogError(DeviceLayer, "Failed to create BLE identity: %d", error);
return System::MapErrorZephyr(error);
}

ChipLogProgress(DeviceLayer, "BLE address: %02X:%02X:%02X:%02X:%02X:%02X", addr.a.val[5], addr.a.val[4], addr.a.val[3],
addr.a.val[2], addr.a.val[1], addr.a.val[0]);
return CHIP_NO_ERROR;
}

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

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

InitRandomStaticAddress();
ReturnErrorOnFailure(InitRandomStaticAddress());
int err = bt_enable(NULL);
VerifyOrReturnError(err == 0, MapErrorZephyr(err));

Expand Down

0 comments on commit 1040720

Please sign in to comment.