1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
90
91
92
93
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
119
120
131
132
138
139
146
147
151
152
153
154
155
156
157
158
159
160
161
162
172
173
174
175
176
177
178
179
183
184
185
186
191
192
193
197
198
199
200
201
202
207
208
209
210
211
212
213
214
218
219
220
221
222
225
226
227
228
229
232
233
234
235
236
237
238
239
246
249
250
251
252
253
256
257
258
259
260
261
262
263
264
272
273
276
277
278
279
280
283
284
288
289
290
291
292
310
311
312
329
330
331
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
427
428
429
430
431
432
435
436
437
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
516
517
522
523
536
537
540
541
544
545
550
551
556
557
561
562
565
566
577
578
586
587
590
591
592
596
597
601
602
606
607
612
613
616
617
620
621
622
628
629
632
633
634
640
641
647
648
654
655
661
662
698
699
700
707
708
713
714
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
810
811
812
813
814
815
816
819
820
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
838
839
840
841
842
843
844
845
846
847
848
849
850
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
882
883
884
885
886
887
888
889
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
944
945
949
950
951
956
957
958
963
964
965
969
970
971
976
977
982
983
988
989
990
991
992
993
994
995
1001
1002
1003
1004
1005
1006
1009
1010
1019
1020
1023
1024
1027
1028
1031
1032
1039
1040
1043
1044
1047
1048
1049
1050
1051
1052
1056
1057
1058
1059
1060
1061
1062
1066
1067
1068
1069
1070
1071
1081
1082
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1129
1130
1131
1132
1133
1142
1143
1144
1145
1146
1147
1157
1158
1163
1164
1165
1175
1176
1177
1178
1179
1184
1189
1194
1199
1204
1209
1214
1219
1224
1229
1235
1241
1246
1251
1256
1261
1266
1271
1277
1282
1283
1284
1285
1286
1287
1288
1289
1294
1299
1304
1309
1314
1320
1321
1322
1336
1337
1338
1339
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1361
1362
1363
1366
1367
1368
1369
1370
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1411
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1431
1432
1433
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1452
1453
1454
1455
1456
1457
1458
1459
1470
1471
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1549
1550
1551
1552
1553
1554
1555
1556
1557
1566
1567
1568
1569
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1589
1591
1592
1593
1594
1595
1596
1597
1598
1608
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1638
1639
1640
1641
1642
1643
1649
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
1686
1687
1688
1689
1694
1695
1696
1697
1698
1707
1708
1709
1710
1712
1713
1714
1715
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1752
1753
1754
1755
1756
1757
1758
1759
1765
1766
1767
1768
1779
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1804
1807
1810
1811
1812
1813
1814
1815
1821
1822
1823
1824
1825
1826
1827
1828
1829
1832
1833
1834
1835
1836
1837
1838
1863
1864
1865
1866
1867
1868
1869
1873
1874
1875
1876
1882
1883
1884
1885
1886
1887
1888
1889
1890
1892
1893
1894
1895
1896
1897
1906
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1932
1933
1934
1935
1936
1937
1940
1943
1946
1949
1952
1953
1954
1955
1967
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1991
1992
1996
2004
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2022
2023
2024
2025
2026
2027
2028
2031
2034
2037
2040
2043
2046
2049
2052
2055
2058
2061
2064
2067
2070
2073
2076
2079
2083
2087
2091
2092
2093
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2113
2114
2115
2116
2119
2120
2121
2122
2123
2124
2125
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2165
2166
2167
2171
2178
2180
2181
2182
2183
2184
2185
2194
2195
2199
2200
2201
2202
2211
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2239
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2394
2395
2398
2399
2400
2401
2402
2405
2406
2407
2408
2409
2410
2411
2412
2413
2419
2420
2421
2422
2423
2424
2425
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2479
2480
2481
2482
2483
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2498
2499
2502
2503
2504
2505
2506
2509
2510
2511
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2529
2530
2531
2535
2536
2537
2538
2539
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2585
2586
2587
2588
2589
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2609
2610
2613
2614
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2656
2657
2658
2659
2660
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2675
2676
2679
2680
2681
2682
2683
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2700
2701
2702
2703
2704
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2721
2722
2725
2726
2729
2730
2731
2732
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2749
2750
2751
2752
2755
2756
2757
2758
2759
2760
2761
2762
2765
2766
2767
2768
2771
2772
2773
2774
2775
2776
2777
2778
2788
2789
2790
2791
2792
2793
2794
2795
2800
2801
2802
2811
2812
2824
2825
2826
2827
2828
2831
2832
2838
2839
2840
2841
2842
2843
2846
2847
2853
2854
2855
2856
2857
2858
2861
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2877
2878
2881
2882
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2912
2913
2923
2924
2925
2926
2927
2928
2929
2930
2934
2935
2936
2937
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2993
2994
2995
2996
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3014
3015
3016
3017
3018
3019
3023
3024
3027
3028
3029
3030
3034
3037
3038
3039
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3058
3059
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3083
3084
3094
3095
3096
3097
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3123
3124
3125
3126
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3169
3170
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3200
3201
3202
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3269
3270
3271
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3330
3337
3344
3347
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3366
3367
3370
3371
3372
3373
3374
3375
3376
3377
3378
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3429
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3443
3444
3447
3448
3449
3450
3451
3454
3455
3458
3459
3460
3461
3462
3469
3470
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
/* ... */
#define BTSTACK_FILE__ "gatt_client.c"
#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include "btstack_config.h"
#include "ble/att_dispatch.h"
#include "ble/att_db.h"
#include "ble/gatt_client.h"
#include "ble/le_device_db.h"
#include "ble/sm.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
#include "btstack_run_loop.h"
#include "btstack_util.h"
#include "hci.h"
#include "hci_dump.h"
#include "l2cap.h"
#include "classic/sdp_client.h"
#include "bluetooth_gatt.h"
#include "bluetooth_sdp.h"
#include "classic/sdp_util.h"
21 includes
#define GATT_CLIENT_COLLISION_BACKOFF_MS 150
static btstack_linked_list_t gatt_client_connections;
static btstack_linked_list_t gatt_client_value_listeners;
static btstack_packet_callback_registration_t hci_event_callback_registration;
static btstack_packet_callback_registration_t sm_event_callback_registration;
static btstack_context_callback_registration_t gatt_client_deferred_event_emit;
static bool gatt_client_mtu_exchange_enabled;
static gap_security_level_t gatt_client_required_security_level;
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code);
#ifdef ENABLE_LE_SIGNED_WRITE
static void att_signed_write_handle_cmac_result(uint8_t hash[8]);
#endif
#ifdef ENABLE_GATT_OVER_CLASSIC
static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid);
static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status);
static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client);
static void gatt_client_classic_retry(btstack_timer_source_t * ts);/* ... */
#endif
#ifdef ENABLE_GATT_OVER_EATT
static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client);
static void gatt_client_le_enhanced_retry(btstack_timer_source_t * ts);/* ... */
#endif
void gatt_client_init(void){
gatt_client_connections = NULL;
gatt_client_mtu_exchange_enabled = true;
gatt_client_required_security_level = LEVEL_0;
hci_event_callback_registration.callback = &gatt_client_event_packet_handler;
hci_add_event_handler(&hci_event_callback_registration);
sm_event_callback_registration.callback = &gatt_client_event_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
att_dispatch_register_client(gatt_client_att_packet_handler);
}{ ... }
void gatt_client_set_required_security_level(gap_security_level_t level){
gatt_client_required_security_level = level;
}{ ... }
static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_connections);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (&gatt_client->gc_timeout == ts) {
return gatt_client;
}if (&gatt_client->gc_timeout == ts) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
gatt_client_t * gatt_client = gatt_client_for_timer(timer);
if (gatt_client == NULL) return;
log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle);
gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT);
}{ ... }
static void gatt_client_timeout_start(gatt_client_t * gatt_client){
log_info("GATT client timeout start, handle 0x%02x", gatt_client->con_handle);
btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler);
btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000);
btstack_run_loop_add_timer(&gatt_client->gc_timeout);
}{ ... }
static void gatt_client_timeout_stop(gatt_client_t * gatt_client){
log_info("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle);
btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
}{ ... }
static gap_security_level_t gatt_client_le_security_level_for_connection(hci_con_handle_t con_handle){
uint8_t encryption_key_size = gap_encryption_key_size(con_handle);
if (encryption_key_size == 0) return LEVEL_0;
bool authenticated = gap_authenticated(con_handle);
if (!authenticated) return LEVEL_2;
return encryption_key_size == 16 ? LEVEL_4 : LEVEL_3;
}{ ... }
static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
gatt_client_t * gatt_client = (gatt_client_t *) it;
if (gatt_client->con_handle == handle){
return gatt_client;
}if (gatt_client->con_handle == handle) { ... }
}for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) { ... }
return NULL;
}{ ... }
static uint8_t gatt_client_provide_context_for_handle(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client != NULL){
*out_gatt_client = gatt_client;
return ERROR_CODE_SUCCESS;
}if (gatt_client != NULL) { ... }
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
if (hci_connection == NULL){
log_error("No connection for handle 0x%04x", con_handle);
*out_gatt_client = NULL;
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (hci_connection == NULL) { ... }
gatt_client = btstack_memory_gatt_client_get();
if (gatt_client == NULL){
*out_gatt_client = NULL;
return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
}if (gatt_client == NULL) { ... }
gatt_client->bearer_type = ATT_BEARER_UNENHANCED_LE;
gatt_client->con_handle = con_handle;
gatt_client->mtu = ATT_DEFAULT_MTU;
gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
if (gatt_client_mtu_exchange_enabled){
gatt_client->mtu_state = SEND_MTU_EXCHANGE;
}if (gatt_client_mtu_exchange_enabled) { ... } else {
gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
}else { ... }
gatt_client->state = P_READY;
#ifdef ENABLE_GATT_OVER_EATT
gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
#endif
btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
if (hci_connection->att_connection.mtu_exchanged){
gatt_client->mtu = hci_connection->att_connection.mtu;
gatt_client->mtu_state = MTU_EXCHANGED;
}if (hci_connection->att_connection.mtu_exchanged) { ... }
*out_gatt_client = gatt_client;
return ERROR_CODE_SUCCESS;
}{ ... }
static bool is_ready(gatt_client_t * gatt_client){
return gatt_client->state == P_READY;
}{ ... }
static uint8_t gatt_client_provide_context_for_request(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
gatt_client_t * gatt_client = NULL;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
#ifdef ENABLE_GATT_OVER_EATT
if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY){
btstack_linked_list_iterator_t it;
gatt_client_t * eatt_client = NULL;
btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_t * client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (client->state == P_READY){
eatt_client = client;
break;
}if (client->state == P_READY) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (eatt_client == NULL){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (eatt_client == NULL) { ... }
gatt_client = eatt_client;
}if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) { ... }
/* ... */#endif
if (is_ready(gatt_client) == false){
return GATT_CLIENT_IN_WRONG_STATE;
}if (is_ready(gatt_client) == false) { ... }
gatt_client_timeout_start(gatt_client);
*out_gatt_client = gatt_client;
return status;
}{ ... }
int gatt_client_is_ready(hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return 0;
}if (status != ERROR_CODE_SUCCESS) { ... }
return is_ready(gatt_client) ? 1 : 0;
}{ ... }
void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){
gatt_client_mtu_exchange_enabled = enabled != 0;
}{ ... }
uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){
*mtu = gatt_client->mtu;
return ERROR_CODE_SUCCESS;
}if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)) { ... }
*mtu = ATT_DEFAULT_MTU;
return GATT_CLIENT_IN_WRONG_STATE;
}{ ... }
static uint8_t *gatt_client_reserve_request_buffer(gatt_client_t *gatt_client) {
switch (gatt_client->bearer_type){
#ifdef ENABLE_GATT_OVER_CLASSIC
case ATT_BEARER_UNENHANCED_CLASSIC:
#endifcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_UNENHANCED_LE:
l2cap_reserve_packet_buffer();
return l2cap_get_outgoing_buffer();
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_ENHANCED_LE:
return gatt_client->eatt_storage_buffer;/* ... */
#endifcase ATT_BEARER_ENHANCED_LE:
default:
btstack_unreachable();
break;default
}switch (gatt_client->bearer_type) { ... }
return NULL;
}{ ... }
static uint8_t gatt_client_send(gatt_client_t * gatt_client, uint16_t len){
switch (gatt_client->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
return l2cap_send_prepared_connectionless(gatt_client->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, len);
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
return l2cap_send_prepared(gatt_client->l2cap_cid, len);/* ... */
#endif
#ifdef ENABLE_GATT_OVER_EATTcase ATT_BEARER_UNENHANCED_CLASSIC:
case ATT_BEARER_ENHANCED_LE:
return l2cap_send(gatt_client->l2cap_cid, gatt_client->eatt_storage_buffer, len);/* ... */
#endifcase ATT_BEARER_ENHANCED_LE:
default:
btstack_unreachable();
return ERROR_CODE_HARDWARE_FAILURE;default
}switch (gatt_client->bearer_type) { ... }
}{ ... }
static uint8_t att_confirmation(gatt_client_t * gatt_client) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = ATT_HANDLE_VALUE_CONFIRMATION;
return gatt_client_send(gatt_client, 1);
}{ ... }
static uint8_t att_find_information_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t start_handle,
uint16_t end_handle) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, start_handle);
little_endian_store_16(request, 3, end_handle);
return gatt_client_send(gatt_client, 5);
}{ ... }
static uint8_t
att_find_by_type_value_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_group_type,
uint16_t start_handle, uint16_t end_handle, uint8_t *value, uint16_t value_size) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, start_handle);
little_endian_store_16(request, 3, end_handle);
little_endian_store_16(request, 5, attribute_group_type);
(void)memcpy(&request[7], value, value_size);
return gatt_client_send(gatt_client, 7u + value_size);
}{ ... }
static uint8_t
att_read_by_type_or_group_request_for_uuid16(gatt_client_t *gatt_client, uint8_t request_type, uint16_t uuid16,
uint16_t start_handle, uint16_t end_handle) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, start_handle);
little_endian_store_16(request, 3, end_handle);
little_endian_store_16(request, 5, uuid16);
return gatt_client_send(gatt_client, 7);
}{ ... }
static uint8_t
att_read_by_type_or_group_request_for_uuid128(gatt_client_t *gatt_client, uint8_t request_type, const uint8_t *uuid128,
uint16_t start_handle, uint16_t end_handle) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, start_handle);
little_endian_store_16(request, 3, end_handle);
reverse_128(uuid128, &request[5]);
return gatt_client_send(gatt_client, 21);
}{ ... }
static uint8_t att_read_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, attribute_handle);
return gatt_client_send(gatt_client, 3);
}{ ... }
static uint8_t att_read_blob_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
uint16_t value_offset) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, attribute_handle);
little_endian_store_16(request, 3, value_offset);
return gatt_client_send(gatt_client, 5);
}{ ... }
static uint8_t
att_read_multiple_request_with_opcode(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles, uint8_t opcode) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = opcode;
uint16_t i;
uint16_t offset = 1;
for (i=0;i<num_value_handles;i++){
little_endian_store_16(request, offset, value_handles[i]);
offset += 2;
}for (i=0;i
return gatt_client_send(gatt_client, offset);
}{ ... }
static uint8_t
att_read_multiple_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_REQUEST);
}{ ... }
#ifdef ENABLE_GATT_OVER_EATT
static uint8_t
att_read_multiple_variable_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
return att_read_multiple_request_with_opcode(gatt_client, num_value_handles, value_handles, ATT_READ_MULTIPLE_VARIABLE_REQ);
}att_read_multiple_variable_request (gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) { ... }
/* ... */#endif
#ifdef ENABLE_LE_SIGNED_WRITE
static uint8_t att_signed_write_request(gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle,
uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, attribute_handle);
(void)memcpy(&request[3], value, value_length);
little_endian_store_32(request, 3 + value_length, sign_counter);
reverse_64(sgn, &request[3 + value_length + 4]);
return gatt_client_send(gatt_client, 3 + value_length + 12);
}att_signed_write_request (gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle, uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) { ... }
/* ... */#endif
static uint8_t
att_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, uint16_t value_length,
uint8_t *value) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, attribute_handle);
(void)memcpy(&request[3], value, value_length);
return gatt_client_send(gatt_client, 3u + value_length);
}{ ... }
static uint8_t att_execute_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint8_t execute_write) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
request[1] = execute_write;
return gatt_client_send(gatt_client, 2);
}{ ... }
static uint8_t att_prepare_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
uint16_t value_offset, uint16_t blob_length, uint8_t *value) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = request_type;
little_endian_store_16(request, 1, attribute_handle);
little_endian_store_16(request, 3, value_offset);
(void)memcpy(&request[5], &value[value_offset], blob_length);
return gatt_client_send(gatt_client, 5u + blob_length);
}{ ... }
static uint8_t att_exchange_mtu_request(gatt_client_t *gatt_client) {
uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
request[0] = ATT_EXCHANGE_MTU_REQUEST;
uint16_t mtu = l2cap_max_le_mtu();
little_endian_store_16(request, 1, mtu);
return gatt_client_send(gatt_client, 3);
}{ ... }
static uint16_t write_blob_length(gatt_client_t * gatt_client){
uint16_t max_blob_length = gatt_client->mtu - 5u;
if (gatt_client->attribute_offset >= gatt_client->attribute_length) {
return 0;
}if (gatt_client->attribute_offset >= gatt_client->attribute_length) { ... }
uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset;
if (max_blob_length > rest_length){
return rest_length;
}if (max_blob_length > rest_length) { ... }
return max_blob_length;
}{ ... }
static void send_gatt_services_request(gatt_client_t *gatt_client){
att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_GROUP_TYPE_REQUEST,
gatt_client->uuid16, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}{ ... }
static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){
if (gatt_client->uuid16 != 0u){
uint8_t uuid16[2];
little_endian_store_16(uuid16, 0, gatt_client->uuid16);
att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2);
return;
}if (gatt_client->uuid16 != 0u) { ... }
uint8_t uuid128[16];
reverse_128(gatt_client->uuid128, uuid128);
att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16);
}{ ... }
static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){
send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID);
}{ ... }
static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){
att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->query_start_handle);
}{ ... }
static void send_gatt_included_service_request(gatt_client_t *gatt_client){
att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
GATT_INCLUDE_SERVICE_UUID, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}{ ... }
static void send_gatt_characteristic_request(gatt_client_t *gatt_client){
att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
GATT_CHARACTERISTICS_UUID, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}{ ... }
static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){
att_find_information_request(gatt_client, ATT_FIND_INFORMATION_REQUEST, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}{ ... }
static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){
att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
}{ ... }
static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){
if (gatt_client->uuid16 != 0u){
att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
gatt_client->uuid16, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}if (gatt_client->uuid16 != 0u) { ... } else {
att_read_by_type_or_group_request_for_uuid128(gatt_client, ATT_READ_BY_TYPE_REQUEST,
gatt_client->uuid128, gatt_client->start_group_handle,
gatt_client->end_group_handle);
}else { ... }
}{ ... }
static void send_gatt_read_blob_request(gatt_client_t *gatt_client){
if (gatt_client->attribute_offset == 0){
att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
}if (gatt_client->attribute_offset == 0) { ... } else {
att_read_blob_request(gatt_client, ATT_READ_BLOB_REQUEST, gatt_client->attribute_handle,
gatt_client->attribute_offset);
}else { ... }
}{ ... }
static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){
att_read_multiple_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
}{ ... }
#ifdef ENABLE_GATT_OVER_EATT
static void send_gatt_read_multiple_variable_request(gatt_client_t * gatt_client){
att_read_multiple_variable_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
}send_gatt_read_multiple_variable_request (gatt_client_t * gatt_client) { ... }
/* ... */#endif
static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){
att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->attribute_handle, gatt_client->attribute_length,
gatt_client->attribute_value);
}{ ... }
static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){
att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->client_characteristic_configuration_handle, 2,
gatt_client->client_characteristic_configuration_value);
}{ ... }
static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){
att_prepare_write_request(gatt_client, ATT_PREPARE_WRITE_REQUEST, gatt_client->attribute_handle,
gatt_client->attribute_offset, write_blob_length(gatt_client),
gatt_client->attribute_value);
}{ ... }
static void send_gatt_execute_write_request(gatt_client_t * gatt_client){
att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 1);
}{ ... }
static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){
att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 0);
}{ ... }
#ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){
att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
GATT_CLIENT_CHARACTERISTICS_CONFIGURATION,
gatt_client->start_group_handle, gatt_client->end_group_handle);
}{ ... }
/* ... */#endif
static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){
att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
}{ ... }
#ifdef ENABLE_LE_SIGNED_WRITE
static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){
att_signed_write_request(gatt_client, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle,
gatt_client->attribute_length, gatt_client->attribute_value, sign_counter,
gatt_client->cmac);
}send_gatt_signed_write_request (gatt_client_t * gatt_client, uint32_t sign_counter) { ... }
/* ... */#endif
static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
if (size < 2) return 0xffff;
uint8_t attr_length = packet[1];
if ((2 + attr_length) > size) return 0xffff;
return little_endian_read_16(packet, size - attr_length + 2u);
}{ ... }
static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){
if (size < 2) return 0xffff;
uint8_t attr_length = packet[1];
if ((2 + attr_length) > size) return 0xffff;
return little_endian_read_16(packet, size - attr_length + 3u);
}{ ... }
static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){
if (size < 2) return 0xffff;
uint8_t attr_length = packet[1];
if ((2 + attr_length) > size) return 0xffff;
return little_endian_read_16(packet, size - attr_length);
}{ ... }
static void gatt_client_notify_can_send_query(gatt_client_t * gatt_client){
#ifdef ENABLE_GATT_OVER_EATT
if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_t * client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (client->state == P_READY){
btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
if (callback == NULL) {
return;
}if (callback == NULL) { ... }
(*callback->callback)(callback->context);
}if (client->state == P_READY) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return;
}if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) { ... }
/* ... */#endif
while (gatt_client->state == P_READY){
#ifdef ENABLE_GATT_OVER_EATT
bool query_sent = gatt_client_le_enhanced_handle_can_send_query(gatt_client);
if (query_sent){
continue;
}if (query_sent) { ... }
/* ... */#endif
btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
if (callback == NULL) {
return;
}if (callback == NULL) { ... }
(*callback->callback)(callback->context);
}while (gatt_client->state == P_READY) { ... }
}{ ... }
static bool gatt_client_accept_server_message(gatt_client_t *gatt_client) {
if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
return !gap_bonded(gatt_client->con_handle) || (gap_encryption_key_size(gatt_client->con_handle) > 0);
}{ ... }
static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
if (!callback) return;
hci_dump_btstack_event(packet, size);
(*callback)(HCI_EVENT_PACKET, 0, packet, size);
}{ ... }
static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it);
if ((notification->con_handle != GATT_CLIENT_ANY_CONNECTION) && (notification->con_handle != con_handle)) continue;
if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue;
(*notification->callback)(HCI_EVENT_PACKET, 0, packet, size);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}{ ... }
static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){
uint8_t packet[5];
packet[0] = GATT_EVENT_QUERY_COMPLETE;
packet[1] = 3;
little_endian_store_16(packet, 2, gatt_client->con_handle);
packet[4] = att_status;
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
uint8_t packet[24];
packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT;
packet[1] = sizeof(packet) - 2u;
little_endian_store_16(packet, 2, gatt_client->con_handle);
little_endian_store_16(packet, 4, start_group_handle);
little_endian_store_16(packet, 6, end_group_handle);
reverse_128(uuid128, &packet[8]);
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void emit_gatt_included_service_query_result_event(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
uint8_t packet[26];
packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT;
packet[1] = sizeof(packet) - 2u;
little_endian_store_16(packet, 2, gatt_client->con_handle);
little_endian_store_16(packet, 4, include_handle);
little_endian_store_16(packet, 6, start_group_handle);
little_endian_store_16(packet, 8, end_group_handle);
reverse_128(uuid128, &packet[10]);
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void emit_gatt_characteristic_query_result_event(gatt_client_t * gatt_client, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle,
uint16_t properties, const uint8_t * uuid128){
uint8_t packet[28];
packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT;
packet[1] = sizeof(packet) - 2u;
little_endian_store_16(packet, 2, gatt_client->con_handle);
little_endian_store_16(packet, 4, start_handle);
little_endian_store_16(packet, 6, value_handle);
little_endian_store_16(packet, 8, end_handle);
little_endian_store_16(packet, 10, properties);
reverse_128(uuid128, &packet[12]);
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void emit_gatt_all_characteristic_descriptors_result_event(
gatt_client_t * gatt_client, uint16_t descriptor_handle, const uint8_t * uuid128){
uint8_t packet[22];
packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT;
packet[1] = sizeof(packet) - 2u;
little_endian_store_16(packet, 2, gatt_client->con_handle);
little_endian_store_16(packet, 4, descriptor_handle);
reverse_128(uuid128, &packet[6]);
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){
uint8_t packet[6];
packet[0] = GATT_EVENT_MTU;
packet[1] = sizeof(packet) - 2u;
little_endian_store_16(packet, 2, gatt_client->con_handle);
little_endian_store_16(packet, 4, new_mtu);
att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu);
emit_event_new(gatt_client->callback, packet, sizeof(packet));
}{ ... }
static void gatt_client_handle_transaction_complete(gatt_client_t *gatt_client, uint8_t att_status) {
gatt_client->state = P_READY;
gatt_client_timeout_stop(gatt_client);
emit_gatt_complete_event(gatt_client, att_status);
gatt_client_notify_can_send_query(gatt_client);
}{ ... }
#define CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 8
static uint8_t * setup_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * value, uint16_t length){
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
static uint8_t packet[1000];
memcpy(&packet[8], value, length);/* ... */
#else
uint8_t * packet = value - CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE;/* ... */
#endif
packet[0] = type;
packet[1] = CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length;
little_endian_store_16(packet, 2, con_handle);
little_endian_store_16(packet, 4, attribute_handle);
little_endian_store_16(packet, 6, length);
return packet;
}{ ... }
#define LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE 10
static uint8_t * setup_long_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * value, uint16_t length){
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
return NULL;/* ... */
#endif
#if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 8)
uint8_t * packet = value - LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE;
packet[0] = type;
packet[1] = LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE - 2 + length;
little_endian_store_16(packet, 2, con_handle);
little_endian_store_16(packet, 4, attribute_handle);
little_endian_store_16(packet, 6, offset);
little_endian_store_16(packet, 8, length);
return packet;/* ... */
#else
log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads");
return NULL;/* ... */
#endif
}{ ... }
#if (LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE > CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE)
#define REPORT_PREBUFFER_HEADER LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE
#else
#define REPORT_PREBUFFER_HEADER CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE
#endif
static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
if (size < 2) return;
uint8_t attr_length = packet[1];
uint8_t uuid_length = attr_length - 4u;
int i;
for (i = 2; (i+attr_length) <= size; i += attr_length){
uint16_t start_group_handle = little_endian_read_16(packet,i);
uint16_t end_group_handle = little_endian_read_16(packet,i+2);
uint8_t uuid128[16];
uint16_t uuid16 = 0;
if (uuid_length == 2u){
uuid16 = little_endian_read_16(packet, i+4);
uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16);
}if (uuid_length == 2u) { ... } else if (uuid_length == 16u) {
reverse_128(&packet[i+4], uuid128);
}else if (uuid_length == 16u) { ... } else {
return;
}else { ... }
emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128);
}for (i = 2; (i+attr_length) <= size; i += attr_length) { ... }
}{ ... }
static void report_gatt_characteristic_start_found(gatt_client_t * gatt_client, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){
uint8_t uuid128[16];
uint16_t uuid16 = 0;
if (uuid_length == 2u){
uuid16 = little_endian_read_16(uuid, 0);
uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16);
}if (uuid_length == 2u) { ... } else if (uuid_length == 16u){
reverse_128(uuid, uuid128);
}else if (uuid_length == 16u) { ... } else {
return;
}else { ... }
if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return;
gatt_client->characteristic_properties = properties;
gatt_client->characteristic_start_handle = start_handle;
gatt_client->attribute_handle = value_handle;
if (gatt_client->filter_with_uuid) return;
gatt_client->uuid16 = uuid16;
(void)memcpy(gatt_client->uuid128, uuid128, 16);
}{ ... }
static void report_gatt_characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){
if (!gatt_client->characteristic_start_handle) return;
emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle,
end_handle, gatt_client->characteristic_properties, gatt_client->uuid128);
gatt_client->characteristic_start_handle = 0;
}{ ... }
static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
if (size < 2u) return;
uint8_t attr_length = packet[1];
if ((attr_length != 7u) && (attr_length != 21u)) return;
uint8_t uuid_length = attr_length - 5u;
int i;
for (i = 2u; (i + attr_length) <= size; i += attr_length){
uint16_t start_handle = little_endian_read_16(packet, i);
uint8_t properties = packet[i+2];
uint16_t value_handle = little_endian_read_16(packet, i+3);
report_gatt_characteristic_end_found(gatt_client, start_handle - 1u);
report_gatt_characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5],
uuid_length);
}for (i = 2u; (i + attr_length) <= size; i += attr_length) { ... }
}{ ... }
static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){
uint8_t normalized_uuid128[16];
uuid_add_bluetooth_prefix(normalized_uuid128, uuid16);
emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
gatt_client->query_end_handle, normalized_uuid128);
}{ ... }
static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, const uint8_t * uuid128){
emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
gatt_client->query_end_handle, uuid128);
}{ ... }
static void report_gatt_notification(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
if (!gatt_client_accept_server_message(gatt_client)) return;
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, gatt_client->con_handle, value_handle, value, length);
emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
}{ ... }
static void report_gatt_indication(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
if (!gatt_client_accept_server_message(gatt_client)) return;
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, gatt_client->con_handle, value_handle, value, length);
emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
}{ ... }
static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value, length);
emit_event_new(gatt_client->callback, packet, CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE + length);
}{ ... }
static void report_gatt_long_characteristic_value_blob(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){
uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value_offset, blob, blob_length);
if (!packet) return;
emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE);
}{ ... }
static void report_gatt_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){
UNUSED(value_offset);
uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value, value_length);
emit_event_new(gatt_client->callback, packet, value_length + 8u);
}{ ... }
static void report_gatt_long_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){
uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value_offset, blob, blob_length);
if (!packet) return;
emit_event_new(gatt_client->callback, packet, blob_length + LONG_CHARACTERISTIC_VALUE_EVENT_HEADER_SIZE);
}{ ... }
static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){
int i;
for (i = 0u; (i + pair_size) <= size; i += pair_size){
uint16_t descriptor_handle = little_endian_read_16(packet,i);
uint8_t uuid128[16];
uint16_t uuid16 = 0;
if (pair_size == 4u){
uuid16 = little_endian_read_16(packet,i+2);
uuid_add_bluetooth_prefix(uuid128, uuid16);
}if (pair_size == 4u) { ... } else {
reverse_128(&packet[i+2], uuid128);
}else { ... }
emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128);
}for (i = 0u; (i + pair_size) <= size; i += pair_size) { ... }
}{ ... }
static bool is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){
return last_result_handle >= gatt_client->end_group_handle;
}{ ... }
static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){
if (is_query_done(gatt_client, last_result_handle)){
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
return;
}if (is_query_done(gatt_client, last_result_handle)) { ... }
gatt_client->start_group_handle = last_result_handle + 1u;
gatt_client->state = next_query_state;
}{ ... }
static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY);
}{ ... }
static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY);
}{ ... }
static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY);
}{ ... }
static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
if (is_query_done(gatt_client, last_result_handle)){
report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle);
}if (is_query_done(gatt_client, last_result_handle)) { ... }
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY);
}{ ... }
static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY);
}{ ... }
static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST);
}{ ... }
static void trigger_next_prepare_write_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, gatt_client_state_t done_state){
gatt_client->attribute_offset += write_blob_length(gatt_client);
uint16_t next_blob_length = write_blob_length(gatt_client);
if (next_blob_length == 0u){
gatt_client->state = done_state;
return;
}if (next_blob_length == 0u) { ... }
gatt_client->state = next_query_state;
}{ ... }
static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){
uint16_t max_blob_length = gatt_client->mtu - 1u;
if (received_blob_length < max_blob_length){
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
return;
}if (received_blob_length < max_blob_length) { ... }
gatt_client->attribute_offset += received_blob_length;
gatt_client->state = next_query_state;
}{ ... }
void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
notification->callback = callback;
notification->con_handle = con_handle;
if (characteristic == NULL){
notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE;
}if (characteristic == NULL) { ... } else {
notification->attribute_handle = characteristic->value_handle;
}else { ... }
btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
}{ ... }
void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
}{ ... }
static bool is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){
uint16_t attribute_handle = little_endian_read_16(packet, 1);
uint16_t value_offset = little_endian_read_16(packet, 3);
if (gatt_client->attribute_handle != attribute_handle) return false;
if (gatt_client->attribute_offset != value_offset) return false;
return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u;
}{ ... }
#ifdef ENABLE_LE_SIGNED_WRITE
static void gatt_client_run_for_client_start_signed_write(gatt_client_t *gatt_client) {
sm_key_t csrk;
le_device_db_local_csrk_get(gatt_client->le_device_index, csrk);
uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
gatt_client->state = P_W4_CMAC_RESULT;
sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, att_signed_write_handle_cmac_result);
}gatt_client_run_for_client_start_signed_write (gatt_client_t *gatt_client) { ... }
/* ... */#endif
static bool gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){
if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
if (gatt_client->reencryption_active) return false;
if (gatt_client->wait_for_authentication_complete) return false;
bool client_request_pending = gatt_client->state != P_READY;
bool check_security;
switch (gatt_client->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
check_security = true;
break;case ATT_BEARER_UNENHANCED_LE:
default:
check_security = false;
break;default
}switch (gatt_client->bearer_type) { ... }
if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security){
log_info("Trigger pairing, current security level %u, required %u\n", gatt_client->security_level, gatt_client_required_security_level);
gatt_client->wait_for_authentication_complete = true;
switch (gatt_client_required_security_level){
case LEVEL_4:
case LEVEL_3:
gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
break;case LEVEL_3:
default:
gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_ENCRYPTION;
break;default
}switch (gatt_client_required_security_level) { ... }
sm_request_pairing(gatt_client->con_handle);
return true;
}if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security) { ... }
switch (gatt_client->mtu_state) {
case SEND_MTU_EXCHANGE:
gatt_client->mtu_state = SENT_MTU_EXCHANGE;
att_exchange_mtu_request(gatt_client);
return true;case SEND_MTU_EXCHANGE:
case SENT_MTU_EXCHANGE:
return false;case SENT_MTU_EXCHANGE:
default:
break;default
}switch (gatt_client->mtu_state) { ... }
if (gatt_client->send_confirmation){
gatt_client->send_confirmation = false;
att_confirmation(gatt_client);
return true;
}if (gatt_client->send_confirmation) { ... }
switch (gatt_client->state){
case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break;
log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
return false;case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
default:
break;default
}switch (gatt_client->state) { ... }
bool packet_sent = true;
bool done = true;
switch (gatt_client->state){
case P_W2_SEND_SERVICE_QUERY:
gatt_client->state = P_W4_SERVICE_QUERY_RESULT;
send_gatt_services_request(gatt_client);
break;
case P_W2_SEND_SERVICE_QUERY:
case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
gatt_client->state = P_W4_SERVICE_WITH_UUID_RESULT;
send_gatt_services_by_uuid_request(gatt_client);
break;
case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
gatt_client->state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
send_gatt_characteristic_request(gatt_client);
break;
case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
gatt_client->state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
send_gatt_characteristic_request(gatt_client);
break;
case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
gatt_client->state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
send_gatt_characteristic_descriptor_request(gatt_client);
break;
case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
case P_W2_SEND_INCLUDED_SERVICE_QUERY:
gatt_client->state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
send_gatt_included_service_request(gatt_client);
break;
case P_W2_SEND_INCLUDED_SERVICE_QUERY:
case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
gatt_client->state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
send_gatt_included_service_uuid_request(gatt_client);
break;
case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
gatt_client->state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
send_gatt_read_characteristic_value_request(gatt_client);
break;
case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
case P_W2_SEND_READ_BLOB_QUERY:
gatt_client->state = P_W4_READ_BLOB_RESULT;
send_gatt_read_blob_request(gatt_client);
break;
case P_W2_SEND_READ_BLOB_QUERY:
case P_W2_SEND_READ_BY_TYPE_REQUEST:
gatt_client->state = P_W4_READ_BY_TYPE_RESPONSE;
send_gatt_read_by_type_request(gatt_client);
break;
case P_W2_SEND_READ_BY_TYPE_REQUEST:
case P_W2_SEND_READ_MULTIPLE_REQUEST:
gatt_client->state = P_W4_READ_MULTIPLE_RESPONSE;
send_gatt_read_multiple_request(gatt_client);
break;
#ifdef ENABLE_GATT_OVER_EATTcase P_W2_SEND_READ_MULTIPLE_REQUEST:
case P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST:
gatt_client->state = P_W4_READ_MULTIPLE_VARIABLE_RESPONSE;
send_gatt_read_multiple_variable_request(gatt_client);
break;/* ... */
#endif
case P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST:
case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
gatt_client->state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
send_gatt_write_attribute_value_request(gatt_client);
break;
case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
case P_W2_PREPARE_WRITE:
gatt_client->state = P_W4_PREPARE_WRITE_RESULT;
send_gatt_prepare_write_request(gatt_client);
break;
case P_W2_PREPARE_WRITE:
case P_W2_PREPARE_WRITE_SINGLE:
gatt_client->state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
send_gatt_prepare_write_request(gatt_client);
break;
case P_W2_PREPARE_WRITE_SINGLE:
case P_W2_PREPARE_RELIABLE_WRITE:
gatt_client->state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
send_gatt_prepare_write_request(gatt_client);
break;
case P_W2_PREPARE_RELIABLE_WRITE:
case P_W2_EXECUTE_PREPARED_WRITE:
gatt_client->state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
send_gatt_execute_write_request(gatt_client);
break;
case P_W2_EXECUTE_PREPARED_WRITE:
case P_W2_CANCEL_PREPARED_WRITE:
gatt_client->state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
send_gatt_cancel_prepared_write_request(gatt_client);
break;
case P_W2_CANCEL_PREPARED_WRITE:
case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
gatt_client->state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
send_gatt_cancel_prepared_write_request(gatt_client);
break;
#ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERYcase P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
send_gatt_characteristic_descriptor_request(gatt_client);/* ... */
#elsecase P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
gatt_client->state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
send_gatt_read_client_characteristic_configuration_request(gatt_client);/* ... */
#endif
break;
case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
gatt_client->state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
send_gatt_read_characteristic_descriptor_request(gatt_client);
break;
case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
gatt_client->state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
send_gatt_read_blob_request(gatt_client);
break;
case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
gatt_client->state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
send_gatt_write_attribute_value_request(gatt_client);
break;
case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
gatt_client->state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
send_gatt_write_client_characteristic_configuration_request(gatt_client);
break;
case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
gatt_client->state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
send_gatt_prepare_write_request(gatt_client);
break;
case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
gatt_client->state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
send_gatt_execute_write_request(gatt_client);
break;
#ifdef ENABLE_LE_SIGNED_WRITEcase P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
case P_W4_IDENTITY_RESOLVING:
log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle));
switch (sm_identity_resolving_state(gatt_client->con_handle)){
case IRK_LOOKUP_SUCCEEDED:
gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle);
gatt_client->state = P_W4_CMAC_READY;
if (sm_cmac_ready()){
gatt_client_run_for_client_start_signed_write(gatt_client);
}if (sm_cmac_ready()) { ... }
break;case IRK_LOOKUP_SUCCEEDED:
case IRK_LOOKUP_FAILED:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
break;case IRK_LOOKUP_FAILED:
default:
break;default
}switch (sm_identity_resolving_state(gatt_client->con_handle)) { ... }
packet_sent = false;
break;
case P_W4_IDENTITY_RESOLVING:
case P_W4_CMAC_READY:
if (sm_cmac_ready()){
gatt_client_run_for_client_start_signed_write(gatt_client);
}if (sm_cmac_ready()) { ... }
packet_sent = false;
break;
case P_W4_CMAC_READY:
case P_W2_SEND_SIGNED_WRITE: {
gatt_client->state = P_W4_SEND_SIGNED_WRITE_DONE;
uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1);
send_gatt_signed_write_request(gatt_client, sign_counter);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;
...}/* ... */
#endif
default:
done = false;
break;default
}switch (gatt_client->state) { ... }
if (done){
return packet_sent;
}if (done) { ... }
btstack_context_callback_registration_t * callback =
(btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->write_without_response_requests);
if (callback != NULL){
(*callback->callback)(callback->context);
return true;
}if (callback != NULL) { ... }
if (gatt_client->write_without_response_callback != NULL){
btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback;
gatt_client->write_without_response_callback = NULL;
uint8_t event[4];
event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE;
event[1] = sizeof(event) - 2u;
little_endian_store_16(event, 2, gatt_client->con_handle);
packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event));
return true;
}if (gatt_client->write_without_response_callback != NULL) { ... }
return false;
}{ ... }
static void gatt_client_run(void){
btstack_linked_item_t *it;
bool packet_sent;
#ifdef ENABLE_GATT_OVER_EATT
btstack_linked_list_iterator_t it_eatt;
#endif
for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
gatt_client_t * gatt_client = (gatt_client_t *) it;
switch (gatt_client->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
#ifdef ENABLE_GATT_OVER_EATT
btstack_linked_list_iterator_init(&it_eatt, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it_eatt)) {
gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it_eatt);
if (eatt_client->state != P_READY){
if (att_dispatch_client_can_send_now(gatt_client->con_handle)){
gatt_client_run_for_gatt_client(eatt_client);
}if (att_dispatch_client_can_send_now(gatt_client->con_handle)) { ... }
}if (eatt_client->state != P_READY) { ... }
}while (btstack_linked_list_iterator_has_next(&it_eatt)) { ... }
/* ... */#endif
if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) {
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
return;
}if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) { ... }
packet_sent = gatt_client_run_for_gatt_client(gatt_client);
if (packet_sent){
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
return;
}if (packet_sent) { ... }
break;
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) {
continue;
}if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) { ... }
if (att_dispatch_client_can_send_now(gatt_client->con_handle) == false) {
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
return;
}if (att_dispatch_client_can_send_now(gatt_client->con_handle) == false) { ... }
packet_sent = gatt_client_run_for_gatt_client(gatt_client);
if (packet_sent){
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
return;
}if (packet_sent) { ... }
break;/* ... */
#endifcase ATT_BEARER_UNENHANCED_CLASSIC:
default:
btstack_unreachable();
break;default
}switch (gatt_client->bearer_type) { ... }
}for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) { ... }
}{ ... }
static void gatt_client_emit_events(void * context){
UNUSED(context);
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) {
gatt_client_t *gatt_client = (gatt_client_t *) it;
if (gatt_client->state == P_W2_EMIT_QUERY_COMPLETE_EVENT){
gatt_client->state = P_READY;
emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
}if (gatt_client->state == P_W2_EMIT_QUERY_COMPLETE_EVENT) { ... }
}for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) { ... }
}{ ... }
static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) {
if (is_ready(gatt_client)) return;
gatt_client_handle_transaction_complete(gatt_client, att_error_code);
}{ ... }
static void gatt_client_handle_reencryption_complete(const uint8_t * packet){
hci_con_handle_t con_handle = sm_event_reencryption_complete_get_handle(packet);
gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client == NULL) return;
gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
gatt_client->reencryption_result = sm_event_reencryption_complete_get_status(packet);
gatt_client->reencryption_active = false;
gatt_client->wait_for_authentication_complete = false;
if (gatt_client->state == P_READY) return;
switch (sm_event_reencryption_complete_get_status(packet)){
case ERROR_CODE_SUCCESS:
log_info("re-encryption success, retry operation");
break;case ERROR_CODE_SUCCESS:
case ERROR_CODE_AUTHENTICATION_FAILURE:
case ERROR_CODE_PIN_OR_KEY_MISSING:
#if defined(ENABLE_GATT_CLIENT_PAIRING) && !defined(ENABLE_LE_PROACTIVE_AUTHENTICATION)
if (gatt_client_required_security_level == LEVEL_0) {
int le_device_db_index = sm_le_device_index(gatt_client->con_handle);
btstack_assert(le_device_db_index >= 0);
log_info("reactive auth with pairing: delete bonding and start pairing");
#ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION
hci_remove_le_device_db_entry_from_resolving_list((uint16_t) le_device_db_index);
#endif
le_device_db_remove(le_device_db_index);
sm_request_pairing(gatt_client->con_handle);
break;
}if (gatt_client_required_security_level == LEVEL_0) { ... }
/* ... */#endif
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
break;case ERROR_CODE_PIN_OR_KEY_MISSING:
default:
gatt_client_handle_transaction_complete(gatt_client, gatt_client->pending_error_code);
break;default
}switch (sm_event_reencryption_complete_get_status(packet)) { ... }
}{ ... }
static void gatt_client_handle_disconnection_complete(const uint8_t * packet){
log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
hci_con_handle_t con_handle = little_endian_read_16(packet,3);
gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client == NULL) return;
gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
gatt_client_timeout_stop(gatt_client);
btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
btstack_memory_gatt_client_free(gatt_client);
}{ ... }
static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(channel);
UNUSED(size);
if (packet_type != HCI_EVENT_PACKET) return;
hci_con_handle_t con_handle;
gatt_client_t * gatt_client;
switch (hci_event_packet_get_type(packet)) {
case HCI_EVENT_DISCONNECTION_COMPLETE:
gatt_client_handle_disconnection_complete(packet);
break;
case HCI_EVENT_DISCONNECTION_COMPLETE:
case SM_EVENT_PAIRING_COMPLETE:
con_handle = sm_event_pairing_complete_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client == NULL) break;
gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
if (gatt_client->wait_for_authentication_complete){
gatt_client->wait_for_authentication_complete = false;
if (sm_event_pairing_complete_get_status(packet) != ERROR_CODE_SUCCESS){
log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code);
gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code);
}if (sm_event_pairing_complete_get_status(packet) != ERROR_CODE_SUCCESS) { ... } else {
log_info("pairing success, retry operation");
}else { ... }
}if (gatt_client->wait_for_authentication_complete) { ... }
break;
#ifdef ENABLE_LE_SIGNED_WRITE
case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
case SM_EVENT_IDENTITY_RESOLVING_FAILED:
break;/* ... */
#endif
case SM_EVENT_REENCRYPTION_STARTED:
con_handle = sm_event_reencryption_complete_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client == NULL) break;
gatt_client->reencryption_active = true;
gatt_client->reencryption_result = ERROR_CODE_SUCCESS;
break;
case SM_EVENT_REENCRYPTION_STARTED:
case SM_EVENT_REENCRYPTION_COMPLETE:
gatt_client_handle_reencryption_complete(packet);
break;case SM_EVENT_REENCRYPTION_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
gatt_client_run();
}{ ... }
static void gatt_client_handle_att_read_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
switch (gatt_client->state) {
case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
if (size >= 17) {
uint8_t uuid128[16];
reverse_128(&packet[1], uuid128);
report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128);
}if (size >= 17) { ... }
trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle);
break;
case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;
case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
size - 1u, 0u);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;
case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_READ_BLOB_RESULT:
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
size - 1u, gatt_client->attribute_offset);
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
break;
case P_W4_READ_BLOB_RESULT:
case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
size - 1u, gatt_client->attribute_offset);
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
size - 1u);
break;
case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
default:
break;default
}switch (gatt_client->state) { ... }
}{ ... }
static void gatt_client_handle_att_read_by_type_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
switch (gatt_client->state) {
case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
report_gatt_characteristics(gatt_client, packet, size);
trigger_next_characteristic_query(gatt_client,
get_last_result_handle_from_characteristics_list(packet, size));
break;case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
report_gatt_characteristics(gatt_client, packet, size);
trigger_next_characteristic_query(gatt_client,
get_last_result_handle_from_characteristics_list(packet, size));
break;case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
if (size < 2u) break;
uint16_t uuid16 = 0;
uint16_t pair_size = packet[1];
if (pair_size == 6u) {
if (size < 8u) break;
gatt_client->start_group_handle = little_endian_read_16(packet, 2);
gatt_client->query_start_handle = little_endian_read_16(packet, 4);
gatt_client->query_end_handle = little_endian_read_16(packet, 6);
gatt_client->state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
break;
}if (pair_size == 6u) { ... }
if (pair_size != 8u) break;
uint16_t offset;
for (offset = 2u; (offset + 8u) <= size; offset += pair_size) {
uint16_t include_handle = little_endian_read_16(packet, offset);
gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u);
gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u);
uuid16 = little_endian_read_16(packet, offset + 6u);
report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
}for (offset = 2u; (offset + 8u) <= size; offset += pair_size) { ... }
trigger_next_included_service_query(gatt_client,
get_last_result_handle_from_included_services_list(packet,
size));
break;
...}
#ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERYcase P_W4_INCLUDED_SERVICE_QUERY_RESULT:
case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
gatt_client->state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
break;/* ... */
#endifcase P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
case P_W4_READ_BY_TYPE_RESPONSE: {
uint16_t pair_size = packet[1];
uint16_t last_result_handle = 0xffff;
if (pair_size > 2) {
uint16_t offset;
for (offset = 2; offset < size; offset += pair_size) {
uint16_t value_handle = little_endian_read_16(packet, offset);
report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
pair_size - 2u);
last_result_handle = value_handle;
}for (offset = 2; offset < size; offset += pair_size) { ... }
}if (pair_size > 2) { ... }
trigger_next_read_by_type_query(gatt_client, last_result_handle);
break;
...}case P_W4_READ_BY_TYPE_RESPONSE:
default:
break;default
}switch (gatt_client->state) { ... }
}{ ... }
static void gatt_client_handle_att_write_response(gatt_client_t *gatt_client) {
switch (gatt_client->state) {
case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
default:
break;default
}switch (gatt_client->state) { ... }
}{ ... }
static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) {
uint8_t att_status;
switch (packet[0]) {
case ATT_EXCHANGE_MTU_RESPONSE: {
if (size < 3u) break;
bool update_gatt_server_att_mtu = false;
uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
uint16_t local_rx_mtu = l2cap_max_le_mtu();
switch (gatt_client->bearer_type){
case ATT_BEARER_UNENHANCED_LE:
update_gatt_server_att_mtu = true;
break;
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_BEARER_UNENHANCED_LE:
case ATT_BEARER_UNENHANCED_CLASSIC:
local_rx_mtu = gatt_client->mtu;
break;/* ... */
#endifcase ATT_BEARER_UNENHANCED_CLASSIC:
default:
btstack_unreachable();
break;default
}switch (gatt_client->bearer_type) { ... }
uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu;
gatt_client->mtu = mtu;
gatt_client->mtu_state = MTU_EXCHANGED;
if (update_gatt_server_att_mtu){
hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
hci_connection->att_connection.mtu = gatt_client->mtu;
hci_connection->att_connection.mtu_exchanged = true;
}if (update_gatt_server_att_mtu) { ... }
emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu);
break;
...}case ATT_EXCHANGE_MTU_RESPONSE:
case ATT_READ_BY_GROUP_TYPE_RESPONSE:
switch (gatt_client->state) {
case P_W4_SERVICE_QUERY_RESULT:
report_gatt_services(gatt_client, packet, size);
trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size));
break;case P_W4_SERVICE_QUERY_RESULT:
default:
break;default
}switch (gatt_client->state) { ... }
break;case ATT_READ_BY_GROUP_TYPE_RESPONSE:
case ATT_HANDLE_VALUE_NOTIFICATION:
if (size < 3u) return;
report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
return;
#ifdef ENABLE_GATT_OVER_EATTcase ATT_HANDLE_VALUE_NOTIFICATION:
case ATT_MULTIPLE_HANDLE_VALUE_NTF:
if (size >= 5u) {
uint16_t offset = 1;
while (true){
uint16_t value_handle = little_endian_read_16(packet, offset);
offset += 2;
uint16_t value_length = little_endian_read_16(packet, offset);
offset += 2;
if ((offset + value_length) > size) break;
report_gatt_notification(gatt_client, value_handle, &packet[offset], value_length);
offset += value_length;
}while (true) { ... }
}if (size >= 5u) { ... }
return;/* ... */
#endifcase ATT_MULTIPLE_HANDLE_VALUE_NTF:
case ATT_HANDLE_VALUE_INDICATION:
if (size < 3u) break;
report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
gatt_client->send_confirmation = true;
break;case ATT_HANDLE_VALUE_INDICATION:
case ATT_READ_BY_TYPE_RESPONSE:
gatt_client_handle_att_read_by_type_response(gatt_client, packet, size);
break;case ATT_READ_BY_TYPE_RESPONSE:
case ATT_READ_RESPONSE:
gatt_client_handle_att_read_response(gatt_client, packet, size);
break;case ATT_READ_RESPONSE:
case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
uint8_t pair_size = 4;
int i;
uint16_t start_group_handle;
uint16_t end_group_handle = 0xffff;
for (i = 1u; (i + pair_size) <= size; i += pair_size) {
start_group_handle = little_endian_read_16(packet, i);
end_group_handle = little_endian_read_16(packet, i + 2);
emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
gatt_client->uuid128);
}for (i = 1u; (i + pair_size) <= size; i += pair_size) { ... }
trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
break;
...}case ATT_FIND_BY_TYPE_VALUE_RESPONSE:
case ATT_FIND_INFORMATION_REPLY: {
if (size < 2u) break;
uint8_t pair_size = 4;
if (packet[1u] == 2u) {
pair_size = 18;
}if (packet[1u] == 2u) { ... }
uint16_t offset = 2;
if (size < (pair_size + offset)) break;
uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
#ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state);
if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
if (pair_size == 4){
while ((offset + 4) <= size){
uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle);
break;
}if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION) { ... }
offset += pair_size;
}while ((offset + 4) <= size) { ... }
}if (pair_size == 4) { ... }
if (is_query_done(gatt_client, last_descriptor_handle)){
}if (is_query_done(gatt_client, last_descriptor_handle)) { ... } else {
gatt_client->start_group_handle = last_descriptor_handle + 1;
gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
}else { ... }
break;
}if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT) { ... }
/* ... */#endif
report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size);
trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle);
break;
...}
case ATT_FIND_INFORMATION_REPLY:
case ATT_WRITE_RESPONSE:
gatt_client_handle_att_write_response(gatt_client);
break;
case ATT_WRITE_RESPONSE:
case ATT_READ_BLOB_RESPONSE: {
uint16_t received_blob_length = size - 1u;
switch (gatt_client->state) {
case P_W4_READ_BLOB_RESULT:
report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
received_blob_length, gatt_client->attribute_offset);
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
break;case P_W4_READ_BLOB_RESULT:
case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
&packet[1], received_blob_length,
gatt_client->attribute_offset);
trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
received_blob_length);
break;case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
default:
break;default
}switch (gatt_client->state) { ... }
break;
...}case ATT_READ_BLOB_RESPONSE:
case ATT_PREPARE_WRITE_RESPONSE:
switch (gatt_client->state) {
case P_W4_PREPARE_WRITE_SINGLE_RESULT:
if (is_value_valid(gatt_client, packet, size)) {
att_status = ATT_ERROR_SUCCESS;
}if (is_value_valid(gatt_client, packet, size)) { ... } else {
att_status = ATT_ERROR_DATA_MISMATCH;
}else { ... }
gatt_client_handle_transaction_complete(gatt_client, att_status);
break;
case P_W4_PREPARE_WRITE_SINGLE_RESULT:
case P_W4_PREPARE_WRITE_RESULT: {
gatt_client->attribute_offset = little_endian_read_16(packet, 3);
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
break;
...}case P_W4_PREPARE_WRITE_RESULT:
case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
gatt_client->attribute_offset = little_endian_read_16(packet, 3);
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
break;
...}case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
if (is_value_valid(gatt_client, packet, size)) {
gatt_client->attribute_offset = little_endian_read_16(packet, 3);
trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
P_W2_EXECUTE_PREPARED_WRITE);
break;
}if (is_value_valid(gatt_client, packet, size)) { ... }
gatt_client->state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
break;
...}case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
default:
break;default
}switch (gatt_client->state) { ... }
break;
case ATT_PREPARE_WRITE_RESPONSE:
case ATT_EXECUTE_WRITE_RESPONSE:
switch (gatt_client->state) {
case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
case P_W4_CANCEL_PREPARED_WRITE_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_CANCEL_PREPARED_WRITE_RESULT:
case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_DATA_MISMATCH);
break;case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
default:
break;
default
}switch (gatt_client->state) { ... }
break;
case ATT_EXECUTE_WRITE_RESPONSE:
case ATT_READ_MULTIPLE_RESPONSE:
switch (gatt_client->state) {
case P_W4_READ_MULTIPLE_RESPONSE:
report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_READ_MULTIPLE_RESPONSE:
default:
break;default
}switch (gatt_client->state) { ... }
break;
#ifdef ENABLE_GATT_OVER_EATTcase ATT_READ_MULTIPLE_RESPONSE:
case ATT_READ_MULTIPLE_VARIABLE_RSP:
switch (gatt_client->state) {
case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
default:
break;default
}switch (gatt_client->state) { ... }
break;/* ... */
#endif
case ATT_READ_MULTIPLE_VARIABLE_RSP:
case ATT_ERROR_RESPONSE:
if (size < 5u) return;
att_status = packet[4];
switch (att_status) {
case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
switch (gatt_client->state) {
case P_W4_SERVICE_QUERY_RESULT:
case P_W4_SERVICE_WITH_UUID_RESULT:
case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
report_gatt_characteristic_end_found(gatt_client, gatt_client->end_group_handle);
gatt_client_handle_transaction_complete(gatt_client, ATT_ERROR_SUCCESS);
break;case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
case P_W4_READ_BY_TYPE_RESPONSE:
if (gatt_client->start_group_handle == gatt_client->query_start_handle) {
att_status = ATT_ERROR_ATTRIBUTE_NOT_FOUND;
}if (gatt_client->start_group_handle == gatt_client->query_start_handle) { ... } else {
att_status = ATT_ERROR_SUCCESS;
}else { ... }
gatt_client_handle_transaction_complete(gatt_client, att_status);
break;case P_W4_READ_BY_TYPE_RESPONSE:
default:
gatt_client_report_error_if_pending(gatt_client, att_status);
break;default
}switch (gatt_client->state) { ... }
break;
...}
#ifdef ENABLE_GATT_CLIENT_PAIRING
case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
if (gatt_client->security_counter > 0) {
gatt_client_report_error_if_pending(gatt_client, att_status);
break;
}if (gatt_client->security_counter > 0) { ... }
gatt_client->security_counter++;
int retry = 1;
switch (gatt_client->state){
case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
break;case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
case P_W4_READ_BLOB_RESULT:
gatt_client->state = P_W2_SEND_READ_BLOB_QUERY;
break;case P_W4_READ_BLOB_RESULT:
case P_W4_READ_BY_TYPE_RESPONSE:
gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
break;case P_W4_READ_BY_TYPE_RESPONSE:
case P_W4_READ_MULTIPLE_RESPONSE:
gatt_client->state = P_W2_SEND_READ_MULTIPLE_REQUEST;
break;case P_W4_READ_MULTIPLE_RESPONSE:
case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
gatt_client->state = P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST;
break;case P_W4_READ_MULTIPLE_VARIABLE_RESPONSE:
case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
break;case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
case P_W4_PREPARE_WRITE_RESULT:
gatt_client->state = P_W2_PREPARE_WRITE;
break;case P_W4_PREPARE_WRITE_RESULT:
case P_W4_PREPARE_WRITE_SINGLE_RESULT:
gatt_client->state = P_W2_PREPARE_WRITE_SINGLE;
break;case P_W4_PREPARE_WRITE_SINGLE_RESULT:
case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
gatt_client->state = P_W2_PREPARE_RELIABLE_WRITE;
break;case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE;
break;case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
case P_W4_CANCEL_PREPARED_WRITE_RESULT:
gatt_client->state = P_W2_CANCEL_PREPARED_WRITE;
break;case P_W4_CANCEL_PREPARED_WRITE_RESULT:
case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
gatt_client->state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
break;case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
break;case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client->state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
break;case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
break;case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
gatt_client->state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
break;case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client->state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
break;case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
break;
#ifdef ENABLE_LE_SIGNED_WRITEcase P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
case P_W4_SEND_SINGED_WRITE_DONE:
gatt_client->state = P_W2_SEND_SIGNED_WRITE;
break;/* ... */
#endifcase P_W4_SEND_SINGED_WRITE_DONE:
default:
log_info("retry not supported for state %x", gatt_client->state);
retry = 0;
break;default
}switch (gatt_client->state) { ... }
if (!retry) {
gatt_client_report_error_if_pending(gatt_client, att_status);
break;
}if (!retry) { ... }
log_info("security error, start pairing");
gatt_client->wait_for_authentication_complete = true;
gatt_client->pending_error_code = att_status;
sm_request_pairing(gatt_client->con_handle);
break;
...}/* ... */
#endif
case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
default:
gatt_client_report_error_if_pending(gatt_client, att_status);
break;default
}switch (att_status) { ... }
break;
case ATT_ERROR_RESPONSE:
default:
log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
break;default
}switch (packet[0]) { ... }
}{ ... }
static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
gatt_client_t *gatt_client;
#ifdef ENABLE_GATT_OVER_CLASSIC
uint8_t status;
hci_connection_t * hci_connection;
hci_con_handle_t con_handle;/* ... */
#endif
if (size < 1u) return;
switch (packet_type){
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
#ifdef ENABLE_GATT_OVER_CLASSIC
case L2CAP_EVENT_CHANNEL_OPENED:
status = l2cap_event_channel_opened_get_status(packet);
gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
if (gatt_client != NULL){
con_handle = l2cap_event_channel_opened_get_handle(packet);
hci_connection = hci_connection_for_handle(con_handle);
if (status == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES){
if ((hci_connection != NULL) && hci_connection->att_server.incoming_connection_request) {
log_info("Collision, retry in 100ms");
gatt_client->state = P_W2_L2CAP_CONNECT;
btstack_run_loop_set_timer(&gatt_client->gc_timeout, GATT_CLIENT_COLLISION_BACKOFF_MS);
btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_classic_retry);
btstack_run_loop_add_timer(&gatt_client->gc_timeout);
break;
}if ((hci_connection != NULL) && hci_connection->att_server.incoming_connection_request) { ... }
}if (status == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES) { ... }
gatt_client->state = P_READY;
gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet);
gatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
gatt_client_classic_handle_connected(gatt_client, status);
}if (gatt_client != NULL) { ... }
break;case L2CAP_EVENT_CHANNEL_OPENED:
case L2CAP_EVENT_CHANNEL_CLOSED:
gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet));
if (gatt_client != NULL){
gatt_client_classic_handle_disconnected(gatt_client);
}if (gatt_client != NULL) { ... }
break;/* ... */
#endif
case L2CAP_EVENT_CAN_SEND_NOW:
gatt_client_run();
break;
case L2CAP_EVENT_CAN_SEND_NOW:
case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
if (size < 6u) break;
gatt_client = gatt_client_get_context_for_handle(handle);
if (gatt_client != NULL) {
gatt_client->mtu = little_endian_read_16(packet, 4);
}if (gatt_client != NULL) { ... }
break;case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
break;
case HCI_EVENT_PACKET:
case ATT_DATA_PACKET:
switch (packet[0]) {
case ATT_HANDLE_VALUE_NOTIFICATION:
case ATT_HANDLE_VALUE_INDICATION:
gatt_client_provide_context_for_handle(handle, &gatt_client);
break;case ATT_HANDLE_VALUE_INDICATION:
default:
gatt_client = gatt_client_get_context_for_handle(handle);
break;default
}switch (packet[0]) { ... }
if (gatt_client != NULL) {
gatt_client_handle_att_response(gatt_client, packet, size);
gatt_client_run();
}if (gatt_client != NULL) { ... }
break;
#ifdef ENABLE_GATT_OVER_CLASSICcase ATT_DATA_PACKET:
case L2CAP_DATA_PACKET:
gatt_client = gatt_client_get_context_for_l2cap_cid(handle);
if (gatt_client != NULL){
gatt_client_handle_att_response(gatt_client, packet, size);
gatt_client_run();
}if (gatt_client != NULL) { ... }
break;/* ... */
#endif
case L2CAP_DATA_PACKET:
default:
break;default
}switch (packet_type) { ... }
}{ ... }
#ifdef ENABLE_LE_SIGNED_WRITE
static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_connections);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (gatt_client->state == P_W4_CMAC_RESULT){
(void)memcpy(gatt_client->cmac, hash, 8);
gatt_client->state = P_W2_SEND_SIGNED_WRITE;
gatt_client_run();
return;
}if (gatt_client->state == P_W4_CMAC_RESULT) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}att_signed_write_handle_cmac_result (uint8_t hash[8]) { ... }
uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (is_ready(gatt_client) == 0){
return GATT_CLIENT_IN_WRONG_STATE;
}if (is_ready(gatt_client) == 0) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_length = message_len;
gatt_client->attribute_value = message;
gatt_client->state = P_W4_IDENTITY_RESOLVING;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}gatt_client_signed_write_without_response (btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message) { ... }
/* ... */#endif
uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = 0x0001;
gatt_client->end_group_handle = 0xffff;
gatt_client->state = P_W2_SEND_SERVICE_QUERY;
gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = 0x0001;
gatt_client->end_group_handle = 0xffff;
gatt_client->state = P_W2_SEND_SERVICE_QUERY;
gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = 0x0001;
gatt_client->end_group_handle = 0xffff;
gatt_client->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
gatt_client->uuid16 = uuid16;
uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16);
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = 0x0001;
gatt_client->end_group_handle = 0xffff;
gatt_client->uuid16 = 0;
(void)memcpy(gatt_client->uuid128, uuid128, 16);
gatt_client->state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = service->start_group_handle;
gatt_client->end_group_handle = service->end_group_handle;
gatt_client->filter_with_uuid = false;
gatt_client->characteristic_start_handle = 0;
gatt_client->state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_find_included_services_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = service->start_group_handle;
gatt_client->end_group_handle = service->end_group_handle;
gatt_client->state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = start_handle;
gatt_client->end_group_handle = end_handle;
gatt_client->filter_with_uuid = true;
gatt_client->uuid16 = uuid16;
uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
gatt_client->characteristic_start_handle = 0;
gatt_client->state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = start_handle;
gatt_client->end_group_handle = end_handle;
gatt_client->filter_with_uuid = true;
gatt_client->uuid16 = 0;
(void)memcpy(gatt_client->uuid128, uuid128, 16);
gatt_client->characteristic_start_handle = 0;
gatt_client->state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, uint16_t uuid16){
return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16);
}{ ... }
uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128){
return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128);
}{ ... }
uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (characteristic->end_handle > characteristic->value_handle){
gatt_client->callback = callback;
gatt_client->start_group_handle = characteristic->value_handle + 1u;
gatt_client->end_group_handle = characteristic->end_handle;
gatt_client->state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
gatt_client_run();
}if (characteristic->end_handle > characteristic->value_handle) { ... } else {
gatt_client->state = P_W2_EMIT_QUERY_COMPLETE_EVENT;
gatt_client_deferred_event_emit.callback = gatt_client_emit_events;
btstack_run_loop_execute_on_main_thread(&gatt_client_deferred_event_emit);
}else { ... }
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_offset = 0;
gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_value_of_characteristics_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = start_handle;
gatt_client->end_group_handle = end_handle;
gatt_client->query_start_handle = start_handle;
gatt_client->query_end_handle = end_handle;
gatt_client->uuid16 = uuid16;
uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_value_of_characteristics_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = start_handle;
gatt_client->end_group_handle = end_handle;
gatt_client->query_start_handle = start_handle;
gatt_client->query_end_handle = end_handle;
gatt_client->uuid16 = 0;
(void)memcpy(gatt_client->uuid128, uuid128, 16);
gatt_client->state = P_W2_SEND_READ_BY_TYPE_REQUEST;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
}{ ... }
uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_offset = offset;
gatt_client->state = P_W2_SEND_READ_BLOB_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0);
}{ ... }
uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
}{ ... }
static uint8_t gatt_client_read_multiple_characteristic_values_with_state(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles, gatt_client_state_t state){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
#ifdef ENABLE_GATT_OVER_EATT
if (state == P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST){
if (gatt_client->bearer_type != ATT_BEARER_ENHANCED_LE){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (gatt_client->bearer_type != ATT_BEARER_ENHANCED_LE) { ... }
}if (state == P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST) { ... }
/* ... */#endif
gatt_client->callback = callback;
gatt_client->read_multiple_handle_count = num_value_handles;
gatt_client->read_multiple_handles = value_handles;
gatt_client->state = state;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_multiple_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){
return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_REQUEST);
}{ ... }
#ifdef ENABLE_GATT_OVER_EATT
uint8_t gatt_client_read_multiple_variable_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){
return gatt_client_read_multiple_characteristic_values_with_state(callback, con_handle, num_value_handles, value_handles, P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST);
}gatt_client_read_multiple_variable_characteristic_values (btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles) { ... }
/* ... */#endif
uint8_t gatt_client_write_value_of_characteristic_without_response(hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG;
if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY;
return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value);
}{ ... }
uint8_t gatt_client_write_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_write_long_value_of_characteristic_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_offset = offset;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_PREPARE_WRITE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
}{ ... }
uint8_t gatt_client_reliable_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = value_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_offset = 0;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_PREPARE_RELIABLE_WRITE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_write_client_characteristic_configuration(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (configuration > 3){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}if (configuration > 3) { ... }
if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) {
log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
}if ((configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) && ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) { ... } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){
log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
}else if ((configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) && ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)) { ... }
gatt_client->callback = callback;
gatt_client->start_group_handle = characteristic->value_handle;
gatt_client->end_group_handle = characteristic->end_handle;
little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration);
#ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
#else
gatt_client->state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
#endif
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = descriptor_handle;
gatt_client->state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
}{ ... }
uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = descriptor_handle;
gatt_client->attribute_offset = offset;
gatt_client->state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
}{ ... }
uint8_t gatt_client_read_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
}{ ... }
uint8_t gatt_client_write_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = descriptor_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_offset = 0;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_write_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
}{ ... }
uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = descriptor_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_offset = offset;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value);
}{ ... }
uint8_t gatt_client_write_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
}{ ... }
/* ... */
uint8_t gatt_client_prepare_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->attribute_handle = attribute_handle;
gatt_client->attribute_length = value_length;
gatt_client->attribute_offset = offset;
gatt_client->attribute_value = value;
gatt_client->state = P_W2_PREPARE_WRITE_SINGLE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
/* ... */
uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->state = P_W2_EXECUTE_PREPARED_WRITE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
/* ... */
uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client->callback = callback;
gatt_client->state = P_W2_CANCEL_PREPARED_WRITE;
gatt_client_run();
return ERROR_CODE_SUCCESS;
}{ ... }
void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
service->start_group_handle = little_endian_read_16(packet, offset);
service->end_group_handle = little_endian_read_16(packet, offset + 2);
reverse_128(&packet[offset + 4], service->uuid128);
if (uuid_has_bluetooth_prefix(service->uuid128)){
service->uuid16 = big_endian_read_32(service->uuid128, 0);
}if (uuid_has_bluetooth_prefix(service->uuid128)) { ... } else {
service->uuid16 = 0;
}else { ... }
}{ ... }
void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
characteristic->start_handle = little_endian_read_16(packet, offset);
characteristic->value_handle = little_endian_read_16(packet, offset + 2);
characteristic->end_handle = little_endian_read_16(packet, offset + 4);
characteristic->properties = little_endian_read_16(packet, offset + 6);
reverse_128(&packet[offset+8], characteristic->uuid128);
if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
}if (uuid_has_bluetooth_prefix(characteristic->uuid128)) { ... } else {
characteristic->uuid16 = 0;
}else { ... }
}{ ... }
void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
descriptor->handle = little_endian_read_16(packet, offset);
reverse_128(&packet[offset+2], descriptor->uuid128);
if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
}if (uuid_has_bluetooth_prefix(descriptor->uuid128)) { ... } else {
descriptor->uuid16 = 0;
}else { ... }
}{ ... }
void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
gatt_client->callback = callback;
gatt_client->mtu_state = SEND_MTU_EXCHANGE;
gatt_client_run();
}if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED) { ... }
}{ ... }
uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration);
if (added == false){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (added == false) { ... } else {
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
return ERROR_CODE_SUCCESS;
}else { ... }
}{ ... }
uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
if (added == false){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (added == false) { ... } else {
gatt_client_notify_can_send_query(gatt_client);
return ERROR_CODE_SUCCESS;
}else { ... }
}{ ... }
uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (gatt_client->write_without_response_callback != NULL){
return GATT_CLIENT_IN_WRONG_STATE;
}if (gatt_client->write_without_response_callback != NULL) { ... }
gatt_client->write_without_response_callback = callback;
att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
return ERROR_CODE_SUCCESS;
}{ ... }
#if defined(ENABLE_GATT_OVER_CLASSIC) || defined(ENABLE_GATT_OVER_EATT)
#include "hci_event.h"
static const hci_event_t gatt_client_connected = {
GATT_EVENT_CONNECTED, 0, "1BH"
...};
static const hci_event_t gatt_client_disconnected = {
GATT_EVENT_DISCONNECTED, 0, "H"
...};
static void gatt_client_emit_connected(btstack_packet_handler_t callback, uint8_t status, bd_addr_t addr,
hci_con_handle_t con_handle) {
uint8_t buffer[20];
uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr, con_handle);
(*callback)(HCI_EVENT_PACKET, 0, buffer, len);
}gatt_client_emit_connected (btstack_packet_handler_t callback, uint8_t status, bd_addr_t addr, hci_con_handle_t con_handle) { ... }
/* ... */
#endif
#ifdef ENABLE_GATT_OVER_CLASSIC
#include "bluetooth_psm.h"
static gatt_client_t * gatt_client_classic_active_sdp_query;
static uint8_t gatt_client_classic_sdp_buffer[32];
static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
gatt_client_t * gatt_client = (gatt_client_t *) it;
if (memcmp(gatt_client->addr, addr, 6) == 0){
return gatt_client;
}if (memcmp(gatt_client->addr, addr, 6) == 0) { ... }
}for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) { ... }
return NULL;
}gatt_client_get_context_for_classic_addr (bd_addr_t addr) { ... }
static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){
btstack_linked_item_t *it;
for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
gatt_client_t * gatt_client = (gatt_client_t *) it;
if (gatt_client->l2cap_cid == l2cap_cid){
return gatt_client;
}if (gatt_client->l2cap_cid == l2cap_cid) { ... }
}for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next) { ... }
return NULL;
}gatt_client_get_context_for_l2cap_cid (uint16_t l2cap_cid) { ... }
static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){
bd_addr_t addr;
memcpy(addr, gatt_client->addr, 6);
hci_con_handle_t con_handle = gatt_client->con_handle;
btstack_packet_handler_t callback = gatt_client->callback;
if (status != ERROR_CODE_SUCCESS){
btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
btstack_memory_gatt_client_free(gatt_client);
}if (status != ERROR_CODE_SUCCESS) { ... }
gatt_client_emit_connected(callback, status, addr, con_handle);
}gatt_client_classic_handle_connected (gatt_client_t * gatt_client, uint8_t status) { ... }
static void gatt_client_classic_retry(btstack_timer_source_t * ts){
gatt_client_t * gatt_client = gatt_client_for_timer(ts);
if (gatt_client != NULL){
gatt_client->state = P_W4_L2CAP_CONNECTION;
att_dispatch_classic_connect(gatt_client->addr, gatt_client->l2cap_psm, &gatt_client->l2cap_cid);
}if (gatt_client != NULL) { ... }
}gatt_client_classic_retry (btstack_timer_source_t * ts) { ... }
static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client){
gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
gatt_client_timeout_stop(gatt_client);
hci_con_handle_t con_handle = gatt_client->con_handle;
btstack_packet_handler_t callback = gatt_client->callback;
btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
btstack_memory_gatt_client_free(gatt_client);
uint8_t buffer[20];
uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, con_handle);
(*callback)(HCI_EVENT_PACKET, 0, buffer, len);
}gatt_client_classic_handle_disconnected (gatt_client_t * gatt_client) { ... }
static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){
des_iterator_t des_list_it;
des_iterator_t prot_it;
if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) {
gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
uint8_t *des_element;
uint8_t *element;
uint32_t uuid;
if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
des_element = des_iterator_get_element(&des_list_it);
des_iterator_init(&prot_it, des_element);
element = des_iterator_get_element(&prot_it);
if (de_get_element_type(element) != DE_UUID) continue;
uuid = de_get_uuid32(element);
des_iterator_next(&prot_it);
switch (uuid){
case BLUETOOTH_PROTOCOL_L2CAP:
if (!des_iterator_has_more(&prot_it)) continue;
de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm);
break;case BLUETOOTH_PROTOCOL_L2CAP:
default:
break;default
}switch (uuid) { ... }
}for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) { ... }
break;
case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
default:
break;default
}switch (sdp_event_query_attribute_byte_get_attribute_id(packet)) { ... }
}if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) { ... }
}if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) { ... }
}gatt_client_handle_sdp_client_query_attribute_value (gatt_client_t * connection, uint8_t *packet) { ... }
static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query;
btstack_assert(gatt_client != NULL);
uint8_t status;
switch (hci_event_packet_get_type(packet)){
case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet);
return;case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
case SDP_EVENT_QUERY_COMPLETE:
status = sdp_event_query_complete_get_status(packet);
gatt_client_classic_active_sdp_query = NULL;
log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status);
if (status != ERROR_CODE_SUCCESS) break;
if (gatt_client->l2cap_psm == 0) {
status = SDP_SERVICE_NOT_FOUND;
break;
}if (gatt_client->l2cap_psm == 0) { ... }
break;case SDP_EVENT_QUERY_COMPLETE:
default:
btstack_assert(false);
return;default
}switch (hci_event_packet_get_type(packet)) { ... }
if (status == ERROR_CODE_SUCCESS){
gatt_client->state = P_W4_L2CAP_CONNECTION;
status = att_dispatch_classic_connect(gatt_client->addr, gatt_client->l2cap_psm, &gatt_client->l2cap_cid);
}if (status == ERROR_CODE_SUCCESS) { ... }
if (status != ERROR_CODE_SUCCESS) {
gatt_client_classic_handle_connected(gatt_client, status);
}if (status != ERROR_CODE_SUCCESS) { ... }
}gatt_client_classic_sdp_handler (uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) { ... }
static void gatt_client_classic_sdp_start(void * context){
gatt_client_classic_active_sdp_query = (gatt_client_t *) context;
gatt_client_classic_active_sdp_query->state = P_W4_SDP_QUERY;
sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
}gatt_client_classic_sdp_start (void * context) { ... }
static void gatt_client_classic_emit_connected(void * context){
gatt_client_t * gatt_client = (gatt_client_t *) context;
gatt_client->state = P_READY;
hci_connection_t * hci_connection = hci_connection_for_handle(gatt_client->con_handle);
btstack_assert(hci_connection != NULL);
gatt_client_emit_connected(gatt_client->callback, ERROR_CODE_SUCCESS, hci_connection->address, gatt_client->con_handle);
}gatt_client_classic_emit_connected (void * context) { ... }
uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){
gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr);
if (gatt_client != NULL){
return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
}if (gatt_client != NULL) { ... }
gatt_client = btstack_memory_gatt_client_get();
if (gatt_client == NULL){
return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
}if (gatt_client == NULL) { ... }
gatt_client->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
gatt_client->con_handle = HCI_CON_HANDLE_INVALID;
memcpy(gatt_client->addr, addr, 6);
gatt_client->mtu = ATT_DEFAULT_MTU;
gatt_client->security_level = LEVEL_0;
gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
gatt_client->callback = callback;
#ifdef ENABLE_GATT_OVER_EATT
gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
#endif
btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
bool already_connected = false;
hci_connection_t * hci_connection = hci_connection_for_bd_addr_and_type(addr, BD_ADDR_TYPE_ACL);
if (hci_connection != NULL){
if (hci_connection->att_server.l2cap_cid != 0){
already_connected = true;
}if (hci_connection->att_server.l2cap_cid != 0) { ... }
}if (hci_connection != NULL) { ... }
gatt_client->callback_request.context = gatt_client;
if (already_connected){
gatt_client->con_handle = hci_connection->con_handle;
gatt_client->callback_request.callback = &gatt_client_classic_emit_connected;
gatt_client->state = P_W2_EMIT_CONNECTED;
btstack_run_loop_execute_on_main_thread(&gatt_client->callback_request);
}if (already_connected) { ... } else {
gatt_client->callback_request.callback = &gatt_client_classic_sdp_start;
gatt_client->state = P_W2_SDP_QUERY;
sdp_client_register_query_callback(&gatt_client->callback_request);
}else { ... }
return ERROR_CODE_SUCCESS;
}gatt_client_classic_connect (btstack_packet_handler_t callback, bd_addr_t addr) { ... }
uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
if (gatt_client == NULL){
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (gatt_client == NULL) { ... }
gatt_client->callback = callback;
return l2cap_disconnect(gatt_client->l2cap_cid);
}gatt_client_classic_disconnect (btstack_packet_handler_t callback, hci_con_handle_t con_handle) { ... }
/* ... */#endif
#ifdef ENABLE_GATT_OVER_EATT
#define MAX_NR_EATT_CHANNELS 5
static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static uint8_t gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client_t * gatt_client, gatt_client_state_t state){
uint8_t num_clients = 0;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it)){
gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (eatt_client->state == state){
num_clients++;
}if (eatt_client->state == state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return num_clients;
}gatt_client_le_enhanced_num_eatt_clients_in_state (gatt_client_t * gatt_client, gatt_client_state_t state) { ... }
static void gatt_client_eatt_finalize(gatt_client_t * gatt_client) {
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_connections);
while (btstack_linked_list_iterator_has_next(&it)) {
gatt_client_t *eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
btstack_linked_list_iterator_remove(&it);
btstack_memory_gatt_client_free(eatt_client);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}gatt_client_eatt_finalize (gatt_client_t * gatt_client) { ... }
static void gatt_client_le_enhanced_handle_connected(gatt_client_t * gatt_client, uint8_t status) {
if (status == ERROR_CODE_SUCCESS){
uint8_t num_ready = gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client, P_READY);
if (num_ready > 0){
gatt_client->eatt_state = GATT_CLIENT_EATT_READY;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_connections);
while (btstack_linked_list_iterator_has_next(&it)) {
gatt_client_t *eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
if (eatt_client->state == P_L2CAP_CLOSED){
btstack_linked_list_iterator_remove(&it);
btstack_memory_gatt_client_free(eatt_client);
}if (eatt_client->state == P_L2CAP_CLOSED) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}if (num_ready > 0) { ... } else {
hci_connection_t * hci_connection = hci_connection_for_handle(gatt_client->con_handle);
btstack_assert(hci_connection != NULL);
if (hci_connection->att_server.incoming_connection_request){
hci_connection->att_server.incoming_connection_request = false;
log_info("Collision, retry in 100ms");
gatt_client->state = P_W2_L2CAP_CONNECT;
btstack_run_loop_set_timer(&gatt_client->gc_timeout, GATT_CLIENT_COLLISION_BACKOFF_MS);
btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_le_enhanced_retry);
btstack_run_loop_add_timer(&gatt_client->gc_timeout);
return;
}if (hci_connection->att_server.incoming_connection_request) { ... } else {
gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
status = ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES;
}else { ... }
}else { ... }
}if (status == ERROR_CODE_SUCCESS) { ... } else {
gatt_client_eatt_finalize(gatt_client);
gatt_client->eatt_state = GATT_CLIENT_EATT_IDLE;
}else { ... }
gatt_client_emit_connected(gatt_client->callback, status, gatt_client->addr, gatt_client->con_handle);
}gatt_client_le_enhanced_handle_connected (gatt_client_t * gatt_client, uint8_t status) { ... }
static void gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client_t * gatt_client, gatt_client_t * eatt_client) {
gatt_client_report_error_if_pending(eatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
btstack_linked_list_remove(&gatt_client->eatt_clients, (btstack_linked_item_t *) eatt_client);
btstack_memory_gatt_client_free(eatt_client);
if (btstack_linked_list_empty(&gatt_client->eatt_clients)){
hci_connection_t * hci_connection = hci_connection_for_handle(gatt_client->con_handle);
hci_connection->att_server.eatt_outgoing_active = false;
if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) {
uint8_t buffer[20];
uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, gatt_client->con_handle);
(*gatt_client->callback)(HCI_EVENT_PACKET, 0, buffer, len);
}if (gatt_client->eatt_state == GATT_CLIENT_EATT_READY) { ... }
}if (btstack_linked_list_empty(&gatt_client->eatt_clients)) { ... }
}gatt_client_le_enhanced_handle_ecbm_disconnected (gatt_client_t * gatt_client, gatt_client_t * eatt_client) { ... }
static gatt_client_t * gatt_client_le_enhanced_get_context_for_l2cap_cid(uint16_t l2cap_cid, gatt_client_t ** out_eatt_client){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client_connections);
while (btstack_linked_list_iterator_has_next(&it)) {
gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
btstack_linked_list_iterator_t it2;
btstack_linked_list_iterator_init(&it2, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it2)) {
gatt_client_t * eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it2);
if (eatt_client->l2cap_cid == l2cap_cid){
*out_eatt_client = eatt_client;
return gatt_client;
}if (eatt_client->l2cap_cid == l2cap_cid) { ... }
}while (btstack_linked_list_iterator_has_next(&it2)) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}gatt_client_le_enhanced_get_context_for_l2cap_cid (uint16_t l2cap_cid, gatt_client_t ** out_eatt_client) { ... }
static void gatt_client_le_enhanced_setup_l2cap_channel(gatt_client_t * gatt_client){
uint8_t num_channels = gatt_client->eatt_num_clients;
uint16_t buffer_size_per_client = gatt_client->eatt_storage_size / num_channels;
uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2;
uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS];
uint16_t new_cids[MAX_NR_EATT_CHANNELS];
memset(gatt_client->eatt_storage_buffer, 0, gatt_client->eatt_storage_size);
uint8_t i;
for (i=0;i<gatt_client->eatt_num_clients; i++){
receive_buffers[i] = &gatt_client->eatt_storage_buffer[REPORT_PREBUFFER_HEADER];
gatt_client->eatt_storage_buffer += REPORT_PREBUFFER_HEADER + max_mtu;
}for (i=0;ieatt_num_clients; i++) { ... }
log_info("%u EATT clients with receive buffer size %u", gatt_client->eatt_num_clients, buffer_size_per_client);
uint8_t status = l2cap_ecbm_create_channels(&gatt_client_le_enhanced_packet_handler,
gatt_client->con_handle,
gatt_client->security_level,
BLUETOOTH_PSM_EATT, num_channels,
L2CAP_LE_AUTOMATIC_CREDITS,
buffer_size_per_client,
receive_buffers,
new_cids);
if (status == ERROR_CODE_SUCCESS){
i = 0;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &gatt_client->eatt_clients);
while (btstack_linked_list_iterator_has_next(&it)) {
gatt_client_t *new_eatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
new_eatt_client->bearer_type = ATT_BEARER_ENHANCED_LE;
new_eatt_client->con_handle = gatt_client->con_handle;
new_eatt_client->mtu = 64;
new_eatt_client->security_level = LEVEL_0;
new_eatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
new_eatt_client->state = P_W4_L2CAP_CONNECTION;
new_eatt_client->l2cap_cid = new_cids[i];
new_eatt_client->eatt_storage_buffer = gatt_client->eatt_storage_buffer;
gatt_client->eatt_storage_buffer += max_mtu;
i++;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
gatt_client->eatt_state = GATT_CLIENT_EATT_L2CAP_SETUP;
}if (status == ERROR_CODE_SUCCESS) { ... } else {
gatt_client_le_enhanced_handle_connected(gatt_client, status);
}else { ... }
}gatt_client_le_enhanced_setup_l2cap_channel (gatt_client_t * gatt_client) { ... }
static void gatt_client_le_enhanced_retry(btstack_timer_source_t * ts){
gatt_client_t * gatt_client = gatt_client_for_timer(ts);
if (gatt_client != NULL){
gatt_client->state = P_W4_L2CAP_CONNECTION;
gatt_client_le_enhanced_setup_l2cap_channel(gatt_client);
}if (gatt_client != NULL) { ... }
}gatt_client_le_enhanced_retry (btstack_timer_source_t * ts) { ... }
static void gatt_client_le_enhanced_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
gatt_client_t *gatt_client;
gatt_client_t *eatt_client;
hci_con_handle_t con_handle;
uint16_t l2cap_cid;
uint8_t status;
gatt_client_characteristic_t characteristic;
gatt_client_service_t service;
btstack_linked_list_iterator_t it;
uint8_t num_pending_channels;
switch (packet_type) {
case HCI_EVENT_PACKET:
switch (hci_event_packet_get_type(packet)) {
case GATT_EVENT_SERVICE_QUERY_RESULT:
con_handle = gatt_event_service_query_result_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
btstack_assert(gatt_client != NULL);
btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE);
gatt_event_service_query_result_get_service(packet, &service);
gatt_client->gatt_service_start_group_handle = service.start_group_handle;
gatt_client->gatt_service_end_group_handle = service.end_group_handle;
break;case GATT_EVENT_SERVICE_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
con_handle = gatt_event_characteristic_value_query_result_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
btstack_assert(gatt_client != NULL);
btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE);
if (gatt_event_characteristic_value_query_result_get_value_length(packet) >= 1) {
gatt_client->gatt_server_supported_features = gatt_event_characteristic_value_query_result_get_value(packet)[0];
}if (gatt_event_characteristic_value_query_result_get_value_length(packet) >= 1) { ... }
break;case GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT:
case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
con_handle = gatt_event_characteristic_query_result_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
btstack_assert(gatt_client != NULL);
btstack_assert(gatt_client->eatt_state == GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE);
gatt_event_characteristic_query_result_get_characteristic(packet, &characteristic);
gatt_client->gatt_client_supported_features_handle = characteristic.value_handle;
break;case GATT_EVENT_CHARACTERISTIC_QUERY_RESULT:
case GATT_EVENT_QUERY_COMPLETE:
con_handle = gatt_event_query_complete_get_handle(packet);
gatt_client = gatt_client_get_context_for_handle(con_handle);
btstack_assert(gatt_client != NULL);
switch (gatt_client->eatt_state){
case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE:
if (gatt_client->gatt_service_start_group_handle == 0){
gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
}if (gatt_client->gatt_service_start_group_handle == 0) { ... } else {
gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND;
}else { ... }
break;case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE:
case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE:
if ((gatt_client->gatt_server_supported_features & 1) == 0) {
gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
}if ((gatt_client->gatt_server_supported_features & 1) == 0) { ... } else {
gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND;
}else { ... }
break;case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE:
case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE:
if (gatt_client->gatt_client_supported_features_handle == 0){
gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE);
}if (gatt_client->gatt_client_supported_features_handle == 0) { ... } else {
gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND;
}else { ... }
break;case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE:
case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE:
gatt_client_le_enhanced_setup_l2cap_channel(gatt_client);
break;case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE:
default:
break;default
}switch (gatt_client->eatt_state) { ... }
break;case GATT_EVENT_QUERY_COMPLETE:
case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
l2cap_cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client);
btstack_assert(gatt_client != NULL);
btstack_assert(eatt_client != NULL);
btstack_assert(eatt_client->state == P_W4_L2CAP_CONNECTION);
status = l2cap_event_channel_opened_get_status(packet);
if (status == ERROR_CODE_SUCCESS){
eatt_client->state = P_READY;
eatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
}if (status == ERROR_CODE_SUCCESS) { ... } else {
eatt_client->state = P_L2CAP_CLOSED;
}else { ... }
if (gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client, P_W4_L2CAP_CONNECTION) == 0){
gatt_client_le_enhanced_handle_connected(gatt_client, ERROR_CODE_SUCCESS);
}if (gatt_client_le_enhanced_num_eatt_clients_in_state(gatt_client, P_W4_L2CAP_CONNECTION) == 0) { ... }
break;case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
case L2CAP_EVENT_CHANNEL_CLOSED:
l2cap_cid = l2cap_event_channel_closed_get_local_cid(packet);
gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(l2cap_cid, &eatt_client);
btstack_assert(gatt_client != NULL);
btstack_assert(eatt_client != NULL);
gatt_client_le_enhanced_handle_ecbm_disconnected(gatt_client, eatt_client);
break;case L2CAP_EVENT_CHANNEL_CLOSED:
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
break;case HCI_EVENT_PACKET:
case L2CAP_DATA_PACKET:
gatt_client = gatt_client_le_enhanced_get_context_for_l2cap_cid(channel, &eatt_client);
btstack_assert(gatt_client != NULL);
btstack_assert(eatt_client != NULL);
gatt_client_handle_att_response(eatt_client, packet, size);
gatt_client_run();
break;case L2CAP_DATA_PACKET:
default:
break;default
}switch (packet_type) { ... }
}gatt_client_le_enhanced_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { ... }
static bool gatt_client_le_enhanced_handle_can_send_query(gatt_client_t * gatt_client){
uint8_t status = ERROR_CODE_SUCCESS;
uint8_t gatt_client_supported_features = 0x06;
switch (gatt_client->eatt_state){
case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND:
gatt_client->gatt_service_start_group_handle = 0;
gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE;
status = gatt_client_discover_primary_services_by_uuid16(&gatt_client_le_enhanced_packet_handler,
gatt_client->con_handle,
ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
break;case GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND:
case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND:
gatt_client->gatt_server_supported_features = 0;
gatt_client->eatt_state = GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE;
status = gatt_client_read_value_of_characteristics_by_uuid16(&gatt_client_le_enhanced_packet_handler,
gatt_client->con_handle,
gatt_client->gatt_service_start_group_handle,
gatt_client->gatt_service_end_group_handle,
ORG_BLUETOOTH_CHARACTERISTIC_SERVER_SUPPORTED_FEATURES);
return true;case GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND:
case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND:
gatt_client->gatt_client_supported_features_handle = 0;
gatt_client->eatt_state = GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE;
status = gatt_client_discover_characteristics_for_handle_range_by_uuid16(&gatt_client_le_enhanced_packet_handler,
gatt_client->con_handle,
gatt_client->gatt_service_start_group_handle,
gatt_client->gatt_service_end_group_handle,
ORG_BLUETOOTH_CHARACTERISTIC_CLIENT_SUPPORTED_FEATURES);
return true;case GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND:
case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND:
gatt_client->eatt_state = GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE;
status = gatt_client_write_value_of_characteristic(&gatt_client_le_enhanced_packet_handler, gatt_client->con_handle,
gatt_client->gatt_client_supported_features_handle, 1,
&gatt_client_supported_features);
return true;case GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND:
default:
break;default
}switch (gatt_client->eatt_state) { ... }
btstack_assert(status == ERROR_CODE_SUCCESS);
UNUSED(status);
return false;
}gatt_client_le_enhanced_handle_can_send_query (gatt_client_t * gatt_client) { ... }
uint8_t gatt_client_le_enhanced_connect(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint8_t num_channels, uint8_t * storage_buffer, uint16_t storage_size) {
gatt_client_t * gatt_client;
uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
if (status != ERROR_CODE_SUCCESS){
return status;
}if (status != ERROR_CODE_SUCCESS) { ... }
if (gatt_client->eatt_state != GATT_CLIENT_EATT_IDLE){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (gatt_client->eatt_state != GATT_CLIENT_EATT_IDLE) { ... }
uint16_t buffer_size_per_client = storage_size / num_channels;
uint16_t max_mtu = (buffer_size_per_client - REPORT_PREBUFFER_HEADER) / 2;
if (max_mtu < 64) {
return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (max_mtu < 64) { ... }
if ((num_channels == 0) || (num_channels > MAX_NR_EATT_CHANNELS)){
return ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if ((num_channels == 0) || (num_channels > MAX_NR_EATT_CHANNELS)) { ... }
uint8_t i;
btstack_linked_list_t eatt_clients = NULL;
for (i=0;i<num_channels;i++) {
gatt_client_t * new_gatt_client = btstack_memory_gatt_client_get();
if (new_gatt_client == NULL) {
break;
}if (new_gatt_client == NULL) { ... }
btstack_linked_list_add(&eatt_clients, (btstack_linked_item_t*)new_gatt_client);
}for (i=0;i
if (i != num_channels){
while (true){
gatt_client = (gatt_client_t *) btstack_linked_list_pop(&eatt_clients);
if (gatt_client == NULL) {
break;
}if (gatt_client == NULL) { ... }
btstack_memory_gatt_client_free(gatt_client);
}while (true) { ... }
return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
}if (i != num_channels) { ... }
hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
hci_connection->att_server.eatt_outgoing_active = true;
gatt_client->callback = callback;
gatt_client->eatt_num_clients = num_channels;
gatt_client->eatt_storage_buffer = storage_buffer;
gatt_client->eatt_storage_size = storage_size;
gatt_client->eatt_clients = eatt_clients;
gatt_client->eatt_state = GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND;
gatt_client_notify_can_send_query(gatt_client);
return ERROR_CODE_SUCCESS;
}gatt_client_le_enhanced_connect (btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint8_t num_channels, uint8_t * storage_buffer, uint16_t storage_size) { ... }
/* ... */
#endif
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
gatt_client_att_packet_handler(packet_type, handle, packet, size);
}gatt_client_att_packet_handler_fuzz (uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) { ... }
uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
return status;
}gatt_client_get_client (hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client) { ... }
/* ... */#endif