OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | 5 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include <algorithm> | 13 #include <algorithm> |
14 #include <memory> | 14 #include <memory> |
15 #include <string> | 15 #include <string> |
16 #include <utility> | 16 #include <utility> |
17 | 17 |
18 #include "base/bind_helpers.h" | |
18 #include "base/location.h" | 19 #include "base/location.h" |
19 #include "base/logging.h" | 20 #include "base/logging.h" |
21 #include "base/memory/ptr_util.h" | |
22 #include "base/memory/ref_counted.h" | |
20 #include "base/rand_util.h" | 23 #include "base/rand_util.h" |
21 #include "base/single_thread_task_runner.h" | 24 #include "base/single_thread_task_runner.h" |
22 #include "base/stl_util.h" | 25 #include "base/stl_util.h" |
23 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
24 #include "base/threading/worker_pool.h" | 27 #include "base/threading/worker_pool.h" |
25 #include "base/time/time.h" | 28 #include "base/time/time.h" |
26 #include "dbus/file_descriptor.h" | 29 #include "dbus/file_descriptor.h" |
30 #include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h" | |
27 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 31 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
28 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | 32 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
29 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | 33 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" |
30 #include "device/bluetooth/dbus/fake_bluetooth_agent_service_provider.h" | 34 #include "device/bluetooth/dbus/fake_bluetooth_agent_service_provider.h" |
31 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | 35 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
32 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" | 36 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" |
33 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" | 37 #include "device/bluetooth/dbus/fake_bluetooth_profile_manager_client.h" |
34 #include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h" | 38 #include "device/bluetooth/dbus/fake_bluetooth_profile_service_provider.h" |
35 #include "third_party/cros_system_api/dbus/service_constants.h" | 39 #include "third_party/cros_system_api/dbus/service_constants.h" |
36 | 40 |
41 namespace bluez { | |
42 | |
37 namespace { | 43 namespace { |
38 | 44 |
39 // Default interval between simulated events. | 45 // Default interval between simulated events. |
40 const int kSimulationIntervalMs = 750; | 46 const int kSimulationIntervalMs = 750; |
41 | 47 |
42 // Minimum and maximum bounds for randomly generated RSSI values. | 48 // Minimum and maximum bounds for randomly generated RSSI values. |
43 const int kMinRSSI = -90; | 49 const int kMinRSSI = -90; |
44 const int kMaxRSSI = -30; | 50 const int kMaxRSSI = -30; |
45 | 51 |
46 // The default value of connection info properties from GetConnInfo(). | 52 // The default value of connection info properties from GetConnInfo(). |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 } | 92 } |
87 | 93 |
88 close(fd); | 94 close(fd); |
89 } | 95 } |
90 | 96 |
91 void SimpleErrorCallback(const std::string& error_name, | 97 void SimpleErrorCallback(const std::string& error_name, |
92 const std::string& error_message) { | 98 const std::string& error_message) { |
93 VLOG(1) << "Bluetooth Error: " << error_name << ": " << error_message; | 99 VLOG(1) << "Bluetooth Error: " << error_name << ": " << error_message; |
94 } | 100 } |
95 | 101 |
102 BluetoothDeviceClient::ServiceRecordList CreateFakeServiceRecords() { | |
103 BluetoothDeviceClient::ServiceRecordList records; | |
104 | |
105 std::unique_ptr<BluetoothServiceRecordBlueZ> record1 = | |
106 base::MakeUnique<BluetoothServiceRecordBlueZ>(); | |
107 // ID 0 = handle. | |
108 record1->AddRecordEntry(0x0, BluetoothServiceAttributeValueBlueZ( | |
109 BluetoothServiceAttributeValueBlueZ::UINT, 4, | |
110 base::MakeUnique<base::FundamentalValue>( | |
111 static_cast<int32_t>(0x1337)))); | |
112 // ID 1 = service class id list. | |
113 std::unique_ptr<BluetoothServiceAttributeValueBlueZ::Sequence> class_id_list = | |
114 base::MakeUnique<BluetoothServiceAttributeValueBlueZ::Sequence>(); | |
115 class_id_list->emplace_back(BluetoothServiceAttributeValueBlueZ( | |
Miao
2016/06/30 04:55:59
emplace_back(...) allows in-place construction of
rkc
2016/06/30 20:02:24
Done.
| |
116 BluetoothServiceAttributeValueBlueZ::UUID, 4, | |
117 base::MakeUnique<base::StringValue>("1802"))); | |
118 record1->AddRecordEntry( | |
119 0x1, BluetoothServiceAttributeValueBlueZ(std::move(class_id_list))); | |
120 records.emplace_back(*record1); | |
121 | |
122 std::unique_ptr<BluetoothServiceRecordBlueZ> record2 = | |
123 base::MakeUnique<BluetoothServiceRecordBlueZ>(); | |
124 // ID 0 = handle. | |
125 record2->AddRecordEntry(0x0, BluetoothServiceAttributeValueBlueZ( | |
126 BluetoothServiceAttributeValueBlueZ::UINT, 4, | |
127 base::MakeUnique<base::FundamentalValue>( | |
128 static_cast<int32_t>(0xffffffff)))); | |
129 records.emplace_back(*record2); | |
130 | |
131 return records; | |
132 } | |
133 | |
96 } // namespace | 134 } // namespace |
97 | 135 |
98 namespace bluez { | |
99 | |
100 const char FakeBluetoothDeviceClient::kTestPinCode[] = "123456"; | 136 const char FakeBluetoothDeviceClient::kTestPinCode[] = "123456"; |
101 const int FakeBluetoothDeviceClient::kTestPassKey = 123456; | 137 const int FakeBluetoothDeviceClient::kTestPassKey = 123456; |
102 | 138 |
103 const char FakeBluetoothDeviceClient::kPairingMethodNone[] = "None"; | 139 const char FakeBluetoothDeviceClient::kPairingMethodNone[] = "None"; |
104 const char FakeBluetoothDeviceClient::kPairingMethodPinCode[] = "PIN Code"; | 140 const char FakeBluetoothDeviceClient::kPairingMethodPinCode[] = "PIN Code"; |
105 const char FakeBluetoothDeviceClient::kPairingMethodPassKey[] = "PassKey"; | 141 const char FakeBluetoothDeviceClient::kPairingMethodPassKey[] = "PassKey"; |
106 | 142 |
107 const char FakeBluetoothDeviceClient::kPairingActionConfirmation[] = | 143 const char FakeBluetoothDeviceClient::kPairingActionConfirmation[] = |
108 "Confirmation"; | 144 "Confirmation"; |
109 const char FakeBluetoothDeviceClient::kPairingActionDisplay[] = "Display"; | 145 const char FakeBluetoothDeviceClient::kPairingActionDisplay[] = "Display"; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
406 } | 442 } |
407 | 443 |
408 void FakeBluetoothDeviceClient::Disconnect( | 444 void FakeBluetoothDeviceClient::Disconnect( |
409 const dbus::ObjectPath& object_path, | 445 const dbus::ObjectPath& object_path, |
410 const base::Closure& callback, | 446 const base::Closure& callback, |
411 const ErrorCallback& error_callback) { | 447 const ErrorCallback& error_callback) { |
412 VLOG(1) << "Disconnect: " << object_path.value(); | 448 VLOG(1) << "Disconnect: " << object_path.value(); |
413 Properties* properties = GetProperties(object_path); | 449 Properties* properties = GetProperties(object_path); |
414 | 450 |
415 if (!properties->connected.value()) { | 451 if (!properties->connected.value()) { |
416 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); | 452 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); |
417 return; | 453 return; |
418 } | 454 } |
419 | 455 |
420 // Hide the Heart Rate Service if disconnected from LE device. | 456 // Hide the Heart Rate Service if disconnected from LE device. |
421 if (object_path == dbus::ObjectPath(kLowEnergyPath)) { | 457 if (object_path == dbus::ObjectPath(kLowEnergyPath)) { |
422 FakeBluetoothGattServiceClient* gatt_service_client = | 458 FakeBluetoothGattServiceClient* gatt_service_client = |
423 static_cast<FakeBluetoothGattServiceClient*>( | 459 static_cast<FakeBluetoothGattServiceClient*>( |
424 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()); | 460 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()); |
425 gatt_service_client->HideHeartRateService(); | 461 gatt_service_client->HideHeartRateService(); |
426 } | 462 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
538 pairing_cancelled_ = true; | 574 pairing_cancelled_ = true; |
539 callback.Run(); | 575 callback.Run(); |
540 } | 576 } |
541 | 577 |
542 void FakeBluetoothDeviceClient::GetConnInfo( | 578 void FakeBluetoothDeviceClient::GetConnInfo( |
543 const dbus::ObjectPath& object_path, | 579 const dbus::ObjectPath& object_path, |
544 const ConnInfoCallback& callback, | 580 const ConnInfoCallback& callback, |
545 const ErrorCallback& error_callback) { | 581 const ErrorCallback& error_callback) { |
546 Properties* properties = GetProperties(object_path); | 582 Properties* properties = GetProperties(object_path); |
547 if (!properties->connected.value()) { | 583 if (!properties->connected.value()) { |
548 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); | 584 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); |
549 return; | 585 return; |
550 } | 586 } |
551 | 587 |
552 callback.Run(connection_rssi_, transmit_power_, max_transmit_power_); | 588 callback.Run(connection_rssi_, transmit_power_, max_transmit_power_); |
553 } | 589 } |
554 | 590 |
591 void FakeBluetoothDeviceClient::GetServiceRecords( | |
592 const dbus::ObjectPath& object_path, | |
593 const ServiceRecordsCallback& callback, | |
594 const ErrorCallback& error_callback) { | |
595 Properties* properties = GetProperties(object_path); | |
596 if (!properties->connected.value()) { | |
597 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); | |
598 return; | |
599 } | |
600 callback.Run(CreateFakeServiceRecords()); | |
601 } | |
602 | |
555 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( | 603 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( |
556 const dbus::ObjectPath& adapter_path) { | 604 const dbus::ObjectPath& adapter_path) { |
557 VLOG(1) << "starting discovery simulation"; | 605 VLOG(1) << "starting discovery simulation"; |
558 | 606 |
559 discovery_simulation_step_ = 1; | 607 discovery_simulation_step_ = 1; |
560 int delay = delay_start_discovery_ ? simulation_interval_ms_ : 0; | 608 int delay = delay_start_discovery_ ? simulation_interval_ms_ : 0; |
561 | 609 |
562 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 610 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
563 FROM_HERE, | 611 FROM_HERE, |
564 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, | 612 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
(...skipping 1179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1744 } | 1792 } |
1745 properties->type.set_valid(true); | 1793 properties->type.set_valid(true); |
1746 | 1794 |
1747 properties_map_.insert(std::make_pair(device_path, std::move(properties))); | 1795 properties_map_.insert(std::make_pair(device_path, std::move(properties))); |
1748 device_list_.push_back(device_path); | 1796 device_list_.push_back(device_path); |
1749 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, | 1797 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, |
1750 DeviceAdded(device_path)); | 1798 DeviceAdded(device_path)); |
1751 } | 1799 } |
1752 | 1800 |
1753 } // namespace bluez | 1801 } // namespace bluez |
OLD | NEW |