Skip to content

Commit 7408f88

Browse files
nimble/ll: Add address provider APIs
This adds 2 new APIs that can be implemented by packages: provide public or static device address. This allows for better support of provisioning of Bluetooth devices address on different devices. The nrf5x driver can act as a provider of both public and static address which is configurable by syscfg settings. By default provider for a static address is enabled since all nRF5x have factory programmed random address in FICR registers. The provider for a public address can be optionally enabled since the same registers can be also be factory programmed with a public address (on request).
1 parent 273bd24 commit 7408f88

File tree

12 files changed

+87
-110
lines changed

12 files changed

+87
-110
lines changed

nimble/controller/include/controller/ble_hw.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ uint8_t ble_hw_resolv_list_size(void);
9797
/* Returns index of resolved address; -1 if not resolved */
9898
int ble_hw_resolv_list_match(void);
9999

100-
/* Returns public device address or -1 if not present */
101-
int ble_hw_get_public_addr(ble_addr_t *addr);
102-
103-
/* Returns random static address or -1 if not present */
104-
int ble_hw_get_static_addr(ble_addr_t *addr);
105-
106100
#ifdef __cplusplus
107101
}
108102
#endif

nimble/controller/include/controller/ble_ll_addr.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ extern "C" {
2626

2727
int ble_ll_addr_init(void);
2828

29+
/* Address provider APIs - should be implemented by packages supporting
30+
* relevant APIs
31+
*/
32+
int ble_ll_addr_provide_public(uint8_t *addr);
33+
int ble_ll_addr_provide_static(uint8_t *addr);
34+
2935
#ifdef __cplusplus
3036
}
3137
#endif

nimble/controller/src/ble_ll_addr.c

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,49 +20,33 @@
2020
#include <stdint.h>
2121
#include <syscfg/syscfg.h>
2222
#include <controller/ble_ll.h>
23-
#include <controller/ble_hw.h>
23+
#include <controller/ble_ll_addr.h>
2424

2525
/* FIXME: both should be static and accessible only via dedicated set/get APIs;
2626
* extern declared in nimble/ble.h should be removed
2727
*/
2828
uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
2929
uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
3030

31-
static bool
32-
ble_ll_addr_is_empty(const uint8_t *addr)
33-
{
34-
return memcmp(addr, BLE_ADDR_ANY, BLE_DEV_ADDR_LEN) == 0;
35-
}
36-
3731
int
3832
ble_ll_addr_init(void)
3933
{
40-
#if MYNEWT_VAL(BLE_LL_PUBLIC_DEV_ADDR)
4134
uint64_t pub_dev_addr;
4235
int i;
43-
#endif
44-
ble_addr_t addr;
45-
int rc;
4636

47-
/* Set public device address if not already set */
48-
if (ble_ll_addr_is_empty(g_dev_addr)) {
49-
#if MYNEWT_VAL(BLE_LL_PUBLIC_DEV_ADDR)
50-
pub_dev_addr = MYNEWT_VAL(BLE_LL_PUBLIC_DEV_ADDR);
37+
/* Set public device address from syscfg. It should be all-zero in normal
38+
* build so no need to add special check for that.
39+
*/
40+
pub_dev_addr = MYNEWT_VAL(BLE_LL_PUBLIC_DEV_ADDR);
41+
for (i = 0; i < BLE_DEV_ADDR_LEN; i++) {
42+
g_dev_addr[i] = pub_dev_addr & 0xff;
43+
pub_dev_addr >>= 8;
44+
}
5145

52-
for (i = 0; i < BLE_DEV_ADDR_LEN; i++) {
53-
g_dev_addr[i] = pub_dev_addr & 0xff;
54-
pub_dev_addr >>= 8;
55-
}
56-
#else
57-
memcpy(g_dev_addr, MYNEWT_VAL(BLE_PUBLIC_DEV_ADDR), BLE_DEV_ADDR_LEN);
46+
/* Set public address from provider API, if available */
47+
#if MYNEWT_API_ble_addr_provider_public
48+
ble_ll_addr_provide_public(g_dev_addr);
5849
#endif
59-
if (ble_ll_addr_is_empty(g_dev_addr)) {
60-
rc = ble_hw_get_public_addr(&addr);
61-
if (!rc) {
62-
memcpy(g_dev_addr, &addr.val[0], BLE_DEV_ADDR_LEN);
63-
}
64-
}
65-
}
6650

6751
return 0;
6852
}

nimble/controller/src/ble_ll_hci_vs.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*/
1919

2020
#include <stdint.h>
21+
#include <controller/ble_ll_addr.h>
22+
2123
#include "syscfg/syscfg.h"
2224
#include "controller/ble_ll_utils.h"
2325
#include "controller/ble_ll.h"
@@ -42,17 +44,18 @@ ble_ll_hci_vs_rd_static_addr(uint16_t ocf,
4244
uint8_t *rspbuf, uint8_t *rsplen)
4345
{
4446
struct ble_hci_vs_rd_static_addr_rp *rsp = (void *) rspbuf;
45-
ble_addr_t addr;
4647

4748
if (cmdlen != 0) {
4849
return BLE_ERR_INV_HCI_CMD_PARMS;
4950
}
5051

51-
if (ble_hw_get_static_addr(&addr) < 0) {
52+
#if MYNEWT_API_ble_addr_provider_static
53+
if (ble_ll_addr_provide_static(rsp->addr) < 0) {
5254
return BLE_ERR_UNSPECIFIED;
5355
}
54-
55-
memcpy(rsp->addr, addr.val, sizeof(rsp->addr));
56+
#else
57+
return BLE_ERR_UNSUPPORTED;
58+
#endif
5659

5760
*rsplen = sizeof(*rsp);
5861

nimble/drivers/dialog_cmac/src/ble_hw.c

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,6 @@ struct ble_hw_resolv_proc {
7777
static struct ble_hw_resolv_list g_ble_hw_resolv_list;
7878
static struct ble_hw_resolv_proc g_ble_hw_resolv_proc;
7979

80-
int
81-
ble_hw_get_public_addr(ble_addr_t *addr)
82-
{
83-
return -1;
84-
}
85-
86-
int
87-
ble_hw_get_static_addr(ble_addr_t *addr)
88-
{
89-
return -1;
90-
}
91-
9280
void
9381
ble_hw_whitelist_clear(void)
9482
{

nimble/drivers/native/src/ble_hw.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,6 @@ static uint8_t g_ble_hw_whitelist_mask;
3737
static ble_rng_isr_cb_t rng_cb;
3838
static bool rng_started;
3939

40-
/* Returns public device address or -1 if not present */
41-
int
42-
ble_hw_get_public_addr(ble_addr_t *addr)
43-
{
44-
return -1;
45-
}
46-
47-
/* Returns random static address or -1 if not present */
48-
int
49-
ble_hw_get_static_addr(ble_addr_t *addr)
50-
{
51-
return -1;
52-
}
53-
5440
/**
5541
* Clear the whitelist
5642
*

nimble/drivers/nrf51/pkg.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ pkg.keywords:
2525
- ble
2626
- bluetooth
2727

28-
pkg.apis: ble_driver
2928
pkg.deps:
3029
- nimble
3130
- nimble/controller
3231

32+
pkg.apis:
33+
- ble_driver
34+
pkg.apis.BLE_PHY_ADDR_PROVIDER_PUBLIC:
35+
- ble_addr_provider_public
36+
pkg.apis.BLE_PHY_ADDR_PROVIDER_STATIC:
37+
- ble_addr_provider_static
38+
3339
# allows to override nimble/controller settings
3440
pkg.subpriority: 1

nimble/drivers/nrf51/src/ble_hw.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,52 +62,47 @@ uint8_t g_nrf_num_irks;
6262

6363
#endif
6464

65-
/* Returns public device address or -1 if not present */
65+
#if MYNEWT_VAL(BLE_PHY_ADDR_PROVIDER_PUBLIC)
6666
int
67-
ble_hw_get_public_addr(ble_addr_t *addr)
67+
ble_ll_addr_provide_public(uint8_t *addr)
6868
{
6969
uint32_t addr_high;
7070
uint32_t addr_low;
7171

72-
/* Does FICR have a public address */
7372
if ((NRF_FICR->DEVICEADDRTYPE & 1) != 0) {
7473
return -1;
7574
}
7675

77-
/* Copy into device address. We can do this because we know platform */
7876
addr_low = NRF_FICR->DEVICEADDR[0];
7977
addr_high = NRF_FICR->DEVICEADDR[1];
80-
memcpy(addr->val, &addr_low, 4);
81-
memcpy(&addr->val[4], &addr_high, 2);
82-
addr->type = BLE_ADDR_PUBLIC;
78+
79+
memcpy(&addr[0], &addr_low, 4);
80+
memcpy(&addr[4], &addr_high, 2);
8381

8482
return 0;
8583
}
84+
#endif
8685

87-
/* Returns random static address or -1 if not present */
86+
#if MYNEWT_VAL(BLE_PHY_ADDR_PROVIDER_STATIC)
8887
int
89-
ble_hw_get_static_addr(ble_addr_t *addr)
88+
ble_ll_addr_provide_static(uint8_t *addr)
9089
{
9190
uint32_t addr_high;
9291
uint32_t addr_low;
93-
int rc;
9492

95-
if ((NRF_FICR->DEVICEADDRTYPE & 1) == 1) {
96-
addr_low = NRF_FICR->DEVICEADDR[0];
97-
addr_high = NRF_FICR->DEVICEADDR[1];
93+
if ((NRF_FICR->DEVICEADDRTYPE & 1) == 0) {
94+
return -1;
95+
}
9896

99-
memcpy(addr->val, &addr_low, 4);
100-
memcpy(&addr->val[4], &addr_high, 2);
97+
addr_low = NRF_FICR->DEVICEADDR[0];
98+
addr_high = NRF_FICR->DEVICEADDR[1];
10199

102-
addr->val[5] |= 0xc0;
103-
addr->type = BLE_ADDR_RANDOM;
104-
rc = 0;
105-
} else {
106-
rc = -1;
107-
}
100+
memcpy(&addr[0], &addr_low, 4);
101+
memcpy(&addr[4], &addr_high, 2);
108102

109-
return rc;
103+
return 0;
110104
}
105+
#endif
111106

112107
/**
113108
* Clear the whitelist

nimble/drivers/nrf51/syscfg.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
# under the License.
1717
#
1818

19+
syscfg.defs:
20+
BLE_PHY_ADDR_PROVIDER_PUBLIC:
21+
description: Enables public address provider API
22+
value: 0
23+
BLE_PHY_ADDR_PROVIDER_STATIC:
24+
description: Enables static address provider API
25+
value: 1
1926

2027
syscfg.vals.!BLE_LL_CFG_FEAT_LL_EXT_ADV:
2128
BLE_LL_STACK_SIZE: 96

nimble/drivers/nrf5x/pkg.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@ pkg.keywords:
2525
- ble
2626
- bluetooth
2727

28-
pkg.apis: ble_driver
2928
pkg.deps:
3029
- nimble
3130
- nimble/controller
3231

32+
pkg.apis:
33+
- ble_driver
34+
pkg.apis.BLE_PHY_ADDR_PROVIDER_PUBLIC:
35+
- ble_addr_provider_public
36+
pkg.apis.BLE_PHY_ADDR_PROVIDER_STATIC:
37+
- ble_addr_provider_static
38+
3339
pkg.ign_dirs.'MCU_TARGET=="nRF5340_NET"':
3440
- nrf52
3541
pkg.ign_dirs.'MCU_TARGET!="nRF5340_NET"':

0 commit comments

Comments
 (0)