blob: ceff662da22e323334b9e3919ad5dfd0a95f32b4 [file] [log] [blame]
[email protected]cf910da22012-02-15 04:21:081// Copyright (c) 2012 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 "dbus/property.h"
6
avi22437c692015-12-22 18:12:457#include <stddef.h>
8
puthik0dd82b12016-10-06 05:03:039#include <memory>
10
[email protected]cf910da22012-02-15 04:21:0811#include "base/bind.h"
12#include "base/logging.h"
13
14#include "dbus/message.h"
15#include "dbus/object_path.h"
16#include "dbus/object_proxy.h"
17
18namespace dbus {
19
20//
21// PropertyBase implementation.
22//
23
deratdce1abec2015-07-16 15:06:4424PropertyBase::PropertyBase() : property_set_(nullptr), is_valid_(false) {}
25
26PropertyBase::~PropertyBase() {}
27
[email protected]cf910da22012-02-15 04:21:0828void PropertyBase::Init(PropertySet* property_set, const std::string& name) {
29 DCHECK(!property_set_);
30 property_set_ = property_set;
jpawlowskied276542015-05-11 11:07:0431 is_valid_ = false;
[email protected]cf910da22012-02-15 04:21:0832 name_ = name;
33}
34
[email protected]cf910da22012-02-15 04:21:0835//
36// PropertySet implementation.
37//
38
[email protected]7375e8b22012-08-15 01:03:3039PropertySet::PropertySet(
40 ObjectProxy* object_proxy,
41 const std::string& interface,
42 const PropertyChangedCallback& property_changed_callback)
[email protected]cf910da22012-02-15 04:21:0843 : object_proxy_(object_proxy),
44 interface_(interface),
45 property_changed_callback_(property_changed_callback),
46 weak_ptr_factory_(this) {}
47
48PropertySet::~PropertySet() {
49}
50
51void PropertySet::RegisterProperty(const std::string& name,
52 PropertyBase* property) {
53 property->Init(this, name);
54 properties_map_[name] = property;
55}
56
57void PropertySet::ConnectSignals() {
58 DCHECK(object_proxy_);
59 object_proxy_->ConnectToSignal(
60 kPropertiesInterface,
61 kPropertiesChanged,
62 base::Bind(&PropertySet::ChangedReceived,
63 weak_ptr_factory_.GetWeakPtr()),
64 base::Bind(&PropertySet::ChangedConnected,
65 weak_ptr_factory_.GetWeakPtr()));
66}
67
68
69void PropertySet::ChangedReceived(Signal* signal) {
[email protected]cf910da22012-02-15 04:21:0870 DCHECK(signal);
[email protected]cf910da22012-02-15 04:21:0871 MessageReader reader(signal);
72
73 std::string interface;
74 if (!reader.PopString(&interface)) {
75 LOG(WARNING) << "Property changed signal has wrong parameters: "
76 << "expected interface name: " << signal->ToString();
77 return;
78 }
79
80 if (interface != this->interface())
81 return;
82
83 if (!UpdatePropertiesFromReader(&reader)) {
84 LOG(WARNING) << "Property changed signal has wrong parameters: "
85 << "expected dictionary: " << signal->ToString();
86 }
87
jpawlowskied276542015-05-11 11:07:0488 if (!InvalidatePropertiesFromReader(&reader)) {
89 LOG(WARNING) << "Property changed signal has wrong parameters: "
90 << "expected array to invalidate: " << signal->ToString();
91 }
[email protected]cf910da22012-02-15 04:21:0892}
93
94void PropertySet::ChangedConnected(const std::string& interface_name,
95 const std::string& signal_name,
96 bool success) {
97 LOG_IF(WARNING, !success) << "Failed to connect to " << signal_name
98 << "signal.";
99}
100
101
[email protected]091e0b62012-06-28 18:43:30102void PropertySet::Get(PropertyBase* property, GetCallback callback) {
103 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
104 MessageWriter writer(&method_call);
105 writer.AppendString(interface());
106 writer.AppendString(property->name());
107
108 DCHECK(object_proxy_);
109 object_proxy_->CallMethod(&method_call,
110 ObjectProxy::TIMEOUT_USE_DEFAULT,
111 base::Bind(&PropertySet::OnGet,
112 GetWeakPtr(),
113 property,
114 callback));
115}
116
117void PropertySet::OnGet(PropertyBase* property, GetCallback callback,
118 Response* response) {
119 if (!response) {
120 LOG(WARNING) << property->name() << ": Get: failed.";
121 return;
122 }
123
124 MessageReader reader(response);
jpawlowskied276542015-05-11 11:07:04125 if (property->PopValueFromReader(&reader)) {
126 property->set_valid(true);
[email protected]091e0b62012-06-28 18:43:30127 NotifyPropertyChanged(property->name());
jpawlowskied276542015-05-11 11:07:04128 } else {
129 if (property->is_valid()) {
130 property->set_valid(false);
131 NotifyPropertyChanged(property->name());
132 }
133 }
[email protected]091e0b62012-06-28 18:43:30134
135 if (!callback.is_null())
136 callback.Run(response);
137}
138
nywanga5523d02015-09-28 22:46:36139bool PropertySet::GetAndBlock(PropertyBase* property) {
140 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
141 MessageWriter writer(&method_call);
142 writer.AppendString(interface());
143 writer.AppendString(property->name());
144
145 DCHECK(object_proxy_);
dcheng2a193282016-04-08 22:55:04146 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
147 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywanga5523d02015-09-28 22:46:36148
149 if (!response.get()) {
150 LOG(WARNING) << property->name() << ": GetAndBlock: failed.";
151 return false;
152 }
153
154 MessageReader reader(response.get());
155 if (property->PopValueFromReader(&reader)) {
156 property->set_valid(true);
157 NotifyPropertyChanged(property->name());
158 } else {
159 if (property->is_valid()) {
160 property->set_valid(false);
161 NotifyPropertyChanged(property->name());
162 }
163 }
164 return true;
165}
166
[email protected]cf910da22012-02-15 04:21:08167void PropertySet::GetAll() {
168 MethodCall method_call(kPropertiesInterface, kPropertiesGetAll);
169 MessageWriter writer(&method_call);
170 writer.AppendString(interface());
171
172 DCHECK(object_proxy_);
173 object_proxy_->CallMethod(&method_call,
174 ObjectProxy::TIMEOUT_USE_DEFAULT,
175 base::Bind(&PropertySet::OnGetAll,
176 weak_ptr_factory_.GetWeakPtr()));
177}
178
179void PropertySet::OnGetAll(Response* response) {
[email protected]cf910da22012-02-15 04:21:08180 if (!response) {
[email protected]e6898662014-07-17 05:03:53181 LOG(WARNING) << "GetAll request failed for: " << interface_;
[email protected]cf910da22012-02-15 04:21:08182 return;
183 }
184
185 MessageReader reader(response);
186 if (!UpdatePropertiesFromReader(&reader)) {
187 LOG(WARNING) << "GetAll response has wrong parameters: "
188 << "expected dictionary: " << response->ToString();
189 }
190}
191
[email protected]091e0b62012-06-28 18:43:30192void PropertySet::Set(PropertyBase* property, SetCallback callback) {
193 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
194 MessageWriter writer(&method_call);
195 writer.AppendString(interface());
196 writer.AppendString(property->name());
197 property->AppendSetValueToWriter(&writer);
198
199 DCHECK(object_proxy_);
200 object_proxy_->CallMethod(&method_call,
201 ObjectProxy::TIMEOUT_USE_DEFAULT,
202 base::Bind(&PropertySet::OnSet,
203 GetWeakPtr(),
204 property,
205 callback));
206}
207
nywang8dbb26e2015-09-23 00:06:40208bool PropertySet::SetAndBlock(PropertyBase* property) {
209 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
210 MessageWriter writer(&method_call);
211 writer.AppendString(interface());
212 writer.AppendString(property->name());
213 property->AppendSetValueToWriter(&writer);
214
215 DCHECK(object_proxy_);
dcheng2a193282016-04-08 22:55:04216 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
217 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywang8dbb26e2015-09-23 00:06:40218 if (response.get())
219 return true;
220 return false;
221}
222
[email protected]e6898662014-07-17 05:03:53223void PropertySet::OnSet(PropertyBase* property,
224 SetCallback callback,
[email protected]091e0b62012-06-28 18:43:30225 Response* response) {
226 LOG_IF(WARNING, !response) << property->name() << ": Set: failed.";
227 if (!callback.is_null())
228 callback.Run(response);
229}
[email protected]cf910da22012-02-15 04:21:08230
231bool PropertySet::UpdatePropertiesFromReader(MessageReader* reader) {
232 DCHECK(reader);
233 MessageReader array_reader(NULL);
234 if (!reader->PopArray(&array_reader))
235 return false;
236
237 while (array_reader.HasMoreData()) {
238 MessageReader dict_entry_reader(NULL);
[email protected]8a3eaffa2012-03-22 20:34:05239 if (array_reader.PopDictEntry(&dict_entry_reader))
240 UpdatePropertyFromReader(&dict_entry_reader);
[email protected]cf910da22012-02-15 04:21:08241 }
242
243 return true;
244}
245
246bool PropertySet::UpdatePropertyFromReader(MessageReader* reader) {
247 DCHECK(reader);
248
249 std::string name;
250 if (!reader->PopString(&name))
251 return false;
252
253 PropertiesMap::iterator it = properties_map_.find(name);
254 if (it == properties_map_.end())
255 return false;
256
257 PropertyBase* property = it->second;
258 if (property->PopValueFromReader(reader)) {
jpawlowskied276542015-05-11 11:07:04259 property->set_valid(true);
[email protected]cf910da22012-02-15 04:21:08260 NotifyPropertyChanged(name);
261 return true;
262 } else {
jpawlowskied276542015-05-11 11:07:04263 if (property->is_valid()) {
264 property->set_valid(false);
265 NotifyPropertyChanged(property->name());
266 }
[email protected]cf910da22012-02-15 04:21:08267 return false;
268 }
269}
270
jpawlowskied276542015-05-11 11:07:04271bool PropertySet::InvalidatePropertiesFromReader(MessageReader* reader) {
272 DCHECK(reader);
273 MessageReader array_reader(NULL);
274 if (!reader->PopArray(&array_reader))
275 return false;
276
277 while (array_reader.HasMoreData()) {
278 std::string name;
279 if (!array_reader.PopString(&name))
280 return false;
281
282 PropertiesMap::iterator it = properties_map_.find(name);
283 if (it == properties_map_.end())
284 continue;
285
286 PropertyBase* property = it->second;
287 if (property->is_valid()) {
288 property->set_valid(false);
289 NotifyPropertyChanged(property->name());
290 }
291 }
292
293 return true;
294}
[email protected]cf910da22012-02-15 04:21:08295
296void PropertySet::NotifyPropertyChanged(const std::string& name) {
297 if (!property_changed_callback_.is_null())
298 property_changed_callback_.Run(name);
299}
300
301//
302// Property<Byte> specialization.
303//
304
305template <>
avi22437c692015-12-22 18:12:45306Property<uint8_t>::Property()
307 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08308
309template <>
avi22437c692015-12-22 18:12:45310bool Property<uint8_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08311 return reader->PopVariantOfByte(&value_);
312}
313
314template <>
avi22437c692015-12-22 18:12:45315void Property<uint8_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30316 writer->AppendVariantOfByte(set_value_);
[email protected]cf910da22012-02-15 04:21:08317}
318
319//
320// Property<bool> specialization.
321//
322
323template <>
[email protected]091e0b62012-06-28 18:43:30324Property<bool>::Property() : value_(false) {
[email protected]cf910da22012-02-15 04:21:08325}
326
327template <>
328bool Property<bool>::PopValueFromReader(MessageReader* reader) {
329 return reader->PopVariantOfBool(&value_);
330}
331
332template <>
[email protected]091e0b62012-06-28 18:43:30333void Property<bool>::AppendSetValueToWriter(MessageWriter* writer) {
334 writer->AppendVariantOfBool(set_value_);
[email protected]cf910da22012-02-15 04:21:08335}
336
337//
avi22437c692015-12-22 18:12:45338// Property<int16_t> specialization.
[email protected]cf910da22012-02-15 04:21:08339//
340
341template <>
avi22437c692015-12-22 18:12:45342Property<int16_t>::Property()
343 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08344
345template <>
avi22437c692015-12-22 18:12:45346bool Property<int16_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08347 return reader->PopVariantOfInt16(&value_);
348}
349
350template <>
avi22437c692015-12-22 18:12:45351void Property<int16_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30352 writer->AppendVariantOfInt16(set_value_);
[email protected]cf910da22012-02-15 04:21:08353}
354
355//
avi22437c692015-12-22 18:12:45356// Property<uint16_t> specialization.
[email protected]cf910da22012-02-15 04:21:08357//
358
359template <>
avi22437c692015-12-22 18:12:45360Property<uint16_t>::Property()
361 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08362
363template <>
avi22437c692015-12-22 18:12:45364bool Property<uint16_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08365 return reader->PopVariantOfUint16(&value_);
366}
367
368template <>
avi22437c692015-12-22 18:12:45369void Property<uint16_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30370 writer->AppendVariantOfUint16(set_value_);
[email protected]cf910da22012-02-15 04:21:08371}
372
373//
avi22437c692015-12-22 18:12:45374// Property<int32_t> specialization.
[email protected]cf910da22012-02-15 04:21:08375//
376
377template <>
avi22437c692015-12-22 18:12:45378Property<int32_t>::Property()
379 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08380
381template <>
avi22437c692015-12-22 18:12:45382bool Property<int32_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08383 return reader->PopVariantOfInt32(&value_);
384}
385
386template <>
avi22437c692015-12-22 18:12:45387void Property<int32_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30388 writer->AppendVariantOfInt32(set_value_);
[email protected]cf910da22012-02-15 04:21:08389}
390
391//
avi22437c692015-12-22 18:12:45392// Property<uint32_t> specialization.
[email protected]cf910da22012-02-15 04:21:08393//
394
395template <>
avi22437c692015-12-22 18:12:45396Property<uint32_t>::Property()
397 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08398
399template <>
avi22437c692015-12-22 18:12:45400bool Property<uint32_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08401 return reader->PopVariantOfUint32(&value_);
402}
403
404template <>
avi22437c692015-12-22 18:12:45405void Property<uint32_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30406 writer->AppendVariantOfUint32(set_value_);
[email protected]cf910da22012-02-15 04:21:08407}
408
409//
avi22437c692015-12-22 18:12:45410// Property<int64_t> specialization.
[email protected]cf910da22012-02-15 04:21:08411//
412
413template <>
avi22437c692015-12-22 18:12:45414Property<int64_t>::Property()
415 : value_(0), set_value_(0) {}
[email protected]cf910da22012-02-15 04:21:08416
417template <>
avi22437c692015-12-22 18:12:45418bool Property<int64_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08419 return reader->PopVariantOfInt64(&value_);
420}
421
422template <>
avi22437c692015-12-22 18:12:45423void Property<int64_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30424 writer->AppendVariantOfInt64(set_value_);
[email protected]cf910da22012-02-15 04:21:08425}
426
427//
avi22437c692015-12-22 18:12:45428// Property<uint64_t> specialization.
[email protected]cf910da22012-02-15 04:21:08429//
430
431template <>
avi22437c692015-12-22 18:12:45432Property<uint64_t>::Property()
433 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08434
435template <>
avi22437c692015-12-22 18:12:45436bool Property<uint64_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08437 return reader->PopVariantOfUint64(&value_);
438}
439
440template <>
avi22437c692015-12-22 18:12:45441void Property<uint64_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30442 writer->AppendVariantOfUint64(set_value_);
[email protected]cf910da22012-02-15 04:21:08443}
444
445//
446// Property<double> specialization.
447//
448
449template <>
[email protected]091e0b62012-06-28 18:43:30450Property<double>::Property() : value_(0.0) {
[email protected]cf910da22012-02-15 04:21:08451}
452
453template <>
454bool Property<double>::PopValueFromReader(MessageReader* reader) {
455 return reader->PopVariantOfDouble(&value_);
456}
457
458template <>
[email protected]091e0b62012-06-28 18:43:30459void Property<double>::AppendSetValueToWriter(MessageWriter* writer) {
460 writer->AppendVariantOfDouble(set_value_);
[email protected]cf910da22012-02-15 04:21:08461}
462
463//
464// Property<std::string> specialization.
465//
466
467template <>
468bool Property<std::string>::PopValueFromReader(MessageReader* reader) {
469 return reader->PopVariantOfString(&value_);
470}
471
472template <>
[email protected]091e0b62012-06-28 18:43:30473void Property<std::string>::AppendSetValueToWriter(MessageWriter* writer) {
474 writer->AppendVariantOfString(set_value_);
[email protected]cf910da22012-02-15 04:21:08475}
476
477//
478// Property<ObjectPath> specialization.
479//
480
481template <>
482bool Property<ObjectPath>::PopValueFromReader(MessageReader* reader) {
483 return reader->PopVariantOfObjectPath(&value_);
484}
485
486template <>
[email protected]091e0b62012-06-28 18:43:30487void Property<ObjectPath>::AppendSetValueToWriter(MessageWriter* writer) {
488 writer->AppendVariantOfObjectPath(set_value_);
[email protected]cf910da22012-02-15 04:21:08489}
490
491//
Ben Chanc7c9c762017-11-08 01:50:21492// Property<std::vector<std::string>> specialization.
[email protected]cf910da22012-02-15 04:21:08493//
494
495template <>
Ben Chanc7c9c762017-11-08 01:50:21496bool Property<std::vector<std::string>>::PopValueFromReader(
[email protected]cf910da22012-02-15 04:21:08497 MessageReader* reader) {
498 MessageReader variant_reader(NULL);
499 if (!reader->PopVariant(&variant_reader))
500 return false;
501
[email protected]72bbacc2012-03-21 23:43:45502 value_.clear();
[email protected]cf910da22012-02-15 04:21:08503 return variant_reader.PopArrayOfStrings(&value_);
504}
505
506template <>
Ben Chanc7c9c762017-11-08 01:50:21507void Property<std::vector<std::string>>::AppendSetValueToWriter(
[email protected]091e0b62012-06-28 18:43:30508 MessageWriter* writer) {
[email protected]cf910da22012-02-15 04:21:08509 MessageWriter variant_writer(NULL);
510 writer->OpenVariant("as", &variant_writer);
[email protected]091e0b62012-06-28 18:43:30511 variant_writer.AppendArrayOfStrings(set_value_);
[email protected]cf910da22012-02-15 04:21:08512 writer->CloseContainer(&variant_writer);
513}
514
515//
Ben Chanc7c9c762017-11-08 01:50:21516// Property<std::vector<ObjectPath>> specialization.
[email protected]cf910da22012-02-15 04:21:08517//
518
519template <>
Ben Chanc7c9c762017-11-08 01:50:21520bool Property<std::vector<ObjectPath>>::PopValueFromReader(
[email protected]cf910da22012-02-15 04:21:08521 MessageReader* reader) {
522 MessageReader variant_reader(NULL);
523 if (!reader->PopVariant(&variant_reader))
524 return false;
525
[email protected]72bbacc2012-03-21 23:43:45526 value_.clear();
[email protected]cf910da22012-02-15 04:21:08527 return variant_reader.PopArrayOfObjectPaths(&value_);
528}
529
530template <>
Ben Chanc7c9c762017-11-08 01:50:21531void Property<std::vector<ObjectPath>>::AppendSetValueToWriter(
[email protected]091e0b62012-06-28 18:43:30532 MessageWriter* writer) {
[email protected]cf910da22012-02-15 04:21:08533 MessageWriter variant_writer(NULL);
534 writer->OpenVariant("ao", &variant_writer);
[email protected]091e0b62012-06-28 18:43:30535 variant_writer.AppendArrayOfObjectPaths(set_value_);
[email protected]cf910da22012-02-15 04:21:08536 writer->CloseContainer(&variant_writer);
537}
538
[email protected]ebbfffa22014-03-15 07:40:49539//
Ben Chanc7c9c762017-11-08 01:50:21540// Property<std::vector<uint8_t>> specialization.
[email protected]ebbfffa22014-03-15 07:40:49541//
542
543template <>
avi22437c692015-12-22 18:12:45544bool Property<std::vector<uint8_t>>::PopValueFromReader(MessageReader* reader) {
[email protected]ebbfffa22014-03-15 07:40:49545 MessageReader variant_reader(NULL);
546 if (!reader->PopVariant(&variant_reader))
547 return false;
548
549 value_.clear();
avi22437c692015-12-22 18:12:45550 const uint8_t* bytes = NULL;
[email protected]ebbfffa22014-03-15 07:40:49551 size_t length = 0;
552 if (!variant_reader.PopArrayOfBytes(&bytes, &length))
553 return false;
554 value_.assign(bytes, bytes + length);
555 return true;
556}
557
558template <>
avi22437c692015-12-22 18:12:45559void Property<std::vector<uint8_t>>::AppendSetValueToWriter(
[email protected]ebbfffa22014-03-15 07:40:49560 MessageWriter* writer) {
561 MessageWriter variant_writer(NULL);
562 writer->OpenVariant("ay", &variant_writer);
563 variant_writer.AppendArrayOfBytes(set_value_.data(), set_value_.size());
564 writer->CloseContainer(&variant_writer);
565}
566
dtapuska32d25452015-02-09 16:02:55567//
568// Property<std::map<std::string, std::string>> specialization.
569//
570
571template <>
572bool Property<std::map<std::string, std::string>>::PopValueFromReader(
573 MessageReader* reader) {
574 MessageReader variant_reader(NULL);
575 MessageReader array_reader(NULL);
576 if (!reader->PopVariant(&variant_reader) ||
577 !variant_reader.PopArray(&array_reader))
578 return false;
579 value_.clear();
580 while (array_reader.HasMoreData()) {
581 dbus::MessageReader dict_entry_reader(NULL);
582 if (!array_reader.PopDictEntry(&dict_entry_reader))
583 return false;
584 std::string key;
585 std::string value;
586 if (!dict_entry_reader.PopString(&key) ||
587 !dict_entry_reader.PopString(&value))
588 return false;
589 value_[key] = value;
590 }
591 return true;
592}
593
594template <>
595void Property<std::map<std::string, std::string>>::AppendSetValueToWriter(
596 MessageWriter* writer) {
597 MessageWriter variant_writer(NULL);
598 MessageWriter dict_writer(NULL);
599 writer->OpenVariant("a{ss}", &variant_writer);
600 variant_writer.OpenArray("{ss}", &dict_writer);
601 for (const auto& pair : set_value_) {
602 dbus::MessageWriter entry_writer(NULL);
603 dict_writer.OpenDictEntry(&entry_writer);
604 entry_writer.AppendString(pair.first);
605 entry_writer.AppendString(pair.second);
606 dict_writer.CloseContainer(&entry_writer);
607 }
608 variant_writer.CloseContainer(&dict_writer);
609 writer->CloseContainer(&variant_writer);
610}
611
612//
613// Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>
614// specialization.
615//
616
617template <>
618bool Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
619 PopValueFromReader(MessageReader* reader) {
620 MessageReader variant_reader(NULL);
621 MessageReader array_reader(NULL);
622 if (!reader->PopVariant(&variant_reader) ||
623 !variant_reader.PopArray(&array_reader))
624 return false;
625
626 value_.clear();
627 while (array_reader.HasMoreData()) {
628 dbus::MessageReader struct_reader(NULL);
629 if (!array_reader.PopStruct(&struct_reader))
630 return false;
631
632 std::pair<std::vector<uint8_t>, uint16_t> entry;
avi22437c692015-12-22 18:12:45633 const uint8_t* bytes = NULL;
dtapuska32d25452015-02-09 16:02:55634 size_t length = 0;
635 if (!struct_reader.PopArrayOfBytes(&bytes, &length))
636 return false;
637 entry.first.assign(bytes, bytes + length);
638 if (!struct_reader.PopUint16(&entry.second))
639 return false;
640 value_.push_back(entry);
641 }
642 return true;
643}
644
645template <>
646void Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
647 AppendSetValueToWriter(MessageWriter* writer) {
648 MessageWriter variant_writer(NULL);
649 MessageWriter array_writer(NULL);
650 writer->OpenVariant("a(ayq)", &variant_writer);
651 variant_writer.OpenArray("(ayq)", &array_writer);
652 for (const auto& pair : set_value_) {
653 dbus::MessageWriter struct_writer(nullptr);
654 array_writer.OpenStruct(&struct_writer);
655 struct_writer.AppendArrayOfBytes(std::get<0>(pair).data(),
656 std::get<0>(pair).size());
657 struct_writer.AppendUint16(std::get<1>(pair));
658 array_writer.CloseContainer(&struct_writer);
659 }
660 variant_writer.CloseContainer(&array_writer);
661 writer->CloseContainer(&variant_writer);
662}
663
puthik0dd82b12016-10-06 05:03:03664//
puthik23399e452016-11-04 19:38:17665// Property<std::unordered_map<std::string, std::vector<uint8_t>>>
puthik0dd82b12016-10-06 05:03:03666// specialization.
667//
668
669template <>
670bool Property<std::unordered_map<std::string, std::vector<uint8_t>>>::
671 PopValueFromReader(MessageReader* reader) {
672 MessageReader variant_reader(nullptr);
673 MessageReader dict_reader(nullptr);
674 if (!reader->PopVariant(&variant_reader) ||
675 !variant_reader.PopArray(&dict_reader))
676 return false;
677
678 value_.clear();
679 while (dict_reader.HasMoreData()) {
680 MessageReader entry_reader(nullptr);
681 if (!dict_reader.PopDictEntry(&entry_reader))
682 return false;
683
684 std::string key;
685 MessageReader value_varient_reader(nullptr);
686 if (!entry_reader.PopString(&key) ||
687 !entry_reader.PopVariant(&value_varient_reader))
688 return false;
689
690 const uint8_t* bytes = nullptr;
691 size_t length = 0;
692 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
693 return false;
694
695 value_[key].assign(bytes, bytes + length);
696 }
697 return true;
698}
699
700template <>
701void Property<std::unordered_map<std::string, std::vector<uint8_t>>>::
702 AppendSetValueToWriter(MessageWriter* writer) {
703 MessageWriter variant_writer(nullptr);
704 MessageWriter dict_writer(nullptr);
705
706 writer->OpenVariant("a{sv}", &variant_writer);
707 variant_writer.OpenArray("{sv}", &dict_writer);
708
709 for (const auto& pair : set_value_) {
710 MessageWriter entry_writer(nullptr);
711 dict_writer.OpenDictEntry(&entry_writer);
712
713 entry_writer.AppendString(pair.first);
714
715 MessageWriter value_varient_writer(nullptr);
716 entry_writer.OpenVariant("ay", &value_varient_writer);
717 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
718 pair.second.size());
719 entry_writer.CloseContainer(&value_varient_writer);
720
721 dict_writer.CloseContainer(&entry_writer);
722 }
723
724 variant_writer.CloseContainer(&dict_writer);
725 writer->CloseContainer(&variant_writer);
726}
727
puthik23399e452016-11-04 19:38:17728//
729// Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>
730// specialization.
731//
732
733template <>
734bool Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>::
735 PopValueFromReader(MessageReader* reader) {
736 MessageReader variant_reader(nullptr);
737 MessageReader dict_reader(nullptr);
738 if (!reader->PopVariant(&variant_reader) ||
739 !variant_reader.PopArray(&dict_reader))
740 return false;
741
742 value_.clear();
743 while (dict_reader.HasMoreData()) {
744 MessageReader entry_reader(nullptr);
745 if (!dict_reader.PopDictEntry(&entry_reader))
746 return false;
747
748 uint16_t key;
749 MessageReader value_varient_reader(nullptr);
750 if (!entry_reader.PopUint16(&key) ||
751 !entry_reader.PopVariant(&value_varient_reader))
752 return false;
753
754 const uint8_t* bytes = nullptr;
755 size_t length = 0;
756 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
757 return false;
758
759 value_[key].assign(bytes, bytes + length);
760 }
761 return true;
762}
763
764template <>
765void Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>::
766 AppendSetValueToWriter(MessageWriter* writer) {
767 MessageWriter variant_writer(nullptr);
768 MessageWriter dict_writer(nullptr);
769
770 writer->OpenVariant("a{qv}", &variant_writer);
771 variant_writer.OpenArray("{qv}", &dict_writer);
772
773 for (const auto& pair : set_value_) {
774 MessageWriter entry_writer(nullptr);
775 dict_writer.OpenDictEntry(&entry_writer);
776
777 entry_writer.AppendUint16(pair.first);
778
779 MessageWriter value_varient_writer(nullptr);
780 entry_writer.OpenVariant("ay", &value_varient_writer);
781 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
782 pair.second.size());
783 entry_writer.CloseContainer(&value_varient_writer);
784
785 dict_writer.CloseContainer(&entry_writer);
786 }
787
788 variant_writer.CloseContainer(&dict_writer);
789 writer->CloseContainer(&variant_writer);
790}
791
avi22437c692015-12-22 18:12:45792template class Property<uint8_t>;
[email protected]2934be42014-07-25 04:04:27793template class Property<bool>;
avi22437c692015-12-22 18:12:45794template class Property<int16_t>;
795template class Property<uint16_t>;
796template class Property<int32_t>;
797template class Property<uint32_t>;
798template class Property<int64_t>;
799template class Property<uint64_t>;
[email protected]2934be42014-07-25 04:04:27800template class Property<double>;
801template class Property<std::string>;
802template class Property<ObjectPath>;
Ben Chanc7c9c762017-11-08 01:50:21803template class Property<std::vector<std::string>>;
804template class Property<std::vector<ObjectPath>>;
avi22437c692015-12-22 18:12:45805template class Property<std::vector<uint8_t>>;
dtapuska32d25452015-02-09 16:02:55806template class Property<std::map<std::string, std::string>>;
807template class Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>;
puthik0dd82b12016-10-06 05:03:03808template class Property<std::unordered_map<std::string, std::vector<uint8_t>>>;
puthik23399e452016-11-04 19:38:17809template class Property<std::unordered_map<uint16_t, std::vector<uint8_t>>>;
[email protected]2934be42014-07-25 04:04:27810
[email protected]cf910da22012-02-15 04:21:08811} // namespace dbus