Skip to content

Commit 9e2093c

Browse files
nimble/ll: Make public and random addresses private in ble_ll_addr
We should now only use helpers from ble_ll_addr to access public and random addresses across all LL code.
1 parent 052b603 commit 9e2093c

File tree

10 files changed

+147
-201
lines changed

10 files changed

+147
-201
lines changed

nimble/controller/include/controller/ble_ll.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -517,16 +517,6 @@ struct ble_ll_acad_channel_map_update_ind {
517517
/* Reset the Link Layer */
518518
int ble_ll_reset(void);
519519

520-
/* 'Boolean' function returning true if address is a valid random address */
521-
int ble_ll_is_valid_random_addr(const uint8_t *addr);
522-
523-
/*
524-
* Check if given own_addr_type is valid for current controller configuration
525-
* given the random address provided (when applicable)
526-
*/
527-
int ble_ll_is_valid_own_addr_type(uint8_t own_addr_type,
528-
const uint8_t *random_addr);
529-
530520
/* Calculate maximum octets of PDU payload which can be transmitted during
531521
* 'usecs' on a PHY 'phy_mode'. */
532522
uint16_t ble_ll_pdu_max_tx_octets_get(uint32_t usecs, int phy_mode);
@@ -539,12 +529,6 @@ int ble_ll_addr_subtype(const uint8_t *addr, uint8_t addr_type);
539529
/* Is this address an identity address? */
540530
int ble_ll_addr_is_id(uint8_t *addr, uint8_t addr_type);
541531

542-
/* Is 'addr' our device address? 'addr_type' is public (0) or random (!=0) */
543-
int ble_ll_is_our_devaddr(const uint8_t* addr, int addr_type);
544-
545-
/* Get identity address 'addr_type' is public (0) or random (!=0) */
546-
uint8_t *ble_ll_get_our_devaddr(uint8_t addr_type);
547-
548532
/**
549533
* Called to put a packet on the Link Layer transmit packet queue.
550534
*

nimble/controller/include/controller/ble_ll_addr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,25 @@
2020
#ifndef H_BLE_LL_ADDR_
2121
#define H_BLE_LL_ADDR_
2222

23+
#include <stdbool.h>
24+
#include <stdint.h>
25+
2326
#ifdef __cplusplus
2427
extern "C" {
2528
#endif
2629

2730
int ble_ll_addr_init(void);
2831
int ble_ll_addr_public_set(const uint8_t *addr);
2932

33+
const uint8_t *ble_ll_addr_get(uint8_t addr_type);
34+
const uint8_t *ble_ll_addr_public_get(void);
35+
const uint8_t *ble_ll_addr_random_get(void);
36+
int ble_ll_addr_random_set(const uint8_t *addr);
37+
38+
bool ble_ll_addr_is_our(int addr_type, const uint8_t *addr);
39+
bool ble_ll_addr_is_valid_own_addr_type(uint8_t addr_type,
40+
const uint8_t *random_addr);
41+
3042
/* Address provider APIs - should be implemented by packages supporting
3143
* relevant APIs
3244
*/

nimble/controller/src/ble_ll.c

Lines changed: 3 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -557,93 +557,11 @@ ble_ll_addr_subtype(const uint8_t *addr, uint8_t addr_type)
557557
}
558558
}
559559

560-
static int
561-
ble_ll_is_valid_addr(const uint8_t *addr)
562-
{
563-
int i;
564-
565-
for (i = 0; i < BLE_DEV_ADDR_LEN; ++i) {
566-
if (addr[i]) {
567-
return 1;
568-
}
569-
}
570-
571-
return 0;
572-
}
573-
574-
/* Checks to see that the device is a valid random address */
575-
int
576-
ble_ll_is_valid_random_addr(const uint8_t *addr)
577-
{
578-
int i;
579-
int rc;
580-
uint16_t sum;
581-
uint8_t addr_type;
582-
583-
/* Make sure all bits are neither one nor zero */
584-
sum = 0;
585-
for (i = 0; i < (BLE_DEV_ADDR_LEN -1); ++i) {
586-
sum += addr[i];
587-
}
588-
sum += addr[5] & 0x3f;
589-
590-
if ((sum == 0) || (sum == ((5*255) + 0x3f))) {
591-
return 0;
592-
}
593-
594-
/* Get the upper two bits of the address */
595-
rc = 1;
596-
addr_type = addr[5] & 0xc0;
597-
if (addr_type == 0xc0) {
598-
/* Static random address. No other checks needed */
599-
} else if (addr_type == 0x40) {
600-
/* Resolvable */
601-
sum = addr[3] + addr[4] + (addr[5] & 0x3f);
602-
if ((sum == 0) || (sum == (255 + 255 + 0x3f))) {
603-
rc = 0;
604-
}
605-
} else if (addr_type == 0) {
606-
/* non-resolvable. Cant be equal to public */
607-
if (!memcmp(g_dev_addr, addr, BLE_DEV_ADDR_LEN)) {
608-
rc = 0;
609-
}
610-
} else {
611-
/* Invalid upper two bits */
612-
rc = 0;
613-
}
614-
615-
return rc;
616-
}
617-
int
618-
ble_ll_is_valid_own_addr_type(uint8_t own_addr_type, const uint8_t *random_addr)
619-
{
620-
int rc;
621-
622-
switch (own_addr_type) {
623-
case BLE_HCI_ADV_OWN_ADDR_PUBLIC:
624-
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
625-
case BLE_HCI_ADV_OWN_ADDR_PRIV_PUB:
626-
#endif
627-
rc = ble_ll_is_valid_addr(g_dev_addr);
628-
break;
629-
case BLE_HCI_ADV_OWN_ADDR_RANDOM:
630-
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
631-
case BLE_HCI_ADV_OWN_ADDR_PRIV_RAND:
632-
#endif
633-
rc = ble_ll_is_valid_addr(random_addr);
634-
break;
635-
default:
636-
rc = 0;
637-
break;
638-
}
639-
640-
return rc;
641-
}
642-
560+
/* FIXME: remove callers */
643561
int
644562
ble_ll_set_public_addr(const uint8_t *addr)
645563
{
646-
memcpy(g_dev_addr, addr, BLE_DEV_ADDR_LEN);
564+
ble_ll_addr_public_set(addr);
647565

648566
return BLE_ERR_SUCCESS;
649567
}
@@ -691,7 +609,7 @@ ble_ll_set_random_addr(const uint8_t *cmdbuf, uint8_t len, bool hci_adv_ext)
691609
}
692610
#endif
693611

694-
memcpy(g_random_addr, cmd->addr, BLE_DEV_ADDR_LEN);
612+
ble_ll_addr_random_set(cmd->addr);
695613

696614
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_EXT_ADV)
697615
#if MYNEWT_VAL(BLE_LL_ROLE_BROADCASTER)
@@ -707,52 +625,6 @@ ble_ll_set_random_addr(const uint8_t *cmdbuf, uint8_t len, bool hci_adv_ext)
707625
return BLE_ERR_SUCCESS;
708626
}
709627

710-
/**
711-
* Checks to see if an address is our device address (either public or
712-
* random)
713-
*
714-
* @param addr
715-
* @param addr_type
716-
*
717-
* @return int 0: not our device address. 1: is our device address
718-
*/
719-
int
720-
ble_ll_is_our_devaddr(const uint8_t* addr, int addr_type)
721-
{
722-
int rc;
723-
uint8_t *our_addr;
724-
725-
if (addr_type) {
726-
our_addr = g_random_addr;
727-
} else {
728-
our_addr = g_dev_addr;
729-
}
730-
731-
rc = 0;
732-
if (!memcmp(our_addr, addr, BLE_DEV_ADDR_LEN)) {
733-
rc = 1;
734-
}
735-
736-
return rc;
737-
}
738-
739-
/**
740-
* Get identity address
741-
*
742-
* @param addr_type Random (1). Public(0)
743-
*
744-
* @return pointer to identity address of given type.
745-
*/
746-
uint8_t*
747-
ble_ll_get_our_devaddr(uint8_t addr_type)
748-
{
749-
if (addr_type) {
750-
return g_random_addr;
751-
}
752-
753-
return g_dev_addr;
754-
}
755-
756628
/**
757629
* Wait for response timeout function
758630
*

nimble/controller/src/ble_ll_addr.c

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@
2222
#include <controller/ble_ll.h>
2323
#include <controller/ble_ll_addr.h>
2424

25-
/* FIXME: both should be static and accessible only via dedicated set/get APIs;
26-
* extern declared in nimble/ble.h should be removed
27-
*/
28-
uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
29-
uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
30-
31-
static uint8_t g_dev_addr_hci[BLE_DEV_ADDR_LEN];
25+
static struct {
26+
uint8_t public_addr[BLE_DEV_ADDR_LEN];
27+
uint8_t random_addr[BLE_DEV_ADDR_LEN];
28+
uint8_t public_addr_hci[BLE_DEV_ADDR_LEN];
29+
} g_ble_ll_addr;
3230

3331
static bool
3432
is_addr_empty(const uint8_t *addr)
@@ -39,11 +37,82 @@ is_addr_empty(const uint8_t *addr)
3937
int
4038
ble_ll_addr_public_set(const uint8_t *addr)
4139
{
42-
memcpy(g_dev_addr_hci, addr, BLE_DEV_ADDR_LEN);
40+
memcpy(g_ble_ll_addr.public_addr_hci, addr, BLE_DEV_ADDR_LEN);
4341

4442
return 0;
4543
}
4644

45+
const uint8_t *
46+
ble_ll_addr_get(uint8_t addr_type)
47+
{
48+
const uint8_t *addr;
49+
50+
if (addr_type) {
51+
addr = g_ble_ll_addr.random_addr;
52+
} else {
53+
addr = g_ble_ll_addr.public_addr;
54+
}
55+
56+
return addr;
57+
}
58+
59+
const uint8_t *
60+
ble_ll_addr_public_get(void)
61+
{
62+
return g_ble_ll_addr.public_addr;
63+
}
64+
65+
66+
const uint8_t *
67+
ble_ll_addr_random_get(void)
68+
{
69+
return g_ble_ll_addr.random_addr;
70+
}
71+
72+
int
73+
ble_ll_addr_random_set(const uint8_t *addr)
74+
{
75+
memcpy(g_ble_ll_addr.random_addr, addr, BLE_DEV_ADDR_LEN);
76+
77+
return 0;
78+
}
79+
80+
bool
81+
ble_ll_addr_is_our(int addr_type, const uint8_t* addr)
82+
{
83+
const uint8_t *our_addr;
84+
85+
our_addr = ble_ll_addr_get(addr_type);
86+
87+
return memcmp(our_addr, addr, BLE_DEV_ADDR_LEN) == 0;
88+
}
89+
90+
bool
91+
ble_ll_addr_is_valid_own_addr_type(uint8_t addr_type,
92+
const uint8_t *random_addr)
93+
{
94+
const uint8_t *addr;
95+
96+
switch (addr_type) {
97+
case BLE_HCI_ADV_OWN_ADDR_PUBLIC:
98+
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
99+
case BLE_HCI_ADV_OWN_ADDR_PRIV_PUB:
100+
#endif
101+
addr = ble_ll_addr_public_get();
102+
break;
103+
case BLE_HCI_ADV_OWN_ADDR_RANDOM:
104+
#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
105+
case BLE_HCI_ADV_OWN_ADDR_PRIV_RAND:
106+
#endif
107+
addr = random_addr ? random_addr : ble_ll_addr_random_get();
108+
break;
109+
default:
110+
return false;
111+
}
112+
113+
return !is_addr_empty(addr);
114+
}
115+
47116
int
48117
ble_ll_addr_init(void)
49118
{
@@ -55,24 +124,24 @@ ble_ll_addr_init(void)
55124
*/
56125
pub_dev_addr = MYNEWT_VAL(BLE_LL_PUBLIC_DEV_ADDR);
57126
for (i = 0; i < BLE_DEV_ADDR_LEN; i++) {
58-
g_dev_addr[i] = pub_dev_addr & 0xff;
127+
g_ble_ll_addr.public_addr[i] = pub_dev_addr & 0xff;
59128
pub_dev_addr >>= 8;
60129
}
61130

62-
if (!is_addr_empty(g_dev_addr_hci)) {
131+
if (!is_addr_empty(g_ble_ll_addr.public_addr_hci)) {
63132
/* Use public address set externally */
64-
memcpy(g_dev_addr, g_dev_addr_hci, BLE_DEV_ADDR_LEN);
133+
memcpy(g_ble_ll_addr.public_addr, g_ble_ll_addr.public_addr_hci, BLE_DEV_ADDR_LEN);
65134
} else {
66135
/* Set public address from provider API, if available */
67136
#if MYNEWT_API_ble_addr_provider_public
68-
ble_ll_addr_provide_public(g_dev_addr);
137+
ble_ll_addr_provide_public(g_ble_ll_addr.public_addr);
69138
#endif
70139
}
71140

72141
#if MYNEWT_VAL(BLE_LL_ADDR_INIT_RANDOM)
73142
/* Set random address from provider API, if available */
74143
#if MYNEWT_API_ble_addr_provider_random
75-
ble_ll_addr_provide_static(g_random_addr);
144+
ble_ll_addr_provide_static(g_ble_ll_addr.random_addr);
76145
#endif
77146
#endif
78147

0 commit comments

Comments
 (0)