1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
48
49
50
59
60
68
69
70
71
72
81
82
83
84
85
86
87
88
89
90
91
92
93
97
98
99
100
101
102
103
104
105
111
112
113
116
117
118
119
122
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
159
160
161
165
170
171
172
173
174
175
176
177
178
179
180
186
191
196
202
205
206
207
210
211
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
260
261
262
266
270
271
274
275
276
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
337
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
365
366
367
368
369
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
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
463
464
467
468
469
470
471
476
477
478
479
480
487
488
489
490
491
492
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
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
567
568
569
570
571
578
579
580
581
582
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
612
613
614
615
616
617
618
619
620
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
660
661
662
663
670
671
684
685
689
690
691
692
693
694
702
703
704
707
712
713
714
719
720
721
722
728
729
732
733
737
738
739
740
741
742
743
744
745
746
747
748
749
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
781
782
787
788
789
790
798
799
802
803
804
805
806
807
808
809
810
814
815
816
817
818
819
820
821
822
823
824
825
840
841
842
843
844
845
846
850
851
857
858
859
860
861
862
863
864
865
870
871
872
891
892
893
897
902
903
904
908
909
910
912
913
914
915
916
917
918
919
920
921
922
923
924
927
928
929
930
931
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
962
963
964
967
968
969
970
973
974
975
976
977
978
979
980
981
988
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1009
1010
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1058
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
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
1112
1113
1114
1117
1118
1119
1120
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
1172
1173
1174
1175
1182
1183
1187
1191
1195
1196
1200
1201
1202
1203
1207
1211
1215
1219
1223
1224
1225
1229
1232
1233
1234
1235
1236
1239
1240
1241
/* ... */
/* ... */
#include "common/bt_target.h"
#if BLE_INCLUDED == TRUE && GATTC_INCLUDED == TRUE
#include <string.h>
#include "osi/allocator.h"
#include "gatt_int.h"
#include "l2c_int.h"
#define GATT_WRITE_LONG_HDR_SIZE 5
#define GATT_READ_CHAR_VALUE_HDL (GATT_READ_CHAR_VALUE | 0x80)
#define GATT_READ_INC_SRV_UUID128 (GATT_DISC_INC_SRVC | 0x90)
#define GATT_PREP_WRITE_RSP_MIN_LEN 4
#define GATT_NOTIFICATION_MIN_LEN 2
#define GATT_WRITE_RSP_MIN_LEN 2
#define GATT_INFO_RSP_MIN_LEN 1
#define GATT_MTU_RSP_MIN_LEN 2
#define GATT_READ_BY_TYPE_RSP_MIN_LEN 19 defines
/* ... */
void gatt_send_prepare_write(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb);
static const UINT8 disc_type_to_att_opcode[GATT_DISC_MAX] = {
0,
GATT_REQ_READ_BY_GRP_TYPE,
GATT_REQ_FIND_TYPE_VALUE,
GATT_REQ_READ_BY_TYPE,
GATT_REQ_READ_BY_TYPE,
GATT_REQ_READ_BY_TYPE,
GATT_REQ_FIND_INFO
}{...};
static const UINT16 disc_type_to_uuid[GATT_DISC_MAX] = {
0,
GATT_UUID_PRI_SERVICE,
GATT_UUID_PRI_SERVICE,
GATT_UUID_INCLUDE_SERVICE,
GATT_UUID_CHAR_DECLARE,
0
}{...};
#define GATT_DISC_INFO(fmt, args...) {if (gatt_cb.auto_disc == FALSE) BT_PRINT_I("BT_GATT", fmt, ## args);}
/* ... */
void gatt_act_discovery(tGATT_CLCB *p_clcb)
{
UINT8 op_code = disc_type_to_att_opcode[p_clcb->op_subtype];
tGATT_CL_MSG cl_req;
tGATT_STATUS st;
if (p_clcb->s_handle <= p_clcb->e_handle && p_clcb->s_handle != 0) {
memset(&cl_req, 0, sizeof(tGATT_CL_MSG));
cl_req.browse.s_handle = p_clcb->s_handle;
cl_req.browse.e_handle = p_clcb->e_handle;
if (disc_type_to_uuid[p_clcb->op_subtype] != 0) {
cl_req.browse.uuid.len = 2;
cl_req.browse.uuid.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
}{...}
if (p_clcb->op_subtype == GATT_DISC_SRVC_BY_UUID) {
cl_req.find_type_value.uuid.len = 2;
cl_req.find_type_value.uuid.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
cl_req.find_type_value.s_handle = p_clcb->s_handle;
cl_req.find_type_value.e_handle = p_clcb->e_handle;
cl_req.find_type_value.value_len = p_clcb->uuid.len;
if (p_clcb->uuid.len == LEN_UUID_32) {
cl_req.find_type_value.value_len = LEN_UUID_128;
gatt_convert_uuid32_to_uuid128(cl_req.find_type_value.value, p_clcb->uuid.uu.uuid32);
}{...} else {
memcpy (cl_req.find_type_value.value, &p_clcb->uuid.uu, p_clcb->uuid.len);
}{...}
}{...}
if (p_clcb->op_subtype == GATT_DISC_CHAR_BY_UUID) {
memcpy(&cl_req.browse.uuid, &p_clcb->uuid, sizeof(tBT_UUID));
}{...}
st = attp_send_cl_msg(p_clcb->p_tcb, p_clcb->clcb_idx, op_code, &cl_req);
if (st != GATT_SUCCESS && st != GATT_CMD_STARTED) {
gatt_end_operation(p_clcb, GATT_ERROR, NULL);
}{...}
}{...} else {
gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
}{...}
}{ ... }
/* ... */
void gatt_act_read (tGATT_CLCB *p_clcb, UINT16 offset)
{
tGATT_TCB *p_tcb = p_clcb->p_tcb;
UINT8 rt = GATT_INTERNAL_ERROR;
tGATT_CL_MSG msg;
UINT8 op_code = 0;
memset (&msg, 0, sizeof(tGATT_CL_MSG));
switch (p_clcb->op_subtype) {
case GATT_READ_CHAR_VALUE:
case GATT_READ_BY_TYPE:
op_code = GATT_REQ_READ_BY_TYPE;
msg.browse.s_handle = p_clcb->s_handle;
msg.browse.e_handle = p_clcb->e_handle;
if (p_clcb->op_subtype == GATT_READ_BY_TYPE) {
memcpy(&msg.browse.uuid, &p_clcb->uuid, sizeof(tBT_UUID));
}{...} else {
msg.browse.uuid.len = LEN_UUID_16;
msg.browse.uuid.uu.uuid16 = GATT_UUID_CHAR_DECLARE;
}{...}
break;
...
case GATT_READ_CHAR_VALUE_HDL:
case GATT_READ_BY_HANDLE:
if (!p_clcb->counter) {
op_code = GATT_REQ_READ;
msg.handle = p_clcb->s_handle;
}{...} else {
if (!p_clcb->first_read_blob_after_read) {
p_clcb->first_read_blob_after_read = TRUE;
}{...} else {
p_clcb->first_read_blob_after_read = FALSE;
}{...}
GATT_TRACE_DEBUG("gatt_act_read first_read_blob_after_read=%d",
p_clcb->first_read_blob_after_read);
op_code = GATT_REQ_READ_BLOB;
msg.read_blob.offset = offset;
msg.read_blob.handle = p_clcb->s_handle;
}{...}
p_clcb->op_subtype &= ~ 0x80;
break;
...
case GATT_READ_PARTIAL:
op_code = GATT_REQ_READ_BLOB;
msg.read_blob.handle = p_clcb->s_handle;
msg.read_blob.offset = offset;
break;
...
case GATT_READ_MULTIPLE:
op_code = GATT_REQ_READ_MULTI;
memcpy (&msg.read_multi, p_clcb->p_attr_buf, sizeof(tGATT_READ_MULTI));
break;
...
case GATT_READ_MULTIPLE_VAR:
op_code = GATT_REQ_READ_MULTI_VAR;
memcpy (&msg.read_multi, p_clcb->p_attr_buf, sizeof(tGATT_READ_MULTI));
break;
...
case GATT_READ_INC_SRV_UUID128:
op_code = GATT_REQ_READ;
msg.handle = p_clcb->s_handle;
p_clcb->op_subtype &= ~ 0x90;
break;
...
default:
GATT_TRACE_ERROR("Unknown read type: %d", p_clcb->op_subtype);
break;...
}{...}
if (op_code != 0) {
rt = attp_send_cl_msg(p_tcb, p_clcb->clcb_idx, op_code, &msg);
}{...}
if ( op_code == 0 || (rt != GATT_SUCCESS && rt != GATT_CMD_STARTED)) {
gatt_end_operation(p_clcb, rt, NULL);
}{...}
}{ ... }
/* ... */
void gatt_act_write (tGATT_CLCB *p_clcb, UINT8 sec_act)
{
tGATT_TCB *p_tcb = p_clcb->p_tcb;
UINT8 rt = GATT_SUCCESS, op_code = 0;
tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
if (p_attr) {
switch (p_clcb->op_subtype) {
case GATT_WRITE_NO_RSP:
l2ble_update_att_acl_pkt_num(L2CA_DECREASE_BTU_NUM, NULL);
p_clcb->s_handle = p_attr->handle;
op_code = (sec_act == GATT_SEC_SIGN_DATA) ? GATT_SIGN_CMD_WRITE : GATT_CMD_WRITE;
rt = gatt_send_write_msg(p_tcb,
p_clcb->clcb_idx,
op_code,
p_attr->handle,
p_attr->len,
0,
p_attr->value);
break;
...
case GATT_WRITE:
if (p_attr->len <= (p_tcb->payload_size - GATT_HDR_SIZE)) {
p_clcb->s_handle = p_attr->handle;
rt = gatt_send_write_msg(p_tcb,
p_clcb->clcb_idx,
GATT_REQ_WRITE,
p_attr->handle,
p_attr->len,
0,
p_attr->value);
}{...} else {
gatt_send_prepare_write(p_tcb, p_clcb);
}{...}
break;
...
case GATT_WRITE_PREPARE:
gatt_send_prepare_write(p_tcb, p_clcb);
break;
...
default:
rt = GATT_INTERNAL_ERROR;
GATT_TRACE_ERROR("Unknown write type: %d", p_clcb->op_subtype);
break;...
}{...}
}{...} else {
rt = GATT_INTERNAL_ERROR;
}{...}
if ((rt != GATT_SUCCESS && rt != GATT_CMD_STARTED && rt != GATT_CONGESTED)
|| (rt != GATT_CMD_STARTED && p_clcb->op_subtype == GATT_WRITE_NO_RSP)) {
if (rt != GATT_SUCCESS) {
GATT_TRACE_DEBUG("gatt_act_write() failed op_code=0x%x rt=%d", op_code, rt);
}{...}
gatt_end_operation(p_clcb, rt, NULL);
}{...}
}{ ... }
/* ... */
void gatt_send_queue_write_cancel (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_EXEC_FLAG flag)
{
UINT8 rt ;
GATT_TRACE_DEBUG("gatt_send_queue_write_cancel ");
rt = attp_send_cl_msg(p_tcb, p_clcb->clcb_idx, GATT_REQ_EXEC_WRITE, (tGATT_CL_MSG *)&flag);
if (rt != GATT_SUCCESS) {
gatt_end_operation(p_clcb, rt, NULL);
}{...}
}{ ... }
/* ... */
BOOLEAN gatt_check_write_long_terminate(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, tGATT_VALUE *p_rsp_value)
{
tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
BOOLEAN exec = FALSE;
tGATT_EXEC_FLAG flag = GATT_PREP_WRITE_EXEC;
GATT_TRACE_DEBUG("gatt_check_write_long_terminate ");
if (p_rsp_value != NULL) {
if (p_rsp_value->handle != p_attr->handle ||
p_rsp_value->len != p_clcb->counter ||
memcmp(p_rsp_value->value, p_attr->value + p_attr->offset, p_rsp_value->len)) {
p_clcb->status = GATT_ERROR;
flag = GATT_PREP_WRITE_CANCEL;
exec = TRUE;
}{...} else {
p_clcb->status = GATT_SUCCESS;
if ((p_attr->offset += p_rsp_value->len) >= p_attr->len) {
exec = TRUE;
}{...}
}{...}
}{...}
if (exec) {
gatt_send_queue_write_cancel (p_tcb, p_clcb, flag);
return TRUE;
}{...}
return FALSE;
}{ ... }
/* ... */
void gatt_send_prepare_write(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb)
{
tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
UINT16 to_send, offset;
UINT8 rt = GATT_SUCCESS;
UINT8 type = p_clcb->op_subtype;
GATT_TRACE_DEBUG("gatt_send_prepare_write type=0x%x", type );
to_send = p_attr->len - p_attr->offset;
if (to_send > (p_tcb->payload_size - GATT_WRITE_LONG_HDR_SIZE)) {
to_send = p_tcb->payload_size - GATT_WRITE_LONG_HDR_SIZE;
}{...}
p_clcb->s_handle = p_attr->handle;
offset = p_attr->offset;
if (type == GATT_WRITE_PREPARE) {
offset += p_clcb->start_offset;
}{...}
GATT_TRACE_DEBUG("offset =0x%x len=%d", offset, to_send );
rt = gatt_send_write_msg(p_tcb,
p_clcb->clcb_idx,
GATT_REQ_PREPARE_WRITE,
p_attr->handle,
to_send,
offset,
p_attr->value + p_attr->offset);
p_clcb->counter = to_send;
if (rt != GATT_SUCCESS && rt != GATT_CMD_STARTED) {
gatt_end_operation(p_clcb, rt, NULL);
}{...}
}{ ... }
/* ... */
void gatt_process_find_type_value_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT16 len, UINT8 *p_data)
{
tGATT_DISC_RES result;
UINT8 *p = p_data;
UNUSED(p_tcb);
GATT_TRACE_DEBUG("gatt_process_find_type_value_rsp ");
if (p_clcb->operation != GATTC_OPTYPE_DISCOVERY || p_clcb->op_subtype != GATT_DISC_SRVC_BY_UUID) {
return;
}{...}
memset (&result, 0, sizeof(tGATT_DISC_RES));
result.type.len = 2;
result.type.uu.uuid16 = GATT_UUID_PRI_SERVICE;
while (len >= 4) {
STREAM_TO_UINT16 (result.handle, p);
STREAM_TO_UINT16 (result.value.group_value.e_handle, p);
GATT_DISC_INFO("%s handle %x, end handle %x", __func__, result.handle, result.value.group_value.e_handle);
memcpy (&result.value.group_value.service_type, &p_clcb->uuid, sizeof(tBT_UUID));
len -= 4;
if (p_clcb->p_reg->app_cb.p_disc_res_cb) {
(*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
}{...}
}{...}
p_clcb->s_handle = (result.value.group_value.e_handle == 0) ? 0 : (result.value.group_value.e_handle + 1);
gatt_act_discovery(p_clcb) ;
}{ ... }
/* ... */
void gatt_process_read_info_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
tGATT_DISC_RES result;
UINT8 *p = p_data, uuid_len = 0, type;
UNUSED(p_tcb);
UNUSED(op_code);
if (len < GATT_INFO_RSP_MIN_LEN) {
GATT_TRACE_ERROR("invalid Info Response PDU received, discard.");
gatt_end_operation(p_clcb, GATT_INVALID_PDU, NULL);
return;
}{...}
if (p_clcb->operation != GATTC_OPTYPE_DISCOVERY || p_clcb->op_subtype != GATT_DISC_CHAR_DSCPT) {
return;
}{...}
STREAM_TO_UINT8(type, p);
len -= 1;
if (type == GATT_INFO_TYPE_PAIR_16) {
uuid_len = LEN_UUID_16;
}{...} else if (type == GATT_INFO_TYPE_PAIR_128) {
uuid_len = LEN_UUID_128;
}{...}
while (len >= uuid_len + 2) {
STREAM_TO_UINT16 (result.handle, p);
if (uuid_len > 0) {
if (!gatt_parse_uuid_from_cmd(&result.type, uuid_len, &p)) {
break;
}{...}
}{...} else {
memcpy (&result.type, &p_clcb->uuid, sizeof(tBT_UUID));
}{...}
len -= (uuid_len + 2);
GATT_DISC_INFO("%s handle %x, uuid %s", __func__, result.handle, gatt_uuid_to_str(&result.type));
if (p_clcb->p_reg->app_cb.p_disc_res_cb) {
(*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
}{...}
}{...}
p_clcb->s_handle = (result.handle == 0) ? 0 : (result.handle + 1);
gatt_act_discovery(p_clcb) ;
}{ ... }
/* ... */
void gatt_proc_disc_error_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 opcode,
UINT16 handle, UINT8 reason)
{
tGATT_STATUS status = (tGATT_STATUS) reason;
UNUSED(p_tcb);
UNUSED(handle);
GATT_TRACE_DEBUG("gatt_proc_disc_error_rsp reason: %02x cmd_code %04x", reason, opcode);
switch (opcode) {
case GATT_REQ_READ_BY_GRP_TYPE:
case GATT_REQ_FIND_TYPE_VALUE:
case GATT_REQ_READ_BY_TYPE:
case GATT_REQ_FIND_INFO:
if (reason == GATT_NOT_FOUND) {
status = GATT_SUCCESS;
GATT_DISC_INFO("Discovery completed");
}{...}
break;...
default:
GATT_TRACE_ERROR("Incorrect discovery opcode %04x", opcode);
break;...
}{...}
gatt_end_operation(p_clcb, status, NULL);
}{ ... }
/* ... */
void gatt_process_error_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
UINT8 opcode, reason, * p = p_data;
UINT16 handle;
tGATT_VALUE *p_attr = (tGATT_VALUE *)p_clcb->p_attr_buf;
UNUSED(op_code);
UNUSED(len);
GATT_TRACE_DEBUG("%s", __func__);
STREAM_TO_UINT8(opcode, p);
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT8(reason, p);
if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
gatt_proc_disc_error_rsp(p_tcb, p_clcb, opcode, handle, reason);
}{...} else {
if ( (p_clcb->operation == GATTC_OPTYPE_WRITE) &&
(p_clcb->op_subtype == GATT_WRITE) &&
(opcode == GATT_REQ_PREPARE_WRITE) &&
(p_attr) &&
(handle == p_attr->handle) ) {
if (reason == GATT_SUCCESS){
reason = GATT_ERROR;
}{...}
p_clcb->status = reason;
gatt_send_queue_write_cancel(p_tcb, p_clcb, GATT_PREP_WRITE_CANCEL);
}{...} else if ((p_clcb->operation == GATTC_OPTYPE_READ) &&
((p_clcb->op_subtype == GATT_READ_CHAR_VALUE_HDL) ||
(p_clcb->op_subtype == GATT_READ_BY_HANDLE)) &&
(opcode == GATT_REQ_READ_BLOB) &&
p_clcb->first_read_blob_after_read &&
(reason == GATT_NOT_LONG)) {
gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p_clcb->p_attr_buf);
}{...} else {
gatt_end_operation(p_clcb, reason, NULL);
}{...}
}{...}
}{ ... }
/* ... */
void gatt_process_prep_write_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
tGATT_VALUE value = {0};
UINT8 *p = p_data;
GATT_TRACE_DEBUG("value resp op_code = %s len = %d", gatt_dbg_op_name(op_code), len);
if (len < GATT_PREP_WRITE_RSP_MIN_LEN) {
GATT_TRACE_ERROR("illegal prepare write response length, discard");
gatt_end_operation(p_clcb, GATT_INVALID_PDU, &value);
return;
}{...}
STREAM_TO_UINT16 (value.handle, p);
STREAM_TO_UINT16 (value.offset, p);
value.len = len - 4;
memcpy (value.value, p, value.len);
if (p_clcb->op_subtype == GATT_WRITE_PREPARE) {
p_clcb->status = GATT_SUCCESS;
/* ... */
gatt_end_operation(p_clcb, p_clcb->status, &value);
}{...} else if (p_clcb->op_subtype == GATT_WRITE ) {
if (!gatt_check_write_long_terminate(p_tcb, p_clcb, &value)) {
gatt_send_prepare_write(p_tcb, p_clcb);
}{...}
}{...}
}{ ... }
/* ... */
void gatt_process_notification(tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
tGATT_VALUE value = {0};
tGATT_REG *p_reg;
UINT16 conn_id;
tGATT_STATUS encrypt_status;
UINT8 *p = p_data, i,
event = (op_code == GATT_HANDLE_VALUE_IND) ? GATTC_OPTYPE_INDICATION: GATTC_OPTYPE_NOTIFICATION;
GATT_TRACE_DEBUG("gatt_process_notification ");
if (len < GATT_NOTIFICATION_MIN_LEN) {
GATT_TRACE_ERROR("illegal notification PDU length, discard");
return;
}{...}
STREAM_TO_UINT16 (value.handle, p);
if (!GATT_HANDLE_IS_VALID(value.handle)) {
if (op_code == GATT_HANDLE_VALUE_IND) {
attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, NULL);
}{...}
return;
}{...}
if (op_code == GATT_HANDLE_MULTI_VALUE_NOTIF) {
if (len < GATT_NOTIFICATION_MIN_LEN + 2) {
GATT_TRACE_ERROR("illegal notification PDU length, discard");
return;
}{...}
STREAM_TO_UINT16(value.len, p);
if (value.len > len - 4) {
return;
}{...}
}{...} else {
value.len = len - 2;
}{...}
if (value.len > GATT_MAX_ATTR_LEN) {
GATT_TRACE_ERROR("value length larger than GATT_MAX_ATTR_LEN, discard");
return;
}{...}
memcpy(value.value, p, value.len);
p += value.len;
if (event == GATTC_OPTYPE_INDICATION) {
if (p_tcb->ind_count) {
/* ... */
GATT_TRACE_ERROR("gatt_process_notification rcv Ind. but ind_count=%d (will reset ind_count)", p_tcb->ind_count);
}{...}
p_tcb->ind_count = 0;
/* ... */
for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
if (p_reg->in_use && p_reg->app_cb.p_cmpl_cb && (event == GATTC_OPTYPE_INDICATION)) {
p_tcb->ind_count++;
}{...}
}{...}
if (p_tcb->ind_count > 0) {
gatt_start_ind_ack_timer(p_tcb);
}{...} else {
attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, NULL);
}{...}
}{...}
encrypt_status = gatt_get_link_encrypt_status(p_tcb);
for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
if (p_reg->in_use && p_reg->app_cb.p_cmpl_cb) {
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
(*p_reg->app_cb.p_cmpl_cb) (conn_id, event, encrypt_status, (tGATT_CL_COMPLETE *)&value);
}{...}
}{...}
if (op_code != GATT_HANDLE_MULTI_VALUE_NOTIF) {
return;
}{...}
if (len < (4 + value.len)) {
GATT_TRACE_ERROR("no remain data for multi notification");
return;
}{...}
len -= (4 + value.len);
while (len > 4) {
STREAM_TO_UINT16(value.handle, p);
STREAM_TO_UINT16(value.len, p);
len -= 4;
value.len = MIN(len, value.len);
memcpy(value.value, p, value.len);
p += value.len;
len -= value.len;
for (i = 0, p_reg = gatt_cb.cl_rcb; i < GATT_MAX_APPS; i++, p_reg++) {
if (p_reg->in_use && p_reg->app_cb.p_cmpl_cb) {
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_reg->gatt_if);
(*p_reg->app_cb.p_cmpl_cb) (conn_id, event, encrypt_status, (tGATT_CL_COMPLETE *)&value);
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void gatt_process_read_by_type_rsp (tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
tGATT_DISC_RES result;
tGATT_DISC_VALUE record_value;
UINT8 *p = p_data, value_len, handle_len = 2;
UINT16 handle = 0;
if (((!p_clcb->p_reg) || (!p_clcb->p_reg->app_cb.p_disc_res_cb)) && (p_clcb->operation == GATTC_OPTYPE_DISCOVERY)) {
return;
}{...}
if (len < GATT_READ_BY_TYPE_RSP_MIN_LEN) {
GATT_TRACE_ERROR("Illegal ReadByType/ReadByGroupType Response length, discard");
gatt_end_operation(p_clcb, GATT_INVALID_PDU, NULL);
return;
}{...}
STREAM_TO_UINT8(value_len, p);
if ((value_len > (p_tcb->payload_size - 2)) || (value_len > (len - 1)) ) {
/* ... */
GATT_TRACE_ERROR("gatt_process_read_by_type_rsp: Discard response op_code=%d vale_len=%d > (MTU-2=%d or msg_len-1=%d)",
op_code, value_len, (p_tcb->payload_size - 2), (len - 1));
gatt_end_operation(p_clcb, GATT_ERROR, NULL);
return;
}{...}
if (op_code == GATT_RSP_READ_BY_GRP_TYPE) {
handle_len = 4;
}{...}
value_len -= handle_len;
len -= 1;
while (len >= (handle_len + value_len)) {
STREAM_TO_UINT16(handle, p);
GATT_DISC_INFO("%s op %x, handle %x", __func__, op_code, handle);
if (!GATT_HANDLE_IS_VALID(handle)) {
gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
return;
}{...}
memset(&result, 0, sizeof(tGATT_DISC_RES));
memset(&record_value, 0, sizeof(tGATT_DISC_VALUE));
result.handle = handle;
result.type.len = 2;
result.type.uu.uuid16 = disc_type_to_uuid[p_clcb->op_subtype];
if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
p_clcb->op_subtype == GATT_DISC_SRVC_ALL &&
op_code == GATT_RSP_READ_BY_GRP_TYPE) {
STREAM_TO_UINT16(handle, p);
if (!GATT_HANDLE_IS_VALID(handle)) {
gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
return;
}{...} else {
record_value.group_value.e_handle = handle;
if (!gatt_parse_uuid_from_cmd(&record_value.group_value.service_type, value_len, &p)) {
GATT_TRACE_ERROR("discover all service response parsing failure");
break;
}{...}
}{...}
GATT_DISC_INFO("DISC ALL SVC end handle %x, uuid %s", record_value.group_value.e_handle, gatt_uuid_to_str(&record_value.group_value.service_type));
}{...}
else if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY && p_clcb->op_subtype == GATT_DISC_INC_SRVC) {
STREAM_TO_UINT16(record_value.incl_service.s_handle, p);
STREAM_TO_UINT16(record_value.incl_service.e_handle, p);
if (!GATT_HANDLE_IS_VALID(record_value.incl_service.s_handle) ||
!GATT_HANDLE_IS_VALID(record_value.incl_service.e_handle)) {
gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
return;
}{...}
if (value_len == 6) {
STREAM_TO_UINT16(record_value.incl_service.service_type.uu.uuid16, p);
record_value.incl_service.service_type.len = LEN_UUID_16;
GATT_DISC_INFO("DISC INC SVC start handle %x, end handle %x, uuid %s",
record_value.incl_service.s_handle, record_value.incl_service.e_handle, gatt_uuid_to_str(&record_value.incl_service.service_type));
}{...} else if (value_len == 4) {
p_clcb->s_handle = record_value.incl_service.s_handle;
p_clcb->read_uuid128.wait_for_read_rsp = TRUE;
p_clcb->read_uuid128.next_disc_start_hdl = handle + 1;
memcpy(&p_clcb->read_uuid128.result, &result, sizeof(result));
memcpy(&p_clcb->read_uuid128.result.value, &record_value, sizeof (result.value));
p_clcb->op_subtype |= 0x90;
gatt_act_read(p_clcb, 0);
return;
}{...} else {
GATT_TRACE_ERROR("gatt_process_read_by_type_rsp INCL_SRVC failed with invalid data value_len=%d", value_len);
gatt_end_operation(p_clcb, GATT_INVALID_PDU, (void *)p);
return;
}{...}
}{...}
else if (p_clcb->operation == GATTC_OPTYPE_READ && p_clcb->op_subtype == GATT_READ_BY_TYPE) {
p_clcb->counter = len - 2;
p_clcb->s_handle = handle;
if ( p_clcb->counter == (p_clcb->p_tcb->payload_size - 4)) {
p_clcb->op_subtype = GATT_READ_BY_HANDLE;
if (!p_clcb->p_attr_buf) {
p_clcb->p_attr_buf = (UINT8 *)osi_malloc(GATT_MAX_ATTR_LEN);
}{...}
if (p_clcb->p_attr_buf && p_clcb->counter <= GATT_MAX_ATTR_LEN) {
memcpy(p_clcb->p_attr_buf, p, p_clcb->counter);
gatt_act_read(p_clcb, p_clcb->counter);
}{...} else {
gatt_end_operation(p_clcb, GATT_INTERNAL_ERROR, (void *)p);
}{...}
}{...} else {
gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p);
}{...}
return;
}{...} else {
STREAM_TO_UINT8 (record_value.dclr_value.char_prop, p);
STREAM_TO_UINT16(record_value.dclr_value.val_handle, p);
if (!GATT_HANDLE_IS_VALID(record_value.dclr_value.val_handle)) {
gatt_end_operation(p_clcb, GATT_INVALID_HANDLE, NULL);
return;
}{...}
if (!gatt_parse_uuid_from_cmd(&record_value.dclr_value.char_uuid, (UINT16)(value_len - 3), &p)) {
gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
return;
}{...}
if (!gatt_uuid_compare(record_value.dclr_value.char_uuid, p_clcb->uuid)) {
len -= (value_len + 2);
continue;
}{...} else if (p_clcb->operation == GATTC_OPTYPE_READ)
{
/* ... */
p_clcb->s_handle = record_value.dclr_value.val_handle;
p_clcb->op_subtype |= 0x80;
gatt_act_read(p_clcb, 0);
return;
}{...}
}{...}
len -= (value_len + handle_len);
memcpy (&result.value, &record_value, sizeof (result.value));
if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY && p_clcb->p_reg->app_cb.p_disc_res_cb) {
(*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &result);
}{...}
}{...}
p_clcb->s_handle = (handle == 0) ? 0 : (handle + 1);
if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY) {
gatt_act_discovery(p_clcb) ;
}{...} else {
gatt_act_read(p_clcb, 0);
}{...}
}{ ... }
/* ... */
void gatt_process_read_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
UINT16 offset = p_clcb->counter;
UINT8 *p = p_data;
UNUSED(op_code);
if (p_clcb->operation == GATTC_OPTYPE_READ) {
if (p_clcb->op_subtype != GATT_READ_BY_HANDLE) {
p_clcb->counter = len;
gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p);
}{...} else {
if (!p_clcb->p_attr_buf) {
p_clcb->p_attr_buf = (UINT8 *)osi_malloc(GATT_MAX_ATTR_LEN);
}{...}
if (p_clcb->p_attr_buf && offset < GATT_MAX_ATTR_LEN) {
if ((len + offset) > GATT_MAX_ATTR_LEN) {
len = GATT_MAX_ATTR_LEN - offset;
}{...}
p_clcb->counter += len;
memcpy(p_clcb->p_attr_buf + offset, p, len);
if (len == (p_tcb->payload_size - 1) &&
len + offset < GATT_MAX_ATTR_LEN) {
GATT_TRACE_DEBUG("full pkt issue read blob for remianing bytes old offset=%d len=%d new offset=%d",
offset, len, p_clcb->counter);
gatt_act_read(p_clcb, p_clcb->counter);
}{...} else {
gatt_end_operation(p_clcb, GATT_SUCCESS, (void *)p_clcb->p_attr_buf);
}{...}
}{...} else {
GATT_TRACE_ERROR("attr offset = %d p_attr_buf = %p ", offset, p_clcb->p_attr_buf);
gatt_end_operation(p_clcb, GATT_NO_RESOURCES, (void *)p_clcb->p_attr_buf);
}{...}
}{...}
}{...} else {
if (p_clcb->operation == GATTC_OPTYPE_DISCOVERY &&
p_clcb->op_subtype == GATT_DISC_INC_SRVC &&
p_clcb->read_uuid128.wait_for_read_rsp ) {
p_clcb->s_handle = p_clcb->read_uuid128.next_disc_start_hdl;
p_clcb->read_uuid128.wait_for_read_rsp = FALSE;
if (len == LEN_UUID_128) {
memcpy(p_clcb->read_uuid128.result.value.incl_service.service_type.uu.uuid128, p, len);
p_clcb->read_uuid128.result.value.incl_service.service_type.len = LEN_UUID_128;
tGATT_INCL_SRVC *inc_srvc = &p_clcb->read_uuid128.result.value.incl_service;
GATT_DISC_INFO("DISC INC SRVC start handle %x, end handle %x, uuid %s",
inc_srvc->s_handle, inc_srvc->e_handle, gatt_uuid_to_str(&inc_srvc->service_type));
if ( p_clcb->p_reg->app_cb.p_disc_res_cb) {
(*p_clcb->p_reg->app_cb.p_disc_res_cb)(p_clcb->conn_id, p_clcb->op_subtype, &p_clcb->read_uuid128.result);
}{...}
gatt_act_discovery(p_clcb) ;
}{...} else {
gatt_end_operation(p_clcb, GATT_INVALID_PDU, (void *)p);
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void gatt_process_handle_rsp(tGATT_CLCB *p_clcb)
{
gatt_end_operation(p_clcb, GATT_SUCCESS, NULL);
}{ ... }
/* ... */
void gatt_process_mtu_rsp(tGATT_TCB *p_tcb, tGATT_CLCB *p_clcb, UINT16 len, UINT8 *p_data)
{
UINT16 mtu;
tGATT_STATUS status = GATT_SUCCESS;
if (len < GATT_MTU_RSP_MIN_LEN) {
GATT_TRACE_ERROR("invalid MTU response PDU received, discard.");
status = GATT_INVALID_PDU;
}{...} else {
STREAM_TO_UINT16(mtu, p_data);
if (mtu < p_tcb->payload_size && mtu >= GATT_DEF_BLE_MTU_SIZE) {
p_tcb->payload_size = mtu;
}{...}
}{...}
/* ... */
gatt_end_operation(p_clcb, status, NULL);
}{ ... }
/* ... */
UINT8 gatt_cmd_to_rsp_code (UINT8 cmd_code)
{
UINT8 rsp_code = 0;
if (cmd_code > 1 && cmd_code != GATT_CMD_WRITE) {
rsp_code = cmd_code + 1;
}{...}
return rsp_code;
}{ ... }
/* ... */
BOOLEAN gatt_cl_send_next_cmd_inq(tGATT_TCB *p_tcb)
{
tGATT_CMD_Q *p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
BOOLEAN sent = FALSE;
UINT8 rsp_code;
tGATT_CLCB *p_clcb = NULL;
tGATT_STATUS att_ret = GATT_SUCCESS;
while (!sent &&
p_tcb->pending_cl_req != p_tcb->next_slot_inq &&
p_cmd->to_send && p_cmd->p_cmd != NULL) {
att_ret = attp_send_msg_to_l2cap(p_tcb, p_cmd->p_cmd);
if (att_ret == GATT_SUCCESS || att_ret == GATT_CONGESTED) {
sent = TRUE;
p_cmd->to_send = FALSE;
if(p_cmd->p_cmd) {
osi_free(p_cmd->p_cmd);
p_cmd->p_cmd = NULL;
}{...}
if (p_cmd->op_code != GATT_CMD_WRITE && p_cmd->op_code != GATT_SIGN_CMD_WRITE) {
gatt_start_rsp_timer (p_cmd->clcb_idx);
}{...} else {
p_clcb = gatt_cmd_dequeue(p_tcb, &rsp_code);
if (att_ret == GATT_SUCCESS) {
sent = FALSE;
}{...}
p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
gatt_end_operation(p_clcb, att_ret, NULL);
}{...}
}{...} else {
GATT_TRACE_ERROR("gatt_cl_send_next_cmd_inq: L2CAP sent error");
memset(p_cmd, 0, sizeof(tGATT_CMD_Q));
p_tcb->pending_cl_req ++;
p_cmd = &p_tcb->cl_cmd_q[p_tcb->pending_cl_req];
}{...}
}{...}
return sent;
}{ ... }
/* ... */
void gatt_client_handle_server_rsp (tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
tGATT_CLCB *p_clcb = NULL;
UINT8 rsp_code;
if (op_code != GATT_HANDLE_VALUE_IND && op_code != GATT_HANDLE_VALUE_NOTIF &&
op_code != GATT_HANDLE_MULTI_VALUE_NOTIF) {
p_clcb = gatt_cmd_dequeue(p_tcb, &rsp_code);
rsp_code = gatt_cmd_to_rsp_code(rsp_code);
if (p_clcb == NULL || (rsp_code != op_code && op_code != GATT_RSP_ERROR)) {
GATT_TRACE_WARNING ("ATT - Ignore wrong response. Receives (%02x) \
Request(%02x) Ignored", op_code, rsp_code);
return;
}{...} else {
btu_stop_timer (&p_clcb->rsp_timer_ent);
p_clcb->retry_count = 0;
}{...}
}{...}
if (len >= p_tcb->payload_size) {
GATT_TRACE_ERROR("invalid response/indicate pkt size: %d, PDU size: %d", len + 1, p_tcb->payload_size);
if (op_code != GATT_HANDLE_VALUE_NOTIF && op_code != GATT_HANDLE_VALUE_IND &&
op_code != GATT_HANDLE_MULTI_VALUE_NOTIF) {
gatt_end_operation(p_clcb, GATT_ERROR, NULL);
}{...}
}{...} else {
switch (op_code) {
case GATT_RSP_ERROR:
gatt_process_error_rsp(p_tcb, p_clcb, op_code, len, p_data);
break;
...
case GATT_RSP_MTU:
gatt_process_mtu_rsp(p_tcb, p_clcb, len , p_data);
break;
...
case GATT_RSP_FIND_INFO:
gatt_process_read_info_rsp(p_tcb, p_clcb, op_code, len, p_data);
break;
...
case GATT_RSP_READ_BY_TYPE:
case GATT_RSP_READ_BY_GRP_TYPE:
gatt_process_read_by_type_rsp(p_tcb, p_clcb, op_code, len, p_data);
break;
...
case GATT_RSP_READ:
case GATT_RSP_READ_BLOB:
case GATT_RSP_READ_MULTI:
case GATT_RSP_READ_MULTI_VAR:
gatt_process_read_rsp(p_tcb, p_clcb, op_code, len, p_data);
break;
...
case GATT_RSP_FIND_TYPE_VALUE:
gatt_process_find_type_value_rsp(p_tcb, p_clcb, len, p_data);
break;
...
case GATT_RSP_WRITE:
gatt_process_handle_rsp(p_clcb);
break;
...
case GATT_RSP_PREPARE_WRITE:
gatt_process_prep_write_rsp(p_tcb, p_clcb, op_code, len, p_data);
break;
...
case GATT_RSP_EXEC_WRITE:
gatt_end_operation(p_clcb, p_clcb->status, NULL);
break;
...
case GATT_HANDLE_VALUE_NOTIF:
case GATT_HANDLE_VALUE_IND:
case GATT_HANDLE_MULTI_VALUE_NOTIF:
gatt_process_notification(p_tcb, op_code, len, p_data);
break;
...
default:
GATT_TRACE_ERROR("Unknown opcode = %d", op_code);
break;...
}{...}
}{...}
if (op_code != GATT_HANDLE_VALUE_IND && op_code != GATT_HANDLE_VALUE_NOTIF &&
op_code != GATT_HANDLE_MULTI_VALUE_NOTIF) {
gatt_cl_send_next_cmd_inq(p_tcb);
}{...}
}{ ... }
/* ... */#endif