blob: a296ded1230e8942ee63d347982a611cb5c044aa [file] [log] [blame]
Darin Petkov63138a92012-02-06 14:09:15 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoned0ceb8c2011-06-02 10:05:39 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Chris Masone8fe2c7e2011-06-09 15:51:19 -07005#include <map>
Chris Masoned0ceb8c2011-06-02 10:05:39 -07006#include <string>
Chris Masone8fe2c7e2011-06-09 15:51:19 -07007#include <vector>
Chris Masoned0ceb8c2011-06-02 10:05:39 -07008
Eric Shienbrood9a245532012-03-07 14:20:39 -05009#include <base/bind.h>
10#include <base/callback.h>
Chris Masoned0ceb8c2011-06-02 10:05:39 -070011#include <dbus-c++/dbus.h>
12
Chris Masone889666b2011-07-03 12:58:50 -070013#include "shill/accessor_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070014#include "shill/dbus_adaptor.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070015#include "shill/error.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070016#include "shill/key_value_store.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070017#include "shill/logging.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070018#include "shill/property_store.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070019
Eric Shienbrood9a245532012-03-07 14:20:39 -050020using base::Bind;
21using base::Owned;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070022using std::map;
23using std::string;
24using std::vector;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070025
26namespace shill {
27
mukesh agrawalcbfb34e2013-04-17 19:33:25 -070028// public static
29const char DBusAdaptor::kNullPath[] = "/";
30// private statics
Paul Stewartced6a0b2011-11-08 15:32:04 -080031const char DBusAdaptor::kByteArraysSig[] = "aay";
mukesh agrawal2366eed2012-03-20 18:21:50 -070032const char DBusAdaptor::kPathsSig[] = "ao";
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033const char DBusAdaptor::kStringmapSig[] = "a{ss}";
Chris Masone27c4aa52011-07-02 13:10:14 -070034const char DBusAdaptor::kStringmapsSig[] = "aa{ss}";
Chris Masone8fe2c7e2011-06-09 15:51:19 -070035const char DBusAdaptor::kStringsSig[] = "as";
mukesh agrawale7c7e652013-06-18 17:19:39 -070036const char DBusAdaptor::kUint16sSig[] = "aq";
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037
38DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path)
Chris Masoned0ceb8c2011-06-02 10:05:39 -070039 : DBus::ObjectAdaptor(*conn, object_path) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070040 SLOG(DBus, 2) << "DBusAdaptor: " << object_path;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070041}
42
43DBusAdaptor::~DBusAdaptor() {}
44
45// static
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080046bool DBusAdaptor::SetProperty(PropertyStore *store,
47 const string &name,
48 const ::DBus::Variant &value,
49 ::DBus::Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070050 Error e;
mukesh agrawalbebf1b82013-04-23 15:06:33 -070051 bool ret;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070052
53 if (DBusAdaptor::IsBool(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070054 ret = store->SetBoolProperty(name, value.reader().get_bool(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055 else if (DBusAdaptor::IsByte(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070056 ret = store->SetUint8Property(name, value.reader().get_byte(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070057 else if (DBusAdaptor::IsInt16(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070058 ret = store->SetInt16Property(name, value.reader().get_int16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070059 else if (DBusAdaptor::IsInt32(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070060 ret = store->SetInt32Property(name, value.reader().get_int32(), &e);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061 else if (DBusAdaptor::IsPath(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070062 ret = store->SetStringProperty(name, value.reader().get_path(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070063 else if (DBusAdaptor::IsString(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070064 ret = store->SetStringProperty(name, value.reader().get_string(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 else if (DBusAdaptor::IsStringmap(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070066 ret = store->SetStringmapProperty(name,
67 value.operator map<string, string>(),
68 &e);
mukesh agrawalffa3d042011-10-06 15:26:10 -070069 else if (DBusAdaptor::IsStringmaps(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070070 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070071 ret = false;
mukesh agrawalffa3d042011-10-06 15:26:10 -070072 e.Populate(Error::kInternalError);
73 } else if (DBusAdaptor::IsStrings(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070074 ret = store->SetStringsProperty(name, value.operator vector<string>(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070075 else if (DBusAdaptor::IsUint16(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070076 ret = store->SetUint16Property(name, value.reader().get_uint16(), &e);
mukesh agrawale7c7e652013-06-18 17:19:39 -070077 else if (DBusAdaptor::IsUint16s(value.signature()))
78 ret = store->SetUint16sProperty(name, value.operator vector<uint16>(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070079 else if (DBusAdaptor::IsUint32(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070080 ret = store->SetUint32Property(name, value.reader().get_uint32(), &e);
Paul Stewarte18c33b2012-07-10 20:48:44 -070081 else if (DBusAdaptor::IsUint64(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070082 ret = store->SetUint64Property(name, value.reader().get_uint64(), &e);
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050083 else if (DBusAdaptor::IsKeyValueStore(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070084 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070085 ret = false;
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050086 e.Populate(Error::kInternalError);
87 } else {
Chris Masone27c4aa52011-07-02 13:10:14 -070088 NOTREACHED() << " unknown type: " << value.signature();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070089 ret = false;
mukesh agrawalffa3d042011-10-06 15:26:10 -070090 e.Populate(Error::kInternalError);
Chris Masone27c4aa52011-07-02 13:10:14 -070091 }
mukesh agrawalffa3d042011-10-06 15:26:10 -070092
93 if (error != NULL) {
94 e.ToDBusError(error);
95 }
96
mukesh agrawalbebf1b82013-04-23 15:06:33 -070097 return ret;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070098}
99
100// static
mukesh agrawalde29fa82011-09-16 16:16:36 -0700101bool DBusAdaptor::GetProperties(const PropertyStore &store,
Chris Masonea8a2c252011-06-27 22:16:30 -0700102 map<string, ::DBus::Variant> *out,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700103 ::DBus::Error */*error*/) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700104 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800105 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700106 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200107 (*out)[it.Key()] = BoolToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700108 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700109 }
110 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800111 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700112 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200113 (*out)[it.Key()] = Int16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700114 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700115 }
116 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800117 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700118 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200119 (*out)[it.Key()] = Int32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700120 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700121 }
122 {
Darin Petkov63138a92012-02-06 14:09:15 +0100123 ReadablePropertyConstIterator<KeyValueStore> it =
124 store.GetKeyValueStorePropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700125 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200126 (*out)[it.Key()] = KeyValueStoreToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700127 }
Darin Petkov63138a92012-02-06 14:09:15 +0100128 }
129 {
mukesh agrawal2366eed2012-03-20 18:21:50 -0700130 ReadablePropertyConstIterator<RpcIdentifiers> it =
131 store.GetRpcIdentifiersPropertiesIter();
132 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200133 const Strings &rpc_identifiers_as_strings = it.value();
mukesh agrawal2366eed2012-03-20 18:21:50 -0700134 vector < ::DBus::Path> rpc_identifiers_as_paths;
135 for (Strings::const_iterator in = rpc_identifiers_as_strings.begin();
136 in != rpc_identifiers_as_strings.end();
137 ++in) {
138 rpc_identifiers_as_paths.push_back(*in);
139 }
140 (*out)[it.Key()] = PathsToVariant(rpc_identifiers_as_paths);
141 }
142 }
143 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800144 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700145 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200146 (*out)[it.Key()] = StringToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700147 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700148 }
149 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800150 ReadablePropertyConstIterator<Stringmap> it =
151 store.GetStringmapPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700152 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200153 (*out)[it.Key()]= StringmapToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700154 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700155 }
156 {
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400157 ReadablePropertyConstIterator<Stringmaps> it =
158 store.GetStringmapsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700159 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200160 (*out)[it.Key()]= StringmapsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700161 }
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400162 }
163 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800164 ReadablePropertyConstIterator<Strings> it =
165 store.GetStringsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700166 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200167 (*out)[it.Key()] = StringsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700168 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700169 }
170 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800171 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700172 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200173 (*out)[it.Key()] = ByteToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700174 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700175 }
176 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800177 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700178 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200179 (*out)[it.Key()] = Uint16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700180 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700181 }
182 {
mukesh agrawale7c7e652013-06-18 17:19:39 -0700183 ReadablePropertyConstIterator<Uint16s> it =
184 store.GetUint16sPropertiesIter();
185 for ( ; !it.AtEnd(); it.Advance()) {
186 (*out)[it.Key()] = Uint16sToVariant(it.value());
187 }
188 }
189 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800190 ReadablePropertyConstIterator<uint32> it = store.GetUint32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700191 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200192 (*out)[it.Key()] = Uint32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700193 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700194 }
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400195 {
Paul Stewarte18c33b2012-07-10 20:48:44 -0700196 ReadablePropertyConstIterator<uint64> it = store.GetUint64PropertiesIter();
197 for ( ; !it.AtEnd(); it.Advance()) {
Paul Stewarte18c33b2012-07-10 20:48:44 -0700198 (*out)[it.Key()] = Uint64ToVariant(it.value());
199 }
200 }
201 {
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400202 ReadablePropertyConstIterator<RpcIdentifier> it =
203 store.GetRpcIdentifierPropertiesIter();
204 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200205 (*out)[it.Key()] = PathToVariant(it.value());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400206 }
207 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700208 return true;
209}
210
211// static
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800212bool DBusAdaptor::ClearProperty(PropertyStore *store,
213 const string &name,
214 ::DBus::Error *error) {
215 Error e;
216 store->ClearProperty(name, &e);
217
218 if (error != NULL) {
219 e.ToDBusError(error);
220 }
221
222 return e.IsSuccess();
223}
224
225// static
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700226void DBusAdaptor::ArgsToKeyValueStore(
227 const map<string, ::DBus::Variant> &args,
228 KeyValueStore *out,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800229 Error *error) { // TODO(quiche): Should be ::DBus::Error?
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700230 for (map<string, ::DBus::Variant>::const_iterator it = args.begin();
231 it != args.end();
232 ++it) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700233 string key = it->first;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700234 DBus::type<string> string_type;
235 DBus::type<bool> bool_type;
236
237 if (it->second.signature() == string_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700238 SLOG(DBus, 5) << "Got string property " << key;
239 out->SetString(key, it->second.reader().get_string());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700240 } else if (it->second.signature() == bool_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700241 SLOG(DBus, 5) << "Got bool property " << key;
242 out->SetBool(key, it->second.reader().get_bool());
Paul Stewart99dc9f32013-06-27 07:39:25 -0700243 } else if (DBusAdaptor::IsStrings(it->second.signature())) {
244 SLOG(DBus, 5) << "Got strings property " << key;
245 out->SetStrings(key, it->second.operator vector<string>());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700246 } else {
mukesh agrawal06175d72012-04-23 16:46:01 -0700247 Error::PopulateAndLog(error, Error::kInternalError,
248 "unsupported type for property " + key);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800249 return; // Skip remaining args after error.
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700250 }
251 }
252}
253
254// static
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700255::DBus::Variant DBusAdaptor::BoolToVariant(bool value) {
256 ::DBus::Variant v;
257 v.writer().append_bool(value);
258 return v;
259}
260
261// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800262::DBus::Variant DBusAdaptor::ByteArraysToVariant(const ByteArrays &value) {
263 ::DBus::MessageIter writer;
264 ::DBus::Variant v;
265
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800266
267 // We have to use a local because the operator<< needs a reference
268 // to work on (the lhs) but writer() returns by-value. C++ prohibits
269 // initializing non-const references from a temporary.
270 // So:
271 // v.writer() << value;
272 // would NOT automagically promote the returned value of v.writer() to
273 // a non-const reference (if you think about it, that's almost always not what
274 // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html).
275 //
276 // One could consider changing writer() to return a reference, but then it
277 // changes writer() semantics as it can not be a const reference. writer()
278 // currently doesn't modify the original object on which it's called.
Paul Stewartced6a0b2011-11-08 15:32:04 -0800279 writer = v.writer();
280 writer << value;
281 return v;
282}
283
284// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700285::DBus::Variant DBusAdaptor::ByteToVariant(uint8 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700286 ::DBus::Variant v;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700287 v.writer().append_byte(value);
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700288 return v;
289}
290
291// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700292::DBus::Variant DBusAdaptor::Int16ToVariant(int16 value) {
293 ::DBus::Variant v;
294 v.writer().append_int16(value);
295 return v;
296}
297
298// static
299::DBus::Variant DBusAdaptor::Int32ToVariant(int32 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700300 ::DBus::Variant v;
301 v.writer().append_int32(value);
302 return v;
303}
304
305// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700306::DBus::Variant DBusAdaptor::PathToVariant(const ::DBus::Path &value) {
307 ::DBus::Variant v;
308 v.writer().append_path(value.c_str());
309 return v;
310}
311
312// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700313::DBus::Variant DBusAdaptor::PathsToVariant(
mukesh agrawal32399322011-09-01 10:53:43 -0700314 const vector< ::DBus::Path> &value) {
315 ::DBus::MessageIter writer;
316 ::DBus::Variant v;
317
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800318 // See note above on why we need to use a local.
mukesh agrawal32399322011-09-01 10:53:43 -0700319 writer = v.writer();
320 writer << value;
321 return v;
322}
323
324// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700325::DBus::Variant DBusAdaptor::StringToVariant(const string &value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700326 ::DBus::Variant v;
327 v.writer().append_string(value.c_str());
328 return v;
329}
330
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700331// static
Chris Masone889666b2011-07-03 12:58:50 -0700332::DBus::Variant DBusAdaptor::StringmapToVariant(const Stringmap &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700333 ::DBus::Variant v;
334 ::DBus::MessageIter writer = v.writer();
335 writer << value;
336 return v;
337}
338
339// static
Chris Masone889666b2011-07-03 12:58:50 -0700340::DBus::Variant DBusAdaptor::StringmapsToVariant(const Stringmaps &value) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700341 ::DBus::Variant v;
342 ::DBus::MessageIter writer = v.writer();
343 writer << value;
344 return v;
345}
346
347// static
Chris Masone889666b2011-07-03 12:58:50 -0700348::DBus::Variant DBusAdaptor::StringsToVariant(const Strings &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700349 ::DBus::Variant v;
350 ::DBus::MessageIter writer = v.writer();
351 writer << value;
352 return v;
353}
354
355// static
Darin Petkov63138a92012-02-06 14:09:15 +0100356::DBus::Variant DBusAdaptor::KeyValueStoreToVariant(
357 const KeyValueStore &value) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200358 DBusPropertiesMap props;
359 DBusProperties::ConvertKeyValueStoreToMap(value, &props);
Chris Masone889666b2011-07-03 12:58:50 -0700360 ::DBus::Variant v;
361 ::DBus::MessageIter writer = v.writer();
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500362 writer << props;
Chris Masone889666b2011-07-03 12:58:50 -0700363 return v;
364}
365
366// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700367::DBus::Variant DBusAdaptor::Uint16ToVariant(uint16 value) {
368 ::DBus::Variant v;
369 v.writer().append_uint16(value);
370 return v;
371}
372
373// static
mukesh agrawale7c7e652013-06-18 17:19:39 -0700374::DBus::Variant DBusAdaptor::Uint16sToVariant(const Uint16s &value) {
375 ::DBus::Variant v;
376 ::DBus::MessageIter writer = v.writer();
377 writer << value;
378 return v;
379}
380
381// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700382::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) {
383 ::DBus::Variant v;
384 v.writer().append_uint32(value);
385 return v;
386}
387
388// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700389::DBus::Variant DBusAdaptor::Uint64ToVariant(uint64 value) {
390 ::DBus::Variant v;
391 v.writer().append_uint64(value);
392 return v;
393}
394
395// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700396bool DBusAdaptor::IsBool(::DBus::Signature signature) {
397 return signature == ::DBus::type<bool>::sig();
398}
399
400// static
401bool DBusAdaptor::IsByte(::DBus::Signature signature) {
402 return signature == ::DBus::type<uint8>::sig();
403}
404
405// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800406bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) {
407 return signature == DBusAdaptor::kByteArraysSig;
408}
409
410// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700411bool DBusAdaptor::IsInt16(::DBus::Signature signature) {
412 return signature == ::DBus::type<int16>::sig();
413}
414
415// static
416bool DBusAdaptor::IsInt32(::DBus::Signature signature) {
417 return signature == ::DBus::type<int32>::sig();
418}
419
420// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700421bool DBusAdaptor::IsPath(::DBus::Signature signature) {
422 return signature == ::DBus::type< ::DBus::Path >::sig();
423}
424
425// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700426bool DBusAdaptor::IsPaths(::DBus::Signature signature) {
427 return signature == DBusAdaptor::kPathsSig;
mukesh agrawal32399322011-09-01 10:53:43 -0700428}
429
430// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700431bool DBusAdaptor::IsString(::DBus::Signature signature) {
432 return signature == ::DBus::type<string>::sig();
433}
434
435// static
436bool DBusAdaptor::IsStringmap(::DBus::Signature signature) {
437 return signature == DBusAdaptor::kStringmapSig;
438}
439
440// static
Chris Masone27c4aa52011-07-02 13:10:14 -0700441bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) {
442 return signature == DBusAdaptor::kStringmapsSig;
443}
444
445// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700446bool DBusAdaptor::IsStrings(::DBus::Signature signature) {
447 return signature == DBusAdaptor::kStringsSig;
448}
449
450// static
451bool DBusAdaptor::IsUint16(::DBus::Signature signature) {
452 return signature == ::DBus::type<uint16>::sig();
453}
454
455// static
mukesh agrawale7c7e652013-06-18 17:19:39 -0700456bool DBusAdaptor::IsUint16s(::DBus::Signature signature) {
457 return signature == DBusAdaptor::kUint16sSig;
458}
459
460// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700461bool DBusAdaptor::IsUint32(::DBus::Signature signature) {
462 return signature == ::DBus::type<uint32>::sig();
463}
464
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100465// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700466bool DBusAdaptor::IsUint64(::DBus::Signature signature) {
467 return signature == ::DBus::type<uint64>::sig();
468}
469
470// static
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500471bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) {
472 return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig();
473}
474
Eric Shienbrood9a245532012-03-07 14:20:39 -0500475void DBusAdaptor::DeferReply(const DBus::Tag *tag) {
476 return_later(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100477}
478
Eric Shienbrood9a245532012-03-07 14:20:39 -0500479void DBusAdaptor::ReplyNow(const DBus::Tag *tag) {
480 Continuation *cont = find_continuation(tag);
481 CHECK(cont);
482 return_now(cont);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100483}
484
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800485template <typename T>
486void DBusAdaptor::TypedReplyNow(const DBus::Tag *tag, const T &value) {
487 Continuation *cont = find_continuation(tag);
488 CHECK(cont);
489 cont->writer() << value;
490 return_now(cont);
491}
492
Eric Shienbrood9a245532012-03-07 14:20:39 -0500493void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag,
494 const DBus::Error &error) {
495 Continuation *cont = find_continuation(tag);
496 CHECK(cont);
497 return_error(cont, error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100498}
499
Eric Shienbrood9a245532012-03-07 14:20:39 -0500500ResultCallback DBusAdaptor::GetMethodReplyCallback(
501 const DBus::Tag *tag) {
502 return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag));
503}
504
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800505ResultStringCallback DBusAdaptor::GetStringMethodReplyCallback(
506 const DBus::Tag *tag) {
507 return Bind(&DBusAdaptor::StringMethodReplyCallback, AsWeakPtr(), Owned(tag));
508}
509
510ResultBoolCallback DBusAdaptor::GetBoolMethodReplyCallback(
511 const DBus::Tag *tag) {
512 return Bind(&DBusAdaptor::BoolMethodReplyCallback, AsWeakPtr(), Owned(tag));
513}
514
515template<typename T>
516void DBusAdaptor::TypedMethodReplyCallback(const DBus::Tag *tag,
517 const Error &error,
518 const T &returned) {
519 if (error.IsFailure()) {
520 DBus::Error dberror;
521 error.ToDBusError(&dberror);
522 ReplyNowWithError(tag, dberror);
523 } else {
524 TypedReplyNow(tag, returned);
525 }
526}
527
Eric Shienbrood9a245532012-03-07 14:20:39 -0500528void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag,
529 const Error &error,
530 DBus::Error *dberror) {
531 if (error.IsOngoing()) {
532 DeferReply(tag);
533 } else if (error.IsFailure()) {
534 error.ToDBusError(dberror);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100535 }
536}
537
Eric Shienbrood9a245532012-03-07 14:20:39 -0500538void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag,
539 const Error &error) {
540 if (error.IsFailure()) {
541 DBus::Error dberror;
542 error.ToDBusError(&dberror);
543 ReplyNowWithError(tag, dberror);
544 } else {
545 ReplyNow(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100546 }
547}
548
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800549void DBusAdaptor::StringMethodReplyCallback(const DBus::Tag *tag,
550 const Error &error,
551 const string &returned) {
552 TypedMethodReplyCallback(tag, error, returned);
553}
554
555void DBusAdaptor::BoolMethodReplyCallback(const DBus::Tag *tag,
556 const Error &error,
557 bool returned) {
558 TypedMethodReplyCallback(tag, error, returned);
559}
560
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700561} // namespace shill