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_descriptor_service_provider.h" | 5 #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | |
10 #include <utility> | |
11 | |
12 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" |
13 #include "base/logging.h" | 11 #include "base/logging.h" |
14 #include "base/macros.h" | 12 #include "base/memory/ref_counted.h" |
15 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
16 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
17 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
18 #include "dbus/exported_object.h" | 16 #include "dbus/exported_object.h" |
19 #include "dbus/message.h" | |
20 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" |
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.
h" | 18 #include "device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_service_provider.
h" |
22 #include "third_party/cros_system_api/dbus/service_constants.h" | 19 #include "third_party/cros_system_api/dbus/service_constants.h" |
23 | 20 |
24 namespace bluez { | 21 namespace bluez { |
25 namespace { | 22 namespace { |
26 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; | 23 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; |
27 const char kErrorPropertyReadOnly[] = | 24 const char kErrorPropertyReadOnly[] = |
28 "org.freedesktop.DBus.Error.PropertyReadOnly"; | 25 "org.freedesktop.DBus.Error.PropertyReadOnly"; |
29 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed"; | 26 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed"; |
30 } // namespace | 27 } // namespace |
31 | 28 |
32 // The BluetoothGattDescriptorServiceProvider implementation used in production. | 29 // The BluetoothGattDescriptorServiceProvider implementation used in production. |
33 class BluetoothGattDescriptorServiceProviderImpl | 30 class BluetoothGattDescriptorServiceProviderImpl |
34 : public BluetoothGattDescriptorServiceProvider { | 31 : public BluetoothGattDescriptorServiceProvider { |
35 public: | 32 public: |
36 BluetoothGattDescriptorServiceProviderImpl( | 33 BluetoothGattDescriptorServiceProviderImpl( |
37 dbus::Bus* bus, | 34 dbus::Bus* bus, |
38 const dbus::ObjectPath& object_path, | 35 const dbus::ObjectPath& object_path, |
39 Delegate* delegate, | 36 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
40 const std::string& uuid, | 37 const std::string& uuid, |
41 const std::vector<std::string>& permissions, | 38 const std::vector<std::string>& permissions, |
42 const dbus::ObjectPath& characteristic_path) | 39 const dbus::ObjectPath& characteristic_path) |
43 : origin_thread_id_(base::PlatformThread::CurrentId()), | 40 : origin_thread_id_(base::PlatformThread::CurrentId()), |
44 uuid_(uuid), | 41 uuid_(uuid), |
45 bus_(bus), | 42 bus_(bus), |
46 delegate_(delegate), | 43 delegate_(std::move(delegate)), |
47 object_path_(object_path), | 44 object_path_(object_path), |
48 characteristic_path_(characteristic_path), | 45 characteristic_path_(characteristic_path), |
49 weak_ptr_factory_(this) { | 46 weak_ptr_factory_(this) { |
50 VLOG(1) << "Created Bluetooth GATT characteristic descriptor: " | 47 VLOG(1) << "Created Bluetooth GATT characteristic descriptor: " |
51 << object_path.value() << " UUID: " << uuid; | 48 << object_path.value() << " UUID: " << uuid; |
52 DCHECK(bus_); | 49 DCHECK(bus_); |
53 DCHECK(delegate_); | 50 DCHECK(delegate_); |
54 DCHECK(!uuid_.empty()); | 51 DCHECK(!uuid_.empty()); |
55 DCHECK(object_path_.IsValid()); | 52 DCHECK(object_path_.IsValid()); |
56 DCHECK(characteristic_path_.IsValid()); | 53 DCHECK(characteristic_path_.IsValid()); |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 dbus::ErrorResponse::FromMethodCall( | 150 dbus::ErrorResponse::FromMethodCall( |
154 method_call, kErrorInvalidArgs, | 151 method_call, kErrorInvalidArgs, |
155 "No such interface: '" + interface_name + "'."); | 152 "No such interface: '" + interface_name + "'."); |
156 response_sender.Run(std::move(error_response)); | 153 response_sender.Run(std::move(error_response)); |
157 return; | 154 return; |
158 } | 155 } |
159 | 156 |
160 // If getting the "Value" property, obtain the value from the delegate. | 157 // If getting the "Value" property, obtain the value from the delegate. |
161 if (property_name == bluetooth_gatt_descriptor::kValueProperty) { | 158 if (property_name == bluetooth_gatt_descriptor::kValueProperty) { |
162 DCHECK(delegate_); | 159 DCHECK(delegate_); |
163 delegate_->GetDescriptorValue( | 160 delegate_->GetValue( |
164 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGet, | 161 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGet, |
165 weak_ptr_factory_.GetWeakPtr(), method_call, | 162 weak_ptr_factory_.GetWeakPtr(), method_call, |
166 response_sender), | 163 response_sender), |
167 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, | 164 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, |
168 weak_ptr_factory_.GetWeakPtr(), method_call, | 165 weak_ptr_factory_.GetWeakPtr(), method_call, |
169 response_sender)); | 166 response_sender)); |
170 return; | 167 return; |
171 } | 168 } |
172 | 169 |
173 std::unique_ptr<dbus::Response> response = | 170 std::unique_ptr<dbus::Response> response = |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 dbus::ErrorResponse::FromMethodCall( | 252 dbus::ErrorResponse::FromMethodCall( |
256 method_call, kErrorInvalidArgs, | 253 method_call, kErrorInvalidArgs, |
257 "Property '" + property_name + "' has type 'ay'."); | 254 "Property '" + property_name + "' has type 'ay'."); |
258 response_sender.Run(std::move(error_response)); | 255 response_sender.Run(std::move(error_response)); |
259 return; | 256 return; |
260 } | 257 } |
261 | 258 |
262 // Pass the set request onto the delegate. | 259 // Pass the set request onto the delegate. |
263 std::vector<uint8_t> value(bytes, bytes + length); | 260 std::vector<uint8_t> value(bytes, bytes + length); |
264 DCHECK(delegate_); | 261 DCHECK(delegate_); |
265 delegate_->SetDescriptorValue( | 262 delegate_->SetValue( |
266 value, base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnSet, | 263 value, base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnSet, |
267 weak_ptr_factory_.GetWeakPtr(), method_call, | 264 weak_ptr_factory_.GetWeakPtr(), method_call, |
268 response_sender), | 265 response_sender), |
269 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, | 266 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, |
270 weak_ptr_factory_.GetWeakPtr(), method_call, | 267 weak_ptr_factory_.GetWeakPtr(), method_call, |
271 response_sender)); | 268 response_sender)); |
272 } | 269 } |
273 | 270 |
274 // Called by dbus:: when the Bluetooth daemon fetches all properties of the | 271 // Called by dbus:: when the Bluetooth daemon fetches all properties of the |
275 // descriptor. | 272 // descriptor. |
(...skipping 21 matching lines...) Expand all Loading... |
297 dbus::ErrorResponse::FromMethodCall( | 294 dbus::ErrorResponse::FromMethodCall( |
298 method_call, kErrorInvalidArgs, | 295 method_call, kErrorInvalidArgs, |
299 "No such interface: '" + interface_name + "'."); | 296 "No such interface: '" + interface_name + "'."); |
300 response_sender.Run(std::move(error_response)); | 297 response_sender.Run(std::move(error_response)); |
301 return; | 298 return; |
302 } | 299 } |
303 | 300 |
304 // Try to obtain the value from the delegate. We will construct the | 301 // Try to obtain the value from the delegate. We will construct the |
305 // response in the success callback. | 302 // response in the success callback. |
306 DCHECK(delegate_); | 303 DCHECK(delegate_); |
307 delegate_->GetDescriptorValue( | 304 delegate_->GetValue( |
308 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGetAll, | 305 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGetAll, |
309 weak_ptr_factory_.GetWeakPtr(), method_call, | 306 weak_ptr_factory_.GetWeakPtr(), method_call, |
310 response_sender), | 307 response_sender), |
311 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, | 308 base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure, |
312 weak_ptr_factory_.GetWeakPtr(), method_call, | 309 weak_ptr_factory_.GetWeakPtr(), method_call, |
313 response_sender)); | 310 response_sender)); |
314 } | 311 } |
315 | 312 |
316 // Called by dbus:: when a method is exported. | 313 // Called by dbus:: when a method is exported. |
317 void OnExported(const std::string& interface_name, | 314 void OnExported(const std::string& interface_name, |
318 const std::string& method_name, | 315 const std::string& method_name, |
319 bool success) { | 316 bool success) { |
320 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." | 317 LOG_IF(WARNING, !success) << "Failed to export " << interface_name << "." |
321 << method_name; | 318 << method_name; |
322 } | 319 } |
323 | 320 |
324 // Called by the Delegate in response to a method to call to get all | 321 // Called by the Delegate in response to a method to call to get all |
325 // properties, in which the delegate has successfully returned the | 322 // properties, in which the delegate has successfully returned the |
326 // descriptor value. | 323 // descriptor value. |
327 void OnGetAll(dbus::MethodCall* method_call, | 324 void OnGetAll(dbus::MethodCall* method_call, |
328 dbus::ExportedObject::ResponseSender response_sender, | 325 dbus::ExportedObject::ResponseSender response_sender, |
329 const std::vector<uint8_t>& value) { | 326 const std::vector<uint8_t>& value) { |
330 VLOG(2) << "Descriptor value obtained from delegate. Responding to " | 327 VLOG(2) << "Descriptor value obtained from delegate. Responding to " |
331 << "GetAll."; | 328 << "GetAll."; |
332 | 329 |
333 std::unique_ptr<dbus::Response> response = | 330 std::unique_ptr<dbus::Response> response = |
334 dbus::Response::FromMethodCall(method_call); | 331 dbus::Response::FromMethodCall(method_call); |
335 dbus::MessageWriter writer(response.get()); | 332 dbus::MessageWriter writer(response.get()); |
| 333 WriteProperties(&writer, &value); |
| 334 response_sender.Run(std::move(response)); |
| 335 } |
| 336 |
| 337 // Writes the characteristics's properties into the provided writer. If |
| 338 // value is not null, it is written also, otherwise no value property is |
| 339 // written. |
| 340 void WriteProperties(dbus::MessageWriter* writer, |
| 341 const std::vector<uint8_t>* value) override { |
336 dbus::MessageWriter array_writer(NULL); | 342 dbus::MessageWriter array_writer(NULL); |
337 dbus::MessageWriter dict_entry_writer(NULL); | 343 dbus::MessageWriter dict_entry_writer(NULL); |
338 dbus::MessageWriter variant_writer(NULL); | 344 dbus::MessageWriter variant_writer(NULL); |
339 | 345 |
340 writer.OpenArray("{sv}", &array_writer); | 346 writer->OpenArray("{sv}", &array_writer); |
341 | 347 |
342 array_writer.OpenDictEntry(&dict_entry_writer); | 348 array_writer.OpenDictEntry(&dict_entry_writer); |
343 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kUUIDProperty); | 349 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kUUIDProperty); |
344 dict_entry_writer.AppendVariantOfString(uuid_); | 350 dict_entry_writer.AppendVariantOfString(uuid_); |
345 array_writer.CloseContainer(&dict_entry_writer); | 351 array_writer.CloseContainer(&dict_entry_writer); |
346 | 352 |
347 array_writer.OpenDictEntry(&dict_entry_writer); | 353 array_writer.OpenDictEntry(&dict_entry_writer); |
348 dict_entry_writer.AppendString( | 354 dict_entry_writer.AppendString( |
349 bluetooth_gatt_descriptor::kCharacteristicProperty); | 355 bluetooth_gatt_descriptor::kCharacteristicProperty); |
350 dict_entry_writer.AppendVariantOfObjectPath(characteristic_path_); | 356 dict_entry_writer.AppendVariantOfObjectPath(characteristic_path_); |
351 array_writer.CloseContainer(&dict_entry_writer); | 357 array_writer.CloseContainer(&dict_entry_writer); |
352 | 358 |
353 array_writer.OpenDictEntry(&dict_entry_writer); | 359 if (value) { |
354 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kValueProperty); | 360 array_writer.OpenDictEntry(&dict_entry_writer); |
355 dict_entry_writer.OpenVariant("ay", &variant_writer); | 361 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kValueProperty); |
356 variant_writer.AppendArrayOfBytes(value.data(), value.size()); | 362 dict_entry_writer.OpenVariant("ay", &variant_writer); |
357 dict_entry_writer.CloseContainer(&variant_writer); | 363 variant_writer.AppendArrayOfBytes(value->data(), value->size()); |
358 array_writer.CloseContainer(&dict_entry_writer); | 364 dict_entry_writer.CloseContainer(&variant_writer); |
| 365 array_writer.CloseContainer(&dict_entry_writer); |
| 366 } |
359 | 367 |
360 // TODO(armansito): Process "Permissions" property. | 368 // TODO(armansito): Process "Permissions" property. |
361 | 369 writer->CloseContainer(&array_writer); |
362 writer.CloseContainer(&array_writer); | |
363 | |
364 response_sender.Run(std::move(response)); | |
365 } | 370 } |
366 | 371 |
367 // Called by the Delegate in response to a successful method call to get the | 372 // Called by the Delegate in response to a successful method call to get the |
368 // descriptor value. | 373 // descriptor value. |
369 void OnGet(dbus::MethodCall* method_call, | 374 void OnGet(dbus::MethodCall* method_call, |
370 dbus::ExportedObject::ResponseSender response_sender, | 375 dbus::ExportedObject::ResponseSender response_sender, |
371 const std::vector<uint8_t>& value) { | 376 const std::vector<uint8_t>& value) { |
372 VLOG(2) << "Returning descriptor value obtained from delegate."; | 377 VLOG(2) << "Returning descriptor value obtained from delegate."; |
373 std::unique_ptr<dbus::Response> response = | 378 std::unique_ptr<dbus::Response> response = |
374 dbus::Response::FromMethodCall(method_call); | 379 dbus::Response::FromMethodCall(method_call); |
(...skipping 19 matching lines...) Expand all Loading... |
394 // the descriptor value. | 399 // the descriptor value. |
395 void OnFailure(dbus::MethodCall* method_call, | 400 void OnFailure(dbus::MethodCall* method_call, |
396 dbus::ExportedObject::ResponseSender response_sender) { | 401 dbus::ExportedObject::ResponseSender response_sender) { |
397 VLOG(2) << "Failed to get/set descriptor value. Report error."; | 402 VLOG(2) << "Failed to get/set descriptor value. Report error."; |
398 std::unique_ptr<dbus::ErrorResponse> error_response = | 403 std::unique_ptr<dbus::ErrorResponse> error_response = |
399 dbus::ErrorResponse::FromMethodCall( | 404 dbus::ErrorResponse::FromMethodCall( |
400 method_call, kErrorFailed, "Failed to get/set descriptor value."); | 405 method_call, kErrorFailed, "Failed to get/set descriptor value."); |
401 response_sender.Run(std::move(error_response)); | 406 response_sender.Run(std::move(error_response)); |
402 } | 407 } |
403 | 408 |
| 409 const dbus::ObjectPath& object_path() const override { return object_path_; } |
| 410 |
404 // Origin thread (i.e. the UI thread in production). | 411 // Origin thread (i.e. the UI thread in production). |
405 base::PlatformThreadId origin_thread_id_; | 412 base::PlatformThreadId origin_thread_id_; |
406 | 413 |
407 // 128-bit descriptor UUID of this object. | 414 // 128-bit descriptor UUID of this object. |
408 std::string uuid_; | 415 std::string uuid_; |
409 | 416 |
410 // D-Bus bus object is exported on, not owned by this object and must | 417 // D-Bus bus object is exported on, not owned by this object and must |
411 // outlive it. | 418 // outlive it. |
412 dbus::Bus* bus_; | 419 dbus::Bus* bus_; |
413 | 420 |
414 // Incoming methods to get and set the "Value" property are passed on to the | 421 // Incoming methods to get and set the "Value" property are passed on to the |
415 // delegate and callbacks passed to generate a reply. |delegate_| is generally | 422 // delegate and callbacks passed to generate a reply. |delegate_| is generally |
416 // the object that owns this one and must outlive it. | 423 // the object that owns this one and must outlive it. |
417 Delegate* delegate_; | 424 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate_; |
418 | 425 |
419 // D-Bus object path of object we are exporting, kept so we can unregister | 426 // D-Bus object path of object we are exporting, kept so we can unregister |
420 // again in our destructor. | 427 // again in our destructor. |
421 dbus::ObjectPath object_path_; | 428 dbus::ObjectPath object_path_; |
422 | 429 |
423 // Object path of the GATT characteristic that the exported descriptor belongs | 430 // Object path of the GATT characteristic that the exported descriptor belongs |
424 // to. | 431 // to. |
425 dbus::ObjectPath characteristic_path_; | 432 dbus::ObjectPath characteristic_path_; |
426 | 433 |
427 // D-Bus object we are exporting, owned by this object. | 434 // D-Bus object we are exporting, owned by this object. |
(...skipping 13 matching lines...) Expand all Loading... |
441 BluetoothGattDescriptorServiceProvider() {} | 448 BluetoothGattDescriptorServiceProvider() {} |
442 | 449 |
443 BluetoothGattDescriptorServiceProvider:: | 450 BluetoothGattDescriptorServiceProvider:: |
444 ~BluetoothGattDescriptorServiceProvider() {} | 451 ~BluetoothGattDescriptorServiceProvider() {} |
445 | 452 |
446 // static | 453 // static |
447 BluetoothGattDescriptorServiceProvider* | 454 BluetoothGattDescriptorServiceProvider* |
448 BluetoothGattDescriptorServiceProvider::Create( | 455 BluetoothGattDescriptorServiceProvider::Create( |
449 dbus::Bus* bus, | 456 dbus::Bus* bus, |
450 const dbus::ObjectPath& object_path, | 457 const dbus::ObjectPath& object_path, |
451 Delegate* delegate, | 458 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
452 const std::string& uuid, | 459 const std::string& uuid, |
453 const std::vector<std::string>& permissions, | 460 const std::vector<std::string>& permissions, |
454 const dbus::ObjectPath& characteristic_path) { | 461 const dbus::ObjectPath& characteristic_path) { |
455 if (!bluez::BluezDBusManager::Get()->IsUsingFakes()) { | 462 if (!bluez::BluezDBusManager::Get()->IsUsingFakes()) { |
456 return new BluetoothGattDescriptorServiceProviderImpl( | 463 return new BluetoothGattDescriptorServiceProviderImpl( |
457 bus, object_path, delegate, uuid, permissions, characteristic_path); | 464 bus, object_path, std::move(delegate), uuid, permissions, |
| 465 characteristic_path); |
458 } | 466 } |
459 return new FakeBluetoothGattDescriptorServiceProvider( | 467 return new FakeBluetoothGattDescriptorServiceProvider( |
460 object_path, delegate, uuid, permissions, characteristic_path); | 468 object_path, std::move(delegate), uuid, permissions, characteristic_path); |
461 } | 469 } |
462 | 470 |
463 } // namespace bluez | 471 } // namespace bluez |
OLD | NEW |