Skip to content

Commit 8ee043c

Browse files
nimble/ll: Add VS HCI command to set public device address
The public device address set by this command is applied only after HCI Reset, thus it can be issues at any time. To revert back to original public address set address to 0x000000000000.
1 parent 4bf65ac commit 8ee043c

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

nimble/controller/include/controller/ble_ll_addr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525
#endif
2626

2727
int ble_ll_addr_init(void);
28+
int ble_ll_addr_public_set(const uint8_t *addr);
2829

2930
/* Address provider APIs - should be implemented by packages supporting
3031
* relevant APIs

nimble/controller/src/ble_ll_addr.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
2929
uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
3030

31+
static uint8_t g_dev_addr_hci[BLE_DEV_ADDR_LEN];
32+
33+
static bool
34+
is_addr_empty(const uint8_t *addr)
35+
{
36+
return memcmp(addr, BLE_ADDR_ANY, BLE_DEV_ADDR_LEN) == 0;
37+
}
38+
39+
int
40+
ble_ll_addr_public_set(const uint8_t *addr)
41+
{
42+
memcpy(g_dev_addr_hci, addr, BLE_DEV_ADDR_LEN);
43+
44+
return 0;
45+
}
46+
3147
int
3248
ble_ll_addr_init(void)
3349
{
@@ -43,10 +59,15 @@ ble_ll_addr_init(void)
4359
pub_dev_addr >>= 8;
4460
}
4561

46-
/* Set public address from provider API, if available */
62+
if (!is_addr_empty(g_dev_addr_hci)) {
63+
/* Use public address set externally */
64+
memcpy(g_dev_addr, g_dev_addr_hci, BLE_DEV_ADDR_LEN);
65+
} else {
66+
/* Set public address from provider API, if available */
4767
#if MYNEWT_API_ble_addr_provider_public
48-
ble_ll_addr_provide_public(g_dev_addr);
68+
ble_ll_addr_provide_public(g_dev_addr);
4969
#endif
70+
}
5071

5172
#if MYNEWT_VAL(BLE_LL_ADDR_INIT_RANDOM)
5273
/* Set random address from provider API, if available */

nimble/controller/src/ble_ll_hci_vs.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,26 @@ ble_ll_hci_vs_set_scan_cfg(uint16_t ocf, const uint8_t *cmdbuf, uint8_t cmdlen,
390390
}
391391
#endif
392392

393+
#if MYNEWT_VAL(BLE_LL_HCI_VS_WR_PUBLIC_ADDR)
394+
static int
395+
ble_ll_hci_vs_wr_public_addr(uint16_t ocf, const uint8_t *cmdbuf, uint8_t cmdlen,
396+
uint8_t *rspbuf, uint8_t *rsplen)
397+
{
398+
const struct ble_hci_vs_wr_public_addr_cp *cp = (const void *)cmdbuf;
399+
int rc;
400+
401+
if (cmdlen != sizeof(*cp)) {
402+
return BLE_ERR_INV_HCI_CMD_PARMS;
403+
}
404+
405+
if (ble_ll_addr_wr_public_addr(cp->addr)) {
406+
return BLE_ERR_UNSPECIFIED;
407+
}
408+
409+
return 0;
410+
}
411+
#endif
412+
393413
static struct ble_ll_hci_vs_cmd g_ble_ll_hci_vs_cmds[] = {
394414
BLE_LL_HCI_VS_CMD(BLE_HCI_OCF_VS_RD_STATIC_ADDR,
395415
ble_ll_hci_vs_rd_static_addr),
@@ -424,6 +444,10 @@ static struct ble_ll_hci_vs_cmd g_ble_ll_hci_vs_cmds[] = {
424444
BLE_LL_HCI_VS_CMD(BLE_HCI_OCF_VS_SET_SCAN_CFG,
425445
ble_ll_hci_vs_set_scan_cfg)
426446
#endif
447+
#if MYNEWT_VAL(BLE_LL_HCI_VS_WR_PUBLIC_ADDR)
448+
BLE_LL_HCI_VS_CMD(BLE_HCI_OCF_VS_WR_PUBLIC_ADDR,
449+
ble_ll_hci_vs_wr_public_addr)
450+
#endif
427451
};
428452

429453
static struct ble_ll_hci_vs_cmd *

nimble/controller/syscfg.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,14 @@ syscfg.defs:
456456
- BLE_LL_HCI_VS if 1
457457
- BLE_LL_CFG_FEAT_LL_EXT_ADV if 1
458458
- BLE_LL_ROLE_OBSERVER if 1
459-
459+
BLE_LL_HCI_VS_WR_PUBLIC_ADDR:
460+
description: >
461+
Enables HCI command to set public device address.
462+
The new address is applied after HCI Reset. To revert back to the
463+
default public address, set address to 0x000000000000.
464+
value: 0
465+
restrictions:
466+
- BLE_LL_HCI_VS if 1
460467

461468
BLE_LL_HCI_VS_EVENT_ON_ASSERT:
462469
description: >

nimble/include/nimble/hci_common.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,12 @@ struct ble_hci_vs_set_scan_cfg_cp {
14061406
int8_t rssi_threshold;
14071407
} __attribute__((packed));
14081408

1409+
/* Set Public Bluetooth Address */
1410+
#define BLE_HCI_OCF_VS_WR_PUBLIC_ADDR (MYNEWT_VAL(BLE_HCI_VS_OCF_OFFSET) + (0x000C))
1411+
struct ble_hci_vs_wr_public_addr_cp {
1412+
uint8_t addr[6];
1413+
} __attribute__((packed));
1414+
14091415
/* Command Specific Definitions */
14101416
/* --- Set controller to host flow control (OGF 0x03, OCF 0x0031) --- */
14111417
#define BLE_HCI_CTLR_TO_HOST_FC_OFF (0)

0 commit comments

Comments
 (0)