| 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 "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider_impl.
h" | 5 #include "device/bluetooth/dbus/bluetooth_gatt_descriptor_service_provider_impl.
h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <cstddef> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "third_party/cros_system_api/dbus/service_constants.h" | 12 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 13 | 13 |
| 14 namespace bluez { | 14 namespace bluez { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; | 18 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs"; |
| 19 const char kErrorPropertyReadOnly[] = | 19 const char kErrorPropertyReadOnly[] = |
| 20 "org.freedesktop.DBus.Error.PropertyReadOnly"; | 20 "org.freedesktop.DBus.Error.PropertyReadOnly"; |
| 21 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed"; | 21 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed"; |
| 22 | 22 |
| 23 } // namespace | 23 } // namespace |
| 24 | 24 |
| 25 // The BluetoothGattDescriptorServiceProvider implementation used in production. | 25 // The BluetoothGattDescriptorServiceProvider implementation used in production. |
| 26 BluetoothGattDescriptorServiceProviderImpl:: | 26 BluetoothGattDescriptorServiceProviderImpl:: |
| 27 BluetoothGattDescriptorServiceProviderImpl( | 27 BluetoothGattDescriptorServiceProviderImpl( |
| 28 dbus::Bus* bus, | 28 dbus::Bus* bus, |
| 29 const dbus::ObjectPath& object_path, | 29 const dbus::ObjectPath& object_path, |
| 30 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, | 30 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, |
| 31 const std::string& uuid, | 31 const std::string& uuid, |
| 32 const std::vector<std::string>& permissions, | 32 const std::vector<std::string>& flags, |
| 33 const dbus::ObjectPath& characteristic_path) | 33 const dbus::ObjectPath& characteristic_path) |
| 34 : origin_thread_id_(base::PlatformThread::CurrentId()), | 34 : origin_thread_id_(base::PlatformThread::CurrentId()), |
| 35 uuid_(uuid), | 35 uuid_(uuid), |
| 36 flags_(flags), |
| 36 bus_(bus), | 37 bus_(bus), |
| 37 delegate_(std::move(delegate)), | 38 delegate_(std::move(delegate)), |
| 38 object_path_(object_path), | 39 object_path_(object_path), |
| 39 characteristic_path_(characteristic_path), | 40 characteristic_path_(characteristic_path), |
| 40 weak_ptr_factory_(this) { | 41 weak_ptr_factory_(this) { |
| 41 VLOG(1) << "Created Bluetooth GATT characteristic descriptor: " | 42 VLOG(1) << "Created Bluetooth GATT characteristic descriptor: " |
| 42 << object_path.value() << " UUID: " << uuid; | 43 << object_path.value() << " UUID: " << uuid; |
| 43 DCHECK(bus_); | 44 |
| 45 // If we have a null bus, this means that this is being initialized for a |
| 46 // test, hence we shouldn't do any other setup. |
| 47 if (!bus_) |
| 48 return; |
| 49 |
| 44 DCHECK(delegate_); | 50 DCHECK(delegate_); |
| 45 DCHECK(!uuid_.empty()); | 51 DCHECK(!uuid_.empty()); |
| 46 DCHECK(object_path_.IsValid()); | 52 DCHECK(object_path_.IsValid()); |
| 47 DCHECK(characteristic_path_.IsValid()); | 53 DCHECK(characteristic_path_.IsValid()); |
| 48 DCHECK(base::StartsWith(object_path_.value(), | 54 DCHECK(base::StartsWith(object_path_.value(), |
| 49 characteristic_path_.value() + "/", | 55 characteristic_path_.value() + "/", |
| 50 base::CompareCase::SENSITIVE)); | 56 base::CompareCase::SENSITIVE)); |
| 51 | 57 |
| 52 exported_object_ = bus_->GetExportedObject(object_path_); | 58 exported_object_ = bus_->GetExportedObject(object_path_); |
| 53 | 59 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 74 } | 80 } |
| 75 | 81 |
| 76 BluetoothGattDescriptorServiceProviderImpl:: | 82 BluetoothGattDescriptorServiceProviderImpl:: |
| 77 ~BluetoothGattDescriptorServiceProviderImpl() { | 83 ~BluetoothGattDescriptorServiceProviderImpl() { |
| 78 VLOG(1) << "Cleaning up Bluetooth GATT characteristic descriptor: " | 84 VLOG(1) << "Cleaning up Bluetooth GATT characteristic descriptor: " |
| 79 << object_path_.value(); | 85 << object_path_.value(); |
| 80 if (bus_) | 86 if (bus_) |
| 81 bus_->UnregisterExportedObject(object_path_); | 87 bus_->UnregisterExportedObject(object_path_); |
| 82 } | 88 } |
| 83 | 89 |
| 84 BluetoothGattDescriptorServiceProviderImpl:: | |
| 85 BluetoothGattDescriptorServiceProviderImpl( | |
| 86 const dbus::ObjectPath& object_path, | |
| 87 const std::string& uuid, | |
| 88 const dbus::ObjectPath& characteristic_path) | |
| 89 : origin_thread_id_(base::PlatformThread::CurrentId()), | |
| 90 uuid_(uuid), | |
| 91 bus_(nullptr), | |
| 92 delegate_(nullptr), | |
| 93 object_path_(object_path), | |
| 94 characteristic_path_(characteristic_path), | |
| 95 weak_ptr_factory_(this) {} | |
| 96 | |
| 97 void BluetoothGattDescriptorServiceProviderImpl::SendValueChanged( | 90 void BluetoothGattDescriptorServiceProviderImpl::SendValueChanged( |
| 98 const std::vector<uint8_t>& value) { | 91 const std::vector<uint8_t>& value) { |
| 99 VLOG(2) << "Emitting a PropertiesChanged signal for descriptor value."; | 92 VLOG(2) << "Emitting a PropertiesChanged signal for descriptor value."; |
| 100 dbus::Signal signal(dbus::kDBusPropertiesInterface, | 93 dbus::Signal signal(dbus::kDBusPropertiesInterface, |
| 101 dbus::kDBusPropertiesChangedSignal); | 94 dbus::kDBusPropertiesChangedSignal); |
| 102 dbus::MessageWriter writer(&signal); | 95 dbus::MessageWriter writer(&signal); |
| 103 dbus::MessageWriter array_writer(NULL); | 96 dbus::MessageWriter array_writer(NULL); |
| 104 dbus::MessageWriter dict_entry_writer(NULL); | 97 dbus::MessageWriter dict_entry_writer(NULL); |
| 105 dbus::MessageWriter variant_writer(NULL); | 98 dbus::MessageWriter variant_writer(NULL); |
| 106 | 99 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 weak_ptr_factory_.GetWeakPtr(), method_call, | 164 weak_ptr_factory_.GetWeakPtr(), method_call, |
| 172 response_sender)); | 165 response_sender)); |
| 173 return; | 166 return; |
| 174 } | 167 } |
| 175 | 168 |
| 176 std::unique_ptr<dbus::Response> response = | 169 std::unique_ptr<dbus::Response> response = |
| 177 dbus::Response::FromMethodCall(method_call); | 170 dbus::Response::FromMethodCall(method_call); |
| 178 dbus::MessageWriter writer(response.get()); | 171 dbus::MessageWriter writer(response.get()); |
| 179 dbus::MessageWriter variant_writer(NULL); | 172 dbus::MessageWriter variant_writer(NULL); |
| 180 | 173 |
| 181 // TODO(armansito): Process the "Permissions" property below. | |
| 182 if (property_name == bluetooth_gatt_descriptor::kUUIDProperty) { | 174 if (property_name == bluetooth_gatt_descriptor::kUUIDProperty) { |
| 183 writer.OpenVariant("s", &variant_writer); | 175 writer.OpenVariant("s", &variant_writer); |
| 184 variant_writer.AppendString(uuid_); | 176 variant_writer.AppendString(uuid_); |
| 185 writer.CloseContainer(&variant_writer); | 177 writer.CloseContainer(&variant_writer); |
| 186 } else if (property_name == | 178 } else if (property_name == |
| 187 bluetooth_gatt_descriptor::kCharacteristicProperty) { | 179 bluetooth_gatt_descriptor::kCharacteristicProperty) { |
| 188 writer.OpenVariant("o", &variant_writer); | 180 writer.OpenVariant("o", &variant_writer); |
| 189 variant_writer.AppendObjectPath(characteristic_path_); | 181 variant_writer.AppendObjectPath(characteristic_path_); |
| 190 writer.CloseContainer(&variant_writer); | 182 writer.CloseContainer(&variant_writer); |
| 183 } else if (property_name == bluetooth_gatt_descriptor::kFlagsProperty) { |
| 184 writer.OpenVariant("as", &variant_writer); |
| 185 variant_writer.AppendArrayOfStrings(flags_); |
| 186 writer.CloseContainer(&variant_writer); |
| 191 } else { | 187 } else { |
| 192 response = dbus::ErrorResponse::FromMethodCall( | 188 response = dbus::ErrorResponse::FromMethodCall( |
| 193 method_call, kErrorInvalidArgs, | 189 method_call, kErrorInvalidArgs, |
| 194 "No such property: '" + property_name + "'."); | 190 "No such property: '" + property_name + "'."); |
| 195 } | 191 } |
| 196 | 192 |
| 197 response_sender.Run(std::move(response)); | 193 response_sender.Run(std::move(response)); |
| 198 } | 194 } |
| 199 | 195 |
| 200 void BluetoothGattDescriptorServiceProviderImpl::Set( | 196 void BluetoothGattDescriptorServiceProviderImpl::Set( |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 | 330 |
| 335 void BluetoothGattDescriptorServiceProviderImpl::WriteProperties( | 331 void BluetoothGattDescriptorServiceProviderImpl::WriteProperties( |
| 336 dbus::MessageWriter* writer, | 332 dbus::MessageWriter* writer, |
| 337 const std::vector<uint8_t>* value) { | 333 const std::vector<uint8_t>* value) { |
| 338 dbus::MessageWriter array_writer(NULL); | 334 dbus::MessageWriter array_writer(NULL); |
| 339 dbus::MessageWriter dict_entry_writer(NULL); | 335 dbus::MessageWriter dict_entry_writer(NULL); |
| 340 dbus::MessageWriter variant_writer(NULL); | 336 dbus::MessageWriter variant_writer(NULL); |
| 341 | 337 |
| 342 writer->OpenArray("{sv}", &array_writer); | 338 writer->OpenArray("{sv}", &array_writer); |
| 343 | 339 |
| 340 // UUID: |
| 344 array_writer.OpenDictEntry(&dict_entry_writer); | 341 array_writer.OpenDictEntry(&dict_entry_writer); |
| 345 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kUUIDProperty); | 342 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kUUIDProperty); |
| 346 dict_entry_writer.AppendVariantOfString(uuid_); | 343 dict_entry_writer.AppendVariantOfString(uuid_); |
| 347 array_writer.CloseContainer(&dict_entry_writer); | 344 array_writer.CloseContainer(&dict_entry_writer); |
| 348 | 345 |
| 346 // Characteristic: |
| 349 array_writer.OpenDictEntry(&dict_entry_writer); | 347 array_writer.OpenDictEntry(&dict_entry_writer); |
| 350 dict_entry_writer.AppendString( | 348 dict_entry_writer.AppendString( |
| 351 bluetooth_gatt_descriptor::kCharacteristicProperty); | 349 bluetooth_gatt_descriptor::kCharacteristicProperty); |
| 352 dict_entry_writer.AppendVariantOfObjectPath(characteristic_path_); | 350 dict_entry_writer.AppendVariantOfObjectPath(characteristic_path_); |
| 353 array_writer.CloseContainer(&dict_entry_writer); | 351 array_writer.CloseContainer(&dict_entry_writer); |
| 354 | 352 |
| 355 if (value) { | 353 if (value) { |
| 354 // Value: |
| 356 array_writer.OpenDictEntry(&dict_entry_writer); | 355 array_writer.OpenDictEntry(&dict_entry_writer); |
| 357 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kValueProperty); | 356 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kValueProperty); |
| 358 dict_entry_writer.OpenVariant("ay", &variant_writer); | 357 dict_entry_writer.OpenVariant("ay", &variant_writer); |
| 359 variant_writer.AppendArrayOfBytes(value->data(), value->size()); | 358 variant_writer.AppendArrayOfBytes(value->data(), value->size()); |
| 360 dict_entry_writer.CloseContainer(&variant_writer); | 359 dict_entry_writer.CloseContainer(&variant_writer); |
| 361 array_writer.CloseContainer(&dict_entry_writer); | 360 array_writer.CloseContainer(&dict_entry_writer); |
| 362 } | 361 } |
| 363 | 362 |
| 364 // TODO(armansito): Process "Permissions" property. | 363 // Flags: |
| 364 array_writer.OpenDictEntry(&dict_entry_writer); |
| 365 dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kFlagsProperty); |
| 366 dict_entry_writer.OpenVariant("as", &variant_writer); |
| 367 variant_writer.AppendArrayOfStrings(flags_); |
| 368 dict_entry_writer.CloseContainer(&variant_writer); |
| 369 array_writer.CloseContainer(&dict_entry_writer); |
| 370 |
| 365 writer->CloseContainer(&array_writer); | 371 writer->CloseContainer(&array_writer); |
| 366 } | 372 } |
| 367 | 373 |
| 368 // Called by the Delegate in response to a successful method call to get the | 374 // Called by the Delegate in response to a successful method call to get the |
| 369 // descriptor value. | 375 // descriptor value. |
| 370 void BluetoothGattDescriptorServiceProviderImpl::OnGet( | 376 void BluetoothGattDescriptorServiceProviderImpl::OnGet( |
| 371 dbus::MethodCall* method_call, | 377 dbus::MethodCall* method_call, |
| 372 dbus::ExportedObject::ResponseSender response_sender, | 378 dbus::ExportedObject::ResponseSender response_sender, |
| 373 const std::vector<uint8_t>& value) { | 379 const std::vector<uint8_t>& value) { |
| 374 VLOG(2) << "Returning descriptor value obtained from delegate."; | 380 VLOG(2) << "Returning descriptor value obtained from delegate."; |
| (...skipping 25 matching lines...) Expand all Loading... |
| 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 |
| 404 const dbus::ObjectPath& | 410 const dbus::ObjectPath& |
| 405 BluetoothGattDescriptorServiceProviderImpl::object_path() const { | 411 BluetoothGattDescriptorServiceProviderImpl::object_path() const { |
| 406 return object_path_; | 412 return object_path_; |
| 407 } | 413 } |
| 408 | 414 |
| 409 } // namespace bluez | 415 } // namespace bluez |
| OLD | NEW |