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
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
|
/******************************************************************************
*
* Copyright 2000-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.
*
******************************************************************************/
/*****************************************************************************
*
* Name: btm_acl.cc
*
* Description: This file contains functions that handle ACL connections.
* This includes operations such as hold and sniff modes,
* supported packet types.
*
* This module contains both internal and external (API)
* functions. External (API) functions are distinguishable
* by their names beginning with uppercase BTM.
*
*
*****************************************************************************/
#define LOG_TAG "btm_acl"
#include <base/logging.h>
#include <cstdint>
#include "bta/include/bta_dm_acl.h"
#include "bta/sys/bta_sys.h"
#include "btif/include/btif_acl.h"
#include "common/metrics.h"
#include "device/include/controller.h"
#include "device/include/interop.h"
#include "include/l2cap_hci_link_interface.h"
#include "main/shim/acl_api.h"
#include "main/shim/btm_api.h"
#include "main/shim/controller.h"
#include "main/shim/dumpsys.h"
#include "main/shim/l2c_api.h"
#include "main/shim/shim.h"
#include "osi/include/allocator.h"
#include "osi/include/log.h"
#include "osi/include/osi.h" // UNUSED_ATTR
#include "stack/acl/acl.h"
#include "stack/acl/peer_packet_types.h"
#include "stack/btm/btm_dev.h"
#include "stack/btm/btm_int_types.h"
#include "stack/btm/btm_sec.h"
#include "stack/btm/security_device_record.h"
#include "stack/gatt/connection_manager.h"
#include "stack/include/acl_api.h"
#include "stack/include/acl_hci_link_interface.h"
#include "stack/include/bt_hdr.h"
#include "stack/include/btm_api.h"
#include "stack/include/btm_iso_api.h"
#include "stack/include/btu.h"
#include "stack/include/hci_error_code.h"
#include "stack/include/l2cap_acl_interface.h"
#include "stack/include/sco_hci_link_interface.h"
#include "types/hci_role.h"
#include "types/raw_address.h"
void BTM_update_version_info(const RawAddress& bd_addr,
const remote_version_info& remote_version_info);
void gatt_find_in_device_record(const RawAddress& bd_addr,
tBLE_BD_ADDR* address_with_type);
void l2c_link_hci_conn_comp(tHCI_STATUS status, uint16_t handle,
const RawAddress& p_bda);
void BTM_db_reset(void);
extern tBTM_CB btm_cb;
struct StackAclBtmAcl {
tACL_CONN* acl_allocate_connection();
tACL_CONN* acl_get_connection_from_handle(uint16_t handle);
tACL_CONN* btm_bda_to_acl(const RawAddress& bda, tBT_TRANSPORT transport);
bool change_connection_packet_types(tACL_CONN& link,
const uint16_t new_packet_type_bitmask);
void btm_establish_continue(tACL_CONN* p_acl_cb);
void btm_set_default_link_policy(tLINK_POLICY settings);
void btm_acl_role_changed(tHCI_STATUS hci_status, const RawAddress& bd_addr,
tHCI_ROLE new_role);
void hci_start_role_switch_to_central(tACL_CONN& p_acl);
void set_default_packet_types_supported(uint16_t packet_types_supported) {
btm_cb.acl_cb_.btm_acl_pkt_types_supported = packet_types_supported;
}
};
struct RoleChangeView {
tHCI_ROLE new_role = HCI_ROLE_UNKNOWN;
RawAddress bd_addr;
};
namespace {
StackAclBtmAcl internal_;
std::unique_ptr<RoleChangeView> delayed_role_change_ = nullptr;
const bluetooth::legacy::hci::Interface& GetLegacyHciInterface() {
return bluetooth::legacy::hci::GetInterface();
}
}
typedef struct {
uint16_t handle;
uint16_t hci_len;
} __attribute__((packed)) acl_header_t;
constexpr uint8_t BTM_MAX_SW_ROLE_FAILED_ATTEMPTS = 3;
/* Define masks for supported and exception 2.0 ACL packet types
*/
constexpr uint16_t BTM_ACL_SUPPORTED_PKTS_MASK =
(HCI_PKT_TYPES_MASK_DM1 | HCI_PKT_TYPES_MASK_DH1 | HCI_PKT_TYPES_MASK_DM3 |
HCI_PKT_TYPES_MASK_DH3 | HCI_PKT_TYPES_MASK_DM5 | HCI_PKT_TYPES_MASK_DH5);
constexpr uint16_t BTM_ACL_EXCEPTION_PKTS_MASK =
(HCI_PKT_TYPES_MASK_NO_2_DH1 | HCI_PKT_TYPES_MASK_NO_3_DH1 |
HCI_PKT_TYPES_MASK_NO_2_DH3 | HCI_PKT_TYPES_MASK_NO_3_DH3 |
HCI_PKT_TYPES_MASK_NO_2_DH5 | HCI_PKT_TYPES_MASK_NO_3_DH5);
inline bool IsEprAvailable(const tACL_CONN& p_acl) {
if (!p_acl.peer_lmp_feature_valid[0]) {
LOG_WARN("Checking incomplete feature page read");
return false;
}
return HCI_ATOMIC_ENCRYPT_SUPPORTED(p_acl.peer_lmp_feature_pages[0]) &&
controller_get_interface()->supports_encryption_pause();
}
static void btm_process_remote_ext_features(tACL_CONN* p_acl_cb,
uint8_t max_page_number);
static void btm_read_failed_contact_counter_timeout(void* data);
static void btm_read_remote_ext_features(uint16_t handle, uint8_t page_number);
static void btm_read_rssi_timeout(void* data);
static void btm_read_tx_power_timeout(void* data);
static void check_link_policy(tLINK_POLICY* settings);
void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy);
namespace {
void NotifyAclLinkUp(tACL_CONN& p_acl) {
if (p_acl.link_up_issued) {
LOG_INFO("Already notified BTA layer that the link is up");
return;
}
p_acl.link_up_issued = true;
BTA_dm_acl_up(p_acl.remote_addr, p_acl.transport);
}
void NotifyAclLinkDown(tACL_CONN& p_acl) {
/* Only notify if link up has had a chance to be issued */
if (p_acl.link_up_issued) {
p_acl.link_up_issued = false;
BTA_dm_acl_down(p_acl.remote_addr, p_acl.transport);
}
}
void NotifyAclRoleSwitchComplete(const RawAddress& bda, tHCI_ROLE new_role,
tHCI_STATUS hci_status) {
BTA_dm_report_role_change(bda, new_role, hci_status);
}
void NotifyAclFeaturesReadComplete(tACL_CONN& p_acl,
UNUSED_ATTR uint8_t max_page_number) {
btm_process_remote_ext_features(&p_acl, max_page_number);
btm_set_link_policy(&p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
BTA_dm_notify_remote_features_complete(p_acl.remote_addr);
}
} // namespace
static void hci_btsnd_hcic_disconnect(tACL_CONN& p_acl, tHCI_STATUS reason,
std::string comment) {
LOG_INFO("Disconnecting peer:%s reason:%s comment:%s",
PRIVATE_ADDRESS(p_acl.remote_addr),
hci_error_code_text(reason).c_str(), comment.c_str());
p_acl.disconnect_reason = reason;
return bluetooth::shim::ACL_Disconnect(
p_acl.hci_handle, p_acl.is_transport_br_edr(), reason, comment);
}
void StackAclBtmAcl::hci_start_role_switch_to_central(tACL_CONN& p_acl) {
GetLegacyHciInterface().StartRoleSwitch(
p_acl.remote_addr, static_cast<uint8_t>(HCI_ROLE_CENTRAL));
p_acl.set_switch_role_in_progress();
p_acl.rs_disc_pending = BTM_SEC_RS_PENDING;
}
void hci_btm_set_link_supervision_timeout(tACL_CONN& link, uint16_t timeout) {
if (link.link_role != HCI_ROLE_CENTRAL) {
/* Only send if current role is Central; 2.0 spec requires this */
LOG_WARN("Can only set link supervision timeout if central role:%s",
RoleText(link.link_role).c_str());
return;
}
if (!bluetooth::shim::
controller_is_write_link_supervision_timeout_supported()) {
LOG_WARN(
"UNSUPPORTED by controller write link supervision timeout:%.2fms "
"bd_addr:%s",
supervision_timeout_to_seconds(timeout),
PRIVATE_ADDRESS(link.RemoteAddress()));
return;
}
LOG_DEBUG("Setting link supervision timeout:%.2fs peer:%s",
double(timeout) * 0.01, PRIVATE_ADDRESS(link.RemoteAddress()));
link.link_super_tout = timeout;
btsnd_hcic_write_link_super_tout(link.Handle(), timeout);
}
/* 3 seconds timeout waiting for responses */
#define BTM_DEV_REPLY_TIMEOUT_MS (3 * 1000)
void BTM_acl_after_controller_started(const controller_t* controller) {
internal_.btm_set_default_link_policy(
HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH | HCI_ENABLE_HOLD_MODE |
HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_PARK_MODE);
/* Create ACL supported packet types mask */
uint16_t btm_acl_pkt_types_supported =
(HCI_PKT_TYPES_MASK_DH1 + HCI_PKT_TYPES_MASK_DM1);
if (controller->supports_3_slot_packets())
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_DH3 + HCI_PKT_TYPES_MASK_DM3);
if (controller->supports_5_slot_packets())
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_DH5 + HCI_PKT_TYPES_MASK_DM5);
/* Add in EDR related ACL types */
if (!controller->supports_classic_2m_phy()) {
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_NO_2_DH1 + HCI_PKT_TYPES_MASK_NO_2_DH3 +
HCI_PKT_TYPES_MASK_NO_2_DH5);
}
if (!controller->supports_classic_3m_phy()) {
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_NO_3_DH1 + HCI_PKT_TYPES_MASK_NO_3_DH3 +
HCI_PKT_TYPES_MASK_NO_3_DH5);
}
/* Check to see if 3 and 5 slot packets are available */
if (controller->supports_classic_2m_phy() ||
controller->supports_classic_3m_phy()) {
if (!controller->supports_3_slot_edr_packets())
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_NO_2_DH3 + HCI_PKT_TYPES_MASK_NO_3_DH3);
if (!controller->supports_5_slot_edr_packets())
btm_acl_pkt_types_supported |=
(HCI_PKT_TYPES_MASK_NO_2_DH5 + HCI_PKT_TYPES_MASK_NO_3_DH5);
}
internal_.set_default_packet_types_supported(btm_acl_pkt_types_supported);
}
/*******************************************************************************
*
* Function btm_bda_to_acl
*
* Description This function returns the FIRST acl_db entry for the passed
* BDA.
*
* Parameters bda : BD address of the remote device
* transport : Physical transport used for ACL connection
* (BR/EDR or LE)
*
* Returns Returns pointer to the ACL DB for the requested BDA if found.
* nullptr if not found.
*
******************************************************************************/
tACL_CONN* StackAclBtmAcl::btm_bda_to_acl(const RawAddress& bda,
tBT_TRANSPORT transport) {
tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
for (uint8_t index = 0; index < MAX_L2CAP_LINKS; index++, p_acl++) {
if ((p_acl->in_use) && p_acl->remote_addr == bda &&
p_acl->transport == transport) {
return p_acl;
}
}
return nullptr;
}
tACL_CONN* acl_get_connection_from_address(const RawAddress& bd_addr,
tBT_TRANSPORT transport) {
return internal_.btm_bda_to_acl(bd_addr, transport);
}
/*******************************************************************************
*
* Function btm_handle_to_acl_index
*
* Description This function returns the FIRST acl_db entry for the passed
* hci_handle.
*
* Returns index to the acl_db or MAX_L2CAP_LINKS.
*
******************************************************************************/
uint8_t btm_handle_to_acl_index(uint16_t hci_handle) {
tACL_CONN* p = &btm_cb.acl_cb_.acl_db[0];
uint8_t xx;
for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
if ((p->in_use) && (p->hci_handle == hci_handle)) {
break;
}
}
/* If here, no BD Addr found */
return (xx);
}
tACL_CONN* StackAclBtmAcl::acl_get_connection_from_handle(uint16_t hci_handle) {
uint8_t index = btm_handle_to_acl_index(hci_handle);
if (index >= MAX_L2CAP_LINKS) return nullptr;
return &btm_cb.acl_cb_.acl_db[index];
}
tACL_CONN* acl_get_connection_from_handle(uint16_t handle) {
return internal_.acl_get_connection_from_handle(handle);
}
void btm_acl_process_sca_cmpl_pkt(uint8_t len, uint8_t* data) {
uint16_t handle;
uint8_t sca;
uint8_t status;
STREAM_TO_UINT8(status, data);
if (status != HCI_SUCCESS) {
LOG_WARN("Peer SCA Command complete failed:%s",
hci_error_code_text(static_cast<tHCI_STATUS>(status)).c_str());
return;
}
STREAM_TO_UINT16(handle, data);
STREAM_TO_UINT8(sca, data);
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
p_acl->sca = sca;
}
tACL_CONN* StackAclBtmAcl::acl_allocate_connection() {
tACL_CONN* p_acl = &btm_cb.acl_cb_.acl_db[0];
for (uint8_t xx = 0; xx < MAX_L2CAP_LINKS; xx++, p_acl++) {
if (!p_acl->in_use) {
return p_acl;
}
}
return nullptr;
}
void btm_acl_created(const RawAddress& bda, uint16_t hci_handle,
tHCI_ROLE link_role, tBT_TRANSPORT transport) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
if (p_acl != (tACL_CONN*)NULL) {
p_acl->hci_handle = hci_handle;
p_acl->link_role = link_role;
p_acl->transport = transport;
if (transport == BT_TRANSPORT_BR_EDR) {
btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
}
LOG_WARN(
"Unable to create duplicate acl when one already exists handle:%hu"
" role:%s transport:%s",
hci_handle, RoleText(link_role).c_str(),
bt_transport_text(transport).c_str());
return;
}
p_acl = internal_.acl_allocate_connection();
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
p_acl->in_use = true;
p_acl->hci_handle = hci_handle;
p_acl->link_role = link_role;
p_acl->link_up_issued = false;
p_acl->remote_addr = bda;
p_acl->sca = 0xFF;
p_acl->transport = transport;
p_acl->switch_role_failed_attempts = 0;
p_acl->reset_switch_role();
BTM_PM_OnConnected(hci_handle, bda);
LOG_DEBUG(
"Created new ACL connection peer:%s role:%s handle:0x%04x transport:%s",
PRIVATE_ADDRESS(bda), RoleText(p_acl->link_role).c_str(), hci_handle,
bt_transport_text(transport).c_str());
if (transport == BT_TRANSPORT_BR_EDR) {
btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
}
if (transport == BT_TRANSPORT_LE) {
btm_ble_refresh_local_resolvable_private_addr(
bda, btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr);
}
/* if BR/EDR do something more */
if (transport == BT_TRANSPORT_BR_EDR) {
btsnd_hcic_read_rmt_clk_offset(hci_handle);
}
if (transport == BT_TRANSPORT_LE) {
btm_ble_get_acl_remote_addr(hci_handle, p_acl->active_remote_addr,
&p_acl->active_remote_addr_type);
if (controller_get_interface()
->supports_ble_peripheral_initiated_feature_exchange() ||
link_role == HCI_ROLE_CENTRAL) {
btsnd_hcic_ble_read_remote_feat(p_acl->hci_handle);
} else {
internal_.btm_establish_continue(p_acl);
}
}
}
void btm_acl_update_conn_addr(uint16_t handle, const RawAddress& address) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
p_acl->conn_addr = address;
}
void btm_configure_data_path(uint8_t direction, uint8_t path_id,
std::vector<uint8_t> vendor_config) {
if (direction != btm_data_direction::CONTROLLER_TO_HOST &&
direction != btm_data_direction::HOST_TO_CONTROLLER) {
LOG_WARN("Unknown data direction");
return;
}
btsnd_hcic_configure_data_path(direction, path_id, vendor_config);
}
/*******************************************************************************
*
* Function btm_acl_removed
*
* Description This function is called by L2CAP when an ACL connection
* is removed. Since only L2CAP creates ACL links, we use
* the L2CAP link index as our index into the control blocks.
*
* Returns void
*
******************************************************************************/
void btm_acl_removed(uint16_t handle) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
p_acl->in_use = false;
NotifyAclLinkDown(*p_acl);
p_acl->Reset();
BTM_PM_OnDisconnected(handle);
}
/*******************************************************************************
*
* Function btm_acl_device_down
*
* Description This function is called when the local device is deemed
* to be down. It notifies L2CAP of the failure.
*
* Returns void
*
******************************************************************************/
void btm_acl_device_down(void) {
tACL_CONN* p = &btm_cb.acl_cb_.acl_db[0];
uint16_t xx;
for (xx = 0; xx < MAX_L2CAP_LINKS; xx++, p++) {
if (p->in_use) {
l2c_link_hci_disc_comp(p->hci_handle, HCI_ERR_HW_FAILURE);
}
}
BTM_db_reset();
}
void btm_acl_set_paging(bool value) { btm_cb.is_paging = value; }
void btm_acl_update_inquiry_status(uint8_t status) {
btm_cb.is_inquiry = status == BTM_INQUIRY_STARTED;
BTIF_dm_report_inquiry_status_change(status);
}
tBTM_STATUS BTM_GetRole(const RawAddress& remote_bd_addr, tHCI_ROLE* p_role) {
if (p_role == nullptr) {
return BTM_ILLEGAL_VALUE;
}
*p_role = HCI_ROLE_UNKNOWN;
tACL_CONN* p_acl =
internal_.btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return BTM_UNKNOWN_ADDR;
}
*p_role = p_acl->link_role;
return BTM_SUCCESS;
}
/*******************************************************************************
*
* Function BTM_SwitchRoleToCentral
*
* Description This function is called to switch role between central and
* peripheral. If role is already set it will do nothing.
*
* Returns BTM_SUCCESS if already in specified role.
* BTM_CMD_STARTED if command issued to controller.
* BTM_NO_RESOURCES if couldn't allocate memory to issue
* command
* BTM_UNKNOWN_ADDR if no active link with bd addr specified
* BTM_MODE_UNSUPPORTED if local device does not support role
* switching
* BTM_BUSY if the previous command is not completed
*
******************************************************************************/
tBTM_STATUS BTM_SwitchRoleToCentral(const RawAddress& remote_bd_addr) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
bluetooth::shim::L2CA_SwitchRoleToCentral(remote_bd_addr);
return BTM_SUCCESS;
}
if (!controller_get_interface()->supports_central_peripheral_role_switch()) {
LOG_INFO("Local controller does not support role switching");
return BTM_MODE_UNSUPPORTED;
}
tACL_CONN* p_acl =
internal_.btm_bda_to_acl(remote_bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return BTM_UNKNOWN_ADDR;
}
if (p_acl->link_role == HCI_ROLE_CENTRAL) {
LOG_INFO("Requested role is already in effect");
return BTM_SUCCESS;
}
if (interop_match_addr(INTEROP_DISABLE_ROLE_SWITCH, &remote_bd_addr)) {
LOG_INFO("Remote device is on list preventing role switch");
return BTM_DEV_RESTRICT_LISTED;
}
if (BTM_IsScoActiveByBdaddr(remote_bd_addr)) {
LOG_INFO("An active SCO to device prevents role switch at this time");
return BTM_NO_RESOURCES;
}
if (!p_acl->is_switch_role_idle()) {
LOG_INFO("Role switch is already progress");
return BTM_BUSY;
}
if (interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &remote_bd_addr)) {
LOG_DEBUG("Device restrict listed under INTEROP_DYNAMIC_ROLE_SWITCH");
return BTM_DEV_RESTRICT_LISTED;
}
tBTM_PM_MODE pwr_mode;
if (!BTM_ReadPowerMode(p_acl->remote_addr, &pwr_mode)) {
LOG_WARN(
"Unable to find device to read current power mode prior to role "
"switch");
return BTM_UNKNOWN_ADDR;
};
if (pwr_mode == BTM_PM_MD_PARK || pwr_mode == BTM_PM_MD_SNIFF) {
if (!BTM_SetLinkPolicyActiveMode(p_acl->remote_addr)) {
LOG_WARN("Unable to set link policy active before attempting switch");
return BTM_WRONG_MODE;
}
p_acl->set_switch_role_changing();
}
/* some devices do not support switch while encryption is on */
else {
if (p_acl->is_encrypted && !IsEprAvailable(*p_acl)) {
/* bypass turning off encryption if change link key is already doing it */
p_acl->set_encryption_off();
p_acl->set_switch_role_encryption_off();
} else {
internal_.hci_start_role_switch_to_central(*p_acl);
}
}
return BTM_CMD_STARTED;
}
/*******************************************************************************
*
* Function btm_acl_encrypt_change
*
* Description This function is when encryption of the connection is
* completed by the LM. Checks to see if a role switch or
* change of link key was active and initiates or continues
* process if needed.
*
* Returns void
*
******************************************************************************/
void btm_acl_encrypt_change(uint16_t handle, uint8_t status,
uint8_t encr_enable) {
tACL_CONN* p = internal_.acl_get_connection_from_handle(handle);
if (p == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
p->is_encrypted = encr_enable;
/* Process Role Switch if active */
if (p->is_switch_role_encryption_off()) {
/* if encryption turn off failed we still will try to switch role */
if (encr_enable) {
p->set_encryption_idle();
p->reset_switch_role();
} else {
p->set_encryption_switching();
p->set_switch_role_switching();
}
internal_.hci_start_role_switch_to_central(*p);
}
/* Finished enabling Encryption after role switch */
else if (p->is_switch_role_encryption_on()) {
p->reset_switch_role();
p->set_encryption_idle();
NotifyAclRoleSwitchComplete(
btm_cb.acl_cb_.switch_role_ref_data.remote_bd_addr,
btm_cb.acl_cb_.switch_role_ref_data.role,
btm_cb.acl_cb_.switch_role_ref_data.hci_status);
/* If a disconnect is pending, issue it now that role switch has completed
*/
if (p->rs_disc_pending == BTM_SEC_DISC_PENDING) {
hci_btsnd_hcic_disconnect(
*p, HCI_ERR_PEER_USER,
"stack::acl::btm_acl::encrypt after role switch");
}
p->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
}
}
static void check_link_policy(tLINK_POLICY* settings) {
const controller_t* controller = controller_get_interface();
if ((*settings & HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH) &&
(!controller->supports_role_switch())) {
*settings &= (~HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
LOG_INFO("Role switch not supported (settings: 0x%04x)", *settings);
}
if ((*settings & HCI_ENABLE_HOLD_MODE) &&
(!controller->supports_hold_mode())) {
*settings &= (~HCI_ENABLE_HOLD_MODE);
LOG_INFO("hold not supported (settings: 0x%04x)", *settings);
}
if ((*settings & HCI_ENABLE_SNIFF_MODE) &&
(!controller->supports_sniff_mode())) {
*settings &= (~HCI_ENABLE_SNIFF_MODE);
LOG_INFO("sniff not supported (settings: 0x%04x)", *settings);
}
if ((*settings & HCI_ENABLE_PARK_MODE) &&
(!controller->supports_park_mode())) {
*settings &= (~HCI_ENABLE_PARK_MODE);
LOG_INFO("park not supported (settings: 0x%04x)", *settings);
}
}
void btm_set_link_policy(tACL_CONN* conn, tLINK_POLICY policy) {
conn->link_policy = policy;
check_link_policy(&conn->link_policy);
if ((conn->link_policy & HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH) &&
interop_match_addr(INTEROP_DISABLE_SNIFF, &(conn->remote_addr))) {
conn->link_policy &= (~HCI_ENABLE_SNIFF_MODE);
}
btsnd_hcic_write_policy_set(conn->hci_handle,
static_cast<uint16_t>(conn->link_policy));
}
static void btm_toggle_policy_on_for(const RawAddress& peer_addr,
uint16_t flag) {
auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
if (!conn) {
LOG_WARN("Unable to find active acl");
return;
}
btm_set_link_policy(conn, conn->link_policy | flag);
}
static void btm_toggle_policy_off_for(const RawAddress& peer_addr,
uint16_t flag) {
auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
if (!conn) {
LOG_WARN("Unable to find active acl");
return;
}
btm_set_link_policy(conn, conn->link_policy & ~flag);
}
bool BTM_is_sniff_allowed_for(const RawAddress& peer_addr) {
auto conn = internal_.btm_bda_to_acl(peer_addr, BT_TRANSPORT_BR_EDR);
if (!conn) {
LOG_WARN("Unable to find active acl");
return false;
}
return conn->link_policy & HCI_ENABLE_SNIFF_MODE;
}
void BTM_unblock_sniff_mode_for(const RawAddress& peer_addr) {
btm_toggle_policy_on_for(peer_addr, HCI_ENABLE_SNIFF_MODE);
}
void BTM_block_sniff_mode_for(const RawAddress& peer_addr) {
btm_toggle_policy_off_for(peer_addr, HCI_ENABLE_SNIFF_MODE);
}
void BTM_unblock_role_switch_for(const RawAddress& peer_addr) {
btm_toggle_policy_on_for(peer_addr, HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
void BTM_block_role_switch_for(const RawAddress& peer_addr) {
btm_toggle_policy_off_for(peer_addr, HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
void BTM_unblock_role_switch_and_sniff_mode_for(const RawAddress& peer_addr) {
btm_toggle_policy_on_for(
peer_addr, HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
void BTM_block_role_switch_and_sniff_mode_for(const RawAddress& peer_addr) {
btm_toggle_policy_off_for(
peer_addr, HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
void StackAclBtmAcl::btm_set_default_link_policy(tLINK_POLICY settings) {
check_link_policy(&settings);
btm_cb.acl_cb_.btm_def_link_policy = settings;
btsnd_hcic_write_def_policy_set(settings);
}
void BTM_default_unblock_role_switch() {
internal_.btm_set_default_link_policy(btm_cb.acl_cb_.DefaultLinkPolicy() |
HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
void BTM_default_block_role_switch() {
internal_.btm_set_default_link_policy(btm_cb.acl_cb_.DefaultLinkPolicy() &
~HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH);
}
/*******************************************************************************
*
* Function btm_read_remote_version_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the remote version info.
*
* Returns void
*
******************************************************************************/
static void maybe_chain_more_commands_after_read_remote_version_complete(
uint8_t status, uint16_t handle) {
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb == nullptr) {
LOG_WARN("Received remote version complete for unknown device");
return;
}
switch (p_acl_cb->transport) {
case BT_TRANSPORT_LE:
l2cble_notify_le_connection(p_acl_cb->remote_addr);
l2cble_use_preferred_conn_params(p_acl_cb->remote_addr);
break;
case BT_TRANSPORT_BR_EDR:
/**
* When running legacy stack continue chain of executing various
* read commands. Skip when gd_acl is enabled because that
* module handles all remote read functionality.
*/
break;
default:
LOG_ERROR("Unable to determine transport:%s device:%s",
bt_transport_text(p_acl_cb->transport).c_str(),
PRIVATE_ADDRESS(p_acl_cb->remote_addr));
}
}
void btm_process_remote_version_complete(uint8_t status, uint16_t handle,
uint8_t lmp_version,
uint16_t manufacturer,
uint16_t lmp_subversion) {
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb == nullptr) {
LOG_WARN("Received remote version complete for unknown acl");
return;
}
if (status == HCI_SUCCESS) {
p_acl_cb->remote_version_info.lmp_version = lmp_version;
p_acl_cb->remote_version_info.manufacturer = manufacturer;
p_acl_cb->remote_version_info.lmp_subversion = lmp_subversion;
p_acl_cb->remote_version_info.valid = true;
BTM_update_version_info(p_acl_cb->RemoteAddress(),
p_acl_cb->remote_version_info);
bluetooth::common::LogRemoteVersionInfo(handle, status, lmp_version,
manufacturer, lmp_subversion);
} else {
bluetooth::common::LogRemoteVersionInfo(handle, status, 0, 0, 0);
}
}
void btm_read_remote_version_complete(tHCI_STATUS status, uint16_t handle,
uint8_t lmp_version,
uint16_t manufacturer,
uint16_t lmp_subversion) {
btm_process_remote_version_complete(status, handle, lmp_version, manufacturer,
lmp_subversion);
maybe_chain_more_commands_after_read_remote_version_complete(status, handle);
}
/*******************************************************************************
*
* Function btm_process_remote_ext_features
*
* Description Local function called to process all extended features pages
* read from a remote device.
*
* Returns void
*
******************************************************************************/
void btm_process_remote_ext_features(tACL_CONN* p_acl_cb,
uint8_t max_page_number) {
CHECK(p_acl_cb != nullptr);
if (!p_acl_cb->peer_lmp_feature_valid[max_page_number]) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
bool ssp_supported =
HCI_SSP_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
bool secure_connections_supported =
HCI_SC_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
bool role_switch_supported =
HCI_SWITCH_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]);
bool br_edr_supported =
!HCI_BREDR_NOT_SPT_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]);
bool le_supported =
HCI_LE_SPT_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0]) &&
HCI_LE_HOST_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[1]);
btm_sec_set_peer_sec_caps(p_acl_cb->hci_handle, ssp_supported,
secure_connections_supported, role_switch_supported,
br_edr_supported, le_supported);
}
/*******************************************************************************
*
* Function btm_read_remote_ext_features
*
* Description Local function called to send a read remote extended
* features
*
* Returns void
*
******************************************************************************/
void btm_read_remote_ext_features(uint16_t handle, uint8_t page_number) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
// GD L2cap reads this automatically
return;
}
btsnd_hcic_rmt_ext_features(handle, page_number);
}
void btm_read_remote_features_complete(uint16_t handle, uint8_t* features) {
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
/* Copy the received features page */
STREAM_TO_ARRAY(p_acl_cb->peer_lmp_feature_pages[0], features,
HCI_FEATURE_BYTES_PER_PAGE);
p_acl_cb->peer_lmp_feature_valid[0] = true;
if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl_cb->peer_lmp_feature_pages[0])) &&
(controller_get_interface()
->supports_reading_remote_extended_features())) {
/* if the remote controller has extended features and local controller
supports HCI_Read_Remote_Extended_Features command then start reading
these feature starting with extended features page 1 */
LOG_DEBUG("Start reading remote extended features");
btm_read_remote_ext_features(handle, 1);
return;
}
/* Remote controller has no extended features. Process remote controller
supported features (features page 0). */
btm_process_remote_ext_features(p_acl_cb, 0);
/* Continue with HCI connection establishment */
internal_.btm_establish_continue(p_acl_cb);
}
/*******************************************************************************
*
* Function btm_read_remote_ext_features_complete
*
* Description This function is called when the remote extended features
* complete event is received from the HCI.
*
* Returns void
*
******************************************************************************/
void btm_read_remote_ext_features_complete_raw(uint8_t* p, uint8_t evt_len) {
uint8_t page_num, max_page;
uint16_t handle;
if (evt_len < HCI_EXT_FEATURES_SUCCESS_EVT_LEN) {
android_errorWriteLog(0x534e4554, "141552859");
LOG_WARN("Remote extended feature length too short. length=%d", evt_len);
return;
}
++p;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT8(page_num, p);
STREAM_TO_UINT8(max_page, p);
if (max_page > HCI_EXT_FEATURES_PAGE_MAX) {
LOG_WARN("Too many max pages read page=%d unknown", max_page);
return;
}
if (page_num > HCI_EXT_FEATURES_PAGE_MAX) {
android_errorWriteLog(0x534e4554, "141552859");
LOG_WARN("Too many received pages num_page=%d invalid", page_num);
return;
}
if (page_num > max_page) {
LOG_WARN("num_page=%d, max_page=%d invalid", page_num, max_page);
}
btm_read_remote_ext_features_complete(handle, page_num, max_page, p);
}
void btm_read_remote_ext_features_complete(uint16_t handle, uint8_t page_num,
uint8_t max_page,
uint8_t* features) {
/* Validate parameters */
auto* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
/* Copy the received features page */
STREAM_TO_ARRAY(p_acl_cb->peer_lmp_feature_pages[page_num], features,
HCI_FEATURE_BYTES_PER_PAGE);
p_acl_cb->peer_lmp_feature_valid[page_num] = true;
/* If there is the next remote features page and
* we have space to keep this page data - read this page */
if ((page_num < max_page) && (page_num < HCI_EXT_FEATURES_PAGE_MAX)) {
page_num++;
LOG_DEBUG("BTM reads next remote extended features page (%d)", page_num);
btm_read_remote_ext_features(handle, page_num);
return;
}
/* Reading of remote feature pages is complete */
LOG_DEBUG("BTM reached last remote extended features page (%d)", page_num);
/* Process the pages */
btm_process_remote_ext_features(p_acl_cb, max_page);
/* Continue with HCI connection establishment */
internal_.btm_establish_continue(p_acl_cb);
}
/*******************************************************************************
*
* Function btm_read_remote_ext_features_failed
*
* Description This function is called when the remote extended features
* complete event returns a failed status.
*
* Returns void
*
******************************************************************************/
void btm_read_remote_ext_features_failed(uint8_t status, uint16_t handle) {
LOG_WARN("status 0x%02x for handle %d", status, handle);
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
/* Process supported features only */
btm_process_remote_ext_features(p_acl_cb, 0);
/* Continue HCI connection establishment */
internal_.btm_establish_continue(p_acl_cb);
}
/*******************************************************************************
*
* Function btm_establish_continue
*
* Description This function is called when the command complete message
* is received from the HCI for the read local link policy
* request.
*
* Returns void
*
******************************************************************************/
void StackAclBtmAcl::btm_establish_continue(tACL_CONN* p_acl) {
CHECK(p_acl != nullptr);
if (p_acl->is_transport_br_edr()) {
/* For now there are a some devices that do not like sending */
/* commands events and data at the same time. */
/* Set the packet types to the default allowed by the device */
const uint16_t default_packet_type_mask =
btm_cb.acl_cb_.DefaultPacketTypes();
if (!internal_.change_connection_packet_types(*p_acl,
default_packet_type_mask)) {
LOG_ERROR("Unable to change connection packet type types:%04x address:%s",
default_packet_type_mask,
PRIVATE_ADDRESS(p_acl->RemoteAddress()));
}
btm_set_link_policy(p_acl, btm_cb.acl_cb_.DefaultLinkPolicy());
}
NotifyAclLinkUp(*p_acl);
}
void btm_establish_continue_from_address(const RawAddress& bda,
tBT_TRANSPORT transport) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, transport);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
internal_.btm_establish_continue(p_acl);
}
/*******************************************************************************
*
* Function BTM_GetLinkSuperTout
*
* Description Read the link supervision timeout value of the connection
*
* Returns status of the operation
*
******************************************************************************/
tBTM_STATUS BTM_GetLinkSuperTout(const RawAddress& remote_bda,
uint16_t* p_timeout) {
CHECK(p_timeout != nullptr);
const tACL_CONN* p_acl =
internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return BTM_UNKNOWN_ADDR;
}
*p_timeout = p_acl->link_super_tout;
return BTM_SUCCESS;
}
/*******************************************************************************
*
* Function BTM_SetLinkSuperTout
*
* Description Create and send HCI "Write Link Supervision Timeout" command
*
* Returns status of the operation
*
******************************************************************************/
tBTM_STATUS BTM_SetLinkSuperTout(const RawAddress& remote_bda,
uint16_t timeout) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return BTM_UNKNOWN_ADDR;
}
/* Only send if current role is Central; 2.0 spec requires this */
if (p_acl->link_role == HCI_ROLE_CENTRAL) {
if (!bluetooth::shim::
controller_is_write_link_supervision_timeout_supported()) {
LOG_WARN(
"UNSUPPORTED by controller write link supervision timeout:%.2fms "
"bd_addr:%s",
supervision_timeout_to_seconds(timeout), PRIVATE_ADDRESS(remote_bda));
return BTM_MODE_UNSUPPORTED;
}
p_acl->link_super_tout = timeout;
btsnd_hcic_write_link_super_tout(p_acl->hci_handle, timeout);
LOG_DEBUG("Set supervision timeout:%.2fms bd_addr:%s",
supervision_timeout_to_seconds(timeout),
PRIVATE_ADDRESS(remote_bda));
return BTM_CMD_STARTED;
} else {
LOG_WARN(
"Role is peripheral so unable to set supervision timeout:%.2fms "
"bd_addr:%s",
supervision_timeout_to_seconds(timeout), PRIVATE_ADDRESS(remote_bda));
return BTM_SUCCESS;
}
}
bool BTM_IsAclConnectionUp(const RawAddress& remote_bda,
tBT_TRANSPORT transport) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_IsLinkEstablished(remote_bda, transport);
}
return internal_.btm_bda_to_acl(remote_bda, transport) != nullptr;
}
bool BTM_IsAclConnectionUpAndHandleValid(const RawAddress& remote_bda,
tBT_TRANSPORT transport) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_IsLinkEstablished(remote_bda, transport);
}
tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, transport);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
return p_acl->hci_handle != HCI_INVALID_HANDLE;
}
bool BTM_IsAclConnectionUpFromHandle(uint16_t hci_handle) {
return internal_.acl_get_connection_from_handle(hci_handle) != nullptr;
}
/*******************************************************************************
*
* Function BTM_GetNumAclLinks
*
* Description This function is called to count the number of
* ACL links that are active.
*
* Returns uint16_t Number of active ACL links
*
******************************************************************************/
uint16_t BTM_GetNumAclLinks(void) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_GetNumLinks();
}
return static_cast<uint16_t>(btm_cb.acl_cb_.NumberOfActiveLinks());
}
/*******************************************************************************
*
* Function btm_get_acl_disc_reason_code
*
* Description This function is called to get the disconnection reason code
* returned by the HCI at disconnection complete event.
*
* Returns true if connection is up, else false.
*
******************************************************************************/
tHCI_REASON btm_get_acl_disc_reason_code(void) {
return btm_cb.acl_cb_.get_disconnect_reason();
}
/*******************************************************************************
*
* Function BTM_GetHCIConnHandle
*
* Description This function is called to get the handle for an ACL
* connection to a specific remote BD Address.
*
* Returns the handle of the connection, or HCI_INVALID_HANDLE if none.
*
******************************************************************************/
uint16_t BTM_GetHCIConnHandle(const RawAddress& remote_bda,
tBT_TRANSPORT transport) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::BTM_GetHCIConnHandle(remote_bda, transport);
}
tACL_CONN* p;
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p != (tACL_CONN*)NULL) {
return (p->hci_handle);
}
/* If here, no BD Addr found */
return HCI_INVALID_HANDLE;
}
/*******************************************************************************
*
* Function BTM_IsPhy2mSupported
*
* Description This function is called to check PHY 2M support
* from peer device
* Returns True when PHY 2M supported false otherwise
*
******************************************************************************/
bool BTM_IsPhy2mSupported(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
tACL_CONN* p;
BTM_TRACE_DEBUG("BTM_IsPhy2mSupported");
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p == (tACL_CONN*)NULL) {
BTM_TRACE_DEBUG("BTM_IsPhy2mSupported: no connection");
return false;
}
if (!p->peer_le_features_valid) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
return HCI_LE_2M_PHY_SUPPORTED(p->peer_le_features);
}
/*******************************************************************************
*
* Function BTM_RequestPeerSCA
*
* Description This function is called to request sleep clock accuracy
* from peer device
*
******************************************************************************/
void BTM_RequestPeerSCA(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
tACL_CONN* p;
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p == (tACL_CONN*)NULL) {
LOG_WARN("Unable to find active acl");
return;
}
btsnd_hcic_req_peer_sca(p->hci_handle);
}
/*******************************************************************************
*
* Function BTM_GetPeerSCA
*
* Description This function is called to get peer sleep clock accuracy
*
* Returns SCA or 0xFF if SCA was never previously requested, request
* is not supported by peer device or ACL does not exist
*
******************************************************************************/
uint8_t BTM_GetPeerSCA(const RawAddress& remote_bda, tBT_TRANSPORT transport) {
tACL_CONN* p;
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p != (tACL_CONN*)NULL) {
return (p->sca);
}
LOG_WARN("Unable to find active acl");
/* If here, no BD Addr found */
return (0xFF);
}
/*******************************************************************************
*
* Function btm_rejectlist_role_change_device
*
* Description This function is used to rejectlist the device if the role
* switch fails for maximum number of times. It also removes
* the device from the black list if the role switch succeeds.
*
* Input Parms bd_addr - remote BD addr
* hci_status - role switch status
*
* Returns void
*
*******************************************************************************/
void btm_rejectlist_role_change_device(const RawAddress& bd_addr,
uint8_t hci_status) {
tACL_CONN* p = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (!p) {
LOG_WARN("Unable to find active acl");
return;
}
if (hci_status == HCI_SUCCESS) {
p->switch_role_failed_attempts = 0;
return;
}
/* check for carkits */
const uint32_t cod_audio_device =
(BTM_COD_SERVICE_AUDIO | BTM_COD_MAJOR_AUDIO) << 8;
const uint8_t* dev_class = btm_get_dev_class(bd_addr);
if (dev_class == nullptr) return;
const uint32_t cod =
((dev_class[0] << 16) | (dev_class[1] << 8) | dev_class[2]) & 0xffffff;
if ((hci_status != HCI_SUCCESS) &&
(p->is_switch_role_switching_or_in_progress()) &&
((cod & cod_audio_device) == cod_audio_device) &&
(!interop_match_addr(INTEROP_DYNAMIC_ROLE_SWITCH, &bd_addr))) {
p->switch_role_failed_attempts++;
if (p->switch_role_failed_attempts == BTM_MAX_SW_ROLE_FAILED_ATTEMPTS) {
LOG_WARN(
"Device %s rejectlisted for role switching - "
"multiple role switch failed attempts: %u",
bd_addr.ToString().c_str(), p->switch_role_failed_attempts);
interop_database_add(INTEROP_DYNAMIC_ROLE_SWITCH, &bd_addr, 3);
}
}
}
/*******************************************************************************
*
* Function btm_acl_role_changed
*
* Description This function is called whan a link's central/peripheral
*role change event or command status event (with error) is received. It updates
*the link control block, and calls the registered callback with status and role
*(if registered).
*
* Returns void
*
******************************************************************************/
void StackAclBtmAcl::btm_acl_role_changed(tHCI_STATUS hci_status,
const RawAddress& bd_addr,
tHCI_ROLE new_role) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
// If we get a role change before connection complete, we cache the new
// role here and then propagate it when ACL Link is created.
RoleChangeView role_change;
role_change.new_role = new_role;
role_change.bd_addr = bd_addr;
delayed_role_change_ =
std::make_unique<RoleChangeView>(std::move(role_change));
LOG_WARN("Unable to find active acl");
return;
}
tBTM_ROLE_SWITCH_CMPL* p_switch_role = &btm_cb.acl_cb_.switch_role_ref_data;
LOG_DEBUG("Role change event received peer:%s hci_status:%s new_role:%s",
PRIVATE_ADDRESS(bd_addr), hci_error_code_text(hci_status).c_str(),
RoleText(new_role).c_str());
p_switch_role->hci_status = hci_status;
if (hci_status == HCI_SUCCESS) {
p_switch_role->role = new_role;
p_switch_role->remote_bd_addr = bd_addr;
/* Update cached value */
p_acl->link_role = new_role;
/* Reload LSTO: link supervision timeout is reset in the LM after a role
* switch */
if (new_role == HCI_ROLE_CENTRAL) {
constexpr uint16_t link_supervision_timeout = 8000;
BTM_SetLinkSuperTout(bd_addr, link_supervision_timeout);
}
} else {
new_role = p_acl->link_role;
}
/* Check if any SCO req is pending for role change */
btm_sco_chk_pend_rolechange(p_acl->hci_handle);
/* if switching state is switching we need to turn encryption on */
/* if idle, we did not change encryption */
if (p_acl->is_switch_role_switching()) {
p_acl->set_encryption_on();
p_acl->set_switch_role_encryption_on();
return;
}
/* Set the switch_role_state to IDLE since the reply received from HCI */
/* regardless of its result either success or failed. */
if (p_acl->is_switch_role_in_progress()) {
p_acl->set_encryption_idle();
p_acl->reset_switch_role();
}
BTA_dm_report_role_change(bd_addr, new_role, hci_status);
btm_sec_role_changed(hci_status, bd_addr, new_role);
/* If a disconnect is pending, issue it now that role switch has completed */
if (p_acl->rs_disc_pending == BTM_SEC_DISC_PENDING) {
hci_btsnd_hcic_disconnect(*p_acl, HCI_ERR_PEER_USER,
"stack::acl::btm_acl::role after role switch");
}
p_acl->rs_disc_pending = BTM_SEC_RS_NOT_PENDING; /* reset flag */
}
void btm_acl_role_changed(tHCI_STATUS hci_status, const RawAddress& bd_addr,
tHCI_ROLE new_role) {
btm_rejectlist_role_change_device(bd_addr, hci_status);
if (hci_status == HCI_SUCCESS) {
l2c_link_role_changed(&bd_addr, new_role, hci_status);
} else {
l2c_link_role_changed(nullptr, HCI_ROLE_UNKNOWN,
HCI_ERR_COMMAND_DISALLOWED);
}
internal_.btm_acl_role_changed(hci_status, bd_addr, new_role);
}
/*******************************************************************************
*
* Function change_connection_packet_types
*
* Description This function sets the packet types used for a specific
* ACL connection. It is called internally by btm_acl_created
* or by an application/profile by BTM_SetPacketTypes.
*
* Returns status of the operation
*
******************************************************************************/
bool StackAclBtmAcl::change_connection_packet_types(
tACL_CONN& link, const uint16_t new_packet_type_mask) {
// Start with the default configured packet types
const uint16_t default_packet_type_mask = btm_cb.acl_cb_.DefaultPacketTypes();
uint16_t packet_type_mask =
default_packet_type_mask &
(new_packet_type_mask & BTM_ACL_SUPPORTED_PKTS_MASK);
/* OR in any exception packet types if at least 2.0 version of spec */
packet_type_mask |=
((new_packet_type_mask & BTM_ACL_EXCEPTION_PKTS_MASK) |
(BTM_ACL_EXCEPTION_PKTS_MASK & default_packet_type_mask));
/* Exclude packet types not supported by the peer */
if (link.peer_lmp_feature_valid[0]) {
PeerPacketTypes peer_packet_types(link.peer_lmp_feature_pages[0]);
packet_type_mask &= peer_packet_types.acl.supported;
packet_type_mask |= peer_packet_types.acl.unsupported;
} else {
LOG_INFO(
"Unable to include remote supported packet types as read feature "
"incomplete");
LOG_INFO("TIP: Maybe wait until read feature complete beforehand");
}
if (packet_type_mask == 0) {
LOG_WARN("Unable to send controller illegal change packet mask:0x%04x",
packet_type_mask);
return false;
}
link.pkt_types_mask = packet_type_mask;
bluetooth::legacy::hci::GetInterface().ChangeConnectionPacketType(
link.Handle(), link.pkt_types_mask);
LOG_DEBUG("Started change connection packet type:0x%04x address:%s",
link.pkt_types_mask, PRIVATE_ADDRESS(link.RemoteAddress()));
return true;
}
void btm_set_packet_types_from_address(const RawAddress& bd_addr,
uint16_t pkt_types) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
if (!internal_.change_connection_packet_types(*p_acl, pkt_types)) {
LOG_ERROR("Unable to change connection packet type types:%04x address:%s",
pkt_types, PRIVATE_ADDRESS(bd_addr));
}
}
/*******************************************************************************
*
* Function BTM_GetMaxPacketSize
*
* Returns Returns maximum packet size that can be used for current
* connection, 0 if connection is not established
*
******************************************************************************/
uint16_t BTM_GetMaxPacketSize(const RawAddress& addr) {
tACL_CONN* p = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
uint16_t pkt_types = 0;
uint16_t pkt_size = 0;
if (p != NULL) {
pkt_types = p->pkt_types_mask;
} else {
/* Special case for when info for the local device is requested */
if (addr == *controller_get_interface()->get_address()) {
pkt_types = btm_cb.acl_cb_.DefaultPacketTypes();
}
}
if (pkt_types) {
if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH5))
pkt_size = HCI_EDR3_DH5_PACKET_SIZE;
else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH5))
pkt_size = HCI_EDR2_DH5_PACKET_SIZE;
else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH3))
pkt_size = HCI_EDR3_DH3_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DH5)
pkt_size = HCI_DH5_PACKET_SIZE;
else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH3))
pkt_size = HCI_EDR2_DH3_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DM5)
pkt_size = HCI_DM5_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DH3)
pkt_size = HCI_DH3_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DM3)
pkt_size = HCI_DM3_PACKET_SIZE;
else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_3_DH1))
pkt_size = HCI_EDR3_DH1_PACKET_SIZE;
else if (!(pkt_types & HCI_PKT_TYPES_MASK_NO_2_DH1))
pkt_size = HCI_EDR2_DH1_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DH1)
pkt_size = HCI_DH1_PACKET_SIZE;
else if (pkt_types & HCI_PKT_TYPES_MASK_DM1)
pkt_size = HCI_DM1_PACKET_SIZE;
}
return (pkt_size);
}
/*******************************************************************************
*
* Function BTM_ReadRemoteVersion
*
* Returns If connected report peer device info
*
******************************************************************************/
bool BTM_ReadRemoteVersion(const RawAddress& addr, uint8_t* lmp_version,
uint16_t* manufacturer, uint16_t* lmp_sub_version) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_ReadRemoteVersion(
addr, lmp_version, manufacturer, lmp_sub_version);
}
const tACL_CONN* p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
p_acl = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_LE);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
}
if (!p_acl->remote_version_info.valid) {
LOG_WARN("Remote version information is invalid");
return false;
}
if (lmp_version) *lmp_version = p_acl->remote_version_info.lmp_version;
if (manufacturer) *manufacturer = p_acl->remote_version_info.manufacturer;
if (lmp_sub_version)
*lmp_sub_version = p_acl->remote_version_info.lmp_subversion;
return true;
}
/*******************************************************************************
*
* Function BTM_ReadRemoteFeatures
*
* Returns pointer to the remote supported features mask (8 bytes)
*
******************************************************************************/
uint8_t* BTM_ReadRemoteFeatures(const RawAddress& addr) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_ReadRemoteFeatures(addr);
}
tACL_CONN* p = internal_.btm_bda_to_acl(addr, BT_TRANSPORT_BR_EDR);
if (p == NULL) {
LOG_WARN("Unable to find active acl");
return (NULL);
}
return (p->peer_lmp_feature_pages[0]);
}
/*******************************************************************************
*
* Function BTM_ReadRSSI
*
* Description This function is called to read the link policy settings.
* The address of link policy results are returned in the
* callback.
* (tBTM_RSSI_RESULT)
*
* Returns BTM_CMD_STARTED if successfully initiated or error code
*
******************************************************************************/
tBTM_STATUS BTM_ReadRSSI(const RawAddress& remote_bda, tBTM_CMPL_CB* p_cb) {
tACL_CONN* p = NULL;
tBT_DEVICE_TYPE dev_type;
tBLE_ADDR_TYPE addr_type;
/* If someone already waiting on the version, do not allow another */
if (btm_cb.devcb.p_rssi_cmpl_cb) return (BTM_BUSY);
BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
if (dev_type & BT_DEVICE_TYPE_BLE) {
p = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
}
if (p == NULL && dev_type & BT_DEVICE_TYPE_BREDR) {
p = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
}
if (p) {
btm_cb.devcb.p_rssi_cmpl_cb = p_cb;
alarm_set_on_mloop(btm_cb.devcb.read_rssi_timer, BTM_DEV_REPLY_TIMEOUT_MS,
btm_read_rssi_timeout, NULL);
btsnd_hcic_read_rssi(p->hci_handle);
return (BTM_CMD_STARTED);
}
LOG_WARN("Unable to find active acl");
/* If here, no BD Addr found */
return (BTM_UNKNOWN_ADDR);
}
/*******************************************************************************
*
* Function BTM_ReadFailedContactCounter
*
* Description This function is called to read the failed contact counter.
* The result is returned in the callback.
* (tBTM_FAILED_CONTACT_COUNTER_RESULT)
*
* Returns BTM_CMD_STARTED if successfully initiated or error code
*
******************************************************************************/
tBTM_STATUS BTM_ReadFailedContactCounter(const RawAddress& remote_bda,
tBTM_CMPL_CB* p_cb) {
tACL_CONN* p;
tBT_TRANSPORT transport = BT_TRANSPORT_BR_EDR;
tBT_DEVICE_TYPE dev_type;
tBLE_ADDR_TYPE addr_type;
/* If someone already waiting on the result, do not allow another */
if (btm_cb.devcb.p_failed_contact_counter_cmpl_cb) return (BTM_BUSY);
BTM_ReadDevInfo(remote_bda, &dev_type, &addr_type);
if (dev_type == BT_DEVICE_TYPE_BLE) transport = BT_TRANSPORT_LE;
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p != (tACL_CONN*)NULL) {
btm_cb.devcb.p_failed_contact_counter_cmpl_cb = p_cb;
alarm_set_on_mloop(btm_cb.devcb.read_failed_contact_counter_timer,
BTM_DEV_REPLY_TIMEOUT_MS,
btm_read_failed_contact_counter_timeout, NULL);
btsnd_hcic_read_failed_contact_counter(p->hci_handle);
return (BTM_CMD_STARTED);
}
LOG_WARN("Unable to find active acl");
/* If here, no BD Addr found */
return (BTM_UNKNOWN_ADDR);
}
/*******************************************************************************
*
* Function BTM_ReadTxPower
*
* Description This function is called to read the current
* TX power of the connection. The tx power level results
* are returned in the callback.
* (tBTM_RSSI_RESULT)
*
* Returns BTM_CMD_STARTED if successfully initiated or error code
*
******************************************************************************/
tBTM_STATUS BTM_ReadTxPower(const RawAddress& remote_bda,
tBT_TRANSPORT transport, tBTM_CMPL_CB* p_cb) {
tACL_CONN* p;
#define BTM_READ_RSSI_TYPE_CUR 0x00
#define BTM_READ_RSSI_TYPE_MAX 0X01
VLOG(2) << __func__ << ": RemBdAddr: " << remote_bda;
/* If someone already waiting on the version, do not allow another */
if (btm_cb.devcb.p_tx_power_cmpl_cb) return (BTM_BUSY);
p = internal_.btm_bda_to_acl(remote_bda, transport);
if (p != (tACL_CONN*)NULL) {
btm_cb.devcb.p_tx_power_cmpl_cb = p_cb;
alarm_set_on_mloop(btm_cb.devcb.read_tx_power_timer,
BTM_DEV_REPLY_TIMEOUT_MS, btm_read_tx_power_timeout,
NULL);
if (p->transport == BT_TRANSPORT_LE) {
btm_cb.devcb.read_tx_pwr_addr = remote_bda;
btsnd_hcic_ble_read_adv_chnl_tx_power();
} else {
btsnd_hcic_read_tx_power(p->hci_handle, BTM_READ_RSSI_TYPE_CUR);
}
return (BTM_CMD_STARTED);
}
LOG_WARN("Unable to find active acl");
/* If here, no BD Addr found */
return (BTM_UNKNOWN_ADDR);
}
/*******************************************************************************
*
* Function btm_read_tx_power_timeout
*
* Description Callback when reading the tx power times out.
*
* Returns void
*
******************************************************************************/
void btm_read_tx_power_timeout(UNUSED_ATTR void* data) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
if (p_cb) (*p_cb)((void*)NULL);
}
/*******************************************************************************
*
* Function btm_read_tx_power_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the read tx power request.
*
* Returns void
*
******************************************************************************/
void btm_read_tx_power_complete(uint8_t* p, bool is_ble) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_tx_power_cmpl_cb;
tBTM_TX_POWER_RESULT result;
alarm_cancel(btm_cb.devcb.read_tx_power_timer);
btm_cb.devcb.p_tx_power_cmpl_cb = NULL;
/* If there was a registered callback, call it */
if (p_cb) {
STREAM_TO_UINT8(result.hci_status, p);
if (result.hci_status == HCI_SUCCESS) {
result.status = BTM_SUCCESS;
if (!is_ble) {
uint16_t handle;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT8(result.tx_power, p);
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb != nullptr) {
result.rem_bda = p_acl_cb->remote_addr;
}
} else {
STREAM_TO_UINT8(result.tx_power, p);
result.rem_bda = btm_cb.devcb.read_tx_pwr_addr;
}
LOG_DEBUG("Transmit power complete: tx_power:%d hci status:%s",
result.tx_power,
hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status))
.c_str());
} else {
result.status = BTM_ERR_PROCESSING;
}
(*p_cb)(&result);
}
}
/*******************************************************************************
*
* Function btm_read_rssi_timeout
*
* Description Callback when reading the RSSI times out.
*
* Returns void
*
******************************************************************************/
void btm_read_rssi_timeout(UNUSED_ATTR void* data) {
tBTM_RSSI_RESULT result;
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
btm_cb.devcb.p_rssi_cmpl_cb = NULL;
result.status = BTM_DEVICE_TIMEOUT;
if (p_cb) (*p_cb)(&result);
}
/*******************************************************************************
*
* Function btm_read_rssi_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the read rssi request.
*
* Returns void
*
******************************************************************************/
void btm_read_rssi_complete(uint8_t* p) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_rssi_cmpl_cb;
tBTM_RSSI_RESULT result;
alarm_cancel(btm_cb.devcb.read_rssi_timer);
btm_cb.devcb.p_rssi_cmpl_cb = NULL;
/* If there was a registered callback, call it */
if (p_cb) {
STREAM_TO_UINT8(result.hci_status, p);
result.status = BTM_ERR_PROCESSING;
if (result.hci_status == HCI_SUCCESS) {
uint16_t handle;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT8(result.rssi, p);
LOG_DEBUG("Read rrsi complete rssi:%hhd hci status:%s", result.rssi,
hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status))
.c_str());
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb != nullptr) {
result.rem_bda = p_acl_cb->remote_addr;
result.status = BTM_SUCCESS;
}
}
(*p_cb)(&result);
}
}
/*******************************************************************************
*
* Function btm_read_failed_contact_counter_timeout
*
* Description Callback when reading the failed contact counter times out.
*
* Returns void
*
******************************************************************************/
void btm_read_failed_contact_counter_timeout(UNUSED_ATTR void* data) {
tBTM_FAILED_CONTACT_COUNTER_RESULT result;
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_failed_contact_counter_cmpl_cb;
btm_cb.devcb.p_failed_contact_counter_cmpl_cb = NULL;
result.status = BTM_DEVICE_TIMEOUT;
if (p_cb) (*p_cb)(&result);
}
/*******************************************************************************
*
* Function btm_read_failed_contact_counter_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the read failed contact
* counter request.
*
* Returns void
*
******************************************************************************/
void btm_read_failed_contact_counter_complete(uint8_t* p) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_failed_contact_counter_cmpl_cb;
tBTM_FAILED_CONTACT_COUNTER_RESULT result;
alarm_cancel(btm_cb.devcb.read_failed_contact_counter_timer);
btm_cb.devcb.p_failed_contact_counter_cmpl_cb = NULL;
/* If there was a registered callback, call it */
if (p_cb) {
uint16_t handle;
STREAM_TO_UINT8(result.hci_status, p);
if (result.hci_status == HCI_SUCCESS) {
result.status = BTM_SUCCESS;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT16(result.failed_contact_counter, p);
LOG_DEBUG(
"Failed contact counter complete: counter %u, hci status:%s",
result.failed_contact_counter,
hci_status_code_text(to_hci_status_code(result.hci_status)).c_str());
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb != nullptr) {
result.rem_bda = p_acl_cb->remote_addr;
}
} else {
result.status = BTM_ERR_PROCESSING;
}
(*p_cb)(&result);
}
}
/*******************************************************************************
*
* Function btm_read_automatic_flush_timeout_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the read automatic flush
* timeout request.
*
* Returns void
*
******************************************************************************/
void btm_read_automatic_flush_timeout_complete(uint8_t* p) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_automatic_flush_timeout_cmpl_cb;
tBTM_AUTOMATIC_FLUSH_TIMEOUT_RESULT result;
alarm_cancel(btm_cb.devcb.read_automatic_flush_timeout_timer);
btm_cb.devcb.p_automatic_flush_timeout_cmpl_cb = nullptr;
/* If there was a registered callback, call it */
if (p_cb) {
uint16_t handle;
STREAM_TO_UINT8(result.hci_status, p);
result.status = BTM_ERR_PROCESSING;
if (result.hci_status == HCI_SUCCESS) {
result.status = BTM_SUCCESS;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT16(result.automatic_flush_timeout, p);
LOG_DEBUG(
"Read automatic flush timeout complete timeout:%hu hci_status:%s",
result.automatic_flush_timeout,
hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status))
.c_str());
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb != nullptr) {
result.rem_bda = p_acl_cb->remote_addr;
}
}
(*p_cb)(&result);
}
}
/*******************************************************************************
*
* Function btm_read_link_quality_timeout
*
* Description Callback when reading the link quality times out.
*
* Returns void
*
******************************************************************************/
void btm_read_link_quality_timeout(UNUSED_ATTR void* data) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
if (p_cb) (*p_cb)((void*)NULL);
}
/*******************************************************************************
*
* Function btm_read_link_quality_complete
*
* Description This function is called when the command complete message
* is received from the HCI for the read link quality.
*
* Returns void
*
******************************************************************************/
void btm_read_link_quality_complete(uint8_t* p) {
tBTM_CMPL_CB* p_cb = btm_cb.devcb.p_link_qual_cmpl_cb;
tBTM_LINK_QUALITY_RESULT result;
alarm_cancel(btm_cb.devcb.read_link_quality_timer);
btm_cb.devcb.p_link_qual_cmpl_cb = NULL;
/* If there was a registered callback, call it */
if (p_cb) {
STREAM_TO_UINT8(result.hci_status, p);
if (result.hci_status == HCI_SUCCESS) {
uint16_t handle;
result.status = BTM_SUCCESS;
STREAM_TO_UINT16(handle, p);
STREAM_TO_UINT8(result.link_quality, p);
LOG_DEBUG("BTM Link Quality Complete: Link Quality %d, hci status:%s",
result.link_quality,
hci_error_code_text(static_cast<tHCI_STATUS>(result.hci_status))
.c_str());
tACL_CONN* p_acl_cb = internal_.acl_get_connection_from_handle(handle);
if (p_acl_cb != nullptr) {
result.rem_bda = p_acl_cb->remote_addr;
}
} else {
result.status = BTM_ERR_PROCESSING;
}
(*p_cb)(&result);
}
}
/*******************************************************************************
*
* Function btm_remove_acl
*
* Description This function is called to disconnect an ACL connection
*
* Returns BTM_SUCCESS if successfully initiated, otherwise
* BTM_UNKNOWN_ADDR.
*
******************************************************************************/
tBTM_STATUS btm_remove_acl(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
if (bluetooth::shim::is_gd_l2cap_enabled()) {
if (transport == BT_TRANSPORT_LE) {
LOG(ERROR) << __func__ << ": Unsupported";
}
bluetooth::shim::L2CA_DisconnectLink(bd_addr);
return BTM_SUCCESS;
}
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return BTM_UNKNOWN_ADDR;
}
if (p_acl->Handle() == HCI_INVALID_HANDLE) {
LOG_WARN("Cannot remove unknown acl bd_addr:%s transport:%s",
PRIVATE_ADDRESS(bd_addr), bt_transport_text(transport).c_str());
return BTM_UNKNOWN_ADDR;
}
if (p_acl->rs_disc_pending == BTM_SEC_RS_PENDING) {
LOG_DEBUG(
"Delay disconnect until role switch is complete bd_addr:%s "
"transport:%s",
PRIVATE_ADDRESS(bd_addr), bt_transport_text(transport).c_str());
p_acl->rs_disc_pending = BTM_SEC_DISC_PENDING;
return BTM_SUCCESS;
}
hci_btsnd_hcic_disconnect(*p_acl, HCI_ERR_PEER_USER,
"stack::acl::btm_acl::btm_remove_acl");
return BTM_SUCCESS;
}
/*******************************************************************************
*
* Function BTM_SetTraceLevel
*
* Description This function sets the trace level for BTM. If called with
* a value of 0xFF, it simply returns the current trace level.
*
* Returns The new or current trace level
*
******************************************************************************/
uint8_t BTM_SetTraceLevel(uint8_t new_level) {
if (new_level != 0xFF) btm_cb.trace_level = new_level;
return (btm_cb.trace_level);
}
void btm_cont_rswitch_from_handle(uint16_t hci_handle) {
tACL_CONN* p = internal_.acl_get_connection_from_handle(hci_handle);
if (p == nullptr) {
LOG_WARN("Role switch received but with no active ACL");
return;
}
/* Check to see if encryption needs to be turned off if pending
change of link key or role switch */
if (p->is_switch_role_mode_change()) {
/* Must turn off Encryption first if necessary */
/* Some devices do not support switch or change of link key while encryption
* is on */
if (p->is_encrypted && !IsEprAvailable(*p)) {
p->set_encryption_off();
if (p->is_switch_role_mode_change()) {
p->set_switch_role_encryption_off();
}
} else /* Encryption not used or EPR supported, continue with switch
and/or change of link key */
{
if (p->is_switch_role_mode_change()) {
internal_.hci_start_role_switch_to_central(*p);
}
}
}
}
/*******************************************************************************
*
* Function btm_acl_resubmit_page
*
* Description send pending page request
*
******************************************************************************/
void btm_acl_resubmit_page(void) {
BT_HDR* p_buf;
uint8_t* pp;
/* If there were other page request schedule can start the next one */
p_buf = (BT_HDR*)fixed_queue_try_dequeue(btm_cb.page_queue);
if (p_buf != NULL) {
/* skip 3 (2 bytes opcode and 1 byte len) to get to the bd_addr
* for both create_conn and rmt_name */
pp = (uint8_t*)(p_buf + 1) + p_buf->offset + 3;
RawAddress bda;
STREAM_TO_BDADDR(bda, pp);
btm_cb.connecting_bda = bda;
memcpy(btm_cb.connecting_dc, btm_get_dev_class(bda), DEV_CLASS_LEN);
btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p_buf);
} else {
btm_cb.paging = false;
}
}
/*******************************************************************************
*
* Function btm_acl_reset_paging
*
* Description set paging to false and free the page queue - called at
* hci_reset
*
******************************************************************************/
void btm_acl_reset_paging(void) {
BT_HDR* p;
/* If we sent reset we are definitely not paging any more */
while ((p = (BT_HDR*)fixed_queue_try_dequeue(btm_cb.page_queue)) != NULL)
osi_free(p);
btm_cb.paging = false;
}
/*******************************************************************************
*
* Function btm_acl_paging
*
* Description send a paging command or queue it in btm_cb
*
******************************************************************************/
void btm_acl_paging(BT_HDR* p, const RawAddress& bda) {
// This function is called by the device initiating the connection.
// If no role change is requested from the remote device, we want
// to classify the connection initiator as the central device.
if (delayed_role_change_ == nullptr) {
RoleChangeView role_change;
role_change.bd_addr = bda;
role_change.new_role = HCI_ROLE_CENTRAL;
delayed_role_change_ =
std::make_unique<RoleChangeView>(std::move(role_change));
}
if (!BTM_IsAclConnectionUp(bda, BT_TRANSPORT_BR_EDR)) {
VLOG(1) << "connecting_bda: " << btm_cb.connecting_bda;
if (btm_cb.paging && bda == btm_cb.connecting_bda) {
fixed_queue_enqueue(btm_cb.page_queue, p);
} else {
btm_cb.connecting_bda = bda;
memcpy(btm_cb.connecting_dc, btm_get_dev_class(bda), DEV_CLASS_LEN);
btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
}
btm_cb.paging = true;
} else /* ACL is already up */
{
btu_hcif_send_cmd(LOCAL_BR_EDR_CONTROLLER_ID, p);
}
}
/*******************************************************************************
*
* Function btm_acl_notif_conn_collision
*
* Description Send connection collision event to upper layer if registered
*
*
******************************************************************************/
void btm_acl_notif_conn_collision(const RawAddress& bda) {
do_in_main_thread(FROM_HERE, base::Bind(bta_sys_notify_collision, bda));
}
bool BTM_BLE_IS_RESOLVE_BDA(const RawAddress& x) {
return ((x.address)[0] & BLE_RESOLVE_ADDR_MASK) == BLE_RESOLVE_ADDR_MSB;
}
bool acl_refresh_remote_address(const RawAddress& identity_address,
tBLE_ADDR_TYPE identity_address_type,
const RawAddress& bda,
tBTM_SEC_BLE::tADDRESS_TYPE rra_type,
const RawAddress& rpa) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bda, BT_TRANSPORT_LE);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
if (rra_type == tBTM_SEC_BLE::BTM_BLE_ADDR_PSEUDO) {
/* use identity address, resolvable_private_addr is empty */
if (rpa.IsEmpty()) {
p_acl->active_remote_addr_type = identity_address_type;
p_acl->active_remote_addr = identity_address;
} else {
p_acl->active_remote_addr_type = BLE_ADDR_RANDOM;
p_acl->active_remote_addr = rpa;
}
} else {
p_acl->active_remote_addr_type = static_cast<tBLE_ADDR_TYPE>(rra_type);
p_acl->active_remote_addr = rpa;
}
LOG_DEBUG("active_remote_addr_type: %d ", p_acl->active_remote_addr_type);
return true;
}
bool acl_peer_supports_ble_connection_parameters_request(
const RawAddress& remote_bda) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_LE);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
if (!p_acl->peer_le_features_valid) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
return HCI_LE_CONN_PARAM_REQ_SUPPORTED(p_acl->peer_le_features);
}
bool acl_peer_supports_sniff_subrating(const RawAddress& remote_bda) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(remote_bda, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
if (!p_acl->peer_lmp_feature_valid[0]) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
return HCI_SNIFF_SUB_RATE_SUPPORTED(p_acl->peer_lmp_feature_pages[0]);
}
/*******************************************************************************
*
* Function BTM_ReadConnectionAddr
*
* Description This function is called to get the local device address
* information.
*
* Returns void
*
******************************************************************************/
void BTM_ReadConnectionAddr(const RawAddress& remote_bda,
RawAddress& local_conn_addr,
tBLE_ADDR_TYPE* p_addr_type) {
if (bluetooth::shim::is_gd_shim_enabled()) {
return bluetooth::shim::BTM_ReadConnectionAddr(remote_bda, local_conn_addr,
p_addr_type);
}
if (bluetooth::shim::is_gd_l2cap_enabled()) {
bluetooth::shim::L2CA_ReadConnectionAddr(remote_bda, local_conn_addr,
p_addr_type);
return;
} else {
bluetooth::shim::ACL_ReadConnectionAddress(remote_bda, local_conn_addr,
p_addr_type);
return;
}
}
/*******************************************************************************
*
* Function BTM_IsBleConnection
*
* Description This function is called to check if the connection handle
* for an LE link
*
* Returns true if connection is LE link, otherwise false.
*
******************************************************************************/
bool BTM_IsBleConnection(uint16_t hci_handle) {
if (bluetooth::shim::is_gd_shim_enabled()) {
ASSERT_LOG(false, "This should not be invoked from code path");
}
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_IsLeLink(hci_handle);
}
const tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
if (p_acl == nullptr) return false;
return p_acl->is_transport_ble();
}
const RawAddress acl_address_from_handle(uint16_t handle) {
tACL_CONN* p_acl = acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
return RawAddress::kEmpty;
}
return p_acl->remote_addr;
}
/*******************************************************************************
*
* Function btm_ble_refresh_local_resolvable_private_addr
*
* Description This function refresh the currently used resolvable private
* address for the active link to the remote device
*
******************************************************************************/
void btm_ble_refresh_local_resolvable_private_addr(
const RawAddress& pseudo_addr, const RawAddress& local_rpa) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(pseudo_addr, BT_TRANSPORT_LE);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
if (btm_cb.ble_ctr_cb.privacy_mode == BTM_PRIVACY_NONE) {
p_acl->conn_addr_type = BLE_ADDR_PUBLIC;
p_acl->conn_addr = *controller_get_interface()->get_address();
} else {
p_acl->conn_addr_type = BLE_ADDR_RANDOM;
if (local_rpa.IsEmpty()) {
p_acl->conn_addr = btm_cb.ble_ctr_cb.addr_mgnt_cb.private_addr;
} else {
p_acl->conn_addr = local_rpa;
}
}
}
bool sco_peer_supports_esco_2m_phy(const RawAddress& remote_bda) {
uint8_t* features = BTM_ReadRemoteFeatures(remote_bda);
if (features == nullptr) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
return false;
}
return HCI_EDR_ESCO_2MPS_SUPPORTED(features);
}
bool sco_peer_supports_esco_3m_phy(const RawAddress& remote_bda) {
uint8_t* features = BTM_ReadRemoteFeatures(remote_bda);
if (features == nullptr) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
return false;
}
return HCI_EDR_ESCO_3MPS_SUPPORTED(features);
}
bool sco_peer_supports_esco_ev3(const RawAddress& remote_bda) {
uint8_t* features = BTM_ReadRemoteFeatures(remote_bda);
if (features == nullptr) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
return false;
}
return HCI_ESCO_EV3_SUPPORTED(features);
}
bool acl_is_switch_role_idle(const RawAddress& bd_addr,
tBT_TRANSPORT transport) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
return p_acl->is_switch_role_idle();
}
/*******************************************************************************
*
* Function BTM_ReadRemoteConnectionAddr
*
* Description This function is read the remote device address currently used
*
* Parameters pseudo_addr: pseudo random address available
* conn_addr:connection address used
* p_addr_type : BD Address type, Public or Random of the address
* used
*
* Returns bool, true if connection to remote device exists, else false
*
******************************************************************************/
bool BTM_ReadRemoteConnectionAddr(const RawAddress& pseudo_addr,
RawAddress& conn_addr,
tBLE_ADDR_TYPE* p_addr_type) {
if (bluetooth::shim::is_gd_shim_enabled()) {
return bluetooth::shim::BTM_ReadRemoteConnectionAddr(pseudo_addr, conn_addr,
p_addr_type);
}
if (bluetooth::shim::is_gd_l2cap_enabled()) {
return bluetooth::shim::L2CA_ReadRemoteConnectionAddr(
pseudo_addr, conn_addr, p_addr_type);
}
bool st = true;
tACL_CONN* p_acl = internal_.btm_bda_to_acl(pseudo_addr, BT_TRANSPORT_LE);
if (p_acl == NULL) {
LOG_WARN("Unable to find active acl");
return false;
}
conn_addr = p_acl->active_remote_addr;
*p_addr_type = p_acl->active_remote_addr_type;
return st;
}
uint8_t acl_link_role_from_handle(uint16_t handle) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
return HCI_ROLE_UNKNOWN;
}
return p_acl->link_role;
}
bool acl_peer_supports_ble_packet_extension(uint16_t hci_handle) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
if (p_acl == nullptr) {
return false;
}
if (!p_acl->peer_le_features_valid) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
return HCI_LE_DATA_LEN_EXT_SUPPORTED(p_acl->peer_le_features);
}
bool acl_peer_supports_ble_2m_phy(uint16_t hci_handle) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
if (p_acl == nullptr) {
return false;
}
if (!p_acl->peer_le_features_valid) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
}
return HCI_LE_2M_PHY_SUPPORTED(p_acl->peer_le_features);
}
bool acl_peer_supports_ble_coded_phy(uint16_t hci_handle) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
if (p_acl == nullptr) {
return false;
}
if (!p_acl->peer_le_features_valid) {
LOG_WARN(
"Checking remote features but remote feature read is "
"incomplete");
return false;
}
return HCI_LE_CODED_PHY_SUPPORTED(p_acl->peer_le_features);
}
void acl_set_disconnect_reason(tHCI_STATUS acl_disc_reason) {
btm_cb.acl_cb_.set_disconnect_reason(acl_disc_reason);
}
bool acl_is_role_switch_allowed() {
return btm_cb.acl_cb_.DefaultLinkPolicy() &
HCI_ENABLE_CENTRAL_PERIPHERAL_SWITCH;
}
uint16_t acl_get_supported_packet_types() {
return btm_cb.acl_cb_.DefaultPacketTypes();
}
bool acl_set_peer_le_features_from_handle(uint16_t hci_handle,
const uint8_t* p) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(hci_handle);
if (p_acl == nullptr) {
return false;
}
STREAM_TO_ARRAY(p_acl->peer_le_features, p, BD_FEATURES_LEN);
p_acl->peer_le_features_valid = true;
LOG_DEBUG("Completed le feature read request");
return true;
}
void on_acl_br_edr_connected(const RawAddress& bda, uint16_t handle,
uint8_t enc_mode) {
if (delayed_role_change_ != nullptr && delayed_role_change_->bd_addr == bda) {
btm_sec_connected(bda, handle, HCI_SUCCESS, enc_mode,
delayed_role_change_->new_role);
} else {
btm_sec_connected(bda, handle, HCI_SUCCESS, enc_mode);
}
delayed_role_change_ = nullptr;
btm_acl_set_paging(false);
l2c_link_hci_conn_comp(HCI_SUCCESS, handle, bda);
constexpr uint16_t link_supervision_timeout = 8000;
BTM_SetLinkSuperTout(bda, link_supervision_timeout);
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
/*
* The legacy code path informs the upper layer via the BTA
* layer after all relevant read_remote_ commands are complete.
* The GD code path has ownership of the read_remote_ commands
* and thus may inform the upper layers about the connection.
*/
NotifyAclLinkUp(*p_acl);
}
void on_acl_br_edr_failed(const RawAddress& bda, tHCI_STATUS status) {
ASSERT_LOG(status != HCI_SUCCESS,
"Successful connection entering failing code path");
if (delayed_role_change_ != nullptr && delayed_role_change_->bd_addr == bda) {
btm_sec_connected(bda, HCI_INVALID_HANDLE, status, false,
delayed_role_change_->new_role);
} else {
btm_sec_connected(bda, HCI_INVALID_HANDLE, status, false);
}
delayed_role_change_ = nullptr;
btm_acl_set_paging(false);
l2c_link_hci_conn_comp(status, HCI_INVALID_HANDLE, bda);
}
void btm_acl_connected(const RawAddress& bda, uint16_t handle,
tHCI_STATUS status, uint8_t enc_mode) {
switch (status) {
case HCI_SUCCESS:
return on_acl_br_edr_connected(bda, handle, enc_mode);
default:
return on_acl_br_edr_failed(bda, status);
}
}
void btm_acl_iso_disconnected(uint16_t handle, tHCI_REASON reason) {
LOG_INFO("ISO disconnection from GD, handle: 0x%02x, reason: 0x%02x", handle,
reason);
bluetooth::hci::IsoManager::GetInstance()->HandleDisconnect(handle, reason);
}
void btm_acl_disconnected(tHCI_STATUS status, uint16_t handle,
tHCI_REASON reason) {
if (status != HCI_SUCCESS) {
LOG_WARN("Received disconnect with error:%s",
hci_error_code_text(status).c_str());
}
/* There can be a case when we rejected PIN code authentication */
/* otherwise save a new reason */
if (btm_get_acl_disc_reason_code() != HCI_ERR_HOST_REJECT_SECURITY) {
acl_set_disconnect_reason(static_cast<tHCI_STATUS>(reason));
}
/* If L2CAP or SCO doesn't know about it, send it to ISO */
if (!l2c_link_hci_disc_comp(handle, reason) &&
!btm_sco_removed(handle, reason)) {
bluetooth::hci::IsoManager::GetInstance()->HandleDisconnect(handle, reason);
}
/* Notify security manager */
btm_sec_disconnected(handle, reason,
"stack::acl::btm_acl::btm_acl_disconnected");
}
void acl_create_classic_connection(const RawAddress& bd_addr,
bool there_are_high_priority_channels,
bool is_bonding) {
return bluetooth::shim::ACL_CreateClassicConnection(bd_addr);
}
void btm_acl_connection_request(const RawAddress& bda, uint8_t* dc) {
btm_sec_conn_req(bda, dc);
l2c_link_hci_conn_req(bda);
}
void acl_accept_connection_request(const RawAddress& bd_addr, uint8_t role) {
btsnd_hcic_accept_conn(bd_addr, role);
}
void acl_reject_connection_request(const RawAddress& bd_addr, uint8_t reason) {
btsnd_hcic_reject_conn(bd_addr, reason);
}
void acl_disconnect_from_handle(uint16_t handle, tHCI_STATUS reason,
std::string comment) {
acl_disconnect_after_role_switch(handle, reason, comment);
}
void acl_disconnect_after_role_switch(uint16_t conn_handle, tHCI_STATUS reason,
std::string comment) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(conn_handle);
if (p_acl == nullptr) {
LOG_ERROR("Sending disconnect for unknown acl:%hu PLEASE FIX", conn_handle);
GetLegacyHciInterface().Disconnect(conn_handle, reason);
return;
}
/* If a role switch is in progress, delay the HCI Disconnect to avoid
* controller problem */
if (p_acl->rs_disc_pending == BTM_SEC_RS_PENDING) {
LOG_DEBUG(
"Role switch in progress - Set DISC Pending flag in "
"btm_sec_send_hci_disconnect "
"to delay disconnect");
p_acl->rs_disc_pending = BTM_SEC_DISC_PENDING;
} else {
LOG_DEBUG("Sending acl disconnect reason:%s [%hu]",
hci_error_code_text(reason).c_str(), reason);
hci_btsnd_hcic_disconnect(*p_acl, reason, comment);
}
}
void acl_send_data_packet_br_edr(const RawAddress& bd_addr, BT_HDR* p_buf) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Acl br_edr data write for unknown device:%s",
PRIVATE_ADDRESS(bd_addr));
osi_free(p_buf);
return;
}
return bluetooth::shim::ACL_WriteData(p_acl->hci_handle, p_buf);
}
void acl_send_data_packet_ble(const RawAddress& bd_addr, BT_HDR* p_buf) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_LE);
if (p_acl == nullptr) {
LOG_WARN("Acl le data write for unknown device:%s",
PRIVATE_ADDRESS(bd_addr));
osi_free(p_buf);
return;
}
return bluetooth::shim::ACL_WriteData(p_acl->hci_handle, p_buf);
}
void acl_write_automatic_flush_timeout(const RawAddress& bd_addr,
uint16_t flush_timeout_in_ticks) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
if (p_acl->flush_timeout_in_ticks == flush_timeout_in_ticks) {
LOG_INFO(
"Ignoring since cached value is same as requested flush_timeout:%hd",
flush_timeout_in_ticks);
return;
}
flush_timeout_in_ticks &= HCI_MAX_AUTOMATIC_FLUSH_TIMEOUT;
p_acl->flush_timeout_in_ticks = flush_timeout_in_ticks;
btsnd_hcic_write_auto_flush_tout(p_acl->hci_handle, flush_timeout_in_ticks);
}
bool acl_create_le_connection_with_id(uint8_t id, const RawAddress& bd_addr) {
tBLE_BD_ADDR address_with_type{
.bda = bd_addr,
.type = BLE_ADDR_PUBLIC,
};
gatt_find_in_device_record(bd_addr, &address_with_type);
LOG_DEBUG("Creating le direct connection to:%s",
PRIVATE_ADDRESS(address_with_type));
if (address_with_type.type == BLE_ADDR_ANONYMOUS) {
LOG_WARN(
"Creating le direct connection to:%s, address type 'anonymous' is "
"invalid",
PRIVATE_ADDRESS(address_with_type));
return false;
}
bluetooth::shim::ACL_AcceptLeConnectionFrom(address_with_type,
/* is_direct */ true);
return true;
}
bool acl_create_le_connection(const RawAddress& bd_addr) {
return acl_create_le_connection_with_id(CONN_MGR_ID_L2CAP, bd_addr);
}
void acl_rcv_acl_data(BT_HDR* p_msg) {
acl_header_t acl_header{
.handle = HCI_INVALID_HANDLE,
.hci_len = 0,
};
const uint8_t* p = (uint8_t*)(p_msg + 1) + p_msg->offset;
STREAM_TO_UINT16(acl_header.handle, p);
acl_header.handle = HCID_GET_HANDLE(acl_header.handle);
STREAM_TO_UINT16(acl_header.hci_len, p);
if (acl_header.hci_len < L2CAP_PKT_OVERHEAD ||
acl_header.hci_len != p_msg->len - sizeof(acl_header)) {
LOG_WARN("Received mismatched hci header length:%u data_len:%zu",
acl_header.hci_len, p_msg->len - sizeof(acl_header));
osi_free(p_msg);
return;
}
l2c_rcv_acl_data(p_msg);
}
void acl_link_segments_xmitted(BT_HDR* p_msg) {
l2c_link_segments_xmitted(p_msg);
}
void acl_packets_completed(uint16_t handle, uint16_t credits) {
l2c_packets_completed(handle, credits);
bluetooth::hci::IsoManager::GetInstance()->HandleGdNumComplDataPkts(handle,
credits);
}
static void acl_parse_num_completed_pkts(uint8_t* p, uint8_t evt_len) {
if (evt_len == 0) {
LOG_ERROR("Received num completed packets with zero length");
return;
}
uint8_t num_handles{0};
STREAM_TO_UINT8(num_handles, p);
if (num_handles > evt_len / (2 * sizeof(uint16_t))) {
android_errorWriteLog(0x534e4554, "141617601");
num_handles = evt_len / (2 * sizeof(uint16_t));
}
for (uint8_t xx = 0; xx < num_handles; xx++) {
uint16_t handle{0};
uint16_t num_packets{0};
STREAM_TO_UINT16(handle, p);
handle = HCID_GET_HANDLE(handle);
STREAM_TO_UINT16(num_packets, p);
acl_packets_completed(handle, num_packets);
}
}
void acl_process_num_completed_pkts(uint8_t* p, uint8_t evt_len) {
acl_parse_num_completed_pkts(p, evt_len);
bluetooth::hci::IsoManager::GetInstance()->HandleNumComplDataPkts(p, evt_len);
}
void acl_process_supported_features(uint16_t handle, uint64_t features) {
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
const uint8_t current_page_number = 0;
memcpy(p_acl->peer_lmp_feature_pages[current_page_number],
(uint8_t*)&features, sizeof(uint64_t));
p_acl->peer_lmp_feature_valid[current_page_number] = true;
LOG_DEBUG(
"Copied supported feature pages handle:%hu current_page_number:%hhu "
"features:%s",
handle, current_page_number,
bd_features_text(p_acl->peer_lmp_feature_pages[current_page_number])
.c_str());
if ((HCI_LMP_EXTENDED_SUPPORTED(p_acl->peer_lmp_feature_pages[0])) &&
(controller_get_interface()
->supports_reading_remote_extended_features())) {
LOG_DEBUG("Waiting for remote extended feature response to arrive");
} else {
LOG_DEBUG("No more remote features outstanding so notify upper layer");
NotifyAclFeaturesReadComplete(*p_acl, current_page_number);
}
}
void acl_process_extended_features(uint16_t handle, uint8_t current_page_number,
uint8_t max_page_number, uint64_t features) {
if (current_page_number > HCI_EXT_FEATURES_PAGE_MAX) {
LOG_WARN("Unable to process current_page_number:%hhu", current_page_number);
return;
}
tACL_CONN* p_acl = internal_.acl_get_connection_from_handle(handle);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return;
}
memcpy(p_acl->peer_lmp_feature_pages[current_page_number],
(uint8_t*)&features, sizeof(uint64_t));
p_acl->peer_lmp_feature_valid[current_page_number] = true;
LOG_DEBUG(
"Copied extended feature pages handle:%hu current_page_number:%hhu "
"max_page_number:%hhu features:%s",
handle, current_page_number, max_page_number,
bd_features_text(p_acl->peer_lmp_feature_pages[current_page_number])
.c_str());
if (max_page_number == 0 || max_page_number == current_page_number) {
NotifyAclFeaturesReadComplete(*p_acl, max_page_number);
}
}
void ACL_RegisterClient(struct acl_client_callback_s* callbacks) {
LOG_DEBUG("UNIMPLEMENTED");
}
void ACL_UnregisterClient(struct acl_client_callback_s* callbacks) {
LOG_DEBUG("UNIMPLEMENTED");
}
bool ACL_SupportTransparentSynchronousData(const RawAddress& bd_addr) {
const tACL_CONN* p_acl =
internal_.btm_bda_to_acl(bd_addr, BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return false;
}
return HCI_LMP_TRANSPNT_SUPPORTED(p_acl->peer_lmp_feature_pages[0]);
}
/**
* Confusingly, immutable device features are stored in the
* ephemeral connection data structure while connection security
* is stored in the device record.
*
* This HACK allows legacy security protocols to work as intended under
* those conditions.
*/
void HACK_acl_check_sm4(tBTM_SEC_DEV_REC& record) {
// Return if we already know this info
if ((record.sm4 & BTM_SM4_TRUE) != BTM_SM4_UNKNOWN) return;
tACL_CONN* p_acl =
internal_.btm_bda_to_acl(record.RemoteAddress(), BT_TRANSPORT_BR_EDR);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl for authentication device:%s",
PRIVATE_ADDRESS(record.RemoteAddress()));
return;
}
// If we have not received the SSP feature record
// we have to wait
if (!p_acl->peer_lmp_feature_valid[1]) {
LOG_WARN(
"Authentication started without extended feature page 1 request "
"response");
return;
}
record.sm4 = (HCI_SSP_HOST_SUPPORTED(p_acl->peer_lmp_feature_pages[1]))
? BTM_SM4_TRUE
: BTM_SM4_KNOWN;
}
tACL_CONN* btm_acl_for_bda(const RawAddress& bd_addr, tBT_TRANSPORT transport) {
tACL_CONN* p_acl = internal_.btm_bda_to_acl(bd_addr, transport);
if (p_acl == nullptr) {
LOG_WARN("Unable to find active acl");
return nullptr;
}
return p_acl;
}
|