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 <utility> | 5 #include <utility> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "device/bluetooth/device.h" | 10 #include "device/bluetooth/device.h" |
11 #include "device/bluetooth/public/interfaces/gatt_result_enum_traits.h" | |
11 #include "mojo/public/cpp/bindings/strong_binding.h" | 12 #include "mojo/public/cpp/bindings/strong_binding.h" |
12 | 13 |
14 using GattResultTraits = | |
15 mojo::EnumTraits<bluetooth::mojom::GattResult, | |
16 device::BluetoothGattService::GattErrorCode>; | |
17 | |
13 namespace bluetooth { | 18 namespace bluetooth { |
14 Device::~Device() { | 19 Device::~Device() { |
15 adapter_->RemoveObserver(this); | 20 adapter_->RemoveObserver(this); |
16 } | 21 } |
17 | 22 |
18 // static | 23 // static |
19 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, | 24 void Device::Create(scoped_refptr<device::BluetoothAdapter> adapter, |
20 std::unique_ptr<device::BluetoothGattConnection> connection, | 25 std::unique_ptr<device::BluetoothGattConnection> connection, |
21 mojom::DeviceRequest request) { | 26 mojom::DeviceRequest request) { |
22 auto device_impl = | 27 auto device_impl = |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 characteristic_info->id = characteristic->GetIdentifier(); | 121 characteristic_info->id = characteristic->GetIdentifier(); |
117 characteristic_info->uuid = characteristic->GetUUID(); | 122 characteristic_info->uuid = characteristic->GetUUID(); |
118 characteristic_info->properties = characteristic->GetProperties(); | 123 characteristic_info->properties = characteristic->GetProperties(); |
119 | 124 |
120 characteristics.push_back(std::move(characteristic_info)); | 125 characteristics.push_back(std::move(characteristic_info)); |
121 } | 126 } |
122 | 127 |
123 callback.Run(std::move(characteristics)); | 128 callback.Run(std::move(characteristics)); |
124 } | 129 } |
125 | 130 |
131 void Device::ReadValueForCharacteristic( | |
132 const std::string& service_id, | |
133 const std::string& characteristic_id, | |
134 const ReadValueForCharacteristicCallback& callback) { | |
135 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
136 DCHECK(device); | |
137 | |
138 device::BluetoothRemoteGattService* service = | |
139 device->GetGattService(service_id); | |
140 if (service == nullptr) { | |
141 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND, | |
142 base::nullopt /* value */); | |
143 return; | |
144 } | |
145 | |
146 device::BluetoothRemoteGattCharacteristic* characteristic = | |
147 service->GetCharacteristic(characteristic_id); | |
148 if (characteristic == nullptr) { | |
149 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND, | |
150 base::nullopt /* value */); | |
151 return; | |
152 } | |
153 | |
154 characteristic->ReadRemoteCharacteristic( | |
155 base::Bind(&Device::OnReadRemoteCharacteristic, | |
156 weak_ptr_factory_.GetWeakPtr(), callback), | |
157 base::Bind(&Device::OnReadRemoteCharacteristicError, | |
158 weak_ptr_factory_.GetWeakPtr(), callback)); | |
159 } | |
160 | |
161 void Device::WriteValueForCharacteristic( | |
162 const std::string& service_id, | |
163 const std::string& characteristic_id, | |
164 const std::vector<uint8_t>& value, | |
165 const WriteValueForCharacteristicCallback& callback) { | |
166 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
167 DCHECK(device); | |
168 | |
169 device::BluetoothRemoteGattService* service = | |
170 device->GetGattService(service_id); | |
171 if (service == nullptr) { | |
172 callback.Run(mojom::GattResult::SERVICE_NOT_FOUND); | |
173 return; | |
174 } | |
175 | |
176 device::BluetoothRemoteGattCharacteristic* characteristic = | |
177 service->GetCharacteristic(characteristic_id); | |
178 if (characteristic == nullptr) { | |
179 callback.Run(mojom::GattResult::CHARACTERISTIC_NOT_FOUND); | |
180 return; | |
181 } | |
182 | |
183 characteristic->WriteRemoteCharacteristic( | |
184 value, base::Bind(&Device::OnWriteRemoteCharacteristic, | |
185 weak_ptr_factory_.GetWeakPtr(), callback), | |
186 base::Bind(&Device::OnWriteRemoteCharacteristicError, | |
187 weak_ptr_factory_.GetWeakPtr(), callback)); | |
188 } | |
189 | |
190 void Device::GetDescriptors(const std::string& service_id, | |
191 const std::string& characteristic_id, | |
192 const GetDescriptorsCallback& callback) { | |
193 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | |
194 if (!device) { | |
195 callback.Run(base::nullopt); | |
196 return; | |
197 } | |
198 | |
199 device::BluetoothRemoteGattService* service = | |
200 device->GetGattService(service_id); | |
201 if (!service) { | |
202 callback.Run(base::nullopt); | |
203 return; | |
204 } | |
205 | |
206 device::BluetoothRemoteGattCharacteristic* characteristic = | |
207 service->GetCharacteristic(characteristic_id); | |
208 if (!characteristic) { | |
209 callback.Run(base::nullopt); | |
210 return; | |
211 } | |
212 | |
213 std::vector<mojom::DescriptorInfoPtr> descriptors; | |
214 | |
215 for (const auto* descriptor : characteristic->GetDescriptors()) { | |
216 mojom::DescriptorInfoPtr descriptor_info = mojom::DescriptorInfo::New(); | |
217 | |
218 descriptor_info->id = descriptor->GetIdentifier(); | |
219 descriptor_info->uuid = descriptor->GetUUID(); | |
220 descriptors.push_back(std::move(descriptor_info)); | |
221 } | |
222 | |
223 callback.Run(std::move(descriptors)); | |
224 } | |
225 | |
126 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, | 226 Device::Device(scoped_refptr<device::BluetoothAdapter> adapter, |
127 std::unique_ptr<device::BluetoothGattConnection> connection) | 227 std::unique_ptr<device::BluetoothGattConnection> connection) |
128 : adapter_(std::move(adapter)), connection_(std::move(connection)) { | 228 : adapter_(std::move(adapter)), |
229 connection_(std::move(connection)), | |
230 weak_ptr_factory_(this) { | |
129 adapter_->AddObserver(this); | 231 adapter_->AddObserver(this); |
130 } | 232 } |
131 | 233 |
132 void Device::GetServicesImpl(const GetServicesCallback& callback) { | 234 void Device::GetServicesImpl(const GetServicesCallback& callback) { |
133 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); | 235 device::BluetoothDevice* device = adapter_->GetDevice(GetAddress()); |
134 DCHECK(device); | 236 DCHECK(device); |
135 | 237 |
136 std::vector<mojom::ServiceInfoPtr> services; | 238 std::vector<mojom::ServiceInfoPtr> services; |
137 | 239 |
138 for (const device::BluetoothRemoteGattService* service : | 240 for (const device::BluetoothRemoteGattService* service : |
139 device->GetGattServices()) { | 241 device->GetGattServices()) { |
140 services.push_back(ConstructServiceInfoStruct(*service)); | 242 services.push_back(ConstructServiceInfoStruct(*service)); |
141 } | 243 } |
142 | 244 |
143 callback.Run(std::move(services)); | 245 callback.Run(std::move(services)); |
144 } | 246 } |
145 | 247 |
146 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( | 248 mojom::ServiceInfoPtr Device::ConstructServiceInfoStruct( |
147 const device::BluetoothRemoteGattService& service) { | 249 const device::BluetoothRemoteGattService& service) { |
148 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); | 250 mojom::ServiceInfoPtr service_info = mojom::ServiceInfo::New(); |
149 | 251 |
150 service_info->id = service.GetIdentifier(); | 252 service_info->id = service.GetIdentifier(); |
151 service_info->uuid = service.GetUUID(); | 253 service_info->uuid = service.GetUUID(); |
152 service_info->is_primary = service.IsPrimary(); | 254 service_info->is_primary = service.IsPrimary(); |
153 | 255 |
154 return service_info; | 256 return service_info; |
155 } | 257 } |
156 | 258 |
259 void Device::OnReadRemoteCharacteristic( | |
260 const ReadValueForCharacteristicCallback& callback, | |
261 const std::vector<uint8_t>& value) { | |
262 callback.Run(mojom::GattResult::SUCCESS, std::move(value)); | |
263 } | |
264 | |
265 void Device::OnReadRemoteCharacteristicError( | |
266 const ReadValueForCharacteristicCallback& callback, | |
267 device::BluetoothGattService::GattErrorCode error_code) { | |
268 callback.Run(GattResultTraits::ToMojom(error_code), | |
dcheng
2017/01/26 19:32:25
I think you need a typemap file as well (see the C
mbrunson
2017/01/26 21:27:40
Ah...yes. The issue with that is I can't do things
| |
269 base::nullopt /* value */); | |
270 } | |
271 | |
272 void Device::OnWriteRemoteCharacteristic( | |
273 const WriteValueForCharacteristicCallback& callback) { | |
274 callback.Run(mojom::GattResult::SUCCESS); | |
275 } | |
276 | |
277 void Device::OnWriteRemoteCharacteristicError( | |
278 const WriteValueForCharacteristicCallback& callback, | |
279 device::BluetoothGattService::GattErrorCode error_code) { | |
280 callback.Run(GattResultTraits::ToMojom(error_code)); | |
281 } | |
282 | |
157 const std::string& Device::GetAddress() { | 283 const std::string& Device::GetAddress() { |
158 return connection_->GetDeviceAddress(); | 284 return connection_->GetDeviceAddress(); |
159 } | 285 } |
160 | 286 |
161 } // namespace bluetooth | 287 } // namespace bluetooth |
OLD | NEW |