Skip to content

Commit 3cccf27

Browse files
Vudentzjhedberg
authored andcommitted
Bluetooth: GATT: Persist Client Features config only when paired
BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2405: 'For clients with a trusted relationship, the characteristic value shall be persistent across connections. For clients without a trusted relationship the characteristic value shall be set to the default value at each connection.' Signed-off-by: Luiz Augusto von Dentz <[email protected]>
1 parent daac1fa commit 3cccf27

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

subsys/bluetooth/host/gatt.c

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,16 @@ struct gatt_cf_cfg {
196196
#define CF_CFG_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
197197
static struct gatt_cf_cfg cf_cfg[CF_CFG_MAX] = {};
198198

199-
static struct gatt_cf_cfg *find_cf_cfg(const bt_addr_le_t *addr)
199+
static struct gatt_cf_cfg *find_cf_cfg(struct bt_conn *conn)
200200
{
201201
int i;
202202

203203
for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
204-
if (!bt_addr_le_cmp(&cf_cfg[i].peer, addr)) {
204+
if (!conn) {
205+
if (!bt_addr_le_cmp(&cf_cfg[i].peer, BT_ADDR_LE_ANY)) {
206+
return &cf_cfg[i];
207+
}
208+
} else if (!bt_conn_addr_le_cmp(conn, &cf_cfg[i].peer)) {
205209
return &cf_cfg[i];
206210
}
207211
}
@@ -215,7 +219,7 @@ static ssize_t cf_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
215219
struct gatt_cf_cfg *cfg;
216220
u8_t data[1] = {};
217221

218-
cfg = find_cf_cfg(&conn->le.dst);
222+
cfg = find_cf_cfg(conn);
219223
if (cfg) {
220224
memcpy(data, cfg->data, sizeof(data));
221225
}
@@ -268,9 +272,9 @@ static ssize_t cf_write(struct bt_conn *conn, const struct bt_gatt_attr *attr,
268272
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
269273
}
270274

271-
cfg = find_cf_cfg(&conn->le.dst);
275+
cfg = find_cf_cfg(conn);
272276
if (!cfg) {
273-
cfg = find_cf_cfg(BT_ADDR_LE_ANY);
277+
cfg = find_cf_cfg(NULL);
274278
}
275279

276280
if (!cfg) {
@@ -421,7 +425,31 @@ static ssize_t db_hash_read(struct bt_conn *conn,
421425
return bt_gatt_attr_read(conn, attr, buf, len, offset, db_hash,
422426
sizeof(db_hash));
423427
}
424-
#endif /* COFNIG_BT_GATT_CACHING */
428+
429+
static void remove_cf_cfg(struct bt_conn *conn)
430+
{
431+
struct gatt_cf_cfg *cfg;
432+
433+
cfg = find_cf_cfg(conn);
434+
if (!cfg) {
435+
return;
436+
}
437+
438+
/* BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2405:
439+
* For clients with a trusted relationship, the characteristic value
440+
* shall be persistent across connections. For clients without a
441+
* trusted relationship the characteristic value shall be set to the
442+
* default value at each connection.
443+
*/
444+
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
445+
bt_addr_le_copy(&cfg->peer, BT_ADDR_LE_ANY);
446+
memset(cfg->data, 0, sizeof(cfg->data));
447+
} else {
448+
/* Update address in case it has changed */
449+
bt_addr_le_copy(&cfg->peer, &conn->le.dst);
450+
}
451+
}
452+
#endif /* CONFIG_BT_GATT_CACHING */
425453

426454
static struct bt_gatt_attr gatt_attrs[] = {
427455
BT_GATT_PRIMARY_SERVICE(BT_UUID_GATT),
@@ -527,7 +555,7 @@ static void sc_indicate_rsp(struct bt_conn *conn,
527555
* A connected client becomes change-aware when...
528556
* The client receives and confirms a Service Changed indication.
529557
*/
530-
cfg = find_cf_cfg(&conn->le.dst);
558+
cfg = find_cf_cfg(conn);
531559
if (cfg && CF_ROBUST_CACHING(cfg)) {
532560
atomic_set_bit(cfg->flags, CF_CHANGE_AWARE);
533561
BT_DBG("%s change-aware", bt_addr_le_str(&cfg->peer));
@@ -2748,7 +2776,7 @@ bool bt_gatt_change_aware(struct bt_conn *conn, bool req)
27482776
#if defined(CONFIG_BT_GATT_CACHING)
27492777
struct gatt_cf_cfg *cfg;
27502778

2751-
cfg = find_cf_cfg(&conn->le.dst);
2779+
cfg = find_cf_cfg(conn);
27522780
if (!cfg || !CF_ROBUST_CACHING(cfg)) {
27532781
return true;
27542782
}
@@ -2808,6 +2836,10 @@ void bt_gatt_disconnected(struct bt_conn *conn)
28082836
#if defined(CONFIG_BT_GATT_CLIENT)
28092837
remove_subscriptions(conn);
28102838
#endif /* CONFIG_BT_GATT_CLIENT */
2839+
2840+
#if defined(CONFIG_BT_GATT_CACHING)
2841+
remove_cf_cfg(conn);
2842+
#endif
28112843
}
28122844

28132845
#if defined(CONFIG_BT_SETTINGS)

0 commit comments

Comments
 (0)