Skip to content

Commit baad0c3

Browse files
Thalleycarlescufi
authored andcommitted
BluetootH: Host: add helper functions for resolved addresses
There is special handling done for resolved addresses to convert them to "regular" addresses for the upper layers. This commits adds two helper functions to check if they are resolved, and if so, then properly copied. Signed-off-by: Emil Gydesen <[email protected]>
1 parent 4af1c99 commit baad0c3

File tree

6 files changed

+49
-30
lines changed

6 files changed

+49
-30
lines changed

subsys/bluetooth/host/addr.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#include <zephyr/bluetooth/addr.h>
1515
#include <zephyr/bluetooth/crypto.h>
1616

17+
#define ADDR_RESOLVED_BITMASK (0x02)
18+
1719
static inline int create_random_addr(bt_addr_le_t *addr)
1820
{
1921
addr->type = BT_ADDR_LE_RANDOM;
@@ -101,3 +103,15 @@ int bt_addr_le_from_str(const char *str, const char *type, bt_addr_le_t *addr)
101103

102104
return 0;
103105
}
106+
107+
void bt_addr_le_copy_resolved(bt_addr_le_t *dst, const bt_addr_le_t *src)
108+
{
109+
bt_addr_le_copy(dst, src);
110+
/* translate to "regular" address type */
111+
dst->type &= ~ADDR_RESOLVED_BITMASK;
112+
}
113+
114+
bool bt_addr_le_is_resolved(const bt_addr_le_t *addr)
115+
{
116+
return (addr->type & ADDR_RESOLVED_BITMASK) != 0;
117+
}

subsys/bluetooth/host/addr_internal.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/** @file
2+
* @brief Internal header for Bluetooth address functions
3+
*/
4+
5+
/*
6+
* Copyright (c) 2023 Nordic Semiconductor ASA
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*/
10+
#include <zephyr/bluetooth/addr.h>
11+
12+
void bt_addr_le_copy_resolved(bt_addr_le_t *dst, const bt_addr_le_t *src);
13+
14+
bool bt_addr_le_is_resolved(const bt_addr_le_t *addr);

subsys/bluetooth/host/adv.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <zephyr/bluetooth/hci.h>
1313
#include <zephyr/bluetooth/buf.h>
1414

15+
#include "addr_internal.h"
1516
#include "hci_core.h"
1617
#include "conn_internal.h"
1718
#include "id.h"
@@ -2092,10 +2093,8 @@ void bt_hci_le_scan_req_received(struct net_buf *buf)
20922093
struct bt_le_ext_adv_scanned_info info;
20932094
bt_addr_le_t id_addr;
20942095

2095-
if (evt->addr.type == BT_ADDR_LE_PUBLIC_ID ||
2096-
evt->addr.type == BT_ADDR_LE_RANDOM_ID) {
2097-
bt_addr_le_copy(&id_addr, &evt->addr);
2098-
id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
2096+
if (bt_addr_le_is_resolved(&evt->addr)) {
2097+
bt_addr_le_copy_resolved(&id_addr, &evt->addr);
20992098
} else {
21002099
bt_addr_le_copy(&id_addr,
21012100
bt_lookup_id_addr(adv->id, &evt->addr));

subsys/bluetooth/host/conn.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#include "common/assert.h"
2929

30+
#include "addr_internal.h"
3031
#include "hci_core.h"
3132
#include "id.h"
3233
#include "adv.h"
@@ -2817,10 +2818,8 @@ int bt_conn_le_create(const bt_addr_le_t *peer,
28172818
return -EINVAL;
28182819
}
28192820

2820-
if (peer->type == BT_ADDR_LE_PUBLIC_ID ||
2821-
peer->type == BT_ADDR_LE_RANDOM_ID) {
2822-
bt_addr_le_copy(&dst, peer);
2823-
dst.type -= BT_ADDR_LE_PUBLIC_ID;
2821+
if (bt_addr_le_is_resolved(peer)) {
2822+
bt_addr_le_copy_resolved(&dst, peer);
28242823
} else {
28252824
bt_addr_le_copy(&dst, bt_lookup_id_addr(BT_ID_DEFAULT, peer));
28262825
}

subsys/bluetooth/host/hci_core.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "adv.h"
4242
#include "scan.h"
4343

44+
#include "addr_internal.h"
4445
#include "conn_internal.h"
4546
#include "iso_internal.h"
4647
#include "l2cap_internal.h"
@@ -1205,11 +1206,8 @@ void bt_hci_le_enh_conn_complete(struct bt_hci_evt_le_enh_conn_complete *evt)
12051206
return;
12061207
}
12071208

1208-
/* Translate "enhanced" identity address type to normal one */
1209-
if (evt->peer_addr.type == BT_ADDR_LE_PUBLIC_ID ||
1210-
evt->peer_addr.type == BT_ADDR_LE_RANDOM_ID) {
1211-
bt_addr_le_copy(&id_addr, &evt->peer_addr);
1212-
id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
1209+
if (bt_addr_le_is_resolved(&evt->peer_addr)) {
1210+
bt_addr_le_copy_resolved(&id_addr, &evt->peer_addr);
12131211

12141212
bt_addr_copy(&peer_addr.a, &evt->peer_rpa);
12151213
peer_addr.type = BT_ADDR_LE_RANDOM;

subsys/bluetooth/host/scan.c

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <zephyr/bluetooth/hci.h>
1818
#include <zephyr/bluetooth/hci_vs.h>
1919

20+
#include "addr_internal.h"
2021
#include "hci_core.h"
2122
#include "conn_internal.h"
2223
#include "direction_internal.h"
@@ -453,10 +454,8 @@ static void le_adv_recv(bt_addr_le_t *addr, struct bt_le_scan_recv_info *info,
453454
return;
454455
}
455456

456-
if (addr->type == BT_ADDR_LE_PUBLIC_ID ||
457-
addr->type == BT_ADDR_LE_RANDOM_ID) {
458-
bt_addr_le_copy(&id_addr, addr);
459-
id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
457+
if (bt_addr_le_is_resolved(addr)) {
458+
bt_addr_le_copy_resolved(&id_addr, addr);
460459
} else if (addr->type == BT_HCI_PEER_ADDR_ANONYMOUS) {
461460
bt_addr_le_copy(&id_addr, BT_ADDR_LE_ANY);
462461
} else {
@@ -912,10 +911,8 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf)
912911
return;
913912
}
914913

915-
if (evt->adv_addr.type == BT_ADDR_LE_PUBLIC_ID ||
916-
evt->adv_addr.type == BT_ADDR_LE_RANDOM_ID) {
917-
bt_addr_le_copy(&id_addr, &evt->adv_addr);
918-
id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
914+
if (bt_addr_le_is_resolved(&evt->adv_addr)) {
915+
bt_addr_le_copy_resolved(&id_addr, &evt->adv_addr);
919916
} else {
920917
bt_addr_le_copy(&id_addr,
921918
bt_lookup_id_addr(BT_ID_DEFAULT,
@@ -979,13 +976,13 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf)
979976
if (atomic_test_bit(pending_per_adv_sync->flags,
980977
BT_PER_ADV_SYNC_SYNCING_USE_LIST)) {
981978
/* Now we know which address and SID we synchronized to. */
982-
bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr);
983979
pending_per_adv_sync->sid = evt->sid;
984980

985-
/* Translate "enhanced" identity address type to normal one */
986-
if (pending_per_adv_sync->addr.type == BT_ADDR_LE_PUBLIC_ID ||
987-
pending_per_adv_sync->addr.type == BT_ADDR_LE_RANDOM_ID) {
988-
pending_per_adv_sync->addr.type -= BT_ADDR_LE_PUBLIC_ID;
981+
if (bt_addr_le_is_resolved(&pending_per_adv_sync->addr)) {
982+
bt_addr_le_copy_resolved(&pending_per_adv_sync->addr,
983+
&id_addr);
984+
} else {
985+
bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr);
989986
}
990987
}
991988

@@ -1055,10 +1052,8 @@ void bt_hci_le_past_received(struct net_buf *buf)
10551052

10561053
atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED);
10571054

1058-
if (evt->addr.type == BT_ADDR_LE_PUBLIC_ID ||
1059-
evt->addr.type == BT_ADDR_LE_RANDOM_ID) {
1060-
bt_addr_le_copy(&id_addr, &evt->addr);
1061-
id_addr.type -= BT_ADDR_LE_PUBLIC_ID;
1055+
if (bt_addr_le_is_resolved(&evt->addr)) {
1056+
bt_addr_le_copy_resolved(&id_addr, &evt->addr);
10621057
} else {
10631058
bt_addr_le_copy(&id_addr,
10641059
bt_lookup_id_addr(BT_ID_DEFAULT, &evt->addr));

0 commit comments

Comments
 (0)