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

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

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

Powered by Google App Engine
This is Rietveld 408576698