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
|
/*****************************************************************************
**
** Name: bta_av_aact.c
**
** Description: This file contains action functions for
** advanced audio/video stream state machine.
** these functions are shared by both audio and video streams.
**
** Copyright (c) 2004-2011, Broadcom Corp., All Rights Reserved.
** Broadcom Bluetooth Core. Proprietary and confidential.
**
*****************************************************************************/
#include "bt_target.h"
#if defined(AV_INCLUDED) && (AV_INCLUDED == TRUE)
#include <string.h>
#include "bta_av_int.h"
#include "avdt_api.h"
#include "bd.h"
#include "utl.h"
#include "l2c_api.h"
#include "l2cdefs.h"
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
#include "bta_ar_api.h"
#endif
/*****************************************************************************
** Constants
*****************************************************************************/
/* the delay time in milliseconds to start service discovery on AVRCP */
#ifndef BTA_AV_RC_DISC_TIME_VAL
#define BTA_AV_RC_DISC_TIME_VAL 3500
#endif
/* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed to be sent */
#ifndef BTA_AV_CLOSE_REQ_TIME_VAL
#define BTA_AV_CLOSE_REQ_TIME_VAL 4000
#endif
/* number to retry on reconfigure failure - some headsets requirs this number to be more than 1 */
#ifndef BTA_AV_RECONFIG_RETRY
#define BTA_AV_RECONFIG_RETRY 6
#endif
/* state machine states */
enum
{
BTA_AV_INIT_SST,
BTA_AV_INCOMING_SST,
BTA_AV_OPENING_SST,
BTA_AV_OPEN_SST,
BTA_AV_RCFG_SST,
BTA_AV_CLOSING_SST
};
/* the call out functions for audio stream */
const tBTA_AV_CO_FUNCTS bta_av_a2d_cos =
{
bta_av_co_audio_init,
bta_av_co_audio_disc_res,
bta_av_co_audio_getconfig,
bta_av_co_audio_setconfig,
bta_av_co_audio_open,
bta_av_co_audio_close,
bta_av_co_audio_start,
bta_av_co_audio_stop,
bta_av_co_audio_src_data_path,
bta_av_co_audio_delay
};
/* ssm action functions for audio stream */
const tBTA_AV_SACT bta_av_a2d_action[] =
{
bta_av_do_disc_a2d, /* BTA_AV_DO_DISC */
bta_av_cleanup, /* BTA_AV_CLEANUP */
bta_av_free_sdb, /* BTA_AV_FREE_SDB */
bta_av_config_ind, /* BTA_AV_CONFIG_IND */
bta_av_disconnect_req, /* BTA_AV_DISCONNECT_REQ */
bta_av_security_req, /* BTA_AV_SECURITY_REQ */
bta_av_security_rsp, /* BTA_AV_SECURITY_RSP */
bta_av_setconfig_rsp, /* BTA_AV_SETCONFIG_RSP */
bta_av_st_rc_timer, /* BTA_AV_ST_RC_TIMER */
bta_av_str_opened, /* BTA_AV_STR_OPENED */
bta_av_security_ind, /* BTA_AV_SECURITY_IND */
bta_av_security_cfm, /* BTA_AV_SECURITY_CFM */
bta_av_do_close, /* BTA_AV_DO_CLOSE */
bta_av_connect_req, /* BTA_AV_CONNECT_REQ */
bta_av_sdp_failed, /* BTA_AV_SDP_FAILED */
bta_av_disc_results, /* BTA_AV_DISC_RESULTS */
bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
bta_av_open_failed, /* BTA_AV_OPEN_FAILED */
bta_av_getcap_results, /* BTA_AV_GETCAP_RESULTS */
bta_av_setconfig_rej, /* BTA_AV_SETCONFIG_REJ */
bta_av_discover_req, /* BTA_AV_DISCOVER_REQ */
bta_av_conn_failed, /* BTA_AV_CONN_FAILED */
bta_av_do_start, /* BTA_AV_DO_START */
bta_av_str_stopped, /* BTA_AV_STR_STOPPED */
bta_av_reconfig, /* BTA_AV_RECONFIG */
bta_av_data_path, /* BTA_AV_DATA_PATH */
bta_av_start_ok, /* BTA_AV_START_OK */
bta_av_start_failed, /* BTA_AV_START_FAILED */
bta_av_str_closed, /* BTA_AV_STR_CLOSED */
bta_av_clr_cong, /* BTA_AV_CLR_CONG */
bta_av_suspend_cfm, /* BTA_AV_SUSPEND_CFM */
bta_av_rcfg_str_ok, /* BTA_AV_RCFG_STR_OK */
bta_av_rcfg_failed, /* BTA_AV_RCFG_FAILED */
bta_av_rcfg_connect, /* BTA_AV_RCFG_CONNECT */
bta_av_rcfg_discntd, /* BTA_AV_RCFG_DISCNTD */
bta_av_suspend_cont, /* BTA_AV_SUSPEND_CONT */
bta_av_rcfg_cfm, /* BTA_AV_RCFG_CFM */
bta_av_rcfg_open, /* BTA_AV_RCFG_OPEN */
bta_av_security_rej, /* BTA_AV_SECURITY_REJ */
bta_av_open_rc, /* BTA_AV_OPEN_RC */
bta_av_chk_2nd_start, /* BTA_AV_CHK_2ND_START */
bta_av_save_caps, /* BTA_AV_SAVE_CAPS */
bta_av_set_use_rc, /* BTA_AV_SET_USE_RC */
bta_av_cco_close, /* BTA_AV_CCO_CLOSE */
bta_av_switch_role, /* BTA_AV_SWITCH_ROLE */
bta_av_role_res, /* BTA_AV_ROLE_RES */
bta_av_delay_co, /* BTA_AV_DELAY_CO */
bta_av_open_at_inc, /* BTA_AV_OPEN_AT_INC */
NULL
};
/* these tables translate AVDT events to SSM events */
static const UINT16 bta_av_stream_evt_ok[] = {
BTA_AV_STR_DISC_OK_EVT, /* AVDT_DISCOVER_CFM_EVT */
BTA_AV_STR_GETCAP_OK_EVT, /* AVDT_GETCAP_CFM_EVT */
BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_CFM_EVT */
BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
BTA_AV_STR_START_OK_EVT, /* AVDT_START_CFM_EVT */
BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
0, /* AVDT_RECONFIG_IND_EVT */
BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
#if (AVDT_REPORTING == TRUE)
BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
#endif
BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
0 /* AVDT_DELAY_REPORT_CFM_EVT */
};
static const UINT16 bta_av_stream_evt_fail[] = {
BTA_AV_STR_DISC_FAIL_EVT, /* AVDT_DISCOVER_CFM_EVT */
BTA_AV_STR_GETCAP_FAIL_EVT, /* AVDT_GETCAP_CFM_EVT */
BTA_AV_STR_OPEN_FAIL_EVT, /* AVDT_OPEN_CFM_EVT */
BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
BTA_AV_STR_START_FAIL_EVT, /* AVDT_START_CFM_EVT */
BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
0, /* AVDT_RECONFIG_IND_EVT */
BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
#if (AVDT_REPORTING == TRUE)
BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
#endif
BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
0 /* AVDT_DELAY_REPORT_CFM_EVT */
};
static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
#if BTA_AV_NUM_STRS > 2
static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
#endif
#if BTA_AV_NUM_STRS > 3
static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
#endif
#if BTA_AV_NUM_STRS > 4
static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
#endif
#if BTA_AV_NUM_STRS > 5
static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
#endif
/* the array of callback functions to receive events from AVDT control channel */
tAVDT_CTRL_CBACK * const bta_av_dt_cback[] =
{
bta_av_stream0_cback
,bta_av_stream1_cback
#if BTA_AV_NUM_STRS > 2
,bta_av_stream2_cback
#endif
#if BTA_AV_NUM_STRS > 3
,bta_av_stream3_cback
#endif
#if BTA_AV_NUM_STRS > 4
,bta_av_stream4_cback
#endif
#if BTA_AV_NUM_STRS > 5
,bta_av_stream5_cback
#endif
};
/*******************************************************************************
**
** Function bta_av_save_addr
**
** Description copy the bd_addr and maybe reset the supported flags
**
**
** Returns void
**
*******************************************************************************/
static void bta_av_save_addr(tBTA_AV_SCB *p_scb, const BD_ADDR b)
{
APPL_TRACE_DEBUG2("bta_av_save_addr r:%d, s:%d",
p_scb->recfg_sup, p_scb->suspend_sup);
if(bdcmp(p_scb->peer_addr, b) != 0)
{
APPL_TRACE_ERROR0("reset flags");
/* a new addr, reset the supported flags */
p_scb->recfg_sup = TRUE;
p_scb->suspend_sup = TRUE;
}
/* do this copy anyway, just in case the first addr matches
* the control block one by accident */
bdcpy(p_scb->peer_addr, b);
}
/*******************************************************************************
**
** Function bta_av_st_rc_timer
**
** Description start the AVRC timer if no RC connection & CT is supported &
** RC is used or
** as ACP (we do not really know if we want AVRC)
**
** Returns void
**
*******************************************************************************/
void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
APPL_TRACE_DEBUG2("bta_av_st_rc_timer rc_handle:%d, use_rc: %d",
p_scb->rc_handle, p_scb->use_rc);
/* for outgoing RC connection as INT/CT */
if( (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
/*(bta_av_cb.features & BTA_AV_FEAT_RCCT) &&*/
(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP)) )
{
if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
else
p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
}
}
/*******************************************************************************
**
** Function bta_av_next_getcap
**
** Description The function gets the capabilities of the next available
** stream found in the discovery results.
**
** Returns TRUE if we sent request to AVDT, FALSE otherwise.
**
*******************************************************************************/
static BOOLEAN bta_av_next_getcap(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
int i;
tAVDT_GETCAP_REQ *p_req;
BOOLEAN sent_cmd = FALSE;
for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++)
{
/* steam not in use, is a sink, and is the right media type (audio/video) */
if ((p_scb->sep_info[i].in_use == FALSE) &&
(p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
(p_scb->sep_info[i].media_type == p_scb->media_type))
{
p_scb->sep_info_idx = i;
/* we got a stream; get its capabilities */
if (p_scb->p_cap == NULL)
{
p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
}
if (p_scb->p_cap == NULL)
{
i = p_scb->num_seps;
break;
}
if (p_scb->avdt_version >= AVDT_VERSION_SYNC)
{
p_req = AVDT_GetAllCapReq;
}
else
{
p_req = AVDT_GetCapReq;
}
(*p_req)(p_scb->peer_addr,
p_scb->sep_info[i].seid,
p_scb->p_cap, bta_av_dt_cback[p_scb->hdi]);
sent_cmd = TRUE;
break;
}
}
/* if no streams available then stream open fails */
if (!sent_cmd)
{
bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
}
return sent_cmd;
}
/*******************************************************************************
**
** Function bta_av_proc_stream_evt
**
** Description Utility function to compose stream events.
**
** Returns void
**
*******************************************************************************/
void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data, int index)
{
tBTA_AV_STR_MSG *p_msg;
UINT16 sec_len = 0;
tBTA_AV_SCB *p_scb = bta_av_cb.p_scb[index];
int xx;
if (p_data)
{
if (event == AVDT_SECURITY_IND_EVT)
{
sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN) ?
p_data->security_ind.len : BTA_AV_SECURITY_MAX_LEN;
}
else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0)
{
sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN) ?
p_data->security_cfm.len : BTA_AV_SECURITY_MAX_LEN;
}
}
if (p_scb && (p_msg = (tBTA_AV_STR_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_STR_MSG) + sec_len))) != NULL)
{
/* copy event data, bd addr, and handle to event message buffer */
p_msg->hdr.offset = 0;
if (bd_addr != NULL)
{
bdcpy(p_msg->bd_addr, bd_addr);
APPL_TRACE_DEBUG6(" bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
bd_addr[0], bd_addr[1],
bd_addr[2], bd_addr[3],
bd_addr[4], bd_addr[5]);
}
if (p_data != NULL)
{
memcpy(&p_msg->msg, p_data, sizeof (tAVDT_CTRL));
/* copy config params to event message buffer */
switch (event)
{
case AVDT_CONFIG_IND_EVT:
/* We might have 2 SEP signallings(A2DP + VDP) with one peer device on one L2CAP.
* If we already have a signalling connection with the bd_addr and the streaming
* SST is at INIT state, change it to INCOMING state to handle the signalling
* from the 2nd SEP. */
if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) && (bta_av_is_scb_init(p_scb)))
{
bta_av_set_scb_sst_incoming (p_scb);
/* When ACP_CONNECT_EVT was received, we put first available scb to incoming state.
* Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and set its state to
* incoming which we do it above.
* We also have to set the old p_scb state to init to be used later */
for (xx = 0; xx < BTA_AV_NUM_STRS; xx++)
{
if ((bta_av_cb.p_scb[xx]) && (xx != index))
{
if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST)
{
bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
bta_av_cb.p_scb[xx]->coll_mask = 0;
break;
}
}
}
}
memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
break;
case AVDT_SECURITY_IND_EVT:
p_msg->msg.security_ind.p_data = (UINT8 *) (p_msg + 1);
memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data, sec_len);
break;
case AVDT_SECURITY_CFM_EVT:
p_msg->msg.security_cfm.p_data = (UINT8 *) (p_msg + 1);
if (p_data->hdr.err_code == 0)
{
memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data, sec_len);
}
break;
case AVDT_SUSPEND_IND_EVT:
p_msg->msg.hdr.err_code = 0;
break;
default:
break;
}
}
else
p_msg->msg.hdr.err_code = 0;
/* look up application event */
if ((p_data == NULL) || (p_data->hdr.err_code == 0))
{
p_msg->hdr.event = bta_av_stream_evt_ok[event];
}
else
{
p_msg->hdr.event = bta_av_stream_evt_fail[event];
}
p_msg->initiator = FALSE;
if (event == AVDT_SUSPEND_CFM_EVT)
p_msg->initiator = TRUE;
APPL_TRACE_EVENT1("hndl:x%x", p_scb->hndl);
p_msg->hdr.layer_specific = p_scb->hndl;
p_msg->handle = handle;
p_msg->avdt_event = event;
bta_sys_sendmsg(p_msg);
}
/* coverity[var_deref_model]: Variable "p_data" tracked as NULL was passed to function "bta_av_conn_cback" that dereferences it.
* false-positive: bta_av_conn_cback only processes AVDT_CONNECT_IND_EVT and AVDT_DISCONNECT_IND_EVT event
* these 2 events always have associated p_data
*/
bta_av_conn_cback(handle, bd_addr, event, p_data);
}
/*******************************************************************************
**
** Function bta_av_stream0_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream0_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
}
/*******************************************************************************
**
** Function bta_av_stream1_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream1_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
}
#if BTA_AV_NUM_STRS > 2
/*******************************************************************************
**
** Function bta_av_stream2_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream2_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
}
#endif
#if BTA_AV_NUM_STRS > 3
/*******************************************************************************
**
** Function bta_av_stream3_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream3_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
}
#endif
/*******************************************************************************
**
** Function bta_av_stream4_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
#if BTA_AV_NUM_STRS > 4
static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream4_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
}
#endif
/*******************************************************************************
**
** Function bta_av_stream5_cback
**
** Description This is the AVDTP callback function for stream events.
**
** Returns void
**
*******************************************************************************/
#if BTA_AV_NUM_STRS > 5
static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
{
APPL_TRACE_EVENT2("bta_av_stream5_cback avdt_handle: %d event=0x%x", handle, event);
bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
}
#endif
/*******************************************************************************
**
** Function bta_av_a2d_sdp_cback
**
** Description A2DP service discovery callback.
**
** Returns void
**
*******************************************************************************/
static void bta_av_a2d_sdp_cback(BOOLEAN found, tA2D_Service *p_service)
{
tBTA_AV_SDP_RES *p_msg;
tBTA_AV_SCB *p_scb;
if ((p_msg = (tBTA_AV_SDP_RES *) GKI_getbuf(sizeof(tBTA_AV_SDP_RES))) != NULL)
{
p_msg->hdr.event = (found) ? BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
if (p_scb)
{
if (found && (p_service != NULL))
p_scb->avdt_version = p_service->avdt_version;
else
p_scb->avdt_version = 0x00;
p_msg->hdr.layer_specific = bta_av_cb.handle;
bta_sys_sendmsg(p_msg);
}
else
{
APPL_TRACE_ERROR1 ("bta_av_a2d_sdp_cback, no scb found for handle(0x%x)", bta_av_cb.handle);
}
}
}
/*******************************************************************************
**
** Function bta_av_adjust_seps_idx
**
** Description adjust the sep_idx
**
** Returns
**
*******************************************************************************/
static void bta_av_adjust_seps_idx(tBTA_AV_SCB *p_scb)
{
int xx;
APPL_TRACE_DEBUG1("bta_av_adjust_seps_idx codec_type: %d", p_scb->codec_type);
for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
{
APPL_TRACE_DEBUG2("av_handle: %d codec_type: %d",
p_scb->seps[xx].av_handle, p_scb->seps[xx].codec_type);
if(p_scb->seps[xx].av_handle && p_scb->codec_type == p_scb->seps[xx].codec_type)
{
p_scb->sep_idx = xx;
p_scb->avdt_handle = p_scb->seps[xx].av_handle;
break;
}
}
}
/*******************************************************************************
**
** Function bta_av_switch_role
**
** Description Switch role was not started and a timer was started.
** another attempt to switch role now - still opening.
**
** Returns void
**
*******************************************************************************/
void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_RS_RES switch_res = BTA_AV_RS_NONE;
tBTA_AV_API_OPEN *p_buf = &p_scb->q_info.open;
APPL_TRACE_DEBUG1("bta_av_switch_role wait:x%x", p_scb->wait);
if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
/* clear the masks set when the timer is started */
p_scb->wait &= ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START);
if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
{
if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
{
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
}
else
{
/* this should not happen in theory. Just in case...
* continue to do_disc_a2d */
switch_res = BTA_AV_RS_DONE;
}
}
else
{
/* report failure on OPEN */
switch_res = BTA_AV_RS_FAIL;
}
if (switch_res != BTA_AV_RS_NONE)
{
if (bta_av_cb.rs_idx == (p_scb->hdi + 1))
{
bta_av_cb.rs_idx = 0;
}
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
p_scb->q_tag = 0;
p_buf->switch_res = switch_res;
bta_av_do_disc_a2d(p_scb, (tBTA_AV_DATA *)p_buf);
}
}
/*******************************************************************************
**
** Function bta_av_role_res
**
** Description Handle the role changed event
**
**
** Returns void
**
*******************************************************************************/
void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
BOOLEAN initiator = FALSE;
tBTA_AV_START start;
tBTA_AV_OPEN av_open;
APPL_TRACE_DEBUG3("bta_av_role_res q_tag:%d, wait:x%x, role:x%x", p_scb->q_tag, p_scb->wait, p_scb->role);
if (p_scb->role & BTA_AV_ROLE_START_INT)
initiator = TRUE;
if (p_scb->q_tag == BTA_AV_Q_TAG_START)
{
if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED)
{
p_scb->role &= ~BTA_AV_ROLE_START_INT;
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
if (p_data->role_res.hci_status != HCI_SUCCESS)
{
bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
/* start failed because of role switch. */
start.chnl = p_scb->chnl;
start.status = BTA_AV_FAIL_ROLE;
start.hndl = p_scb->hndl;
start.initiator = initiator;
(*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
}
else
{
bta_av_start_ok(p_scb, p_data);
}
}
else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
}
else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
{
if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN)
{
p_scb->role &= ~BTA_AV_ROLE_START_INT;
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
if (p_data->role_res.hci_status != HCI_SUCCESS)
{
/* Open failed because of role switch. */
bdcpy(av_open.bd_addr, p_scb->peer_addr);
av_open.chnl = p_scb->chnl;
av_open.hndl = p_scb->hndl;
start.status = BTA_AV_FAIL_ROLE;
(*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *)&av_open);
}
else
{
/* Continue av open process */
p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
}
}
else
{
APPL_TRACE_WARNING2 ("Unexpected role switch event: q_tag = %d wait = %d", p_scb->q_tag, p_scb->wait);
}
}
APPL_TRACE_DEBUG2("wait:x%x, role:x%x", p_scb->wait, p_scb->role);
}
/*******************************************************************************
**
** Function bta_av_delay_co
**
** Description Call the delay call-out function to report the delay report
** from SNK
**
** Returns void
**
*******************************************************************************/
void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
}
/*******************************************************************************
**
** Function bta_av_do_disc_a2d
**
** Description Do service discovery for A2DP.
**
** Returns void
**
*******************************************************************************/
void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
BOOLEAN ok_continue = FALSE;
tA2D_SDP_DB_PARAMS db_params;
UINT16 attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
ATTR_ID_PROTOCOL_DESC_LIST,
ATTR_ID_BT_PROFILE_DESC_LIST};
APPL_TRACE_DEBUG3("bta_av_do_disc_a2d use_rc: %d rs:%d, oc:%d",
p_data->api_open.use_rc, p_data->api_open.switch_res, bta_av_cb.audio_open_cnt);
memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
switch(p_data->api_open.switch_res)
{
case BTA_AV_RS_NONE:
if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
{
/* waiting for role switch result. save the api to control block */
memcpy(&p_scb->q_info.open, &p_data->api_open, sizeof(tBTA_AV_API_OPEN));
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
}
else
{
ok_continue = TRUE;
}
break;
case BTA_AV_RS_FAIL:
/* report a new failure event */
p_scb->open_status = BTA_AV_FAIL_ROLE;
bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
break;
case BTA_AV_RS_OK:
p_data = (tBTA_AV_DATA *)&p_scb->q_info.open;
/* continue to open if link role is ok */
if (bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
{
ok_continue = TRUE;
}
else
{
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
}
break;
case BTA_AV_RS_DONE:
ok_continue = TRUE;
break;
}
APPL_TRACE_DEBUG3("ok_continue: %d wait:x%x, q_tag: %d", ok_continue, p_scb->wait, p_scb->q_tag);
if (!ok_continue)
return;
/* clear the role switch bits */
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
if (p_scb->wait & BTA_AV_WAIT_CHECK_RC)
{
p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
}
if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
{
L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
if (bta_av_cb.audio_open_cnt == 1)
{
/* there's already an A2DP connection. do not allow switch */
bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
}
}
/* store peer addr other parameters */
bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
p_scb->sec_mask = p_data->api_open.sec_mask;
p_scb->use_rc = p_data->api_open.use_rc;
bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
/* allocate discovery database */
if (p_scb->p_disc_db == NULL)
{
p_scb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
}
/* only one A2D find service is active at a time */
bta_av_cb.handle = p_scb->hndl;
if(p_scb->p_disc_db)
{
/* set up parameters */
db_params.db_len = BTA_AV_DISC_BUF_SIZE;
db_params.num_attr = 3;
db_params.p_db = p_scb->p_disc_db;
db_params.p_attrs = attr_list;
if(A2D_FindService(UUID_SERVCLASS_AUDIO_SINK, p_scb->peer_addr, &db_params,
bta_av_a2d_sdp_cback) == A2D_SUCCESS)
{
return;
}
}
/* when the code reaches here, either the DB is NULL
* or A2D_FindService is not successful */
bta_av_a2d_sdp_cback(FALSE, NULL);
}
/*******************************************************************************
**
** Function bta_av_cleanup
**
** Description cleanup AV stream control block.
**
** Returns void
**
*******************************************************************************/
void bta_av_cleanup(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_CONN_CHG msg;
int xx;
UINT8 role = BTA_AV_ROLE_AD_INT;
APPL_TRACE_DEBUG0("bta_av_cleanup");
/* free any buffers */
utl_freebuf((void **) &p_scb->p_cap);
utl_freebuf((void **) &p_scb->p_disc_db);
p_scb->avdt_version = 0;
/* initialize some control block variables */
p_scb->open_status = BTA_AV_SUCCESS;
/* if de-registering shut everything down */
msg.hdr.layer_specific = p_scb->hndl;
p_scb->started = FALSE;
p_scb->cong = FALSE;
p_scb->role = role;
p_scb->cur_psc_mask = 0;
p_scb->wait = 0;
p_scb->num_disc_snks = 0;
bta_sys_stop_timer(&p_scb->timer);
if (p_scb->deregistring)
{
/* remove stream */
for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
{
if(p_scb->seps[xx].av_handle)
AVDT_RemoveStream(p_scb->seps[xx].av_handle);
p_scb->seps[xx].av_handle = 0;
}
bta_av_dereg_comp((tBTA_AV_DATA *) &msg);
}
else
{
/* report stream closed to main SM */
msg.is_up = FALSE;
bdcpy(msg.peer_addr, p_scb->peer_addr);
bta_av_conn_chg((tBTA_AV_DATA *) &msg);
}
}
/*******************************************************************************
**
** Function bta_av_free_sdb
**
** Description Free service discovery db buffer.
**
** Returns void
**
*******************************************************************************/
void bta_av_free_sdb(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
utl_freebuf((void **) &p_scb->p_disc_db);
}
/*******************************************************************************
**
** Function bta_av_config_ind
**
** Description Handle a stream configuration indication from the peer.
**
** Returns void
**
*******************************************************************************/
void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_CI_SETCONFIG setconfig;
tAVDT_SEP_INFO *p_info;
tAVDT_CFG *p_evt_cfg = &p_data->str_msg.cfg;
UINT8 psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
p_scb->codec_type = p_evt_cfg->codec_info[BTA_AV_CODEC_TYPE_IDX];
bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
/* Clear collision mask */
p_scb->coll_mask = 0;
bta_sys_stop_timer(&bta_av_cb.acp_sig_tmr);
/* if no codec parameters in configuration, fail */
if ((p_evt_cfg->num_codec == 0) ||
/* or the peer requests for a service we do not support */
((psc_mask != p_scb->cfg.psc_mask) &&
(psc_mask != (p_scb->cfg.psc_mask&~AVDT_PSC_DELAY_RPT))) )
{
setconfig.hndl = p_scb->hndl; /* we may not need this */
setconfig.err_code = AVDT_ERR_UNSUP_CFG;
bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT, (tBTA_AV_DATA *) &setconfig);
}
else
{
p_info = &p_scb->sep_info[0];
p_info->in_use = 0;
p_info->media_type = p_scb->media_type;
p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
p_info->tsep = AVDT_TSEP_SNK;
p_scb->role |= BTA_AV_ROLE_AD_ACP;
p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
p_scb->use_rc = TRUE;
else
p_scb->use_rc = FALSE;
p_scb->num_seps = 1;
p_scb->sep_info_idx = 0;
APPL_TRACE_DEBUG3("bta_av_config_ind: SEID: %d use_rc: %d cur_psc_mask:0x%x", p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
p_evt_cfg->codec_info,
p_info->seid,
p_scb->peer_addr,
p_evt_cfg->num_protect,
p_evt_cfg->protect_info);
}
}
/*******************************************************************************
**
** Function bta_av_disconnect_req
**
** Description Disconnect AVDTP connection.
**
** Returns void
**
*******************************************************************************/
void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_RCB *p_rcb;
APPL_TRACE_DEBUG1("bta_av_disconnect_req conn_lcb: 0x%x", bta_av_cb.conn_lcb);
bta_sys_stop_timer(&bta_av_cb.sig_tmr);
bta_sys_stop_timer(&p_scb->timer);
if(bta_av_cb.conn_lcb)
{
p_rcb = bta_av_get_rcb_by_shdl((UINT8)(p_scb->hdi + 1));
if (p_rcb)
bta_av_del_rc(p_rcb);
AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
}
else
{
bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
}
}
/*******************************************************************************
**
** Function bta_av_security_req
**
** Description Send an AVDTP security request.
**
** Returns void
**
*******************************************************************************/
void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
{
AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
p_data->api_protect_req.len);
}
}
/*******************************************************************************
**
** Function bta_av_security_rsp
**
** Description Send an AVDTP security response.
**
** Returns void
**
*******************************************************************************/
void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
{
AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->api_protect_rsp.error_code,
p_data->api_protect_rsp.p_data, p_data->api_protect_rsp.len);
}
else
{
AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC,
NULL, 0);
}
}
/*******************************************************************************
**
** Function bta_av_setconfig_rsp
**
** Description setconfig is OK
**
** Returns void
**
*******************************************************************************/
void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 num = p_data->ci_setconfig.num_seid + 1;
/* we like this codec_type. find the sep_idx */
bta_av_adjust_seps_idx(p_scb);
APPL_TRACE_DEBUG2("bta_av_setconfig_rsp: sep_idx: %d cur_psc_mask:0x%x", p_scb->sep_idx, p_scb->cur_psc_mask);
AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->ci_setconfig.err_code,
p_data->ci_setconfig.category);
bta_sys_stop_timer(&bta_av_cb.sig_tmr);
if(p_data->ci_setconfig.err_code == AVDT_SUCCESS)
{
p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
if(p_data->ci_setconfig.recfg_needed)
p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
APPL_TRACE_ERROR3("bta_av_setconfig_rsp recfg_needed:%d role:x%x num:%d",
p_data->ci_setconfig.recfg_needed, p_scb->role, num);
/* callout module tells BTA the number of "good" SEPs and their SEIDs.
* getcap on these SEID */
p_scb->num_seps = num;
if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
p_scb->avdt_version = AVDT_VERSION_SYNC;
/* We need to know all the SEPs on SNK */
/* Getcap for all SEPs will follow after Discover */
APPL_TRACE_DEBUG0("bta_av_setconfig_rsp: start DISCOVER");
bta_av_discover_req(p_scb, NULL);
}
}
/*******************************************************************************
**
** Function bta_av_str_opened
**
** Description Stream opened OK (incoming/outgoing).
**
** Returns void
**
*******************************************************************************/
void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_CONN_CHG msg;
tBTA_AV_OPEN open;
UINT8 *p;
UINT16 mtu;
msg.hdr.layer_specific = p_scb->hndl;
msg.is_up = TRUE;
bdcpy(msg.peer_addr, p_scb->peer_addr);
p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
bta_av_conn_chg((tBTA_AV_DATA *) &msg);
/* set the congestion flag, so AV would not send media packets by accident */
p_scb->cong = TRUE;
p_scb->stream_mtu = p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
APPL_TRACE_DEBUG3("bta_av_str_opened l2c_cid: 0x%x stream_mtu: %d mtu: %d",
p_scb->l2c_cid, p_scb->stream_mtu, mtu);
if(mtu == 0 || mtu > p_scb->stream_mtu)
mtu = p_scb->stream_mtu;
/* Set the media channel as medium priority */
L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
L2CA_SetChnlFlushability (p_scb->l2c_cid, TRUE);
bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
p_scb->l2c_bufs = 0;
p_scb->p_cos->open(p_scb->hndl,
p_scb->codec_type, p_scb->cfg.codec_info, mtu);
{
/* TODO check if other audio channel is open.
* If yes, check if reconfig is needed
* Rigt now we do not do this kind of checking.
* BTA-AV is INT for 2nd audio connection.
* The application needs to make sure the current codec_info is proper.
* If one audio connection is open and another SNK attempts to connect to AV,
* the connection will be rejected.
*/
/* check if other audio channel is started. If yes, start */
bdcpy(open.bd_addr, p_scb->peer_addr);
open.chnl = p_scb->chnl;
open.hndl = p_scb->hndl;
open.status = BTA_AV_SUCCESS;
open.starting = bta_av_chk_start(p_scb);
open.edr = 0;
if( NULL != (p = BTM_ReadRemoteFeatures(p_scb->peer_addr)))
{
if(HCI_EDR_ACL_2MPS_SUPPORTED(p))
open.edr |= BTA_AV_EDR_2MBPS;
if(HCI_EDR_ACL_3MPS_SUPPORTED(p))
open.edr |= BTA_AV_EDR_3MBPS;
}
#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
#endif
(*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
if(open.starting)
{
bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
}
}
}
/*******************************************************************************
**
** Function bta_av_security_ind
**
** Description Handle an AVDTP security indication.
**
** Returns void
**
*******************************************************************************/
void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_PROTECT_REQ protect_req;
p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
{
protect_req.chnl = p_scb->chnl;
protect_req.hndl = p_scb->hndl;
/*
APPL_TRACE_EVENT1("sec ind handle: x%x", protect_req.hndl);
*/
protect_req.p_data = p_data->str_msg.msg.security_ind.p_data;
protect_req.len = p_data->str_msg.msg.security_ind.len;
(*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV *) &protect_req);
}
/* app doesn't support security indication; respond with failure */
else
{
AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL, 0);
}
}
/*******************************************************************************
**
** Function bta_av_security_cfm
**
** Description Handle an AVDTP security confirm.
**
** Returns void
**
*******************************************************************************/
void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_PROTECT_RSP protect_rsp;
if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
{
protect_rsp.chnl = p_scb->chnl;
protect_rsp.hndl = p_scb->hndl;
protect_rsp.p_data = p_data->str_msg.msg.security_cfm.p_data;
protect_rsp.len = p_data->str_msg.msg.security_cfm.len;
protect_rsp.err_code= p_data->str_msg.msg.hdr.err_code;
(*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV *) &protect_rsp);
}
}
/*******************************************************************************
**
** Function bta_av_do_close
**
** Description Close stream.
**
** Returns void
**
*******************************************************************************/
void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
/* stop stream if started */
if (p_scb->co_started)
{
bta_av_str_stopped(p_scb, NULL);
}
bta_sys_stop_timer(&bta_av_cb.sig_tmr);
/* close stream */
p_scb->started = FALSE;
/* drop the buffers queued in L2CAP */
L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
AVDT_CloseReq(p_scb->avdt_handle);
/* just in case that the link is congested, link is flow controled by peer or
* for whatever reason the the close request can not be sent in time.
* when this timer expires, AVDT_DisconnectReq will be called to disconnect the link
*/
bta_sys_start_timer(&p_scb->timer,
(UINT16)BTA_AV_API_CLOSE_EVT,
BTA_AV_CLOSE_REQ_TIME_VAL);
}
/*******************************************************************************
**
** Function bta_av_connect_req
**
** Description Connect AVDTP connection.
**
** Returns void
**
*******************************************************************************/
void bta_av_connect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
utl_freebuf((void **) &p_scb->p_disc_db);
if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
{
/* SNK initiated L2C connection while SRC was doing SDP. */
/* Wait until timeout to check if SNK starts signalling. */
APPL_TRACE_EVENT1("bta_av_connect_req: coll_mask = 0x%2X", p_scb->coll_mask);
return;
}
AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
}
/*******************************************************************************
**
** Function bta_av_sdp_failed
**
** Description Service discovery failed.
**
** Returns void
**
*******************************************************************************/
void bta_av_sdp_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
if (!p_scb->open_status)
p_scb->open_status = BTA_AV_FAIL_SDP;
utl_freebuf((void **) &p_scb->p_disc_db);
bta_av_str_closed(p_scb, p_data);
}
/*******************************************************************************
**
** Function bta_av_disc_results
**
** Description Handle the AVDTP discover results. Search through the
** results and find the first available stream, and get
** its capabilities.
**
** Returns void
**
*******************************************************************************/
void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 num_snks = 0, i;
/* store number of stream endpoints returned */
p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
for (i = 0; i < p_scb->num_seps; i++)
{
/* steam not in use, is a sink, and is audio */
if ((p_scb->sep_info[i].in_use == FALSE) &&
(p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
(p_scb->sep_info[i].media_type == p_scb->media_type))
{
num_snks++;
}
}
p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
p_scb->num_disc_snks = num_snks;
/* if we got any */
if (p_scb->num_seps > 0)
{
/* initialize index into discovery results */
p_scb->sep_info_idx = 0;
/* get the capabilities of the first available stream */
bta_av_next_getcap(p_scb, p_data);
}
/* else we got discover response but with no streams; we're done */
else
{
bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
}
}
/*******************************************************************************
**
** Function bta_av_disc_res_as_acp
**
** Description Handle the AVDTP discover results. Search through the
** results and find the first available stream, and get
** its capabilities.
**
** Returns void
**
*******************************************************************************/
void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 num_snks = 0, i;
/* store number of stream endpoints returned */
p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
for (i = 0; i < p_scb->num_seps; i++)
{
/* steam is a sink, and is audio */
if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
(p_scb->sep_info[i].media_type == p_scb->media_type))
{
p_scb->sep_info[i].in_use = FALSE;
num_snks++;
}
}
p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
p_scb->num_disc_snks = num_snks;
/* if we got any */
if (p_scb->num_seps > 0)
{
/* initialize index into discovery results */
p_scb->sep_info_idx = 0;
/* get the capabilities of the first available stream */
bta_av_next_getcap(p_scb, p_data);
}
/* else we got discover response but with no streams; we're done */
else
{
bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
}
}
/*******************************************************************************
**
** Function bta_av_save_caps
**
** Description report the SNK SEP capabilities to application
**
** Returns void
**
*******************************************************************************/
void bta_av_save_caps(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tAVDT_CFG cfg;
tAVDT_SEP_INFO *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
UINT8 old_wait = p_scb->wait;
BOOLEAN getcap_done = FALSE;
APPL_TRACE_DEBUG3("bta_av_save_caps num_seps:%d sep_info_idx:%d wait:x%x",
p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
/* let application know the capability of the SNK */
p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info[BTA_AV_CODEC_TYPE_IDX],
cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
&cfg.num_protect, cfg.protect_info);
p_scb->sep_info_idx++;
if(p_scb->num_seps > p_scb->sep_info_idx)
{
/* Some devices have seps at the end of the discover list, which is not */
/* matching media type(video not audio). */
/* In this case, we are done with getcap without sending another */
/* request to AVDT. */
if (!bta_av_next_getcap(p_scb, p_data))
getcap_done = TRUE;
}
else
getcap_done = TRUE;
if (getcap_done)
{
/* we are done getting capabilities. restore the p_cb->sep_info_idx */
p_scb->sep_info_idx = 0;
p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON|BTA_AV_WAIT_ACP_CAPS_STARTED);
if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED)
{
bta_av_start_ok (p_scb, NULL);
}
}
}
/*******************************************************************************
**
** Function bta_av_set_use_rc
**
** Description set to use AVRC for this stream control block.
**
** Returns void
**
*******************************************************************************/
void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
p_scb->use_rc = TRUE;
}
/*******************************************************************************
**
** Function bta_av_cco_close
**
** Description call close call-out function.
**
** Returns void
**
*******************************************************************************/
void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT16 mtu;
mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
}
/*******************************************************************************
**
** Function bta_av_open_failed
**
** Description Failed to open an AVDT stream
**
** Returns void
**
*******************************************************************************/
void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
p_scb->open_status = BTA_AV_FAIL_STREAM;
bta_av_cco_close(p_scb, p_data);
AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
}
/*******************************************************************************
**
** Function bta_av_getcap_results
**
** Description Handle the AVDTP get capabilities results. Check the codec
** type and see if it matches ours. If it does not, get the
** capabilities of the next stream, if any.
**
** Returns void
**
*******************************************************************************/
void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tAVDT_CFG cfg;
UINT8 media_type;
tAVDT_SEP_INFO *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
cfg.num_codec = 1;
cfg.num_protect = p_scb->p_cap->num_protect;
memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
media_type = p_scb->p_cap->codec_info[BTA_AV_MEDIA_TYPE_IDX] >> 4;
APPL_TRACE_DEBUG1("num_codec %d", p_scb->p_cap->num_codec);
APPL_TRACE_DEBUG2("media type x%x, x%x", media_type, p_scb->media_type);
#if AVDT_MULTIPLEXING == TRUE
APPL_TRACE_DEBUG2("mux x%x, x%x", cfg.mux_mask, p_scb->p_cap->mux_mask);
#endif
/* if codec present and we get a codec configuration */
if ((p_scb->p_cap->num_codec != 0) &&
(media_type == p_scb->media_type) &&
(p_scb->p_cos->getcfg(p_scb->hndl, p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX],
cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
&cfg.num_protect, cfg.protect_info) == 0))
{
#if AVDT_MULTIPLEXING == TRUE
cfg.mux_mask &= p_scb->p_cap->mux_mask;
APPL_TRACE_DEBUG1("mux_mask used x%x", cfg.mux_mask);
#endif
/* save copy of codec type and configuration */
p_scb->codec_type = cfg.codec_info[BTA_AV_CODEC_TYPE_IDX];
memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
bta_av_adjust_seps_idx(p_scb);
/* use only the services peer supports */
cfg.psc_mask &= p_scb->p_cap->psc_mask;
p_scb->cur_psc_mask = cfg.psc_mask;
/* open the stream */
AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
if (!bta_av_is_rcfg_sst(p_scb))
{
/* free capabilities buffer */
utl_freebuf((void **) &p_scb->p_cap);
}
}
else
{
/* try the next stream, if any */
p_scb->sep_info_idx++;
bta_av_next_getcap(p_scb, p_data);
}
}
/*******************************************************************************
**
** Function bta_av_setconfig_rej
**
** Description Send AVDTP set config reject.
**
** Returns void
**
*******************************************************************************/
void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_REJECT reject;
APPL_TRACE_DEBUG0("bta_av_setconfig_rej");
AVDT_ConfigRsp(p_data->str_msg.handle, p_data->str_msg.msg.hdr.label, AVDT_ERR_BAD_STATE, 0);
bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
reject.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV *) &reject);
}
/*******************************************************************************
**
** Function bta_av_discover_req
**
** Description Send an AVDTP discover request to the peer.
**
** Returns void
**
*******************************************************************************/
void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
/* send avdtp discover request */
AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
}
/*******************************************************************************
**
** Function bta_av_conn_failed
**
** Description AVDTP connection failed.
**
** Returns void
**
*******************************************************************************/
void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
p_scb->open_status = BTA_AV_FAIL_STREAM;
bta_av_str_closed(p_scb, p_data);
}
/*******************************************************************************
**
** Function bta_av_do_start
**
** Description Start stream.
**
** Returns void
**
*******************************************************************************/
void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 policy = HCI_ENABLE_SNIFF_MODE;
UINT8 cur_role;
APPL_TRACE_DEBUG3("bta_av_do_start sco_occupied:%d, role:x%x, started:%d", bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
if (bta_av_cb.sco_occupied)
{
bta_av_start_failed(p_scb, p_data);
return;
}
/* disallow role switch during streaming, only if we are the master role
* i.e. allow role switch, if we are slave.
* It would not hurt us, if the peer device wants us to be master */
if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
(cur_role == BTM_ROLE_MASTER) )
{
policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
}
bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
if ((p_scb->started == FALSE) && ((p_scb->role & BTA_AV_ROLE_START_INT) == 0))
{
p_scb->role |= BTA_AV_ROLE_START_INT;
bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
AVDT_StartReq(&p_scb->avdt_handle, 1);
}
else
{
bta_av_start_ok(p_scb, NULL);
}
APPL_TRACE_DEBUG1("role:x%x", p_scb->role);
}
/*******************************************************************************
**
** Function bta_av_str_stopped
**
** Description Stream stopped.
**
** Returns void
**
*******************************************************************************/
void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_SUSPEND suspend_rsp;
UINT8 start = p_scb->started;
BOOLEAN sus_evt = TRUE;
BT_HDR *p_buf;
UINT8 policy = HCI_ENABLE_SNIFF_MODE;
APPL_TRACE_ERROR1("bta_av_str_stopped:audio_open_cnt=%d", bta_av_cb.audio_open_cnt);
bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
if(p_scb->co_started)
{
bta_av_stream_chg(p_scb, FALSE);
p_scb->co_started = FALSE;
p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
}
/* if q_info.a2d is not empty, drop it now */
if(BTA_AV_CHNL_AUDIO == p_scb->chnl)
{
while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
GKI_freebuf(p_buf);
/* drop the audio buffers queued in L2CAP */
if(p_data && p_data->api_stop.flush)
L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
}
suspend_rsp.chnl = p_scb->chnl;
suspend_rsp.hndl = p_scb->hndl;
if (p_data && p_data->api_stop.suspend)
{
APPL_TRACE_DEBUG2("suspending: %d, sup:%d", start, p_scb->suspend_sup);
if ((start) && (p_scb->suspend_sup))
{
sus_evt = FALSE;
p_scb->l2c_bufs = 0;
AVDT_SuspendReq(&p_scb->avdt_handle, 1);
}
if(sus_evt)
{
suspend_rsp.status = BTA_AV_SUCCESS;
suspend_rsp.initiator = TRUE;
(*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
}
}
else
{
(*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV *) &suspend_rsp);
}
}
/*******************************************************************************
**
** Function bta_av_reconfig
**
** Description process the reconfigure request.
** save the parameter in control block and
** suspend, reconfigure or close the stream
**
** Returns void
**
*******************************************************************************/
void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tAVDT_CFG *p_cfg;
tBTA_AV_API_STOP stop;
tBTA_AV_RECONFIG evt;
tBTA_AV_API_RCFG *p_rcfg = &p_data->api_reconfig;
APPL_TRACE_DEBUG4("bta_av_reconfig r:%d, s:%d idx: %d (o:%d)",
p_scb->recfg_sup, p_scb->suspend_sup,
p_scb->rcfg_idx, p_scb->sep_info_idx);
p_scb->num_recfg = 0;
/* store the new configuration in control block */
if (p_scb->p_cap == NULL)
{
p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
}
if((p_cfg = p_scb->p_cap) == NULL)
{
/* report failure */
evt.status = BTA_AV_FAIL_RESOURCES;
evt.chnl = p_scb->chnl;
evt.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
/* this event is not possible in this state.
* use it to bring the SSM back to open state */
bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_OK_EVT, NULL);
return;
}
/*if(bta_av_cb.features & BTA_AV_FEAT_RCCT)*/
bta_sys_stop_timer(&p_scb->timer);
memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
p_cfg->num_protect = p_rcfg->num_protect;
memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
p_scb->rcfg_idx = p_rcfg->sep_info_idx;
p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
/* if the requested index differs from the current one, we can only close/open */
if ((p_scb->rcfg_idx == p_scb->sep_info_idx) &&
(p_rcfg->suspend)&& (p_scb->recfg_sup) && (p_scb->suspend_sup))
{
if(p_scb->started)
{
stop.flush = FALSE;
stop.suspend = TRUE;
bta_av_str_stopped(p_scb, (tBTA_AV_DATA *)&stop);
}
else
{
APPL_TRACE_DEBUG0("Reconfig");
AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
}
}
else
{
/* close the stream */
APPL_TRACE_DEBUG1("close/open num_protect: %d", p_cfg->num_protect);
if(p_scb->started)
bta_av_str_stopped(p_scb, NULL);
p_scb->started = FALSE;
/* drop the buffers queued in L2CAP */
L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
AVDT_CloseReq(p_scb->avdt_handle);
}
}
/*******************************************************************************
**
** Function bta_av_data_path
**
** Description Handle stream data path.
**
** Returns void
**
*******************************************************************************/
void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
BT_HDR *p_buf;
UINT32 data_len;
UINT32 timestamp;
BOOLEAN new_buf = FALSE;
UINT8 m_pt = 0x60 | p_scb->codec_type;
if (!p_scb->cong)
{
/*
APPL_TRACE_ERROR1("q: %d", p_scb->l2c_bufs);
*/
//Always get the current number of bufs que'd up
p_scb->l2c_bufs = (UINT8)L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
p_buf = (BT_HDR *)GKI_dequeue (&p_scb->q_info.a2d);
if(p_buf)
{
/* use q_info.a2d data, read the timestamp */
timestamp = *(UINT32 *)(p_buf + 1);
}
else
{
new_buf = TRUE;
/* q_info.a2d empty, call co_data, dup data to other channels */
p_buf = (BT_HDR *)p_scb->p_cos->data(p_scb->codec_type, &data_len,
×tamp);
if (p_buf)
{
/* use the offset area for the time stamp */
*(UINT32 *)(p_buf + 1) = timestamp;
/* dup the data to other channels */
bta_av_dup_audio_buf(p_scb, p_buf);
}
}
if(p_buf)
{
if(p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM))
{
/* there's a buffer, just queue it to L2CAP */
/* There's no need to increment it here, it is always read from L2CAP see above */
/* p_scb->l2c_bufs++; */
/*
APPL_TRACE_ERROR1("qw: %d", p_scb->l2c_bufs);
*/
AVDT_WriteReq(p_scb->avdt_handle, p_buf, timestamp, m_pt);
p_scb->cong = TRUE;
}
else
{
/* there's a buffer, but L2CAP does not seem to be moving data */
if(new_buf)
{
/* just got this buffer from co_data,
* put it in queue */
GKI_enqueue(&p_scb->q_info.a2d, p_buf);
}
else
{
/* just dequeue it from the q_info.a2d */
if(p_scb->q_info.a2d.count < 3)
{
/* put it back to the queue */
GKI_enqueue_head (&p_scb->q_info.a2d, p_buf);
}
else
{
/* too many buffers in q_info.a2d, drop it. */
bta_av_co_audio_drop(p_scb->hndl);
GKI_freebuf(p_buf);
}
}
}
}
}
}
/*******************************************************************************
**
** Function bta_av_start_ok
**
** Description Stream started.
**
** Returns void
**
*******************************************************************************/
void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_START start;
tBTA_AV_API_STOP stop;
BOOLEAN initiator = FALSE;
BOOLEAN suspend = FALSE;
UINT16 flush_to;
UINT8 new_role = p_scb->role;
BT_HDR hdr;
APPL_TRACE_DEBUG2("bta_av_start_ok wait:x%x, role:x%x", p_scb->wait, p_scb->role);
p_scb->started = TRUE;
if (p_scb->sco_suspend)
{
p_scb->sco_suspend = FALSE;
}
if (new_role & BTA_AV_ROLE_START_INT)
initiator = TRUE;
if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED)
{
/* role switch has failed */
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
p_data = (tBTA_AV_DATA *)&hdr;
hdr.offset = BTA_AV_RS_FAIL;
}
APPL_TRACE_DEBUG1("wait:x%x", p_scb->wait);
if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE))
{
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
if (p_data->hdr.offset == BTA_AV_RS_FAIL)
{
bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
start.chnl = p_scb->chnl;
start.status = BTA_AV_FAIL_ROLE;
start.hndl = p_scb->hndl;
start.initiator = initiator;
(*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
return;
}
}
if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
p_scb->q_tag = BTA_AV_Q_TAG_START;
else
{
/* The wait flag may be set here while we are already master on the link */
/* this could happen if a role switch complete event occurred during reconfig */
/* if we are now master on the link, there is no need to wait for the role switch, */
/* complete anymore so we can clear the wait for role switch flag */
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
}
if (p_scb->wait & (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START))
{
p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
p_scb->q_tag = BTA_AV_Q_TAG_START;
}
if (p_scb->wait & BTA_AV_WAIT_ACP_CAPS_ON)
{
p_scb->wait |= BTA_AV_WAIT_ACP_CAPS_STARTED;
}
if (p_scb->wait)
{
APPL_TRACE_DEBUG2("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
return;
}
/* tell role manager to check M/S role */
bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
if(p_scb->media_type == AVDT_MEDIA_AUDIO)
{
/* in normal logic, conns should be bta_av_cb.audio_count - 1,
* However, bta_av_stream_chg is not called to increase bta_av_cb.audio_count yet.
* If the code were to be re-arranged for some reasons, this number may need to be changed
*/
p_scb->co_started = bta_av_cb.audio_open_cnt;
flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
}
else
{
flush_to = p_bta_av_cfg->video_flush_to;
}
L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to );
/* clear the congestion flag */
p_scb->cong = FALSE;
if (new_role & BTA_AV_ROLE_START_INT)
{
new_role &= ~BTA_AV_ROLE_START_INT;
}
else if ((new_role & BTA_AV_ROLE_AD_ACP) && (new_role & BTA_AV_ROLE_SUSPEND_OPT))
{
suspend = TRUE;
}
if (!suspend)
{
p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
bta_av_stream_chg(p_scb, TRUE);
}
{
p_scb->role = new_role;
p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
p_scb->p_cos->start(p_scb->hndl, p_scb->codec_type);
p_scb->co_started = TRUE;
APPL_TRACE_ERROR2("bta_av_start_ok suspending: %d, role:x%x", suspend, p_scb->role);
start.suspending = suspend;
start.initiator = initiator;
start.chnl = p_scb->chnl;
start.status = BTA_AV_SUCCESS;
start.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
if(suspend)
{
p_scb->role |= BTA_AV_ROLE_SUSPEND;
p_scb->cong = TRUE; /* do not allow the media data to go through */
/* do not duplicate the media packets to this channel */
p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
p_scb->co_started = FALSE;
stop.flush = FALSE;
stop.suspend = TRUE;
bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
}
}
}
/*******************************************************************************
**
** Function bta_av_start_failed
**
** Description Stream start failed.
**
** Returns void
**
*******************************************************************************/
void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_START start;
if(p_scb->started == FALSE && p_scb->co_started == FALSE)
{
/* if start failed, clear role */
p_scb->role &= ~BTA_AV_ROLE_START_INT;
bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
start.chnl = p_scb->chnl;
start.status = BTA_AV_FAIL;
start.initiator = TRUE;
start.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
}
bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_SNIFF_MODE|HCI_ENABLE_MASTER_SLAVE_SWITCH), p_scb->peer_addr);
p_scb->sco_suspend = FALSE;
}
/*******************************************************************************
**
** Function bta_av_str_closed
**
** Description Stream closed.
**
** Returns void
**
*******************************************************************************/
void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV data;
tBTA_AV_EVT event;
UINT16 mtu;
UINT8 policy = HCI_ENABLE_SNIFF_MODE;
if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
if (bta_av_cb.audio_open_cnt <= 1)
{
/* last connection - restore the allow switch flag */
L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
}
if (p_scb->open_status)
{
/* must be failure when opening the stream */
bdcpy(data.open.bd_addr, p_scb->peer_addr);
data.open.status = p_scb->open_status;
data.open.chnl = p_scb->chnl;
data.open.hndl = p_scb->hndl;
event = BTA_AV_OPEN_EVT;
p_scb->open_status = BTA_AV_SUCCESS;
bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
bta_av_cleanup(p_scb, p_data);
(*bta_av_cb.p_cback)(event, &data);
}
else
{
/* do stop if we were started */
if (p_scb->co_started)
{
bta_av_str_stopped(p_scb, NULL);
}
/* Update common mtu shared by remaining connectons */
mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
{
p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
data.close.chnl = p_scb->chnl;
data.close.hndl = p_scb->hndl;
event = BTA_AV_CLOSE_EVT;
bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
bta_av_cleanup(p_scb, p_data);
(*bta_av_cb.p_cback)(event, &data);
}
}
}
/*******************************************************************************
**
** Function bta_av_clr_cong
**
** Description Clear stream congestion flag.
**
** Returns void
**
*******************************************************************************/
void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
if(p_scb->co_started)
p_scb->cong = FALSE;
}
/*******************************************************************************
**
** Function bta_av_suspend_cfm
**
** Description process the suspend response
**
** Returns void
**
*******************************************************************************/
void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_SUSPEND suspend_rsp;
UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
UINT8 policy = HCI_ENABLE_SNIFF_MODE;
APPL_TRACE_DEBUG2 ("bta_av_suspend_cfm:audio_open_cnt = %d, err_code = %d",
bta_av_cb.audio_open_cnt, err_code);
suspend_rsp.status = BTA_AV_SUCCESS;
if(err_code)
{
p_scb->suspend_sup = FALSE;
suspend_rsp.status = BTA_AV_FAIL;
APPL_TRACE_ERROR0 ("bta_av_suspend_cfm: suspend failed, closing connection");
/* SUSPEND failed. Close connection. */
bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
}
else
{
/* only set started to FALSE when suspend is successful */
p_scb->started = FALSE;
}
if(p_scb->role & BTA_AV_ROLE_SUSPEND)
{
p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
p_scb->cong = FALSE;
}
bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
/* in case that we received suspend_ind, we may need to call co_stop here */
if(p_scb->co_started)
{
bta_av_stream_chg(p_scb, FALSE);
{
p_scb->co_started = FALSE;
p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
}
L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
}
{
suspend_rsp.chnl = p_scb->chnl;
suspend_rsp.hndl = p_scb->hndl;
suspend_rsp.initiator = p_data->str_msg.initiator;
(*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
}
}
/*******************************************************************************
**
** Function bta_av_rcfg_str_ok
**
** Description report reconfigure successful
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_RECONFIG evt;
p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
APPL_TRACE_DEBUG1("bta_av_rcfg_str_ok: l2c_cid: %d", p_scb->l2c_cid);
/* rc listen */
bta_av_st_rc_timer(p_scb, NULL);
utl_freebuf((void **)&p_scb->p_cap);
/* No need to keep the role bits once reconfig is done. */
p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
p_scb->role &= ~BTA_AV_ROLE_START_INT;
{
/* reconfigure success */
evt.status = BTA_AV_SUCCESS;
evt.chnl = p_scb->chnl;
evt.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
}
}
/*******************************************************************************
**
** Function bta_av_rcfg_failed
**
** Description process reconfigure failed
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_RECONFIG evt;
APPL_TRACE_DEBUG2("bta_av_rcfg_failed num_recfg: %d, conn_lcb:0x%x",
p_scb->num_recfg, bta_av_cb.conn_lcb);
if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
{
bta_av_cco_close(p_scb, p_data);
/* report failure */
evt.status = BTA_AV_FAIL_STREAM;
evt.chnl = p_scb->chnl;
evt.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
/* go to closing state */
bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
}
else
{
/* open failed. try again */
p_scb->num_recfg++;
if(bta_av_cb.conn_lcb)
{
AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
}
else
{
bta_av_connect_req(p_scb, NULL);
}
}
}
/*******************************************************************************
**
** Function bta_av_rcfg_connect
**
** Description stream closed. reconnect the stream
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
p_scb->cong = FALSE;
p_scb->num_recfg++;
APPL_TRACE_DEBUG1("bta_av_rcfg_connect num_recfg: %d", p_scb->num_recfg);
if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
{
/* let bta_av_rcfg_failed report fail */
bta_av_rcfg_failed(p_scb, NULL);
}
else
AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
}
/*******************************************************************************
**
** Function bta_av_rcfg_discntd
**
** Description AVDT disconnected. reconnect the stream
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_RECONFIG evt;
APPL_TRACE_DEBUG1("bta_av_rcfg_discntd num_recfg: %d", p_scb->num_recfg);
p_scb->num_recfg++;
if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
{
/* report failure */
evt.status = BTA_AV_FAIL_STREAM;
evt.chnl = p_scb->chnl;
evt.hndl = p_scb->hndl;
(*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
/* report close event & go to init state */
bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
}
else
AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
}
/*******************************************************************************
**
** Function bta_av_suspend_cont
**
** Description received the suspend response.
** continue to reconfigure the stream
**
** Returns void
**
*******************************************************************************/
void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
tBTA_AV_RECONFIG evt;
p_scb->started = FALSE;
p_scb->cong = FALSE;
if(err_code)
{
if(AVDT_ERR_CONNECT == err_code)
{
/* report failure */
evt.status = BTA_AV_FAIL;
(*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
}
else
{
APPL_TRACE_ERROR0("suspend rejected, try close");
p_scb->suspend_sup = FALSE;
/* drop the buffers queued in L2CAP */
L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
AVDT_CloseReq(p_scb->avdt_handle);
}
}
else
{
APPL_TRACE_DEBUG0("bta_av_suspend_cont calling AVDT_ReconfigReq");
/* reconfig the stream */
AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
}
}
/*******************************************************************************
**
** Function bta_av_rcfg_cfm
**
** Description if reconfigure is successful, report the event
** otherwise, close the stream.
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
/*
APPL_TRACE_DEBUG0("bta_av_rcfg_cfm");
*/
if(err_code)
{
APPL_TRACE_ERROR0("reconfig rejected, try close");
p_scb->recfg_sup = FALSE;
/* started flag is FALSE when reconfigure command is sent */
/* drop the buffers queued in L2CAP */
L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
AVDT_CloseReq(p_scb->avdt_handle);
}
else
{
/* take the SSM back to OPEN state */
bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
}
}
/*******************************************************************************
**
** Function bta_av_rcfg_open
**
** Description AVDT is connected. open the stream with the new configuration
**
** Returns void
**
*******************************************************************************/
void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
APPL_TRACE_DEBUG1("bta_av_rcfg_open, num_disc_snks = %d", p_scb->num_disc_snks);
if (p_scb->num_disc_snks == 0)
{
/* Need to update call-out module so that it will be ready for discover */
p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
/* send avdtp discover request */
AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
}
else
{
p_scb->codec_type = p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX];
memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
/* we may choose to use a different SEP at reconfig.
* adjust the sep_idx now */
bta_av_adjust_seps_idx(p_scb);
/* open the stream with the new config */
p_scb->sep_info_idx = p_scb->rcfg_idx;
AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
}
}
/*******************************************************************************
**
** Function bta_av_security_rej
**
** Description Send an AVDTP security reject.
**
** Returns void
**
*******************************************************************************/
void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
NULL, 0);
}
/*******************************************************************************
**
** Function bta_av_chk_2nd_start
**
** Description check if this is 2nd stream and if it needs to be started.
** This function needs to be kept very similar to bta_av_chk_start
**
** Returns void
**
*******************************************************************************/
void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_SCB *p_scbi;
int i;
BOOLEAN new_started = FALSE;
if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2))
{
/* more than one audio channel is connected */
if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT))
{
/* this channel does not need to be reconfigured.
* if there is other channel streaming, start the stream now */
for(i=0; i<BTA_AV_NUM_STRS; i++)
{
p_scbi = bta_av_cb.p_scb[i];
if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
{
if (!new_started)
{
/* start the new stream */
new_started = TRUE;
bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
}
/* may need to update the flush timeout of this already started stream */
if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
{
p_scbi->co_started = bta_av_cb.audio_open_cnt;
L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
}
}
}
}
}
}
/*******************************************************************************
**
** Function bta_av_open_rc
**
** Description Send a message to main SM to open RC channel.
**
** Returns void
**
*******************************************************************************/
void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_START start;
APPL_TRACE_DEBUG3("bta_av_open_rc use_rc: %d, wait: x%x role:x%x", p_scb->use_rc, p_scb->wait, p_scb->role);
if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) && (p_scb->q_tag == BTA_AV_Q_TAG_START))
{
/* waiting for role switch for some reason & the timer expires */
if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
{
APPL_TRACE_ERROR0 ("failed to start streaming for role management reasons!!");
bta_sys_stop_timer(&p_scb->timer);
start.chnl = p_scb->chnl;
start.status = BTA_AV_FAIL_ROLE;
start.initiator = TRUE;
start.hndl = p_scb->hndl;
p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
bta_av_cb.rs_idx = 0;
(*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
}
else
{
/* role switch is done. continue to start streaming */
bta_av_cb.rs_idx = 0;
p_data->hdr.offset = BTA_AV_RS_OK;
bta_av_start_ok (p_scb, p_data);
}
return;
}
if(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP) )
{
if(bta_av_cb.disc)
{
/* AVRC discover db is in use */
if(p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE)
{
/* AVRC channel is not connected. delay a little bit */
if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
else
p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
}
}
else
{
/* use main SM for AVRC SDP activities */
bta_av_rc_disc((UINT8)(p_scb->hdi + 1));
}
}
else
{
if(BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle)
{
/* the open API said that this handle does not want a RC connection.
* disconnect it now */
AVRC_Close(p_scb->rc_handle);
}
}
}
/*******************************************************************************
**
** Function bta_av_open_at_inc
**
** Description This function is called if API open is called by application
** while state-machine is at incoming state.
**
** Returns void
**
*******************************************************************************/
void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
{
tBTA_AV_API_OPEN *p_buf;
memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
{
p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
/* API open will be handled at timeout if SNK did not start signalling. */
/* API open will be ignored if SNK starts signalling. */
}
else
{
/* SNK did not start signalling, API was called N seconds timeout. */
/* We need to switch to INIT state and start opening connection. */
p_scb->coll_mask = 0;
bta_av_set_scb_sst_init (p_scb);
if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
{
memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
bta_sys_sendmsg(p_buf);
}
}
}
#endif /* AV_INCLUDED */
|