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::UUID, 4, |
| 116 base::MakeUnique<base::StringValue>("1802")); |
| 117 record1->AddRecordEntry( |
| 118 0x1, BluetoothServiceAttributeValueBlueZ(std::move(class_id_list))); |
| 119 records.emplace_back(*record1); |
| 120 |
| 121 std::unique_ptr<BluetoothServiceRecordBlueZ> record2 = |
| 122 base::MakeUnique<BluetoothServiceRecordBlueZ>(); |
| 123 // ID 0 = handle. |
| 124 record2->AddRecordEntry(0x0, BluetoothServiceAttributeValueBlueZ( |
| 125 BluetoothServiceAttributeValueBlueZ::UINT, 4, |
| 126 base::MakeUnique<base::FundamentalValue>( |
| 127 static_cast<int32_t>(0xffffffff)))); |
| 128 records.emplace_back(*record2); |
| 129 |
| 130 return records; |
| 131 } |
| 132 |
96 } // namespace | 133 } // namespace |
97 | 134 |
98 namespace bluez { | |
99 | |
100 const char FakeBluetoothDeviceClient::kTestPinCode[] = "123456"; | 135 const char FakeBluetoothDeviceClient::kTestPinCode[] = "123456"; |
101 const int FakeBluetoothDeviceClient::kTestPassKey = 123456; | 136 const int FakeBluetoothDeviceClient::kTestPassKey = 123456; |
102 | 137 |
103 const char FakeBluetoothDeviceClient::kPairingMethodNone[] = "None"; | 138 const char FakeBluetoothDeviceClient::kPairingMethodNone[] = "None"; |
104 const char FakeBluetoothDeviceClient::kPairingMethodPinCode[] = "PIN Code"; | 139 const char FakeBluetoothDeviceClient::kPairingMethodPinCode[] = "PIN Code"; |
105 const char FakeBluetoothDeviceClient::kPairingMethodPassKey[] = "PassKey"; | 140 const char FakeBluetoothDeviceClient::kPairingMethodPassKey[] = "PassKey"; |
106 | 141 |
107 const char FakeBluetoothDeviceClient::kPairingActionConfirmation[] = | 142 const char FakeBluetoothDeviceClient::kPairingActionConfirmation[] = |
108 "Confirmation"; | 143 "Confirmation"; |
109 const char FakeBluetoothDeviceClient::kPairingActionDisplay[] = "Display"; | 144 const char FakeBluetoothDeviceClient::kPairingActionDisplay[] = "Display"; |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 } | 441 } |
407 | 442 |
408 void FakeBluetoothDeviceClient::Disconnect( | 443 void FakeBluetoothDeviceClient::Disconnect( |
409 const dbus::ObjectPath& object_path, | 444 const dbus::ObjectPath& object_path, |
410 const base::Closure& callback, | 445 const base::Closure& callback, |
411 const ErrorCallback& error_callback) { | 446 const ErrorCallback& error_callback) { |
412 VLOG(1) << "Disconnect: " << object_path.value(); | 447 VLOG(1) << "Disconnect: " << object_path.value(); |
413 Properties* properties = GetProperties(object_path); | 448 Properties* properties = GetProperties(object_path); |
414 | 449 |
415 if (!properties->connected.value()) { | 450 if (!properties->connected.value()) { |
416 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); | 451 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); |
417 return; | 452 return; |
418 } | 453 } |
419 | 454 |
420 // Hide the Heart Rate Service if disconnected from LE device. | 455 // Hide the Heart Rate Service if disconnected from LE device. |
421 if (object_path == dbus::ObjectPath(kLowEnergyPath)) { | 456 if (object_path == dbus::ObjectPath(kLowEnergyPath)) { |
422 FakeBluetoothGattServiceClient* gatt_service_client = | 457 FakeBluetoothGattServiceClient* gatt_service_client = |
423 static_cast<FakeBluetoothGattServiceClient*>( | 458 static_cast<FakeBluetoothGattServiceClient*>( |
424 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()); | 459 bluez::BluezDBusManager::Get()->GetBluetoothGattServiceClient()); |
425 gatt_service_client->HideHeartRateService(); | 460 gatt_service_client->HideHeartRateService(); |
426 } | 461 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
538 pairing_cancelled_ = true; | 573 pairing_cancelled_ = true; |
539 callback.Run(); | 574 callback.Run(); |
540 } | 575 } |
541 | 576 |
542 void FakeBluetoothDeviceClient::GetConnInfo( | 577 void FakeBluetoothDeviceClient::GetConnInfo( |
543 const dbus::ObjectPath& object_path, | 578 const dbus::ObjectPath& object_path, |
544 const ConnInfoCallback& callback, | 579 const ConnInfoCallback& callback, |
545 const ErrorCallback& error_callback) { | 580 const ErrorCallback& error_callback) { |
546 Properties* properties = GetProperties(object_path); | 581 Properties* properties = GetProperties(object_path); |
547 if (!properties->connected.value()) { | 582 if (!properties->connected.value()) { |
548 error_callback.Run("org.bluez.Error.NotConnected", "Not Connected"); | 583 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); |
549 return; | 584 return; |
550 } | 585 } |
551 | 586 |
552 callback.Run(connection_rssi_, transmit_power_, max_transmit_power_); | 587 callback.Run(connection_rssi_, transmit_power_, max_transmit_power_); |
553 } | 588 } |
554 | 589 |
| 590 void FakeBluetoothDeviceClient::GetServiceRecords( |
| 591 const dbus::ObjectPath& object_path, |
| 592 const ServiceRecordsCallback& callback, |
| 593 const ErrorCallback& error_callback) { |
| 594 Properties* properties = GetProperties(object_path); |
| 595 if (!properties->connected.value()) { |
| 596 error_callback.Run(bluetooth_device::kErrorNotConnected, "Not Connected"); |
| 597 return; |
| 598 } |
| 599 callback.Run(CreateFakeServiceRecords()); |
| 600 } |
| 601 |
555 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( | 602 void FakeBluetoothDeviceClient::BeginDiscoverySimulation( |
556 const dbus::ObjectPath& adapter_path) { | 603 const dbus::ObjectPath& adapter_path) { |
557 VLOG(1) << "starting discovery simulation"; | 604 VLOG(1) << "starting discovery simulation"; |
558 | 605 |
559 discovery_simulation_step_ = 1; | 606 discovery_simulation_step_ = 1; |
560 int delay = delay_start_discovery_ ? simulation_interval_ms_ : 0; | 607 int delay = delay_start_discovery_ ? simulation_interval_ms_ : 0; |
561 | 608 |
562 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 609 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
563 FROM_HERE, | 610 FROM_HERE, |
564 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, | 611 base::Bind(&FakeBluetoothDeviceClient::DiscoverySimulationTimer, |
(...skipping 1179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1744 } | 1791 } |
1745 properties->type.set_valid(true); | 1792 properties->type.set_valid(true); |
1746 | 1793 |
1747 properties_map_.insert(std::make_pair(device_path, std::move(properties))); | 1794 properties_map_.insert(std::make_pair(device_path, std::move(properties))); |
1748 device_list_.push_back(device_path); | 1795 device_list_.push_back(device_path); |
1749 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, | 1796 FOR_EACH_OBSERVER(BluetoothDeviceClient::Observer, observers_, |
1750 DeviceAdded(device_path)); | 1797 DeviceAdded(device_path)); |
1751 } | 1798 } |
1752 | 1799 |
1753 } // namespace bluez | 1800 } // namespace bluez |
OLD | NEW |