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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
124
125
126
127
128
129
130
131
132
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
176
177
178
179
183
190
191
192
195
196
200
201
202
203
204
208
209
214
215
216
217
218
219
220
221
222
223
224
243
244
245
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
284
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
321
322
323
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
363
364
365
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
405
406
407
411
412
413
414
425
426
427
428
431
432
433
434
435
436
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
480
481
486
487
488
489
493
494
495
499
500
501
502
503
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
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
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
610
611
615
616
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
682
683
687
688
689
690
691
692
693
694
695
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
734
735
739
740
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
776
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
837
838
839
840
841
842
843
844
845
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
880
881
885
886
887
890
891
895
896
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
937
938
942
943
947
948
949
950
951
952
953
954
958
959
960
961
962
963
964
965
966
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
1003
1004
1008
1009
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1027
1028
1034
1035
1036
1037
1038
1039
1042
1043
1044
1046
1047
1048
1052
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
1087
1088
1092
1093
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1111
1112
1115
1118
1119
1122
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
1158
1159
1163
1164
1168
1169
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
1214
1217
1218
1219
1220
1225
1226
1227
1228
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
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
1334
1335
1336
1337
1338
1339
1341
1342
1343
1344
1348
1349
1350
1353
1354
1361
1362
1363
1364
1365
1366
1367
1368
1375
1376
1377
1378
1379
1380
1381
1382
1383
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1467
1468
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1510
1511
1512
1513
1514
1515
1516
1517
1526
1529
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1604
1605
1608
1609
1610
1611
1612
1613
1614
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
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
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
1721
1722
1727
1728
1729
1730
1731
1732
1733
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1760
1761
1765
1766
1769
1770
1771
1772
1773
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1795
1796
1797
1798
1799
1804
1805
/* ... */
/* ... */
#include "common/bt_target.h"
#if defined(BTA_GATT_INCLUDED) && (BTA_GATT_INCLUDED == TRUE)
#include "osi/allocator.h"
#include <string.h>
#include "stack/gatt_api.h"
#include "gatt_int.h"
#include "stack/l2c_api.h"
#include "btm_int.h"
#include "stack/sdpdefs.h"
#include "stack/sdp_api.h"8 includes
/* ... */
UINT8 GATT_SetTraceLevel (UINT8 new_level)
{
if (new_level != 0xFF) {
gatt_cb.trace_level = new_level;
}{...}
return (gatt_cb.trace_level);
}{ ... }
#if (GATTS_INCLUDED == TRUE)
/* ... */
/* ... */
BOOLEAN GATTS_AddHandleRange(tGATTS_HNDL_RANGE *p_hndl_range)
{
tGATT_HDL_LIST_ELEM *p_buf;
BOOLEAN status = FALSE;
if ((p_buf = gatt_alloc_hdl_buffer()) != NULL) {
p_buf->asgn_range = *p_hndl_range;
status = gatt_add_an_item_to_list(&gatt_cb.hdl_list_info, p_buf);
}{...}
return status;
}{ ... }
/* ... */
BOOLEAN GATTS_NVRegister (const tGATT_APPL_INFO *p_cb_info)
{
BOOLEAN status = FALSE;
if (p_cb_info) {
gatt_cb.cb_info = *p_cb_info;
status = TRUE;
gatt_init_srv_chg();
}{...}
return status;
}{ ... }
#if GATTS_ROBUST_CACHING_ENABLED
static void gatt_update_for_database_change(void)
{
UINT8 i;
gatts_calculate_datebase_hash(gatt_cb.database_hash);
for (i = 0; i < GATT_MAX_PHY_CHANNEL; i++) {
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(i);
if (p_tcb && p_tcb->in_use) {
gatt_sr_update_cl_status(p_tcb, false);
}{...}
}{...}
}{...}
/* ... */#endif
/* ... */
UINT16 GATTS_CreateService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid,
UINT16 svc_inst, UINT16 num_handles, BOOLEAN is_pri)
{
tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
tGATT_HDL_LIST_ELEM *p_list = NULL;
UINT16 s_hdl = 0;
BOOLEAN save_hdl = FALSE;
tGATTS_PENDING_NEW_SRV_START *p_buf = NULL;
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tBT_UUID *p_app_uuid128;
GATT_TRACE_API ("GATTS_CreateService\n" );
if (p_reg == NULL) {
GATT_TRACE_ERROR ("Inavlid gatt_if=%d\n", gatt_if);
return (0);
}{...}
p_app_uuid128 = &p_reg->app_uuid128;
if ((p_list = gatt_find_hdl_buffer_by_app_id(p_app_uuid128, p_svc_uuid, svc_inst)) != NULL) {
s_hdl = p_list->asgn_range.s_handle;
GATT_TRACE_DEBUG ("Service already been created!!\n");
}{...} else {
if ( (p_svc_uuid->len == LEN_UUID_16) && (p_svc_uuid->uu.uuid16 == UUID_SERVCLASS_GATT_SERVER)) {
s_hdl = gatt_cb.hdl_cfg.gatt_start_hdl;
save_hdl = TRUE;
}{...} else if ((p_svc_uuid->len == LEN_UUID_16) && (p_svc_uuid->uu.uuid16 == UUID_SERVCLASS_GAP_SERVER)) {
s_hdl = gatt_cb.hdl_cfg.gap_start_hdl;
save_hdl = TRUE;
}{...} else {
p_list = p_list_info->p_first;
if (p_list) {
s_hdl = p_list->asgn_range.e_handle + 1;
}{...}
if (s_hdl < gatt_cb.hdl_cfg.app_start_hdl) {
s_hdl = gatt_cb.hdl_cfg.app_start_hdl;
}{...}
save_hdl = TRUE;
}{...}
if (num_handles > (0xFFFF - s_hdl + 1)) {
GATT_TRACE_ERROR ("GATTS_ReserveHandles: no handles, s_hdl: %u needed: %u\n", s_hdl, num_handles);
return (0);
}{...}
if ( (p_list = gatt_alloc_hdl_buffer()) == NULL) {
GATT_TRACE_ERROR ("GATTS_ReserveHandles: no free handle blocks\n");
return (0);
}{...}
p_list->asgn_range.app_uuid128 = *p_app_uuid128;
p_list->asgn_range.svc_uuid = *p_svc_uuid;
p_list->asgn_range.svc_inst = svc_inst;
p_list->asgn_range.s_handle = s_hdl;
p_list->asgn_range.e_handle = s_hdl + num_handles - 1;
p_list->asgn_range.is_primary = is_pri;
gatt_add_an_item_to_list(p_list_info, p_list);
if (save_hdl) {
if (gatt_cb.cb_info.p_nv_save_callback) {
(*gatt_cb.cb_info.p_nv_save_callback)(TRUE, &p_list->asgn_range);
}{...}
if ( (p_buf = gatt_add_pending_new_srv_start(&p_list->asgn_range)) == NULL) {
GATT_TRACE_ERROR ("gatt_add_pending_new_srv_start: no free blocks\n");
if (p_list) {
gatt_remove_an_item_from_list(p_list_info, p_list);
gatt_free_attr_value_buffer(p_list);
gatt_free_hdl_buffer(p_list);
}{...}
return (0);
}{...}
GATT_TRACE_DEBUG ("Add a new srv chg item\n");
}{...}
}{...}
if (!gatts_init_service_db(&p_list->svc_db, p_svc_uuid, is_pri, s_hdl , num_handles)) {
GATT_TRACE_ERROR ("GATTS_ReserveHandles: service DB initialization failed\n");
if (p_list) {
gatt_remove_an_item_from_list(p_list_info, p_list);
gatt_free_attr_value_buffer(p_list);
gatt_free_hdl_buffer(p_list);
}{...}
if (p_buf) {
osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
}{...}
return (0);
}{...}
return (s_hdl);
}{ ... }
/* ... */
UINT16 GATTS_AddIncludeService (UINT16 service_handle, UINT16 include_svc_handle)
{
tGATT_HDL_LIST_ELEM *p_decl, *p_incl_decl;
if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
GATT_TRACE_DEBUG("Service not created");
return 0;
}{...}
if ((p_incl_decl = gatt_find_hdl_buffer_by_handle(include_svc_handle)) == NULL) {
GATT_TRACE_DEBUG("Included Service not created");
return 0;
}{...}
return gatts_add_included_service(&p_decl->svc_db,
p_incl_decl->asgn_range.s_handle,
p_incl_decl->asgn_range.e_handle,
p_incl_decl->asgn_range.svc_uuid);
}{ ... }
/* ... */
UINT16 GATTS_AddCharacteristic (UINT16 service_handle, tBT_UUID *p_char_uuid,
tGATT_PERM perm, tGATT_CHAR_PROP property,
tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
{
tGATT_HDL_LIST_ELEM *p_decl;
if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
GATT_TRACE_DEBUG("Service not created\n");
return 0;
}{...}
if ( ((property & GATT_CHAR_PROP_BIT_AUTH) && !(perm & GATT_WRITE_SIGNED_PERM)) ||
((perm & GATT_WRITE_SIGNED_PERM) && !(property & GATT_CHAR_PROP_BIT_AUTH)) ) {
GATT_TRACE_DEBUG("Invalid configuration property=0x%x perm=0x%x\n ", property, perm);
return 0;
}{...}
return gatts_add_characteristic(&p_decl->svc_db,
perm,
property,
p_char_uuid,
attr_val, control);
}{ ... }
/* ... */
UINT16 GATTS_AddCharDescriptor (UINT16 service_handle,
tGATT_PERM perm,
tBT_UUID *p_descr_uuid, tGATT_ATTR_VAL *attr_val, tGATTS_ATTR_CONTROL *control)
{
tGATT_HDL_LIST_ELEM *p_decl;
if ((p_decl = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
GATT_TRACE_DEBUG("Service not created");
return 0;
}{...}
if (p_descr_uuid == NULL ||
(p_descr_uuid->len != LEN_UUID_128 && p_descr_uuid->len != LEN_UUID_16
&& p_descr_uuid->len != LEN_UUID_32)) {
GATT_TRACE_DEBUG("Illegal parameter");
return 0;
}{...}
return gatts_add_char_descr(&p_decl->svc_db,
perm,
p_descr_uuid,
attr_val, control);
}{ ... }
/* ... */
BOOLEAN GATTS_DeleteService (tGATT_IF gatt_if, tBT_UUID *p_svc_uuid, UINT16 svc_inst)
{
tGATT_HDL_LIST_INFO *p_list_info = &gatt_cb.hdl_list_info;
tGATT_HDL_LIST_ELEM *p_list = NULL;
UINT8 i_sreg;
tGATTS_PENDING_NEW_SRV_START *p_buf;
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tBT_UUID *p_app_uuid128;
GATT_TRACE_DEBUG ("GATTS_DeleteService");
if (p_reg == NULL) {
GATT_TRACE_ERROR ("Application not foud");
return (FALSE);
}{...}
p_app_uuid128 = &p_reg->app_uuid128;
if ((p_list = gatt_find_hdl_buffer_by_app_id(p_app_uuid128, p_svc_uuid, svc_inst)) == NULL) {
GATT_TRACE_ERROR ("No Service found");
return (FALSE);
}{...}
if ( (p_buf = gatt_sr_is_new_srv_chg(&p_list->asgn_range.app_uuid128,
&p_list->asgn_range.svc_uuid,
p_list->asgn_range.svc_inst)) != NULL) {
GATT_TRACE_DEBUG ("Delete a new service changed item - the service has not yet started");
osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
}{...} else {
#if GATTS_ROBUST_CACHING_ENABLED
gatt_update_for_database_change();
#endif
if (gatt_cb.srv_chg_mode == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}{...}
}{...}
if ((i_sreg = gatt_sr_find_i_rcb_by_app_id (p_app_uuid128,
p_svc_uuid,
svc_inst)) != GATT_MAX_SR_PROFILES) {
GATTS_StopService(gatt_cb.sr_reg[i_sreg].s_hdl);
}{...}
GATT_TRACE_DEBUG ("released handles s_hdl=%u e_hdl=%u",
p_list->asgn_range.s_handle , p_list->asgn_range.e_handle );
if ( (p_list->asgn_range.s_handle >= gatt_cb.hdl_cfg.app_start_hdl)
&& gatt_cb.cb_info.p_nv_save_callback) {
(*gatt_cb.cb_info.p_nv_save_callback)(FALSE, &p_list->asgn_range);
}{...}
gatt_remove_an_item_from_list(p_list_info, p_list);
gatt_free_attr_value_buffer(p_list);
gatt_free_hdl_buffer(p_list);
return (TRUE);
}{ ... }
/* ... */
tGATT_STATUS GATTS_StartService (tGATT_IF gatt_if, UINT16 service_handle,
tGATT_TRANSPORT sup_transport)
{
tGATT_SR_REG *p_sreg;
tGATT_HDL_LIST_ELEM *p_list = NULL;
UINT8 i_sreg;
#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
tBT_UUID *p_uuid;
#endif
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATTS_PENDING_NEW_SRV_START *p_buf;
GATT_TRACE_API ("GATTS_StartService");
if (p_reg == NULL) {
GATT_TRACE_ERROR ("Application not found ");
return GATT_NOT_FOUND;
}{...}
if ((p_list = gatt_find_hdl_buffer_by_handle(service_handle)) == NULL) {
GATT_TRACE_ERROR ("no service found");
return GATT_NOT_FOUND;
}{...}
if (gatt_sr_find_i_rcb_by_app_id (&p_list->asgn_range.app_uuid128,
&p_list->asgn_range.svc_uuid,
p_list->asgn_range.svc_inst) != GATT_MAX_SR_PROFILES) {
GATT_TRACE_ERROR ("Duplicate Service start - Service already started");
return GATT_SERVICE_STARTED;
}{...}
if ((i_sreg = gatt_sr_alloc_rcb(p_list)) == GATT_MAX_SR_PROFILES) {
GATT_TRACE_ERROR ("GATTS_StartService: no free server registration block");
return GATT_NO_RESOURCES;
}{...}
p_sreg = &gatt_cb.sr_reg[i_sreg];
p_sreg->gatt_if = gatt_if;
switch (sup_transport) {
case GATT_TRANSPORT_BR_EDR:
case GATT_TRANSPORT_LE_BR_EDR:
if (p_sreg->type == GATT_UUID_PRI_SERVICE) {
#if (SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
p_uuid = gatts_get_service_uuid (p_sreg->p_db);
p_sreg->sdp_handle = gatt_add_sdp_record(p_uuid, p_sreg->s_hdl, p_sreg->e_hdl);/* ... */
#endif
}{...}
break;...
default:
break;...
}{...}
gatts_update_srv_list_elem(i_sreg, p_sreg->s_hdl,
p_list->asgn_range.is_primary);
gatt_add_a_srv_to_list(&gatt_cb.srv_list_info, &gatt_cb.srv_list[i_sreg]);
GATT_TRACE_DEBUG ("allocated i_sreg=%d\n", i_sreg);
GATT_TRACE_DEBUG ("s_hdl=%d e_hdl=%d type=0x%x svc_inst=%d sdp_hdl=0x%x\n",
p_sreg->s_hdl, p_sreg->e_hdl,
p_sreg->type, p_sreg->service_instance,
p_sreg->sdp_handle);
if ( (p_buf = gatt_sr_is_new_srv_chg(&p_list->asgn_range.app_uuid128,
&p_list->asgn_range.svc_uuid,
p_list->asgn_range.svc_inst)) != NULL) {
#if GATTS_ROBUST_CACHING_ENABLED
gatt_update_for_database_change();
#endif
if (gatt_cb.srv_chg_mode == GATTS_SEND_SERVICE_CHANGE_AUTO) {
gatt_proc_srv_chg();
}{...}
osi_free(fixed_queue_try_remove_from_queue(gatt_cb.pending_new_srv_start_q, p_buf));
}{...}
return GATT_SUCCESS;
}{ ... }
/* ... */
void GATTS_StopService (UINT16 service_handle)
{
UINT8 ii = gatt_sr_find_i_rcb_by_handle(service_handle);
GATT_TRACE_API("GATTS_StopService %u", service_handle);
if ( (ii > 0) && (ii < GATT_MAX_SR_PROFILES) && (gatt_cb.sr_reg[ii].in_use) ) {
#if(SDP_INCLUDED == TRUE && CLASSIC_BT_GATT_INCLUDED == TRUE)
if (gatt_cb.sr_reg[ii].sdp_handle) {
SDP_DeleteRecord(gatt_cb.sr_reg[ii].sdp_handle);
}{...}
#endif/* ... */
gatt_remove_a_srv_from_list(&gatt_cb.srv_list_info, &gatt_cb.srv_list[ii]);
gatt_cb.srv_list[ii].in_use = FALSE;
memset (&gatt_cb.sr_reg[ii], 0, sizeof(tGATT_SR_REG));
}{...} else {
GATT_TRACE_ERROR("GATTS_StopService service_handle: %u is not in use", service_handle);
}{...}
}{ ... }
/* ... */
tGATT_STATUS GATTS_HandleValueIndication (UINT16 conn_id, UINT16 attr_handle, UINT16 val_len, UINT8 *p_val)
{
tGATT_STATUS cmd_status = GATT_NO_RESOURCES;
tGATT_VALUE indication;
BT_HDR *p_msg;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
GATT_TRACE_API ("GATTS_HandleValueIndication");
if ( (p_reg == NULL) || (p_tcb == NULL)) {
GATT_TRACE_ERROR ("GATTS_HandleValueIndication Unknown conn_id: %u ", conn_id);
return (tGATT_STATUS) GATT_INVALID_CONN_ID;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_WRONG_STATE;
}{...}
if (! GATT_HANDLE_IS_VALID (attr_handle)) {
return GATT_ILLEGAL_PARAMETER;
}{...}
indication.conn_id = conn_id;
indication.handle = attr_handle;
indication.len = val_len;
memcpy (indication.value, p_val, val_len);
indication.auth_req = GATT_AUTH_REQ_NONE;
if (GATT_HANDLE_IS_VALID(p_tcb->indicate_handle)) {
/* ... */
return GATT_BUSY;
}{...} else {
if ( (p_msg = attp_build_sr_msg (p_tcb, GATT_HANDLE_VALUE_IND, (tGATT_SR_MSG *)&indication)) != NULL) {
cmd_status = attp_send_sr_msg (p_tcb, p_msg);
if (cmd_status == GATT_SUCCESS || cmd_status == GATT_CONGESTED) {
p_tcb->indicate_handle = indication.handle;
gatt_start_conf_timer(p_tcb);
}{...}
}{...}
}{...}
return cmd_status;
}{ ... }
/* ... */
tGATT_STATUS GATTS_HandleValueNotification (UINT16 conn_id, UINT16 attr_handle,
UINT16 val_len, UINT8 *p_val)
{
tGATT_STATUS cmd_sent = GATT_ILLEGAL_PARAMETER;
BT_HDR *p_buf;
tGATT_VALUE notif;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
GATT_TRACE_API ("GATTS_HandleValueNotification");
if ( (p_reg == NULL) || (p_tcb == NULL)) {
GATT_TRACE_ERROR ("GATTS_HandleValueNotification Unknown conn_id: %u \n", conn_id);
return (tGATT_STATUS) GATT_INVALID_CONN_ID;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_WRONG_STATE;
}{...}
if (GATT_HANDLE_IS_VALID (attr_handle)) {
notif.handle = attr_handle;
notif.len = val_len;
memcpy (notif.value, p_val, val_len);
notif.auth_req = GATT_AUTH_REQ_NONE;
if ((p_buf = attp_build_sr_msg (p_tcb, GATT_HANDLE_VALUE_NOTIF, (tGATT_SR_MSG *)¬if))
!= NULL) {
cmd_sent = attp_send_sr_msg (p_tcb, p_buf);
}{...} else {
cmd_sent = GATT_NO_RESOURCES;
}{...}
}{...}
return cmd_sent;
}{ ... }
/* ... */
tGATT_STATUS GATTS_SendRsp (UINT16 conn_id, UINT32 trans_id,
tGATT_STATUS status, tGATTS_RSP *p_msg)
{
tGATT_STATUS cmd_sent = GATT_ILLEGAL_PARAMETER;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
GATT_TRACE_API ("GATTS_SendRsp: conn_id: %u trans_id: %u Status: 0x%04x\n",
conn_id, trans_id, status);
if ( (p_reg == NULL) || (p_tcb == NULL)) {
GATT_TRACE_ERROR ("GATTS_SendRsp Unknown conn_id: %u\n", conn_id);
return (tGATT_STATUS) GATT_INVALID_CONN_ID;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_WRONG_STATE;
}{...}
if (p_tcb->sr_cmd.trans_id != trans_id) {
GATT_TRACE_ERROR ("GATTS_SendRsp conn_id: %u waiting for op_code = %02x\n",
conn_id, p_tcb->sr_cmd.op_code);
return (GATT_WRONG_STATE);
}{...}
cmd_sent = gatt_sr_process_app_rsp (p_tcb, gatt_if, trans_id, p_tcb->sr_cmd.op_code, status, p_msg);
return cmd_sent;
}{ ... }
/* ... */
tGATT_STATUS GATTS_SetAttributeValue(UINT16 attr_handle, UINT16 length, UINT8 *value)
{
tGATT_STATUS status;
tGATT_HDL_LIST_ELEM *p_decl = NULL;
GATT_TRACE_DEBUG("GATTS_SetAttributeValue: attr_handle: %u length: %u \n",
attr_handle, length);
if (length <= 0){
return GATT_INVALID_ATTR_LEN;
}{...}
if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
GATT_TRACE_DEBUG("Service not created\n");
return GATT_INVALID_HANDLE;
}{...}
status = gatts_set_attribute_value(&p_decl->svc_db, attr_handle, length, value);
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTS_GetAttributeValue(UINT16 attr_handle, UINT16 *length, UINT8 **value)
{
tGATT_STATUS status;
tGATT_HDL_LIST_ELEM *p_decl;
GATT_TRACE_DEBUG("GATTS_GetAttributeValue: attr_handle: %u\n",
attr_handle);
if ((p_decl = gatt_find_hdl_buffer_by_attr_handle(attr_handle)) == NULL) {
GATT_TRACE_ERROR("Service not created\n");
*length = 0;
return GATT_INVALID_HANDLE;
}{...}
status = gatts_get_attribute_value(&p_decl->svc_db, attr_handle, length, value);
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTS_GetAttributeValueInternal(UINT16 attr_handle, UINT16 *length, UINT8 **value)
{
return gatts_get_attr_value_internal(attr_handle, length, value);
}{ ... }
#endif/* ... */
#if (GATTC_INCLUDED == TRUE)
...
/* ... */
tGATT_STATUS GATTC_ConfigureMTU (UINT16 conn_id)
{
UINT8 ret = GATT_NO_RESOURCES;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_CLCB *p_clcb;
uint16_t mtu = gatt_get_local_mtu();
GATT_TRACE_API ("GATTC_ConfigureMTU conn_id=%d mtu=%d", conn_id, mtu );
if ( (p_tcb == NULL) || (p_reg == NULL) || (mtu < GATT_DEF_BLE_MTU_SIZE) || (mtu > GATT_MAX_MTU_SIZE)) {
return GATT_ILLEGAL_PARAMETER;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_ERROR;
}{...}
if (p_tcb->transport != BT_TRANSPORT_LE) {
return GATT_ERROR;
}{...}
if (gatt_is_clcb_allocated(conn_id)) {
GATT_TRACE_ERROR("GATTC_ConfigureMTU GATT_BUSY conn_id = %d", conn_id);
return GATT_BUSY;
}{...}
if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL) {
p_clcb->p_tcb->payload_size = mtu;
p_clcb->operation = GATTC_OPTYPE_CONFIG;
ret = attp_send_cl_msg (p_clcb->p_tcb, p_clcb->clcb_idx, GATT_REQ_MTU, (tGATT_CL_MSG *)&mtu);
}{...}
return ret;
}{ ... }
/* ... */
tGATT_STATUS GATTC_Discover (UINT16 conn_id, tGATT_DISC_TYPE disc_type,
tGATT_DISC_PARAM *p_param)
{
tGATT_STATUS status = GATT_SUCCESS;
tGATT_CLCB *p_clcb;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
GATT_TRACE_API ("GATTC_Discover conn_id=%d disc_type=%d", conn_id, disc_type);
if ( (p_tcb == NULL) || (p_reg == NULL) || (p_param == NULL) ||
(disc_type >= GATT_DISC_MAX)) {
GATT_TRACE_ERROR("GATTC_Discover Illegal param: disc_type %d conn_id = %d", disc_type, conn_id);
return GATT_ILLEGAL_PARAMETER;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_ERROR;
}{...}
if (gatt_is_clcb_allocated(conn_id)) {
GATT_TRACE_ERROR("GATTC_Discover GATT_BUSY conn_id = %d", conn_id);
return GATT_BUSY;
}{...}
if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
if (!GATT_HANDLE_IS_VALID(p_param->s_handle) ||
!GATT_HANDLE_IS_VALID(p_param->e_handle) ||
(disc_type == GATT_DISC_SRVC_BY_UUID &&
p_param->service.len == 0)) {
gatt_clcb_dealloc(p_clcb);
return GATT_ILLEGAL_PARAMETER;
}{...}
p_clcb->operation = GATTC_OPTYPE_DISCOVERY;
p_clcb->op_subtype = disc_type;
p_clcb->s_handle = p_param->s_handle;
p_clcb->e_handle = p_param->e_handle;
p_clcb->uuid = p_param->service;
gatt_act_discovery(p_clcb);
}{...} else {
status = GATT_NO_RESOURCES;
}{...}
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTC_Read (UINT16 conn_id, tGATT_READ_TYPE type, tGATT_READ_PARAM *p_read)
{
tGATT_STATUS status = GATT_SUCCESS;
tGATT_CLCB *p_clcb;
tGATT_READ_MULTI *p_read_multi;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
GATT_TRACE_API ("GATTC_Read conn_id=%d type=%d", conn_id, type);
if ( (p_tcb == NULL) || (p_reg == NULL) || (p_read == NULL) || ((type >= GATT_READ_MAX) || (type == 0))) {
GATT_TRACE_ERROR("GATT_Read Illegal param: conn_id %d, type 0%d,", conn_id, type);
return GATT_ILLEGAL_PARAMETER;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_ERROR;
}{...}
if (gatt_is_clcb_allocated(conn_id)) {
GATT_TRACE_ERROR("GATTC_Read GATT_BUSY conn_id = %d", conn_id);
return GATT_BUSY;
}{...}
if ( (p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
p_clcb->operation = GATTC_OPTYPE_READ;
p_clcb->op_subtype = type;
p_clcb->auth_req = p_read->by_handle.auth_req;
p_clcb->counter = 0;
switch (type) {
case GATT_READ_BY_TYPE:
case GATT_READ_CHAR_VALUE:
p_clcb->s_handle = p_read->service.s_handle;
p_clcb->e_handle = p_read->service.e_handle;
memcpy(&p_clcb->uuid, &p_read->service.uuid, sizeof(tBT_UUID));
break;...
case GATT_READ_MULTIPLE:
case GATT_READ_MULTIPLE_VAR:
p_clcb->s_handle = 0;
p_read_multi = (tGATT_READ_MULTI *)osi_malloc(sizeof(tGATT_READ_MULTI));
p_clcb->p_attr_buf = (UINT8 *)p_read_multi;
memcpy (p_read_multi, &p_read->read_multiple, sizeof(tGATT_READ_MULTI));...
case GATT_READ_BY_HANDLE:
case GATT_READ_PARTIAL:
memset(&p_clcb->uuid, 0, sizeof(tBT_UUID));
p_clcb->s_handle = p_read->by_handle.handle;
if (type == GATT_READ_PARTIAL) {
p_clcb->counter = p_read->partial.offset;
}{...}
break;...
default:
break;...
}{...}
if (gatt_security_check_start(p_clcb) == FALSE) {
status = GATT_NO_RESOURCES;
gatt_clcb_dealloc(p_clcb);
}{...}
}{...} else {
status = GATT_NO_RESOURCES;
}{...}
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTC_Write (UINT16 conn_id, tGATT_WRITE_TYPE type, tGATT_VALUE *p_write)
{
tGATT_STATUS status = GATT_SUCCESS;
tGATT_CLCB *p_clcb;
tGATT_VALUE *p;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
if ( (p_tcb == NULL) || (p_reg == NULL) || (p_write == NULL) ||
((type != GATT_WRITE) && (type != GATT_WRITE_PREPARE) && (type != GATT_WRITE_NO_RSP)) ) {
GATT_TRACE_ERROR("GATT_Write Illegal param: conn_id %d, type 0%d,", conn_id, type);
return GATT_ILLEGAL_PARAMETER;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_ERROR;
}{...}
if (gatt_is_clcb_allocated(conn_id)) {
GATT_TRACE_ERROR("GATTC_Write GATT_BUSY conn_id = %d", conn_id);
return GATT_BUSY;
}{...}
if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL ) {
p_clcb->operation = GATTC_OPTYPE_WRITE;
p_clcb->op_subtype = type;
p_clcb->auth_req = p_write->auth_req;
if (( p_clcb->p_attr_buf = (UINT8 *)osi_malloc((UINT16)sizeof(tGATT_VALUE))) != NULL) {
memcpy(p_clcb->p_attr_buf, (void *)p_write, sizeof(tGATT_VALUE));
p = (tGATT_VALUE *)p_clcb->p_attr_buf;
if (type == GATT_WRITE_PREPARE) {
p_clcb->start_offset = p_write->offset;
p->offset = 0;
}{...}
if (gatt_security_check_start(p_clcb) == FALSE) {
status = GATT_NO_RESOURCES;
}{...}
}{...} else {
status = GATT_NO_RESOURCES;
}{...}
if (status == GATT_NO_RESOURCES) {
gatt_clcb_dealloc(p_clcb);
}{...}
}{...} else {
status = GATT_NO_RESOURCES;
}{...}
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTC_ExecuteWrite (UINT16 conn_id, BOOLEAN is_execute)
{
tGATT_STATUS status = GATT_SUCCESS;
tGATT_CLCB *p_clcb;
tGATT_EXEC_FLAG flag;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
GATT_TRACE_API ("GATTC_ExecuteWrite conn_id=%d is_execute=%d", conn_id, is_execute);
if ( (p_tcb == NULL) || (p_reg == NULL) ) {
GATT_TRACE_ERROR("GATTC_ExecuteWrite Illegal param: conn_id %d", conn_id);
return GATT_ILLEGAL_PARAMETER;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_ERROR;
}{...}
if (gatt_is_clcb_allocated(conn_id)) {
GATT_TRACE_ERROR("GATTC_Write GATT_BUSY conn_id = %d", conn_id);
return GATT_BUSY;
}{...}
if ((p_clcb = gatt_clcb_alloc(conn_id)) != NULL) {
p_clcb->operation = GATTC_OPTYPE_EXE_WRITE;
flag = is_execute ? GATT_PREP_WRITE_EXEC : GATT_PREP_WRITE_CANCEL;
gatt_send_queue_write_cancel (p_clcb->p_tcb, p_clcb, flag);
}{...} else {
GATT_TRACE_ERROR("Unable to allocate client CB for conn_id %d ", conn_id);
status = GATT_NO_RESOURCES;
}{...}
return status;
}{ ... }
/* ... */
tGATT_STATUS GATTC_SendHandleValueConfirm (UINT16 conn_id, UINT16 handle)
{
tGATT_STATUS ret = GATT_ILLEGAL_PARAMETER;
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(GATT_GET_TCB_IDX(conn_id));
GATT_TRACE_API ("GATTC_SendHandleValueConfirm conn_id=%d handle=0x%x", conn_id, handle);
if (p_tcb) {
if (p_tcb->ind_count > 0 ) {
btu_stop_timer (&p_tcb->ind_ack_timer_ent);
GATT_TRACE_DEBUG ("notif_count=%d ", p_tcb->ind_count);
ret = attp_send_cl_msg(p_tcb, 0, GATT_HANDLE_VALUE_CONF, (tGATT_CL_MSG *)&handle);
p_tcb->ind_count = 0;
}{...} else {
GATT_TRACE_DEBUG ("GATTC_SendHandleValueConfirm - conn_id: %u - ignored not waiting for indicaiton ack", conn_id);
ret = GATT_SUCCESS;
}{...}
}{...} else {
GATT_TRACE_ERROR ("GATTC_SendHandleValueConfirm - Unknown conn_id: %u", conn_id);
}{...}
return ret;
}{ ... }
tGATT_STATUS GATTC_AutoDiscoverEnable(UINT8 enable)
{
gatt_cb.auto_disc = (enable > 0) ? TRUE : FALSE;
return GATT_SUCCESS;
}{ ... }
/* ... */#endif
...
/* ... */
void GATT_SetIdleTimeout (BD_ADDR bd_addr, UINT16 idle_tout, tBT_TRANSPORT transport)
{
tGATT_TCB *p_tcb;
BOOLEAN status = FALSE;
if ((p_tcb = gatt_find_tcb_by_addr (bd_addr, transport)) != NULL) {
if (p_tcb->att_lcid == L2CAP_ATT_CID) {
status = L2CA_SetFixedChannelTout (bd_addr, L2CAP_ATT_CID, idle_tout);
if (idle_tout == GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP) {
L2CA_SetIdleTimeoutByBdAddr(p_tcb->peer_bda,
GATT_LINK_IDLE_TIMEOUT_WHEN_NO_APP, BT_TRANSPORT_LE);
}{...}
}{...} else {
status = L2CA_SetIdleTimeout (p_tcb->att_lcid, idle_tout, FALSE);
}{...}
}{...}
#if (CONFIG_BT_STACK_NO_LOG)
(void) status;
#endif
GATT_TRACE_API ("GATT_SetIdleTimeout idle_tout=%d status=%d(1-OK 0-not performed)",
idle_tout, status);
}{ ... }
/* ... */
tGATT_IF GATT_Register (tBT_UUID *p_app_uuid128, const tGATT_CBACK *p_cb_info)
{
tGATT_REG *p_reg;
UINT8 i_gatt_if = 0;
tGATT_IF gatt_if = 0;
GATT_TRACE_API ("GATT_Register");
gatt_dbg_display_uuid(*p_app_uuid128);
for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS; i_gatt_if++, p_reg++) {
if (p_reg->in_use && !memcmp(p_app_uuid128->uu.uuid128, p_reg->app_uuid128.uu.uuid128, LEN_UUID_128)) {
GATT_TRACE_ERROR("application already registered.");
return 0;
}{...}
}{...}
for (i_gatt_if = 0, p_reg = gatt_cb.cl_rcb; i_gatt_if < GATT_MAX_APPS; i_gatt_if++, p_reg++) {
if (!p_reg->in_use) {
memset(p_reg, 0 , sizeof(tGATT_REG));
i_gatt_if++;
p_reg->app_uuid128 = *p_app_uuid128;
gatt_if =
p_reg->gatt_if = (tGATT_IF)i_gatt_if;
p_reg->app_cb = *p_cb_info;
p_reg->in_use = TRUE;
break;
}{...}
}{...}
GATT_TRACE_API ("allocated gatt_if=%d\n", gatt_if);
return gatt_if;
}{ ... }
/* ... */
void GATT_Deregister (tGATT_IF gatt_if)
{
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb;
tGATT_CLCB *p_clcb;
list_node_t *p_node = NULL;
list_node_t *p_next = NULL;
#if (GATTS_INCLUDED == TRUE)
UINT8 ii;
tGATT_SR_REG *p_sreg;/* ... */
#endif
GATT_TRACE_API ("GATT_Deregister gatt_if=%d", gatt_if);
if ( (gatt_if == 0) || (p_reg == NULL) ) {
GATT_TRACE_ERROR ("GATT_Deregister with invalid gatt_if: %u", gatt_if);
return;
}{...}
/* ... */
#if (GATTS_INCLUDED == TRUE)
for (ii = 0, p_sreg = gatt_cb.sr_reg; ii < GATT_MAX_SR_PROFILES; ii++, p_sreg++) {
if (p_sreg->in_use && (p_sreg->gatt_if == gatt_if)) {
GATTS_StopService(p_sreg->s_hdl);
}{...}
}{...}
gatt_free_srvc_db_buffer_app_id(&p_reg->app_uuid128);/* ... */
#endif
for(p_node = list_begin(gatt_cb.p_tcb_list); p_node; p_node = p_next) {
p_tcb = list_node(p_node);
p_next = list_next(p_node);
if (p_tcb->in_use) {
if (gatt_get_ch_state(p_tcb) != GATT_CH_CLOSE) {
gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
if (!gatt_num_apps_hold_link(p_tcb)) {
gatt_disconnect(p_tcb);
}{...}
}{...}
list_node_t *p_node_clcb = NULL;
list_node_t *p_node_next = NULL;
for(p_node_clcb = list_begin(gatt_cb.p_clcb_list); p_node_clcb; p_node_clcb = p_node_next) {
p_clcb = list_node(p_node_clcb);
p_node_next = list_next(p_node_clcb);
if (p_clcb->in_use &&
(p_clcb->p_reg->gatt_if == gatt_if) &&
(p_clcb->p_tcb->tcb_idx == p_tcb->tcb_idx)) {
btu_stop_timer(&p_clcb->rsp_timer_ent);
gatt_clcb_dealloc (p_clcb);
break;
}{...}
}{...}
}{...}
}{...}
gatt_deregister_bgdev_list(gatt_if);
#if (defined(BLE_PERIPHERAL_MODE_SUPPORT) && (BLE_PERIPHERAL_MODE_SUPPORT == TRUE))
GATT_Listen(gatt_if, FALSE, NULL);
#endif
memset (p_reg, 0, sizeof(tGATT_REG));
}{ ... }
/* ... */
void GATT_StartIf (tGATT_IF gatt_if)
{
tGATT_REG *p_reg;
tGATT_TCB *p_tcb;
BD_ADDR bda;
UINT8 start_idx, found_idx;
UINT16 conn_id;
tGATT_TRANSPORT transport ;
GATT_TRACE_API ("GATT_StartIf gatt_if=%d", gatt_if);
if ((p_reg = gatt_get_regcb(gatt_if)) != NULL) {
start_idx = 0;
while (gatt_find_the_connected_bda(start_idx, bda, &found_idx, &transport)) {
p_tcb = gatt_find_tcb_by_addr(bda, transport);
if (p_reg->app_cb.p_conn_cb && p_tcb) {
conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
(*p_reg->app_cb.p_conn_cb)(gatt_if, bda, conn_id, TRUE, 0, transport);
}{...}
start_idx = ++found_idx;
}{...}
}{...}
}{ ... }
/* ... */
BOOLEAN GATT_Connect (tGATT_IF gatt_if, BD_ADDR bd_addr, tBLE_ADDR_TYPE bd_addr_type,
BOOLEAN is_direct, tBT_TRANSPORT transport, BOOLEAN is_aux)
{
tGATT_REG *p_reg;
BOOLEAN status = FALSE;
GATT_TRACE_API ("GATT_Connect gatt_if=%d", gatt_if);
if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
GATT_TRACE_ERROR("GATT_Connect - gatt_if =%d is not registered", gatt_if);
return (FALSE);
}{...}
if (is_direct) {
status = gatt_act_connect (p_reg, bd_addr, bd_addr_type, transport, is_aux);
}{...} else {
if (transport == BT_TRANSPORT_LE) {
status = gatt_update_auto_connect_dev(gatt_if, TRUE, bd_addr, TRUE);
}{...} else {
GATT_TRACE_ERROR("Unsupported transport for background connection");
}{...}
}{...}
return status;
}{ ... }
/* ... */
BOOLEAN GATT_CancelConnect (tGATT_IF gatt_if, BD_ADDR bd_addr, BOOLEAN is_direct)
{
tGATT_REG *p_reg;
tGATT_TCB *p_tcb;
BOOLEAN status = TRUE;
tGATT_IF temp_gatt_if;
UINT8 start_idx, found_idx;
GATT_TRACE_API ("GATT_CancelConnect gatt_if=%d", gatt_if);
if ((gatt_if != 0) && ((p_reg = gatt_get_regcb(gatt_if)) == NULL)) {
GATT_TRACE_ERROR("GATT_CancelConnect - gatt_if =%d is not registered", gatt_if);
return (FALSE);
}{...}
if (is_direct) {
if (!gatt_if) {
GATT_TRACE_DEBUG("GATT_CancelConnect - unconditional");
start_idx = 0;
p_tcb = gatt_find_tcb_by_addr(bd_addr, BT_TRANSPORT_LE);
if (p_tcb && gatt_num_apps_hold_link(p_tcb)) {
while (status && gatt_find_app_hold_link(p_tcb, start_idx, &found_idx, &temp_gatt_if)) {
status = gatt_cancel_open(temp_gatt_if, bd_addr);
start_idx = ++found_idx;
}{...}
}{...} else {
GATT_TRACE_ERROR("GATT_CancelConnect - no app found");
status = FALSE;
}{...}
}{...} else {
status = gatt_cancel_open(gatt_if, bd_addr);
}{...}
}{...} else {
if (!gatt_if) {
if (gatt_get_num_apps_for_bg_dev(bd_addr)) {
while (gatt_find_app_for_bg_dev(bd_addr, &temp_gatt_if)) {
gatt_remove_bg_dev_for_app(temp_gatt_if, bd_addr);
}{...}
}{...} else {
GATT_TRACE_ERROR("GATT_CancelConnect -no app associated with the bg device for unconditional removal");
status = FALSE;
}{...}
}{...} else {
status = gatt_remove_bg_dev_for_app(gatt_if, bd_addr);
}{...}
}{...}
return status;
}{ ... }
/* ... */
tGATT_STATUS GATT_Disconnect (UINT16 conn_id)
{
tGATT_STATUS ret = GATT_ILLEGAL_PARAMETER;
tGATT_TCB *p_tcb = NULL;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
GATT_TRACE_API ("GATT_Disconnect conn_id=%d ", conn_id);
p_tcb = gatt_get_tcb_by_idx(tcb_idx);
if (p_tcb) {
gatt_update_app_use_link_flag(gatt_if, p_tcb, FALSE, FALSE);
if (!gatt_num_apps_hold_link(p_tcb)) {
gatt_disconnect(p_tcb);
}{...}
ret = GATT_SUCCESS;
}{...}
return ret;
}{ ... }
/* ... */
tGATT_STATUS GATT_SendServiceChangeIndication (BD_ADDR bd_addr)
{
UINT8 start_idx, found_idx;
BOOLEAN srv_chg_ind_pending = FALSE;
tGATT_TCB *p_tcb;
tBT_TRANSPORT transport;
tGATT_STATUS status = GATT_NOT_FOUND;
if (gatt_cb.srv_chg_mode == GATTS_SEND_SERVICE_CHANGE_AUTO) {
status = GATT_WRONG_STATE;
GATT_TRACE_ERROR ("%s can't send service change indication manually, please configure the option through menuconfig", __func__);
return status;
}{...}
if(bd_addr) {
status = gatt_send_srv_chg_ind(bd_addr);
}{...} else {
start_idx = 0;
BD_ADDR addr;
while (gatt_find_the_connected_bda(start_idx, addr, &found_idx, &transport)) {
p_tcb = gatt_get_tcb_by_idx(found_idx);
srv_chg_ind_pending = gatt_is_srv_chg_ind_pending(p_tcb);
if (!srv_chg_ind_pending) {
status = gatt_send_srv_chg_ind(addr);
}{...} else {
status = GATT_BUSY;
GATT_TRACE_DEBUG("discard srv chg - already has one in the queue");
}{...}
start_idx = ++found_idx;
}{...}
}{...}
return status;
}{ ... }
/* ... */
BOOLEAN GATT_GetConnectionInfor(UINT16 conn_id, tGATT_IF *p_gatt_if, BD_ADDR bd_addr,
tBT_TRANSPORT *p_transport)
{
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
BOOLEAN status = FALSE;
GATT_TRACE_API ("GATT_GetConnectionInfor conn_id=%d", conn_id);
if (p_tcb && p_reg ) {
memcpy(bd_addr, p_tcb->peer_bda, BD_ADDR_LEN);
*p_gatt_if = gatt_if;
*p_transport = p_tcb->transport;
status = TRUE;
}{...}
return status;
}{ ... }
/* ... */
BOOLEAN GATT_GetConnIdIfConnected(tGATT_IF gatt_if, BD_ADDR bd_addr, UINT16 *p_conn_id,
tBT_TRANSPORT transport)
{
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb = gatt_find_tcb_by_addr(bd_addr, transport);
BOOLEAN status = FALSE;
if (p_reg && p_tcb && (gatt_get_ch_state(p_tcb) == GATT_CH_OPEN) ) {
*p_conn_id = GATT_CREATE_CONN_ID(p_tcb->tcb_idx, gatt_if);
status = TRUE;
}{...}
GATT_TRACE_API ("GATT_GetConnIdIfConnected status=%d\n", status);
return status;
}{ ... }
/* ... */
BOOLEAN GATT_Listen (tGATT_IF gatt_if, BOOLEAN start, BD_ADDR_PTR bd_addr)
{
tGATT_REG *p_reg;
GATT_TRACE_API ("GATT_Listen gatt_if=%d", gatt_if);
if ((p_reg = gatt_get_regcb(gatt_if)) == NULL) {
GATT_TRACE_ERROR("GATT_Listen - gatt_if =%d is not registered", gatt_if);
return (FALSE);
}{...}
if (bd_addr != NULL) {
gatt_update_auto_connect_dev(gatt_if, start, bd_addr, FALSE);
}{...} else {
p_reg->listening = start ? GATT_LISTEN_TO_ALL : GATT_LISTEN_TO_NONE;
}{...}
return gatt_update_listen_mode();
}{ ... }
tGATT_STATUS GATTS_SetServiceChangeMode(UINT8 mode)
{
if (mode > GATTS_SEND_SERVICE_CHANGE_MANUAL) {
GATT_TRACE_ERROR("%s invalid service change mode %u", __func__, mode);
return GATT_VALUE_NOT_ALLOWED;
}{...}
gatt_cb.srv_chg_mode = mode;
return GATT_SUCCESS;
}{ ... }
tGATT_STATUS GATTS_HandleMultiValueNotification (UINT16 conn_id, tGATT_HLV *tuples, UINT16 num_tuples)
{
tGATT_STATUS cmd_sent = GATT_ILLEGAL_PARAMETER;
BT_HDR *p_buf;
tGATT_VALUE notif;
tGATT_IF gatt_if = GATT_GET_GATT_IF(conn_id);
UINT8 tcb_idx = GATT_GET_TCB_IDX(conn_id);
tGATT_REG *p_reg = gatt_get_regcb(gatt_if);
tGATT_TCB *p_tcb = gatt_get_tcb_by_idx(tcb_idx);
UINT8 *p = notif.value;
tGATT_HLV *p_hlv = tuples;
GATT_TRACE_API ("GATTS_HandleMultiValueNotification");
if ( (p_reg == NULL) || (p_tcb == NULL)) {
GATT_TRACE_ERROR ("GATTS_HandleMultiValueNotification Unknown conn_id: %u \n", conn_id);
return (tGATT_STATUS) GATT_INVALID_CONN_ID;
}{...}
if (!gatt_check_connection_state_by_tcb(p_tcb)) {
GATT_TRACE_ERROR("connection not established\n");
return GATT_WRONG_STATE;
}{...}
if (tuples == NULL) {
return GATT_ILLEGAL_PARAMETER;
}{...}
notif.len = 0;
while (num_tuples) {
if (!GATT_HANDLE_IS_VALID (p_hlv->handle)) {
return GATT_ILLEGAL_PARAMETER;
}{...}
UINT16_TO_STREAM(p, p_hlv->handle);
UINT16_TO_STREAM(p, p_hlv->length);
memcpy (p, p_hlv->value, p_hlv->length);
GATT_TRACE_DEBUG("%s handle %x, length %u", __func__, p_hlv->handle, p_hlv->length);
p += p_hlv->length;
notif.len += 4 + p_hlv->length;
num_tuples--;
p_hlv++;
}{...}
notif.auth_req = GATT_AUTH_REQ_NONE;
p_buf = attp_build_sr_msg (p_tcb, GATT_HANDLE_MULTI_VALUE_NOTIF, (tGATT_SR_MSG *)¬if);
if (p_buf != NULL) {
cmd_sent = attp_send_sr_msg (p_tcb, p_buf);
}{...} else {
cmd_sent = GATT_NO_RESOURCES;
}{...}
return cmd_sent;
}{ ... }
tGATT_STATUS GATTS_ShowLocalDatabase(void)
{
gatts_show_local_database();
return GATT_SUCCESS;
}{ ... }
...#endif/* ... */