/
gatt.c
3566 lines (2875 loc) · 84.6 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 <atomic.h>
#include <misc/byteorder.h>
#include <misc/util.h>
#include <settings/settings.h>
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
#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_DYNAMIC_DB */
#include <bluetooth/hci.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/conn.h>
#include <bluetooth/uuid.h>
#include <bluetooth/gatt.h>
#include <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)
/* Linker-defined symbols bound to the bt_gatt_service_static structs */
extern const struct bt_gatt_service_static _bt_services_start[];
extern const struct bt_gatt_service_static _bt_services_end[];
static u16_t last_static_handle;
/* Persistent storage format for GATT CCC */
struct ccc_store {
u16_t handle;
u16_t value;
};
#if defined(CONFIG_BT_GATT_CLIENT)
static sys_slist_t subscriptions;
#endif /* CONFIG_BT_GATT_CLIENT */
static const u16_t gap_appearance = CONFIG_BT_DEVICE_APPEARANCE;
static sys_slist_t db;
static atomic_t init;
static ssize_t read_name(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_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, u16_t len, u16_t offset,
u8_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,
u16_t len, u16_t offset)
{
u16_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, u16_t len, u16_t offset)
{
struct __packed {
u16_t min_int;
u16_t max_int;
u16_t latency;
u16_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,
u16_t len, u16_t offset)
{
u8_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
);
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
static struct bt_gatt_ccc_cfg sc_ccc_cfg[BT_GATT_CCC_MAX] = {};
static void sc_ccc_cfg_changed(const struct bt_gatt_attr *attr,
u16_t value)
{
BT_DBG("value 0x%04x", value);
}
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_ROBUST_CACHING(_cfg) (_cfg->data[0] & BIT(0))
struct gatt_cf_cfg {
u8_t id;
bt_addr_le_t peer;
u8_t data[1];
ATOMIC_DEFINE(flags, CF_NUM_FLAGS);
};
#if defined(CONFIG_BT_GATT_CACHING)
#define CF_CFG_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
static struct gatt_cf_cfg cf_cfg[CF_CFG_MAX] = {};
static struct gatt_cf_cfg *find_cf_cfg(struct bt_conn *conn)
{
int i;
for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
if (!conn) {
if (!bt_addr_le_cmp(&cf_cfg[i].peer, BT_ADDR_LE_ANY)) {
return &cf_cfg[i];
}
} else if (!bt_conn_addr_le_cmp(conn, &cf_cfg[i].peer)) {
return &cf_cfg[i];
}
}
return NULL;
}
static ssize_t cf_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
struct gatt_cf_cfg *cfg;
u8_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 u8_t *value, u16_t len)
{
u16_t i;
u8_t last_byte = 1U;
u8_t last_bit = 1U;
/* Validate the bits */
for (i = 0U; i < len && i < last_byte; i++) {
u8_t chg_bits = value[i] ^ cfg->data[i];
u8_t bit;
for (bit = 0U; bit < last_bit; bit++) {
/* A client shall never clear a bit it has set */
if ((BIT(bit) & chg_bits) &&
(BIT(bit) & cfg->data[i])) {
return false;
}
}
}
/* Set the bits for each octect */
for (i = 0U; i < len && i < last_byte; i++) {
cfg->data[i] |= value[i] & ((1 << last_bit) - 1);
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, u16_t len, u16_t offset, u8_t flags)
{
struct gatt_cf_cfg *cfg;
const u8_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);
atomic_set_bit(cfg->flags, CF_CHANGE_AWARE);
return len;
}
static u8_t db_hash[16];
struct k_delayed_work db_hash_work;
struct gen_hash_state {
struct tc_cmac_struct state;
int err;
};
static u8_t gen_hash_m(const struct bt_gatt_attr *attr, void *user_data)
{
struct gen_hash_state *state = user_data;
struct bt_uuid_16 *u16;
u8_t data[16];
ssize_t len;
u16_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 0x2800: /* GATT Primary Service */
case 0x2801: /* GATT Secondary Service */
case 0x2802: /* GATT Include Service */
case 0x2803: /* GATT Characteristic */
case 0x2900: /* GATT Characteristic Extended Properties */
value = sys_cpu_to_le16(attr->handle);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(attr->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 0x2901: /* GATT Characteristic User Descriptor */
case 0x2902: /* GATT Client Characteristic Configuration */
case 0x2903: /* GATT Server Characteristic Configuration */
case 0x2904: /* GATT Characteristic Presentation Format */
case 0x2905: /* GATT Characteristic Aggregated Format */
value = sys_cpu_to_le16(attr->handle);
if (tc_cmac_update(&state->state, (uint8_t *)&value,
sizeof(attr->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");
}
static void db_hash_gen(bool store)
{
u8_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;
}
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, u16_t len, u16_t offset)
{
/* Check if db_hash is already pending in which case it shall be
* generated immediately instead of waiting the work to complete.
*/
if (k_delayed_work_remaining_get(&db_hash_work)) {
k_delayed_work_cancel(&db_hash_work);
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 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);
}
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);
}
}
#endif /* CONFIG_BT_GATT_CACHING */
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
BT_GATT_SERVICE_DEFINE(_1_gatt_svc,
BT_GATT_PRIMARY_SERVICE(BT_UUID_GATT),
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
/* 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(sc_ccc_cfg, sc_ccc_cfg_changed),
#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),
#endif /* CONFIG_BT_GATT_CACHING */
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
);
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
static int gatt_register(struct bt_gatt_service *svc)
{
struct bt_gatt_service *last;
u16_t handle;
struct bt_gatt_attr *attrs = svc->attrs;
u16_t count = svc->attr_count;
if (sys_slist_is_empty(&db)) {
handle = last_static_handle;
goto populate;
}
last = SYS_SLIST_PEEK_TAIL_CONTAINER(&db, last, node);
handle = last->attrs[last->attr_count - 1].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 {
/* 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);
}
sys_slist_append(&db, &svc->node);
return 0;
}
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,
};
static struct gatt_sc {
struct bt_gatt_indicate_params params;
u16_t start;
u16_t end;
struct k_delayed_work work;
ATOMIC_DEFINE(flags, SC_NUM_FLAGS);
} gatt_sc;
static void sc_indicate_rsp(struct bt_conn *conn,
const struct bt_gatt_attr *attr, u8_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 */
k_delayed_work_submit(&gatt_sc.work, 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);
u16_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 /* CONFIG_BT_GATT_DYNAMIC_DB */
#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)
{
u8_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 bool gatt_ccc_conn_queue_is_empty(void)
{
for (size_t i = 0; i < CONFIG_BT_MAX_CONN; i++) {
if (gatt_ccc_store.conn_list[i]) {
return false;
}
}
return true;
}
static void ccc_delayed_store(struct k_work *work)
{
struct gatt_ccc_store *ccc_store =
CONTAINER_OF(work, struct gatt_ccc_store, work);
for (size_t i = 0; i < CONFIG_BT_MAX_CONN; i++) {
struct bt_conn *conn = ccc_store->conn_list[i];
if (!conn) {
continue;
}
if (bt_addr_le_is_bonded(conn->id, &conn->le.dst)) {
bt_gatt_store_ccc(conn->id, &conn->le.dst);
bt_conn_unref(conn);
ccc_store->conn_list[i] = NULL;
}
}
}
#endif
void bt_gatt_init(void)
{
const struct bt_gatt_service_static *svc;
if (!atomic_cas(&init, 0, 1)) {
return;
}
for (svc = _bt_services_start; svc < _bt_services_end; svc++) {
last_static_handle += svc->attr_count;
}
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
#if defined(CONFIG_BT_GATT_CACHING)
k_delayed_work_init(&db_hash_work, db_hash_process);
db_hash_gen(false);
#endif /* COFNIG_BT_GATT_CACHING */
k_delayed_work_init(&gatt_sc.work, sc_process);
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
#if defined(CONFIG_BT_SETTINGS_CCC_STORE_ON_WRITE)
k_delayed_work_init(&gatt_ccc_store.work, ccc_delayed_store);
#endif
}
#if defined(CONFIG_BT_GATT_DYNAMIC_DB)
static bool update_range(u16_t *start, u16_t *end, u16_t new_start,
u16_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_indicate(struct gatt_sc *sc, u16_t start, u16_t end)
{
if (!atomic_test_and_set_bit(sc->flags, SC_RANGE_CHANGED)) {
sc->start = start;
sc->end = end;
goto submit;
}
if (!update_range(&sc->start, &sc->end, start, end)) {
return;
}
submit:
if (atomic_test_bit(sc->flags, SC_INDICATE_PENDING)) {
BT_DBG("indicate pending, waiting until complete...");
return;
}
/* Reschedule since the range has changed */
k_delayed_work_submit(&sc->work, SC_TIMEOUT);
}
static void db_changed(void)
{
#if defined(CONFIG_BT_GATT_CACHING)
int i;
k_delayed_work_submit(&db_hash_work, DB_HASH_TIMEOUT);
for (i = 0; i < ARRAY_SIZE(cf_cfg); i++) {
struct gatt_cf_cfg *cfg = &cf_cfg[i];
if (!bt_addr_le_cmp(&cfg->peer, BT_ADDR_LE_ANY)) {
continue;
}
if (CF_ROBUST_CACHING(cfg) &&
atomic_test_and_clear_bit(cfg->flags, CF_CHANGE_AWARE)) {
BT_DBG("%s change-unaware", bt_addr_le_str(&cfg->peer));
}
}
#endif
}
int bt_gatt_service_register(struct bt_gatt_service *svc)
{
int err;
__ASSERT(svc, "invalid parameters\n");
__ASSERT(svc->attrs, "invalid parameters\n");
__ASSERT(svc->attr_count, "invalid parameters\n");
/* Init GATT core services */
bt_gatt_init();
/* Do no allow to register mandatory services twice */
if (!bt_uuid_cmp(svc->attrs[0].uuid, BT_UUID_GAP) ||
!bt_uuid_cmp(svc->attrs[0].uuid, BT_UUID_GATT)) {
return -EALREADY;
}
err = gatt_register(svc);
if (err < 0) {
return err;
}
sc_indicate(&gatt_sc, svc->attrs[0].handle,
svc->attrs[svc->attr_count - 1].handle);
db_changed();
return 0;
}
int bt_gatt_service_unregister(struct bt_gatt_service *svc)
{
__ASSERT(svc, "invalid parameters\n");
if (!sys_slist_find_and_remove(&db, &svc->node)) {
return -ENOENT;
}
sc_indicate(&gatt_sc, svc->attrs[0].handle,
svc->attrs[svc->attr_count - 1].handle);
db_changed();
return 0;
}
#endif /* CONFIG_BT_GATT_DYNAMIC_DB */
ssize_t bt_gatt_attr_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
void *buf, u16_t buf_len, u16_t offset,
const void *value, u16_t value_len)
{
u16_t len;
if (offset > value_len) {
return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
}
len = MIN(buf_len, value_len - offset);
BT_DBG("handle 0x%04x offset %u length %u", attr->handle, offset,
len);
memcpy(buf, (u8_t *)value + offset, len);
return len;
}
ssize_t bt_gatt_attr_read_service(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
struct bt_uuid *uuid = attr->user_data;
if (uuid->type == BT_UUID_TYPE_16) {
u16_t uuid16 = sys_cpu_to_le16(BT_UUID_16(uuid)->val);
return bt_gatt_attr_read(conn, attr, buf, len, offset,
&uuid16, 2);
}
return bt_gatt_attr_read(conn, attr, buf, len, offset,
BT_UUID_128(uuid)->val, 16);
}
struct gatt_incl {
u16_t start_handle;
u16_t end_handle;
u16_t uuid16;
} __packed;
static u8_t get_service_handles(const struct bt_gatt_attr *attr,
void *user_data)
{
struct gatt_incl *include = user_data;
/* Stop if attribute is a service */
if (!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_PRIMARY) ||
!bt_uuid_cmp(attr->uuid, BT_UUID_GATT_SECONDARY)) {
return BT_GATT_ITER_STOP;
}
include->end_handle = attr->handle;
return BT_GATT_ITER_CONTINUE;
}
ssize_t bt_gatt_attr_read_included(struct bt_conn *conn,
const struct bt_gatt_attr *attr,
void *buf, u16_t len, u16_t offset)
{
struct bt_gatt_attr *incl = attr->user_data;
struct bt_uuid *uuid = incl->user_data;
struct gatt_incl pdu;
u8_t value_len;
/* first attr points to the start handle */
pdu.start_handle = sys_cpu_to_le16(incl->handle);
value_len = sizeof(pdu.start_handle) + sizeof(pdu.end_handle);
/*
* Core 4.2, Vol 3, Part G, 3.2,
* The Service UUID shall only be present when the UUID is a
* 16-bit Bluetooth UUID.
*/
if (uuid->type == BT_UUID_TYPE_16) {
pdu.uuid16 = sys_cpu_to_le16(BT_UUID_16(uuid)->val);
value_len += sizeof(pdu.uuid16);
}
/* Lookup for service end handle */
bt_gatt_foreach_attr(incl->handle + 1, 0xffff, get_service_handles,
&pdu);
return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
}
struct gatt_chrc {
u8_t properties;
u16_t value_handle;
union {
u16_t uuid16;
u8_t uuid[16];
};
} __packed;
ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
const struct bt_gatt_attr *attr, void *buf,
u16_t len, u16_t offset)
{
struct bt_gatt_chrc *chrc = attr->user_data;
struct gatt_chrc pdu;
u8_t value_len;
pdu.properties = chrc->properties;
/* BLUETOOTH SPECIFICATION Version 4.2 [Vol 3, Part G] page 534:
* 3.3.2 Characteristic Value Declaration
* The Characteristic Value declaration contains the value of the
* characteristic. It is the first Attribute after the characteristic
* declaration. All characteristic definitions shall have a
* Characteristic Value declaration.
*/
pdu.value_handle = sys_cpu_to_le16(attr->handle + 1);
value_len = sizeof(pdu.properties) + sizeof(pdu.value_handle);
if (chrc->uuid->type == BT_UUID_TYPE_16) {
pdu.uuid16 = sys_cpu_to_le16(BT_UUID_16(chrc->uuid)->val);
value_len += 2U;
} else {
memcpy(pdu.uuid, BT_UUID_128(chrc->uuid)->val, 16);
value_len += 16U;
}
return bt_gatt_attr_read(conn, attr, buf, len, offset, &pdu, value_len);
}
static u8_t gatt_foreach_iter(const struct bt_gatt_attr *attr,
u16_t start_handle, u16_t end_handle,
bt_gatt_attr_func_t func, void *user_data)
{
/* Stop if over the requested range */
if (attr->handle > end_handle) {
return BT_GATT_ITER_STOP;
}
/* Check if attribute handle is within range */
if (attr->handle < start_handle) {
return BT_GATT_ITER_CONTINUE;
}
return func(attr, user_data);
}
void bt_gatt_foreach_attr(u16_t start_handle, u16_t end_handle,
bt_gatt_attr_func_t func, void *user_data)
{
struct bt_gatt_service *svc;
int i;
if (start_handle <= last_static_handle) {
const struct bt_gatt_service_static *static_svc;
u16_t handle;
for (static_svc = _bt_services_start, handle = 1;
static_svc < _bt_services_end; static_svc++) {
/* Skip ahead if start is not within service handles */
if (handle + static_svc->attr_count < start_handle) {
handle += static_svc->attr_count;
continue;
}
for (i = 0; i < static_svc->attr_count; i++, handle++) {
struct bt_gatt_attr attr;
memcpy(&attr, &static_svc->attrs[i],
sizeof(attr));
attr.handle = handle;