-
Notifications
You must be signed in to change notification settings - Fork 406
/
Copy pathble_gatts.c
2184 lines (1832 loc) · 60.1 KB
/
ble_gatts.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "nimble/ble.h"
#include "host/ble_uuid.h"
#include "host/ble_store.h"
#include "ble_hs_priv.h"
#define BLE_GATTS_INCLUDE_SZ 6
#define BLE_GATTS_CHR_MAX_SZ 19
static const ble_uuid_t *uuid_pri =
BLE_UUID16_DECLARE(BLE_ATT_UUID_PRIMARY_SERVICE);
static const ble_uuid_t *uuid_sec =
BLE_UUID16_DECLARE(BLE_ATT_UUID_SECONDARY_SERVICE);
static const ble_uuid_t *uuid_inc =
BLE_UUID16_DECLARE(BLE_ATT_UUID_INCLUDE);
static const ble_uuid_t *uuid_chr =
BLE_UUID16_DECLARE(BLE_ATT_UUID_CHARACTERISTIC);
static const ble_uuid_t *uuid_ccc =
BLE_UUID16_DECLARE(BLE_GATT_DSC_CLT_CFG_UUID16);
static const struct ble_gatt_svc_def **ble_gatts_svc_defs;
static int ble_gatts_num_svc_defs;
struct ble_gatts_svc_entry {
const struct ble_gatt_svc_def *svc;
uint16_t handle; /* 0 means unregistered. */
uint16_t end_group_handle; /* 0xffff means unset. */
};
static struct ble_gatts_svc_entry *ble_gatts_svc_entries;
static uint16_t ble_gatts_num_svc_entries;
static os_membuf_t *ble_gatts_clt_cfg_mem;
static struct os_mempool ble_gatts_clt_cfg_pool;
struct ble_gatts_clt_cfg {
uint16_t chr_val_handle;
uint8_t flags;
uint8_t allowed;
};
/** A cached array of handles for the configurable characteristics. */
static struct ble_gatts_clt_cfg *ble_gatts_clt_cfgs;
static int ble_gatts_num_cfgable_chrs;
STATS_SECT_DECL(ble_gatts_stats) ble_gatts_stats;
STATS_NAME_START(ble_gatts_stats)
STATS_NAME(ble_gatts_stats, svcs)
STATS_NAME(ble_gatts_stats, chrs)
STATS_NAME(ble_gatts_stats, dscs)
STATS_NAME(ble_gatts_stats, svc_def_reads)
STATS_NAME(ble_gatts_stats, svc_inc_reads)
STATS_NAME(ble_gatts_stats, chr_def_reads)
STATS_NAME(ble_gatts_stats, chr_val_reads)
STATS_NAME(ble_gatts_stats, chr_val_writes)
STATS_NAME(ble_gatts_stats, dsc_reads)
STATS_NAME(ble_gatts_stats, dsc_writes)
STATS_NAME_END(ble_gatts_stats)
static int
ble_gatts_svc_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t op, uint16_t offset, struct os_mbuf **om,
void *arg)
{
const struct ble_gatt_svc_def *svc;
uint8_t *buf;
STATS_INC(ble_gatts_stats, svc_def_reads);
BLE_HS_DBG_ASSERT(op == BLE_ATT_ACCESS_OP_READ);
svc = arg;
buf = os_mbuf_extend(*om, ble_uuid_length(svc->uuid));
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
ble_uuid_flat(svc->uuid, buf);
return 0;
}
static int
ble_gatts_inc_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t op, uint16_t offset, struct os_mbuf **om,
void *arg)
{
const struct ble_gatts_svc_entry *entry;
uint16_t uuid16;
uint8_t *buf;
STATS_INC(ble_gatts_stats, svc_inc_reads);
BLE_HS_DBG_ASSERT(op == BLE_ATT_ACCESS_OP_READ);
entry = arg;
buf = os_mbuf_extend(*om, 4);
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
put_le16(buf + 0, entry->handle);
put_le16(buf + 2, entry->end_group_handle);
/* Only include the service UUID if it has a 16-bit representation. */
uuid16 = ble_uuid_u16(entry->svc->uuid);
if (uuid16 != 0) {
buf = os_mbuf_extend(*om, 2);
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
put_le16(buf, uuid16);
}
return 0;
}
static uint16_t
ble_gatts_chr_clt_cfg_allowed(const struct ble_gatt_chr_def *chr)
{
uint16_t flags;
flags = 0;
if (chr->flags & BLE_GATT_CHR_F_NOTIFY) {
flags |= BLE_GATTS_CLT_CFG_F_NOTIFY;
}
if (chr->flags & BLE_GATT_CHR_F_INDICATE) {
flags |= BLE_GATTS_CLT_CFG_F_INDICATE;
}
return flags;
}
static uint8_t
ble_gatts_att_flags_from_chr_flags(ble_gatt_chr_flags chr_flags)
{
uint8_t att_flags;
att_flags = 0;
if (chr_flags & BLE_GATT_CHR_F_READ) {
att_flags |= BLE_ATT_F_READ;
}
if (chr_flags & (BLE_GATT_CHR_F_WRITE_NO_RSP | BLE_GATT_CHR_F_WRITE)) {
att_flags |= BLE_ATT_F_WRITE;
}
if (chr_flags & BLE_GATT_CHR_F_READ_ENC) {
att_flags |= BLE_ATT_F_READ_ENC;
}
if (chr_flags & BLE_GATT_CHR_F_READ_AUTHEN) {
att_flags |= BLE_ATT_F_READ_AUTHEN;
}
if (chr_flags & BLE_GATT_CHR_F_READ_AUTHOR) {
att_flags |= BLE_ATT_F_READ_AUTHOR;
}
if (chr_flags & BLE_GATT_CHR_F_WRITE_ENC) {
att_flags |= BLE_ATT_F_WRITE_ENC;
}
if (chr_flags & BLE_GATT_CHR_F_WRITE_AUTHEN) {
att_flags |= BLE_ATT_F_WRITE_AUTHEN;
}
if (chr_flags & BLE_GATT_CHR_F_WRITE_AUTHOR) {
att_flags |= BLE_ATT_F_WRITE_AUTHOR;
}
return att_flags;
}
static uint8_t
ble_gatts_chr_properties(const struct ble_gatt_chr_def *chr)
{
uint8_t properties;
properties = 0;
if (chr->flags & BLE_GATT_CHR_F_BROADCAST) {
properties |= BLE_GATT_CHR_PROP_BROADCAST;
}
if (chr->flags & BLE_GATT_CHR_F_READ) {
properties |= BLE_GATT_CHR_PROP_READ;
}
if (chr->flags & BLE_GATT_CHR_F_WRITE_NO_RSP) {
properties |= BLE_GATT_CHR_PROP_WRITE_NO_RSP;
}
if (chr->flags & BLE_GATT_CHR_F_WRITE) {
properties |= BLE_GATT_CHR_PROP_WRITE;
}
if (chr->flags & BLE_GATT_CHR_F_NOTIFY) {
properties |= BLE_GATT_CHR_PROP_NOTIFY;
}
if (chr->flags & BLE_GATT_CHR_F_INDICATE) {
properties |= BLE_GATT_CHR_PROP_INDICATE;
}
if (chr->flags & BLE_GATT_CHR_F_AUTH_SIGN_WRITE) {
properties |= BLE_GATT_CHR_PROP_AUTH_SIGN_WRITE;
}
if (chr->flags &
(BLE_GATT_CHR_F_RELIABLE_WRITE | BLE_GATT_CHR_F_AUX_WRITE)) {
properties |= BLE_GATT_CHR_PROP_EXTENDED;
}
return properties;
}
static int
ble_gatts_chr_def_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t op, uint16_t offset, struct os_mbuf **om,
void *arg)
{
const struct ble_gatt_chr_def *chr;
uint8_t *buf;
STATS_INC(ble_gatts_stats, chr_def_reads);
BLE_HS_DBG_ASSERT(op == BLE_ATT_ACCESS_OP_READ);
chr = arg;
buf = os_mbuf_extend(*om, 3);
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
buf[0] = ble_gatts_chr_properties(chr);
/* The value attribute is always immediately after the declaration. */
put_le16(buf + 1, attr_handle + 1);
buf = os_mbuf_extend(*om, ble_uuid_length(chr->uuid));
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
ble_uuid_flat(chr->uuid, buf);
return 0;
}
static int
ble_gatts_chr_is_sane(const struct ble_gatt_chr_def *chr)
{
if (chr->uuid == NULL) {
return 0;
}
if (chr->access_cb == NULL) {
return 0;
}
/* XXX: Check properties. */
return 1;
}
static uint8_t
ble_gatts_chr_op(uint8_t att_op)
{
switch (att_op) {
case BLE_ATT_ACCESS_OP_READ:
return BLE_GATT_ACCESS_OP_READ_CHR;
case BLE_ATT_ACCESS_OP_WRITE:
return BLE_GATT_ACCESS_OP_WRITE_CHR;
default:
BLE_HS_DBG_ASSERT(0);
return BLE_GATT_ACCESS_OP_READ_CHR;
}
}
static void
ble_gatts_chr_inc_val_stat(uint8_t gatt_op)
{
switch (gatt_op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
STATS_INC(ble_gatts_stats, chr_val_reads);
break;
case BLE_GATT_ACCESS_OP_WRITE_CHR:
STATS_INC(ble_gatts_stats, chr_val_writes);
break;
default:
break;
}
}
/**
* Indicates whether the set of registered services can be modified. The
* service set is mutable if:
* o No peers are connected, and
* o No GAP operations are active (advertise, discover, or connect).
*
* @return true if the GATT service set can be modified;
* false otherwise.
*/
static bool
ble_gatts_mutable(void)
{
/* Ensure no active GAP procedures. */
if (ble_gap_adv_active() ||
ble_gap_disc_active() ||
ble_gap_conn_active()) {
return false;
}
/* Ensure no established connections. */
if (ble_hs_conn_first() != NULL) {
return false;
}
return true;
}
static int
ble_gatts_val_access(uint16_t conn_handle, uint16_t attr_handle,
uint16_t offset, struct ble_gatt_access_ctxt *gatt_ctxt,
struct os_mbuf **om, ble_gatt_access_fn *access_cb,
void *cb_arg)
{
uint16_t initial_len;
int attr_len;
int new_om;
int rc;
switch (gatt_ctxt->op) {
case BLE_GATT_ACCESS_OP_READ_CHR:
case BLE_GATT_ACCESS_OP_READ_DSC:
/* A characteristic value is being read.
*
* If the read specifies an offset of 0:
* just append the characteristic value directly onto the response
* mbuf.
*
* Else:
* allocate a new mbuf to hold the characteristic data, then append
* the requested portion onto the response mbuf.
*/
if (offset == 0) {
new_om = 0;
gatt_ctxt->om = *om;
} else {
new_om = 1;
gatt_ctxt->om = os_msys_get_pkthdr(0, 0);
if (gatt_ctxt->om == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
}
initial_len = OS_MBUF_PKTLEN(gatt_ctxt->om);
rc = access_cb(conn_handle, attr_handle, gatt_ctxt, cb_arg);
if (rc == 0) {
attr_len = OS_MBUF_PKTLEN(gatt_ctxt->om) - initial_len - offset;
if (attr_len >= 0) {
if (new_om) {
os_mbuf_appendfrom(*om, gatt_ctxt->om, offset, attr_len);
}
} else {
rc = BLE_ATT_ERR_INVALID_OFFSET;
}
}
if (new_om) {
os_mbuf_free_chain(gatt_ctxt->om);
}
return rc;
case BLE_GATT_ACCESS_OP_WRITE_CHR:
case BLE_GATT_ACCESS_OP_WRITE_DSC:
gatt_ctxt->om = *om;
rc = access_cb(conn_handle, attr_handle, gatt_ctxt, cb_arg);
*om = gatt_ctxt->om;
return rc;
default:
BLE_HS_DBG_ASSERT(0);
return BLE_ATT_ERR_UNLIKELY;
}
}
static int
ble_gatts_chr_val_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t att_op, uint16_t offset,
struct os_mbuf **om, void *arg)
{
const struct ble_gatt_chr_def *chr_def;
struct ble_gatt_access_ctxt gatt_ctxt;
int rc;
chr_def = arg;
BLE_HS_DBG_ASSERT(chr_def != NULL && chr_def->access_cb != NULL);
gatt_ctxt.op = ble_gatts_chr_op(att_op);
gatt_ctxt.chr = chr_def;
ble_gatts_chr_inc_val_stat(gatt_ctxt.op);
rc = ble_gatts_val_access(conn_handle, attr_handle, offset, &gatt_ctxt, om,
chr_def->access_cb, chr_def->arg);
return rc;
}
static int
ble_gatts_find_svc_entry_idx(const struct ble_gatt_svc_def *svc)
{
int i;
for (i = 0; i < ble_gatts_num_svc_entries; i++) {
if (ble_gatts_svc_entries[i].svc == svc) {
return i;
}
}
return -1;
}
static int
ble_gatts_svc_incs_satisfied(const struct ble_gatt_svc_def *svc)
{
int idx;
int i;
if (svc->includes == NULL) {
/* No included services. */
return 1;
}
for (i = 0; svc->includes[i] != NULL; i++) {
idx = ble_gatts_find_svc_entry_idx(svc->includes[i]);
if (idx == -1 || ble_gatts_svc_entries[idx].handle == 0) {
return 0;
}
}
return 1;
}
static int
ble_gatts_register_inc(struct ble_gatts_svc_entry *entry)
{
uint16_t handle;
int rc;
BLE_HS_DBG_ASSERT(entry->handle != 0);
BLE_HS_DBG_ASSERT(entry->end_group_handle != 0xffff);
rc = ble_att_svr_register(uuid_inc, BLE_ATT_F_READ, 0, &handle,
ble_gatts_inc_access, entry);
if (rc != 0) {
return rc;
}
return 0;
}
static uint8_t
ble_gatts_dsc_op(uint8_t att_op)
{
switch (att_op) {
case BLE_ATT_ACCESS_OP_READ:
return BLE_GATT_ACCESS_OP_READ_DSC;
case BLE_ATT_ACCESS_OP_WRITE:
return BLE_GATT_ACCESS_OP_WRITE_DSC;
default:
BLE_HS_DBG_ASSERT(0);
return BLE_GATT_ACCESS_OP_READ_DSC;
}
}
static void
ble_gatts_dsc_inc_stat(uint8_t gatt_op)
{
switch (gatt_op) {
case BLE_GATT_ACCESS_OP_READ_DSC:
STATS_INC(ble_gatts_stats, dsc_reads);
break;
case BLE_GATT_ACCESS_OP_WRITE_DSC:
STATS_INC(ble_gatts_stats, dsc_writes);
break;
default:
break;
}
}
static int
ble_gatts_dsc_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t att_op, uint16_t offset, struct os_mbuf **om,
void *arg)
{
const struct ble_gatt_dsc_def *dsc_def;
struct ble_gatt_access_ctxt gatt_ctxt;
int rc;
dsc_def = arg;
BLE_HS_DBG_ASSERT(dsc_def != NULL && dsc_def->access_cb != NULL);
gatt_ctxt.op = ble_gatts_dsc_op(att_op);
gatt_ctxt.dsc = dsc_def;
ble_gatts_dsc_inc_stat(gatt_ctxt.op);
rc = ble_gatts_val_access(conn_handle, attr_handle, offset, &gatt_ctxt, om,
dsc_def->access_cb, dsc_def->arg);
return rc;
}
static int
ble_gatts_dsc_is_sane(const struct ble_gatt_dsc_def *dsc)
{
if (dsc->uuid == NULL) {
return 0;
}
if (dsc->access_cb == NULL) {
return 0;
}
return 1;
}
static int
ble_gatts_register_dsc(const struct ble_gatt_svc_def *svc,
const struct ble_gatt_chr_def *chr,
const struct ble_gatt_dsc_def *dsc,
uint16_t chr_def_handle,
ble_gatt_register_fn *register_cb, void *cb_arg)
{
struct ble_gatt_register_ctxt register_ctxt;
uint16_t dsc_handle;
int rc;
if (!ble_gatts_dsc_is_sane(dsc)) {
return BLE_HS_EINVAL;
}
rc = ble_att_svr_register(dsc->uuid, dsc->att_flags, dsc->min_key_size,
&dsc_handle, ble_gatts_dsc_access, (void *)dsc);
if (rc != 0) {
return rc;
}
if (register_cb != NULL) {
register_ctxt.op = BLE_GATT_REGISTER_OP_DSC;
register_ctxt.dsc.handle = dsc_handle;
register_ctxt.dsc.svc_def = svc;
register_ctxt.dsc.chr_def = chr;
register_ctxt.dsc.dsc_def = dsc;
register_cb(®ister_ctxt, cb_arg);
}
STATS_INC(ble_gatts_stats, dscs);
return 0;
}
static int
ble_gatts_clt_cfg_find_idx(struct ble_gatts_clt_cfg *cfgs,
uint16_t chr_val_handle)
{
struct ble_gatts_clt_cfg *cfg;
int i;
for (i = 0; i < ble_gatts_num_cfgable_chrs; i++) {
cfg = cfgs + i;
if (cfg->chr_val_handle == chr_val_handle) {
return i;
}
}
return -1;
}
static struct ble_gatts_clt_cfg *
ble_gatts_clt_cfg_find(struct ble_gatts_clt_cfg *cfgs,
uint16_t chr_val_handle)
{
int idx;
idx = ble_gatts_clt_cfg_find_idx(cfgs, chr_val_handle);
if (idx == -1) {
return NULL;
} else {
return cfgs + idx;
}
}
static void
ble_gatts_subscribe_event(uint16_t conn_handle, uint16_t attr_handle,
uint8_t reason,
uint8_t prev_flags, uint8_t cur_flags)
{
if ((prev_flags ^ cur_flags) & ~BLE_GATTS_CLT_CFG_F_RESERVED) {
ble_gap_subscribe_event(conn_handle,
attr_handle,
reason,
prev_flags & BLE_GATTS_CLT_CFG_F_NOTIFY,
cur_flags & BLE_GATTS_CLT_CFG_F_NOTIFY,
prev_flags & BLE_GATTS_CLT_CFG_F_INDICATE,
cur_flags & BLE_GATTS_CLT_CFG_F_INDICATE);
}
}
/**
* Performs a read or write access on a client characteritic configuration
* descriptor (CCCD).
*
* @param conn The connection of the peer doing the accessing.
* @apram attr_handle The handle of the CCCD.
* @param att_op The ATT operation being performed (read or
* write).
* @param ctxt Communication channel between this function and
* the caller within the nimble stack.
* Semantics depends on the operation being
* performed.
* @param out_cccd If the CCCD should be persisted as a result of
* the access, the data-to-be-persisted gets
* written here. If no persistence is
* necessary, out_cccd->chr_val_handle is set
* to 0.
*
* @return 0 on success; nonzero on failure.
*/
static int
ble_gatts_clt_cfg_access_locked(struct ble_hs_conn *conn, uint16_t attr_handle,
uint8_t att_op, uint16_t offset,
struct os_mbuf *om,
struct ble_store_value_cccd *out_cccd,
uint8_t *out_prev_clt_cfg_flags,
uint8_t *out_cur_clt_cfg_flags)
{
struct ble_gatts_clt_cfg *clt_cfg;
uint16_t chr_val_handle;
uint16_t flags;
uint8_t gatt_op;
uint8_t *buf;
/* Assume nothing needs to be persisted. */
out_cccd->chr_val_handle = 0;
/* We always register the client characteristics descriptor with handle
* (chr_val + 1).
*/
chr_val_handle = attr_handle - 1;
if (chr_val_handle > attr_handle) {
/* Attribute handle wrapped somehow. */
return BLE_ATT_ERR_UNLIKELY;
}
clt_cfg = ble_gatts_clt_cfg_find(conn->bhc_gatt_svr.clt_cfgs,
chr_val_handle);
if (clt_cfg == NULL) {
return BLE_ATT_ERR_UNLIKELY;
}
/* Assume no change in flags. */
*out_prev_clt_cfg_flags = clt_cfg->flags;
*out_cur_clt_cfg_flags = clt_cfg->flags;
gatt_op = ble_gatts_dsc_op(att_op);
ble_gatts_dsc_inc_stat(gatt_op);
switch (gatt_op) {
case BLE_GATT_ACCESS_OP_READ_DSC:
STATS_INC(ble_gatts_stats, dsc_reads);
buf = os_mbuf_extend(om, 2);
if (buf == NULL) {
return BLE_ATT_ERR_INSUFFICIENT_RES;
}
put_le16(buf, clt_cfg->flags & ~BLE_GATTS_CLT_CFG_F_RESERVED);
break;
case BLE_GATT_ACCESS_OP_WRITE_DSC:
STATS_INC(ble_gatts_stats, dsc_writes);
if (OS_MBUF_PKTLEN(om) != 2) {
return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
}
om = os_mbuf_pullup(om, 2);
BLE_HS_DBG_ASSERT(om != NULL);
flags = get_le16(om->om_data);
if ((flags & ~clt_cfg->allowed) != 0) {
return BLE_ATT_ERR_REQ_NOT_SUPPORTED;
}
if (clt_cfg->flags != flags) {
clt_cfg->flags = flags;
*out_cur_clt_cfg_flags = flags;
/* Successful writes get persisted for bonded connections. */
if (conn->bhc_sec_state.bonded) {
out_cccd->peer_addr = conn->bhc_peer_addr;
out_cccd->peer_addr.type =
ble_hs_misc_peer_addr_type_to_id(conn->bhc_peer_addr.type);
out_cccd->chr_val_handle = chr_val_handle;
out_cccd->flags = clt_cfg->flags;
out_cccd->value_changed = 0;
}
}
break;
default:
BLE_HS_DBG_ASSERT(0);
return BLE_ATT_ERR_UNLIKELY;
}
return 0;
}
int
ble_gatts_clt_cfg_access(uint16_t conn_handle, uint16_t attr_handle,
uint8_t op, uint16_t offset, struct os_mbuf **om,
void *arg)
{
struct ble_store_value_cccd cccd_value;
struct ble_store_key_cccd cccd_key;
struct ble_hs_conn *conn;
uint16_t chr_val_handle;
uint8_t prev_flags;
uint8_t cur_flags;
int rc;
ble_hs_lock();
conn = ble_hs_conn_find(conn_handle);
if (conn == NULL) {
rc = BLE_ATT_ERR_UNLIKELY;
} else {
rc = ble_gatts_clt_cfg_access_locked(conn, attr_handle, op, offset,
*om, &cccd_value, &prev_flags,
&cur_flags);
}
ble_hs_unlock();
if (rc != 0) {
return rc;
}
/* The value attribute is always immediately after the declaration. */
chr_val_handle = attr_handle - 1;
/* Tell the application if the peer changed its subscription state. */
ble_gatts_subscribe_event(conn_handle, chr_val_handle,
BLE_GAP_SUBSCRIBE_REASON_WRITE,
prev_flags, cur_flags);
/* Persist the CCCD if required. */
if (cccd_value.chr_val_handle != 0) {
if (cccd_value.flags == 0) {
ble_store_key_from_value_cccd(&cccd_key, &cccd_value);
rc = ble_store_delete_cccd(&cccd_key);
} else {
rc = ble_store_write_cccd(&cccd_value);
}
}
return rc;
}
static int
ble_gatts_register_clt_cfg_dsc(uint16_t *att_handle)
{
int rc;
rc = ble_att_svr_register(uuid_ccc, BLE_ATT_F_READ | BLE_ATT_F_WRITE, 0,
att_handle, ble_gatts_clt_cfg_access, NULL);
if (rc != 0) {
return rc;
}
STATS_INC(ble_gatts_stats, dscs);
return 0;
}
static int
ble_gatts_register_chr(const struct ble_gatt_svc_def *svc,
const struct ble_gatt_chr_def *chr,
ble_gatt_register_fn *register_cb, void *cb_arg)
{
struct ble_gatt_register_ctxt register_ctxt;
struct ble_gatt_dsc_def *dsc;
uint16_t def_handle;
uint16_t val_handle;
uint16_t dsc_handle;
uint8_t att_flags;
int rc;
if (!ble_gatts_chr_is_sane(chr)) {
return BLE_HS_EINVAL;
}
if (ble_gatts_chr_clt_cfg_allowed(chr) != 0) {
if (ble_gatts_num_cfgable_chrs > ble_hs_max_client_configs) {
return BLE_HS_ENOMEM;
}
ble_gatts_num_cfgable_chrs++;
}
/* Register characteristic definition attribute (cast away const on
* callback arg).
*/
rc = ble_att_svr_register(uuid_chr, BLE_ATT_F_READ, 0, &def_handle,
ble_gatts_chr_def_access, (void *)chr);
if (rc != 0) {
return rc;
}
/* Register characteristic value attribute (cast away const on callback
* arg).
*/
att_flags = ble_gatts_att_flags_from_chr_flags(chr->flags);
rc = ble_att_svr_register(chr->uuid, att_flags, chr->min_key_size,
&val_handle, ble_gatts_chr_val_access,
(void *)chr);
if (rc != 0) {
return rc;
}
BLE_HS_DBG_ASSERT(val_handle == def_handle + 1);
if (chr->val_handle != NULL) {
*chr->val_handle = val_handle;
}
if (register_cb != NULL) {
register_ctxt.op = BLE_GATT_REGISTER_OP_CHR;
register_ctxt.chr.def_handle = def_handle;
register_ctxt.chr.val_handle = val_handle;
register_ctxt.chr.svc_def = svc;
register_ctxt.chr.chr_def = chr;
register_cb(®ister_ctxt, cb_arg);
}
if (ble_gatts_chr_clt_cfg_allowed(chr) != 0) {
rc = ble_gatts_register_clt_cfg_dsc(&dsc_handle);
if (rc != 0) {
return rc;
}
BLE_HS_DBG_ASSERT(dsc_handle == def_handle + 2);
}
/* Register each descriptor. */
if (chr->descriptors != NULL) {
for (dsc = chr->descriptors; dsc->uuid != NULL; dsc++) {
rc = ble_gatts_register_dsc(svc, chr, dsc, def_handle, register_cb,
cb_arg);
if (rc != 0) {
return rc;
}
}
}
STATS_INC(ble_gatts_stats, chrs);
return 0;
}
static int
ble_gatts_svc_type_to_uuid(uint8_t svc_type, const ble_uuid_t **uuid)
{
switch (svc_type) {
case BLE_GATT_SVC_TYPE_PRIMARY:
*uuid = uuid_pri;
return 0;
case BLE_GATT_SVC_TYPE_SECONDARY:
*uuid = uuid_sec;
return 0;
default:
return BLE_HS_EINVAL;
}
}
static int
ble_gatts_svc_is_sane(const struct ble_gatt_svc_def *svc)
{
if (svc->type != BLE_GATT_SVC_TYPE_PRIMARY &&
svc->type != BLE_GATT_SVC_TYPE_SECONDARY) {
return 0;
}
if (svc->uuid == NULL) {
return 0;
}
return 1;
}
static int
ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
uint16_t *out_handle,
ble_gatt_register_fn *register_cb, void *cb_arg)
{
const struct ble_gatt_chr_def *chr;
struct ble_gatt_register_ctxt register_ctxt;
const ble_uuid_t *uuid;
int idx;
int rc;
int i;
if (!ble_gatts_svc_incs_satisfied(svc)) {
return BLE_HS_EAGAIN;
}
if (!ble_gatts_svc_is_sane(svc)) {
return BLE_HS_EINVAL;
}
/* Prevent spurious maybe-uninitialized gcc warning. */
uuid = NULL;
rc = ble_gatts_svc_type_to_uuid(svc->type, &uuid);
BLE_HS_DBG_ASSERT_EVAL(rc == 0);
/* Register service definition attribute (cast away const on callback
* arg).
*/
rc = ble_att_svr_register(uuid, BLE_ATT_F_READ, 0, out_handle,
ble_gatts_svc_access, (void *)svc);
if (rc != 0) {
return rc;
}
if (register_cb != NULL) {
register_ctxt.op = BLE_GATT_REGISTER_OP_SVC;
register_ctxt.svc.handle = *out_handle;
register_ctxt.svc.svc_def = svc;
register_cb(®ister_ctxt, cb_arg);
}
/* Register each include. */
if (svc->includes != NULL) {
for (i = 0; svc->includes[i] != NULL; i++) {
idx = ble_gatts_find_svc_entry_idx(svc->includes[i]);
BLE_HS_DBG_ASSERT_EVAL(idx != -1);
rc = ble_gatts_register_inc(ble_gatts_svc_entries + idx);
if (rc != 0) {
return rc;
}
}
}
/* Register each characteristic. */
if (svc->characteristics != NULL) {
for (chr = svc->characteristics; chr->uuid != NULL; chr++) {
rc = ble_gatts_register_chr(svc, chr, register_cb, cb_arg);
if (rc != 0) {
return rc;
}
}
}
STATS_INC(ble_gatts_stats, svcs);
return 0;
}
static int
ble_gatts_register_round(int *out_num_registered, ble_gatt_register_fn *cb,
void *cb_arg)
{
struct ble_gatts_svc_entry *entry;
uint16_t handle;
int rc;
int i;
*out_num_registered = 0;
for (i = 0; i < ble_gatts_num_svc_entries; i++) {