@@ -196,12 +196,16 @@ struct gatt_cf_cfg {
196
196
#define CF_CFG_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
197
197
static struct gatt_cf_cfg cf_cfg [CF_CFG_MAX ] = {};
198
198
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 )
200
200
{
201
201
int i ;
202
202
203
203
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 )) {
205
209
return & cf_cfg [i ];
206
210
}
207
211
}
@@ -215,7 +219,7 @@ static ssize_t cf_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
215
219
struct gatt_cf_cfg * cfg ;
216
220
u8_t data [1 ] = {};
217
221
218
- cfg = find_cf_cfg (& conn -> le . dst );
222
+ cfg = find_cf_cfg (conn );
219
223
if (cfg ) {
220
224
memcpy (data , cfg -> data , sizeof (data ));
221
225
}
@@ -268,9 +272,9 @@ static ssize_t cf_write(struct bt_conn *conn, const struct bt_gatt_attr *attr,
268
272
return BT_GATT_ERR (BT_ATT_ERR_INVALID_ATTRIBUTE_LEN );
269
273
}
270
274
271
- cfg = find_cf_cfg (& conn -> le . dst );
275
+ cfg = find_cf_cfg (conn );
272
276
if (!cfg ) {
273
- cfg = find_cf_cfg (BT_ADDR_LE_ANY );
277
+ cfg = find_cf_cfg (NULL );
274
278
}
275
279
276
280
if (!cfg ) {
@@ -421,7 +425,31 @@ static ssize_t db_hash_read(struct bt_conn *conn,
421
425
return bt_gatt_attr_read (conn , attr , buf , len , offset , db_hash ,
422
426
sizeof (db_hash ));
423
427
}
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 */
425
453
426
454
static struct bt_gatt_attr gatt_attrs [] = {
427
455
BT_GATT_PRIMARY_SERVICE (BT_UUID_GATT ),
@@ -527,7 +555,7 @@ static void sc_indicate_rsp(struct bt_conn *conn,
527
555
* A connected client becomes change-aware when...
528
556
* The client receives and confirms a Service Changed indication.
529
557
*/
530
- cfg = find_cf_cfg (& conn -> le . dst );
558
+ cfg = find_cf_cfg (conn );
531
559
if (cfg && CF_ROBUST_CACHING (cfg )) {
532
560
atomic_set_bit (cfg -> flags , CF_CHANGE_AWARE );
533
561
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)
2748
2776
#if defined(CONFIG_BT_GATT_CACHING )
2749
2777
struct gatt_cf_cfg * cfg ;
2750
2778
2751
- cfg = find_cf_cfg (& conn -> le . dst );
2779
+ cfg = find_cf_cfg (conn );
2752
2780
if (!cfg || !CF_ROBUST_CACHING (cfg )) {
2753
2781
return true;
2754
2782
}
@@ -2808,6 +2836,10 @@ void bt_gatt_disconnected(struct bt_conn *conn)
2808
2836
#if defined(CONFIG_BT_GATT_CLIENT )
2809
2837
remove_subscriptions (conn );
2810
2838
#endif /* CONFIG_BT_GATT_CLIENT */
2839
+
2840
+ #if defined(CONFIG_BT_GATT_CACHING )
2841
+ remove_cf_cfg (conn );
2842
+ #endif
2811
2843
}
2812
2844
2813
2845
#if defined(CONFIG_BT_SETTINGS )
0 commit comments