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
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2006-2007 Nokia Corporation
* Copyright (C) 2004-2008 Marcel Holtmann <marcel@holtmann.org>
*
*
* 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
#define _GNU_SOURCE
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <glib.h>
#include <dbus/dbus.h>
#include <gdbus.h>
#include "hcid.h"
#include "sdpd.h"
#include "manager.h"
#include "adapter.h"
#include "device.h"
#include "textfile.h"
#include "dbus-common.h"
#include "dbus-hci.h"
#include "dbus-database.h"
#include "error.h"
#include "glib-helper.h"
#include "logging.h"
#include "agent.h"
#define NUM_ELEMENTS(table) (sizeof(table)/sizeof(const char *))
#define IO_CAPABILITY_DISPLAYONLY 0x00
#define IO_CAPABILITY_DISPLAYYESNO 0x01
#define IO_CAPABILITY_KEYBOARDONLY 0x02
#define IO_CAPABILITY_NOINPUTOUTPUT 0x03
#define IO_CAPABILITY_INVALID 0xFF
#define check_address(address) bachk(address)
static DBusConnection *connection = NULL;
static GSList *adapter_drivers = NULL;
struct record_list {
sdp_list_t *recs;
const gchar *addr;
};
struct mode_req {
struct adapter *adapter;
DBusConnection *conn; /* Connection reference */
DBusMessage *msg; /* Message reference */
uint8_t mode; /* Requested mode */
guint id; /* Listener id */
};
struct service_auth {
service_auth_cb cb;
void *user_data;
};
static inline DBusMessage *invalid_args(DBusMessage *msg)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".InvalidArguments",
"Invalid arguments in method call");
}
static inline DBusMessage *not_available(DBusMessage *msg)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".NotAvailable",
"Not Available");
}
static inline DBusMessage *adapter_not_ready(DBusMessage *msg)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".NotReady",
"Adapter is not ready");
}
static inline DBusMessage *no_such_adapter(DBusMessage *msg)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".NoSuchAdapter",
"No such adapter");
}
static inline DBusMessage *failed_strerror(DBusMessage *msg, int err)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
strerror(err));
}
static inline DBusMessage *in_progress(DBusMessage *msg, const char *str)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".InProgress", str);
}
static inline DBusMessage *not_in_progress(DBusMessage *msg, const char *str)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".NotInProgress", str);
}
static inline DBusMessage *not_authorized(DBusMessage *msg)
{
return g_dbus_create_error(msg, ERROR_INTERFACE ".NotAuthorized",
"Not authorized");
}
static inline DBusMessage *unsupported_major_class(DBusMessage *msg)
{
return g_dbus_create_error(msg,
ERROR_INTERFACE ".UnsupportedMajorClass",
"Unsupported Major Class");
}
static DBusHandlerResult error_failed(DBusConnection *conn,
DBusMessage *msg, const char * desc)
{
return error_common_reply(conn, msg, ERROR_INTERFACE ".Failed", desc);
}
static DBusHandlerResult error_failed_errno(DBusConnection *conn,
DBusMessage *msg, int err)
{
const char *desc = strerror(err);
return error_failed(conn, msg, desc);
}
static DBusHandlerResult error_connection_attempt_failed(DBusConnection *conn,
DBusMessage *msg, int err)
{
return error_common_reply(conn, msg,
ERROR_INTERFACE ".ConnectionAttemptFailed",
err > 0 ? strerror(err) : "Connection attempt failed");
}
static int auth_req_cmp(const void *p1, const void *p2)
{
const struct pending_auth_info *pb1 = p1;
const bdaddr_t *bda = p2;
return bda ? bacmp(&pb1->bdaddr, bda) : -1;
}
struct pending_auth_info *adapter_find_auth_request(struct adapter *adapter,
bdaddr_t *dba)
{
GSList *l;
l = g_slist_find_custom(adapter->auth_reqs, dba, auth_req_cmp);
if (l)
return l->data;
return NULL;
}
void adapter_remove_auth_request(struct adapter *adapter, bdaddr_t *dba)
{
GSList *l;
struct pending_auth_info *auth;
l = g_slist_find_custom(adapter->auth_reqs, dba, auth_req_cmp);
if (!l)
return;
auth = l->data;
adapter->auth_reqs = g_slist_remove(adapter->auth_reqs, auth);
g_free(auth);
}
struct pending_auth_info *adapter_new_auth_request(struct adapter *adapter,
bdaddr_t *dba,
auth_type_t type)
{
struct pending_auth_info *info;
debug("hcid_dbus_new_auth_request");
info = g_new0(struct pending_auth_info, 1);
bacpy(&info->bdaddr, dba);
info->type = type;
adapter->auth_reqs = g_slist_append(adapter->auth_reqs, info);
if (adapter->bonding && !bacmp(dba, &adapter->bonding->bdaddr))
adapter->bonding->auth_active = 1;
return info;
}
int pending_remote_name_cancel(struct adapter *adapter)
{
struct remote_dev_info *dev, match;
GSList *l;
int dd, err = 0;
/* find the pending remote name request */
memset(&match, 0, sizeof(struct remote_dev_info));
bacpy(&match.bdaddr, BDADDR_ANY);
match.name_status = NAME_REQUESTED;
l = g_slist_find_custom(adapter->found_devices, &match,
(GCompareFunc) found_device_cmp);
if (!l) /* no pending request */
return 0;
dd = hci_open_dev(adapter->dev_id);
if (dd < 0)
return -ENODEV;
dev = l->data;
if (hci_read_remote_name_cancel(dd, &dev->bdaddr, 1000) < 0) {
error("Remote name cancel failed: %s(%d)", strerror(errno), errno);
err = -errno;
}
/* free discovered devices list */
g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
g_slist_free(adapter->found_devices);
adapter->found_devices = NULL;
hci_close_dev(dd);
return err;
}
static int auth_info_agent_cmp(const void *a, const void *b)
{
const struct pending_auth_info *auth = a;
const struct agent *agent = b;
if (auth->agent == agent)
return 0;
return -1;
}
static void device_agent_removed(struct agent *agent, void *user_data)
{
struct btd_device *device = user_data;
struct pending_auth_info *auth;
GSList *l;
struct adapter *adapter;
adapter = device_get_adapter(device);
device_set_agent(device, NULL);
l = g_slist_find_custom(adapter->auth_reqs, agent,
auth_info_agent_cmp);
if (!l)
return;
auth = l->data;
auth->agent = NULL;
}
static struct bonding_request_info *bonding_request_new(DBusConnection *conn,
DBusMessage *msg,
struct adapter *adapter,
const char *address,
const char *agent_path,
uint8_t capability)
{
struct bonding_request_info *bonding;
struct btd_device *device;
const char *name = dbus_message_get_sender(msg);
const gchar *destination;
struct agent *agent;
debug("bonding_request_new(%s)", address);
device = adapter_get_device(conn, adapter, address);
if (!device)
return NULL;
destination = device_get_address(device);
agent = agent_create(adapter, name, agent_path,
capability,
device_agent_removed,
device);
device_set_agent(device, agent);
debug("Temporary agent registered for hci%d/%s at %s:%s",
adapter->dev_id, destination, name,
agent_path);
bonding = g_new0(struct bonding_request_info, 1);
bonding->conn = dbus_connection_ref(conn);
bonding->msg = dbus_message_ref(msg);
bonding->adapter = adapter;
str2ba(address, &bonding->bdaddr);
return bonding;
}
const char *mode2str(uint8_t mode)
{
switch(mode) {
case MODE_OFF:
return "off";
case MODE_CONNECTABLE:
return "connectable";
case MODE_DISCOVERABLE:
return "discoverable";
case MODE_LIMITED:
return "limited";
default:
return "unknown";
}
}
static uint8_t on_mode(const char *addr)
{
char mode[14];
bdaddr_t sba;
str2ba(addr, &sba);
if (read_on_mode(&sba, mode, sizeof(mode)) < 0)
return MODE_CONNECTABLE;
return str2mode(addr, mode);
}
uint8_t str2mode(const char *addr, const char *mode)
{
if (strcasecmp("off", mode) == 0)
return MODE_OFF;
else if (strcasecmp("connectable", mode) == 0)
return MODE_CONNECTABLE;
else if (strcasecmp("discoverable", mode) == 0)
return MODE_DISCOVERABLE;
else if (strcasecmp("limited", mode) == 0)
return MODE_LIMITED;
else if (strcasecmp("on", mode) == 0)
return on_mode(addr);
else
return MODE_UNKNOWN;
}
static DBusMessage *set_mode(DBusConnection *conn, DBusMessage *msg,
uint8_t new_mode, void *data)
{
struct adapter *adapter = data;
uint8_t scan_enable;
uint8_t current_scan = adapter->scan_mode;
bdaddr_t local;
gboolean limited;
int err, dd;
const char *mode;
switch(new_mode) {
case MODE_OFF:
scan_enable = SCAN_DISABLED;
break;
case MODE_CONNECTABLE:
scan_enable = SCAN_PAGE;
break;
case MODE_DISCOVERABLE:
case MODE_LIMITED:
scan_enable = (SCAN_PAGE | SCAN_INQUIRY);
break;
default:
return invalid_args(msg);
}
/* Do reverse resolution in case of "on" mode */
mode = mode2str(new_mode);
dd = hci_open_dev(adapter->dev_id);
if (dd < 0)
return no_such_adapter(msg);
if (!adapter->up &&
(hcid.offmode == HCID_OFFMODE_NOSCAN ||
(hcid.offmode == HCID_OFFMODE_DEVDOWN &&
scan_enable != SCAN_DISABLED))) {
/* Start HCI device */
if (ioctl(dd, HCIDEVUP, adapter->dev_id) == 0)
goto done; /* on success */
if (errno != EALREADY) {
err = errno;
error("Can't init device hci%d: %s (%d)\n",
adapter->dev_id, strerror(errno), errno);
hci_close_dev(dd);
return failed_strerror(msg, err);
}
}
if (adapter->up && scan_enable == SCAN_DISABLED &&
hcid.offmode == HCID_OFFMODE_DEVDOWN) {
if (ioctl(dd, HCIDEVDOWN, adapter->dev_id) < 0) {
hci_close_dev(dd);
return failed_strerror(msg, errno);
}
goto done;
}
limited = (new_mode == MODE_LIMITED ? TRUE : FALSE);
err = set_limited_discoverable(dd, adapter->dev.class, limited);
if (err < 0) {
hci_close_dev(dd);
return failed_strerror(msg, -err);
}
if (current_scan != scan_enable) {
struct hci_request rq;
uint8_t status = 0;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_HOST_CTL;
rq.ocf = OCF_WRITE_SCAN_ENABLE;
rq.cparam = &scan_enable;
rq.clen = sizeof(scan_enable);
rq.rparam = &status;
rq.rlen = sizeof(status);
rq.event = EVT_CMD_COMPLETE;
if (hci_send_req(dd, &rq, 1000) < 0) {
err = errno;
error("Sending write scan enable command failed: %s (%d)",
strerror(errno), errno);
hci_close_dev(dd);
return failed_strerror(msg, err);
}
if (status) {
error("Setting scan enable failed with status 0x%02x",
status);
hci_close_dev(dd);
return failed_strerror(msg, bt_error(status));
}
} else {
/* discoverable or limited */
if ((scan_enable & SCAN_INQUIRY) && (new_mode != adapter->mode)) {
if (adapter->discov_timeout_id) {
g_source_remove(adapter->discov_timeout_id);
adapter->discov_timeout_id = 0;
}
if (!adapter->sessions && !adapter->discov_timeout)
adapter_set_discov_timeout(adapter,
adapter->discov_timeout * 1000);
}
}
done:
str2ba(adapter->address, &local);
write_device_mode(&local, mode);
hci_close_dev(dd);
adapter->mode = new_mode;
return dbus_message_new_method_return(msg);
}
static gint find_session(struct mode_req *req, DBusMessage *msg)
{
const char *name = dbus_message_get_sender(req->msg);
const char *sender = dbus_message_get_sender(msg);
return strcmp(name, sender);
}
static void confirm_mode_cb(struct agent *agent, DBusError *err, void *data)
{
struct mode_req *req = data;
DBusMessage *reply;
if (err && dbus_error_is_set(err)) {
reply = dbus_message_new_error(req->msg, err->name, err->message);
dbus_connection_send(req->conn, reply, NULL);
dbus_message_unref(reply);
goto cleanup;
}
reply = set_mode(req->conn, req->msg, req->mode, req->adapter);
dbus_connection_send(req->conn, reply, NULL);
dbus_message_unref(reply);
if (!g_slist_find_custom(req->adapter->sessions, req->msg,
(GCompareFunc) find_session))
goto cleanup;
return;
cleanup:
dbus_message_unref(req->msg);
if (req->id)
g_dbus_remove_watch(req->conn, req->id);
dbus_connection_unref(req->conn);
g_free(req);
}
static DBusMessage *confirm_mode(DBusConnection *conn, DBusMessage *msg,
const char *mode, void *data)
{
struct adapter *adapter = data;
struct mode_req *req;
int ret;
if (!adapter->agent)
return dbus_message_new_method_return(msg);
req = g_new0(struct mode_req, 1);
req->adapter = adapter;
req->conn = dbus_connection_ref(conn);
req->msg = dbus_message_ref(msg);
req->mode = str2mode(adapter->address, mode);
ret = agent_confirm_mode_change(adapter->agent, mode, confirm_mode_cb,
req);
if (ret < 0) {
dbus_connection_unref(req->conn);
dbus_message_unref(req->msg);
g_free(req);
return invalid_args(msg);
}
return NULL;
}
static DBusMessage *set_discoverable_timeout(DBusConnection *conn,
DBusMessage *msg,
uint32_t timeout,
void *data)
{
struct adapter *adapter = data;
bdaddr_t bdaddr;
const char *path;
if (adapter->discov_timeout_id) {
g_source_remove(adapter->discov_timeout_id);
adapter->discov_timeout_id = 0;
}
if ((timeout != 0) && (adapter->scan_mode & SCAN_INQUIRY))
adapter_set_discov_timeout(adapter, timeout * 1000);
adapter->discov_timeout = timeout;
str2ba(adapter->address, &bdaddr);
write_discoverable_timeout(&bdaddr, timeout);
path = dbus_message_get_path(msg);
dbus_connection_emit_property_changed(conn, path,
ADAPTER_INTERFACE,
"DiscoverableTimeout",
DBUS_TYPE_UINT32, &timeout);
return dbus_message_new_method_return(msg);
}
static void update_ext_inquiry_response(int dd, struct hci_dev *dev)
{
uint8_t fec = 0, data[240];
if (!(dev->features[6] & LMP_EXT_INQ))
return;
memset(data, 0, sizeof(data));
if (dev->ssp_mode > 0)
create_ext_inquiry_response((char *) dev->name, data);
if (hci_write_ext_inquiry_response(dd, fec, data, 2000) < 0)
error("Can't write extended inquiry response: %s (%d)",
strerror(errno), errno);
}
static int adapter_set_name(struct adapter *adapter, const char *name)
{
struct hci_dev *dev = &adapter->dev;
int dd, err;
bdaddr_t bdaddr;
str2ba(adapter->address, &bdaddr);
write_local_name(&bdaddr, (char *) name);
if (!adapter->up)
return 0;
dd = hci_open_dev(adapter->dev_id);
if (dd < 0) {
err = errno;
error("Can't open device hci%d: %s (%d)",
adapter->dev_id, strerror(err), err);
return -err;
}
if (hci_write_local_name(dd, name, 5000) < 0) {
err = errno;
error("Can't write name for hci%d: %s (%d)",
adapter->dev_id, strerror(err), err);
hci_close_dev(dd);
return -err;
}
strncpy((char *) dev->name, name, 248);
update_ext_inquiry_response(dd, dev);
hci_close_dev(dd);
return 0;
}
static DBusMessage *set_name(DBusConnection *conn, DBusMessage *msg,
const char *name, void *data)
{
struct adapter *adapter = data;
int ecode;
const char *path;
if (!g_utf8_validate(name, -1, NULL)) {
error("Name change failed: the supplied name isn't valid UTF-8");
return invalid_args(msg);
}
ecode = adapter_set_name(adapter, name);
if (ecode < 0)
return failed_strerror(msg, -ecode);
path = dbus_message_get_path(msg);
dbus_connection_emit_property_changed(conn, path,
ADAPTER_INTERFACE,
"Name", DBUS_TYPE_STRING,
&name);
return dbus_message_new_method_return(msg);
}
static void reply_authentication_failure(struct bonding_request_info *bonding)
{
DBusMessage *reply;
int status;
status = bonding->hci_status ?
bonding->hci_status : HCI_AUTHENTICATION_FAILURE;
reply = new_authentication_return(bonding->msg, status);
if (reply) {
dbus_connection_send(bonding->conn, reply, NULL);
dbus_message_unref(reply);
}
}
struct btd_device *adapter_find_device(struct adapter *adapter, const char *dest)
{
struct btd_device *device;
GSList *l;
if (!adapter)
return NULL;
l = g_slist_find_custom(adapter->devices,
dest, (GCompareFunc) device_address_cmp);
if (!l)
return NULL;
device = l->data;
return device;
}
struct btd_device *adapter_create_device(DBusConnection *conn,
struct adapter *adapter, const char *address)
{
struct btd_device *device;
debug("adapter_create_device(%s)", address);
device = device_create(conn, adapter, address);
if (!device)
return NULL;
device_set_temporary(device, TRUE);
adapter->devices = g_slist_append(adapter->devices, device);
return device;
}
static DBusMessage *remove_bonding(DBusConnection *conn, DBusMessage *msg,
const char *address, void *data)
{
struct adapter *adapter = data;
struct btd_device *device;
char filename[PATH_MAX + 1];
char *str;
bdaddr_t src, dst;
GSList *l;
int dev, err;
gboolean paired;
str2ba(adapter->address, &src);
str2ba(address, &dst);
dev = hci_open_dev(adapter->dev_id);
if (dev < 0 && msg)
return no_such_adapter(msg);
create_name(filename, PATH_MAX, STORAGEDIR, adapter->address,
"linkkeys");
/* textfile_del doesn't return an error when the key is not found */
str = textfile_caseget(filename, address);
paired = str ? TRUE : FALSE;
g_free(str);
if (!paired && msg) {
hci_close_dev(dev);
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"Bonding does not exist");
}
/* Delete the link key from storage */
if (textfile_casedel(filename, address) < 0 && msg) {
hci_close_dev(dev);
err = errno;
return failed_strerror(msg, err);
}
/* Delete the link key from the Bluetooth chip */
hci_delete_stored_link_key(dev, &dst, 0, 1000);
/* find the connection */
l = g_slist_find_custom(adapter->active_conn, &dst,
active_conn_find_by_bdaddr);
if (l) {
struct active_conn_info *con = l->data;
/* Send the HCI disconnect command */
if ((hci_disconnect(dev, htobs(con->handle),
HCI_OE_USER_ENDED_CONNECTION, 500) < 0)
&& msg){
int err = errno;
error("Disconnect failed");
hci_close_dev(dev);
return failed_strerror(msg, err);
}
}
hci_close_dev(dev);
device = adapter_find_device(adapter, address);
if (!device)
goto proceed;
if (paired) {
gboolean paired = FALSE;
const gchar *dev_path = device_get_path(device);
dbus_connection_emit_property_changed(conn, dev_path,
DEVICE_INTERFACE, "Paired",
DBUS_TYPE_BOOLEAN, &paired);
}
proceed:
if(!msg)
goto done;
return dbus_message_new_method_return(msg);
done:
return NULL;
}
void adapter_remove_device(DBusConnection *conn, struct adapter *adapter,
struct btd_device *device)
{
bdaddr_t src;
const gchar *destination = device_get_address(device);
const gchar *dev_path = device_get_path(device);
struct agent *agent;
str2ba(adapter->address, &src);
delete_entry(&src, "profiles", destination);
if (!device_is_temporary(device)) {
remove_bonding(conn, NULL, destination, adapter);
g_dbus_emit_signal(conn, adapter->path,
ADAPTER_INTERFACE,
"DeviceRemoved",
DBUS_TYPE_OBJECT_PATH, &dev_path,
DBUS_TYPE_INVALID);
}
agent = device_get_agent(device);
if (agent) {
agent_destroy(agent, FALSE);
device_set_agent(device, NULL);
}
adapter->devices = g_slist_remove(adapter->devices, device);
device_remove(conn, device);
}
struct btd_device *adapter_get_device(DBusConnection *conn,
struct adapter *adapter, const gchar *address)
{
struct btd_device *device;
debug("adapter_get_device(%s)", address);
if (!adapter)
return NULL;
device = adapter_find_device(adapter, address);
if (device)
return device;
return adapter_create_device(conn, adapter, address);
}
void remove_pending_device(struct adapter *adapter)
{
struct btd_device *device;
char address[18];
ba2str(&adapter->bonding->bdaddr, address);
device = adapter_find_device(adapter, address);
if (!device)
return;
if (device_is_temporary(device))
adapter_remove_device(adapter->bonding->conn, adapter, device);
}
static gboolean create_bonding_conn_complete(GIOChannel *io, GIOCondition cond,
struct adapter *adapter)
{
struct hci_request rq;
auth_requested_cp cp;
evt_cmd_status rp;
struct l2cap_conninfo cinfo;
socklen_t len;
int sk, dd, ret;
if (!adapter->bonding) {
/* If we come here it implies a bug somewhere */
debug("create_bonding_conn_complete: no pending bonding!");
g_io_channel_close(io);
g_io_channel_unref(io);
return FALSE;
}
if (cond & G_IO_NVAL) {
DBusMessage *reply;
reply = new_authentication_return(adapter->bonding->msg, 0x09);
g_dbus_send_message(adapter->bonding->conn, reply);
goto cleanup;
}
if (cond & (G_IO_HUP | G_IO_ERR)) {
debug("Hangup or error on bonding IO channel");
if (!adapter->bonding->auth_active)
error_connection_attempt_failed(adapter->bonding->conn,
adapter->bonding->msg,
ENETDOWN);
else
reply_authentication_failure(adapter->bonding);
goto failed;
}
sk = g_io_channel_unix_get_fd(io);
len = sizeof(ret);
if (getsockopt(sk, SOL_SOCKET, SO_ERROR, &ret, &len) < 0) {
error("Can't get socket error: %s (%d)",
strerror(errno), errno);
error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,
errno);
goto failed;
}
if (ret != 0) {
if (adapter->bonding->auth_active)
reply_authentication_failure(adapter->bonding);
else
error_connection_attempt_failed(adapter->bonding->conn,
adapter->bonding->msg,
ret);
goto failed;
}
len = sizeof(cinfo);
if (getsockopt(sk, SOL_L2CAP, L2CAP_CONNINFO, &cinfo, &len) < 0) {
error("Can't get connection info: %s (%d)",
strerror(errno), errno);
error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,
errno);
goto failed;
}
dd = hci_open_dev(adapter->dev_id);
if (dd < 0) {
DBusMessage *reply = no_such_adapter(adapter->bonding->msg);
g_dbus_send_message(adapter->bonding->conn, reply);
goto failed;
}
memset(&rp, 0, sizeof(rp));
memset(&cp, 0, sizeof(cp));
cp.handle = htobs(cinfo.hci_handle);
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LINK_CTL;
rq.ocf = OCF_AUTH_REQUESTED;
rq.cparam = &cp;
rq.clen = AUTH_REQUESTED_CP_SIZE;
rq.rparam = &rp;
rq.rlen = EVT_CMD_STATUS_SIZE;
rq.event = EVT_CMD_STATUS;
if (hci_send_req(dd, &rq, 500) < 0) {
error("Unable to send HCI request: %s (%d)",
strerror(errno), errno);
error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,
errno);
hci_close_dev(dd);
goto failed;
}
if (rp.status) {
error("HCI_Authentication_Requested failed with status 0x%02x",
rp.status);
error_failed_errno(adapter->bonding->conn, adapter->bonding->msg,
bt_error(rp.status));
hci_close_dev(dd);
goto failed;
}
hci_close_dev(dd);
adapter->bonding->auth_active = 1;
adapter->bonding->io_id = g_io_add_watch(io,
G_IO_NVAL | G_IO_HUP | G_IO_ERR,
(GIOFunc) create_bonding_conn_complete,
adapter);
return FALSE;
failed:
g_io_channel_close(io);
remove_pending_device(adapter);
cleanup:
g_dbus_remove_watch(adapter->bonding->conn,
adapter->bonding->listener_id);
bonding_request_free(adapter->bonding);
adapter->bonding = NULL;
return FALSE;
}
static void cancel_auth_request(struct pending_auth_info *auth, int dev_id)
{
int dd;
if (auth->replied)
return;
dd = hci_open_dev(dev_id);
if (dd < 0) {
error("hci_open_dev: %s (%d)", strerror(errno), errno);
return;
}
switch (auth->type) {
case AUTH_TYPE_PINCODE:
hci_send_cmd(dd, OGF_LINK_CTL, OCF_PIN_CODE_NEG_REPLY,
6, &auth->bdaddr);
break;
case AUTH_TYPE_CONFIRM:
hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_CONFIRM_NEG_REPLY,
6, &auth->bdaddr);
break;
case AUTH_TYPE_PASSKEY:
hci_send_cmd(dd, OGF_LINK_CTL, OCF_USER_PASSKEY_NEG_REPLY,
6, &auth->bdaddr);
break;
case AUTH_TYPE_NOTIFY:
/* User Notify doesn't require any reply */
break;
}
auth->replied = TRUE;
hci_close_dev(dd);
}
static void create_bond_req_exit(void *user_data)
{
struct adapter *adapter = user_data;
struct pending_auth_info *auth;
debug("CreateConnection requestor exited before bonding was completed");
auth = adapter_find_auth_request(adapter, &adapter->bonding->bdaddr);
if (auth) {
cancel_auth_request(auth, adapter->dev_id);
if (auth->agent)
agent_cancel(auth->agent);
adapter_remove_auth_request(adapter, &adapter->bonding->bdaddr);
}
remove_pending_device(adapter);
g_io_channel_close(adapter->bonding->io);
if (adapter->bonding->io_id)
g_source_remove(adapter->bonding->io_id);
bonding_request_free(adapter->bonding);
adapter->bonding = NULL;
}
static DBusMessage *create_bonding(DBusConnection *conn, DBusMessage *msg,
const char *address, const char *agent_path,
uint8_t capability, void *data)
{
char filename[PATH_MAX + 1];
char *str;
struct adapter *adapter = data;
struct bonding_request_info *bonding;
bdaddr_t bdaddr;
int sk;
str2ba(address, &bdaddr);
/* check if there is a pending discover: requested by D-Bus/non clients */
if (adapter->state & STD_INQUIRY)
return in_progress(msg, "Discover in progress");
pending_remote_name_cancel(adapter);
if (adapter->bonding)
return in_progress(msg, "Bonding in progress");
if (adapter_find_auth_request(adapter, &bdaddr))
return in_progress(msg, "Bonding in progress");
/* check if a link key already exists */
create_name(filename, PATH_MAX, STORAGEDIR, adapter->address,
"linkkeys");
str = textfile_caseget(filename, address);
if (str) {
free(str);
return g_dbus_create_error(msg,
ERROR_INTERFACE ".AlreadyExists",
"Bonding already exists");
}
sk = l2raw_connect(adapter->address, &bdaddr);
if (sk < 0)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".ConnectionAttemptFailed",
"Connection attempt failed");
bonding = bonding_request_new(conn, msg, adapter, address, agent_path,
capability);
if (!bonding) {
close(sk);
return NULL;
}
bonding->io = g_io_channel_unix_new(sk);
bonding->io_id = g_io_add_watch(bonding->io,
G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR,
(GIOFunc) create_bonding_conn_complete,
adapter);
bonding->listener_id = g_dbus_add_disconnect_watch(conn,
dbus_message_get_sender(msg),
create_bond_req_exit, adapter,
NULL);
adapter->bonding = bonding;
return NULL;
}
static void periodic_discover_req_exit(void *user_data)
{
struct adapter *adapter = user_data;
debug("PeriodicDiscovery requestor exited");
/* Cleanup the discovered devices list and send the cmd to exit from
* periodic inquiry or cancel remote name request. The return value can
* be ignored. */
cancel_periodic_discovery(adapter);
}
static DBusMessage *adapter_start_periodic(DBusConnection *conn,
DBusMessage *msg, void *data)
{
periodic_inquiry_cp cp;
struct hci_request rq;
struct adapter *adapter = data;
uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
uint8_t status;
int dd;
if (!adapter->up)
return adapter_not_ready(msg);
if (dbus_message_is_method_call(msg, ADAPTER_INTERFACE,
"StartPeriodicDiscovery")) {
if (!dbus_message_has_signature(msg,
DBUS_TYPE_INVALID_AS_STRING))
return invalid_args(msg);
}
if ((adapter->state & STD_INQUIRY) || (adapter->state & PERIODIC_INQUIRY))
return in_progress(msg, "Discover in progress");
pending_remote_name_cancel(adapter);
dd = hci_open_dev(adapter->dev_id);
if (dd < 0)
return no_such_adapter(msg);
memset(&cp, 0, sizeof(cp));
memcpy(&cp.lap, lap, 3);
cp.max_period = htobs(24);
cp.min_period = htobs(16);
cp.length = 0x08;
cp.num_rsp = 0x00;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LINK_CTL;
rq.ocf = OCF_PERIODIC_INQUIRY;
rq.cparam = &cp;
rq.clen = PERIODIC_INQUIRY_CP_SIZE;
rq.rparam = &status;
rq.rlen = sizeof(status);
rq.event = EVT_CMD_COMPLETE;
if (hci_send_req(dd, &rq, 1000) < 0) {
int err = errno;
error("Unable to start periodic inquiry: %s (%d)",
strerror(errno), errno);
hci_close_dev(dd);
return failed_strerror(msg, err);
}
if (status) {
error("HCI_Periodic_Inquiry_Mode failed with status 0x%02x",
status);
hci_close_dev(dd);
return failed_strerror(msg, bt_error(status));
}
adapter->pdiscov_requestor = g_strdup(dbus_message_get_sender(msg));
hci_close_dev(dd);
/* track the request owner to cancel it automatically if the owner
* exits */
adapter->pdiscov_listener = g_dbus_add_disconnect_watch(conn,
dbus_message_get_sender(msg),
periodic_discover_req_exit,
adapter, NULL);
return dbus_message_new_method_return(msg);
}
static DBusMessage *adapter_stop_periodic(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
int err;
if (!adapter->up)
return adapter_not_ready(msg);
if (!(adapter->state & PERIODIC_INQUIRY))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".NotAuthorized",
"Not authorized");
/*
* Cleanup the discovered devices list and send the cmd to exit
* from periodic inquiry mode or cancel remote name request.
*/
err = cancel_periodic_discovery(adapter);
if (err < 0) {
if (err == -ENODEV)
return no_such_adapter(msg);
else
return failed_strerror(msg, -err);
}
return dbus_message_new_method_return(msg);
}
static void discover_devices_req_exit(void *user_data)
{
struct adapter *adapter = user_data;
debug("DiscoverDevices requestor exited");
/* Cleanup the discovered devices list and send the command to cancel
* inquiry or cancel remote name request. The return can be ignored. */
cancel_discovery(adapter);
}
static DBusMessage *adapter_discover_devices(DBusConnection *conn,
DBusMessage *msg, void *data)
{
inquiry_cp cp;
evt_cmd_status rp;
struct hci_request rq;
struct adapter *adapter = data;
uint8_t lap[3] = { 0x33, 0x8b, 0x9e };
int dd;
if (!adapter->up)
return adapter_not_ready(msg);
if (!dbus_message_has_signature(msg, DBUS_TYPE_INVALID_AS_STRING))
return invalid_args(msg);
if (adapter->state & STD_INQUIRY)
return in_progress(msg, "Discover in progress");
pending_remote_name_cancel(adapter);
if (adapter->bonding)
return in_progress(msg, "Bonding in progress");
dd = hci_open_dev(adapter->dev_id);
if (dd < 0)
return no_such_adapter(msg);
memset(&cp, 0, sizeof(cp));
memcpy(&cp.lap, lap, 3);
cp.length = 0x08;
cp.num_rsp = 0x00;
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_LINK_CTL;
rq.ocf = OCF_INQUIRY;
rq.cparam = &cp;
rq.clen = INQUIRY_CP_SIZE;
rq.rparam = &rp;
rq.rlen = EVT_CMD_STATUS_SIZE;
rq.event = EVT_CMD_STATUS;
if (hci_send_req(dd, &rq, 500) < 0) {
int err = errno;
error("Unable to start inquiry: %s (%d)",
strerror(errno), errno);
hci_close_dev(dd);
return failed_strerror(msg, err);
}
if (rp.status) {
error("HCI_Inquiry command failed with status 0x%02x",
rp.status);
hci_close_dev(dd);
return failed_strerror(msg, bt_error(rp.status));
}
adapter->state |= (STD_INQUIRY | RESOLVE_NAME);
adapter->discov_requestor = g_strdup(dbus_message_get_sender(msg));
hci_close_dev(dd);
/* track the request owner to cancel it automatically if the owner
* exits */
adapter->discov_listener = g_dbus_add_disconnect_watch(conn,
dbus_message_get_sender(msg),
discover_devices_req_exit,
adapter, NULL);
return dbus_message_new_method_return(msg);
}
static DBusMessage *adapter_cancel_discovery(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
int err;
if (!adapter->up)
return adapter_not_ready(msg);
if (!dbus_message_has_signature(msg, DBUS_TYPE_INVALID_AS_STRING))
return invalid_args(msg);
/* is there discover pending? or discovery cancel was requested
* previously */
if (!(adapter->state & STD_INQUIRY) || adapter->discovery_cancel)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".NotAuthorized",
"Not Authorized");
/* only the discover requestor can cancel the inquiry process */
if (!adapter->discov_requestor ||
strcmp(adapter->discov_requestor, dbus_message_get_sender(msg)))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".NotAuthorized",
"Not Authorized");
/* Cleanup the discovered devices list and send the cmd to cancel
* inquiry or cancel remote name request */
err = cancel_discovery(adapter);
if (err < 0) {
if (err == -ENODEV)
return no_such_adapter(msg);
else
return failed_strerror(msg, -err);
}
/* Reply before send DiscoveryCompleted */
adapter->discovery_cancel = dbus_message_ref(msg);
return NULL;
}
struct remote_device_list_t {
GSList *list;
time_t time;
};
static DBusMessage *get_properties(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
const char *property;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter dict;
bdaddr_t ba;
char str[249];
gboolean discov_active;
if (check_address(adapter->address) < 0)
return adapter_not_ready(msg);
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
/* Address */
property = adapter->address;
dbus_message_iter_append_dict_entry(&dict, "Address",
DBUS_TYPE_STRING, &property);
/* Name */
memset(str, 0, sizeof(str));
property = str;
str2ba(adapter->address, &ba);
if (!read_local_name(&ba, str))
dbus_message_iter_append_dict_entry(&dict, "Name",
DBUS_TYPE_STRING, &property);
/* Mode */
property = mode2str(adapter->mode);
dbus_message_iter_append_dict_entry(&dict, "Mode",
DBUS_TYPE_STRING, &property);
/* DiscoverableTimeout */
dbus_message_iter_append_dict_entry(&dict, "DiscoverableTimeout",
DBUS_TYPE_UINT32, &adapter->discov_timeout);
discov_active = (adapter->state & PERIODIC_INQUIRY) ? TRUE:FALSE;
/* PeriodicDiscovery */
dbus_message_iter_append_dict_entry(&dict, "PeriodicDiscovery",
DBUS_TYPE_BOOLEAN, &discov_active);
dbus_message_iter_close_container(&iter, &dict);
return reply;
}
static DBusMessage *set_property(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
DBusMessageIter iter;
DBusMessageIter sub;
const char *property;
if (!dbus_message_iter_init(msg, &iter))
return invalid_args(msg);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_STRING)
return invalid_args(msg);
dbus_message_iter_get_basic(&iter, &property);
dbus_message_iter_next(&iter);
if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
return invalid_args(msg);
dbus_message_iter_recurse(&iter, &sub);
if (g_str_equal("Name", property)) {
const char *name;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)
return invalid_args(msg);
dbus_message_iter_get_basic(&sub, &name);
return set_name(conn, msg, name, data);
} else if (g_str_equal("DiscoverableTimeout", property)) {
uint32_t timeout;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT32)
return invalid_args(msg);
dbus_message_iter_get_basic(&sub, &timeout);
return set_discoverable_timeout(conn, msg, timeout, data);
} else if (g_str_equal("PeriodicDiscovery", property)) {
dbus_bool_t value;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_BOOLEAN)
return invalid_args(msg);
dbus_message_iter_get_basic(&sub, &value);
if (value)
return adapter_start_periodic(conn, msg, data);
else
return adapter_stop_periodic(conn, msg, data);
} else if (g_str_equal("Mode", property)) {
const char *mode;
if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)
return invalid_args(msg);
dbus_message_iter_get_basic(&sub, &mode);
adapter->global_mode = str2mode(adapter->address, mode);
if (adapter->global_mode == adapter->mode)
return dbus_message_new_method_return(msg);
if (adapter->sessions && adapter->global_mode < adapter->mode)
return confirm_mode(conn, msg, mode, data);
return set_mode(conn, msg, str2mode(adapter->address, mode),
data);
}
return invalid_args(msg);
}
static void session_exit(void *data)
{
struct mode_req *req = data;
struct adapter *adapter = req->adapter;
adapter->sessions = g_slist_remove(adapter->sessions, req);
if (!adapter->sessions) {
debug("Falling back to '%s' mode", mode2str(adapter->global_mode));
/* FIXME: fallback to previous mode
set_mode(req->conn, req->msg, adapter->global_mode, adapter);
*/
}
dbus_connection_unref(req->conn);
dbus_message_unref(req->msg);
g_free(req);
}
static DBusMessage *request_mode(DBusConnection *conn,
DBusMessage *msg, void *data)
{
const char *mode;
struct adapter *adapter = data;
struct mode_req *req;
uint8_t new_mode;
int ret;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &mode,
DBUS_TYPE_INVALID))
return invalid_args(msg);
new_mode = str2mode(adapter->address, mode);
if (new_mode != MODE_CONNECTABLE && new_mode != MODE_DISCOVERABLE)
return invalid_args(msg);
if (!adapter->agent)
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
"No agent registered");
if (g_slist_find_custom(adapter->sessions, msg,
(GCompareFunc) find_session))
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
"Mode already requested");
req = g_new0(struct mode_req, 1);
req->adapter = adapter;
req->conn = dbus_connection_ref(conn);
req->msg = dbus_message_ref(msg);
req->mode = new_mode;
req->id = g_dbus_add_disconnect_watch(conn,
dbus_message_get_sender(msg),
session_exit, req, NULL);
if (!adapter->sessions)
adapter->global_mode = adapter->mode;
adapter->sessions = g_slist_append(adapter->sessions, req);
/* No need to change mode */
if (adapter->mode >= new_mode)
return dbus_message_new_method_return(msg);
ret = agent_confirm_mode_change(adapter->agent, mode, confirm_mode_cb,
req);
if (ret < 0) {
dbus_message_unref(req->msg);
g_dbus_remove_watch(req->conn, req->id);
dbus_connection_unref(req->conn);
g_free(req);
return invalid_args(msg);
}
return NULL;
}
static DBusMessage *release_mode(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
GSList *l;
l = g_slist_find_custom(adapter->sessions, msg,
(GCompareFunc) find_session);
if (!l)
return g_dbus_create_error(msg, ERROR_INTERFACE ".Failed",
"No Mode to release");
session_exit(l->data);
return dbus_message_new_method_return(msg);
}
static DBusMessage *list_devices(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
DBusMessage *reply;
GSList *l;
DBusMessageIter iter;
DBusMessageIter array_iter;
const gchar *dev_path;
if (!dbus_message_has_signature(msg, DBUS_TYPE_INVALID_AS_STRING))
return invalid_args(msg);
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dbus_message_iter_init_append(reply, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
DBUS_TYPE_OBJECT_PATH_AS_STRING, &array_iter);
for (l = adapter->devices; l; l = l->next) {
struct btd_device *device = l->data;
if (device_is_temporary(device))
continue;
dev_path = device_get_path(device);
dbus_message_iter_append_basic(&array_iter,
DBUS_TYPE_OBJECT_PATH, &dev_path);
}
dbus_message_iter_close_container(&iter, &array_iter);
return reply;
}
static DBusMessage *create_device(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
struct btd_device *device;
const gchar *address;
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID) == FALSE)
return invalid_args(msg);
if (check_address(address) < 0)
return invalid_args(msg);
if (adapter_find_device(adapter, address))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".AlreadyExists",
"Device already exists");
debug("create_device(%s)", address);
device = device_create(conn, adapter, address);
if (!device)
return NULL;
device_set_temporary(device, FALSE);
device_browse(device, conn, msg, NULL);
adapter->devices = g_slist_append(adapter->devices, device);
return NULL;
}
static uint8_t parse_io_capability(const char *capability)
{
if (g_str_equal(capability, ""))
return IO_CAPABILITY_DISPLAYYESNO;
if (g_str_equal(capability, "DisplayOnly"))
return IO_CAPABILITY_DISPLAYONLY;
if (g_str_equal(capability, "DisplayYesNo"))
return IO_CAPABILITY_DISPLAYYESNO;
if (g_str_equal(capability, "KeyboardOnly"))
return IO_CAPABILITY_KEYBOARDONLY;
if (g_str_equal(capability, "NoInputOutput"))
return IO_CAPABILITY_NOINPUTOUTPUT;
return IO_CAPABILITY_INVALID;
}
static DBusMessage *create_paired_device(DBusConnection *conn,
DBusMessage *msg, void *data)
{
const gchar *address, *agent_path, *capability;
uint8_t cap;
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address,
DBUS_TYPE_OBJECT_PATH, &agent_path,
DBUS_TYPE_STRING, &capability,
DBUS_TYPE_INVALID) == FALSE)
return invalid_args(msg);
if (check_address(address) < 0)
return invalid_args(msg);
cap = parse_io_capability(capability);
if (cap == IO_CAPABILITY_INVALID)
return invalid_args(msg);
return create_bonding(conn, msg, address, agent_path, cap, data);
}
static gint device_path_cmp(struct btd_device *device, const gchar *path)
{
const gchar *dev_path = device_get_path(device);
return strcasecmp(dev_path, path);
}
static DBusMessage *remove_device(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
struct btd_device *device;
const char *path;
GSList *l;
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID) == FALSE)
return invalid_args(msg);
l = g_slist_find_custom(adapter->devices,
path, (GCompareFunc) device_path_cmp);
if (!l)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"Device does not exist");
device = l->data;
if (device_is_temporary(device) || device_is_busy(device))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"Device creation in progress");
adapter_remove_device(conn, adapter, device);
return dbus_message_new_method_return(msg);
}
static DBusMessage *find_device(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
struct btd_device *device;
DBusMessage *reply;
const gchar *address;
GSList *l;
const gchar *dev_path;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &address,
DBUS_TYPE_INVALID))
return invalid_args(msg);
l = g_slist_find_custom(adapter->devices,
address, (GCompareFunc) device_address_cmp);
if (!l)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"Device does not exist");
device = l->data;
if (device_is_temporary(device))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"Device creation in progress");
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dev_path = device_get_path(device);
dbus_message_append_args(reply,
DBUS_TYPE_OBJECT_PATH, &dev_path,
DBUS_TYPE_INVALID);
return reply;
}
static void agent_removed(struct agent *agent, struct adapter *adapter)
{
struct pending_auth_info *auth;
GSList *l;
adapter->agent = NULL;
l = g_slist_find_custom(adapter->auth_reqs, agent,
auth_info_agent_cmp);
if (!l)
return;
auth = l->data;
auth->agent = NULL;
}
static DBusMessage *register_agent(DBusConnection *conn,
DBusMessage *msg, void *data)
{
const char *path, *name, *capability;
struct agent *agent;
struct adapter *adapter = data;
uint8_t cap;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_STRING, &capability, DBUS_TYPE_INVALID))
return NULL;
if (adapter->agent)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".AlreadyExists",
"Agent already exists");
cap = parse_io_capability(capability);
if (cap == IO_CAPABILITY_INVALID)
return invalid_args(msg);
name = dbus_message_get_sender(msg);
agent = agent_create(adapter, name, path, cap,
(agent_remove_cb) agent_removed, adapter);
if (!agent)
return g_dbus_create_error(msg,
ERROR_INTERFACE ".Failed",
"Failed to create a new agent");
adapter->agent = agent;
debug("Agent registered for hci%d at %s:%s", adapter->dev_id, name,
path);
return dbus_message_new_method_return(msg);
}
static DBusMessage *unregister_agent(DBusConnection *conn,
DBusMessage *msg, void *data)
{
const char *path, *name;
struct adapter *adapter = data;
if (!dbus_message_get_args(msg, NULL, DBUS_TYPE_OBJECT_PATH, &path,
DBUS_TYPE_INVALID))
return NULL;
name = dbus_message_get_sender(msg);
if (!adapter->agent || !agent_matches(adapter->agent, name, path))
return g_dbus_create_error(msg,
ERROR_INTERFACE ".DoesNotExist",
"No such agent");
agent_destroy(adapter->agent, FALSE);
adapter->agent = NULL;
return dbus_message_new_method_return(msg);
}
static DBusMessage *add_service_record(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
DBusMessage *reply;
const char *sender, *record;
dbus_uint32_t handle;
bdaddr_t src;
int err;
if (dbus_message_get_args(msg, NULL,
DBUS_TYPE_STRING, &record, DBUS_TYPE_INVALID) == FALSE)
return NULL;
sender = dbus_message_get_sender(msg);
str2ba(adapter->address, &src);
err = add_xml_record(conn, sender, &src, record, &handle);
if (err < 0)
return failed_strerror(msg, err);
reply = dbus_message_new_method_return(msg);
if (!reply)
return NULL;
dbus_message_append_args(reply, DBUS_TYPE_UINT32, &handle,
DBUS_TYPE_INVALID);
return reply;
}
static DBusMessage *update_service_record(DBusConnection *conn,
DBusMessage *msg, void *data)
{
struct adapter *adapter = data;
bdaddr_t src;
str2ba(adapter->address, &src);
return update_xml_record(conn, msg, &src);
}
static DBusMessage *remove_service_record(DBusConnection *conn,
DBusMessage *msg, void *data)
{
dbus_uint32_t handle;
const char *sender;
if (dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32, &handle,
DBUS_TYPE_INVALID) == FALSE)
return NULL;
sender = dbus_message_get_sender(msg);
if (remove_record(conn, sender, handle) < 0)
return not_available(msg);
return dbus_message_new_method_return(msg);
}
static DBusMessage *request_authorization(DBusConnection *conn,
DBusMessage *msg, void *data)
{
/* FIXME implement the request */
return NULL;
}
static DBusMessage *cancel_authorization(DBusConnection *conn,
DBusMessage *msg, void *data)
{
/* FIXME implement cancel request */
return dbus_message_new_method_return(msg);
}
/* BlueZ 4.0 API */
static GDBusMethodTable adapter_methods[] = {
{ "GetProperties", "", "a{sv}",get_properties },
{ "SetProperty", "sv", "", set_property,
G_DBUS_METHOD_FLAG_ASYNC},
{ "RequestMode", "s", "", request_mode,
G_DBUS_METHOD_FLAG_ASYNC},
{ "ReleaseMode", "", "", release_mode },
{ "DiscoverDevices", "", "", adapter_discover_devices},
{ "CancelDiscovery", "", "", adapter_cancel_discovery,
G_DBUS_METHOD_FLAG_ASYNC},
{ "ListDevices", "", "ao", list_devices },
{ "CreateDevice", "s", "o", create_device,
G_DBUS_METHOD_FLAG_ASYNC},
{ "CreatePairedDevice", "sos", "o", create_paired_device,
G_DBUS_METHOD_FLAG_ASYNC},
{ "RemoveDevice", "o", "", remove_device },
{ "FindDevice", "s", "o", find_device },
{ "RegisterAgent", "os", "", register_agent },
{ "UnregisterAgent", "o", "", unregister_agent },
{ "AddServiceRecord", "s", "u", add_service_record },
{ "UpdateServiceRecord","us", "", update_service_record },
{ "RemoveServiceRecord","u", "", remove_service_record },
{ "RequestAuthorization","su", "", request_authorization,
G_DBUS_METHOD_FLAG_ASYNC},
{ "CancelAuthorization","", "", cancel_authorization },
{ }
};
static GDBusSignalTable adapter_signals[] = {
{ "DiscoveryStarted", "" },
{ "DiscoveryCompleted", "" },
{ "DeviceCreated", "o" },
{ "DeviceRemoved", "o" },
{ "DeviceFound", "sa{sv}" },
{ "PropertyChanged", "sv" },
{ "DeviceDisappeared", "s" },
{ }
};
static inline uint8_t get_inquiry_mode(struct hci_dev *dev)
{
if (dev->features[6] & LMP_EXT_INQ)
return 2;
if (dev->features[3] & LMP_RSSI_INQ)
return 1;
if (dev->manufacturer == 11 &&
dev->hci_rev == 0x00 && dev->lmp_subver == 0x0757)
return 1;
if (dev->manufacturer == 15) {
if (dev->hci_rev == 0x03 && dev->lmp_subver == 0x6963)
return 1;
if (dev->hci_rev == 0x09 && dev->lmp_subver == 0x6963)
return 1;
if (dev->hci_rev == 0x00 && dev->lmp_subver == 0x6965)
return 1;
}
if (dev->manufacturer == 31 &&
dev->hci_rev == 0x2005 && dev->lmp_subver == 0x1805)
return 1;
return 0;
}
static int device_read_bdaddr(uint16_t dev_id, const char *address)
{
int dd, err;
bdaddr_t bdaddr;
dd = hci_open_dev(dev_id);
if (dd < 0) {
err = errno;
error("Can't open device hci%d: %s (%d)",
dev_id, strerror(err), err);
return -err;
}
str2ba(address, &bdaddr);
if (hci_read_bd_addr(dd, &bdaddr, 2000) < 0) {
err = errno;
error("Can't read address for hci%d: %s (%d)",
dev_id, strerror(err), err);
hci_close_dev(dd);
return -err;
}
hci_close_dev(dd);
return 0;
}
static int adapter_setup(struct adapter *adapter, int dd)
{
struct hci_dev *dev = &adapter->dev;
uint8_t events[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00 };
uint8_t inqmode;
bdaddr_t bdaddr;
int err;
char name[249];
if (dev->hci_rev > 1) {
if (dev->features[5] & LMP_SNIFF_SUBR)
events[5] |= 0x20;
if (dev->features[5] & LMP_PAUSE_ENC)
events[5] |= 0x80;
if (dev->features[6] & LMP_EXT_INQ)
events[5] |= 0x40;
if (dev->features[6] & LMP_NFLUSH_PKTS)
events[7] |= 0x01;
if (dev->features[7] & LMP_LSTO)
events[6] |= 0x80;
if (dev->features[6] & LMP_SIMPLE_PAIR) {
events[6] |= 0x01; /* IO Capability Request */
events[6] |= 0x02; /* IO Capability Response */
events[6] |= 0x04; /* User Confirmation Request */
events[6] |= 0x08; /* User Passkey Request */
events[6] |= 0x10; /* Remote OOB Data Request */
events[6] |= 0x20; /* Simple Pairing Complete */
events[7] |= 0x04; /* User Passkey Notification */
events[7] |= 0x08; /* Keypress Notification */
events[7] |= 0x10; /* Remote Host Supported Features Notification */
}
hci_send_cmd(dd, OGF_HOST_CTL, OCF_SET_EVENT_MASK,
sizeof(events), events);
}
str2ba(adapter->address, &bdaddr);
if (read_local_name(&bdaddr, name) == 0) {
memcpy(dev->name, name, 248);
hci_write_local_name(dd, name, 5000);
}
update_ext_inquiry_response(dd, dev);
inqmode = get_inquiry_mode(dev);
if (inqmode < 1)
return 0;
if (hci_write_inquiry_mode(dd, inqmode, 2000) < 0) {
err = errno;
error("Can't write inquiry mode for %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
return 0;
}
static int active_conn_append(GSList **list, bdaddr_t *bdaddr,
uint16_t handle)
{
struct active_conn_info *dev;
dev = g_new0(struct active_conn_info, 1);
bacpy(&dev->bdaddr, bdaddr);
dev->handle = handle;
*list = g_slist_append(*list, dev);
return 0;
}
static void create_stored_records_from_keys(char *key, char *value,
void *user_data)
{
struct record_list *rec_list = user_data;
const gchar *addr = rec_list->addr;
sdp_record_t *rec;
int size, i, len;
uint8_t *pdata;
char tmp[3] = "";
if (strstr(key, addr) == NULL)
return;
size = strlen(value)/2;
pdata = g_malloc0(size);
for (i = 0; i < size; i++) {
memcpy(tmp, value + (i * 2), 2);
pdata[i] = (uint8_t) strtol(tmp, NULL, 16);
}
rec = sdp_extract_pdu(pdata, size, &len);
free(pdata);
rec_list->recs = sdp_list_append(rec_list->recs, rec);
}
static void create_stored_device_from_profiles(char *key, char *value,
void *user_data)
{
char filename[PATH_MAX + 1];
struct adapter *adapter = user_data;
GSList *uuids = bt_string2list(value);
struct btd_device *device;
const gchar *src;
struct record_list rec_list;
if (g_slist_find_custom(adapter->devices,
key, (GCompareFunc) device_address_cmp))
return;
device = device_create(connection, adapter, key);
if (!device)
return;
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
src = adapter->address;
rec_list.addr = device_get_address(device);
rec_list.recs = NULL;
create_name(filename, PATH_MAX, STORAGEDIR, src, "sdp");
textfile_foreach(filename, create_stored_records_from_keys, &rec_list);
device_probe_drivers(device, uuids, rec_list.recs);
if (rec_list.recs != NULL)
sdp_list_free(rec_list.recs, (sdp_free_func_t) sdp_record_free);
g_slist_free(uuids);
}
static void create_stored_device_from_linkkeys(char *key, char *value,
void *user_data)
{
struct adapter *adapter = user_data;
struct btd_device *device;
if (g_slist_find_custom(adapter->devices,
key, (GCompareFunc) device_address_cmp))
return;
device = device_create(connection, adapter, key);
if (device) {
device_set_temporary(device, FALSE);
adapter->devices = g_slist_append(adapter->devices, device);
}
}
static void load_devices(struct adapter *adapter)
{
char filename[PATH_MAX + 1];
create_name(filename, PATH_MAX, STORAGEDIR, adapter->address, "profiles");
textfile_foreach(filename, create_stored_device_from_profiles, adapter);
create_name(filename, PATH_MAX, STORAGEDIR, adapter->address, "linkkeys");
textfile_foreach(filename, create_stored_device_from_linkkeys, adapter);
}
static void load_drivers(struct adapter *adapter)
{
GSList *l;
for (l = adapter_drivers; l; l = l->next) {
struct btd_adapter_driver *driver = l->data;
if (driver->probe)
driver->probe(adapter);
}
}
static void adapter_up(struct adapter *adapter, int dd)
{
struct hci_conn_list_req *cl = NULL;
struct hci_conn_info *ci;
const char *mode;
int i;
adapter->up = 1;
adapter->discov_timeout = get_discoverable_timeout(adapter->dev_id);
adapter->state = DISCOVER_TYPE_NONE;
adapter->scan_mode = get_startup_scan(adapter->dev_id);
hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_SCAN_ENABLE,
1, &adapter->scan_mode);
adapter->mode = get_startup_mode(adapter->dev_id);
if (adapter->mode == MODE_LIMITED)
set_limited_discoverable(dd, adapter->dev.class, TRUE);
/*
* retrieve the active connections: address the scenario where
* the are active connections before the daemon've started
*/
cl = g_malloc0(10 * sizeof(*ci) + sizeof(*cl));
cl->dev_id = adapter->dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(dd, HCIGETCONNLIST, cl) == 0) {
for (i = 0; i < cl->conn_num; i++, ci++)
active_conn_append(&adapter->active_conn,
&ci->bdaddr, ci->handle);
}
g_free(cl);
mode = mode2str(adapter->mode);
dbus_connection_emit_property_changed(connection, adapter->path,
ADAPTER_INTERFACE, "Mode",
DBUS_TYPE_STRING, &mode);
load_drivers(adapter);
load_devices(adapter);
}
int adapter_start(struct adapter *adapter)
{
struct hci_dev *dev = &adapter->dev;
struct hci_dev_info di;
struct hci_version ver;
uint8_t features[8];
int dd, err;
char name[249];
if (hci_devinfo(adapter->dev_id, &di) < 0)
return -errno;
if (hci_test_bit(HCI_RAW, &di.flags)) {
dev->ignore = 1;
return -1;
}
if (bacmp(&di.bdaddr, BDADDR_ANY))
ba2str(&di.bdaddr, adapter->address);
else {
int err = device_read_bdaddr(adapter->dev_id, adapter->address);
if (err < 0)
return err;
}
memcpy(dev->features, di.features, 8);
dd = hci_open_dev(adapter->dev_id);
if (dd < 0) {
err = errno;
error("Can't open adapter %s: %s (%d)",
adapter->path, strerror(err), err);
return -err;
}
if (hci_read_local_version(dd, &ver, 1000) < 0) {
err = errno;
error("Can't read version info for %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
dev->hci_rev = ver.hci_rev;
dev->lmp_ver = ver.lmp_ver;
dev->lmp_subver = ver.lmp_subver;
dev->manufacturer = ver.manufacturer;
if (hci_read_local_features(dd, features, 1000) < 0) {
err = errno;
error("Can't read features for %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
memcpy(dev->features, features, 8);
if (hci_read_class_of_dev(dd, dev->class, 1000) < 0) {
err = errno;
error("Can't read class of adapter on %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
if (hci_read_local_name(dd, sizeof(name), name, 2000) < 0) {
err = errno;
error("Can't read local name on %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
memcpy(dev->name, name, 248);
if (!(features[6] & LMP_SIMPLE_PAIR))
goto setup;
if (ioctl(dd, HCIGETAUTHINFO, NULL) < 0 && errno != EINVAL)
hci_write_simple_pairing_mode(dd, 0x01, 2000);
if (hci_read_simple_pairing_mode(dd, &dev->ssp_mode, 1000) < 0) {
err = errno;
error("Can't read simple pairing mode on %s: %s (%d)",
adapter->path, strerror(err), err);
hci_close_dev(dd);
return -err;
}
setup:
if (hci_test_bit(HCI_INQUIRY, &di.flags))
adapter->state |= STD_INQUIRY;
else
adapter->state &= ~STD_INQUIRY;
adapter_setup(adapter, dd);
adapter_up(adapter, dd);
hci_close_dev(dd);
info("Adapter %s has been enabled", adapter->path);
return 0;
}
static void reply_pending_requests(struct adapter *adapter)
{
DBusMessage *reply;
if (!adapter)
return;
/* pending bonding */
if (adapter->bonding) {
reply = new_authentication_return(adapter->bonding->msg,
HCI_OE_USER_ENDED_CONNECTION);
g_dbus_send_message(connection, reply);
remove_pending_device(adapter);
g_dbus_remove_watch(adapter->bonding->conn,
adapter->bonding->listener_id);
if (adapter->bonding->io_id)
g_source_remove(adapter->bonding->io_id);
g_io_channel_close(adapter->bonding->io);
bonding_request_free(adapter->bonding);
adapter->bonding = NULL;
}
/* If there is a pending reply for discovery cancel */
if (adapter->discovery_cancel) {
reply = dbus_message_new_method_return(adapter->discovery_cancel);
dbus_connection_send(connection, reply, NULL);
dbus_message_unref(reply);
dbus_message_unref(adapter->discovery_cancel);
adapter->discovery_cancel = NULL;
}
if (adapter->state & STD_INQUIRY) {
/* Send discovery completed signal if there isn't name
* to resolve */
g_dbus_emit_signal(connection, adapter->path,
ADAPTER_INTERFACE, "DiscoveryCompleted",
DBUS_TYPE_INVALID);
/* Cancel inquiry initiated by D-Bus client */
if (adapter->discov_requestor)
cancel_discovery(adapter);
}
if (adapter->state & PERIODIC_INQUIRY) {
/* Stop periodic inquiry initiated by D-Bus client */
if (adapter->pdiscov_requestor)
cancel_periodic_discovery(adapter);
}
}
static void unload_drivers(struct adapter *adapter)
{
GSList *l;
for (l = adapter_drivers; l; l = l->next) {
struct btd_adapter_driver *driver = l->data;
if (driver->remove)
driver->remove(adapter);
}
}
int adapter_stop(struct adapter *adapter)
{
const char *mode = "off";
/* cancel pending timeout */
if (adapter->discov_timeout_id) {
g_source_remove(adapter->discov_timeout_id);
adapter->discov_timeout_id = 0;
}
/* check pending requests */
reply_pending_requests(adapter);
if (adapter->discov_requestor) {
g_dbus_remove_watch(connection, adapter->discov_listener);
adapter->discov_listener = 0;
g_free(adapter->discov_requestor);
adapter->discov_requestor = NULL;
}
if (adapter->pdiscov_requestor) {
g_dbus_remove_watch(connection, adapter->pdiscov_listener);
adapter->pdiscov_listener = 0;
g_free(adapter->pdiscov_requestor);
adapter->pdiscov_requestor = NULL;
}
if (adapter->found_devices) {
g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
g_slist_free(adapter->found_devices);
adapter->found_devices = NULL;
}
if (adapter->oor_devices) {
g_slist_foreach(adapter->oor_devices, (GFunc) free, NULL);
g_slist_free(adapter->oor_devices);
adapter->oor_devices = NULL;
}
if (adapter->auth_reqs) {
g_slist_foreach(adapter->auth_reqs, (GFunc) g_free, NULL);
g_slist_free(adapter->auth_reqs);
adapter->auth_reqs = NULL;
}
if (adapter->active_conn) {
g_slist_foreach(adapter->active_conn, (GFunc) g_free, NULL);
g_slist_free(adapter->active_conn);
adapter->active_conn = NULL;
}
dbus_connection_emit_property_changed(connection, adapter->path,
ADAPTER_INTERFACE, "Mode",
DBUS_TYPE_STRING, &mode);
adapter->up = 0;
adapter->scan_mode = SCAN_DISABLED;
adapter->mode = MODE_OFF;
adapter->state = DISCOVER_TYPE_NONE;
unload_drivers(adapter);
info("Adapter %s has been disabled", adapter->path);
return 0;
}
int adapter_update(struct adapter *adapter)
{
struct hci_dev *dev = &adapter->dev;
int dd;
if (dev->ignore)
return 0;
dd = hci_open_dev(adapter->dev_id);
if (dd < 0) {
int err = errno;
error("Can't open adapter %s: %s (%d)",
adapter->path, strerror(err), err);
return -err;
}
update_ext_inquiry_response(dd, dev);
hci_close_dev(dd);
return 0;
}
int adapter_get_class(struct adapter *adapter, uint8_t *cls)
{
struct hci_dev *dev = &adapter->dev;
memcpy(cls, dev->class, 3);
return 0;
}
int adapter_set_class(struct adapter *adapter, uint8_t *cls)
{
struct hci_dev *dev = &adapter->dev;
memcpy(dev->class, cls, 3);
return 0;
}
int adapter_update_ssp_mode(struct adapter *adapter, int dd, uint8_t mode)
{
struct hci_dev *dev = &adapter->dev;
dev->ssp_mode = mode;
update_ext_inquiry_response(dd, dev);
hci_close_dev(dd);
return 0;
}
static void adapter_free(gpointer user_data)
{
struct adapter *adapter = user_data;
g_free(adapter->path);
g_free(adapter);
return;
}
struct adapter *adapter_create(DBusConnection *conn, int id)
{
char path[MAX_PATH_LENGTH];
struct adapter *adapter;
if (!connection)
connection = conn;
snprintf(path, sizeof(path), "%s/hci%d", "/org/bluez", id);
adapter = g_try_new0(struct adapter, 1);
if (!adapter) {
error("Failed to alloc memory to D-Bus path register data (%s)",
path);
return NULL;
}
adapter->dev_id = id;
adapter->state |= RESOLVE_NAME;
adapter->path = g_strdup(path);
if (!g_dbus_register_interface(conn, path, ADAPTER_INTERFACE,
adapter_methods, adapter_signals, NULL,
adapter, adapter_free)) {
error("Adapter interface init failed on path %s", path);
adapter_free(adapter);
return NULL;
}
return adapter;
}
void adapter_remove(struct adapter *adapter)
{
GSList *l;
char *path = g_strdup(adapter->path);
debug("Removing adapter %s", path);
for (l = adapter->devices; l; l = l->next)
device_remove(connection, l->data);
g_slist_free(adapter->devices);
g_dbus_unregister_interface(connection, path, ADAPTER_INTERFACE);
g_free(path);
}
uint16_t adapter_get_dev_id(struct adapter *adapter)
{
return adapter->dev_id;
}
const gchar *adapter_get_path(struct adapter *adapter)
{
if (!adapter)
return NULL;
return adapter->path;
}
const gchar *adapter_get_address(struct adapter *adapter)
{
if (!adapter)
return NULL;
return adapter->address;
}
static gboolean discov_timeout_handler(void *data)
{
struct adapter *adapter = data;
struct hci_request rq;
int dd;
uint8_t scan_enable = adapter->scan_mode;
uint8_t status = 0;
gboolean retval = TRUE;
uint16_t dev_id = adapter->dev_id;
scan_enable &= ~SCAN_INQUIRY;
dd = hci_open_dev(dev_id);
if (dd < 0) {
error("HCI device open failed: hci%d", dev_id);
return TRUE;
}
memset(&rq, 0, sizeof(rq));
rq.ogf = OGF_HOST_CTL;
rq.ocf = OCF_WRITE_SCAN_ENABLE;
rq.cparam = &scan_enable;
rq.clen = sizeof(scan_enable);
rq.rparam = &status;
rq.rlen = sizeof(status);
rq.event = EVT_CMD_COMPLETE;
if (hci_send_req(dd, &rq, 1000) < 0) {
error("Sending write scan enable command to hci%d failed: %s (%d)",
dev_id, strerror(errno), errno);
goto failed;
}
if (status) {
error("Setting scan enable failed with status 0x%02x", status);
goto failed;
}
set_limited_discoverable(dd, adapter->dev.class, FALSE);
adapter_remove_discov_timeout(adapter);
retval = FALSE;
failed:
if (dd >= 0)
hci_close_dev(dd);
return retval;
}
void adapter_set_discov_timeout(struct adapter *adapter, guint interval)
{
if (!adapter)
return;
if (adapter->discov_timeout_id) {
error("Timeout already added for adapter %s", adapter->path);
return;
}
adapter->discov_timeout_id = g_timeout_add(interval, discov_timeout_handler, adapter);
}
void adapter_remove_discov_timeout(struct adapter *adapter)
{
if (!adapter)
return;
if(adapter->discov_timeout_id == 0)
return;
g_source_remove(adapter->discov_timeout_id);
adapter->discov_timeout_id = 0;
}
void adapter_set_scan_mode(struct adapter *adapter, uint8_t scan_mode)
{
if (!adapter)
return;
adapter->scan_mode = scan_mode;
}
uint8_t adapter_get_scan_mode(struct adapter *adapter)
{
return adapter->scan_mode;
}
void adapter_set_mode(struct adapter *adapter, uint8_t mode)
{
if (!adapter)
return;
adapter->mode = mode;
}
uint8_t adapter_get_mode(struct adapter *adapter)
{
return adapter->mode;
}
void adapter_set_state(struct adapter *adapter, int state)
{
if (!adapter)
return;
/* Both Standard and periodic Inquiry are in progress */
if ((state & STD_INQUIRY) && (state & PERIODIC_INQUIRY))
goto set;
if (!adapter->found_devices)
goto set;
/* Free list if standard/periodic inquiry is done */
if ((adapter->state & (STD_INQUIRY | PERIODIC_INQUIRY)) &&
(state & (~STD_INQUIRY | ~PERIODIC_INQUIRY))) {
g_slist_foreach(adapter->found_devices, (GFunc) g_free, NULL);
g_slist_free(adapter->found_devices);
adapter->found_devices = NULL;
}
set:
adapter->state = state;
}
int adapter_get_state(struct adapter *adapter)
{
return adapter->state;
}
struct remote_dev_info *adapter_search_found_devices(struct adapter *adapter,
struct remote_dev_info *match)
{
GSList *l;
l = g_slist_find_custom(adapter->found_devices, match,
(GCompareFunc) found_device_cmp);
if (l)
return l->data;
return NULL;
}
int btd_register_adapter_driver(struct btd_adapter_driver *driver)
{
adapter_drivers = g_slist_append(adapter_drivers, driver);
return 0;
}
void btd_unregister_adapter_driver(struct btd_adapter_driver *driver)
{
adapter_drivers = g_slist_remove(adapter_drivers, driver);
}
static void agent_auth_cb(struct agent *agent, DBusError *derr, void *user_data)
{
struct service_auth *auth = user_data;
auth->cb(derr, auth->user_data);
g_free(auth);
}
int btd_request_authorization(const bdaddr_t *src, const bdaddr_t *dst,
const char *uuid, service_auth_cb cb, void *user_data)
{
struct service_auth *auth;
struct adapter *adapter;
struct btd_device *device;
struct agent *agent;
char address[18];
gboolean trusted;
const gchar *dev_path;
if (src == NULL || dst == NULL)
return -EINVAL;
adapter = manager_find_adapter(src);
if (!adapter)
return -EPERM;
/* Device connected? */
if (!g_slist_find_custom(adapter->active_conn,
dst, active_conn_find_by_bdaddr))
return -ENOTCONN;
ba2str(dst, address);
trusted = read_trust(src, address, GLOBAL_TRUST);
if (trusted) {
cb(NULL, user_data);
return 0;
}
device = adapter_find_device(adapter, address);
if (!device)
return -EPERM;
agent = device_get_agent(device);
if (!agent)
agent = adapter->agent;
if (!agent)
return -EPERM;
auth = g_try_new0(struct service_auth, 1);
if (!auth)
return -ENOMEM;
auth->cb = cb;
auth->user_data = user_data;
dev_path = device_get_path(device);
return agent_authorize(agent, dev_path, uuid, agent_auth_cb, auth);
}
int btd_cancel_authorization(const bdaddr_t *src, const bdaddr_t *dst)
{
struct adapter *adapter = manager_find_adapter(src);
struct btd_device *device;
struct agent *agent;
char address[18];
if (!adapter)
return -EPERM;
ba2str(dst, address);
device = adapter_find_device(adapter, address);
if (!device)
return -EPERM;
/*
* FIXME: Cancel fails if authorization is requested to adapter's
* agent and in the meanwhile CreatePairedDevice is called.
*/
agent = device_get_agent(device);
if (!agent)
agent = adapter->agent;
if (!agent)
return -EPERM;
return agent_cancel(agent);
}
|