1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
|
/*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2011 Intel Corporation. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <getopt.h>
#include <stdbool.h>
#include <wordexp.h>
#include <ctype.h>
#include "lib/bluetooth.h"
#include "lib/hci.h"
#include "lib/hci_lib.h"
#include "lib/sdp.h"
#include "lib/sdp_lib.h"
#include "src/uuid-helper.h"
#include "lib/mgmt.h"
#include "src/shared/mainloop.h"
#include "src/shared/io.h"
#include "src/shared/util.h"
#include "src/shared/mgmt.h"
#include "src/shared/shell.h"
#define SCAN_TYPE_BREDR (1 << BDADDR_BREDR)
#define SCAN_TYPE_LE ((1 << BDADDR_LE_PUBLIC) | (1 << BDADDR_LE_RANDOM))
#define SCAN_TYPE_DUAL (SCAN_TYPE_BREDR | SCAN_TYPE_LE)
static struct mgmt *mgmt = NULL;
static uint16_t mgmt_index = MGMT_INDEX_NONE;
static bool discovery = false;
static bool resolve_names = true;
static struct {
uint16_t index;
uint16_t req;
struct mgmt_addr_info addr;
} prompt = {
.index = MGMT_INDEX_NONE,
};
static int pending_index = 0;
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define PROMPT_ON COLOR_BLUE "[mgmt]" COLOR_OFF "# "
static void set_index(const char *arg)
{
if (!arg || !strcmp(arg, "none") || !strcmp(arg, "any") ||
!strcmp(arg, "all"))
mgmt_index = MGMT_INDEX_NONE;
else if(strlen(arg) > 3 && !strncasecmp(arg, "hci", 3))
mgmt_index = atoi(&arg[3]);
else
mgmt_index = atoi(arg);
}
static void update_prompt(uint16_t index)
{
char str[32];
if (index == MGMT_INDEX_NONE)
snprintf(str, sizeof(str), "%s# ",
COLOR_BLUE "[mgmt]" COLOR_OFF);
else
snprintf(str, sizeof(str),
COLOR_BLUE "[hci%u]" COLOR_OFF "# ", index);
bt_shell_set_prompt(str);
}
#define print(fmt, arg...) do { \
bt_shell_printf(fmt "\n", ## arg); \
} while (0)
#define error(fmt, arg...) do { \
bt_shell_printf(COLOR_RED fmt "\n" COLOR_OFF, ## arg); \
} while (0)
static size_t hex2bin(const char *hexstr, uint8_t *buf, size_t buflen)
{
size_t i, len;
len = MIN((strlen(hexstr) / 2), buflen);
memset(buf, 0, len);
for (i = 0; i < len; i++)
sscanf(hexstr + (i * 2), "%02hhX", &buf[i]);
return len;
}
static size_t bin2hex(const uint8_t *buf, size_t buflen, char *str,
size_t strlen)
{
size_t i;
for (i = 0; i < buflen && i < (strlen / 2); i++)
sprintf(str + (i * 2), "%02x", buf[i]);
return i;
}
static void print_eir(const uint8_t *eir, uint16_t eir_len)
{
uint16_t parsed = 0;
char str[33];
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
switch (eir[1]) {
case 0x01:
print("Flags: 0x%02x", eir[2]);
break;
case 0x0d:
print("Class of Device: 0x%02x%02x%02x",
eir[4], eir[3], eir[2]);
break;
case 0x0e:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Hash C-192: %s", str);
break;
case 0x0f:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Rand R-192: %s", str);
break;
case 0x1b:
ba2str((bdaddr_t *) (eir + 2), str);
print("LE Device Address: %s (%s)", str,
eir[8] ? "random" : "public");
break;
case 0x1c:
print("LE Role: 0x%02x", eir[2]);
break;
case 0x1d:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Hash C-256: %s", str);
break;
case 0x1e:
bin2hex(eir + 2, 16, str, sizeof(str));
print("SSP Rand R-256: %s", str);
break;
case 0x22:
bin2hex(eir + 2, 16, str, sizeof(str));
print("LE SC Confirmation Value: %s", str);
break;
case 0x23:
bin2hex(eir + 2, 16, str, sizeof(str));
print("LE SC Random Value: %s", str);
break;
default:
print("Type %u: %u byte%s", eir[1], field_len - 1,
(field_len - 1) == 1 ? "" : "s");
break;
}
eir += field_len + 1;
}
}
static bool load_identity(const char *path, struct mgmt_irk_info *irk)
{
char *addr, *key;
unsigned int type;
int n;
FILE *fp;
fp = fopen(path, "r");
if (!fp) {
error("Failed to open identity file: %s", strerror(errno));
return false;
}
n = fscanf(fp, "%m[0-9a-f:] (type %u) %m[0-9a-f]", &addr, &type, &key);
fclose(fp);
if (n != 3)
return false;
str2ba(addr, &irk->addr.bdaddr);
hex2bin(key, irk->val, sizeof(irk->val));
free(addr);
free(key);
switch (type) {
case 0:
irk->addr.type = BDADDR_LE_PUBLIC;
break;
case 1:
irk->addr.type = BDADDR_LE_RANDOM;
break;
default:
error("Invalid address type %u", type);
return false;
}
return true;
}
static void controller_error(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_controller_error *ev = param;
if (len < sizeof(*ev)) {
error("Too short (%u bytes) controller error event", len);
return;
}
print("hci%u error 0x%02x", index, ev->error_code);
}
static void index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u added", index);
}
static void index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u removed", index);
}
static void unconf_index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u added (unconfigured)", index);
}
static void unconf_index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
print("hci%u removed (unconfigured)", index);
}
static void ext_index_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_ext_index_added *ev = param;
print("hci%u added (type %u bus %u)", index, ev->type, ev->bus);
}
static void ext_index_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_ext_index_removed *ev = param;
print("hci%u removed (type %u bus %u)", index, ev->type, ev->bus);
}
static const char *options_str[] = {
"external",
"public-address",
};
static const char *options2str(uint32_t options)
{
static char str[256];
unsigned i;
int off;
off = 0;
str[0] = '\0';
for (i = 0; i < NELEM(options_str); i++) {
if ((options & (1 << i)) != 0)
off += snprintf(str + off, sizeof(str) - off, "%s ",
options_str[i]);
}
return str;
}
static void new_config_options(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const uint32_t *ev = param;
if (len < sizeof(*ev)) {
error("Too short new_config_options event (%u)", len);
return;
}
print("hci%u new_config_options: %s", index, options2str(get_le32(ev)));
}
static const char *settings_str[] = {
"powered",
"connectable",
"fast-connectable",
"discoverable",
"bondable",
"link-security",
"ssp",
"br/edr",
"hs",
"le",
"advertising",
"secure-conn",
"debug-keys",
"privacy",
"configuration",
"static-addr",
};
static const char *settings2str(uint32_t settings)
{
static char str[256];
unsigned i;
int off;
off = 0;
str[0] = '\0';
for (i = 0; i < NELEM(settings_str); i++) {
if ((settings & (1 << i)) != 0)
off += snprintf(str + off, sizeof(str) - off, "%s ",
settings_str[i]);
}
return str;
}
static void new_settings(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const uint32_t *ev = param;
if (len < sizeof(*ev)) {
error("Too short new_settings event (%u)", len);
return;
}
print("hci%u new_settings: %s", index, settings2str(get_le32(ev)));
}
static void discovering(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_discovering *ev = param;
if (len < sizeof(*ev)) {
error("Too short (%u bytes) discovering event", len);
return;
}
print("hci%u type %u discovering %s", index, ev->type,
ev->discovering ? "on" : "off");
if (ev->discovering == 0 && discovery)
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void new_link_key(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_new_link_key *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid new_link_key length (%u bytes)", len);
return;
}
ba2str(&ev->key.addr.bdaddr, addr);
print("hci%u new_link_key %s type 0x%02x pin_len %d store_hint %u",
index, addr, ev->key.type, ev->key.pin_len, ev->store_hint);
}
static const char *typestr(uint8_t type)
{
static const char *str[] = { "BR/EDR", "LE Public", "LE Random" };
if (type <= BDADDR_LE_RANDOM)
return str[type];
return "(unknown)";
}
static void connected(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_connected *ev = param;
uint16_t eir_len;
char addr[18];
if (len < sizeof(*ev)) {
error("Invalid connected event length (%u bytes)", len);
return;
}
eir_len = get_le16(&ev->eir_len);
if (len != sizeof(*ev) + eir_len) {
error("Invalid connected event length (%u != eir_len %u)",
len, eir_len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s connected eir_len %u", index, addr,
typestr(ev->addr.type), eir_len);
}
static void disconnected(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_disconnected *ev = param;
char addr[18];
uint8_t reason;
if (len < sizeof(struct mgmt_addr_info)) {
error("Invalid disconnected event length (%u bytes)", len);
return;
}
if (len < sizeof(*ev))
reason = MGMT_DEV_DISCONN_UNKNOWN;
else
reason = ev->reason;
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s disconnected with reason %u",
index, addr, typestr(ev->addr.type), reason);
}
static void conn_failed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_connect_failed *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid connect_failed event length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s type %s connect failed (status 0x%02x, %s)",
index, addr, typestr(ev->addr.type), ev->status,
mgmt_errstr(ev->status));
}
static void auth_failed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_auth_failed *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid auth_failed event length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s auth failed with status 0x%02x (%s)",
index, addr, ev->status, mgmt_errstr(ev->status));
}
static void class_of_dev_changed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_class_of_dev_changed *ev = param;
if (len != sizeof(*ev)) {
error("Invalid class_of_dev_changed length (%u bytes)", len);
return;
}
print("hci%u class of device changed: 0x%02x%02x%02x", index,
ev->dev_class[2], ev->dev_class[1], ev->dev_class[0]);
}
static void local_name_changed(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_local_name_changed *ev = param;
if (len != sizeof(*ev)) {
error("Invalid local_name_changed length (%u bytes)", len);
return;
}
print("hci%u name changed: %s", index, ev->name);
}
static void confirm_name_rsp(uint8_t status, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_rp_confirm_name *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("confirm_name failed with status 0x%02x (%s)", status,
mgmt_errstr(status));
return;
}
if (len != sizeof(*rp)) {
error("confirm_name rsp length %u instead of %zu",
len, sizeof(*rp));
return;
}
ba2str(&rp->addr.bdaddr, addr);
if (status != 0)
error("confirm_name for %s failed: 0x%02x (%s)",
addr, status, mgmt_errstr(status));
else
print("confirm_name succeeded for %s", addr);
}
static char *eir_get_name(const uint8_t *eir, uint16_t eir_len)
{
uint8_t parsed = 0;
if (eir_len < 2)
return NULL;
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
/* Check for short of complete name */
if (eir[1] == 0x09 || eir[1] == 0x08)
return strndup((char *) &eir[2], field_len - 1);
eir += field_len + 1;
}
return NULL;
}
static unsigned int eir_get_flags(const uint8_t *eir, uint16_t eir_len)
{
uint8_t parsed = 0;
if (eir_len < 2)
return 0;
while (parsed < eir_len - 1) {
uint8_t field_len = eir[0];
if (field_len == 0)
break;
parsed += field_len + 1;
if (parsed > eir_len)
break;
/* Check for flags */
if (eir[1] == 0x01)
return eir[2];
eir += field_len + 1;
}
return 0;
}
static void device_found(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_device_found *ev = param;
struct mgmt *mgmt = user_data;
uint16_t eir_len;
uint32_t flags;
if (len < sizeof(*ev)) {
error("Too short device_found length (%u bytes)", len);
return;
}
flags = btohl(ev->flags);
eir_len = get_le16(&ev->eir_len);
if (len != sizeof(*ev) + eir_len) {
error("dev_found: expected %zu bytes, got %u bytes",
sizeof(*ev) + eir_len, len);
return;
}
if (discovery) {
char addr[18], *name;
ba2str(&ev->addr.bdaddr, addr);
print("hci%u dev_found: %s type %s rssi %d "
"flags 0x%04x ", index, addr,
typestr(ev->addr.type), ev->rssi, flags);
if (ev->addr.type != BDADDR_BREDR)
print("AD flags 0x%02x ",
eir_get_flags(ev->eir, eir_len));
name = eir_get_name(ev->eir, eir_len);
if (name)
print("name %s", name);
else
print("eir_len %u", eir_len);
free(name);
}
if (discovery && (flags & MGMT_DEV_FOUND_CONFIRM_NAME)) {
struct mgmt_cp_confirm_name cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, &ev->addr, sizeof(cp.addr));
if (resolve_names)
cp.name_known = 0;
else
cp.name_known = 1;
mgmt_reply(mgmt, MGMT_OP_CONFIRM_NAME, index, sizeof(cp), &cp,
confirm_name_rsp, NULL, NULL);
}
}
static void pin_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("PIN Code reply failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("PIN Reply successful");
}
static int mgmt_pin_reply(uint16_t index, const struct mgmt_addr_info *addr,
const char *pin, size_t len)
{
struct mgmt_cp_pin_code_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(cp.addr));
cp.pin_len = len;
memcpy(cp.pin_code, pin, len);
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_REPLY, index,
sizeof(cp), &cp, pin_rsp, NULL, NULL);
}
static void pin_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("PIN Neg reply failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("PIN Negative Reply successful");
}
static int mgmt_pin_neg_reply(uint16_t index, const struct mgmt_addr_info *addr)
{
struct mgmt_cp_pin_code_neg_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(cp.addr));
return mgmt_reply(mgmt, MGMT_OP_PIN_CODE_NEG_REPLY, index,
sizeof(cp), &cp, pin_neg_rsp, NULL, NULL);
}
static void confirm_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("User Confirm reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Confirm Reply successful");
}
static int mgmt_confirm_reply(uint16_t index, const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_confirm_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_REPLY, index,
sizeof(cp), &cp, confirm_rsp, NULL, NULL);
}
static void confirm_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Confirm Neg reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Confirm Negative Reply successful");
}
static int mgmt_confirm_neg_reply(uint16_t index,
const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_confirm_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_CONFIRM_NEG_REPLY, index,
sizeof(cp), &cp, confirm_neg_rsp, NULL, NULL);
}
static void passkey_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("User Passkey reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Passkey Reply successful");
}
static int mgmt_passkey_reply(uint16_t index, const struct mgmt_addr_info *addr,
uint32_t passkey)
{
struct mgmt_cp_user_passkey_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
put_le32(passkey, &cp.passkey);
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_REPLY, index,
sizeof(cp), &cp, passkey_rsp, NULL, NULL);
}
static void passkey_neg_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Passkey Neg reply failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("User Passkey Negative Reply successful");
}
static int mgmt_passkey_neg_reply(uint16_t index,
const struct mgmt_addr_info *addr)
{
struct mgmt_cp_user_passkey_reply cp;
memset(&cp, 0, sizeof(cp));
memcpy(&cp.addr, addr, sizeof(*addr));
return mgmt_reply(mgmt, MGMT_OP_USER_PASSKEY_NEG_REPLY, index,
sizeof(cp), &cp, passkey_neg_rsp, NULL, NULL);
}
static void prompt_input(const char *input, void *user_data)
{
size_t len;
len = strlen(input);
switch (prompt.req) {
case MGMT_EV_PIN_CODE_REQUEST:
if (len)
mgmt_pin_reply(prompt.index, &prompt.addr, input, len);
else
mgmt_pin_neg_reply(prompt.index, &prompt.addr);
break;
case MGMT_EV_USER_PASSKEY_REQUEST:
if (strlen(input) > 0)
mgmt_passkey_reply(prompt.index, &prompt.addr,
atoi(input));
else
mgmt_passkey_neg_reply(prompt.index,
&prompt.addr);
break;
case MGMT_EV_USER_CONFIRM_REQUEST:
if (input[0] == 'y' || input[0] == 'Y')
mgmt_confirm_reply(prompt.index, &prompt.addr);
else
mgmt_confirm_neg_reply(prompt.index, &prompt.addr);
break;
}
}
static void ask(uint16_t index, uint16_t req, const struct mgmt_addr_info *addr,
const char *fmt, ...)
{
char msg[256];
va_list ap;
int off;
prompt.index = index;
prompt.req = req;
memcpy(&prompt.addr, addr, sizeof(*addr));
va_start(ap, fmt);
off = vsnprintf(msg, sizeof(msg), fmt, ap);
va_end(ap);
snprintf(msg + off, sizeof(msg) - off, " %s ",
COLOR_BOLDGRAY ">>" COLOR_OFF);
bt_shell_prompt_input("", msg, prompt_input, NULL);
}
static void request_pin(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_pin_code_request *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid pin_code request length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s request PIN", index, addr);
ask(index, MGMT_EV_PIN_CODE_REQUEST, &ev->addr,
"PIN Request (press enter to reject)");
}
static void user_confirm(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_user_confirm_request *ev = param;
uint32_t val;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid user_confirm request length (%u)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
val = get_le32(&ev->value);
print("hci%u %s User Confirm %06u hint %u", index, addr,
val, ev->confirm_hint);
if (ev->confirm_hint)
ask(index, MGMT_EV_USER_CONFIRM_REQUEST, &ev->addr,
"Accept pairing with %s (yes/no)", addr);
else
ask(index, MGMT_EV_USER_CONFIRM_REQUEST, &ev->addr,
"Confirm value %06u for %s (yes/no)", val, addr);
}
static void request_passkey(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_user_passkey_request *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid passkey request length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s request passkey", index, addr);
ask(index, MGMT_EV_USER_PASSKEY_REQUEST, &ev->addr,
"Passkey Request (press enter to reject)");
}
static void passkey_notify(uint16_t index, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_ev_passkey_notify *ev = param;
char addr[18];
if (len != sizeof(*ev)) {
error("Invalid passkey request length (%u bytes)", len);
return;
}
ba2str(&ev->addr.bdaddr, addr);
print("hci%u %s request passkey", index, addr);
print("Passkey Notify: %06u (entered %u)", get_le32(&ev->passkey),
ev->entered);
}
static void local_oob_data_updated(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_local_oob_data_updated *ev = param;
uint16_t eir_len;
if (len < sizeof(*ev)) {
error("Too small (%u bytes) local_oob_updated event", len);
return;
}
eir_len = le16_to_cpu(ev->eir_len);
if (len != sizeof(*ev) + eir_len) {
error("local_oob_updated: expected %zu bytes, got %u bytes",
sizeof(*ev) + eir_len, len);
return;
}
print("hci%u oob data updated: type %u len %u", index,
ev->type, eir_len);
}
static void advertising_added(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_advertising_added *ev = param;
if (len < sizeof(*ev)) {
error("Too small (%u bytes) advertising_added event", len);
return;
}
print("hci%u advertising_added: instance %u", index, ev->instance);
}
static void advertising_removed(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_ev_advertising_removed *ev = param;
if (len < sizeof(*ev)) {
error("Too small (%u bytes) advertising_removed event", len);
return;
}
print("hci%u advertising_removed: instance %u", index, ev->instance);
}
static void version_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_version *rp = param;
if (status != 0) {
error("Reading mgmt version failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small version reply (%u bytes)", len);
goto done;
}
print("MGMT Version %u, revision %u", rp->version,
get_le16(&rp->revision));
done:
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_version(int argc, char **argv)
{
if (mgmt_send(mgmt, MGMT_OP_READ_VERSION, MGMT_INDEX_NONE,
0, NULL, version_rsp, NULL, NULL) == 0) {
error("Unable to send read_version cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void commands_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_commands *rp = param;
uint16_t num_commands, num_events;
const uint16_t *opcode;
size_t expected_len;
int i;
if (status != 0) {
error("Read Supported Commands failed: status 0x%02x (%s)",
status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small commands reply (%u bytes)", len);
goto done;
}
num_commands = get_le16(&rp->num_commands);
num_events = get_le16(&rp->num_events);
expected_len = sizeof(*rp) + num_commands * sizeof(uint16_t) +
num_events * sizeof(uint16_t);
if (len < expected_len) {
error("Too small commands reply (%u != %zu)",
len, expected_len);
goto done;
}
opcode = rp->opcodes;
print("%u commands:", num_commands);
for (i = 0; i < num_commands; i++) {
uint16_t op = get_le16(opcode++);
print("\t%s (0x%04x)", mgmt_opstr(op), op);
}
print("%u events:", num_events);
for (i = 0; i < num_events; i++) {
uint16_t ev = get_le16(opcode++);
print("\t%s (0x%04x)", mgmt_evstr(ev), ev);
}
done:
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_commands(int argc,
char **argv)
{
if (mgmt_send(mgmt, MGMT_OP_READ_COMMANDS, MGMT_INDEX_NONE,
0, NULL, commands_rsp, NULL, NULL) == 0) {
error("Unable to send read_commands cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void config_info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_config_info *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint32_t supported_options, missing_options;
if (status != 0) {
error("Reading hci%u config failed with status 0x%02x (%s)",
index, status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small info reply (%u bytes)", len);
goto done;
}
print("hci%u:\tUnconfigured controller", index);
print("\tmanufacturer %u", le16_to_cpu(rp->manufacturer));
supported_options = le32_to_cpu(rp->supported_options);
print("\tsupported options: %s", options2str(supported_options));
missing_options = le32_to_cpu(rp->missing_options);
print("\tmissing options: %s", options2str(missing_options));
done:
pending_index--;
if (pending_index > 0)
return;
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void unconf_index_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_unconf_index_list *rp = param;
uint16_t count;
unsigned int i;
if (status != 0) {
error("Reading index list failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small index list reply (%u bytes)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count = le16_to_cpu(rp->num_controllers);
if (len < sizeof(*rp) + count * sizeof(uint16_t)) {
error("Index count (%u) doesn't match reply length (%u)",
count, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Unconfigured index list with %u item%s",
count, count != 1 ? "s" : "");
for (i = 0; i < count; i++) {
uint16_t index = le16_to_cpu(rp->index[i]);
if (!mgmt_send(mgmt, MGMT_OP_READ_CONFIG_INFO, index, 0, NULL,
config_info_rsp, UINT_TO_PTR(index), NULL)) {
error("Unable to send read_config_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
pending_index++;
}
if (!count)
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_config(int argc, char **argv)
{
if (mgmt_index == MGMT_INDEX_NONE) {
if (!mgmt_send(mgmt, MGMT_OP_READ_UNCONF_INDEX_LIST,
MGMT_INDEX_NONE, 0, NULL,
unconf_index_rsp, mgmt, NULL)) {
error("Unable to send unconf_index_list cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
return;
}
if (!mgmt_send(mgmt, MGMT_OP_READ_CONFIG_INFO, mgmt_index, 0, NULL,
config_info_rsp, UINT_TO_PTR(index), NULL)) {
error("Unable to send read_config_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void config_options_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_config_info *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint32_t supported_options, missing_options;
if (status != 0) {
error("Reading hci%u config failed with status 0x%02x (%s)",
index, status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small info reply (%u bytes)", len);
goto done;
}
print("hci%u:\tConfiguration options", index);
supported_options = le32_to_cpu(rp->supported_options);
print("\tsupported options: %s", options2str(supported_options));
missing_options = le32_to_cpu(rp->missing_options);
print("\tmissing options: %s", options2str(missing_options));
done:
pending_index--;
if (pending_index > 0)
return;
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_info *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint32_t supported_settings, current_settings;
char addr[18];
if (status != 0) {
error("Reading hci%u info failed with status 0x%02x (%s)",
index, status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small info reply (%u bytes)", len);
goto done;
}
print("hci%u:\tPrimary controller", index);
ba2str(&rp->bdaddr, addr);
print("\taddr %s version %u manufacturer %u class 0x%02x%02x%02x",
addr, rp->version, le16_to_cpu(rp->manufacturer),
rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
supported_settings = le32_to_cpu(rp->supported_settings);
print("\tsupported settings: %s", settings2str(supported_settings));
current_settings = le32_to_cpu(rp->current_settings);
print("\tcurrent settings: %s", settings2str(current_settings));
print("\tname %s", rp->name);
print("\tshort name %s", rp->short_name);
if (supported_settings & MGMT_SETTING_CONFIGURATION) {
if (!mgmt_send(mgmt, MGMT_OP_READ_CONFIG_INFO,
index, 0, NULL, config_options_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read_config cmd");
goto done;
}
return;
}
done:
pending_index--;
if (pending_index > 0)
return;
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void ext_info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_ext_info *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint32_t supported_settings, current_settings;
char addr[18];
if (status != 0) {
error("Reading hci%u info failed with status 0x%02x (%s)",
index, status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small info reply (%u bytes)", len);
goto done;
}
print("hci%u:\tPrimary controller", index);
ba2str(&rp->bdaddr, addr);
print("\taddr %s version %u manufacturer %u",
addr, rp->version, le16_to_cpu(rp->manufacturer));
supported_settings = le32_to_cpu(rp->supported_settings);
print("\tsupported settings: %s", settings2str(supported_settings));
current_settings = le32_to_cpu(rp->current_settings);
print("\tcurrent settings: %s", settings2str(current_settings));
if (supported_settings & MGMT_SETTING_CONFIGURATION) {
if (!mgmt_send(mgmt, MGMT_OP_READ_CONFIG_INFO,
index, 0, NULL, config_options_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read_config cmd");
goto done;
}
return;
}
done:
pending_index--;
if (pending_index > 0)
return;
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void index_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_index_list *rp = param;
struct mgmt *mgmt = user_data;
uint16_t count;
unsigned int i;
if (status != 0) {
error("Reading index list failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small index list reply (%u bytes)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count = le16_to_cpu(rp->num_controllers);
if (len < sizeof(*rp) + count * sizeof(uint16_t)) {
error("Index count (%u) doesn't match reply length (%u)",
count, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Index list with %u item%s", count, count != 1 ? "s" : "");
for (i = 0; i < count; i++) {
uint16_t index = le16_to_cpu(rp->index[i]);
if (!mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
info_rsp, UINT_TO_PTR(index), NULL)) {
error("Unable to send read_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
pending_index++;
}
if (!count)
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_info(int argc, char **argv)
{
if (mgmt_index == MGMT_INDEX_NONE) {
if (!mgmt_send(mgmt, MGMT_OP_READ_INDEX_LIST,
MGMT_INDEX_NONE, 0, NULL,
index_rsp, mgmt, NULL)) {
error("Unable to send index_list cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
return;
}
if (!mgmt_send(mgmt, MGMT_OP_READ_INFO, mgmt_index, 0, NULL, info_rsp,
UINT_TO_PTR(mgmt_index), NULL)) {
error("Unable to send read_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void ext_index_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_ext_index_list *rp = param;
uint16_t count, index_filter = PTR_TO_UINT(user_data);
unsigned int i;
if (status != 0) {
error("Reading ext index list failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small ext index list reply (%u bytes)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count = get_le16(&rp->num_controllers);
if (len < sizeof(*rp) + count * (sizeof(uint16_t) + sizeof(uint8_t))) {
error("Index count (%u) doesn't match reply length (%u)",
count, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Extended index list with %u item%s",
count, count != 1 ? "s" : "");
for (i = 0; i < count; i++) {
uint16_t index = le16_to_cpu(rp->entry[i].index);
char *busstr = hci_bustostr(rp->entry[i].bus);
if (index_filter != MGMT_INDEX_NONE && index_filter != index)
continue;
switch (rp->entry[i].type) {
case 0x00:
print("Primary controller (hci%u,%s)", index, busstr);
if (!mgmt_send(mgmt, MGMT_OP_READ_EXT_INFO,
index, 0, NULL, ext_info_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read_ext_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
pending_index++;
break;
case 0x01:
print("Unconfigured controller (hci%u,%s)",
index, busstr);
if (!mgmt_send(mgmt, MGMT_OP_READ_CONFIG_INFO,
index, 0, NULL, config_info_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read_config cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
pending_index++;
break;
case 0x02:
print("AMP controller (hci%u,%s)", index, busstr);
break;
default:
print("Type %u controller (hci%u,%s)",
rp->entry[i].type, index, busstr);
break;
}
}
print("");
if (!count)
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_extinfo(
int argc, char **argv)
{
if (!mgmt_send(mgmt, MGMT_OP_READ_EXT_INDEX_LIST,
MGMT_INDEX_NONE, 0, NULL,
ext_index_rsp, UINT_TO_PTR(index), NULL)) {
error("Unable to send ext_index_list cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void auto_power_enable_rsp(uint8_t status, uint16_t len,
const void *param, void *user_data)
{
uint16_t index = PTR_TO_UINT(user_data);
print("Successfully enabled controller with index %u", index);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void auto_power_info_rsp(uint8_t status, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_rp_read_info *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint32_t supported_settings, current_settings, missing_settings;
uint8_t val = 0x01;
if (status) {
error("Reading info failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
supported_settings = le32_to_cpu(rp->supported_settings);
current_settings = le32_to_cpu(rp->current_settings);
missing_settings = current_settings ^ supported_settings;
if (missing_settings & MGMT_SETTING_BREDR)
mgmt_send(mgmt, MGMT_OP_SET_BREDR, index, sizeof(val), &val,
NULL, NULL, NULL);
if (missing_settings & MGMT_SETTING_SSP)
mgmt_send(mgmt, MGMT_OP_SET_SSP, index, sizeof(val), &val,
NULL, NULL, NULL);
if (missing_settings & MGMT_SETTING_LE)
mgmt_send(mgmt, MGMT_OP_SET_LE, index, sizeof(val), &val,
NULL, NULL, NULL);
if (missing_settings & MGMT_SETTING_SECURE_CONN)
mgmt_send(mgmt, MGMT_OP_SET_SECURE_CONN, index,
sizeof(val), &val,
NULL, NULL, NULL);
if (missing_settings & MGMT_SETTING_BONDABLE)
mgmt_send(mgmt, MGMT_OP_SET_BONDABLE, index, sizeof(val), &val,
NULL, NULL, NULL);
if (current_settings & MGMT_SETTING_POWERED)
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
if (!mgmt_send(mgmt, MGMT_OP_SET_POWERED, index, sizeof(val), &val,
auto_power_enable_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send set powerd cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void auto_power_index_evt(uint16_t index, uint16_t len,
const void *param, void *user_data)
{
uint16_t index_filter = PTR_TO_UINT(user_data);
if (index != index_filter)
return;
print("New controller with index %u", index);
if (!mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
auto_power_info_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void auto_power_index_rsp(uint8_t status, uint16_t len,
const void *param, void *user_data)
{
const struct mgmt_rp_read_index_list *rp = param;
uint16_t index = PTR_TO_UINT(user_data);
uint16_t i, count;
bool found = false;
if (status) {
error("Reading index list failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count = le16_to_cpu(rp->num_controllers);
for (i = 0; i < count; i++) {
if (le16_to_cpu(rp->index[i]) == index)
found = true;
}
if (!found) {
print("Waiting for index %u to appear", index);
mgmt_register(mgmt, MGMT_EV_INDEX_ADDED, index,
auto_power_index_evt,
UINT_TO_PTR(index), NULL);
return;
}
print("Found controller with index %u", index);
if (!mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
auto_power_info_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_auto_power(int argc, char **argv)
{
int index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (!mgmt_send(mgmt, MGMT_OP_READ_INDEX_LIST, MGMT_INDEX_NONE, 0, NULL,
auto_power_index_rsp,
UINT_TO_PTR(index), NULL)) {
error("Unable to send read index list cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
/* Wrapper to get the index and opcode to the response callback */
struct command_data {
uint16_t id;
uint16_t op;
void (*callback) (uint16_t id, uint16_t op, uint8_t status,
uint16_t len, const void *param);
};
static void cmd_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
struct command_data *data = user_data;
data->callback(data->op, data->id, status, len, param);
}
static unsigned int send_cmd(struct mgmt *mgmt, uint16_t op, uint16_t id,
uint16_t len, const void *param,
void (*cb)(uint16_t id, uint16_t op,
uint8_t status, uint16_t len,
const void *param))
{
struct command_data *data;
unsigned int send_id;
data = new0(struct command_data, 1);
if (!data)
return 0;
data->id = id;
data->op = op;
data->callback = cb;
send_id = mgmt_send(mgmt, op, id, len, param, cmd_rsp, data, free);
if (send_id == 0)
free(data);
return send_id;
}
static void setting_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
const void *param)
{
const uint32_t *rp = param;
if (status != 0) {
error("%s for hci%u failed with status 0x%02x (%s)",
mgmt_opstr(op), id, status, mgmt_errstr(status));
goto done;
}
if (len < sizeof(*rp)) {
error("Too small %s response (%u bytes)",
mgmt_opstr(op), len);
goto done;
}
print("hci%u %s complete, settings: %s", id, mgmt_opstr(op),
settings2str(get_le32(rp)));
done:
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static bool parse_setting(int argc, char **argv, uint8_t *val)
{
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
*val = 1;
else if (strcasecmp(argv[1], "off") == 0)
*val = 0;
else
*val = atoi(argv[1]);
return true;
}
static void cmd_setting(uint16_t op, int argc, char **argv)
{
int index;
uint8_t val;
if (parse_setting(argc, argv, &val) == false)
return bt_shell_noninteractive_quit(EXIT_FAILURE);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (send_cmd(mgmt, op, index, sizeof(val), &val, setting_rsp) == 0) {
error("Unable to send %s cmd", mgmt_opstr(op));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_power(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_POWERED, argc, argv);
}
static void cmd_discov(int argc, char **argv)
{
struct mgmt_cp_set_discoverable cp;
uint16_t index;
memset(&cp, 0, sizeof(cp));
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
cp.val = 1;
else if (strcasecmp(argv[1], "off") == 0)
cp.val = 0;
else if (strcasecmp(argv[1], "limited") == 0)
cp.val = 2;
else
cp.val = atoi(argv[1]);
if (argc > 2)
cp.timeout = htobs(atoi(argv[2]));
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (send_cmd(mgmt, MGMT_OP_SET_DISCOVERABLE, index, sizeof(cp), &cp,
setting_rsp) == 0) {
error("Unable to send set_discoverable cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_connectable(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_CONNECTABLE, argc, argv);
}
static void cmd_fast_conn(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_FAST_CONNECTABLE, argc, argv);
}
static void cmd_bondable(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_BONDABLE, argc, argv);
}
static void cmd_linksec(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_LINK_SECURITY, argc, argv);
}
static void cmd_ssp(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_SSP, argc, argv);
}
static void cmd_sc(int argc, char **argv)
{
uint8_t val;
uint16_t index;
if (strcasecmp(argv[1], "on") == 0 || strcasecmp(argv[1], "yes") == 0)
val = 1;
else if (strcasecmp(argv[1], "off") == 0)
val = 0;
else if (strcasecmp(argv[1], "only") == 0)
val = 2;
else
val = atoi(argv[1]);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (send_cmd(mgmt, MGMT_OP_SET_SECURE_CONN, index,
sizeof(val), &val, setting_rsp) == 0) {
error("Unable to send set_secure_conn cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_hs(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_HS, argc, argv);
}
static void cmd_le(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_LE, argc, argv);
}
static void cmd_advertising(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_ADVERTISING, argc, argv);
}
static void cmd_bredr(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_BREDR, argc, argv);
}
static void cmd_privacy(int argc, char **argv)
{
struct mgmt_cp_set_privacy cp;
uint16_t index;
if (parse_setting(argc, argv, &cp.privacy) == false)
return bt_shell_noninteractive_quit(EXIT_FAILURE);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (argc > 2) {
if (hex2bin(argv[2], cp.irk,
sizeof(cp.irk)) != sizeof(cp.irk)) {
error("Invalid key format");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
} else {
int fd;
fd = open("/dev/urandom", O_RDONLY);
if (fd < 0) {
error("open(/dev/urandom): %s", strerror(errno));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (read(fd, cp.irk, sizeof(cp.irk)) != sizeof(cp.irk)) {
error("Reading from urandom failed");
close(fd);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
close(fd);
}
if (send_cmd(mgmt, MGMT_OP_SET_PRIVACY, index, sizeof(cp), &cp,
setting_rsp) == 0) {
error("Unable to send Set Privacy command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void class_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
const void *param)
{
const struct mgmt_ev_class_of_dev_changed *rp = param;
if (len == 0 && status != 0) {
error("%s failed, status 0x%02x (%s)",
mgmt_opstr(op), status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Unexpected %s len %u", mgmt_opstr(op), len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("%s succeeded. Class 0x%02x%02x%02x", mgmt_opstr(op),
rp->dev_class[2], rp->dev_class[1], rp->dev_class[0]);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_class(int argc, char **argv)
{
uint8_t class[2];
uint16_t index;
class[0] = atoi(argv[1]);
class[1] = atoi(argv[2]);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (send_cmd(mgmt, MGMT_OP_SET_DEV_CLASS, index, sizeof(class), class,
class_rsp) == 0) {
error("Unable to send set_dev_class cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void disconnect_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_disconnect *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("Disconnect failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Invalid disconnect response length (%u)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->addr.bdaddr, addr);
if (status == 0)
print("%s disconnected", addr);
else
error("Disconnecting %s failed with status 0x%02x (%s)",
addr, status, mgmt_errstr(status));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option disconnect_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_disconnect(int argc, char **argv)
{
struct mgmt_cp_disconnect cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", disconnect_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
if (mgmt_send(mgmt, MGMT_OP_DISCONNECT, index, sizeof(cp), &cp,
disconnect_rsp, NULL, NULL) == 0) {
error("Unable to send disconnect cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void con_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_get_connections *rp = param;
uint16_t count, i;
if (len < sizeof(*rp)) {
error("Too small (%u bytes) get_connections rsp", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count = get_le16(&rp->conn_count);
if (len != sizeof(*rp) + count * sizeof(struct mgmt_addr_info)) {
error("Invalid get_connections length (count=%u, len=%u)",
count, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
for (i = 0; i < count; i++) {
char addr[18];
ba2str(&rp->addr[i].bdaddr, addr);
print("%s type %s", addr, typestr(rp->addr[i].type));
}
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_con(int argc, char **argv)
{
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (mgmt_send(mgmt, MGMT_OP_GET_CONNECTIONS, index, 0, NULL,
con_rsp, NULL, NULL) == 0) {
error("Unable to send get_connections cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void find_service_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Start Service Discovery failed: status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Service discovery started");
discovery = true;
}
static struct option find_service_options[] = {
{ "help", no_argument, 0, 'h' },
{ "le-only", no_argument, 0, 'l' },
{ "bredr-only", no_argument, 0, 'b' },
{ "uuid", required_argument, 0, 'u' },
{ "rssi", required_argument, 0, 'r' },
{ 0, 0, 0, 0 }
};
static void uuid_to_uuid128(uuid_t *uuid128, const uuid_t *uuid)
{
if (uuid->type == SDP_UUID16)
sdp_uuid16_to_uuid128(uuid128, uuid);
else if (uuid->type == SDP_UUID32)
sdp_uuid32_to_uuid128(uuid128, uuid);
else
memcpy(uuid128, uuid, sizeof(*uuid));
}
#define MAX_UUIDS 4
static void cmd_find_service(int argc, char **argv)
{
struct mgmt_cp_start_service_discovery *cp;
uint8_t buf[sizeof(*cp) + 16 * MAX_UUIDS];
uuid_t uuid;
uint128_t uint128;
uuid_t uuid128;
uint8_t type = SCAN_TYPE_DUAL;
int8_t rssi;
uint16_t count;
int opt;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
rssi = 127;
count = 0;
while ((opt = getopt_long(argc, argv, "+lbu:r:h",
find_service_options, NULL)) != -1) {
switch (opt) {
case 'l':
type &= ~SCAN_TYPE_BREDR;
type |= SCAN_TYPE_LE;
break;
case 'b':
type |= SCAN_TYPE_BREDR;
type &= ~SCAN_TYPE_LE;
break;
case 'u':
if (count == MAX_UUIDS) {
print("Max %u UUIDs supported", MAX_UUIDS);
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (bt_string2uuid(&uuid, optarg) < 0) {
print("Invalid UUID: %s", optarg);
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
cp = (void *) buf;
uuid_to_uuid128(&uuid128, &uuid);
ntoh128((uint128_t *) uuid128.value.uuid128.data,
&uint128);
htob128(&uint128, (uint128_t *) cp->uuids[count++]);
break;
case 'r':
rssi = atoi(optarg);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
cp = (void *) buf;
cp->type = type;
cp->rssi = rssi;
cp->uuid_count = cpu_to_le16(count);
if (mgmt_send(mgmt, MGMT_OP_START_SERVICE_DISCOVERY, index,
sizeof(*cp) + count * 16, cp,
find_service_rsp, NULL, NULL) == 0) {
error("Unable to send start_service_discovery cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void find_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Unable to start discovery. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Discovery started");
discovery = true;
}
static struct option find_options[] = {
{ "help", 0, 0, 'h' },
{ "le-only", 1, 0, 'l' },
{ "bredr-only", 1, 0, 'b' },
{ "limited", 1, 0, 'L' },
{ 0, 0, 0, 0 }
};
static void cmd_find(int argc, char **argv)
{
struct mgmt_cp_start_discovery cp;
uint8_t op = MGMT_OP_START_DISCOVERY;
uint8_t type = SCAN_TYPE_DUAL;
int opt;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
while ((opt = getopt_long(argc, argv, "+lbLh", find_options,
NULL)) != -1) {
switch (opt) {
case 'l':
type &= ~SCAN_TYPE_BREDR;
type |= SCAN_TYPE_LE;
break;
case 'b':
type |= SCAN_TYPE_BREDR;
type &= ~SCAN_TYPE_LE;
break;
case 'L':
op = MGMT_OP_START_LIMITED_DISCOVERY;
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
memset(&cp, 0, sizeof(cp));
cp.type = type;
if (mgmt_send(mgmt, op, index, sizeof(cp), &cp, find_rsp,
NULL, NULL) == 0) {
error("Unable to send start_discovery cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void stop_find_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0) {
error("Stop Discovery failed: status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
print("Discovery stopped");
discovery = false;
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option stop_find_options[] = {
{ "help", 0, 0, 'h' },
{ "le-only", 1, 0, 'l' },
{ "bredr-only", 1, 0, 'b' },
{ 0, 0, 0, 0 }
};
static void cmd_stop_find(int argc, char **argv)
{
struct mgmt_cp_stop_discovery cp;
uint8_t type = SCAN_TYPE_DUAL;
int opt;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
while ((opt = getopt_long(argc, argv, "+lbh", stop_find_options,
NULL)) != -1) {
switch (opt) {
case 'l':
type &= ~SCAN_TYPE_BREDR;
type |= SCAN_TYPE_LE;
break;
case 'b':
type |= SCAN_TYPE_BREDR;
type &= ~SCAN_TYPE_LE;
break;
case 'h':
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
}
argc -= optind;
argv += optind;
optind = 0;
memset(&cp, 0, sizeof(cp));
cp.type = type;
if (mgmt_send(mgmt, MGMT_OP_STOP_DISCOVERY, index, sizeof(cp), &cp,
stop_find_rsp, NULL, NULL) == 0) {
error("Unable to send stop_discovery cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void name_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Unable to set local name with status 0x%02x (%s)",
status, mgmt_errstr(status));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_name(int argc, char **argv)
{
struct mgmt_cp_set_local_name cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
strncpy((char *) cp.name, argv[1], HCI_MAX_NAME_LENGTH);
if (argc > 2)
strncpy((char *) cp.short_name, argv[2],
MGMT_MAX_SHORT_NAME_LENGTH);
if (mgmt_send(mgmt, MGMT_OP_SET_LOCAL_NAME, index, sizeof(cp), &cp,
name_rsp, NULL, NULL) == 0) {
error("Unable to send set_name cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void pair_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_pair_device *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("Pairing failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Unexpected pair_rsp len %u", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->addr.bdaddr, addr);
if (status)
error("Pairing with %s (%s) failed. status 0x%02x (%s)",
addr, typestr(rp->addr.type), status,
mgmt_errstr(status));
else
print("Paired with %s (%s)", addr, typestr(rp->addr.type));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option pair_options[] = {
{ "help", 0, 0, 'h' },
{ "capability", 1, 0, 'c' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_pair(int argc, char **argv)
{
struct mgmt_cp_pair_device cp;
uint8_t cap = 0x01;
uint8_t type = BDADDR_BREDR;
char addr[18];
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+c:t:h", pair_options,
NULL)) != -1) {
switch (opt) {
case 'c':
cap = strtol(optarg, NULL, 0);
break;
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
cp.io_cap = cap;
ba2str(&cp.addr.bdaddr, addr);
print("Pairing with %s (%s)", addr, typestr(cp.addr.type));
if (mgmt_send(mgmt, MGMT_OP_PAIR_DEVICE, index, sizeof(cp), &cp,
pair_rsp, NULL, NULL) == 0) {
error("Unable to send pair_device cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cancel_pair_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_addr_info *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("Cancel Pairing failed with 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Unexpected cancel_pair_rsp len %u", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->bdaddr, addr);
if (status)
error("Cancel Pairing with %s (%s) failed. 0x%02x (%s)",
addr, typestr(rp->type), status,
mgmt_errstr(status));
else
print("Pairing Cancelled with %s", addr);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option cancel_pair_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_cancel_pair(int argc, char **argv)
{
struct mgmt_addr_info cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", cancel_pair_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.bdaddr);
cp.type = type;
if (mgmt_reply(mgmt, MGMT_OP_CANCEL_PAIR_DEVICE, index, sizeof(cp), &cp,
cancel_pair_rsp, NULL, NULL) == 0) {
error("Unable to send cancel_pair_device cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void unpair_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_unpair_device *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("Unpair device failed. status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Unexpected unpair_device_rsp len %u", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->addr.bdaddr, addr);
if (status)
error("Unpairing %s failed. status 0x%02x (%s)",
addr, status, mgmt_errstr(status));
else
print("%s unpaired", addr);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option unpair_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_unpair(int argc, char **argv)
{
struct mgmt_cp_unpair_device cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index = mgmt_index;
while ((opt = getopt_long(argc, argv, "+t:h", unpair_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
cp.disconnect = 1;
if (mgmt_send(mgmt, MGMT_OP_UNPAIR_DEVICE, index, sizeof(cp), &cp,
unpair_rsp, NULL, NULL) == 0) {
error("Unable to send unpair_device cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void keys_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Load keys failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Keys successfully loaded");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_keys(int argc, char **argv)
{
struct mgmt_cp_load_link_keys cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
if (mgmt_send(mgmt, MGMT_OP_LOAD_LINK_KEYS, index, sizeof(cp), &cp,
keys_rsp, NULL, NULL) == 0) {
error("Unable to send load_keys cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void ltks_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Load keys failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Long term keys successfully loaded");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_ltks(int argc, char **argv)
{
struct mgmt_cp_load_long_term_keys cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
if (mgmt_send(mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS, index, sizeof(cp), &cp,
ltks_rsp, NULL, NULL) == 0) {
error("Unable to send load_ltks cmd");
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
}
static void irks_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Load IRKs failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Identity Resolving Keys successfully loaded");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option irks_options[] = {
{ "help", 0, 0, 'h' },
{ "local", 1, 0, 'l' },
{ "file", 1, 0, 'f' },
{ 0, 0, 0, 0 }
};
#define MAX_IRKS 4
static void cmd_irks(int argc, char **argv)
{
struct mgmt_cp_load_irks *cp;
uint8_t buf[sizeof(*cp) + 23 * MAX_IRKS];
uint16_t count, local_index;
char path[PATH_MAX];
int opt;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp = (void *) buf;
count = 0;
while ((opt = getopt_long(argc, argv, "+l:f:h",
irks_options, NULL)) != -1) {
switch (opt) {
case 'l':
if (count >= MAX_IRKS) {
error("Number of IRKs exceeded");
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (strlen(optarg) > 3 &&
strncasecmp(optarg, "hci", 3) == 0)
local_index = atoi(optarg + 3);
else
local_index = atoi(optarg);
snprintf(path, sizeof(path),
"/sys/kernel/debug/bluetooth/hci%u/identity",
local_index);
if (!load_identity(path, &cp->irks[count])) {
error("Unable to load identity");
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count++;
break;
case 'f':
if (count >= MAX_IRKS) {
error("Number of IRKs exceeded");
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (!load_identity(optarg, &cp->irks[count])) {
error("Unable to load identities");
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
count++;
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
cp->irk_count = cpu_to_le16(count);
if (mgmt_send(mgmt, MGMT_OP_LOAD_IRKS, index,
sizeof(*cp) + count * 23, cp,
irks_rsp, NULL, NULL) == 0) {
error("Unable to send load_irks cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void block_rsp(uint16_t op, uint16_t id, uint8_t status, uint16_t len,
const void *param)
{
const struct mgmt_addr_info *rp = param;
char addr[18];
if (len == 0 && status != 0) {
error("%s failed, status 0x%02x (%s)",
mgmt_opstr(op), status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Unexpected %s len %u", mgmt_opstr(op), len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->bdaddr, addr);
if (status)
error("%s %s (%s) failed. status 0x%02x (%s)",
mgmt_opstr(op), addr, typestr(rp->type),
status, mgmt_errstr(status));
else
print("%s %s succeeded", mgmt_opstr(op), addr);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option block_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_block(int argc, char **argv)
{
struct mgmt_cp_block_device cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", block_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
if (send_cmd(mgmt, MGMT_OP_BLOCK_DEVICE, index, sizeof(cp), &cp,
block_rsp) == 0) {
error("Unable to send block_device cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_unblock(int argc, char **argv)
{
struct mgmt_cp_unblock_device cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", block_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
if (send_cmd(mgmt, MGMT_OP_UNBLOCK_DEVICE, index, sizeof(cp), &cp,
block_rsp) == 0) {
error("Unable to send unblock_device cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_add_uuid(int argc, char **argv)
{
struct mgmt_cp_add_uuid cp;
uint128_t uint128;
uuid_t uuid, uuid128;
uint16_t index;
if (argc < 3) {
print("UUID and service hint needed");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (bt_string2uuid(&uuid, argv[1]) < 0) {
print("Invalid UUID: %s", argv[1]);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
memset(&cp, 0, sizeof(cp));
uuid_to_uuid128(&uuid128, &uuid);
ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
htob128(&uint128, (uint128_t *) cp.uuid);
cp.svc_hint = atoi(argv[2]);
if (send_cmd(mgmt, MGMT_OP_ADD_UUID, index, sizeof(cp), &cp,
class_rsp) == 0) {
error("Unable to send add_uuid cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_remove_uuid(int argc, char **argv)
{
struct mgmt_cp_remove_uuid cp;
uint128_t uint128;
uuid_t uuid, uuid128;
uint16_t index;
if (argc < 2) {
print("UUID needed");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (bt_string2uuid(&uuid, argv[1]) < 0) {
print("Invalid UUID: %s", argv[1]);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
memset(&cp, 0, sizeof(cp));
uuid_to_uuid128(&uuid128, &uuid);
ntoh128((uint128_t *) uuid128.value.uuid128.data, &uint128);
htob128(&uint128, (uint128_t *) cp.uuid);
if (send_cmd(mgmt, MGMT_OP_REMOVE_UUID, index, sizeof(cp), &cp,
class_rsp) == 0) {
error("Unable to send remove_uuid cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_clr_uuids(int argc, char **argv)
{
char *uuid_any = "00000000-0000-0000-0000-000000000000";
char *rm_argv[] = { "rm-uuid", uuid_any, NULL };
cmd_remove_uuid(2, rm_argv);
}
static void local_oob_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_local_oob_data *rp = param;
char str[33];
if (status != 0) {
error("Read Local OOB Data failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small (%u bytes) read_local_oob rsp", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
bin2hex(rp->hash192, 16, str, sizeof(str));
print("Hash C from P-192: %s", str);
bin2hex(rp->rand192, 16, str, sizeof(str));
print("Randomizer R with P-192: %s", str);
if (len < sizeof(*rp))
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
bin2hex(rp->hash256, 16, str, sizeof(str));
print("Hash C from P-256: %s", str);
bin2hex(rp->rand256, 16, str, sizeof(str));
print("Randomizer R with P-256: %s", str);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_local_oob(int argc, char **argv)
{
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (mgmt_send(mgmt, MGMT_OP_READ_LOCAL_OOB_DATA, index, 0, NULL,
local_oob_rsp, NULL, NULL) == 0) {
error("Unable to send read_local_oob cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void remote_oob_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_addr_info *rp = param;
char addr[18];
if (status != 0) {
error("Add Remote OOB Data failed: 0x%02x (%s)",
status, mgmt_errstr(status));
return;
}
if (len < sizeof(*rp)) {
error("Too small (%u bytes) add_remote_oob rsp", len);
return;
}
ba2str(&rp->bdaddr, addr);
print("Remote OOB data added for %s (%u)", addr, rp->type);
}
static struct option remote_oob_opt[] = {
{ "help", 0, 0, '?' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_remote_oob(int argc, char **argv)
{
struct mgmt_cp_add_remote_oob_data cp;
int opt;
uint16_t index;
memset(&cp, 0, sizeof(cp));
cp.addr.type = BDADDR_BREDR;
while ((opt = getopt_long(argc, argv, "+t:r:R:h:H:",
remote_oob_opt, NULL)) != -1) {
switch (opt) {
case 't':
cp.addr.type = strtol(optarg, NULL, 0);
break;
case 'r':
hex2bin(optarg, cp.rand192, 16);
break;
case 'h':
hex2bin(optarg, cp.hash192, 16);
break;
case 'R':
hex2bin(optarg, cp.rand256, 16);
break;
case 'H':
hex2bin(optarg, cp.hash256, 16);
break;
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
str2ba(argv[0], &cp.addr.bdaddr);
print("Adding OOB data for %s (%s)", argv[0], typestr(cp.addr.type));
if (mgmt_send(mgmt, MGMT_OP_ADD_REMOTE_OOB_DATA, index,
sizeof(cp), &cp, remote_oob_rsp,
NULL, NULL) == 0) {
error("Unable to send add_remote_oob cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void did_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Set Device ID failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Device ID successfully set");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_did(int argc, char **argv)
{
struct mgmt_cp_set_device_id cp;
uint16_t vendor, product, version , source;
int result;
uint16_t index;
result = sscanf(argv[1], "bluetooth:%4hx:%4hx:%4hx", &vendor, &product,
&version);
if (result == 3) {
source = 0x0001;
goto done;
}
result = sscanf(argv[1], "usb:%4hx:%4hx:%4hx", &vendor, &product,
&version);
if (result == 3) {
source = 0x0002;
goto done;
}
return;
done:
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp.source = htobs(source);
cp.vendor = htobs(vendor);
cp.product = htobs(product);
cp.version = htobs(version);
if (mgmt_send(mgmt, MGMT_OP_SET_DEVICE_ID, index, sizeof(cp), &cp,
did_rsp, NULL, NULL) == 0) {
error("Unable to send set_device_id cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void static_addr_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Set static address failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Static address successfully set");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_static_addr(int argc, char **argv)
{
struct mgmt_cp_set_static_address cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
str2ba(argv[1], &cp.bdaddr);
if (mgmt_send(mgmt, MGMT_OP_SET_STATIC_ADDRESS, index, sizeof(cp), &cp,
static_addr_rsp, NULL, NULL) == 0) {
error("Unable to send set_static_address cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void options_rsp(uint16_t op, uint16_t id, uint8_t status,
uint16_t len, const void *param)
{
const uint32_t *rp = param;
if (status != 0) {
error("%s for hci%u failed with status 0x%02x (%s)",
mgmt_opstr(op), id, status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small %s response (%u bytes)",
mgmt_opstr(op), len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("hci%u %s complete, options: %s", id, mgmt_opstr(op),
options2str(get_le32(rp)));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_public_addr(int argc, char **argv)
{
struct mgmt_cp_set_public_address cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
str2ba(argv[1], &cp.bdaddr);
if (send_cmd(mgmt, MGMT_OP_SET_PUBLIC_ADDRESS, index, sizeof(cp), &cp,
options_rsp) == 0) {
error("Unable to send Set Public Address cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_ext_config(int argc, char **argv)
{
struct mgmt_cp_set_external_config cp;
uint16_t index;
if (parse_setting(argc, argv, &cp.config) == false)
return bt_shell_noninteractive_quit(EXIT_FAILURE);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (send_cmd(mgmt, MGMT_OP_SET_EXTERNAL_CONFIG, index, sizeof(cp), &cp,
options_rsp) == 0) {
error("Unable to send Set External Config cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_debug_keys(int argc, char **argv)
{
cmd_setting(MGMT_OP_SET_DEBUG_KEYS, argc, argv);
}
static void conn_info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_get_conn_info *rp = param; char addr[18];
if (len == 0 && status != 0) {
error("Get Conn Info failed, status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Unexpected Get Conn Info len %u", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
ba2str(&rp->addr.bdaddr, addr);
if (status) {
error("Get Conn Info for %s (%s) failed. status 0x%02x (%s)",
addr, typestr(rp->addr.type),
status, mgmt_errstr(status));
} else {
print("Connection Information for %s (%s)",
addr, typestr(rp->addr.type));
print("\tRSSI %d\tTX power %d\tmaximum TX power %d",
rp->rssi, rp->tx_power, rp->max_tx_power);
}
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option conn_info_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_conn_info(int argc, char **argv)
{
struct mgmt_cp_get_conn_info cp;
uint8_t type = BDADDR_BREDR;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", conn_info_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
if (mgmt_send(mgmt, MGMT_OP_GET_CONN_INFO, index, sizeof(cp), &cp,
conn_info_rsp, NULL, NULL) == 0) {
error("Unable to send get_conn_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void io_cap_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Could not set IO Capability with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("IO Capabilities successfully set");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_io_cap(int argc, char **argv)
{
struct mgmt_cp_set_io_capability cp;
uint8_t cap;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cap = strtol(argv[1], NULL, 0);
memset(&cp, 0, sizeof(cp));
cp.io_capability = cap;
if (mgmt_send(mgmt, MGMT_OP_SET_IO_CAPABILITY, index, sizeof(cp), &cp,
io_cap_rsp, NULL, NULL) == 0) {
error("Unable to send set-io-cap cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void scan_params_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Set scan parameters failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Scan parameters successfully set");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_scan_params(int argc, char **argv)
{
struct mgmt_cp_set_scan_params cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp.interval = strtol(argv[1], NULL, 0);
cp.window = strtol(argv[2], NULL, 0);
if (mgmt_send(mgmt, MGMT_OP_SET_SCAN_PARAMS, index, sizeof(cp), &cp,
scan_params_rsp, NULL, NULL) == 0) {
error("Unable to send set_scan_params cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void clock_info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_get_clock_info *rp = param;
if (len < sizeof(*rp)) {
error("Unexpected Get Clock Info len %u", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (status) {
error("Get Clock Info failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Local Clock: %u", le32_to_cpu(rp->local_clock));
print("Piconet Clock: %u", le32_to_cpu(rp->piconet_clock));
print("Accurary: %u", le16_to_cpu(rp->accuracy));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_clock_info(int argc, char **argv)
{
struct mgmt_cp_get_clock_info cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
if (argc > 1)
str2ba(argv[1], &cp.addr.bdaddr);
if (mgmt_send(mgmt, MGMT_OP_GET_CLOCK_INFO, index, sizeof(cp), &cp,
clock_info_rsp, NULL, NULL) == 0) {
error("Unable to send get_clock_info cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void add_device_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Add device failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option add_device_options[] = {
{ "help", 0, 0, 'h' },
{ "action", 1, 0, 'a' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_add_device(int argc, char **argv)
{
struct mgmt_cp_add_device cp;
uint8_t action = 0x00;
uint8_t type = BDADDR_BREDR;
char addr[18];
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+a:t:h", add_device_options,
NULL)) != -1) {
switch (opt) {
case 'a':
action = strtol(optarg, NULL, 0);
break;
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
cp.action = action;
ba2str(&cp.addr.bdaddr, addr);
print("Adding device with %s (%s)", addr, typestr(cp.addr.type));
if (mgmt_send(mgmt, MGMT_OP_ADD_DEVICE, index, sizeof(cp), &cp,
add_device_rsp, NULL, NULL) == 0) {
error("Unable to send add device command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void remove_device_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Remove device failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static struct option del_device_options[] = {
{ "help", 0, 0, 'h' },
{ "type", 1, 0, 't' },
{ 0, 0, 0, 0 }
};
static void cmd_del_device(int argc, char **argv)
{
struct mgmt_cp_remove_device cp;
uint8_t type = BDADDR_BREDR;
char addr[18];
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+t:h", del_device_options,
NULL)) != -1) {
switch (opt) {
case 't':
type = strtol(optarg, NULL, 0);
break;
case 'h':
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
default:
bt_shell_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc < 1) {
bt_shell_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
str2ba(argv[0], &cp.addr.bdaddr);
cp.addr.type = type;
ba2str(&cp.addr.bdaddr, addr);
print("Removing device with %s (%s)", addr, typestr(cp.addr.type));
if (mgmt_send(mgmt, MGMT_OP_REMOVE_DEVICE, index, sizeof(cp), &cp,
remove_device_rsp, NULL, NULL) == 0) {
error("Unable to send remove device command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_clr_devices(int argc, char **argv)
{
char *bdaddr_any = "00:00:00:00:00:00";
char *rm_argv[] = { "del-device", bdaddr_any, NULL };
cmd_del_device(2, rm_argv);
}
static void local_oob_ext_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_local_oob_ext_data *rp = param;
uint16_t eir_len;
if (status != 0) {
error("Read Local OOB Ext Data failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small (%u bytes) read_local_oob_ext rsp", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
eir_len = le16_to_cpu(rp->eir_len);
if (len != sizeof(*rp) + eir_len) {
error("local_oob_ext: expected %zu bytes, got %u bytes",
sizeof(*rp) + eir_len, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print_eir(rp->eir, eir_len);
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_bredr_oob(int argc, char **argv)
{
struct mgmt_cp_read_local_oob_ext_data cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp.type = SCAN_TYPE_BREDR;
if (!mgmt_send(mgmt, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
index, sizeof(cp), &cp,
local_oob_ext_rsp, NULL, NULL)) {
error("Unable to send read_local_oob_ext cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_le_oob(int argc, char **argv)
{
struct mgmt_cp_read_local_oob_ext_data cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp.type = SCAN_TYPE_LE;
if (!mgmt_send(mgmt, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
index, sizeof(cp), &cp,
local_oob_ext_rsp, NULL, NULL)) {
error("Unable to send read_local_oob_ext cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static const char *adv_flags_str[] = {
"connectable",
"general-discoverable",
"limited-discoverable",
"managed-flags",
"tx-power",
"scan-rsp-appearance",
"scan-rsp-local-name",
};
static const char *adv_flags2str(uint32_t flags)
{
static char str[256];
unsigned i;
int off;
off = 0;
str[0] = '\0';
for (i = 0; i < NELEM(adv_flags_str); i++) {
if ((flags & (1 << i)) != 0)
off += snprintf(str + off, sizeof(str) - off, "%s ",
adv_flags_str[i]);
}
return str;
}
static void adv_features_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_read_adv_features *rp = param;
uint32_t supported_flags;
if (status != 0) {
error("Reading adv features failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small adv features reply (%u bytes)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp) + rp->num_instances * sizeof(uint8_t)) {
error("Instances count (%u) doesn't match reply length (%u)",
rp->num_instances, len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
supported_flags = le32_to_cpu(rp->supported_flags);
print("Supported flags: %s", adv_flags2str(supported_flags));
print("Max advertising data len: %u", rp->max_adv_data_len);
print("Max scan response data len: %u", rp->max_scan_rsp_len);
print("Max instances: %u", rp->max_instances);
print("Instances list with %u item%s", rp->num_instances,
rp->num_instances != 1 ? "s" : "");
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_advinfo(int argc, char **argv)
{
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
if (!mgmt_send(mgmt, MGMT_OP_READ_ADV_FEATURES, index, 0, NULL,
adv_features_rsp, NULL, NULL)) {
error("Unable to send advertising features command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void adv_size_info_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_get_adv_size_info *rp = param;
uint32_t flags;
if (status != 0) {
error("Reading adv size info failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len < sizeof(*rp)) {
error("Too small adv size info reply (%u bytes)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
flags = le32_to_cpu(rp->flags);
print("Instance: %u", rp->instance);
print("Flags: %s", adv_flags2str(flags));
print("Max advertising data len: %u", rp->max_adv_data_len);
print("Max scan response data len: %u", rp->max_scan_rsp_len);
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void advsize_usage(void)
{
bt_shell_usage();
print("Options:\n"
"\t -c, --connectable \"connectable\" flag\n"
"\t -g, --general-discov \"general-discoverable\" flag\n"
"\t -l, --limited-discov \"limited-discoverable\" flag\n"
"\t -m, --managed-flags \"managed-flags\" flag\n"
"\t -p, --tx-power \"tx-power\" flag\n"
"\t -a, --appearance \"appearance\" flag\n"
"\t -n, --local-name \"local-name\" flag");
}
static struct option advsize_options[] = {
{ "help", 0, 0, 'h' },
{ "connectable", 0, 0, 'c' },
{ "general-discov", 0, 0, 'g' },
{ "limited-discov", 0, 0, 'l' },
{ "managed-flags", 0, 0, 'm' },
{ "tx-power", 0, 0, 'p' },
{ "appearance", 0, 0, 'a' },
{ "local-name", 0, 0, 'n' },
{ 0, 0, 0, 0}
};
static void cmd_advsize(int argc, char **argv)
{
struct mgmt_cp_get_adv_size_info cp;
uint8_t instance;
uint32_t flags = 0;
int opt;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+cglmphna",
advsize_options, NULL)) != -1) {
switch (opt) {
case 'c':
flags |= MGMT_ADV_FLAG_CONNECTABLE;
break;
case 'g':
flags |= MGMT_ADV_FLAG_DISCOV;
break;
case 'l':
flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
break;
case 'm':
flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
break;
case 'p':
flags |= MGMT_ADV_FLAG_TX_POWER;
break;
case 'a':
flags |= MGMT_ADV_FLAG_APPEARANCE;
break;
case 'n':
flags |= MGMT_ADV_FLAG_LOCAL_NAME;
break;
default:
advsize_usage();
optind = 0;
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc != 1) {
advsize_usage();
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
instance = strtol(argv[0], NULL, 0);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
cp.instance = instance;
cp.flags = cpu_to_le32(flags);
if (!mgmt_send(mgmt, MGMT_OP_GET_ADV_SIZE_INFO, index, sizeof(cp), &cp,
adv_size_info_rsp, NULL, NULL)) {
error("Unable to send advertising size info command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void add_adv_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_add_advertising *rp = param;
if (status != 0) {
error("Add Advertising failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Invalid Add Advertising response length (%u)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Instance added: %u", rp->instance);
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void add_adv_usage(void)
{
bt_shell_usage();
print("Options:\n"
"\t -u, --uuid <uuid> Service UUID\n"
"\t -d, --adv-data <data> Advertising Data bytes\n"
"\t -s, --scan-rsp <data> Scan Response Data bytes\n"
"\t -t, --timeout <timeout> Timeout in seconds\n"
"\t -D, --duration <duration> Duration in seconds\n"
"\t -c, --connectable \"connectable\" flag\n"
"\t -g, --general-discov \"general-discoverable\" flag\n"
"\t -l, --limited-discov \"limited-discoverable\" flag\n"
"\t -n, --scan-rsp-local-name \"local-name\" flag\n"
"\t -a, --scan-rsp-appearance \"appearance\" flag\n"
"\t -m, --managed-flags \"managed-flags\" flag\n"
"\t -p, --tx-power \"tx-power\" flag\n"
"e.g.:\n"
"\tadd-adv -u 180d -u 180f -d 080954657374204C45 1");
}
static struct option add_adv_options[] = {
{ "help", 0, 0, 'h' },
{ "uuid", 1, 0, 'u' },
{ "adv-data", 1, 0, 'd' },
{ "scan-rsp", 1, 0, 's' },
{ "timeout", 1, 0, 't' },
{ "duration", 1, 0, 'D' },
{ "connectable", 0, 0, 'c' },
{ "general-discov", 0, 0, 'g' },
{ "limited-discov", 0, 0, 'l' },
{ "managed-flags", 0, 0, 'm' },
{ "tx-power", 0, 0, 'p' },
{ 0, 0, 0, 0}
};
static bool parse_bytes(char *optarg, uint8_t **bytes, size_t *len)
{
unsigned i;
if (!optarg) {
add_adv_usage();
return false;
}
*len = strlen(optarg);
if (*len % 2) {
error("Malformed data");
return false;
}
*len /= 2;
if (*len > UINT8_MAX) {
error("Data too long");
return false;
}
*bytes = malloc(*len);
if (!*bytes) {
error("Failed to allocate memory");
return false;
}
for (i = 0; i < *len; i++) {
if (sscanf(optarg + (i * 2), "%2hhx", *bytes + i) != 1) {
error("Invalid data");
free(*bytes);
*bytes = NULL;
return false;
}
}
return true;
}
#define MAX_AD_UUID_BYTES 32
static void cmd_add_adv(int argc, char **argv)
{
struct mgmt_cp_add_advertising *cp = NULL;
int opt;
uint8_t *adv_data = NULL, *scan_rsp = NULL;
size_t adv_len = 0, scan_rsp_len = 0;
size_t cp_len;
uint8_t uuids[MAX_AD_UUID_BYTES];
size_t uuid_bytes = 0;
uint8_t uuid_type = 0;
uint16_t timeout = 0, duration = 0;
uint8_t instance;
uuid_t uuid;
bool success = false;
bool quit = true;
uint32_t flags = 0;
uint16_t index;
while ((opt = getopt_long(argc, argv, "+u:d:s:t:D:cglmphna",
add_adv_options, NULL)) != -1) {
switch (opt) {
case 'u':
if (bt_string2uuid(&uuid, optarg) < 0) {
print("Invalid UUID: %s", optarg);
goto done;
}
if (uuid_type && uuid_type != uuid.type) {
print("UUID types must be consistent");
goto done;
}
if (uuid.type == SDP_UUID16) {
if (uuid_bytes + 2 >= MAX_AD_UUID_BYTES) {
print("Too many UUIDs");
goto done;
}
put_le16(uuid.value.uuid16, uuids + uuid_bytes);
uuid_bytes += 2;
} else if (uuid.type == SDP_UUID128) {
if (uuid_bytes + 16 >= MAX_AD_UUID_BYTES) {
print("Too many UUIDs");
goto done;
}
bswap_128(uuid.value.uuid128.data,
uuids + uuid_bytes);
uuid_bytes += 16;
} else {
printf("Unsupported UUID type");
goto done;
}
if (!uuid_type)
uuid_type = uuid.type;
break;
case 'd':
if (adv_len) {
print("Only one adv-data option allowed");
goto done;
}
if (!parse_bytes(optarg, &adv_data, &adv_len))
goto done;
break;
case 's':
if (scan_rsp_len) {
print("Only one scan-rsp option allowed");
goto done;
}
if (!parse_bytes(optarg, &scan_rsp, &scan_rsp_len))
goto done;
break;
case 't':
timeout = strtol(optarg, NULL, 0);
break;
case 'D':
duration = strtol(optarg, NULL, 0);
break;
case 'c':
flags |= MGMT_ADV_FLAG_CONNECTABLE;
break;
case 'g':
flags |= MGMT_ADV_FLAG_DISCOV;
break;
case 'l':
flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
break;
case 'm':
flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
break;
case 'p':
flags |= MGMT_ADV_FLAG_TX_POWER;
break;
case 'n':
flags |= MGMT_ADV_FLAG_LOCAL_NAME;
break;
case 'a':
flags |= MGMT_ADV_FLAG_APPEARANCE;
break;
case 'h':
success = true;
/* fall through */
default:
add_adv_usage();
optind = 0;
goto done;
}
}
argc -= optind;
argv += optind;
optind = 0;
if (argc != 1) {
add_adv_usage();
goto done;
}
if (uuid_bytes)
uuid_bytes += 2;
instance = strtol(argv[0], NULL, 0);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp_len = sizeof(*cp) + uuid_bytes + adv_len + scan_rsp_len;
cp = malloc0(cp_len);
if (!cp)
goto done;
cp->instance = instance;
put_le32(flags, &cp->flags);
put_le16(timeout, &cp->timeout);
put_le16(duration, &cp->duration);
cp->adv_data_len = adv_len + uuid_bytes;
cp->scan_rsp_len = scan_rsp_len;
if (uuid_bytes) {
cp->data[0] = uuid_bytes - 1;
cp->data[1] = uuid_type == SDP_UUID16 ? 0x03 : 0x07;
memcpy(cp->data + 2, uuids, uuid_bytes - 2);
}
memcpy(cp->data + uuid_bytes, adv_data, adv_len);
memcpy(cp->data + uuid_bytes + adv_len, scan_rsp, scan_rsp_len);
if (!mgmt_send(mgmt, MGMT_OP_ADD_ADVERTISING, index, cp_len, cp,
add_adv_rsp, NULL, NULL)) {
error("Unable to send \"Add Advertising\" command");
goto done;
}
quit = false;
done:
free(adv_data);
free(scan_rsp);
free(cp);
if (quit)
bt_shell_noninteractive_quit(success ? EXIT_SUCCESS : EXIT_FAILURE);
}
static void rm_adv_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
const struct mgmt_rp_remove_advertising *rp = param;
if (status != 0) {
error("Remove Advertising failed with status 0x%02x (%s)",
status, mgmt_errstr(status));
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
if (len != sizeof(*rp)) {
error("Invalid Remove Advertising response length (%u)", len);
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
print("Instance removed: %u", rp->instance);
return bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_rm_adv(int argc, char **argv)
{
struct mgmt_cp_remove_advertising cp;
uint8_t instance;
uint16_t index;
instance = strtol(argv[1], NULL, 0);
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
memset(&cp, 0, sizeof(cp));
cp.instance = instance;
if (!mgmt_send(mgmt, MGMT_OP_REMOVE_ADVERTISING, index, sizeof(cp), &cp,
rm_adv_rsp, NULL, NULL)) {
error("Unable to send \"Remove Advertising\" command");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void cmd_clr_adv(int argc, char **argv)
{
char *all_instances = "0";
char *rm_argv[] = { "rm-adv", all_instances, NULL };
cmd_rm_adv(2, rm_argv);
}
static void appearance_rsp(uint8_t status, uint16_t len, const void *param,
void *user_data)
{
if (status != 0)
error("Could not set Appearance with status 0x%02x (%s)",
status, mgmt_errstr(status));
else
print("Appearance successfully set");
bt_shell_noninteractive_quit(EXIT_SUCCESS);
}
static void cmd_appearance(int argc, char **argv)
{
struct mgmt_cp_set_appearance cp;
uint16_t index;
index = mgmt_index;
if (index == MGMT_INDEX_NONE)
index = 0;
cp.appearance = cpu_to_le16(strtol(argv[1], NULL, 0));
if (mgmt_send(mgmt, MGMT_OP_SET_APPEARANCE, index, sizeof(cp), &cp,
appearance_rsp, NULL, NULL) == 0) {
error("Unable to send appearance cmd");
return bt_shell_noninteractive_quit(EXIT_FAILURE);
}
}
static void register_mgmt_callbacks(struct mgmt *mgmt, uint16_t index)
{
mgmt_register(mgmt, MGMT_EV_CONTROLLER_ERROR, index, controller_error,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_INDEX_ADDED, index, index_added,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_INDEX_REMOVED, index, index_removed,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_NEW_SETTINGS, index, new_settings,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_DISCOVERING, index, discovering,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_NEW_LINK_KEY, index, new_link_key,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_DEVICE_CONNECTED, index, connected,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_DEVICE_DISCONNECTED, index, disconnected,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_CONNECT_FAILED, index, conn_failed,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_AUTH_FAILED, index, auth_failed,
NULL, NULL);
mgmt_register(mgmt, MGMT_EV_CLASS_OF_DEV_CHANGED, index,
class_of_dev_changed, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_LOCAL_NAME_CHANGED, index,
local_name_changed, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_DEVICE_FOUND, index, device_found,
mgmt, NULL);
mgmt_register(mgmt, MGMT_EV_PIN_CODE_REQUEST, index, request_pin,
mgmt, NULL);
mgmt_register(mgmt, MGMT_EV_USER_CONFIRM_REQUEST, index, user_confirm,
mgmt, NULL);
mgmt_register(mgmt, MGMT_EV_USER_PASSKEY_REQUEST, index,
request_passkey, mgmt, NULL);
mgmt_register(mgmt, MGMT_EV_PASSKEY_NOTIFY, index,
passkey_notify, mgmt, NULL);
mgmt_register(mgmt, MGMT_EV_UNCONF_INDEX_ADDED, index,
unconf_index_added, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_UNCONF_INDEX_REMOVED, index,
unconf_index_removed, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_NEW_CONFIG_OPTIONS, index,
new_config_options, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_EXT_INDEX_ADDED, index,
ext_index_added, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_EXT_INDEX_REMOVED, index,
ext_index_removed, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_LOCAL_OOB_DATA_UPDATED, index,
local_oob_data_updated, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_ADVERTISING_ADDED, index,
advertising_added, NULL, NULL);
mgmt_register(mgmt, MGMT_EV_ADVERTISING_REMOVED, index,
advertising_removed, NULL, NULL);
}
static void cmd_select(int argc, char **argv)
{
mgmt_cancel_all(mgmt);
mgmt_unregister_all(mgmt);
set_index(argv[1]);
register_mgmt_callbacks(mgmt, mgmt_index);
print("Selected index %u", mgmt_index);
update_prompt(mgmt_index);
}
static const struct bt_shell_menu main_menu = {
.name = "main",
.entries = {
{ "select", "<index>",
cmd_select, "Select a different index" },
{ "version", NULL,
cmd_version, "Get the MGMT Version" },
{ "commands", NULL,
cmd_commands, "List supported commands" },
{ "config", NULL,
cmd_config, "Show configuration info" },
{ "info", NULL,
cmd_info, "Show controller info" },
{ "extinfo", NULL,
cmd_extinfo, "Show extended controller info" },
{ "auto-power", NULL,
cmd_auto_power, "Power all available features" },
{ "power", "<on/off>",
cmd_power, "Toggle powered state" },
{ "discov", "<yes/no/limited> [timeout]",
cmd_discov, "Toggle discoverable state" },
{ "connectable", "<on/off>",
cmd_connectable, "Toggle connectable state" },
{ "fast-conn", "<on/off>",
cmd_fast_conn, "Toggle fast connectable state" },
{ "bondable", "<on/off>",
cmd_bondable, "Toggle bondable state" },
{ "pairable", "<on/off>",
cmd_bondable, "Toggle bondable state" },
{ "linksec", "<on/off>",
cmd_linksec, "Toggle link level security" },
{ "ssp", "<on/off>",
cmd_ssp, "Toggle SSP mode" },
{ "sc", "<on/off/only>",
cmd_sc, "Toogle SC support" },
{ "hs", "<on/off>",
cmd_hs, "Toggle HS support" },
{ "le", "<on/off>",
cmd_le, "Toggle LE support" },
{ "advertising", "<on/off>",
cmd_advertising, "Toggle LE advertising", },
{ "bredr", "<on/off>",
cmd_bredr, "Toggle BR/EDR support", },
{ "privacy", "<on/off>",
cmd_privacy, "Toggle privacy support" },
{ "class", "<major> <minor>",
cmd_class, "Set device major/minor class" },
{ "disconnect", "[-t type] <remote address>",
cmd_disconnect, "Disconnect device" },
{ "con", NULL,
cmd_con, "List connections" },
{ "find", "[-l|-b] [-L]",
cmd_find, "Discover nearby devices" },
{ "find-service", "[-u UUID] [-r RSSI_Threshold] [-l|-b]",
cmd_find_service, "Discover nearby service" },
{ "stop-find", "[-l|-b]",
cmd_stop_find, "Stop discovery" },
{ "name", "<name> [shortname]",
cmd_name, "Set local name" },
{ "pair", "[-c cap] [-t type] <remote address>",
cmd_pair, "Pair with a remote device" },
{ "cancelpair", "[-t type] <remote address>",
cmd_cancel_pair, "Cancel pairing" },
{ "unpair", "[-t type] <remote address>",
cmd_unpair, "Unpair device" },
{ "keys", NULL,
cmd_keys, "Load Link Keys" },
{ "ltks", NULL,
cmd_ltks, "Load Long Term Keys" },
{ "irks", "[--local <index>] [--file <file path>]",
cmd_irks, "Load Identity Resolving Keys" },
{ "block", "[-t type] <remote address>",
cmd_block, "Block Device" },
{ "unblock", "[-t type] <remote address>",
cmd_unblock, "Unblock Device" },
{ "add-uuid", "<UUID> <service class hint>",
cmd_add_uuid, "Add UUID" },
{ "rm-uuid", "<UUID>",
cmd_remove_uuid, "Remove UUID" },
{ "clr-uuids", NULL,
cmd_clr_uuids, "Clear UUIDs" },
{ "local-oob", NULL,
cmd_local_oob, "Local OOB data" },
{ "remote-oob", "[-t <addr_type>] [-r <rand192>] "
"[-h <hash192>] [-R <rand256>] "
"[-H <hash256>] <addr>",
cmd_remote_oob, "Remote OOB data" },
{ "did", "<source>:<vendor>:<product>:<version>",
cmd_did, "Set Device ID" },
{ "static-addr", "<address>",
cmd_static_addr, "Set static address" },
{ "public-addr", "<address>",
cmd_public_addr, "Set public address" },
{ "ext-config", "<on/off>",
cmd_ext_config, "External configuration" },
{ "debug-keys", "<on/off>",
cmd_debug_keys, "Toogle debug keys" },
{ "conn-info", "[-t type] <remote address>",
cmd_conn_info, "Get connection information" },
{ "io-cap", "<cap>",
cmd_io_cap, "Set IO Capability" },
{ "scan-params", "<interval> <window>",
cmd_scan_params, "Set Scan Parameters" },
{ "get-clock", "[address]",
cmd_clock_info, "Get Clock Information" },
{ "add-device", "[-a action] [-t type] <address>",
cmd_add_device, "Add Device" },
{ "del-device", "[-t type] <address>",
cmd_del_device, "Remove Device" },
{ "clr-devices", NULL,
cmd_clr_devices, "Clear Devices" },
{ "bredr-oob", NULL,
cmd_bredr_oob, "Local OOB data (BR/EDR)" },
{ "le-oob", NULL,
cmd_le_oob, "Local OOB data (LE)" },
{ "advinfo", NULL,
cmd_advinfo, "Show advertising features" },
{ "advsize", "[options] <instance_id>",
cmd_advsize, "Show advertising size info" },
{ "add-adv", "[options] <instance_id>",
cmd_add_adv, "Add advertising instance" },
{ "rm-adv", "<instance_id>",
cmd_rm_adv, "Remove advertising instance" },
{ "clr-adv", NULL,
cmd_clr_adv, "Clear advertising instances" },
{ "appearance", "<appearance>",
cmd_appearance, "Set appearance" },
{} },
};
static void mgmt_debug(const char *str, void *user_data)
{
const char *prefix = user_data;
print("%s%s", prefix, str);
}
static const char *index_option;
static struct option main_options[] = {
{ "index", 1, 0, 'i' },
{ 0, 0, 0, 0 }
};
static const char **optargs[] = {
&index_option
};
static const char *help[] = {
"Specify adapter index\n"
};
static const struct bt_shell_opt opt = {
.options = main_options,
.optno = sizeof(main_options) / sizeof(struct option),
.optstr = "i:V",
.optarg = optargs,
.help = help,
};
int main(int argc, char *argv[])
{
int status;
bt_shell_init(argc, argv, &opt);
bt_shell_set_menu(&main_menu);
mgmt = mgmt_new_default();
if (!mgmt) {
fprintf(stderr, "Unable to open mgmt_socket\n");
return EXIT_FAILURE;
}
if (getenv("MGMT_DEBUG"))
mgmt_set_debug(mgmt, mgmt_debug, "mgmt: ", NULL);
if (index_option)
set_index(index_option);
register_mgmt_callbacks(mgmt, mgmt_index);
bt_shell_attach(fileno(stdin));
update_prompt(mgmt_index);
bt_shell_run();
mgmt_cancel_all(mgmt);
mgmt_unregister_all(mgmt);
mgmt_unref(mgmt);
return status;
}
|