From baad0c300fc0981b729a791fcdefa8fc3a0629ad Mon Sep 17 00:00:00 2001 From: Emil Gydesen Date: Wed, 11 Jan 2023 17:40:02 +0100 Subject: [PATCH] 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 --- subsys/bluetooth/host/addr.c | 14 +++++++++++++ subsys/bluetooth/host/addr_internal.h | 14 +++++++++++++ subsys/bluetooth/host/adv.c | 7 +++---- subsys/bluetooth/host/conn.c | 7 +++---- subsys/bluetooth/host/hci_core.c | 8 +++----- subsys/bluetooth/host/scan.c | 29 +++++++++++---------------- 6 files changed, 49 insertions(+), 30 deletions(-) create mode 100644 subsys/bluetooth/host/addr_internal.h diff --git a/subsys/bluetooth/host/addr.c b/subsys/bluetooth/host/addr.c index 1fb66d06073..cc4da7b3e4e 100644 --- a/subsys/bluetooth/host/addr.c +++ b/subsys/bluetooth/host/addr.c @@ -14,6 +14,8 @@ #include #include +#define ADDR_RESOLVED_BITMASK (0x02) + static inline int create_random_addr(bt_addr_le_t *addr) { 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) return 0; } + +void bt_addr_le_copy_resolved(bt_addr_le_t *dst, const bt_addr_le_t *src) +{ + bt_addr_le_copy(dst, src); + /* translate to "regular" address type */ + dst->type &= ~ADDR_RESOLVED_BITMASK; +} + +bool bt_addr_le_is_resolved(const bt_addr_le_t *addr) +{ + return (addr->type & ADDR_RESOLVED_BITMASK) != 0; +} diff --git a/subsys/bluetooth/host/addr_internal.h b/subsys/bluetooth/host/addr_internal.h new file mode 100644 index 00000000000..e61f62a5714 --- /dev/null +++ b/subsys/bluetooth/host/addr_internal.h @@ -0,0 +1,14 @@ +/** @file + * @brief Internal header for Bluetooth address functions + */ + +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include + +void bt_addr_le_copy_resolved(bt_addr_le_t *dst, const bt_addr_le_t *src); + +bool bt_addr_le_is_resolved(const bt_addr_le_t *addr); diff --git a/subsys/bluetooth/host/adv.c b/subsys/bluetooth/host/adv.c index 68ff2563ac1..1c110e77f73 100644 --- a/subsys/bluetooth/host/adv.c +++ b/subsys/bluetooth/host/adv.c @@ -12,6 +12,7 @@ #include #include +#include "addr_internal.h" #include "hci_core.h" #include "conn_internal.h" #include "id.h" @@ -2092,10 +2093,8 @@ void bt_hci_le_scan_req_received(struct net_buf *buf) struct bt_le_ext_adv_scanned_info info; bt_addr_le_t id_addr; - if (evt->addr.type == BT_ADDR_LE_PUBLIC_ID || - evt->addr.type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&id_addr, &evt->addr); - id_addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(&evt->addr)) { + bt_addr_le_copy_resolved(&id_addr, &evt->addr); } else { bt_addr_le_copy(&id_addr, bt_lookup_id_addr(adv->id, &evt->addr)); diff --git a/subsys/bluetooth/host/conn.c b/subsys/bluetooth/host/conn.c index c9c1a8ab6c8..a6bcba77df0 100644 --- a/subsys/bluetooth/host/conn.c +++ b/subsys/bluetooth/host/conn.c @@ -27,6 +27,7 @@ #include "common/assert.h" +#include "addr_internal.h" #include "hci_core.h" #include "id.h" #include "adv.h" @@ -2817,10 +2818,8 @@ int bt_conn_le_create(const bt_addr_le_t *peer, return -EINVAL; } - if (peer->type == BT_ADDR_LE_PUBLIC_ID || - peer->type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&dst, peer); - dst.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(peer)) { + bt_addr_le_copy_resolved(&dst, peer); } else { bt_addr_le_copy(&dst, bt_lookup_id_addr(BT_ID_DEFAULT, peer)); } diff --git a/subsys/bluetooth/host/hci_core.c b/subsys/bluetooth/host/hci_core.c index 85e1fd92027..2b9c86436b9 100644 --- a/subsys/bluetooth/host/hci_core.c +++ b/subsys/bluetooth/host/hci_core.c @@ -41,6 +41,7 @@ #include "adv.h" #include "scan.h" +#include "addr_internal.h" #include "conn_internal.h" #include "iso_internal.h" #include "l2cap_internal.h" @@ -1205,11 +1206,8 @@ void bt_hci_le_enh_conn_complete(struct bt_hci_evt_le_enh_conn_complete *evt) return; } - /* Translate "enhanced" identity address type to normal one */ - if (evt->peer_addr.type == BT_ADDR_LE_PUBLIC_ID || - evt->peer_addr.type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&id_addr, &evt->peer_addr); - id_addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(&evt->peer_addr)) { + bt_addr_le_copy_resolved(&id_addr, &evt->peer_addr); bt_addr_copy(&peer_addr.a, &evt->peer_rpa); peer_addr.type = BT_ADDR_LE_RANDOM; diff --git a/subsys/bluetooth/host/scan.c b/subsys/bluetooth/host/scan.c index 9235c89f56b..e71c792c571 100644 --- a/subsys/bluetooth/host/scan.c +++ b/subsys/bluetooth/host/scan.c @@ -17,6 +17,7 @@ #include #include +#include "addr_internal.h" #include "hci_core.h" #include "conn_internal.h" #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, return; } - if (addr->type == BT_ADDR_LE_PUBLIC_ID || - addr->type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&id_addr, addr); - id_addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(addr)) { + bt_addr_le_copy_resolved(&id_addr, addr); } else if (addr->type == BT_HCI_PEER_ADDR_ANONYMOUS) { bt_addr_le_copy(&id_addr, BT_ADDR_LE_ANY); } else { @@ -912,10 +911,8 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf) return; } - if (evt->adv_addr.type == BT_ADDR_LE_PUBLIC_ID || - evt->adv_addr.type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&id_addr, &evt->adv_addr); - id_addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(&evt->adv_addr)) { + bt_addr_le_copy_resolved(&id_addr, &evt->adv_addr); } else { bt_addr_le_copy(&id_addr, bt_lookup_id_addr(BT_ID_DEFAULT, @@ -979,13 +976,13 @@ void bt_hci_le_per_adv_sync_established(struct net_buf *buf) if (atomic_test_bit(pending_per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCING_USE_LIST)) { /* Now we know which address and SID we synchronized to. */ - bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr); pending_per_adv_sync->sid = evt->sid; - /* Translate "enhanced" identity address type to normal one */ - if (pending_per_adv_sync->addr.type == BT_ADDR_LE_PUBLIC_ID || - pending_per_adv_sync->addr.type == BT_ADDR_LE_RANDOM_ID) { - pending_per_adv_sync->addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(&pending_per_adv_sync->addr)) { + bt_addr_le_copy_resolved(&pending_per_adv_sync->addr, + &id_addr); + } else { + bt_addr_le_copy(&pending_per_adv_sync->addr, &id_addr); } } @@ -1055,10 +1052,8 @@ void bt_hci_le_past_received(struct net_buf *buf) atomic_set_bit(per_adv_sync->flags, BT_PER_ADV_SYNC_SYNCED); - if (evt->addr.type == BT_ADDR_LE_PUBLIC_ID || - evt->addr.type == BT_ADDR_LE_RANDOM_ID) { - bt_addr_le_copy(&id_addr, &evt->addr); - id_addr.type -= BT_ADDR_LE_PUBLIC_ID; + if (bt_addr_le_is_resolved(&evt->addr)) { + bt_addr_le_copy_resolved(&id_addr, &evt->addr); } else { bt_addr_le_copy(&id_addr, bt_lookup_id_addr(BT_ID_DEFAULT, &evt->addr));