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