Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider_impl.cc

Issue 1954643002: DBus support for attribute properties and permissions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@register_and_events
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_characteristic_service_provider_i mpl.h" 5 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider_i mpl.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 BluetoothGattCharacteristicServiceProviderImpl:: 25 BluetoothGattCharacteristicServiceProviderImpl::
26 BluetoothGattCharacteristicServiceProviderImpl( 26 BluetoothGattCharacteristicServiceProviderImpl(
27 dbus::Bus* bus, 27 dbus::Bus* bus,
28 const dbus::ObjectPath& object_path, 28 const dbus::ObjectPath& object_path,
29 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate, 29 std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate,
30 const std::string& uuid, 30 const std::string& uuid,
31 const std::vector<std::string>& flags, 31 const std::vector<std::string>& flags,
32 const std::vector<std::string>& permissions,
33 const dbus::ObjectPath& service_path) 32 const dbus::ObjectPath& service_path)
34 : origin_thread_id_(base::PlatformThread::CurrentId()), 33 : origin_thread_id_(base::PlatformThread::CurrentId()),
35 uuid_(uuid), 34 uuid_(uuid),
35 flags_(flags),
36 bus_(bus), 36 bus_(bus),
37 delegate_(std::move(delegate)), 37 delegate_(std::move(delegate)),
38 object_path_(object_path), 38 object_path_(object_path),
39 service_path_(service_path), 39 service_path_(service_path),
40 weak_ptr_factory_(this) { 40 weak_ptr_factory_(this) {
41 VLOG(1) << "Created Bluetooth GATT characteristic: " << object_path.value() 41 VLOG(1) << "Created Bluetooth GATT characteristic: " << object_path.value()
42 << " UUID: " << uuid; 42 << " UUID: " << uuid;
43 DCHECK(bus_); 43
44 // If we have a null bus, this means that this is being initialized for a
45 // test, hence we shouldn't do any other setup.
46 if (!bus_)
47 return;
48
44 DCHECK(delegate_); 49 DCHECK(delegate_);
45 DCHECK(!uuid_.empty()); 50 DCHECK(!uuid_.empty());
46 DCHECK(object_path_.IsValid()); 51 DCHECK(object_path_.IsValid());
47 DCHECK(service_path_.IsValid()); 52 DCHECK(service_path_.IsValid());
48 DCHECK(base::StartsWith(object_path_.value(), service_path_.value() + "/", 53 DCHECK(base::StartsWith(object_path_.value(), service_path_.value() + "/",
49 base::CompareCase::SENSITIVE)); 54 base::CompareCase::SENSITIVE));
50 55
51 exported_object_ = bus_->GetExportedObject(object_path_); 56 exported_object_ = bus_->GetExportedObject(object_path_);
52 57
53 exported_object_->ExportMethod( 58 exported_object_->ExportMethod(
(...skipping 19 matching lines...) Expand all
73 } 78 }
74 79
75 BluetoothGattCharacteristicServiceProviderImpl:: 80 BluetoothGattCharacteristicServiceProviderImpl::
76 ~BluetoothGattCharacteristicServiceProviderImpl() { 81 ~BluetoothGattCharacteristicServiceProviderImpl() {
77 VLOG(1) << "Cleaning up Bluetooth GATT characteristic: " 82 VLOG(1) << "Cleaning up Bluetooth GATT characteristic: "
78 << object_path_.value(); 83 << object_path_.value();
79 if (bus_) 84 if (bus_)
80 bus_->UnregisterExportedObject(object_path_); 85 bus_->UnregisterExportedObject(object_path_);
81 } 86 }
82 87
83 BluetoothGattCharacteristicServiceProviderImpl::
84 BluetoothGattCharacteristicServiceProviderImpl(
85 const dbus::ObjectPath& object_path,
86 const std::string& uuid,
87 const dbus::ObjectPath& service_path)
88 : origin_thread_id_(base::PlatformThread::CurrentId()),
89 uuid_(uuid),
90 bus_(nullptr),
91 delegate_(nullptr),
92 object_path_(object_path),
93 service_path_(service_path),
94 weak_ptr_factory_(this) {}
95
96 void BluetoothGattCharacteristicServiceProviderImpl::SendValueChanged( 88 void BluetoothGattCharacteristicServiceProviderImpl::SendValueChanged(
97 const std::vector<uint8_t>& value) { 89 const std::vector<uint8_t>& value) {
98 VLOG(2) << "Emitting a PropertiesChanged signal for characteristic value."; 90 VLOG(2) << "Emitting a PropertiesChanged signal for characteristic value.";
99 dbus::Signal signal(dbus::kDBusPropertiesInterface, 91 dbus::Signal signal(dbus::kDBusPropertiesInterface,
100 dbus::kDBusPropertiesChangedSignal); 92 dbus::kDBusPropertiesChangedSignal);
101 dbus::MessageWriter writer(&signal); 93 dbus::MessageWriter writer(&signal);
102 dbus::MessageWriter array_writer(NULL); 94 dbus::MessageWriter array_writer(NULL);
103 dbus::MessageWriter dict_entry_writer(NULL); 95 dbus::MessageWriter dict_entry_writer(NULL);
104 dbus::MessageWriter variant_writer(NULL); 96 dbus::MessageWriter variant_writer(NULL);
105 97
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 weak_ptr_factory_.GetWeakPtr(), method_call, 163 weak_ptr_factory_.GetWeakPtr(), method_call,
172 response_sender)); 164 response_sender));
173 return; 165 return;
174 } 166 }
175 167
176 std::unique_ptr<dbus::Response> response = 168 std::unique_ptr<dbus::Response> response =
177 dbus::Response::FromMethodCall(method_call); 169 dbus::Response::FromMethodCall(method_call);
178 dbus::MessageWriter writer(response.get()); 170 dbus::MessageWriter writer(response.get());
179 dbus::MessageWriter variant_writer(NULL); 171 dbus::MessageWriter variant_writer(NULL);
180 172
181 // TODO(armansito): Process the "Flags" and "Permissions" properties below.
182 if (property_name == bluetooth_gatt_characteristic::kUUIDProperty) { 173 if (property_name == bluetooth_gatt_characteristic::kUUIDProperty) {
183 writer.OpenVariant("s", &variant_writer); 174 writer.OpenVariant("s", &variant_writer);
184 variant_writer.AppendString(uuid_); 175 variant_writer.AppendString(uuid_);
185 writer.CloseContainer(&variant_writer); 176 writer.CloseContainer(&variant_writer);
186 } else if (property_name == bluetooth_gatt_characteristic::kServiceProperty) { 177 } else if (property_name == bluetooth_gatt_characteristic::kServiceProperty) {
187 writer.OpenVariant("o", &variant_writer); 178 writer.OpenVariant("o", &variant_writer);
188 variant_writer.AppendObjectPath(service_path_); 179 variant_writer.AppendObjectPath(service_path_);
189 writer.CloseContainer(&variant_writer); 180 writer.CloseContainer(&variant_writer);
181 } else if (property_name == bluetooth_gatt_characteristic::kFlagsProperty) {
182 writer.OpenVariant("as", &variant_writer);
183 variant_writer.AppendArrayOfStrings(flags_);
184 writer.CloseContainer(&variant_writer);
190 } else { 185 } else {
191 response = dbus::ErrorResponse::FromMethodCall( 186 response = dbus::ErrorResponse::FromMethodCall(
192 method_call, kErrorInvalidArgs, 187 method_call, kErrorInvalidArgs,
193 "No such property: '" + property_name + "'."); 188 "No such property: '" + property_name + "'.");
194 } 189 }
195 190
196 response_sender.Run(std::move(response)); 191 response_sender.Run(std::move(response));
197 } 192 }
198 193
199 void BluetoothGattCharacteristicServiceProviderImpl::Set( 194 void BluetoothGattCharacteristicServiceProviderImpl::Set(
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 328
334 void BluetoothGattCharacteristicServiceProviderImpl::WriteProperties( 329 void BluetoothGattCharacteristicServiceProviderImpl::WriteProperties(
335 dbus::MessageWriter* writer, 330 dbus::MessageWriter* writer,
336 const std::vector<uint8_t>* value) { 331 const std::vector<uint8_t>* value) {
337 dbus::MessageWriter array_writer(NULL); 332 dbus::MessageWriter array_writer(NULL);
338 dbus::MessageWriter dict_entry_writer(NULL); 333 dbus::MessageWriter dict_entry_writer(NULL);
339 dbus::MessageWriter variant_writer(NULL); 334 dbus::MessageWriter variant_writer(NULL);
340 335
341 writer->OpenArray("{sv}", &array_writer); 336 writer->OpenArray("{sv}", &array_writer);
342 337
338 // UUID:
343 array_writer.OpenDictEntry(&dict_entry_writer); 339 array_writer.OpenDictEntry(&dict_entry_writer);
344 dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kUUIDProperty); 340 dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kUUIDProperty);
345 dict_entry_writer.AppendVariantOfString(uuid_); 341 dict_entry_writer.AppendVariantOfString(uuid_);
346 array_writer.CloseContainer(&dict_entry_writer); 342 array_writer.CloseContainer(&dict_entry_writer);
347 343
344 // Service:
348 array_writer.OpenDictEntry(&dict_entry_writer); 345 array_writer.OpenDictEntry(&dict_entry_writer);
349 dict_entry_writer.AppendString( 346 dict_entry_writer.AppendString(
350 bluetooth_gatt_characteristic::kServiceProperty); 347 bluetooth_gatt_characteristic::kServiceProperty);
351 dict_entry_writer.AppendVariantOfObjectPath(service_path_); 348 dict_entry_writer.AppendVariantOfObjectPath(service_path_);
352 array_writer.CloseContainer(&dict_entry_writer); 349 array_writer.CloseContainer(&dict_entry_writer);
353 350
354 if (value) { 351 if (value) {
352 // Value:
355 array_writer.OpenDictEntry(&dict_entry_writer); 353 array_writer.OpenDictEntry(&dict_entry_writer);
356 dict_entry_writer.AppendString( 354 dict_entry_writer.AppendString(
357 bluetooth_gatt_characteristic::kValueProperty); 355 bluetooth_gatt_characteristic::kValueProperty);
358 dict_entry_writer.OpenVariant("ay", &variant_writer); 356 dict_entry_writer.OpenVariant("ay", &variant_writer);
359 variant_writer.AppendArrayOfBytes(value->data(), value->size()); 357 variant_writer.AppendArrayOfBytes(value->data(), value->size());
360 dict_entry_writer.CloseContainer(&variant_writer); 358 dict_entry_writer.CloseContainer(&variant_writer);
361 array_writer.CloseContainer(&dict_entry_writer); 359 array_writer.CloseContainer(&dict_entry_writer);
362 } 360 }
363 361
364 // TODO(armansito): Process Flags & Permissions properties. 362 // Flags:
363 array_writer.OpenDictEntry(&dict_entry_writer);
364 dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kFlagsProperty);
365 dict_entry_writer.OpenVariant("as", &variant_writer);
366 variant_writer.AppendArrayOfStrings(flags_);
367 dict_entry_writer.CloseContainer(&variant_writer);
368 array_writer.CloseContainer(&dict_entry_writer);
365 369
366 writer->CloseContainer(&array_writer); 370 writer->CloseContainer(&array_writer);
367 }; 371 }
368 372
369 void BluetoothGattCharacteristicServiceProviderImpl::OnGet( 373 void BluetoothGattCharacteristicServiceProviderImpl::OnGet(
370 dbus::MethodCall* method_call, 374 dbus::MethodCall* method_call,
371 dbus::ExportedObject::ResponseSender response_sender, 375 dbus::ExportedObject::ResponseSender response_sender,
372 const std::vector<uint8_t>& value) { 376 const std::vector<uint8_t>& value) {
373 VLOG(2) << "Returning characteristic value obtained from delegate."; 377 VLOG(2) << "Returning characteristic value obtained from delegate.";
374 std::unique_ptr<dbus::Response> response = 378 std::unique_ptr<dbus::Response> response =
375 dbus::Response::FromMethodCall(method_call); 379 dbus::Response::FromMethodCall(method_call);
376 dbus::MessageWriter writer(response.get()); 380 dbus::MessageWriter writer(response.get());
377 dbus::MessageWriter variant_writer(NULL); 381 dbus::MessageWriter variant_writer(NULL);
(...skipping 21 matching lines...) Expand all
399 method_call, kErrorFailed, "Failed to get/set characteristic value."); 403 method_call, kErrorFailed, "Failed to get/set characteristic value.");
400 response_sender.Run(std::move(error_response)); 404 response_sender.Run(std::move(error_response));
401 } 405 }
402 406
403 const dbus::ObjectPath& 407 const dbus::ObjectPath&
404 BluetoothGattCharacteristicServiceProviderImpl::object_path() const { 408 BluetoothGattCharacteristicServiceProviderImpl::object_path() const {
405 return object_path_; 409 return object_path_;
406 } 410 }
407 411
408 } // namespace bluez 412 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698