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
46
47
48
49
50
51
52
53
54
57
58
59
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
140
141
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
172
173
174
175
179
180
181
182
183
184
185
186
187
188
189
190
191
195
196
197
198
199
200
201
202
203
204
205
206
209
210
211
221
222
223
224
225
226
234
235
245
246
249
250
254
255
256
257
258
259
269
270
271
272
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
293
294
295
296
300
301
302
303
304
305
306
307
308
309
310
311
312
316
317
318
319
320
321
322
323
324
325
326
327
330
331
332
342
343
344
345
346
347
351
352
353
363
367
368
369
370
371
380
381
382
383
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
422
425
426
429
430
431
432
433
440
441
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
517
518
519
533
534
535
551
552
553
554
557
558
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
635
640
641
642
643
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
681
682
683
688
689
692
693
694
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
737
738
739
749
750
751
752
753
754
755
756
757
763
764
767
768
769
770
771
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
806
807
808
809
810
811
812
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
882
883
884
885
901
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
961
962
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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1022
1023
1024
1025
1026
1027
1028
1029
1030
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1065
1066
1067
1074
1075
1079
1080
1081
1082
1083
1084
1085
1086
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
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
1186
1190
1191
1192
1193
1194
1195
1196
1197
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
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1277
1278
1279
1280
1281
1282
1286
1287
1288
1289
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1399
1400
1401
1402
1405
1406
1407
1408
1409
1410
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1430
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1449
1450
1451
1452
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1483
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1585
1586
1587
1591
1593
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1732
1733
1734
1735
1736
1737
1738
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1774
1775
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1790
1791
1794
1795
1796
1797
1800
1801
1802
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1833
1834
1835
1836
1837
1838
1839
1840
1841
1844
1845
1846
1847
1852
1853
1854
1858
1862
1868
1869
1870
1871
1872
1873
1877
1881
1885
1889
1890
1894
1896
1897
1898
1899
1900
/* ... */
/* ... */
#include "common/bt_target.h"
#include "osi/allocator.h"
#if BLE_INCLUDED == TRUE && GATTS_INCLUDED == TRUE
#include <string.h>
#include "gatt_int.h"
#include "stack/l2c_api.h"
#include "l2c_int.h"
#define GATT_MTU_REQ_MIN_LEN 2
/* ... */
tGATT_STATUS gatt_send_packet (tGATT_TCB *p_tcb, UINT8 *p_data, UINT16 len)
{
BT_HDR *p_msg = NULL;
UINT8 *p_m = NULL;
UINT16 buf_len;
tGATT_STATUS status;
if (len > p_tcb->payload_size){
return GATT_ILLEGAL_PARAMETER;
}{...}
buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
if ((p_msg = (BT_HDR *)osi_malloc(buf_len)) == NULL) {
return GATT_NO_RESOURCES;
}{...}
memset(p_msg, 0, buf_len);
p_msg->len = len;
p_m = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
memcpy(p_m, p_data, len);
status = attp_send_sr_msg(p_tcb, p_msg);
return status;
}{ ... }
/* ... */
UINT32 gatt_sr_enqueue_cmd (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 handle)
{
tGATT_SR_CMD *p_cmd = &p_tcb->sr_cmd;
UINT32 trans_id = 0;
if ( (p_cmd->op_code == 0) ||
(op_code == GATT_HANDLE_VALUE_CONF)) {
if (op_code == GATT_CMD_WRITE ||
op_code == GATT_SIGN_CMD_WRITE ||
op_code == GATT_REQ_MTU ||
op_code == GATT_HANDLE_VALUE_CONF) {
trans_id = ++p_tcb->trans_id;
}{...} else {
p_cmd->trans_id = ++p_tcb->trans_id;
p_cmd->op_code = op_code;
p_cmd->handle = handle;
p_cmd->status = GATT_NOT_FOUND;
p_tcb->trans_id %= GATT_TRANS_ID_MAX;
trans_id = p_cmd->trans_id;
}{...}
}{...}
return trans_id;
}{ ... }
/* ... */
BOOLEAN gatt_sr_cmd_empty (tGATT_TCB *p_tcb)
{
return (p_tcb->sr_cmd.op_code == 0);
}{ ... }
/* ... */
void gatt_dequeue_sr_cmd (tGATT_TCB *p_tcb)
{
GATT_TRACE_DEBUG("gatt_dequeue_sr_cmd" );
if (p_tcb->sr_cmd.p_rsp_msg) {
GATT_TRACE_ERROR("%s free msg %p", __func__, p_tcb->sr_cmd.p_rsp_msg);
osi_free(p_tcb->sr_cmd.p_rsp_msg);
p_tcb->sr_cmd.p_rsp_msg = NULL;
}{...}
if (p_tcb->sr_cmd.multi_rsp_q) {
while (!fixed_queue_is_empty(p_tcb->sr_cmd.multi_rsp_q)) {
osi_free(fixed_queue_dequeue(p_tcb->sr_cmd.multi_rsp_q, 0));
}{...}
fixed_queue_free(p_tcb->sr_cmd.multi_rsp_q, NULL);
}{...}
memset( &p_tcb->sr_cmd, 0, sizeof(tGATT_SR_CMD));
}{ ... }
/* ... */
static BOOLEAN process_read_multi_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
tGATTS_RSP *p_msg, UINT16 mtu)
{
UINT16 ii, total_len, len;
UINT8 *p;
BOOLEAN is_overflow = FALSE;
GATT_TRACE_DEBUG ("process_read_multi_rsp status=%d mtu=%d", status, mtu);
if (p_cmd->multi_rsp_q == NULL) {
p_cmd->multi_rsp_q = fixed_queue_new(QUEUE_SIZE_MAX);
}{...}
BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(tGATTS_RSP));
if (p_buf == NULL) {
p_cmd->status = GATT_INSUF_RESOURCE;
return FALSE;
}{...}
memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
p_cmd->status = status;
if (status == GATT_SUCCESS) {
GATT_TRACE_DEBUG ("Multi read count=%d num_hdls=%d",
fixed_queue_length(p_cmd->multi_rsp_q),
p_cmd->multi_req.num_handles);
if (fixed_queue_length(p_cmd->multi_rsp_q) == p_cmd->multi_req.num_handles) {
len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
if ((p_buf = (BT_HDR *)osi_calloc(len)) == NULL) {
p_cmd->status = GATT_INSUF_RESOURCE;
return (TRUE);
}{...}
p_buf->offset = L2CAP_MIN_OFFSET;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
*p++ = GATT_RSP_READ_MULTI;
p_buf->len = 1;
list_t *list = NULL;
const list_node_t *node = NULL;
if (! fixed_queue_is_empty(p_cmd->multi_rsp_q)) {
list = fixed_queue_get_list(p_cmd->multi_rsp_q);
}{...}
for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
tGATTS_RSP *p_rsp = NULL;
if (list != NULL) {
if (ii == 0) {
node = list_begin(list);
}{...} else {
node = list_next(node);
}{...}
if (node != list_end(list)) {
p_rsp = (tGATTS_RSP *)list_node(node);
}{...}
}{...}
if (p_rsp != NULL) {
total_len = (p_buf->len + p_rsp->attr_value.len);
if (total_len > mtu) {
len = p_rsp->attr_value.len - (total_len - mtu);
is_overflow = TRUE;
GATT_TRACE_DEBUG ("multi read overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
}{...} else {
len = p_rsp->attr_value.len;
}{...}
if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
memcpy (p, p_rsp->attr_value.value, len);
if (!is_overflow) {
p += len;
}{...}
p_buf->len += len;
}{...} else {
p_cmd->status = GATT_NOT_FOUND;
break;
}{...}
if (is_overflow) {
break;
}{...}
}{...} else {
p_cmd->status = GATT_NOT_FOUND;
break;
}{...}
}{...}
if (p_buf->len == 0) {
GATT_TRACE_ERROR("process_read_multi_rsp - nothing found!!");
p_cmd->status = GATT_NOT_FOUND;
osi_free (p_buf);
GATT_TRACE_DEBUG(" osi_free (p_buf)");
}{...} else if (p_cmd->p_rsp_msg != NULL) {
osi_free (p_buf);
}{...} else {
p_cmd->p_rsp_msg = p_buf;
}{...}
return (TRUE);
}{...}
}{...} else {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
static BOOLEAN process_read_multi_var_rsp (tGATT_SR_CMD *p_cmd, tGATT_STATUS status,
tGATTS_RSP *p_msg, UINT16 mtu)
{
UINT16 ii;
UINT16 total_len;
UINT16 len;
UINT8 *p;
GATT_TRACE_DEBUG ("process_read_multi_var rsp status=%d mtu=%d", status, mtu);
if (p_cmd->multi_rsp_q == NULL) {
p_cmd->multi_rsp_q = fixed_queue_new(QUEUE_SIZE_MAX);
}{...}
BT_HDR *p_buf = (BT_HDR *)osi_malloc(sizeof(tGATTS_RSP));
if (p_buf == NULL) {
p_cmd->status = GATT_INSUF_RESOURCE;
return FALSE;
}{...}
memcpy((void *)p_buf, (const void *)p_msg, sizeof(tGATTS_RSP));
fixed_queue_enqueue(p_cmd->multi_rsp_q, p_buf, FIXED_QUEUE_MAX_TIMEOUT);
p_cmd->status = status;
if (status == GATT_SUCCESS) {
GATT_TRACE_DEBUG ("Multi var read count=%d num_hdls=%d",
fixed_queue_length(p_cmd->multi_rsp_q),
p_cmd->multi_req.num_handles);
if (fixed_queue_length(p_cmd->multi_rsp_q) == p_cmd->multi_req.num_handles) {
len = sizeof(BT_HDR) + L2CAP_MIN_OFFSET + mtu;
if ((p_buf = (BT_HDR *)osi_calloc(len)) == NULL) {
p_cmd->status = GATT_INSUF_RESOURCE;
return (TRUE);
}{...}
p_buf->offset = L2CAP_MIN_OFFSET;
p = (UINT8 *)(p_buf + 1) + p_buf->offset;
*p++ = GATT_RSP_READ_MULTI_VAR;
p_buf->len = 1;
list_t *list = NULL;
const list_node_t *node = NULL;
if (! fixed_queue_is_empty(p_cmd->multi_rsp_q)) {
list = fixed_queue_get_list(p_cmd->multi_rsp_q);
}{...}
for (ii = 0; ii < p_cmd->multi_req.num_handles; ii++) {
tGATTS_RSP *p_rsp = NULL;
if (list != NULL) {
if (ii == 0) {
node = list_begin(list);
}{...} else {
node = list_next(node);
}{...}
if (node != list_end(list)) {
p_rsp = (tGATTS_RSP *)list_node(node);
}{...}
}{...}
if (p_rsp != NULL) {
total_len = (p_buf->len + 2);
if (total_len > mtu) {
GATT_TRACE_DEBUG ("multi read variable overflow available len=%d val_len=%d", len, p_rsp->attr_value.len );
break;
}{...}
len = MIN(p_rsp->attr_value.len, (mtu - total_len));
if (p_rsp->attr_value.handle == p_cmd->multi_req.handles[ii]) {
GATT_TRACE_DEBUG("%s handle %x len %u", __func__, p_rsp->attr_value.handle, p_rsp->attr_value.len);
UINT16_TO_STREAM(p, p_rsp->attr_value.len);
memcpy (p, p_rsp->attr_value.value, len);
p += len;
p_buf->len += (2+len);
}{...} else {
p_cmd->status = GATT_NOT_FOUND;
break;
}{...}
}{...} else {
p_cmd->status = GATT_NOT_FOUND;
break;
}{...}
}{...}
if (p_buf->len == 0) {
GATT_TRACE_ERROR("%s - nothing found!!", __func__);
p_cmd->status = GATT_NOT_FOUND;
osi_free (p_buf);
}{...} else if (p_cmd->p_rsp_msg != NULL) {
osi_free (p_buf);
}{...} else {
p_cmd->p_rsp_msg = p_buf;
}{...}
return (TRUE);
}{...}
}{...} else {
return (TRUE);
}{...}
return (FALSE);
}{ ... }
/* ... */
tGATT_STATUS gatt_sr_process_app_rsp (tGATT_TCB *p_tcb, tGATT_IF gatt_if,
UINT32 trans_id, UINT8 op_code,
tGATT_STATUS status, tGATTS_RSP *p_msg)
{
tGATT_STATUS ret_code = GATT_SUCCESS;
UNUSED(trans_id);
GATT_TRACE_DEBUG("gatt_sr_process_app_rsp gatt_if=%d\n", gatt_if);
gatt_sr_update_cback_cnt(p_tcb, gatt_if, FALSE, FALSE);
if (op_code == GATT_REQ_READ_MULTI) {
if (!process_read_multi_rsp (&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size)) {
return (GATT_SUCCESS);
}{...}
}{...} else if (op_code == GATT_REQ_READ_MULTI_VAR) {
if (!process_read_multi_var_rsp(&p_tcb->sr_cmd, status, p_msg, p_tcb->payload_size)) {
return (GATT_SUCCESS);
}{...}
}{...} else {
if (op_code == GATT_REQ_PREPARE_WRITE && status == GATT_SUCCESS) {
gatt_sr_update_prep_cnt(p_tcb, gatt_if, TRUE, FALSE);
}{...}
if (op_code == GATT_REQ_EXEC_WRITE && status != GATT_SUCCESS) {
gatt_sr_reset_cback_cnt(p_tcb);
}{...}
p_tcb->sr_cmd.status = status;
if (gatt_sr_is_cback_cnt_zero(p_tcb)
&& status == GATT_SUCCESS) {
if (p_tcb->sr_cmd.p_rsp_msg == NULL) {
p_tcb->sr_cmd.p_rsp_msg = attp_build_sr_msg (p_tcb, (UINT8)(op_code + 1), (tGATT_SR_MSG *)p_msg);
}{...} else {
GATT_TRACE_ERROR("Exception!!! already has respond message\n");
}{...}
}{...}
}{...}
if (gatt_sr_is_cback_cnt_zero(p_tcb)) {
if ( (p_tcb->sr_cmd.status == GATT_SUCCESS) && (p_tcb->sr_cmd.p_rsp_msg) ) {
ret_code = attp_send_sr_msg (p_tcb, p_tcb->sr_cmd.p_rsp_msg);
p_tcb->sr_cmd.p_rsp_msg = NULL;
}{...} else {
if (p_tcb->sr_cmd.status == GATT_SUCCESS){
status = GATT_UNKNOWN_ERROR;
}{...}
ret_code = gatt_send_error_rsp (p_tcb, status, op_code, p_tcb->sr_cmd.handle, FALSE);
}{...}
gatt_dequeue_sr_cmd(p_tcb);
}{...}
GATT_TRACE_DEBUG("gatt_sr_process_app_rsp ret_code=%d\n", ret_code);
return ret_code;
}{ ... }
/* ... */
void gatt_process_exec_write_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
{
UINT8 *p = p_data, flag, i = 0;
UINT32 trans_id = 0;
tGATT_IF gatt_if;
UINT16 conn_id;
UINT16 queue_num = 0;
BOOLEAN is_first = TRUE;
BOOLEAN is_prepare_write_valid = FALSE;
BOOLEAN is_need_dequeue_sr_cmd = FALSE;
tGATT_PREPARE_WRITE_RECORD *prepare_record = NULL;
tGATT_PREPARE_WRITE_QUEUE_DATA * queue_data = NULL;
UNUSED(len);
#if GATT_CONFORMANCE_TESTING == TRUE
if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
GATT_TRACE_DEBUG("Conformance tst: forced err rspv for Execute Write: error status=%d",
gatt_cb.err_status);
gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, gatt_cb.handle, FALSE);
return;
}{...}
#endif/* ... */
STREAM_TO_UINT8(flag, p);
flag &= GATT_PREP_WRITE_EXEC;
prepare_record = &(p_tcb->prepare_write_record);
queue_num = fixed_queue_length(prepare_record->queue);
if (((prepare_record->error_code_app == GATT_SUCCESS) &&
(prepare_record->total_num == queue_num))
|| (flag == GATT_PREP_WRITE_CANCEL)){
tGATT_EXEC_WRITE_RSP gatt_exec_write_rsp;
gatt_exec_write_rsp.op_code = GATT_RSP_EXEC_WRITE;
gatt_send_packet(p_tcb, (UINT8 *)(&gatt_exec_write_rsp), sizeof(gatt_exec_write_rsp));
gatt_dequeue_sr_cmd(p_tcb);
if (flag != GATT_PREP_WRITE_CANCEL){
is_prepare_write_valid = TRUE;
}{...}
GATT_TRACE_DEBUG("Send execute_write_rsp\n");
}{...} else if ((prepare_record->error_code_app == GATT_SUCCESS) &&
(prepare_record->total_num > queue_num)){
is_prepare_write_valid = TRUE;
}{...} else if(prepare_record->total_num < queue_num) {
GATT_TRACE_ERROR("Error in %s, line=%d, prepare write total number (%d) \
should not smaller than prepare queue number (%d)\n", \
__func__, __LINE__,prepare_record->total_num, queue_num);
}{...} else if (prepare_record->error_code_app != GATT_SUCCESS){
GATT_TRACE_DEBUG("Send error code for execute_write, code=0x%x\n", prepare_record->error_code_app);
is_need_dequeue_sr_cmd = (prepare_record->total_num == queue_num) ? TRUE : FALSE;
gatt_send_error_rsp(p_tcb, prepare_record->error_code_app, GATT_REQ_EXEC_WRITE, 0, is_need_dequeue_sr_cmd);
}{...}
while(fixed_queue_try_peek_first(prepare_record->queue)) {
queue_data = fixed_queue_dequeue(prepare_record->queue, FIXED_QUEUE_MAX_TIMEOUT);
if (is_prepare_write_valid){
if((queue_data->p_attr->p_value != NULL) && (queue_data->p_attr->p_value->attr_val.attr_val != NULL)){
if(is_first) {
queue_data->p_attr->p_value->attr_val.attr_len = 0;
is_first = FALSE;
}{...}
memcpy(queue_data->p_attr->p_value->attr_val.attr_val+queue_data->offset, queue_data->value, queue_data->len);
queue_data->p_attr->p_value->attr_val.attr_len += queue_data->len;
}{...}
}{...}
osi_free(queue_data);
}{...}
fixed_queue_free(prepare_record->queue, NULL);
prepare_record->queue = NULL;
/* ... */
if (!gatt_sr_is_prep_cnt_zero(p_tcb)) {
if (prepare_record->total_num > queue_num){
trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, 0);
gatt_sr_copy_prep_cnt_to_cback_cnt(p_tcb);
}{...}
for (i = 0; i < GATT_MAX_APPS; i++) {
if (p_tcb->prep_cnt[i]) {
gatt_if = (tGATT_IF) (i + 1);
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
gatt_sr_send_req_callback(conn_id,
trans_id,
GATTS_REQ_TYPE_WRITE_EXEC,
(tGATTS_DATA *)&flag);
p_tcb->prep_cnt[i] = 0;
}{...}
}{...}
}{...}
prepare_record->total_num = 0;
prepare_record->error_code_app = GATT_SUCCESS;
}{ ... }
/* ... */
void gatt_process_read_multi_req (tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
{
UINT32 trans_id;
UINT16 handle = 0, ll = len;
UINT8 *p = p_data, i_rcb;
tGATT_STATUS err = GATT_SUCCESS;
UINT8 sec_flag, key_size;
tGATTS_RSP *p_msg;
GATT_TRACE_DEBUG("gatt_process_read_multi_req" );
p_tcb->sr_cmd.multi_req.num_handles = 0;
gatt_sr_get_sec_info(p_tcb->peer_bda,
p_tcb->transport,
&sec_flag,
&key_size);
#if GATT_CONFORMANCE_TESTING == TRUE
if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
GATT_TRACE_DEBUG("Conformance tst: forced err rspvofr ReadMultiple: error status=%d\n", gatt_cb.err_status);
STREAM_TO_UINT16(handle, p);
gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
return;
}{...}
#endif/* ... */
while (ll >= 2 && p_tcb->sr_cmd.multi_req.num_handles < GATT_MAX_READ_MULTI_HANDLES) {
STREAM_TO_UINT16(handle, p);
if ((i_rcb = gatt_sr_find_i_rcb_by_handle(handle)) < GATT_MAX_SR_PROFILES) {
p_tcb->sr_cmd.multi_req.handles[p_tcb->sr_cmd.multi_req.num_handles++] = handle;
if ((err = gatts_read_attr_perm_check( gatt_cb.sr_reg[i_rcb].p_db,
FALSE,
handle,
sec_flag,
key_size))
!= GATT_SUCCESS) {
GATT_TRACE_ERROR("read permission denied : 0x%02x", err);
break;
}{...}
}{...} else {
err = GATT_INVALID_HANDLE;
break;
}{...}
ll -= 2;
}{...}
if (err == GATT_SUCCESS) {
if (ll != 0) {
GATT_TRACE_ERROR("max attribute handle reached in ReadMultiple Request.");
err = GATT_INVALID_HANDLE;
}{...}
if (p_tcb->sr_cmd.multi_req.num_handles == 0) {
err = GATT_INVALID_HANDLE;
}{...}
}{...}
if (err == GATT_SUCCESS) {
if ((trans_id = gatt_sr_enqueue_cmd (p_tcb, op_code, p_tcb->sr_cmd.multi_req.handles[0])) != 0) {
gatt_sr_reset_cback_cnt(p_tcb);
for (ll = 0; ll < p_tcb->sr_cmd.multi_req.num_handles; ll ++) {
if ((p_msg = (tGATTS_RSP *)osi_malloc(sizeof(tGATTS_RSP))) != NULL) {
memset(p_msg, 0, sizeof(tGATTS_RSP))
;
handle = p_tcb->sr_cmd.multi_req.handles[ll];
i_rcb = gatt_sr_find_i_rcb_by_handle(handle);
p_msg->attr_value.handle = handle;
err = gatts_read_attr_value_by_handle(p_tcb,
gatt_cb.sr_reg[i_rcb].p_db,
op_code,
handle,
0,
p_msg->attr_value.value,
&p_msg->attr_value.len,
GATT_MAX_ATTR_LEN,
sec_flag,
key_size,
trans_id);
if (err == GATT_SUCCESS || err == GATT_STACK_RSP) {
gatt_sr_process_app_rsp(p_tcb, gatt_cb.sr_reg[i_rcb].gatt_if , trans_id, op_code, GATT_SUCCESS, p_msg);
}{...}
osi_free(p_msg);
}{...} else {
err = GATT_NO_RESOURCES;
gatt_dequeue_sr_cmd(p_tcb);
break;
}{...}
}{...}
}{...} else {
err = GATT_NO_RESOURCES;
}{...}
}{...}
if (err != GATT_SUCCESS && err != GATT_STACK_RSP && err != GATT_PENDING && err != GATT_BUSY) {
gatt_send_error_rsp(p_tcb, err, op_code, handle, FALSE);
}{...}
}{ ... }
/* ... */
static tGATT_STATUS gatt_build_primary_service_rsp (BT_HDR *p_msg, tGATT_TCB *p_tcb,
UINT8 op_code, UINT16 s_hdl,
UINT16 e_hdl, UINT8 *p_data, tBT_UUID value)
{
tGATT_STATUS status = GATT_NOT_FOUND;
UINT8 handle_len = 4, *p ;
tGATT_SR_REG *p_rcb;
tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
tGATT_SRV_LIST_ELEM *p_srv = NULL;
tBT_UUID *p_uuid;
UNUSED(p_data);
p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
p_srv = p_list->p_first;
while (p_srv) {
p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
if (p_rcb->in_use &&
p_rcb->s_hdl >= s_hdl &&
p_rcb->s_hdl <= e_hdl &&
p_rcb->type == GATT_UUID_PRI_SERVICE) {
if ((p_uuid = gatts_get_service_uuid (p_rcb->p_db)) != NULL) {
if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
handle_len = 4 + p_uuid->len;
}{...}
if (p_msg->offset == 0) {
*p ++ = op_code + 1;
p_msg->len ++;
p_msg->offset = handle_len;
if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
*p ++ = (UINT8)p_msg->offset;
p_msg->len ++;
}{...}
}{...}
if (p_msg->len + p_msg->offset <= p_tcb->payload_size &&
handle_len == p_msg->offset) {
if (op_code != GATT_REQ_FIND_TYPE_VALUE ||
gatt_uuid_compare(value, *p_uuid)) {
UINT16_TO_STREAM(p, p_rcb->s_hdl);
if (p_list->p_last_primary == p_srv &&
p_list->p_last_primary == p_list->p_last) {
GATT_TRACE_DEBUG("Use 0xFFFF for the last primary attribute");
UINT16_TO_STREAM(p, 0xFFFF);
}{...} else {
UINT16_TO_STREAM(p, p_rcb->e_hdl);
}{...}
if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
gatt_build_uuid_to_stream(&p, *p_uuid);
}{...}
status = GATT_SUCCESS;
p_msg->len += p_msg->offset;
}{...}
}{...} else {
break;
}{...}
}{...}
}{...}
p_srv = p_srv->p_next;
}{...}
p_msg->offset = L2CAP_MIN_OFFSET;
return status;
}{ ... }
/* ... */
static tGATT_STATUS gatt_build_find_info_rsp(tGATT_SR_REG *p_rcb, BT_HDR *p_msg, UINT16 *p_len,
UINT16 s_hdl, UINT16 e_hdl)
{
tGATT_STATUS status = GATT_NOT_FOUND;
UINT8 *p;
UINT16 len = *p_len;
tGATT_ATTR16 *p_attr = NULL;
UINT8 info_pair_len[2] = {4, 18};
if (!p_rcb->p_db || !p_rcb->p_db->p_attr_list) {
return status;
}{...}
p_attr = (tGATT_ATTR16 *) p_rcb->p_db->p_attr_list;
p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET + p_msg->len;
while (p_attr) {
if (p_attr->handle > e_hdl) {
break;
}{...}
if (p_attr->handle >= s_hdl) {
if (p_msg->offset == 0) {
p_msg->offset = (p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) ? GATT_INFO_TYPE_PAIR_16 : GATT_INFO_TYPE_PAIR_128;
}{...}
if (len >= info_pair_len[p_msg->offset - 1]) {
if (p_msg->offset == GATT_INFO_TYPE_PAIR_16 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_16) {
UINT16_TO_STREAM(p, p_attr->handle);
UINT16_TO_STREAM(p, p_attr->uuid);
}{...} else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_128 ) {
UINT16_TO_STREAM(p, p_attr->handle);
ARRAY_TO_STREAM (p, ((tGATT_ATTR128 *) p_attr)->uuid, LEN_UUID_128);
}{...} else if (p_msg->offset == GATT_INFO_TYPE_PAIR_128 && p_attr->uuid_type == GATT_ATTR_UUID_TYPE_32) {
UINT16_TO_STREAM(p, p_attr->handle);
gatt_convert_uuid32_to_uuid128(p, ((tGATT_ATTR32 *) p_attr)->uuid);
p += LEN_UUID_128;
}{...} else {
GATT_TRACE_ERROR("format mismatch");
status = GATT_NO_RESOURCES;
break;
}{...}
p_msg->len += info_pair_len[p_msg->offset - 1];
len -= info_pair_len[p_msg->offset - 1];
status = GATT_SUCCESS;
}{...} else {
status = GATT_NO_RESOURCES;
break;
}{...}
}{...}
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
}{...}
*p_len = len;
return status;
}{ ... }
/* ... */
static tGATT_STATUS gatts_validate_packet_format(UINT8 op_code, UINT16 *p_len,
UINT8 **p_data, tBT_UUID *p_uuid_filter,
UINT16 *p_s_hdl, UINT16 *p_e_hdl)
{
tGATT_STATUS reason = GATT_SUCCESS;
UINT16 uuid_len, s_hdl = 0, e_hdl = 0;
UINT16 len = *p_len;
UINT8 *p = *p_data;
if (len >= 4) {
STREAM_TO_UINT16(s_hdl, p);
STREAM_TO_UINT16(e_hdl, p);
len -= 4;
if (s_hdl > e_hdl || !GATT_HANDLE_IS_VALID(s_hdl) || !GATT_HANDLE_IS_VALID(e_hdl)) {
reason = GATT_INVALID_HANDLE;
}{...}
else if (op_code == GATT_REQ_READ_BY_GRP_TYPE ||
op_code == GATT_REQ_FIND_TYPE_VALUE ||
op_code == GATT_REQ_READ_BY_TYPE) {
if (len >= 2 && p_uuid_filter != NULL) {
uuid_len = (op_code == GATT_REQ_FIND_TYPE_VALUE) ? 2 : len;
if (gatt_parse_uuid_from_cmd (p_uuid_filter, uuid_len, &p) == FALSE ||
p_uuid_filter->len == 0) {
GATT_TRACE_DEBUG("UUID filter does not exsit");
reason = GATT_INVALID_PDU;
}{...} else {
len -= p_uuid_filter->len;
}{...}
}{...} else {
reason = GATT_INVALID_PDU;
}{...}
}{...}
}{...} else {
reason = GATT_INVALID_PDU;
}{...}
*p_data = p;
*p_len = len;
*p_s_hdl = s_hdl;
*p_e_hdl = e_hdl;
return reason;
}{ ... }
/* ... */
void gatts_process_primary_service_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
{
UINT8 reason = GATT_INVALID_PDU;
UINT16 s_hdl = 0, e_hdl = 0;
tBT_UUID uuid, value, primary_service = {LEN_UUID_16, {GATT_UUID_PRI_SERVICE}};
BT_HDR *p_msg = NULL;
UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
memset (&value, 0, sizeof(tBT_UUID));
reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
if (reason == GATT_SUCCESS) {
if (gatt_uuid_compare(uuid, primary_service)) {
if (op_code == GATT_REQ_FIND_TYPE_VALUE) {
if (gatt_parse_uuid_from_cmd(&value, len, &p_data) == FALSE) {
reason = GATT_INVALID_PDU;
}{...}
}{...}
if (reason == GATT_SUCCESS) {
if ((p_msg = (BT_HDR *)osi_calloc(msg_len)) == NULL) {
GATT_TRACE_ERROR("gatts_process_primary_service_req failed. no resources.");
reason = GATT_NO_RESOURCES;
}{...} else {
reason = gatt_build_primary_service_rsp (p_msg, p_tcb, op_code, s_hdl, e_hdl, p_data, value);
}{...}
}{...}
}{...} else {
if (op_code == GATT_REQ_READ_BY_GRP_TYPE) {
reason = GATT_UNSUPPORT_GRP_TYPE;
GATT_TRACE_DEBUG("unexpected ReadByGrpType Group: 0x%04x", uuid.uu.uuid16);
}{...} else {
reason = GATT_NOT_FOUND;
GATT_TRACE_DEBUG("unexpected ReadByTypeValue type: 0x%04x", uuid.uu.uuid16);
}{...}
}{...}
}{...}
if (reason != GATT_SUCCESS) {
if (p_msg) {
osi_free(p_msg);
}{...}
gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
}{...} else {
attp_send_sr_msg(p_tcb, p_msg);
}{...}
}{ ... }
/* ... */
static void gatts_process_find_info(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
{
UINT8 reason = GATT_INVALID_PDU, *p;
UINT16 s_hdl = 0, e_hdl = 0, buf_len;
BT_HDR *p_msg = NULL;
tGATT_SR_REG *p_rcb;
tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
tGATT_SRV_LIST_ELEM *p_srv = NULL;
reason = gatts_validate_packet_format(op_code, &len, &p_data, NULL, &s_hdl, &e_hdl);
if (reason == GATT_SUCCESS) {
buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
if ((p_msg = (BT_HDR *)osi_calloc(buf_len)) == NULL) {
reason = GATT_NO_RESOURCES;
}{...} else {
reason = GATT_NOT_FOUND;
p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
*p ++ = op_code + 1;
p_msg->len = 2;
buf_len = p_tcb->payload_size - 2;
p_srv = p_list->p_first;
while (p_srv) {
p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
if (p_rcb->in_use &&
!(p_rcb->s_hdl > e_hdl ||
p_rcb->e_hdl < s_hdl)) {
reason = gatt_build_find_info_rsp(p_rcb, p_msg, &buf_len, s_hdl, e_hdl);
if (reason == GATT_NO_RESOURCES) {
reason = GATT_SUCCESS;
break;
}{...}
}{...}
p_srv = p_srv->p_next;
}{...}
*p = (UINT8)p_msg->offset;
p_msg->offset = L2CAP_MIN_OFFSET;
}{...}
}{...}
if (reason != GATT_SUCCESS) {
if (p_msg) {
osi_free(p_msg);
}{...}
gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
}{...} else {
attp_send_sr_msg(p_tcb, p_msg);
}{...}
}{ ... }
/* ... */
static void gatts_process_mtu_req (tGATT_TCB *p_tcb, UINT16 len, UINT8 *p_data)
{
UINT16 mtu = 0;
UINT8 *p = p_data, i;
BT_HDR *p_buf;
UINT16 conn_id;
if (p_tcb->att_lcid != L2CAP_ATT_CID) {
gatt_send_error_rsp (p_tcb, GATT_REQ_NOT_SUPPORTED, GATT_REQ_MTU, 0, FALSE);
}{...} else if (len < GATT_MTU_REQ_MIN_LEN) {
GATT_TRACE_ERROR("invalid MTU request PDU received.\n");
gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, GATT_REQ_MTU, 0, FALSE);
}{...} else {
STREAM_TO_UINT16 (mtu, p);
if (mtu < GATT_DEF_BLE_MTU_SIZE) {
p_tcb->payload_size = GATT_DEF_BLE_MTU_SIZE;
}{...} else if (mtu > gatt_default.local_mtu) {
p_tcb->payload_size = gatt_default.local_mtu;
}{...} else {
p_tcb->payload_size = mtu;
}{...}
/* ... */
if ((p_buf = attp_build_sr_msg(p_tcb, GATT_RSP_MTU, (tGATT_SR_MSG *) &p_tcb->payload_size)) != NULL) {
attp_send_sr_msg (p_tcb, p_buf);
for (i = 0; i < GATT_MAX_APPS; i ++) {
if (gatt_cb.cl_rcb[i].in_use ) {
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_cb.cl_rcb[i].gatt_if);
gatt_sr_send_req_callback(conn_id, 0, GATTS_REQ_TYPE_MTU,
(tGATTS_DATA *)&p_tcb->payload_size);
}{...}
}{...}
}{...}
}{...}
}{ ... }
/* ... */
void gatts_process_read_by_type_req(tGATT_TCB *p_tcb, UINT8 op_code, UINT16 len, UINT8 *p_data)
{
tBT_UUID uuid;
tGATT_SR_REG *p_rcb;
UINT16 msg_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET),
buf_len,
s_hdl, e_hdl, err_hdl = 0;
BT_HDR *p_msg = NULL;
tGATT_STATUS reason, ret;
UINT8 *p;
UINT8 sec_flag, key_size;
tGATT_SRV_LIST_INFO *p_list = &gatt_cb.srv_list_info;
tGATT_SRV_LIST_ELEM *p_srv = NULL;
reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
GATT_TRACE_DEBUG("%s, op_code =%x, len = %x\n", __func__, op_code, len);
#if GATT_CONFORMANCE_TESTING == TRUE
if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
GATT_TRACE_DEBUG("Conformance tst: forced err rsp for ReadByType: error status=%d\n", gatt_cb.err_status);
gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, s_hdl, FALSE);
return;
}{...}
#endif/* ... */
if (reason == GATT_SUCCESS) {
if ((p_msg = (BT_HDR *)osi_calloc(msg_len)) == NULL) {
GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.\n");
reason = GATT_NO_RESOURCES;
}{...} else {
p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
*p ++ = op_code + 1;
p_msg->len = 2;
buf_len = p_tcb->payload_size - 2;
reason = GATT_NOT_FOUND;
p_srv = p_list->p_first;
while (p_srv) {
p_rcb = GATT_GET_SR_REG_PTR(p_srv->i_sreg);
if (p_rcb->in_use &&
!(p_rcb->s_hdl > e_hdl ||
p_rcb->e_hdl < s_hdl)) {
gatt_sr_get_sec_info(p_tcb->peer_bda,
p_tcb->transport,
&sec_flag,
&key_size);
ret = gatts_db_read_attr_value_by_type(p_tcb,
p_rcb->p_db,
op_code,
p_msg,
s_hdl,
e_hdl,
uuid,
&buf_len,
sec_flag,
key_size,
0,
&err_hdl);
if (ret != GATT_NOT_FOUND) {
reason = ret;
if (ret == GATT_NO_RESOURCES) {
reason = GATT_SUCCESS;
}{...}
}{...}
if (ret != GATT_SUCCESS && ret != GATT_NOT_FOUND) {
s_hdl = err_hdl;
break;
}{...}
}{...}
p_srv = p_srv->p_next;
}{...}
*p = (UINT8)p_msg->offset;
p_msg->offset = L2CAP_MIN_OFFSET;
}{...}
}{...}
if (reason != GATT_SUCCESS && reason != GATT_STACK_RSP) {
if (p_msg) {
osi_free(p_msg);
}{...}
if (reason != GATT_PENDING && reason != GATT_BUSY) {
gatt_send_error_rsp (p_tcb, reason, op_code, s_hdl, FALSE);
}{...}
}{...} else {
attp_send_sr_msg(p_tcb, p_msg);
gatt_dequeue_sr_cmd(p_tcb);
}{...}
}{ ... }
/* ... */
void gatts_process_write_req (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
UINT8 op_code, UINT16 len, UINT8 *p_data)
{
tGATTS_DATA sr_data;
UINT32 trans_id;
tGATT_STATUS status;
UINT8 sec_flag, key_size, *p = p_data;
tGATT_SR_REG *p_sreg;
UINT16 conn_id, offset = 0;
memset(&sr_data, 0, sizeof(tGATTS_DATA));
sr_data.write_req.need_rsp = FALSE;
switch (op_code) {
case GATT_SIGN_CMD_WRITE:
if (op_code == GATT_SIGN_CMD_WRITE) {
GATT_TRACE_DEBUG("Write CMD with data signing" );
len -= GATT_AUTH_SIGN_LEN;
}{...}
...
case GATT_CMD_WRITE:
case GATT_REQ_WRITE:
sr_data.write_req.handle = handle;
sr_data.write_req.len = len;
if (len != 0 && p != NULL) {
memcpy (sr_data.write_req.value, p, len);
}{...}
break;...
}{...}
gatt_sr_get_sec_info(p_tcb->peer_bda,
p_tcb->transport,
&sec_flag,
&key_size);
status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
op_code,
handle,
sr_data.write_req.offset,
p,
len,
sec_flag,
key_size);
if (status == GATT_SUCCESS) {
if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0) {
p_sreg = &gatt_cb.sr_reg[i_rcb];
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
status = gatts_write_attr_value_by_handle(gatt_cb.sr_reg[i_rcb].p_db,
handle, offset, p, len);
if((op_code == GATT_REQ_WRITE) && (status == GATT_APP_RSP)){
sr_data.write_req.need_rsp = TRUE;
status = GATT_PENDING;
}{...}
gatt_sr_send_req_callback(conn_id,
trans_id,
GATTS_REQ_TYPE_WRITE,
&sr_data);
}{...} else {
GATT_TRACE_ERROR("Error in %s, line=%d, max pending command, send error\n", __func__, __LINE__);
status = GATT_BUSY;
}{...}
}{...}
if ((op_code == GATT_REQ_WRITE) && (sr_data.write_req.need_rsp == FALSE)){
if (status == GATT_SUCCESS){
tGATT_WRITE_REQ_RSP gatt_write_req_rsp;
gatt_write_req_rsp.op_code = GATT_RSP_WRITE;
gatt_send_packet(p_tcb, (UINT8 *)(&gatt_write_req_rsp), sizeof(gatt_write_req_rsp));
gatt_dequeue_sr_cmd(p_tcb);
}{...} else if (status != GATT_PENDING){
gatt_send_error_rsp (p_tcb, status, op_code, handle, TRUE);
}{...}
}{...}
return;
}{ ... }
/* ... */
void gatt_attr_process_prepare_write (tGATT_TCB *p_tcb, UINT8 i_rcb, UINT16 handle,
UINT8 op_code, UINT16 len, UINT8 *p_data)
{
tGATT_STATUS status;
tGATT_PREPARE_WRITE_QUEUE_DATA * queue_data = NULL;
tGATT_ATTR16 *p_attr;
tGATT_ATTR16 *p_attr_temp;
tGATTS_DATA sr_data;
UINT32 trans_id = 0;
UINT8 sec_flag, key_size, *p = p_data;
tGATT_SR_REG *p_sreg;
UINT16 conn_id, offset = 0;
tGATT_SVC_DB *p_db;
BOOLEAN is_need_prepare_write_rsp = FALSE;
BOOLEAN is_need_queue_data = FALSE;
tGATT_PREPARE_WRITE_RECORD *prepare_record = NULL;
memset(&sr_data, 0, sizeof(tGATTS_DATA));
if (len < 2) {
GATT_TRACE_ERROR("%s: Prepare write request was invalid - missing offset, sending error response", __func__);
gatt_send_error_rsp(p_tcb, GATT_INVALID_PDU, op_code, handle, FALSE);
return;
}{...}
STREAM_TO_UINT16(offset, p);
len -= 2;
p_sreg = &gatt_cb.sr_reg[i_rcb];
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_sreg->gatt_if);
prepare_record = &(p_tcb->prepare_write_record);
gatt_sr_get_sec_info(p_tcb->peer_bda,
p_tcb->transport,
&sec_flag,
&key_size);
status = gatts_write_attr_perm_check (gatt_cb.sr_reg[i_rcb].p_db,
op_code,
handle,
sr_data.write_req.offset,
p,
len,
sec_flag,
key_size);
if (status == GATT_SUCCESS){
if ((trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle)) != 0) {
p_db = gatt_cb.sr_reg[i_rcb].p_db;
if (p_db && p_db->p_attr_list) {
p_attr = (tGATT_ATTR16 *)p_db->p_attr_list;
while (p_attr && handle >= p_attr->handle) {
if (p_attr->handle == handle ) {
p_attr_temp = p_attr;
if (p_attr->control.auto_rsp == GATT_RSP_BY_APP) {
status = GATT_APP_RSP;
}{...} else if (p_attr->p_value != NULL &&
offset > p_attr->p_value->attr_val.attr_max_len) {
status = GATT_INVALID_OFFSET;
is_need_prepare_write_rsp = TRUE;
is_need_queue_data = TRUE;
}{...} else if (p_attr->p_value != NULL &&
((offset + len) > p_attr->p_value->attr_val.attr_max_len)){
status = GATT_INVALID_ATTR_LEN;
is_need_prepare_write_rsp = TRUE;
is_need_queue_data = TRUE;
}{...} else if (p_attr->p_value == NULL) {
GATT_TRACE_ERROR("Error in %s, attribute of handle 0x%x not allocate value buffer\n",
__func__, handle);
status = GATT_UNKNOWN_ERROR;
}{...} else {
is_need_prepare_write_rsp = TRUE;
is_need_queue_data = TRUE;
}{...}
}{...}
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
}{...}
}{...}
}{...} else{
status = GATT_UNKNOWN_ERROR;
GATT_TRACE_ERROR("Error in %s, Line %d: GATT BUSY\n", __func__, __LINE__);
}{...}
}{...}
if (is_need_queue_data){
queue_data = (tGATT_PREPARE_WRITE_QUEUE_DATA *)osi_malloc(len + sizeof(tGATT_PREPARE_WRITE_QUEUE_DATA));
if (queue_data == NULL){
status = GATT_PREPARE_Q_FULL;
}{...} else {
queue_data->p_attr = p_attr_temp;
queue_data->len = len;
queue_data->handle = handle;
queue_data->offset = offset;
memcpy(queue_data->value, p, len);
if (prepare_record->queue == NULL) {
prepare_record->queue = fixed_queue_new(QUEUE_SIZE_MAX);
}{...}
fixed_queue_enqueue(prepare_record->queue, queue_data, FIXED_QUEUE_MAX_TIMEOUT);
}{...}
}{...}
if (is_need_prepare_write_rsp){
if (queue_data != NULL){
queue_data->op_code = op_code + 1;
tGATT_STATUS rsp_send_status = gatt_send_packet(p_tcb, &(queue_data->op_code), queue_data->len + 5);
gatt_sr_update_prep_cnt(p_tcb, p_sreg->gatt_if, TRUE, FALSE);
gatt_dequeue_sr_cmd(p_tcb);
if (rsp_send_status != GATT_SUCCESS){
GATT_TRACE_ERROR("Error in %s, line=%d, fail to send prepare_write_rsp, status=0x%x\n",
__func__, __LINE__, rsp_send_status);
}{...}
}{...} else{
GATT_TRACE_ERROR("Error in %s, line=%d, queue_data should not be NULL here, fail to send prepare_write_rsp\n",
__func__, __LINE__);
}{...}
}{...}
if ((status == GATT_APP_RSP) || (is_need_prepare_write_rsp)){
prepare_record->total_num++;
memset(&sr_data, 0, sizeof(sr_data));
sr_data.write_req.is_prep = TRUE;
sr_data.write_req.handle = handle;
sr_data.write_req.offset = offset;
sr_data.write_req.len = len;
sr_data.write_req.need_rsp = (status == GATT_APP_RSP) ? TRUE : FALSE;
memcpy(sr_data.write_req.value, p, len);
gatt_sr_send_req_callback(conn_id, trans_id, GATTS_REQ_TYPE_WRITE, &sr_data);
}{...} else{
gatt_send_error_rsp(p_tcb, status, GATT_REQ_PREPARE_WRITE, handle, TRUE);
}{...}
if ((prepare_record->error_code_app == GATT_SUCCESS)
&& (status == GATT_INVALID_OFFSET || status == GATT_INVALID_ATTR_LEN || status == GATT_REQ_NOT_SUPPORTED)) {
prepare_record->error_code_app = status;
}{...}
}{ ... }
/* ... */
static void gatts_process_read_req(tGATT_TCB *p_tcb, tGATT_SR_REG *p_rcb, UINT8 op_code,
UINT16 handle, UINT16 len, UINT8 *p_data)
{
UINT16 buf_len = (UINT16)(sizeof(BT_HDR) + p_tcb->payload_size + L2CAP_MIN_OFFSET);
tGATT_STATUS reason;
BT_HDR *p_msg = NULL;
UINT8 sec_flag, key_size, *p;
UINT16 offset = 0, value_len = 0;
UNUSED (len);
if ((p_msg = (BT_HDR *)osi_calloc(buf_len)) == NULL) {
GATT_TRACE_ERROR("gatts_process_find_info failed. no resources.\n");
reason = GATT_NO_RESOURCES;
}{...} else {
if (op_code == GATT_REQ_READ_BLOB) {
STREAM_TO_UINT16(offset, p_data);
}{...}
p = (UINT8 *)(p_msg + 1) + L2CAP_MIN_OFFSET;
*p ++ = op_code + 1;
p_msg->len = 1;
buf_len = p_tcb->payload_size - 1;
gatt_sr_get_sec_info(p_tcb->peer_bda,
p_tcb->transport,
&sec_flag,
&key_size);
reason = gatts_read_attr_value_by_handle(p_tcb,
p_rcb->p_db,
op_code,
handle,
offset,
p,
&value_len,
buf_len,
sec_flag,
key_size,
0);
p_msg->len += value_len;
}{...}
if (reason != GATT_SUCCESS && reason != GATT_PENDING && reason != GATT_STACK_RSP) {
if (p_msg) {
osi_free(p_msg);
}{...}
if (reason != GATT_BUSY) {
gatt_send_error_rsp (p_tcb, reason, op_code, handle, FALSE);
gatt_dequeue_sr_cmd(p_tcb);
}{...}
}{...} else if (reason == GATT_SUCCESS || reason == GATT_STACK_RSP) {
attp_send_sr_msg(p_tcb, p_msg);
gatt_dequeue_sr_cmd(p_tcb);
}{...} else {
if (p_msg) {
osi_free(p_msg);
}{...}
}{...}
}{ ... }
/* ... */
void gatts_process_attribute_req (tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
UINT16 handle = 0;
UINT8 *p = p_data, i;
tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
tGATT_STATUS status = GATT_INVALID_HANDLE;
tGATT_ATTR16 *p_attr;
if (len < 2) {
GATT_TRACE_ERROR("Illegal PDU length, discard request\n");
status = GATT_INVALID_PDU;
}{...} else {
STREAM_TO_UINT16(handle, p);
len -= 2;
}{...}
#if GATT_CONFORMANCE_TESTING == TRUE
gatt_cb.handle = handle;
if (gatt_cb.enable_err_rsp && gatt_cb.req_op_code == op_code) {
GATT_TRACE_DEBUG("Conformance tst: forced err rsp: error status=%d\n", gatt_cb.err_status);
gatt_send_error_rsp (p_tcb, gatt_cb.err_status, gatt_cb.req_op_code, handle, FALSE);
return;
}{...}
#endif/* ... */
if (GATT_HANDLE_IS_VALID(handle)) {
for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++) {
if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle) {
p_attr = (tGATT_ATTR16 *)p_rcb->p_db->p_attr_list;
while (p_attr) {
if (p_attr->handle == handle) {
switch (op_code) {
case GATT_REQ_READ:
case GATT_REQ_READ_BLOB:
gatts_process_read_req(p_tcb, p_rcb, op_code, handle, len, p);
break;
...
case GATT_REQ_WRITE:
case GATT_CMD_WRITE:
case GATT_SIGN_CMD_WRITE:
gatts_process_write_req(p_tcb, i, handle, op_code, len, p);
break;
...
case GATT_REQ_PREPARE_WRITE:
gatt_attr_process_prepare_write (p_tcb, i, handle, op_code, len, p);...
default:
break;...
}{...}
status = GATT_SUCCESS;
break;
}{...}
p_attr = (tGATT_ATTR16 *)p_attr->p_next;
}{...}
break;
}{...}
}{...}
}{...}
if (status != GATT_SUCCESS && op_code != GATT_CMD_WRITE && op_code != GATT_SIGN_CMD_WRITE) {
gatt_send_error_rsp (p_tcb, status, op_code, handle, FALSE);
}{...}
}{ ... }
/* ... */
static void gatts_proc_srv_chg_ind_ack(tGATT_TCB *p_tcb )
{
tGATTS_SRV_CHG_REQ req;
tGATTS_SRV_CHG *p_buf = NULL;
GATT_TRACE_DEBUG("gatts_proc_srv_chg_ind_ack");
if ((p_buf = gatt_is_bda_in_the_srv_chg_clt_list(p_tcb->peer_bda)) != NULL) {
GATT_TRACE_DEBUG("NV update set srv chg = FALSE");
p_buf->srv_changed = FALSE;
memcpy(&req.srv_chg, p_buf, sizeof(tGATTS_SRV_CHG));
if (gatt_cb.cb_info.p_srv_chg_callback) {
(*gatt_cb.cb_info.p_srv_chg_callback)(GATTS_SRV_CHG_CMD_UPDATE_CLIENT, &req, NULL);
}{...}
}{...}
}{ ... }
/* ... */
static void gatts_chk_pending_ind(tGATT_TCB *p_tcb )
{
#if (GATTS_INCLUDED == TRUE)
tGATT_VALUE *p_buf = (tGATT_VALUE *)fixed_queue_try_peek_first(p_tcb->pending_ind_q);
GATT_TRACE_DEBUG("gatts_chk_pending_ind");
if (p_buf ) {
GATTS_HandleValueIndication (p_buf->conn_id,
p_buf->handle,
p_buf->len,
p_buf->value);
osi_free(fixed_queue_try_remove_from_queue(p_tcb->pending_ind_q,
p_buf));
}{...}
/* ... */#endif
}{ ... }
/* ... */
static BOOLEAN gatts_proc_ind_ack(tGATT_TCB *p_tcb, UINT16 ack_handle)
{
BOOLEAN continue_processing = TRUE;
GATT_TRACE_DEBUG ("gatts_proc_ind_ack ack handle=%d", ack_handle);
if (ack_handle == gatt_cb.handle_of_h_r) {
gatts_proc_srv_chg_ind_ack(p_tcb);
continue_processing = FALSE;
#if GATTS_ROBUST_CACHING_ENABLED
gatt_sr_update_cl_status(p_tcb, true);/* ... */
#endif
}{...}
gatts_chk_pending_ind(p_tcb);
return continue_processing;
}{ ... }
/* ... */
void gatts_process_value_conf(tGATT_TCB *p_tcb, UINT8 op_code)
{
UINT16 handle = p_tcb->indicate_handle;
UINT32 trans_id;
UINT8 i;
tGATT_SR_REG *p_rcb = gatt_cb.sr_reg;
BOOLEAN continue_processing;
UINT16 conn_id;
btu_stop_timer (&p_tcb->conf_timer_ent);
if (GATT_HANDLE_IS_VALID(handle)) {
p_tcb->indicate_handle = 0;
continue_processing = gatts_proc_ind_ack(p_tcb, handle);
if (continue_processing) {
for (i = 0; i < GATT_MAX_SR_PROFILES; i ++, p_rcb ++) {
if (p_rcb->in_use && p_rcb->s_hdl <= handle && p_rcb->e_hdl >= handle) {
trans_id = gatt_sr_enqueue_cmd(p_tcb, op_code, handle);
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, p_rcb->gatt_if);
tGATTS_DATA p_data = {0};
p_data.handle = handle;
gatt_sr_send_req_callback(conn_id,
trans_id, GATTS_REQ_TYPE_CONF, &p_data);
}{...}
}{...}
}{...}
}{...} else {
GATT_TRACE_ERROR("unexpected handle value confirmation");
}{...}
}{ ... }
#if GATTS_ROBUST_CACHING_ENABLED
static BOOLEAN gatts_handle_db_out_of_sync(tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
if (gatt_sr_is_cl_change_aware(p_tcb)) {
return false;
}{...}
bool should_ignore = true;
bool should_rsp = true;
switch (op_code) {
case GATT_REQ_READ_BY_TYPE:
{
tBT_UUID uuid;
UINT16 s_hdl = 0;
UINT16 e_hdl = 0;
UINT16 db_hash_handle = gatt_cb.handle_of_database_hash;
tGATT_STATUS reason = gatts_validate_packet_format(op_code, &len, &p_data, &uuid, &s_hdl, &e_hdl);
if (reason == GATT_SUCCESS &&
(s_hdl <= db_hash_handle && db_hash_handle <= e_hdl) &&
(uuid.uu.uuid16 == GATT_UUID_GATT_DATABASE_HASH)) {
should_ignore = false;
}{...}
break;
}{...}
case GATT_REQ_READ:
#if 0
{
UINT16 handle = 0;
UINT8 *p = p_data;
tGATT_STATUS status = GATT_SUCCESS;
if (len < 2) {
status = GATT_INVALID_PDU;
}{...} else {
STREAM_TO_UINT16(handle, p);
len -= 2;
}{...}
if (status == GATT_SUCCESS && handle == gatt_cb.handle_of_database_hash) {
should_ignore = false;
}{...}
break;
}/* ... */
{...} #endif...
case GATT_REQ_READ_BY_GRP_TYPE:
case GATT_REQ_FIND_TYPE_VALUE:
case GATT_REQ_FIND_INFO:
case GATT_REQ_READ_BLOB:
case GATT_REQ_READ_MULTI:
case GATT_REQ_READ_MULTI_VAR:
case GATT_REQ_WRITE:
case GATT_REQ_PREPARE_WRITE:
break;...
case GATT_CMD_WRITE:
case GATT_SIGN_CMD_WRITE:
should_rsp = false;
break;...
case GATT_REQ_MTU:
case GATT_REQ_EXEC_WRITE:
case GATT_HANDLE_VALUE_CONF:
default:
should_ignore = false;
break;...
}{...}
if (should_ignore) {
if (should_rsp) {
gatt_send_error_rsp(p_tcb, GATT_DATABASE_OUT_OF_SYNC, op_code, 0x0000, false);
}{...}
GATT_TRACE_ERROR("database out of sync op_code %x, should_rsp %d", op_code, should_rsp);
gatt_sr_update_cl_status(p_tcb, should_rsp);
}{...}
return should_ignore;
}{...}
/* ... */
#endif
/* ... */
void gatt_server_handle_client_req (tGATT_TCB *p_tcb, UINT8 op_code,
UINT16 len, UINT8 *p_data)
{
if (!gatt_sr_cmd_empty(p_tcb) && op_code != GATT_HANDLE_VALUE_CONF) {
return;
}{...}
if (len >= p_tcb->payload_size) {
GATT_TRACE_ERROR("server receive invalid PDU size:%d pdu size:%d", len + 1, p_tcb->payload_size );
if (op_code != GATT_CMD_WRITE &&
op_code != GATT_SIGN_CMD_WRITE &&
op_code != GATT_HANDLE_VALUE_CONF) {
gatt_send_error_rsp (p_tcb, GATT_INVALID_PDU, op_code, 0, FALSE);
}{...}
}{...} else {
#if GATTS_ROBUST_CACHING_ENABLED
if (gatts_handle_db_out_of_sync(p_tcb, op_code, len, p_data)) {
return;
}{...}
#endif/* ... */
switch (op_code) {
case GATT_REQ_READ_BY_GRP_TYPE:
case GATT_REQ_FIND_TYPE_VALUE:
gatts_process_primary_service_req (p_tcb, op_code, len, p_data);
break;
...
case GATT_REQ_FIND_INFO:
gatts_process_find_info(p_tcb, op_code, len, p_data);
break;
...
case GATT_REQ_READ_BY_TYPE:
gatts_process_read_by_type_req(p_tcb, op_code, len, p_data);
break;
...
case GATT_REQ_READ:
case GATT_REQ_READ_BLOB:
case GATT_REQ_WRITE:
case GATT_CMD_WRITE:
case GATT_SIGN_CMD_WRITE:
case GATT_REQ_PREPARE_WRITE:
gatts_process_attribute_req (p_tcb, op_code, len, p_data);
break;
...
case GATT_HANDLE_VALUE_CONF:
gatts_process_value_conf (p_tcb, op_code);
break;
...
case GATT_REQ_MTU:
gatts_process_mtu_req (p_tcb, len, p_data);
break;
...
case GATT_REQ_EXEC_WRITE:
gatt_process_exec_write_req (p_tcb, op_code, len, p_data);
break;
...
case GATT_REQ_READ_MULTI:
case GATT_REQ_READ_MULTI_VAR:
gatt_process_read_multi_req (p_tcb, op_code, len, p_data);
break;
...
default:
break;...
}{...}
}{...}
}{ ... }
/* ... */#endif