OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/bluetooth_gatt_characteristic_service_provider.h
" | 5 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider.h
" |
6 | 6 |
7 #include <stddef.h> | 7 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider_i
mpl.h" |
8 | |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | |
13 #include "base/logging.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/strings/string_util.h" | |
17 #include "base/threading/platform_thread.h" | |
18 #include "dbus/exported_object.h" | |
19 #include "dbus/message.h" | |
20 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 8 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provi
der.h" | 9 #include "device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_service_provi
der.h" |
22 #include "third_party/cros_system_api/dbus/service_constants.h" | |
23 | 10 |
24 namespace bluez { | 11 namespace bluez { |
25 namespace { | |
26 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; | |
27 const char kErrorPropertyReadOnly[] = | |
28 "org.freedesktop.DBus.Error.PropertyReadOnly"; | |
29 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed"; | |
30 } // namespace | |
31 | |
32 // The BluetoothGattCharacteristicServiceProvider implementation used in | |
33 // production. | |
34 class BluetoothGattCharacteristicServiceProviderImpl | |
35 : public BluetoothGattCharacteristicServiceProvider { | |
36 public: | |
37 BluetoothGattCharacteristicServiceProviderImpl( | |
38 dbus::Bus* bus, | |
39 const dbus::ObjectPath& object_path, | |
40 Delegate* delegate, | |
41 const std::string& uuid, | |
42 const std::vector<std::string>& flags, | |
43 const std::vector<std::string>& permissions, | |
44 const dbus::ObjectPath& service_path) | |
45 : origin_thread_id_(base::PlatformThread::CurrentId()), | |
46 uuid_(uuid), | |
47 bus_(bus), | |
48 delegate_(delegate), | |
49 object_path_(object_path), | |
50 service_path_(service_path), | |
51 weak_ptr_factory_(this) { | |
52 VLOG(1) << "Created Bluetooth GATT characteristic: " << object_path.value() | |
53 << " UUID: " << uuid; | |
54 DCHECK(bus_); | |
55 DCHECK(delegate_); | |
56 DCHECK(!uuid_.empty()); | |
57 DCHECK(object_path_.IsValid()); | |
58 DCHECK(service_path_.IsValid()); | |
59 DCHECK(base::StartsWith(object_path_.value(), service_path_.value() + "/", | |
60 base::CompareCase::SENSITIVE)); | |
61 | |
62 exported_object_ = bus_->GetExportedObject(object_path_); | |
63 | |
64 exported_object_->ExportMethod( | |
65 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGet, | |
66 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::Get, | |
67 weak_ptr_factory_.GetWeakPtr()), | |
68 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported, | |
69 weak_ptr_factory_.GetWeakPtr())); | |
70 | |
71 exported_object_->ExportMethod( | |
72 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesSet, | |
73 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::Set, | |
74 weak_ptr_factory_.GetWeakPtr()), | |
75 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported, | |
76 weak_ptr_factory_.GetWeakPtr())); | |
77 | |
78 exported_object_->ExportMethod( | |
79 dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGetAll, | |
80 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::GetAll, | |
81 weak_ptr_factory_.GetWeakPtr()), | |
82 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported, | |
83 weak_ptr_factory_.GetWeakPtr())); | |
84 } | |
85 | |
86 ~BluetoothGattCharacteristicServiceProviderImpl() override { | |
87 VLOG(1) << "Cleaning up Bluetooth GATT characteristic: " | |
88 << object_path_.value(); | |
89 bus_->UnregisterExportedObject(object_path_); | |
90 } | |
91 | |
92 // BluetoothGattCharacteristicServiceProvider override. | |
93 void SendValueChanged(const std::vector<uint8_t>& value) override { | |
94 VLOG(2) << "Emitting a PropertiesChanged signal for characteristic value."; | |
95 dbus::Signal signal(dbus::kDBusPropertiesInterface, | |
96 dbus::kDBusPropertiesChangedSignal); | |
97 dbus::MessageWriter writer(&signal); | |
98 dbus::MessageWriter array_writer(NULL); | |
99 dbus::MessageWriter dict_entry_writer(NULL); | |
100 dbus::MessageWriter variant_writer(NULL); | |
101 | |
102 // interface_name | |
103 writer.AppendString( | |
104 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface); | |
105 | |
106 // changed_properties | |
107 writer.OpenArray("{sv}", &array_writer); | |
108 array_writer.OpenDictEntry(&dict_entry_writer); | |
109 dict_entry_writer.AppendString( | |
110 bluetooth_gatt_characteristic::kValueProperty); | |
111 dict_entry_writer.OpenVariant("ay", &variant_writer); | |
112 variant_writer.AppendArrayOfBytes(value.data(), value.size()); | |
113 dict_entry_writer.CloseContainer(&variant_writer); | |
114 array_writer.CloseContainer(&dict_entry_writer); | |
115 writer.CloseContainer(&array_writer); | |
116 | |
117 // invalidated_properties. | |
118 writer.OpenArray("s", &array_writer); | |
119 writer.CloseContainer(&array_writer); | |
120 | |
121 exported_object_->SendSignal(&signal); | |
122 } | |
123 | |
124 private: | |
125 // Returns true if the current thread is on the origin thread. | |
126 bool OnOriginThread() { | |
127 return base::PlatformThread::CurrentId() == origin_thread_id_; | |
128 } | |
129 | |
130 // Called by dbus:: when the Bluetooth daemon fetches a single property of | |
131 // the characteristic. | |
132 void Get(dbus::MethodCall* method_call, | |
133 dbus::ExportedObject::ResponseSender response_sender) { | |
134 VLOG(2) << "BluetoothGattCharacteristicServiceProvider::Get: " | |
135 << object_path_.value(); | |
136 DCHECK(OnOriginThread()); | |
137 | |
138 dbus::MessageReader reader(method_call); | |
139 | |
140 std::string interface_name; | |
141 std::string property_name; | |
142 if (!reader.PopString(&interface_name) || | |
143 !reader.PopString(&property_name) || reader.HasMoreData()) { | |
144 std::unique_ptr<dbus::ErrorResponse> error_response = | |
145 dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs, | |
146 "Expected 'ss'."); | |
147 response_sender.Run(std::move(error_response)); | |
148 return; | |
149 } | |
150 | |
151 // Only the GATT characteristic interface is supported. | |
152 if (interface_name != | |
153 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface) { | |
154 std::unique_ptr<dbus::ErrorResponse> error_response = | |
155 dbus::ErrorResponse::FromMethodCall( | |
156 method_call, kErrorInvalidArgs, | |
157 "No such interface: '" + interface_name + "'."); | |
158 response_sender.Run(std::move(error_response)); | |
159 return; | |
160 } | |
161 | |
162 // If getting the "Value" property, obtain the value from the delegate. | |
163 if (property_name == bluetooth_gatt_characteristic::kValueProperty) { | |
164 DCHECK(delegate_); | |
165 delegate_->GetCharacteristicValue( | |
166 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnGet, | |
167 weak_ptr_factory_.GetWeakPtr(), method_call, | |
168 response_sender), | |
169 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure, | |
170 weak_ptr_factory_.GetWeakPtr(), method_call, | |
171 response_sender)); | |
172 return; | |
173 } | |
174 | |
175 std::unique_ptr<dbus::Response> response = | |
176 dbus::Response::FromMethodCall(method_call); | |
177 dbus::MessageWriter writer(response.get()); | |
178 dbus::MessageWriter variant_writer(NULL); | |
179 | |
180 // TODO(armansito): Process the "Flags" and "Permissions" properties below. | |
181 if (property_name == bluetooth_gatt_characteristic::kUUIDProperty) { | |
182 writer.OpenVariant("s", &variant_writer); | |
183 variant_writer.AppendString(uuid_); | |
184 writer.CloseContainer(&variant_writer); | |
185 } else if (property_name == | |
186 bluetooth_gatt_characteristic::kServiceProperty) { | |
187 writer.OpenVariant("o", &variant_writer); | |
188 variant_writer.AppendObjectPath(service_path_); | |
189 writer.CloseContainer(&variant_writer); | |
190 } else { | |
191 response = dbus::ErrorResponse::FromMethodCall( | |
192 method_call, kErrorInvalidArgs, | |
193 "No such property: '" + property_name + "'."); | |
194 } | |
195 | |
196 response_sender.Run(std::move(response)); | |
197 } | |
198 | |
199 // Called by dbus:: when the Bluetooth daemon sets a single property of the | |
200 // characteristic. | |
201 void Set(dbus::MethodCall* method_call, | |
202 dbus::ExportedObject::ResponseSender response_sender) { | |
203 VLOG(2) << "BluetoothGattCharacteristicServiceProvider::Set: " | |
204 << object_path_.value(); | |
205 DCHECK(OnOriginThread()); | |
206 | |
207 dbus::MessageReader reader(method_call); | |
208 | |
209 std::string interface_name; | |
210 std::string property_name; | |
211 dbus::MessageReader variant_reader(NULL); | |
212 if (!reader.PopString(&interface_name) || | |
213 !reader.PopString(&property_name) || | |
214 !reader.PopVariant(&variant_reader) || reader.HasMoreData()) { | |
215 std::unique_ptr<dbus::ErrorResponse> error_response = | |
216 dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs, | |
217 "Expected 'ssv'."); | |
218 response_sender.Run(std::move(error_response)); | |
219 return; | |
220 } | |
221 | |
222 // Only the GATT characteristic interface is allowed. | |
223 if (interface_name != | |
224 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface) { | |
225 std::unique_ptr<dbus::ErrorResponse> error_response = | |
226 dbus::ErrorResponse::FromMethodCall( | |
227 method_call, kErrorInvalidArgs, | |
228 "No such interface: '" + interface_name + "'."); | |
229 response_sender.Run(std::move(error_response)); | |
230 return; | |
231 } | |
232 | |
233 // Only the "Value" property is writeable. | |
234 if (property_name != bluetooth_gatt_characteristic::kValueProperty) { | |
235 std::string error_name; | |
236 std::string error_message; | |
237 if (property_name == bluetooth_gatt_characteristic::kUUIDProperty || | |
238 property_name == bluetooth_gatt_characteristic::kServiceProperty) { | |
239 error_name = kErrorPropertyReadOnly; | |
240 error_message = "Read-only property: '" + property_name + "'."; | |
241 } else { | |
242 error_name = kErrorInvalidArgs; | |
243 error_message = "No such property: '" + property_name + "'."; | |
244 } | |
245 std::unique_ptr<dbus::ErrorResponse> error_response = | |
246 dbus::ErrorResponse::FromMethodCall(method_call, error_name, | |
247 error_message); | |
248 response_sender.Run(std::move(error_response)); | |
249 return; | |
250 } | |
251 | |
252 // Obtain the value. | |
253 const uint8_t* bytes = NULL; | |
254 size_t length = 0; | |
255 if (!variant_reader.PopArrayOfBytes(&bytes, &length)) { | |
256 std::unique_ptr<dbus::ErrorResponse> error_response = | |
257 dbus::ErrorResponse::FromMethodCall( | |
258 method_call, kErrorInvalidArgs, | |
259 "Property '" + property_name + "' has type 'ay'."); | |
260 response_sender.Run(std::move(error_response)); | |
261 return; | |
262 } | |
263 | |
264 // Pass the set request onto the delegate. | |
265 std::vector<uint8_t> value(bytes, bytes + length); | |
266 DCHECK(delegate_); | |
267 delegate_->SetCharacteristicValue( | |
268 value, | |
269 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnSet, | |
270 weak_ptr_factory_.GetWeakPtr(), method_call, | |
271 response_sender), | |
272 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure, | |
273 weak_ptr_factory_.GetWeakPtr(), method_call, | |
274 response_sender)); | |
275 } | |
276 | |
277 // Called by dbus:: when the Bluetooth daemon fetches all properties of the | |
278 // characteristic. | |
279 void GetAll(dbus::MethodCall* method_call, | |
280 dbus::ExportedObject::ResponseSender response_sender) { | |
281 VLOG(2) << "BluetoothGattCharacteristicServiceProvider::GetAll: " | |
282 << object_path_.value(); | |
283 DCHECK(OnOriginThread()); | |
284 | |
285 dbus::MessageReader reader(method_call); | |
286 | |
287 std::string interface_name; | |
288 if (!reader.PopString(&interface_name) || reader.HasMoreData()) { | |
289 std::unique_ptr<dbus::ErrorResponse> error_response = | |
290 dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs, | |
291 "Expected 's'."); | |
292 response_sender.Run(std::move(error_response)); | |
293 return; | |
294 } | |
295 | |
296 // Only the GATT characteristic interface is supported. | |
297 if (interface_name != | |
298 bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface) { | |
299 std::unique_ptr<dbus::ErrorResponse> error_response = | |
300 dbus::ErrorResponse::FromMethodCall( | |
301 method_call, kErrorInvalidArgs, | |
302 "No such interface: '" + interface_name + "'."); | |
303 response_sender.Run(std::move(error_response)); | |
304 return; | |
305 } | |
306 | |
307 // Try to obtain the value from the delegate. We will construct the | |
308 // response in the success callback. | |
309 DCHECK(delegate_); | |
310 delegate_->GetCharacteristicValue( | |
311 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnGetAll, | |
312 weak_ptr_factory_.GetWeakPtr(), method_call, | |
313 response_sender), | |
314 base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure, | |
315 weak_ptr_factory_.GetWeakPtr(), method_call, | |
316 response_sender)); | |
317 } | |
318 | |
319 // Called by dbus:: when a method is exported. | |
320 void OnExported(const std::string& interface_name, | |
321 const std::string& method_name, | |
322 bool success) { | |
323 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." | |
324 << method_name; | |
325 } | |
326 | |
327 // Called by the Delegate in response to a method to call to get all | |
328 // properties, in which the delegate has successfully returned the | |
329 // characteristic value. | |
330 void OnGetAll(dbus::MethodCall* method_call, | |
331 dbus::ExportedObject::ResponseSender response_sender, | |
332 const std::vector<uint8_t>& value) { | |
333 VLOG(2) << "Characteristic value obtained from delegate. Responding to " | |
334 << "GetAll."; | |
335 | |
336 std::unique_ptr<dbus::Response> response = | |
337 dbus::Response::FromMethodCall(method_call); | |
338 dbus::MessageWriter writer(response.get()); | |
339 dbus::MessageWriter array_writer(NULL); | |
340 dbus::MessageWriter dict_entry_writer(NULL); | |
341 dbus::MessageWriter variant_writer(NULL); | |
342 | |
343 writer.OpenArray("{sv}", &array_writer); | |
344 | |
345 array_writer.OpenDictEntry(&dict_entry_writer); | |
346 dict_entry_writer.AppendString( | |
347 bluetooth_gatt_characteristic::kUUIDProperty); | |
348 dict_entry_writer.AppendVariantOfString(uuid_); | |
349 array_writer.CloseContainer(&dict_entry_writer); | |
350 | |
351 array_writer.OpenDictEntry(&dict_entry_writer); | |
352 dict_entry_writer.AppendString( | |
353 bluetooth_gatt_characteristic::kServiceProperty); | |
354 dict_entry_writer.AppendVariantOfObjectPath(service_path_); | |
355 array_writer.CloseContainer(&dict_entry_writer); | |
356 | |
357 array_writer.OpenDictEntry(&dict_entry_writer); | |
358 dict_entry_writer.AppendString( | |
359 bluetooth_gatt_characteristic::kValueProperty); | |
360 dict_entry_writer.OpenVariant("ay", &variant_writer); | |
361 variant_writer.AppendArrayOfBytes(value.data(), value.size()); | |
362 dict_entry_writer.CloseContainer(&variant_writer); | |
363 array_writer.CloseContainer(&dict_entry_writer); | |
364 | |
365 // TODO(armansito): Process Flags & Permissions properties. | |
366 | |
367 writer.CloseContainer(&array_writer); | |
368 | |
369 response_sender.Run(std::move(response)); | |
370 } | |
371 | |
372 // Called by the Delegate in response to a successful method call to get the | |
373 // characteristic value. | |
374 void OnGet(dbus::MethodCall* method_call, | |
375 dbus::ExportedObject::ResponseSender response_sender, | |
376 const std::vector<uint8_t>& value) { | |
377 VLOG(2) << "Returning characteristic value obtained from delegate."; | |
378 std::unique_ptr<dbus::Response> response = | |
379 dbus::Response::FromMethodCall(method_call); | |
380 dbus::MessageWriter writer(response.get()); | |
381 dbus::MessageWriter variant_writer(NULL); | |
382 | |
383 writer.OpenVariant("ay", &variant_writer); | |
384 variant_writer.AppendArrayOfBytes(value.data(), value.size()); | |
385 writer.CloseContainer(&variant_writer); | |
386 | |
387 response_sender.Run(std::move(response)); | |
388 } | |
389 | |
390 // Called by the Delegate in response to a successful method call to set the | |
391 // characteristic value. | |
392 void OnSet(dbus::MethodCall* method_call, | |
393 dbus::ExportedObject::ResponseSender response_sender) { | |
394 VLOG(2) << "Successfully set characteristic value. Return success."; | |
395 response_sender.Run(dbus::Response::FromMethodCall(method_call)); | |
396 } | |
397 | |
398 // Called by the Delegate in response to a failed method call to get or set | |
399 // the characteristic value. | |
400 void OnFailure(dbus::MethodCall* method_call, | |
401 dbus::ExportedObject::ResponseSender response_sender) { | |
402 VLOG(2) << "Failed to get/set characteristic value. Report error."; | |
403 std::unique_ptr<dbus::ErrorResponse> error_response = | |
404 dbus::ErrorResponse::FromMethodCall( | |
405 method_call, kErrorFailed, | |
406 "Failed to get/set characteristic value."); | |
407 response_sender.Run(std::move(error_response)); | |
408 } | |
409 | |
410 // Origin thread (i.e. the UI thread in production). | |
411 base::PlatformThreadId origin_thread_id_; | |
412 | |
413 // 128-bit characteristic UUID of this object. | |
414 std::string uuid_; | |
415 | |
416 // D-Bus bus object is exported on, not owned by this object and must | |
417 // outlive it. | |
418 dbus::Bus* bus_; | |
419 | |
420 // Incoming methods to get and set the "Value" property are passed on to the | |
421 // delegate and callbacks passed to generate a reply. |delegate_| is generally | |
422 // the object that owns this one and must outlive it. | |
423 Delegate* delegate_; | |
424 | |
425 // D-Bus object path of object we are exporting, kept so we can unregister | |
426 // again in our destructor. | |
427 dbus::ObjectPath object_path_; | |
428 | |
429 // Object path of the GATT service that the exported characteristic belongs | |
430 // to. | |
431 dbus::ObjectPath service_path_; | |
432 | |
433 // D-Bus object we are exporting, owned by this object. | |
434 scoped_refptr<dbus::ExportedObject> exported_object_; | |
435 | |
436 // Weak pointer factory for generating 'this' pointers that might live longer | |
437 // than we do. | |
438 // Note: This should remain the last member so it'll be destroyed and | |
439 // invalidate its weak pointers before any other members are destroyed. | |
440 base::WeakPtrFactory<BluetoothGattCharacteristicServiceProviderImpl> | |
441 weak_ptr_factory_; | |
442 | |
443 DISALLOW_COPY_AND_ASSIGN(BluetoothGattCharacteristicServiceProviderImpl); | |
444 }; | |
445 | 12 |
446 BluetoothGattCharacteristicServiceProvider:: | 13 BluetoothGattCharacteristicServiceProvider:: |
447 BluetoothGattCharacteristicServiceProvider() {} | 14 BluetoothGattCharacteristicServiceProvider() {} |
448 | 15 |
449 BluetoothGattCharacteristicServiceProvider:: | 16 BluetoothGattCharacteristicServiceProvider:: |
450 ~BluetoothGattCharacteristicServiceProvider() {} | 17 ~BluetoothGattCharacteristicServiceProvider() {} |
451 | 18 |
452 // static | 19 // static |
453 BluetoothGattCharacteristicServiceProvider* | 20 BluetoothGattCharacteristicServiceProvider* |
454 BluetoothGattCharacteristicServiceProvider::Create( | 21 BluetoothGattCharacteristicServiceProvider::Create( |
455 dbus::Bus* bus, | 22 dbus::Bus* bus, |
456 const dbus::ObjectPath& object_path, | 23 const dbus::ObjectPath& object_path, |
457 Delegate* delegate, | 24 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
458 const std::string& uuid, | 25 const std::string& uuid, |
459 const std::vector<std::string>& flags, | 26 const std::vector<std::string>& flags, |
460 const std::vector<std::string>& permissions, | 27 const std::vector<std::string>& permissions, |
461 const dbus::ObjectPath& service_path) { | 28 const dbus::ObjectPath& service_path) { |
462 if (!bluez::BluezDBusManager::Get()->IsUsingFakes()) { | 29 if (!bluez::BluezDBusManager::Get()->IsUsingFakes()) { |
463 return new BluetoothGattCharacteristicServiceProviderImpl( | 30 return new BluetoothGattCharacteristicServiceProviderImpl( |
464 bus, object_path, delegate, uuid, flags, permissions, service_path); | 31 bus, object_path, std::move(delegate), uuid, flags, permissions, |
| 32 service_path); |
465 } | 33 } |
466 return new FakeBluetoothGattCharacteristicServiceProvider( | 34 return new FakeBluetoothGattCharacteristicServiceProvider( |
467 object_path, delegate, uuid, flags, permissions, service_path); | 35 object_path, std::move(delegate), uuid, flags, permissions, service_path); |
468 } | 36 } |
469 | 37 |
470 } // namespace bluez | 38 } // namespace bluez |
OLD | NEW |