Skip to content

Commit

Permalink
[ESP32] Set LE Random Address for BLE Advertising (#20301)
Browse files Browse the repository at this point in the history
ESP32-C3 devices were advertising with the same address on every boot.
Because of this even with different discriminator chip-tool was
connecting with the random device.

These changes make sure that we set the random address for advertising
  • Loading branch information
shubhamdp authored and pull[bot] committed Sep 30, 2023
1 parent acd35c6 commit 1103621
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/platform/ESP32/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ class BLEManagerImpl final : public BLEManager,
bool UnsetSubscribed(uint16_t conId);
bool IsSubscribed(uint16_t conId);

static CHIP_ERROR bleprph_set_random_addr(void);
static void bleprph_host_task(void * param);
static void bleprph_on_sync(void);
static void bleprph_on_reset(int);
Expand Down
38 changes: 33 additions & 5 deletions src/platform/ESP32/nimble/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ const ble_uuid128_t UUID_CHIPoBLEChar_C3 = {

SemaphoreHandle_t semaphoreHandle = NULL;

// LE Random Device Address
// (see Bluetooth® Core Specification 4.2 Vol 6, Part B, Section 1.3.2.1 "Static device address")
uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;

} // unnamed namespace

BLEManagerImpl BLEManagerImpl::sInstance;
Expand Down Expand Up @@ -589,6 +593,34 @@ void BLEManagerImpl::bleprph_on_reset(int reason)
ESP_LOGE(TAG, "Resetting state; reason=%d\n", reason);
}

CHIP_ERROR BLEManagerImpl::bleprph_set_random_addr(void)
{
ble_addr_t addr;

// Generates a new static random address
int rc = ble_hs_id_gen_rnd(0, &addr);
if (rc != 0)
{
ESP_LOGE(TAG, "Failed to generate random address err: %d", rc);
return CHIP_ERROR_INTERNAL;
}
// Set generated address
rc = ble_hs_id_set_rnd(addr.val);
if (rc != 0)
{
ESP_LOGE(TAG, "Failed to set random address err: %d", rc);
return CHIP_ERROR_INTERNAL;
}
// Try to configure the device with random static address
rc = ble_hs_util_ensure_addr(1);
if (rc != 0)
{
ESP_LOGE(TAG, "Failed to configure random address err: %d", rc);
return CHIP_ERROR_INTERNAL;
}
return CHIP_NO_ERROR;
}

void BLEManagerImpl::bleprph_on_sync(void)
{
ESP_LOGI(TAG, "BLE host-controller synced");
Expand Down Expand Up @@ -665,6 +697,7 @@ CHIP_ERROR BLEManagerImpl::InitESPBleLayer(void)
sInstance.mFlags.Set(Flags::kESPBLELayerInitialized);
sInstance.mFlags.Set(Flags::kGATTServiceStarted);

err = bleprph_set_random_addr();
exit:
return err;
}
Expand Down Expand Up @@ -1160,11 +1193,6 @@ CHIP_ERROR BLEManagerImpl::StartAdvertising(void)
CHIP_ERROR err;
ble_gap_adv_params adv_params;
memset(&adv_params, 0, sizeof(adv_params));
#ifdef CONFIG_BT_NIMBLE_HOST_BASED_PRIVACY
uint8_t own_addr_type = BLE_OWN_ADDR_RANDOM;
#else
uint8_t own_addr_type = BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT;
#endif
adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN;

mFlags.Clear(Flags::kAdvertisingRefreshNeeded);
Expand Down

0 comments on commit 1103621

Please sign in to comment.