blob: 5c158b546f9c1de35535ebe287def21f1276d85b [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
Chris Masone8fe2c7e2011-06-09 15:51:19 -070028// static
Paul Stewartced6a0b2011-11-08 15:32:04 -080029const char DBusAdaptor::kByteArraysSig[] = "aay";
30// static
mukesh agrawal2366eed2012-03-20 18:21:50 -070031const char DBusAdaptor::kPathsSig[] = "ao";
mukesh agrawal32399322011-09-01 10:53:43 -070032// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -070033const char DBusAdaptor::kStringmapSig[] = "a{ss}";
34// static
Chris Masone27c4aa52011-07-02 13:10:14 -070035const char DBusAdaptor::kStringmapsSig[] = "aa{ss}";
36// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -070037const char DBusAdaptor::kStringsSig[] = "as";
38
39DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path)
Chris Masoned0ceb8c2011-06-02 10:05:39 -070040 : DBus::ObjectAdaptor(*conn, object_path) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070041 SLOG(DBus, 2) << "DBusAdaptor: " << object_path;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070042}
43
44DBusAdaptor::~DBusAdaptor() {}
45
46// static
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080047bool DBusAdaptor::SetProperty(PropertyStore *store,
48 const string &name,
49 const ::DBus::Variant &value,
50 ::DBus::Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070051 Error e;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070052
53 if (DBusAdaptor::IsBool(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070054 store->SetBoolProperty(name, value.reader().get_bool(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070055 else if (DBusAdaptor::IsByte(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070056 store->SetUint8Property(name, value.reader().get_byte(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070057 else if (DBusAdaptor::IsInt16(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070058 store->SetInt16Property(name, value.reader().get_int16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070059 else if (DBusAdaptor::IsInt32(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070060 store->SetInt32Property(name, value.reader().get_int32(), &e);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070061 else if (DBusAdaptor::IsPath(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070062 store->SetStringProperty(name, value.reader().get_path(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070063 else if (DBusAdaptor::IsString(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070064 store->SetStringProperty(name, value.reader().get_string(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 else if (DBusAdaptor::IsStringmap(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070066 store->SetStringmapProperty(name,
67 value.operator map<string, string>(),
68 &e);
69 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 agrawalffa3d042011-10-06 15:26:10 -070071 e.Populate(Error::kInternalError);
72 } else if (DBusAdaptor::IsStrings(value.signature()))
73 store->SetStringsProperty(name, value.operator vector<string>(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070074 else if (DBusAdaptor::IsUint16(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070075 store->SetUint16Property(name, value.reader().get_uint16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070076 else if (DBusAdaptor::IsUint32(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070077 store->SetUint32Property(name, value.reader().get_uint32(), &e);
Paul Stewarte18c33b2012-07-10 20:48:44 -070078 else if (DBusAdaptor::IsUint64(value.signature()))
79 store->SetUint64Property(name, value.reader().get_uint64(), &e);
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050080 else if (DBusAdaptor::IsKeyValueStore(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070081 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050082 e.Populate(Error::kInternalError);
83 } else {
Chris Masone27c4aa52011-07-02 13:10:14 -070084 NOTREACHED() << " unknown type: " << value.signature();
mukesh agrawalffa3d042011-10-06 15:26:10 -070085 e.Populate(Error::kInternalError);
Chris Masone27c4aa52011-07-02 13:10:14 -070086 }
mukesh agrawalffa3d042011-10-06 15:26:10 -070087
88 if (error != NULL) {
89 e.ToDBusError(error);
90 }
91
92 return e.IsSuccess();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070093}
94
95// static
mukesh agrawalde29fa82011-09-16 16:16:36 -070096bool DBusAdaptor::GetProperties(const PropertyStore &store,
Chris Masonea8a2c252011-06-27 22:16:30 -070097 map<string, ::DBus::Variant> *out,
mukesh agrawal1830fa12011-09-26 14:31:40 -070098 ::DBus::Error */*error*/) {
Chris Masonea8a2c252011-06-27 22:16:30 -070099 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800100 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700101 for ( ; !it.AtEnd(); it.Advance()) {
102 SLOG(DBus, 5) << __func__ << " serializing bool " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200103 (*out)[it.Key()] = BoolToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700104 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700105 }
106 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800107 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700108 for ( ; !it.AtEnd(); it.Advance()) {
109 SLOG(DBus, 5) << __func__ << " serializing int16 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200110 (*out)[it.Key()] = Int16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700111 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700112 }
113 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800114 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700115 for ( ; !it.AtEnd(); it.Advance()) {
116 SLOG(DBus, 5) << __func__ << " serializing int32 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200117 (*out)[it.Key()] = Int32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700118 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700119 }
120 {
Darin Petkov63138a92012-02-06 14:09:15 +0100121 ReadablePropertyConstIterator<KeyValueStore> it =
122 store.GetKeyValueStorePropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700123 for ( ; !it.AtEnd(); it.Advance()) {
124 SLOG(DBus, 5) << __func__ << " serializing KeyValueStore " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200125 (*out)[it.Key()] = KeyValueStoreToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700126 }
Darin Petkov63138a92012-02-06 14:09:15 +0100127 }
128 {
mukesh agrawal2366eed2012-03-20 18:21:50 -0700129 ReadablePropertyConstIterator<RpcIdentifiers> it =
130 store.GetRpcIdentifiersPropertiesIter();
131 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200132 const Strings &rpc_identifiers_as_strings = it.value();
mukesh agrawal2366eed2012-03-20 18:21:50 -0700133 vector < ::DBus::Path> rpc_identifiers_as_paths;
134 for (Strings::const_iterator in = rpc_identifiers_as_strings.begin();
135 in != rpc_identifiers_as_strings.end();
136 ++in) {
137 rpc_identifiers_as_paths.push_back(*in);
138 }
mukesh agrawal465331c2012-05-30 11:26:11 -0700139 SLOG(DBus, 5) << __func__ << " serializing RpcIdentifiers " << it.Key();
mukesh agrawal2366eed2012-03-20 18:21:50 -0700140 (*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()) {
146 SLOG(DBus, 5) << __func__ << " serializing string " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200147 (*out)[it.Key()] = StringToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700148 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700149 }
150 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800151 ReadablePropertyConstIterator<Stringmap> it =
152 store.GetStringmapPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700153 for ( ; !it.AtEnd(); it.Advance()) {
154 SLOG(DBus, 5) << __func__ << " serializing Stringmap " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200155 (*out)[it.Key()]= StringmapToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700156 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700157 }
158 {
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400159 ReadablePropertyConstIterator<Stringmaps> it =
160 store.GetStringmapsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700161 for ( ; !it.AtEnd(); it.Advance()) {
162 SLOG(DBus, 5) << __func__ << " serializing Stringmaps " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200163 (*out)[it.Key()]= StringmapsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700164 }
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400165 }
166 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800167 ReadablePropertyConstIterator<Strings> it =
168 store.GetStringsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700169 for ( ; !it.AtEnd(); it.Advance()) {
170 SLOG(DBus, 5) << __func__ << " serializing Strings " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200171 (*out)[it.Key()] = StringsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700172 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700173 }
174 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800175 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700176 for ( ; !it.AtEnd(); it.Advance()) {
177 SLOG(DBus, 5) << __func__ << " serializing uint8 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200178 (*out)[it.Key()] = ByteToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700179 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700180 }
181 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800182 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700183 for ( ; !it.AtEnd(); it.Advance()) {
184 SLOG(DBus, 5) << __func__ << " serializing uint16 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200185 (*out)[it.Key()] = Uint16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700186 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700187 }
188 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800189 ReadablePropertyConstIterator<uint32> it = store.GetUint32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700190 for ( ; !it.AtEnd(); it.Advance()) {
191 SLOG(DBus, 5) << __func__ << " serializing uint32 " << it.Key();
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()) {
198 SLOG(DBus, 5) << __func__ << " serializing uint64 " << it.Key();
199 (*out)[it.Key()] = Uint64ToVariant(it.value());
200 }
201 }
202 {
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400203 ReadablePropertyConstIterator<RpcIdentifier> it =
204 store.GetRpcIdentifierPropertiesIter();
205 for ( ; !it.AtEnd(); it.Advance()) {
mukesh agrawal465331c2012-05-30 11:26:11 -0700206 SLOG(DBus, 5) << __func__ << " serializing RpcIdentifier " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200207 (*out)[it.Key()] = PathToVariant(it.value());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400208 }
209 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700210 return true;
211}
212
213// static
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800214bool DBusAdaptor::ClearProperty(PropertyStore *store,
215 const string &name,
216 ::DBus::Error *error) {
217 Error e;
218 store->ClearProperty(name, &e);
219
220 if (error != NULL) {
221 e.ToDBusError(error);
222 }
223
224 return e.IsSuccess();
225}
226
227// static
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700228void DBusAdaptor::ArgsToKeyValueStore(
229 const map<string, ::DBus::Variant> &args,
230 KeyValueStore *out,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800231 Error *error) { // TODO(quiche): Should be ::DBus::Error?
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700232 for (map<string, ::DBus::Variant>::const_iterator it = args.begin();
233 it != args.end();
234 ++it) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700235 string key = it->first;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700236 DBus::type<string> string_type;
237 DBus::type<bool> bool_type;
238
239 if (it->second.signature() == string_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700240 SLOG(DBus, 5) << "Got string property " << key;
241 out->SetString(key, it->second.reader().get_string());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700242 } else if (it->second.signature() == bool_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700243 SLOG(DBus, 5) << "Got bool property " << key;
244 out->SetBool(key, it->second.reader().get_bool());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700245 } else {
mukesh agrawal06175d72012-04-23 16:46:01 -0700246 Error::PopulateAndLog(error, Error::kInternalError,
247 "unsupported type for property " + key);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800248 return; // Skip remaining args after error.
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700249 }
250 }
251}
252
253// static
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700254::DBus::Variant DBusAdaptor::BoolToVariant(bool value) {
255 ::DBus::Variant v;
256 v.writer().append_bool(value);
257 return v;
258}
259
260// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800261::DBus::Variant DBusAdaptor::ByteArraysToVariant(const ByteArrays &value) {
262 ::DBus::MessageIter writer;
263 ::DBus::Variant v;
264
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800265
266 // We have to use a local because the operator<< needs a reference
267 // to work on (the lhs) but writer() returns by-value. C++ prohibits
268 // initializing non-const references from a temporary.
269 // So:
270 // v.writer() << value;
271 // would NOT automagically promote the returned value of v.writer() to
272 // a non-const reference (if you think about it, that's almost always not what
273 // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html).
274 //
275 // One could consider changing writer() to return a reference, but then it
276 // changes writer() semantics as it can not be a const reference. writer()
277 // currently doesn't modify the original object on which it's called.
Paul Stewartced6a0b2011-11-08 15:32:04 -0800278 writer = v.writer();
279 writer << value;
280 return v;
281}
282
283// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700284::DBus::Variant DBusAdaptor::ByteToVariant(uint8 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700285 ::DBus::Variant v;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700286 v.writer().append_byte(value);
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700287 return v;
288}
289
290// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700291::DBus::Variant DBusAdaptor::Int16ToVariant(int16 value) {
292 ::DBus::Variant v;
293 v.writer().append_int16(value);
294 return v;
295}
296
297// static
298::DBus::Variant DBusAdaptor::Int32ToVariant(int32 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700299 ::DBus::Variant v;
300 v.writer().append_int32(value);
301 return v;
302}
303
304// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700305::DBus::Variant DBusAdaptor::PathToVariant(const ::DBus::Path &value) {
306 ::DBus::Variant v;
307 v.writer().append_path(value.c_str());
308 return v;
309}
310
311// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700312::DBus::Variant DBusAdaptor::PathsToVariant(
mukesh agrawal32399322011-09-01 10:53:43 -0700313 const vector< ::DBus::Path> &value) {
314 ::DBus::MessageIter writer;
315 ::DBus::Variant v;
316
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800317 // See note above on why we need to use a local.
mukesh agrawal32399322011-09-01 10:53:43 -0700318 writer = v.writer();
319 writer << value;
320 return v;
321}
322
323// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700324::DBus::Variant DBusAdaptor::StringToVariant(const string &value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700325 ::DBus::Variant v;
326 v.writer().append_string(value.c_str());
327 return v;
328}
329
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700330// static
Chris Masone889666b2011-07-03 12:58:50 -0700331::DBus::Variant DBusAdaptor::StringmapToVariant(const Stringmap &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700332 ::DBus::Variant v;
333 ::DBus::MessageIter writer = v.writer();
334 writer << value;
335 return v;
336}
337
338// static
Chris Masone889666b2011-07-03 12:58:50 -0700339::DBus::Variant DBusAdaptor::StringmapsToVariant(const Stringmaps &value) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700340 ::DBus::Variant v;
341 ::DBus::MessageIter writer = v.writer();
342 writer << value;
343 return v;
344}
345
346// static
Chris Masone889666b2011-07-03 12:58:50 -0700347::DBus::Variant DBusAdaptor::StringsToVariant(const Strings &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700348 ::DBus::Variant v;
349 ::DBus::MessageIter writer = v.writer();
350 writer << value;
351 return v;
352}
353
354// static
Darin Petkov63138a92012-02-06 14:09:15 +0100355::DBus::Variant DBusAdaptor::KeyValueStoreToVariant(
356 const KeyValueStore &value) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200357 DBusPropertiesMap props;
358 DBusProperties::ConvertKeyValueStoreToMap(value, &props);
Chris Masone889666b2011-07-03 12:58:50 -0700359 ::DBus::Variant v;
360 ::DBus::MessageIter writer = v.writer();
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500361 writer << props;
Chris Masone889666b2011-07-03 12:58:50 -0700362 return v;
363}
364
365// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700366::DBus::Variant DBusAdaptor::Uint16ToVariant(uint16 value) {
367 ::DBus::Variant v;
368 v.writer().append_uint16(value);
369 return v;
370}
371
372// static
373::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) {
374 ::DBus::Variant v;
375 v.writer().append_uint32(value);
376 return v;
377}
378
379// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700380::DBus::Variant DBusAdaptor::Uint64ToVariant(uint64 value) {
381 ::DBus::Variant v;
382 v.writer().append_uint64(value);
383 return v;
384}
385
386// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700387bool DBusAdaptor::IsBool(::DBus::Signature signature) {
388 return signature == ::DBus::type<bool>::sig();
389}
390
391// static
392bool DBusAdaptor::IsByte(::DBus::Signature signature) {
393 return signature == ::DBus::type<uint8>::sig();
394}
395
396// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800397bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) {
398 return signature == DBusAdaptor::kByteArraysSig;
399}
400
401// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700402bool DBusAdaptor::IsInt16(::DBus::Signature signature) {
403 return signature == ::DBus::type<int16>::sig();
404}
405
406// static
407bool DBusAdaptor::IsInt32(::DBus::Signature signature) {
408 return signature == ::DBus::type<int32>::sig();
409}
410
411// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700412bool DBusAdaptor::IsPath(::DBus::Signature signature) {
413 return signature == ::DBus::type< ::DBus::Path >::sig();
414}
415
416// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700417bool DBusAdaptor::IsPaths(::DBus::Signature signature) {
418 return signature == DBusAdaptor::kPathsSig;
mukesh agrawal32399322011-09-01 10:53:43 -0700419}
420
421// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700422bool DBusAdaptor::IsString(::DBus::Signature signature) {
423 return signature == ::DBus::type<string>::sig();
424}
425
426// static
427bool DBusAdaptor::IsStringmap(::DBus::Signature signature) {
428 return signature == DBusAdaptor::kStringmapSig;
429}
430
431// static
Chris Masone27c4aa52011-07-02 13:10:14 -0700432bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) {
433 return signature == DBusAdaptor::kStringmapsSig;
434}
435
436// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700437bool DBusAdaptor::IsStrings(::DBus::Signature signature) {
438 return signature == DBusAdaptor::kStringsSig;
439}
440
441// static
442bool DBusAdaptor::IsUint16(::DBus::Signature signature) {
443 return signature == ::DBus::type<uint16>::sig();
444}
445
446// static
447bool DBusAdaptor::IsUint32(::DBus::Signature signature) {
448 return signature == ::DBus::type<uint32>::sig();
449}
450
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100451// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700452bool DBusAdaptor::IsUint64(::DBus::Signature signature) {
453 return signature == ::DBus::type<uint64>::sig();
454}
455
456// static
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500457bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) {
458 return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig();
459}
460
Eric Shienbrood9a245532012-03-07 14:20:39 -0500461void DBusAdaptor::DeferReply(const DBus::Tag *tag) {
462 return_later(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100463}
464
Eric Shienbrood9a245532012-03-07 14:20:39 -0500465void DBusAdaptor::ReplyNow(const DBus::Tag *tag) {
466 Continuation *cont = find_continuation(tag);
467 CHECK(cont);
468 return_now(cont);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100469}
470
Eric Shienbrood9a245532012-03-07 14:20:39 -0500471void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag,
472 const DBus::Error &error) {
473 Continuation *cont = find_continuation(tag);
474 CHECK(cont);
475 return_error(cont, error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100476}
477
Eric Shienbrood9a245532012-03-07 14:20:39 -0500478ResultCallback DBusAdaptor::GetMethodReplyCallback(
479 const DBus::Tag *tag) {
480 return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag));
481}
482
483void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag,
484 const Error &error,
485 DBus::Error *dberror) {
486 if (error.IsOngoing()) {
487 DeferReply(tag);
488 } else if (error.IsFailure()) {
489 error.ToDBusError(dberror);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100490 }
491}
492
Eric Shienbrood9a245532012-03-07 14:20:39 -0500493void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag,
494 const Error &error) {
495 if (error.IsFailure()) {
496 DBus::Error dberror;
497 error.ToDBusError(&dberror);
498 ReplyNowWithError(tag, dberror);
499 } else {
500 ReplyNow(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100501 }
502}
503
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700504} // namespace shill