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
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
#include <fcntl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <string>
#include <utility>
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/rand_util.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/worker_pool.h"
#include "base/time/time.h"
#include "dbus/file_descriptor.h"
#include "device/bluetooth/dbus/bluez_dbus_manager.h"
#include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_agent_service_provider.h"
#include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_input_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h"
#include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace {
// Default interval between simulated events.
const int kSimulationIntervalMs = 750;
// Minimum and maximum bounds for randomly generated RSSI values.
const int kMinRSSI = -90;
const int kMaxRSSI = -30;
// The default value of connection info properties from GetConnInfo().
const int kUnkownPower = 127;
// This is meant to delay the removal of a pre defined device until the
// developer has time to see it.
const int kVanishingDevicePairTimeMultiplier = 4;
// Meant to delay a pair request for an observable amount of time.
const int kIncomingSimulationPairTimeMultiplier = 45;
// Meant to delay a request that asks for pair requests for an observable
// amount of time.
const int kIncomingSimulationStartPairTimeMultiplier = 30;
// This allows the PIN code dialog to be shown for a long enough time to see
// the PIN code UI in detail.
const int kPinCodeDevicePairTimeMultiplier = 7;
// This allows the pairing dialog to be shown for a long enough time to see
// its UI in detail.
const int kSimulateNormalPairTimeMultiplier = 3;
void SimulatedProfileSocket(int fd) {
// Simulate a server-side socket of a profile; read data from the socket,
// write it back, and then close.
char buf[1024];
ssize_t len;
ssize_t count;
len = read(fd, buf, sizeof buf);
if (len < 0) {
close(fd);
return;
}
count = len;
len = write(fd, buf, count);
if (len < 0) {
close(fd);
return;
}
close(fd);
}
void SimpleErrorCallback(const std::string& error_name,
const std::string& error_message) {
VLOG(1) << "Bluetooth Error: " << error_name << ": " << error_message;
}
} // namespace
namespace bluez {
const char FakeBluetoothDeviceClient::kTestPinCode[] = "123456";
const int FakeBluetoothDeviceClient::kTestPassKey = 123456;
const char FakeBluetoothDeviceClient::kPairingMethodNone[] = "None";
const char FakeBluetoothDeviceClient::kPairingMethodPinCode[] = "PIN Code";
const char FakeBluetoothDeviceClient::kPairingMethodPassKey[] = "PassKey";
const char FakeBluetoothDeviceClient::kPairingActionConfirmation[] =
"Confirmation";
const char FakeBluetoothDeviceClient::kPairingActionDisplay[] = "Display";
const char FakeBluetoothDeviceClient::kPairingActionFail[] = "Fail";
const char FakeBluetoothDeviceClient::kPairingActionRequest[] = "Request";
const char FakeBluetoothDeviceClient::kPairedDevicePath[] = "/fake/hci0/dev0";
const char FakeBluetoothDeviceClient::kPairedDeviceAddress[] =
"00:11:22:33:44:55";
const char FakeBluetoothDeviceClient::kPairedDeviceName[] = "Fake Device";
const uint32 FakeBluetoothDeviceClient::kPairedDeviceClass = 0x000104;
const char FakeBluetoothDeviceClient::kLegacyAutopairPath[] = "/fake/hci0/dev1";
const char FakeBluetoothDeviceClient::kLegacyAutopairAddress[] =
"28:CF:DA:00:00:00";
const char FakeBluetoothDeviceClient::kLegacyAutopairName[] =
"Bluetooth 2.0 Mouse";
const uint32 FakeBluetoothDeviceClient::kLegacyAutopairClass = 0x002580;
const char FakeBluetoothDeviceClient::kDisplayPinCodePath[] = "/fake/hci0/dev2";
const char FakeBluetoothDeviceClient::kDisplayPinCodeAddress[] =
"28:37:37:00:00:00";
const char FakeBluetoothDeviceClient::kDisplayPinCodeName[] =
"Bluetooth 2.0 Keyboard";
const uint32 FakeBluetoothDeviceClient::kDisplayPinCodeClass = 0x002540;
const char FakeBluetoothDeviceClient::kVanishingDevicePath[] =
"/fake/hci0/dev3";
const char FakeBluetoothDeviceClient::kVanishingDeviceAddress[] =
"01:02:03:04:05:06";
const char FakeBluetoothDeviceClient::kVanishingDeviceName[] =
"Vanishing Device";
const uint32 FakeBluetoothDeviceClient::kVanishingDeviceClass = 0x000104;
const char FakeBluetoothDeviceClient::kConnectUnpairablePath[] =
"/fake/hci0/dev4";
const char FakeBluetoothDeviceClient::kConnectUnpairableAddress[] =
"7C:ED:8D:00:00:00";
const char FakeBluetoothDeviceClient::kConnectUnpairableName[] =
"Unpairable Device";
const uint32 FakeBluetoothDeviceClient::kConnectUnpairableClass = 0x002580;
const char FakeBluetoothDeviceClient::kDisplayPasskeyPath[] = "/fake/hci0/dev5";
const char FakeBluetoothDeviceClient::kDisplayPasskeyAddress[] =
"00:0F:F6:00:00:00";
const char FakeBluetoothDeviceClient::kDisplayPasskeyName[] =
"Bluetooth 2.1+ Keyboard";
const uint32 FakeBluetoothDeviceClient::kDisplayPasskeyClass = 0x002540;
const char FakeBluetoothDeviceClient::kRequestPinCodePath[] = "/fake/hci0/dev6";
const char FakeBluetoothDeviceClient::kRequestPinCodeAddress[] =
"00:24:BE:00:00:00";
const char FakeBluetoothDeviceClient::kRequestPinCodeName[] = "PIN Device";
const uint32 FakeBluetoothDeviceClient::kRequestPinCodeClass = 0x240408;
const char FakeBluetoothDeviceClient::kConfirmPasskeyPath[] = "/fake/hci0/dev7";
const char FakeBluetoothDeviceClient::kConfirmPasskeyAddress[] =
"20:7D:74:00:00:00";
const char FakeBluetoothDeviceClient::kConfirmPasskeyName[] = "Phone";
const uint32 FakeBluetoothDeviceClient::kConfirmPasskeyClass = 0x7a020c;
const char FakeBluetoothDeviceClient::kRequestPasskeyPath[] = "/fake/hci0/dev8";
const char FakeBluetoothDeviceClient::kRequestPasskeyAddress[] =
"20:7D:74:00:00:01";
const char FakeBluetoothDeviceClient::kRequestPasskeyName[] = "Passkey Device";
const uint32 FakeBluetoothDeviceClient::kRequestPasskeyClass = 0x7a020c;
const char FakeBluetoothDeviceClient::kUnconnectableDevicePath[] =
"/fake/hci0/dev9";
const char FakeBluetoothDeviceClient::kUnconnectableDeviceAddress[] =
"20:7D:74:00:00:02";
const char FakeBluetoothDeviceClient::kUnconnectableDeviceName[] =
"Unconnectable Device";
const uint32 FakeBluetoothDeviceClient::kUnconnectableDeviceClass = 0x7a020c;
const char FakeBluetoothDeviceClient::kUnpairableDevicePath[] =
"/fake/hci0/devA";
const char FakeBluetoothDeviceClient::kUnpairableDeviceAddress[] =
"20:7D:74:00:00:03";
const char FakeBluetoothDeviceClient::kUnpairableDeviceName[] =
"Unpairable Device";
const uint32 FakeBluetoothDeviceClient::kUnpairableDeviceClass = 0x002540;
const char FakeBluetoothDeviceClient::kJustWorksPath[] = "/fake/hci0/devB";
const char FakeBluetoothDeviceClient::kJustWorksAddress[] = "00:0C:8A:00:00:00";
const char FakeBluetoothDeviceClient::kJustWorksName[] = "Just-Works Device";
const uint32 FakeBluetoothDeviceClient::kJustWorksClass = 0x240428;
const char FakeBluetoothDeviceClient::kLowEnergyPath[] = "/fake/hci0/devC";
const char FakeBluetoothDeviceClient::kLowEnergyAddress[] = "00:1A:11:00:15:30";
const char FakeBluetoothDeviceClient::kLowEnergyName[] =
"Bluetooth 4.0 Heart Rate Monitor";
const uint32 FakeBluetoothDeviceClient::kLowEnergyClass =
0x000918; // Major class "Health", Minor class "Heart/Pulse Rate Monitor."
const char FakeBluetoothDeviceClient::kPairedUnconnectableDevicePath[] =
"/fake/hci0/devD";
const char FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress[] =
"20:7D:74:00:00:04";
const char FakeBluetoothDeviceClient::kPairedUnconnectableDeviceName[] =
"Paired Unconnectable Device";
const uint32 FakeBluetoothDeviceClient::kPairedUnconnectableDeviceClass =
0x000104;
FakeBluetoothDeviceClient::Properties::Properties(
const PropertyChangedCallback& callback)
: BluetoothDeviceClient::Properties(
NULL,
bluetooth_device::kBluetoothDeviceInterface,
callback) {}
FakeBluetoothDeviceClient::Properties::~Properties() {}
void FakeBluetoothDeviceClient::Properties::Get(
dbus::PropertyBase* property,
dbus::PropertySet::GetCallback callback) {
VLOG(1) << "Get " << property->name();
callback.Run(false);
}
void FakeBluetoothDeviceClient::Properties::GetAll() {
VLOG(1) << "GetAll";
}
void FakeBluetoothDeviceClient::Properties::Set(
dbus::PropertyBase* property,
dbus::PropertySet::SetCallback callback) {
VLOG(1) << "Set " << property->name();
if (property->name() == trusted.name()) {
callback.Run(true);
property->ReplaceValueWithSetValue();
} else {
callback.Run(false);
}
}
FakeBluetoothDeviceClient::SimulatedPairingOptions::SimulatedPairingOptions() {}
FakeBluetoothDeviceClient::SimulatedPairingOptions::~SimulatedPairingOptions() {
}
FakeBluetoothDeviceClient::IncomingDeviceProperties::
IncomingDeviceProperties() {}
FakeBluetoothDeviceClient::IncomingDeviceProperties::
~IncomingDeviceProperties() {}
FakeBluetoothDeviceClient::FakeBluetoothDeviceClient()
: simulation_interval_ms_(kSimulationIntervalMs),
discovery_simulation_step_(0),
incoming_pairing_simulation_step_(0),
pairing_cancelled_(false),
connection_rssi_(kUnkownPower),
transmit_power_(kUnkownPower),
max_transmit_power_(kUnkownPower) {
scoped_ptr<Properties> properties(new Properties(
base::Bind(&FakeBluetoothDeviceClient::OnPropertyChanged,
base::Unretained(this), dbus::ObjectPath(kPairedDevicePath))));
properties->address.ReplaceValue(kPairedDeviceAddress);
properties->bluetooth_class.ReplaceValue(kPairedDeviceClass);
properties->name.ReplaceValue("Fake Device (Name)");
properties->alias.ReplaceValue(kPairedDeviceName);
properties->paired.ReplaceValue(true);
properties->trusted.ReplaceValue(true);
properties->adapter.ReplaceValue(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
std::vector<std::string> uuids;
uuids.push_back("00001800-0000-1000-8000-00805f9b34fb");
uuids.push_back("00001801-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(uuids);
properties->modalias.ReplaceValue("usb:v05ACp030Dd0306");
properties_map_.insert(dbus::ObjectPath(kPairedDevicePath),
properties.Pass());
device_list_.push_back(dbus::ObjectPath(kPairedDevicePath));
properties.reset(new Properties(base::Bind(
&FakeBluetoothDeviceClient::OnPropertyChanged, base::Unretained(this),
dbus::ObjectPath(kPairedUnconnectableDevicePath))));
properties->address.ReplaceValue(kPairedUnconnectableDeviceAddress);
properties->bluetooth_class.ReplaceValue(kPairedUnconnectableDeviceClass);
properties->name.ReplaceValue("Fake Device 2 (Unconnectable)");
properties->alias.ReplaceValue(kPairedUnconnectableDeviceName);
properties->paired.ReplaceValue(true);
properties->trusted.ReplaceValue(true);
properties->adapter.ReplaceValue(
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
properties->uuids.ReplaceValue(uuids);
properties->modalias.ReplaceValue("usb:v05ACp030Dd0306");
properties_map_.insert(dbus::ObjectPath(kPairedUnconnectableDevicePath),
properties.Pass());
device_list_.push_back(dbus::ObjectPath(kPairedUnconnectableDevicePath));
}
FakeBluetoothDeviceClient::~FakeBluetoothDeviceClient() {}
void FakeBluetoothDeviceClient::Init(dbus::Bus* bus) {}
void FakeBluetoothDeviceClient::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void FakeBluetoothDeviceClient::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
std::vector<dbus::ObjectPath> FakeBluetoothDeviceClient::GetDevicesForAdapter(
const dbus::ObjectPath& adapter_path) {
if (adapter_path ==
dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath))
return device_list_;
else
return std::vector<dbus::ObjectPath>();
}
FakeBluetoothDeviceClient::Properties* FakeBluetoothDeviceClient::GetProperties(
const dbus::ObjectPath& object_path) {
PropertiesMap::const_iterator iter = properties_map_.find(object_path);
if (iter != properties_map_.end())
return iter->second;
return NULL;
}
FakeBluetoothDeviceClient::SimulatedPairingOptions*
FakeBluetoothDeviceClient::GetPairingOptions(
const dbus::ObjectPath& object_path) {
PairingOptionsMap::const_iterator iter =
pairing_options_map_.find(object_path);
if (iter != pairing_options_map_.end())
return iter->second;
return iter != pairing_options_map_.end() ? iter->second : nullptr;
}
void FakeBluetoothDeviceClient::Connect(const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "Connect: " << object_path.value();
Properties* properties = GetProperties(object_path);
if (properties->connected.value() == true) {
// Already connected.
callback.Run();
return;
}
if (properties->paired.value() != true &&
object_path != dbus::ObjectPath(kConnectUnpairablePath) &&
object_path != dbus::ObjectPath(kLowEnergyPath)) {
// Must be paired.
error_callback.Run(bluetooth_device::kErrorFailed, "Not paired");
return;
} else if (properties->paired.value() == true &&
(object_path == dbus::ObjectPath(kUnconnectableDevicePath) ||
object_path ==
dbus::ObjectPath(kPairedUnconnectableDevicePath))) {
// Must not be paired
error_callback.Run(bluetooth_device::kErrorFailed,
"Connection fails while paired");
return;
}
// The device can be connected.
properties->connected.ReplaceValue(true);
callback.Run();
// Expose GATT services if connected to LE device.
if (object_path == dbus::ObjectPath(kLowEnergyPath)) {
FakeBluetoothGattServiceClient* gatt_service_client =
static_cast<FakeBluetoothGattServiceClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient());
gatt_service_client->ExposeHeartRateService(
dbus::ObjectPath(kLowEnergyPath));
}
AddInputDeviceIfNeeded(object_path, properties);
}
void FakeBluetoothDeviceClient::Disconnect(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "Disconnect: " << object_path.value();
Properties* properties = GetProperties(object_path);
if (!properties->connected.value()) {
error_callback.Run("org.bluez.Error.NotConnected", "Not Connected");
return;
}
// Hide the Heart Rate Service if disconnected from LE device.
if (object_path == dbus::ObjectPath(kLowEnergyPath)) {
FakeBluetoothGattServiceClient* gatt_service_client =
static_cast<FakeBluetoothGattServiceClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient());
gatt_service_client->HideHeartRateService();
}
callback.Run();
properties->connected.ReplaceValue(false);
}
void FakeBluetoothDeviceClient::ConnectProfile(
const dbus::ObjectPath& object_path,
const std::string& uuid,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "ConnectProfile: " << object_path.value() << " " << uuid;
FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client =
static_cast<FakeBluetoothProfileManagerClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothProfileManagerClient());
FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(uuid);
if (profile_service_provider == NULL) {
error_callback.Run(kNoResponseError, "Missing profile");
return;
}
if (object_path == dbus::ObjectPath(kPairedUnconnectableDevicePath)) {
error_callback.Run(bluetooth_device::kErrorFailed, "unconnectable");
return;
}
// Make a socket pair of a compatible type with the type used by Bluetooth;
// spin up a thread to simulate the server side and wrap the client side in
// a D-Bus file descriptor object.
int socket_type = SOCK_STREAM;
if (uuid == FakeBluetoothProfileManagerClient::kL2capUuid)
socket_type = SOCK_SEQPACKET;
int fds[2];
if (socketpair(AF_UNIX, socket_type, 0, fds) < 0) {
error_callback.Run(kNoResponseError, "socketpair call failed");
return;
}
int args;
args = fcntl(fds[1], F_GETFL, NULL);
if (args < 0) {
error_callback.Run(kNoResponseError, "failed to get socket flags");
return;
}
args |= O_NONBLOCK;
if (fcntl(fds[1], F_SETFL, args) < 0) {
error_callback.Run(kNoResponseError, "failed to set socket non-blocking");
return;
}
base::WorkerPool::GetTaskRunner(false)
->PostTask(FROM_HERE, base::Bind(&SimulatedProfileSocket, fds[0]));
scoped_ptr<dbus::FileDescriptor> fd(new dbus::FileDescriptor(fds[1]));
// Post the new connection to the service provider.
BluetoothProfileServiceProvider::Delegate::Options options;
profile_service_provider->NewConnection(
object_path, fd.Pass(), options,
base::Bind(&FakeBluetoothDeviceClient::ConnectionCallback,
base::Unretained(this), object_path, callback,
error_callback));
}
void FakeBluetoothDeviceClient::DisconnectProfile(
const dbus::ObjectPath& object_path,
const std::string& uuid,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "DisconnectProfile: " << object_path.value() << " " << uuid;
FakeBluetoothProfileManagerClient* fake_bluetooth_profile_manager_client =
static_cast<FakeBluetoothProfileManagerClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothProfileManagerClient());
FakeBluetoothProfileServiceProvider* profile_service_provider =
fake_bluetooth_profile_manager_client->GetProfileServiceProvider(uuid);
if (profile_service_provider == NULL) {
error_callback.Run(kNoResponseError, "Missing profile");
return;
}
profile_service_provider->RequestDisconnection(
object_path, base::Bind(&FakeBluetoothDeviceClient::DisconnectionCallback,
base::Unretained(this), object_path, callback,
error_callback));
}
void FakeBluetoothDeviceClient::Pair(const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "Pair: " << object_path.value();
Properties* properties = GetProperties(object_path);
if (properties->paired.value() == true) {
// Already paired.
callback.Run();
return;
}
SimulatePairing(object_path, false, callback, error_callback);
}
void FakeBluetoothDeviceClient::CancelPairing(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "CancelPairing: " << object_path.value();
pairing_cancelled_ = true;
callback.Run();
}
void FakeBluetoothDeviceClient::GetConnInfo(
const dbus::ObjectPath& object_path,
const ConnInfoCallback& callback,
const ErrorCallback& error_callback) {
Properties* properties = GetProperties(object_path);
if (!properties->connected.value()) {
error_callback.Run("org.bluez.Error.NotConnected", "Not Connected");
return;
}
callback.Run(connection_rssi_, transmit_power_, max_transmit_power_);
}
void FakeBluetoothDeviceClient::BeginDiscoverySimulation(
const dbus::ObjectPath& adapter_path) {
VLOG(1) << "starting discovery simulation";
discovery_simulation_step_ = 1;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
void FakeBluetoothDeviceClient::EndDiscoverySimulation(
const dbus::ObjectPath& adapter_path) {
VLOG(1) << "stopping discovery simulation";
discovery_simulation_step_ = 0;
}
void FakeBluetoothDeviceClient::BeginIncomingPairingSimulation(
const dbus::ObjectPath& adapter_path) {
VLOG(1) << "starting incoming pairing simulation";
incoming_pairing_simulation_step_ = 1;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::IncomingPairingSimulationTimer,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(
kIncomingSimulationStartPairTimeMultiplier *
simulation_interval_ms_));
}
void FakeBluetoothDeviceClient::EndIncomingPairingSimulation(
const dbus::ObjectPath& adapter_path) {
VLOG(1) << "stopping incoming pairing simulation";
incoming_pairing_simulation_step_ = 0;
}
void FakeBluetoothDeviceClient::SetSimulationIntervalMs(int interval_ms) {
simulation_interval_ms_ = interval_ms;
}
void FakeBluetoothDeviceClient::CreateDevice(
const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& device_path) {
if (std::find(device_list_.begin(), device_list_.end(), device_path) !=
device_list_.end())
return;
scoped_ptr<Properties> properties(
new Properties(base::Bind(&FakeBluetoothDeviceClient::OnPropertyChanged,
base::Unretained(this), device_path)));
properties->adapter.ReplaceValue(adapter_path);
if (device_path == dbus::ObjectPath(kLegacyAutopairPath)) {
properties->address.ReplaceValue(kLegacyAutopairAddress);
properties->bluetooth_class.ReplaceValue(kLegacyAutopairClass);
properties->name.ReplaceValue("LegacyAutopair");
properties->alias.ReplaceValue(kLegacyAutopairName);
std::vector<std::string> uuids;
uuids.push_back("00001124-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(uuids);
} else if (device_path == dbus::ObjectPath(kDisplayPinCodePath)) {
properties->address.ReplaceValue(kDisplayPinCodeAddress);
properties->bluetooth_class.ReplaceValue(kDisplayPinCodeClass);
properties->name.ReplaceValue("DisplayPinCode");
properties->alias.ReplaceValue(kDisplayPinCodeName);
std::vector<std::string> uuids;
uuids.push_back("00001124-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(uuids);
} else if (device_path == dbus::ObjectPath(kVanishingDevicePath)) {
properties->address.ReplaceValue(kVanishingDeviceAddress);
properties->bluetooth_class.ReplaceValue(kVanishingDeviceClass);
properties->name.ReplaceValue("VanishingDevice");
properties->alias.ReplaceValue(kVanishingDeviceName);
} else if (device_path == dbus::ObjectPath(kConnectUnpairablePath)) {
properties->address.ReplaceValue(kConnectUnpairableAddress);
properties->bluetooth_class.ReplaceValue(kConnectUnpairableClass);
properties->name.ReplaceValue("ConnectUnpairable");
properties->alias.ReplaceValue(kConnectUnpairableName);
std::vector<std::string> uuids;
uuids.push_back("00001124-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(uuids);
} else if (device_path == dbus::ObjectPath(kDisplayPasskeyPath)) {
properties->address.ReplaceValue(kDisplayPasskeyAddress);
properties->bluetooth_class.ReplaceValue(kDisplayPasskeyClass);
properties->name.ReplaceValue("DisplayPasskey");
properties->alias.ReplaceValue(kDisplayPasskeyName);
std::vector<std::string> uuids;
uuids.push_back("00001124-0000-1000-8000-00805f9b34fb");
properties->uuids.ReplaceValue(uuids);
} else if (device_path == dbus::ObjectPath(kRequestPinCodePath)) {
properties->address.ReplaceValue(kRequestPinCodeAddress);
properties->bluetooth_class.ReplaceValue(kRequestPinCodeClass);
properties->name.ReplaceValue("RequestPinCode");
properties->alias.ReplaceValue(kRequestPinCodeName);
} else if (device_path == dbus::ObjectPath(kConfirmPasskeyPath)) {
properties->address.ReplaceValue(kConfirmPasskeyAddress);
properties->bluetooth_class.ReplaceValue(kConfirmPasskeyClass);
properties->name.ReplaceValue("ConfirmPasskey");
properties->alias.ReplaceValue(kConfirmPasskeyName);
} else if (device_path == dbus::ObjectPath(kRequestPasskeyPath)) {
properties->address.ReplaceValue(kRequestPasskeyAddress);
properties->bluetooth_class.ReplaceValue(kRequestPasskeyClass);
properties->name.ReplaceValue("RequestPasskey");
properties->alias.ReplaceValue(kRequestPasskeyName);
} else if (device_path == dbus::ObjectPath(kUnconnectableDevicePath)) {
properties->address.ReplaceValue(kUnconnectableDeviceAddress);
properties->bluetooth_class.ReplaceValue(kUnconnectableDeviceClass);
properties->name.ReplaceValue("UnconnectableDevice");
properties->alias.ReplaceValue(kUnconnectableDeviceName);
} else if (device_path == dbus::ObjectPath(kUnpairableDevicePath)) {
properties->address.ReplaceValue(kUnpairableDeviceAddress);
properties->bluetooth_class.ReplaceValue(kUnpairableDeviceClass);
properties->name.ReplaceValue("Fake Unpairable Device");
properties->alias.ReplaceValue(kUnpairableDeviceName);
} else if (device_path == dbus::ObjectPath(kJustWorksPath)) {
properties->address.ReplaceValue(kJustWorksAddress);
properties->bluetooth_class.ReplaceValue(kJustWorksClass);
properties->name.ReplaceValue("JustWorks");
properties->alias.ReplaceValue(kJustWorksName);
} else if (device_path == dbus::ObjectPath(kLowEnergyPath)) {
properties->address.ReplaceValue(kLowEnergyAddress);
properties->bluetooth_class.ReplaceValue(kLowEnergyClass);
properties->name.ReplaceValue("Heart Rate Monitor");
properties->alias.ReplaceValue(kLowEnergyName);
std::vector<std::string> uuids;
uuids.push_back(FakeBluetoothGattServiceClient::kHeartRateServiceUUID);
properties->uuids.ReplaceValue(uuids);
} else {
NOTREACHED();
}
properties_map_.insert(device_path, properties.Pass());
device_list_.push_back(device_path);
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DeviceAdded(device_path));
}
void FakeBluetoothDeviceClient::CreateDeviceWithProperties(
const dbus::ObjectPath& adapter_path,
const IncomingDeviceProperties& props) {
dbus::ObjectPath device_path(props.device_path);
if (std::find(device_list_.begin(), device_list_.end(), device_path) !=
device_list_.end())
return;
scoped_ptr<Properties> properties(
new Properties(base::Bind(&FakeBluetoothDeviceClient::OnPropertyChanged,
base::Unretained(this), device_path)));
properties->adapter.ReplaceValue(adapter_path);
properties->name.ReplaceValue(props.device_name);
properties->alias.ReplaceValue(props.device_alias);
properties->address.ReplaceValue(props.device_address);
properties->bluetooth_class.ReplaceValue(props.device_class);
properties->trusted.ReplaceValue(props.is_trusted);
if (props.is_trusted)
properties->paired.ReplaceValue(true);
scoped_ptr<SimulatedPairingOptions> options(new SimulatedPairingOptions);
options->pairing_method = props.pairing_method;
options->pairing_auth_token = props.pairing_auth_token;
options->pairing_action = props.pairing_action;
options->incoming = props.incoming;
properties_map_.insert(device_path, properties.Pass());
device_list_.push_back(device_path);
pairing_options_map_.insert(device_path, options.Pass());
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DeviceAdded(device_path));
}
scoped_ptr<base::ListValue>
FakeBluetoothDeviceClient::GetBluetoothDevicesAsDictionaries() const {
scoped_ptr<base::ListValue> predefined_devices(new base::ListValue);
scoped_ptr<base::DictionaryValue> pairedDevice(new base::DictionaryValue);
pairedDevice->SetString("path", kPairedDevicePath);
pairedDevice->SetString("address", kPairedDeviceAddress);
pairedDevice->SetString("name", kPairedDeviceName);
pairedDevice->SetString("alias", kPairedDeviceName);
pairedDevice->SetString("pairingMethod", "");
pairedDevice->SetString("pairingAuthToken", "");
pairedDevice->SetString("pairingAction", "");
pairedDevice->SetInteger("classValue", kPairedDeviceClass);
pairedDevice->SetBoolean("discoverable", true);
pairedDevice->SetBoolean("isTrusted", true);
pairedDevice->SetBoolean("paired", true);
pairedDevice->SetBoolean("incoming", false);
predefined_devices->Append(pairedDevice.Pass());
scoped_ptr<base::DictionaryValue> legacyDevice(new base::DictionaryValue);
legacyDevice->SetString("path", kLegacyAutopairPath);
legacyDevice->SetString("address", kLegacyAutopairAddress);
legacyDevice->SetString("name", kLegacyAutopairName);
legacyDevice->SetString("alias", kLegacyAutopairName);
legacyDevice->SetString("pairingMethod", "");
legacyDevice->SetString("pairingAuthToken", "");
legacyDevice->SetString("pairingAction", "");
legacyDevice->SetInteger("classValue", kLegacyAutopairClass);
legacyDevice->SetBoolean("isTrusted", true);
legacyDevice->SetBoolean("discoverable", false);
legacyDevice->SetBoolean("paired", false);
legacyDevice->SetBoolean("incoming", false);
predefined_devices->Append(legacyDevice.Pass());
scoped_ptr<base::DictionaryValue> pin(new base::DictionaryValue);
pin->SetString("path", kDisplayPinCodePath);
pin->SetString("address", kDisplayPinCodeAddress);
pin->SetString("name", kDisplayPinCodeName);
pin->SetString("alias", kDisplayPinCodeName);
pin->SetString("pairingMethod", kPairingMethodPinCode);
pin->SetString("pairingAuthToken", kTestPinCode);
pin->SetString("pairingAction", kPairingActionDisplay);
pin->SetInteger("classValue", kDisplayPinCodeClass);
pin->SetBoolean("isTrusted", false);
pin->SetBoolean("discoverable", false);
pin->SetBoolean("paired", false);
pin->SetBoolean("incoming", false);
predefined_devices->Append(pin.Pass());
scoped_ptr<base::DictionaryValue> vanishing(new base::DictionaryValue);
vanishing->SetString("path", kVanishingDevicePath);
vanishing->SetString("address", kVanishingDeviceAddress);
vanishing->SetString("name", kVanishingDeviceName);
vanishing->SetString("alias", kVanishingDeviceName);
vanishing->SetString("pairingMethod", "");
vanishing->SetString("pairingAuthToken", "");
vanishing->SetString("pairingAction", "");
vanishing->SetInteger("classValue", kVanishingDeviceClass);
vanishing->SetBoolean("isTrusted", false);
vanishing->SetBoolean("discoverable", false);
vanishing->SetBoolean("paired", false);
vanishing->SetBoolean("incoming", false);
predefined_devices->Append(vanishing.Pass());
scoped_ptr<base::DictionaryValue> connect_unpairable(
new base::DictionaryValue);
connect_unpairable->SetString("path", kConnectUnpairablePath);
connect_unpairable->SetString("address", kConnectUnpairableAddress);
connect_unpairable->SetString("name", kConnectUnpairableName);
connect_unpairable->SetString("pairingMethod", "");
connect_unpairable->SetString("pairingAuthToken", "");
connect_unpairable->SetString("pairingAction", "");
connect_unpairable->SetString("alias", kConnectUnpairableName);
connect_unpairable->SetInteger("classValue", kConnectUnpairableClass);
connect_unpairable->SetBoolean("isTrusted", false);
connect_unpairable->SetBoolean("discoverable", false);
connect_unpairable->SetBoolean("paired", false);
connect_unpairable->SetBoolean("incoming", false);
predefined_devices->Append(connect_unpairable.Pass());
scoped_ptr<base::DictionaryValue> passkey(new base::DictionaryValue);
passkey->SetString("path", kDisplayPasskeyPath);
passkey->SetString("address", kDisplayPasskeyAddress);
passkey->SetString("name", kDisplayPasskeyName);
passkey->SetString("alias", kDisplayPasskeyName);
passkey->SetString("pairingMethod", kPairingMethodPassKey);
passkey->SetInteger("pairingAuthToken", kTestPassKey);
passkey->SetString("pairingAction", kPairingActionDisplay);
passkey->SetInteger("classValue", kDisplayPasskeyClass);
passkey->SetBoolean("isTrusted", false);
passkey->SetBoolean("discoverable", false);
passkey->SetBoolean("paired", false);
passkey->SetBoolean("incoming", false);
predefined_devices->Append(passkey.Pass());
scoped_ptr<base::DictionaryValue> request_pin(new base::DictionaryValue);
request_pin->SetString("path", kRequestPinCodePath);
request_pin->SetString("address", kRequestPinCodeAddress);
request_pin->SetString("name", kRequestPinCodeName);
request_pin->SetString("alias", kRequestPinCodeName);
request_pin->SetString("pairingMethod", "");
request_pin->SetString("pairingAuthToken", "");
request_pin->SetString("pairingAction", kPairingActionRequest);
request_pin->SetInteger("classValue", kRequestPinCodeClass);
request_pin->SetBoolean("isTrusted", false);
request_pin->SetBoolean("discoverable", false);
request_pin->SetBoolean("paired", false);
request_pin->SetBoolean("incoming", false);
predefined_devices->Append(request_pin.Pass());
scoped_ptr<base::DictionaryValue> confirm(new base::DictionaryValue);
confirm->SetString("path", kConfirmPasskeyPath);
confirm->SetString("address", kConfirmPasskeyAddress);
confirm->SetString("name", kConfirmPasskeyName);
confirm->SetString("alias", kConfirmPasskeyName);
confirm->SetString("pairingMethod", "");
confirm->SetInteger("pairingAuthToken", kTestPassKey);
confirm->SetString("pairingAction", kPairingActionConfirmation);
confirm->SetInteger("classValue", kConfirmPasskeyClass);
confirm->SetBoolean("isTrusted", false);
confirm->SetBoolean("discoverable", false);
confirm->SetBoolean("paired", false);
confirm->SetBoolean("incoming", false);
predefined_devices->Append(confirm.Pass());
scoped_ptr<base::DictionaryValue> request_passkey(new base::DictionaryValue);
request_passkey->SetString("path", kRequestPasskeyPath);
request_passkey->SetString("address", kRequestPasskeyAddress);
request_passkey->SetString("name", kRequestPasskeyName);
request_passkey->SetString("alias", kRequestPasskeyName);
request_passkey->SetString("pairingMethod", kPairingMethodPassKey);
request_passkey->SetString("pairingAction", kPairingActionRequest);
request_passkey->SetInteger("pairingAuthToken", kTestPassKey);
request_passkey->SetInteger("classValue", kRequestPasskeyClass);
request_passkey->SetBoolean("isTrusted", false);
request_passkey->SetBoolean("discoverable", false);
request_passkey->SetBoolean("paired", false);
request_passkey->SetBoolean("incoming", false);
predefined_devices->Append(request_passkey.Pass());
scoped_ptr<base::DictionaryValue> unconnectable(new base::DictionaryValue);
unconnectable->SetString("path", kUnconnectableDevicePath);
unconnectable->SetString("address", kUnconnectableDeviceAddress);
unconnectable->SetString("name", kUnconnectableDeviceName);
unconnectable->SetString("alias", kUnconnectableDeviceName);
unconnectable->SetString("pairingMethod", "");
unconnectable->SetString("pairingAuthToken", "");
unconnectable->SetString("pairingAction", "");
unconnectable->SetInteger("classValue", kUnconnectableDeviceClass);
unconnectable->SetBoolean("isTrusted", true);
unconnectable->SetBoolean("discoverable", false);
unconnectable->SetBoolean("paired", false);
unconnectable->SetBoolean("incoming", false);
predefined_devices->Append(unconnectable.Pass());
scoped_ptr<base::DictionaryValue> unpairable(new base::DictionaryValue);
unpairable->SetString("path", kUnpairableDevicePath);
unpairable->SetString("address", kUnpairableDeviceAddress);
unpairable->SetString("name", kUnpairableDeviceName);
unpairable->SetString("alias", kUnpairableDeviceName);
unpairable->SetString("pairingMethod", "");
unpairable->SetString("pairingAuthToken", "");
unpairable->SetString("pairingAction", kPairingActionFail);
unpairable->SetInteger("classValue", kUnpairableDeviceClass);
unpairable->SetBoolean("isTrusted", false);
unpairable->SetBoolean("discoverable", false);
unpairable->SetBoolean("paired", false);
unpairable->SetBoolean("incoming", false);
predefined_devices->Append(unpairable.Pass());
scoped_ptr<base::DictionaryValue> just_works(new base::DictionaryValue);
just_works->SetString("path", kJustWorksPath);
just_works->SetString("address", kJustWorksAddress);
just_works->SetString("name", kJustWorksName);
just_works->SetString("alias", kJustWorksName);
just_works->SetString("pairingMethod", "");
just_works->SetString("pairingAuthToken", "");
just_works->SetString("pairingAction", "");
just_works->SetInteger("classValue", kJustWorksClass);
just_works->SetBoolean("isTrusted", false);
just_works->SetBoolean("discoverable", false);
just_works->SetBoolean("paired", false);
just_works->SetBoolean("incoming", false);
predefined_devices->Append(just_works.Pass());
scoped_ptr<base::DictionaryValue> low_energy(new base::DictionaryValue);
low_energy->SetString("path", kLowEnergyPath);
low_energy->SetString("address", kLowEnergyAddress);
low_energy->SetString("name", kLowEnergyName);
low_energy->SetString("alias", kLowEnergyName);
low_energy->SetString("pairingMethod", "");
low_energy->SetString("pairingAuthToken", "");
low_energy->SetString("pairingAction", "");
low_energy->SetInteger("classValue", kLowEnergyClass);
low_energy->SetBoolean("isTrusted", false);
low_energy->SetBoolean("discoverable", false);
low_energy->SetBoolean("paireed", false);
low_energy->SetBoolean("incoming", false);
predefined_devices->Append(low_energy.Pass());
scoped_ptr<base::DictionaryValue> paired_unconnectable(
new base::DictionaryValue);
paired_unconnectable->SetString("path", kPairedUnconnectableDevicePath);
paired_unconnectable->SetString("address", kPairedUnconnectableDeviceAddress);
paired_unconnectable->SetString("name", kPairedUnconnectableDeviceName);
paired_unconnectable->SetString("pairingMethod", "");
paired_unconnectable->SetString("pairingAuthToken", "");
paired_unconnectable->SetString("pairingAction", "");
paired_unconnectable->SetString("alias", kPairedUnconnectableDeviceName);
paired_unconnectable->SetInteger("classValue",
kPairedUnconnectableDeviceClass);
paired_unconnectable->SetBoolean("isTrusted", false);
paired_unconnectable->SetBoolean("discoverable", true);
paired_unconnectable->SetBoolean("paired", true);
paired_unconnectable->SetBoolean("incoming", false);
predefined_devices->Append(paired_unconnectable.Pass());
return predefined_devices.Pass();
}
void FakeBluetoothDeviceClient::RemoveDevice(
const dbus::ObjectPath& adapter_path,
const dbus::ObjectPath& device_path) {
std::vector<dbus::ObjectPath>::iterator listiter =
std::find(device_list_.begin(), device_list_.end(), device_path);
if (listiter == device_list_.end())
return;
PropertiesMap::const_iterator iter = properties_map_.find(device_path);
Properties* properties = iter->second;
VLOG(1) << "removing device: " << properties->alias.value();
device_list_.erase(listiter);
// Remove the Input interface if it exists. This should be called before the
// BluetoothDeviceClient::Observer::DeviceRemoved because it deletes the
// BluetoothDeviceChromeOS object, including the device_path referenced here.
FakeBluetoothInputClient* fake_bluetooth_input_client =
static_cast<FakeBluetoothInputClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothInputClient());
fake_bluetooth_input_client->RemoveInputDevice(device_path);
if (device_path == dbus::ObjectPath(kLowEnergyPath)) {
FakeBluetoothGattServiceClient* gatt_service_client =
static_cast<FakeBluetoothGattServiceClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient());
gatt_service_client->HideHeartRateService();
}
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DeviceRemoved(device_path));
properties_map_.erase(iter);
PairingOptionsMap::const_iterator options_iter =
pairing_options_map_.find(device_path);
if (options_iter != pairing_options_map_.end()) {
pairing_options_map_.erase(options_iter);
}
}
void FakeBluetoothDeviceClient::OnPropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) {
VLOG(2) << "Fake Bluetooth device property changed: " << object_path.value()
<< ": " << property_name;
FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_,
DevicePropertyChanged(object_path, property_name));
}
void FakeBluetoothDeviceClient::DiscoverySimulationTimer() {
if (!discovery_simulation_step_)
return;
// Timer fires every .75s, the numbers below are arbitrary to give a feel
// for a discovery process.
VLOG(1) << "discovery simulation, step " << discovery_simulation_step_;
if (discovery_simulation_step_ == 2) {
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kLegacyAutopairPath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kLowEnergyPath));
} else if (discovery_simulation_step_ == 4) {
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kDisplayPinCodePath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kVanishingDevicePath));
} else if (discovery_simulation_step_ == 7) {
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kConnectUnpairablePath));
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
} else if (discovery_simulation_step_ == 8) {
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kDisplayPasskeyPath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kRequestPinCodePath));
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
} else if (discovery_simulation_step_ == 10) {
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kConfirmPasskeyPath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kRequestPasskeyPath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kUnconnectableDevicePath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kUnpairableDevicePath));
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kJustWorksPath));
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
} else if (discovery_simulation_step_ == 13) {
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
RemoveDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kVanishingDevicePath));
} else if (discovery_simulation_step_ == 14) {
UpdateDeviceRSSI(dbus::ObjectPath(kLowEnergyPath),
base::RandInt(kMinRSSI, kMaxRSSI));
return;
}
++discovery_simulation_step_;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
void FakeBluetoothDeviceClient::IncomingPairingSimulationTimer() {
if (!incoming_pairing_simulation_step_)
return;
VLOG(1) << "incoming pairing simulation, step "
<< incoming_pairing_simulation_step_;
switch (incoming_pairing_simulation_step_) {
case 1:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kConfirmPasskeyPath));
SimulatePairing(dbus::ObjectPath(kConfirmPasskeyPath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
case 2:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kJustWorksPath));
SimulatePairing(dbus::ObjectPath(kJustWorksPath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
case 3:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kDisplayPinCodePath));
SimulatePairing(dbus::ObjectPath(kDisplayPinCodePath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
case 4:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kDisplayPasskeyPath));
SimulatePairing(dbus::ObjectPath(kDisplayPasskeyPath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
case 5:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kRequestPinCodePath));
SimulatePairing(dbus::ObjectPath(kRequestPinCodePath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
case 6:
CreateDevice(dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
dbus::ObjectPath(kRequestPasskeyPath));
SimulatePairing(dbus::ObjectPath(kRequestPasskeyPath), true,
base::Bind(&base::DoNothing),
base::Bind(&SimpleErrorCallback));
break;
default:
return;
}
++incoming_pairing_simulation_step_;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::IncomingPairingSimulationTimer,
base::Unretained(this)),
base::TimeDelta::FromMilliseconds(kIncomingSimulationPairTimeMultiplier *
simulation_interval_ms_));
}
void FakeBluetoothDeviceClient::SimulatePairing(
const dbus::ObjectPath& object_path,
bool incoming_request,
const base::Closure& callback,
const ErrorCallback& error_callback) {
pairing_cancelled_ = false;
FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client =
static_cast<FakeBluetoothAgentManagerClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothAgentManagerClient());
FakeBluetoothAgentServiceProvider* agent_service_provider =
fake_bluetooth_agent_manager_client->GetAgentServiceProvider();
CHECK(agent_service_provider != NULL);
// Grab the device's pairing properties.
PairingOptionsMap::const_iterator iter =
pairing_options_map_.find(object_path);
// If the device with path |object_path| has simulated pairing properties
// defined, then pair it based on its |pairing_method|.
if (iter != pairing_options_map_.end()) {
if (iter->second->pairing_action == kPairingActionFail) {
// Fails the pairing with an org.bluez.Error.Failed error.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::FailSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (iter->second->pairing_method == kPairingMethodNone ||
iter->second->pairing_method.empty()) {
if (!iter->second->incoming) {
// Simply pair and connect the device.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(
kSimulateNormalPairTimeMultiplier * simulation_interval_ms_));
} else {
agent_service_provider->RequestAuthorization(
object_path,
base::Bind(&FakeBluetoothDeviceClient::ConfirmationCallback,
base::Unretained(this), object_path, callback,
error_callback));
}
} else if (iter->second->pairing_method == kPairingMethodPinCode) {
if (iter->second->pairing_action == kPairingActionDisplay) {
// Display a Pincode, and wait before acting as if the other end
// accepted it.
agent_service_provider->DisplayPinCode(
object_path, iter->second->pairing_auth_token);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kPinCodeDevicePairTimeMultiplier *
simulation_interval_ms_));
} else if (iter->second->pairing_action == kPairingActionRequest) {
// Request a pin code.
agent_service_provider->RequestPinCode(
object_path, base::Bind(&FakeBluetoothDeviceClient::PinCodeCallback,
base::Unretained(this), object_path,
callback, error_callback));
} else if (iter->second->pairing_action == kPairingActionConfirmation) {
error_callback.Run(kNoResponseError, "No confirm for pincode pairing.");
}
} else if (iter->second->pairing_method == kPairingMethodPassKey) {
// Display a passkey, and each interval act as if another key was entered
// for it.
if (iter->second->pairing_action == kPairingActionDisplay) {
agent_service_provider->DisplayPasskey(
object_path, std::stoi(iter->second->pairing_auth_token), 0);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::Bind(&FakeBluetoothDeviceClient::SimulateKeypress,
base::Unretained(this), 1, object_path,
callback, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (iter->second->pairing_action == kPairingActionRequest) {
agent_service_provider->RequestPasskey(
object_path, base::Bind(&FakeBluetoothDeviceClient::PasskeyCallback,
base::Unretained(this), object_path,
callback, error_callback));
} else if (iter->second->pairing_action == kPairingActionConfirmation) {
agent_service_provider->RequestConfirmation(
object_path, std::stoi(iter->second->pairing_auth_token),
base::Bind(&FakeBluetoothDeviceClient::ConfirmationCallback,
base::Unretained(this), object_path, callback,
error_callback));
}
}
} else {
if (object_path == dbus::ObjectPath(kLegacyAutopairPath) ||
object_path == dbus::ObjectPath(kConnectUnpairablePath) ||
object_path == dbus::ObjectPath(kUnconnectableDevicePath) ||
object_path == dbus::ObjectPath(kLowEnergyPath)) {
// No need to call anything on the pairing delegate, just wait 3 times
// the interval before acting as if the other end accepted it.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kSimulateNormalPairTimeMultiplier *
simulation_interval_ms_));
} else if (object_path == dbus::ObjectPath(kDisplayPinCodePath)) {
// Display a Pincode, and wait before acting as if the other end accepted
// it.
agent_service_provider->DisplayPinCode(object_path, kTestPinCode);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kPinCodeDevicePairTimeMultiplier *
simulation_interval_ms_));
} else if (object_path == dbus::ObjectPath(kVanishingDevicePath)) {
// The vanishing device simulates being too far away, and thus times out.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::TimeoutSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(kVanishingDevicePairTimeMultiplier *
simulation_interval_ms_));
} else if (object_path == dbus::ObjectPath(kDisplayPasskeyPath)) {
// Display a passkey, and each interval act as if another key was entered
// for it.
agent_service_provider->DisplayPasskey(object_path, kTestPassKey, 0);
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::Bind(&FakeBluetoothDeviceClient::SimulateKeypress,
base::Unretained(this), 1, object_path,
callback, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (object_path == dbus::ObjectPath(kRequestPinCodePath)) {
// Request a Pincode.
agent_service_provider->RequestPinCode(
object_path, base::Bind(&FakeBluetoothDeviceClient::PinCodeCallback,
base::Unretained(this), object_path, callback,
error_callback));
} else if (object_path == dbus::ObjectPath(kConfirmPasskeyPath)) {
// Request confirmation of a Passkey.
agent_service_provider->RequestConfirmation(
object_path, kTestPassKey,
base::Bind(&FakeBluetoothDeviceClient::ConfirmationCallback,
base::Unretained(this), object_path, callback,
error_callback));
} else if (object_path == dbus::ObjectPath(kRequestPasskeyPath)) {
// Request a Passkey from the user.
agent_service_provider->RequestPasskey(
object_path, base::Bind(&FakeBluetoothDeviceClient::PasskeyCallback,
base::Unretained(this), object_path, callback,
error_callback));
} else if (object_path == dbus::ObjectPath(kUnpairableDevicePath)) {
// Fails the pairing with an org.bluez.Error.Failed error.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::FailSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (object_path == dbus::ObjectPath(kJustWorksPath)) {
if (incoming_request) {
agent_service_provider->RequestAuthorization(
object_path,
base::Bind(&FakeBluetoothDeviceClient::ConfirmationCallback,
base::Unretained(this), object_path, callback,
error_callback));
} else {
// No need to call anything on the pairing delegate, just wait before
// acting as if the other end accepted it.
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(
kSimulateNormalPairTimeMultiplier * simulation_interval_ms_));
}
} else {
error_callback.Run(kNoResponseError, "No pairing fake");
}
}
}
void FakeBluetoothDeviceClient::CompleteSimulatedPairing(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "CompleteSimulatedPairing: " << object_path.value();
if (pairing_cancelled_) {
pairing_cancelled_ = false;
error_callback.Run(bluetooth_device::kErrorAuthenticationCanceled,
"Cancelled");
} else {
Properties* properties = GetProperties(object_path);
properties->paired.ReplaceValue(true);
callback.Run();
AddInputDeviceIfNeeded(object_path, properties);
}
}
void FakeBluetoothDeviceClient::TimeoutSimulatedPairing(
const dbus::ObjectPath& object_path,
const ErrorCallback& error_callback) {
VLOG(1) << "TimeoutSimulatedPairing: " << object_path.value();
error_callback.Run(bluetooth_device::kErrorAuthenticationTimeout,
"Timed out");
}
void FakeBluetoothDeviceClient::CancelSimulatedPairing(
const dbus::ObjectPath& object_path,
const ErrorCallback& error_callback) {
VLOG(1) << "CancelSimulatedPairing: " << object_path.value();
error_callback.Run(bluetooth_device::kErrorAuthenticationCanceled,
"Canceled");
}
void FakeBluetoothDeviceClient::RejectSimulatedPairing(
const dbus::ObjectPath& object_path,
const ErrorCallback& error_callback) {
VLOG(1) << "RejectSimulatedPairing: " << object_path.value();
error_callback.Run(bluetooth_device::kErrorAuthenticationRejected,
"Rejected");
}
void FakeBluetoothDeviceClient::FailSimulatedPairing(
const dbus::ObjectPath& object_path,
const ErrorCallback& error_callback) {
VLOG(1) << "FailSimulatedPairing: " << object_path.value();
error_callback.Run(bluetooth_device::kErrorFailed, "Failed");
}
void FakeBluetoothDeviceClient::AddInputDeviceIfNeeded(
const dbus::ObjectPath& object_path,
Properties* properties) {
// If the paired device is a HID device based on it's bluetooth class,
// simulate the Input interface.
FakeBluetoothInputClient* fake_bluetooth_input_client =
static_cast<FakeBluetoothInputClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothInputClient());
if ((properties->bluetooth_class.value() & 0x001f03) == 0x000500)
fake_bluetooth_input_client->AddInputDevice(object_path);
}
void FakeBluetoothDeviceClient::UpdateDeviceRSSI(
const dbus::ObjectPath& object_path,
int16 rssi) {
PropertiesMap::const_iterator iter = properties_map_.find(object_path);
if (iter == properties_map_.end()) {
VLOG(2) << "Fake device does not exist: " << object_path.value();
return;
}
Properties* properties = iter->second;
DCHECK(properties);
properties->rssi.ReplaceValue(rssi);
}
void FakeBluetoothDeviceClient::UpdateConnectionInfo(
uint16 connection_rssi,
uint16 transmit_power,
uint16 max_transmit_power) {
connection_rssi_ = connection_rssi;
transmit_power_ = transmit_power;
max_transmit_power_ = max_transmit_power;
}
void FakeBluetoothDeviceClient::PinCodeCallback(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback,
BluetoothAgentServiceProvider::Delegate::Status status,
const std::string& pincode) {
VLOG(1) << "PinCodeCallback: " << object_path.value();
if (status == BluetoothAgentServiceProvider::Delegate::SUCCESS) {
PairingOptionsMap::const_iterator iter =
pairing_options_map_.find(object_path);
bool success = true;
// If the device has pairing options defined
if (iter != pairing_options_map_.end()) {
success = iter->second->pairing_auth_token == pincode;
}
if (success) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kSimulateNormalPairTimeMultiplier *
simulation_interval_ms_));
} else {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
} else if (status == BluetoothAgentServiceProvider::Delegate::CANCELLED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (status == BluetoothAgentServiceProvider::Delegate::REJECTED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
}
void FakeBluetoothDeviceClient::PasskeyCallback(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback,
BluetoothAgentServiceProvider::Delegate::Status status,
uint32 passkey) {
VLOG(1) << "PasskeyCallback: " << object_path.value();
if (status == BluetoothAgentServiceProvider::Delegate::SUCCESS) {
PairingOptionsMap::const_iterator iter =
pairing_options_map_.find(object_path);
bool success = true;
if (iter != pairing_options_map_.end()) {
success = static_cast<uint32>(
std::stoi(iter->second->pairing_auth_token)) == passkey;
}
if (success) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kSimulateNormalPairTimeMultiplier *
simulation_interval_ms_));
} else {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
} else if (status == BluetoothAgentServiceProvider::Delegate::CANCELLED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (status == BluetoothAgentServiceProvider::Delegate::REJECTED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
}
void FakeBluetoothDeviceClient::ConfirmationCallback(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback,
BluetoothAgentServiceProvider::Delegate::Status status) {
VLOG(1) << "ConfirmationCallback: " << object_path.value();
if (status == BluetoothAgentServiceProvider::Delegate::SUCCESS) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(kSimulateNormalPairTimeMultiplier *
simulation_interval_ms_));
} else if (status == BluetoothAgentServiceProvider::Delegate::CANCELLED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CancelSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else if (status == BluetoothAgentServiceProvider::Delegate::REJECTED) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::RejectSimulatedPairing,
base::Unretained(this), object_path, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
}
void FakeBluetoothDeviceClient::SimulateKeypress(
uint16 entered,
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
VLOG(1) << "SimulateKeypress " << entered << ": " << object_path.value();
FakeBluetoothAgentManagerClient* fake_bluetooth_agent_manager_client =
static_cast<FakeBluetoothAgentManagerClient*>(
bluez::BluezDBusManager::Get()->GetBluetoothAgentManagerClient());
FakeBluetoothAgentServiceProvider* agent_service_provider =
fake_bluetooth_agent_manager_client->GetAgentServiceProvider();
// The agent service provider object could have been destroyed after the
// pairing is canceled.
if (!agent_service_provider)
return;
agent_service_provider->DisplayPasskey(object_path, kTestPassKey, entered);
if (entered < 7) {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE, base::Bind(&FakeBluetoothDeviceClient::SimulateKeypress,
base::Unretained(this), entered + 1, object_path,
callback, error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
} else {
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&FakeBluetoothDeviceClient::CompleteSimulatedPairing,
base::Unretained(this), object_path, callback,
error_callback),
base::TimeDelta::FromMilliseconds(simulation_interval_ms_));
}
}
void FakeBluetoothDeviceClient::ConnectionCallback(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback,
BluetoothProfileServiceProvider::Delegate::Status status) {
VLOG(1) << "ConnectionCallback: " << object_path.value();
if (status == BluetoothProfileServiceProvider::Delegate::SUCCESS) {
callback.Run();
} else if (status == BluetoothProfileServiceProvider::Delegate::CANCELLED) {
// TODO(keybuk): tear down this side of the connection
error_callback.Run(bluetooth_device::kErrorFailed, "Canceled");
} else if (status == BluetoothProfileServiceProvider::Delegate::REJECTED) {
// TODO(keybuk): tear down this side of the connection
error_callback.Run(bluetooth_device::kErrorFailed, "Rejected");
}
}
void FakeBluetoothDeviceClient::DisconnectionCallback(
const dbus::ObjectPath& object_path,
const base::Closure& callback,
const ErrorCallback& error_callback,
BluetoothProfileServiceProvider::Delegate::Status status) {
VLOG(1) << "DisconnectionCallback: " << object_path.value();
if (status == BluetoothProfileServiceProvider::Delegate::SUCCESS) {
// TODO(keybuk): tear down this side of the connection
callback.Run();
} else if (status == BluetoothProfileServiceProvider::Delegate::CANCELLED) {
error_callback.Run(bluetooth_device::kErrorFailed, "Canceled");
} else if (status == BluetoothProfileServiceProvider::Delegate::REJECTED) {
error_callback.Run(bluetooth_device::kErrorFailed, "Rejected");
}
}
} // namespace bluez
|