-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
gatt.c
5224 lines (4207 loc) · 123 KB
/
gatt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* gatt.c - Generic Attribute Profile handling */
/*
* Copyright (c) 2015-2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <sys/atomic.h>
#include <sys/byteorder.h>
#include <sys/util.h>
#include <settings/settings.h>
#if defined(CONFIG_BT_GATT_CACHING)
#include <tinycrypt/constants.h>
#include <tinycrypt/utils.h>
#include <tinycrypt/aes.h>
#include <tinycrypt/cmac_mode.h>
#include <tinycrypt/ccm_mode.h>
#endif /* CONFIG_BT_GATT_CACHING */
#include <bluetooth/hci.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <drivers/bluetooth/hci_driver.h>
#define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_GATT)
#define LOG_MODULE_NAME bt_gatt
#include "common/log.h"
#include "hci_core.h"
#include "conn_internal.h"
#include "keys.h"
#include "l2cap_internal.h"
#include "att_internal.h"
#include "smp.h"
#include "settings.h"
#include "gatt_internal.h"
#define SC_TIMEOUT K_MSEC(10)
#define CCC_STORE_DELAY K_SECONDS(1)
#define DB_HASH_TIMEOUT K_MSEC(10)
static uint16_t last_static_handle;
/* Persistent storage format for GATT CCC */
struct ccc_store {
uint16_t handle;
uint16_t value;
};
struct gatt_sub {
uint8_t id;
bt_addr_le_t peer;
sys_slist_t list;
};
#if defined(CONFIG_BT_GATT_CLIENT)
#define SUB_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
#else
#define SUB_MAX 0
#endif /* CONFIG_BT_GATT_CLIENT */
static struct gatt_sub subscriptions[SUB_MAX];
static const uint16_t gap_appearance = CONFIG_BT_DEVICE_APPEARANCE;
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
static sys_slist_t db;
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
static atomic_t init;
static atomic_t service_init;
static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
const char *name = bt_get_name();
return bt_gatt_attr_read(conn, attr, buf, len, offset, name,
strlen(name));
}
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
static ssize_t write_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset,
uint8_t flags)
{
char value[CONFIG_BT_DEVICE_NAME_MAX] = {};
if (offset) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if (len >= sizeof(value)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
memcpy(value, buf, len);
bt_set_name(value);
return len;
}
#endif /* CONFIG_BT_DEVICE_NAME_GATT_WRITABLE */
static ssize_t read_appearance(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
uint16_t appearance = sys_cpu_to_le16(gap_appearance);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &appearance,
sizeof(appearance));
}
#if defined (CONFIG_BT_GAP_PERIPHERAL_PREF_PARAMS)
/* This checks if the range entered is valid */
BUILD_ASSERT(!(CONFIG_BT_PERIPHERAL_PREF_MIN_INT > 3200 &&
CONFIG_BT_PERIPHERAL_PREF_MIN_INT < 0xffff));
BUILD_ASSERT(!(CONFIG_BT_PERIPHERAL_PREF_MAX_INT > 3200 &&
CONFIG_BT_PERIPHERAL_PREF_MAX_INT < 0xffff));
BUILD_ASSERT(!(CONFIG_BT_PERIPHERAL_PREF_TIMEOUT > 3200 &&
CONFIG_BT_PERIPHERAL_PREF_TIMEOUT < 0xffff));
BUILD_ASSERT((CONFIG_BT_PERIPHERAL_PREF_MIN_INT == 0xffff) ||
(CONFIG_BT_PERIPHERAL_PREF_MIN_INT <=
CONFIG_BT_PERIPHERAL_PREF_MAX_INT));
static ssize_t read_ppcp(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct __packed {
uint16_t min_int;
uint16_t max_int;
uint16_t latency;
uint16_t timeout;
} ppcp;
ppcp.min_int = sys_cpu_to_le16(CONFIG_BT_PERIPHERAL_PREF_MIN_INT);
ppcp.max_int = sys_cpu_to_le16(CONFIG_BT_PERIPHERAL_PREF_MAX_INT);
ppcp.latency = sys_cpu_to_le16(CONFIG_BT_PERIPHERAL_PREF_SLAVE_LATENCY);
ppcp.timeout = sys_cpu_to_le16(CONFIG_BT_PERIPHERAL_PREF_TIMEOUT);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &ppcp,
sizeof(ppcp));
}
#endif
#if defined(CONFIG_BT_CENTRAL) && defined(CONFIG_BT_PRIVACY)
static ssize_t read_central_addr_res(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
uint16_t len, uint16_t offset)
{
uint8_t central_addr_res = BT_GATT_CENTRAL_ADDR_RES_SUPP;
return bt_gatt_attr_read(conn, attr, buf, len, offset,
¢ral_addr_res, sizeof(central_addr_res));
}
#endif /* CONFIG_BT_CENTRAL && CONFIG_BT_PRIVACY */
BT_GATT_SERVICE_DEFINE(_2_gap_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_GAP),
#if defined(CONFIG_BT_DEVICE_NAME_GATT_WRITABLE)
/* Require pairing for writes to device name */
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT,
read_name, write_name, bt_dev.name),
#else
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_DEVICE_NAME, BT_GATT_CHRC_READ,
BT_GATT_PERM_READ, read_name, NULL, NULL),
#endif /* CONFIG_BT_DEVICE_NAME_GATT_WRITABLE */
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_APPEARANCE, BT_GATT_CHRC_READ,
BT_GATT_PERM_READ, read_appearance, NULL, NULL),
#if defined(CONFIG_BT_CENTRAL) && defined(CONFIG_BT_PRIVACY)
BT_GATT_CHARACTERISTIC(BT_UUID_CENTRAL_ADDR_RES,
BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
read_central_addr_res, NULL, NULL),
#endif /* CONFIG_BT_CENTRAL && CONFIG_BT_PRIVACY */
#if defined(CONFIG_BT_GAP_PERIPHERAL_PREF_PARAMS)
BT_GATT_CHARACTERISTIC(BT_UUID_GAP_PPCP, BT_GATT_CHRC_READ,
BT_GATT_PERM_READ, read_ppcp, NULL, NULL),
#endif
);
struct sc_data {
uint16_t start;
uint16_t end;
} __packed;
struct gatt_sc_cfg {
uint8_t id;
bt_addr_le_t peer;
struct {
uint16_t start;
uint16_t end;
} data;
};
#if defined(CONFIG_BT_GATT_SERVICE_CHANGED)
#define SC_CFG_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
#else
#define SC_CFG_MAX 0
#endif
static struct gatt_sc_cfg sc_cfg[SC_CFG_MAX];
BUILD_ASSERT(sizeof(struct sc_data) == sizeof(sc_cfg[0].data));
static struct gatt_sc_cfg *find_sc_cfg(uint8_t id, bt_addr_le_t *addr)
{
BT_DBG("id: %u, addr: %s", id, bt_addr_le_str(addr));
for (size_t i = 0; i < ARRAY_SIZE(sc_cfg); i++) {
if (id == sc_cfg[i].id &&
!bt_addr_le_cmp(&sc_cfg[i].peer, addr)) {
return &sc_cfg[i];
}
}
return NULL;
}
static void sc_store(struct gatt_sc_cfg *cfg)
{
char key[BT_SETTINGS_KEY_MAX];
int err;
if (cfg->id) {
char id_str[4];
u8_to_dec(id_str, sizeof(id_str), cfg->id);
bt_settings_encode_key(key, sizeof(key), "sc",
&cfg->peer, id_str);
} else {
bt_settings_encode_key(key, sizeof(key), "sc",
&cfg->peer, NULL);
}
err = settings_save_one(key, (char *)&cfg->data, sizeof(cfg->data));
if (err) {
BT_ERR("failed to store SC (err %d)", err);
return;
}
BT_DBG("stored SC for %s (%s, 0x%04x-0x%04x)",
bt_addr_le_str(&cfg->peer), log_strdup(key), cfg->data.start,
cfg->data.end);
}
static void clear_sc_cfg(struct gatt_sc_cfg *cfg)
{
memset(cfg, 0, sizeof(*cfg));
}
static int bt_gatt_clear_sc(uint8_t id, const bt_addr_le_t *addr)
{
struct gatt_sc_cfg *cfg;
cfg = find_sc_cfg(id, (bt_addr_le_t *)addr);
if (!cfg) {
return 0;
}
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
char key[BT_SETTINGS_KEY_MAX];
int err;
if (cfg->id) {
char id_str[4];
u8_to_dec(id_str, sizeof(id_str), cfg->id);
bt_settings_encode_key(key, sizeof(key), "sc",
&cfg->peer, id_str);
} else {
bt_settings_encode_key(key, sizeof(key), "sc",
&cfg->peer, NULL);
}
err = settings_delete(key);
if (err) {
BT_ERR("failed to delete SC (err %d)", err);
} else {
BT_DBG("deleted SC for %s (%s)",
bt_addr_le_str(&cfg->peer),
log_strdup(key));
}
}
clear_sc_cfg(cfg);
return 0;
}
static void sc_clear(struct bt_conn *conn)
{
if (bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
int err;
err = bt_gatt_clear_sc(conn->id, &conn->le.dst);
if (err) {
BT_ERR("Failed to clear SC %d", err);
}
} else {
struct gatt_sc_cfg *cfg;
cfg = find_sc_cfg(conn->id, &conn->le.dst);
if (cfg) {
clear_sc_cfg(cfg);
}
}
}
static void sc_reset(struct gatt_sc_cfg *cfg)
{
BT_DBG("peer %s", bt_addr_le_str(&cfg->peer));
memset(&cfg->data, 0, sizeof(cfg->data));
if (IS_ENABLED(CONFIG_BT_SETTINGS)) {
sc_store(cfg);
}
}
static bool update_range(uint16_t *start, uint16_t *end, uint16_t new_start,
uint16_t new_end)
{
BT_DBG("start 0x%04x end 0x%04x new_start 0x%04x new_end 0x%04x",
*start, *end, new_start, new_end);
/* Check if inside existing range */
if (new_start >= *start && new_end <= *end) {
return false;
}
/* Update range */
if (*start > new_start) {
*start = new_start;
}
if (*end < new_end) {
*end = new_end;
}
return true;
}
static void sc_save(uint8_t id, bt_addr_le_t *peer, uint16_t start, uint16_t end)
{
struct gatt_sc_cfg *cfg;
bool modified = false;
BT_DBG("peer %s start 0x%04x end 0x%04x", bt_addr_le_str(peer), start,
end);
cfg = find_sc_cfg(id, peer);
if (!cfg) {
/* Find and initialize a free sc_cfg entry */
cfg = find_sc_cfg(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
if (!cfg) {
BT_ERR("unable to save SC: no cfg left");
return;
}
cfg->id = id;
bt_addr_le_copy(&cfg->peer, peer);
}
/* Check if there is any change stored */
if (!(cfg->data.start || cfg->data.end)) {
cfg->data.start = start;
cfg->data.end = end;
modified = true;
goto done;
}
modified = update_range(&cfg->data.start, &cfg->data.end, start, end);
done:
if (IS_ENABLED(CONFIG_BT_SETTINGS) &&
modified && bt_addr_le_is_bonded(cfg->id, &cfg->peer)) {
sc_store(cfg);
}
}
static ssize_t sc_ccc_cfg_write(struct bt_conn *conn,
const struct bt_gatt_attr *attr, uint16_t value)
{
BT_DBG("value 0x%04x", value);
if (value == BT_GATT_CCC_INDICATE) {
/* Create a new SC configuration entry if subscribed */
sc_save(conn->id, &conn->le.dst, 0, 0);
} else {
sc_clear(conn);
}
return sizeof(value);
}
static struct _bt_gatt_ccc sc_ccc = BT_GATT_CCC_INITIALIZER(NULL,
sc_ccc_cfg_write,
NULL);
enum {
CF_CHANGE_AWARE, /* Client is changed aware */
CF_OUT_OF_SYNC, /* Client is out of sync */
/* Total number of flags - must be at the end of the enum */
CF_NUM_FLAGS,
};
#define CF_BIT_ROBUST_CACHING 0
#define CF_BIT_EATT 1
#define CF_BIT_NOTIFY_MULTI 2
#define CF_BIT_LAST CF_BIT_NOTIFY_MULTI
#define CF_NUM_BITS (CF_BIT_LAST + 1)
#define CF_NUM_BYTES ((CF_BIT_LAST / 8) + 1)
#define CF_ROBUST_CACHING(_cfg) (_cfg->data[0] & BIT(CF_BIT_ROBUST_CACHING))
#define CF_EATT(_cfg) (_cfg->data[0] & BIT(CF_BIT_EATT))
#define CF_NOTIFY_MULTI(_cfg) (_cfg->data[0] & BIT(CF_BIT_NOTIFY_MULTI))
struct gatt_cf_cfg {
uint8_t id;
bt_addr_le_t peer;
uint8_t data[CF_NUM_BYTES];
ATOMIC_DEFINE(flags, CF_NUM_FLAGS);
};
#if defined(CONFIG_BT_GATT_CACHING)
#define CF_CFG_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
#else
#define CF_CFG_MAX 0
#endif /* CONFIG_BT_GATT_CACHING */
static struct gatt_cf_cfg cf_cfg[CF_CFG_MAX] = {};
static void clear_cf_cfg(struct gatt_cf_cfg *cfg)
{
bt_addr_le_copy(&cfg->peer, BT_ADDR_LE_ANY);
memset(cfg->data, 0, sizeof(cfg->data));
atomic_set(cfg->flags, 0);
}
#if defined(CONFIG_BT_GATT_CACHING)
static struct gatt_cf_cfg *find_cf_cfg(struct bt_conn *conn)
{
int i;
for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
struct gatt_cf_cfg *cfg = &cf_cfg[i];
if (!conn) {
if (!bt_addr_le_cmp(&cfg->peer, BT_ADDR_LE_ANY)) {
return cfg;
}
} else if (bt_conn_is_peer_addr_le(conn, cfg->id, &cfg->peer)) {
return cfg;
}
}
return NULL;
}
static ssize_t cf_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
struct gatt_cf_cfg *cfg;
uint8_t data[1] = {};
cfg = find_cf_cfg(conn);
if (cfg) {
memcpy(data, cfg->data, sizeof(data));
}
return bt_gatt_attr_read(conn, attr, buf, len, offset, data,
sizeof(data));
}
static bool cf_set_value(struct gatt_cf_cfg *cfg, const uint8_t *value, uint16_t len)
{
uint16_t i;
/* Validate the bits */
for (i = 0U; i <= CF_BIT_LAST && (i / 8) < len; i++) {
if ((cfg->data[i / 8] & BIT(i % 8)) &&
!(value[i / 8] & BIT(i % 8))) {
/* A client shall never clear a bit it has set */
return false;
}
}
/* Set the bits for each octect */
for (i = 0U; i < len && i < CF_NUM_BYTES; i++) {
if (i == (CF_NUM_BYTES - 1)) {
cfg->data[i] |= value[i] & BIT_MASK(CF_NUM_BITS % 8);
} else {
cfg->data[i] |= value[i];
}
BT_DBG("byte %u: data 0x%02x value 0x%02x", i, cfg->data[i],
value[i]);
}
return true;
}
static ssize_t cf_write(struct bt_conn *conn, const struct bt_gatt_attr *attr,
const void *buf, uint16_t len, uint16_t offset, uint8_t flags)
{
struct gatt_cf_cfg *cfg;
const uint8_t *value = buf;
if (offset > sizeof(cfg->data)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
if (offset + len > sizeof(cfg->data)) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
}
cfg = find_cf_cfg(conn);
if (!cfg) {
cfg = find_cf_cfg(NULL);
}
if (!cfg) {
BT_WARN("No space to store Client Supported Features");
return BT_GATT_ERR(BT_ATT_ERR_INSUFFICIENT_RESOURCES);
}
BT_DBG("handle 0x%04x len %u", attr->handle, len);
if (!cf_set_value(cfg, value, len)) {
return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED);
}
bt_addr_le_copy(&cfg->peer, &conn->le.dst);
cfg->id = conn->id;
atomic_set_bit(cfg->flags, CF_CHANGE_AWARE);
return len;
}
static uint8_t db_hash[16];
struct k_delayed_work db_hash_work;
struct gen_hash_state {
struct tc_cmac_struct state;
int err;
};
static uint8_t gen_hash_m(const struct bt_gatt_attr *attr, uint16_t handle,
void *user_data)
{
struct gen_hash_state *state = user_data;
struct bt_uuid_16 *u16;
uint8_t data[16];
ssize_t len;
uint16_t value;
if (attr->uuid->type != BT_UUID_TYPE_16)
return BT_GATT_ITER_CONTINUE;
u16 = (struct bt_uuid_16 *)attr->uuid;
switch (u16->val) {
/* Attributes to hash: handle + UUID + value */
case BT_UUID_GATT_PRIMARY_VAL:
case BT_UUID_GATT_SECONDARY_VAL:
case BT_UUID_GATT_INCLUDE_VAL:
case BT_UUID_GATT_CHRC_VAL:
case BT_UUID_GATT_CEP_VAL:
value = sys_cpu_to_le16(handle);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(handle)) == TC_CRYPTO_FAIL) {
state->err = -EINVAL;
return BT_GATT_ITER_STOP;
}
value = sys_cpu_to_le16(u16->val);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(u16->val)) == TC_CRYPTO_FAIL) {
state->err = -EINVAL;
return BT_GATT_ITER_STOP;
}
len = attr->read(NULL, attr, data, sizeof(data), 0);
if (len < 0) {
state->err = len;
return BT_GATT_ITER_STOP;
}
if (tc_cmac_update(&state->state, data, len) ==
TC_CRYPTO_FAIL) {
state->err = -EINVAL;
return BT_GATT_ITER_STOP;
}
break;
/* Attributes to hash: handle + UUID */
case BT_UUID_GATT_CUD_VAL:
case BT_UUID_GATT_CCC_VAL:
case BT_UUID_GATT_SCC_VAL:
case BT_UUID_GATT_CPF_VAL:
case BT_UUID_GATT_CAF_VAL:
value = sys_cpu_to_le16(handle);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(handle)) == TC_CRYPTO_FAIL) {
state->err = -EINVAL;
return BT_GATT_ITER_STOP;
}
value = sys_cpu_to_le16(u16->val);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(u16->val)) == TC_CRYPTO_FAIL) {
state->err = -EINVAL;
return BT_GATT_ITER_STOP;
}
break;
default:
return BT_GATT_ITER_CONTINUE;
}
return BT_GATT_ITER_CONTINUE;
}
static void db_hash_store(void)
{
int err;
err = settings_save_one("bt/hash", &db_hash, sizeof(db_hash));
if (err) {
BT_ERR("Failed to save Database Hash (err %d)", err);
}
BT_DBG("Database Hash stored");
}
/* Once the db_hash work has started we cannot cancel it anymore, so the
* assumption is made that the in-progress work cannot be pre-empted.
* This assumption should hold as long as calculation does not make any calls
* that would make it unready.
* If this assumption is no longer true we will have to solve the case where
* k_delayed_work_cancel failed because the work was in-progress but pre-empted.
*/
static void db_hash_gen(bool store)
{
uint8_t key[16] = {};
struct tc_aes_key_sched_struct sched;
struct gen_hash_state state;
if (tc_cmac_setup(&state.state, key, &sched) == TC_CRYPTO_FAIL) {
BT_ERR("Unable to setup AES CMAC");
return;
}
bt_gatt_foreach_attr(0x0001, 0xffff, gen_hash_m, &state);
if (tc_cmac_final(db_hash, &state.state) == TC_CRYPTO_FAIL) {
BT_ERR("Unable to calculate hash");
return;
}
/**
* Core 5.1 does not state the endianess of the hash.
* However Vol 3, Part F, 3.3.1 says that multi-octet Characteristic
* Values shall be LE unless otherwise defined. PTS expects hash to be
* in little endianess as well. bt_smp_aes_cmac calculates the hash in
* big endianess so we have to swap.
*/
sys_mem_swap(db_hash, sizeof(db_hash));
BT_HEXDUMP_DBG(db_hash, sizeof(db_hash), "Hash: ");
if (IS_ENABLED(CONFIG_BT_SETTINGS) && store) {
db_hash_store();
}
}
static void db_hash_process(struct k_work *work)
{
db_hash_gen(true);
}
static ssize_t db_hash_read(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
int err;
/* Check if db_hash is already pending in which case it shall be
* generated immediately instead of waiting for the work to complete.
*/
err = k_delayed_work_cancel(&db_hash_work);
if (!err) {
db_hash_gen(true);
}
/* BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2347:
* 2.5.2.1 Robust Caching
* A connected client becomes change-aware when...
* The client reads the Database Hash characteristic and then the server
* receives another ATT request from the client.
*/
bt_gatt_change_aware(conn, true);
return bt_gatt_attr_read(conn, attr, buf, len, offset, db_hash,
sizeof(db_hash));
}
static void remove_cf_cfg(struct bt_conn *conn)
{
struct gatt_cf_cfg *cfg;
cfg = find_cf_cfg(conn);
if (!cfg) {
return;
}
/* 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.
*/
if (!bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
clear_cf_cfg(cfg);
} else {
/* Update address in case it has changed */
bt_addr_le_copy(&cfg->peer, &conn->le.dst);
atomic_clear_bit(cfg->flags, CF_OUT_OF_SYNC);
}
}
#if defined(CONFIG_BT_EATT)
#define SF_BIT_EATT 0
#define SF_BIT_LAST SF_BIT_EATT
static ssize_t sf_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, uint16_t len, uint16_t offset)
{
uint8_t value = BIT(SF_BIT_EATT);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &value,
sizeof(value));
}
#endif /* CONFIG_BT_EATT */
#endif /* CONFIG_BT_GATT_CACHING */
BT_GATT_SERVICE_DEFINE(_1_gatt_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_GATT),
#if defined(CONFIG_BT_GATT_SERVICE_CHANGED)
/* Bluetooth 5.0, Vol3 Part G:
* The Service Changed characteristic Attribute Handle on the server
* shall not change if the server has a trusted relationship with any
* client.
*/
BT_GATT_CHARACTERISTIC(BT_UUID_GATT_SC, BT_GATT_CHRC_INDICATE,
BT_GATT_PERM_NONE, NULL, NULL, NULL),
BT_GATT_CCC_MANAGED(&sc_ccc, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),
#if defined(CONFIG_BT_GATT_CACHING)
BT_GATT_CHARACTERISTIC(BT_UUID_GATT_CLIENT_FEATURES,
BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
cf_read, cf_write, NULL),
BT_GATT_CHARACTERISTIC(BT_UUID_GATT_DB_HASH,
BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
db_hash_read, NULL, NULL),
#if defined(CONFIG_BT_EATT)
BT_GATT_CHARACTERISTIC(BT_UUID_GATT_SERVER_FEATURES,
BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
sf_read, NULL, NULL),
#endif /* CONFIG_BT_EATT */
#endif /* CONFIG_BT_GATT_CACHING */
#endif /* CONFIG_BT_GATT_SERVICE_CHANGED */
);
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
static uint8_t found_attr(const struct bt_gatt_attr *attr, uint16_t handle,
void *user_data)
{
const struct bt_gatt_attr **found = user_data;
*found = attr;
return BT_GATT_ITER_STOP;
}
static const struct bt_gatt_attr *find_attr(uint16_t handle)
{
const struct bt_gatt_attr *attr = NULL;
bt_gatt_foreach_attr(handle, handle, found_attr, &attr);
return attr;
}
static void gatt_insert(struct bt_gatt_service *svc, uint16_t last_handle)
{
struct bt_gatt_service *tmp, *prev = NULL;
if (last_handle == 0 || svc->attrs[0].handle > last_handle) {
sys_slist_append(&db, &svc->node);
return;
}
/* DB shall always have its service in ascending order */
SYS_SLIST_FOR_EACH_CONTAINER(&db, tmp, node) {
if (tmp->attrs[0].handle > svc->attrs[0].handle) {
if (prev) {
sys_slist_insert(&db, &prev->node, &svc->node);
} else {
sys_slist_prepend(&db, &svc->node);
}
return;
}
prev = tmp;
}
}
static int gatt_register(struct bt_gatt_service *svc)
{
struct bt_gatt_service *last;
uint16_t handle, last_handle;
struct bt_gatt_attr *attrs = svc->attrs;
uint16_t count = svc->attr_count;
if (sys_slist_is_empty(&db)) {
handle = last_static_handle;
last_handle = 0;
goto populate;
}
last = SYS_SLIST_PEEK_TAIL_CONTAINER(&db, last, node);
handle = last->attrs[last->attr_count - 1].handle;
last_handle = handle;
populate:
/* Populate the handles and append them to the list */
for (; attrs && count; attrs++, count--) {
if (!attrs->handle) {
/* Allocate handle if not set already */
attrs->handle = ++handle;
} else if (attrs->handle > handle) {
/* Use existing handle if valid */
handle = attrs->handle;
} else if (find_attr(attrs->handle)) {
/* Service has conflicting handles */
BT_ERR("Unable to register handle 0x%04x",
attrs->handle);
return -EINVAL;
}
BT_DBG("attr %p handle 0x%04x uuid %s perm 0x%02x",
attrs, attrs->handle, bt_uuid_str(attrs->uuid),
attrs->perm);
}
gatt_insert(svc, last_handle);
return 0;
}
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
enum {
SC_RANGE_CHANGED, /* SC range changed */
SC_INDICATE_PENDING, /* SC indicate pending */
/* Total number of flags - must be at the end of the enum */
SC_NUM_FLAGS,
};
#if defined(CONFIG_BT_GATT_SERVICE_CHANGED)
static struct gatt_sc {
struct bt_gatt_indicate_params params;
uint16_t start;
uint16_t end;
struct k_delayed_work work;
ATOMIC_DEFINE(flags, SC_NUM_FLAGS);
} gatt_sc;
#endif /* defined(CONFIG_BT_GATT_SERVICE_CHANGED) */
static inline void sc_work_submit(k_timeout_t timeout)
{
#if defined(CONFIG_BT_GATT_SERVICE_CHANGED)
k_delayed_work_submit(&gatt_sc.work, timeout);
#endif
}
#if defined(CONFIG_BT_GATT_SERVICE_CHANGED)
static void sc_indicate_rsp(struct bt_conn *conn,
struct bt_gatt_indicate_params *params, uint8_t err)
{
#if defined(CONFIG_BT_GATT_CACHING)
struct gatt_cf_cfg *cfg;
#endif
BT_DBG("err 0x%02x", err);
atomic_clear_bit(gatt_sc.flags, SC_INDICATE_PENDING);
/* Check if there is new change in the meantime */
if (atomic_test_bit(gatt_sc.flags, SC_RANGE_CHANGED)) {
/* Reschedule without any delay since it is waiting already */
sc_work_submit(K_NO_WAIT);
}
#if defined(CONFIG_BT_GATT_CACHING)
/* BLUETOOTH CORE SPECIFICATION Version 5.1 | Vol 3, Part G page 2347:
* 2.5.2.1 Robust Caching
* A connected client becomes change-aware when...
* The client receives and confirms a Service Changed indication.
*/
cfg = find_cf_cfg(conn);
if (cfg && CF_ROBUST_CACHING(cfg)) {
atomic_set_bit(cfg->flags, CF_CHANGE_AWARE);
BT_DBG("%s change-aware", bt_addr_le_str(&cfg->peer));
}
#endif
}
static void sc_process(struct k_work *work)
{
struct gatt_sc *sc = CONTAINER_OF(work, struct gatt_sc, work);
uint16_t sc_range[2];
__ASSERT(!atomic_test_bit(sc->flags, SC_INDICATE_PENDING),
"Indicate already pending");
BT_DBG("start 0x%04x end 0x%04x", sc->start, sc->end);
sc_range[0] = sys_cpu_to_le16(sc->start);
sc_range[1] = sys_cpu_to_le16(sc->end);
atomic_clear_bit(sc->flags, SC_RANGE_CHANGED);
sc->start = 0U;
sc->end = 0U;
sc->params.attr = &_1_gatt_svc.attrs[2];
sc->params.func = sc_indicate_rsp;
sc->params.data = &sc_range[0];
sc->params.len = sizeof(sc_range);
if (bt_gatt_indicate(NULL, &sc->params)) {
/* No connections to indicate */
return;
}
atomic_set_bit(sc->flags, SC_INDICATE_PENDING);
}
#endif /* defined(CONFIG_BT_GATT_SERVICE_CHANGED) */
static void clear_ccc_cfg(struct bt_gatt_ccc_cfg *cfg)
{
bt_addr_le_copy(&cfg->peer, BT_ADDR_LE_ANY);
cfg->id = 0U;
cfg->value = 0U;
}
#if defined(CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE)
static struct gatt_ccc_store {
struct bt_conn *conn_list[CONFIG_BT_MAX_CONN];
struct k_delayed_work work;
} gatt_ccc_store;
static bool gatt_ccc_conn_is_queued(struct bt_conn *conn)
{
return (conn == gatt_ccc_store.conn_list[bt_conn_index(conn)]);
}
static void gatt_ccc_conn_unqueue(struct bt_conn *conn)
{
uint8_t index = bt_conn_index(conn);
if (gatt_ccc_store.conn_list[index] != NULL) {
bt_conn_unref(gatt_ccc_store.conn_list[index]);
gatt_ccc_store.conn_list[index] = NULL;
}
}
static void gatt_ccc_conn_enqueue(struct bt_conn *conn)
{
if ((!gatt_ccc_conn_is_queued(conn)) &&
bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
/* Store the connection with the same index it has in
* the conns array