blob: e1fb05ac7bdd61e1ec2640061eda34ffe756c494 [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
Chris Watkins3740aae2017-11-29 07:44:1126PropertyBase::~PropertyBase() = default;
deratdce1abec2015-07-16 15:06:4427
[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
Chris Watkins3740aae2017-11-29 07:44:1148PropertySet::~PropertySet() = default;
[email protected]cf910da22012-02-15 04:21:0849
50void PropertySet::RegisterProperty(const std::string& name,
51 PropertyBase* property) {
52 property->Init(this, name);
53 properties_map_[name] = property;
54}
55
56void PropertySet::ConnectSignals() {
57 DCHECK(object_proxy_);
58 object_proxy_->ConnectToSignal(
59 kPropertiesInterface,
60 kPropertiesChanged,
61 base::Bind(&PropertySet::ChangedReceived,
62 weak_ptr_factory_.GetWeakPtr()),
63 base::Bind(&PropertySet::ChangedConnected,
64 weak_ptr_factory_.GetWeakPtr()));
65}
66
67
68void PropertySet::ChangedReceived(Signal* signal) {
[email protected]cf910da22012-02-15 04:21:0869 DCHECK(signal);
[email protected]cf910da22012-02-15 04:21:0870 MessageReader reader(signal);
71
72 std::string interface;
73 if (!reader.PopString(&interface)) {
74 LOG(WARNING) << "Property changed signal has wrong parameters: "
75 << "expected interface name: " << signal->ToString();
76 return;
77 }
78
79 if (interface != this->interface())
80 return;
81
82 if (!UpdatePropertiesFromReader(&reader)) {
83 LOG(WARNING) << "Property changed signal has wrong parameters: "
84 << "expected dictionary: " << signal->ToString();
85 }
86
jpawlowskied276542015-05-11 11:07:0487 if (!InvalidatePropertiesFromReader(&reader)) {
88 LOG(WARNING) << "Property changed signal has wrong parameters: "
89 << "expected array to invalidate: " << signal->ToString();
90 }
[email protected]cf910da22012-02-15 04:21:0891}
92
93void PropertySet::ChangedConnected(const std::string& interface_name,
94 const std::string& signal_name,
95 bool success) {
96 LOG_IF(WARNING, !success) << "Failed to connect to " << signal_name
97 << "signal.";
98}
99
100
[email protected]091e0b62012-06-28 18:43:30101void PropertySet::Get(PropertyBase* property, GetCallback callback) {
102 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
103 MessageWriter writer(&method_call);
104 writer.AppendString(interface());
105 writer.AppendString(property->name());
106
107 DCHECK(object_proxy_);
108 object_proxy_->CallMethod(&method_call,
109 ObjectProxy::TIMEOUT_USE_DEFAULT,
110 base::Bind(&PropertySet::OnGet,
111 GetWeakPtr(),
112 property,
113 callback));
114}
115
116void PropertySet::OnGet(PropertyBase* property, GetCallback callback,
117 Response* response) {
118 if (!response) {
119 LOG(WARNING) << property->name() << ": Get: failed.";
120 return;
121 }
122
123 MessageReader reader(response);
jpawlowskied276542015-05-11 11:07:04124 if (property->PopValueFromReader(&reader)) {
125 property->set_valid(true);
[email protected]091e0b62012-06-28 18:43:30126 NotifyPropertyChanged(property->name());
jpawlowskied276542015-05-11 11:07:04127 } else {
128 if (property->is_valid()) {
129 property->set_valid(false);
130 NotifyPropertyChanged(property->name());
131 }
132 }
[email protected]091e0b62012-06-28 18:43:30133
134 if (!callback.is_null())
135 callback.Run(response);
136}
137
nywanga5523d02015-09-28 22:46:36138bool PropertySet::GetAndBlock(PropertyBase* property) {
139 MethodCall method_call(kPropertiesInterface, kPropertiesGet);
140 MessageWriter writer(&method_call);
141 writer.AppendString(interface());
142 writer.AppendString(property->name());
143
144 DCHECK(object_proxy_);
dcheng2a193282016-04-08 22:55:04145 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
146 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywanga5523d02015-09-28 22:46:36147
148 if (!response.get()) {
149 LOG(WARNING) << property->name() << ": GetAndBlock: failed.";
150 return false;
151 }
152
153 MessageReader reader(response.get());
154 if (property->PopValueFromReader(&reader)) {
155 property->set_valid(true);
156 NotifyPropertyChanged(property->name());
157 } else {
158 if (property->is_valid()) {
159 property->set_valid(false);
160 NotifyPropertyChanged(property->name());
161 }
162 }
163 return true;
164}
165
[email protected]cf910da22012-02-15 04:21:08166void PropertySet::GetAll() {
167 MethodCall method_call(kPropertiesInterface, kPropertiesGetAll);
168 MessageWriter writer(&method_call);
169 writer.AppendString(interface());
170
171 DCHECK(object_proxy_);
172 object_proxy_->CallMethod(&method_call,
173 ObjectProxy::TIMEOUT_USE_DEFAULT,
174 base::Bind(&PropertySet::OnGetAll,
175 weak_ptr_factory_.GetWeakPtr()));
176}
177
178void PropertySet::OnGetAll(Response* response) {
[email protected]cf910da22012-02-15 04:21:08179 if (!response) {
[email protected]e6898662014-07-17 05:03:53180 LOG(WARNING) << "GetAll request failed for: " << interface_;
[email protected]cf910da22012-02-15 04:21:08181 return;
182 }
183
184 MessageReader reader(response);
185 if (!UpdatePropertiesFromReader(&reader)) {
186 LOG(WARNING) << "GetAll response has wrong parameters: "
187 << "expected dictionary: " << response->ToString();
188 }
189}
190
[email protected]091e0b62012-06-28 18:43:30191void PropertySet::Set(PropertyBase* property, SetCallback callback) {
192 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
193 MessageWriter writer(&method_call);
194 writer.AppendString(interface());
195 writer.AppendString(property->name());
196 property->AppendSetValueToWriter(&writer);
197
198 DCHECK(object_proxy_);
199 object_proxy_->CallMethod(&method_call,
200 ObjectProxy::TIMEOUT_USE_DEFAULT,
201 base::Bind(&PropertySet::OnSet,
202 GetWeakPtr(),
203 property,
204 callback));
205}
206
nywang8dbb26e2015-09-23 00:06:40207bool PropertySet::SetAndBlock(PropertyBase* property) {
208 MethodCall method_call(kPropertiesInterface, kPropertiesSet);
209 MessageWriter writer(&method_call);
210 writer.AppendString(interface());
211 writer.AppendString(property->name());
212 property->AppendSetValueToWriter(&writer);
213
214 DCHECK(object_proxy_);
dcheng2a193282016-04-08 22:55:04215 std::unique_ptr<dbus::Response> response(object_proxy_->CallMethodAndBlock(
216 &method_call, ObjectProxy::TIMEOUT_USE_DEFAULT));
nywang8dbb26e2015-09-23 00:06:40217 if (response.get())
218 return true;
219 return false;
220}
221
[email protected]e6898662014-07-17 05:03:53222void PropertySet::OnSet(PropertyBase* property,
223 SetCallback callback,
[email protected]091e0b62012-06-28 18:43:30224 Response* response) {
225 LOG_IF(WARNING, !response) << property->name() << ": Set: failed.";
226 if (!callback.is_null())
227 callback.Run(response);
228}
[email protected]cf910da22012-02-15 04:21:08229
230bool PropertySet::UpdatePropertiesFromReader(MessageReader* reader) {
231 DCHECK(reader);
Ben Chan14d500372017-11-09 20:20:16232 MessageReader array_reader(nullptr);
[email protected]cf910da22012-02-15 04:21:08233 if (!reader->PopArray(&array_reader))
234 return false;
235
236 while (array_reader.HasMoreData()) {
Ben Chan14d500372017-11-09 20:20:16237 MessageReader dict_entry_reader(nullptr);
[email protected]8a3eaffa2012-03-22 20:34:05238 if (array_reader.PopDictEntry(&dict_entry_reader))
239 UpdatePropertyFromReader(&dict_entry_reader);
[email protected]cf910da22012-02-15 04:21:08240 }
241
242 return true;
243}
244
245bool PropertySet::UpdatePropertyFromReader(MessageReader* reader) {
246 DCHECK(reader);
247
248 std::string name;
249 if (!reader->PopString(&name))
250 return false;
251
252 PropertiesMap::iterator it = properties_map_.find(name);
253 if (it == properties_map_.end())
254 return false;
255
256 PropertyBase* property = it->second;
257 if (property->PopValueFromReader(reader)) {
jpawlowskied276542015-05-11 11:07:04258 property->set_valid(true);
[email protected]cf910da22012-02-15 04:21:08259 NotifyPropertyChanged(name);
260 return true;
261 } else {
jpawlowskied276542015-05-11 11:07:04262 if (property->is_valid()) {
263 property->set_valid(false);
264 NotifyPropertyChanged(property->name());
265 }
[email protected]cf910da22012-02-15 04:21:08266 return false;
267 }
268}
269
jpawlowskied276542015-05-11 11:07:04270bool PropertySet::InvalidatePropertiesFromReader(MessageReader* reader) {
271 DCHECK(reader);
Ben Chan14d500372017-11-09 20:20:16272 MessageReader array_reader(nullptr);
jpawlowskied276542015-05-11 11:07:04273 if (!reader->PopArray(&array_reader))
274 return false;
275
276 while (array_reader.HasMoreData()) {
277 std::string name;
278 if (!array_reader.PopString(&name))
279 return false;
280
281 PropertiesMap::iterator it = properties_map_.find(name);
282 if (it == properties_map_.end())
283 continue;
284
285 PropertyBase* property = it->second;
286 if (property->is_valid()) {
287 property->set_valid(false);
288 NotifyPropertyChanged(property->name());
289 }
290 }
291
292 return true;
293}
[email protected]cf910da22012-02-15 04:21:08294
295void PropertySet::NotifyPropertyChanged(const std::string& name) {
296 if (!property_changed_callback_.is_null())
297 property_changed_callback_.Run(name);
298}
299
300//
301// Property<Byte> specialization.
302//
303
304template <>
avi22437c692015-12-22 18:12:45305Property<uint8_t>::Property()
306 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08307
308template <>
avi22437c692015-12-22 18:12:45309bool Property<uint8_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08310 return reader->PopVariantOfByte(&value_);
311}
312
313template <>
avi22437c692015-12-22 18:12:45314void Property<uint8_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30315 writer->AppendVariantOfByte(set_value_);
[email protected]cf910da22012-02-15 04:21:08316}
317
318//
319// Property<bool> specialization.
320//
321
322template <>
[email protected]091e0b62012-06-28 18:43:30323Property<bool>::Property() : value_(false) {
[email protected]cf910da22012-02-15 04:21:08324}
325
326template <>
327bool Property<bool>::PopValueFromReader(MessageReader* reader) {
328 return reader->PopVariantOfBool(&value_);
329}
330
331template <>
[email protected]091e0b62012-06-28 18:43:30332void Property<bool>::AppendSetValueToWriter(MessageWriter* writer) {
333 writer->AppendVariantOfBool(set_value_);
[email protected]cf910da22012-02-15 04:21:08334}
335
336//
avi22437c692015-12-22 18:12:45337// Property<int16_t> specialization.
[email protected]cf910da22012-02-15 04:21:08338//
339
340template <>
avi22437c692015-12-22 18:12:45341Property<int16_t>::Property()
342 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08343
344template <>
avi22437c692015-12-22 18:12:45345bool Property<int16_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08346 return reader->PopVariantOfInt16(&value_);
347}
348
349template <>
avi22437c692015-12-22 18:12:45350void Property<int16_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30351 writer->AppendVariantOfInt16(set_value_);
[email protected]cf910da22012-02-15 04:21:08352}
353
354//
avi22437c692015-12-22 18:12:45355// Property<uint16_t> specialization.
[email protected]cf910da22012-02-15 04:21:08356//
357
358template <>
avi22437c692015-12-22 18:12:45359Property<uint16_t>::Property()
360 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08361
362template <>
avi22437c692015-12-22 18:12:45363bool Property<uint16_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08364 return reader->PopVariantOfUint16(&value_);
365}
366
367template <>
avi22437c692015-12-22 18:12:45368void Property<uint16_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30369 writer->AppendVariantOfUint16(set_value_);
[email protected]cf910da22012-02-15 04:21:08370}
371
372//
avi22437c692015-12-22 18:12:45373// Property<int32_t> specialization.
[email protected]cf910da22012-02-15 04:21:08374//
375
376template <>
avi22437c692015-12-22 18:12:45377Property<int32_t>::Property()
378 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08379
380template <>
avi22437c692015-12-22 18:12:45381bool Property<int32_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08382 return reader->PopVariantOfInt32(&value_);
383}
384
385template <>
avi22437c692015-12-22 18:12:45386void Property<int32_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30387 writer->AppendVariantOfInt32(set_value_);
[email protected]cf910da22012-02-15 04:21:08388}
389
390//
avi22437c692015-12-22 18:12:45391// Property<uint32_t> specialization.
[email protected]cf910da22012-02-15 04:21:08392//
393
394template <>
avi22437c692015-12-22 18:12:45395Property<uint32_t>::Property()
396 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08397
398template <>
avi22437c692015-12-22 18:12:45399bool Property<uint32_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08400 return reader->PopVariantOfUint32(&value_);
401}
402
403template <>
avi22437c692015-12-22 18:12:45404void Property<uint32_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30405 writer->AppendVariantOfUint32(set_value_);
[email protected]cf910da22012-02-15 04:21:08406}
407
408//
avi22437c692015-12-22 18:12:45409// Property<int64_t> specialization.
[email protected]cf910da22012-02-15 04:21:08410//
411
412template <>
avi22437c692015-12-22 18:12:45413Property<int64_t>::Property()
414 : value_(0), set_value_(0) {}
[email protected]cf910da22012-02-15 04:21:08415
416template <>
avi22437c692015-12-22 18:12:45417bool Property<int64_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08418 return reader->PopVariantOfInt64(&value_);
419}
420
421template <>
avi22437c692015-12-22 18:12:45422void Property<int64_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30423 writer->AppendVariantOfInt64(set_value_);
[email protected]cf910da22012-02-15 04:21:08424}
425
426//
avi22437c692015-12-22 18:12:45427// Property<uint64_t> specialization.
[email protected]cf910da22012-02-15 04:21:08428//
429
430template <>
avi22437c692015-12-22 18:12:45431Property<uint64_t>::Property()
432 : value_(0) {}
[email protected]cf910da22012-02-15 04:21:08433
434template <>
avi22437c692015-12-22 18:12:45435bool Property<uint64_t>::PopValueFromReader(MessageReader* reader) {
[email protected]cf910da22012-02-15 04:21:08436 return reader->PopVariantOfUint64(&value_);
437}
438
439template <>
avi22437c692015-12-22 18:12:45440void Property<uint64_t>::AppendSetValueToWriter(MessageWriter* writer) {
[email protected]091e0b62012-06-28 18:43:30441 writer->AppendVariantOfUint64(set_value_);
[email protected]cf910da22012-02-15 04:21:08442}
443
444//
445// Property<double> specialization.
446//
447
448template <>
[email protected]091e0b62012-06-28 18:43:30449Property<double>::Property() : value_(0.0) {
[email protected]cf910da22012-02-15 04:21:08450}
451
452template <>
453bool Property<double>::PopValueFromReader(MessageReader* reader) {
454 return reader->PopVariantOfDouble(&value_);
455}
456
457template <>
[email protected]091e0b62012-06-28 18:43:30458void Property<double>::AppendSetValueToWriter(MessageWriter* writer) {
459 writer->AppendVariantOfDouble(set_value_);
[email protected]cf910da22012-02-15 04:21:08460}
461
462//
463// Property<std::string> specialization.
464//
465
466template <>
467bool Property<std::string>::PopValueFromReader(MessageReader* reader) {
468 return reader->PopVariantOfString(&value_);
469}
470
471template <>
[email protected]091e0b62012-06-28 18:43:30472void Property<std::string>::AppendSetValueToWriter(MessageWriter* writer) {
473 writer->AppendVariantOfString(set_value_);
[email protected]cf910da22012-02-15 04:21:08474}
475
476//
477// Property<ObjectPath> specialization.
478//
479
480template <>
481bool Property<ObjectPath>::PopValueFromReader(MessageReader* reader) {
482 return reader->PopVariantOfObjectPath(&value_);
483}
484
485template <>
[email protected]091e0b62012-06-28 18:43:30486void Property<ObjectPath>::AppendSetValueToWriter(MessageWriter* writer) {
487 writer->AppendVariantOfObjectPath(set_value_);
[email protected]cf910da22012-02-15 04:21:08488}
489
490//
Ben Chanc7c9c762017-11-08 01:50:21491// Property<std::vector<std::string>> specialization.
[email protected]cf910da22012-02-15 04:21:08492//
493
494template <>
Ben Chanc7c9c762017-11-08 01:50:21495bool Property<std::vector<std::string>>::PopValueFromReader(
[email protected]cf910da22012-02-15 04:21:08496 MessageReader* reader) {
Ben Chan14d500372017-11-09 20:20:16497 MessageReader variant_reader(nullptr);
[email protected]cf910da22012-02-15 04:21:08498 if (!reader->PopVariant(&variant_reader))
499 return false;
500
[email protected]72bbacc2012-03-21 23:43:45501 value_.clear();
[email protected]cf910da22012-02-15 04:21:08502 return variant_reader.PopArrayOfStrings(&value_);
503}
504
505template <>
Ben Chanc7c9c762017-11-08 01:50:21506void Property<std::vector<std::string>>::AppendSetValueToWriter(
[email protected]091e0b62012-06-28 18:43:30507 MessageWriter* writer) {
Ben Chan14d500372017-11-09 20:20:16508 MessageWriter variant_writer(nullptr);
[email protected]cf910da22012-02-15 04:21:08509 writer->OpenVariant("as", &variant_writer);
[email protected]091e0b62012-06-28 18:43:30510 variant_writer.AppendArrayOfStrings(set_value_);
[email protected]cf910da22012-02-15 04:21:08511 writer->CloseContainer(&variant_writer);
512}
513
514//
Ben Chanc7c9c762017-11-08 01:50:21515// Property<std::vector<ObjectPath>> specialization.
[email protected]cf910da22012-02-15 04:21:08516//
517
518template <>
Ben Chanc7c9c762017-11-08 01:50:21519bool Property<std::vector<ObjectPath>>::PopValueFromReader(
[email protected]cf910da22012-02-15 04:21:08520 MessageReader* reader) {
Ben Chan14d500372017-11-09 20:20:16521 MessageReader variant_reader(nullptr);
[email protected]cf910da22012-02-15 04:21:08522 if (!reader->PopVariant(&variant_reader))
523 return false;
524
[email protected]72bbacc2012-03-21 23:43:45525 value_.clear();
[email protected]cf910da22012-02-15 04:21:08526 return variant_reader.PopArrayOfObjectPaths(&value_);
527}
528
529template <>
Ben Chanc7c9c762017-11-08 01:50:21530void Property<std::vector<ObjectPath>>::AppendSetValueToWriter(
[email protected]091e0b62012-06-28 18:43:30531 MessageWriter* writer) {
Ben Chan14d500372017-11-09 20:20:16532 MessageWriter variant_writer(nullptr);
[email protected]cf910da22012-02-15 04:21:08533 writer->OpenVariant("ao", &variant_writer);
[email protected]091e0b62012-06-28 18:43:30534 variant_writer.AppendArrayOfObjectPaths(set_value_);
[email protected]cf910da22012-02-15 04:21:08535 writer->CloseContainer(&variant_writer);
536}
537
[email protected]ebbfffa22014-03-15 07:40:49538//
Ben Chanc7c9c762017-11-08 01:50:21539// Property<std::vector<uint8_t>> specialization.
[email protected]ebbfffa22014-03-15 07:40:49540//
541
542template <>
avi22437c692015-12-22 18:12:45543bool Property<std::vector<uint8_t>>::PopValueFromReader(MessageReader* reader) {
Ben Chan14d500372017-11-09 20:20:16544 MessageReader variant_reader(nullptr);
[email protected]ebbfffa22014-03-15 07:40:49545 if (!reader->PopVariant(&variant_reader))
546 return false;
547
548 value_.clear();
Ben Chan14d500372017-11-09 20:20:16549 const uint8_t* bytes = nullptr;
[email protected]ebbfffa22014-03-15 07:40:49550 size_t length = 0;
551 if (!variant_reader.PopArrayOfBytes(&bytes, &length))
552 return false;
553 value_.assign(bytes, bytes + length);
554 return true;
555}
556
557template <>
avi22437c692015-12-22 18:12:45558void Property<std::vector<uint8_t>>::AppendSetValueToWriter(
[email protected]ebbfffa22014-03-15 07:40:49559 MessageWriter* writer) {
Ben Chan14d500372017-11-09 20:20:16560 MessageWriter variant_writer(nullptr);
[email protected]ebbfffa22014-03-15 07:40:49561 writer->OpenVariant("ay", &variant_writer);
562 variant_writer.AppendArrayOfBytes(set_value_.data(), set_value_.size());
563 writer->CloseContainer(&variant_writer);
564}
565
dtapuska32d25452015-02-09 16:02:55566//
567// Property<std::map<std::string, std::string>> specialization.
568//
569
570template <>
571bool Property<std::map<std::string, std::string>>::PopValueFromReader(
572 MessageReader* reader) {
Ben Chan14d500372017-11-09 20:20:16573 MessageReader variant_reader(nullptr);
574 MessageReader array_reader(nullptr);
dtapuska32d25452015-02-09 16:02:55575 if (!reader->PopVariant(&variant_reader) ||
576 !variant_reader.PopArray(&array_reader))
577 return false;
578 value_.clear();
579 while (array_reader.HasMoreData()) {
Ben Chan14d500372017-11-09 20:20:16580 dbus::MessageReader dict_entry_reader(nullptr);
dtapuska32d25452015-02-09 16:02:55581 if (!array_reader.PopDictEntry(&dict_entry_reader))
582 return false;
583 std::string key;
584 std::string value;
585 if (!dict_entry_reader.PopString(&key) ||
586 !dict_entry_reader.PopString(&value))
587 return false;
588 value_[key] = value;
589 }
590 return true;
591}
592
593template <>
594void Property<std::map<std::string, std::string>>::AppendSetValueToWriter(
595 MessageWriter* writer) {
Ben Chan14d500372017-11-09 20:20:16596 MessageWriter variant_writer(nullptr);
597 MessageWriter dict_writer(nullptr);
dtapuska32d25452015-02-09 16:02:55598 writer->OpenVariant("a{ss}", &variant_writer);
599 variant_writer.OpenArray("{ss}", &dict_writer);
600 for (const auto& pair : set_value_) {
Ben Chan14d500372017-11-09 20:20:16601 dbus::MessageWriter entry_writer(nullptr);
dtapuska32d25452015-02-09 16:02:55602 dict_writer.OpenDictEntry(&entry_writer);
603 entry_writer.AppendString(pair.first);
604 entry_writer.AppendString(pair.second);
605 dict_writer.CloseContainer(&entry_writer);
606 }
607 variant_writer.CloseContainer(&dict_writer);
608 writer->CloseContainer(&variant_writer);
609}
610
611//
612// Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>
613// specialization.
614//
615
616template <>
617bool Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
618 PopValueFromReader(MessageReader* reader) {
Ben Chan14d500372017-11-09 20:20:16619 MessageReader variant_reader(nullptr);
620 MessageReader array_reader(nullptr);
dtapuska32d25452015-02-09 16:02:55621 if (!reader->PopVariant(&variant_reader) ||
622 !variant_reader.PopArray(&array_reader))
623 return false;
624
625 value_.clear();
626 while (array_reader.HasMoreData()) {
Ben Chan14d500372017-11-09 20:20:16627 dbus::MessageReader struct_reader(nullptr);
dtapuska32d25452015-02-09 16:02:55628 if (!array_reader.PopStruct(&struct_reader))
629 return false;
630
631 std::pair<std::vector<uint8_t>, uint16_t> entry;
Ben Chan14d500372017-11-09 20:20:16632 const uint8_t* bytes = nullptr;
dtapuska32d25452015-02-09 16:02:55633 size_t length = 0;
634 if (!struct_reader.PopArrayOfBytes(&bytes, &length))
635 return false;
636 entry.first.assign(bytes, bytes + length);
637 if (!struct_reader.PopUint16(&entry.second))
638 return false;
639 value_.push_back(entry);
640 }
641 return true;
642}
643
644template <>
645void Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>::
646 AppendSetValueToWriter(MessageWriter* writer) {
Ben Chan14d500372017-11-09 20:20:16647 MessageWriter variant_writer(nullptr);
648 MessageWriter array_writer(nullptr);
dtapuska32d25452015-02-09 16:02:55649 writer->OpenVariant("a(ayq)", &variant_writer);
650 variant_writer.OpenArray("(ayq)", &array_writer);
651 for (const auto& pair : set_value_) {
652 dbus::MessageWriter struct_writer(nullptr);
653 array_writer.OpenStruct(&struct_writer);
654 struct_writer.AppendArrayOfBytes(std::get<0>(pair).data(),
655 std::get<0>(pair).size());
656 struct_writer.AppendUint16(std::get<1>(pair));
657 array_writer.CloseContainer(&struct_writer);
658 }
659 variant_writer.CloseContainer(&array_writer);
660 writer->CloseContainer(&variant_writer);
661}
662
puthik0dd82b12016-10-06 05:03:03663//
Sonny Sasakabd3098c62018-03-13 00:59:50664// Property<std::map<std::string, std::vector<uint8_t>>>
puthik0dd82b12016-10-06 05:03:03665// specialization.
666//
667
668template <>
Sonny Sasakabd3098c62018-03-13 00:59:50669bool Property<std::map<std::string, std::vector<uint8_t>>>::PopValueFromReader(
670 MessageReader* reader) {
puthik0dd82b12016-10-06 05:03:03671 MessageReader variant_reader(nullptr);
672 MessageReader dict_reader(nullptr);
673 if (!reader->PopVariant(&variant_reader) ||
674 !variant_reader.PopArray(&dict_reader))
675 return false;
676
677 value_.clear();
678 while (dict_reader.HasMoreData()) {
679 MessageReader entry_reader(nullptr);
680 if (!dict_reader.PopDictEntry(&entry_reader))
681 return false;
682
683 std::string key;
684 MessageReader value_varient_reader(nullptr);
685 if (!entry_reader.PopString(&key) ||
686 !entry_reader.PopVariant(&value_varient_reader))
687 return false;
688
689 const uint8_t* bytes = nullptr;
690 size_t length = 0;
691 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
692 return false;
693
694 value_[key].assign(bytes, bytes + length);
695 }
696 return true;
697}
698
699template <>
Sonny Sasakabd3098c62018-03-13 00:59:50700void Property<std::map<std::string, std::vector<uint8_t>>>::
puthik0dd82b12016-10-06 05:03:03701 AppendSetValueToWriter(MessageWriter* writer) {
702 MessageWriter variant_writer(nullptr);
703 MessageWriter dict_writer(nullptr);
704
705 writer->OpenVariant("a{sv}", &variant_writer);
706 variant_writer.OpenArray("{sv}", &dict_writer);
707
708 for (const auto& pair : set_value_) {
709 MessageWriter entry_writer(nullptr);
710 dict_writer.OpenDictEntry(&entry_writer);
711
712 entry_writer.AppendString(pair.first);
713
714 MessageWriter value_varient_writer(nullptr);
715 entry_writer.OpenVariant("ay", &value_varient_writer);
716 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
717 pair.second.size());
718 entry_writer.CloseContainer(&value_varient_writer);
719
720 dict_writer.CloseContainer(&entry_writer);
721 }
722
723 variant_writer.CloseContainer(&dict_writer);
724 writer->CloseContainer(&variant_writer);
725}
726
puthik23399e452016-11-04 19:38:17727//
Sonny Sasakabd3098c62018-03-13 00:59:50728// Property<std::map<uint16_t, std::vector<uint8_t>>>
puthik23399e452016-11-04 19:38:17729// specialization.
730//
731
732template <>
Sonny Sasakabd3098c62018-03-13 00:59:50733bool Property<std::map<uint16_t, std::vector<uint8_t>>>::PopValueFromReader(
734 MessageReader* reader) {
puthik23399e452016-11-04 19:38:17735 MessageReader variant_reader(nullptr);
736 MessageReader dict_reader(nullptr);
737 if (!reader->PopVariant(&variant_reader) ||
738 !variant_reader.PopArray(&dict_reader))
739 return false;
740
741 value_.clear();
742 while (dict_reader.HasMoreData()) {
743 MessageReader entry_reader(nullptr);
744 if (!dict_reader.PopDictEntry(&entry_reader))
745 return false;
746
747 uint16_t key;
748 MessageReader value_varient_reader(nullptr);
749 if (!entry_reader.PopUint16(&key) ||
750 !entry_reader.PopVariant(&value_varient_reader))
751 return false;
752
753 const uint8_t* bytes = nullptr;
754 size_t length = 0;
755 if (!value_varient_reader.PopArrayOfBytes(&bytes, &length))
756 return false;
757
758 value_[key].assign(bytes, bytes + length);
759 }
760 return true;
761}
762
763template <>
Sonny Sasakabd3098c62018-03-13 00:59:50764void Property<std::map<uint16_t, std::vector<uint8_t>>>::AppendSetValueToWriter(
765 MessageWriter* writer) {
puthik23399e452016-11-04 19:38:17766 MessageWriter variant_writer(nullptr);
767 MessageWriter dict_writer(nullptr);
768
769 writer->OpenVariant("a{qv}", &variant_writer);
770 variant_writer.OpenArray("{qv}", &dict_writer);
771
772 for (const auto& pair : set_value_) {
773 MessageWriter entry_writer(nullptr);
774 dict_writer.OpenDictEntry(&entry_writer);
775
776 entry_writer.AppendUint16(pair.first);
777
778 MessageWriter value_varient_writer(nullptr);
779 entry_writer.OpenVariant("ay", &value_varient_writer);
780 value_varient_writer.AppendArrayOfBytes(pair.second.data(),
781 pair.second.size());
782 entry_writer.CloseContainer(&value_varient_writer);
783
784 dict_writer.CloseContainer(&entry_writer);
785 }
786
787 variant_writer.CloseContainer(&dict_writer);
788 writer->CloseContainer(&variant_writer);
789}
790
avi22437c692015-12-22 18:12:45791template class Property<uint8_t>;
[email protected]2934be42014-07-25 04:04:27792template class Property<bool>;
avi22437c692015-12-22 18:12:45793template class Property<int16_t>;
794template class Property<uint16_t>;
795template class Property<int32_t>;
796template class Property<uint32_t>;
797template class Property<int64_t>;
798template class Property<uint64_t>;
[email protected]2934be42014-07-25 04:04:27799template class Property<double>;
800template class Property<std::string>;
801template class Property<ObjectPath>;
Ben Chanc7c9c762017-11-08 01:50:21802template class Property<std::vector<std::string>>;
803template class Property<std::vector<ObjectPath>>;
avi22437c692015-12-22 18:12:45804template class Property<std::vector<uint8_t>>;
dtapuska32d25452015-02-09 16:02:55805template class Property<std::map<std::string, std::string>>;
806template class Property<std::vector<std::pair<std::vector<uint8_t>, uint16_t>>>;
Sonny Sasakabd3098c62018-03-13 00:59:50807template class Property<std::map<std::string, std::vector<uint8_t>>>;
808template class Property<std::map<uint16_t, std::vector<uint8_t>>>;
[email protected]2934be42014-07-25 04:04:27809
[email protected]cf910da22012-02-15 04:21:08810} // namespace dbus