| 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/base64.h" |
| 18 #include "base/bind_helpers.h" | 19 #include "base/bind_helpers.h" |
| 19 #include "base/location.h" | 20 #include "base/location.h" |
| 20 #include "base/logging.h" | 21 #include "base/logging.h" |
| 21 #include "base/memory/ptr_util.h" | 22 #include "base/memory/ptr_util.h" |
| 22 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 23 #include "base/rand_util.h" | 24 #include "base/rand_util.h" |
| 24 #include "base/single_thread_task_runner.h" | 25 #include "base/single_thread_task_runner.h" |
| 25 #include "base/stl_util.h" | 26 #include "base/stl_util.h" |
| 27 #include "base/strings/string_util.h" |
| 26 #include "base/task_scheduler/post_task.h" | 28 #include "base/task_scheduler/post_task.h" |
| 27 #include "base/threading/thread_task_runner_handle.h" | 29 #include "base/threading/thread_task_runner_handle.h" |
| 28 #include "base/time/time.h" | 30 #include "base/time/time.h" |
| 29 #include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h" | 31 #include "device/bluetooth/bluez/bluetooth_service_attribute_value_bluez.h" |
| 30 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 32 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
| 31 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | 33 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" |
| 32 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | 34 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" |
| 33 #include "device/bluetooth/dbus/fake_bluetooth_agent_service_provider.h" | 35 #include "device/bluetooth/dbus/fake_bluetooth_agent_service_provider.h" |
| 34 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | 36 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" |
| 35 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" | 37 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" |
| (...skipping 1732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1768 | 1770 |
| 1769 void FakeBluetoothDeviceClient::CreateTestDevice( | 1771 void FakeBluetoothDeviceClient::CreateTestDevice( |
| 1770 const dbus::ObjectPath& adapter_path, | 1772 const dbus::ObjectPath& adapter_path, |
| 1771 const base::Optional<std::string> name, | 1773 const base::Optional<std::string> name, |
| 1772 const std::string alias, | 1774 const std::string alias, |
| 1773 const std::string device_address, | 1775 const std::string device_address, |
| 1774 const std::vector<std::string>& service_uuids, | 1776 const std::vector<std::string>& service_uuids, |
| 1775 device::BluetoothTransport type) { | 1777 device::BluetoothTransport type) { |
| 1776 // Create a random device path. | 1778 // Create a random device path. |
| 1777 dbus::ObjectPath device_path; | 1779 dbus::ObjectPath device_path; |
| 1780 std::string id; |
| 1778 do { | 1781 do { |
| 1779 device_path = dbus::ObjectPath(adapter_path.value() + "/dev" + | 1782 // Construct an id that is valid according to the DBUS specification. |
| 1780 base::RandBytesAsString(10)); | 1783 base::Base64Encode(base::RandBytesAsString(10), &id); |
| 1784 base::RemoveChars(id, "+/=", &id); |
| 1785 device_path = dbus::ObjectPath(adapter_path.value() + "/dev" + id); |
| 1781 } while (std::find(device_list_.begin(), device_list_.end(), device_path) != | 1786 } while (std::find(device_list_.begin(), device_list_.end(), device_path) != |
| 1782 device_list_.end()); | 1787 device_list_.end()); |
| 1783 | 1788 |
| 1784 std::unique_ptr<Properties> properties( | 1789 std::unique_ptr<Properties> properties( |
| 1785 new Properties(base::Bind(&FakeBluetoothDeviceClient::OnPropertyChanged, | 1790 new Properties(base::Bind(&FakeBluetoothDeviceClient::OnPropertyChanged, |
| 1786 base::Unretained(this), device_path))); | 1791 base::Unretained(this), device_path))); |
| 1787 properties->adapter.ReplaceValue(adapter_path); | 1792 properties->adapter.ReplaceValue(adapter_path); |
| 1788 | 1793 |
| 1789 properties->address.ReplaceValue(device_address); | 1794 properties->address.ReplaceValue(device_address); |
| 1790 properties->name.ReplaceValue( | 1795 properties->name.ReplaceValue( |
| 1791 name.value_or("Invalid Device Name set in " | 1796 name.value_or("Invalid Device Name set in " |
| 1792 "FakeBluetoothDeviceClient::CreateTestDevice")); | 1797 "FakeBluetoothDeviceClient::CreateTestDevice")); |
| 1793 properties->name.set_valid(name.has_value()); | 1798 properties->name.set_valid(name.has_value()); |
| 1794 properties->alias.ReplaceValue(alias); | 1799 properties->alias.ReplaceValue(alias); |
| 1795 | 1800 |
| 1796 properties->uuids.ReplaceValue(service_uuids); | 1801 properties->uuids.ReplaceValue(service_uuids); |
| 1802 properties->bluetooth_class.ReplaceValue( |
| 1803 0x1F00u); // Unspecified Device Class |
| 1797 | 1804 |
| 1798 switch (type) { | 1805 switch (type) { |
| 1799 case device::BLUETOOTH_TRANSPORT_CLASSIC: | 1806 case device::BLUETOOTH_TRANSPORT_CLASSIC: |
| 1800 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeBredr); | 1807 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeBredr); |
| 1801 break; | 1808 break; |
| 1802 case device::BLUETOOTH_TRANSPORT_LE: | 1809 case device::BLUETOOTH_TRANSPORT_LE: |
| 1803 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeLe); | 1810 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeLe); |
| 1804 break; | 1811 break; |
| 1805 case device::BLUETOOTH_TRANSPORT_DUAL: | 1812 case device::BLUETOOTH_TRANSPORT_DUAL: |
| 1806 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeDual); | 1813 properties->type.ReplaceValue(BluetoothDeviceClient::kTypeDual); |
| 1807 break; | 1814 break; |
| 1808 default: | 1815 default: |
| 1809 NOTREACHED(); | 1816 NOTREACHED(); |
| 1810 } | 1817 } |
| 1811 properties->type.set_valid(true); | 1818 properties->type.set_valid(true); |
| 1812 | 1819 |
| 1813 properties_map_.insert(std::make_pair(device_path, std::move(properties))); | 1820 properties_map_.insert(std::make_pair(device_path, std::move(properties))); |
| 1814 device_list_.push_back(device_path); | 1821 device_list_.push_back(device_path); |
| 1815 for (auto& observer : observers_) | 1822 for (auto& observer : observers_) |
| 1816 observer.DeviceAdded(device_path); | 1823 observer.DeviceAdded(device_path); |
| 1817 } | 1824 } |
| 1818 | 1825 |
| 1819 } // namespace bluez | 1826 } // namespace bluez |
| OLD | NEW |