summaryrefslogtreecommitdiff
path: root/monitor/att.c
blob: 0e12ee352078a40df3413781437d76adec6731f1 (plain)
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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2011-2014  Intel Corporation
 *  Copyright (C) 2002-2010  Marcel Holtmann <marcel@holtmann.org>
 *  Copyright 2023 NXP
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <stdbool.h>
#include <errno.h>
#include <linux/limits.h>
#include <sys/stat.h>

#include <glib.h>

#include "lib/bluetooth.h"
#include "lib/uuid.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"

#include "src/shared/util.h"
#include "src/shared/queue.h"
#include "src/shared/att.h"
#include "src/shared/gatt-db.h"
#include "src/textfile.h"
#include "src/settings.h"
#include "bt.h"
#include "packet.h"
#include "display.h"
#include "l2cap.h"
#include "att.h"
#include "keys.h"

struct att_read {
	struct gatt_db_attribute *attr;
	bool in;
	uint16_t chan;
	void (*func)(const struct l2cap_frame *frame);
};

struct att_conn_data {
	struct gatt_db *ldb;
	struct timespec ldb_mtim;
	struct gatt_db *rdb;
	struct timespec rdb_mtim;
	struct queue *reads;
};

static void print_uuid(const char *label, const void *data, uint16_t size)
{
	const char *str;
	char uuidstr[MAX_LEN_UUID_STR];

	switch (size) {
	case 2:
		str = bt_uuid16_to_str(get_le16(data));
		print_field("%s: %s (0x%4.4x)", label, str, get_le16(data));
		break;
	case 4:
		str = bt_uuid32_to_str(get_le32(data));
		print_field("%s: %s (0x%8.8x)", label, str, get_le32(data));
		break;
	case 16:
		sprintf(uuidstr, "%8.8x-%4.4x-%4.4x-%4.4x-%8.8x%4.4x",
				get_le32(data + 12), get_le16(data + 10),
				get_le16(data + 8), get_le16(data + 6),
				get_le32(data + 2), get_le16(data + 0));
		str = bt_uuidstr_to_str(uuidstr);
		print_field("%s: %s (%s)", label, str, uuidstr);
		break;
	default:
		packet_hexdump(data, size);
		break;
	}
}

static void print_handle_range(const char *label, const void *data)
{
	print_field("%s: 0x%4.4x-0x%4.4x", label,
				get_le16(data), get_le16(data + 2));
}

static bool match_read_frame(const void *data, const void *match_data)
{
	const struct att_read *read = data;
	const struct l2cap_frame *frame = match_data;

	/* Read frame and response frame shall be in the opposite direction to
	 * match.
	 */
	if (read->in == frame->in)
		return false;

	return read->chan == frame->chan;
}

static struct att_read *att_get_read(const struct l2cap_frame *frame)
{
	struct packet_conn_data *conn;
	struct att_conn_data *data;

	conn = packet_get_conn_data(frame->handle);
	if (!conn)
		return NULL;

	data = conn->data;
	if (!data)
		return NULL;

	return queue_remove_if(data->reads, match_read_frame, (void *)frame);
}

static void print_value(struct gatt_db_attribute *attr)
{
	uint16_t handle;
	struct gatt_db_attribute *val;
	const bt_uuid_t *uuid;
	bt_uuid_t chrc = {
		.type = BT_UUID16,
		.value.u16 = 0x2803,
	};
	char label[27];

	uuid = gatt_db_attribute_get_type(attr);
	if (!uuid)
		return;

	/* Skip in case of characteristic declaration since it already prints
	 * the value handle and properties.
	 */
	if (!bt_uuid_cmp(uuid, &chrc))
		return;

	val = gatt_db_attribute_get_value(attr);
	if (!val || val == attr)
		return;

	uuid = gatt_db_attribute_get_type(val);
	if (!uuid)
		return;

	handle = gatt_db_attribute_get_handle(val);
	if (!handle)
		return;

	switch (uuid->type) {
	case BT_UUID16:
		sprintf(label, "Value Handle: 0x%4.4x Type", handle);
		print_field("%s: %s (0x%4.4x)", label,
				bt_uuid16_to_str(uuid->value.u16),
				uuid->value.u16);
		return;
	case BT_UUID128:
		sprintf(label, "Value Handle: 0x%4.4x Type", handle);
		print_uuid(label, &uuid->value.u128, 16);
		return;
	case BT_UUID_UNSPEC:
	case BT_UUID32:
		break;
	}
}

static void print_attribute(struct gatt_db_attribute *attr)
{
	uint16_t handle;
	const bt_uuid_t *uuid;
	char label[21];

	handle = gatt_db_attribute_get_handle(attr);
	if (!handle)
		goto done;

	uuid = gatt_db_attribute_get_type(attr);
	if (!uuid)
		goto done;

	switch (uuid->type) {
	case BT_UUID16:
		sprintf(label, "Handle: 0x%4.4x Type", handle);
		print_field("%s: %s (0x%4.4x)", label,
				bt_uuid16_to_str(uuid->value.u16),
				uuid->value.u16);
		print_value(attr);
		return;
	case BT_UUID128:
		sprintf(label, "Handle: 0x%4.4x Type", handle);
		print_uuid(label, &uuid->value.u128, 16);
		print_value(attr);
		return;
	case BT_UUID_UNSPEC:
	case BT_UUID32:
		break;
	}

done:
	print_field("Handle: 0x%4.4x", handle);
}

static void print_data_list(const char *label, uint8_t length,
					const struct l2cap_frame *frame)
{
	struct att_read *read;
	uint8_t count;

	if (length == 0)
		return;

	read = att_get_read(frame);

	count = frame->size / length;

	print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");

	while (frame->size >= length) {
		if (!l2cap_frame_print_le16((void *)frame, "Handle"))
			break;

		print_hex_field("Value", frame->data, length - 2);

		if (read) {
			struct l2cap_frame f;

			l2cap_frame_clone_size(&f, frame, length - 2);

			read->func(&f);
		}

		if (!l2cap_frame_pull((void *)frame, frame, length - 2))
			break;
	}

	packet_hexdump(frame->data, frame->size);
	free(read);
}

static void print_attribute_info(uint16_t type, const void *data, uint16_t len)
{
	const char *str = bt_uuid16_to_str(type);

	print_field("%s: %s (0x%4.4x)", "Attribute type", str, type);

	switch (type) {
	case 0x2800:	/* Primary Service */
	case 0x2801:	/* Secondary Service */
		print_uuid("  UUID", data, len);
		break;
	case 0x2802:	/* Include */
		if (len < 4) {
			print_hex_field("  Value", data, len);
			break;
		}
		print_handle_range("  Handle range", data);
		print_uuid("  UUID", data + 4, len - 4);
		break;
	case 0x2803:	/* Characteristic */
		if (len < 3) {
			print_hex_field("  Value", data, len);
			break;
		}
		print_field("  Properties: 0x%2.2x", *((uint8_t *) data));
		print_field("  Handle: 0x%2.2x", get_le16(data + 1));
		print_uuid("  UUID", data + 3, len - 3);
		break;
	default:
		print_hex_field("Value", data, len);
		break;
	}
}

static const char *att_opcode_to_str(uint8_t opcode);

static void att_error_response(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_error_response *pdu = frame->data;
	const char *str;

	switch (pdu->error) {
	case 0x01:
		str = "Invalid Handle";
		break;
	case 0x02:
		str = "Read Not Permitted";
		break;
	case 0x03:
		str = "Write Not Permitted";
		break;
	case 0x04:
		str = "Invalid PDU";
		break;
	case 0x05:
		str = "Insufficient Authentication";
		break;
	case 0x06:
		str = "Request Not Supported";
		break;
	case 0x07:
		str = "Invalid Offset";
		break;
	case 0x08:
		str = "Insufficient Authorization";
		break;
	case 0x09:
		str = "Prepare Queue Full";
		break;
	case 0x0a:
		str = "Attribute Not Found";
		break;
	case 0x0b:
		str = "Attribute Not Long";
		break;
	case 0x0c:
		str = "Insufficient Encryption Key Size";
		break;
	case 0x0d:
		str = "Invalid Attribute Value Length";
		break;
	case 0x0e:
		str = "Unlikely Error";
		break;
	case 0x0f:
		str = "Insufficient Encryption";
		break;
	case 0x10:
		str = "Unsupported Group Type";
		break;
	case 0x11:
		str = "Insufficient Resources";
		break;
	case 0x12:
		str = "Database Out of Sync";
		break;
	case 0x13:
		str = "Value Not Allowed";
		break;
	case 0xfd:
		str = "CCC Improperly Configured";
		break;
	case 0xfe:
		str = "Procedure Already in Progress";
		break;
	case 0xff:
		str = "Out of Range";
		break;
	default:
		str = "Reserved";
		break;
	}

	print_field("%s (0x%2.2x)", att_opcode_to_str(pdu->request),
							pdu->request);
	print_field("Handle: 0x%4.4x", le16_to_cpu(pdu->handle));
	print_field("Error: %s (0x%2.2x)", str, pdu->error);

	/* Read/Read By Type/Read By Group Type may create a read object which
	 * needs to be dequeued and freed in case the operation fails.
	 */
	if (pdu->request == 0x08 || pdu->request == 0x0a ||
					pdu->request == 0x10)
		free(att_get_read(frame));
}

static const struct bitfield_data chrc_prop_table[] = {
	{  0, "Broadcast (0x01)"		},
	{  1, "Read (0x02)"			},
	{  2, "Write Without Response (0x04)"	},
	{  3, "Write (0x08)"			},
	{  4, "Notify (0x10)"			},
	{  5, "Indicate (0x20)"			},
	{  6, "Authorize (0x40)"		},
	{  6, "Extended Properties (0x80)"	},
	{ }
};

static void att_conn_data_free(void *data)
{
	struct att_conn_data *att_data = data;

	gatt_db_unref(att_data->rdb);
	gatt_db_unref(att_data->ldb);
	queue_destroy(att_data->reads, free);
	free(att_data);
}

static struct att_conn_data *att_get_conn_data(struct packet_conn_data *conn)
{
	struct att_conn_data *data;

	if (!conn)
		return NULL;

	data = conn->data;

	if (data)
		return data;

	data = new0(struct att_conn_data, 1);
	data->rdb = gatt_db_new();
	data->ldb = gatt_db_new();
	conn->data = data;
	conn->destroy = att_conn_data_free;

	return data;
}

static void gatt_load_db(struct gatt_db *db, const char *filename,
						struct timespec *mtim)
{
	struct stat st;

	if (lstat(filename, &st))
		return;

	if (!gatt_db_isempty(db)) {
		/* Check if file has been modified since last time */
		if (st.st_mtim.tv_sec == mtim->tv_sec &&
				    st.st_mtim.tv_nsec == mtim->tv_nsec)
			return;
		/* Clear db before reloading */
		gatt_db_clear(db);
	}

	*mtim = st.st_mtim;

	btd_settings_gatt_db_load(db, filename);
}

static void load_gatt_db(struct packet_conn_data *conn)
{
	struct att_conn_data *data = att_get_conn_data(conn);
	char filename[PATH_MAX];
	char local[18];
	char peer[18];
	uint8_t id[6], id_type;

	ba2str((bdaddr_t *)conn->src, local);

	if (keys_resolve_identity(conn->dst, id, &id_type))
		ba2str((bdaddr_t *)id, peer);
	else
		ba2str((bdaddr_t *)conn->dst, peer);

	create_filename(filename, PATH_MAX, "/%s/attributes", local);
	gatt_load_db(data->ldb, filename, &data->ldb_mtim);

	create_filename(filename, PATH_MAX, "/%s/cache/%s", local, peer);
	gatt_load_db(data->rdb, filename, &data->rdb_mtim);
}

static struct gatt_db *get_db(const struct l2cap_frame *frame, bool rsp)
{
	struct packet_conn_data *conn;
	struct att_conn_data *data;
	struct gatt_db *db;

	conn = packet_get_conn_data(frame->handle);
	if (!conn)
		return NULL;

	/* Try loading local and remote gatt_db if not loaded yet */
	load_gatt_db(conn);

	data = conn->data;
	if (!data)
		return NULL;

	if (frame->in) {
		if (rsp)
			db = data->rdb;
		else
			db = data->ldb;
	} else {
		if (rsp)
			db = data->ldb;
		else
			db = data->rdb;
	}

	return db;
}

static struct gatt_db_attribute *insert_chrc(const struct l2cap_frame *frame,
						uint16_t handle,
						bt_uuid_t *uuid, uint8_t prop,
						bool rsp)
{
	struct gatt_db *db;

	db = get_db(frame, rsp);
	if (!db)
		return NULL;

	return gatt_db_insert_characteristic(db, handle, uuid, 0, prop, NULL,
							NULL, NULL);
}

static int bt_uuid_from_data(bt_uuid_t *uuid, const void *data, uint16_t size)
{
	uint128_t u128;

	if (!uuid)
		return -EINVAL;

	switch (size) {
	case 2:
		return bt_uuid16_create(uuid, get_le16(data));
	case 4:
		return bt_uuid32_create(uuid, get_le32(data));
	case 16:
		memcpy(u128.data, data, sizeof(u128.data));
		return bt_uuid128_create(uuid, u128);
	}

	return -EINVAL;
}

static bool svc_read(const struct l2cap_frame *frame, uint16_t *start,
			uint16_t *end, bt_uuid_t *uuid)
{
	if (!l2cap_frame_get_le16((void *)frame, start))
		return false;

	if (!l2cap_frame_get_le16((void *)frame, end))
		return false;

	return !bt_uuid_from_data(uuid, frame->data, frame->size);
}

static struct gatt_db_attribute *insert_svc(const struct l2cap_frame *frame,
						uint16_t handle,
						bt_uuid_t *uuid, bool primary,
						bool rsp, uint16_t num_handles)
{
	struct gatt_db *db;

	db = get_db(frame, rsp);
	if (!db)
		return NULL;

	return gatt_db_insert_service(db, handle, uuid, primary, num_handles);
}

static void pri_svc_read(const struct l2cap_frame *frame)
{
	uint16_t start, end;
	bt_uuid_t uuid;

	if (!svc_read(frame, &start, &end, &uuid))
		return;

	insert_svc(frame, start, &uuid, true, true, end - start + 1);
}

static void sec_svc_read(const struct l2cap_frame *frame)
{
	uint16_t start, end;
	bt_uuid_t uuid;

	if (!svc_read(frame, &start, &end, &uuid))
		return;

	insert_svc(frame, start, &uuid, true, false, end - start + 1);
}

static void print_chrc(const struct l2cap_frame *frame)
{
	uint8_t prop;
	uint8_t mask;
	uint16_t handle;
	bt_uuid_t uuid;

	if (!l2cap_frame_get_u8((void *)frame, &prop)) {
		print_text(COLOR_ERROR, "Property: invalid size");
		return;
	}

	print_field("    Properties: 0x%2.2x", prop);

	mask = print_bitfield(6, prop, chrc_prop_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);

	if (!l2cap_frame_get_le16((void *)frame, &handle)) {
		print_text(COLOR_ERROR, "    Value Handle: invalid size");
		return;
	}

	print_field("    Value Handle: 0x%4.4x", handle);
	print_uuid("    Value UUID", frame->data, frame->size);
	bt_uuid_from_data(&uuid, frame->data, frame->size);

	insert_chrc(frame, handle, &uuid, prop, true);
}

static void chrc_read(const struct l2cap_frame *frame)
{
	print_chrc(frame);
}

static const struct bitfield_data ccc_value_table[] = {
	{  0, "Notification (0x01)"		},
	{  1, "Indication (0x02)"		},
	{ }
};

static void print_ccc_value(const struct l2cap_frame *frame)
{
	uint8_t value;
	uint8_t mask;

	if (!l2cap_frame_get_u8((void *)frame, &value)) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	mask = print_bitfield(4, value, ccc_value_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);
}

static void ccc_read(const struct l2cap_frame *frame)
{
	print_ccc_value(frame);
}

static void ccc_write(const struct l2cap_frame *frame)
{
	print_ccc_value(frame);
}

static bool print_ase_codec(const struct l2cap_frame *frame)
{
	uint8_t codec_id;
	uint16_t codec_cid, codec_vid;

	if (!l2cap_frame_get_u8((void *)frame, &codec_id)) {
		print_text(COLOR_ERROR, "Codec: invalid size");
		return false;
	}

	packet_print_codec_id("    Codec", codec_id);

	if (!l2cap_frame_get_le16((void *)frame, &codec_cid)) {
		print_text(COLOR_ERROR, "Codec Company ID: invalid size");
		return false;
	}

	if (!l2cap_frame_get_le16((void *)frame, &codec_vid)) {
		print_text(COLOR_ERROR, "Codec Vendor ID: invalid size");
		return false;
	}

	if (codec_id == 0xff) {
		print_field("    Codec Company ID: %s (0x%04x)",
						bt_compidtostr(codec_cid),
						codec_cid);
		print_field("    Codec Vendor ID: 0x%04x", codec_vid);
	}

	return true;
}

static bool print_ase_lv(const struct l2cap_frame *frame, const char *label,
			struct packet_ltv_decoder *decoder, size_t decoder_len)
{
	struct bt_hci_lv_data *lv;

	lv = l2cap_frame_pull((void *)frame, frame, sizeof(*lv));
	if (!lv) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	if (!l2cap_frame_pull((void *)frame, frame, lv->len)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	packet_print_ltv(label, lv->data, lv->len, decoder, decoder_len);

	return true;
}

static bool print_ase_cc(const struct l2cap_frame *frame, const char *label,
			struct packet_ltv_decoder *decoder, size_t decoder_len)
{
	return print_ase_lv(frame, label, decoder, decoder_len);
}

static const struct bitfield_data pac_context_table[] = {
	{  0, "Unspecified (0x0001)"			},
	{  1, "Conversational (0x0002)"			},
	{  2, "Media (0x0004)"				},
	{  3, "Game (0x0008)"				},
	{  4, "Instructional (0x0010)"			},
	{  5, "Voice Assistants (0x0020)"		},
	{  6, "Live (0x0040)"				},
	{  7, "Sound Effects (0x0080)"			},
	{  8, "Notifications (0x0100)"			},
	{  9, "Ringtone (0x0200)"			},
	{  10, "Alerts (0x0400)"			},
	{  11, "Emergency alarm (0x0800)"		},
	{  12, "RFU (0x1000)"				},
	{  13, "RFU (0x2000)"				},
	{  14, "RFU (0x4000)"				},
	{  15, "RFU (0x8000)"				},
	{ }
};

static void print_context(const struct l2cap_frame *frame, const char *label)
{
	uint16_t value;
	uint16_t mask;

	if (!l2cap_frame_get_le16((void *)frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("%s: 0x%4.4x", label, value);

	mask = print_bitfield(8, value, pac_context_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
								mask);

done:
	if (frame->size)
		print_hex_field("    Data", frame->data, frame->size);
}

static void ase_decode_preferred_context(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	print_context(&frame, "      Preferred Context");
}

static void ase_decode_context(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	print_context(&frame, "      Context");
}

static void ase_decode_program_info(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	const char *str;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	str = l2cap_frame_pull(&frame, &frame, len);
	if (!str) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Program Info: %s", str);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static void ase_decode_language(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint32_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_le24(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Language: 0x%6.6x", value);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

struct packet_ltv_decoder ase_metadata_table[] = {
	LTV_DEC(0x01, ase_decode_preferred_context),
	LTV_DEC(0x02, ase_decode_context),
	LTV_DEC(0x03, ase_decode_program_info),
	LTV_DEC(0x04, ase_decode_language)
};

static bool print_ase_metadata(const struct l2cap_frame *frame)
{
	return print_ase_lv(frame, "    Metadata", NULL, 0);
}

static const struct bitfield_data pac_freq_table[] = {
	{  0, "8 Khz (0x0001)"				},
	{  1, "11.25 Khz (0x0002)"			},
	{  2, "16 Khz (0x0004)"				},
	{  3, "22.05 Khz (0x0008)"			},
	{  4, "24 Khz (0x0010)"				},
	{  5, "32 Khz (0x0020)"				},
	{  6, "44.1 Khz (0x0040)"			},
	{  7, "48 Khz (0x0080)"				},
	{  8, "88.2 Khz (0x0100)"			},
	{  9, "96 Khz (0x0200)"				},
	{  10, "176.4 Khz (0x0400)"			},
	{  11, "192 Khz (0x0800)"			},
	{  12, "384 Khz (0x1000)"			},
	{  13, "RFU (0x2000)"				},
	{  14, "RFU (0x4000)"				},
	{  15, "RFU (0x8000)"				},
	{ }
};

static void pac_decode_freq(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint16_t value;
	uint16_t mask;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_le16(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Sampling Frequencies: 0x%4.4x", value);

	mask = print_bitfield(8, value, pac_freq_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
								mask);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static const struct bitfield_data pac_duration_table[] = {
	{  0, "7.5 ms (0x01)"				},
	{  1, "10 ms (0x02)"				},
	{  2, "RFU (0x04)"				},
	{  3, "RFU (0x08)"				},
	{  4, "7.5 ms preferred (0x10)"			},
	{  5, "10 ms preferred (0x20)"			},
	{  6, "RFU (0x40)"				},
	{  7, "RFU (0x80)"				},
	{ }
};

static void pac_decode_duration(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;
	uint8_t mask;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Frame Duration: 0x%4.4x", value);

	mask = print_bitfield(8, value, pac_duration_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static const struct bitfield_data pac_channel_table[] = {
	{  0, "1 channel (0x01)"			},
	{  1, "2 channels (0x02)"			},
	{  2, "3 channels (0x04)"			},
	{  3, "4 chanenls (0x08)"			},
	{  4, "5 channels (0x10)"			},
	{  5, "6 channels (0x20)"			},
	{  6, "7 channels (0x40)"			},
	{  7, "8 channels (0x80)"			},
	{ }
};

static void pac_decode_channels(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;
	uint8_t mask;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Audio Channel Count: 0x%2.2x", value);

	mask = print_bitfield(8, value, pac_channel_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static void pac_decode_frame_length(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint16_t min, max;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_le16(&frame, &min)) {
		print_text(COLOR_ERROR, "    min: invalid size");
		goto done;
	}

	if (!l2cap_frame_get_le16(&frame, &max)) {
		print_text(COLOR_ERROR, "    min: invalid size");
		goto done;
	}

	print_field("      Frame Length: %u (0x%4.4x) - %u (0x%4.4x)",
							min, min, max, max);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static void pac_decode_sdu(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Max SDU: %u (0x%2.2x)", value, value);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

struct packet_ltv_decoder pac_cap_table[] = {
	LTV_DEC(0x01, pac_decode_freq),
	LTV_DEC(0x02, pac_decode_duration),
	LTV_DEC(0x03, pac_decode_channels),
	LTV_DEC(0x04, pac_decode_frame_length),
	LTV_DEC(0x05, pac_decode_sdu)
};

static void print_pac(const struct l2cap_frame *frame)
{
	uint8_t num = 0, i;

	if (!l2cap_frame_get_u8((void *)frame, &num)) {
		print_text(COLOR_ERROR, "Number of PAC(s): invalid size");
		goto done;
	}

	print_field("  Number of PAC(s): %u", num);

	for (i = 0; i < num; i++) {
		print_field("  PAC #%u:", i);

		if (!print_ase_codec(frame))
			goto done;

		if (!print_ase_cc(frame, "    Codec Specific Capabilities",
				pac_cap_table, ARRAY_SIZE(pac_cap_table)))
			break;

		if (!print_ase_metadata(frame))
			break;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void pac_read(const struct l2cap_frame *frame)
{
	print_pac(frame);
}

static void pac_notify(const struct l2cap_frame *frame)
{
	print_pac(frame);
}

static bool print_prefer_framing(const struct l2cap_frame *frame)
{
	uint8_t framing;

	if (!l2cap_frame_get_u8((void *)frame, &framing)) {
		print_text(COLOR_ERROR, "    Framing: invalid size");
		return false;
	}

	switch (framing) {
	case 0x00:
		print_field("    Framing: Unframed PDUs supported (0x00)");
		break;
	case 0x01:
		print_field("    Framing: Unframed PDUs not supported (0x01)");
		break;
	default:
		print_field("    Framing: Reserved (0x%2.2x)", framing);
		break;
	}

	return true;
}

static const struct bitfield_data prefer_phy_table[] = {
	{  0, "LE 1M PHY preffered (0x01)"		},
	{  1, "LE 2M PHY preffered (0x02)"		},
	{  2, "LE Codec PHY preffered (0x04)"		},
	{ }
};

static bool print_prefer_phy(const struct l2cap_frame *frame)
{
	uint8_t phy, mask;

	if (!l2cap_frame_get_u8((void *)frame, &phy)) {
		print_text(COLOR_ERROR, "PHY: invalid size");
		return false;
	}

	print_field("    PHY: 0x%2.2x", phy);

	mask = print_bitfield(4, phy, prefer_phy_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);

	return true;
}

static bool print_ase_rtn(const struct l2cap_frame *frame, const char *label)
{
	uint8_t rtn;

	if (!l2cap_frame_get_u8((void *)frame, &rtn)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: %u", label, rtn);

	return true;
}

static bool print_ase_latency(const struct l2cap_frame *frame,
						const char *label)
{
	uint16_t latency;

	if (!l2cap_frame_get_le16((void *)frame, &latency)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: %u", label, latency);

	return true;
}

static bool print_ase_pd(const struct l2cap_frame *frame, const char *label)
{
	uint32_t pd;

	if (!l2cap_frame_get_le24((void *)frame, &pd)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: %u us", label, pd);

	return true;
}

static void ase_decode_freq(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	switch (value) {
	case 0x01:
		print_field("      Sampling Frequency: 8 Khz (0x01)");
		break;
	case 0x02:
		print_field("      Sampling Frequency: 11.25 Khz (0x02)");
		break;
	case 0x03:
		print_field("      Sampling Frequency: 16 Khz (0x03)");
		break;
	case 0x04:
		print_field("      Sampling Frequency: 22.05 Khz (0x04)");
		break;
	case 0x05:
		print_field("      Sampling Frequency: 24 Khz (0x05)");
		break;
	case 0x06:
		print_field("      Sampling Frequency: 32 Khz (0x06)");
		break;
	case 0x07:
		print_field("      Sampling Frequency: 44.1 Khz (0x07)");
		break;
	case 0x08:
		print_field("      Sampling Frequency: 48 Khz (0x08)");
		break;
	case 0x09:
		print_field("      Sampling Frequency: 88.2 Khz (0x09)");
		break;
	case 0x0a:
		print_field("      Sampling Frequency: 96 Khz (0x0a)");
		break;
	case 0x0b:
		print_field("      Sampling Frequency: 176.4 Khz (0x0b)");
		break;
	case 0x0c:
		print_field("      Sampling Frequency: 192 Khz (0x0c)");
		break;
	case 0x0d:
		print_field("      Sampling Frequency: 384 Khz (0x0d)");
		break;
	default:
		print_field("      Sampling Frequency: RFU (0x%2.2x)", value);
		break;
	}

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static void ase_decode_duration(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	switch (value) {
	case 0x00:
		print_field("      Frame Duration: 7.5 ms (0x00)");
		break;
	case 0x01:
		print_field("      Frame Duration: 10 ms (0x01)");
		break;
	default:
		print_field("      Frame Duration: RFU (0x%2.2x)", value);
		break;
	}

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static const struct bitfield_data channel_location_table[] = {
	{  0, "Front Left (0x00000001)"			},
	{  1, "Front Right (0x00000002)"		},
	{  2, "Front Center (0x00000004)"		},
	{  3, "Low Frequency Effects 1 (0x00000008)"	},
	{  4, "Back Left (0x00000010)"			},
	{  5, "Back Right (0x00000020)"			},
	{  6, "Front Left of Center (0x00000040)"	},
	{  7, "Front Right of Center (0x00000080)"	},
	{  8, "Back Center (0x00000100)"		},
	{  9, "Low Frequency Effects 2 (0x00000200)"	},
	{  10, "Side Left (0x00000400)"			},
	{  11, "Side Right (0x00000800)"		},
	{  12, "Top Front Left (0x00001000)"		},
	{  13, "Top Front Right (0x00002000)"		},
	{  14, "Top Front Center (0x00004000)"		},
	{  15, "Top Center (0x00008000)"		},
	{  16, "Top Back Left (0x00010000)"		},
	{  17, "Top Back Right (0x00020000)"		},
	{  18, "Top Side Left (0x00040000)"		},
	{  19, "Top Side Right (0x00080000)"		},
	{  20, "Top Back Center (0x00100000)"		},
	{  21, "Bottom Front Center (0x00200000)"	},
	{  22, "Bottom Front Left (0x00400000)"		},
	{  23, "Bottom Front Right (0x00800000)"	},
	{  24, "Front Left Wide (0x01000000)"		},
	{  25, "Front Right Wide (0x02000000)"		},
	{  26, "Left Surround (0x04000000)"		},
	{  27, "Right Surround (0x08000000)"		},
	{  28, "RFU (0x10000000)"			},
	{  29, "RFU (0x20000000)"			},
	{  30, "RFU (0x40000000)"			},
	{  31, "RFU (0x80000000)"			},
	{ }
};

static void print_location(const struct l2cap_frame *frame)
{
	uint32_t value;
	uint32_t mask;

	if (!l2cap_frame_get_le32((void *)frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("   Location: 0x%8.8x", value);

	mask = print_bitfield(6, value, channel_location_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%8.8x)",
								mask);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void ase_decode_location(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	print_location(&frame);
}

static void ase_decode_frame_length(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint16_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_le16(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Frame Length: %u (0x%4.4x)", value, value);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

static void ase_decode_blocks(const uint8_t *data, uint8_t len)
{
	struct l2cap_frame frame;
	uint8_t value;

	l2cap_frame_init(&frame, 0, 0, 0, 0, 0, 0, data, len);

	if (!l2cap_frame_get_u8(&frame, &value)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Frame Blocks per SDU: %u (0x%2.2x)", value, value);

done:
	if (frame.size)
		print_hex_field("    Data", frame.data, frame.size);
}

struct packet_ltv_decoder ase_cc_table[] = {
	LTV_DEC(0x01, ase_decode_freq),
	LTV_DEC(0x02, ase_decode_duration),
	LTV_DEC(0x03, ase_decode_location),
	LTV_DEC(0x04, ase_decode_frame_length),
	LTV_DEC(0x05, ase_decode_blocks)
};

static void print_ase_config(const struct l2cap_frame *frame)
{
	if (!print_prefer_framing(frame))
		return;

	if (!print_prefer_phy(frame))
		return;

	if (!print_ase_rtn(frame, "    RTN"))
		return;

	if (!print_ase_latency(frame, "    Max Transport Latency"))
		return;

	if (!print_ase_pd(frame, "    Presentation Delay Min"))
		return;

	if (!print_ase_pd(frame, "    Presentation Delay Max"))
		return;

	if (!print_ase_pd(frame, "    Preferred Presentation Delay Min"))
		return;

	if (!print_ase_pd(frame, "    Preferred Presentation Delay Max"))
		return;

	if (!print_ase_codec(frame))
		return;

	print_ase_cc(frame, "    Codec Specific Configuration",
			ase_cc_table, ARRAY_SIZE(ase_cc_table));
}

static bool print_ase_framing(const struct l2cap_frame *frame,
						const char *label)
{
	uint8_t framing;

	if (!l2cap_frame_get_u8((void *)frame, &framing)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	switch (framing) {
	case 0x00:
		print_field("%s: Unframed (0x00)", label);
		break;
	case 0x01:
		print_field("%s: Framed (0x01)", label);
		break;
	default:
		print_field("%s: Reserved (0x%2.2x)", label, framing);
	}

	return true;
}

static const struct bitfield_data phy_table[] = {
	{  0, "LE 1M PHY (0x01)"		},
	{  1, "LE 2M PHY (0x02)"		},
	{  2, "LE Codec PHY (0x04)"		},
	{ }
};

static bool print_ase_phy(const struct l2cap_frame *frame, const char *label)
{
	uint8_t phy, mask;

	if (!l2cap_frame_get_u8((void *)frame, &phy)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: 0x%2.2x", label, phy);

	mask = print_bitfield(4, phy, phy_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%2.2x)",
								mask);

	return true;
}

static bool print_ase_interval(const struct l2cap_frame *frame,
						const char *label)
{
	uint32_t interval;

	if (!l2cap_frame_get_le24((void *)frame, &interval)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: %u usec", label, interval);

	return true;
}

static bool print_ase_sdu(const struct l2cap_frame *frame, const char *label)
{
	uint16_t sdu;

	if (!l2cap_frame_get_le16((void *)frame, &sdu)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	print_field("%s: %u", label, sdu);

	return true;
}

static void print_ase_qos(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
		return;

	if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
		return;

	if (!print_ase_interval(frame, "    SDU Interval"))
		return;

	if (!print_ase_framing(frame, "    Framing"))
		return;

	if (!print_ase_phy(frame, "    PHY"))
		return;

	if (!print_ase_sdu(frame, "    Max SDU"))
		return;

	if (!print_ase_rtn(frame, "    RTN"))
		return;

	if (!print_ase_latency(frame, "    Max Transport Latency"))
		return;

	print_ase_pd(frame, "    Presentation Delay");
}

static void print_ase_metadata_status(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
		return;

	if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
		return;

	print_ase_metadata(frame);
}

static void print_ase_status(const struct l2cap_frame *frame)
{
	uint8_t id, state;

	if (!l2cap_frame_get_u8((void *)frame, &id)) {
		print_text(COLOR_ERROR, "ASE ID: invalid size");
		goto done;
	}

	print_field("    ASE ID: %u", id);

	if (!l2cap_frame_get_u8((void *)frame, &state)) {
		print_text(COLOR_ERROR, "ASE State: invalid size");
		goto done;
	}

	switch (state) {
	/* ASE_State = 0x00 (Idle) */
	case 0x00:
		print_field("    State: Idle (0x00)");
		break;
	/* ASE_State = 0x01 (Codec Configured) */
	case 0x01:
		print_field("    State: Codec Configured (0x01)");
		print_ase_config(frame);
		break;
	/* ASE_State = 0x02 (QoS Configured) */
	case 0x02:
		print_field("    State: QoS Configured (0x02)");
		print_ase_qos(frame);
		break;
	/* ASE_Status = 0x03 (Enabling) */
	case 0x03:
		print_field("    State: Enabling (0x03)");
		print_ase_metadata_status(frame);
		break;
	/* ASE_Status = 0x04 (Streaming) */
	case 0x04:
		print_field("    State: Streaming (0x04)");
		print_ase_metadata_status(frame);
		break;
	/* ASE_Status = 0x05 (Disabling) */
	case 0x05:
		print_field("    State: Disabling (0x05)");
		print_ase_metadata_status(frame);
		break;
	/* ASE_Status = 0x06 (Releasing) */
	case 0x06:
		print_field("    State: Releasing (0x06)");
		break;
	default:
		print_field("    State: Reserved (0x%2.2x)", state);
		break;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void ase_read(const struct l2cap_frame *frame)
{
	print_ase_status(frame);
}

static void ase_notify(const struct l2cap_frame *frame)
{
	print_ase_status(frame);
}

static bool print_ase_target_latency(const struct l2cap_frame *frame)
{
	uint8_t latency;

	if (!l2cap_frame_get_u8((void *)frame, &latency)) {
		print_text(COLOR_ERROR, "    Target Latency: invalid size");
		return false;
	}

	switch (latency) {
	case 0x01:
		print_field("    Target Latency: Low Latency (0x01)");
		break;
	case 0x02:
		print_field("    Target Latency: Balance Latency/Reliability "
								"(0x02)");
		break;
	case 0x03:
		print_field("    Target Latency: High Reliability (0x03)");
		break;
	default:
		print_field("    Target Latency: Reserved (0x%2.2x)", latency);
		break;
	}

	return true;
}

static bool ase_config_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	if (!print_ase_target_latency(frame))
		return false;

	if (!print_ase_phy(frame, "    PHY"))
		return false;

	if (!print_ase_codec(frame))
		return false;

	if (!print_ase_cc(frame, "    Codec Specific Configuration",
				ase_cc_table, ARRAY_SIZE(ase_cc_table)))
		return false;

	return true;
}

static bool ase_qos_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	if (!l2cap_frame_print_u8((void *)frame, "    CIG ID"))
		return false;

	if (!l2cap_frame_print_u8((void *)frame, "    CIS ID"))
		return false;

	if (!print_ase_interval(frame, "    SDU Interval"))
		return false;

	if (!print_ase_framing(frame, "    Framing"))
		return false;

	if (!print_ase_phy(frame, "    PHY"))
		return false;

	if (!print_ase_sdu(frame, "    Max SDU"))
		return false;

	if (!print_ase_rtn(frame, "    RTN"))
		return false;

	if (!print_ase_latency(frame, "    Max Transport Latency"))
		return false;

	if (!print_ase_pd(frame, "    Presentation Delay"))
		return false;

	return true;
}

static bool ase_enable_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	if (!print_ase_metadata(frame))
		return false;

	return true;
}

static bool ase_start_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	return true;
}

static bool ase_disable_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	return true;
}

static bool ase_stop_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	return true;
}

static bool ase_metadata_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	if (!print_ase_metadata(frame))
		return false;

	return true;
}

static bool ase_release_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
		return false;

	return true;
}

#define ASE_CMD(_op, _desc, _func) \
[_op] = { \
	.desc = _desc, \
	.func = _func, \
}

struct ase_cmd {
	const char *desc;
	bool (*func)(const struct l2cap_frame *frame);
} ase_cmd_table[] = {
	/* Opcode = 0x01 (Codec Configuration) */
	ASE_CMD(0x01, "Codec Configuration", ase_config_cmd),
	/* Opcode = 0x02 (QoS Configuration) */
	ASE_CMD(0x02, "QoS Configuration", ase_qos_cmd),
	/* Opcode = 0x03 (Enable) */
	ASE_CMD(0x03, "Enable", ase_enable_cmd),
	/* Opcode = 0x04 (Receiver Start Ready) */
	ASE_CMD(0x04, "Receiver Start Ready", ase_start_cmd),
	/* Opcode = 0x05 (Disable) */
	ASE_CMD(0x05, "Disable", ase_disable_cmd),
	/* Opcode = 0x06 (Receiver Stop Ready) */
	ASE_CMD(0x06, "Receiver Stop Ready", ase_stop_cmd),
	/* Opcode = 0x07 (Update Metadata) */
	ASE_CMD(0x07, "Update Metadata", ase_metadata_cmd),
	/* Opcode = 0x08 (Release) */
	ASE_CMD(0x08, "Release", ase_release_cmd),
};

static struct ase_cmd *ase_get_cmd(uint8_t op)
{
	if (op > ARRAY_SIZE(ase_cmd_table))
		return NULL;

	return &ase_cmd_table[op];
}

static void print_ase_cmd(const struct l2cap_frame *frame)
{
	uint8_t op, num, i;
	struct ase_cmd *cmd;

	if (!l2cap_frame_get_u8((void *)frame, &op)) {
		print_text(COLOR_ERROR, "opcode: invalid size");
		goto done;
	}

	if (!l2cap_frame_get_u8((void *)frame, &num)) {
		print_text(COLOR_ERROR, "num: invalid size");
		goto done;
	}

	cmd = ase_get_cmd(op);
	if (!cmd) {
		print_field("    Opcode: Reserved (0x%2.2x)", op);
		goto done;
	}

	print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
	print_field("    Number of ASE(s): %u", num);

	for (i = 0; i < num && frame->size; i++) {
		print_field("    ASE: #%u", i);

		if (!cmd->func(frame))
			break;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void ase_cp_write(const struct l2cap_frame *frame)
{
	print_ase_cmd(frame);
}

static bool print_ase_cp_rsp_code(const struct l2cap_frame *frame)
{
	uint8_t code;

	if (!l2cap_frame_get_u8((void *)frame, &code)) {
		print_text(COLOR_ERROR, "    ASE Response Code: invalid size");
		return false;
	}

	switch (code) {
	case 0x00:
		print_field("    ASE Response Code: Success (0x00)");
		break;
	case 0x01:
		print_field("    ASE Response Code: Unsupported Opcode (0x01)");
		break;
	case 0x02:
		print_field("    ASE Response Code: Invalid Length (0x02)");
		break;
	case 0x03:
		print_field("    ASE Response Code: Invalid ASE ID (0x03)");
		break;
	case 0x04:
		print_field("    ASE Response Code: Invalid ASE State (0x04)");
		break;
	case 0x05:
		print_field("    ASE Response Code: Invalid ASE Direction "
								"(0x05)");
		break;
	case 0x06:
		print_field("    ASE Response Code: Unsupported Audio "
							"Capabilities (0x06)");
		break;
	case 0x07:
		print_field("    ASE Response Code: Unsupported Configuration "
								"(0x07)");
		break;
	case 0x08:
		print_field("    ASE Response Code: Rejected Configuration "
								"(0x08)");
		break;
	case 0x09:
		print_field("    ASE Response Code: Invalid Configuration "
								"(0x09)");
		break;
	case 0x0a:
		print_field("    ASE Response Code: Unsupported Metadata "
								"(0x0a)");
		break;
	case 0x0b:
		print_field("    ASE Response Code: Rejected Metadata (0x0b)");
		break;
	case 0x0c:
		print_field("    ASE Response Code: Invalid Metadata (0x0c)");
		break;
	case 0x0d:
		print_field("    ASE Response Code: Insufficient Resources "
								"(0x0d)");
		break;
	case 0x0e:
		print_field("    ASE Response Code: Unspecified Error (0x0e)");
		break;
	default:
		print_field("    ASE Response Code: Reserved (0x%2.2x)", code);
		break;
	}

	return true;
}

static bool print_ase_cp_rsp_reason(const struct l2cap_frame *frame)
{
	uint8_t reason;

	if (!l2cap_frame_get_u8((void *)frame, &reason)) {
		print_text(COLOR_ERROR,
				"    ASE Response Reason: invalid size");
		return false;
	}

	switch (reason) {
	case 0x00:
		print_field("    ASE Response Reason: None (0x00)");
		break;
	case 0x01:
		print_field("    ASE Response Reason: ASE ID (0x01)");
		break;
	case 0x02:
		print_field("    ASE Response Reason: Codec Specific "
						"Configuration (0x02)");
		break;
	case 0x03:
		print_field("    ASE Response Reason: SDU Interval (0x03)");
		break;
	case 0x04:
		print_field("    ASE Response Reason: Framing (0x04)");
		break;
	case 0x05:
		print_field("    ASE Response Reason: PHY (0x05)");
		break;
	case 0x06:
		print_field("    ASE Response Reason: Max SDU (0x06)");
		break;
	case 0x07:
		print_field("    ASE Response Reason: RTN (0x07)");
		break;
	case 0x08:
		print_field("    ASE Response Reason: Max Transport Latency "
								"(0x08)");
		break;
	case 0x09:
		print_field("    ASE Response Reason: Presentation Delay "
								"(0x09)");
		break;
	case 0x0a:
		print_field("    ASE Response Reason: Invalid ASE/CIS Mapping "
								"(0x0a)");
		break;
	default:
		print_field("    ASE Response Reason: Reserved (0x%2.2x)",
								reason);
		break;
	}

	return true;
}

static void print_ase_cp_rsp(const struct l2cap_frame *frame)
{
	uint8_t op, num, i;
	struct ase_cmd *cmd;

	if (!l2cap_frame_get_u8((void *)frame, &op)) {
		print_text(COLOR_ERROR, "    opcode: invalid size");
		goto done;
	}

	if (!l2cap_frame_get_u8((void *)frame, &num)) {
		print_text(COLOR_ERROR, "    Number of ASE(s): invalid size");
		goto done;
	}

	cmd = ase_get_cmd(op);
	if (!cmd) {
		print_field("    Opcode: Reserved (0x%2.2x)", op);
		goto done;
	}

	print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
	print_field("    Number of ASE(s): %u", num);

	for (i = 0; i < num && frame->size; i++) {
		print_field("    ASE: #%u", i);

		if (!l2cap_frame_print_u8((void *)frame, "    ASE ID"))
			break;

		if (!print_ase_cp_rsp_code(frame))
			break;

		if (!print_ase_cp_rsp_reason(frame))
			break;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void ase_cp_notify(const struct l2cap_frame *frame)
{
	print_ase_cp_rsp(frame);
}

static void pac_loc_read(const struct l2cap_frame *frame)
{
	print_location(frame);
}

static void pac_loc_notify(const struct l2cap_frame *frame)
{
	print_location(frame);
}

static void print_pac_context(const struct l2cap_frame *frame)
{
	uint16_t snk, src;
	uint16_t mask;

	if (!l2cap_frame_get_le16((void *)frame, &snk)) {
		print_text(COLOR_ERROR, "  sink: invalid size");
		goto done;
	}

	print_field("  Sink Context: 0x%4.4x", snk);

	mask = print_bitfield(4, snk, pac_context_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "  Unknown fields (0x%4.4x)",
								mask);

	if (!l2cap_frame_get_le16((void *)frame, &src)) {
		print_text(COLOR_ERROR, "  source: invalid size");
		goto done;
	}

	print_field("  Source Context: 0x%4.4x", src);

	mask = print_bitfield(4, src, pac_context_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "  Unknown fields (0x%4.4x)",
								mask);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void pac_context_read(const struct l2cap_frame *frame)
{
	print_pac_context(frame);
}

static void pac_context_notify(const struct l2cap_frame *frame)
{
	print_pac_context(frame);
}

static void csip_rank_read(const struct l2cap_frame *frame)
{
	uint8_t rank;

	if (!l2cap_frame_get_u8((void *)frame, &rank)) {
		print_text(COLOR_ERROR, "Rank: invalid size");
		goto done;
	}

	print_field("    Rank: 0x%02x", rank);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void csip_lock_read(const struct l2cap_frame *frame)
{
	uint8_t lock;

	if (!l2cap_frame_get_u8((void *)frame, &lock)) {
		print_text(COLOR_ERROR, "Lock: invalid size");
		goto done;
	}

	switch (lock) {
	case 0x01:
		print_field("    Unlocked (0x%02x)", lock);
		break;
	case 0x02:
		print_field("    Locked (0x%02x)", lock);
		break;
	default:
		print_field("    RFU (0x%02x)", lock);
		break;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void print_csip_size(const struct l2cap_frame *frame)
{
	uint8_t size;

	if (!l2cap_frame_get_u8((void *)frame, &size)) {
		print_text(COLOR_ERROR, "Size: invalid size");
		goto done;
	}
	print_field("    Size: 0x%02x", size);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void csip_size_read(const struct l2cap_frame *frame)
{
	print_csip_size(frame);
}

static void csip_size_notify(const struct l2cap_frame *frame)
{
	print_csip_size(frame);
}

static void csip_sirk_read(const struct l2cap_frame *frame)
{
	if (frame->size)
		print_hex_field("  SIRK", frame->data, frame->size);
}

static void csip_sirk_notify(const struct l2cap_frame *frame)
{
	if (frame->size)
		print_hex_field("  SIRK", frame->data, frame->size);
}

static void print_vcs_state(const struct l2cap_frame *frame)
{
	uint8_t vol_set, mute, chng_ctr;

	if (!l2cap_frame_get_u8((void *)frame, &vol_set)) {
		print_text(COLOR_ERROR, "Volume Settings: invalid size");
		goto done;
	}
	print_field("    Volume Setting: %u", vol_set);

	if (!l2cap_frame_get_u8((void *)frame, &mute)) {
		print_text(COLOR_ERROR, "Mute Filed: invalid size");
		goto done;
	}

	switch (mute) {
	case 0x00:
		print_field("    Not Muted: %u", mute);
		break;
	case 0x01:
		print_field("    Muted: %u", mute);
		break;
	default:
		print_field("    Unknown Mute Value: %u", mute);
		break;
	}

	if (!l2cap_frame_get_u8((void *)frame, &chng_ctr)) {
		print_text(COLOR_ERROR, "Change Counter: invalid size");
		goto done;
	}
	print_field("    Change Counter: %u", chng_ctr);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void vol_state_read(const struct l2cap_frame *frame)
{
	print_vcs_state(frame);
}

static void vol_state_notify(const struct l2cap_frame *frame)
{
	print_vcs_state(frame);
}

static bool vcs_config_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    Change Counter"))
		return false;

	return true;
}

static bool vcs_absolute_cmd(const struct l2cap_frame *frame)
{
	if (!l2cap_frame_print_u8((void *)frame, "    Change Counter"))
		return false;

	if (!l2cap_frame_print_u8((void *)frame, "    Volume Setting"))
		return false;

	return true;
}

#define VCS_CMD(_op, _desc, _func) \
[_op] = { \
	.desc = _desc, \
	.func = _func, \
}

struct vcs_cmd {
	const char *desc;
	bool (*func)(const struct l2cap_frame *frame);
} vcs_cmd_table[] = {
	/* Opcode = 0x00 (Relative Volume Down) */
	VCS_CMD(0x00, "Relative Volume Down", vcs_config_cmd),
	/* Opcode = 0x01 (Relative Volume Up) */
	VCS_CMD(0x01, "Relative Volume Up", vcs_config_cmd),
	/* Opcode = 0x02 (Unmute/Relative Volume Down) */
	VCS_CMD(0x02, "Unmute/Relative Volume Down", vcs_config_cmd),
	/* Opcode = 0x03 (Unmute/Relative Volume Up) */
	VCS_CMD(0x03, "Unmute/Relative Volume Up", vcs_config_cmd),
	/* Opcode = 0x04 (Set Absolute Volume) */
	VCS_CMD(0x04, "Set Absolute Volume", vcs_absolute_cmd),
	/* Opcode = 0x05 (Unmute) */
	VCS_CMD(0x05, "Unmute", vcs_config_cmd),
	/* Opcode = 0x06 (Mute) */
	VCS_CMD(0x06, "Mute", vcs_config_cmd),
};

static struct vcs_cmd *vcs_get_cmd(uint8_t op)
{
	if (op > ARRAY_SIZE(vcs_cmd_table))
		return NULL;

	return &vcs_cmd_table[op];
}

static void print_vcs_cmd(const struct l2cap_frame *frame)
{
	uint8_t op;
	struct vcs_cmd *cmd;

	if (!l2cap_frame_get_u8((void *)frame, &op)) {
		print_text(COLOR_ERROR, "opcode: invalid size");
		goto done;
	}

	cmd = vcs_get_cmd(op);
	if (!cmd) {
		print_field("    Opcode: Reserved (0x%2.2x)", op);
		goto done;
	}

	print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
	if (!cmd->func(frame))
		print_field("    Unknown Opcode");

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void vol_cp_write(const struct l2cap_frame *frame)
{
	print_vcs_cmd(frame);
}

static void print_vcs_flag(const struct l2cap_frame *frame)
{
	uint8_t vol_flag;

	if (!l2cap_frame_get_u8((void *)frame, &vol_flag)) {
		print_text(COLOR_ERROR, "Volume Flag: invalid size");
		goto done;
	}
	print_field("    Volume Falg: %u", vol_flag);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void vol_flag_read(const struct l2cap_frame *frame)
{
	print_vcs_flag(frame);
}

static void vol_flag_notify(const struct l2cap_frame *frame)
{
	print_vcs_flag(frame);
}

static char *name2utf8(const uint8_t *name, uint16_t len)
{
	char utf8_name[HCI_MAX_NAME_LENGTH + 2];
	int i;

	if (g_utf8_validate((const char *) name, len, NULL))
		return g_strndup((char *) name, len);

	len = MIN(len, sizeof(utf8_name) - 1);

	memset(utf8_name, 0, sizeof(utf8_name));
	strncpy(utf8_name, (char *) name, len);

	/* Assume ASCII, and replace all non-ASCII with spaces */
	for (i = 0; utf8_name[i] != '\0'; i++) {
		if (!isascii(utf8_name[i]))
			utf8_name[i] = ' ';
	}

	/* Remove leading and trailing whitespace characters */
	g_strstrip(utf8_name);

	return g_strdup(utf8_name);
}

static void print_mp_name(const struct l2cap_frame *frame)
{
	char *name;

	name = name2utf8((uint8_t *)frame->data, frame->size);

	print_field("  Media Player Name: %s", name);
}

static void mp_name_read(const struct l2cap_frame *frame)
{
	print_mp_name(frame);
}

static void mp_name_notify(const struct l2cap_frame *frame)
{
	print_mp_name(frame);
}

static void print_track_changed(const struct l2cap_frame *frame)
{
	print_field("  Track Changed");
}

static void track_changed_notify(const struct l2cap_frame *frame)
{
	print_track_changed(frame);
}

static void print_track_title(const struct l2cap_frame *frame)
{
	char *name;

	name = name2utf8((uint8_t *)frame->data, frame->size);

	print_field("  Track Title: %s", name);
}

static void track_title_read(const struct l2cap_frame *frame)
{
	print_track_title(frame);
}

static void track_title_notify(const struct l2cap_frame *frame)
{
	print_track_title(frame);
}

static void print_track_duration(const struct l2cap_frame *frame)
{
	int32_t duration;

	if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&duration)) {
		print_text(COLOR_ERROR, "  Track Duration: invalid size");
		goto done;
	}

	print_field("  Track Duration: %u", duration);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void track_duration_read(const struct l2cap_frame *frame)
{
	print_track_duration(frame);
}

static void track_duration_notify(const struct l2cap_frame *frame)
{
	print_track_duration(frame);
}

static void print_track_position(const struct l2cap_frame *frame)
{
	int32_t position;

	if (!l2cap_frame_get_le32((void *)frame, (uint32_t *)&position)) {
		print_text(COLOR_ERROR, "  Track Position: invalid size");
		goto done;
	}

	print_field("  Track Position: %u", position);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void track_position_read(const struct l2cap_frame *frame)
{
	print_track_position(frame);
}

static void track_position_write(const struct l2cap_frame *frame)
{
	print_track_position(frame);
}

static void track_position_notify(const struct l2cap_frame *frame)
{
	print_track_position(frame);
}

static void print_playback_speed(const struct l2cap_frame *frame)
{
	int8_t playback_speed;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playback_speed)) {
		print_text(COLOR_ERROR, "  Playback Speed: invalid size");
		goto done;
	}

	print_field("  Playback Speed: %u", playback_speed);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void playback_speed_read(const struct l2cap_frame *frame)
{
	print_playback_speed(frame);
}

static void playback_speed_write(const struct l2cap_frame *frame)
{
	print_playback_speed(frame);
}

static void playback_speed_notify(const struct l2cap_frame *frame)
{
	print_playback_speed(frame);
}

static void print_seeking_speed(const struct l2cap_frame *frame)
{
	int8_t seeking_speed;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&seeking_speed)) {
		print_text(COLOR_ERROR, "  Seeking Speed: invalid size");
		goto done;
	}

	print_field("  Seeking Speed: %u", seeking_speed);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void seeking_speed_read(const struct l2cap_frame *frame)
{
	print_seeking_speed(frame);
}

static void seeking_speed_notify(const struct l2cap_frame *frame)
{
	print_seeking_speed(frame);
}

static const char *play_order_str(uint8_t order)
{
	switch (order) {
	case 0x01:
		return "Single once";
	case 0x02:
		return "Single repeat";
	case 0x03:
		return "In order once";
	case 0x04:
		return "In order repeat";
	case 0x05:
		return "Oldest once";
	case 0x06:
		return "Oldest repeat";
	case 0x07:
		return "Newest once";
	case 0x08:
		return "Newest repeat";
	case 0x09:
		return "Shuffle once";
	case 0x0A:
		return "Shuffle repeat";
	default:
		return "RFU";
	}
}

static void print_playing_order(const struct l2cap_frame *frame)
{
	int8_t playing_order;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&playing_order)) {
		print_text(COLOR_ERROR, "  Playing Order: invalid size");
		goto done;
	}

	print_field("  Playing Order: %s", play_order_str(playing_order));

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void playing_order_read(const struct l2cap_frame *frame)
{
	print_playing_order(frame);
}

static void playing_order_write(const struct l2cap_frame *frame)
{
	print_playing_order(frame);
}

static void playing_order_notify(const struct l2cap_frame *frame)
{
	print_playing_order(frame);
}

static const struct bitfield_data playing_orders_table[] = {
	{  0, "Single once (0x0001)"	    },
	{  1, "Single repeat (0x0002)"		},
	{  2, "In order once (0x0004)"		},
	{  3, "In Order Repeat (0x0008)"	},
	{  4, "Oldest once (0x0010)"		},
	{  5, "Oldest repeat (0x0020)"		},
	{  6, "Newest once (0x0040)"		},
	{  7, "Newest repeat (0x0080)"	    },
	{  8, "Shuffle once (0x0100)"		},
	{  9, "Shuffle repeat (0x0200)"		},
	{  10, "RFU (0x0400)"			    },
	{  11, "RFU (0x0800)"		        },
	{  12, "RFU (0x1000)"				},
	{  13, "RFU (0x2000)"				},
	{  14, "RFU (0x4000)"				},
	{  15, "RFU (0x8000)"				},
	{ }
};

static void print_playing_orders_supported(const struct l2cap_frame *frame)
{
	uint16_t supported_orders;
	uint16_t mask;

	if (!l2cap_frame_get_le16((void *)frame, &supported_orders)) {
		print_text(COLOR_ERROR,
				"    Supported Playing Orders: invalid size");
		goto done;
	}

	print_field("      Supported Playing Orders: 0x%4.4x",
				supported_orders);

	mask = print_bitfield(8, supported_orders, playing_orders_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
								mask);

done:
	if (frame->size)
		print_hex_field("    Data", frame->data, frame->size);
}

static void playing_orders_supported_read(const struct l2cap_frame *frame)
{
	print_playing_orders_supported(frame);
}

static const char *media_state_str(uint8_t state)
{
	switch (state) {
	case 0x00:
		return "Inactive";
	case 0x01:
		return "Playing";
	case 0x02:
		return "Paused";
	case 0x03:
		return "Seeking";
	default:
		return "RFU";
	}
}

static void print_media_state(const struct l2cap_frame *frame)
{
	int8_t state;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&state)) {
		print_text(COLOR_ERROR, "  Media State: invalid size");
		goto done;
	}

	print_field("  Media State: %s", media_state_str(state));

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void media_state_read(const struct l2cap_frame *frame)
{
	print_media_state(frame);
}

static void media_state_notify(const struct l2cap_frame *frame)
{
	print_media_state(frame);
}

struct media_cp_opcode {
	uint8_t opcode;
	const char *opcode_str;
} media_cp_opcode_table[] = {
	{0x01,	"Play"},
	{0x02,	"Pause"},
	{0x03,	"Fast Rewind"},
	{0x04,	"Fast Forward"},
	{0x05,	"Stop"},
	{0x10,	"Move Relative"},
	{0x20,	"Previous Segment"},
	{0x21,	"Next Segment"},
	{0x22,	"First Segment"},
	{0x23,	"Last Segment"},
	{0x24,	"Goto Segment"},
	{0x30,	"Previous Track"},
	{0x31,	"Next Track"},
	{0x32,	"First Track"},
	{0x33,	"Last Track"},
	{0x34,	"Goto Track"},
	{0x40,	"Previous Group"},
	{0x41,	"Next Group"},
	{0x42,	"First Group"},
	{0x43,	"Last Group"},
	{0x44,	"Goto Group"},
};

static const char *cp_opcode_str(uint8_t opcode)
{
	size_t i;

	for (i = 0; i < ARRAY_SIZE(media_cp_opcode_table); i++) {
		const char *str = media_cp_opcode_table[i].opcode_str;

		if (opcode == media_cp_opcode_table[i].opcode)
			return str;
	}

	return "RFU";
}

static void print_media_cp(const struct l2cap_frame *frame)
{
	int8_t opcode;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&opcode)) {
		print_text(COLOR_ERROR, "  Media Control Point: invalid size");
		goto done;
	}

	print_field("  Media Control Point: %s", cp_opcode_str(opcode));

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void media_cp_write(const struct l2cap_frame *frame)
{
	print_media_cp(frame);
}

static void media_cp_notify(const struct l2cap_frame *frame)
{
	print_media_cp(frame);
}

static const struct bitfield_data supported_opcodes_table[] = {
	{0, "Play (0x00000001)"				},
	{1, "Pause (0x00000002)"			},
	{2, "Fast Rewind	(0x00000004)"	},
	{3, "Fast Forward (0x00000008)"		},
	{4, "Stop (0x00000010)"				},
	{5, "Move Relative (0x00000020)"	},
	{6, "Previous Segment (0x00000040)"	},
	{7, "Next Segment (0x00000080)"		},
	{8, "First Segment (0x00000100)"	},
	{9, "Last Segment (0x00000200)"		},
	{10, "Goto Segment (0x00000400)"	},
	{11, "Previous Track (0x00000800)"	},
	{12, "Next Track (0x00001000)"		},
	{13, "First Track (0x00002000)"		},
	{14, "Last Track (0x00004000)"		},
	{15, "Goto Track (0x00008000)"		},
	{16, "Previous Group (0x00010000)"	},
	{17, "Next Group (0x00020000)"		},
	{18, "First Group (0x00040000)"		},
	{19, "Last Group (0x00080000)"		},
	{20, "Goto Group (0x00100000)"		},
	{21, "RFU (0x00200000)"				},
	{22, "RFU (0x00400000)"				},
	{23, "RFU (0x00800000)"				},
	{24, "RFU (0x01000000)"				},
	{25, "RFU (0x02000000)"				},
	{26, "RFU (0x04000000)"				},
	{27, "RFU (0x08000000)"				},
	{28, "RFU (0x10000000)"				},
	{29, "RFU (0x20000000)"				},
	{30, "RFU (0x40000000)"				},
	{31, "RFU (0x80000000)"				},
	{ }
};

static void print_media_cp_op_supported(const struct l2cap_frame *frame)
{
	uint32_t supported_opcodes;
	uint32_t mask;

	if (!l2cap_frame_get_le32((void *)frame, &supported_opcodes)) {
		print_text(COLOR_ERROR, "    value: invalid size");
		goto done;
	}

	print_field("      Supported Opcodes: 0x%8.8x", supported_opcodes);

	mask = print_bitfield(8, supported_opcodes, supported_opcodes_table);
	if (mask)
		print_text(COLOR_WHITE_BG, "    Unknown fields (0x%4.4x)",
								mask);

done:
	if (frame->size)
		print_hex_field("    Data", frame->data, frame->size);
}

static void media_cp_op_supported_read(const struct l2cap_frame *frame)
{
	print_media_cp_op_supported(frame);
}

static void media_cp_op_supported_notify(const struct l2cap_frame *frame)
{
	print_media_cp_op_supported(frame);
}

static void print_content_control_id(const struct l2cap_frame *frame)
{
	int8_t ccid;

	if (!l2cap_frame_get_u8((void *)frame, (uint8_t *)&ccid)) {
		print_text(COLOR_ERROR, "  Content Control ID: invalid size");
		goto done;
	}

	print_field("  Content Control ID: 0x%2.2x", ccid);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void content_control_id_read(const struct l2cap_frame *frame)
{
	print_content_control_id(frame);
}

static const struct pa_sync_state_decoder {
	uint8_t code;
	char *value;
} pa_sync_state_decoders[] = {
	{ 0x00, "Not synchronized to PA" },
	{ 0x01, "SyncInfo Request" },
	{ 0x02, "Synchronized to PA" },
	{ 0x03, "Failed to synchronize to PA" },
	{ 0x04, "No PAST" },
};

static const struct cp_pa_sync_state_decoder {
	uint8_t code;
	char *value;
} cp_pa_sync_state_decoders[] = {
	{ 0x00, "Do not synchronize to PA" },
	{ 0x01, "Synchronize to PA - PAST available" },
	{ 0x02, "Synchronize to PA - PAST not available" },
};

static const struct big_enc_decoder {
	uint8_t code;
	char *value;
} big_enc_decoders[] = {
	{ 0x00, "Not encrypted" },
	{ 0x01, "Broadcast_Code required" },
	{ 0x02, "Decrypting" },
	{ 0x03, "Bad_Code (incorrect encryption key)" },
};

static bool print_subgroup_lv(const struct l2cap_frame *frame,
		const char *label, struct packet_ltv_decoder *decoder,
		size_t decoder_len)
{
	struct bt_hci_lv_data *lv;

	lv = l2cap_frame_pull((void *)frame, frame, sizeof(*lv));
	if (!lv) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	if (!l2cap_frame_pull((void *)frame, frame, lv->len)) {
		print_text(COLOR_ERROR, "%s: invalid size", label);
		return false;
	}

	packet_print_ltv(label, lv->data, lv->len, decoder, decoder_len);

	return true;
}

static bool print_subgroup_metadata(const char *label,
				const struct l2cap_frame *frame)
{
	return print_subgroup_lv(frame, label, NULL, 0);
}

static void print_bcast_recv_state(const struct l2cap_frame *frame)
{
	uint8_t i;
	uint8_t id;
	uint8_t addr_type;
	uint8_t *addr;
	uint8_t sid;
	uint32_t bid;
	uint8_t pa_sync_state;
	uint8_t enc;
	uint8_t *bad_code;
	uint8_t num_subgroups = 0;
	uint32_t bis_sync_state;

	if (frame->size == 0) {
		print_field("  Empty characteristic");
		goto done;
	}

	if (!l2cap_frame_get_u8((void *)frame, &id)) {
		print_text(COLOR_ERROR, "Source_ID: invalid size");
		goto done;
	}

	print_field("  Source_ID: %u", id);

	if (!l2cap_frame_get_u8((void *)frame, &addr_type)) {
		print_text(COLOR_ERROR, "Source_Address_Type: invalid size");
		goto done;
	}

	print_field("  Source_Address_Type: %u", addr_type);

	addr = l2cap_frame_pull((void *)frame, frame, sizeof(bdaddr_t));
	if (!addr) {
		print_text(COLOR_ERROR, "Source_Address: invalid size");
		goto done;
	}

	print_field("  Source_Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
					addr[5], addr[4],
					addr[3], addr[2],
					addr[1], addr[0]);

	if (!l2cap_frame_get_u8((void *)frame, &sid)) {
		print_text(COLOR_ERROR, "Source_Adv_SID: invalid size");
		goto done;
	}

	print_field("  Source_Adv_SID: %u", sid);

	if (!l2cap_frame_get_le24((void *)frame, &bid)) {
		print_text(COLOR_ERROR, "Broadcast_ID: invalid size");
		goto done;
	}

	print_field("  Broadcast_ID: 0x%06x", bid);

	if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
		print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
		goto done;
	}

	for (i = 0; i < ARRAY_SIZE(pa_sync_state_decoders); i++) {
		const struct pa_sync_state_decoder *decoder;

		decoder = &pa_sync_state_decoders[i];

		if (decoder->code == pa_sync_state) {
			print_field("  PA_Sync_State: %s", decoder->value);
			break;
		}
	}

	if (i == ARRAY_SIZE(pa_sync_state_decoders))
		print_field("  PA_Sync_State: %s", "Invalid value");

	if (!l2cap_frame_get_u8((void *)frame, &enc)) {
		print_text(COLOR_ERROR, "BIG_Encryption: invalid size");
		goto done;
	}

	for (i = 0; i < ARRAY_SIZE(big_enc_decoders); i++) {
		const struct big_enc_decoder *decoder;

		decoder = &big_enc_decoders[i];

		if (decoder->code == enc) {
			print_field("  BIG_Encryption: %s", decoder->value);
			break;
		}
	}

	if (i == ARRAY_SIZE(big_enc_decoders))
		print_field("  BIG_Encryption: %s", "Invalid value");

	if (enc == 0x03) {
		bad_code = l2cap_frame_pull((void *)frame, frame, 16);
		if (!bad_code) {
			print_text(COLOR_ERROR, "Bad_Code: invalid size");
			goto done;
		}

		print_hex_field("  Bad_Code", bad_code, 16);
	}

	if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
		print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
		goto done;
	}

	print_field("  Num_Subgroups: %u", num_subgroups);

	for (i = 0; i < num_subgroups; i++) {
		print_field("  Subgroup #%u:", i);

		if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
			print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
			goto done;
		}

		print_field("    BIS_Sync State: 0x%8.8x", bis_sync_state);

		if (!print_subgroup_metadata("    Metadata", frame))
			goto done;
	}

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void bcast_recv_state_read(const struct l2cap_frame *frame)
{
	print_bcast_recv_state(frame);
}

static void bcast_recv_state_notify(const struct l2cap_frame *frame)
{
	print_bcast_recv_state(frame);
}

#define BCAST_AUDIO_SCAN_CP_CMD(_op, _desc, _func) \
[_op] = { \
	.desc = _desc, \
	.func = _func, \
}

static void bcast_audio_scan_cp_add_src_cmd(const struct l2cap_frame *frame)
{
	uint8_t i;
	uint8_t addr_type;
	uint8_t *addr;
	uint8_t sid;
	uint32_t bid;
	uint8_t pa_sync_state;
	uint16_t pa_interval;
	uint8_t num_subgroups = 0;
	uint32_t bis_sync_state;

	if (!l2cap_frame_get_u8((void *)frame, &addr_type)) {
		print_text(COLOR_ERROR, "Source_Address_Type: invalid size");
		return;
	}

	print_field("    Source_Address_Type: %u", addr_type);

	addr = l2cap_frame_pull((void *)frame, frame, sizeof(bdaddr_t));
	if (!addr) {
		print_text(COLOR_ERROR, "Source_Address: invalid size");
		return;
	}

	print_field("    Source_Address: %2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
					addr[5], addr[4],
					addr[3], addr[2],
					addr[1], addr[0]);

	if (!l2cap_frame_get_u8((void *)frame, &sid)) {
		print_text(COLOR_ERROR, "Source_Adv_SID: invalid size");
		return;
	}

	print_field("    Source_Adv_SID: %u", sid);

	if (!l2cap_frame_get_le24((void *)frame, &bid)) {
		print_text(COLOR_ERROR, "Broadcast_ID: invalid size");
		return;
	}

	print_field("    Broadcast_ID: 0x%06x", bid);

	if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
		print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
		return;
	}

	for (i = 0; i < ARRAY_SIZE(cp_pa_sync_state_decoders); i++) {
		const struct cp_pa_sync_state_decoder *decoder;

		decoder = &cp_pa_sync_state_decoders[i];

		if (decoder->code == pa_sync_state) {
			print_field("    PA_Sync_State: %s", decoder->value);
			break;
		}
	}

	if (i == ARRAY_SIZE(cp_pa_sync_state_decoders))
		print_field("    PA_Sync_State: %s", "Invalid value");

	if (!l2cap_frame_get_le16((void *)frame, &pa_interval)) {
		print_text(COLOR_ERROR, "PA_Interval: invalid size");
		return;
	}

	print_field("    PA_Interval: 0x%04x", pa_interval);

	if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
		print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
		return;
	}

	print_field("    Num_Subgroups: %u", num_subgroups);

	for (i = 0; i < num_subgroups; i++) {
		print_field("    Subgroup #%u:", i);

		if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
			print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
			return;
		}

		print_field("      BIS_Sync State: 0x%8.8x", bis_sync_state);

		if (!print_subgroup_metadata("      Metadata", frame))
			return;
	}
}

static void bcast_audio_scan_cp_mod_src_cmd(const struct l2cap_frame *frame)
{
	uint8_t i;
	uint8_t id;
	uint8_t pa_sync_state;
	uint16_t pa_interval;
	uint8_t num_subgroups = 0;
	uint32_t bis_sync_state;

	if (!l2cap_frame_get_u8((void *)frame, &id)) {
		print_text(COLOR_ERROR, "Source_ID: invalid size");
		return;
	}

	print_field("    Source_ID: %u", id);

	if (!l2cap_frame_get_u8((void *)frame, &pa_sync_state)) {
		print_text(COLOR_ERROR, "PA_Sync_State: invalid size");
		return;
	}

	for (i = 0; i < ARRAY_SIZE(cp_pa_sync_state_decoders); i++) {
		const struct cp_pa_sync_state_decoder *decoder;

		decoder = &cp_pa_sync_state_decoders[i];

		if (decoder->code == pa_sync_state) {
			print_field("    PA_Sync_State: %s", decoder->value);
			break;
		}
	}

	if (i == ARRAY_SIZE(cp_pa_sync_state_decoders))
		print_field("    PA_Sync_State: %s", "Invalid value");

	if (!l2cap_frame_get_le16((void *)frame, &pa_interval)) {
		print_text(COLOR_ERROR, "PA_Interval: invalid size");
		return;
	}

	print_field("    PA_Interval: 0x%04x", pa_interval);

	if (!l2cap_frame_get_u8((void *)frame, &num_subgroups)) {
		print_text(COLOR_ERROR, "Num_Subgroups: invalid size");
		return;
	}

	print_field("    Num_Subgroups: %u", num_subgroups);

	for (i = 0; i < num_subgroups; i++) {
		print_field("    Subgroup #%u:", i);

		if (!l2cap_frame_get_le32((void *)frame, &bis_sync_state)) {
			print_text(COLOR_ERROR, "BIS_Sync State: invalid size");
			return;
		}

		print_field("      BIS_Sync State: 0x%8.8x", bis_sync_state);

		if (!print_subgroup_metadata("      Metadata", frame))
			return;
	}
}

static void bcast_audio_scan_cp_set_bcode_cmd(const struct l2cap_frame *frame)
{
	uint8_t id;
	uint8_t *bcast_code;

	if (!l2cap_frame_get_u8((void *)frame, &id)) {
		print_text(COLOR_ERROR, "Source_ID: invalid size");
		return;
	}

	print_field("    Source_ID: %u", id);

	bcast_code = l2cap_frame_pull((void *)frame, frame, 16);
	if (!bcast_code) {
		print_text(COLOR_ERROR, "Broadcast_Code: invalid size");
		return;
	}

	print_hex_field("    Broadcast_Code", bcast_code, 16);

}

static void bcast_audio_scan_cp_remove_src_cmd(const struct l2cap_frame *frame)
{
	uint8_t id;

	if (!l2cap_frame_get_u8((void *)frame, &id)) {
		print_text(COLOR_ERROR, "Source_ID: invalid size");
		return;
	}

	print_field("    Source_ID: %u", id);
}

struct bcast_audio_scan_cp_cmd {
	const char *desc;
	void (*func)(const struct l2cap_frame *frame);
} bcast_audio_scan_cp_cmd_table[] = {
	/* Opcode = 0x00 (Remote Scan Stopped) */
	BCAST_AUDIO_SCAN_CP_CMD(0x00, "Remote Scan Stopped", NULL),
	/* Opcode = 0x01 (Remote Scan Started) */
	BCAST_AUDIO_SCAN_CP_CMD(0x01, "Remote Scan Started", NULL),
	/* Opcode = 0x02 (Add Source) */
	BCAST_AUDIO_SCAN_CP_CMD(0x02, "Add Source",
					bcast_audio_scan_cp_add_src_cmd),
	/* Opcode = 0x03 (Modify Source) */
	BCAST_AUDIO_SCAN_CP_CMD(0x03, "Modify Source",
					bcast_audio_scan_cp_mod_src_cmd),
	/* Opcode = 0x04 (Set Broadcast_Code) */
	BCAST_AUDIO_SCAN_CP_CMD(0x04, "Set Broadcast_Code",
					bcast_audio_scan_cp_set_bcode_cmd),
	/* Opcode = 0x05 (Remove Source) */
	BCAST_AUDIO_SCAN_CP_CMD(0x05, "Remove Source",
					bcast_audio_scan_cp_remove_src_cmd),
};

static struct bcast_audio_scan_cp_cmd *bcast_audio_scan_cp_get_cmd(uint8_t op)
{
	if (op > ARRAY_SIZE(bcast_audio_scan_cp_cmd_table))
		return NULL;

	return &bcast_audio_scan_cp_cmd_table[op];
}

static void print_bcast_audio_scan_cp_cmd(const struct l2cap_frame *frame)
{
	uint8_t op;
	struct bcast_audio_scan_cp_cmd *cmd;

	if (!l2cap_frame_get_u8((void *)frame, &op)) {
		print_text(COLOR_ERROR, "Opcode: invalid size");
		goto done;
	}

	cmd = bcast_audio_scan_cp_get_cmd(op);
	if (!cmd) {
		print_field("    Opcode: Reserved (0x%2.2x)", op);
		goto done;
	}

	print_field("    Opcode: %s (0x%2.2x)", cmd->desc, op);
	if (cmd->func)
		cmd->func(frame);

done:
	if (frame->size)
		print_hex_field("  Data", frame->data, frame->size);
}

static void bcast_audio_scan_cp_write(const struct l2cap_frame *frame)
{
	print_bcast_audio_scan_cp_cmd(frame);
}

#define GATT_HANDLER(_uuid, _read, _write, _notify) \
{ \
	.uuid = { \
		.type = BT_UUID16, \
		.value.u16 = _uuid, \
	}, \
	.read = _read, \
	.write = _write, \
	.notify = _notify \
}

struct gatt_handler {
	bt_uuid_t uuid;
	void (*read)(const struct l2cap_frame *frame);
	void (*write)(const struct l2cap_frame *frame);
	void (*notify)(const struct l2cap_frame *frame);
} gatt_handlers[] = {
	GATT_HANDLER(0x2800, pri_svc_read, NULL, NULL),
	GATT_HANDLER(0x2801, sec_svc_read, NULL, NULL),
	GATT_HANDLER(0x2803, chrc_read, NULL, NULL),
	GATT_HANDLER(0x2902, ccc_read, ccc_write, NULL),
	GATT_HANDLER(0x2bc4, ase_read, NULL, ase_notify),
	GATT_HANDLER(0x2bc5, ase_read, NULL, ase_notify),
	GATT_HANDLER(0x2bc6, NULL, ase_cp_write, ase_cp_notify),
	GATT_HANDLER(0x2bc9, pac_read, NULL, pac_notify),
	GATT_HANDLER(0x2bca, pac_loc_read, NULL, pac_loc_notify),
	GATT_HANDLER(0x2bcb, pac_read, NULL, pac_notify),
	GATT_HANDLER(0x2bcc, pac_loc_read, NULL, pac_loc_notify),
	GATT_HANDLER(0x2bcd, pac_context_read, NULL, pac_context_notify),
	GATT_HANDLER(0x2bce, pac_context_read, NULL, pac_context_notify),
	GATT_HANDLER(0x2b7d, vol_state_read, NULL, vol_state_notify),
	GATT_HANDLER(0x2b7e, NULL, vol_cp_write, NULL),
	GATT_HANDLER(0x2b7f, vol_flag_read, NULL, vol_flag_notify),

	GATT_HANDLER(0x2b84, csip_sirk_read, NULL, csip_sirk_notify),
	GATT_HANDLER(0x2b85, csip_size_read, NULL, csip_size_notify),
	GATT_HANDLER(0x2b86, csip_lock_read, NULL, NULL),
	GATT_HANDLER(0x2b87, csip_rank_read, NULL, NULL),

	GATT_HANDLER(0x2b93, mp_name_read, NULL, mp_name_notify),
	GATT_HANDLER(0x2b96, NULL, NULL, track_changed_notify),
	GATT_HANDLER(0x2b97, track_title_read, NULL, track_title_notify),
	GATT_HANDLER(0x2b98, track_duration_read, NULL, track_duration_notify),
	GATT_HANDLER(0x2b99, track_position_read, track_position_write,
					track_position_notify),
	GATT_HANDLER(0x2b9a, playback_speed_read, playback_speed_write,
					playback_speed_notify),
	GATT_HANDLER(0x2b9b, seeking_speed_read, NULL, seeking_speed_notify),
	GATT_HANDLER(0x2ba1, playing_order_read, playing_order_write,
					playing_order_notify),
	GATT_HANDLER(0x2ba2, playing_orders_supported_read, NULL, NULL),
	GATT_HANDLER(0x2ba3, media_state_read, NULL, media_state_notify),
	GATT_HANDLER(0x2ba4, NULL, media_cp_write, media_cp_notify),
	GATT_HANDLER(0x2ba5, media_cp_op_supported_read, NULL,
					media_cp_op_supported_notify),
	GATT_HANDLER(0x2bba, content_control_id_read, NULL, NULL),

	GATT_HANDLER(0x2bc7, NULL, bcast_audio_scan_cp_write, NULL),
	GATT_HANDLER(0x2bc8, bcast_recv_state_read, NULL,
					bcast_recv_state_notify),
};

static struct gatt_handler *get_handler_uuid(const bt_uuid_t *uuid)
{
	size_t i;

	if (!uuid)
		return NULL;

	for (i = 0; i < ARRAY_SIZE(gatt_handlers); i++) {
		struct gatt_handler *handler = &gatt_handlers[i];

		if (!bt_uuid_cmp(&handler->uuid, uuid))
			return handler;
	}

	return NULL;
}

static struct gatt_handler *get_handler(struct gatt_db_attribute *attr)
{
	return get_handler_uuid(gatt_db_attribute_get_type(attr));
}

static void att_exchange_mtu_req(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_exchange_mtu_req *pdu = frame->data;

	print_field("Client RX MTU: %d", le16_to_cpu(pdu->mtu));
}

static void att_exchange_mtu_rsp(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_exchange_mtu_rsp *pdu = frame->data;

	print_field("Server RX MTU: %d", le16_to_cpu(pdu->mtu));
}

static void att_find_info_req(const struct l2cap_frame *frame)
{
	print_handle_range("Handle range", frame->data);
}

static const char *att_format_str(uint8_t format)
{
	switch (format) {
	case 0x01:
		return "UUID-16";
	case 0x02:
		return "UUID-128";
	default:
		return "unknown";
	}
}

static struct gatt_db_attribute *insert_desc(const struct l2cap_frame *frame,
						uint16_t handle,
						bt_uuid_t *uuid, bool rsp)
{
	struct gatt_db *db;

	db = get_db(frame, rsp);
	if (!db)
		return NULL;

	return gatt_db_insert_descriptor(db, handle, uuid, 0, NULL, NULL, NULL);
}

static void att_find_info_rsp_16(const struct l2cap_frame *frame)
{
	while (frame->size >= 4) {
		uint16_t handle;
		uint16_t u16;
		bt_uuid_t uuid;

		if (!l2cap_frame_get_le16((void *)frame, &handle)) {
			print_text(COLOR_ERROR, "    Handle: invalid size");
			return;
		}

		if (!l2cap_frame_get_le16((void *)frame, &u16)) {
			print_text(COLOR_ERROR, "    UUID: invalid size");
			return;
		}

		print_field("Handle: 0x%4.4x", handle);
		print_uuid("UUID", &u16, 2);

		bt_uuid16_create(&uuid, u16);

		insert_desc(frame, handle, &uuid, true);
	}
}

static void att_find_info_rsp_128(const struct l2cap_frame *frame)
{
	while (frame->size >= 18) {
		uint16_t handle;
		bt_uuid_t uuid;

		if (!l2cap_frame_get_le16((void *)frame, &handle)) {
			print_text(COLOR_ERROR, "    Handle: invalid size");
			return;
		}

		if (frame->size < 16) {
			print_text(COLOR_ERROR, "    UUID: invalid size");
			return;
		}

		print_field("Handle: 0x%4.4x", handle);
		print_uuid("UUID", frame->data, 16);

		bt_uuid_from_data(&uuid, frame->data, 16);

		if (!l2cap_frame_pull((void *)frame, frame, 16))
			return;

		insert_desc(frame, handle, &uuid, true);
	}
}

static void att_find_info_rsp(const struct l2cap_frame *frame)
{
	uint8_t format;

	if (!l2cap_frame_get_u8((void *)frame, &format)) {
		print_text(COLOR_ERROR, "    Format: invalid size");
		goto done;
	}

	print_field("Format: %s (0x%2.2x)", att_format_str(format), format);

	switch (format) {
	case 0x01:
		att_find_info_rsp_16(frame);
		break;
	case 0x02:
		att_find_info_rsp_128(frame);
		break;
	}

done:
	if (frame->size)
		packet_hexdump(frame->data, frame->size);
}

static void att_find_by_type_val_req(const struct l2cap_frame *frame)
{
	uint16_t type;

	print_handle_range("Handle range", frame->data);

	type = get_le16(frame->data + 4);
	print_attribute_info(type, frame->data + 6, frame->size - 6);
}

static void att_find_by_type_val_rsp(const struct l2cap_frame *frame)
{
	const uint8_t *ptr = frame->data;
	uint16_t len = frame->size;

	while (len >= 4) {
		print_handle_range("Handle range", ptr);
		ptr += 4;
		len -= 4;
	}

	packet_hexdump(ptr, len);
}

static struct gatt_db_attribute *get_attribute(const struct l2cap_frame *frame,
						uint16_t handle, bool rsp)
{
	struct gatt_db *db;

	db = get_db(frame, rsp);
	if (!db)
		return NULL;

	return gatt_db_get_attribute(db, handle);
}

static void queue_read(const struct l2cap_frame *frame, bt_uuid_t *uuid,
					uint16_t handle)
{
	struct packet_conn_data *conn;
	struct att_conn_data *data;
	struct att_read *read;
	struct gatt_db_attribute *attr = NULL;
	struct gatt_handler *handler;

	if (handle) {
		attr = get_attribute(frame, handle, false);
		if (!attr)
			return;
	}

	handler = attr ? get_handler(attr) : get_handler_uuid(uuid);
	if (!handler || !handler->read)
		return;

	conn = packet_get_conn_data(frame->handle);
	data = att_get_conn_data(conn);
	if (!data)
		return;

	if (!data->reads)
		data->reads = queue_new();

	read = new0(struct att_read, 1);
	read->attr = attr;
	read->in = frame->in;
	read->chan = frame->chan;
	read->func = handler->read;

	queue_push_tail(data->reads, read);
}

static void att_read_type_req(const struct l2cap_frame *frame)
{
	bt_uuid_t uuid;

	print_handle_range("Handle range", frame->data);
	print_uuid("Attribute type", frame->data + 4, frame->size - 4);

	if (bt_uuid_from_data(&uuid, frame->data + 4, frame->size - 4))
		return;

	queue_read(frame, &uuid, 0x0000);
}

static void att_read_type_rsp(const struct l2cap_frame *frame)
{
	uint8_t len;

	if (!l2cap_frame_get_u8((void *)frame, &len)) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	print_field("Attribute data length: %d", len);
	print_data_list("Attribute data list", len, frame);
}

static void print_handle(const struct l2cap_frame *frame, uint16_t handle,
								bool rsp)
{
	struct gatt_db_attribute *attr;

	attr = get_attribute(frame, handle, rsp);
	if (!attr) {
		print_field("Handle: 0x%4.4x", handle);
		return;
	}

	print_attribute(attr);
}

static void att_read_req(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_read_req *pdu = frame->data;
	uint16_t handle;

	l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));

	handle = le16_to_cpu(pdu->handle);
	print_handle(frame, handle, false);

	queue_read(frame, NULL, handle);
}

static void att_read_rsp(const struct l2cap_frame *frame)
{
	struct att_read *read;

	read = att_get_read(frame);
	if (!read)
		return;

	print_attribute(read->attr);
	print_hex_field("Value", frame->data, frame->size);

	read->func(frame);

	free(read);
}

static void att_read_blob_req(const struct l2cap_frame *frame)
{
	print_handle(frame, get_le16(frame->data), false);
	print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
}

static void att_read_blob_rsp(const struct l2cap_frame *frame)
{
	packet_hexdump(frame->data, frame->size);
}

static void att_read_multiple_req(const struct l2cap_frame *frame)
{
	int i, count;

	count = frame->size / 2;

	for (i = 0; i < count; i++)
		print_handle(frame, get_le16(frame->data + (i * 2)), false);
}

static void att_read_group_type_req(const struct l2cap_frame *frame)
{
	bt_uuid_t uuid;

	print_handle_range("Handle range", frame->data);
	print_uuid("Attribute group type", frame->data + 4, frame->size - 4);

	if (bt_uuid_from_data(&uuid, frame->data + 4, frame->size - 4))
		return;

	queue_read(frame, &uuid, 0x0000);
}

static void print_group_list(const char *label, uint8_t length,
					const struct l2cap_frame *frame)
{
	struct att_read *read;
	uint8_t count;

	if (length == 0)
		return;

	read = att_get_read(frame);

	count = frame->size / length;

	print_field("%s: %u entr%s", label, count, count == 1 ? "y" : "ies");

	while (frame->size >= length) {
		print_handle_range("Handle range", frame->data);
		print_uuid("UUID", frame->data + 4, length - 4);

		if (read) {
			struct l2cap_frame f;

			l2cap_frame_clone_size(&f, frame, length);

			read->func(&f);
		}

		if (!l2cap_frame_pull((void *)frame, frame, length))
			break;
	}

	packet_hexdump(frame->data, frame->size);
	free(read);
}

static void att_read_group_type_rsp(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_read_group_type_rsp *pdu = frame->data;

	l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));

	print_field("Attribute data length: %d", pdu->length);
	print_group_list("Attribute group list", pdu->length, frame);
}

static void print_write(const struct l2cap_frame *frame, uint16_t handle,
							size_t len)
{
	struct gatt_db_attribute *attr;
	struct gatt_handler *handler;

	print_handle(frame, handle, false);

	if (len > frame->size) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	print_hex_field("  Data", frame->data, len);

	attr = get_attribute(frame, handle, false);
	if (!attr)
		return;

	handler = get_handler(attr);
	if (!handler || !handler->write)
		return;

	handler->write(frame);
}

static void att_write_req(const struct l2cap_frame *frame)
{
	uint16_t handle;

	if (!l2cap_frame_get_le16((void *)frame, &handle)) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	print_write(frame, handle, frame->size);
}

static void att_write_rsp(const struct l2cap_frame *frame)
{
}

static void att_prepare_write_req(const struct l2cap_frame *frame)
{
	print_handle(frame, get_le16(frame->data), false);
	print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
	print_hex_field("  Data", frame->data + 4, frame->size - 4);
}

static void att_prepare_write_rsp(const struct l2cap_frame *frame)
{
	print_handle(frame, get_le16(frame->data), true);
	print_field("Offset: 0x%4.4x", get_le16(frame->data + 2));
	print_hex_field("  Data", frame->data + 4, frame->size - 4);
}

static void att_execute_write_req(const struct l2cap_frame *frame)
{
	uint8_t flags = *(uint8_t *) frame->data;
	const char *flags_str;

	switch (flags) {
	case 0x00:
		flags_str = "Cancel all prepared writes";
		break;
	case 0x01:
		flags_str = "Immediately write all pending values";
		break;
	default:
		flags_str = "Unknown";
		break;
	}

	print_field("Flags: %s (0x%02x)", flags_str, flags);
}

static void print_notify(const struct l2cap_frame *frame, uint16_t handle,
								size_t len)
{
	struct gatt_db_attribute *attr;
	struct gatt_handler *handler;
	struct l2cap_frame clone;

	print_handle(frame, handle, true);
	print_hex_field("  Data", frame->data, len);

	if (len > frame->size) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	attr = get_attribute(frame, handle, true);
	if (!attr)
		return;

	handler = get_handler(attr);
	if (!handler)
		return;

	/* Use a clone if the callback is not expected to parse the whole
	 * frame.
	 */
	if (len != frame->size) {
		l2cap_frame_clone(&clone, frame);
		clone.size = len;
		frame = &clone;
	}

	handler->notify(frame);
}

static void att_handle_value_notify(const struct l2cap_frame *frame)
{
	uint16_t handle;
	const struct bt_l2cap_att_handle_value_notify *pdu = frame->data;

	l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));

	handle = le16_to_cpu(pdu->handle);
	print_notify(frame, handle, frame->size);
}

static void att_handle_value_ind(const struct l2cap_frame *frame)
{
	const struct bt_l2cap_att_handle_value_ind *pdu = frame->data;

	l2cap_frame_pull((void *)frame, frame, sizeof(*pdu));

	print_notify(frame, le16_to_cpu(pdu->handle), frame->size);
}

static void att_handle_value_conf(const struct l2cap_frame *frame)
{
}

static void att_multiple_vl_rsp(const struct l2cap_frame *frame)
{
	struct l2cap_frame *f = (void *) frame;

	while (frame->size) {
		uint16_t handle;
		uint16_t len;

		if (!l2cap_frame_get_le16(f, &handle))
			return;

		if (!l2cap_frame_get_le16(f, &len))
			return;

		print_field("Length: 0x%4.4x", len);

		print_notify(frame, handle, len);

		l2cap_frame_pull(f, f, len);
	}
}

static void att_write_command(const struct l2cap_frame *frame)
{
	uint16_t handle;

	if (!l2cap_frame_get_le16((void *)frame, &handle)) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	print_write(frame, handle, frame->size);
}

static void att_signed_write_command(const struct l2cap_frame *frame)
{
	uint16_t handle;

	if (!l2cap_frame_get_le16((void *)frame, &handle)) {
		print_text(COLOR_ERROR, "invalid size");
		return;
	}

	print_write(frame, handle, frame->size - 12);
	print_hex_field("  Signature", frame->data + frame->size - 12, 12);
}

struct att_opcode_data {
	uint8_t opcode;
	const char *str;
	void (*func) (const struct l2cap_frame *frame);
	uint8_t size;
	bool fixed;
};

static const struct att_opcode_data att_opcode_table[] = {
	{ 0x01, "Error Response",
			att_error_response, 4, true },
	{ 0x02, "Exchange MTU Request",
			att_exchange_mtu_req, 2, true },
	{ 0x03, "Exchange MTU Response",
			att_exchange_mtu_rsp, 2, true },
	{ 0x04, "Find Information Request",
			att_find_info_req, 4, true },
	{ 0x05, "Find Information Response",
			att_find_info_rsp, 5, false },
	{ 0x06, "Find By Type Value Request",
			att_find_by_type_val_req, 6, false },
	{ 0x07, "Find By Type Value Response",
			att_find_by_type_val_rsp, 4, false },
	{ 0x08, "Read By Type Request",
			att_read_type_req, 6, false },
	{ 0x09, "Read By Type Response",
			att_read_type_rsp, 3, false },
	{ 0x0a, "Read Request",
			att_read_req, 2, true },
	{ 0x0b, "Read Response",
			att_read_rsp, 0, false },
	{ 0x0c, "Read Blob Request",
			att_read_blob_req, 4, true },
	{ 0x0d, "Read Blob Response",
			att_read_blob_rsp, 0, false },
	{ 0x0e, "Read Multiple Request",
			att_read_multiple_req, 4, false },
	{ 0x0f, "Read Multiple Response"	},
	{ 0x10, "Read By Group Type Request",
			att_read_group_type_req, 6, false },
	{ 0x11, "Read By Group Type Response",
			att_read_group_type_rsp, 4, false },
	{ 0x12, "Write Request"	,
			att_write_req, 2, false	},
	{ 0x13, "Write Response",
			att_write_rsp, 0, true	},
	{ 0x16, "Prepare Write Request",
			att_prepare_write_req, 4, false },
	{ 0x17, "Prepare Write Response",
			att_prepare_write_rsp, 4, false },
	{ 0x18, "Execute Write Request",
			att_execute_write_req, 1, true },
	{ 0x19, "Execute Write Response"	},
	{ 0x1b, "Handle Value Notification",
			att_handle_value_notify, 2, false },
	{ 0x1d, "Handle Value Indication",
			att_handle_value_ind, 2, false },
	{ 0x1e, "Handle Value Confirmation",
			att_handle_value_conf, 0, true },
	{ 0x20, "Read Multiple Request Variable Length",
			att_read_multiple_req, 4, false },
	{ 0x21, "Read Multiple Response Variable Length",
			att_multiple_vl_rsp, 4, false },
	{ 0x23, "Handle Multiple Value Notification",
			att_multiple_vl_rsp, 4, false },
	{ 0x52, "Write Command",
			att_write_command, 2, false },
	{ 0xd2, "Signed Write Command", att_signed_write_command, 14, false },
	{ }
};

static const char *att_opcode_to_str(uint8_t opcode)
{
	int i;

	for (i = 0; att_opcode_table[i].str; i++) {
		if (att_opcode_table[i].opcode == opcode)
			return att_opcode_table[i].str;
	}

	return "Unknown";
}

void att_packet(uint16_t index, bool in, uint16_t handle, uint16_t cid,
					const void *data, uint16_t size)
{
	struct l2cap_frame frame;
	uint8_t opcode = *((const uint8_t *) data);
	const struct att_opcode_data *opcode_data = NULL;
	const char *opcode_color, *opcode_str;
	int i;

	if (size < 1) {
		print_text(COLOR_ERROR, "malformed attribute packet");
		packet_hexdump(data, size);
		return;
	}

	for (i = 0; att_opcode_table[i].str; i++) {
		if (att_opcode_table[i].opcode == opcode) {
			opcode_data = &att_opcode_table[i];
			break;
		}
	}

	if (opcode_data) {
		if (opcode_data->func) {
			if (in)
				opcode_color = COLOR_MAGENTA;
			else
				opcode_color = COLOR_BLUE;
		} else
			opcode_color = COLOR_WHITE_BG;
		opcode_str = opcode_data->str;
	} else {
		opcode_color = COLOR_WHITE_BG;
		opcode_str = "Unknown";
	}

	print_indent(6, opcode_color, "ATT: ", opcode_str, COLOR_OFF,
				" (0x%2.2x) len %d", opcode, size - 1);

	if (!opcode_data || !opcode_data->func) {
		packet_hexdump(data + 1, size - 1);
		return;
	}

	if (opcode_data->fixed) {
		if (size - 1 != opcode_data->size) {
			print_text(COLOR_ERROR, "invalid size");
			packet_hexdump(data + 1, size - 1);
			return;
		}
	} else {
		if (size - 1 < opcode_data->size) {
			print_text(COLOR_ERROR, "too short packet");
			packet_hexdump(data + 1, size - 1);
			return;
		}
	}

	l2cap_frame_init(&frame, index, in, handle, 0, cid, 0,
						data + 1, size - 1);
	opcode_data->func(&frame);
}