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 "device/bluetooth/adapter.h" | 9 #include "device/bluetooth/adapter.h" |
10 #include "device/bluetooth/device.h" | 10 #include "device/bluetooth/device.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
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::ConnectErrorCode::DEVICE_NO_LONGER_IN_RANGE, |
43 nullptr /* Device */); | |
ortuno
2016/10/26 02:52:21
nit: s/Device/device/
mbrunson
2016/10/28 21:06:47
Done.
| |
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 mojo::MakeStrongBinding( | |
99 base::MakeUnique<Device>(adapter_, std::move(connection)), | |
100 mojo::GetProxy(&device_ptr)); | |
101 callback.Run(mojom::ConnectErrorCode::SUCCESS, std::move(device_ptr)); | |
102 } | |
103 | |
104 void Adapter::OnConnectError( | |
105 const ConnectToDeviceCallback& callback, | |
106 device::BluetoothDevice::ConnectErrorCode error_code) { | |
107 mojom::ConnectErrorCode code = BluetoothErrorCodeToMojomErrorCode(error_code); | |
108 callback.Run(code, nullptr /* Device */); | |
109 } | |
110 | |
111 mojom::ConnectErrorCode Adapter::BluetoothErrorCodeToMojomErrorCode( | |
112 device::BluetoothDevice::ConnectErrorCode error_code) { | |
113 switch (error_code) { | |
114 case device::BluetoothDevice::ConnectErrorCode:: | |
115 ERROR_ATTRIBUTE_LENGTH_INVALID: | |
116 return mojom::ConnectErrorCode::ATTRIBUTE_LENGTH_INVALID; | |
117 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_CANCELED: | |
118 return mojom::ConnectErrorCode::AUTH_CANCELED; | |
119 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_FAILED: | |
120 return mojom::ConnectErrorCode::AUTH_FAILED; | |
121 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_REJECTED: | |
122 return mojom::ConnectErrorCode::AUTH_REJECTED; | |
123 case device::BluetoothDevice::ConnectErrorCode::ERROR_AUTH_TIMEOUT: | |
124 return mojom::ConnectErrorCode::AUTH_TIMEOUT; | |
125 case device::BluetoothDevice::ConnectErrorCode::ERROR_CONNECTION_CONGESTED: | |
126 return mojom::ConnectErrorCode::CONNECTION_CONGESTED; | |
127 case device::BluetoothDevice::ConnectErrorCode::ERROR_FAILED: | |
128 return mojom::ConnectErrorCode::FAILED; | |
129 case device::BluetoothDevice::ConnectErrorCode::ERROR_INPROGRESS: | |
130 return mojom::ConnectErrorCode::INPROGRESS; | |
131 case device::BluetoothDevice::ConnectErrorCode:: | |
132 ERROR_INSUFFICIENT_ENCRYPTION: | |
133 return mojom::ConnectErrorCode::INSUFFICIENT_ENCRYPTION; | |
134 case device::BluetoothDevice::ConnectErrorCode::ERROR_OFFSET_INVALID: | |
135 return mojom::ConnectErrorCode::OFFSET_INVALID; | |
136 case device::BluetoothDevice::ConnectErrorCode::ERROR_READ_NOT_PERMITTED: | |
137 return mojom::ConnectErrorCode::READ_NOT_PERMITTED; | |
138 case device::BluetoothDevice::ConnectErrorCode::ERROR_REQUEST_NOT_SUPPORTED: | |
139 return mojom::ConnectErrorCode::REQUEST_NOT_SUPPORTED; | |
140 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNKNOWN: | |
141 return mojom::ConnectErrorCode::UNKNOWN; | |
142 case device::BluetoothDevice::ConnectErrorCode::ERROR_UNSUPPORTED_DEVICE: | |
143 return mojom::ConnectErrorCode::UNSUPPORTED_DEVICE; | |
144 case device::BluetoothDevice::ConnectErrorCode::ERROR_WRITE_NOT_PERMITTED: | |
145 return mojom::ConnectErrorCode::WRITE_NOT_PERMITTED; | |
146 case device::BluetoothDevice::ConnectErrorCode::NUM_CONNECT_ERROR_CODES: | |
ortuno
2016/10/26 02:52:21
This is not an actual error :) add a NOTREACHED an
mbrunson
2016/10/28 21:06:47
Done.
| |
147 return mojom::ConnectErrorCode::NUM_CONNECT_ERROR_CODES; | |
148 default: | |
ortuno
2016/10/26 02:52:21
When a new value is added to an enum the compiler
mbrunson
2016/10/28 21:06:47
Done.
| |
149 return mojom::ConnectErrorCode::UNKNOWN; | |
150 } | |
151 } | |
85 } // namespace bluetooth | 152 } // namespace bluetooth |
OLD | NEW |