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
44
45
46
47
48
49
50
51
52
53
54
55
56
59
60
61
62
63
66
72
73
76
81
82
85
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
139
140
141
142
143
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
183
184
185
190
191
192
193
194
195
196
197
198
199
200
201
202
205
206
207
210
211
212
213
214
216
217
218
220
221
222
223
226
227
228
229
230
231
232
233
237
238
239
240
241
242
244
245
246
247
248
249
250
251
252
253
254
255
256
258
259
260
261
262
263
264
265
266
267
268
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
297
298
301
302
305
306
309
310
311
312
313
314
315
316
323
324
325
326
327
332
333
338
339
340
341
342
343
344
345
346
347
348
352
353
354
355
356
357
358
359
360
361
362
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
449
450
451
452
453
454
455
456
457
458
459
460
461
462
466
467
471
472
473
474
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
568
569
570
571
572
579
580
581
582
583
587
591
595
599
603
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
682
683
684
685
686
687
688
689
690
693
694
695
696
706
707
708
709
710
711
712
718
719
720
721
722
723
727
728
729
730
731
732
733
734
735
736
737
738
739
749
750
751
752
753
754
755
759
760
773
774
787
788
789
790
791
792
793
794
795
796
801
802
803
804
805
806
807
808
809
810
811
812
813
814
817
818
822
823
824
832
833
834
835
836
837
838
839
842
843
844
845
849
850
851
852
853
854
855
856
857
858
859
860
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
902
903
904
905
914
915
916
917
918
929
930
938
939
940
941
942
943
944
945
946
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
968
969
970
971
974
975
976
977
978
979
980
981
982
983
984
985
988
989
990
991
994
995
996
997
998
1003
1004
1005
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1021
1022
1025
1026
1027
1028
1029
1030
1031
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1051
1052
1055
1056
1057
1060
1061
1062
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1084
1085
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1162
1163
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1194
1195
1196
1197
1198
1214
1215
1226
1227
1228
1232
1233
1234
1235
1245
1246
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1304
1305
1306
1307
1331
1332
1333
1334
1338
1339
1340
1344
1348
1351
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1366
1367
1368
1369
1370
1371
1372
1380
1381
1382
1383
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1415
1416
1417
1418
1423
1424
1425
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1473
1474
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1495
1496
1497
1501
1502
1506
1507
1508
1509
1510
1511
1512
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1544
1545
1549
1550
1554
1555
1556
1557
1558
1559
1560
1561
1564
1565
1568
1569
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1589
1590
1591
1592
1593
1594
1595
1596
1601
1602
1603
1604
1605
1611
1612
1613
1619
1620
1630
1631
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1720
1721
1722
1723
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1891
1892
1893
1895
1896
1897
1898
1899
1900
1905
1906
1907
1908
1909
1910
1911
1916
1919
1922
1923
1924
1930
1936
1937
1938
1939
1940
1941
1942
1947
1948
1949
1950
1951
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1996
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2068
2073
2078
2083
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2104
2105
2106
2107
2108
2109
2111
2113
2115
2116
2117
2118
2120
2123
2124
2125
2126
2127
2128
2129
2130
2134
2135
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2269
2270
2271
2272
2273
2274
2275
2276
2277
2286
2287
2288
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2381
2382
2386
2387
2388
2389
2390
2391
2392
2393
2394
2410
2411
2412
2413
2414
2415
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2467
2477
2478
2479
2480
2481
2482
2483
2484
2485
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2635
2636
2637
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2660
2661
2662
2663
2664
2665
2666
2667
2668
2678
2679
2680
2686
2688
2689
2690
2691
2692
2693
2694
2695
2696
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2717
2722
2726
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2804
2808
2809
2810
2811
2812
2813
2822
2823
2824
2833
2834
2835
2836
2839
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2855
2858
2859
2860
2861
2862
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2880
2881
2882
2883
2884
2885
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2961
2966
2967
2968
2969
2973
2974
2978
2981
2982
2983
2984
2985
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3013
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3030
3031
3032
3033
3034
3035
3036
3058
3059
3060
3061
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3081
3082
3083
3084
3085
3086
3087
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3115
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3133
3134
3135
3140
3141
3142
3143
3151
3152
3153
3154
3155
3159
3160
3161
3162
3163
3164
3165
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3188
3189
3190
3191
3192
3193
3197
3198
3199
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3254
3255
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3301
3302
3318
3320
3321
3322
3325
3326
3327
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3384
3385
3386
3391
3392
3393
3394
3395
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
3426
3427
3428
3429
3430
3431
3432
3433
3438
3439
3440
3441
3447
3451
3452
3453
3454
3455
3456
3457
3458
3461
3462
3463
3464
3465
3466
3467
3468
3469
3472
3473
3474
3475
3476
3477
3490
3491
3496
3497
3498
3499
3500
3503
3506
3507
3508
3513
3514
3515
3516
3517
3518
3519
3521
3522
3523
3524
3525
3535
3536
3537
3538
3539
3540
3541
3552
3556
3559
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3669
3673
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3693
3694
3700
3701
3702
3703
3704
3705
3706
3709
3710
3711
3712
3713
3714
3716
3717
3718
3719
3720
3721
3722
3723
3726
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3753
3754
3755
3756
3757
3758
3759
3760
3761
3779
3780
3781
3782
3783
3784
3785
3786
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3806
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3844
3845
3846
3853
3854
3855
3856
3857
3860
3861
3862
3863
3865
3867
3869
3870
3871
3872
3874
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3910
3911
3912
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3949
3950
3951
3954
3955
3956
3957
3958
3959
3960
3961
3962
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4031
4032
4033
4034
4035
4036
4037
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4118
4119
4120
4131
4132
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4215
4216
4220
4221
4225
4226
4230
4231
4232
4233
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4276
4277
4278
4279
4280
4281
4282
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4311
4312
4313
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4457
4458
4459
4465
4466
4467
4473
4474
4475
4476
4477
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4577
4578
4579
4580
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4623
4624
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4652
4653
4654
4655
4656
4657
4658
4661
4662
4663
4664
4665
4670
4671
4672
4673
4674
4675
4676
4679
4680
4681
4682
4683
4689
4692
4693
4694
4697
4698
4699
4705
4706
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4735
4736
4739
4742
4743
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4827
4828
4829
4830
4831
4832
4833
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4860
4868
4869
4870
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4915
4924
4925
4926
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4948
4949
4950
4951
4952
4953
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4999
5000
5001
5002
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5085
5086
5087
5088
5094
5095
5096
5097
5098
5102
5103
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5122
5123
5124
5127
5128
5129
5130
5131
5132
5133
5134
5135
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5170
5171
5172
5173
5174
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5194
5195
5196
5197
5200
5201
5202
5203
5204
5205
5206
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5225
5226
5227
5232
5233
5234
5235
5236
5237
5238
5239
5240
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5307
5308
5309
5310
5311
5312
5313
5314
5317
5318
5319
5320
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5443
5444
5445
5448
5449
5450
5451
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5472
5473
5474
5475
5476
5481
5482
5483
5484
5485
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5500
5501
5502
5503
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5534
5535
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5577
5578
5579
5580
5581
5582
5583
5584
5590
5591
5592
5593
5594
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5684
5685
5686
5687
5688
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5722
5723
5724
5725
5738
5739
5740
5741
5745
5746
5747
5751
5752
5753
5757
5758
5759
5763
5764
5765
5771
5772
5773
5777
5778
5779
5783
5784
5785
5789
5790
5791
5795
5796
5797
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5847
/* ... */
#define BTSTACK_FILE__ "l2cap.c"
/* ... */
#include "l2cap.h"
#include "hci.h"
#include "hci_dump.h"
#include "bluetooth_sdp.h"
#include "bluetooth_psm.h"
#include "btstack_bool.h"
#include "btstack_debug.h"
#include "btstack_event.h"
#include "btstack_memory.h"
9 includes
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
#include "ble/sm.h"
/* ... */#endif
#include <stdarg.h>
#include <string.h>
/* ... */
typedef enum {
L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY = 0,
L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT,
L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY,
L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT
...} l2cap_supervisory_function_t;
/* ... */
typedef enum {
L2CAP_INFO_TYPE_CONNECTIONLESS_MTU = 1,
L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED,
L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED,
...} l2cap_info_type_t;
/* ... */
typedef enum {
L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT = 1,
L2CAP_CONFIG_OPTION_TYPE_FLUSH_TIMEOUT,
L2CAP_CONFIG_OPTION_TYPE_QUALITY_OF_SERVICE,
L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL,
L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE,
L2CAP_CONFIG_OPTION_TYPE_EXTENDED_FLOW_SPECIFICATION,
L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE,
...} l2cap_config_option_type_t;
#define L2CAP_SIG_ID_INVALID 0
#define COMPLETE_L2CAP_HEADER (HCI_ACL_HEADER_SIZE + L2CAP_HEADER_SIZE)
#define L2CAP_CONF_RESULT_SUCCESS 0x0000
#define L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS 0x0001
#define L2CAP_CONF_RESULT_REJECT 0x0002
#define L2CAP_CONF_RESULT_UNKNOWN_OPTIONS 0x0003
#define L2CAP_CONF_RESULT_PENDING 0x0004
#define L2CAP_CONF_RESULT_FLOW_SPEC_REJECTED 0x0005
#define L2CAP_REJ_CMD_UNKNOWN 0x0000
#define L2CAP_REJ_MTU_EXCEEDED 0x0001
#define L2CAP_REJ_INVALID_CID 0x0002
#define L2CAP_RTX_TIMEOUT_MS 10000
#define L2CAP_ERTX_TIMEOUT_MS 120000
#define NR_BUFFERED_ACL_PACKETS 3
#define NR_PENDING_SIGNALING_RESPONSES 3
#define L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_AUTOMATIC_CREDITS_WATERMARK 5
#define L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_AUTOMATIC_CREDITS_INCREMENT 5
#define L2CAP_SIGNALING_COMMAND_CODE_OFFSET 0
#define L2CAP_SIGNALING_COMMAND_SIGID_OFFSET 1
#define L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET 2
#define L2CAP_SIGNALING_COMMAND_DATA_OFFSET 4
21 defines
#if defined(ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE) && !defined(ENABLE_CLASSIC)
#error "ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE depends on ENABLE_CLASSIC."
#error "Please remove ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE or add ENABLE_CLASSIC in btstack_config.h"/* ... */
#endif
#ifdef ENABLE_LE_DATA_CHANNELS
#warning "ENABLE_LE_DATA_CHANNELS has been deprecated."
#warning "Please use ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE instead in btstack_config.h"
#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
/* ... */#endif
#if defined(ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE) || defined(ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE)
#define L2CAP_USES_CREDIT_BASED_CHANNELS
#endif
#if defined(L2CAP_USES_CREDIT_BASED_CHANNELS) || defined(ENABLE_CLASSIC)
#define L2CAP_USES_CHANNELS
#endif
static void l2cap_run(void);
static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size );
static void l2cap_notify_channel_can_send(void);
static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel);
static uint8_t l2cap_next_sig_id(void);
static l2cap_fixed_channel_t * l2cap_fixed_channel_for_channel_id(uint16_t local_cid);
#ifdef ENABLE_CLASSIC
static void l2cap_handle_security_level_incoming_sufficient(l2cap_channel_t * channel);
static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm);
static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel);
static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel);
static void l2cap_handle_information_request_complete(hci_connection_t * connection);
static inline l2cap_service_t * l2cap_get_service(uint16_t psm);
static void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
static void l2cap_emit_channel_closed(l2cap_channel_t *channel);
static void l2cap_emit_incoming_connection(l2cap_channel_t *channel);
static int l2cap_channel_ready_for_open(l2cap_channel_t *channel);
static uint8_t l2cap_classic_send(l2cap_channel_t * channel, const uint8_t *data, uint16_t len);/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status);
static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel);
static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel);
static void l2cap_cbm_finialize_channel_close(l2cap_channel_t *channel);
static inline l2cap_service_t * l2cap_cbm_get_service(uint16_t le_psm);/* ... */
#endif
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size);
static void l2cap_credit_based_send_pdu(l2cap_channel_t *channel);
static void l2cap_credit_based_send_credits(l2cap_channel_t *channel);
static bool l2cap_credit_based_handle_credit_indication(hci_con_handle_t handle, const uint8_t * command, uint16_t len);
static void l2cap_credit_based_handle_pdu(l2cap_channel_t * l2cap_channel, const uint8_t * packet, uint16_t size);/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
static uint16_t l2cap_next_local_cid(void);
static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid);
static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code);
static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size);
static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, l2cap_channel_type_t channel_type, bd_addr_t address, bd_addr_type_t address_type,
uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level);
static void l2cap_finalize_channel_close(l2cap_channel_t *channel);
static void l2cap_free_channel_entry(l2cap_channel_t * channel);/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
static void l2cap_ertm_notify_channel_can_send(l2cap_channel_t * channel);
static void l2cap_ertm_monitor_timeout_callback(btstack_timer_source_t * ts);
static void l2cap_ertm_retransmission_timeout_callback(btstack_timer_source_t * ts);/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
static void l2cap_ecbm_handle_security_level_incoming(l2cap_channel_t *channel);
static int l2cap_ecbm_signaling_handler_dispatch(hci_con_handle_t handle, uint16_t signaling_cid, uint8_t * command, uint8_t sig_id);
static void l2cap_run_trigger_callback(void * context);/* ... */
#endif
#ifdef ENABLE_BLE
static l2cap_fixed_channel_t l2cap_fixed_channel_le_att;
static l2cap_fixed_channel_t l2cap_fixed_channel_le_sm;/* ... */
#endif
#ifdef ENABLE_CLASSIC
static l2cap_fixed_channel_t l2cap_fixed_channel_classic_connectionless;
static l2cap_fixed_channel_t l2cap_fixed_channel_classic_sm;/* ... */
#endif
#ifdef ENABLE_CLASSIC
static btstack_linked_list_t l2cap_services;
static uint8_t l2cap_require_security_level2_for_outgoing_sdp;
static bd_addr_t l2cap_outgoing_classic_addr;/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
static btstack_linked_list_t l2cap_le_services;
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
static btstack_linked_list_t l2cap_enhanced_services;
static uint16_t l2cap_enhanced_mps_min;
static uint16_t l2cap_enhanced_mps_max;
static btstack_context_callback_registration_t l2cap_trigger_run_registration;/* ... */
#endif
static btstack_linked_list_t l2cap_channels;
#ifdef L2CAP_USES_CHANNELS
static uint16_t l2cap_local_source_cid;/* ... */
#endif
static uint8_t l2cap_sig_seq_nr;
static l2cap_signaling_response_t l2cap_signaling_responses[NR_PENDING_SIGNALING_RESPONSES];
static int l2cap_signaling_responses_pending;
static btstack_packet_callback_registration_t l2cap_hci_event_callback_registration;
static bool l2cap_call_notify_channel_in_run;
#ifdef ENABLE_BLE
static uint16_t l2cap_le_custom_max_mtu;/* ... */
#endif
static btstack_linked_list_t l2cap_event_handlers;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
/* ... */
static const uint16_t crc16_table[256] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40, 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40, 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240, 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640, 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240, 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41, 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40, 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
...};
static uint16_t crc16_calc(uint8_t * data, uint16_t len){
uint16_t crc = 0;
while (len--){
crc = (crc >> 8) ^ crc16_table[ (crc ^ ((uint16_t) *data++)) & 0x00FF ];
}while (len--) { ... }
return crc;
}crc16_calc (uint8_t * data, uint16_t len) { ... }
static inline uint16_t l2cap_encanced_control_field_for_information_frame(uint8_t tx_seq, int final, uint8_t req_seq, l2cap_segmentation_and_reassembly_t sar){
return (((uint16_t) sar) << 14) | (req_seq << 8) | (final << 7) | (tx_seq << 1) | 0;
}l2cap_encanced_control_field_for_information_frame (uint8_t tx_seq, int final, uint8_t req_seq, l2cap_segmentation_and_reassembly_t sar) { ... }
static inline uint16_t l2cap_encanced_control_field_for_supevisor_frame(l2cap_supervisory_function_t supervisory_function, int poll, int final, uint8_t req_seq){
return (req_seq << 8) | (final << 7) | (poll << 4) | (((int) supervisory_function) << 2) | 1;
}l2cap_encanced_control_field_for_supevisor_frame (l2cap_supervisory_function_t supervisory_function, int poll, int final, uint8_t req_seq) { ... }
static int l2cap_next_ertm_seq_nr(int seq_nr){
return (seq_nr + 1) & 0x3f;
}l2cap_next_ertm_seq_nr (int seq_nr) { ... }
static bool l2cap_ertm_can_store_packet_now(l2cap_channel_t * channel){
int num_free_tx_buffers = channel->num_tx_buffers - channel->num_stored_tx_frames;
int num_tx_buffers_for_max_remote_mtu;
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
if (channel->remote_mtu <= effective_mps){
num_tx_buffers_for_max_remote_mtu = 1;
}if (channel->remote_mtu <= effective_mps) { ... } else {
num_tx_buffers_for_max_remote_mtu = (channel->remote_mtu + 2 + (effective_mps - 1)) / effective_mps;
}else { ... }
log_debug("num_free_tx_buffers %u, num_tx_buffers_for_max_remote_mtu %u", num_free_tx_buffers, num_tx_buffers_for_max_remote_mtu);
return num_tx_buffers_for_max_remote_mtu <= num_free_tx_buffers;
}l2cap_ertm_can_store_packet_now (l2cap_channel_t * channel) { ... }
static void l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel_t * l2cap_channel){
log_info("Retransmit unacknowleged frames");
l2cap_channel->unacked_frames = 0;;
l2cap_channel->tx_send_index = l2cap_channel->tx_read_index;
}l2cap_ertm_retransmit_unacknowleded_frames (l2cap_channel_t * l2cap_channel) { ... }
static void l2cap_ertm_next_tx_write_index(l2cap_channel_t * channel){
channel->tx_write_index++;
if (channel->tx_write_index < channel->num_tx_buffers) return;
channel->tx_write_index = 0;
}l2cap_ertm_next_tx_write_index (l2cap_channel_t * channel) { ... }
static void l2cap_ertm_start_monitor_timer(l2cap_channel_t * channel){
log_info("Start Monitor timer");
btstack_run_loop_remove_timer(&channel->monitor_timer);
btstack_run_loop_set_timer_handler(&channel->monitor_timer, &l2cap_ertm_monitor_timeout_callback);
btstack_run_loop_set_timer_context(&channel->monitor_timer, channel);
btstack_run_loop_set_timer(&channel->monitor_timer, channel->local_monitor_timeout_ms);
btstack_run_loop_add_timer(&channel->monitor_timer);
}l2cap_ertm_start_monitor_timer (l2cap_channel_t * channel) { ... }
static void l2cap_ertm_stop_monitor_timer(l2cap_channel_t * channel){
log_info("Stop Monitor timer");
btstack_run_loop_remove_timer(&channel->monitor_timer);
}l2cap_ertm_stop_monitor_timer (l2cap_channel_t * channel) { ... }
static void l2cap_ertm_start_retransmission_timer(l2cap_channel_t * channel){
log_info("Start Retransmission timer");
btstack_run_loop_remove_timer(&channel->retransmission_timer);
btstack_run_loop_set_timer_handler(&channel->retransmission_timer, &l2cap_ertm_retransmission_timeout_callback);
btstack_run_loop_set_timer_context(&channel->retransmission_timer, channel);
btstack_run_loop_set_timer(&channel->retransmission_timer, channel->local_retransmission_timeout_ms);
btstack_run_loop_add_timer(&channel->retransmission_timer);
}l2cap_ertm_start_retransmission_timer (l2cap_channel_t * channel) { ... }
static void l2cap_ertm_stop_retransmission_timer(l2cap_channel_t * l2cap_channel){
log_info("Stop Retransmission timer");
btstack_run_loop_remove_timer(&l2cap_channel->retransmission_timer);
}l2cap_ertm_stop_retransmission_timer (l2cap_channel_t * l2cap_channel) { ... }
static void l2cap_ertm_monitor_timeout_callback(btstack_timer_source_t * ts){
log_info("Monitor timeout");
l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts);
l2cap_ertm_tx_packet_state_t * tx_state;
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
if (tx_state->retry_count < l2cap_channel->remote_max_transmit){
tx_state->retry_count++;
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
l2cap_ertm_start_monitor_timer(l2cap_channel);
l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1;
}if (tx_state->retry_count < l2cap_channel->remote_max_transmit) { ... } else {
log_info("Monitor timer expired & retry count >= max transmit -> disconnect");
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
}else { ... }
l2cap_run();
}l2cap_ertm_monitor_timeout_callback (btstack_timer_source_t * ts) { ... }
static void l2cap_ertm_retransmission_timeout_callback(btstack_timer_source_t * ts){
log_info("Retransmission timeout");
l2cap_channel_t * l2cap_channel = (l2cap_channel_t *) btstack_run_loop_get_timer_context(ts);
l2cap_ertm_tx_packet_state_t * tx_state;
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
tx_state->retry_count = 1;
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
l2cap_ertm_start_monitor_timer(l2cap_channel);
l2cap_channel->send_supervisor_frame_receiver_ready_poll = 1;
l2cap_run();
}l2cap_ertm_retransmission_timeout_callback (btstack_timer_source_t * ts) { ... }
static int l2cap_ertm_send_information_frame(l2cap_channel_t * channel, int index, int final){
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index];
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
uint16_t control = l2cap_encanced_control_field_for_information_frame(tx_state->tx_seq, final, channel->req_seq, tx_state->sar);
log_info("I-Frame: control 0x%04x", control);
little_endian_store_16(acl_buffer, 8, control);
(void)memcpy(&acl_buffer[8 + 2],
&channel->tx_packets_data[index * channel->local_mps],
tx_state->len);
l2cap_ertm_start_retransmission_timer(channel);
return l2cap_send_prepared(channel->local_cid, 2 + tx_state->len);
}l2cap_ertm_send_information_frame (l2cap_channel_t * channel, int index, int final) { ... }
static void l2cap_ertm_store_fragment(l2cap_channel_t * channel, l2cap_segmentation_and_reassembly_t sar, uint16_t sdu_length, const uint8_t * data, uint16_t len){
int index = channel->tx_write_index;
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[index];
tx_state->tx_seq = channel->next_tx_seq;
tx_state->sar = sar;
tx_state->retry_count = 0;
uint8_t * tx_packet = &channel->tx_packets_data[index * channel->local_mps];
log_debug("index %u, local mps %u, remote mps %u, packet tx %p, len %u", index, channel->local_mps, channel->remote_mps, tx_packet, len);
int pos = 0;
if (sar == L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU){
little_endian_store_16(tx_packet, 0, sdu_length);
pos += 2;
}if (sar == L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU) { ... }
(void)memcpy(&tx_packet[pos], data, len);
tx_state->len = pos + len;
channel->num_stored_tx_frames++;
channel->next_tx_seq = l2cap_next_ertm_seq_nr(channel->next_tx_seq);
l2cap_ertm_next_tx_write_index(channel);
log_info("l2cap_ertm_store_fragment: tx_read_index %u, tx_write_index %u, num stored %u", channel->tx_read_index, channel->tx_write_index, channel->num_stored_tx_frames);
}l2cap_ertm_store_fragment (l2cap_channel_t * channel, l2cap_segmentation_and_reassembly_t sar, uint16_t sdu_length, const uint8_t * data, uint16_t len) { ... }
static uint8_t l2cap_ertm_send(l2cap_channel_t * channel, const uint8_t * data, uint16_t len){
if (len > channel->remote_mtu){
log_error("l2cap_ertm_send cid 0x%02x, data length exceeds remote MTU.", channel->local_cid);
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
}if (len > channel->remote_mtu) { ... }
if (!l2cap_ertm_can_store_packet_now(channel)){
log_error("l2cap_ertm_send cid 0x%02x, fragment store full", channel->local_cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (!l2cap_ertm_can_store_packet_now(channel)) { ... }
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
if (len > effective_mps){
l2cap_segmentation_and_reassembly_t sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU;
uint16_t chunk_len = 0;
while (len){
switch (sar){
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
chunk_len = effective_mps - 2;
l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len);
sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU;
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
chunk_len = effective_mps;
if (chunk_len >= len){
sar = L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU;
chunk_len = len;
}if (chunk_len >= len) { ... }
l2cap_ertm_store_fragment(channel, sar, len, data, chunk_len);
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
default:
btstack_unreachable();
break;default
}switch (sar) { ... }
len -= chunk_len;
data += chunk_len;
}while (len) { ... }
}if (len > effective_mps) { ... } else {
l2cap_ertm_store_fragment(channel, L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU, 0, data, len);
}else { ... }
l2cap_notify_channel_can_send();
return ERROR_CODE_SUCCESS;
}l2cap_ertm_send (l2cap_channel_t * channel, const uint8_t * data, uint16_t len) { ... }
static uint16_t l2cap_setup_options_ertm_request(l2cap_channel_t * channel, uint8_t * config_options){
int pos = 0;
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL;
config_options[pos++] = 9;
config_options[pos++] = (uint8_t) channel->mode;
config_options[pos++] = channel->num_rx_buffers;
config_options[pos++] = channel->local_max_transmit;
little_endian_store_16( config_options, pos, channel->local_retransmission_timeout_ms);
pos += 2;
little_endian_store_16( config_options, pos, channel->local_monitor_timeout_ms);
pos += 2;
little_endian_store_16( config_options, pos, channel->local_mps);
pos += 2;
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT;
config_options[pos++] = 2;
little_endian_store_16(config_options, pos, channel->local_mtu);
pos += 2;
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE;
config_options[pos++] = 1;
config_options[pos++] = channel->fcs_option;
return pos;
}l2cap_setup_options_ertm_request (l2cap_channel_t * channel, uint8_t * config_options) { ... }
static uint16_t l2cap_setup_options_ertm_response(l2cap_channel_t * channel, uint8_t * config_options){
int pos = 0;
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL;
config_options[pos++] = 9;
config_options[pos++] = (uint8_t) channel->mode;
config_options[pos++] = btstack_min(channel->num_tx_buffers, channel->remote_tx_window_size);
config_options[pos++] = channel->remote_max_transmit;
little_endian_store_16( config_options, pos, channel->local_retransmission_timeout_ms);
pos += 2;
little_endian_store_16( config_options, pos, channel->local_monitor_timeout_ms);
pos += 2;
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
little_endian_store_16( config_options, pos, effective_mps);
pos += 2;
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT;
config_options[pos++] = 2;
little_endian_store_16(config_options, pos, channel->remote_mtu);
pos += 2;
#if 0
config_options[pos++] = L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE;
config_options[pos++] = 1;
config_options[pos++] = channel->fcs_option;/* ... */
#endif
return pos;
}l2cap_setup_options_ertm_response (l2cap_channel_t * channel, uint8_t * config_options) { ... }
static int l2cap_ertm_send_supervisor_frame(l2cap_channel_t * channel, uint16_t control){
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
log_info("S-Frame: control 0x%04x", control);
little_endian_store_16(acl_buffer, 8, control);
return l2cap_send_prepared(channel->local_cid, 2);
}l2cap_ertm_send_supervisor_frame (l2cap_channel_t * channel, uint16_t control) { ... }
static uint8_t l2cap_ertm_validate_local_config(l2cap_ertm_config_t * ertm_config){
uint8_t result = ERROR_CODE_SUCCESS;
if (ertm_config->max_transmit < 1){
log_error("max_transmit must be >= 1");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->max_transmit < 1) { ... }
if (ertm_config->retransmission_timeout_ms < 2000){
log_error("retransmission_timeout_ms must be >= 2000 ms");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->retransmission_timeout_ms < 2000) { ... }
if (ertm_config->monitor_timeout_ms < 12000){
log_error("monitor_timeout_ms must be >= 12000 ms");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->monitor_timeout_ms < 12000) { ... }
if (ertm_config->local_mtu < 48){
log_error("local_mtu must be >= 48");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->local_mtu < 48) { ... }
if (ertm_config->num_rx_buffers < 1){
log_error("num_rx_buffers must be >= 1");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->num_rx_buffers < 1) { ... }
if (ertm_config->num_tx_buffers < 1){
log_error("num_rx_buffers must be >= 1");
result = ERROR_CODE_INVALID_HCI_COMMAND_PARAMETERS;
}if (ertm_config->num_tx_buffers < 1) { ... }
return result;
}l2cap_ertm_validate_local_config (l2cap_ertm_config_t * ertm_config) { ... }
static void l2cap_ertm_setup_buffers(l2cap_channel_t * channel, uint8_t * buffer, uint32_t size){
btstack_assert( (((uintptr_t) buffer) & 0x0f) == 0);
uint32_t pos = 0;
channel->rx_packets_state = (l2cap_ertm_rx_packet_state_t *) (void *) &buffer[pos];
pos += channel->num_rx_buffers * sizeof(l2cap_ertm_rx_packet_state_t);
channel->tx_packets_state = (l2cap_ertm_tx_packet_state_t *) (void *) &buffer[pos];
pos += channel->num_tx_buffers * sizeof(l2cap_ertm_tx_packet_state_t);
channel->reassembly_buffer = &buffer[pos];
pos += channel->local_mtu;
channel->rx_packets_data = &buffer[pos];
pos += channel->num_rx_buffers * channel->local_mps;
channel->tx_packets_data = &buffer[pos];
pos += channel->num_rx_buffers * channel->remote_mps;
btstack_assert(pos <= size);
UNUSED(pos);
}l2cap_ertm_setup_buffers (l2cap_channel_t * channel, uint8_t * buffer, uint32_t size) { ... }
static void l2cap_ertm_configure_channel(l2cap_channel_t * channel, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size){
channel->mode = L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION;
channel->ertm_mandatory = ertm_config->ertm_mandatory;
channel->local_max_transmit = ertm_config->max_transmit;
channel->local_retransmission_timeout_ms = ertm_config->retransmission_timeout_ms;
channel->local_monitor_timeout_ms = ertm_config->monitor_timeout_ms;
channel->local_mtu = ertm_config->local_mtu;
channel->num_rx_buffers = ertm_config->num_rx_buffers;
channel->num_tx_buffers = ertm_config->num_tx_buffers;
channel->fcs_option = ertm_config->fcs_option;
int bytes_till_alignment = 16 - (((uintptr_t) buffer) & 0x0f);
buffer += bytes_till_alignment;
size -= bytes_till_alignment;
uint32_t state_len = channel->num_rx_buffers * sizeof(l2cap_ertm_rx_packet_state_t) + channel->num_tx_buffers * sizeof(l2cap_ertm_tx_packet_state_t);
uint32_t buffer_space = size - state_len - channel->local_mtu;
uint16_t mps = buffer_space / (ertm_config->num_rx_buffers + ertm_config->num_tx_buffers);
channel->local_mps = mps;
channel->remote_mps = mps;
l2cap_ertm_setup_buffers(channel, buffer, size);
log_info("Local MPS: %u", channel->local_mps);
}l2cap_ertm_configure_channel (l2cap_channel_t * channel, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size) { ... }
uint8_t l2cap_ertm_create_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm,
l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid){
log_info("l2cap_ertm_create_channel addr %s, psm 0x%x, local mtu %u", bd_addr_to_str(address), psm, ertm_config->local_mtu);
uint8_t result = l2cap_ertm_validate_local_config(ertm_config);
if (result) return result;
const gap_security_level_t security_level = l2cap_security_level_0_allowed_for_PSM(psm) ? LEVEL_0 : gap_get_security_level();
l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, address, BD_ADDR_TYPE_ACL, psm, ertm_config->local_mtu, security_level);
if (!channel) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!channel) { ... }
l2cap_ertm_configure_channel(channel, ertm_config, buffer, size);
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
if (out_local_cid){
*out_local_cid = channel->local_cid;
}if (out_local_cid) { ... }
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_ACL);
if (conn){
log_info("l2cap_create_channel, hci connection already exists");
l2cap_handle_connection_complete(conn->con_handle, channel);
if (hci_remote_features_available(conn->con_handle)) {
l2cap_handle_remote_supported_features_received(channel);
}if (hci_remote_features_available(conn->con_handle)) { ... } else {
hci_remote_features_query(conn->con_handle);
}else { ... };
}if (conn) { ... }
l2cap_run();
return 0;
}l2cap_ertm_create_channel (btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid) { ... }
static void l2cap_ertm_notify_channel_can_send(l2cap_channel_t * channel){
if (l2cap_ertm_can_store_packet_now(channel)){
channel->waiting_for_can_send_now = 0;
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
}if (l2cap_ertm_can_store_packet_now(channel)) { ... }
}l2cap_ertm_notify_channel_can_send (l2cap_channel_t * channel) { ... }
uint8_t l2cap_ertm_accept_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size){
log_info("l2cap_ertm_accept_connection local_cid 0x%x", local_cid);
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
log_error("l2cap_accept_connection called but local_cid 0x%x not found", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
uint8_t result = l2cap_ertm_validate_local_config(ertm_config);
if (result) return result;
l2cap_ertm_configure_channel(channel, ertm_config, buffer, size);
channel->state = L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES;
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
btstack_assert(connection != NULL);
switch (connection->l2cap_state.information_state){
case L2CAP_INFORMATION_STATE_IDLE:
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;
break;case L2CAP_INFORMATION_STATE_IDLE:
case L2CAP_INFORMATION_STATE_DONE:
l2cap_handle_information_request_complete(connection);
break;case L2CAP_INFORMATION_STATE_DONE:
default:
break;default
}switch (connection->l2cap_state.information_state) { ... }
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_ertm_accept_connection (uint16_t local_cid, l2cap_ertm_config_t * ertm_config, uint8_t * buffer, uint32_t size) { ... }
uint8_t l2cap_ertm_decline_connection(uint16_t local_cid){
l2cap_decline_connection(local_cid);
return ERROR_CODE_SUCCESS;
}l2cap_ertm_decline_connection (uint16_t local_cid) { ... }
uint8_t l2cap_ertm_set_busy(uint16_t local_cid){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
if (!channel) {
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
if (!channel->local_busy){
channel->local_busy = 1;
channel->send_supervisor_frame_receiver_not_ready = 1;
l2cap_run();
}if (!channel->local_busy) { ... }
return ERROR_CODE_SUCCESS;
}l2cap_ertm_set_busy (uint16_t local_cid) { ... }
uint8_t l2cap_ertm_set_ready(uint16_t local_cid){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
if (!channel) {
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
if (channel->local_busy){
channel->local_busy = 0;
channel->send_supervisor_frame_receiver_ready_poll = 1;
l2cap_run();
}if (channel->local_busy) { ... }
return ERROR_CODE_SUCCESS;
}l2cap_ertm_set_ready (uint16_t local_cid) { ... }
static void l2cap_ertm_process_req_seq(l2cap_channel_t * l2cap_channel, uint8_t req_seq){
int num_buffers_acked = 0;
l2cap_ertm_tx_packet_state_t * tx_state;
log_info("l2cap_ertm_process_req_seq: tx_read_index %u, tx_write_index %u, req_seq %u", l2cap_channel->tx_read_index, l2cap_channel->tx_write_index, req_seq);
while (true){
if (l2cap_channel->unacked_frames == 0) {
l2cap_ertm_stop_retransmission_timer(l2cap_channel);
break;
}if (l2cap_channel->unacked_frames == 0) { ... }
tx_state = &l2cap_channel->tx_packets_state[l2cap_channel->tx_read_index];
int delta = (req_seq - tx_state->tx_seq) & 0x03f;
if (delta == 0) break;
if (delta > l2cap_channel->remote_tx_window_size) break;
num_buffers_acked++;
l2cap_channel->num_stored_tx_frames--;
l2cap_channel->unacked_frames--;
log_info("RR seq %u => packet with tx_seq %u done", req_seq, tx_state->tx_seq);
l2cap_channel->tx_read_index++;
if (l2cap_channel->tx_read_index >= l2cap_channel->num_rx_buffers){
l2cap_channel->tx_read_index = 0;
}if (l2cap_channel->tx_read_index >= l2cap_channel->num_rx_buffers) { ... }
}while (true) { ... }
if (num_buffers_acked){
log_info("num_buffers_acked %u", num_buffers_acked);
l2cap_ertm_notify_channel_can_send(l2cap_channel);
}if (num_buffers_acked) { ... }
}l2cap_ertm_process_req_seq (l2cap_channel_t * l2cap_channel, uint8_t req_seq) { ... }
static l2cap_ertm_tx_packet_state_t * l2cap_ertm_get_tx_state(l2cap_channel_t * l2cap_channel, uint8_t tx_seq){
int i;
for (i=0;i<l2cap_channel->num_tx_buffers;i++){
l2cap_ertm_tx_packet_state_t * tx_state = &l2cap_channel->tx_packets_state[i];
if (tx_state->tx_seq == tx_seq) return tx_state;
}for (i=0;inum_tx_buffers;i++) { ... }
return NULL;
}l2cap_ertm_get_tx_state (l2cap_channel_t * l2cap_channel, uint8_t tx_seq) { ... }
static void l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, int delta, const uint8_t * payload, uint16_t size){
log_info("Store SDU with delta %u", delta);
int index = l2cap_channel->rx_store_index + delta - 1;
if (index > l2cap_channel->num_rx_buffers){
index -= l2cap_channel->num_rx_buffers;
}if (index > l2cap_channel->num_rx_buffers) { ... }
log_info("Index of packet to store %u", index);
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
if (rx_state->valid){
log_error("Packet buffer already used");
return;
}if (rx_state->valid) { ... }
rx_state->valid = 1;
rx_state->sar = sar;
rx_state->len = size;
uint8_t * rx_buffer = &l2cap_channel->rx_packets_data[index];
(void)memcpy(rx_buffer, payload, size);
}l2cap_ertm_handle_out_of_sequence_sdu (l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, int delta, const uint8_t * payload, uint16_t size) { ... }
static void l2cap_ertm_handle_in_sequence_sdu(l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, const uint8_t * payload, uint16_t size){
uint16_t reassembly_sdu_length;
switch (sar){
case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU:
if (size > l2cap_channel->local_mtu) break;
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, (uint8_t*) payload, size);
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
reassembly_sdu_length = little_endian_read_16(payload, 0);
payload += 2;
size -= 2;
if (reassembly_sdu_length > l2cap_channel->local_mtu) break;
l2cap_channel->reassembly_sdu_length = reassembly_sdu_length;
(void)memcpy(&l2cap_channel->reassembly_buffer[0], payload, size);
l2cap_channel->reassembly_pos = size;
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
if (l2cap_channel->reassembly_pos + size > l2cap_channel->local_mtu) break;
(void)memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos],
payload, size);
l2cap_channel->reassembly_pos += size;
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
if (l2cap_channel->reassembly_pos + size > l2cap_channel->local_mtu) break;
(void)memcpy(&l2cap_channel->reassembly_buffer[l2cap_channel->reassembly_pos],
payload, size);
l2cap_channel->reassembly_pos += size;
if (l2cap_channel->reassembly_pos != l2cap_channel->reassembly_sdu_length) break;
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->reassembly_buffer, l2cap_channel->reassembly_pos);
l2cap_channel->reassembly_pos = 0;
break; case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
default:
btstack_assert(false);
break;default
}switch (sar) { ... }
}l2cap_ertm_handle_in_sequence_sdu (l2cap_channel_t * l2cap_channel, l2cap_segmentation_and_reassembly_t sar, const uint8_t * payload, uint16_t size) { ... }
static void l2cap_ertm_channel_send_information_frame(l2cap_channel_t * channel){
channel->unacked_frames++;
int index = channel->tx_send_index;
channel->tx_send_index++;
if (channel->tx_send_index >= channel->num_tx_buffers){
channel->tx_send_index = 0;
}if (channel->tx_send_index >= channel->num_tx_buffers) { ... }
l2cap_ertm_send_information_frame(channel, index, 0);
}l2cap_ertm_channel_send_information_frame (l2cap_channel_t * channel) { ... }
/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
static uint16_t l2cap_next_local_cid(void){
do {
if (l2cap_local_source_cid == 0xfffeu) {
l2cap_local_source_cid = 0x40;
}if (l2cap_local_source_cid == 0xfffeu) { ... } else {
l2cap_local_source_cid++;
}else { ... }
...} while (l2cap_get_channel_for_local_cid(l2cap_local_source_cid) != NULL);
return l2cap_local_source_cid;
}l2cap_next_local_cid (void) { ... }
/* ... */#endif
static uint8_t l2cap_next_sig_id(void){
if (l2cap_sig_seq_nr == 0xffu) {
l2cap_sig_seq_nr = 1;
}if (l2cap_sig_seq_nr == 0xffu) { ... } else {
l2cap_sig_seq_nr++;
}else { ... }
return l2cap_sig_seq_nr;
}{ ... }
void l2cap_init(void){
#ifdef L2CAP_USES_CHANNELS
l2cap_local_source_cid = 0x40;
#endif
l2cap_sig_seq_nr = 0xff;
#ifdef ENABLE_CLASSIC
l2cap_fixed_channel_classic_connectionless.local_cid = L2CAP_CID_CONNECTIONLESS_CHANNEL;
l2cap_fixed_channel_classic_connectionless.channel_type = L2CAP_CHANNEL_TYPE_CONNECTIONLESS;
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_classic_connectionless);/* ... */
#endif
#ifdef ENABLE_BLE
l2cap_fixed_channel_le_att.local_cid = L2CAP_CID_ATTRIBUTE_PROTOCOL;
l2cap_fixed_channel_le_att.channel_type = L2CAP_CHANNEL_TYPE_FIXED_LE;
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_le_att);
l2cap_fixed_channel_le_sm.local_cid = L2CAP_CID_SECURITY_MANAGER_PROTOCOL;
l2cap_fixed_channel_le_sm.channel_type = L2CAP_CHANNEL_TYPE_FIXED_LE;
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_le_sm);/* ... */
#endif
#ifdef ENABLE_CLASSIC
l2cap_fixed_channel_classic_sm.local_cid = L2CAP_CID_BR_EDR_SECURITY_MANAGER;
l2cap_fixed_channel_classic_sm.channel_type = L2CAP_CHANNEL_TYPE_FIXED_CLASSIC;
btstack_linked_list_add(&l2cap_channels, (btstack_linked_item_t *) &l2cap_fixed_channel_classic_sm);/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_enhanced_mps_min = 0x0001;
l2cap_enhanced_mps_max = 0xffff;
l2cap_trigger_run_registration.callback = &l2cap_run_trigger_callback;/* ... */
#endif
l2cap_hci_event_callback_registration.callback = &l2cap_hci_event_handler;
hci_add_event_handler(&l2cap_hci_event_callback_registration);
hci_register_acl_packet_handler(&l2cap_acl_handler);
#ifndef ENABLE_EXPLICIT_CONNECTABLE_MODE_CONTROL
#ifdef ENABLE_CLASSIC
gap_connectable_control(0);
#endif/* ... */
#endif
}{ ... }
/* ... */
void l2cap_deinit(void){
l2cap_channels = NULL;
l2cap_signaling_responses_pending = 0;
#ifdef ENABLE_CLASSIC
l2cap_require_security_level2_for_outgoing_sdp = 0;
(void)memset(&l2cap_fixed_channel_classic_connectionless, 0, sizeof(l2cap_fixed_channel_classic_connectionless));
l2cap_services = NULL;
(void)memset(l2cap_outgoing_classic_addr, 0, 6);
(void)memset(&l2cap_fixed_channel_classic_sm, 0, sizeof(l2cap_fixed_channel_classic_sm));/* ... */
#endif
#ifdef ENABLE_BLE
l2cap_le_custom_max_mtu = 0;
(void)memset(&l2cap_fixed_channel_le_att, 0, sizeof(l2cap_fixed_channel_le_att));
(void)memset(&l2cap_fixed_channel_le_sm, 0, sizeof(l2cap_fixed_channel_le_sm));/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_le_services = NULL;
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_enhanced_services = NULL;
#endif
l2cap_event_handlers = NULL;
}{ ... }
void l2cap_add_event_handler(btstack_packet_callback_registration_t * callback_handler){
btstack_linked_list_add_tail(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
}{ ... }
void l2cap_remove_event_handler(btstack_packet_callback_registration_t * callback_handler){
btstack_linked_list_remove(&l2cap_event_handlers, (btstack_linked_item_t*) callback_handler);
}{ ... }
static void l2cap_emit_event(uint8_t *event, uint16_t size) {
hci_dump_btstack_event( event, size);
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_event_handlers);
while (btstack_linked_list_iterator_has_next(&it)){
btstack_packet_callback_registration_t * entry = (btstack_packet_callback_registration_t*) btstack_linked_list_iterator_next(&it);
entry->callback(HCI_EVENT_PACKET, 0, event, size);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}{ ... }
void l2cap_request_can_send_fix_channel_now_event(hci_con_handle_t con_handle, uint16_t channel_id){
UNUSED(con_handle);
l2cap_fixed_channel_t * channel = l2cap_fixed_channel_for_channel_id(channel_id);
if (!channel) return;
channel->waiting_for_can_send_now = 1;
l2cap_notify_channel_can_send();
}{ ... }
bool l2cap_can_send_fixed_channel_packet_now(hci_con_handle_t con_handle, uint16_t channel_id){
UNUSED(channel_id);
return hci_can_send_acl_packet_now(con_handle);
}{ ... }
uint8_t *l2cap_get_outgoing_buffer(void){
return hci_get_outgoing_packet_buffer() + COMPLETE_L2CAP_HEADER;
}{ ... }
void l2cap_reserve_packet_buffer(void){
hci_reserve_packet_buffer();
}{ ... }
void l2cap_release_packet_buffer(void){
hci_release_packet_buffer();
}{ ... }
static void l2cap_setup_header(uint8_t * acl_buffer, hci_con_handle_t con_handle, uint8_t packet_boundary, uint16_t remote_cid, uint16_t len){
little_endian_store_16(acl_buffer, 0u, con_handle | (packet_boundary << 12u) | (0u << 14u));
little_endian_store_16(acl_buffer, 2u, len + 4u);
little_endian_store_16(acl_buffer, 4u, len + 0u);
little_endian_store_16(acl_buffer, 6, remote_cid);
}{ ... }
uint8_t l2cap_send_prepared_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint16_t len){
if (!hci_is_packet_buffer_reserved()){
log_error("l2cap_send_prepared_connectionless called without reserving packet first");
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_is_packet_buffer_reserved()) { ... }
if (!hci_can_send_prepared_acl_packet_now(con_handle)){
log_info("l2cap_send_prepared_connectionless handle 0x%02x, cid 0x%02x, cannot send", con_handle, cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_can_send_prepared_acl_packet_now(con_handle)) { ... }
log_debug("l2cap_send_prepared_connectionless handle %u, cid 0x%02x", con_handle, cid);
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
l2cap_setup_header(acl_buffer, con_handle, 0, cid, len);
return hci_send_acl_packet_buffer(len+8u);
}{ ... }
uint8_t l2cap_send_connectionless(hci_con_handle_t con_handle, uint16_t cid, uint8_t *data, uint16_t len){
if (!hci_can_send_acl_packet_now(con_handle)){
log_info("l2cap_send cid 0x%02x, cannot send", cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_can_send_acl_packet_now(con_handle)) { ... }
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
(void)memcpy(&acl_buffer[8], data, len);
return l2cap_send_prepared_connectionless(con_handle, cid, len);
}{ ... }
static void l2cap_emit_can_send_now(btstack_packet_handler_t packet_handler, uint16_t channel) {
log_debug("L2CAP_EVENT_CHANNEL_CAN_SEND_NOW local_cid 0x%x", channel);
uint8_t event[4];
event[0] = L2CAP_EVENT_CAN_SEND_NOW;
event[1] = sizeof(event) - 2u;
little_endian_store_16(event, 2, channel);
hci_dump_btstack_event( event, sizeof(event));
packet_handler(HCI_EVENT_PACKET, channel, event, sizeof(event));
}{ ... }
#ifdef L2CAP_USES_CHANNELS
static void l2cap_dispatch_to_channel(l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size){
(* (channel->packet_handler))(type, channel->local_cid, data, size);
}l2cap_dispatch_to_channel (l2cap_channel_t *channel, uint8_t type, uint8_t * data, uint16_t size) { ... }
static void l2cap_emit_simple_event_with_cid(l2cap_channel_t * channel, uint8_t event_code){
uint8_t event[4];
event[0] = event_code;
event[1] = sizeof(event) - 2u;
little_endian_store_16(event, 2, channel->local_cid);
hci_dump_btstack_event( event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_emit_simple_event_with_cid (l2cap_channel_t * channel, uint8_t event_code) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
void l2cap_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
log_info("L2CAP_EVENT_CHANNEL_OPENED status 0x%x addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u, flush_timeout %u",
status, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu, channel->flush_timeout);
uint8_t event[26];
event[0] = L2CAP_EVENT_CHANNEL_OPENED;
event[1] = sizeof(event) - 2;
event[2] = status;
reverse_bd_addr(channel->address, &event[3]);
little_endian_store_16(event, 9, channel->con_handle);
little_endian_store_16(event, 11, channel->psm);
little_endian_store_16(event, 13, channel->local_cid);
little_endian_store_16(event, 15, channel->remote_cid);
little_endian_store_16(event, 17, channel->local_mtu);
little_endian_store_16(event, 19, channel->remote_mtu);
little_endian_store_16(event, 21, channel->flush_timeout);
event[23] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
log_info("ERTM mode %u, fcs enabled %u", channel->mode, channel->fcs_option);
event[24] = channel->mode;
event[25] = channel->fcs_option;
/* ... */
#else
event[24] = L2CAP_CHANNEL_MODE_BASIC;
event[25] = 0;/* ... */
#endif
hci_dump_btstack_event( event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_emit_channel_opened (l2cap_channel_t *channel, uint8_t status) { ... }
static void l2cap_emit_incoming_connection(l2cap_channel_t *channel) {
log_info("L2CAP_EVENT_INCOMING_CONNECTION addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x",
bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid);
uint8_t event[16];
event[0] = L2CAP_EVENT_INCOMING_CONNECTION;
event[1] = sizeof(event) - 2;
reverse_bd_addr(channel->address, &event[2]);
little_endian_store_16(event, 8, channel->con_handle);
little_endian_store_16(event, 10, channel->psm);
little_endian_store_16(event, 12, channel->local_cid);
little_endian_store_16(event, 14, channel->remote_cid);
hci_dump_btstack_event( event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_emit_incoming_connection (l2cap_channel_t *channel) { ... }
static void l2cap_handle_channel_open_failed(l2cap_channel_t * channel, uint8_t status){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
l2cap_emit_channel_opened(channel, status);
}l2cap_handle_channel_open_failed (l2cap_channel_t * channel, uint8_t status) { ... }
/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
static void l2cap_emit_channel_closed(l2cap_channel_t *channel) {
log_info("L2CAP_EVENT_CHANNEL_CLOSED local_cid 0x%x", channel->local_cid);
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
}l2cap_emit_channel_closed (l2cap_channel_t *channel) { ... }
static void l2cap_handle_channel_closed(l2cap_channel_t * channel){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
l2cap_emit_channel_closed(channel);
}l2cap_handle_channel_closed (l2cap_channel_t * channel) { ... }
/* ... */#endif
static l2cap_fixed_channel_t * l2cap_channel_item_by_cid(uint16_t cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_fixed_channel_t * channel = (l2cap_fixed_channel_t*) btstack_linked_list_iterator_next(&it);
if (channel->local_cid == cid) {
return channel;
}if (channel->local_cid == cid) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}{ ... }
static l2cap_fixed_channel_t * l2cap_fixed_channel_for_channel_id(uint16_t local_cid){
if (local_cid >= 0x40u) return NULL;
return (l2cap_fixed_channel_t*) l2cap_channel_item_by_cid(local_cid);
}{ ... }
#ifdef L2CAP_USES_CHANNELS
static int l2cap_is_dynamic_channel_type(l2cap_channel_type_t channel_type) {
switch (channel_type) {
case L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
return 1;case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
return 0;default
}switch (channel_type) { ... }
}l2cap_is_dynamic_channel_type (l2cap_channel_type_t channel_type) { ... }
static l2cap_channel_t * l2cap_get_channel_for_local_cid(uint16_t local_cid){
if (local_cid < 0x40u) return NULL;
return (l2cap_channel_t*) l2cap_channel_item_by_cid(local_cid);
}l2cap_get_channel_for_local_cid (uint16_t local_cid) { ... }
static l2cap_channel_t * l2cap_get_channel_for_local_cid_and_handle(uint16_t local_cid, hci_con_handle_t con_handle){
if (local_cid < 0x40u) return NULL;
l2cap_channel_t * l2cap_channel = (l2cap_channel_t*) l2cap_channel_item_by_cid(local_cid);
if (l2cap_channel == NULL) return NULL;
if (l2cap_channel->con_handle != con_handle) return NULL;
return l2cap_channel;
}l2cap_get_channel_for_local_cid_and_handle (uint16_t local_cid, hci_con_handle_t con_handle) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
static l2cap_channel_t * l2cap_get_channel_for_remote_handle_and_cid(hci_con_handle_t con_handle, uint16_t remote_cid){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_fixed_channel_t * channel = (l2cap_fixed_channel_t*) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
l2cap_channel_t * dynamic_channel = (l2cap_channel_t *) channel;
if (dynamic_channel->con_handle != con_handle) continue;
if (dynamic_channel->remote_cid != remote_cid) continue;
return dynamic_channel;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}l2cap_get_channel_for_remote_handle_and_cid (hci_con_handle_t con_handle, uint16_t remote_cid) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CHANNELS
uint8_t l2cap_request_can_send_now_event(uint16_t local_cid){
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
channel->waiting_for_can_send_now = 1;
switch (channel->channel_type){
case L2CAP_CHANNEL_TYPE_CLASSIC:
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
l2cap_ertm_notify_channel_can_send(channel);
return ERROR_CODE_SUCCESS;
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
l2cap_notify_channel_can_send();
break;
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
l2cap_credit_based_notify_channel_can_send(channel);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
l2cap_credit_based_notify_channel_can_send(channel);
break;/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
break;default
}switch (channel->channel_type) { ... }
return ERROR_CODE_SUCCESS;
}l2cap_request_can_send_now_event (uint16_t local_cid) { ... }
bool l2cap_can_send_packet_now(uint16_t local_cid){
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return false;
if (channel->state != L2CAP_STATE_OPEN) return false;
switch (channel->channel_type){
case L2CAP_CHANNEL_TYPE_CLASSIC:
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
return l2cap_ertm_can_store_packet_now(channel);
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
return hci_can_send_acl_packet_now(channel->con_handle);
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
return channel->send_sdu_buffer == NULL;/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
return channel->send_sdu_buffer == NULL;/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
return false;default
}switch (channel->channel_type) { ... }
}l2cap_can_send_packet_now (uint16_t local_cid) { ... }
uint8_t l2cap_send(uint16_t local_cid, const uint8_t *data, uint16_t len){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
log_error("l2cap_send no channel for cid 0x%02x", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
switch (channel->channel_type){
#ifdef ENABLE_CLASSIC
case L2CAP_CHANNEL_TYPE_CLASSIC:
return l2cap_classic_send(channel, data, len);/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
return l2cap_credit_based_send_data(channel, data, len);/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
return l2cap_credit_based_send_data(channel, data, len);/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
return ERROR_CODE_UNSPECIFIED_ERROR;default
}switch (channel->channel_type) { ... }
}l2cap_send (uint16_t local_cid, const uint8_t *data, uint16_t len) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
bool l2cap_can_send_prepared_packet_now(uint16_t local_cid){
l2cap_channel_t *channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return false;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
return false;
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
return hci_can_send_prepared_acl_packet_now(channel->con_handle);
}l2cap_can_send_prepared_packet_now (uint16_t local_cid) { ... }
/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
uint16_t l2cap_get_remote_mtu_for_local_cid(uint16_t local_cid){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (channel) {
return channel->remote_mtu;
}if (channel) { ... }
return 0;
}l2cap_get_remote_mtu_for_local_cid (uint16_t local_cid) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static l2cap_channel_t * l2cap_channel_for_rtx_timer(btstack_timer_source_t * ts){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (&channel->rtx == ts) {
return channel;
}if (&channel->rtx == ts) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}l2cap_channel_for_rtx_timer (btstack_timer_source_t * ts) { ... }
static void l2cap_rtx_timeout(btstack_timer_source_t * ts){
l2cap_channel_t * channel = l2cap_channel_for_rtx_timer(ts);
if (!channel) return;
log_info("l2cap_rtx_timeout for local cid 0x%02x", channel->local_cid);
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_RTX_TIMEOUT);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
}l2cap_rtx_timeout (btstack_timer_source_t * ts) { ... }
static uint8_t l2cap_classic_packet_boundary_flag(void){
return (hci_non_flushable_packet_boundary_flag_supported() && (hci_automatic_flush_timeout() == 0)) ? 0x00 : 0x02;
}l2cap_classic_packet_boundary_flag (void) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CHANNELS
static void l2cap_stop_rtx(l2cap_channel_t * channel){
log_info("l2cap_stop_rtx for local cid 0x%02x", channel->local_cid);
btstack_run_loop_remove_timer(&channel->rtx);
}l2cap_stop_rtx (l2cap_channel_t * channel) { ... }
/* ... */#endif
static uint8_t l2cap_send_signaling_packet(hci_con_handle_t handle, uint8_t pb_flags, uint16_t cid, L2CAP_SIGNALING_COMMANDS cmd, int identifier, va_list argptr){
if (!hci_can_send_acl_packet_now(handle)){
log_info("l2cap_send_classic_signaling_packet, cannot send");
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_can_send_acl_packet_now(handle)) { ... }
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
uint16_t len = l2cap_create_signaling_packet(acl_buffer, handle, pb_flags, cid, cmd, identifier, argptr);
va_end(argptr);
return hci_send_acl_packet_buffer(len);
}{ ... }
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
static int l2cap_send_general_signaling_packet(hci_con_handle_t handle, uint16_t signaling_cid, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...){
va_list argptr;
va_start(argptr, identifier);
uint8_t pb_flags = 0x00;
#ifdef ENABLE_CLASSIC
if (signaling_cid == L2CAP_CID_SIGNALING){
pb_flags = l2cap_classic_packet_boundary_flag();
}if (signaling_cid == L2CAP_CID_SIGNALING) { ... }
/* ... */#endif
uint8_t result = l2cap_send_signaling_packet(handle, pb_flags, signaling_cid, cmd, identifier, argptr);
va_end(argptr);
return result;
}l2cap_send_general_signaling_packet (hci_con_handle_t handle, uint16_t signaling_cid, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static void l2cap_start_rtx(l2cap_channel_t * channel){
l2cap_stop_rtx(channel);
log_info("l2cap_start_rtx for local cid 0x%02x", channel->local_cid);
btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
btstack_run_loop_set_timer(&channel->rtx, L2CAP_RTX_TIMEOUT_MS);
btstack_run_loop_add_timer(&channel->rtx);
}l2cap_start_rtx (l2cap_channel_t * channel) { ... }
static void l2cap_start_ertx(l2cap_channel_t * channel){
log_info("l2cap_start_ertx for local cid 0x%02x", channel->local_cid);
l2cap_stop_rtx(channel);
btstack_run_loop_set_timer_handler(&channel->rtx, l2cap_rtx_timeout);
btstack_run_loop_set_timer(&channel->rtx, L2CAP_ERTX_TIMEOUT_MS);
btstack_run_loop_add_timer(&channel->rtx);
}l2cap_start_ertx (l2cap_channel_t * channel) { ... }
void l2cap_require_security_level_2_for_outgoing_sdp(void){
l2cap_require_security_level2_for_outgoing_sdp = 1;
}l2cap_require_security_level_2_for_outgoing_sdp (void) { ... }
static int l2cap_security_level_0_allowed_for_PSM(uint16_t psm){
return (psm == BLUETOOTH_PSM_SDP) && (!l2cap_require_security_level2_for_outgoing_sdp);
}l2cap_security_level_0_allowed_for_PSM (uint16_t psm) { ... }
static int l2cap_send_classic_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...){
va_list argptr;
va_start(argptr, identifier);
uint8_t pb_flag = l2cap_classic_packet_boundary_flag();
uint8_t result = l2cap_send_signaling_packet(handle, pb_flag, L2CAP_CID_SIGNALING, cmd, identifier, argptr);
va_end(argptr);
return result;
}l2cap_send_classic_signaling_packet (hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...) { ... }
uint8_t l2cap_send_prepared(uint16_t local_cid, uint16_t len){
if (!hci_is_packet_buffer_reserved()){
log_error("l2cap_send_prepared called without reserving packet first");
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_is_packet_buffer_reserved()) { ... }
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
log_error("l2cap_send_prepared no channel for cid 0x%02x", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)){
log_info("l2cap_send_prepared cid 0x%02x, cannot send", local_cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_can_send_prepared_acl_packet_now(channel->con_handle)) { ... }
log_debug("l2cap_send_prepared cid 0x%02x, handle %u, 1 credit used", local_cid, channel->con_handle);
int fcs_size = 0;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->fcs_option){
fcs_size = 2;
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->fcs_option) { ... }
/* ... */#endif
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
uint8_t packet_boundary_flag = l2cap_classic_packet_boundary_flag();
l2cap_setup_header(acl_buffer, channel->con_handle, packet_boundary_flag, channel->remote_cid, len + fcs_size);
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (fcs_size){
uint16_t fcs = crc16_calc(acl_buffer + 4, 4 + len);
log_info("I-Frame: fcs 0x%04x", fcs);
little_endian_store_16(acl_buffer, 8 + len, fcs);
}if (fcs_size) { ... }
/* ... */#endif
return hci_send_acl_packet_buffer(len+8+fcs_size);
}l2cap_send_prepared (uint16_t local_cid, uint16_t len) { ... }
static uint8_t l2cap_classic_send(l2cap_channel_t * channel, const uint8_t *data, uint16_t len){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
return l2cap_ertm_send(channel, data, len);
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
if (len > channel->remote_mtu){
log_error("l2cap_send cid 0x%02x, data length exceeds remote MTU.", channel->local_cid);
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
}if (len > channel->remote_mtu) { ... }
if (!hci_can_send_acl_packet_now(channel->con_handle)){
log_info("l2cap_send cid 0x%02x, cannot send", channel->local_cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (!hci_can_send_acl_packet_now(channel->con_handle)) { ... }
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
(void)memcpy(&acl_buffer[8], data, len);
return l2cap_send_prepared(channel->local_cid, len);
}l2cap_classic_send (l2cap_channel_t * channel, const uint8_t *data, uint16_t len) { ... }
int l2cap_send_echo_request(hci_con_handle_t con_handle, uint8_t *data, uint16_t len){
return l2cap_send_classic_signaling_packet(con_handle, ECHO_REQUEST, l2cap_next_sig_id(), len, data);
}l2cap_send_echo_request (hci_con_handle_t con_handle, uint8_t *data, uint16_t len) { ... }
static inline void channelStateVarSetFlag(l2cap_channel_t *channel, uint16_t flag){
channel->state_var = channel->state_var | flag;
}channelStateVarSetFlag (l2cap_channel_t *channel, uint16_t flag) { ... }
static inline void channelStateVarClearFlag(l2cap_channel_t *channel, uint16_t flag){
channel->state_var = channel->state_var & ~flag;
}channelStateVarClearFlag (l2cap_channel_t *channel, uint16_t flag) { ... }
/* ... */#endif
#ifdef ENABLE_BLE
static int l2cap_send_le_signaling_packet(hci_con_handle_t handle, L2CAP_SIGNALING_COMMANDS cmd, int identifier, ...){
va_list argptr;
va_start(argptr, identifier);
uint8_t pb_flags = 0x00;
uint8_t result = l2cap_send_signaling_packet(handle, pb_flags, L2CAP_CID_SIGNALING_LE, cmd, identifier, argptr);
va_end(argptr);
return result;
}{ ... }
/* ... */#endif
uint16_t l2cap_max_mtu(void){
return HCI_ACL_PAYLOAD_SIZE - L2CAP_HEADER_SIZE;
}{ ... }
#ifdef ENABLE_BLE
uint16_t l2cap_max_le_mtu(void){
if (l2cap_le_custom_max_mtu != 0u) return l2cap_le_custom_max_mtu;
return l2cap_max_mtu();
}{ ... }
void l2cap_set_max_le_mtu(uint16_t max_mtu){
if (max_mtu < l2cap_max_mtu()){
l2cap_le_custom_max_mtu = max_mtu;
}if (max_mtu < l2cap_max_mtu()) { ... }
}{ ... }
#endif/* ... */
#ifdef ENABLE_CLASSIC
static uint16_t l2cap_setup_options_mtu(uint8_t * config_options, uint16_t mtu){
config_options[0] = L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT;
config_options[1] = 2;
little_endian_store_16(config_options, 2, mtu);
return 4;
}l2cap_setup_options_mtu (uint8_t * config_options, uint16_t mtu) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
static int l2cap_ertm_mode(l2cap_channel_t * channel){
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
return ((connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_DONE)
&& (connection->l2cap_state.extended_feature_mask & 0x08));
}l2cap_ertm_mode (l2cap_channel_t * channel) { ... }
/* ... */#endif
static uint16_t l2cap_setup_options_request(l2cap_channel_t * channel, uint8_t * config_options){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (l2cap_ertm_mode(channel) && channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
return l2cap_setup_options_ertm_request(channel, config_options);
}if (l2cap_ertm_mode(channel) && channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
uint16_t mtu = channel->local_mtu;
return l2cap_setup_options_mtu(config_options, mtu);
}l2cap_setup_options_request (l2cap_channel_t * channel, uint8_t * config_options) { ... }
static uint16_t l2cap_setup_options_mtu_response(l2cap_channel_t * channel, uint8_t * config_options){
uint16_t mtu = btstack_min(channel->local_mtu, channel->remote_mtu);
return l2cap_setup_options_mtu(config_options, mtu);
}l2cap_setup_options_mtu_response (l2cap_channel_t * channel, uint8_t * config_options) { ... }
static uint32_t l2cap_extended_features_mask(void){
uint32_t features = 0x280;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
features |= 0x0028;
#endif
return features;
}l2cap_extended_features_mask (void) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static bool l2cap_run_for_classic_channel(l2cap_channel_t * channel){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
uint8_t config_options[18];
#else
uint8_t config_options[10];
#endif
switch (channel->state){
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) {
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND);
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONN_RESP_PEND);
l2cap_send_classic_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id,
channel->local_cid, channel->remote_cid, 1, 0);
}if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND) { ... }
break;
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
if (!hci_can_send_command_packet_now()) break;
channel->state = L2CAP_STATE_WAIT_CONNECTION_COMPLETE;
(void)memcpy(l2cap_outgoing_classic_addr, channel->address, 6);
hci_send_cmd(&hci_create_connection, channel->address, hci_usable_acl_packet_types(), 0, 0, 0, hci_get_allow_role_switch());
break;
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
channel->state = L2CAP_STATE_INVALID;
l2cap_send_classic_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id,
channel->local_cid, channel->remote_cid, channel->reason, 0);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
channel = NULL;
break;
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
channel->state = L2CAP_STATE_CONFIG;
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
l2cap_send_classic_signaling_packet(channel->con_handle, CONNECTION_RESPONSE, channel->remote_sig_id,
channel->local_cid, channel->remote_cid, 0, 0);
break;
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_CONNECT_RSP;
l2cap_send_classic_signaling_packet(channel->con_handle, CONNECTION_REQUEST, channel->local_sig_id,
channel->psm, channel->local_cid);
l2cap_start_rtx(channel);
break;
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
case L2CAP_STATE_CONFIG:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
if (!l2cap_ertm_mode(channel)){
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
}if (!l2cap_ertm_mode(channel)) { ... }
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP){
uint16_t flags = 0;
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) {
flags = 1;
}if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT) { ... } else {
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
}else { ... }
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID){
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id,
channel->remote_cid, flags, L2CAP_CONF_RESULT_UNKNOWN_OPTIONS,
1, &channel->unknown_option);
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
}if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID) { ... } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED){
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED);
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP);
uint16_t options_size = l2cap_setup_options_ertm_response(channel, config_options);
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id,
channel->remote_cid, flags,
L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS, options_size,
&config_options);
}else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED) { ... } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM){
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM);
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
uint16_t options_size = l2cap_setup_options_ertm_response(channel, config_options);
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id,
channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS,
options_size, &config_options);
#endif
}else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM) { ... } else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU){
channelStateVarClearFlag(channel,L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
uint16_t options_size = l2cap_setup_options_mtu_response(channel, config_options);
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id,
channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS,
options_size, &config_options);
}else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU) { ... } else {
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_RESPONSE, channel->remote_sig_id,
channel->remote_cid, flags, L2CAP_CONF_RESULT_SUCCESS, 0, NULL);
}else { ... }
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
}if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP) { ... }
else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ){
channelStateVarClearFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SENT_CONF_REQ);
channel->local_sig_id = l2cap_next_sig_id();
uint16_t options_size = l2cap_setup_options_request(channel, config_options);
l2cap_send_classic_signaling_packet(channel->con_handle, CONFIGURE_REQUEST, channel->local_sig_id,
channel->remote_cid, 0, options_size, &config_options);
l2cap_start_rtx(channel);
}else if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ) { ... }
if (l2cap_channel_ready_for_open(channel)){
channel->state = L2CAP_STATE_OPEN;
l2cap_emit_channel_opened(channel, 0);
}if (l2cap_channel_ready_for_open(channel)) { ... }
break;
case L2CAP_STATE_CONFIG:
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
channel->state = L2CAP_STATE_INVALID;
l2cap_send_classic_signaling_packet(channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id,
channel->local_cid, channel->remote_cid);
l2cap_finalize_channel_close(channel);
channel = NULL;
break;
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
if (!hci_can_send_acl_packet_now(channel->con_handle)) return false;
channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_DISCONNECT;
l2cap_send_classic_signaling_packet(channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id,
channel->remote_cid, channel->local_cid);
break;case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
default:
break;default
}switch (channel->state) { ... }
return channel == NULL;
}l2cap_run_for_classic_channel (l2cap_channel_t * channel) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
static void l2cap_run_for_classic_channel_ertm(l2cap_channel_t * channel){
if (channel->mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) return;
if (channel->con_handle == HCI_CON_HANDLE_INVALID) return;
if (!hci_can_send_acl_packet_now(channel->con_handle)) return;
if (channel->send_supervisor_frame_receiver_ready){
channel->send_supervisor_frame_receiver_ready = 0;
log_info("Send S-Frame: RR %u, final %u", channel->req_seq, channel->set_final_bit_after_packet_with_poll_bit_set);
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->req_seq);
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
l2cap_ertm_send_supervisor_frame(channel, control);
return;
}if (channel->send_supervisor_frame_receiver_ready) { ... }
if (channel->send_supervisor_frame_receiver_ready_poll){
channel->send_supervisor_frame_receiver_ready_poll = 0;
log_info("Send S-Frame: RR %u with poll=1 ", channel->req_seq);
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY, 1, 0, channel->req_seq);
l2cap_ertm_send_supervisor_frame(channel, control);
return;
}if (channel->send_supervisor_frame_receiver_ready_poll) { ... }
if (channel->send_supervisor_frame_receiver_not_ready){
channel->send_supervisor_frame_receiver_not_ready = 0;
log_info("Send S-Frame: RNR %u", channel->req_seq);
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY, 0, 0, channel->req_seq);
l2cap_ertm_send_supervisor_frame(channel, control);
return;
}if (channel->send_supervisor_frame_receiver_not_ready) { ... }
if (channel->send_supervisor_frame_reject){
channel->send_supervisor_frame_reject = 0;
log_info("Send S-Frame: REJ %u", channel->req_seq);
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT, 0, 0, channel->req_seq);
l2cap_ertm_send_supervisor_frame(channel, control);
return;
}if (channel->send_supervisor_frame_reject) { ... }
if (channel->send_supervisor_frame_selective_reject){
channel->send_supervisor_frame_selective_reject = 0;
log_info("Send S-Frame: SREJ %u", channel->expected_tx_seq);
uint16_t control = l2cap_encanced_control_field_for_supevisor_frame( L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT, 0, channel->set_final_bit_after_packet_with_poll_bit_set, channel->expected_tx_seq);
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
l2cap_ertm_send_supervisor_frame(channel, control);
return;
}if (channel->send_supervisor_frame_selective_reject) { ... }
if (channel->srej_active){
int i;
for (i=0;i<channel->num_tx_buffers;i++){
l2cap_ertm_tx_packet_state_t * tx_state = &channel->tx_packets_state[i];
if (tx_state->retransmission_requested) {
tx_state->retransmission_requested = 0;
uint8_t final = channel->set_final_bit_after_packet_with_poll_bit_set;
channel->set_final_bit_after_packet_with_poll_bit_set = 0;
l2cap_ertm_send_information_frame(channel, i, final);
break;
}if (tx_state->retransmission_requested) { ... }
}for (i=0;inum_tx_buffers;i++) { ... }
if (i == channel->num_tx_buffers){
channel->srej_active = 0;
}if (i == channel->num_tx_buffers) { ... } else {
return;
}else { ... }
}if (channel->srej_active) { ... }
}l2cap_run_for_classic_channel_ertm (l2cap_channel_t * channel) { ... }
/* ... */#endif /* ... */
#endif
static void l2cap_run_signaling_response(void) {
while (l2cap_signaling_responses_pending){
hci_con_handle_t handle = l2cap_signaling_responses[0].handle;
if (!hci_can_send_acl_packet_now(handle)) break;
uint8_t sig_id = l2cap_signaling_responses[0].sig_id;
uint8_t response_code = l2cap_signaling_responses[0].code;
uint16_t result = l2cap_signaling_responses[0].data;
uint8_t buffer[4];
uint16_t source_cid = l2cap_signaling_responses[0].cid;
#ifdef ENABLE_CLASSIC
uint16_t dest_cid = l2cap_signaling_responses[0].data;
uint16_t info_type = l2cap_signaling_responses[0].data; /* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
uint8_t num_channels = l2cap_signaling_responses[0].cid >> 8;
uint16_t signaling_cid = (uint16_t) l2cap_signaling_responses[0].cid & 0xff; /* ... */
#endif
l2cap_signaling_responses_pending--;
int i;
for (i=0; i < l2cap_signaling_responses_pending; i++){
(void)memcpy(&l2cap_signaling_responses[i],
&l2cap_signaling_responses[i + 1],
sizeof(l2cap_signaling_response_t));
}for (i=0; i < l2cap_signaling_responses_pending; i++) { ... }
switch (response_code){
#ifdef ENABLE_CLASSIC
case CONNECTION_REQUEST:
l2cap_send_classic_signaling_packet(handle, CONNECTION_RESPONSE, sig_id, source_cid, 0, result, 0);
break;case CONNECTION_REQUEST:
case CONFIGURE_REQUEST:
little_endian_store_16(buffer, 0, source_cid);
little_endian_store_16(buffer, 2, 0);
l2cap_send_classic_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 4, buffer);
break;case CONFIGURE_REQUEST:
case DISCONNECTION_RESPONSE:
l2cap_send_classic_signaling_packet(handle, DISCONNECTION_RESPONSE, sig_id, dest_cid, source_cid);
break;case DISCONNECTION_RESPONSE:
case ECHO_REQUEST:
l2cap_send_classic_signaling_packet(handle, ECHO_RESPONSE, sig_id, 0, NULL);
break;case ECHO_REQUEST:
case INFORMATION_REQUEST:
switch (info_type){
case L2CAP_INFO_TYPE_CONNECTIONLESS_MTU: {
uint16_t connectionless_mtu = hci_max_acl_data_packet_length();
l2cap_send_classic_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0,
sizeof(connectionless_mtu), &connectionless_mtu);
...}
break;case L2CAP_INFO_TYPE_CONNECTIONLESS_MTU:
case L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED: {
uint32_t features = l2cap_extended_features_mask();
l2cap_send_classic_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0,
sizeof(features), &features);
...}
break;case L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED:
case L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED: {
uint8_t map[8];
memset(map, 0, 8);
map[0] = (1 << L2CAP_CID_SIGNALING) | (1 << L2CAP_CID_CONNECTIONLESS_CHANNEL);
#if defined (ENABLE_EXPLICIT_BR_EDR_SECURITY_MANAGER) || (defined(ENABLE_BLE) && defined(ENABLE_CROSS_TRANSPORT_KEY_DERIVATION))
if (gap_secure_connections_active()){
map[0] |= (1 << L2CAP_CID_BR_EDR_SECURITY_MANAGER);
}if (gap_secure_connections_active()) { ... }
/* ... */#endif
l2cap_send_classic_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 0,
sizeof(map), &map);
...}
break;case L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED:
default:
l2cap_send_classic_signaling_packet(handle, INFORMATION_RESPONSE, sig_id, info_type, 1, 0, NULL);
break;default
}switch (info_type) { ... }
break;case INFORMATION_REQUEST:
case COMMAND_REJECT:
l2cap_send_classic_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
break;/* ... */
#endif
#ifdef ENABLE_BLE
case LE_CREDIT_BASED_CONNECTION_REQUEST:
l2cap_send_le_signaling_packet(handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, sig_id, 0, 0, 0, 0, result);
break;case LE_CREDIT_BASED_CONNECTION_REQUEST:
case DISCONNECTION_REQUEST:
little_endian_store_16(buffer, 0, result);
little_endian_store_16(buffer, 2, source_cid);
l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, 0x0002, 4, buffer);
break;case DISCONNECTION_REQUEST:
case COMMAND_REJECT_LE:
l2cap_send_le_signaling_packet(handle, COMMAND_REJECT, sig_id, result, 0, NULL);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST: {
uint16_t cids[6];
(void) memset(cids, 0xff, sizeof(cids));
(void) memset(cids, 0x00, num_channels * sizeof(uint16_t));
l2cap_send_general_signaling_packet(handle, signaling_cid, L2CAP_CREDIT_BASED_CONNECTION_RESPONSE,
sig_id, 0, 0, 0, result, cids);
break;
...}
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST:
case L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST:
l2cap_send_general_signaling_packet(handle, signaling_cid, L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE,
sig_id, result);
break;/* ... */
#endif
case SM_PAIRING_FAILED:
buffer[0] = SM_CODE_PAIRING_FAILED;
buffer[1] = (uint8_t) result;
l2cap_send_connectionless(handle, source_cid, buffer, 2);
break;case SM_PAIRING_FAILED:
default:
break;default
}switch (response_code) { ... }
}while (l2cap_signaling_responses_pending) { ... }
}{ ... }
#ifdef ENABLE_CLASSIC
static bool l2ap_run_information_requests(void){
btstack_linked_list_iterator_t it;
hci_connections_get_iterator(&it);
uint8_t info_type;
l2cap_information_state_t new_state;
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
switch (connection->l2cap_state.information_state){
case L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST:
info_type = L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED;
new_state = L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE;
break;case L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST:
case L2CAP_INFORMATION_STATE_W2_SEND_FIXED_CHANNELS_REQUEST:
info_type = L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED;
new_state = L2CAP_INFORMATION_STATE_W4_FIXED_CHANNELS_RESPONSE;
break;case L2CAP_INFORMATION_STATE_W2_SEND_FIXED_CHANNELS_REQUEST:
default:
continue;default
}switch (connection->l2cap_state.information_state) { ... }
if (!hci_can_send_acl_packet_now(connection->con_handle)) break;
connection->l2cap_state.information_state = new_state;
uint8_t sig_id = l2cap_next_sig_id();
l2cap_send_classic_signaling_packet(connection->con_handle, INFORMATION_REQUEST, sig_id, info_type);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return false;
}l2ap_run_information_requests (void) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
static bool l2cap_cbm_run_channel(l2cap_channel_t * channel) {
uint16_t mps;
bool channel_closed = false;
switch (channel->state){
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
channel->state = L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE;
channel->local_sig_id = l2cap_next_sig_id();
channel->credits_incoming = channel->new_credits_incoming;
channel->new_credits_incoming = 0;
channel->local_mps = btstack_min(l2cap_max_le_mtu(), channel->local_mtu);
l2cap_send_le_signaling_packet( channel->con_handle, LE_CREDIT_BASED_CONNECTION_REQUEST,
channel->local_sig_id, channel->psm, channel->local_cid, channel->local_mtu,
channel->local_mps, channel->credits_incoming);
break;case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
channel->state = L2CAP_STATE_OPEN;
channel->credits_incoming = channel->new_credits_incoming;
channel->new_credits_incoming = 0;
mps = btstack_min(l2cap_max_le_mtu(), channel->local_mtu);
channel->local_mps = mps;
l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->local_mtu, mps, channel->credits_incoming, 0);
l2cap_cbm_emit_channel_opened(channel, ERROR_CODE_SUCCESS);
break;case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
channel->state = L2CAP_STATE_INVALID;
l2cap_send_le_signaling_packet(channel->con_handle, LE_CREDIT_BASED_CONNECTION_RESPONSE, channel->remote_sig_id, 0, 0, 0, 0, channel->reason);
channel_closed = true;
break;case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
case L2CAP_STATE_OPEN:
if (channel->new_credits_incoming){
l2cap_credit_based_send_credits(channel);
}if (channel->new_credits_incoming) { ... }
break;case L2CAP_STATE_OPEN:
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_DISCONNECT;
l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_REQUEST, channel->local_sig_id, channel->remote_cid, channel->local_cid);
break;case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
channel->state = L2CAP_STATE_INVALID;
l2cap_send_le_signaling_packet( channel->con_handle, DISCONNECTION_RESPONSE, channel->remote_sig_id, channel->local_cid, channel->remote_cid);
l2cap_cbm_finialize_channel_close(channel);
break;case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
default:
break;default
}switch (channel->state) { ... }
return channel_closed;
}l2cap_cbm_run_channel (l2cap_channel_t * channel) { ... }
static void l2cap_cbm_run_channels(void){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->channel_type != L2CAP_CHANNEL_TYPE_CHANNEL_CBM) continue;
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
bool channel_closed = l2cap_cbm_run_channel(channel);
if (channel_closed) {
btstack_linked_list_iterator_remove(&it);
l2cap_free_channel_entry(channel);
}if (channel_closed) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_cbm_run_channels (void) { ... }
static inline uint8_t l2cap_cbm_status_for_result(uint16_t result) {
switch (result) {
case L2CAP_CBM_CONNECTION_RESULT_SUCCESS:
return ERROR_CODE_SUCCESS;case L2CAP_CBM_CONNECTION_RESULT_SUCCESS:
case L2CAP_CBM_CONNECTION_RESULT_SPSM_NOT_SUPPORTED:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_PSM;case L2CAP_CBM_CONNECTION_RESULT_SPSM_NOT_SUPPORTED:
case L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES;case L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE:
case L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_AUTHENTICATION:
case L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_AUTHORIZATION:
case L2CAP_CBM_CONNECTION_RESULT_ENCYRPTION_KEY_SIZE_TOO_SHORT:
case L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_ENCRYPTION:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY;case L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_ENCRYPTION:
default:
return L2CAP_CONNECTION_RESPONSE_UNKNOWN_ERROR;default
}switch (result) { ... }
}l2cap_cbm_status_for_result (uint16_t result) { ... }
/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
static void l2cap_run_trigger_callback(void * context){
UNUSED(context);
l2cap_run();
}l2cap_run_trigger_callback (void * context) { ... }
static void l2cap_run_trigger(void){
btstack_run_loop_execute_on_main_thread(&l2cap_trigger_run_registration);
}l2cap_run_trigger (void) { ... }
static void l2cap_ecbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
log_info("opened ecbm channel status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u",
status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
uint8_t event[23];
event[0] = L2CAP_EVENT_ECBM_CHANNEL_OPENED;
event[1] = sizeof(event) - 2u;
event[2] = status;
event[3] = channel->address_type;
reverse_bd_addr(channel->address, &event[4]);
little_endian_store_16(event, 10, channel->con_handle);
event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
little_endian_store_16(event, 13, channel->psm);
little_endian_store_16(event, 15, channel->local_cid);
little_endian_store_16(event, 17, channel->remote_cid);
little_endian_store_16(event, 19, channel->local_mtu);
little_endian_store_16(event, 21, channel->remote_mtu);
hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_ecbm_emit_channel_opened (l2cap_channel_t *channel, uint8_t status) { ... }
static void l2cap_ecbm_emit_reconfigure_complete(l2cap_channel_t *channel, uint16_t result) {
uint8_t event[6];
event[0] = L2CAP_EVENT_ECBM_RECONFIGURATION_COMPLETE;
event[1] = sizeof(event) - 2;
little_endian_store_16(event, 2, channel->local_cid);
little_endian_store_16(event, 4, result);
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_ecbm_emit_reconfigure_complete (l2cap_channel_t *channel, uint16_t result) { ... }
static void l2cap_ecbm_run_channels(void) {
hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
uint16_t cids[L2CAP_ECBM_MAX_CID_ARRAY_SIZE + 1];
uint8_t num_cids = 0;
uint8_t sig_id;
uint16_t spsm;
L2CAP_STATE matching_state;
bool match_remote_sig_cid;
uint8_t result = 0;
uint16_t local_mtu;
uint16_t initial_credits;
uint16_t signaling_cid;
L2CAP_STATE new_state;
uint16_t local_mps;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->channel_type != L2CAP_CHANNEL_TYPE_CHANNEL_ECBM) continue;
if (con_handle == HCI_CON_HANDLE_INVALID) {
switch (channel->state) {
case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
local_mtu = channel->local_mtu;
local_mps = channel->local_mps;
spsm = channel->psm;
result = channel->reason;
initial_credits = channel->credits_incoming;
sig_id = channel->local_sig_id;
new_state = L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE;
match_remote_sig_cid = false;
break;case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST:
case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
local_mtu = channel->local_mtu;
local_mps = channel->local_mps;
initial_credits = channel->credits_incoming;
sig_id = channel->remote_sig_id;
new_state = L2CAP_STATE_OPEN;
match_remote_sig_cid = true;
break;case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE:
case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
sig_id = channel->local_sig_id;
local_mtu = channel->renegotiate_mtu;
local_mps = channel->local_mps;
new_state = L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE;
match_remote_sig_cid = false;
break;case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
case L2CAP_STATE_OPEN:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
if (channel->new_credits_incoming) {
l2cap_credit_based_send_credits(channel);
}if (channel->new_credits_incoming) { ... }
continue;case L2CAP_STATE_OPEN:
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
channel->local_sig_id = l2cap_next_sig_id();
channel->state = L2CAP_STATE_WAIT_DISCONNECT;
signaling_cid = channel->address_type == BD_ADDR_TYPE_ACL ? L2CAP_CID_SIGNALING : L2CAP_CID_SIGNALING_LE;
l2cap_send_general_signaling_packet(channel->con_handle, signaling_cid, DISCONNECTION_REQUEST,
channel->local_sig_id, channel->remote_cid, channel->local_cid);
continue;case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
if (!hci_can_send_acl_packet_now(channel->con_handle)) continue;
channel->state = L2CAP_STATE_INVALID;
signaling_cid = channel->address_type == BD_ADDR_TYPE_ACL ? L2CAP_CID_SIGNALING : L2CAP_CID_SIGNALING_LE;
l2cap_send_general_signaling_packet(channel->con_handle, signaling_cid, DISCONNECTION_RESPONSE,
channel->remote_sig_id, channel->local_cid,
channel->remote_cid);
l2cap_cbm_finialize_channel_close(channel);
continue;case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
default:
continue;default
}switch (channel->state) { ... }
(void) memset(cids, 0xff, sizeof(cids));
(void) memset(cids, 0, channel->num_cids * sizeof(uint16_t));
matching_state = channel->state;
con_handle = channel->con_handle;
signaling_cid = channel->address_type == BD_ADDR_TYPE_ACL ? L2CAP_CID_SIGNALING : L2CAP_CID_SIGNALING_LE;
num_cids = channel->num_cids;
}if (con_handle == HCI_CON_HANDLE_INVALID) { ... } else {
if (matching_state != channel->state) continue;
if (channel->con_handle != con_handle) continue;
if (match_remote_sig_cid) {
if (channel->remote_sig_id != sig_id) continue;
}if (match_remote_sig_cid) { ... } else {
if (channel->local_sig_id != sig_id) continue;
}else { ... }
}else { ... }
cids[channel->cid_index] = channel->local_cid;
channel->state = new_state;
if (matching_state == L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE) {
if (channel->reason == L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS) {
l2cap_ecbm_emit_channel_opened(channel, ERROR_CODE_SUCCESS);
}if (channel->reason == L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS) { ... } else {
result = channel->reason;
btstack_linked_list_iterator_remove(&it);
btstack_memory_l2cap_channel_free(channel);
}else { ... }
}if (matching_state == L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (con_handle != HCI_CON_HANDLE_INVALID) {
switch (matching_state) {
case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST:
log_info("send combined connection request for %u cids", num_cids);
l2cap_send_general_signaling_packet(con_handle, signaling_cid, L2CAP_CREDIT_BASED_CONNECTION_REQUEST,
sig_id, spsm, local_mtu, local_mps, initial_credits, cids);
break;case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST:
case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE:
log_info("send combined connection response for %u cids", num_cids);
l2cap_send_general_signaling_packet(con_handle, signaling_cid, L2CAP_CREDIT_BASED_CONNECTION_RESPONSE,
sig_id, local_mtu, local_mps, initial_credits, result, cids);
break;case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE:
case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
log_info("send combined renegotiation request for %u cids", num_cids);
l2cap_send_general_signaling_packet(con_handle, signaling_cid, L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST,
sig_id, local_mtu, local_mps, cids);
break;case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
default:
break;default
}switch (matching_state) { ... }
}if (con_handle != HCI_CON_HANDLE_INVALID) { ... }
}l2cap_ecbm_run_channels (void) { ... }
/* ... */#endif
static void l2cap_run(void){
l2cap_run_signaling_response();
#ifdef ENABLE_CLASSIC
bool done = l2ap_run_information_requests();
if (done) return;/* ... */
#endif
#if defined(ENABLE_CLASSIC) || defined(ENABLE_BLE)
btstack_linked_list_iterator_t it;
#endif
#ifdef ENABLE_CLASSIC
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->channel_type != L2CAP_CHANNEL_TYPE_CLASSIC) continue;
bool finalized = l2cap_run_for_classic_channel(channel);
if (!finalized) {
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
l2cap_run_for_classic_channel_ertm(channel);
#endif
}if (!finalized) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_cbm_run_channels();
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_ecbm_run_channels();
#endif
#ifdef ENABLE_BLE
hci_connections_get_iterator(&it);
while(btstack_linked_list_iterator_has_next(&it)){
hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
if (!hci_is_le_connection_type(connection->address_type)) continue;
if (!hci_can_send_acl_packet_now(connection->con_handle)) continue;
switch (connection->le_con_parameter_update_state){
case CON_PARAMETER_UPDATE_SEND_REQUEST:
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_REQUEST, l2cap_next_sig_id(),
connection->le_conn_interval_min, connection->le_conn_interval_max, connection->le_conn_latency, connection->le_supervision_timeout);
break;case CON_PARAMETER_UPDATE_SEND_REQUEST:
case CON_PARAMETER_UPDATE_SEND_RESPONSE:
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_CHANGE_HCI_CON_PARAMETERS;
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 0);
break;case CON_PARAMETER_UPDATE_SEND_RESPONSE:
case CON_PARAMETER_UPDATE_DENY:
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_NONE;
l2cap_send_le_signaling_packet(connection->con_handle, CONNECTION_PARAMETER_UPDATE_RESPONSE, connection->le_con_param_update_identifier, 1);
break;case CON_PARAMETER_UPDATE_DENY:
default:
break;default
}switch (connection->le_con_parameter_update_state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
/* ... */#endif
if (l2cap_call_notify_channel_in_run){
l2cap_call_notify_channel_in_run = false;
l2cap_notify_channel_can_send();
}if (l2cap_call_notify_channel_in_run) { ... }
}{ ... }
#ifdef ENABLE_CLASSIC
static void l2cap_ready_to_connect(l2cap_channel_t * channel){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
hci_connection_t * connection = hci_connection_for_handle(channel->con_handle);
switch (connection->l2cap_state.information_state){
case L2CAP_INFORMATION_STATE_DONE:
break;case L2CAP_INFORMATION_STATE_DONE:
case L2CAP_INFORMATION_STATE_IDLE:
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;
case L2CAP_INFORMATION_STATE_IDLE:
default:
channel->state = L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES;
return;default
}switch (connection->l2cap_state.information_state) { ... }
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
}l2cap_ready_to_connect (l2cap_channel_t * channel) { ... }
static void l2cap_handle_connection_complete(hci_con_handle_t con_handle, l2cap_channel_t * channel){
if ((channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE) || (channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION)) {
log_info("connection complete con_handle %04x - for channel %p cid 0x%04x", (int) con_handle, channel, channel->local_cid);
channel->con_handle = con_handle;
if (channel->required_security_level > LEVEL_0){
channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
hci_remote_features_query(con_handle);
}if (channel->required_security_level > LEVEL_0) { ... } else {
l2cap_ready_to_connect(channel);
}else { ... }
}if ((channel->state == L2CAP_STATE_WAIT_CONNECTION_COMPLETE) || (channel->state == L2CAP_STATE_WILL_SEND_CREATE_CONNECTION)) { ... }
}l2cap_handle_connection_complete (hci_con_handle_t con_handle, l2cap_channel_t * channel) { ... }
static void l2cap_handle_remote_supported_features_received(l2cap_channel_t * channel){
if (channel->state != L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) return;
bool security_required = channel->required_security_level > LEVEL_0;
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) != 0){
if ((gap_get_security_mode() == GAP_SECURITY_MODE_4)
&& gap_ssp_supported_on_both_sides(channel->con_handle)
&& (channel->psm != PSM_SDP)
&& (gap_encryption_key_size(channel->con_handle) == 0)){
hci_disconnect_security_block(channel->con_handle);
return;
}if ((gap_get_security_mode() == GAP_SECURITY_MODE_4) && gap_ssp_supported_on_both_sides(channel->con_handle) && (channel->psm != PSM_SDP) && (gap_encryption_key_size(channel->con_handle) == 0)) { ... }
channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
if (channel->required_security_level <= gap_security_level(channel->con_handle)){
l2cap_handle_security_level_incoming_sufficient(channel);
}if (channel->required_security_level <= gap_security_level(channel->con_handle)) { ... } else {
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONN_RESP_PEND) == 0){
channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
}if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONN_RESP_PEND) == 0) { ... }
gap_request_security_level(channel->con_handle, channel->required_security_level);
}else { ... }
}if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) != 0) { ... } else {
if (security_required){
channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
gap_request_security_level(channel->con_handle, channel->required_security_level);
}if (security_required) { ... } else {
l2cap_ready_to_connect(channel);
}else { ... }
}else { ... }
}l2cap_handle_remote_supported_features_received (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CHANNELS
static l2cap_channel_t * l2cap_create_channel_entry(btstack_packet_handler_t packet_handler, l2cap_channel_type_t channel_type, bd_addr_t address, bd_addr_type_t address_type,
uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level){
l2cap_channel_t * channel = btstack_memory_l2cap_channel_get();
if (!channel) {
return NULL;
}if (!channel) { ... }
channel->packet_handler = packet_handler;
channel->channel_type = channel_type;
bd_addr_copy(channel->address, address);
channel->address_type = address_type;
channel->psm = psm;
channel->local_mtu = local_mtu;
channel->remote_mtu = L2CAP_DEFAULT_MTU;
channel->required_security_level = security_level;
channel->local_cid = l2cap_next_local_cid();
channel->con_handle = HCI_CON_HANDLE_INVALID;
channel->state = L2CAP_STATE_WILL_SEND_CREATE_CONNECTION;
channel->state_var = L2CAP_CHANNEL_STATE_VAR_NONE;
channel->remote_sig_id = L2CAP_SIG_ID_INVALID;
channel->local_sig_id = L2CAP_SIG_ID_INVALID;
log_info("create channel %p, local_cid 0x%04x", channel, channel->local_cid);
return channel;
}l2cap_create_channel_entry (btstack_packet_handler_t packet_handler, l2cap_channel_type_t channel_type, bd_addr_t address, bd_addr_type_t address_type, uint16_t psm, uint16_t local_mtu, gap_security_level_t security_level) { ... }
static void l2cap_free_channel_entry(l2cap_channel_t * channel){
log_info("free channel %p, local_cid 0x%04x", channel, channel->local_cid);
l2cap_stop_rtx(channel);
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
l2cap_ertm_stop_retransmission_timer(channel);
l2cap_ertm_stop_monitor_timer(channel);/* ... */
#endif
btstack_memory_l2cap_channel_free(channel);
}l2cap_free_channel_entry (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
/* ... */
uint8_t l2cap_create_channel(btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid){
uint16_t local_mtu = btstack_min(mtu, l2cap_max_mtu());
const gap_security_level_t security_level = l2cap_security_level_0_allowed_for_PSM(psm) ? LEVEL_0 : gap_get_security_level();
log_info("create channel addr %s psm 0x%x mtu %u -> local mtu %u, sec level %u", bd_addr_to_str(address), psm, mtu, local_mtu, (int) security_level);
l2cap_channel_t * channel = l2cap_create_channel_entry(channel_packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, address, BD_ADDR_TYPE_ACL, psm, local_mtu, security_level);
if (!channel) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!channel) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
#endif
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
if (out_local_cid){
*out_local_cid = channel->local_cid;
}if (out_local_cid) { ... }
hci_connection_t * conn = hci_connection_for_bd_addr_and_type(address, BD_ADDR_TYPE_ACL);
if (conn && conn->state == OPEN){
l2cap_handle_connection_complete(conn->con_handle, channel);
if ((channel->state == L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) && hci_remote_features_available(conn->con_handle)) {
l2cap_handle_remote_supported_features_received(channel);
}if ((channel->state == L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES) && hci_remote_features_available(conn->con_handle)) { ... }
}if (conn && conn->state == OPEN) { ... }
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_create_channel (btstack_packet_handler_t channel_packet_handler, bd_addr_t address, uint16_t psm, uint16_t mtu, uint16_t * out_local_cid) { ... }
static void l2cap_handle_connection_failed_for_addr(bd_addr_t address, uint8_t status){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (bd_addr_cmp( channel->address, address) != 0) continue;
switch (channel->state){
case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
channel->state = L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD;
break;case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
default:
break;default
}switch (channel->state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
int done = 0;
while (!done) {
done = 1;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->state == L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD){
done = 0;
l2cap_handle_channel_open_failed(channel, status);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
break;
}if (channel->state == L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}while (!done) { ... }
}l2cap_handle_connection_failed_for_addr (bd_addr_t address, uint8_t status) { ... }
static void l2cap_handle_connection_success_for_addr(bd_addr_t address, hci_con_handle_t handle){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if ( ! bd_addr_cmp( channel->address, address) ){
l2cap_handle_connection_complete(handle, channel);
}if (! bd_addr_cmp( channel->address, address)) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
#ifdef ENABLE_L2CAP_INFORMATION_REQUESTS_ON_CONNECT
hci_connection_t * hci_connection = hci_connection_for_handle(handle);
btstack_assert(hci_connection != NULL);
hci_connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;/* ... */
#endif
l2cap_run();
}l2cap_handle_connection_success_for_addr (bd_addr_t address, hci_con_handle_t handle) { ... }
/* ... */#endif
static bool l2cap_channel_ready_to_send(l2cap_channel_t * channel){
switch (channel->channel_type){
#ifdef ENABLE_CLASSIC
case L2CAP_CHANNEL_TYPE_CLASSIC:
if (channel->state != L2CAP_STATE_OPEN) return false;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) {
if (channel->unacked_frames >= btstack_min(channel->num_stored_tx_frames, channel->remote_tx_window_size)) return false;
return hci_can_send_acl_packet_now(channel->con_handle);
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
if (!channel->waiting_for_can_send_now) return false;
return hci_can_send_acl_packet_now(channel->con_handle);case L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_FIXED_CLASSIC:
case L2CAP_CHANNEL_TYPE_CONNECTIONLESS:
if (!channel->waiting_for_can_send_now) return false;
return hci_can_send_acl_classic_packet_now();/* ... */
#endif
#ifdef ENABLE_BLE
case L2CAP_CHANNEL_TYPE_FIXED_LE:
if (!channel->waiting_for_can_send_now) return false;
return hci_can_send_acl_le_packet_now();
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_FIXED_LE:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
if (channel->state != L2CAP_STATE_OPEN) return false;
if (channel->send_sdu_buffer == NULL) return false;
if (channel->credits_outgoing == 0u) return false;
return hci_can_send_acl_packet_now(channel->con_handle);/* ... */
#endif/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
if (channel->state != L2CAP_STATE_OPEN) return false;
if (channel->send_sdu_buffer == NULL) return false;
if (channel->credits_outgoing == 0u) return false;
return hci_can_send_acl_packet_now(channel->con_handle);/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
return false;default
}switch (channel->channel_type) { ... }
}{ ... }
static void l2cap_channel_trigger_send(l2cap_channel_t * channel){
switch (channel->channel_type){
#ifdef ENABLE_CLASSIC
case L2CAP_CHANNEL_TYPE_CLASSIC:
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) {
l2cap_ertm_channel_send_information_frame(channel);
return;
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
channel->waiting_for_can_send_now = 0;
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
break;case L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CONNECTIONLESS:
case L2CAP_CHANNEL_TYPE_FIXED_CLASSIC:
channel->waiting_for_can_send_now = 0;
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
break;/* ... */
#endif
#ifdef ENABLE_BLE
case L2CAP_CHANNEL_TYPE_FIXED_LE:
channel->waiting_for_can_send_now = 0;
l2cap_emit_can_send_now(channel->packet_handler, channel->local_cid);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_FIXED_LE:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
l2cap_credit_based_send_pdu(channel);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
l2cap_credit_based_send_pdu(channel);
break;/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
btstack_unreachable();
break;default
}switch (channel->channel_type) { ... }
}{ ... }
static void l2cap_notify_channel_can_send(void){
bool done = false;
while (!done){
done = true;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
bool ready = l2cap_channel_ready_to_send(channel);
if (!ready) continue;
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_channel_trigger_send(channel);
done = false;
break;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}while (!done) { ... }
}{ ... }
#ifdef L2CAP_USES_CHANNELS
uint8_t l2cap_disconnect(uint16_t local_cid){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_disconnect (uint16_t local_cid) { ... }
static int l2cap_send_open_failed_on_hci_disconnect(l2cap_channel_t * channel){
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) return 0;
switch (channel->state){
case L2CAP_STATE_WILL_SEND_CREATE_CONNECTION:
case L2CAP_STATE_WAIT_CONNECTION_COMPLETE:
case L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES:
case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
case L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT:
case L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES:
case L2CAP_STATE_WAIT_CONNECT_RSP:
case L2CAP_STATE_CONFIG:
case L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST:
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST:
case L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE:
case L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD:
return 1;
case L2CAP_STATE_EMIT_OPEN_FAILED_AND_DISCARD:
case L2CAP_STATE_OPEN:
case L2CAP_STATE_CLOSED:
case L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES:
case L2CAP_STATE_WAIT_DISCONNECT:
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_INSUFFICIENT_SECURITY:
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE:
case L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT:
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
case L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE:
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE:
case L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT:
case L2CAP_STATE_INVALID:
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
return 0;
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
default:
btstack_assert(false);
return 0;default
}switch (channel->state) { ... }
}l2cap_send_open_failed_on_hci_disconnect (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static void l2cap_handle_hci_disconnect_event(l2cap_channel_t * channel){
if (l2cap_send_open_failed_on_hci_disconnect(channel)){
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
}if (l2cap_send_open_failed_on_hci_disconnect(channel)) { ... } else {
l2cap_handle_channel_closed(channel);
}else { ... }
l2cap_free_channel_entry(channel);
}l2cap_handle_hci_disconnect_event (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
static void l2cap_handle_hci_le_disconnect_event(l2cap_channel_t * channel){
if (l2cap_send_open_failed_on_hci_disconnect(channel)){
l2cap_cbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
}if (l2cap_send_open_failed_on_hci_disconnect(channel)) { ... } else {
l2cap_emit_channel_closed(channel);
}else { ... }
l2cap_free_channel_entry(channel);
}l2cap_handle_hci_le_disconnect_event (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static void l2cap_check_classic_timeout(hci_con_handle_t handle){
if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) {
return;
}if (gap_get_connection_type(handle) != GAP_CONNECTION_ACL) { ... }
if (hci_authentication_active_for_handle(handle)) {
return;
}if (hci_authentication_active_for_handle(handle)) { ... }
bool hci_con_used = false;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
hci_con_used = true;
break;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (hci_con_used) {
return;
}if (hci_con_used) { ... }
if (!hci_can_send_command_packet_now()) {
return;
}if (!hci_can_send_command_packet_now()) { ... }
hci_send_cmd(&hci_disconnect, handle, 0x13);
}l2cap_check_classic_timeout (hci_con_handle_t handle) { ... }
static void l2cap_handle_features_complete(hci_con_handle_t handle){
if (hci_remote_features_available(handle) == false){
return;
}if (hci_remote_features_available(handle) == false) { ... }
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
log_info("remote supported features, channel %p, cid %04x - state %u", channel, channel->local_cid, channel->state);
l2cap_handle_remote_supported_features_received(channel);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_handle_features_complete (hci_con_handle_t handle) { ... }
static void l2cap_handle_security_level_incoming_sufficient(l2cap_channel_t * channel){
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
l2cap_emit_incoming_connection(channel);
}l2cap_handle_security_level_incoming_sufficient (l2cap_channel_t * channel) { ... }
static void l2cap_handle_security_level(hci_con_handle_t handle, gap_security_level_t actual_level){
log_info("security level update for handle 0x%04x", handle);
if (actual_level > LEVEL_0){
hci_connection_t * hci_connection = hci_connection_for_handle(handle);
btstack_assert(hci_connection != NULL);
if (hci_connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_IDLE){
hci_connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_EXTENDED_FEATURE_REQUEST;
}if (hci_connection->l2cap_state.information_state == L2CAP_INFORMATION_STATE_IDLE) { ... }
}if (actual_level > LEVEL_0) { ... }
bool done = false;
while (!done){
done = true;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
gap_security_level_t required_level = channel->required_security_level;
log_info("channel %p, cid %04x - state %u: actual %u >= required %u?", channel, channel->local_cid, channel->state, actual_level, required_level);
switch (channel->state){
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
if (actual_level >= required_level){
l2cap_handle_security_level_incoming_sufficient(channel);
}if (actual_level >= required_level) { ... } else {
channel->reason = L2CAP_CONNECTION_RESULT_SECURITY_BLOCK;
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
}else { ... }
break;
case L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE:
case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
if (actual_level >= required_level){
l2cap_ready_to_connect(channel);
}if (actual_level >= required_level) { ... } else {
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
}else { ... }
break;
case L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE:
default:
break;default
}switch (channel->state) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}while (!done) { ... }
}l2cap_handle_security_level (hci_con_handle_t handle, gap_security_level_t actual_level) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CHANNELS
static void l2cap_handle_disconnection_complete(hci_con_handle_t handle){
btstack_linked_list_t channels_to_close = NULL;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
btstack_linked_list_iterator_remove(&it);
btstack_linked_list_add(&channels_to_close, (btstack_linked_item_t *) channel);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
btstack_linked_list_iterator_init(&it, &channels_to_close);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
btstack_linked_list_iterator_remove(&it);
switch(channel->channel_type){
#ifdef ENABLE_CLASSIC
case L2CAP_CHANNEL_TYPE_CLASSIC:
l2cap_handle_hci_disconnect_event(channel);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CLASSIC:
case L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
l2cap_handle_hci_le_disconnect_event(channel);
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase L2CAP_CHANNEL_TYPE_CHANNEL_CBM:
case L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
switch (channel->state) {
case L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST:
case L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE:
l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_BASEBAND_DISCONNECT);
break;case L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE:
case L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST:
case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
l2cap_ecbm_emit_reconfigure_complete(channel, 0xffff);
break;case L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE:
default:
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
break;default
}switch (channel->state) { ... }
l2cap_free_channel_entry(channel);
break;/* ... */
#endifcase L2CAP_CHANNEL_TYPE_CHANNEL_ECBM:
default:
break;default
}switch (channel->channel_type) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_handle_disconnection_complete (hci_con_handle_t handle) { ... }
/* ... */#endif
static void l2cap_hci_event_handler(uint8_t packet_type, uint16_t cid, uint8_t *packet, uint16_t size){
UNUSED(packet_type);
UNUSED(cid);
UNUSED(size);
#ifdef ENABLE_CLASSIC
bd_addr_t address;
gap_security_level_t security_level;/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
hci_con_handle_t handle;
#endif
switch(hci_event_packet_get_type(packet)){
case HCI_EVENT_TRANSPORT_PACKET_SENT:
case HCI_EVENT_NUMBER_OF_COMPLETED_PACKETS:
case BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
#ifdef ENABLE_TESTING_SUPPORTcase BTSTACK_EVENT_NR_CONNECTIONS_CHANGED:
case HCI_EVENT_NOP:
#endif
l2cap_call_notify_channel_in_run = true;
break;
case HCI_EVENT_NOP:
case HCI_EVENT_COMMAND_STATUS:
#ifdef ENABLE_CLASSIC
if (hci_event_command_status_get_command_opcode(packet) == HCI_OPCODE_HCI_CREATE_CONNECTION){
(void)memcpy(address, l2cap_outgoing_classic_addr, 6);
memset(l2cap_outgoing_classic_addr, 0, 6);
uint8_t status = hci_event_command_status_get_status(packet);
if (status){
l2cap_handle_connection_failed_for_addr(address, status);
}if (status) { ... }
}if (hci_event_command_status_get_command_opcode(packet) == HCI_OPCODE_HCI_CREATE_CONNECTION) { ... }
/* ... */#endif
l2cap_run();
break;
#ifdef ENABLE_CLASSIC
case HCI_EVENT_CONNECTION_COMPLETE:
reverse_bd_addr(&packet[5], address);
if (packet[2] == 0){
handle = little_endian_read_16(packet, 3);
l2cap_handle_connection_success_for_addr(address, handle);
}if (packet[2] == 0) { ... } else {
l2cap_handle_connection_failed_for_addr(address, packet[2]);
}else { ... }
break;
case HCI_EVENT_CONNECTION_COMPLETE:
case HCI_EVENT_COMMAND_COMPLETE:
if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_CREATE_CONNECTION_CANCEL) {
if (packet[5] == 0){
reverse_bd_addr(&packet[6], address);
l2cap_handle_connection_failed_for_addr(address, 0x16);
}if (packet[5] == 0) { ... }
}if (hci_event_command_complete_get_command_opcode(packet) == HCI_OPCODE_HCI_CREATE_CONNECTION_CANCEL) { ... }
l2cap_run();
break;/* ... */
#endif
#ifdef L2CAP_USES_CHANNELS
case HCI_EVENT_DISCONNECTION_COMPLETE:
handle = little_endian_read_16(packet, 3);
l2cap_handle_disconnection_complete(handle);
break;/* ... */
#endif
#ifdef ENABLE_CLASSIC
case L2CAP_EVENT_TIMEOUT_CHECK:
handle = little_endian_read_16(packet, 2);
l2cap_check_classic_timeout(handle);
break;
case L2CAP_EVENT_TIMEOUT_CHECK:
case HCI_EVENT_READ_REMOTE_SUPPORTED_FEATURES_COMPLETE:
case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE:
handle = little_endian_read_16(packet, 3);
l2cap_handle_features_complete(handle);
break;
case HCI_EVENT_READ_REMOTE_EXTENDED_FEATURES_COMPLETE:
case GAP_EVENT_SECURITY_LEVEL:
handle = little_endian_read_16(packet, 2);
security_level = (gap_security_level_t) packet[4];
l2cap_handle_security_level(handle, security_level);
break;/* ... */
#endif
default:
break;default
}switch (hci_event_packet_get_type(packet)) { ... }
l2cap_run();
}{ ... }
static void l2cap_register_signaling_response(hci_con_handle_t handle, uint8_t code, uint8_t sig_id, uint16_t cid, uint16_t data){
if (l2cap_signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) {
l2cap_signaling_responses[l2cap_signaling_responses_pending].handle = handle;
l2cap_signaling_responses[l2cap_signaling_responses_pending].code = code;
l2cap_signaling_responses[l2cap_signaling_responses_pending].sig_id = sig_id;
l2cap_signaling_responses[l2cap_signaling_responses_pending].cid = cid;
l2cap_signaling_responses[l2cap_signaling_responses_pending].data = data;
l2cap_signaling_responses_pending++;
l2cap_run();
}if (l2cap_signaling_responses_pending < NR_PENDING_SIGNALING_RESPONSES) { ... }
}{ ... }
#ifdef ENABLE_CLASSIC
static void l2cap_handle_disconnect_request(l2cap_channel_t *channel, uint8_t identifier){
switch (channel->state){
case L2CAP_STATE_CONFIG:
case L2CAP_STATE_OPEN:
case L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST:
case L2CAP_STATE_WAIT_DISCONNECT:
break;case L2CAP_STATE_WAIT_DISCONNECT:
default:
return;default
}switch (channel->state) { ... }
channel->remote_sig_id = identifier;
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
l2cap_run();
}l2cap_handle_disconnect_request (l2cap_channel_t *channel, uint8_t identifier) { ... }
static void l2cap_handle_connection_request(hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid){
l2cap_service_t *service = l2cap_get_service(psm);
if (!service) {
l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CONNECTION_RESULT_PSM_NOT_SUPPORTED);
return;
}if (!service) { ... }
hci_connection_t * hci_connection = hci_connection_for_handle( handle );
if (!hci_connection) {
log_error("no hci_connection for handle %u", handle);
return;
}if (!hci_connection) { ... }
gap_security_level_t required_level = service->required_security_level;
if (gap_get_secure_connections_only_mode() && (required_level != LEVEL_0)){
if (gap_secure_connection(handle)){
required_level = LEVEL_4;
}if (gap_secure_connection(handle)) { ... } else {
l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CONNECTION_RESULT_SECURITY_BLOCK);
return;
}else { ... }
}if (gap_get_secure_connections_only_mode() && (required_level != LEVEL_0)) { ... }
l2cap_channel_t * channel = l2cap_create_channel_entry(service->packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, hci_connection->address, BD_ADDR_TYPE_ACL,
psm, service->mtu, required_level);
if (!channel){
l2cap_register_signaling_response(handle, CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE);
return;
}if (!channel) { ... }
channel->con_handle = handle;
channel->remote_cid = source_cid;
channel->remote_sig_id = sig_id;
if (channel->local_mtu > l2cap_max_mtu()) {
channel->local_mtu = l2cap_max_mtu();
}if (channel->local_mtu > l2cap_max_mtu()) { ... }
channel->state = L2CAP_STATE_WAIT_REMOTE_SUPPORTED_FEATURES;
channel->state_var = L2CAP_CHANNEL_STATE_VAR_INCOMING;
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
if (required_level > LEVEL_0){
if (hci_remote_features_available(handle)) {
l2cap_handle_remote_supported_features_received(channel);
}if (hci_remote_features_available(handle)) { ... } else {
channel->state_var |= L2CAP_CHANNEL_STATE_VAR_SEND_CONN_RESP_PEND;
hci_remote_features_query(handle);
}else { ... }
}if (required_level > LEVEL_0) { ... } else {
l2cap_handle_security_level_incoming_sufficient(channel);
}else { ... }
}l2cap_handle_connection_request (hci_con_handle_t handle, uint8_t sig_id, uint16_t psm, uint16_t source_cid) { ... }
void l2cap_accept_connection(uint16_t local_cid){
log_info("L2CAP_ACCEPT_CONNECTION local_cid 0x%x", local_cid);
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
log_error("accept called but local_cid 0x%x not found", local_cid);
return;
}if (!channel) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
channel->mode = L2CAP_CHANNEL_MODE_BASIC;/* ... */
#endif
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
l2cap_run();
}l2cap_accept_connection (uint16_t local_cid) { ... }
void l2cap_decline_connection(uint16_t local_cid){
log_info("decline local_cid 0x%x", local_cid);
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid( local_cid);
if (!channel) {
log_error( "l2cap_decline_connection called but local_cid 0x%x not found", local_cid);
return;
}if (!channel) { ... }
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
channel->reason = L2CAP_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE;
l2cap_run();
}l2cap_decline_connection (uint16_t local_cid) { ... }
static void l2cap_signaling_handle_configure_request(l2cap_channel_t *channel, uint8_t *command){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
uint8_t use_fcs = 1;
#endif
channel->remote_sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
uint16_t flags = little_endian_read_16(command, 6);
if (flags & 1) {
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT);
}if (flags & 1) { ... }
uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
uint16_t pos = 8;
while (pos < end_pos){
uint8_t option_hint = command[pos] >> 7;
uint8_t option_type = command[pos] & 0x7f;
pos++;
uint8_t length = command[pos++];
if ((option_type == L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT) && (length == 2)){
channel->remote_mtu = little_endian_read_16(command, pos);
log_info("Remote MTU %u", channel->remote_mtu);
if (channel->remote_mtu > l2cap_max_mtu()){
log_info("Remote MTU %u larger than outgoing buffer, only using MTU = %u", channel->remote_mtu, l2cap_max_mtu());
channel->remote_mtu = l2cap_max_mtu();
}if (channel->remote_mtu > l2cap_max_mtu()) { ... }
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_MTU);
}if ((option_type == L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT) && (length == 2)) { ... }
if ((option_type == L2CAP_CONFIG_OPTION_TYPE_FLUSH_TIMEOUT) && (length == 2)){
channel->flush_timeout = little_endian_read_16(command, pos);
log_info("Flush timeout: %u ms", channel->flush_timeout);
}if ((option_type == L2CAP_CONFIG_OPTION_TYPE_FLUSH_TIMEOUT) && (length == 2)) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9){
l2cap_channel_mode_t mode = (l2cap_channel_mode_t) command[pos];
switch(channel->mode){
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
channel->remote_tx_window_size = command[pos+1];
channel->remote_max_transmit = command[pos+2];
channel->remote_retransmission_timeout_ms = little_endian_read_16(command, pos + 3);
channel->remote_monitor_timeout_ms = little_endian_read_16(command, pos + 5);
{
uint16_t remote_mps = little_endian_read_16(command, pos + 7);
if (remote_mps < channel->remote_mps){
uint16_t num_bytes_per_tx_buffer_before = sizeof(l2cap_ertm_tx_packet_state_t) + channel->remote_mps;
uint16_t tx_storage = channel->num_tx_buffers * num_bytes_per_tx_buffer_before;
channel->remote_mps = remote_mps;
uint16_t num_bytes_per_tx_buffer_now = sizeof(l2cap_ertm_tx_packet_state_t) + channel->remote_mps;
channel->num_tx_buffers = tx_storage / num_bytes_per_tx_buffer_now;
uint32_t total_storage = (sizeof(l2cap_ertm_rx_packet_state_t) + channel->local_mps) * channel->num_rx_buffers + tx_storage + channel->local_mtu;
l2cap_ertm_setup_buffers(channel, (uint8_t *) channel->rx_packets_state, total_storage);
}if (remote_mps < channel->remote_mps) { ... }
uint16_t effective_mtu = channel->remote_mps * channel->num_tx_buffers - 2;
channel->remote_mtu = btstack_min( effective_mtu, channel->remote_mtu);
...}
log_info("FC&C config: tx window: %u, max transmit %u, retrans timeout %u, monitor timeout %u, mps %u",
channel->remote_tx_window_size,
channel->remote_max_transmit,
channel->remote_retransmission_timeout_ms,
channel->remote_monitor_timeout_ms,
channel->remote_mps);
if (channel->ertm_mandatory && mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
}if (channel->ertm_mandatory && mode != L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... } else {
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM);
}else { ... }
break;case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
case L2CAP_CHANNEL_MODE_BASIC:
switch (mode){
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED){
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
}if (channel->state_var & L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED) { ... }
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_BASIC_FALLBACK_TRIED);
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_REJECTED);
break;case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
default:
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM);
break;default
}switch (mode) { ... }
break;case L2CAP_CHANNEL_MODE_BASIC:
default:
break;default
}switch (channel->mode) { ... }
}if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9) { ... }
if (option_type == L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE && length == 1){
use_fcs = command[pos];
}if (option_type == L2CAP_CONFIG_OPTION_TYPE_FRAME_CHECK_SEQUENCE && length == 1) { ... } /* ... */
#endif
if ((option_hint == 0) && ((option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT) || (option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE))){
log_info("l2cap cid %u, unknown option 0x%02x", channel->local_cid, option_type);
channel->unknown_option = option_type;
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
}if ((option_hint == 0) && ((option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT) || (option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE))) { ... }
pos += length;
}while (pos < end_pos) { ... }
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
uint8_t update = channel->fcs_option || use_fcs;
log_info("local fcs: %u, remote fcs: %u -> %u", channel->fcs_option, use_fcs, update);
channel->fcs_option = update;
if (((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM) == 0) & (channel->ertm_mandatory)){
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
}if (((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_ERTM) == 0) & (channel->ertm_mandatory)) { ... }
/* ... */#endif
}l2cap_signaling_handle_configure_request (l2cap_channel_t *channel, uint8_t *command) { ... }
static void l2cap_signaling_handle_configure_response(l2cap_channel_t *channel, uint16_t result, uint8_t *command){
log_info("l2cap_signaling_handle_configure_response");
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
uint16_t end_pos = 4 + little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
uint16_t pos = 10;
while (pos < end_pos){
uint8_t option_hint = command[pos] >> 7;
uint8_t option_type = command[pos] & 0x7f;
pos++;
uint8_t length = command[pos++];
if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9){
switch (channel->mode){
case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
if (channel->ertm_mandatory){
}if (channel->ertm_mandatory) { ... } else {
if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
}if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS) { ... }
}else { ... }
break;case L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION:
case L2CAP_CHANNEL_MODE_BASIC:
if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS){
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
}if (result == L2CAP_CONF_RESULT_UNACCEPTABLE_PARAMETERS) { ... }
break;case L2CAP_CHANNEL_MODE_BASIC:
default:
break;default
}switch (channel->mode) { ... }
}if (option_type == L2CAP_CONFIG_OPTION_TYPE_RETRANSMISSION_AND_FLOW_CONTROL && length == 9) { ... }
if (option_hint == 0 && (option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT || option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE)){
log_info("l2cap cid %u, unknown option 0x%02x", channel->local_cid, option_type);
channel->unknown_option = option_type;
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_INVALID);
}if (option_hint == 0 && (option_type < L2CAP_CONFIG_OPTION_TYPE_MAX_TRANSMISSION_UNIT || option_type > L2CAP_CONFIG_OPTION_TYPE_EXTENDED_WINDOW_SIZE)) { ... }
pos += length;
}while (pos < end_pos) { ... }
/* ... */#else
UNUSED(channel);
UNUSED(result);
UNUSED(command); /* ... */
#endif
}l2cap_signaling_handle_configure_response (l2cap_channel_t *channel, uint16_t result, uint8_t *command) { ... }
static int l2cap_channel_ready_for_open(l2cap_channel_t *channel){
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP) == 0) return 0;
if ((channel->state_var & L2CAP_CHANNEL_STATE_VAR_SENT_CONF_RSP) == 0) return 0;
if (channel->state == L2CAP_STATE_OPEN) return 0;
return 1;
}l2cap_channel_ready_for_open (l2cap_channel_t *channel) { ... }
static void l2cap_signaling_handler_channel(l2cap_channel_t *channel, uint8_t *command){
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
uint8_t identifier = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
uint16_t cmd_len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
uint16_t result = 0;
log_info("L2CAP signaling handler code %u, state %u", code, channel->state);
if (code == DISCONNECTION_REQUEST){
l2cap_handle_disconnect_request(channel, identifier);
return;
}if (code == DISCONNECTION_REQUEST) { ... }
switch (channel->state) {
case L2CAP_STATE_WAIT_CONNECT_RSP:
switch (code){
case CONNECTION_RESPONSE:
if (cmd_len < 8){
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
break;
}if (cmd_len < 8) { ... }
l2cap_stop_rtx(channel);
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
switch (result) {
case 0:
channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
channel->state = L2CAP_STATE_CONFIG;
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
break;case 0:
case 1:
l2cap_start_ertx(channel);
break;case 1:
default:
channel->state = L2CAP_STATE_CLOSED;
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result);
if ((L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result) == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY){
gap_drop_link_key_for_bd_addr(channel->address);
}if ((L2CAP_CONNECTION_RESPONSE_RESULT_SUCCESSFUL + result) == L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY) { ... }
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
break;default
}switch (result) { ... }
break;
case CONNECTION_RESPONSE:
default:
break;default
}switch (code) { ... }
break;
case L2CAP_STATE_WAIT_CONNECT_RSP:
case L2CAP_STATE_CONFIG:
switch (code) {
case CONFIGURE_REQUEST:
if (cmd_len < 4){
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
break;
}if (cmd_len < 4) { ... }
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP);
l2cap_signaling_handle_configure_request(channel, command);
if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)){
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_REQ);
}if (!(channel->state_var & L2CAP_CHANNEL_STATE_VAR_SEND_CONF_RSP_CONT)) { ... }
break;case CONFIGURE_REQUEST:
case CONFIGURE_RESPONSE:
if (cmd_len < 6){
l2cap_register_signaling_response(channel->con_handle, COMMAND_REJECT, identifier, 0, L2CAP_REJ_CMD_UNKNOWN);
break;
}if (cmd_len < 6) { ... }
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
l2cap_stop_rtx(channel);
l2cap_signaling_handle_configure_response(channel, result, command);
switch (result){
case 0:
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_RCVD_CONF_RSP);
break;case 0:
case 4:
l2cap_start_ertx(channel);
break;case 4:
default:
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->ertm_mandatory){
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
break;
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION && channel->ertm_mandatory) { ... } /* ... */
#endif
channelStateVarSetFlag(channel, L2CAP_CHANNEL_STATE_VAR_SEND_CONF_REQ);
break;default
}switch (result) { ... }
break;case CONFIGURE_RESPONSE:
default:
break;default
}switch (code) { ... }
if (l2cap_channel_ready_for_open(channel)){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
uint16_t effective_mps = btstack_min(channel->remote_mps, channel->local_mps);
uint16_t usable_mtu = channel->num_tx_buffers == 1 ? effective_mps : channel->num_tx_buffers * effective_mps - 2;
if (usable_mtu < channel->remote_mtu){
log_info("Remote MTU %u > max storable ERTM packet, only using MTU = %u", channel->remote_mtu, usable_mtu);
channel->remote_mtu = usable_mtu;
}if (usable_mtu < channel->remote_mtu) { ... }
}if (channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
channel->state = L2CAP_STATE_OPEN;
l2cap_emit_channel_opened(channel, 0);
}if (l2cap_channel_ready_for_open(channel)) { ... }
break;
case L2CAP_STATE_CONFIG:
case L2CAP_STATE_WAIT_DISCONNECT:
switch (code) {
case DISCONNECTION_RESPONSE:
l2cap_finalize_channel_close(channel);
break;case DISCONNECTION_RESPONSE:
default:
break;default
}switch (code) { ... }
break;
case L2CAP_STATE_WAIT_DISCONNECT:
case L2CAP_STATE_CLOSED:
break;
case L2CAP_STATE_CLOSED:
case L2CAP_STATE_OPEN:
break;case L2CAP_STATE_OPEN:
default:
break;default
}switch (channel->state) { ... }
}l2cap_signaling_handler_channel (l2cap_channel_t *channel, uint8_t *command) { ... }
#ifdef ENABLE_CLASSIC
static void l2cap_handle_information_request_complete(hci_connection_t * connection){
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_DONE;
uint8_t event[8];
event[0] = L2CAP_EVENT_INFORMATION_RESPONSE;
event[1] = sizeof(event) - 2;
little_endian_store_16(event, 2, connection->con_handle);
little_endian_store_16(event, 4, connection->l2cap_state.extended_feature_mask);
little_endian_store_16(event, 6, connection->l2cap_state.fixed_channels_supported);
l2cap_emit_event(event, sizeof(event));
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != connection->con_handle) continue;
if (channel->state == L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES){
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_ACCEPT;
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)){
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
if (channel->ertm_mandatory){
log_info("ERTM mandatory -> reject connection");
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_RESPONSE_DECLINE;
channel->reason = L2CAP_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE;
}if (channel->ertm_mandatory) { ... } else {
log_info("ERTM not supported by remote -> use Basic mode");
}else { ... }
}if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)) { ... }
/* ... */#endif
continue;
}if (channel->state == L2CAP_STATE_WAIT_INCOMING_EXTENDED_FEATURES) { ... }
if (channel->state == L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES){
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)){
if (channel->ertm_mandatory){
channel->state = L2CAP_STATE_CLOSED;
l2cap_handle_channel_open_failed(channel, L2CAP_CONNECTION_RESPONSE_RESULT_ERTM_NOT_SUPPORTED);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
continue;
}if (channel->ertm_mandatory) { ... } else {
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_ERTM_BUFFER_RELEASED);
channel->mode = L2CAP_CHANNEL_MODE_BASIC;
}else { ... }
}if ((channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) && ((connection->l2cap_state.extended_feature_mask & 0x08) == 0)) { ... }
/* ... */#endif
channel->state = L2CAP_STATE_WILL_SEND_CONNECTION_REQUEST;
continue;
}if (channel->state == L2CAP_STATE_WAIT_OUTGOING_EXTENDED_FEATURES) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_handle_information_request_complete (hci_connection_t * connection) { ... }
/* ... */#endif
static void l2cap_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command){
hci_connection_t * connection;
btstack_linked_list_iterator_t it;
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
uint8_t sig_id = command[L2CAP_SIGNALING_COMMAND_SIGID_OFFSET];
uint16_t cmd_len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
switch(code) {
case CONNECTION_REQUEST:
if (cmd_len == 4){
uint16_t psm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
l2cap_handle_connection_request(handle, sig_id, psm, source_cid);
}if (cmd_len == 4) { ... } else {
l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
}else { ... }
return;
case CONNECTION_REQUEST:
case ECHO_REQUEST:
l2cap_register_signaling_response(handle, code, sig_id, 0, 0);
return;
case ECHO_REQUEST:
case INFORMATION_REQUEST:
if (cmd_len == 2) {
uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
l2cap_register_signaling_response(handle, code, sig_id, 0, info_type);
return;
}if (cmd_len == 2) { ... }
break;
case INFORMATION_REQUEST:
case INFORMATION_RESPONSE:
connection = hci_connection_for_handle(handle);
if (!connection) return;
switch (connection->l2cap_state.information_state){
case L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE:
if (cmd_len >= 6) {
uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
uint16_t result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
if (result == 0 && info_type == L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED) {
connection->l2cap_state.extended_feature_mask = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
}if (result == 0 && info_type == L2CAP_INFO_TYPE_EXTENDED_FEATURES_SUPPORTED) { ... }
log_info("extended features mask 0x%02x", connection->l2cap_state.extended_feature_mask);
if ((connection->l2cap_state.extended_feature_mask & 0x80) != 0){
connection->l2cap_state.information_state = L2CAP_INFORMATION_STATE_W2_SEND_FIXED_CHANNELS_REQUEST;
}if ((connection->l2cap_state.extended_feature_mask & 0x80) != 0) { ... } else {
l2cap_handle_information_request_complete(connection);
}else { ... }
}if (cmd_len >= 6) { ... }
break;case L2CAP_INFORMATION_STATE_W4_EXTENDED_FEATURE_RESPONSE:
case L2CAP_INFORMATION_STATE_W4_FIXED_CHANNELS_RESPONSE:
if (cmd_len >= 12) {
uint16_t info_type = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
uint16_t result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
if (result == 0 && info_type == L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED) {
connection->l2cap_state.fixed_channels_supported = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
}if (result == 0 && info_type == L2CAP_INFO_TYPE_FIXED_CHANNELS_SUPPORTED) { ... }
log_info("fixed channels mask 0x%02x", connection->l2cap_state.fixed_channels_supported);
l2cap_handle_information_request_complete(connection);
}if (cmd_len >= 12) { ... }
break;case L2CAP_INFORMATION_STATE_W4_FIXED_CHANNELS_RESPONSE:
default:
break;default
}switch (connection->l2cap_state.information_state) { ... }
return;
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase INFORMATION_RESPONSE:
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST:
case L2CAP_CREDIT_BASED_CONNECTION_RESPONSE:
case L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST:
case L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE:
l2cap_ecbm_signaling_handler_dispatch(handle, L2CAP_CID_SIGNALING, command, sig_id);
return;case L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE:
case L2CAP_FLOW_CONTROL_CREDIT_INDICATION:
if (l2cap_credit_based_handle_credit_indication(handle, command, cmd_len)) return;
break;
case L2CAP_FLOW_CONTROL_CREDIT_INDICATION:
case COMMAND_REJECT:
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (channel->local_sig_id != sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE) continue;
channel->state = L2CAP_STATE_CLOSED;
l2cap_ecbm_emit_channel_opened(channel,
ERROR_CODE_CONNECTION_REJECTED_DUE_TO_LIMITED_RESOURCES);
btstack_linked_list_iterator_remove(&it);
l2cap_free_channel_entry(channel);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
break;/* ... */
#endif
default:
break;default
}switch (code) { ... }
if (cmd_len >= 2){
uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (code & 1) {
if (channel->local_sig_id == sig_id) {
l2cap_signaling_handler_channel(channel, command);
return;
}if (channel->local_sig_id == sig_id) { ... }
}if (code & 1) { ... } else {
if (channel->local_cid == dest_cid) {
l2cap_signaling_handler_channel(channel, command);
return;
}if (channel->local_cid == dest_cid) { ... }
}else { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}if (cmd_len >= 2) { ... }
switch (code){
case CONNECTION_RESPONSE:
case CONFIGURE_RESPONSE:
case DISCONNECTION_RESPONSE:
return;case DISCONNECTION_RESPONSE:
case DISCONNECTION_REQUEST:
if (cmd_len == 4){
uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
uint16_t source_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
l2cap_register_signaling_response(handle, DISCONNECTION_RESPONSE, sig_id, source_cid, dest_cid);
return;
}if (cmd_len == 4) { ... }
break;case DISCONNECTION_REQUEST:
case CONFIGURE_REQUEST:
if (cmd_len >= 2){
uint16_t dest_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
l2cap_register_signaling_response(handle, CONFIGURE_REQUEST, sig_id, dest_cid, L2CAP_REJ_INVALID_CID);
return;
}if (cmd_len >= 2) { ... }
break;case CONFIGURE_REQUEST:
default:
break;default
}switch (code) { ... }
l2cap_register_signaling_response(handle, COMMAND_REJECT, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
}l2cap_signaling_handler_dispatch (hci_con_handle_t handle, uint8_t * command) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CHANNELS
static l2cap_service_t * l2cap_get_service_internal(btstack_linked_list_t * services, uint16_t psm){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, services);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
if ( service->psm == psm){
return service;
}if (service->psm == psm) { ... };
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return NULL;
}l2cap_get_service_internal (btstack_linked_list_t * services, uint16_t psm) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
static uint8_t l2cap_ecbm_setup_channels(btstack_linked_list_t * channels,
btstack_packet_handler_t packet_handler, uint8_t num_channels,
hci_connection_t * connection, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){
uint8_t i;
uint8_t status = ERROR_CODE_SUCCESS;
for (i=0;i<num_channels;i++){
l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, L2CAP_CHANNEL_TYPE_CHANNEL_ECBM, connection->address, connection->address_type, psm, mtu, security_level);
if (!channel) {
status = BTSTACK_MEMORY_ALLOC_FAILED;
break;
}if (!channel) { ... }
channel->con_handle = connection->con_handle;
btstack_linked_list_add_tail(channels, (btstack_linked_item_t *) channel);
}for (i=0;i
if (status != ERROR_CODE_SUCCESS){
while (true) {
l2cap_channel_t * channel = (l2cap_channel_t *) btstack_linked_list_pop(channels);
if (channel == NULL) break;
l2cap_free_channel_entry(channel);
}while (true) { ... }
}if (status != ERROR_CODE_SUCCESS) { ... }
return status;
}l2cap_ecbm_setup_channels (btstack_linked_list_t * channels, btstack_packet_handler_t packet_handler, uint8_t num_channels, hci_connection_t * connection, uint16_t psm, uint16_t mtu, gap_security_level_t security_level) { ... }
static inline l2cap_service_t * l2cap_ecbm_get_service(uint16_t spsm){
return l2cap_get_service_internal(&l2cap_enhanced_services, spsm);
}l2cap_ecbm_get_service (uint16_t spsm) { ... }
static inline uint8_t l2cap_ecbm_status_for_result(uint16_t result) {
switch (result) {
case L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS:
return ERROR_CODE_SUCCESS;case L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS:
case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_SPSM_NOT_SUPPORTED:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_PSM;case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_SPSM_NOT_SUPPORTED:
case L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_RESOURCES;case L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE:
case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_AUTHENTICATION:
case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_AUTHORIZATION:
case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_ENCYRPTION_KEY_SIZE_TOO_SHORT:
case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_ENCRYPTION:
return L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_SECURITY;case L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_ENCRYPTION:
default:
return L2CAP_CONNECTION_RESPONSE_UNKNOWN_ERROR;default
}switch (result) { ... }
}l2cap_ecbm_status_for_result (uint16_t result) { ... }
static void
l2cap_ecbm_emit_incoming_connection(l2cap_channel_t *channel, uint8_t num_channels) {
uint8_t event[16];
event[0] = L2CAP_EVENT_ECBM_INCOMING_CONNECTION;
event[1] = sizeof(event) - 2;
event[2] = channel->address_type;
reverse_bd_addr(channel->address, &event[3]);
little_endian_store_16(event, 9, channel->con_handle);
little_endian_store_16(event, 11, channel->psm);
event[13] = num_channels;
little_endian_store_16(event, 14, channel->local_cid);
hci_dump_packet(HCI_EVENT_PACKET, 1, event, sizeof(event));
(*channel->packet_handler)(HCI_EVENT_PACKET, 0, event, sizeof(event));
}l2cap_ecbm_emit_incoming_connection (l2cap_channel_t *channel, uint8_t num_channels) { ... }
static uint8_t
l2cap_ecbm_security_status_for_connection_request(hci_con_handle_t handle, gap_security_level_t required_security_level,
bool requires_authorization) {
uint8_t security_status = L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS;
if (required_security_level >= LEVEL_2) {
if (gap_encryption_key_size(handle) < 16) {
security_status = L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_ENCYRPTION_KEY_SIZE_TOO_SHORT;
}if (gap_encryption_key_size(handle) < 16) { ... }
if (gap_encryption_key_size(handle) == 0){
security_status = L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_ENCRYPTION;
}if (gap_encryption_key_size(handle) == 0) { ... }
}if (required_security_level >= LEVEL_2) { ... }
if (required_security_level >= LEVEL_3) {
if (!gap_authenticated(handle)) {
security_status = requires_authorization ?
L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_AUTHORIZATION :
L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INSUFFICIENT_AUTHENTICATION;
}if (!gap_authenticated(handle)) { ... }
}if (required_security_level >= LEVEL_3) { ... }
return security_status;
}l2cap_ecbm_security_status_for_connection_request (hci_con_handle_t handle, gap_security_level_t required_security_level, bool requires_authorization) { ... }
static void l2cap_ecbm_handle_security_level_incoming(l2cap_channel_t * channel){
uint8_t sig_id = channel->remote_sig_id;
hci_con_handle_t con_handle = channel->con_handle;
uint8_t security_status = l2cap_ecbm_security_status_for_connection_request(channel->con_handle,
channel->required_security_level,
false);
bool security_sufficient = security_status == L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS;
uint8_t num_channels = 0;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != con_handle) continue;
if (channel->remote_sig_id != sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE) continue;
num_channels++;
if (security_sufficient){
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
}if (security_sufficient) { ... } else {
btstack_linked_list_iterator_remove(&it);
btstack_memory_l2cap_channel_free(channel);
}else { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (security_sufficient){
l2cap_ecbm_emit_incoming_connection(channel, num_channels);
}if (security_sufficient) { ... } else {
uint16_t signaling_cid = L2CAP_CID_SIGNALING;
uint16_t num_channels_and_signaling_cid = (num_channels << 8) | signaling_cid;
l2cap_register_signaling_response(con_handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, security_status);
}else { ... }
}l2cap_ecbm_handle_security_level_incoming (l2cap_channel_t * channel) { ... }
void l2cap_ecbm_trigger_pending_connection_responses(hci_con_handle_t con_handle){
bool done = false;
while (!done) {
done = true;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (channel->channel_type != L2CAP_CHANNEL_TYPE_CHANNEL_ECBM) continue;
if (channel->con_handle != con_handle) continue;
if (channel->state == L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE) {
l2cap_ecbm_handle_security_level_incoming(channel);
done = false;
}if (channel->state == L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE) { ... };
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}while (!done) { ... }
}l2cap_ecbm_trigger_pending_connection_responses (hci_con_handle_t con_handle) { ... }
static int l2cap_ecbm_signaling_handler_dispatch(hci_con_handle_t handle, uint16_t signaling_cid, uint8_t *command,
uint8_t sig_id) {
hci_connection_t *connection;
btstack_linked_list_iterator_t it;
l2cap_service_t *service;
uint16_t spsm;
uint8_t num_channels;
uint16_t num_channels_and_signaling_cid;
uint8_t i;
uint16_t new_mtu;
uint16_t new_mps;
uint16_t initial_credits;
uint16_t result;
uint8_t status;
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
uint16_t len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
log_info("enhanced dispatch: command 0x%02x, sig id %u, len %u", code, sig_id, len);
switch (code) {
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST:
if (len < 10u) return 0u;
connection = hci_connection_for_handle(handle);
btstack_assert(connection != NULL);
num_channels = (len - 8) / sizeof(uint16_t);
num_channels_and_signaling_cid = (num_channels << 8) | signaling_cid;
spsm = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
service = l2cap_ecbm_get_service(spsm);
if (service) {
uint16_t remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
uint16_t remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
uint16_t credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
if (service->mtu > remote_mtu) {
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_INVALID_PARAMETERS);
return 1;
}if (service->mtu > remote_mtu) { ... }
bool send_pending = false;
uint8_t security_status = l2cap_ecbm_security_status_for_connection_request(handle,
service->required_security_level,
service->requires_authorization);
if (security_status != L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS) {
if (gap_get_bondable_mode() != 0) {
send_pending = true;
}if (gap_get_bondable_mode() != 0) { ... } else {
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, security_status);
return 1;
}else { ... }
}if (security_status != L2CAP_ECBM_CONNECTION_RESULT_ALL_SUCCESS) { ... }
result = 0;
l2cap_channel_t * a_channel = NULL;
for (i = 0; i < num_channels; i++) {
uint16_t source_cid = little_endian_read_16(command, 12 + (i * 2));
if (source_cid < 0x40u) {
result = L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INVALID_SOURCE_CID;
continue;
}if (source_cid < 0x40u) { ... }
btstack_linked_list_iterator_init(&it, &l2cap_channels);
bool source_cid_allocated = false;
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (channel->remote_cid != source_cid) continue;
source_cid_allocated = true;
break;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (source_cid_allocated) {
result = 0x00a;
continue;
}if (source_cid_allocated) { ... }
l2cap_channel_t *channel = l2cap_create_channel_entry(service->packet_handler,
L2CAP_CHANNEL_TYPE_CHANNEL_ECBM,
connection->address,
connection->address_type, spsm,
service->mtu,
service->required_security_level);
if (channel == NULL) {
result = 0x004;
continue;
}if (channel == NULL) { ... }
channel->state = L2CAP_STATE_WAIT_INCOMING_SECURITY_LEVEL_UPDATE;
channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
channel->con_handle = connection->con_handle;
channel->remote_sig_id = sig_id;
channel->remote_cid = source_cid;
channel->remote_mtu = remote_mtu;
channel->remote_mps = remote_mps;
channel->credits_outgoing = credits_outgoing;
channel->cid_index = i;
channel->num_cids = num_channels;
channel->reason = result;
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
a_channel = channel;
}for (i = 0; i < num_channels; i++) { ... }
if (a_channel == NULL) {
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, result);
return 1;
}if (a_channel == NULL) { ... }
if (send_pending){
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, L2CAP_ECBM_CONNECTION_RESULT_ALL_PENDING_AUTHENTICATION);
}if (send_pending) { ... } else {
if (service->requires_authorization){
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, L2CAP_ECBM_CONNECTION_RESULT_ALL_PENDING_AUTHORIZATION);
}if (service->requires_authorization) { ... }
l2cap_ecbm_handle_security_level_incoming(a_channel);
}else { ... }
}if (service) { ... } else {
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_CONNECTION_REQUEST, sig_id,
num_channels_and_signaling_cid, L2CAP_ECBM_CONNECTION_RESULT_ALL_REFUSED_SPSM_NOT_SUPPORTED);
}else { ... }
return 1;
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST:
case L2CAP_CREDIT_BASED_CONNECTION_RESPONSE:
if (len < 10u) return 0u;
connection = hci_connection_for_handle(handle);
if (connection == NULL) return 0;
new_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
new_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
initial_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
status = l2cap_ecbm_status_for_result(result);
num_channels = (len - 8) / sizeof(uint16_t);
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
uint8_t channel_status = status;
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (channel->local_sig_id != sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE) continue;
if (channel->cid_index < num_channels) {
uint16_t remote_cid = little_endian_read_16(command, 12 + channel->cid_index * sizeof(uint16_t));
if (remote_cid != 0) {
l2cap_channel_t * original_channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
if (original_channel == NULL){
channel->state = L2CAP_STATE_OPEN;
channel->remote_cid = remote_cid;
channel->remote_mtu = new_mtu;
channel->remote_mps = new_mps;
channel->credits_outgoing = initial_credits;
l2cap_ecbm_emit_channel_opened(channel, ERROR_CODE_SUCCESS);
continue;
}if (original_channel == NULL) { ... }
original_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
channel_status = ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
}if (remote_cid != 0) { ... }
}if (channel->cid_index < num_channels) { ... }
l2cap_ecbm_emit_channel_opened(channel, channel_status);
btstack_linked_list_iterator_remove(&it);
btstack_memory_l2cap_channel_free(channel);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
return 1;
case L2CAP_CREDIT_BASED_CONNECTION_RESPONSE:
case L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST:
if (len < 6) return 0u;
connection = hci_connection_for_handle(handle);
btstack_assert(connection != NULL);
num_channels = (len - 4) / sizeof(uint16_t);
new_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
new_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
result = 0;
for (i = 0; i < num_channels; i++) {
uint16_t remote_cid = little_endian_read_16(command, 8 + (i * sizeof(uint16_t)));
l2cap_channel_t *channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
if (channel == NULL) {
result = L2CAP_ECBM_RECONFIGURE_FAILED_DESTINATION_CID_INVALID;
break;
}if (channel == NULL) { ... }
if (channel->remote_mtu > new_mtu) {
result = L2CAP_ECBM_RECONFIGURE_FAILED_MTU_REDUCTION_NOT_ALLOWED;
break;
}if (channel->remote_mtu > new_mtu) { ... }
if ((num_channels > 1) && (channel->remote_mps > new_mps)) {
result = L2CAP_ECBM_RECONFIGURE_FAILED_MPS_REDUCTION_MULTIPLE_CHANNELS;
break;
}if ((num_channels > 1) && (channel->remote_mps > new_mps)) { ... }
if (new_mps < l2cap_enhanced_mps_min) {
result = L2CAP_ECBM_RECONFIGURE_FAILED_UNACCEPTABLE_PARAMETERS;
break;
}if (new_mps < l2cap_enhanced_mps_min) { ... }
}for (i = 0; i < num_channels; i++) { ... }
if (result != 0) {
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST, sig_id, signaling_cid,
result);
return 1;
}if (result != 0) { ... }
for (i = 0; i < num_channels; i++) {
uint16_t remote_cid = little_endian_read_16(command, 8 + (i * sizeof(uint16_t)));
l2cap_channel_t *channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
channel->remote_mps = new_mps;
channel->remote_mtu = new_mtu;
uint8_t event[8];
event[0] = L2CAP_EVENT_ECBM_RECONFIGURED;
event[1] = sizeof(event) - 2;
little_endian_store_16(event, 2, channel->local_cid);
little_endian_store_16(event, 4, new_mtu);
little_endian_store_16(event, 6, new_mps);
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}for (i = 0; i < num_channels; i++) { ... }
l2cap_register_signaling_response(handle, L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST, sig_id, signaling_cid, 0);
return 1;
case L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST:
case L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE:
if (len < 2u) return 0u;
result = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (channel->local_sig_id != sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_ENHANCED_RENEGOTIATION_RESPONSE) continue;
if (result == 0) {
channel->receive_sdu_buffer = channel->renegotiate_sdu_buffer;
channel->local_mtu = channel->renegotiate_mtu;
}if (result == 0) { ... }
channel->state = L2CAP_STATE_OPEN;
l2cap_ecbm_emit_reconfigure_complete(channel, result);
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
break;
case L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE:
default:
btstack_unreachable();
break;default
}switch (code) { ... }
return 0;
}l2cap_ecbm_signaling_handler_dispatch (hci_con_handle_t handle, uint16_t signaling_cid, uint8_t *command, uint8_t sig_id) { ... }
/* ... */
#endif
#ifdef ENABLE_BLE
static void l2cap_emit_connection_parameter_update_response(hci_con_handle_t con_handle, uint16_t result){
uint8_t event[6];
event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_RESPONSE;
event[1] = 4;
little_endian_store_16(event, 2, con_handle);
little_endian_store_16(event, 4, result);
l2cap_emit_event(event, sizeof(event));
}{ ... }
static int l2cap_le_signaling_handler_dispatch(hci_con_handle_t handle, uint8_t * command, uint8_t sig_id){
hci_connection_t * connection;
uint16_t result;
uint8_t event[12];
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
l2cap_channel_t * channel;
btstack_linked_list_iterator_t it;/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
uint16_t local_cid;
uint16_t le_psm;
l2cap_service_t * service;
uint16_t source_cid;/* ... */
#endif
uint8_t code = command[L2CAP_SIGNALING_COMMAND_CODE_OFFSET];
uint16_t len = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
log_info("le dispatch: command 0x%02x, sig id %u, len %u", code, sig_id, len);
switch (code){
case CONNECTION_PARAMETER_UPDATE_REQUEST:
if (len < 8u) return 0u;
connection = hci_connection_for_handle(handle);
if (connection != NULL){
if (connection->role != HCI_ROLE_MASTER){
return 0;
}if (connection->role != HCI_ROLE_MASTER) { ... }
le_connection_parameter_range_t existing_range;
gap_get_connection_parameter_range(&existing_range);
uint16_t le_conn_interval_min = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET);
uint16_t le_conn_interval_max = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+2);
uint16_t le_conn_latency = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+4);
uint16_t le_supervision_timeout = little_endian_read_16(command,L2CAP_SIGNALING_COMMAND_DATA_OFFSET+6);
int update_parameter = gap_connection_parameter_range_included(&existing_range, le_conn_interval_min, le_conn_interval_max, le_conn_latency, le_supervision_timeout);
if (update_parameter){
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_SEND_RESPONSE;
connection->le_conn_interval_min = le_conn_interval_min;
connection->le_conn_interval_max = le_conn_interval_max;
connection->le_conn_latency = le_conn_latency;
connection->le_supervision_timeout = le_supervision_timeout;
}if (update_parameter) { ... } else {
connection->le_con_parameter_update_state = CON_PARAMETER_UPDATE_DENY;
}else { ... }
connection->le_con_param_update_identifier = sig_id;
}if (connection != NULL) { ... }
event[0] = L2CAP_EVENT_CONNECTION_PARAMETER_UPDATE_REQUEST;
event[1] = 10;
little_endian_store_16(event, 2, handle);
(void)memcpy(&event[4], &command[4], 8);
l2cap_emit_event(event, sizeof(event));
break;
case CONNECTION_PARAMETER_UPDATE_REQUEST:
case CONNECTION_PARAMETER_UPDATE_RESPONSE:
if (len < 2u) return 0u;
result = little_endian_read_16(command, 4);
l2cap_emit_connection_parameter_update_response(handle, result);
break;
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODEcase CONNECTION_PARAMETER_UPDATE_RESPONSE:
case L2CAP_CREDIT_BASED_CONNECTION_REQUEST:
case L2CAP_CREDIT_BASED_CONNECTION_RESPONSE:
case L2CAP_CREDIT_BASED_RECONFIGURE_REQUEST:
case L2CAP_CREDIT_BASED_RECONFIGURE_RESPONSE:
return l2cap_ecbm_signaling_handler_dispatch(handle, L2CAP_CID_SIGNALING_LE, command, sig_id);/* ... */
#endif
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
case COMMAND_REJECT:
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != handle) continue;
if (channel->local_sig_id != sig_id) continue;
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE){
channel->state = L2CAP_STATE_CLOSED;
l2cap_cbm_emit_channel_opened(channel, L2CAP_CBM_CONNECTION_RESULT_SPSM_NOT_SUPPORTED);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
continue;
}if (channel->state == L2CAP_STATE_WAIT_LE_CONNECTION_RESPONSE) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
if (channel->state == L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE){
channel->state = L2CAP_STATE_CLOSED;
l2cap_ecbm_emit_channel_opened(channel, L2CAP_CONNECTION_RESPONSE_RESULT_REFUSED_PSM);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
continue;
}if (channel->state == L2CAP_STATE_WAIT_ENHANCED_CONNECTION_RESPONSE) { ... }
/* ... */#endif
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
break;/* ... */
#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODEcase COMMAND_REJECT:
case LE_CREDIT_BASED_CONNECTION_REQUEST:
if (len < 10u) return 0u;
connection = hci_connection_for_handle(handle);
if (!connection) return 0;
le_psm = little_endian_read_16(command, 4);
service = l2cap_cbm_get_service(le_psm);
source_cid = little_endian_read_16(command, 6);
if (service){
if (source_cid < 0x40u){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_INVALID_SOURCE_CID);
return 1;
}if (source_cid < 0x40u) { ... }
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(a_channel->channel_type)) continue;
if (a_channel->con_handle != handle) continue;
if (a_channel->remote_cid != source_cid) continue;
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_SOURCE_CID_ALREADY_ALLOCATED);
return 1;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (service->required_security_level >= LEVEL_2){
if (gap_encryption_key_size(handle) == 0){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_ENCRYPTION);
return 1;
}if (gap_encryption_key_size(handle) == 0) { ... }
if (gap_encryption_key_size(handle) < 16){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_ENCYRPTION_KEY_SIZE_TOO_SHORT);
return 1;
}if (gap_encryption_key_size(handle) < 16) { ... }
}if (service->required_security_level >= LEVEL_2) { ... }
if (service->required_security_level >= LEVEL_3){
if (!gap_authenticated(handle)){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_AUTHENTICATION);
return 1;
}if (!gap_authenticated(handle)) { ... }
}if (service->required_security_level >= LEVEL_3) { ... }
if (service->required_security_level >= LEVEL_4){
if (gap_authorization_state(handle) != AUTHORIZATION_GRANTED){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_INSUFFICIENT_AUTHORIZATION);
return 1;
}if (gap_authorization_state(handle) != AUTHORIZATION_GRANTED) { ... }
}if (service->required_security_level >= LEVEL_4) { ... }
channel = l2cap_create_channel_entry(service->packet_handler, L2CAP_CHANNEL_TYPE_CHANNEL_CBM, connection->address,
connection->address_type, le_psm, service->mtu, service->required_security_level);
if (!channel){
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE);
return 1;
}if (!channel) { ... }
channel->con_handle = handle;
channel->remote_cid = source_cid;
channel->remote_sig_id = sig_id;
channel->remote_mtu = little_endian_read_16(command, 8);
channel->remote_mps = little_endian_read_16(command, 10);
channel->credits_outgoing = little_endian_read_16(command, 12);
channel->state = L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT;
channel->state_var |= L2CAP_CHANNEL_STATE_VAR_INCOMING;
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_cbm_emit_incoming_connection(channel);
}if (service) { ... } else {
l2cap_register_signaling_response(handle, LE_CREDIT_BASED_CONNECTION_REQUEST, sig_id, source_cid, L2CAP_CBM_CONNECTION_RESULT_SPSM_NOT_SUPPORTED);
}else { ... }
break;
case LE_CREDIT_BASED_CONNECTION_REQUEST:
case LE_CREDIT_BASED_CONNECTION_RESPONSE:
if (len < 10u) return 0u;
channel = NULL;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * a_channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(a_channel->channel_type)) continue;
if (a_channel->con_handle != handle) continue;
if (a_channel->local_sig_id != sig_id) continue;
channel = a_channel;
break;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
if (!channel) break;
result = little_endian_read_16 (command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET+8);
if (result){
channel->state = L2CAP_STATE_CLOSED;
uint8_t status = l2cap_cbm_status_for_result(result);
l2cap_cbm_emit_channel_opened(channel, status);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
break;
}if (result) { ... }
channel->remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
channel->remote_mtu = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
channel->remote_mps = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 4);
channel->credits_outgoing = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 6);
channel->state = L2CAP_STATE_OPEN;
l2cap_cbm_emit_channel_opened(channel, ERROR_CODE_SUCCESS);
break;/* ... */
#endif
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
case L2CAP_FLOW_CONTROL_CREDIT_INDICATION:
return l2cap_credit_based_handle_credit_indication(handle, command, len) ? 1 : 0;
case L2CAP_FLOW_CONTROL_CREDIT_INDICATION:
case DISCONNECTION_REQUEST:
if (len < 4u) return 0u;
local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
channel = l2cap_get_channel_for_local_cid_and_handle(local_cid, handle);
if (!channel) {
l2cap_register_signaling_response(handle, DISCONNECTION_REQUEST, sig_id,
little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2), local_cid);
break;
}if (!channel) { ... }
channel->remote_sig_id = sig_id;
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_RESPONSE;
break;
case DISCONNECTION_REQUEST:
case DISCONNECTION_RESPONSE:
if (len < 4u) return 0u;
local_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
channel = l2cap_get_channel_for_local_cid_and_handle(local_cid, handle);
if (!channel) break;
if (channel->local_sig_id == sig_id) {
if (channel->state == L2CAP_STATE_WAIT_DISCONNECT){
l2cap_finalize_channel_close(channel);
}if (channel->state == L2CAP_STATE_WAIT_DISCONNECT) { ... }
}if (channel->local_sig_id == sig_id) { ... }
break;/* ... */
#endif
default:
return 0;default
}switch (code) { ... }
return 1;
}{ ... }
#endif/* ... */
#ifdef ENABLE_CLASSIC
static void l2cap_acl_classic_handler_for_channel(l2cap_channel_t * l2cap_channel, uint8_t * packet, uint16_t size){
if (l2cap_channel->state != L2CAP_STATE_OPEN) return;
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
if (l2cap_channel->channel_type == L2CAP_CHANNEL_TYPE_CHANNEL_ECBM){
l2cap_credit_based_handle_pdu(l2cap_channel, packet, size);
return;
}if (l2cap_channel->channel_type == L2CAP_CHANNEL_TYPE_CHANNEL_ECBM) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
if (l2cap_channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION){
int fcs_size = l2cap_channel->fcs_option ? 2 : 0;
if (size < COMPLETE_L2CAP_HEADER+2+fcs_size) return;
if (l2cap_channel->fcs_option){
uint16_t fcs_calculated = crc16_calc(&packet[4], size - (4+2));
uint16_t fcs_packet = little_endian_read_16(packet, size-2);
#ifdef L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL
static int counter = 0;
if (++counter == L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL) {
log_info("Simulate fcs error");
fcs_calculated++;
counter = 0;
}if (++counter == L2CAP_ERTM_SIMULATE_FCS_ERROR_INTERVAL) { ... }
/* ... */#endif
if (fcs_calculated == fcs_packet){
log_info("Packet FCS 0x%04x verified", fcs_packet);
}if (fcs_calculated == fcs_packet) { ... } else {
log_error("FCS mismatch! Packet 0x%04x, calculated 0x%04x", fcs_packet, fcs_calculated);
return;
}else { ... }
}if (l2cap_channel->fcs_option) { ... }
uint16_t control = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
uint8_t req_seq = (control >> 8) & 0x3f;
int final = (control >> 7) & 0x01;
if (control & 1){
int poll = (control >> 4) & 0x01;
l2cap_supervisory_function_t s = (l2cap_supervisory_function_t) ((control >> 2) & 0x03);
log_info("Control: 0x%04x => Supervisory function %u, ReqSeq %02u", control, (int) s, req_seq);
l2cap_ertm_tx_packet_state_t * tx_state;
switch (s){
case L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY:
log_info("L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY");
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
if (poll && final){
log_error("P=F=1 in S-Frame");
break;
}if (poll && final) { ... }
if (poll){
int i;
int num_stored_out_of_order_packets = 0;
for (i=0;i<l2cap_channel->num_rx_buffers;i++){
int index = l2cap_channel->rx_store_index + i;
if (index >= l2cap_channel->num_rx_buffers){
index -= l2cap_channel->num_rx_buffers;
}if (index >= l2cap_channel->num_rx_buffers) { ... }
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
if (!rx_state->valid) continue;
num_stored_out_of_order_packets++;
}for (i=0;inum_rx_buffers;i++) { ... }
if (num_stored_out_of_order_packets){
l2cap_channel->send_supervisor_frame_selective_reject = 1;
}if (num_stored_out_of_order_packets) { ... } else {
l2cap_channel->send_supervisor_frame_receiver_ready = 1;
}else { ... }
l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = 1;
}if (poll) { ... }
if (final){
l2cap_ertm_stop_monitor_timer(l2cap_channel);
if (l2cap_channel->unacked_frames){
l2cap_ertm_start_retransmission_timer(l2cap_channel);
}if (l2cap_channel->unacked_frames) { ... }
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
}if (final) { ... }
break;case L2CAP_SUPERVISORY_FUNCTION_RR_RECEIVER_READY:
case L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT:
log_info("L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT");
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
break;case L2CAP_SUPERVISORY_FUNCTION_REJ_REJECT:
case L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY:
log_error("L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY");
break;case L2CAP_SUPERVISORY_FUNCTION_RNR_RECEIVER_NOT_READY:
case L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT:
log_info("L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT");
if (poll){
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
}if (poll) { ... }
tx_state = l2cap_ertm_get_tx_state(l2cap_channel, req_seq);
if (tx_state){
log_info("Retransmission for tx_seq %u requested", req_seq);
l2cap_channel->set_final_bit_after_packet_with_poll_bit_set = poll;
tx_state->retransmission_requested = 1;
l2cap_channel->srej_active = 1;
}if (tx_state) { ... }
break;case L2CAP_SUPERVISORY_FUNCTION_SREJ_SELECTIVE_REJECT:
default:
break;default
}switch (s) { ... }
}if (control & 1) { ... } else {
l2cap_segmentation_and_reassembly_t sar = (l2cap_segmentation_and_reassembly_t) (control >> 14);
uint8_t tx_seq = (control >> 1) & 0x3f;
log_info("Control: 0x%04x => SAR %u, ReqSeq %02u, R?, TxSeq %02u", control, (int) sar, req_seq, tx_seq);
log_info("SAR: pos %u", l2cap_channel->reassembly_pos);
log_info("State: expected_tx_seq %02u, req_seq %02u", l2cap_channel->expected_tx_seq, l2cap_channel->req_seq);
l2cap_ertm_process_req_seq(l2cap_channel, req_seq);
if (final){
l2cap_ertm_retransmit_unacknowleded_frames(l2cap_channel);
}if (final) { ... }
const uint8_t * payload_data = &packet[COMPLETE_L2CAP_HEADER+2];
uint16_t payload_len = size-(COMPLETE_L2CAP_HEADER+2+fcs_size);
uint16_t max_payload_size = 0;
switch (sar){
case L2CAP_SEGMENTATION_AND_REASSEMBLY_UNSEGMENTED_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
max_payload_size = l2cap_channel->local_mps + 2;
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_START_OF_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_CONTINUATION_OF_L2CAP_SDU:
case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
max_payload_size = l2cap_channel->local_mps;
break;case L2CAP_SEGMENTATION_AND_REASSEMBLY_END_OF_L2CAP_SDU:
default:
btstack_assert(false);
break;default
}switch (sar) { ... }
if (payload_len > max_payload_size){
log_info("payload len %u > max payload %u -> drop packet", payload_len, max_payload_size);
return;
}if (payload_len > max_payload_size) { ... }
if (l2cap_channel->expected_tx_seq == tx_seq){
log_info("Received expected frame with TxSeq == ExpectedTxSeq == %02u", tx_seq);
l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq);
l2cap_channel->req_seq = l2cap_channel->expected_tx_seq;
l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, sar, payload_data, payload_len);
while (true){
int index = l2cap_channel->rx_store_index;
l2cap_ertm_rx_packet_state_t * rx_state = &l2cap_channel->rx_packets_state[index];
if (!rx_state->valid) break;
log_info("Processing stored frame with TxSeq == ExpectedTxSeq == %02u", l2cap_channel->expected_tx_seq);
l2cap_channel->expected_tx_seq = l2cap_next_ertm_seq_nr(l2cap_channel->expected_tx_seq);
l2cap_channel->req_seq = l2cap_channel->expected_tx_seq;
rx_state->valid = 0;
l2cap_ertm_handle_in_sequence_sdu(l2cap_channel, rx_state->sar, &l2cap_channel->rx_packets_data[index], rx_state->len);
index++;
if (index >= l2cap_channel->num_rx_buffers){
index = 0;
}if (index >= l2cap_channel->num_rx_buffers) { ... }
l2cap_channel->rx_store_index = index;
}while (true) { ... }
l2cap_channel->send_supervisor_frame_receiver_ready = 1;
}if (l2cap_channel->expected_tx_seq == tx_seq) { ... } else {
int delta = (tx_seq - l2cap_channel->expected_tx_seq) & 0x3f;
if (delta < 2){
l2cap_ertm_handle_out_of_sequence_sdu(l2cap_channel, sar, delta, payload_data, payload_len);
log_info("Received unexpected frame TxSeq %u but expected %u -> send S-SREJ", tx_seq, l2cap_channel->expected_tx_seq);
l2cap_channel->send_supervisor_frame_selective_reject = 1;
}if (delta < 2) { ... } else {
log_info("Received unexpected frame TxSeq %u but expected %u -> send S-REJ", tx_seq, l2cap_channel->expected_tx_seq);
l2cap_channel->send_supervisor_frame_reject = 1;
}else { ... }
}else { ... }
}else { ... }
return;
}if (l2cap_channel->mode == L2CAP_CHANNEL_MODE_ENHANCED_RETRANSMISSION) { ... }
/* ... */#endif
uint16_t payload_size = size - COMPLETE_L2CAP_HEADER;
if (l2cap_channel->local_mtu < payload_size) return;
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, &packet[COMPLETE_L2CAP_HEADER], payload_size);
}l2cap_acl_classic_handler_for_channel (l2cap_channel_t * l2cap_channel, uint8_t * packet, uint16_t size) { ... }
/* ... */#endif
static void l2cap_acl_classic_handler(hci_con_handle_t handle, uint8_t *packet, uint16_t size){
#ifdef ENABLE_CLASSIC
l2cap_channel_t * l2cap_channel;
l2cap_fixed_channel_t * l2cap_fixed_channel;
uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
uint8_t broadcast_flag = READ_ACL_FLAGS(packet) >> 2;
switch (channel_id) {
case L2CAP_CID_SIGNALING: {
if (broadcast_flag != 0) break;
uint32_t command_offset = 8;
while ((command_offset + L2CAP_SIGNALING_COMMAND_DATA_OFFSET) <= size) {
uint16_t data_len = little_endian_read_16(packet, command_offset + L2CAP_SIGNALING_COMMAND_LENGTH_OFFSET);
uint32_t next_command_offset = command_offset + L2CAP_SIGNALING_COMMAND_DATA_OFFSET + data_len;
if (next_command_offset > size){
log_error("signaling command incomplete -> drop");
break;
}if (next_command_offset > size) { ... }
l2cap_signaling_handler_dispatch(handle, &packet[command_offset]);
command_offset = next_command_offset;
}while ((command_offset + L2CAP_SIGNALING_COMMAND_DATA_OFFSET) <= size) { ... }
if (command_offset < size) {
log_error("signaling command incomplete -> reject");
l2cap_register_signaling_response(handle, COMMAND_REJECT, 0, 0, L2CAP_REJ_CMD_UNKNOWN);
}if (command_offset < size) { ... }
break;
...}
case L2CAP_CID_SIGNALING:
case L2CAP_CID_CONNECTIONLESS_CHANNEL:
if (broadcast_flag == 0) break;
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_CONNECTIONLESS_CHANNEL);
if (!l2cap_fixed_channel) break;
if (!l2cap_fixed_channel->packet_handler) break;
(*l2cap_fixed_channel->packet_handler)(UCD_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
break;
#ifdef ENABLE_BLEcase L2CAP_CID_CONNECTIONLESS_CHANNEL:
case L2CAP_CID_BR_EDR_SECURITY_MANAGER:
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_BR_EDR_SECURITY_MANAGER);
if ((l2cap_fixed_channel == NULL) || (l2cap_fixed_channel->packet_handler == NULL)){
l2cap_register_signaling_response(handle, (uint8_t) SM_PAIRING_FAILED, 0, L2CAP_CID_BR_EDR_SECURITY_MANAGER, SM_REASON_PAIRING_NOT_SUPPORTED);
}if ((l2cap_fixed_channel == NULL) || (l2cap_fixed_channel->packet_handler == NULL)) { ... } else {
(*l2cap_fixed_channel->packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
}else { ... }
break;/* ... */
#endif
case L2CAP_CID_BR_EDR_SECURITY_MANAGER:
default:
if (broadcast_flag != 0) break;
l2cap_channel = l2cap_get_channel_for_local_cid_and_handle(channel_id, handle);
if (l2cap_channel != NULL){
l2cap_acl_classic_handler_for_channel(l2cap_channel, packet, size);
}if (l2cap_channel != NULL) { ... }
break;default
}switch (channel_id) { ... }
/* ... */#else
UNUSED(handle);
UNUSED(packet);
UNUSED(size); /* ... */
#endif
}{ ... }
static void l2cap_acl_le_handler(hci_con_handle_t handle, uint8_t *packet, uint16_t size){
#ifdef ENABLE_BLE
l2cap_fixed_channel_t * l2cap_fixed_channel;
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_channel_t * l2cap_channel;
#endif
uint16_t channel_id = READ_L2CAP_CHANNEL_ID(packet);
switch (channel_id) {
case L2CAP_CID_SIGNALING_LE: {
uint8_t sig_id = packet[COMPLETE_L2CAP_HEADER + 1];
uint16_t len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER + 2);
if ((COMPLETE_L2CAP_HEADER + 4u + len) > size) break;
int valid = l2cap_le_signaling_handler_dispatch(handle, &packet[COMPLETE_L2CAP_HEADER], sig_id);
if (!valid){
l2cap_register_signaling_response(handle, COMMAND_REJECT_LE, sig_id, 0, L2CAP_REJ_CMD_UNKNOWN);
}if (!valid) { ... }
break;
...}
case L2CAP_CID_SIGNALING_LE:
case L2CAP_CID_ATTRIBUTE_PROTOCOL:
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_ATTRIBUTE_PROTOCOL);
if (!l2cap_fixed_channel) break;
if (!l2cap_fixed_channel->packet_handler) break;
(*l2cap_fixed_channel->packet_handler)(ATT_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
break;
case L2CAP_CID_ATTRIBUTE_PROTOCOL:
case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
l2cap_fixed_channel = l2cap_fixed_channel_for_channel_id(L2CAP_CID_SECURITY_MANAGER_PROTOCOL);
if ((l2cap_fixed_channel == NULL) || (l2cap_fixed_channel->packet_handler == NULL)){
l2cap_register_signaling_response(handle, (uint8_t) SM_PAIRING_FAILED, 0, L2CAP_CID_SECURITY_MANAGER_PROTOCOL, SM_REASON_PAIRING_NOT_SUPPORTED);
}if ((l2cap_fixed_channel == NULL) || (l2cap_fixed_channel->packet_handler == NULL)) { ... } else {
(*l2cap_fixed_channel->packet_handler)(SM_DATA_PACKET, handle, &packet[COMPLETE_L2CAP_HEADER], size-COMPLETE_L2CAP_HEADER);
}else { ... }
break;
case L2CAP_CID_SECURITY_MANAGER_PROTOCOL:
default:
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
l2cap_channel = l2cap_get_channel_for_local_cid_and_handle(channel_id, handle);
if (l2cap_channel != NULL) {
l2cap_credit_based_handle_pdu(l2cap_channel, packet, size);
}if (l2cap_channel != NULL) { ... }
/* ... */#endif
break;default
}switch (channel_id) { ... }
/* ... */#else
UNUSED(handle);
UNUSED(packet);
UNUSED(size); /* ... */
#endif
}{ ... }
static void l2cap_acl_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
UNUSED(packet_type);
UNUSED(channel);
if (size < COMPLETE_L2CAP_HEADER) return;
hci_con_handle_t handle = READ_ACL_CONNECTION_HANDLE(packet);
hci_connection_t *conn = hci_connection_for_handle(handle);
if (!conn) return;
if (conn->address_type == BD_ADDR_TYPE_ACL){
l2cap_acl_classic_handler(handle, packet, size);
}if (conn->address_type == BD_ADDR_TYPE_ACL) { ... } else {
l2cap_acl_le_handler(handle, packet, size);
}else { ... }
l2cap_run();
}{ ... }
void l2cap_register_fixed_channel(btstack_packet_handler_t packet_handler, uint16_t channel_id) {
l2cap_fixed_channel_t * channel = l2cap_fixed_channel_for_channel_id(channel_id);
if (!channel) return;
channel->packet_handler = packet_handler;
}{ ... }
#ifdef L2CAP_USES_CHANNELS
void l2cap_finalize_channel_close(l2cap_channel_t * channel){
channel->state = L2CAP_STATE_CLOSED;
l2cap_handle_channel_closed(channel);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
}l2cap_finalize_channel_close (l2cap_channel_t * channel) { ... }
/* ... */#endif
#ifdef ENABLE_CLASSIC
static inline l2cap_service_t * l2cap_get_service(uint16_t psm){
return l2cap_get_service_internal(&l2cap_services, psm);
}l2cap_get_service (uint16_t psm) { ... }
static void l2cap_update_minimal_security_level(void){
gap_security_level_t minimal_level = LEVEL_1;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_services);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_service_t * service = (l2cap_service_t *) btstack_linked_list_iterator_next(&it);
if (service->required_security_level > minimal_level){
minimal_level = service->required_security_level;
}if (service->required_security_level > minimal_level) { ... };
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
gap_set_minimal_service_security_level(minimal_level);
}l2cap_update_minimal_security_level (void) { ... }
uint8_t l2cap_register_service(btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level){
log_info("L2CAP_REGISTER_SERVICE psm 0x%x mtu %u", psm, mtu);
l2cap_service_t *service = l2cap_get_service(psm);
if (service) {
log_error("register: PSM %u already registered", psm);
return L2CAP_SERVICE_ALREADY_REGISTERED;
}if (service) { ... }
service = btstack_memory_l2cap_service_get();
if (!service) {
log_error("register: no memory for l2cap_service_t");
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!service) { ... }
service->psm = psm;
service->mtu = mtu;
service->packet_handler = service_packet_handler;
service->required_security_level = security_level;
btstack_linked_list_add(&l2cap_services, (btstack_linked_item_t *) service);
l2cap_update_minimal_security_level();
#ifndef ENABLE_EXPLICIT_CONNECTABLE_MODE_CONTROL
gap_connectable_control(1);/* ... */
#endif
return ERROR_CODE_SUCCESS;
}l2cap_register_service (btstack_packet_handler_t service_packet_handler, uint16_t psm, uint16_t mtu, gap_security_level_t security_level) { ... }
uint8_t l2cap_unregister_service(uint16_t psm){
log_info("unregister psm 0x%x", psm);
l2cap_service_t *service = l2cap_get_service(psm);
if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
btstack_linked_list_remove(&l2cap_services, (btstack_linked_item_t *) service);
btstack_memory_l2cap_service_free(service);
l2cap_update_minimal_security_level();
#ifndef ENABLE_EXPLICIT_CONNECTABLE_MODE_CONTROL
if (btstack_linked_list_empty(&l2cap_services)) {
gap_connectable_control(0);
}if (btstack_linked_list_empty(&l2cap_services)) { ... }
/* ... */#endif
return ERROR_CODE_SUCCESS;
}l2cap_unregister_service (uint16_t psm) { ... }
/* ... */#endif
#ifdef L2CAP_USES_CREDIT_BASED_CHANNELS
static void l2cap_credit_based_send_pdu(l2cap_channel_t *channel) {
btstack_assert(channel != NULL);
btstack_assert(channel->send_sdu_buffer != NULL);
btstack_assert(channel->credits_outgoing > 0);
hci_reserve_packet_buffer();
uint8_t *acl_buffer = hci_get_outgoing_packet_buffer();
uint8_t *l2cap_payload = acl_buffer + 8;
uint16_t pos = 0;
if (!channel->send_sdu_pos) {
channel->send_sdu_pos += 2u;
little_endian_store_16(l2cap_payload, pos, channel->send_sdu_len);
pos += 2u;
}if (!channel->send_sdu_pos) { ... }
uint16_t payload_size = btstack_min(channel->send_sdu_len + 2u - channel->send_sdu_pos, channel->remote_mps - pos);
log_info("len %u, pos %u => payload %u, credits %u", channel->send_sdu_len, channel->send_sdu_pos, payload_size,
channel->credits_outgoing);
(void) memcpy(&l2cap_payload[pos],
&channel->send_sdu_buffer[channel->send_sdu_pos - 2u],
payload_size);
pos += payload_size;
channel->send_sdu_pos += payload_size;
l2cap_setup_header(acl_buffer, channel->con_handle, 0, channel->remote_cid, pos);
channel->credits_outgoing--;
bool done = channel->send_sdu_pos >= (channel->send_sdu_len + 2u);
if (done) {
channel->send_sdu_buffer = NULL;
}if (done) { ... }
hci_send_acl_packet_buffer(8u + pos);
if (done) {
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_PACKET_SENT);
l2cap_credit_based_notify_channel_can_send(channel);
}if (done) { ... }
}l2cap_credit_based_send_pdu (l2cap_channel_t *channel) { ... }
static uint8_t l2cap_credit_based_send_data(l2cap_channel_t * channel, const uint8_t * data, uint16_t size){
if (size > channel->remote_mtu){
log_error("l2cap send, cid 0x%02x, data length exceeds remote MTU.", channel->local_cid);
return L2CAP_DATA_LEN_EXCEEDS_REMOTE_MTU;
}if (size > channel->remote_mtu) { ... }
if (channel->send_sdu_buffer){
log_info("l2cap send, cid 0x%02x, cannot send", channel->local_cid);
return BTSTACK_ACL_BUFFERS_FULL;
}if (channel->send_sdu_buffer) { ... }
channel->send_sdu_buffer = data;
channel->send_sdu_len = size;
channel->send_sdu_pos = 0;
l2cap_notify_channel_can_send();
return ERROR_CODE_SUCCESS;
}l2cap_credit_based_send_data (l2cap_channel_t * channel, const uint8_t * data, uint16_t size) { ... }
static uint8_t l2cap_credit_based_provide_credits(uint16_t local_cid, uint16_t credits){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
log_error("le credits no channel for cid 0x%02x", local_cid);
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
if (channel->state != L2CAP_STATE_OPEN){
log_error("le credits but channel 0x%02x not open yet", local_cid);
}if (channel->state != L2CAP_STATE_OPEN) { ... }
if (channel->automatic_credits) return ERROR_CODE_SUCCESS;
uint32_t total_credits = channel->credits_incoming;
total_credits += channel->new_credits_incoming;
total_credits += credits;
if (total_credits > 0xffffu){
log_error("le credits overrun: current %u, scheduled %u, additional %u", channel->credits_incoming,
channel->new_credits_incoming, credits);
}if (total_credits > 0xffffu) { ... }
channel->new_credits_incoming += credits;
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_credit_based_provide_credits (uint16_t local_cid, uint16_t credits) { ... }
static void l2cap_credit_based_send_credits(l2cap_channel_t *channel) {
log_info("l2cap: sending %u credits", channel->new_credits_incoming);
channel->local_sig_id = l2cap_next_sig_id();
uint16_t new_credits = channel->new_credits_incoming;
channel->new_credits_incoming = 0;
channel->credits_incoming += new_credits;
uint16_t signaling_cid = channel->address_type == BD_ADDR_TYPE_ACL ? L2CAP_CID_SIGNALING : L2CAP_CID_SIGNALING_LE;
l2cap_send_general_signaling_packet(channel->con_handle, signaling_cid, L2CAP_FLOW_CONTROL_CREDIT_INDICATION, channel->local_sig_id, channel->local_cid, new_credits);
}l2cap_credit_based_send_credits (l2cap_channel_t *channel) { ... }
static bool l2cap_credit_based_handle_credit_indication(hci_con_handle_t handle, const uint8_t * command, uint16_t len){
if (len < 4u) return false;
uint16_t remote_cid = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 0);
l2cap_channel_t * channel = l2cap_get_channel_for_remote_handle_and_cid(handle, remote_cid);
if (!channel) {
log_error("credit: no channel for handle 0x%04x/cid 0x%02x", handle, remote_cid);
return true;
}if (!channel) { ... }
uint16_t new_credits = little_endian_read_16(command, L2CAP_SIGNALING_COMMAND_DATA_OFFSET + 2);
uint16_t credits_before = channel->credits_outgoing;
channel->credits_outgoing += new_credits;
if (credits_before > channel->credits_outgoing) {
log_error("credit: new credits caused overrrun for cid 0x%02x, disconnecting", channel->local_cid);
channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
return true;
}if (credits_before > channel->credits_outgoing) { ... }
log_info("credit: %u credits for 0x%02x, now %u", new_credits, channel->local_cid, channel->credits_outgoing);
l2cap_call_notify_channel_in_run = true;
return true;
}l2cap_credit_based_handle_credit_indication (hci_con_handle_t handle, const uint8_t * command, uint16_t len) { ... }
static void l2cap_credit_based_handle_pdu(l2cap_channel_t * l2cap_channel, const uint8_t * packet, uint16_t size){
if (size == COMPLETE_L2CAP_HEADER) return;
if (l2cap_channel->credits_incoming == 0u){
log_info("(e)CBM: packet received but no incoming credits");
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
return;
}if (l2cap_channel->credits_incoming == 0u) { ... }
l2cap_channel->credits_incoming--;
if ((l2cap_channel->credits_incoming < L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_AUTOMATIC_CREDITS_WATERMARK) && l2cap_channel->automatic_credits){
l2cap_channel->new_credits_incoming = L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_AUTOMATIC_CREDITS_INCREMENT;
}if ((l2cap_channel->credits_incoming < L2CAP_CREDIT_BASED_FLOW_CONTROL_MODE_AUTOMATIC_CREDITS_WATERMARK) && l2cap_channel->automatic_credits) { ... }
uint16_t pos = 0;
if (!l2cap_channel->receive_sdu_len){
if (size < (COMPLETE_L2CAP_HEADER + 2)) return;
uint16_t sdu_len = little_endian_read_16(packet, COMPLETE_L2CAP_HEADER);
if (sdu_len > l2cap_channel->local_mtu) {
log_info("(e)CBM: packet received larger than MTU");
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
return;
}if (sdu_len > l2cap_channel->local_mtu) { ... }
l2cap_channel->receive_sdu_len = sdu_len;
l2cap_channel->receive_sdu_pos = 0;
pos += 2u;
size -= 2u;
}if (!l2cap_channel->receive_sdu_len) { ... }
uint16_t fragment_size = size-COMPLETE_L2CAP_HEADER;
if (fragment_size > l2cap_channel->local_mps) {
log_info("(e)CBM: fragment larger than local MPS");
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
return;
}if (fragment_size > l2cap_channel->local_mps) { ... }
if ((l2cap_channel->receive_sdu_pos + fragment_size) > l2cap_channel->receive_sdu_len){
log_info("(e)CBM: fragments larger than SDU");
l2cap_channel->state = L2CAP_STATE_WILL_SEND_DISCONNECT_REQUEST;
return;
}if ((l2cap_channel->receive_sdu_pos + fragment_size) > l2cap_channel->receive_sdu_len) { ... }
(void)memcpy(&l2cap_channel->receive_sdu_buffer[l2cap_channel->receive_sdu_pos],
&packet[COMPLETE_L2CAP_HEADER + pos],
fragment_size);
l2cap_channel->receive_sdu_pos += fragment_size;
log_debug("le packet pos %u, len %u", l2cap_channel->receive_sdu_pos, l2cap_channel->receive_sdu_len);
if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len){
l2cap_dispatch_to_channel(l2cap_channel, L2CAP_DATA_PACKET, l2cap_channel->receive_sdu_buffer, l2cap_channel->receive_sdu_len);
l2cap_channel->receive_sdu_len = 0;
}if (l2cap_channel->receive_sdu_pos >= l2cap_channel->receive_sdu_len) { ... }
}l2cap_credit_based_handle_pdu (l2cap_channel_t * l2cap_channel, const uint8_t * packet, uint16_t size) { ... }
static void l2cap_credit_based_notify_channel_can_send(l2cap_channel_t *channel){
if (!channel->waiting_for_can_send_now) return;
if (channel->send_sdu_buffer) return;
channel->waiting_for_can_send_now = 0;
log_debug("le can send now, local_cid 0x%x", channel->local_cid);
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CAN_SEND_NOW);
}l2cap_credit_based_notify_channel_can_send (l2cap_channel_t *channel) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
static void l2cap_cbm_emit_incoming_connection(l2cap_channel_t *channel) {
log_info("le incoming addr_type %u, addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x, remote_mtu %u",
channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm, channel->local_cid, channel->remote_cid, channel->remote_mtu);
uint8_t event[19];
event[0] = L2CAP_EVENT_CBM_INCOMING_CONNECTION;
event[1] = sizeof(event) - 2u;
event[2] = channel->address_type;
reverse_bd_addr(channel->address, &event[3]);
little_endian_store_16(event, 9, channel->con_handle);
little_endian_store_16(event, 11, channel->psm);
little_endian_store_16(event, 13, channel->local_cid);
little_endian_store_16(event, 15, channel->remote_cid);
little_endian_store_16(event, 17, channel->remote_mtu);
hci_dump_btstack_event( event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_cbm_emit_incoming_connection (l2cap_channel_t *channel) { ... }
static void l2cap_cbm_emit_channel_opened(l2cap_channel_t *channel, uint8_t status) {
log_info("opened le channel status 0x%x addr_type %u addr %s handle 0x%x psm 0x%x local_cid 0x%x remote_cid 0x%x local_mtu %u, remote_mtu %u",
status, channel->address_type, bd_addr_to_str(channel->address), channel->con_handle, channel->psm,
channel->local_cid, channel->remote_cid, channel->local_mtu, channel->remote_mtu);
uint8_t event[23];
event[0] = L2CAP_EVENT_CBM_CHANNEL_OPENED;
event[1] = sizeof(event) - 2u;
event[2] = status;
event[3] = channel->address_type;
reverse_bd_addr(channel->address, &event[4]);
little_endian_store_16(event, 10, channel->con_handle);
event[12] = (channel->state_var & L2CAP_CHANNEL_STATE_VAR_INCOMING) ? 1 : 0;
little_endian_store_16(event, 13, channel->psm);
little_endian_store_16(event, 15, channel->local_cid);
little_endian_store_16(event, 17, channel->remote_cid);
little_endian_store_16(event, 19, channel->local_mtu);
little_endian_store_16(event, 21, channel->remote_mtu);
hci_dump_btstack_event( event, sizeof(event));
l2cap_dispatch_to_channel(channel, HCI_EVENT_PACKET, event, sizeof(event));
}l2cap_cbm_emit_channel_opened (l2cap_channel_t *channel, uint8_t status) { ... }
void l2cap_cbm_finialize_channel_close(l2cap_channel_t * channel){
channel->state = L2CAP_STATE_CLOSED;
l2cap_emit_simple_event_with_cid(channel, L2CAP_EVENT_CHANNEL_CLOSED);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
}l2cap_cbm_finialize_channel_close (l2cap_channel_t * channel) { ... }
static inline l2cap_service_t * l2cap_cbm_get_service(uint16_t le_psm){
return l2cap_get_service_internal(&l2cap_le_services, le_psm);
}l2cap_cbm_get_service (uint16_t le_psm) { ... }
uint8_t l2cap_cbm_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
log_info("l2cap_cbm_register_service psm 0x%x", psm);
l2cap_service_t *service = l2cap_cbm_get_service(psm);
if (service) {
return L2CAP_SERVICE_ALREADY_REGISTERED;
}if (service) { ... }
service = btstack_memory_l2cap_service_get();
if (!service) {
log_error("register: no memory for l2cap_service_t");
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!service) { ... }
service->psm = psm;
service->mtu = 0;
service->packet_handler = packet_handler;
service->required_security_level = security_level;
service->requires_authorization = false;
btstack_linked_list_add(&l2cap_le_services, (btstack_linked_item_t *) service);
return ERROR_CODE_SUCCESS;
}l2cap_cbm_register_service (btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level) { ... }
uint8_t l2cap_cbm_unregister_service(uint16_t psm) {
log_info("l2cap_cbm_unregister_service psm 0x%x", psm);
l2cap_service_t *service = l2cap_cbm_get_service(psm);
if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
btstack_linked_list_remove(&l2cap_le_services, (btstack_linked_item_t *) service);
btstack_memory_l2cap_service_free(service);
return ERROR_CODE_SUCCESS;
}l2cap_cbm_unregister_service (uint16_t psm) { ... }
uint8_t l2cap_cbm_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT) { ... }
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_ACCEPT;
channel->receive_sdu_buffer = receive_sdu_buffer;
channel->local_mtu = mtu;
channel->new_credits_incoming = initial_credits;
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_cbm_accept_connection (uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits) { ... }
uint8_t l2cap_cbm_decline_connection(uint16_t local_cid, uint16_t result) {
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT) { ... }
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_RESPONSE_DECLINE;
channel->reason = result;
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_cbm_decline_connection (uint16_t local_cid, uint16_t result) { ... }
static gap_security_level_t l2cap_cbm_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;
}l2cap_cbm_security_level_for_connection (hci_con_handle_t con_handle) { ... }
static void l2cap_cbm_sm_packet_handler(uint8_t packet_type, uint16_t channel_nr, uint8_t *packet, uint16_t size) {
UNUSED(channel_nr);
UNUSED(size);
UNUSED(packet_type);
btstack_assert(packet_type == HCI_EVENT_PACKET);
if (hci_event_packet_get_type(packet) != SM_EVENT_PAIRING_COMPLETE) return;
hci_con_handle_t con_handle = sm_event_pairing_complete_get_handle(packet);
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
l2cap_channel_t *channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != con_handle) continue;
if (channel->state != L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE) continue;
if (l2cap_cbm_security_level_for_connection(con_handle) < channel->required_security_level){
l2cap_cbm_emit_channel_opened(channel, ERROR_CODE_INSUFFICIENT_SECURITY);
btstack_linked_list_remove(&l2cap_channels, (btstack_linked_item_t *) channel);
l2cap_free_channel_entry(channel);
}if (l2cap_cbm_security_level_for_connection(con_handle) < channel->required_security_level) { ... } else {
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
l2cap_run();
}else { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_cbm_sm_packet_handler (uint8_t packet_type, uint16_t channel_nr, uint8_t *packet, uint16_t size) { ... }
uint8_t l2cap_cbm_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
uint16_t * out_local_cid) {
static btstack_packet_callback_registration_t sm_event_callback_registration;
static bool sm_callback_registered = false;
log_info("create, handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
hci_connection_t * connection = hci_connection_for_handle(con_handle);
if (!connection) {
log_error("no hci_connection for handle 0x%04x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (!connection) { ... }
l2cap_channel_t * channel = l2cap_create_channel_entry(packet_handler, L2CAP_CHANNEL_TYPE_CHANNEL_CBM, connection->address, connection->address_type, psm, mtu, security_level);
if (!channel) {
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!channel) { ... }
log_info("created %p", channel);
if (out_local_cid){
*out_local_cid = channel->local_cid;
}if (out_local_cid) { ... }
channel->con_handle = con_handle;
channel->receive_sdu_buffer = receive_sdu_buffer;
channel->new_credits_incoming = initial_credits;
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
if (l2cap_cbm_security_level_for_connection(con_handle) < channel->required_security_level){
if (!sm_callback_registered){
sm_callback_registered = true;
sm_event_callback_registration.callback = &l2cap_cbm_sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
}if (!sm_callback_registered) { ... }
channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
sm_request_pairing(con_handle);
}if (l2cap_cbm_security_level_for_connection(con_handle) < channel->required_security_level) { ... } else {
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
l2cap_run();
}else { ... }
return ERROR_CODE_SUCCESS;
}l2cap_cbm_create_channel (btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, uint16_t * out_local_cid) { ... }
uint8_t l2cap_cbm_provide_credits(uint16_t local_cid, uint16_t credits){
return l2cap_credit_based_provide_credits(local_cid, credits);
}l2cap_cbm_provide_credits (uint16_t local_cid, uint16_t credits) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_ENHANCED_CREDIT_BASED_FLOW_CONTROL_MODE
uint8_t l2cap_ecbm_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t min_remote_mtu,
gap_security_level_t security_level, bool authorization_required) {
l2cap_service_t *service = l2cap_ecbm_get_service(psm);
if (service) {
return L2CAP_SERVICE_ALREADY_REGISTERED;
}if (service) { ... }
service = btstack_memory_l2cap_service_get();
if (!service) {
log_error("register: no memory for l2cap_service_t");
return BTSTACK_MEMORY_ALLOC_FAILED;
}if (!service) { ... }
service->psm = psm;
service->mtu = min_remote_mtu;
service->packet_handler = packet_handler;
service->required_security_level = security_level;
service->requires_authorization = authorization_required;
btstack_linked_list_add(&l2cap_enhanced_services, (btstack_linked_item_t *) service);
return ERROR_CODE_SUCCESS;
}l2cap_ecbm_register_service (btstack_packet_handler_t packet_handler, uint16_t psm, uint16_t min_remote_mtu, gap_security_level_t security_level, bool authorization_required) { ... }
uint8_t l2cap_ecbm_unregister_service(uint16_t psm) {
l2cap_service_t *service = l2cap_ecbm_get_service(psm);
if (!service) return L2CAP_SERVICE_DOES_NOT_EXIST;
btstack_linked_list_remove(&l2cap_enhanced_services, (btstack_linked_item_t *) service);
btstack_memory_l2cap_service_free(service);
return ERROR_CODE_SUCCESS;
}l2cap_ecbm_unregister_service (uint16_t psm) { ... }
void l2cap_ecbm_mps_set_min(uint16_t mps_min){
l2cap_enhanced_mps_min = mps_min;
}l2cap_ecbm_mps_set_min (uint16_t mps_min) { ... }
void l2cap_ecbm_mps_set_max(uint16_t mps_max){
l2cap_enhanced_mps_max = mps_max;
}l2cap_ecbm_mps_set_max (uint16_t mps_max) { ... }
uint8_t l2cap_ecbm_create_channels(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
gap_security_level_t security_level,
uint16_t psm, uint8_t num_channels, uint16_t initial_credits, uint16_t mtu,
uint8_t ** receive_sdu_buffers, uint16_t * out_local_cid){
log_info("create enhanced, handle 0x%04x psm 0x%x mtu %u", con_handle, psm, mtu);
hci_connection_t * connection = hci_connection_for_handle(con_handle);
if (!connection) {
log_error("no hci_connection for handle 0x%04x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}if (!connection) { ... }
btstack_linked_list_t channels = NULL;
uint8_t status = l2cap_ecbm_setup_channels(&channels, packet_handler, num_channels, connection, psm, mtu,
security_level);
uint16_t local_mps = btstack_min(l2cap_enhanced_mps_max, btstack_min(l2cap_max_le_mtu(), mtu));
l2cap_channel_t * channel;
uint8_t i = 0;
uint8_t local_sig_id = l2cap_next_sig_id();
while (true) {
channel = (l2cap_channel_t *) btstack_linked_list_pop(&channels);
if (channel == NULL) break;
channel->state = L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_REQUEST;
channel->local_sig_id = local_sig_id;
channel->local_mps = local_mps;
channel->cid_index = i;
channel->num_cids = num_channels;
channel->credits_incoming = initial_credits;
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
channel->receive_sdu_buffer = receive_sdu_buffers[i];
if (out_local_cid){
out_local_cid[i] = channel->local_cid;
}if (out_local_cid) { ... }
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
i++;
}while (true) { ... }
#if 0
if (l2cap_le_security_level_for_connection(con_handle) < channel->required_security_level){
if (!sm_callback_registered){
sm_callback_registered = true;
sm_event_callback_registration.callback = &l2cap_sm_packet_handler;
sm_add_event_handler(&sm_event_callback_registration);
}if (!sm_callback_registered) { ... }
channel->state = L2CAP_STATE_WAIT_OUTGOING_SECURITY_LEVEL_UPDATE;
sm_request_pairing(con_handle);
}if (l2cap_le_security_level_for_connection(con_handle) < channel->required_security_level) { ... } else {
channel->state = L2CAP_STATE_WILL_SEND_LE_CONNECTION_REQUEST;
l2cap_run();
}else { ... }
/* ... */#endif
l2cap_run();
return status;
}l2cap_ecbm_create_channels (btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, gap_security_level_t security_level, uint16_t psm, uint8_t num_channels, uint16_t initial_credits, uint16_t mtu, uint8_t ** receive_sdu_buffers, uint16_t * out_local_cid) { ... }
uint8_t l2cap_ecbm_accept_channels(uint16_t local_cid, uint8_t num_channels, uint16_t initial_credits,
uint16_t receive_buffer_size, uint8_t ** receive_buffers, uint16_t * out_local_cids){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
uint16_t local_mps = btstack_min(l2cap_enhanced_mps_max, btstack_min(l2cap_max_le_mtu(), receive_buffer_size));
hci_con_handle_t con_handle = channel->con_handle;
uint8_t local_sig_id = channel->local_sig_id;
uint8_t channel_index = 0;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != con_handle) continue;
if (channel->local_sig_id != local_sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT) continue;
if (channel_index < num_channels){
out_local_cids[channel_index] = channel->local_cid;
channel->receive_sdu_buffer = receive_buffers[channel_index];
channel->local_mtu = receive_buffer_size;
channel->local_mps = local_mps;
channel->credits_incoming = initial_credits;
channel->automatic_credits = initial_credits == L2CAP_LE_AUTOMATIC_CREDITS;
channel_index++;
}if (channel_index < num_channels) { ... } else {
channel->local_cid = 0;
channel->reason = L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE;
}else { ... }
channel->state = L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
l2cap_run_trigger();
return ERROR_CODE_SUCCESS;
}l2cap_ecbm_accept_channels (uint16_t local_cid, uint8_t num_channels, uint16_t initial_credits, uint16_t receive_buffer_size, uint8_t ** receive_buffers, uint16_t * out_local_cids) { ... }
uint8_t l2cap_ecbm_decline_channels(uint16_t local_cid, uint16_t result){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cid);
if (!channel) {
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
hci_con_handle_t con_handle = channel->con_handle;
uint8_t local_sig_id = channel->local_sig_id;
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)) {
channel = (l2cap_channel_t *) btstack_linked_list_iterator_next(&it);
if (!l2cap_is_dynamic_channel_type(channel->channel_type)) continue;
if (channel->con_handle != con_handle) continue;
if (channel->local_sig_id != local_sig_id) continue;
if (channel->state != L2CAP_STATE_WAIT_CLIENT_ACCEPT_OR_REJECT) continue;
channel->local_cid = 0;
channel->reason = result;
channel->state = L2CAP_STATE_WILL_SEND_ENHANCED_CONNECTION_RESPONSE;
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
l2cap_run_trigger();
return ERROR_CODE_SUCCESS;
}l2cap_ecbm_decline_channels (uint16_t local_cid, uint16_t result) { ... }
uint8_t l2cap_ecbm_reconfigure_channels(uint8_t num_cids, uint16_t * local_cids, int16_t receive_buffer_size, uint8_t ** receive_buffers){
btstack_assert(receive_buffers != NULL);
btstack_assert(local_cids != NULL);
if (num_cids > L2CAP_ECBM_MAX_CID_ARRAY_SIZE){
return ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS;
}if (num_cids > L2CAP_ECBM_MAX_CID_ARRAY_SIZE) { ... }
uint8_t i;
hci_con_handle_t con_handle = HCI_CON_HANDLE_INVALID;
for (i = 0 ; i < num_cids ; i++){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cids[i]);
if (!channel) {
return L2CAP_LOCAL_CID_DOES_NOT_EXIST;
}if (!channel) { ... }
if (channel->state != L2CAP_STATE_OPEN){
return ERROR_CODE_COMMAND_DISALLOWED;
}if (channel->state != L2CAP_STATE_OPEN) { ... }
if (con_handle == HCI_CON_HANDLE_INVALID){
con_handle = channel->con_handle;
}if (con_handle == HCI_CON_HANDLE_INVALID) { ... } else if (con_handle != channel->con_handle){
return ERROR_CODE_UNACCEPTABLE_CONNECTION_PARAMETERS;
}else if (con_handle != channel->con_handle) { ... }
}for (i = 0 ; i < num_cids ; i++) { ... }
uint8_t sig_id = l2cap_next_sig_id();
for (i = 0 ; i < num_cids ; i++){
l2cap_channel_t * channel = l2cap_get_channel_for_local_cid(local_cids[i]);
channel->cid_index = i;
channel->num_cids = num_cids;
channel->local_sig_id = sig_id;
channel->renegotiate_mtu = receive_buffer_size;
channel->renegotiate_sdu_buffer = receive_buffers[i];
channel->state = L2CAP_STATE_WILL_SEND_EHNANCED_RENEGOTIATION_REQUEST;
}for (i = 0 ; i < num_cids ; i++) { ... }
l2cap_run();
return ERROR_CODE_SUCCESS;
}l2cap_ecbm_reconfigure_channels (uint8_t num_cids, uint16_t * local_cids, int16_t receive_buffer_size, uint8_t ** receive_buffers) { ... }
uint8_t l2cap_ecbm_provide_credits(uint16_t local_cid, uint16_t credits){
return l2cap_credit_based_provide_credits(local_cid, credits);
}l2cap_ecbm_provide_credits (uint16_t local_cid, uint16_t credits) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
uint8_t l2cap_create_ertm_channel(btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm,
l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid){
log_error("deprecated - please use l2cap_ertm_create_channel");
return l2cap_ertm_create_channel(packet_handler, address, psm, ertm_contig, buffer, size, out_local_cid);
}l2cap_create_ertm_channel (btstack_packet_handler_t packet_handler, bd_addr_t address, uint16_t psm, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size, uint16_t * out_local_cid) { ... };
uint8_t l2cap_accept_ertm_connection(uint16_t local_cid, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size){
log_error("deprecated - please use l2cap_ertm_accept_connection");
return l2cap_ertm_accept_connection(local_cid, ertm_contig, buffer, size);
}l2cap_accept_ertm_connection (uint16_t local_cid, l2cap_ertm_config_t * ertm_contig, uint8_t * buffer, uint32_t size) { ... }
/* ... */#endif
#ifdef ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE
uint8_t l2cap_le_register_service(btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level){
log_error("deprecated - please use l2cap_cbm_register_service");
return l2cap_cbm_register_service(packet_handler, psm, security_level);
}l2cap_le_register_service (btstack_packet_handler_t packet_handler, uint16_t psm, gap_security_level_t security_level) { ... }
uint8_t l2cap_le_unregister_service(uint16_t psm){
log_error("deprecated - please use l2cap_cbm_unregister_service");
return l2cap_cbm_unregister_service(psm);
}l2cap_le_unregister_service (uint16_t psm) { ... }
uint8_t l2cap_le_accept_connection(uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits){
log_error("deprecated - please use l2cap_cbm_accept_connection");
return l2cap_cbm_accept_connection(local_cid, receive_sdu_buffer, mtu, initial_credits);
}l2cap_le_accept_connection (uint16_t local_cid, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits) { ... }
uint8_t l2cap_le_decline_connection(uint16_t local_cid){
log_error("deprecated - please use l2cap_cbm_decline_connection");
return l2cap_cbm_decline_connection(local_cid, L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE);
}l2cap_le_decline_connection (uint16_t local_cid) { ... }
uint8_t l2cap_le_create_channel(btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle,
uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level,
uint16_t * out_local_cid){
log_error("deprecated - please use l2cap_cbm_create_channel");
return l2cap_cbm_create_channel(packet_handler, con_handle, psm, receive_sdu_buffer, mtu, initial_credits, security_level, out_local_cid);
}l2cap_le_create_channel (btstack_packet_handler_t packet_handler, hci_con_handle_t con_handle, uint16_t psm, uint8_t * receive_sdu_buffer, uint16_t mtu, uint16_t initial_credits, gap_security_level_t security_level, uint16_t * out_local_cid) { ... }
uint8_t l2cap_le_provide_credits(uint16_t local_cid, uint16_t credits){
log_error("deprecated - please use l2cap_cbm_provide_credits");
return l2cap_cbm_provide_credits(local_cid, credits);
}l2cap_le_provide_credits (uint16_t local_cid, uint16_t credits) { ... }
bool l2cap_le_can_send_now(uint16_t local_cid){
log_error("deprecated - please use l2cap_can_send_packet_now");
return l2cap_can_send_packet_now(local_cid);
}l2cap_le_can_send_now (uint16_t local_cid) { ... }
uint8_t l2cap_le_request_can_send_now_event(uint16_t local_cid){
log_error("deprecated - please use l2cap_request_can_send_now_event");
return l2cap_request_can_send_now_event(local_cid);
}l2cap_le_request_can_send_now_event (uint16_t local_cid) { ... }
uint8_t l2cap_le_send_data(uint16_t local_cid, const uint8_t * data, uint16_t size){
log_error("deprecated - please use l2cap_cbm_send_data");
return l2cap_send(local_cid, data, size);
}l2cap_le_send_data (uint16_t local_cid, const uint8_t * data, uint16_t size) { ... }
uint8_t l2cap_le_disconnect(uint16_t local_cid){
log_error("deprecated - please use l2cap_disconnect");
return l2cap_disconnect(local_cid);
}l2cap_le_disconnect (uint16_t local_cid) { ... }
/* ... */#endif
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
static void fuzz_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
}fuzz_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) { ... }
void l2cap_setup_test_channels_fuzz(void) {
bd_addr_t address;
l2cap_channel_t * channel;
channel = l2cap_create_channel_entry(fuzz_packet_handler, L2CAP_CHANNEL_TYPE_CLASSIC, address,
BD_ADDR_TYPE_ACL, 0x01, 100, LEVEL_4);
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
channel = l2cap_create_channel_entry(fuzz_packet_handler, L2CAP_CHANNEL_TYPE_CHANNEL_CBM, address,
BD_ADDR_TYPE_LE_PUBLIC, 0x03, 100, LEVEL_4);
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
channel = l2cap_create_channel_entry(fuzz_packet_handler, L2CAP_CHANNEL_TYPE_CHANNEL_ECBM,
address, BD_ADDR_TYPE_LE_PUBLIC, 0x05, 100, LEVEL_4);
btstack_linked_list_add_tail(&l2cap_channels, (btstack_linked_item_t *) channel);
}l2cap_setup_test_channels_fuzz (void) { ... }
void l2cap_free_channels_fuzz(void){
btstack_linked_list_iterator_t it;
btstack_linked_list_iterator_init(&it, &l2cap_channels);
while (btstack_linked_list_iterator_has_next(&it)){
l2cap_channel_t * channel = (l2cap_channel_t*) btstack_linked_list_iterator_next(&it);
bool fixed_channel = false;
switch (channel->channel_type) {
case L2CAP_CHANNEL_TYPE_FIXED_LE:
case L2CAP_CHANNEL_TYPE_FIXED_CLASSIC:
case L2CAP_CHANNEL_TYPE_CONNECTIONLESS:
fixed_channel = true;
break;case L2CAP_CHANNEL_TYPE_CONNECTIONLESS:
default:
break;default
}switch (channel->channel_type) { ... }
if (fixed_channel == false) {
btstack_linked_list_iterator_remove(&it);
btstack_memory_l2cap_channel_free(channel);
}if (fixed_channel == false) { ... }
}while (btstack_linked_list_iterator_has_next(&it)) { ... }
}l2cap_free_channels_fuzz (void) { ... }
/* ... */#endif