OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <string> | 5 #include <string> |
6 #include <utility> | 6 #include <utility> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/logging.h" | |
9 #include "device/bluetooth/adapter.h" | 10 #include "device/bluetooth/adapter.h" |
10 #include "device/bluetooth/device.h" | 11 #include "device/bluetooth/device.h" |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | |
12 | 12 |
13 namespace bluetooth { | 13 namespace bluetooth { |
14 | 14 |
15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) | 15 Adapter::Adapter(scoped_refptr<device::BluetoothAdapter> adapter) |
16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { | 16 : adapter_(std::move(adapter)), client_(nullptr), weak_ptr_factory_(this) { |
17 adapter_->AddObserver(this); | 17 adapter_->AddObserver(this); |
18 } | 18 } |
19 | 19 |
20 Adapter::~Adapter() { | 20 Adapter::~Adapter() { |
21 adapter_->RemoveObserver(this); | 21 adapter_->RemoveObserver(this); |
22 adapter_ = nullptr; | 22 adapter_ = nullptr; |
23 } | 23 } |
24 | 24 |
25 void Adapter::GetInfo(const GetInfoCallback& callback) { | 25 void Adapter::GetInfo(const GetInfoCallback& callback) { |
26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); | 26 mojom::AdapterInfoPtr adapter_info = mojom::AdapterInfo::New(); |
27 adapter_info->address = adapter_->GetAddress(); | 27 adapter_info->address = adapter_->GetAddress(); |
28 adapter_info->name = adapter_->GetName(); | 28 adapter_info->name = adapter_->GetName(); |
29 adapter_info->initialized = adapter_->IsInitialized(); | 29 adapter_info->initialized = adapter_->IsInitialized(); |
30 adapter_info->present = adapter_->IsPresent(); | 30 adapter_info->present = adapter_->IsPresent(); |
31 adapter_info->powered = adapter_->IsPowered(); | 31 adapter_info->powered = adapter_->IsPowered(); |
32 adapter_info->discoverable = adapter_->IsDiscoverable(); | 32 adapter_info->discoverable = adapter_->IsDiscoverable(); |
33 adapter_info->discovering = adapter_->IsDiscovering(); | 33 adapter_info->discovering = adapter_->IsDiscovering(); |
34 callback.Run(std::move(adapter_info)); | 34 callback.Run(std::move(adapter_info)); |
35 } | 35 } |
36 | 36 |
37 void Adapter::GetDevice(const std::string& address, | 37 void Adapter::ConnectToDevice(const std::string& address, |
38 const GetDeviceCallback& callback) { | 38 const ConnectToDeviceCallback& callback) { |
39 mojom::DevicePtr device_ptr; | 39 device::BluetoothDevice* device = adapter_->GetDevice(address); |
40 mojo::MakeStrongBinding(base::MakeUnique<Device>(address, adapter_), | 40 |
41 mojo::GetProxy(&device_ptr)); | 41 if (!device) { |
42 callback.Run(std::move(device_ptr)); | 42 callback.Run(mojom::ConnectResult::DEVICE_NO_LONGER_IN_RANGE, |
43 nullptr /* device */); | |
44 return; | |
45 } | |
46 | |
47 device->CreateGattConnection( | |
48 base::Bind(&Adapter::OnGattConnected, weak_ptr_factory_.GetWeakPtr(), | |
49 callback), | |
50 base::Bind(&Adapter::OnConnectError, weak_ptr_factory_.GetWeakPtr(), | |
51 callback)); | |
43 } | 52 } |
44 | 53 |
45 void Adapter::GetDevices(const GetDevicesCallback& callback) { | 54 void Adapter::GetDevices(const GetDevicesCallback& callback) { |
46 std::vector<mojom::DeviceInfoPtr> devices; | 55 std::vector<mojom::DeviceInfoPtr> devices; |
47 | 56 |
48 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { | 57 for (const device::BluetoothDevice* device : adapter_->GetDevices()) { |
49 mojom::DeviceInfoPtr device_info = | 58 mojom::DeviceInfoPtr device_info = |
50 Device::ConstructDeviceInfoStruct(device); | 59 Device::ConstructDeviceInfoStruct(device); |
51 devices.push_back(std::move(device_info)); | 60 devices.push_back(std::move(device_info)); |
52 } | 61 } |
(...skipping 22 matching lines...) Expand all Loading... | |
75 } | 84 } |
76 | 85 |
77 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, | 86 void Adapter::DeviceChanged(device::BluetoothAdapter* adapter, |
78 device::BluetoothDevice* device) { | 87 device::BluetoothDevice* device) { |
79 if (client_) { | 88 if (client_) { |
80 auto device_info = Device::ConstructDeviceInfoStruct(device); | 89 auto device_info = Device::ConstructDeviceInfoStruct(device); |
81 client_->DeviceChanged(std::move(device_info)); | 90 client_->DeviceChanged(std::move(device_info)); |
82 } | 91 } |
83 } | 92 } |
84 | 93 |
94 void Adapter::OnGattConnected( | |
95 const ConnectToDeviceCallback& callback, | |
96 std::unique_ptr<device::BluetoothGattConnection> connection) { | |
97 mojom::DevicePtr device_ptr; | |
98 | |
99 // Owns itself. | |
100 new Device(adapter_, std::move(connection), mojo::GetProxy(&device_ptr)); | |
101 | |
102 callback.Run(mojom::ConnectResult::SUCCESS, std::move(device_ptr)); | |
103 } | |
104 | |
105 void Adapter::OnConnectError( | |
106 const ConnectToDeviceCallback& callback, | |
107 device::BluetoothDevice::ConnectErrorCode error_code) { | |
108 mojom::ConnectResult code = BluetoothErrorCodeToMojomResult(error_code); | |
109 callback.Run(code, nullptr /* Device */); | |
110 } | |
111 | |
112 mojom::ConnectResult Adapter::BluetoothErrorCodeToMojomResult( | |
ortuno
2016/11/01 06:27:39
nit: Move this no an anonymous namespace i.e.:
na
mbrunson
2016/11/02 01:25:45
Done.
| |
113 device::BluetoothDevice::ConnectErrorCode error_code) { | |
114 switch (error_code) { | |
115 case device::BluetoothDevice::ConnectErrorCode:: | |
116 ERROR_ATTRIBUTE_LENGTH_INVALID: | |
117 return mojom::ConnectResult::ATTRIBUTE_LENGTH_INVALID; | |
118 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: | |
119 return mojom::ConnectResult::AUTH_CANCELED; | |
120 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: | |
121 return mojom::ConnectResult::AUTH_FAILED; | |
122 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: | |
123 return mojom::ConnectResult::AUTH_REJECTED; | |
124 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: | |
125 return mojom::ConnectResult::AUTH_TIMEOUT; | |
126 case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: | |
127 return mojom::ConnectResult::CONNECTION_CONGESTED; | |
128 case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: | |
129 return mojom::ConnectResult::FAILED; | |
130 case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: | |
131 return mojom::ConnectResult::INPROGRESS; | |
132 case device::BluetoothDevice::ConnectErrorCode:: | |
133 ERROR_INSUFFICIENT_ENCRYPTION: | |
134 return mojom::ConnectResult::INSUFFICIENT_ENCRYPTION; | |
135 case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: | |
136 return mojom::ConnectResult::OFFSET_INVALID; | |
137 case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: | |
138 return mojom::ConnectResult::READ_NOT_PERMITTED; | |
139 case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: | |
140 return mojom::ConnectResult::REQUEST_NOT_SUPPORTED; | |
141 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: | |
142 return mojom::ConnectResult::UNKNOWN; | |
143 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: | |
144 return mojom::ConnectResult::UNSUPPORTED_DEVICE; | |
145 case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: | |
146 return mojom::ConnectResult::WRITE_NOT_PERMITTED; | |
147 case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: | |
148 NOTREACHED(); | |
149 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
150 } | |
151 NOTREACHED(); | |
152 return mojom::ConnectResult::UNTRANSLATED_CONNECT_ERROR_CODE; | |
153 } | |
85 } // namespace bluetooth | 154 } // namespace bluetooth |
OLD | NEW |