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
3 // found in the LICENSE file.
4 
5 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_service_provider_impl.h"
6 
7 #include <cstddef>
8 
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "base/strings/string_util.h"
13 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
14 #include "device/bluetooth/dbus/bluetooth_gatt_attribute_helpers.h"
15 #include "device/bluetooth/dbus/bluetooth_gatt_characteristic_delegate_wrapper.h"
16 #include "third_party/cros_system_api/dbus/service_constants.h"
17 
18 namespace bluez {
19 
20 namespace {
21 
22 const char kErrorInvalidArgs[] = "org.freedesktop.DBus.Error.InvalidArgs";
23 const char kErrorPropertyReadOnly[] =
24     "org.freedesktop.DBus.Error.PropertyReadOnly";
25 const char kErrorFailed[] = "org.freedesktop.DBus.Error.Failed";
26 
27 }  // namespace
28 
29 BluetoothGattCharacteristicServiceProviderImpl::
BluetoothGattCharacteristicServiceProviderImpl(dbus::Bus * bus,const dbus::ObjectPath & object_path,std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate,const std::string & uuid,const std::vector<std::string> & flags,const dbus::ObjectPath & service_path)30     BluetoothGattCharacteristicServiceProviderImpl(
31         dbus::Bus* bus,
32         const dbus::ObjectPath& object_path,
33         std::unique_ptr<BluetoothGattAttributeValueDelegate> delegate,
34         const std::string& uuid,
35         const std::vector<std::string>& flags,
36         const dbus::ObjectPath& service_path)
37     : origin_thread_id_(base::PlatformThread::CurrentId()),
38       uuid_(uuid),
39       flags_(flags),
40       bus_(bus),
41       delegate_(std::move(delegate)),
42       object_path_(object_path),
43       service_path_(service_path) {
44   DVLOG(1) << "Created Bluetooth GATT characteristic: " << object_path.value()
45            << " UUID: " << uuid;
46   if (!bus_)
47     return;
48 
49   DCHECK(delegate_);
50   DCHECK(!uuid_.empty());
51   DCHECK(object_path_.IsValid());
52   DCHECK(service_path_.IsValid());
53   DCHECK(base::StartsWith(object_path_.value(), service_path_.value() + "/",
54                           base::CompareCase::SENSITIVE));
55 
56   exported_object_ = bus_->GetExportedObject(object_path_);
57 
58   // org.freedesktop.DBus.Properties interface:
59   exported_object_->ExportMethod(
60       dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGet,
61       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::Get,
62                  weak_ptr_factory_.GetWeakPtr()),
63       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
64                  weak_ptr_factory_.GetWeakPtr()));
65   exported_object_->ExportMethod(
66       dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesSet,
67       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::Set,
68                  weak_ptr_factory_.GetWeakPtr()),
69       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
70                  weak_ptr_factory_.GetWeakPtr()));
71   exported_object_->ExportMethod(
72       dbus::kDBusPropertiesInterface, dbus::kDBusPropertiesGetAll,
73       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::GetAll,
74                  weak_ptr_factory_.GetWeakPtr()),
75       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
76                  weak_ptr_factory_.GetWeakPtr()));
77 
78   // org.bluez.GattCharacteristic1 interface:
79   exported_object_->ExportMethod(
80       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
81       bluetooth_gatt_characteristic::kReadValue,
82       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::ReadValue,
83                  weak_ptr_factory_.GetWeakPtr()),
84       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
85                  weak_ptr_factory_.GetWeakPtr()));
86   exported_object_->ExportMethod(
87       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
88       bluetooth_gatt_characteristic::kWriteValue,
89       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::WriteValue,
90                  weak_ptr_factory_.GetWeakPtr()),
91       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
92                  weak_ptr_factory_.GetWeakPtr()));
93   exported_object_->ExportMethod(
94       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
95       bluetooth_gatt_characteristic::kPrepareWriteValue,
96       base::Bind(
97           &BluetoothGattCharacteristicServiceProviderImpl::PrepareWriteValue,
98           weak_ptr_factory_.GetWeakPtr()),
99       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
100                  weak_ptr_factory_.GetWeakPtr()));
101   exported_object_->ExportMethod(
102       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
103       bluetooth_gatt_characteristic::kStartNotify,
104       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::StartNotify,
105                  weak_ptr_factory_.GetWeakPtr()),
106       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
107                  weak_ptr_factory_.GetWeakPtr()));
108   exported_object_->ExportMethod(
109       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface,
110       bluetooth_gatt_characteristic::kStopNotify,
111       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::StopNotify,
112                  weak_ptr_factory_.GetWeakPtr()),
113       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnExported,
114                  weak_ptr_factory_.GetWeakPtr()));
115 }
116 
117 BluetoothGattCharacteristicServiceProviderImpl::
~BluetoothGattCharacteristicServiceProviderImpl()118     ~BluetoothGattCharacteristicServiceProviderImpl() {
119   DVLOG(1) << "Cleaning up Bluetooth GATT characteristic: "
120            << object_path_.value();
121   if (bus_)
122     bus_->UnregisterExportedObject(object_path_);
123 }
124 
SendValueChanged(const std::vector<uint8_t> & value)125 void BluetoothGattCharacteristicServiceProviderImpl::SendValueChanged(
126     const std::vector<uint8_t>& value) {
127   // Running a test, don't actually try to write to use DBus.
128   if (!bus_)
129     return;
130 
131   DVLOG(2) << "Emitting a PropertiesChanged signal for characteristic value.";
132   dbus::Signal signal(dbus::kDBusPropertiesInterface,
133                       dbus::kDBusPropertiesChangedSignal);
134   dbus::MessageWriter writer(&signal);
135   dbus::MessageWriter array_writer(nullptr);
136   dbus::MessageWriter dict_entry_writer(nullptr);
137   dbus::MessageWriter variant_writer(nullptr);
138 
139   // interface_name
140   writer.AppendString(
141       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface);
142 
143   // changed_properties
144   writer.OpenArray("{sv}", &array_writer);
145   array_writer.OpenDictEntry(&dict_entry_writer);
146   dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kValueProperty);
147   dict_entry_writer.OpenVariant("ay", &variant_writer);
148   variant_writer.AppendArrayOfBytes(value.data(), value.size());
149   dict_entry_writer.CloseContainer(&variant_writer);
150   array_writer.CloseContainer(&dict_entry_writer);
151   writer.CloseContainer(&array_writer);
152 
153   // invalidated_properties.
154   writer.OpenArray("s", &array_writer);
155   writer.CloseContainer(&array_writer);
156 
157   exported_object_->SendSignal(&signal);
158 }
159 
160 // Returns true if the current thread is on the origin thread.
OnOriginThread()161 bool BluetoothGattCharacteristicServiceProviderImpl::OnOriginThread() {
162   return base::PlatformThread::CurrentId() == origin_thread_id_;
163 }
164 
Get(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)165 void BluetoothGattCharacteristicServiceProviderImpl::Get(
166     dbus::MethodCall* method_call,
167     dbus::ExportedObject::ResponseSender response_sender) {
168   DVLOG(2) << "BluetoothGattCharacteristicServiceProvider::Get: "
169            << object_path_.value();
170   DCHECK(OnOriginThread());
171 
172   dbus::MessageReader reader(method_call);
173 
174   std::string interface_name;
175   std::string property_name;
176   if (!reader.PopString(&interface_name) || !reader.PopString(&property_name) ||
177       reader.HasMoreData()) {
178     std::unique_ptr<dbus::ErrorResponse> error_response =
179         dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs,
180                                             "Expected 'ss'.");
181     std::move(response_sender).Run(std::move(error_response));
182     return;
183   }
184 
185   // Only the GATT characteristic interface is supported.
186   if (interface_name !=
187       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface) {
188     std::unique_ptr<dbus::ErrorResponse> error_response =
189         dbus::ErrorResponse::FromMethodCall(
190             method_call, kErrorInvalidArgs,
191             "No such interface: '" + interface_name + "'.");
192     std::move(response_sender).Run(std::move(error_response));
193     return;
194   }
195 
196   std::unique_ptr<dbus::Response> response =
197       dbus::Response::FromMethodCall(method_call);
198   dbus::MessageWriter writer(response.get());
199   dbus::MessageWriter variant_writer(nullptr);
200 
201   if (property_name == bluetooth_gatt_characteristic::kUUIDProperty) {
202     writer.OpenVariant("s", &variant_writer);
203     variant_writer.AppendString(uuid_);
204     writer.CloseContainer(&variant_writer);
205   } else if (property_name == bluetooth_gatt_characteristic::kServiceProperty) {
206     writer.OpenVariant("o", &variant_writer);
207     variant_writer.AppendObjectPath(service_path_);
208     writer.CloseContainer(&variant_writer);
209   } else if (property_name == bluetooth_gatt_characteristic::kFlagsProperty) {
210     writer.OpenVariant("as", &variant_writer);
211     variant_writer.AppendArrayOfStrings(flags_);
212     writer.CloseContainer(&variant_writer);
213   } else {
214     response = dbus::ErrorResponse::FromMethodCall(
215         method_call, kErrorInvalidArgs,
216         "No such property: '" + property_name + "'.");
217   }
218 
219   std::move(response_sender).Run(std::move(response));
220 }
221 
Set(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)222 void BluetoothGattCharacteristicServiceProviderImpl::Set(
223     dbus::MethodCall* method_call,
224     dbus::ExportedObject::ResponseSender response_sender) {
225   DVLOG(2) << "BluetoothGattCharacteristicServiceProviderImpl::Set: "
226            << object_path_.value();
227   DCHECK(OnOriginThread());
228   // All of the properties on this interface are read-only, so just return
229   // error.
230   std::unique_ptr<dbus::ErrorResponse> error_response =
231       dbus::ErrorResponse::FromMethodCall(method_call, kErrorPropertyReadOnly,
232                                           "All properties are read-only.");
233   std::move(response_sender).Run(std::move(error_response));
234 }
235 
GetAll(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)236 void BluetoothGattCharacteristicServiceProviderImpl::GetAll(
237     dbus::MethodCall* method_call,
238     dbus::ExportedObject::ResponseSender response_sender) {
239   DVLOG(2) << "BluetoothGattCharacteristicServiceProvider::GetAll: "
240            << object_path_.value();
241   DCHECK(OnOriginThread());
242 
243   dbus::MessageReader reader(method_call);
244 
245   std::string interface_name;
246   if (!reader.PopString(&interface_name) || reader.HasMoreData()) {
247     std::unique_ptr<dbus::ErrorResponse> error_response =
248         dbus::ErrorResponse::FromMethodCall(method_call, kErrorInvalidArgs,
249                                             "Expected 's'.");
250     std::move(response_sender).Run(std::move(error_response));
251     return;
252   }
253 
254   // Only the GATT characteristic interface is supported.
255   if (interface_name !=
256       bluetooth_gatt_characteristic::kBluetoothGattCharacteristicInterface) {
257     std::unique_ptr<dbus::ErrorResponse> error_response =
258         dbus::ErrorResponse::FromMethodCall(
259             method_call, kErrorInvalidArgs,
260             "No such interface: '" + interface_name + "'.");
261     std::move(response_sender).Run(std::move(error_response));
262     return;
263   }
264 
265   std::unique_ptr<dbus::Response> response =
266       dbus::Response::FromMethodCall(method_call);
267   dbus::MessageWriter writer(response.get());
268   WriteProperties(&writer);
269   std::move(response_sender).Run(std::move(response));
270 }
271 
ReadValue(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)272 void BluetoothGattCharacteristicServiceProviderImpl::ReadValue(
273     dbus::MethodCall* method_call,
274     dbus::ExportedObject::ResponseSender response_sender) {
275   DVLOG(3) << "BluetoothGattCharacteristicServiceProvider::ReadValue: "
276            << object_path_.value();
277   DCHECK(OnOriginThread());
278 
279   dbus::MessageReader reader(method_call);
280   std::map<std::string, dbus::MessageReader> options;
281   dbus::ObjectPath device_path;
282   ReadOptions(&reader, &options);
283   auto it = options.find(bluetooth_gatt_characteristic::kOptionDevice);
284   if (it != options.end())
285     it->second.PopObjectPath(&device_path);
286 
287   if (device_path.value().empty()) {
288     LOG(WARNING) << "ReadValue called with incorrect parameters: "
289                  << method_call->ToString();
290     // Continue on with an empty device path. This will return a null device to
291     // the delegate, which should know how to handle it.
292   }
293 
294   // GetValue() promises to only call either the success or error callback.
295   auto response_sender_adapted =
296       base::AdaptCallbackForRepeating(std::move(response_sender));
297 
298   DCHECK(delegate_);
299   delegate_->GetValue(
300       device_path,
301       base::BindOnce(
302           &BluetoothGattCharacteristicServiceProviderImpl::OnReadValue,
303           weak_ptr_factory_.GetWeakPtr(), method_call, response_sender_adapted),
304       base::BindOnce(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure,
305                      weak_ptr_factory_.GetWeakPtr(), method_call,
306                      response_sender_adapted));
307 }
308 
WriteValue(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)309 void BluetoothGattCharacteristicServiceProviderImpl::WriteValue(
310     dbus::MethodCall* method_call,
311     dbus::ExportedObject::ResponseSender response_sender) {
312   DVLOG(3) << "BluetoothGattCharacteristicServiceProvider::WriteValue: "
313            << object_path_.value();
314   DCHECK(OnOriginThread());
315 
316   dbus::MessageReader reader(method_call);
317   const uint8_t* bytes = nullptr;
318   size_t length = 0;
319 
320   std::vector<uint8_t> value;
321   if (!reader.PopArrayOfBytes(&bytes, &length)) {
322     LOG(WARNING) << "Error reading value parameter. WriteValue called with "
323                     "incorrect parameters: "
324                  << method_call->ToString();
325   }
326   if (bytes)
327     value.assign(bytes, bytes + length);
328 
329   std::map<std::string, dbus::MessageReader> options;
330   dbus::ObjectPath device_path;
331   ReadOptions(&reader, &options);
332   auto it = options.find(bluetooth_gatt_characteristic::kOptionDevice);
333   if (it != options.end())
334     it->second.PopObjectPath(&device_path);
335 
336   if (device_path.value().empty()) {
337     LOG(WARNING) << "WriteValue called with incorrect parameters: "
338                  << method_call->ToString();
339     // Continue on with an empty device path. This will return a null device to
340     // the delegate, which should know how to handle it.
341   }
342 
343   // SetValue() promises to only call either the success or error callback.
344   auto response_sender_adapted =
345       base::AdaptCallbackForRepeating(std::move(response_sender));
346 
347   DCHECK(delegate_);
348   delegate_->SetValue(
349       device_path, value,
350       base::BindOnce(
351           &BluetoothGattCharacteristicServiceProviderImpl::OnWriteValue,
352           weak_ptr_factory_.GetWeakPtr(), method_call, response_sender_adapted),
353       base::BindOnce(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure,
354                      weak_ptr_factory_.GetWeakPtr(), method_call,
355                      response_sender_adapted));
356 }
357 
PrepareWriteValue(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)358 void BluetoothGattCharacteristicServiceProviderImpl::PrepareWriteValue(
359     dbus::MethodCall* method_call,
360     dbus::ExportedObject::ResponseSender response_sender) {
361   DVLOG(3) << "BluetoothGattCharacteristicServiceProvider::PrepareWriteValue: "
362            << object_path_.value();
363   DCHECK(OnOriginThread());
364 
365   dbus::MessageReader reader(method_call);
366   const uint8_t* bytes = nullptr;
367   size_t length = 0;
368 
369   std::vector<uint8_t> value;
370   if (!reader.PopArrayOfBytes(&bytes, &length)) {
371     LOG(WARNING) << "Error reading value parameter. PrepareWriteValue called "
372                  << "with incorrect parameters: " << method_call->ToString();
373   }
374   if (bytes)
375     value.assign(bytes, bytes + length);
376 
377   std::map<std::string, dbus::MessageReader> options;
378   dbus::ObjectPath device_path;
379   uint16_t offset = 0;
380   bool has_subsequent_write = false;
381   ReadOptions(&reader, &options);
382   auto it = options.find(bluetooth_gatt_characteristic::kOptionDevice);
383   if (it != options.end())
384     it->second.PopObjectPath(&device_path);
385   it = options.find(bluetooth_gatt_characteristic::kOptionOffset);
386   if (it != options.end())
387     it->second.PopUint16(&offset);
388   it = options.find(bluetooth_gatt_characteristic::kOptionHasSubsequentWrite);
389   if (it != options.end())
390     it->second.PopBool(&has_subsequent_write);
391 
392   if (device_path.value().empty()) {
393     LOG(WARNING) << "PrepareWriteValue called with incorrect parameters: "
394                  << method_call->ToString();
395     // Continue on with an empty device path. This will return a null device to
396     // the delegate, which should know how to handle it.
397   }
398 
399   // PrepareSetValue() promises to only call either the success or error
400   // callback.
401   auto response_sender_adapted =
402       base::AdaptCallbackForRepeating(std::move(response_sender));
403 
404   DCHECK(delegate_);
405   delegate_->PrepareSetValue(
406       device_path, value, offset, has_subsequent_write,
407       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnWriteValue,
408                  weak_ptr_factory_.GetWeakPtr(), method_call,
409                  response_sender_adapted),
410       base::Bind(&BluetoothGattCharacteristicServiceProviderImpl::OnFailure,
411                  weak_ptr_factory_.GetWeakPtr(), method_call,
412                  response_sender_adapted));
413 }
414 
StartNotify(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)415 void BluetoothGattCharacteristicServiceProviderImpl::StartNotify(
416     dbus::MethodCall* method_call,
417     dbus::ExportedObject::ResponseSender response_sender) {
418   DVLOG(3) << "BluetoothGattCharacteristicServiceProvider::StartNotify: "
419            << object_path_.value();
420   DCHECK(OnOriginThread());
421 
422   dbus::MessageReader reader(method_call);
423   uint8_t cccd_value = 0;
424   if (!reader.PopByte(&cccd_value)) {
425     LOG(WARNING) << "Error reading cccd_value parameter. StartNotify called "
426                  << "with incorrect parameters: " << method_call->ToString();
427   }
428 
429   std::map<std::string, dbus::MessageReader> options;
430   dbus::ObjectPath device_path;
431   ReadOptions(&reader, &options);
432   auto it = options.find(bluetooth_gatt_characteristic::kOptionDevice);
433   if (it != options.end())
434     it->second.PopObjectPath(&device_path);
435 
436   DCHECK(delegate_);
437   delegate_->StartNotifications(
438       device_path,
439       cccd_value == static_cast<uint8_t>(device::BluetoothGattCharacteristic::
440                                              NotificationType::kIndication)
441           ? device::BluetoothGattCharacteristic::NotificationType::kIndication
442           : device::BluetoothGattCharacteristic::NotificationType::
443                 kNotification);
444 }
445 
StopNotify(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)446 void BluetoothGattCharacteristicServiceProviderImpl::StopNotify(
447     dbus::MethodCall* method_call,
448     dbus::ExportedObject::ResponseSender response_sender) {
449   DVLOG(3) << "BluetoothGattCharacteristicServiceProvider::StopNotify: "
450            << object_path_.value();
451   DCHECK(OnOriginThread());
452 
453   dbus::MessageReader reader(method_call);
454   std::map<std::string, dbus::MessageReader> options;
455   dbus::ObjectPath device_path;
456   ReadOptions(&reader, &options);
457   auto it = options.find(bluetooth_gatt_characteristic::kOptionDevice);
458   if (it != options.end())
459     it->second.PopObjectPath(&device_path);
460 
461   DCHECK(delegate_);
462   delegate_->StopNotifications(device_path);
463 }
464 
OnExported(const std::string & interface_name,const std::string & method_name,bool success)465 void BluetoothGattCharacteristicServiceProviderImpl::OnExported(
466     const std::string& interface_name,
467     const std::string& method_name,
468     bool success) {
469   DVLOG_IF(1, !success) << "Failed to export " << interface_name << "."
470                         << method_name;
471 }
472 
OnReadValue(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender,const std::vector<uint8_t> & value)473 void BluetoothGattCharacteristicServiceProviderImpl::OnReadValue(
474     dbus::MethodCall* method_call,
475     dbus::ExportedObject::ResponseSender response_sender,
476     const std::vector<uint8_t>& value) {
477   DVLOG(3) << "Characteristic value obtained from delegate. Responding to "
478               "ReadValue.";
479 
480   std::unique_ptr<dbus::Response> response =
481       dbus::Response::FromMethodCall(method_call);
482   dbus::MessageWriter writer(response.get());
483   writer.AppendArrayOfBytes(value.data(), value.size());
484   std::move(response_sender).Run(std::move(response));
485 }
486 
OnWriteValue(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)487 void BluetoothGattCharacteristicServiceProviderImpl::OnWriteValue(
488     dbus::MethodCall* method_call,
489     dbus::ExportedObject::ResponseSender response_sender) {
490   DVLOG(3) << "Responding to WriteValue.";
491 
492   std::unique_ptr<dbus::Response> response =
493       dbus::Response::FromMethodCall(method_call);
494   std::move(response_sender).Run(std::move(response));
495 }
496 
WriteProperties(dbus::MessageWriter * writer)497 void BluetoothGattCharacteristicServiceProviderImpl::WriteProperties(
498     dbus::MessageWriter* writer) {
499   dbus::MessageWriter array_writer(nullptr);
500   dbus::MessageWriter dict_entry_writer(nullptr);
501   dbus::MessageWriter variant_writer(nullptr);
502 
503   writer->OpenArray("{sv}", &array_writer);
504 
505   // UUID:
506   array_writer.OpenDictEntry(&dict_entry_writer);
507   dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kUUIDProperty);
508   dict_entry_writer.AppendVariantOfString(uuid_);
509   array_writer.CloseContainer(&dict_entry_writer);
510 
511   // Service:
512   array_writer.OpenDictEntry(&dict_entry_writer);
513   dict_entry_writer.AppendString(
514       bluetooth_gatt_characteristic::kServiceProperty);
515   dict_entry_writer.AppendVariantOfObjectPath(service_path_);
516   array_writer.CloseContainer(&dict_entry_writer);
517 
518   // Flags:
519   array_writer.OpenDictEntry(&dict_entry_writer);
520   dict_entry_writer.AppendString(bluetooth_gatt_characteristic::kFlagsProperty);
521   dict_entry_writer.OpenVariant("as", &variant_writer);
522   variant_writer.AppendArrayOfStrings(flags_);
523   dict_entry_writer.CloseContainer(&variant_writer);
524   array_writer.CloseContainer(&dict_entry_writer);
525 
526   writer->CloseContainer(&array_writer);
527 }
528 
OnFailure(dbus::MethodCall * method_call,dbus::ExportedObject::ResponseSender response_sender)529 void BluetoothGattCharacteristicServiceProviderImpl::OnFailure(
530     dbus::MethodCall* method_call,
531     dbus::ExportedObject::ResponseSender response_sender) {
532   DVLOG(2) << "Failed to get/set characteristic value. Report error.";
533   std::unique_ptr<dbus::ErrorResponse> error_response =
534       dbus::ErrorResponse::FromMethodCall(
535           method_call, kErrorFailed, "Failed to get/set characteristic value.");
536   std::move(response_sender).Run(std::move(error_response));
537 }
538 
539 const dbus::ObjectPath&
object_path() const540 BluetoothGattCharacteristicServiceProviderImpl::object_path() const {
541   return object_path_;
542 }
543 
544 }  // namespace bluez
545