Skip to content

Commit 5918427

Browse files
Thalleykartben
authored andcommitted
Bluetooth: Host: Make bt_le_addr_is_bonded public
Some GATT services and profiles define specific behavior based on whether the remote device is bonded or not. The internal function, bt_addr_le_is_bonded, is the only function to do this, but it was kept internal, and could thus not be used for those services without including hci_core.h. The function has been moved to the public API so that application can determine if a remote address is bonded or not, and has been renamed to not use the bt_addr namespace, but rather the bt_le. Signed-off-by: Emil Gydesen <[email protected]>
1 parent b8a9f1f commit 5918427

File tree

13 files changed

+37
-31
lines changed

13 files changed

+37
-31
lines changed

doc/releases/release-notes-4.2.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ New APIs and options
8484
* Host
8585

8686
* :c:func:`bt_le_get_local_features`
87+
* :c:func:`bt_le_bond_exists`
8788

8889
New Boards
8990
**********

include/zephyr/bluetooth/bluetooth.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2856,6 +2856,18 @@ int bt_le_per_adv_set_response_data(struct bt_le_per_adv_sync *per_adv_sync,
28562856
const struct bt_le_per_adv_response_params *params,
28572857
const struct net_buf_simple *data);
28582858

2859+
/** @brief Check if a device identified by a Bluetooth LE address is bonded.
2860+
*
2861+
* Valid Bluetooth LE identity addresses are either public address or
2862+
* random static address.
2863+
*
2864+
* @param id Local identity (typically @ref BT_ID_DEFAULT).
2865+
* @param addr Bluetooth LE device address.
2866+
*
2867+
* @return true if @p addr is bonded with local @p id
2868+
*/
2869+
bool bt_le_bond_exists(uint8_t id, const bt_addr_le_t *addr);
2870+
28592871
/**
28602872
* @}
28612873
*/

subsys/bluetooth/audio/bap_scan_delegator.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ static void scan_delegator_security_changed(struct bt_conn *conn,
271271
enum bt_security_err err)
272272
{
273273

274-
if (err != 0 || level < BT_SECURITY_L2 || !bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
274+
if (err != 0 || level < BT_SECURITY_L2 || !bt_le_bond_exists(conn->id, &conn->le.dst)) {
275275
return;
276276
}
277277

subsys/bluetooth/audio/csip_set_coordinator.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
#include "common/bt_str.h"
5353
#include "host/conn_internal.h"
5454
#include "host/keys.h"
55-
#include "host/hci_core.h"
5655

5756
LOG_MODULE_REGISTER(bt_csip_set_coordinator, CONFIG_BT_CSIP_SET_COORDINATOR_LOG_LEVEL);
5857

@@ -1674,7 +1673,7 @@ static bool all_members_bonded(const struct bt_csip_set_coordinator_set_member *
16741673
int err;
16751674

16761675
err = bt_conn_get_info(client->conn, &info);
1677-
if (err != 0 || !bt_addr_le_is_bonded(info.id, info.le.dst)) {
1676+
if (err != 0 || !bt_le_bond_exists(info.id, info.le.dst)) {
16781677
LOG_DBG("Member[%zu] is not bonded", i);
16791678

16801679
return false;

subsys/bluetooth/audio/csip_set_member.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <zephyr/sys/check.h>
3939

4040
#include "../host/conn_internal.h"
41-
#include "../host/hci_core.h"
4241
#include "../host/keys.h"
4342

4443
#include "common/bt_str.h"
@@ -464,7 +463,7 @@ static void csip_security_changed(struct bt_conn *conn, bt_security_t level,
464463
return;
465464
}
466465

467-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
466+
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
468467
return;
469468
}
470469

@@ -521,7 +520,7 @@ static void csip_disconnected(struct bt_conn *conn, uint8_t reason)
521520
{
522521
LOG_DBG("Disconnected: %s (reason %u)", bt_addr_le_str(bt_conn_get_dst(conn)), reason);
523522

524-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
523+
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
525524
for (size_t i = 0U; i < ARRAY_SIZE(svc_insts); i++) {
526525
handle_csip_disconnect(&svc_insts[i], conn);
527526
}

subsys/bluetooth/audio/has.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <zephyr/sys_clock.h>
3535
#include <zephyr/toolchain.h>
3636

37-
#include "../bluetooth/host/hci_core.h"
3837
#include "../bluetooth/host/settings.h"
3938

4039
#include "audio_internal.h"
@@ -270,7 +269,7 @@ static void client_free(struct has_client *client)
270269
err = bt_conn_get_info(client->conn, &info);
271270
__ASSERT_NO_MSG(err == 0);
272271

273-
if (client->context != NULL && !bt_addr_le_is_bonded(info.id, info.le.dst)) {
272+
if (client->context != NULL && !bt_le_bond_exists(info.id, info.le.dst)) {
274273
/* Free stored context of non-bonded client */
275274
context_free(client->context);
276275
client->context = NULL;
@@ -383,7 +382,7 @@ static void security_changed(struct bt_conn *conn, bt_security_t level, enum bt_
383382
return;
384383
}
385384

386-
if (!bt_addr_le_is_bonded(info.id, info.le.dst)) {
385+
if (!bt_le_bond_exists(info.id, info.le.dst)) {
387386
return;
388387
}
389388

subsys/bluetooth/audio/pacs.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include <zephyr/sys/util.h>
3838
#include <zephyr/sys/util_macro.h>
3939

40-
#include "host/hci_core.h"
4140
#include "common/bt_str.h"
4241

4342
#include "audio_internal.h"
@@ -1223,7 +1222,7 @@ static void pacs_security_changed(struct bt_conn *conn, bt_security_t level,
12231222
return;
12241223
}
12251224

1226-
if (!bt_addr_le_is_bonded(info.id, info.le.dst)) {
1225+
if (!bt_le_bond_exists(info.id, info.le.dst)) {
12271226
return;
12281227
}
12291228

subsys/bluetooth/host/gatt.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static int bt_gatt_clear_sc(uint8_t id, const bt_addr_le_t *addr)
384384

385385
static void sc_clear(struct bt_conn *conn)
386386
{
387-
if (bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
387+
if (bt_le_bond_exists(conn->id, &conn->le.dst)) {
388388
int err;
389389

390390
err = bt_gatt_clear_sc(conn->id, &conn->le.dst);
@@ -466,8 +466,7 @@ static void sc_save(uint8_t id, bt_addr_le_t *peer, uint16_t start, uint16_t end
466466
modified = update_range(&cfg->data.start, &cfg->data.end, start, end);
467467

468468
done:
469-
if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
470-
modified && bt_addr_le_is_bonded(cfg->id, &cfg->peer)) {
469+
if (IS_ENABLED(CONFIG_BT_SETTINGS) && modified && bt_le_bond_exists(cfg->id, &cfg->peer)) {
471470
sc_store(cfg);
472471
}
473472
}
@@ -1030,7 +1029,7 @@ static void remove_cf_cfg(struct bt_conn *conn)
10301029
* trusted relationship the characteristic value shall be set to the
10311030
* default value at each connection.
10321031
*/
1033-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
1032+
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
10341033
clear_cf_cfg(cfg);
10351034
} else {
10361035
/* Update address in case it has changed */
@@ -1148,7 +1147,7 @@ static void bt_gatt_identity_resolved(struct bt_conn *conn, const bt_addr_le_t *
11481147
.private_addr = private_addr,
11491148
.id_addr = id_addr
11501149
};
1151-
bool is_bonded = bt_addr_le_is_bonded(conn->id, &conn->le.dst);
1150+
bool is_bonded = bt_le_bond_exists(conn->id, &conn->le.dst);
11521151

11531152
bt_gatt_foreach_attr(0x0001, 0xffff, convert_to_id_on_match, &user_data);
11541153

@@ -1448,7 +1447,7 @@ static struct ds_peer *gatt_delayed_store_alloc(uint8_t id,
14481447
static void gatt_delayed_store_enqueue(uint8_t id, const bt_addr_le_t *peer_addr,
14491448
enum delayed_store_flags flag)
14501449
{
1451-
bool bonded = bt_addr_le_is_bonded(id, peer_addr);
1450+
bool bonded = bt_le_bond_exists(id, peer_addr);
14521451
struct ds_peer *el = gatt_delayed_store_find(id, peer_addr);
14531452

14541453
if (bonded) {
@@ -1483,7 +1482,7 @@ static void gatt_store_ccc_cf(uint8_t id, const bt_addr_le_t *peer_addr)
14831482
{
14841483
struct ds_peer *el = gatt_delayed_store_find(id, peer_addr);
14851484

1486-
if (bt_addr_le_is_bonded(id, peer_addr)) {
1485+
if (bt_le_bond_exists(id, peer_addr)) {
14871486
if (!IS_ENABLED(CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE) ||
14881487
(IS_ENABLED(CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE) && el &&
14891488
atomic_test_and_clear_bit(el->flags, DELAYED_STORE_CCC))) {
@@ -1671,7 +1670,7 @@ static void gatt_unregister_ccc(struct _bt_gatt_ccc *ccc)
16711670
}
16721671

16731672
if (IS_ENABLED(CONFIG_BT_SETTINGS) && store &&
1674-
bt_addr_le_is_bonded(cfg->id, &cfg->peer)) {
1673+
bt_le_bond_exists(cfg->id, &cfg->peer)) {
16751674
bt_gatt_store_ccc(cfg->id, &cfg->peer);
16761675
}
16771676

@@ -3424,7 +3423,7 @@ static uint8_t disconnected_cb(const struct bt_gatt_attr *attr, uint16_t handle,
34243423
}
34253424
} else {
34263425
/* Clear value if not paired */
3427-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
3426+
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
34283427
if (ccc == &sc_ccc) {
34293428
sc_clear(conn);
34303429
}
@@ -3740,9 +3739,8 @@ static void remove_subscriptions(struct bt_conn *conn)
37403739
SYS_SLIST_FOR_EACH_CONTAINER_SAFE(&sub->list, params, tmp, node) {
37413740
atomic_clear_bit(params->flags, BT_GATT_SUBSCRIBE_FLAG_SENT);
37423741

3743-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst) ||
3744-
(atomic_test_bit(params->flags,
3745-
BT_GATT_SUBSCRIBE_FLAG_VOLATILE))) {
3742+
if (!bt_le_bond_exists(conn->id, &conn->le.dst) ||
3743+
(atomic_test_bit(params->flags, BT_GATT_SUBSCRIBE_FLAG_VOLATILE))) {
37463744
/* Remove subscription */
37473745
params->value = 0U;
37483746
gatt_sub_remove(conn, sub, prev, params);
@@ -5675,7 +5673,7 @@ static void add_subscriptions(struct bt_conn *conn)
56755673
struct gatt_sub *sub;
56765674
struct bt_gatt_subscribe_params *params;
56775675

5678-
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
5676+
if (!bt_le_bond_exists(conn->id, &conn->le.dst)) {
56795677
return;
56805678
}
56815679

@@ -5933,7 +5931,7 @@ void bt_gatt_connected(struct bt_conn *conn)
59335931

59345932
/* Load CCC settings from backend if bonded */
59355933
if (IS_ENABLED(CONFIG_BT_SETTINGS_CCC_LAZY_LOADING) &&
5936-
bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
5934+
bt_le_bond_exists(conn->id, &conn->le.dst)) {
59375935
char key[BT_SETTINGS_KEY_MAX];
59385936

59395937
if (conn->id) {
@@ -6521,7 +6519,7 @@ void bt_gatt_disconnected(struct bt_conn *conn)
65216519

65226520
/* Make sure to clear the CCC entry when using lazy loading */
65236521
if (IS_ENABLED(CONFIG_BT_SETTINGS_CCC_LAZY_LOADING) &&
6524-
bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
6522+
bt_le_bond_exists(conn->id, &conn->le.dst)) {
65256523
struct addr_with_id addr_with_id = {
65266524
.addr = &conn->le.dst,
65276525
.id = conn->id,

subsys/bluetooth/host/hci_core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4541,7 +4541,7 @@ int bt_le_get_local_features(struct bt_le_local_features *remote_info)
45414541
return 0;
45424542
}
45434543

4544-
bool bt_addr_le_is_bonded(uint8_t id, const bt_addr_le_t *addr)
4544+
bool bt_le_bond_exists(uint8_t id, const bt_addr_le_t *addr)
45454545
{
45464546
if (IS_ENABLED(CONFIG_BT_SMP)) {
45474547
struct bt_keys *keys = bt_keys_find_addr(id, addr);

subsys/bluetooth/host/hci_core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,6 @@ int bt_le_create_conn_cancel(void);
480480
int bt_le_create_conn_synced(const struct bt_conn *conn, const struct bt_le_ext_adv *adv,
481481
uint8_t subevent);
482482

483-
bool bt_addr_le_is_bonded(uint8_t id, const bt_addr_le_t *addr);
484483
const bt_addr_le_t *bt_lookup_id_addr(uint8_t id, const bt_addr_le_t *addr);
485484

486485
int bt_send(struct net_buf *buf);

0 commit comments

Comments
 (0)