Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Set LE Random Address for BLE Advertising #20301

Merged
merged 1 commit into from
Jul 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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