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
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2010 Nokia Corporation
* Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
* Copyright (C) 2011 Texas Instruments, Inc.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <assert.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <gdbus/gdbus.h>
#include "lib/uuid.h"
#include "src/plugin.h"
#include "src/adapter.h"
#include "src/device.h"
#include "src/profile.h"
#include "src/service.h"
#include "src/log.h"
#include "src/error.h"
#include "src/sdpd.h"
#include "src/dbus-common.h"
#include "avctp.h"
#include "avrcp.h"
#include "control.h"
#include "player.h"
#include "transport.h"
/* Company IDs for vendor dependent commands */
#define IEEEID_BTSIG 0x001958
/* Status codes */
#define AVRCP_STATUS_INVALID_COMMAND 0x00
#define AVRCP_STATUS_INVALID_PARAM 0x01
#define AVRCP_STATUS_PARAM_NOT_FOUND 0x02
#define AVRCP_STATUS_INTERNAL_ERROR 0x03
#define AVRCP_STATUS_SUCCESS 0x04
#define AVRCP_STATUS_OUT_OF_BOUNDS 0x0b
#define AVRCP_STATUS_INVALID_PLAYER_ID 0x11
#define AVRCP_STATUS_PLAYER_NOT_BROWSABLE 0x12
#define AVRCP_STATUS_NO_AVAILABLE_PLAYERS 0x15
#define AVRCP_STATUS_ADDRESSED_PLAYER_CHANGED 0x16
/* Packet types */
#define AVRCP_PACKET_TYPE_SINGLE 0x00
#define AVRCP_PACKET_TYPE_START 0x01
#define AVRCP_PACKET_TYPE_CONTINUING 0x02
#define AVRCP_PACKET_TYPE_END 0x03
/* PDU types for metadata transfer */
#define AVRCP_GET_CAPABILITIES 0x10
#define AVRCP_LIST_PLAYER_ATTRIBUTES 0X11
#define AVRCP_LIST_PLAYER_VALUES 0x12
#define AVRCP_GET_CURRENT_PLAYER_VALUE 0x13
#define AVRCP_SET_PLAYER_VALUE 0x14
#define AVRCP_GET_PLAYER_ATTRIBUTE_TEXT 0x15
#define AVRCP_GET_PLAYER_VALUE_TEXT 0x16
#define AVRCP_DISPLAYABLE_CHARSET 0x17
#define AVRCP_CT_BATTERY_STATUS 0x18
#define AVRCP_GET_ELEMENT_ATTRIBUTES 0x20
#define AVRCP_GET_PLAY_STATUS 0x30
#define AVRCP_REGISTER_NOTIFICATION 0x31
#define AVRCP_REQUEST_CONTINUING 0x40
#define AVRCP_ABORT_CONTINUING 0x41
#define AVRCP_SET_ABSOLUTE_VOLUME 0x50
#define AVRCP_SET_BROWSED_PLAYER 0x70
#define AVRCP_GET_FOLDER_ITEMS 0x71
#define AVRCP_CHANGE_PATH 0x72
#define AVRCP_GET_ITEM_ATTRIBUTES 0x73
#define AVRCP_PLAY_ITEM 0x74
#define AVRCP_SEARCH 0x80
#define AVRCP_ADD_TO_NOW_PLAYING 0x90
#define AVRCP_GENERAL_REJECT 0xA0
/* Capabilities for AVRCP_GET_CAPABILITIES pdu */
#define CAP_COMPANY_ID 0x02
#define CAP_EVENTS_SUPPORTED 0x03
#define AVRCP_REGISTER_NOTIFICATION_PARAM_LENGTH 5
#define AVRCP_GET_CAPABILITIES_PARAM_LENGTH 1
#define AVRCP_FEATURE_CATEGORY_1 0x0001
#define AVRCP_FEATURE_CATEGORY_2 0x0002
#define AVRCP_FEATURE_CATEGORY_3 0x0004
#define AVRCP_FEATURE_CATEGORY_4 0x0008
#define AVRCP_FEATURE_PLAYER_SETTINGS 0x0010
#define AVRCP_FEATURE_BROWSING 0x0040
#define AVRCP_BATTERY_STATUS_NORMAL 0
#define AVRCP_BATTERY_STATUS_WARNING 1
#define AVRCP_BATTERY_STATUS_CRITICAL 2
#define AVRCP_BATTERY_STATUS_EXTERNAL 3
#define AVRCP_BATTERY_STATUS_FULL_CHARGE 4
#define AVRCP_CHARSET_UTF8 106
#define AVRCP_BROWSING_TIMEOUT 1
#if __BYTE_ORDER == __LITTLE_ENDIAN
struct avrcp_header {
uint8_t company_id[3];
uint8_t pdu_id;
uint8_t packet_type:2;
uint8_t rsvd:6;
uint16_t params_len;
uint8_t params[0];
} __attribute__ ((packed));
#define AVRCP_HEADER_LENGTH 7
#elif __BYTE_ORDER == __BIG_ENDIAN
struct avrcp_header {
uint8_t company_id[3];
uint8_t pdu_id;
uint8_t rsvd:6;
uint8_t packet_type:2;
uint16_t params_len;
uint8_t params[0];
} __attribute__ ((packed));
#define AVRCP_HEADER_LENGTH 7
#else
#error "Unknown byte order"
#endif
#define AVRCP_MTU (AVC_MTU - AVC_HEADER_LENGTH)
#define AVRCP_PDU_MTU (AVRCP_MTU - AVRCP_HEADER_LENGTH)
struct avrcp_browsing_header {
uint8_t pdu_id;
uint16_t param_len;
uint8_t params[0];
} __attribute__ ((packed));
#define AVRCP_BROWSING_HEADER_LENGTH 3
struct avrcp_server {
struct btd_adapter *adapter;
uint32_t tg_record_id;
uint32_t ct_record_id;
GSList *players;
GSList *sessions;
};
struct pending_pdu {
uint8_t pdu_id;
GList *attr_ids;
uint16_t offset;
};
struct pending_list_items {
GSList *items;
uint32_t start;
uint32_t end;
};
struct avrcp_player {
struct avrcp_server *server;
GSList *sessions;
uint16_t id;
uint8_t scope;
uint64_t uid;
uint16_t uid_counter;
bool browsed;
uint8_t *features;
char *path;
struct pending_list_items *p;
char *change_path;
struct avrcp_player_cb *cb;
void *user_data;
GDestroyNotify destroy;
};
struct avrcp_data {
struct avrcp_player *player;
uint16_t version;
int features;
GSList *players;
};
struct avrcp {
struct avrcp_server *server;
struct avctp *conn;
struct btd_device *dev;
struct avrcp_data *target;
struct avrcp_data *controller;
const struct passthrough_handler *passthrough_handlers;
const struct control_pdu_handler *control_handlers;
unsigned int passthrough_id;
unsigned int control_id;
unsigned int browsing_id;
unsigned int browsing_timer;
uint16_t supported_events;
uint16_t registered_events;
uint8_t transaction;
uint8_t transaction_events[AVRCP_EVENT_LAST + 1];
struct pending_pdu *pending_pdu;
};
struct passthrough_handler {
uint8_t op;
bool (*func) (struct avrcp *session);
};
struct control_pdu_handler {
uint8_t pdu_id;
uint8_t code;
uint8_t (*func) (struct avrcp *session, struct avrcp_header *pdu,
uint8_t transaction);
};
static GSList *servers = NULL;
static unsigned int avctp_id = 0;
/* Company IDs supported by this device */
static uint32_t company_ids[] = {
IEEEID_BTSIG,
};
static void avrcp_register_notification(struct avrcp *session, uint8_t event);
static sdp_record_t *avrcp_ct_record(void)
{
sdp_list_t *svclass_id, *pfseq, *apseq, *apseq1, *root;
uuid_t root_uuid, l2cap, avctp, avrct, avrctr;
sdp_profile_desc_t profile[1];
sdp_list_t *aproto, *aproto1, *proto[2], *proto1[2];
sdp_record_t *record;
sdp_data_t *psm[2], *version, *features;
uint16_t lp = AVCTP_CONTROL_PSM, ap = AVCTP_BROWSING_PSM;
uint16_t avrcp_ver = 0x0105, avctp_ver = 0x0103;
uint16_t feat = ( AVRCP_FEATURE_CATEGORY_1 |
AVRCP_FEATURE_CATEGORY_2 |
AVRCP_FEATURE_CATEGORY_3 |
AVRCP_FEATURE_CATEGORY_4 |
AVRCP_FEATURE_BROWSING);
record = sdp_record_alloc();
if (!record)
return NULL;
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root = sdp_list_append(NULL, &root_uuid);
sdp_set_browse_groups(record, root);
/* Service Class ID List */
sdp_uuid16_create(&avrct, AV_REMOTE_SVCLASS_ID);
svclass_id = sdp_list_append(NULL, &avrct);
sdp_uuid16_create(&avrctr, AV_REMOTE_CONTROLLER_SVCLASS_ID);
svclass_id = sdp_list_append(svclass_id, &avrctr);
sdp_set_service_classes(record, svclass_id);
/* Protocol Descriptor List */
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto[0] = sdp_list_append(NULL, &l2cap);
psm[0] = sdp_data_alloc(SDP_UINT16, &lp);
proto[0] = sdp_list_append(proto[0], psm[0]);
apseq = sdp_list_append(NULL, proto[0]);
sdp_uuid16_create(&avctp, AVCTP_UUID);
proto[1] = sdp_list_append(NULL, &avctp);
version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
proto[1] = sdp_list_append(proto[1], version);
apseq = sdp_list_append(apseq, proto[1]);
aproto = sdp_list_append(NULL, apseq);
sdp_set_access_protos(record, aproto);
/* Additional Protocol Descriptor List */
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto1[0] = sdp_list_append(NULL, &l2cap);
psm[1] = sdp_data_alloc(SDP_UINT16, &ap);
proto1[0] = sdp_list_append(proto1[0], psm[1]);
apseq1 = sdp_list_append(NULL, proto1[0]);
sdp_uuid16_create(&avctp, AVCTP_UUID);
proto1[1] = sdp_list_append(NULL, &avctp);
proto1[1] = sdp_list_append(proto1[1], version);
apseq1 = sdp_list_append(apseq1, proto1[1]);
aproto1 = sdp_list_append(NULL, apseq1);
sdp_set_add_access_protos(record, aproto1);
/* Bluetooth Profile Descriptor List */
sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
profile[0].version = avrcp_ver;
pfseq = sdp_list_append(NULL, &profile[0]);
sdp_set_profile_descs(record, pfseq);
features = sdp_data_alloc(SDP_UINT16, &feat);
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
sdp_set_info_attr(record, "AVRCP CT", NULL, NULL);
free(psm[0]);
free(psm[1]);
free(version);
sdp_list_free(proto[0], NULL);
sdp_list_free(proto[1], NULL);
sdp_list_free(apseq, NULL);
sdp_list_free(proto1[0], NULL);
sdp_list_free(proto1[1], NULL);
sdp_list_free(aproto1, NULL);
sdp_list_free(apseq1, NULL);
sdp_list_free(pfseq, NULL);
sdp_list_free(aproto, NULL);
sdp_list_free(root, NULL);
sdp_list_free(svclass_id, NULL);
return record;
}
static sdp_record_t *avrcp_tg_record(void)
{
sdp_list_t *svclass_id, *pfseq, *apseq, *root, *apseq_browsing;
uuid_t root_uuid, l2cap, avctp, avrtg;
sdp_profile_desc_t profile[1];
sdp_list_t *aproto_control, *proto_control[2];
sdp_record_t *record;
sdp_data_t *psm_control, *version, *features, *psm_browsing;
sdp_list_t *aproto_browsing, *proto_browsing[2] = {0};
uint16_t lp = AVCTP_CONTROL_PSM;
uint16_t lp_browsing = AVCTP_BROWSING_PSM;
uint16_t avrcp_ver = 0x0104, avctp_ver = 0x0103;
uint16_t feat = ( AVRCP_FEATURE_CATEGORY_1 |
AVRCP_FEATURE_CATEGORY_2 |
AVRCP_FEATURE_CATEGORY_3 |
AVRCP_FEATURE_CATEGORY_4 |
AVRCP_FEATURE_BROWSING |
AVRCP_FEATURE_PLAYER_SETTINGS );
record = sdp_record_alloc();
if (!record)
return NULL;
sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
root = sdp_list_append(NULL, &root_uuid);
sdp_set_browse_groups(record, root);
/* Service Class ID List */
sdp_uuid16_create(&avrtg, AV_REMOTE_TARGET_SVCLASS_ID);
svclass_id = sdp_list_append(NULL, &avrtg);
sdp_set_service_classes(record, svclass_id);
/* Protocol Descriptor List */
sdp_uuid16_create(&l2cap, L2CAP_UUID);
proto_control[0] = sdp_list_append(NULL, &l2cap);
psm_control = sdp_data_alloc(SDP_UINT16, &lp);
proto_control[0] = sdp_list_append(proto_control[0], psm_control);
apseq = sdp_list_append(NULL, proto_control[0]);
sdp_uuid16_create(&avctp, AVCTP_UUID);
proto_control[1] = sdp_list_append(NULL, &avctp);
version = sdp_data_alloc(SDP_UINT16, &avctp_ver);
proto_control[1] = sdp_list_append(proto_control[1], version);
apseq = sdp_list_append(apseq, proto_control[1]);
aproto_control = sdp_list_append(NULL, apseq);
sdp_set_access_protos(record, aproto_control);
proto_browsing[0] = sdp_list_append(NULL, &l2cap);
psm_browsing = sdp_data_alloc(SDP_UINT16, &lp_browsing);
proto_browsing[0] = sdp_list_append(proto_browsing[0], psm_browsing);
apseq_browsing = sdp_list_append(NULL, proto_browsing[0]);
proto_browsing[1] = sdp_list_append(NULL, &avctp);
proto_browsing[1] = sdp_list_append(proto_browsing[1], version);
apseq_browsing = sdp_list_append(apseq_browsing, proto_browsing[1]);
aproto_browsing = sdp_list_append(NULL, apseq_browsing);
sdp_set_add_access_protos(record, aproto_browsing);
/* Bluetooth Profile Descriptor List */
sdp_uuid16_create(&profile[0].uuid, AV_REMOTE_PROFILE_ID);
profile[0].version = avrcp_ver;
pfseq = sdp_list_append(NULL, &profile[0]);
sdp_set_profile_descs(record, pfseq);
features = sdp_data_alloc(SDP_UINT16, &feat);
sdp_attr_add(record, SDP_ATTR_SUPPORTED_FEATURES, features);
sdp_set_info_attr(record, "AVRCP TG", NULL, NULL);
free(psm_browsing);
sdp_list_free(proto_browsing[0], NULL);
sdp_list_free(proto_browsing[1], NULL);
sdp_list_free(apseq_browsing, NULL);
sdp_list_free(aproto_browsing, NULL);
free(psm_control);
free(version);
sdp_list_free(proto_control[0], NULL);
sdp_list_free(proto_control[1], NULL);
sdp_list_free(apseq, NULL);
sdp_list_free(aproto_control, NULL);
sdp_list_free(pfseq, NULL);
sdp_list_free(root, NULL);
sdp_list_free(svclass_id, NULL);
return record;
}
static unsigned int attr_get_max_val(uint8_t attr)
{
switch (attr) {
case AVRCP_ATTRIBUTE_EQUALIZER:
return AVRCP_EQUALIZER_ON;
case AVRCP_ATTRIBUTE_REPEAT_MODE:
return AVRCP_REPEAT_MODE_GROUP;
case AVRCP_ATTRIBUTE_SHUFFLE:
return AVRCP_SHUFFLE_GROUP;
case AVRCP_ATTRIBUTE_SCAN:
return AVRCP_SCAN_GROUP;
}
return 0;
}
static const char *battery_status_to_str(uint8_t status)
{
switch (status) {
case AVRCP_BATTERY_STATUS_NORMAL:
return "normal";
case AVRCP_BATTERY_STATUS_WARNING:
return "warning";
case AVRCP_BATTERY_STATUS_CRITICAL:
return "critical";
case AVRCP_BATTERY_STATUS_EXTERNAL:
return "external";
case AVRCP_BATTERY_STATUS_FULL_CHARGE:
return "fullcharge";
}
return NULL;
}
/*
* get_company_id:
*
* Get three-byte Company_ID from incoming AVRCP message
*/
static uint32_t get_company_id(const uint8_t cid[3])
{
return cid[0] << 16 | cid[1] << 8 | cid[2];
}
/*
* set_company_id:
*
* Set three-byte Company_ID into outgoing AVRCP message
*/
static void set_company_id(uint8_t cid[3], const uint32_t cid_in)
{
cid[0] = cid_in >> 16;
cid[1] = cid_in >> 8;
cid[2] = cid_in;
}
static const char *attr_to_str(uint8_t attr)
{
switch (attr) {
case AVRCP_ATTRIBUTE_EQUALIZER:
return "Equalizer";
case AVRCP_ATTRIBUTE_REPEAT_MODE:
return "Repeat";
case AVRCP_ATTRIBUTE_SHUFFLE:
return "Shuffle";
case AVRCP_ATTRIBUTE_SCAN:
return "Scan";
}
return NULL;
}
static int attrval_to_val(uint8_t attr, const char *value)
{
int ret;
switch (attr) {
case AVRCP_ATTRIBUTE_EQUALIZER:
if (!strcmp(value, "off"))
ret = AVRCP_EQUALIZER_OFF;
else if (!strcmp(value, "on"))
ret = AVRCP_EQUALIZER_ON;
else
ret = -EINVAL;
return ret;
case AVRCP_ATTRIBUTE_REPEAT_MODE:
if (!strcmp(value, "off"))
ret = AVRCP_REPEAT_MODE_OFF;
else if (!strcmp(value, "singletrack"))
ret = AVRCP_REPEAT_MODE_SINGLE;
else if (!strcmp(value, "alltracks"))
ret = AVRCP_REPEAT_MODE_ALL;
else if (!strcmp(value, "group"))
ret = AVRCP_REPEAT_MODE_GROUP;
else
ret = -EINVAL;
return ret;
case AVRCP_ATTRIBUTE_SHUFFLE:
if (!strcmp(value, "off"))
ret = AVRCP_SHUFFLE_OFF;
else if (!strcmp(value, "alltracks"))
ret = AVRCP_SHUFFLE_ALL;
else if (!strcmp(value, "group"))
ret = AVRCP_SHUFFLE_GROUP;
else
ret = -EINVAL;
return ret;
case AVRCP_ATTRIBUTE_SCAN:
if (!strcmp(value, "off"))
ret = AVRCP_SCAN_OFF;
else if (!strcmp(value, "alltracks"))
ret = AVRCP_SCAN_ALL;
else if (!strcmp(value, "group"))
ret = AVRCP_SCAN_GROUP;
else
ret = -EINVAL;
return ret;
}
return -EINVAL;
}
static int attr_to_val(const char *str)
{
if (!strcasecmp(str, "Equalizer"))
return AVRCP_ATTRIBUTE_EQUALIZER;
else if (!strcasecmp(str, "Repeat"))
return AVRCP_ATTRIBUTE_REPEAT_MODE;
else if (!strcasecmp(str, "Shuffle"))
return AVRCP_ATTRIBUTE_SHUFFLE;
else if (!strcasecmp(str, "Scan"))
return AVRCP_ATTRIBUTE_SCAN;
return -EINVAL;
}
static int player_get_setting(struct avrcp_player *player, uint8_t id)
{
const char *key;
const char *value;
if (player == NULL)
return -ENOENT;
key = attr_to_str(id);
if (key == NULL)
return -EINVAL;
value = player->cb->get_setting(key, player->user_data);
if (value == NULL)
return -EINVAL;
return attrval_to_val(id, value);
}
static int play_status_to_val(const char *status)
{
if (!strcasecmp(status, "stopped"))
return AVRCP_PLAY_STATUS_STOPPED;
else if (!strcasecmp(status, "playing"))
return AVRCP_PLAY_STATUS_PLAYING;
else if (!strcasecmp(status, "paused"))
return AVRCP_PLAY_STATUS_PAUSED;
else if (!strcasecmp(status, "forward-seek"))
return AVRCP_PLAY_STATUS_FWD_SEEK;
else if (!strcasecmp(status, "reverse-seek"))
return AVRCP_PLAY_STATUS_REV_SEEK;
else if (!strcasecmp(status, "error"))
return AVRCP_PLAY_STATUS_ERROR;
return -EINVAL;
}
void avrcp_player_event(struct avrcp_player *player, uint8_t id,
const void *data)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 9];
struct avrcp_header *pdu = (void *) buf;
uint16_t size;
GSList *l;
int attr;
int val;
if (player->sessions == NULL)
return;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_REGISTER_NOTIFICATION;
pdu->params[0] = id;
DBG("id=%u", id);
switch (id) {
case AVRCP_EVENT_STATUS_CHANGED:
size = 2;
pdu->params[1] = play_status_to_val(data);
break;
case AVRCP_EVENT_TRACK_CHANGED:
size = 9;
memcpy(&pdu->params[1], data, sizeof(uint64_t));
break;
case AVRCP_EVENT_TRACK_REACHED_END:
case AVRCP_EVENT_TRACK_REACHED_START:
size = 1;
break;
case AVRCP_EVENT_SETTINGS_CHANGED:
size = 2;
pdu->params[1] = 1;
attr = attr_to_val(data);
if (attr < 0)
return;
val = player_get_setting(player, attr);
if (val < 0)
return;
pdu->params[size++] = attr;
pdu->params[size++] = val;
break;
default:
error("Unknown event %u", id);
return;
}
pdu->params_len = htons(size);
for (l = player->sessions; l; l = l->next) {
struct avrcp *session = l->data;
int err;
if (!(session->registered_events & (1 << id)))
continue;
err = avctp_send_vendordep(session->conn,
session->transaction_events[id],
AVC_CTYPE_CHANGED, AVC_SUBUNIT_PANEL,
buf, size + AVRCP_HEADER_LENGTH);
if (err < 0)
continue;
/* Unregister event as per AVRCP 1.3 spec, section 5.4.2 */
session->registered_events ^= 1 << id;
}
return;
}
static const char *metadata_to_str(uint32_t id)
{
switch (id) {
case AVRCP_MEDIA_ATTRIBUTE_TITLE:
return "Title";
case AVRCP_MEDIA_ATTRIBUTE_ARTIST:
return "Artist";
case AVRCP_MEDIA_ATTRIBUTE_ALBUM:
return "Album";
case AVRCP_MEDIA_ATTRIBUTE_GENRE:
return "Genre";
case AVRCP_MEDIA_ATTRIBUTE_TRACK:
return "TrackNumber";
case AVRCP_MEDIA_ATTRIBUTE_N_TRACKS:
return "NumberOfTracks";
case AVRCP_MEDIA_ATTRIBUTE_DURATION:
return "Duration";
}
return NULL;
}
static const char *player_get_metadata(struct avrcp_player *player,
uint32_t id)
{
const char *key;
key = metadata_to_str(id);
if (key == NULL)
return NULL;
if (player != NULL)
return player->cb->get_metadata(key, player->user_data);
if (id == AVRCP_MEDIA_ATTRIBUTE_TITLE)
return "";
return NULL;
}
static uint16_t player_write_media_attribute(struct avrcp_player *player,
uint32_t id, uint8_t *buf,
uint16_t *pos,
uint16_t *offset)
{
uint16_t len;
uint16_t attr_len;
const char *value = NULL;
DBG("%u", id);
value = player_get_metadata(player, id);
if (value == NULL) {
*offset = 0;
return 0;
}
attr_len = strlen(value);
value = ((char *) value) + *offset;
len = attr_len - *offset;
if (len > AVRCP_PDU_MTU - *pos) {
len = AVRCP_PDU_MTU - *pos;
*offset += len;
} else {
*offset = 0;
}
memcpy(&buf[*pos], value, len);
*pos += len;
return attr_len;
}
static GList *player_fill_media_attribute(struct avrcp_player *player,
GList *attr_ids, uint8_t *buf,
uint16_t *pos, uint16_t *offset)
{
struct media_attribute_header {
uint32_t id;
uint16_t charset;
uint16_t len;
} *hdr = NULL;
GList *l;
for (l = attr_ids; l != NULL; l = g_list_delete_link(l, l)) {
uint32_t attr = GPOINTER_TO_UINT(l->data);
uint16_t attr_len;
if (*offset == 0) {
if (*pos + sizeof(*hdr) >= AVRCP_PDU_MTU)
break;
hdr = (void *) &buf[*pos];
hdr->id = htonl(attr);
/* Always use UTF-8 */
hdr->charset = htons(AVRCP_CHARSET_UTF8);
*pos += sizeof(*hdr);
}
attr_len = player_write_media_attribute(player, attr, buf,
pos, offset);
if (hdr != NULL)
hdr->len = htons(attr_len);
if (*offset > 0)
break;
}
return l;
}
static struct pending_pdu *pending_pdu_new(uint8_t pdu_id, GList *attr_ids,
unsigned int offset)
{
struct pending_pdu *pending = g_new(struct pending_pdu, 1);
pending->pdu_id = pdu_id;
pending->attr_ids = attr_ids;
pending->offset = offset;
return pending;
}
static gboolean session_abort_pending_pdu(struct avrcp *session)
{
if (session->pending_pdu == NULL)
return FALSE;
g_list_free(session->pending_pdu->attr_ids);
g_free(session->pending_pdu);
session->pending_pdu = NULL;
return TRUE;
}
static const char *attrval_to_str(uint8_t attr, uint8_t value)
{
switch (attr) {
case AVRCP_ATTRIBUTE_EQUALIZER:
switch (value) {
case AVRCP_EQUALIZER_ON:
return "on";
case AVRCP_EQUALIZER_OFF:
return "off";
}
break;
case AVRCP_ATTRIBUTE_REPEAT_MODE:
switch (value) {
case AVRCP_REPEAT_MODE_OFF:
return "off";
case AVRCP_REPEAT_MODE_SINGLE:
return "singletrack";
case AVRCP_REPEAT_MODE_ALL:
return "alltracks";
case AVRCP_REPEAT_MODE_GROUP:
return "group";
}
break;
/* Shuffle and scan have the same values */
case AVRCP_ATTRIBUTE_SHUFFLE:
case AVRCP_ATTRIBUTE_SCAN:
switch (value) {
case AVRCP_SCAN_OFF:
return "off";
case AVRCP_SCAN_ALL:
return "alltracks";
case AVRCP_SCAN_GROUP:
return "group";
}
break;
}
return NULL;
}
static int player_set_setting(struct avrcp_player *player, uint8_t id,
uint8_t val)
{
const char *key, *value;
key = attr_to_str(id);
if (key == NULL)
return -EINVAL;
value = attrval_to_str(id, val);
if (value == NULL)
return -EINVAL;
if (player == NULL)
return -ENOENT;
return player->cb->set_setting(key, value, player->user_data);
}
static uint8_t avrcp_handle_get_capabilities(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
uint16_t len = ntohs(pdu->params_len);
unsigned int i;
if (len != 1)
goto err;
DBG("id=%u", pdu->params[0]);
switch (pdu->params[0]) {
case CAP_COMPANY_ID:
for (i = 0; i < G_N_ELEMENTS(company_ids); i++) {
set_company_id(&pdu->params[2 + i * 3],
company_ids[i]);
}
pdu->params_len = htons(2 + (3 * G_N_ELEMENTS(company_ids)));
pdu->params[1] = G_N_ELEMENTS(company_ids);
return AVC_CTYPE_STABLE;
case CAP_EVENTS_SUPPORTED:
pdu->params[1] = 0;
for (i = 1; i <= AVRCP_EVENT_LAST; i++) {
if (session->supported_events & (1 << i)) {
pdu->params[1]++;
pdu->params[pdu->params[1] + 1] = i;
}
}
pdu->params_len = htons(2 + pdu->params[1]);
return AVC_CTYPE_STABLE;
}
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_list_player_attributes(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
unsigned int i;
if (len != 0) {
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
if (!player)
goto done;
for (i = 1; i <= AVRCP_ATTRIBUTE_SCAN; i++) {
if (player_get_setting(player, i) < 0)
continue;
len++;
pdu->params[len] = i;
}
done:
pdu->params[0] = len;
pdu->params_len = htons(len + 1);
return AVC_CTYPE_STABLE;
}
static uint8_t avrcp_handle_list_player_values(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
unsigned int i;
if (len != 1)
goto err;
if (player_get_setting(player, pdu->params[0]) < 0)
goto err;
len = attr_get_max_val(pdu->params[0]);
for (i = 1; i <= len; i++)
pdu->params[i] = i;
pdu->params[0] = len;
pdu->params_len = htons(len + 1);
return AVC_CTYPE_STABLE;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint32_t str_to_metadata(const char *str)
{
if (strcasecmp(str, "Title") == 0)
return AVRCP_MEDIA_ATTRIBUTE_TITLE;
else if (strcasecmp(str, "Artist") == 0)
return AVRCP_MEDIA_ATTRIBUTE_ARTIST;
else if (strcasecmp(str, "Album") == 0)
return AVRCP_MEDIA_ATTRIBUTE_ALBUM;
else if (strcasecmp(str, "Genre") == 0)
return AVRCP_MEDIA_ATTRIBUTE_GENRE;
else if (strcasecmp(str, "TrackNumber") == 0)
return AVRCP_MEDIA_ATTRIBUTE_TRACK;
else if (strcasecmp(str, "NumberOfTracks") == 0)
return AVRCP_MEDIA_ATTRIBUTE_N_TRACKS;
else if (strcasecmp(str, "Duration") == 0)
return AVRCP_MEDIA_ATTRIBUTE_DURATION;
return 0;
}
static GList *player_list_metadata(struct avrcp_player *player)
{
GList *l, *attrs = NULL;
if (player == NULL)
return g_list_prepend(NULL,
GUINT_TO_POINTER(AVRCP_MEDIA_ATTRIBUTE_TITLE));
l = player->cb->list_metadata(player->user_data);
for (; l; l = l->next) {
const char *key = l->data;
attrs = g_list_append(attrs,
GUINT_TO_POINTER(str_to_metadata(key)));
}
return attrs;
}
static uint8_t avrcp_handle_get_element_attributes(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
uint64_t identifier = bt_get_le64(&pdu->params[0]);
uint16_t pos;
uint8_t nattr;
GList *attr_ids;
uint16_t offset;
if (len < 9 || identifier != 0)
goto err;
nattr = pdu->params[8];
if (len < nattr * sizeof(uint32_t) + 1)
goto err;
if (!nattr) {
/*
* Return all available information, at least
* title must be returned if there's a track selected.
*/
attr_ids = player_list_metadata(player);
len = g_list_length(attr_ids);
} else {
unsigned int i;
for (i = 0, len = 0, attr_ids = NULL; i < nattr; i++) {
uint32_t id;
id = bt_get_be32(&pdu->params[9] + (i * sizeof(id)));
/* Don't add invalid attributes */
if (id == AVRCP_MEDIA_ATTRIBUTE_ILLEGAL ||
id > AVRCP_MEDIA_ATTRIBUTE_LAST)
continue;
len++;
attr_ids = g_list_prepend(attr_ids,
GUINT_TO_POINTER(id));
}
attr_ids = g_list_reverse(attr_ids);
}
if (!len)
goto err;
session_abort_pending_pdu(session);
pos = 1;
offset = 0;
attr_ids = player_fill_media_attribute(player, attr_ids, pdu->params,
&pos, &offset);
if (attr_ids != NULL) {
session->pending_pdu = pending_pdu_new(pdu->pdu_id, attr_ids,
offset);
pdu->packet_type = AVRCP_PACKET_TYPE_START;
}
pdu->params[0] = len;
pdu->params_len = htons(pos);
return AVC_CTYPE_STABLE;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_get_current_player_value(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
uint8_t *settings;
unsigned int i;
if (len <= 1 || pdu->params[0] != len - 1)
goto err;
/*
* Save a copy of requested settings because we can override them
* while responding
*/
settings = g_memdup(&pdu->params[1], pdu->params[0]);
len = 0;
/*
* From sec. 5.7 of AVRCP 1.3 spec, we should igore non-existent IDs
* and send a response with the existent ones. Only if all IDs are
* non-existent we should send an error.
*/
for (i = 0; i < pdu->params[0]; i++) {
int val;
if (settings[i] < AVRCP_ATTRIBUTE_EQUALIZER ||
settings[i] > AVRCP_ATTRIBUTE_SCAN) {
DBG("Ignoring %u", settings[i]);
continue;
}
val = player_get_setting(player, settings[i]);
if (val < 0)
continue;
pdu->params[++len] = settings[i];
pdu->params[++len] = val;
}
g_free(settings);
if (len) {
pdu->params[0] = len / 2;
pdu->params_len = htons(len + 1);
return AVC_CTYPE_STABLE;
}
error("No valid attributes in request");
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_set_player_value(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
unsigned int i;
uint8_t *param;
if (len < 3 || len > 2 * pdu->params[0] + 1U || player == NULL)
goto err;
/*
* From sec. 5.7 of AVRCP 1.3 spec, we should igore non-existent IDs
* and set the existent ones. Sec. 5.2.4 is not clear however how to
* indicate that a certain ID was not accepted. If at least one
* attribute is valid, we respond with no parameters. Otherwise an
* AVRCP_STATUS_INVALID_PARAM is sent.
*/
for (len = 0, i = 0, param = &pdu->params[1]; i < pdu->params[0];
i++, param += 2) {
if (player_set_setting(player, param[0], param[1]) < 0)
continue;
len++;
}
if (len) {
pdu->params_len = 0;
return AVC_CTYPE_ACCEPTED;
}
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_displayable_charset(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
uint16_t len = ntohs(pdu->params_len);
if (len < 3) {
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
/*
* We acknowledge the commands, but we always use UTF-8 for
* encoding since CT is obliged to support it.
*/
pdu->params_len = 0;
return AVC_CTYPE_STABLE;
}
static uint8_t avrcp_handle_ct_battery_status(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
uint16_t len = ntohs(pdu->params_len);
const char *valstr;
if (len != 1)
goto err;
valstr = battery_status_to_str(pdu->params[0]);
if (valstr == NULL)
goto err;
pdu->params_len = 0;
return AVC_CTYPE_STABLE;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint32_t player_get_position(struct avrcp_player *player)
{
if (player == NULL)
return 0;
return player->cb->get_position(player->user_data);
}
static uint32_t player_get_duration(struct avrcp_player *player)
{
uint32_t num;
if (player == NULL)
return UINT32_MAX;
num = player->cb->get_duration(player->user_data);
if (num == 0)
return UINT32_MAX;
return num;
}
static uint8_t player_get_status(struct avrcp_player *player)
{
const char *value;
if (player == NULL)
return AVRCP_PLAY_STATUS_STOPPED;
value = player->cb->get_status(player->user_data);
if (value == NULL)
return AVRCP_PLAY_STATUS_STOPPED;
return play_status_to_val(value);
}
static uint8_t avrcp_handle_get_play_status(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
uint32_t position;
uint32_t duration;
if (len != 0) {
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
position = player_get_position(player);
duration = player_get_duration(player);
position = htonl(position);
duration = htonl(duration);
memcpy(&pdu->params[0], &duration, 4);
memcpy(&pdu->params[4], &position, 4);
pdu->params[8] = player_get_status(player);
pdu->params_len = htons(9);
return AVC_CTYPE_STABLE;
}
static uint64_t player_get_uid(struct avrcp_player *player)
{
if (player == NULL)
return UINT64_MAX;
return player->cb->get_uid(player->user_data);
}
static GList *player_list_settings(struct avrcp_player *player)
{
if (player == NULL)
return NULL;
return player->cb->list_settings(player->user_data);
}
static bool avrcp_handle_play(struct avrcp *session)
{
struct avrcp_player *player = session->target->player;
if (player == NULL)
return false;
return player->cb->play(player->user_data);
}
static bool avrcp_handle_stop(struct avrcp *session)
{
struct avrcp_player *player = session->target->player;
if (player == NULL)
return false;
return player->cb->stop(player->user_data);
}
static bool avrcp_handle_pause(struct avrcp *session)
{
struct avrcp_player *player = session->target->player;
if (player == NULL)
return false;
return player->cb->pause(player->user_data);
}
static bool avrcp_handle_next(struct avrcp *session)
{
struct avrcp_player *player = session->target->player;
if (player == NULL)
return false;
return player->cb->next(player->user_data);
}
static bool avrcp_handle_previous(struct avrcp *session)
{
struct avrcp_player *player = session->target->player;
if (player == NULL)
return false;
return player->cb->previous(player->user_data);
}
static const struct passthrough_handler passthrough_handlers[] = {
{ AVC_PLAY, avrcp_handle_play },
{ AVC_STOP, avrcp_handle_stop },
{ AVC_PAUSE, avrcp_handle_pause },
{ AVC_FORWARD, avrcp_handle_next },
{ AVC_BACKWARD, avrcp_handle_previous },
{ },
};
static bool handle_passthrough(struct avctp *conn, uint8_t op, bool pressed,
void *user_data)
{
struct avrcp *session = user_data;
const struct passthrough_handler *handler;
for (handler = session->passthrough_handlers; handler->func;
handler++) {
if (handler->op == op)
break;
}
if (handler->func == NULL)
return false;
/* Do not trigger handler on release */
if (!pressed)
return true;
return handler->func(session);
}
static uint8_t avrcp_handle_register_notification(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
struct btd_device *dev = session->dev;
uint16_t len = ntohs(pdu->params_len);
uint64_t uid;
GList *settings;
/*
* 1 byte for EventID, 4 bytes for Playback interval but the latest
* one is applicable only for EVENT_PLAYBACK_POS_CHANGED. See AVRCP
* 1.3 spec, section 5.4.2.
*/
if (len != 5)
goto err;
/* Check if event is supported otherwise reject */
if (!(session->supported_events & (1 << pdu->params[0])))
goto err;
switch (pdu->params[0]) {
case AVRCP_EVENT_STATUS_CHANGED:
len = 2;
pdu->params[1] = player_get_status(player);
break;
case AVRCP_EVENT_TRACK_CHANGED:
len = 9;
uid = player_get_uid(session->target->player);
memcpy(&pdu->params[1], &uid, sizeof(uint64_t));
break;
case AVRCP_EVENT_TRACK_REACHED_END:
case AVRCP_EVENT_TRACK_REACHED_START:
len = 1;
break;
case AVRCP_EVENT_SETTINGS_CHANGED:
len = 1;
settings = player_list_settings(player);
pdu->params[len++] = g_list_length(settings);
for (; settings; settings = settings->next) {
const char *key = settings->data;
int attr;
int val;
attr = attr_to_val(key);
if (attr < 0)
continue;
val = player_get_setting(player, attr);
if (val < 0)
continue;
pdu->params[len++] = attr;
pdu->params[len++] = val;
}
break;
case AVRCP_EVENT_VOLUME_CHANGED:
pdu->params[1] = media_transport_get_device_volume(dev);
if (pdu->params[1] > 127)
goto err;
len = 2;
break;
default:
/* All other events are not supported yet */
goto err;
}
/* Register event and save the transaction used */
session->registered_events |= (1 << pdu->params[0]);
session->transaction_events[pdu->params[0]] = transaction;
pdu->params_len = htons(len);
return AVC_CTYPE_INTERIM;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_request_continuing(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->target->player;
uint16_t len = ntohs(pdu->params_len);
struct pending_pdu *pending;
if (len != 1 || session->pending_pdu == NULL)
goto err;
pending = session->pending_pdu;
if (pending->pdu_id != pdu->params[0])
goto err;
len = 0;
pending->attr_ids = player_fill_media_attribute(player,
pending->attr_ids,
pdu->params, &len,
&pending->offset);
pdu->pdu_id = pending->pdu_id;
if (pending->attr_ids == NULL) {
g_free(session->pending_pdu);
session->pending_pdu = NULL;
pdu->packet_type = AVRCP_PACKET_TYPE_END;
} else {
pdu->packet_type = AVRCP_PACKET_TYPE_CONTINUING;
}
pdu->params_len = htons(len);
return AVC_CTYPE_STABLE;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_abort_continuing(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
uint16_t len = ntohs(pdu->params_len);
struct pending_pdu *pending;
if (len != 1 || session->pending_pdu == NULL)
goto err;
pending = session->pending_pdu;
if (pending->pdu_id != pdu->params[0])
goto err;
session_abort_pending_pdu(session);
pdu->params_len = 0;
return AVC_CTYPE_ACCEPTED;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static uint8_t avrcp_handle_set_absolute_volume(struct avrcp *session,
struct avrcp_header *pdu,
uint8_t transaction)
{
struct avrcp_player *player = session->controller->player;
uint16_t len = ntohs(pdu->params_len);
uint8_t volume;
if (len != 1)
goto err;
if (!player)
goto err;
volume = pdu->params[0] & 0x7F;
media_transport_update_device_volume(session->dev, volume);
return AVC_CTYPE_ACCEPTED;
err:
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
return AVC_CTYPE_REJECTED;
}
static const struct control_pdu_handler control_handlers[] = {
{ AVRCP_GET_CAPABILITIES, AVC_CTYPE_STATUS,
avrcp_handle_get_capabilities },
{ AVRCP_LIST_PLAYER_ATTRIBUTES, AVC_CTYPE_STATUS,
avrcp_handle_list_player_attributes },
{ AVRCP_LIST_PLAYER_VALUES, AVC_CTYPE_STATUS,
avrcp_handle_list_player_values },
{ AVRCP_GET_ELEMENT_ATTRIBUTES, AVC_CTYPE_STATUS,
avrcp_handle_get_element_attributes },
{ AVRCP_GET_CURRENT_PLAYER_VALUE, AVC_CTYPE_STATUS,
avrcp_handle_get_current_player_value },
{ AVRCP_SET_PLAYER_VALUE, AVC_CTYPE_CONTROL,
avrcp_handle_set_player_value },
{ AVRCP_GET_PLAYER_ATTRIBUTE_TEXT, AVC_CTYPE_STATUS,
NULL },
{ AVRCP_GET_PLAYER_VALUE_TEXT, AVC_CTYPE_STATUS,
NULL },
{ AVRCP_DISPLAYABLE_CHARSET, AVC_CTYPE_STATUS,
avrcp_handle_displayable_charset },
{ AVRCP_CT_BATTERY_STATUS, AVC_CTYPE_STATUS,
avrcp_handle_ct_battery_status },
{ AVRCP_GET_PLAY_STATUS, AVC_CTYPE_STATUS,
avrcp_handle_get_play_status },
{ AVRCP_REGISTER_NOTIFICATION, AVC_CTYPE_NOTIFY,
avrcp_handle_register_notification },
{ AVRCP_SET_ABSOLUTE_VOLUME, AVC_CTYPE_CONTROL,
avrcp_handle_set_absolute_volume },
{ AVRCP_REQUEST_CONTINUING, AVC_CTYPE_CONTROL,
avrcp_handle_request_continuing },
{ AVRCP_ABORT_CONTINUING, AVC_CTYPE_CONTROL,
avrcp_handle_abort_continuing },
{ },
};
/* handle vendordep pdu inside an avctp packet */
static size_t handle_vendordep_pdu(struct avctp *conn, uint8_t transaction,
uint8_t *code, uint8_t *subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
const struct control_pdu_handler *handler;
struct avrcp_header *pdu = (void *) operands;
uint32_t company_id = get_company_id(pdu->company_id);
if (company_id != IEEEID_BTSIG) {
*code = AVC_CTYPE_NOT_IMPLEMENTED;
return 0;
}
DBG("AVRCP PDU 0x%02X, company 0x%06X len 0x%04X",
pdu->pdu_id, company_id, pdu->params_len);
pdu->packet_type = 0;
pdu->rsvd = 0;
if (operand_count < AVRCP_HEADER_LENGTH) {
pdu->params[0] = AVRCP_STATUS_INVALID_COMMAND;
goto err_metadata;
}
for (handler = session->control_handlers; handler->pdu_id; handler++) {
if (handler->pdu_id == pdu->pdu_id)
break;
}
if (!handler || handler->code != *code) {
pdu->params[0] = AVRCP_STATUS_INVALID_COMMAND;
goto err_metadata;
}
if (!handler->func) {
pdu->params[0] = AVRCP_STATUS_INVALID_PARAM;
goto err_metadata;
}
*code = handler->func(session, pdu, transaction);
if (*code != AVC_CTYPE_REJECTED &&
pdu->pdu_id != AVRCP_GET_ELEMENT_ATTRIBUTES &&
pdu->pdu_id != AVRCP_REQUEST_CONTINUING &&
pdu->pdu_id != AVRCP_ABORT_CONTINUING)
session_abort_pending_pdu(session);
return AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
err_metadata:
pdu->params_len = htons(1);
*code = AVC_CTYPE_REJECTED;
return AVRCP_HEADER_LENGTH + 1;
}
static struct browsing_pdu_handler {
uint8_t pdu_id;
void (*func) (struct avrcp *session, struct avrcp_browsing_header *pdu,
uint8_t transaction);
} browsing_handlers[] = {
{ },
};
size_t avrcp_browsing_general_reject(uint8_t *operands)
{
struct avrcp_browsing_header *pdu = (void *) operands;
uint8_t status;
pdu->pdu_id = AVRCP_GENERAL_REJECT;
status = AVRCP_STATUS_INVALID_COMMAND;
pdu->param_len = htons(sizeof(status));
memcpy(pdu->params, &status, (sizeof(status)));
return AVRCP_BROWSING_HEADER_LENGTH + sizeof(status);
}
static size_t handle_browsing_pdu(struct avctp *conn,
uint8_t transaction, uint8_t *operands,
size_t operand_count, void *user_data)
{
struct avrcp *session = user_data;
struct browsing_pdu_handler *handler;
struct avrcp_browsing_header *pdu = (void *) operands;
DBG("AVRCP Browsing PDU 0x%02X, len 0x%04X", pdu->pdu_id,
pdu->param_len);
for (handler = browsing_handlers; handler->pdu_id; handler++) {
if (handler->pdu_id == pdu->pdu_id)
break;
}
if (handler == NULL || handler->func == NULL)
return avrcp_browsing_general_reject(operands);
session->transaction = transaction;
handler->func(session, pdu, transaction);
return AVRCP_BROWSING_HEADER_LENGTH + ntohs(pdu->param_len);
}
size_t avrcp_handle_vendor_reject(uint8_t *code, uint8_t *operands)
{
struct avrcp_header *pdu = (void *) operands;
uint32_t company_id = get_company_id(pdu->company_id);
*code = AVC_CTYPE_REJECTED;
pdu->params_len = htons(1);
pdu->params[0] = AVRCP_STATUS_INTERNAL_ERROR;
DBG("rejecting AVRCP PDU 0x%02X, company 0x%06X len 0x%04X",
pdu->pdu_id, company_id, pdu->params_len);
return AVRCP_HEADER_LENGTH + 1;
}
static struct avrcp_server *find_server(GSList *list, struct btd_adapter *a)
{
for (; list; list = list->next) {
struct avrcp_server *server = list->data;
if (server->adapter == a)
return server;
}
return NULL;
}
static const char *status_to_string(uint8_t status)
{
switch (status) {
case AVRCP_PLAY_STATUS_STOPPED:
return "stopped";
case AVRCP_PLAY_STATUS_PLAYING:
return "playing";
case AVRCP_PLAY_STATUS_PAUSED:
return "paused";
case AVRCP_PLAY_STATUS_FWD_SEEK:
return "forward-seek";
case AVRCP_PLAY_STATUS_REV_SEEK:
return "reverse-seek";
case AVRCP_PLAY_STATUS_ERROR:
return "error";
default:
return NULL;
}
}
static gboolean avrcp_get_play_status_rsp(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
struct avrcp_header *pdu = (void *) operands;
uint32_t duration;
uint32_t position;
uint8_t status;
if (pdu == NULL || code == AVC_CTYPE_REJECTED ||
ntohs(pdu->params_len) != 9)
return FALSE;
memcpy(&duration, pdu->params, sizeof(uint32_t));
duration = ntohl(duration);
media_player_set_duration(mp, duration);
memcpy(&position, pdu->params + 4, sizeof(uint32_t));
position = ntohl(position);
media_player_set_position(mp, position);
memcpy(&status, pdu->params + 8, sizeof(uint8_t));
media_player_set_status(mp, status_to_string(status));
return FALSE;
}
static void avrcp_get_play_status(struct avrcp *session)
{
uint8_t buf[AVRCP_HEADER_LENGTH];
struct avrcp_header *pdu = (void *) buf;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_GET_PLAY_STATUS;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, sizeof(buf),
avrcp_get_play_status_rsp,
session);
}
static const char *status_to_str(uint8_t status)
{
switch (status) {
case AVRCP_STATUS_INVALID_COMMAND:
return "Invalid Command";
case AVRCP_STATUS_INVALID_PARAM:
return "Invalid Parameter";
case AVRCP_STATUS_INTERNAL_ERROR:
return "Internal Error";
case AVRCP_STATUS_SUCCESS:
return "Success";
default:
return "Unknown";
}
}
static gboolean avrcp_player_value_rsp(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
struct avrcp_header *pdu = (void *) operands;
uint8_t count;
int i;
if (pdu == NULL) {
media_player_set_setting(mp, "Error", "Timeout");
return FALSE;
}
if (code == AVC_CTYPE_REJECTED) {
media_player_set_setting(mp, "Error",
status_to_str(pdu->params[0]));
return FALSE;
}
count = pdu->params[0];
if (pdu->params_len < count * 2)
return FALSE;
for (i = 1; count > 0; count--, i += 2) {
const char *key;
const char *value;
key = attr_to_str(pdu->params[i]);
if (key == NULL)
continue;
value = attrval_to_str(pdu->params[i], pdu->params[i + 1]);
if (value == NULL)
continue;
media_player_set_setting(mp, key, value);
}
return FALSE;
}
static void avrcp_get_current_player_value(struct avrcp *session,
uint8_t *attrs, uint8_t count)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 5];
struct avrcp_header *pdu = (void *) buf;
int i;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_GET_CURRENT_PLAYER_VALUE;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params_len = htons(count + 1);
pdu->params[0] = count;
for (i = 0; count > 0; count--, i++)
pdu->params[i + 1] = attrs[i];
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, sizeof(buf),
avrcp_player_value_rsp, session);
}
static gboolean avrcp_list_player_attributes_rsp(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_header *pdu = (void *) operands;
uint8_t count;
if (code == AVC_CTYPE_REJECTED)
return FALSE;
count = pdu->params[0];
if (ntohs(pdu->params_len) < count) {
error("Invalid parameters");
return FALSE;
}
avrcp_get_current_player_value(session, &pdu->params[1],
pdu->params[0]);
return FALSE;
}
static void avrcp_list_player_attributes(struct avrcp *session)
{
uint8_t buf[AVRCP_HEADER_LENGTH];
struct avrcp_header *pdu = (void *) buf;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_LIST_PLAYER_ATTRIBUTES;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, sizeof(buf),
avrcp_list_player_attributes_rsp,
session);
}
static void avrcp_parse_attribute_list(struct avrcp_player *player,
uint8_t *operands, uint8_t count)
{
struct media_player *mp = player->user_data;
struct media_item *item;
int i;
item = media_player_set_playlist_item(mp, player->uid);
for (i = 0; count > 0; count--) {
uint32_t id;
uint16_t charset, len;
id = bt_get_be32(&operands[i]);
i += sizeof(uint32_t);
charset = bt_get_be16(&operands[i]);
i += sizeof(uint16_t);
len = bt_get_be16(&operands[i]);
i += sizeof(uint16_t);
if (charset == 106) {
const char *key = metadata_to_str(id);
if (key != NULL)
media_player_set_metadata(mp, item,
metadata_to_str(id),
&operands[i], len);
}
i += len;
}
}
static gboolean avrcp_get_element_attributes_rsp(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands,
size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct avrcp_header *pdu = (void *) operands;
uint8_t count;
if (code == AVC_CTYPE_REJECTED)
return FALSE;
count = pdu->params[0];
if (ntohs(pdu->params_len) - 1 < count * 8) {
error("Invalid parameters");
return FALSE;
}
avrcp_parse_attribute_list(player, &pdu->params[1], count);
avrcp_get_play_status(session);
return FALSE;
}
static void avrcp_get_element_attributes(struct avrcp *session)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 9];
struct avrcp_header *pdu = (void *) buf;
uint16_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_GET_ELEMENT_ATTRIBUTES;
pdu->params_len = htons(9);
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, length,
avrcp_get_element_attributes_rsp,
session);
}
static const char *type_to_string(uint8_t type)
{
switch (type & 0x0F) {
case 0x01:
return "Audio";
case 0x02:
return "Video";
case 0x03:
return "Audio, Video";
case 0x04:
return "Audio Broadcasting";
case 0x05:
return "Audio, Audio Broadcasting";
case 0x06:
return "Video, Audio Broadcasting";
case 0x07:
return "Audio, Video, Audio Broadcasting";
case 0x08:
return "Video Broadcasting";
case 0x09:
return "Audio, Video Broadcasting";
case 0x0A:
return "Video, Video Broadcasting";
case 0x0B:
return "Audio, Video, Video Broadcasting";
case 0x0C:
return "Audio Broadcasting, Video Broadcasting";
case 0x0D:
return "Audio, Audio Broadcasting, Video Broadcasting";
case 0x0E:
return "Video, Audio Broadcasting, Video Broadcasting";
case 0x0F:
return "Audio, Video, Audio Broadcasting, Video Broadcasting";
}
return "None";
}
static const char *subtype_to_string(uint32_t subtype)
{
switch (subtype & 0x03) {
case 0x01:
return "Audio Book";
case 0x02:
return "Podcast";
case 0x03:
return "Audio Book, Podcast";
}
return "None";
}
static struct media_item *parse_media_element(struct avrcp *session,
uint8_t *operands, uint16_t len)
{
struct avrcp_player *player;
struct media_player *mp;
struct media_item *item;
uint16_t namelen;
char name[255];
uint64_t uid;
if (len < 13)
return NULL;
uid = bt_get_be64(&operands[0]);
namelen = MIN(bt_get_be16(&operands[11]), sizeof(name) - 1);
if (namelen > 0) {
memcpy(name, &operands[13], namelen);
name[namelen] = '\0';
}
player = session->controller->player;
mp = player->user_data;
item = media_player_create_item(mp, name, PLAYER_ITEM_TYPE_AUDIO, uid);
if (item == NULL)
return NULL;
media_item_set_playable(item, true);
return item;
}
static struct media_item *parse_media_folder(struct avrcp *session,
uint8_t *operands, uint16_t len)
{
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
uint16_t namelen;
char name[255];
uint64_t uid;
uint8_t type;
if (len < 12)
return NULL;
uid = bt_get_be64(&operands[0]);
type = operands[9];
namelen = MIN(bt_get_be16(&operands[12]), sizeof(name) - 1);
if (namelen > 0) {
memcpy(name, &operands[14], namelen);
name[namelen] = '\0';
}
return media_player_create_folder(mp, name, type, uid);
}
static void avrcp_list_items(struct avrcp *session, uint32_t start,
uint32_t end);
static gboolean avrcp_list_items_rsp(struct avctp *conn, uint8_t *operands,
size_t operand_count, void *user_data)
{
struct avrcp_browsing_header *pdu = (void *) operands;
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct pending_list_items *p = player->p;
uint16_t count;
uint32_t items, total;
size_t i;
int err = 0;
if (pdu == NULL) {
err = -ETIMEDOUT;
goto done;
}
/* AVRCP 1.5 - Page 76:
* If the TG receives a GetFolderItems command for an empty folder then
* the TG shall return the error (= Range Out of Bounds) in the status
* field of the GetFolderItems response.
*/
if (pdu->params[0] == AVRCP_STATUS_OUT_OF_BOUNDS)
goto done;
if (pdu->params[0] != AVRCP_STATUS_SUCCESS || operand_count < 5) {
err = -EINVAL;
goto done;
}
count = bt_get_be16(&operands[6]);
if (count == 0)
goto done;
for (i = 8; count && i + 3 < operand_count; count--) {
struct media_item *item;
uint8_t type;
uint16_t len;
type = operands[i++];
len = bt_get_be16(&operands[i]);
i += 2;
if (type != 0x03 && type != 0x02) {
i += len;
continue;
}
if (i + len > operand_count) {
error("Invalid item length");
break;
}
if (type == 0x03)
item = parse_media_element(session, &operands[i], len);
else
item = parse_media_folder(session, &operands[i], len);
if (item) {
if (g_slist_find(p->items, item))
goto done;
p->items = g_slist_append(p->items, item);
}
i += len;
}
items = g_slist_length(p->items);
total = p->end - p->start;
if (items < total) {
avrcp_list_items(session, p->start + items + 1, p->end);
return FALSE;
}
done:
media_player_list_complete(player->user_data, p->items, err);
g_slist_free(p->items);
g_free(p);
player->p = NULL;
return FALSE;
}
static void avrcp_list_items(struct avrcp *session, uint32_t start,
uint32_t end)
{
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 10 +
AVRCP_MEDIA_ATTRIBUTE_LAST * sizeof(uint32_t)];
struct avrcp_player *player = session->controller->player;
struct avrcp_browsing_header *pdu = (void *) buf;
uint16_t length = AVRCP_BROWSING_HEADER_LENGTH + 10;
uint32_t attribute;
memset(buf, 0, sizeof(buf));
pdu->pdu_id = AVRCP_GET_FOLDER_ITEMS;
pdu->param_len = htons(10 + sizeof(uint32_t));
pdu->params[0] = player->scope;
bt_put_be32(start, &pdu->params[1]);
bt_put_be32(end, &pdu->params[5]);
pdu->params[9] = 1;
/* Only the title (0x01) is mandatory. This can be extended to
* support AVRCP_MEDIA_ATTRIBUTE_* attributes */
attribute = htonl(AVRCP_MEDIA_ATTRIBUTE_TITLE);
memcpy(&pdu->params[10], &attribute, sizeof(uint32_t));
length += sizeof(uint32_t);
avctp_send_browsing_req(session->conn, buf, length,
avrcp_list_items_rsp, session);
}
static gboolean avrcp_change_path_rsp(struct avctp *conn,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp_browsing_header *pdu = (void *) operands;
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
int ret;
if (pdu == NULL) {
ret = -ETIMEDOUT;
goto done;
}
if (pdu->params[0] != AVRCP_STATUS_SUCCESS) {
ret = -EINVAL;
goto done;
}
ret = bt_get_be32(&pdu->params[1]);
done:
if (ret < 0) {
g_free(player->change_path);
player->change_path = NULL;
} else {
g_free(player->path);
player->path = player->change_path;
player->change_path = NULL;
}
media_player_change_folder_complete(mp, player->path, ret);
return FALSE;
}
static gboolean avrcp_set_browsed_player_rsp(struct avctp *conn,
uint8_t *operands,
size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
struct avrcp_browsing_header *pdu = (void *) operands;
uint32_t items;
char **folders;
uint8_t depth, count;
size_t i;
if (pdu == NULL || pdu->params[0] != AVRCP_STATUS_SUCCESS ||
operand_count < 13)
return FALSE;
player->uid_counter = bt_get_be16(&pdu->params[1]);
player->browsed = true;
items = bt_get_be32(&pdu->params[3]);
depth = pdu->params[9];
folders = g_new0(char *, depth + 2);
folders[0] = g_strdup("/Filesystem");
for (i = 10, count = 1; count - 1 < depth && i < operand_count;
count++) {
uint8_t len;
len = pdu->params[i++];
if (i + len > operand_count || len == 0) {
error("Invalid folder length");
break;
}
folders[count] = g_memdup(&pdu->params[i], len);
i += len;
}
player->path = g_build_pathv("/", folders);
g_strfreev(folders);
media_player_set_folder(mp, player->path, items);
return FALSE;
}
static void avrcp_set_browsed_player(struct avrcp *session,
struct avrcp_player *player)
{
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 2];
struct avrcp_browsing_header *pdu = (void *) buf;
uint16_t id;
memset(buf, 0, sizeof(buf));
pdu->pdu_id = AVRCP_SET_BROWSED_PLAYER;
id = htons(player->id);
memcpy(pdu->params, &id, 2);
pdu->param_len = htons(2);
avctp_send_browsing_req(session->conn, buf, sizeof(buf),
avrcp_set_browsed_player_rsp, session);
}
static gboolean avrcp_get_item_attributes_rsp(struct avctp *conn,
uint8_t *operands,
size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->controller->player;
struct avrcp_browsing_header *pdu = (void *) operands;
uint8_t count;
if (pdu == NULL) {
avrcp_get_element_attributes(session);
return FALSE;
}
if (pdu->params[0] != AVRCP_STATUS_SUCCESS || operand_count < 4) {
avrcp_get_element_attributes(session);
return FALSE;
}
count = pdu->params[1];
if (ntohs(pdu->param_len) - 1 < count * 8) {
error("Invalid parameters");
return FALSE;
}
avrcp_parse_attribute_list(player, &pdu->params[2], count);
avrcp_get_play_status(session);
return FALSE;
}
static void avrcp_get_item_attributes(struct avrcp *session, uint64_t uid)
{
struct avrcp_player *player = session->controller->player;
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 12];
struct avrcp_browsing_header *pdu = (void *) buf;
memset(buf, 0, sizeof(buf));
pdu->pdu_id = AVRCP_GET_ITEM_ATTRIBUTES;
pdu->params[0] = 0x03;
bt_put_be64(uid, &pdu->params[1]);
bt_put_be16(player->uid_counter, &pdu->params[9]);
pdu->param_len = htons(12);
avctp_send_browsing_req(session->conn, buf, sizeof(buf),
avrcp_get_item_attributes_rsp, session);
}
static void avrcp_player_parse_features(struct avrcp_player *player,
uint8_t *features)
{
struct media_player *mp = player->user_data;
player->features = g_memdup(features, 16);
if (features[7] & 0x08) {
media_player_set_browsable(mp, true);
media_player_create_folder(mp, "/Filesystem",
PLAYER_FOLDER_TYPE_MIXED, 0);
}
if (features[7] & 0x10)
media_player_set_searchable(mp, true);
if (features[8] & 0x02) {
media_player_create_folder(mp, "/NowPlaying",
PLAYER_FOLDER_TYPE_MIXED, 0);
media_player_set_playlist(mp, "/NowPlaying");
}
}
static void avrcp_set_player_value(struct avrcp *session, uint8_t attr,
uint8_t val)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 3];
struct avrcp_header *pdu = (void *) buf;
uint8_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_SET_PLAYER_VALUE;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params[0] = 1;
pdu->params[1] = attr;
pdu->params[2] = val;
pdu->params_len = htons(3);
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_CONTROL,
AVC_SUBUNIT_PANEL, buf, length,
avrcp_player_value_rsp, session);
}
static bool ct_set_setting(struct media_player *mp, const char *key,
const char *value, void *user_data)
{
struct avrcp_player *player = user_data;
int attr;
int val;
struct avrcp *session;
session = player->sessions->data;
if (session == NULL)
return false;
if (session->controller->version < 0x0103)
return false;
attr = attr_to_val(key);
if (attr < 0)
return false;
val = attrval_to_val(attr, value);
if (val < 0)
return false;
avrcp_set_player_value(session, attr, val);
return true;
}
static int ct_press(struct avrcp_player *player, uint8_t op)
{
int err;
struct avrcp *session;
session = player->sessions->data;
if (session == NULL)
return -ENOTCONN;
err = avctp_send_passthrough(session->conn, op);
if (err < 0)
return err;
return 0;
}
static int ct_play(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_PLAY);
}
static int ct_pause(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_PAUSE);
}
static int ct_stop(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_STOP);
}
static int ct_next(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_FORWARD);
}
static int ct_previous(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_BACKWARD);
}
static int ct_fast_forward(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_FAST_FORWARD);
}
static int ct_rewind(struct media_player *mp, void *user_data)
{
struct avrcp_player *player = user_data;
return ct_press(player, AVC_REWIND);
}
static int ct_list_items(struct media_player *mp, const char *name,
uint32_t start, uint32_t end, void *user_data)
{
struct avrcp_player *player = user_data;
struct avrcp *session;
struct pending_list_items *p;
if (player->p != NULL)
return -EBUSY;
session = player->sessions->data;
if (g_str_has_prefix(name, "/NowPlaying"))
player->scope = 0x03;
else if (g_str_has_suffix(name, "/search"))
player->scope = 0x02;
else
player->scope = 0x01;
avrcp_list_items(session, start, end);
p = g_new0(struct pending_list_items, 1);
p->start = start;
p->end = end;
player->p = p;
return 0;
}
static void avrcp_change_path(struct avrcp *session, uint8_t direction,
uint64_t uid)
{
struct avrcp_player *player = session->controller->player;
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 11];
struct avrcp_browsing_header *pdu = (void *) buf;
memset(buf, 0, sizeof(buf));
bt_put_be16(player->uid_counter, &pdu->params[0]);
pdu->params[2] = direction;
bt_put_be64(uid, &pdu->params[3]);
pdu->pdu_id = AVRCP_CHANGE_PATH;
pdu->param_len = htons(11);
avctp_send_browsing_req(session->conn, buf, sizeof(buf),
avrcp_change_path_rsp, session);
}
static int ct_change_folder(struct media_player *mp, const char *path,
uint64_t uid, void *user_data)
{
struct avrcp_player *player = user_data;
struct avrcp *session;
uint8_t direction;
session = player->sessions->data;
player->change_path = g_strdup(path);
direction = g_str_has_prefix(path, player->path) ? 0x01 : 0x00;
avrcp_change_path(session, direction, uid);
return 0;
}
static gboolean avrcp_search_rsp(struct avctp *conn, uint8_t *operands,
size_t operand_count, void *user_data)
{
struct avrcp_browsing_header *pdu = (void *) operands;
struct avrcp *session = (void *) user_data;
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
int ret;
if (pdu == NULL) {
ret = -ETIMEDOUT;
goto done;
}
if (pdu->params[0] != AVRCP_STATUS_SUCCESS || operand_count < 7) {
ret = -EINVAL;
goto done;
}
player->uid_counter = bt_get_be16(&pdu->params[1]);
ret = bt_get_be32(&pdu->params[3]);
done:
media_player_search_complete(mp, ret);
return FALSE;
}
static void avrcp_search(struct avrcp *session, const char *string)
{
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 255];
struct avrcp_browsing_header *pdu = (void *) buf;
uint16_t len, stringlen;
memset(buf, 0, sizeof(buf));
len = AVRCP_BROWSING_HEADER_LENGTH + 4;
stringlen = strnlen(string, sizeof(buf) - len);
len += stringlen;
bt_put_be16(AVRCP_CHARSET_UTF8, &pdu->params[0]);
bt_put_be16(stringlen, &pdu->params[2]);
memcpy(&pdu->params[4], string, stringlen);
pdu->pdu_id = AVRCP_SEARCH;
pdu->param_len = htons(len - AVRCP_BROWSING_HEADER_LENGTH);
avctp_send_browsing_req(session->conn, buf, len, avrcp_search_rsp,
session);
}
static int ct_search(struct media_player *mp, const char *string,
void *user_data)
{
struct avrcp_player *player = user_data;
struct avrcp *session;
session = player->sessions->data;
avrcp_search(session, string);
return 0;
}
static void avrcp_play_item(struct avrcp *session, uint64_t uid)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 11];
struct avrcp_player *player = session->controller->player;
struct avrcp_header *pdu = (void *) buf;
uint16_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_PLAY_ITEM;
pdu->params_len = htons(11);
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params[0] = player->scope;
bt_put_be64(uid, &pdu->params[1]);
bt_put_be16(player->uid_counter, &pdu->params[9]);
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, length,
NULL, session);
}
static int ct_play_item(struct media_player *mp, const char *name,
uint64_t uid, void *user_data)
{
struct avrcp_player *player = user_data;
struct avrcp *session;
if (player->p != NULL)
return -EBUSY;
session = player->sessions->data;
if (g_strrstr(name, "/NowPlaying"))
player->scope = 0x03;
else
player->scope = 0x01;
avrcp_play_item(session, uid);
return 0;
}
static void avrcp_add_to_nowplaying(struct avrcp *session, uint64_t uid)
{
uint8_t buf[AVRCP_HEADER_LENGTH + 11];
struct avrcp_player *player = session->controller->player;
struct avrcp_header *pdu = (void *) buf;
uint16_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_ADD_TO_NOW_PLAYING;
pdu->params_len = htons(11);
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params[0] = player->scope;
bt_put_be64(uid, &pdu->params[1]);
bt_put_be16(player->uid_counter, &pdu->params[9]);
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, length,
NULL, session);
}
static int ct_add_to_nowplaying(struct media_player *mp, const char *name,
uint64_t uid, void *user_data)
{
struct avrcp_player *player = user_data;
struct avrcp *session;
if (player->p != NULL)
return -EBUSY;
session = player->sessions->data;
if (g_strrstr(name, "/NowPlaying"))
player->scope = 0x03;
else
player->scope = 0x01;
avrcp_add_to_nowplaying(session, uid);
return 0;
}
static const struct media_player_callback ct_cbs = {
.set_setting = ct_set_setting,
.play = ct_play,
.pause = ct_pause,
.stop = ct_stop,
.next = ct_next,
.previous = ct_previous,
.fast_forward = ct_fast_forward,
.rewind = ct_rewind,
.list_items = ct_list_items,
.change_folder = ct_change_folder,
.search = ct_search,
.play_item = ct_play_item,
.add_to_nowplaying = ct_add_to_nowplaying,
};
static struct avrcp_player *create_ct_player(struct avrcp *session,
uint16_t id)
{
struct avrcp_player *player;
struct media_player *mp;
const char *path;
player = g_new0(struct avrcp_player, 1);
player->sessions = g_slist_prepend(player->sessions, session);
path = device_get_path(session->dev);
mp = media_player_controller_create(path, id);
if (mp == NULL)
return NULL;
media_player_set_callbacks(mp, &ct_cbs, player);
player->user_data = mp;
player->destroy = (GDestroyNotify) media_player_destroy;
if (session->controller->player == NULL)
session->controller->player = player;
session->controller->players = g_slist_prepend(
session->controller->players,
player);
return player;
}
static struct avrcp_player *find_ct_player(struct avrcp *session, uint16_t id)
{
GSList *l;
for (l = session->controller->players; l; l = l->next) {
struct avrcp_player *player = l->data;
if (player->id == 0) {
player->id = id;
return player;
}
if (player->id == id)
return player;
}
return NULL;
}
static struct avrcp_player *
avrcp_parse_media_player_item(struct avrcp *session, uint8_t *operands,
uint16_t len)
{
struct avrcp_player *player;
struct media_player *mp;
uint16_t id, namelen;
uint32_t subtype;
const char *curval, *strval;
char name[255];
if (len < 28)
return NULL;
id = bt_get_be16(&operands[0]);
player = find_ct_player(session, id);
if (player == NULL) {
player = create_ct_player(session, id);
if (player == NULL)
return NULL;
} else if (player->features != NULL)
return player;
mp = player->user_data;
media_player_set_type(mp, type_to_string(operands[2]));
subtype = bt_get_be32(&operands[3]);
media_player_set_subtype(mp, subtype_to_string(subtype));
curval = media_player_get_status(mp);
strval = status_to_string(operands[7]);
if (g_strcmp0(curval, strval) != 0) {
media_player_set_status(mp, strval);
avrcp_get_play_status(session);
}
avrcp_player_parse_features(player, &operands[8]);
namelen = bt_get_be16(&operands[26]);
if (namelen > 0 && namelen + 28 == len) {
namelen = MIN(namelen, sizeof(name) - 1);
memcpy(name, &operands[28], namelen);
name[namelen] = '\0';
media_player_set_name(mp, name);
}
if (session->controller->player == player && !player->browsed)
avrcp_set_browsed_player(session, player);
return player;
}
static void player_destroy(gpointer data)
{
struct avrcp_player *player = data;
if (player->destroy)
player->destroy(player->user_data);
g_slist_free(player->sessions);
g_free(player->path);
g_free(player->change_path);
g_free(player->features);
g_free(player);
}
static void player_remove(gpointer data)
{
struct avrcp_player *player = data;
GSList *l;
for (l = player->sessions; l; l = l->next) {
struct avrcp *session = l->data;
session->controller->players = g_slist_remove(
session->controller->players,
player);
}
player_destroy(player);
}
static gboolean avrcp_get_media_player_list_rsp(struct avctp *conn,
uint8_t *operands,
size_t operand_count,
void *user_data)
{
struct avrcp_browsing_header *pdu = (void *) operands;
struct avrcp *session = user_data;
uint16_t count;
size_t i;
GSList *removed;
if (pdu == NULL || pdu->params[0] != AVRCP_STATUS_SUCCESS ||
operand_count < 5)
return FALSE;
removed = g_slist_copy(session->controller->players);
count = bt_get_be16(&operands[6]);
for (i = 8; count && i < operand_count; count--) {
struct avrcp_player *player;
uint8_t type;
uint16_t len;
type = operands[i++];
len = bt_get_be16(&operands[i]);
i += 2;
if (type != 0x01) {
i += len;
continue;
}
if (i + len > operand_count) {
error("Invalid player item length");
return FALSE;
}
player = avrcp_parse_media_player_item(session, &operands[i],
len);
if (player)
removed = g_slist_remove(removed, player);
i += len;
}
if (g_slist_find(removed, session->controller->player))
session->controller->player = NULL;
g_slist_free_full(removed, player_remove);
return FALSE;
}
static void avrcp_get_media_player_list(struct avrcp *session)
{
uint8_t buf[AVRCP_BROWSING_HEADER_LENGTH + 10];
struct avrcp_browsing_header *pdu = (void *) buf;
memset(buf, 0, sizeof(buf));
pdu->pdu_id = AVRCP_GET_FOLDER_ITEMS;
pdu->param_len = htons(10);
avctp_send_browsing_req(session->conn, buf, sizeof(buf),
avrcp_get_media_player_list_rsp, session);
}
static void avrcp_volume_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
struct avrcp_player *player = session->target->player;
uint8_t volume;
if (player == NULL)
return;
volume = pdu->params[1] & 0x7F;
player->cb->set_volume(volume, session->dev, player->user_data);
}
static void avrcp_status_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
uint8_t value;
const char *curval, *strval;
value = pdu->params[1];
curval = media_player_get_status(mp);
strval = status_to_string(value);
if (g_strcmp0(curval, strval) == 0)
return;
media_player_set_status(mp, strval);
avrcp_get_play_status(session);
}
static void avrcp_track_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
if (session->browsing_id) {
struct avrcp_player *player = session->controller->player;
player->uid = bt_get_be64(&pdu->params[1]);
avrcp_get_item_attributes(session, player->uid);
} else
avrcp_get_element_attributes(session);
}
static void avrcp_setting_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
struct avrcp_player *player = session->controller->player;
struct media_player *mp = player->user_data;
uint8_t count = pdu->params[1];
int i;
for (i = 2; count > 0; count--, i += 2) {
const char *key;
const char *value;
key = attr_to_str(pdu->params[i]);
if (key == NULL)
continue;
value = attrval_to_str(pdu->params[i], pdu->params[i + 1]);
if (value == NULL)
continue;
media_player_set_setting(mp, key, value);
}
}
static void avrcp_available_players_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
avrcp_get_media_player_list(session);
}
static void avrcp_addressed_player_changed(struct avrcp *session,
struct avrcp_header *pdu)
{
struct avrcp_player *player = session->controller->player;
uint16_t id = bt_get_be16(&pdu->params[1]);
if (player != NULL && player->id == id)
return;
player = find_ct_player(session, id);
if (player == NULL) {
player = create_ct_player(session, id);
if (player == NULL)
return;
}
player->uid_counter = bt_get_be16(&pdu->params[3]);
session->controller->player = player;
if (player->features != NULL)
return;
avrcp_get_media_player_list(session);
}
static void avrcp_uids_changed(struct avrcp *session, struct avrcp_header *pdu)
{
struct avrcp_player *player = session->controller->player;
player->uid_counter = bt_get_be16(&pdu->params[1]);
}
static gboolean avrcp_handle_event(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_header *pdu = (void *) operands;
uint8_t event;
if ((code != AVC_CTYPE_INTERIM && code != AVC_CTYPE_CHANGED) ||
pdu == NULL)
return FALSE;
event = pdu->params[0];
if (code == AVC_CTYPE_CHANGED) {
session->registered_events ^= (1 << event);
avrcp_register_notification(session, event);
return FALSE;
}
switch (event) {
case AVRCP_EVENT_VOLUME_CHANGED:
avrcp_volume_changed(session, pdu);
break;
case AVRCP_EVENT_STATUS_CHANGED:
avrcp_status_changed(session, pdu);
break;
case AVRCP_EVENT_TRACK_CHANGED:
avrcp_track_changed(session, pdu);
break;
case AVRCP_EVENT_SETTINGS_CHANGED:
avrcp_setting_changed(session, pdu);
break;
case AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED:
avrcp_available_players_changed(session, pdu);
break;
case AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED:
avrcp_addressed_player_changed(session, pdu);
break;
case AVRCP_EVENT_UIDS_CHANGED:
avrcp_uids_changed(session, pdu);
break;
}
session->registered_events |= (1 << event);
return TRUE;
}
static void avrcp_register_notification(struct avrcp *session, uint8_t event)
{
uint8_t buf[AVRCP_HEADER_LENGTH + AVRCP_REGISTER_NOTIFICATION_PARAM_LENGTH];
struct avrcp_header *pdu = (void *) buf;
uint8_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_REGISTER_NOTIFICATION;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params[0] = event;
pdu->params_len = htons(AVRCP_REGISTER_NOTIFICATION_PARAM_LENGTH);
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_NOTIFY,
AVC_SUBUNIT_PANEL, buf, length,
avrcp_handle_event, session);
}
static gboolean avrcp_get_capabilities_resp(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_header *pdu = (void *) operands;
uint16_t events = 0;
uint8_t count;
if (pdu == NULL || pdu->params[0] != CAP_EVENTS_SUPPORTED)
return FALSE;
/* Connect browsing if pending */
if (session->browsing_timer > 0) {
g_source_remove(session->browsing_timer);
session->browsing_timer = 0;
avctp_connect_browsing(session->conn);
}
count = pdu->params[1];
for (; count > 0; count--) {
uint8_t event = pdu->params[1 + count];
events |= (1 << event);
switch (event) {
case AVRCP_EVENT_STATUS_CHANGED:
case AVRCP_EVENT_TRACK_CHANGED:
case AVRCP_EVENT_SETTINGS_CHANGED:
case AVRCP_EVENT_ADDRESSED_PLAYER_CHANGED:
case AVRCP_EVENT_UIDS_CHANGED:
case AVRCP_EVENT_AVAILABLE_PLAYERS_CHANGED:
case AVRCP_EVENT_VOLUME_CHANGED:
avrcp_register_notification(session, event);
break;
}
}
if (!(events & (1 << AVRCP_EVENT_SETTINGS_CHANGED)))
avrcp_list_player_attributes(session);
if (!(events & (1 << AVRCP_EVENT_STATUS_CHANGED)))
avrcp_get_play_status(session);
if (!(events & (1 << AVRCP_EVENT_STATUS_CHANGED)))
avrcp_get_element_attributes(session);
return FALSE;
}
static void avrcp_get_capabilities(struct avrcp *session)
{
uint8_t buf[AVRCP_HEADER_LENGTH + AVRCP_GET_CAPABILITIES_PARAM_LENGTH];
struct avrcp_header *pdu = (void *) buf;
uint8_t length;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
pdu->pdu_id = AVRCP_GET_CAPABILITIES;
pdu->packet_type = AVRCP_PACKET_TYPE_SINGLE;
pdu->params[0] = CAP_EVENTS_SUPPORTED;
pdu->params_len = htons(AVRCP_GET_CAPABILITIES_PARAM_LENGTH);
length = AVRCP_HEADER_LENGTH + ntohs(pdu->params_len);
avctp_send_vendordep_req(session->conn, AVC_CTYPE_STATUS,
AVC_SUBUNIT_PANEL, buf, length,
avrcp_get_capabilities_resp,
session);
}
static struct avrcp *find_session(GSList *list, struct btd_device *dev)
{
for (; list; list = list->next) {
struct avrcp *session = list->data;
if (session->dev == dev)
return session;
}
return NULL;
}
static void destroy_browsing(void *data)
{
struct avrcp *session = data;
session->browsing_id = 0;
}
static void session_init_browsing(struct avrcp *session)
{
if (session->browsing_timer > 0) {
g_source_remove(session->browsing_timer);
session->browsing_timer = 0;
}
session->browsing_id = avctp_register_browsing_pdu_handler(
session->conn,
handle_browsing_pdu,
session,
destroy_browsing);
}
static struct avrcp_data *data_init(struct avrcp *session, const char *uuid)
{
struct avrcp_data *data;
const sdp_record_t *rec;
sdp_list_t *list;
sdp_profile_desc_t *desc;
data = g_new0(struct avrcp_data, 1);
rec = btd_device_get_record(session->dev, uuid);
if (rec == NULL)
return data;
if (sdp_get_profile_descs(rec, &list) == 0) {
desc = list->data;
data->version = desc->version;
}
sdp_get_int_attr(rec, SDP_ATTR_SUPPORTED_FEATURES, &data->features);
sdp_list_free(list, free);
return data;
}
static gboolean connect_browsing(gpointer user_data)
{
struct avrcp *session = user_data;
session->browsing_timer = 0;
avctp_connect_browsing(session->conn);
return FALSE;
}
static void avrcp_connect_browsing(struct avrcp *session)
{
/* Immediately connect browsing channel if initiator otherwise delay
* it to avoid possible collisions
*/
if (avctp_is_initiator(session->conn)) {
avctp_connect_browsing(session->conn);
return;
}
if (session->browsing_timer > 0)
return;
session->browsing_timer = g_timeout_add_seconds(AVRCP_BROWSING_TIMEOUT,
connect_browsing,
session);
}
static void target_init(struct avrcp *session)
{
struct avrcp_server *server = session->server;
struct avrcp_data *target;
struct avrcp_player *player;
struct btd_service *service;
if (session->target != NULL)
return;
target = data_init(session, AVRCP_REMOTE_UUID);
session->target = target;
DBG("%p version 0x%04x", target, target->version);
service = btd_device_get_service(session->dev, AVRCP_REMOTE_UUID);
if (service != NULL)
btd_service_connecting_complete(service, 0);
player = g_slist_nth_data(server->players, 0);
if (player != NULL) {
target->player = player;
player->sessions = g_slist_prepend(player->sessions, session);
}
session->supported_events |= (1 << AVRCP_EVENT_STATUS_CHANGED) |
(1 << AVRCP_EVENT_TRACK_CHANGED) |
(1 << AVRCP_EVENT_TRACK_REACHED_START) |
(1 << AVRCP_EVENT_TRACK_REACHED_END) |
(1 << AVRCP_EVENT_SETTINGS_CHANGED);
if (target->version < 0x0104)
return;
/* Only check capabilities if controller is not supported */
if (session->controller == NULL)
avrcp_get_capabilities(session);
if (!(target->features & AVRCP_FEATURE_BROWSING))
return;
avrcp_connect_browsing(session);
}
static void controller_init(struct avrcp *session)
{
struct avrcp_player *player;
struct btd_service *service;
struct avrcp_data *controller;
if (session->controller != NULL)
return;
controller = data_init(session, AVRCP_TARGET_UUID);
session->controller = controller;
DBG("%p version 0x%04x", controller, controller->version);
if (controller->version >= 0x0104)
session->supported_events |= (1 << AVRCP_EVENT_VOLUME_CHANGED);
service = btd_device_get_service(session->dev, AVRCP_TARGET_UUID);
if (service != NULL)
btd_service_connecting_complete(service, 0);
/* Only create player if category 1 is supported */
if (!(controller->features & AVRCP_FEATURE_CATEGORY_1))
return;
player = create_ct_player(session, 0);
if (player == NULL)
return;
if (controller->version < 0x0103)
return;
avrcp_get_capabilities(session);
if (controller->version < 0x0104)
return;
if (!(controller->features & AVRCP_FEATURE_BROWSING))
return;
avrcp_connect_browsing(session);
}
static void session_init_control(struct avrcp *session)
{
session->passthrough_id = avctp_register_passthrough_handler(
session->conn,
handle_passthrough,
session);
session->passthrough_handlers = passthrough_handlers;
session->control_id = avctp_register_pdu_handler(session->conn,
AVC_OP_VENDORDEP,
handle_vendordep_pdu,
session);
session->control_handlers = control_handlers;
if (btd_device_get_service(session->dev, AVRCP_TARGET_UUID) != NULL)
controller_init(session);
if (btd_device_get_service(session->dev, AVRCP_REMOTE_UUID) != NULL)
target_init(session);
}
static void controller_destroy(struct avrcp *session)
{
struct avrcp_data *controller = session->controller;
struct btd_service *service;
DBG("%p", controller);
g_slist_free_full(controller->players, player_destroy);
service = btd_device_get_service(session->dev, AVRCP_TARGET_UUID);
if (session->control_id == 0)
btd_service_connecting_complete(service, -EIO);
else
btd_service_disconnecting_complete(service, 0);
g_free(controller);
}
static void target_destroy(struct avrcp *session)
{
struct avrcp_data *target = session->target;
struct avrcp_player *player = target->player;
struct btd_service *service;
DBG("%p", target);
if (player != NULL)
player->sessions = g_slist_remove(player->sessions, session);
service = btd_device_get_service(session->dev, AVRCP_REMOTE_UUID);
if (session->control_id == 0)
btd_service_connecting_complete(service, -EIO);
else
btd_service_disconnecting_complete(service, 0);
g_free(target);
}
static void session_destroy(struct avrcp *session)
{
struct avrcp_server *server = session->server;
server->sessions = g_slist_remove(server->sessions, session);
if (session->browsing_timer > 0)
g_source_remove(session->browsing_timer);
if (session->controller != NULL)
controller_destroy(session);
if (session->target != NULL)
target_destroy(session);
if (session->passthrough_id > 0)
avctp_unregister_passthrough_handler(session->passthrough_id);
if (session->control_id > 0)
avctp_unregister_pdu_handler(session->control_id);
if (session->browsing_id > 0)
avctp_unregister_browsing_pdu_handler(session->browsing_id);
g_free(session);
}
static struct avrcp *session_create(struct avrcp_server *server,
struct btd_device *device)
{
struct avrcp *session;
session = g_new0(struct avrcp, 1);
session->server = server;
session->conn = avctp_connect(device);
session->dev = device;
server->sessions = g_slist_append(server->sessions, session);
return session;
}
static void state_changed(struct btd_device *device, avctp_state_t old_state,
avctp_state_t new_state, void *user_data)
{
struct avrcp_server *server;
struct avrcp *session;
server = find_server(servers, device_get_adapter(device));
if (!server)
return;
session = find_session(server->sessions, device);
switch (new_state) {
case AVCTP_STATE_DISCONNECTED:
if (session == NULL)
break;
session_destroy(session);
break;
case AVCTP_STATE_CONNECTING:
if (session != NULL)
break;
session_create(server, device);
break;
case AVCTP_STATE_CONNECTED:
if (session == NULL || session->control_id > 0)
break;
session_init_control(session);
break;
case AVCTP_STATE_BROWSING_CONNECTED:
if (session == NULL || session->browsing_id > 0)
break;
session_init_browsing(session);
break;
default:
return;
}
}
static struct avrcp_server *avrcp_server_register(struct btd_adapter *adapter)
{
struct avrcp_server *server;
if (avctp_register(adapter, TRUE) < 0)
return NULL;
server = g_new0(struct avrcp_server, 1);
server->adapter = btd_adapter_ref(adapter);
servers = g_slist_append(servers, server);
if (!avctp_id)
avctp_id = avctp_add_state_cb(NULL, state_changed, NULL);
return server;
}
static void avrcp_server_unregister(struct avrcp_server *server)
{
g_slist_free_full(server->sessions, g_free);
g_slist_free_full(server->players, player_destroy);
servers = g_slist_remove(servers, server);
avctp_unregister(server->adapter);
btd_adapter_unref(server->adapter);
g_free(server);
if (servers)
return;
if (avctp_id) {
avctp_remove_state_cb(avctp_id);
avctp_id = 0;
}
}
struct avrcp_player *avrcp_register_player(struct btd_adapter *adapter,
struct avrcp_player_cb *cb,
void *user_data,
GDestroyNotify destroy)
{
struct avrcp_server *server;
struct avrcp_player *player;
GSList *l;
server = find_server(servers, adapter);
if (!server)
return NULL;
player = g_new0(struct avrcp_player, 1);
player->server = server;
player->cb = cb;
player->user_data = user_data;
player->destroy = destroy;
server->players = g_slist_append(server->players, player);
/* Assign player to session without current player */
for (l = server->sessions; l; l = l->next) {
struct avrcp *session = l->data;
struct avrcp_data *target = session->target;
if (target == NULL)
continue;
if (target->player == NULL) {
target->player = player;
player->sessions = g_slist_append(player->sessions,
session);
}
}
return player;
}
void avrcp_unregister_player(struct avrcp_player *player)
{
struct avrcp_server *server = player->server;
GSList *l;
server->players = g_slist_remove(server->players, player);
/* Remove player from sessions using it */
for (l = player->sessions; l; l = l->next) {
struct avrcp *session = l->data;
struct avrcp_data *target = session->target;
if (target == NULL)
continue;
if (target->player == player)
target->player = g_slist_nth_data(server->players, 0);
}
player_destroy(player);
}
static gboolean avrcp_handle_set_volume(struct avctp *conn,
uint8_t code, uint8_t subunit,
uint8_t *operands, size_t operand_count,
void *user_data)
{
struct avrcp *session = user_data;
struct avrcp_player *player = session->target->player;
struct avrcp_header *pdu = (void *) operands;
uint8_t volume;
if (code == AVC_CTYPE_REJECTED || code == AVC_CTYPE_NOT_IMPLEMENTED ||
pdu == NULL)
return FALSE;
volume = pdu->params[0] & 0x7F;
if (player != NULL)
player->cb->set_volume(volume, session->dev, player->user_data);
return FALSE;
}
int avrcp_set_volume(struct btd_device *dev, uint8_t volume)
{
struct avrcp_server *server;
struct avrcp *session;
uint8_t buf[AVRCP_HEADER_LENGTH + 2];
struct avrcp_header *pdu = (void *) buf;
server = find_server(servers, device_get_adapter(dev));
if (server == NULL)
return -EINVAL;
session = find_session(server->sessions, dev);
if (session == NULL || session->target == NULL)
return -ENOTCONN;
if (session->target->version < 0x0104)
return -ENOTSUP;
memset(buf, 0, sizeof(buf));
set_company_id(pdu->company_id, IEEEID_BTSIG);
DBG("volume=%u", volume);
pdu->pdu_id = AVRCP_SET_ABSOLUTE_VOLUME;
pdu->params[0] = volume;
pdu->params_len = htons(1);
return avctp_send_vendordep_req(session->conn,
AVC_CTYPE_CONTROL, AVC_SUBUNIT_PANEL,
buf, sizeof(buf),
avrcp_handle_set_volume, session);
}
static int avrcp_connect(struct btd_service *service)
{
struct btd_device *dev = btd_service_get_device(service);
const char *path = device_get_path(dev);
DBG("path %s", path);
return control_connect(service);
}
static int avrcp_disconnect(struct btd_service *service)
{
struct btd_device *dev = btd_service_get_device(service);
const char *path = device_get_path(dev);
DBG("path %s", path);
return control_disconnect(service);
}
static int avrcp_target_probe(struct btd_service *service)
{
struct btd_device *dev = btd_service_get_device(service);
DBG("path %s", device_get_path(dev));
return control_init_target(service);
}
static void avrcp_target_remove(struct btd_service *service)
{
control_unregister(service);
}
static void avrcp_target_server_remove(struct btd_profile *p,
struct btd_adapter *adapter)
{
struct avrcp_server *server;
DBG("path %s", adapter_get_path(adapter));
server = find_server(servers, adapter);
if (!server)
return;
if (server->tg_record_id != 0) {
adapter_service_remove(adapter, server->tg_record_id);
server->tg_record_id = 0;
}
if (server->ct_record_id == 0)
avrcp_server_unregister(server);
}
static int avrcp_target_server_probe(struct btd_profile *p,
struct btd_adapter *adapter)
{
sdp_record_t *record;
struct avrcp_server *server;
DBG("path %s", adapter_get_path(adapter));
server = find_server(servers, adapter);
if (server != NULL)
goto done;
server = avrcp_server_register(adapter);
if (server == NULL)
return -EPROTONOSUPPORT;
done:
record = avrcp_tg_record();
if (!record) {
error("Unable to allocate new service record");
avrcp_target_server_remove(p, adapter);
return -1;
}
if (adapter_service_add(adapter, record) < 0) {
error("Unable to register AVRCP target service record");
avrcp_target_server_remove(p, adapter);
sdp_record_free(record);
return -1;
}
server->tg_record_id = record->handle;
return 0;
}
static struct btd_profile avrcp_target_profile = {
.name = "audio-avrcp-target",
.remote_uuid = AVRCP_TARGET_UUID,
.device_probe = avrcp_target_probe,
.device_remove = avrcp_target_remove,
.connect = avrcp_connect,
.disconnect = avrcp_disconnect,
.adapter_probe = avrcp_target_server_probe,
.adapter_remove = avrcp_target_server_remove,
};
static int avrcp_controller_probe(struct btd_service *service)
{
struct btd_device *dev = btd_service_get_device(service);
DBG("path %s", device_get_path(dev));
return control_init_remote(service);
}
static void avrcp_controller_remove(struct btd_service *service)
{
control_unregister(service);
}
static void avrcp_controller_server_remove(struct btd_profile *p,
struct btd_adapter *adapter)
{
struct avrcp_server *server;
DBG("path %s", adapter_get_path(adapter));
server = find_server(servers, adapter);
if (!server)
return;
if (server->ct_record_id != 0) {
adapter_service_remove(adapter, server->ct_record_id);
server->ct_record_id = 0;
}
if (server->tg_record_id == 0)
avrcp_server_unregister(server);
}
static int avrcp_controller_server_probe(struct btd_profile *p,
struct btd_adapter *adapter)
{
sdp_record_t *record;
struct avrcp_server *server;
DBG("path %s", adapter_get_path(adapter));
server = find_server(servers, adapter);
if (server != NULL)
goto done;
server = avrcp_server_register(adapter);
if (server == NULL)
return -EPROTONOSUPPORT;
done:
record = avrcp_ct_record();
if (!record) {
error("Unable to allocate new service record");
avrcp_controller_server_remove(p, adapter);
return -1;
}
if (adapter_service_add(adapter, record) < 0) {
error("Unable to register AVRCP service record");
avrcp_controller_server_remove(p, adapter);
sdp_record_free(record);
return -1;
}
server->ct_record_id = record->handle;
return 0;
}
static struct btd_profile avrcp_controller_profile = {
.name = "avrcp-controller",
.remote_uuid = AVRCP_REMOTE_UUID,
.device_probe = avrcp_controller_probe,
.device_remove = avrcp_controller_remove,
.connect = avrcp_connect,
.disconnect = avrcp_disconnect,
.adapter_probe = avrcp_controller_server_probe,
.adapter_remove = avrcp_controller_server_remove,
};
static int avrcp_init(void)
{
btd_profile_register(&avrcp_controller_profile);
btd_profile_register(&avrcp_target_profile);
return 0;
}
static void avrcp_exit(void)
{
btd_profile_unregister(&avrcp_controller_profile);
btd_profile_unregister(&avrcp_target_profile);
}
BLUETOOTH_PLUGIN_DEFINE(avrcp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
avrcp_init, avrcp_exit)
|