1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
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
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
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
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
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
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
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
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
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
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
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
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
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
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
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
2520
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
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
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
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
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
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
|
/******************************************************************************
*
* Copyright (C) 1999-2012 Broadcom Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
/******************************************************************************
*
* This file contains the L2CAP API code
*
******************************************************************************/
#define LOG_TAG "bt_l2cap"
#include <base/logging.h>
#include <base/strings/stringprintf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bt_common.h"
#include "bt_types.h"
#include "btm_api.h"
#include "btu.h"
#include "device/include/controller.h"
#include "hcidefs.h"
#include "hcimsgs.h"
#include "l2c_int.h"
#include "l2cdefs.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"
using base::StringPrintf;
extern fixed_queue_t* btu_general_alarm_queue;
tL2C_AVDT_CHANNEL_INFO av_media_channels[MAX_ACTIVE_AVDT_CONN];
/*******************************************************************************
*
* Function L2CA_Register
*
* Description Other layers call this function to register for L2CAP
* services.
*
* Returns PSM to use or zero if error. Typically, the PSM returned
* is the same as was passed in, but for an outgoing-only
* connection to a dynamic PSM, a "virtual" PSM is returned
* and should be used in the calls to L2CA_ConnectReq(),
* L2CA_ErtmConnectReq() and L2CA_Deregister()
*
******************************************************************************/
uint16_t L2CA_Register(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info,
bool enable_snoop) {
tL2C_RCB* p_rcb;
uint16_t vpsm = psm;
L2CAP_TRACE_WARNING("L2CAP - L2CA_Register() called for PSM: 0x%04x", psm);
/* Verify that the required callback info has been filled in
** Note: Connection callbacks are required but not checked
** for here because it is possible to be only a client
** or only a server.
*/
if ((!p_cb_info->pL2CA_ConfigCfm_Cb) || (!p_cb_info->pL2CA_ConfigInd_Cb) ||
(!p_cb_info->pL2CA_DataInd_Cb) || (!p_cb_info->pL2CA_DisconnectInd_Cb)) {
L2CAP_TRACE_ERROR("L2CAP - no cb registering PSM: 0x%04x", psm);
return (0);
}
/* Verify PSM is valid */
if (L2C_INVALID_PSM(psm)) {
L2CAP_TRACE_ERROR("L2CAP - invalid PSM value, PSM: 0x%04x", psm);
return (0);
}
/* Check if this is a registration for an outgoing-only connection to */
/* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
if ((psm >= 0x1001) && (p_cb_info->pL2CA_ConnectInd_Cb == NULL)) {
for (vpsm = 0x1002; vpsm < 0x8000; vpsm += 2) {
p_rcb = l2cu_find_rcb_by_psm(vpsm);
if (p_rcb == NULL) break;
}
L2CAP_TRACE_API("L2CA_Register - Real PSM: 0x%04x Virtual PSM: 0x%04x",
psm, vpsm);
}
/* If registration block already there, just overwrite it */
p_rcb = l2cu_find_rcb_by_psm(vpsm);
if (p_rcb == NULL) {
p_rcb = l2cu_allocate_rcb(vpsm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no RCB available, PSM: 0x%04x vPSM: 0x%04x",
psm, vpsm);
return (0);
}
}
p_rcb->log_packets = enable_snoop;
p_rcb->api = *p_cb_info;
p_rcb->real_psm = psm;
return (vpsm);
}
/*******************************************************************************
*
* Function L2CA_Deregister
*
* Description Other layers call this function to de-register for L2CAP
* services.
*
* Returns void
*
******************************************************************************/
void L2CA_Deregister(uint16_t psm) {
tL2C_RCB* p_rcb;
tL2C_CCB* p_ccb;
tL2C_LCB* p_lcb;
int ii;
L2CAP_TRACE_WARNING("L2CAP - L2CA_Deregister() called for PSM: 0x%04x", psm);
p_rcb = l2cu_find_rcb_by_psm(psm);
if (p_rcb != NULL) {
p_lcb = &l2cb.lcb_pool[0];
for (ii = 0; ii < MAX_L2CAP_LINKS; ii++, p_lcb++) {
if (p_lcb->in_use) {
p_ccb = p_lcb->ccb_queue.p_first_ccb;
if ((p_ccb == NULL) || (p_lcb->link_state == LST_DISCONNECTING)) {
continue;
}
if ((p_ccb->in_use) &&
((p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP) ||
(p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))) {
continue;
}
if (p_ccb->p_rcb == p_rcb) {
l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
}
}
}
l2cu_release_rcb(p_rcb);
} else {
L2CAP_TRACE_WARNING("L2CAP - PSM: 0x%04x not found for deregistration",
psm);
}
}
/*******************************************************************************
*
* Function L2CA_AllocatePSM
*
* Description Other layers call this function to find an unused PSM for
* L2CAP services.
*
* Returns PSM to use.
*
******************************************************************************/
uint16_t L2CA_AllocatePSM(void) {
bool done = false;
uint16_t psm = l2cb.dyn_psm;
L2CAP_TRACE_API("L2CA_AllocatePSM");
while (!done) {
psm += 2;
if (psm > 0xfeff) {
psm = 0x1001;
} else if (psm & 0x0100) {
/* the upper byte must be even */
psm += 0x0100;
}
/* if psm is in range of reserved BRCM Aware features */
if ((BRCM_RESERVED_PSM_START <= psm) && (psm <= BRCM_RESERVED_PSM_END))
continue;
/* make sure the newlly allocated psm is not used right now */
if ((l2cu_find_rcb_by_psm(psm)) == NULL) done = true;
}
l2cb.dyn_psm = psm;
return (psm);
}
/*******************************************************************************
*
* Function L2CA_AllocateCocPSM
*
* Description To find an unused LE PSM for L2CAP services.
*
* Returns LE_PSM to use if success. Otherwise returns 0.
*
******************************************************************************/
uint16_t L2CA_AllocateCocPSM(void) {
bool done = false;
uint16_t psm = l2cb.coc_dyn_psm;
uint16_t count = 0;
L2CAP_TRACE_API("%s: last psm=%d", __func__, psm);
while (!done) {
count++;
if (count > L2CAP_COC_DYNAMIC_PSM_RANGE) {
L2CAP_TRACE_ERROR("%s: Out of free Coc PSM", __func__);
return 0;
}
psm++;
if (psm < L2CAP_COC_DYNAMIC_PSM_START) {
L2CAP_TRACE_ERROR("%s: Invalid BLE PSM", __func__);
return 0;
}
if (psm > L2CAP_COC_DYNAMIC_PSM_END) {
psm = L2CAP_COC_DYNAMIC_PSM_START;
}
if (!l2cb.coc_dyn_psm_assigned[psm - L2CAP_COC_DYNAMIC_PSM_START]) {
/* make sure the newly allocated psm is not used right now */
if (l2cu_find_coc_rcb_by_psm(psm)) { //TODO change it coc pool
L2CAP_TRACE_WARNING("%s: supposedly-free PSM=%d have allocated rcb!",
__func__, psm);
continue;
}
l2cb.coc_dyn_psm_assigned[psm - L2CAP_COC_DYNAMIC_PSM_START] = true;
L2CAP_TRACE_DEBUG("%s: assigned PSM=%d", __func__, psm);
done = true;
break;
}
}
l2cb.coc_dyn_psm = psm;
return (psm);
}
/*******************************************************************************
*
* Function L2CA_FreeCocPSM
*
* Description Free an assigned COC PSM.
*
* Returns void
*
******************************************************************************/
void L2CA_FreeCocPSM(uint16_t psm) {
L2CAP_TRACE_API("%s: to free psm=%d", __func__, psm);
if ((psm < L2CAP_COC_DYNAMIC_PSM_START) || (psm > L2CAP_COC_DYNAMIC_PSM_END)) {
L2CAP_TRACE_ERROR("%s: Invalid PSM=%d value!", __func__, psm);
return;
}
if (!l2cb.coc_dyn_psm_assigned[psm - L2CAP_COC_DYNAMIC_PSM_START]) {
L2CAP_TRACE_WARNING("%s: PSM=%d was not allocated!", __func__, psm);
}
l2cb.coc_dyn_psm_assigned[psm - L2CAP_COC_DYNAMIC_PSM_START] = false;
}
/*******************************************************************************
*
* Function L2CA_ConnectReq
*
* Description Higher layers call this function to create an L2CAP
* connection. Note that the connection is not established at
* this time, but connection establishment gets started. The
* callback function will be invoked when connection
* establishes or fails.
*
* Returns the CID of the connection, or 0 if it failed to start
*
******************************************************************************/
uint16_t L2CA_ConnectReq(uint16_t psm, const RawAddress& p_bd_addr) {
return L2CA_ErtmConnectReq(psm, p_bd_addr, NULL);
}
/*******************************************************************************
*
* Function L2CA_ErtmConnectReq
*
* Description Higher layers call this function to create an L2CAP
* connection. Note that the connection is not established at
* this time, but connection establishment gets started. The
* callback function will be invoked when connection
* establishes or fails.
*
* Parameters: PSM: L2CAP PSM for the connection
* BD address of the peer
* Enhaced retransmission mode configurations
* Returns the CID of the connection, or 0 if it failed to start
*
******************************************************************************/
uint16_t L2CA_ErtmConnectReq(uint16_t psm, const RawAddress& p_bd_addr,
tL2CAP_ERTM_INFO* p_ertm_info) {
tL2C_LCB* p_lcb;
tL2C_CCB* p_ccb;
tL2C_RCB* p_rcb;
VLOG(1) << __func__ << "BDA " << p_bd_addr
<< StringPrintf(" PSM: 0x%04x allowed:0x%x preferred:%d", psm,
(p_ertm_info) ? p_ertm_info->allowed_modes : 0,
(p_ertm_info) ? p_ertm_info->preferred_mode : 0);
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("L2CAP connect req - BTU not ready");
return (0);
}
/* Fail if the PSM is not registered */
p_rcb = l2cu_find_rcb_by_psm(psm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no RCB for L2CA_conn_req, PSM: 0x%04x", psm);
return (0);
}
/* First, see if we already have a link to the remote */
/* assume all ERTM l2cap connection is going over BR/EDR for now */
p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
p_lcb = l2cu_allocate_lcb(p_bd_addr, false, BT_TRANSPORT_BR_EDR);
/* currently use BR/EDR for ERTM mode l2cap connection */
if ((p_lcb == NULL) ||
(l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == false)) {
L2CAP_TRACE_WARNING(
"L2CAP - conn not started for PSM: 0x%04x p_lcb: 0x%08x", psm,
p_lcb);
return (0);
}
}
/* Allocate a channel control block */
p_ccb = l2cu_allocate_ccb(p_lcb, 0);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_conn_req, PSM: 0x%04x", psm);
return (0);
}
/* Save registration info */
p_ccb->p_rcb = p_rcb;
if (p_ertm_info) {
p_ccb->ertm_info = *p_ertm_info;
/* Replace default indicators with the actual default pool */
if (p_ccb->ertm_info.fcr_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
if (p_ccb->ertm_info.fcr_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
if (p_ccb->ertm_info.user_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
if (p_ccb->ertm_info.user_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
p_ccb->max_rx_mtu =
p_ertm_info->user_rx_buf_size -
(L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
}
/* If link is up, start the L2CAP connection */
if (p_lcb->link_state == LST_CONNECTED) {
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
}
/* If link is disconnecting, save link info to retry after disconnect
* Possible Race condition when a reconnect occurs
* on the channel during a disconnect of link. This
* ccb will be automatically retried after link disconnect
* arrives
*/
else if (p_lcb->link_state == LST_DISCONNECTING) {
L2CAP_TRACE_DEBUG("L2CAP API - link disconnecting: RETRY LATER");
/* Save ccb so it can be started after disconnect is finished */
p_lcb->p_pending_ccb = p_ccb;
}
L2CAP_TRACE_API("L2CAP - L2CA_conn_req(psm: 0x%04x) returned CID: 0x%04x",
psm, p_ccb->local_cid);
/* Return the local CID as our handle */
return (p_ccb->local_cid);
}
/*******************************************************************************
*
* Function L2CA_RegisterLECoc
*
* Description Other layers call this function to register for L2CAP
* Connection Oriented Channel.
*
* Returns PSM to use or zero if error. Typically, the PSM returned
* is the same as was passed in, but for an outgoing-only
* connection to a dynamic PSM, a "virtual" PSM is returned
* and should be used in the calls to L2CA_ConnectLECocReq()
* and L2CA_DeregisterLECoc()
*
******************************************************************************/
uint16_t L2CA_RegisterLECoc(uint16_t psm, tL2CAP_APPL_INFO* p_cb_info) {
L2CAP_TRACE_API("%s called for LE PSM: 0x%04x", __func__, psm);
/* Verify that the required callback info has been filled in
** Note: Connection callbacks are required but not checked
** for here because it is possible to be only a client
** or only a server.
*/
if ((!p_cb_info->pL2CA_DataInd_Cb) || (!p_cb_info->pL2CA_DisconnectInd_Cb)) {
L2CAP_TRACE_ERROR("%s No cb registering BLE PSM: 0x%04x", __func__, psm);
return 0;
}
/* Verify PSM is valid */
if (!L2C_IS_VALID_COC_PSM(psm)) {
L2CAP_TRACE_ERROR("%s Invalid BLE PSM value, PSM: 0x%04x", __func__, psm);
return 0;
}
tL2C_RCB* p_rcb;
uint16_t vpsm = psm;
/* Check if this is a registration for an outgoing-only connection to */
/* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
if ((psm >= L2CAP_COC_DYNAMIC_PSM_START) &&
(p_cb_info->pL2CA_ConnectInd_Cb == NULL)) {
vpsm = L2CA_AllocateCocPSM();
if (vpsm == 0) {
L2CAP_TRACE_ERROR("%s: Out of free BLE PSM", __func__);
return 0;
}
L2CAP_TRACE_API("%s Real PSM: 0x%04x Virtual PSM: 0x%04x", __func__, psm,
vpsm);
}
/* If registration block already there, just overwrite it */
p_rcb = l2cu_find_coc_rcb_by_psm(vpsm);
if (p_rcb == NULL) {
L2CAP_TRACE_API("%s Allocate rcp for Virtual PSM: 0x%04x", __func__, vpsm);
p_rcb = l2cu_allocate_coc_rcb(vpsm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("%s No BLE RCB available, PSM: 0x%04x vPSM: 0x%04x",
__func__, psm, vpsm);
return 0;
}
}
p_rcb->api = *p_cb_info;
p_rcb->real_psm = psm;
return vpsm;
}
/*******************************************************************************
*
* Function L2CA_RegisterCoc
*
* Description Other layers call this function to register for L2CAP
* Connection Oriented Channel.
*
* Returns PSM to use or zero if error. Typically, the PSM returned
* is the same as was passed in, but for an outgoing-only
* connection to a dynamic PSM, a "virtual" PSM is returned
* and should be used in the calls to L2CA_ConnectLECocReq()
* and L2CA_DeregisterLECoc()
*
******************************************************************************/
uint16_t L2CA_RegisterCoc(uint16_t psm, tL2CAP_COC_APPL_INFO *p_coc_cb_info,
bool enable_snoop) {
L2CAP_TRACE_API("%s called for PSM: 0x%04x", __func__, psm);
/* Verify that the required callback info has been filled in
** Note: Connection callbacks are required but not checked
** for here because it is possible to be only a client
** or only a server.
*/
if (!p_coc_cb_info) {
L2CAP_TRACE_ERROR("%s Invalid CB for registering COC PSM: 0x%04x",
__func__, psm);
return 0;
}
if ((!p_coc_cb_info->pL2CA_CocDataInd_Cb) ||
(!p_coc_cb_info->pL2CA_DisconnectInd_Cb)) {
L2CAP_TRACE_ERROR("%s No cb registering COC PSM: 0x%04x", __func__, psm);
return 0;
}
/* Verify PSM is valid */
if (!L2C_IS_VALID_COC_PSM(psm)) {
L2CAP_TRACE_ERROR("%s Invalid COC PSM value, PSM: 0x%04x", __func__, psm);
return 0;
}
tL2C_RCB* p_rcb;
uint16_t vpsm = psm;
/* Check if this is a registration for an outgoing-only connection to */
/* a dynamic PSM. If so, allocate a "virtual" PSM for the app to use. */
if ((psm >= L2CAP_COC_DYNAMIC_PSM_START) &&
(p_coc_cb_info->pL2CA_CocConnectInd_Cb == NULL)) {
vpsm = L2CA_AllocateCocPSM();
if (vpsm == 0) {
L2CAP_TRACE_ERROR("%s: Out of free Dynamic PSM", __func__);
return 0;
}
L2CAP_TRACE_API("%s Real PSM: 0x%04x Virtual PSM: 0x%04x", __func__, psm,
vpsm);
}
/* If registration block already there, just overwrite it */
p_rcb = l2cu_find_coc_rcb_by_psm(vpsm);
if (p_rcb == NULL) {
L2CAP_TRACE_API("%s Allocate rcp for Virtual PSM: 0x%04x", __func__, vpsm);
p_rcb = l2cu_allocate_coc_rcb(vpsm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("%s No COC RCB available, PSM: 0x%04x vPSM: 0x%04x",
__func__, psm, vpsm);
return 0;
}
}
p_rcb->log_packets = enable_snoop;
p_rcb->coc_api = *p_coc_cb_info;
p_rcb->real_psm = psm;
return vpsm;
}
/*******************************************************************************
*
* Function L2CA_DeregisterCoc
*
* Description Other layers call this function to de-register for L2CAP
* Connection Oriented Channel.
*
* Returns void
*
******************************************************************************/
void L2CA_DeregisterCoc(uint16_t psm) {
L2CAP_TRACE_API("%s called for PSM: 0x%04x", __func__, psm);
tL2C_RCB* p_rcb = l2cu_find_coc_rcb_by_psm(psm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("%s PSM: 0x%04x not found for deregistration", __func__,
psm);
return;
}
tL2C_LCB* p_lcb = &l2cb.lcb_pool[0];
for (int i = 0; i < MAX_L2CAP_LINKS; i++, p_lcb++) {
if (!p_lcb->in_use || p_lcb->transport != BT_TRANSPORT_LE) continue;
tL2C_CCB* p_ccb = p_lcb->ccb_queue.p_first_ccb;
if ((p_ccb == NULL) || (p_lcb->link_state == LST_DISCONNECTING)) continue;
if (p_ccb->in_use && (p_ccb->chnl_state == CST_W4_L2CAP_DISCONNECT_RSP ||
p_ccb->chnl_state == CST_W4_L2CA_DISCONNECT_RSP))
continue;
if (p_ccb->p_rcb == p_rcb)
l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
}
l2cu_release_coc_rcb(p_rcb);
}
/*******************************************************************************
*
* Function L2CA_ConnectLECocReq
*
* Description Higher layers call this function to create an L2CAP
* connection. Note that the connection is not established at
* this time, but connection establishment gets started. The
* callback function will be invoked when connection
* establishes or fails.
*
* Parameters: PSM: L2CAP PSM for the connection
* BD address of the peer
* Local Coc configurations
* Returns the CID of the connection, or 0 if it failed to start
*
******************************************************************************/
uint16_t L2CA_ConnectLECocReq(uint16_t psm, const RawAddress& p_bd_addr,
tL2CAP_COC_CFG_INFO* p_cfg) {
VLOG(1) << __func__ << " BDA: " << p_bd_addr
<< StringPrintf(" PSM: 0x%04x", psm);
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("%s BTU not ready", __func__);
return 0;
}
/* Fail if the PSM is not registered */
tL2C_RCB* p_rcb = l2cu_find_coc_rcb_by_psm(psm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("%s No BLE RCB, PSM: 0x%04x", __func__, psm);
return 0;
}
/* First, see if we already have a le link to the remote */
tL2C_LCB* p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
p_lcb = l2cu_allocate_lcb(p_bd_addr, false, BT_TRANSPORT_LE);
if ((p_lcb == NULL)
/* currently use BR/EDR for ERTM mode l2cap connection */
|| (l2cu_create_conn(p_lcb, BT_TRANSPORT_LE) == false)) {
L2CAP_TRACE_WARNING("%s conn not started for PSM: 0x%04x p_lcb: 0x%08x",
__func__, psm, p_lcb);
return 0;
}
}
/* Allocate a channel control block */
tL2C_CCB* p_ccb = l2cu_allocate_ccb(p_lcb, 0);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("%s no CCB, PSM: 0x%04x", __func__, psm);
return 0;
}
/* Save registration info */
p_ccb->p_rcb = p_rcb;
/* Save the configuration */
if (p_cfg) {
memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_COC_CFG_INFO));
p_ccb->remote_credit_count = p_cfg->credits;
}
/* If link is up, start the L2CAP connection */
if (p_lcb->link_state == LST_CONNECTED) {
if (p_ccb->p_lcb->transport == BT_TRANSPORT_LE) {
L2CAP_TRACE_DEBUG("%s LE Link is up", __func__);
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_REQ, NULL);
}
}
/* If link is disconnecting, save link info to retry after disconnect
* Possible Race condition when a reconnect occurs
* on the channel during a disconnect of link. This
* ccb will be automatically retried after link disconnect
* arrives
*/
else if (p_lcb->link_state == LST_DISCONNECTING) {
L2CAP_TRACE_DEBUG("%s link disconnecting: RETRY LATER", __func__);
/* Save ccb so it can be started after disconnect is finished */
p_lcb->p_pending_ccb = p_ccb;
}
L2CAP_TRACE_API("%s(psm: 0x%04x) returned CID: 0x%04x", __func__, psm,
p_ccb->local_cid);
/* Return the local CID as our handle */
return p_ccb->local_cid;
}
/*******************************************************************************
*
* Function L2CA_ConnectLECocRsp
*
* Description Higher layers call this function to accept an incoming
* L2CAP COC connection, for which they had gotten an connect
* indication callback.
*
* Returns true for success, false for failure
*
******************************************************************************/
bool L2CA_ConnectLECocRsp(const RawAddress& p_bd_addr, uint8_t id,
uint16_t lcid, uint16_t result, uint16_t status,
tL2CAP_COC_CFG_INFO* p_cfg) {
VLOG(1) << __func__ << " BDA: " << p_bd_addr
<< StringPrintf(" CID: 0x%04x Result: %d Status: %d", lcid, result,
status);
/* First, find the link control block */
tL2C_LCB* p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_LE);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
L2CAP_TRACE_WARNING("%s no LCB", __func__);
return false;
}
/* Now, find the channel control block */
tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("%s no CCB", __func__);
return false;
}
/* The IDs must match */
if (p_ccb->remote_id != id) {
L2CAP_TRACE_WARNING("%s bad id. Expected: %d Got: %d", __func__,
p_ccb->remote_id, id);
return false;
}
if (p_cfg) {
memcpy(&p_ccb->local_conn_cfg, p_cfg, sizeof(tL2CAP_COC_CFG_INFO));
p_ccb->remote_credit_count = p_cfg->credits;
}
if (result == L2CAP_CONN_OK)
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
else {
tL2C_CONN_INFO conn_info;
conn_info.bd_addr = p_bd_addr;
conn_info.l2cap_result = result;
conn_info.l2cap_status = status;
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
}
return true;
}
/*******************************************************************************
*
* Function L2CA_ConnectCocReq
*
* Description Higher layers call this function to create an L2CAP ECFC
* connection. Note that the connection is not established at
* this time, but connection establishment gets started. The
* callback function will be invoked when connection
* establishes or fails.
*
* Returns 0 - L2C_COC_CONNECT_REQ_SUCCESS
* 1 - L2C_PSM_NOT_REGISTERED
* 2 - L2C_LCB_NOT_ALLOCATED
* 3 - L2C_CONTROLLER_NOT_READY
* 4 - L2C_NO_RESOURCE_AVALIABLE
* 5 - L2C_SOME_CONNS_ACCEPTED
* 6 - L2C_INVALID_CONNECTION_PARAMS
* Note Upper layer should refer conn_req->sr_cids[i]
* to know the local CID's which are created.
******************************************************************************/
int8_t L2CA_ConnectCocReq(tL2CAP_COC_CONN_REQ* conn_req) {
VLOG(1) << __func__ << " BDA: " << conn_req->p_bd_addr
<< StringPrintf(" PSM: 0x%04x", conn_req->psm);
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("%s BTU not ready", __func__);
return L2C_CONTROLLER_NOT_READY;
}
/* Fail if the PSM is not registered */
tL2C_RCB* p_rcb = l2cu_find_coc_rcb_by_psm(conn_req->psm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("%s No RCB, PSM: 0x%04x", __func__, conn_req->psm);
return L2C_PSM_NOT_REGISTERED;
}
/* First, see if we already have a le link to the remote */
tL2C_LCB* p_lcb = l2cu_find_lcb_by_bd_addr(conn_req->p_bd_addr,
conn_req->transport);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
p_lcb = l2cu_allocate_lcb(conn_req->p_bd_addr, false, conn_req->transport);
if ((p_lcb == NULL)
|| (l2cu_create_conn(p_lcb, conn_req->transport) == false)) {
L2CAP_TRACE_WARNING("%s conn not started for PSM: 0x%04x p_lcb: 0x%08x",
__func__, conn_req->psm, p_lcb);
return L2C_LCB_NOT_ALLOCATED;
}
}
if ((conn_req->mtu < L2CAP_COC_MIN_MTU) ||
(conn_req->num_chnls > L2C_MAX_ECFC_CHNLS_PER_CONN) ||
(conn_req->num_chnls <= 0)) {
return L2C_INVALID_CONNECTION_PARAMS;
}
/* Allocate a channel control block */
tL2C_CCB* p_ccb[L2C_MAX_ECFC_CHNLS_PER_CONN];
uint8_t chnls_allocated = conn_req->num_chnls;
//TODO l2cu_allocate_coc_ccbs can be changed to lamda function
uint8_t chnl_status = l2cu_allocate_coc_ccbs(p_lcb, &p_ccb[0],
&chnls_allocated);
if (chnl_status == L2C_NO_RESOURCE_AVALIABLE) {
L2CAP_TRACE_WARNING("%s no CCB, PSM: 0x%04x", __func__, conn_req->psm);
return L2C_NO_RESOURCE_AVALIABLE;
}
conn_req->num_chnls = chnls_allocated;
uint16_t mps;
if (conn_req->transport == BT_TRANSPORT_LE) {
mps = controller_get_interface()->get_acl_data_size_ble();
} else {
mps = controller_get_interface()->get_acl_data_size_classic();
}
for (int i = 0; i < chnls_allocated; i++) {
if(p_ccb[i]) {
/* Save registration info */
p_ccb[i]->p_rcb = p_rcb;
/* Save the configuration */
p_ccb[i]->local_conn_cfg.mtu = conn_req->mtu;
p_ccb[i]->local_conn_cfg.mps = mps;
p_ccb[i]->local_conn_cfg.credits = L2CAP_COC_CREDIT_DEFAULT;
p_ccb[i]->coc_cmd_info.num_coc_chnls = chnls_allocated;
p_ccb[i]->peer_cfg.fcr.mode = L2CAP_FCR_ECFC_MODE;
conn_req->sr_cids[i] = p_ccb[i]->local_cid;
}
}
/* If link is up, start the L2CAP connection */
if (p_lcb->link_state == LST_CONNECTED) {
L2CAP_TRACE_DEBUG("%s Link is up And Num Chnls %d ", __func__, conn_req->num_chnls);
l2c_csm_execute(p_ccb[0], L2CEVT_L2CA_COC_CONNECT_REQ, NULL);
}
/* If link is disconnecting, save link info to retry after disconnect
* Possible Race condition when a reconnect occurs
* on the channel during a disconnect of link. This
* ccb will be automatically retried after link disconnect
* arrives
*/
else if (p_lcb->link_state == LST_DISCONNECTING) {
L2CAP_TRACE_DEBUG("%s link disconnecting: RETRY LATER %s ", __func__,
p_lcb->remote_bd_addr.ToString().c_str());
/* Save ccb so it can be started after disconnect is finished */
p_lcb->p_pending_ccb = p_ccb[0];
}
/* Return status for connection establishment procedure */
return chnl_status;
}
/*******************************************************************************
*
* Function L2CA_ConnectCocRsp
*
* Description Higher layers call this function to send the response to
* ecfc connection request.
*
* Return value: true or false
*
******************************************************************************/
bool L2CA_ConnectCocRsp(tL2CAP_COC_CONN_REQ *p_conn_req,
uint16_t l2cap_id,
uint16_t result,
uint16_t status) {
VLOG(1) << __func__ << " BDA: " << p_conn_req->p_bd_addr
<< StringPrintf(" Result: %d Status: %d", result, status);
uint16_t ecfc_conn_result = 0;
/* First, find the link control block */
tL2C_LCB* p_lcb = l2cu_find_lcb_by_bd_addr(p_conn_req->p_bd_addr,
p_conn_req->transport);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
L2CAP_TRACE_WARNING("%s no Active link for Bd_addr %s", __func__,
p_conn_req->p_bd_addr.ToString().c_str());
return false;
}
if (!l2cu_is_unaccepted_coc_result_code(result)) {
/* Now, find the channel control block */
tL2C_CCB* p_ccb[L2C_MAX_ECFC_CHNLS_PER_CONN];
tL2C_CCB* p_actual_ccb[L2C_MAX_ECFC_CHNLS_PER_CONN];
uint16_t valid_chnls = 0;
for (int i = 0; i < p_conn_req->num_chnls; i++) {
p_ccb[i] = l2cu_find_ccb_by_cid(p_lcb, p_conn_req->sr_cids[i]);
if (p_ccb[i] == NULL) {
L2CAP_TRACE_WARNING("%s channel closed for CID %d ", __func__,
p_conn_req->sr_cids[i]);
ecfc_conn_result = L2CAP_ECFC_SOME_CONNS_REFUSED_INSUFF_RESOURCES;
} else {
p_actual_ccb[valid_chnls] = p_ccb[i];
L2CAP_TRACE_WARNING("%s chnl_index channel CID %d %d", __func__, valid_chnls,
p_conn_req->sr_cids[i], p_actual_ccb[valid_chnls]->local_cid);
valid_chnls++;
}
}
if (valid_chnls == 0) {
L2CAP_TRACE_WARNING("%s All channels in the ECFC Connect request are closed ",
__func__);
return false;
}
if (valid_chnls < p_conn_req->num_chnls) {
p_conn_req->num_chnls = valid_chnls;
} else {
if (result == L2CAP_ECFC_SOME_CONNS_REFUSED_INSUFF_RESOURCES) {
L2CAP_TRACE_WARNING("%s upper layer rejected the some channels ",
__func__);
uint16_t sr_cids[L2C_MAX_ECFC_CHNLS_PER_CONN] = {0};
for (int i = 0; i < p_conn_req->num_chnls; i++) {
sr_cids[i] = p_actual_ccb[i]->local_cid;
p_ccb[i] = p_actual_ccb[i];
}
for (int i = p_conn_req->num_chnls;
i < p_actual_ccb[0]->coc_cmd_info.requested_ecfc_chnls; i++) {
sr_cids[i] = 0;
if (p_actual_ccb[i] != NULL) {
l2cu_release_ccb(p_actual_ccb[i]);
}
p_ccb[i] = NULL;
}
l2cu_set_coc_conn_rsp_cids(sr_cids, p_ccb[0], p_conn_req->num_chnls);
for (int i = 0; i < p_actual_ccb[0]->coc_cmd_info.requested_ecfc_chnls;
i++) {
L2CAP_TRACE_WARNING("%s peer_rsp_cid %d", __func__,
p_ccb[0]->coc_cmd_info.peer_rsp_cids[i]);
}
} else {
p_conn_req->num_chnls = valid_chnls;
}
}
//TODO move below to separate API
for (int i = 0; i < p_conn_req->num_chnls; i++) {
/* The IDs must match */
if (p_ccb[i]!= NULL) {
if (p_ccb[i]->remote_id != l2cap_id) {
L2CAP_TRACE_WARNING("%s bad id. Expected: %d Got: %d", __func__,
p_ccb[i]->remote_id, l2cap_id);
ecfc_conn_result = L2CAP_ECFC_SOME_CONNS_REFUSED_INSUFF_RESOURCES;
valid_chnls--;
}
}
}
if (valid_chnls == 0) {
return false;
}
uint16_t mps;
if (p_conn_req->transport == BT_TRANSPORT_LE) {
mps = controller_get_interface()->get_acl_data_size_ble();
} else {
mps = controller_get_interface()->get_acl_data_size_classic();
}
if (ecfc_conn_result && (result != L2CAP_ECFC_SOME_CONNS_REFUSED_INSUFF_RESOURCES))
result = ecfc_conn_result;
for (int i = 0; i < valid_chnls; i++) {
if (p_ccb[i] != NULL) {
p_ccb[i]->local_conn_cfg.mtu = p_conn_req->mtu;
p_ccb[i]->local_conn_cfg.mps = mps;
p_ccb[i]->local_conn_cfg.credits = L2CAP_COC_CREDIT_DEFAULT;
p_ccb[i]->coc_cmd_info.ecfc_conn_result = result;
}
}
switch(result) {
case L2CAP_ECFC_ALL_CONNS_SUCCESSFUL:
case L2CAP_ECFC_SOME_CONNS_REFUSED_INSUFF_RESOURCES:
case L2CAP_ECFC_SOME_CONNS_REFUSED_INVALID_SOURCE_CID:
case L2CAP_ECFC_SOME_CONNS_REFUSED_SOURCE_CID_ALREADY_ALLOCATED:
l2c_csm_execute(p_ccb[0], L2CEVT_L2CA_COC_CONNECT_RSP, NULL);
break;
default:
l2c_csm_execute(p_ccb[0], L2CEVT_L2CA_COC_CONNECT_NEG_RSP, NULL);
}
} else {
tL2C_CCB* tmp_ccb = l2cu_find_ccb_by_l2cap_id(p_lcb, l2cap_id);
if (tmp_ccb != NULL) {
L2CAP_TRACE_API("%s CID: 0x%04x Result %d", __func__,
tmp_ccb->local_cid, result);
tmp_ccb->coc_cmd_info.ecfc_conn_result = result;
tmp_ccb->coc_cmd_info.num_coc_chnls =
tmp_ccb->coc_cmd_info.requested_ecfc_chnls;
l2c_csm_execute(tmp_ccb, L2CEVT_L2CA_COC_CONNECT_NEG_RSP, NULL);
} else {
L2CAP_TRACE_API("%s Invalid Channel and result %d", __func__, result);
return false;
}
}
return true;
}
/*******************************************************************************
*
* Function L2CA_GetPeerLECocConfig
*
* Description Get a peers configuration for LE Connection Oriented
* Channel.
*
* Parameters: local channel id
* Pointers to peers configuration storage area
*
* Return value: true if peer is connected
*
******************************************************************************/
bool L2CA_GetPeerLECocConfig(uint16_t lcid, tL2CAP_COC_CFG_INFO* peer_cfg) {
L2CAP_TRACE_API("%s CID: 0x%04x", __func__, lcid);
tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
if (p_ccb == NULL) {
L2CAP_TRACE_ERROR("%s No CCB for CID:0x%04x", __func__, lcid);
return false;
}
if (peer_cfg != NULL)
memcpy(peer_cfg, &p_ccb->peer_conn_cfg, sizeof(tL2CAP_COC_CFG_INFO));
return true;
}
bool L2CA_SetConnectionCallbacks(uint16_t local_cid,
const tL2CAP_APPL_INFO* callbacks) {
CHECK(callbacks != NULL);
CHECK(callbacks->pL2CA_ConnectInd_Cb == NULL);
CHECK(callbacks->pL2CA_ConnectCfm_Cb != NULL);
CHECK(callbacks->pL2CA_ConfigInd_Cb != NULL);
CHECK(callbacks->pL2CA_ConfigCfm_Cb != NULL);
CHECK(callbacks->pL2CA_DisconnectInd_Cb != NULL);
CHECK(callbacks->pL2CA_DisconnectCfm_Cb != NULL);
CHECK(callbacks->pL2CA_CongestionStatus_Cb != NULL);
CHECK(callbacks->pL2CA_DataInd_Cb != NULL);
CHECK(callbacks->pL2CA_TxComplete_Cb != NULL);
tL2C_CCB* channel_control_block = l2cu_find_ccb_by_cid(NULL, local_cid);
if (!channel_control_block) {
LOG_ERROR(LOG_TAG,
"%s no channel control block found for L2CAP LCID=0x%04x.",
__func__, local_cid);
return false;
}
// We're making a connection-specific registration control block so we check
// if we already have a private one allocated to us on the heap. If not, we
// make a new allocation, mark it as heap-allocated, and inherit the fields
// from the old control block.
tL2C_RCB* registration_control_block = channel_control_block->p_rcb;
if (!channel_control_block->should_free_rcb) {
registration_control_block = (tL2C_RCB*)osi_calloc(sizeof(tL2C_RCB));
*registration_control_block = *channel_control_block->p_rcb;
channel_control_block->p_rcb = registration_control_block;
channel_control_block->should_free_rcb = true;
}
registration_control_block->api = *callbacks;
return true;
}
/*******************************************************************************
*
* Function L2CA_ConnectRsp
*
* Description Higher layers call this function to accept an incoming
* L2CAP connection, for which they had gotten an connect
* indication callback.
*
* Returns true for success, false for failure
*
******************************************************************************/
bool L2CA_ConnectRsp(const RawAddress& p_bd_addr, uint8_t id, uint16_t lcid,
uint16_t result, uint16_t status) {
return L2CA_ErtmConnectRsp(p_bd_addr, id, lcid, result, status, NULL);
}
/*******************************************************************************
*
* Function L2CA_ErtmConnectRsp
*
* Description Higher layers call this function to accept an incoming
* L2CAP connection, for which they had gotten an connect
* indication callback.
*
* Returns true for success, false for failure
*
******************************************************************************/
bool L2CA_ErtmConnectRsp(const RawAddress& p_bd_addr, uint8_t id, uint16_t lcid,
uint16_t result, uint16_t status,
tL2CAP_ERTM_INFO* p_ertm_info) {
tL2C_LCB* p_lcb;
tL2C_CCB* p_ccb;
VLOG(1) << __func__ << " BDA: " << p_bd_addr
<< StringPrintf(" CID:0x%04x Result:%d Status:%d", lcid, result,
status);
/* First, find the link control block */
p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
L2CAP_TRACE_WARNING("L2CAP - no LCB for L2CA_conn_rsp");
return (false);
}
/* Now, find the channel control block */
p_ccb = l2cu_find_ccb_by_cid(p_lcb, lcid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_conn_rsp");
return (false);
}
/* The IDs must match */
if (p_ccb->remote_id != id) {
L2CAP_TRACE_WARNING("L2CAP - bad id in L2CA_conn_rsp. Exp: %d Got: %d",
p_ccb->remote_id, id);
return (false);
}
if (p_ertm_info) {
p_ccb->ertm_info = *p_ertm_info;
/* Replace default indicators with the actual default pool */
if (p_ccb->ertm_info.fcr_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.fcr_rx_buf_size = L2CAP_FCR_RX_BUF_SIZE;
if (p_ccb->ertm_info.fcr_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.fcr_tx_buf_size = L2CAP_FCR_TX_BUF_SIZE;
if (p_ccb->ertm_info.user_rx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.user_rx_buf_size = L2CAP_USER_RX_BUF_SIZE;
if (p_ccb->ertm_info.user_tx_buf_size == L2CAP_INVALID_ERM_BUF_SIZE)
p_ccb->ertm_info.user_tx_buf_size = L2CAP_USER_TX_BUF_SIZE;
p_ccb->max_rx_mtu =
p_ertm_info->user_rx_buf_size -
(L2CAP_MIN_OFFSET + L2CAP_SDU_LEN_OFFSET + L2CAP_FCS_LEN);
}
if (result == L2CAP_CONN_OK) {
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP, NULL);
} else {
tL2C_CONN_INFO conn_info;
conn_info.l2cap_result = result;
conn_info.l2cap_status = status;
if (result == L2CAP_CONN_PENDING)
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP, &conn_info);
else
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONNECT_RSP_NEG, &conn_info);
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_ConfigReq
*
* Description Higher layers call this function to send configuration.
*
* Note: The FCR options of p_cfg are not used.
*
* Returns true if configuration sent, else false
*
******************************************************************************/
bool L2CA_ConfigReq(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API(
"L2CA_ConfigReq() CID 0x%04x: fcr_present:%d (mode %d) mtu_present:%d "
"(%d)",
cid, p_cfg->fcr_present, p_cfg->fcr.mode, p_cfg->mtu_present, p_cfg->mtu);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_cfg_req, CID: %d", cid);
return (false);
}
/* We need to have at least one mode type common with the peer */
if (!l2c_fcr_adj_our_req_options(p_ccb, p_cfg)) return (false);
/* Don't adjust FCR options if not used */
if ((!p_cfg->fcr_present) || (p_cfg->fcr.mode == L2CAP_FCR_BASIC_MODE)) {
/* FCR and FCS options are not used in basic mode */
p_cfg->fcs_present = false;
p_cfg->ext_flow_spec_present = false;
if ((p_cfg->mtu_present) && (p_cfg->mtu > L2CAP_MTU_SIZE)) {
L2CAP_TRACE_WARNING("L2CAP - adjust MTU: %u too large", p_cfg->mtu);
p_cfg->mtu = L2CAP_MTU_SIZE;
}
}
/* Save the adjusted configuration in case it needs to be used for
* renegotiation */
p_ccb->our_cfg = *p_cfg;
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONFIG_REQ, p_cfg);
return (true);
}
/*********************************************************************************
*
* Function L2CA_ReconfigCocReq
*
* Description Upper layer call this function to reconfigure l2cap channel
* in Enhanced Credit Based Flow Control mode. Upper layer
* can only update MTU value. L2CAP layer assigns MPS.
*
* Parameters chmap_info: info of the channels to be reconfigured.
* mtu: Maximum transmission unit as required by upper layer.
*
* Returns true if reconfiguration request sent, otherwise false
*
******************************************************************************/
bool L2CA_ReconfigCocReq(tL2CAP_COC_CHMAP_INFO* chmap_info, uint16_t mtu) {
L2CAP_TRACE_API("%s, mtu = %d", __func__, mtu);
if (!chmap_info || !chmap_info->num_chnls) {
L2CAP_TRACE_WARNING("%s: Incomplete channel information", __func__);
return (false);
}
//check if MTU value is acceptable
if (l2cu_validate_mtu_for_chnls(mtu, chmap_info->sr_cids, chmap_info->num_chnls, true)) {
return (false);
}
// upper layer has no control over mps.
return l2cu_reconfig_coc_req(chmap_info, mtu, 0);
}
/*********************************************************************************
*
* Function L2CA_ReconfigCocReq
*
* Description Upper layer call this function to reconfigure l2cap channel
* in Enhanced Credit Based Floe Control mode. Upper layer
* can only update mtu value. L2CAP layer assigns for mps.
*
* Parameters chmap_info: info of the channels to be reconfigured.
* mtu: Maximum transmission unit as required by upper layer.
*
* Returns true if reconfiguration request sent, otherwise false
*
******************************************************************************/
bool L2CA_ReconfigCocReqMps(tL2CAP_COC_CHMAP_INFO* chmap_info, uint16_t mps) {
L2CAP_TRACE_API("%s, mps = %d", __func__, mps);
if (!chmap_info || !chmap_info->num_chnls) {
L2CAP_TRACE_WARNING("%s: Incomplete channel information", __func__);
return (false);
}
//check if MTU value is acceptable
if (l2cu_validate_mps_for_chnls(mps, chmap_info->sr_cids, chmap_info->num_chnls, true)) {
return (false);
}
return l2cu_reconfig_coc_req(chmap_info, 0, mps); // upper layer has no control over mps
}
/*******************************************************************************
*
* Function L2CA_ConfigRsp
*
* Description Higher layers call this function to send a configuration
* response.
*
* Returns true if configuration response sent, else false
*
******************************************************************************/
bool L2CA_ConfigRsp(uint16_t cid, tL2CAP_CFG_INFO* p_cfg) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API(
"L2CA_ConfigRsp() CID: 0x%04x Result: %d MTU present:%d Flush TO:%d "
"FCR:%d FCS:%d",
cid, p_cfg->result, p_cfg->mtu_present, p_cfg->flush_to_present,
p_cfg->fcr_present, p_cfg->fcs_present);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_cfg_rsp, CID: %d", cid);
return (false);
}
if ((p_cfg->result == L2CAP_CFG_OK) || (p_cfg->result == L2CAP_CFG_PENDING))
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONFIG_RSP, p_cfg);
else {
p_cfg->fcr_present =
false; /* FCR options already negotiated before this point */
/* Clear out any cached options that are being returned as an error
* (excluding FCR) */
if (p_cfg->mtu_present) p_ccb->peer_cfg.mtu_present = false;
if (p_cfg->flush_to_present) p_ccb->peer_cfg.flush_to_present = false;
if (p_cfg->qos_present) p_ccb->peer_cfg.qos_present = false;
l2c_csm_execute(p_ccb, L2CEVT_L2CA_CONFIG_RSP_NEG, p_cfg);
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_ReconfigCocRsp
*
* Description Upper layer calls this function to send reconfiguration
* response in Enhanced Credit based flow control mode.
*
* Parameters chmap_info : information about l2cap channels in response
* result : Result code for the response.
*
* Returns true if Reconfiguration response is sent, else false
*
******************************************************************************/
bool L2CA_ReconfigCocRsp(tL2CAP_COC_CHMAP_INFO* chmap_info, uint16_t result) {
tL2C_CCB* p_ccb = NULL;
if (!chmap_info || !chmap_info->num_chnls) {
L2CAP_TRACE_WARNING("%s: Incomplete channel information.", __func__);
return (false);
}
if (!l2cu_validate_cids_in_use_status(chmap_info, &p_ccb)) {
if (p_ccb) {
L2CAP_TRACE_WARNING("L2CAP - some of ccb's are not active");
p_ccb->pending_inc_cfg->result = L2CAP_RCFG_INVALID_DCID;
} else {
L2CAP_TRACE_WARNING("L2CAP - all channels are closed, ignore response");
return (false);
}
} else {
p_ccb->pending_inc_cfg->result = result;
}
l2c_csm_execute(p_ccb, L2CEVT_L2CA_COC_RECONFIG_RSP, p_ccb->pending_inc_cfg);
return (true);
}
/*******************************************************************************
*
* Function L2CA_DisconnectReq
*
* Description Higher layers call this function to disconnect a channel.
*
* Returns true if disconnect sent, else false
*
******************************************************************************/
bool L2CA_DisconnectReq(uint16_t cid) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_WARNING("L2CA_DisconnectReq() CID: 0x%04x", cid);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_disc_req, CID: %d", cid);
return (false);
}
l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_REQ, NULL);
return (true);
}
/*******************************************************************************
*
* Function L2CA_DisconnectRsp
*
* Description Higher layers call this function to acknowledge the
* disconnection of a channel.
*
* Returns void
*
******************************************************************************/
bool L2CA_DisconnectRsp(uint16_t cid) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_WARNING("L2CA_DisconnectRsp() CID: 0x%04x", cid);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_disc_rsp, CID: %d", cid);
return (false);
}
l2c_csm_execute(p_ccb, L2CEVT_L2CA_DISCONNECT_RSP, NULL);
return (true);
}
/*******************************************************************************
*
* Function L2CA_Ping
*
* Description Higher layers call this function to send an echo request.
*
* Returns true if echo request sent, else false.
*
******************************************************************************/
bool L2CA_Ping(const RawAddress& p_bd_addr, tL2CA_ECHO_RSP_CB* p_callback) {
tL2C_LCB* p_lcb;
VLOG(1) << __func__ << " BDA: " << p_bd_addr;
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) return (false);
/* First, see if we already have a link to the remote */
p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
/* No link. Get an LCB and start link establishment */
p_lcb = l2cu_allocate_lcb(p_bd_addr, false, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no LCB for L2CA_ping");
return (false);
}
if (l2cu_create_conn(p_lcb, BT_TRANSPORT_BR_EDR) == false) {
return (false);
}
p_lcb->p_echo_rsp_cb = p_callback;
return (true);
}
/* We only allow 1 ping outstanding at a time */
if (p_lcb->p_echo_rsp_cb != NULL) {
L2CAP_TRACE_WARNING("L2CAP - rejected second L2CA_ping");
return (false);
}
/* Have a link control block. If link is disconnecting, tell user to retry
* later */
if (p_lcb->link_state == LST_DISCONNECTING) {
L2CAP_TRACE_WARNING("L2CAP - L2CA_ping rejected - link disconnecting");
return (false);
}
/* Save address of callback */
p_lcb->p_echo_rsp_cb = p_callback;
if (p_lcb->link_state == LST_CONNECTED) {
l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID); /* Make sure not using Broadcom ID */
l2cu_send_peer_echo_req(p_lcb, NULL, 0);
alarm_set_on_mloop(p_lcb->l2c_lcb_timer, L2CAP_ECHO_RSP_TIMEOUT_MS,
l2c_lcb_timer_timeout, p_lcb);
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_Echo
*
* Description Higher layers call this function to send an echo request
* with application-specific data.
*
* Returns true if echo request sent, else false.
*
******************************************************************************/
bool L2CA_Echo(const RawAddress& p_bd_addr, BT_HDR* p_data,
tL2CA_ECHO_DATA_CB* p_callback) {
tL2C_LCB* p_lcb;
uint8_t* pp;
VLOG(1) << __func__ << " BDA: " << p_bd_addr;
;
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) return (false);
if (RawAddress::kAny == p_bd_addr && (p_data == NULL)) {
/* Only register callback without sending message. */
l2cb.p_echo_data_cb = p_callback;
return true;
}
/* We assume the upper layer will call this function only when the link is
* established. */
p_lcb = l2cu_find_lcb_by_bd_addr(p_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
L2CAP_TRACE_ERROR("L2CA_Echo ERROR : link not established");
return false;
}
if (p_lcb->link_state != LST_CONNECTED) {
L2CAP_TRACE_ERROR("L2CA_Echo ERROR : link is not connected");
return false;
}
/* Save address of callback */
l2cb.p_echo_data_cb = p_callback;
/* Set the pointer to the beginning of the data */
pp = (uint8_t*)(p_data + 1) + p_data->offset;
l2cu_adj_id(p_lcb, L2CAP_ADJ_BRCM_ID); /* Make sure not using Broadcom ID */
l2cu_send_peer_echo_req(p_lcb, pp, p_data->len);
return (true);
}
bool L2CA_GetIdentifiers(uint16_t lcid, uint16_t* rcid, uint16_t* handle) {
tL2C_CCB* control_block = l2cu_find_ccb_by_cid(NULL, lcid);
if (!control_block) return false;
if (rcid) *rcid = control_block->remote_cid;
if (handle) *handle = control_block->p_lcb->handle;
return true;
}
/*******************************************************************************
*
* Function L2CA_SetIdleTimeout
*
* Description Higher layers call this function to set the idle timeout for
* a connection, or for all future connections. The "idle
* timeout" is the amount of time that a connection can remain
* up with no L2CAP channels on it. A timeout of zero means
* that the connection will be torn down immediately when the
* last channel is removed. A timeout of 0xFFFF means no
* timeout. Values are in seconds.
*
* Returns true if command succeeded, false if failed
*
* NOTE This timeout takes effect after at least 1 channel has been
* established and removed. L2CAP maintains its own timer from
* whan a connection is established till the first channel is
* set up.
******************************************************************************/
bool L2CA_SetIdleTimeout(uint16_t cid, uint16_t timeout, bool is_global) {
tL2C_CCB* p_ccb;
tL2C_LCB* p_lcb;
if (is_global) {
l2cb.idle_timeout = timeout;
} else {
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_SetIdleTimeout, CID: %d",
cid);
return (false);
}
p_lcb = p_ccb->p_lcb;
if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED))
p_lcb->idle_timeout = timeout;
else
return (false);
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_SetIdleTimeoutByBdAddr
*
* Description Higher layers call this function to set the idle timeout for
* a connection. The "idle timeout" is the amount of time that
* a connection can remain up with no L2CAP channels on it.
* A timeout of zero means that the connection will be torn
* down immediately when the last channel is removed.
* A timeout of 0xFFFF means no timeout. Values are in seconds.
* A bd_addr is the remote BD address. If bd_addr =
* RawAddress::kAny, then the idle timeouts for all active
* l2cap links will be changed.
*
* Returns true if command succeeded, false if failed
*
* NOTE This timeout applies to all logical channels active on the
* ACL link.
******************************************************************************/
bool L2CA_SetIdleTimeoutByBdAddr(const RawAddress& bd_addr, uint16_t timeout,
tBT_TRANSPORT transport) {
tL2C_LCB* p_lcb;
if (RawAddress::kAny != bd_addr) {
p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, transport);
if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
p_lcb->idle_timeout = timeout;
if (!p_lcb->ccb_queue.p_first_ccb) l2cu_no_dynamic_ccbs(p_lcb);
} else
return false;
} else {
int xx;
tL2C_LCB* p_lcb = &l2cb.lcb_pool[0];
for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
p_lcb->idle_timeout = timeout;
if (!p_lcb->ccb_queue.p_first_ccb) l2cu_no_dynamic_ccbs(p_lcb);
}
}
}
return true;
}
/*******************************************************************************
*
* Function L2CA_SetTraceLevel
*
* Description This function sets the trace level for L2CAP. If called with
* a value of 0xFF, it simply reads the current trace level.
*
* Returns the new (current) trace level
*
******************************************************************************/
uint8_t L2CA_SetTraceLevel(uint8_t new_level) {
if (new_level != 0xFF) l2cb.l2cap_trace_level = new_level;
return (l2cb.l2cap_trace_level);
}
/*******************************************************************************
*
* Function L2CA_SetDesireRole
*
* Description This function sets the desire role for L2CAP.
* If the new role is L2CAP_ROLE_ALLOW_SWITCH, allow switch on
* HciCreateConnection.
* If the new role is L2CAP_ROLE_DISALLOW_SWITCH, do not allow
* switch on HciCreateConnection.
*
* If the new role is a valid role (HCI_ROLE_MASTER or
* HCI_ROLE_SLAVE), the desire role is set to the new value.
* Otherwise, it is not changed.
*
* Returns the new (current) role
*
******************************************************************************/
uint8_t L2CA_SetDesireRole(uint8_t new_role) {
L2CAP_TRACE_WARNING("L2CA_SetDesireRole() new:x%x, disallow_switch:%d", new_role,
l2cb.disallow_switch);
if (L2CAP_ROLE_CHECK_SWITCH != (L2CAP_ROLE_CHECK_SWITCH & new_role)) {
/* do not process the allow_switch when both bits are set */
if (new_role & L2CAP_ROLE_ALLOW_SWITCH) {
l2cb.disallow_switch = false;
}
if (new_role & L2CAP_ROLE_DISALLOW_SWITCH) {
l2cb.disallow_switch = true;
}
}
if (new_role == HCI_ROLE_MASTER || new_role == HCI_ROLE_SLAVE)
l2cb.desire_role = new_role;
return (l2cb.desire_role);
}
/*******************************************************************************
*
* Function L2CA_LocalLoopbackReq
*
* Description This function sets up a CID for local loopback
*
* Returns CID of 0 if none.
*
******************************************************************************/
uint16_t L2CA_LocalLoopbackReq(uint16_t psm, uint16_t handle,
const RawAddress& p_bd_addr) {
tL2C_LCB* p_lcb;
tL2C_CCB* p_ccb;
tL2C_RCB* p_rcb;
L2CAP_TRACE_API("L2CA_LocalLoopbackReq() PSM: %d Handle: 0x%04x", psm,
handle);
/* Fail if we have not established communications with the controller */
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("L2CAP loop req - BTU not ready");
return (0);
}
/* Fail if the PSM is not registered */
p_rcb = l2cu_find_rcb_by_psm(psm);
if (p_rcb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no RCB for L2CA_conn_req, PSM: %d", psm);
return (0);
}
p_lcb = l2cu_allocate_lcb(p_bd_addr, false, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no LCB for L2CA_conn_req");
return (0);
}
p_lcb->link_state = LST_CONNECTED;
p_lcb->handle = handle;
/* Allocate a channel control block */
p_ccb = l2cu_allocate_ccb(p_lcb, 0);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_conn_req");
return (0);
}
/* Save registration info */
p_ccb->p_rcb = p_rcb;
p_ccb->chnl_state = CST_OPEN;
p_ccb->remote_cid = p_ccb->local_cid;
p_ccb->config_done = CFG_DONE_MASK;
/* Return the local CID as our handle */
return (p_ccb->local_cid);
}
/*******************************************************************************
*
* Function L2CA_SetAclPriority
*
* Description Sets the transmission priority for a channel.
* (For initial implementation only two values are valid.
* L2CAP_PRIORITY_NORMAL and L2CAP_PRIORITY_HIGH).
*
* Returns true if a valid channel, else false
*
******************************************************************************/
bool L2CA_SetAclPriority(const RawAddress& bd_addr, uint8_t priority) {
VLOG(1) << __func__ << " BDA: " << bd_addr << ", priority: " << priority;
return (l2cu_set_acl_priority(bd_addr, priority, false));
}
/*******************************************************************************
*
* Function L2CA_FlowControl
*
* Description Higher layers call this function to flow control a channel.
*
* data_enabled - true data flows, false data is stopped
*
* Returns true if valid channel, else false
*
******************************************************************************/
bool L2CA_FlowControl(uint16_t cid, bool data_enabled) {
tL2C_CCB* p_ccb;
bool on_off = !data_enabled;
L2CAP_TRACE_WARNING("L2CA_FlowControl(%d) CID: 0x%04x", on_off, cid);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING(
"L2CAP - no CCB for L2CA_FlowControl, CID: 0x%04x data_enabled: %d",
cid, data_enabled);
return (false);
}
if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) {
L2CAP_TRACE_EVENT("L2CA_FlowControl() invalid mode:%d",
p_ccb->peer_cfg.fcr.mode);
return (false);
}
if (p_ccb->fcrb.local_busy != on_off) {
p_ccb->fcrb.local_busy = on_off;
if ((p_ccb->chnl_state == CST_OPEN) && (!p_ccb->fcrb.wait_ack)) {
if (on_off)
l2c_fcr_send_S_frame(p_ccb, L2CAP_FCR_SUP_RNR, 0);
else
l2c_fcr_send_S_frame(p_ccb, L2CAP_FCR_SUP_RR, L2CAP_FCR_P_BIT);
}
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_SendTestSFrame
*
* Description Higher layers call this function to send a test S-frame.
*
* Returns true if valid Channel, else false
*
******************************************************************************/
bool L2CA_SendTestSFrame(uint16_t cid, uint8_t sup_type, uint8_t back_track) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API(
"L2CA_SendTestSFrame() CID: 0x%04x Type: 0x%02x back_track: %u", cid,
sup_type, back_track);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_SendTestSFrame, CID: %d", cid);
return (false);
}
if ((p_ccb->chnl_state != CST_OPEN) ||
(p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE))
return (false);
p_ccb->fcrb.next_seq_expected -= back_track;
l2c_fcr_send_S_frame(
p_ccb, (uint16_t)(sup_type & 3),
(uint16_t)(sup_type & (L2CAP_FCR_P_BIT | L2CAP_FCR_F_BIT)));
return (true);
}
/*******************************************************************************
*
* Function L2CA_SetTxPriority
*
* Description Sets the transmission priority for a channel.
*
* Returns true if a valid channel, else false
*
******************************************************************************/
bool L2CA_SetTxPriority(uint16_t cid, tL2CAP_CHNL_PRIORITY priority) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API("L2CA_SetTxPriority() CID: 0x%04x, priority:%d", cid,
priority);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_SetTxPriority, CID: %d", cid);
return (false);
}
/* it will update the order of CCB in LCB by priority and update round robin
* service variables */
l2cu_change_pri_ccb(p_ccb, priority);
return (true);
}
/*******************************************************************************
*
* Function L2CA_SetChnlDataRate
*
* Description Sets the tx/rx data rate for a channel.
*
* Returns true if a valid channel, else false
*
******************************************************************************/
bool L2CA_SetChnlDataRate(uint16_t cid, tL2CAP_CHNL_DATA_RATE tx,
tL2CAP_CHNL_DATA_RATE rx) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API("L2CA_SetChnlDataRate() CID: 0x%04x, tx:%d, rx:%d", cid, tx,
rx);
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_SetChnlDataRate, CID: %d",
cid);
return (false);
}
p_ccb->tx_data_rate = tx;
p_ccb->rx_data_rate = rx;
/* Adjust channel buffer allocation */
l2c_link_adjust_chnl_allocation();
return (true);
}
/*******************************************************************************
*
* Function L2CA_SetFlushTimeout
*
* Description This function set the automatic flush time out in Baseband
* for ACL-U packets.
* BdAddr : the remote BD address of ACL link. If it is
* BT_DB_ANY then the flush time out will be applied to
* all ACL links.
* FlushTimeout: flush time out in ms
* 0x0000 : No automatic flush
* L2CAP_NO_RETRANSMISSION : No retransmission
* 0x0002 - 0xFFFE : flush time out, if
* (flush_tout * 8) + 3 / 5) <=
* HCI_MAX_AUTOMATIC_FLUSH_TIMEOUT
* (in 625us slot).
* Otherwise, return false.
* L2CAP_NO_AUTOMATIC_FLUSH : No automatic flush
*
* Returns true if command succeeded, false if failed
*
* NOTE This flush timeout applies to all logical channels active on
* the ACL link.
******************************************************************************/
bool L2CA_SetFlushTimeout(const RawAddress& bd_addr, uint16_t flush_tout) {
tL2C_LCB* p_lcb;
uint16_t hci_flush_to;
uint32_t temp;
/* no automatic flush (infinite timeout) */
if (flush_tout == 0x0000) {
hci_flush_to = flush_tout;
flush_tout = L2CAP_NO_AUTOMATIC_FLUSH;
}
/* no retransmission */
else if (flush_tout == L2CAP_NO_RETRANSMISSION) {
/* not mandatory range for controller */
/* Packet is flushed before getting any ACK/NACK */
/* To do this, flush timeout should be 1 baseband slot */
hci_flush_to = flush_tout;
}
/* no automatic flush (infinite timeout) */
else if (flush_tout == L2CAP_NO_AUTOMATIC_FLUSH) {
hci_flush_to = 0x0000;
} else {
/* convert L2CAP flush_to to 0.625 ms units, with round */
temp = (((uint32_t)flush_tout * 8) + 3) / 5;
/* if L2CAP flush_to within range of HCI, set HCI flush timeout */
if (temp > HCI_MAX_AUTOMATIC_FLUSH_TIMEOUT) {
L2CAP_TRACE_WARNING(
"WARNING L2CA_SetFlushTimeout timeout(0x%x) is out of range",
flush_tout);
return false;
} else {
hci_flush_to = (uint16_t)temp;
}
}
if (RawAddress::kAny != bd_addr) {
p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_BR_EDR);
if ((p_lcb) && (p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
if (p_lcb->link_flush_tout != flush_tout) {
p_lcb->link_flush_tout = flush_tout;
VLOG(1) << __func__ << " BDA: " << bd_addr << " " << flush_tout << "ms";
btsnd_hcic_write_auto_flush_tout(p_lcb->handle, hci_flush_to);
}
} else {
LOG(WARNING) << __func__ << " No lcb for bd_addr " << bd_addr;
return (false);
}
} else {
int xx;
p_lcb = &l2cb.lcb_pool[0];
for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_lcb++) {
if ((p_lcb->in_use) && (p_lcb->link_state == LST_CONNECTED)) {
if (p_lcb->link_flush_tout != flush_tout) {
p_lcb->link_flush_tout = flush_tout;
VLOG(1) << __func__ << " BDA: " << p_lcb->remote_bd_addr << " "
<< flush_tout << "ms";
btsnd_hcic_write_auto_flush_tout(p_lcb->handle, hci_flush_to);
}
}
}
}
return (true);
}
/*******************************************************************************
*
* Function L2CA_GetPeerFeatures
*
* Description Get a peers features and fixed channel map
*
* Parameters: BD address of the peer
* Pointers to features and channel mask storage area
*
* Return value: true if peer is connected
*
******************************************************************************/
bool L2CA_GetPeerFeatures(const RawAddress& bd_addr, uint32_t* p_ext_feat,
uint8_t* p_chnl_mask) {
tL2C_LCB* p_lcb;
/* We must already have a link to the remote */
p_lcb = l2cu_find_lcb_by_bd_addr(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_lcb == NULL) {
LOG(WARNING) << __func__ << " No BDA: " << bd_addr;
return false;
}
VLOG(1) << __func__ << " BDA: " << bd_addr
<< StringPrintf(" ExtFea: 0x%08x Chnl_Mask[0]: 0x%02x",
p_lcb->peer_ext_fea, p_lcb->peer_chnl_mask[0]);
*p_ext_feat = p_lcb->peer_ext_fea;
memcpy(p_chnl_mask, p_lcb->peer_chnl_mask, L2CAP_FIXED_CHNL_ARRAY_SIZE);
return true;
}
/*******************************************************************************
*
* Function L2CA_GetBDAddrbyHandle
*
* Description Get BD address for the given HCI handle
*
* Parameters: HCI handle
* BD address of the peer
*
* Return value: true if found lcb for the given handle, false otherwise
*
******************************************************************************/
bool L2CA_GetBDAddrbyHandle(uint16_t handle, RawAddress& bd_addr) {
tL2C_LCB* p_lcb = NULL;
bool found_dev = false;
p_lcb = l2cu_find_lcb_by_handle(handle);
if (p_lcb) {
found_dev = true;
bd_addr = p_lcb->remote_bd_addr;
}
return found_dev;
}
/*******************************************************************************
*
* Function L2CA_GetChnlFcrMode
*
* Description Get the channel FCR mode
*
* Parameters: Local CID
*
* Return value: Channel mode
*
******************************************************************************/
uint8_t L2CA_GetChnlFcrMode(uint16_t lcid) {
tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
if (p_ccb) {
L2CAP_TRACE_API("L2CA_GetChnlFcrMode() returns mode %d",
p_ccb->peer_cfg.fcr.mode);
return (p_ccb->peer_cfg.fcr.mode);
}
L2CAP_TRACE_API("L2CA_GetChnlFcrMode() returns mode L2CAP_FCR_BASIC_MODE");
return (L2CAP_FCR_BASIC_MODE);
}
#if (L2CAP_NUM_FIXED_CHNLS > 0)
/*******************************************************************************
*
* Function L2CA_RegisterFixedChannel
*
* Description Register a fixed channel.
*
* Parameters: Fixed Channel #
* Channel Callbacks and config
*
* Return value: -
*
******************************************************************************/
bool L2CA_RegisterFixedChannel(uint16_t fixed_cid,
tL2CAP_FIXED_CHNL_REG* p_freg) {
if ((fixed_cid < L2CAP_FIRST_FIXED_CHNL) ||
(fixed_cid > L2CAP_LAST_FIXED_CHNL)) {
L2CAP_TRACE_ERROR("L2CA_RegisterFixedChannel() Invalid CID: 0x%04x",
fixed_cid);
return (false);
}
l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = *p_freg;
return (true);
}
/*******************************************************************************
*
* Function L2CA_ConnectFixedChnl
*
* Description Connect an fixed signalling channel to a remote device.
*
* Parameters: Fixed CID
* BD Address of remote
*
* Return value: true if connection started
*
******************************************************************************/
bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda) {
uint8_t phy = controller_get_interface()->get_le_all_initiating_phys();
return L2CA_ConnectFixedChnl(fixed_cid, rem_bda, phy);
}
bool L2CA_ConnectFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda,
uint8_t initiating_phys) {
tL2C_LCB* p_lcb;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VLOG(1) << __func__ << " BDA: " << rem_bda
<< StringPrintf("CID: 0x%04x ", fixed_cid);
// Check CID is valid and registered
if ((fixed_cid < L2CAP_FIRST_FIXED_CHNL) ||
(fixed_cid > L2CAP_LAST_FIXED_CHNL) ||
(l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb ==
NULL)) {
L2CAP_TRACE_ERROR("%s() Invalid CID: 0x%04x", __func__, fixed_cid);
return (false);
}
// Fail if BT is not yet up
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("%s(0x%04x) - BTU not ready", __func__, fixed_cid);
return (false);
}
if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
transport = BT_TRANSPORT_LE;
tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
// If we already have a link to the remote, check if it supports that CID
p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, transport);
if (p_lcb != NULL) {
// Fixed channels are mandatory on LE transports so ignore the received
// channel mask and use the locally cached LE channel mask.
if (transport == BT_TRANSPORT_LE)
peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
else
peer_channel_mask = p_lcb->peer_chnl_mask[0];
// Check for supported channel
if (!(peer_channel_mask & (1 << fixed_cid))) {
VLOG(2) << __func__ << " BDA " << rem_bda
<< StringPrintf(" CID:0x%04x not supported", fixed_cid);
return false;
}
// Get a CCB and link the lcb to it
if (!l2cu_initialize_fixed_ccb(
p_lcb, fixed_cid,
&l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL]
.fixed_chnl_opts)) {
L2CAP_TRACE_WARNING("%s(0x%04x) - LCB but no CCB", __func__, fixed_cid);
return false;
}
// racing with disconnecting, queue the connection request
if (p_lcb->link_state == LST_DISCONNECTING) {
L2CAP_TRACE_DEBUG("$s() - link disconnecting: RETRY LATER", __func__);
/* Save ccb so it can be started after disconnect is finished */
p_lcb->p_pending_ccb =
p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
return true;
}
(*l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedConn_Cb)(
fixed_cid, p_lcb->remote_bd_addr, true, 0, p_lcb->transport);
return true;
}
// No link. Get an LCB and start link establishment
p_lcb = l2cu_allocate_lcb(rem_bda, false, transport);
if (p_lcb == NULL) {
L2CAP_TRACE_WARNING("%s(0x%04x) - no LCB", __func__, fixed_cid);
return false;
}
// Get a CCB and link the lcb to it
if (!l2cu_initialize_fixed_ccb(
p_lcb, fixed_cid, &l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL]
.fixed_chnl_opts)) {
p_lcb->disc_reason = L2CAP_CONN_NO_RESOURCES;
L2CAP_TRACE_WARNING("%s(0x%04x) - no CCB", __func__, fixed_cid);
l2cu_release_lcb(p_lcb);
return false;
}
if (!l2cu_create_conn(p_lcb, transport, initiating_phys)) {
L2CAP_TRACE_WARNING("%s() - create_conn failed", __func__);
l2cu_release_lcb(p_lcb);
return false;
}
return true;
}
/*******************************************************************************
*
* Function L2CA_SendFixedChnlData
*
* Description Write data on a fixed channel.
*
* Parameters: Fixed CID
* BD Address of remote
* Pointer to buffer of type BT_HDR
*
* Return value L2CAP_DW_SUCCESS, if data accepted
* L2CAP_DW_FAILED, if error
*
******************************************************************************/
uint16_t L2CA_SendFixedChnlData(uint16_t fixed_cid, const RawAddress& rem_bda,
BT_HDR* p_buf) {
tL2C_LCB* p_lcb;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
VLOG(2) << __func__ << " BDA: " << rem_bda
<< StringPrintf(" CID: 0x%04x", fixed_cid);
if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
transport = BT_TRANSPORT_LE;
// Check CID is valid and registered
if ((fixed_cid < L2CAP_FIRST_FIXED_CHNL) ||
(fixed_cid > L2CAP_LAST_FIXED_CHNL) ||
(l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb ==
NULL)) {
L2CAP_TRACE_ERROR("L2CA_SendFixedChnlData() Invalid CID: 0x%04x",
fixed_cid);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
// Fail if BT is not yet up
if (!BTM_IsDeviceUp()) {
L2CAP_TRACE_WARNING("L2CA_SendFixedChnlData(0x%04x) - BTU not ready",
fixed_cid);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
// We need to have a link up
p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, transport);
if (p_lcb == NULL || p_lcb->link_state == LST_DISCONNECTING) {
/* if link is disconnecting, also report data sending failure */
L2CAP_TRACE_WARNING("L2CA_SendFixedChnlData(0x%04x) - no LCB", fixed_cid);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
tL2C_BLE_FIXED_CHNLS_MASK peer_channel_mask;
// Select peer channels mask to use depending on transport
if (transport == BT_TRANSPORT_LE)
peer_channel_mask = l2cb.l2c_ble_fixed_chnls_mask;
else
peer_channel_mask = p_lcb->peer_chnl_mask[0];
if ((peer_channel_mask & (1 << fixed_cid)) == 0) {
L2CAP_TRACE_WARNING(
"L2CA_SendFixedChnlData() - peer does not support fixed chnl: 0x%04x",
fixed_cid);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
p_buf->event = 0;
p_buf->layer_specific = L2CAP_FLUSHABLE_CH_BASED;
if (!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]) {
if (!l2cu_initialize_fixed_ccb(
p_lcb, fixed_cid,
&l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL]
.fixed_chnl_opts)) {
L2CAP_TRACE_WARNING("L2CA_SendFixedChnlData() - no CCB for chnl: 0x%4x",
fixed_cid);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
}
// If already congested, do not accept any more packets
if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent) {
L2CAP_TRACE_ERROR(
"L2CAP - CID: 0x%04x cannot send, already congested \
xmit_hold_q.count: %u buff_quota: %u",
fixed_cid, fixed_queue_length(
p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]
->xmit_hold_q),
p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->buff_quota);
osi_free(p_buf);
return (L2CAP_DW_FAILED);
}
l2c_enqueue_peer_data(p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL],
p_buf);
l2c_link_check_send_pkts(p_lcb, NULL, NULL);
// If there is no dynamic CCB on the link, restart the idle timer each time
// something is sent
if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED &&
!p_lcb->ccb_queue.p_first_ccb) {
l2cu_no_dynamic_ccbs(p_lcb);
}
if (p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]->cong_sent)
return (L2CAP_DW_CONGESTED);
return (L2CAP_DW_SUCCESS);
}
/*******************************************************************************
*
* Function L2CA_RemoveFixedChnl
*
* Description Remove a fixed channel to a remote device.
*
* Parameters: Fixed CID
* BD Address of remote
* Idle timeout to use (or 0xFFFF if don't care)
*
* Return value: true if channel removed
*
******************************************************************************/
bool L2CA_RemoveFixedChnl(uint16_t fixed_cid, const RawAddress& rem_bda) {
tL2C_LCB* p_lcb;
tL2C_CCB* p_ccb;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
/* Check CID is valid and registered */
if ((fixed_cid < L2CAP_FIRST_FIXED_CHNL) ||
(fixed_cid > L2CAP_LAST_FIXED_CHNL) ||
(l2cb.fixed_reg[fixed_cid - L2CAP_FIRST_FIXED_CHNL].pL2CA_FixedData_Cb ==
NULL)) {
L2CAP_TRACE_ERROR("L2CA_RemoveFixedChnl() Invalid CID: 0x%04x", fixed_cid);
return (false);
}
if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
transport = BT_TRANSPORT_LE;
/* Is a fixed channel connected to the remote BDA ?*/
p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, transport);
if (((p_lcb) == NULL) ||
(!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL])) {
LOG(WARNING) << __func__ << " BDA: " << rem_bda
<< StringPrintf(" CID: 0x%04x not connected", fixed_cid);
return (false);
}
VLOG(2) << __func__ << " BDA: " << rem_bda
<< StringPrintf(" CID: 0x%04x", fixed_cid);
/* Release the CCB, starting an inactivity timeout on the LCB if no other CCBs
* exist */
p_ccb = p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL];
p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL] = NULL;
p_lcb->disc_reason = HCI_ERR_CONN_CAUSE_LOCAL_HOST;
// Retain the link for a few more seconds after SMP pairing is done, since
// the Android platform always does service discovery after pairing is
// complete. This will avoid the link down (pairing is complete) and an
// immediate re-connection for service discovery.
// Some devices do not do auto advertising when link is dropped, thus fail
// the second connection and service discovery.
if ((fixed_cid == L2CAP_ATT_CID) && !p_lcb->ccb_queue.p_first_ccb)
p_lcb->idle_timeout = 0;
l2cu_release_ccb(p_ccb);
return (true);
}
/*******************************************************************************
*
* Function L2CA_SetFixedChannelTout
*
* Description Higher layers call this function to set the idle timeout for
* a fixed channel. The "idle timeout" is the amount of time
* that a connection can remain up with no L2CAP channels on
* it. A timeout of zero means that the connection will be torn
* down immediately when the last channel is removed.
* A timeout of 0xFFFF means no timeout. Values are in seconds.
* A bd_addr is the remote BD address.
*
* Returns true if command succeeded, false if failed
*
******************************************************************************/
bool L2CA_SetFixedChannelTout(const RawAddress& rem_bda, uint16_t fixed_cid,
uint16_t idle_tout) {
tL2C_LCB* p_lcb;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
if (fixed_cid >= L2CAP_ATT_CID && fixed_cid <= L2CAP_SMP_CID)
transport = BT_TRANSPORT_LE;
/* Is a fixed channel connected to the remote BDA ?*/
p_lcb = l2cu_find_lcb_by_bd_addr(rem_bda, transport);
if (((p_lcb) == NULL) ||
(!p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL])) {
LOG(WARNING) << __func__ << " BDA: " << rem_bda
<< StringPrintf(" CID: 0x%04x not connected", fixed_cid);
return (false);
}
p_lcb->p_fixed_ccbs[fixed_cid - L2CAP_FIRST_FIXED_CHNL]
->fixed_chnl_idle_tout = idle_tout;
if (p_lcb->in_use && p_lcb->link_state == LST_CONNECTED &&
!p_lcb->ccb_queue.p_first_ccb) {
/* If there are no dynamic CCBs, (re)start the idle timer in case we changed
* it */
l2cu_no_dynamic_ccbs(p_lcb);
}
return true;
}
#endif /* #if (L2CAP_NUM_FIXED_CHNLS > 0) */
/*******************************************************************************
*
* Function L2CA_GetCurrentConfig
*
* Description This function returns configurations of L2CAP channel
* pp_our_cfg : pointer of our saved configuration options
* p_our_cfg_bits : valid config in bitmap
* pp_peer_cfg: pointer of peer's saved configuration options
* p_peer_cfg_bits : valid config in bitmap
*
* Returns true if successful
*
******************************************************************************/
bool L2CA_GetCurrentConfig(uint16_t lcid, tL2CAP_CFG_INFO** pp_our_cfg,
tL2CAP_CH_CFG_BITS* p_our_cfg_bits,
tL2CAP_CFG_INFO** pp_peer_cfg,
tL2CAP_CH_CFG_BITS* p_peer_cfg_bits) {
tL2C_CCB* p_ccb;
L2CAP_TRACE_API("L2CA_GetCurrentConfig() CID: 0x%04x", lcid);
p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
if (p_ccb) {
*pp_our_cfg = &(p_ccb->our_cfg);
/* convert valid config items into bitmap */
*p_our_cfg_bits = 0;
if (p_ccb->our_cfg.mtu_present) *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_MTU;
if (p_ccb->our_cfg.qos_present) *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_QOS;
if (p_ccb->our_cfg.flush_to_present)
*p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FLUSH_TO;
if (p_ccb->our_cfg.fcr_present) *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCR;
if (p_ccb->our_cfg.fcs_present) *p_our_cfg_bits |= L2CAP_CH_CFG_MASK_FCS;
if (p_ccb->our_cfg.ext_flow_spec_present)
*p_our_cfg_bits |= L2CAP_CH_CFG_MASK_EXT_FLOW_SPEC;
*pp_peer_cfg = &(p_ccb->peer_cfg);
*p_peer_cfg_bits = p_ccb->peer_cfg_bits;
return true;
} else {
L2CAP_TRACE_ERROR("No CCB for CID:0x%04x", lcid);
return false;
}
}
/*******************************************************************************
*
* Function L2CA_GetConnectionConfig
*
* Description This function returns configurations of L2CAP channel
* pp_l2c_ccb : pointer to this channels L2CAP ccb data.
*
* Returns true if successful
*
******************************************************************************/
bool L2CA_GetConnectionConfig(uint16_t lcid, uint16_t* mtu, uint16_t* rcid,
uint16_t* handle) {
tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
;
L2CAP_TRACE_API("%s CID: 0x%04x", __func__, lcid);
if (p_ccb) {
*mtu = L2CAP_MTU_SIZE;
if (p_ccb->our_cfg.mtu_present) *mtu = p_ccb->our_cfg.mtu;
*rcid = p_ccb->remote_cid;
*handle = p_ccb->p_lcb->handle;
return true;
}
L2CAP_TRACE_ERROR("%s No CCB for CID:0x%04x", __func__, lcid);
return false;
}
/*******************************************************************************
*
* Function L2CA_RegForNoCPEvt
*
* Description Register callback for Number of Completed Packets event.
*
* Input Param p_cb - callback for Number of completed packets event
* p_bda - BT address of remote device
*
* Returns true if registered OK, else false
*
******************************************************************************/
bool L2CA_RegForNoCPEvt(tL2CA_NOCP_CB* p_cb, const RawAddress& p_bda) {
tL2C_LCB* p_lcb;
/* Find the link that is associated with this remote bdaddr */
p_lcb = l2cu_find_lcb_by_bd_addr(p_bda, BT_TRANSPORT_BR_EDR);
/* If no link for this handle, nothing to do. */
if (!p_lcb) return false;
p_lcb->p_nocp_cb = p_cb;
return true;
}
/*******************************************************************************
*
* Function L2CA_DataWrite
*
* Description Higher layers call this function to write data.
*
* Returns L2CAP_DW_SUCCESS, if data accepted, else false
* L2CAP_DW_CONGESTED, if data accepted and the channel is
* congested
* L2CAP_DW_FAILED, if error
*
******************************************************************************/
uint8_t L2CA_DataWrite(uint16_t cid, BT_HDR* p_data) {
L2CAP_TRACE_API("L2CA_DataWrite() CID: 0x%04x Len: %d", cid, p_data->len);
return l2c_data_write(cid, p_data, L2CAP_FLUSHABLE_CH_BASED);
}
/*******************************************************************************
*
* Function L2CA_SetChnlFlushability
*
* Description Higher layers call this function to set a channels
* flushability flags
*
* Returns true if CID found, else false
*
******************************************************************************/
bool L2CA_SetChnlFlushability(uint16_t cid, bool is_flushable) {
#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
tL2C_CCB* p_ccb;
/* Find the channel control block. We don't know the link it is on. */
p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - no CCB for L2CA_SetChnlFlushability, CID: %d",
cid);
return (false);
}
p_ccb->is_flushable = is_flushable;
L2CAP_TRACE_API("L2CA_SetChnlFlushability() CID: 0x%04x is_flushable: %d",
cid, is_flushable);
#endif
return (true);
}
/*******************************************************************************
*
* Function L2CA_DataWriteEx
*
* Description Higher layers call this function to write data with extended
* flags.
* flags : L2CAP_FLUSHABLE_CH_BASED
* L2CAP_FLUSHABLE_PKT
* L2CAP_NON_FLUSHABLE_PKT
*
* Returns L2CAP_DW_SUCCESS, if data accepted, else false
* L2CAP_DW_CONGESTED, if data accepted and the channel is
* congested
* L2CAP_DW_FAILED, if error
*
******************************************************************************/
uint8_t L2CA_DataWriteEx(uint16_t cid, BT_HDR* p_data, uint16_t flags) {
L2CAP_TRACE_API("L2CA_DataWriteEx() CID: 0x%04x Len: %d Flags:0x%04X", cid,
p_data->len, flags);
return l2c_data_write(cid, p_data, flags);
}
/*******************************************************************************
*
* Function L2CA_FlushChannel
*
* Description This function flushes none, some or all buffers queued up
* for xmission for a particular CID. If called with
* L2CAP_FLUSH_CHANS_GET (0), it simply returns the number
* of buffers queued for that CID L2CAP_FLUSH_CHANS_ALL (0xffff)
* flushes all buffers. All other values specifies the maximum
* buffers to flush.
*
* Returns Number of buffers left queued for that CID
*
******************************************************************************/
uint16_t L2CA_FlushChannel(uint16_t lcid, uint16_t num_to_flush) {
tL2C_CCB* p_ccb;
tL2C_LCB* p_lcb;
uint16_t num_left = 0, num_flushed1 = 0, num_flushed2 = 0;
p_ccb = l2cu_find_ccb_by_cid(NULL, lcid);
if (!p_ccb || (p_ccb->p_lcb == NULL)) {
L2CAP_TRACE_WARNING(
"L2CA_FlushChannel() abnormally returning 0 CID: 0x%04x", lcid);
return (0);
}
p_lcb = p_ccb->p_lcb;
if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
L2CAP_TRACE_API(
"L2CA_FlushChannel (FLUSH) CID: 0x%04x NumToFlush: %d QC: %u "
"pFirst: 0x%08x",
lcid, num_to_flush, fixed_queue_length(p_ccb->xmit_hold_q),
fixed_queue_try_peek_first(p_ccb->xmit_hold_q));
} else {
L2CAP_TRACE_API("L2CA_FlushChannel (QUERY) CID: 0x%04x", lcid);
}
/* Cannot flush eRTM buffers once they have a sequence number */
if (p_ccb->peer_cfg.fcr.mode != L2CAP_FCR_ERTM_MODE) {
#if (L2CAP_NON_FLUSHABLE_PB_INCLUDED == TRUE)
if (num_to_flush != L2CAP_FLUSH_CHANS_GET) {
/* If the controller supports enhanced flush, flush the data queued at the
* controller */
if ((HCI_NON_FLUSHABLE_PB_SUPPORTED(BTM_ReadLocalFeatures())) &&
(BTM_GetNumScoLinks() == 0)) {
if (l2cb.is_flush_active == false) {
l2cb.is_flush_active = true;
/* The only packet type defined - 0 - Automatically-Flushable Only */
btsnd_hcic_enhanced_flush(p_lcb->handle, 0);
}
}
}
#endif
// Iterate though list and flush the amount requested from
// the transmit data queue that satisfy the layer and event conditions.
for (const list_node_t* node = list_begin(p_lcb->link_xmit_data_q);
(num_to_flush > 0) && node != list_end(p_lcb->link_xmit_data_q);) {
BT_HDR* p_buf = (BT_HDR*)list_node(node);
node = list_next(node);
if ((p_buf->layer_specific == 0) && (p_buf->event == lcid)) {
num_to_flush--;
num_flushed1++;
list_remove(p_lcb->link_xmit_data_q, p_buf);
osi_free(p_buf);
}
}
}
/* If needed, flush buffers in the CCB xmit hold queue */
while ((num_to_flush != 0) && (!fixed_queue_is_empty(p_ccb->xmit_hold_q))) {
BT_HDR* p_buf = (BT_HDR*)fixed_queue_try_dequeue(p_ccb->xmit_hold_q);
osi_free(p_buf);
num_to_flush--;
num_flushed2++;
}
/* If app needs to track all packets, call him */
if ((p_ccb->p_rcb) && (p_ccb->p_rcb->api.pL2CA_TxComplete_Cb) &&
(num_flushed2))
(*p_ccb->p_rcb->api.pL2CA_TxComplete_Cb)(p_ccb->local_cid, num_flushed2);
/* Now count how many are left */
for (const list_node_t* node = list_begin(p_lcb->link_xmit_data_q);
node != list_end(p_lcb->link_xmit_data_q); node = list_next(node)) {
BT_HDR* p_buf = (BT_HDR*)list_node(node);
if (p_buf->event == lcid) num_left++;
}
/* Add in the number in the CCB xmit queue */
num_left += fixed_queue_length(p_ccb->xmit_hold_q);
/* Return the local number of buffers left for the CID */
L2CAP_TRACE_DEBUG("L2CA_FlushChannel() flushed: %u + %u, num_left: %u",
num_flushed1, num_flushed2, num_left);
/* If we were congested, and now we are not, tell the app */
l2cu_check_channel_congestion(p_ccb);
return (num_left);
}
/*******************************************************************************
**
** Function L2CA_SetMediaStreamChannel
**
** Description This function is called to set/reset the ccb of active media
** streaming channel
**
** Parameters: local_media_cid: The local cid provided to A2DP to be used
** for streaming
** status: The status of media streaming on this channel
**
** Returns void
**
*******************************************************************************/
void L2CA_SetMediaStreamChannel(uint16_t local_media_cid, bool status)
{
uint16_t i;
int set_channel = -1;
if(status)
{
for(i = 0; i < MAX_ACTIVE_AVDT_CONN; i++)
{
if(!(av_media_channels[i].is_active))
{
set_channel = i;
break;
}
}
if(set_channel < 0) {
L2CAP_TRACE_ERROR("%s: No empty slot found to set media channel", __func__);
return;
}
av_media_channels[set_channel].p_ccb = l2cu_find_ccb_by_cid(NULL, local_media_cid);
if(!av_media_channels[set_channel].p_ccb)
return;
av_media_channels[set_channel].local_cid = local_media_cid;
L2CAP_TRACE_EVENT("%s: Set A2DP media snoop filtering for local_cid: %d, remote_cid: %d",
__func__, local_media_cid, av_media_channels[set_channel].p_ccb->remote_cid);
}
else
{
for(i = 0; i < MAX_ACTIVE_AVDT_CONN; i++)
{
if(av_media_channels[i].is_active && av_media_channels[i].local_cid == local_media_cid)
{
set_channel = i;
break;
}
}
if(set_channel < 0) {
L2CAP_TRACE_ERROR("%s: The channel %d not found in active media channels",
__func__, local_media_cid);
return;
}
L2CAP_TRACE_EVENT("%s: Reset A2DP media snoop filtering for local_cid: %d",
__func__, local_media_cid);
}
av_media_channels[set_channel].is_active = status;
}
/*******************************************************************************
**
** Function L2CA_isMediaChannel
**
** Description This function returns if the channel id passed as parameter
** is an A2DP streaming channel
**
** Parameters: handle: Connection handle with the remote device
** channel_id: Channel ID
** is_local_cid: Signifies if the channel id passed is local
** cid or remote cid (true if local, remote otherwise)
**
** Returns bool
**
*******************************************************************************/
bool L2CA_isMediaChannel(uint16_t handle, uint16_t channel_id, bool is_local_cid)
{
int i;
bool ret = false;
for(i = 0; i < MAX_ACTIVE_AVDT_CONN; i++)
{
if(av_media_channels[i].is_active
&& ((!is_local_cid && channel_id == av_media_channels[i].p_ccb->remote_cid) ||
(is_local_cid && channel_id == av_media_channels[i].p_ccb->local_cid))
&& handle == av_media_channels[i].p_ccb->p_lcb->handle)
{
ret = true;
break;
}
}
return ret;
}
/*******************************************************************************
**
** Function L2CA_ReadData
**
** Description Upper layer should call this api to read data from L2CAP which
** remote has sent. This API should be called on receiving
** pL2CA_DataInd_Cb callback in Enhanced Credit Based Flow Control
** mode.
**
** Parameters: cid: channel id of the local l2cap channel
**
** Returns pointer to SDU data to be given to upper layer.
**
*******************************************************************************/
BT_HDR* L2CA_ReadData (uint16_t cid) {
BT_HDR* p_data = NULL;
tL2C_CCB* p_ccb = l2cu_find_ccb_by_cid(NULL, cid);
if (p_ccb == NULL) {
L2CAP_TRACE_WARNING("L2CAP - CCB not present, CID: %x", cid);
return (NULL);
}
if (fixed_queue_is_empty(p_ccb->rx_buf.rcv_data_q)) {
L2CAP_TRACE_WARNING("L2CAP - No data to fetch from CID: %x Rx queue", cid);
return (NULL);
}
/* mark that upper layer is fetching data properly (is_dequeued flag is set)
if data is fetched after reaching threshold limit. after next timeout,
this flag is checked and credit indication is sent to remote by l2cap */
if (p_ccb->remote_credit_count <= L2CAP_LE_CREDIT_THRESHOLD) {
p_ccb->rx_buf.is_dequeued = true;
}
p_data = (BT_HDR *)fixed_queue_dequeue(p_ccb->rx_buf.rcv_data_q);
return (p_data);
}
|