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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
|
/******************************************************************************
*
* Copyright 2008-2012 Broadcom Corporation
*
* Licensed 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.
*
******************************************************************************/
/******************************************************************************
*
* this file contains the main GATT server attributes access request
* handling functions.
*
******************************************************************************/
#include <bluetooth/log.h>
#include <deque>
#include <map>
#include "base/functional/callback.h"
#include "btif/include/btif_storage.h"
#include "eatt/eatt.h"
#include "gatt_api.h"
#include "gatt_int.h"
#include "device/include/interop.h"
#include "internal_include/bt_target.h"
#include "stack/include/bt_types.h"
#include "stack/include/bt_uuid16.h"
#include "stack/include/btm_sec_api.h"
#include "stack/include/btm_ble_addr.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"
using bluetooth::Uuid;
using namespace bluetooth;
#define BLE_GATT_SVR_SUP_FEAT_EATT_BITMASK 0x01
#define BLE_GATT_CL_SUP_FEAT_CACHING_BITMASK 0x01
#define BLE_GATT_CL_SUP_FEAT_EATT_BITMASK 0x02
#define BLE_GATT_CL_SUP_FEAT_MULTI_NOTIF_BITMASK 0x04
#define BLE_GATT_CL_ANDROID_SUP_FEAT \
(BLE_GATT_CL_SUP_FEAT_EATT_BITMASK | BLE_GATT_CL_SUP_FEAT_MULTI_NOTIF_BITMASK)
using gatt_sr_supported_feat_cb = base::OnceCallback<void(const RawAddress&, uint8_t)>;
using gatt_sirk_cb = base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
uint8_t sirk_type, Octet16& sirk)>;
typedef struct {
uint16_t op_uuid;
gatt_sr_supported_feat_cb cb;
gatt_sirk_cb sirk_cb;
} gatt_op_cb_data;
static std::map<tCONN_ID, std::deque<gatt_op_cb_data>> OngoingOps;
static void gatt_request_cback(tCONN_ID conn_id, uint32_t trans_id, uint8_t op_code,
tGATTS_DATA* p_data);
static void gatt_connect_cback(tGATT_IF /* gatt_if */, const RawAddress& bda, tCONN_ID conn_id,
bool connected, tGATT_DISCONN_REASON reason,
tBT_TRANSPORT transport);
static void gatt_disc_res_cback(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
tGATT_DISC_RES* p_data);
static void gatt_disc_cmpl_cback(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type, tGATT_STATUS status);
static void gatt_cl_op_cmpl_cback(tCONN_ID conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
tGATT_CL_COMPLETE* p_data);
static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb);
static bool gatt_sr_is_robust_caching_enabled();
static bool read_sr_supported_feat_req(tCONN_ID conn_id,
base::OnceCallback<void(const RawAddress&, uint8_t)> cb);
static bool read_sr_sirk_req(tCONN_ID conn_id,
base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
uint8_t sirk_type, Octet16& sirk)>
cb);
static tGATT_STATUS gatt_sr_read_db_hash(tCONN_ID conn_id, tGATT_VALUE* p_value);
static tGATT_STATUS gatt_sr_read_cl_supp_feat(tCONN_ID conn_id, tGATT_VALUE* p_value);
static tGATT_STATUS gatt_sr_write_cl_supp_feat(tCONN_ID conn_id, tGATT_WRITE_REQ* p_data);
static tGATT_CBACK gatt_profile_cback = {
.p_conn_cb = gatt_connect_cback,
.p_cmpl_cb = gatt_cl_op_cmpl_cback,
.p_disc_res_cb = gatt_disc_res_cback,
.p_disc_cmpl_cb = gatt_disc_cmpl_cback,
.p_req_cb = gatt_request_cback,
.p_enc_cmpl_cb = nullptr,
.p_congestion_cb = nullptr,
.p_phy_update_cb = nullptr,
.p_conn_update_cb = nullptr,
.p_subrate_chg_cb = nullptr,
};
/*******************************************************************************
*
* Function gatt_profile_find_conn_id_by_bd_addr
*
* Description Find the connection ID by remote address
*
* Returns Connection ID
*
******************************************************************************/
tCONN_ID gatt_profile_find_conn_id_by_bd_addr(const RawAddress& remote_bda) {
tCONN_ID conn_id = GATT_INVALID_CONN_ID;
if (!GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_LE)) {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
remote_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_LE));
}
if (conn_id == GATT_INVALID_CONN_ID) {
if (!GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_BR_EDR)) {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
remote_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_BR_EDR));
}
}
return conn_id;
}
/*******************************************************************************
*
* Function gatt_profile_find_clcb_by_conn_id
*
* Description find clcb by Connection ID
*
* Returns Pointer to the found link conenction control block.
*
******************************************************************************/
static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_conn_id(tCONN_ID conn_id) {
uint8_t i_clcb;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
if (p_clcb->in_use && p_clcb->conn_id == conn_id) {
return p_clcb;
}
}
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_find_clcb_by_bd_addr
*
* Description The function searches all LCBs with macthing bd address.
*
* Returns Pointer to the found link conenction control block.
*
******************************************************************************/
static tGATT_PROFILE_CLCB* gatt_profile_find_clcb_by_bd_addr(const RawAddress& bda,
tBT_TRANSPORT transport) {
uint8_t i_clcb;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
if (p_clcb->in_use && p_clcb->transport == transport && p_clcb->connected &&
p_clcb->bda == bda) {
return p_clcb;
}
}
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_clcb_alloc
*
* Description The function allocates a GATT profile connection link
* control block
*
* Returns NULL if not found. Otherwise pointer to the connection link
* block.
*
******************************************************************************/
static tGATT_PROFILE_CLCB* gatt_profile_clcb_alloc(tCONN_ID conn_id, const RawAddress& bda,
tBT_TRANSPORT tranport) {
uint8_t i_clcb = 0;
tGATT_PROFILE_CLCB* p_clcb = NULL;
for (i_clcb = 0, p_clcb = gatt_cb.profile_clcb; i_clcb < GATT_MAX_APPS; i_clcb++, p_clcb++) {
if (!p_clcb->in_use) {
p_clcb->in_use = true;
p_clcb->conn_id = conn_id;
p_clcb->connected = true;
p_clcb->transport = tranport;
p_clcb->bda = bda;
break;
}
}
if (i_clcb < GATT_MAX_APPS) {
return p_clcb;
}
return NULL;
}
/*******************************************************************************
*
* Function gatt_profile_clcb_dealloc
*
* Description The function deallocates a GATT profile connection link
* control block
*
* Returns void
*
******************************************************************************/
static void gatt_profile_clcb_dealloc(tGATT_PROFILE_CLCB* p_clcb) {
memset(p_clcb, 0, sizeof(tGATT_PROFILE_CLCB));
}
/** GAP Attributes Database Request callback */
static tGATT_STATUS read_attr_value(tCONN_ID conn_id, uint16_t handle, tGATT_VALUE* p_value,
bool is_long) {
uint8_t* p = p_value->value;
if (handle == gatt_cb.handle_sr_supported_feat) {
/* GATT_UUID_SERVER_SUP_FEAT*/
if (is_long) {
return GATT_NOT_LONG;
}
UINT8_TO_STREAM(p, gatt_cb.gatt_svr_supported_feat_mask);
p_value->len = sizeof(gatt_cb.gatt_svr_supported_feat_mask);
return GATT_SUCCESS;
}
if (handle == gatt_cb.handle_cl_supported_feat) {
/*GATT_UUID_CLIENT_SUP_FEAT */
if (is_long) {
return GATT_NOT_LONG;
}
return gatt_sr_read_cl_supp_feat(conn_id, p_value);
}
if (handle == gatt_cb.handle_of_database_hash) {
/* GATT_UUID_DATABASE_HASH */
if (is_long) {
return GATT_NOT_LONG;
}
return gatt_sr_read_db_hash(conn_id, p_value);
}
if (handle == gatt_cb.handle_of_h_r) {
/* GATT_UUID_GATT_SRV_CHGD */
return GATT_READ_NOT_PERMIT;
}
return GATT_NOT_FOUND;
}
/** GAP Attributes Database Read/Read Blob Request process */
static tGATT_STATUS proc_read_req(tCONN_ID conn_id, tGATTS_REQ_TYPE, tGATT_READ_REQ* p_data,
tGATTS_RSP* p_rsp) {
if (p_data->is_long) {
p_rsp->attr_value.offset = p_data->offset;
}
p_rsp->attr_value.handle = p_data->handle;
return read_attr_value(conn_id, p_data->handle, &p_rsp->attr_value, p_data->is_long);
}
/** GAP ATT server process a write request */
static tGATT_STATUS proc_write_req(tCONN_ID conn_id, tGATTS_REQ_TYPE, tGATT_WRITE_REQ* p_data) {
uint16_t handle = p_data->handle;
/* GATT_UUID_SERVER_SUP_FEAT*/
if (handle == gatt_cb.handle_sr_supported_feat) {
return GATT_WRITE_NOT_PERMIT;
}
/* GATT_UUID_CLIENT_SUP_FEAT*/
if (handle == gatt_cb.handle_cl_supported_feat) {
return gatt_sr_write_cl_supp_feat(conn_id, p_data);
}
/* GATT_UUID_DATABASE_HASH */
if (handle == gatt_cb.handle_of_database_hash) {
return GATT_WRITE_NOT_PERMIT;
}
/* GATT_UUID_GATT_SRV_CHGD */
if (handle == gatt_cb.handle_of_h_r) {
return GATT_WRITE_NOT_PERMIT;
}
return GATT_NOT_FOUND;
}
/*******************************************************************************
*
* Function gatt_request_cback
*
* Description GATT profile attribute access request callback.
*
* Returns void.
*
******************************************************************************/
static void gatt_request_cback(tCONN_ID conn_id, uint32_t trans_id, tGATTS_REQ_TYPE type,
tGATTS_DATA* p_data) {
tGATT_STATUS status = GATT_INVALID_PDU;
tGATTS_RSP rsp_msg;
bool rsp_needed = true;
memset(&rsp_msg, 0, sizeof(tGATTS_RSP));
switch (type) {
case GATTS_REQ_TYPE_READ_CHARACTERISTIC:
case GATTS_REQ_TYPE_READ_DESCRIPTOR:
status = proc_read_req(conn_id, type, &p_data->read_req, &rsp_msg);
break;
case GATTS_REQ_TYPE_WRITE_CHARACTERISTIC:
case GATTS_REQ_TYPE_WRITE_DESCRIPTOR:
case GATTS_REQ_TYPE_WRITE_EXEC:
case GATT_CMD_WRITE:
if (!p_data->write_req.need_rsp) {
rsp_needed = false;
}
status = proc_write_req(conn_id, type, &p_data->write_req);
break;
case GATTS_REQ_TYPE_MTU:
log::verbose("Get MTU exchange new mtu size: {}", p_data->mtu);
rsp_needed = false;
break;
default:
log::verbose("Unknown/unexpected LE GAP ATT request: 0x{:x}", type);
break;
}
if (rsp_needed) {
if (GATTS_SendRsp(conn_id, trans_id, status, &rsp_msg) != GATT_SUCCESS) {
log::warn("Unable to send GATT server response conn_id:{}", conn_id);
}
}
}
/*******************************************************************************
*
* Function gatt_connect_cback
*
* Description Gatt profile connection callback.
*
* Returns void
*
******************************************************************************/
static void gatt_connect_cback(tGATT_IF /* gatt_if */, const RawAddress& bda, tCONN_ID conn_id,
bool connected, tGATT_DISCONN_REASON /* reason */,
tBT_TRANSPORT transport) {
log::verbose("from {} connected: {}, conn_id: 0x{:x}", bda, connected, conn_id);
// if the device is not trusted, remove data when the link is disconnected
if (!connected && !btm_sec_is_a_bonded_dev(bda)) {
log::info("remove untrusted client status, bda={}", bda);
btif_storage_remove_gatt_cl_supp_feat(bda);
btif_storage_remove_gatt_cl_db_hash(bda);
}
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_bd_addr(bda, transport);
if (p_clcb == NULL) {
return;
}
if (connected) {
p_clcb->conn_id = conn_id;
p_clcb->connected = true;
if (p_clcb->ccc_stage == GATT_SVC_CHANGED_CONNECTING) {
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
} else {
gatt_profile_clcb_dealloc(p_clcb);
}
}
/*******************************************************************************
*
* Function gatt_profile_db_init
*
* Description Initializa the GATT profile attribute database.
*
******************************************************************************/
void gatt_profile_db_init(void) {
/* Fill our internal UUID with a fixed pattern 0x81 */
std::array<uint8_t, Uuid::kNumBytes128> tmp;
tmp.fill(0x81);
OngoingOps.clear();
/* Create a GATT profile service */
gatt_cb.gatt_if =
GATT_Register(Uuid::From128BitBE(tmp), "GattProfileDb", &gatt_profile_cback, false);
GATT_StartIf(gatt_cb.gatt_if);
Uuid service_uuid = Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER);
Uuid srv_changed_char_uuid = Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD);
Uuid svr_sup_feat_uuid = Uuid::From16Bit(GATT_UUID_SERVER_SUP_FEAT);
Uuid cl_sup_feat_uuid = Uuid::From16Bit(GATT_UUID_CLIENT_SUP_FEAT);
Uuid database_hash_uuid = Uuid::From16Bit(GATT_UUID_DATABASE_HASH);
btgatt_db_element_t service[] = {
{
.uuid = service_uuid,
.type = BTGATT_DB_PRIMARY_SERVICE,
},
{
.uuid = srv_changed_char_uuid,
.type = BTGATT_DB_CHARACTERISTIC,
.properties = GATT_CHAR_PROP_BIT_INDICATE,
.permissions = 0,
},
{
.uuid = svr_sup_feat_uuid,
.type = BTGATT_DB_CHARACTERISTIC,
.properties = GATT_CHAR_PROP_BIT_READ,
.permissions = GATT_PERM_READ,
},
{
.uuid = cl_sup_feat_uuid,
.type = BTGATT_DB_CHARACTERISTIC,
.properties = GATT_CHAR_PROP_BIT_READ | GATT_CHAR_PROP_BIT_WRITE,
.permissions = GATT_PERM_READ | GATT_PERM_WRITE,
},
{
.uuid = database_hash_uuid,
.type = BTGATT_DB_CHARACTERISTIC,
.properties = GATT_CHAR_PROP_BIT_READ,
.permissions = GATT_PERM_READ,
}};
if (GATTS_AddService(gatt_cb.gatt_if, service, sizeof(service) / sizeof(btgatt_db_element_t)) !=
GATT_SERVICE_STARTED) {
log::warn("Unable to add GATT server service gatt_if:{}", gatt_cb.gatt_if);
}
gatt_cb.handle_of_h_r = service[1].attribute_handle;
gatt_cb.handle_sr_supported_feat = service[2].attribute_handle;
gatt_cb.handle_cl_supported_feat = service[3].attribute_handle;
gatt_cb.handle_of_database_hash = service[4].attribute_handle;
gatt_cb.gatt_svr_supported_feat_mask |= BLE_GATT_SVR_SUP_FEAT_EATT_BITMASK;
gatt_cb.gatt_cl_supported_feat_mask |= BLE_GATT_CL_ANDROID_SUP_FEAT;
gatt_cb.gatt_cl_supported_feat_mask |= BLE_GATT_CL_SUP_FEAT_CACHING_BITMASK;
log::verbose("gatt_if={} EATT supported", gatt_cb.gatt_if);
}
/*******************************************************************************
*
* Function gatt_disc_res_cback
*
* Description Gatt profile discovery result callback
*
* Returns void
*
******************************************************************************/
static void gatt_disc_res_cback(tCONN_ID conn_id, tGATT_DISC_TYPE disc_type,
tGATT_DISC_RES* p_data) {
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (p_clcb == NULL) {
return;
}
switch (disc_type) {
case GATT_DISC_SRVC_BY_UUID: /* stage 1 */
p_clcb->e_handle = p_data->value.group_value.e_handle;
p_clcb->ccc_result++;
break;
case GATT_DISC_CHAR: /* stage 2 */
p_clcb->s_handle = p_data->value.dclr_value.val_handle;
p_clcb->ccc_result++;
break;
case GATT_DISC_CHAR_DSCPT: /* stage 3 */
if (p_data->type == Uuid::From16Bit(GATT_UUID_CHAR_CLIENT_CONFIG)) {
p_clcb->s_handle = p_data->handle;
p_clcb->ccc_result++;
}
break;
case GATT_DISC_SRVC_ALL:
case GATT_DISC_INC_SRVC:
case GATT_DISC_MAX:
log::error("Illegal discovery item handled");
break;
}
}
/*******************************************************************************
*
* Function gatt_disc_cmpl_cback
*
* Description Gatt profile discovery complete callback
*
* Returns void
*
******************************************************************************/
static void gatt_disc_cmpl_cback(tCONN_ID conn_id, tGATT_DISC_TYPE /* disc_type */,
tGATT_STATUS status) {
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (p_clcb == NULL) {
log::warn("Unable to find gatt profile after discovery complete");
return;
}
if (status != GATT_SUCCESS) {
log::warn("Gatt discovery completed with errors status:{}", status);
return;
}
if (p_clcb->ccc_result == 0) {
log::warn("Gatt discovery completed but connection was idle id:{}", conn_id);
return;
}
p_clcb->ccc_result = 0;
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
static bool gatt_svc_read_cl_supp_feat_req(tCONN_ID conn_id) {
tGATT_READ_PARAM param;
memset(¶m, 0, sizeof(tGATT_READ_PARAM));
param.service.s_handle = 1;
param.service.e_handle = 0xFFFF;
param.service.auth_req = 0;
param.service.uuid = bluetooth::Uuid::From16Bit(GATT_UUID_CLIENT_SUP_FEAT);
tGATT_STATUS status = GATTC_Read(conn_id, GATT_READ_BY_TYPE, ¶m);
if (status != GATT_SUCCESS) {
log::error("Read failed. Status: 0x{:x}", static_cast<uint8_t>(status));
return false;
}
gatt_op_cb_data cb_data;
cb_data.cb =
base::BindOnce([](const RawAddress& /* bdaddr */, uint8_t /* support */) { return; });
cb_data.op_uuid = GATT_UUID_CLIENT_SUP_FEAT;
OngoingOps[conn_id].emplace_back(std::move(cb_data));
return true;
}
static bool gatt_att_write_cl_supp_feat(tCONN_ID conn_id, uint16_t handle) {
tGATT_VALUE attr;
memset(&attr, 0, sizeof(tGATT_VALUE));
attr.conn_id = conn_id;
attr.handle = handle;
attr.len = 1;
attr.value[0] = gatt_cb.gatt_cl_supported_feat_mask;
tGATT_STATUS status = GATTC_Write(conn_id, GATT_WRITE, &attr);
if (status != GATT_SUCCESS) {
log::error("Write failed. Status: 0x{:x}", static_cast<uint8_t>(status));
return false;
}
return true;
}
/*******************************************************************************
*
* Function gatt_cl_op_cmpl_cback
*
* Description Gatt profile client operation complete callback
*
* Returns void
*
******************************************************************************/
static void gatt_cl_op_cmpl_cback(tCONN_ID conn_id, tGATTC_OPTYPE op, tGATT_STATUS status,
tGATT_CL_COMPLETE* p_data) {
auto iter = OngoingOps.find(conn_id);
log::verbose("opcode: 0x{:x} status: {} conn id: 0x{:x}", static_cast<uint8_t>(op), status,
static_cast<int>(conn_id));
if (op != GATTC_OPTYPE_READ && op != GATTC_OPTYPE_WRITE) {
log::verbose("Not interested in opcode {}", op);
return;
}
if (iter == OngoingOps.end() || (iter->second.size() == 0)) {
/* If OngoingOps is empty it means we are not interested in the result here.
*/
log::debug("Unexpected read complete");
return;
}
uint16_t cl_op_uuid = iter->second.front().op_uuid;
if (op == GATTC_OPTYPE_WRITE) {
if (cl_op_uuid == GATT_UUID_GATT_SRV_CHGD) {
log::debug("Write response from Service Changed CCC");
iter->second.pop_front();
/* Read server supported features here supported */
read_sr_supported_feat_req(conn_id, base::BindOnce([](const RawAddress& /* bdaddr */,
uint8_t /* support */) { return; }));
} else {
log::debug("Not interested in that write response");
}
return;
}
/* Handle Read operations */
uint8_t* pp = p_data->att_value.value;
log::verbose("cl_op_uuid 0x{:x}", cl_op_uuid);
switch (cl_op_uuid) {
case GATT_UUID_SERVER_SUP_FEAT: {
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
auto operation_callback_data = std::move(iter->second.front());
iter->second.pop_front();
/* Check if EATT is supported */
if (status == GATT_SUCCESS) {
STREAM_TO_UINT8(tcb.sr_supp_feat, pp);
btif_storage_set_gatt_sr_supp_feat(tcb.peer_bda, tcb.sr_supp_feat);
}
/* Notify user about the supported features */
std::move(operation_callback_data.cb).Run(tcb.peer_bda, tcb.sr_supp_feat);
/* If server supports EATT lets try to find handle for the
* client supported features characteristic, where we could write
* our supported features as a client.
*/
if (tcb.sr_supp_feat & BLE_GATT_SVR_SUP_FEAT_EATT_BITMASK) {
gatt_svc_read_cl_supp_feat_req(conn_id);
}
break;
}
case GATT_UUID_CSIS_SIRK: {
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
auto operation_callback_data = std::move(iter->second.front());
iter->second.pop_front();
tcb.gatt_status = status;
if (status == GATT_SUCCESS) {
STREAM_TO_UINT8(tcb.sirk_type, pp);
STREAM_TO_ARRAY(tcb.sirk.data(), pp, 16);
}
std::move(operation_callback_data.sirk_cb)
.Run(tcb.gatt_status, tcb.peer_bda, tcb.sirk_type, tcb.sirk);
break;
}
case GATT_UUID_CLIENT_SUP_FEAT:
/*We don't need callback data anymore */
iter->second.pop_front();
if (status != GATT_SUCCESS) {
log::info("Client supported features charcteristic not found");
return;
}
/* Write our client supported features to the remote device */
gatt_att_write_cl_supp_feat(conn_id, p_data->att_value.handle);
break;
}
}
/*******************************************************************************
*
* Function gatt_cl_start_config_ccc
*
* Description Gatt profile start configure service change CCC
*
* Returns void
*
******************************************************************************/
static void gatt_cl_start_config_ccc(tGATT_PROFILE_CLCB* p_clcb) {
log::verbose("stage: {}", p_clcb->ccc_stage);
switch (p_clcb->ccc_stage) {
case GATT_SVC_CHANGED_SERVICE: /* discover GATT service */
if (GATTC_Discover(p_clcb->conn_id, GATT_DISC_SRVC_BY_UUID, 0x0001, 0xffff,
Uuid::From16Bit(UUID_SERVCLASS_GATT_SERVER)) != GATT_SUCCESS) {
log::warn("Unable to discovery GATT client conn_id:{}", p_clcb->conn_id);
}
break;
case GATT_SVC_CHANGED_CHARACTERISTIC: /* discover service change char */
if (GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR, 0x0001, p_clcb->e_handle,
Uuid::From16Bit(GATT_UUID_GATT_SRV_CHGD)) != GATT_SUCCESS) {
log::warn("Unable to discovery GATT client conn_id:{}", p_clcb->conn_id);
}
break;
case GATT_SVC_CHANGED_DESCRIPTOR: /* discover service change ccc */
if (GATTC_Discover(p_clcb->conn_id, GATT_DISC_CHAR_DSCPT, p_clcb->s_handle,
p_clcb->e_handle) != GATT_SUCCESS) {
log::warn("Unable to discovery GATT client conn_id:{}", p_clcb->conn_id);
}
break;
case GATT_SVC_CHANGED_CONFIGURE_CCCD: /* write ccc */
{
tGATT_VALUE ccc_value;
memset(&ccc_value, 0, sizeof(tGATT_VALUE));
ccc_value.handle = p_clcb->s_handle;
ccc_value.len = 2;
ccc_value.value[0] = GATT_CLT_CONFIG_INDICATION;
if (GATTC_Write(p_clcb->conn_id, GATT_WRITE, &ccc_value) != GATT_SUCCESS) {
log::warn("Unable to write GATT client data conn_id:{}", p_clcb->conn_id);
}
gatt_op_cb_data cb_data;
cb_data.cb =
base::BindOnce([](const RawAddress& /* bdaddr */, uint8_t /* support */) { return; });
cb_data.op_uuid = GATT_UUID_GATT_SRV_CHGD;
OngoingOps[p_clcb->conn_id].emplace_back(std::move(cb_data));
break;
}
}
}
/*******************************************************************************
*
* Function GATT_ConfigServiceChangeCCC
*
* Description Configure service change indication on remote device
*
* Returns none
*
******************************************************************************/
void GATT_ConfigServiceChangeCCC(const RawAddress& remote_bda, bool /* enable */,
tBT_TRANSPORT transport) {
tGATT_PROFILE_CLCB* p_clcb = gatt_profile_find_clcb_by_bd_addr(remote_bda, transport);
if (p_clcb == NULL) {
p_clcb = gatt_profile_clcb_alloc(0, remote_bda, transport);
}
if (p_clcb == NULL) {
return;
}
if (GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &p_clcb->conn_id, transport)) {
p_clcb->connected = true;
} else {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
remote_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_LE));
}
/* hold the link here */
if (!GATT_Connect(gatt_cb.gatt_if, remote_bda, BTM_BLE_DIRECT_CONNECTION, transport, true)) {
log::warn(
"Unable to connect GATT client gatt_if:{} peer:{} transport:{} "
"connection_tyoe:{} opporunistic:{}",
gatt_cb.gatt_if, remote_bda, bt_transport_text(transport), "BTM_BLE_DIRECT_CONNECTION",
true);
}
p_clcb->ccc_stage = GATT_SVC_CHANGED_CONNECTING;
if (!p_clcb->connected) {
/* wait for connection */
return;
}
p_clcb->ccc_stage++;
gatt_cl_start_config_ccc(p_clcb);
}
/*******************************************************************************
*
* Function gatt_cl_init_sr_status
*
* Description Restore status for trusted GATT Server device
*
* Returns none
*
******************************************************************************/
void gatt_cl_init_sr_status(tGATT_TCB& tcb) {
tcb.sr_supp_feat = btif_storage_get_sr_supp_feat(tcb.peer_bda);
if (tcb.sr_supp_feat & BLE_GATT_SVR_SUP_FEAT_EATT_BITMASK) {
bluetooth::eatt::EattExtension::AddFromStorage(tcb.peer_bda);
}
}
static bool read_sr_supported_feat_req(tCONN_ID conn_id,
base::OnceCallback<void(const RawAddress&, uint8_t)> cb) {
tGATT_READ_PARAM param = {};
param.service.s_handle = 1;
param.service.e_handle = 0xFFFF;
param.service.auth_req = 0;
param.service.uuid = bluetooth::Uuid::From16Bit(GATT_UUID_SERVER_SUP_FEAT);
if (GATTC_Read(conn_id, GATT_READ_BY_TYPE, ¶m) != GATT_SUCCESS) {
log::error("Read GATT Support features GATT_Read Failed");
return false;
}
gatt_op_cb_data cb_data;
cb_data.cb = std::move(cb);
cb_data.op_uuid = GATT_UUID_SERVER_SUP_FEAT;
OngoingOps[conn_id].emplace_back(std::move(cb_data));
return true;
}
static bool read_sr_sirk_req(tCONN_ID conn_id,
base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
uint8_t sirk_type, Octet16& sirk)>
cb) {
tGATT_READ_PARAM param = {};
tBLE_ADDR_TYPE address_type = BLE_ADDR_PUBLIC;
param.service.s_handle = 1;
param.service.e_handle = 0xFFFF;
param.service.auth_req = 0;
param.service.uuid = bluetooth::Uuid::From16Bit(GATT_UUID_CSIS_SIRK);
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
RawAddress identity_address = tcb.peer_bda;
btm_random_pseudo_to_identity_addr(&identity_address, &address_type);
if (address_type == BLE_ADDR_PUBLIC &&
interop_match_addr(INTEROP_DISABLE_SIRK_READ_BY_TYPE, &identity_address)) {
if (GATTC_Read(conn_id, GATT_READ_CHAR_VALUE, ¶m) != GATT_SUCCESS) {
log::error("Read GATT Support features GATT_Read Failed, conn_id: {}",
static_cast<int>(conn_id));
return false;
}
}
else{
if (GATTC_Read(conn_id, GATT_READ_BY_TYPE, ¶m) != GATT_SUCCESS) {
log::error("Read GATT Support features GATT_Read Failed, conn_id: {}",
static_cast<int>(conn_id));
return false;
}
}
gatt_op_cb_data cb_data;
cb_data.sirk_cb = std::move(cb);
cb_data.op_uuid = GATT_UUID_CSIS_SIRK;
OngoingOps[conn_id].emplace_back(std::move(cb_data));
return true;
}
/*******************************************************************************
*
* Function gatt_cl_read_sr_supp_feat_req
*
* Description Read remote device supported GATT feature mask.
*
* Returns bool
*
******************************************************************************/
bool gatt_cl_read_sr_supp_feat_req(const RawAddress& peer_bda,
base::OnceCallback<void(const RawAddress&, uint8_t)> cb) {
tGATT_PROFILE_CLCB* p_clcb;
tCONN_ID conn_id;
if (!cb) {
return false;
}
log::verbose("BDA: {} read gatt supported features", peer_bda);
if (!GATT_GetConnIdIfConnected(gatt_cb.gatt_if, peer_bda, &conn_id, BT_TRANSPORT_LE)) {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
peer_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_LE));
}
if (conn_id == GATT_INVALID_CONN_ID) {
return false;
}
p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (!p_clcb) {
p_clcb = gatt_profile_clcb_alloc(conn_id, peer_bda, BT_TRANSPORT_LE);
}
if (!p_clcb) {
log::verbose("p_clcb is NULL 0x{:x}", conn_id);
return false;
}
auto it = OngoingOps.find(conn_id);
if (it == OngoingOps.end()) {
OngoingOps[conn_id] = std::deque<gatt_op_cb_data>();
}
return read_sr_supported_feat_req(conn_id, std::move(cb));
}
/*******************************************************************************
*
* Function gatt_cl_read_sirk_req
*
* Description Read remote SIRK if it's a set member device.
*
* Returns bool
*
******************************************************************************/
bool gatt_cl_read_sirk_req(const RawAddress& peer_bda,
base::OnceCallback<void(tGATT_STATUS status, const RawAddress&,
uint8_t sirk_type, Octet16& sirk)>
cb) {
tGATT_PROFILE_CLCB* p_clcb;
tCONN_ID conn_id;
if (!cb) {
return false;
}
log::debug("BDA: {}, read SIRK", peer_bda);
if (!GATT_GetConnIdIfConnected(gatt_cb.gatt_if, peer_bda, &conn_id, BT_TRANSPORT_LE)) {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
peer_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_LE));
}
if (conn_id == GATT_INVALID_CONN_ID) {
return false;
}
p_clcb = gatt_profile_find_clcb_by_conn_id(conn_id);
if (!p_clcb) {
p_clcb = gatt_profile_clcb_alloc(conn_id, peer_bda, BT_TRANSPORT_LE);
}
if (!p_clcb) {
log::verbose("p_clcb is NULL, conn_id: {:04x}", conn_id);
return false;
}
auto it = OngoingOps.find(conn_id);
if (it == OngoingOps.end()) {
OngoingOps[conn_id] = std::deque<gatt_op_cb_data>();
}
return read_sr_sirk_req(conn_id, std::move(cb));
}
/*******************************************************************************
*
* Function gatt_profile_get_eatt_support
*
* Description Check if EATT is supported with remote device.
*
* Returns if EATT is supported.
*
******************************************************************************/
bool gatt_profile_get_eatt_support(const RawAddress& remote_bda) {
tCONN_ID conn_id;
log::verbose("BDA: {} read GATT support", remote_bda);
if (!GATT_GetConnIdIfConnected(gatt_cb.gatt_if, remote_bda, &conn_id, BT_TRANSPORT_LE)) {
log::warn(
"Unable to get GATT connection id if connected peer:{} gatt_if:{} "
"transport:{}",
remote_bda, gatt_cb.gatt_if, bt_transport_text(BT_TRANSPORT_LE));
}
/* This read is important only when connected */
if (conn_id == GATT_INVALID_CONN_ID) {
return false;
}
return gatt_profile_get_eatt_support_by_conn_id(conn_id);
}
bool gatt_profile_get_eatt_support_by_conn_id(tCONN_ID conn_id) {
/* Get tcb info */
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
return tcb.sr_supp_feat & BLE_GATT_SVR_SUP_FEAT_EATT_BITMASK;
}
/*******************************************************************************
*
* Function gatt_sr_is_robust_caching_enabled
*
* Description Check if Robust Caching is enabled on server side.
*
* Returns true if enabled in gd flag, otherwise false
*
******************************************************************************/
static bool gatt_sr_is_robust_caching_enabled() { return false; }
/*******************************************************************************
*
* Function gatt_sr_is_cl_robust_caching_supported
*
* Description Check if Robust Caching is supported for the connection
*
* Returns true if enabled by client side, otherwise false
*
******************************************************************************/
static bool gatt_sr_is_cl_robust_caching_supported(tGATT_TCB& tcb) {
// if robust caching is not enabled, should always return false
if (!gatt_sr_is_robust_caching_enabled()) {
return false;
}
return tcb.cl_supp_feat & BLE_GATT_CL_SUP_FEAT_CACHING_BITMASK;
}
/*******************************************************************************
*
* Function gatt_sr_is_cl_multi_variable_len_notif_supported
*
* Description Check if Multiple Variable Length Notifications
* supported for the connection
*
* Returns true if enabled by client side, otherwise false
*
******************************************************************************/
bool gatt_sr_is_cl_multi_variable_len_notif_supported(tGATT_TCB& tcb) {
return tcb.cl_supp_feat & BLE_GATT_CL_SUP_FEAT_MULTI_NOTIF_BITMASK;
}
/*******************************************************************************
*
* Function gatt_sr_is_cl_change_aware
*
* Description Check if the connection is change-aware
*
* Returns true if change aware, otherwise false
*
******************************************************************************/
bool gatt_sr_is_cl_change_aware(tGATT_TCB& tcb) {
// if robust caching is not supported, should always return true by default
if (!gatt_sr_is_cl_robust_caching_supported(tcb)) {
return true;
}
return tcb.is_robust_cache_change_aware;
}
/*******************************************************************************
*
* Function gatt_sr_init_cl_status
*
* Description Restore status for trusted device
*
* Returns none
*
******************************************************************************/
void gatt_sr_init_cl_status(tGATT_TCB& tcb) {
tcb.cl_supp_feat = btif_storage_get_gatt_cl_supp_feat(tcb.peer_bda);
// This is used to reset bit when robust caching is disabled
if (!gatt_sr_is_robust_caching_enabled()) {
tcb.cl_supp_feat &= ~BLE_GATT_CL_SUP_FEAT_CACHING_BITMASK;
}
if (gatt_sr_is_cl_robust_caching_supported(tcb)) {
Octet16 stored_hash = btif_storage_get_gatt_cl_db_hash(tcb.peer_bda);
tcb.is_robust_cache_change_aware = (stored_hash == gatt_cb.database_hash);
} else {
// set default value for untrusted device
tcb.is_robust_cache_change_aware = true;
}
log::info("bda={}, cl_supp_feat=0x{:x}, aware={}", tcb.peer_bda, tcb.cl_supp_feat,
tcb.is_robust_cache_change_aware);
}
/*******************************************************************************
*
* Function gatt_sr_update_cl_status
*
* Description Update change-aware status for the remote device
*
* Returns none
*
******************************************************************************/
void gatt_sr_update_cl_status(tGATT_TCB& tcb, bool chg_aware) {
// if robust caching is not supported, do nothing
if (!gatt_sr_is_cl_robust_caching_supported(tcb)) {
return;
}
// only when client status is changed from change-unaware to change-aware, we
// can then store database hash into btif_storage
if (!tcb.is_robust_cache_change_aware && chg_aware) {
btif_storage_set_gatt_cl_db_hash(tcb.peer_bda, gatt_cb.database_hash);
}
// only when the status is changed, print the log
if (tcb.is_robust_cache_change_aware != chg_aware) {
log::info("bda={}, chg_aware={}", tcb.peer_bda, chg_aware);
}
tcb.is_robust_cache_change_aware = chg_aware;
}
/* handle request for reading database hash */
static tGATT_STATUS gatt_sr_read_db_hash(tCONN_ID conn_id, tGATT_VALUE* p_value) {
log::info("conn_id=0x{:x}", conn_id);
uint8_t* p = p_value->value;
Octet16& db_hash = gatt_cb.database_hash;
ARRAY_TO_STREAM(p, db_hash.data(), (uint16_t)db_hash.size());
p_value->len = (uint16_t)db_hash.size();
// Every time when database hash is requested, reset flag.
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
gatt_sr_update_cl_status(gatt_cb.tcb[tcb_idx], /* chg_aware= */ true);
return GATT_SUCCESS;
}
/* handle request for reading client supported features */
static tGATT_STATUS gatt_sr_read_cl_supp_feat(tCONN_ID conn_id, tGATT_VALUE* p_value) {
// Get tcb info
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
uint8_t* p = p_value->value;
UINT8_TO_STREAM(p, tcb.cl_supp_feat);
p_value->len = 1;
return GATT_SUCCESS;
}
/* handle request for writing client supported features */
static tGATT_STATUS gatt_sr_write_cl_supp_feat(tCONN_ID conn_id, tGATT_WRITE_REQ* p_data) {
std::list<uint8_t> tmp;
uint16_t len = p_data->len;
uint8_t value, *p = p_data->value;
// Read all octets into list
while (len > 0) {
STREAM_TO_UINT8(value, p);
tmp.push_back(value);
len--;
}
// Remove trailing zero octets
while (!tmp.empty()) {
if (tmp.back() != 0x00) {
break;
}
tmp.pop_back();
}
// Get tcb info
uint8_t tcb_idx = gatt_get_tcb_idx(conn_id);
tGATT_TCB& tcb = gatt_cb.tcb[tcb_idx];
std::list<uint8_t> feature_list;
feature_list.push_back(tcb.cl_supp_feat);
// If input length is zero, return value_not_allowed
if (tmp.empty()) {
log::info("zero length, conn_id=0x{:x}, bda={}", conn_id, tcb.peer_bda);
return GATT_VALUE_NOT_ALLOWED;
}
// if original length is longer than new one, it must be the bit reset case.
if (feature_list.size() > tmp.size()) {
log::info("shorter length, conn_id=0x{:x}, bda={}", conn_id, tcb.peer_bda);
return GATT_VALUE_NOT_ALLOWED;
}
// new length is longer or equals to the original, need to check bits
// one by one. Here we use bit-wise operation.
// 1. Use XOR to locate the change bit, val_xor is the change bit mask
// 2. Use AND for val_xor and *it_new to get val_and
// 3. If val_and != val_xor, it means the change is from 1 to 0
auto it_old = feature_list.cbegin();
auto it_new = tmp.cbegin();
for (; it_old != feature_list.cend(); it_old++, it_new++) {
uint8_t val_xor = *it_old ^ *it_new;
uint8_t val_and = val_xor & *it_new;
if (val_and != val_xor) {
log::info("bit cannot be reset, conn_id=0x{:x}, bda={}", conn_id, tcb.peer_bda);
return GATT_VALUE_NOT_ALLOWED;
}
}
// get current robust caching status before setting new one
bool curr_caching_state = gatt_sr_is_cl_robust_caching_supported(tcb);
tcb.cl_supp_feat = tmp.front();
if (!gatt_sr_is_robust_caching_enabled()) {
// remove robust caching bit
tcb.cl_supp_feat &= ~BLE_GATT_CL_SUP_FEAT_CACHING_BITMASK;
log::info("reset robust caching bit, conn_id=0x{:x}, bda={}", conn_id, tcb.peer_bda);
}
// TODO(hylo): save data as byte array
btif_storage_set_gatt_cl_supp_feat(tcb.peer_bda, tcb.cl_supp_feat);
// get new robust caching status after setting new one
bool new_caching_state = gatt_sr_is_cl_robust_caching_supported(tcb);
// only when the first time robust caching request, print the log
if (!curr_caching_state && new_caching_state) {
log::info("robust caching enabled by client, conn_id=0x{:x}", conn_id);
}
return GATT_SUCCESS;
}
|