blob: 0cf97b89dbd5b60d4837106687497cf875aba4a8 [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 Masone8fe2c7e2011-06-09 15:51:19 -070011#include <base/logging.h>
Chris Masoned0ceb8c2011-06-02 10:05:39 -070012#include <dbus-c++/dbus.h>
13
Chris Masone889666b2011-07-03 12:58:50 -070014#include "shill/accessor_interface.h"
Chris Masoned0ceb8c2011-06-02 10:05:39 -070015#include "shill/dbus_adaptor.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070016#include "shill/error.h"
mukesh agrawal7a4e4002011-09-06 11:26:05 -070017#include "shill/key_value_store.h"
Chris Masoneb925cc82011-06-22 15:39:57 -070018#include "shill/property_store.h"
Ben Chanfad4a0b2012-04-18 15:49:59 -070019#include "shill/scope_logger.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070020
Eric Shienbrood9a245532012-03-07 14:20:39 -050021using base::Bind;
22using base::Owned;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070023using std::map;
24using std::string;
25using std::vector;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070026
27namespace shill {
28
Chris Masone8fe2c7e2011-06-09 15:51:19 -070029// static
Paul Stewartced6a0b2011-11-08 15:32:04 -080030const char DBusAdaptor::kByteArraysSig[] = "aay";
31// static
mukesh agrawal2366eed2012-03-20 18:21:50 -070032const char DBusAdaptor::kPathsSig[] = "ao";
mukesh agrawal32399322011-09-01 10:53:43 -070033// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -070034const char DBusAdaptor::kStringmapSig[] = "a{ss}";
35// static
Chris Masone27c4aa52011-07-02 13:10:14 -070036const char DBusAdaptor::kStringmapsSig[] = "aa{ss}";
37// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -070038const char DBusAdaptor::kStringsSig[] = "as";
39
40DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path)
Chris Masoned0ceb8c2011-06-02 10:05:39 -070041 : DBus::ObjectAdaptor(*conn, object_path) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070042 SLOG(DBus, 2) << "DBusAdaptor: " << object_path;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070043}
44
45DBusAdaptor::~DBusAdaptor() {}
46
47// static
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080048bool DBusAdaptor::SetProperty(PropertyStore *store,
49 const string &name,
50 const ::DBus::Variant &value,
51 ::DBus::Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070052 Error e;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070053
54 if (DBusAdaptor::IsBool(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070055 store->SetBoolProperty(name, value.reader().get_bool(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056 else if (DBusAdaptor::IsByte(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070057 store->SetUint8Property(name, value.reader().get_byte(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070058 else if (DBusAdaptor::IsInt16(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070059 store->SetInt16Property(name, value.reader().get_int16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070060 else if (DBusAdaptor::IsInt32(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070061 store->SetInt32Property(name, value.reader().get_int32(), &e);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070062 else if (DBusAdaptor::IsPath(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070063 store->SetStringProperty(name, value.reader().get_path(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070064 else if (DBusAdaptor::IsString(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070065 store->SetStringProperty(name, value.reader().get_string(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070066 else if (DBusAdaptor::IsStringmap(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070067 store->SetStringmapProperty(name,
68 value.operator map<string, string>(),
69 &e);
70 else if (DBusAdaptor::IsStringmaps(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070071 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
mukesh agrawalffa3d042011-10-06 15:26:10 -070072 e.Populate(Error::kInternalError);
73 } else if (DBusAdaptor::IsStrings(value.signature()))
74 store->SetStringsProperty(name, value.operator vector<string>(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070075 else if (DBusAdaptor::IsUint16(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070076 store->SetUint16Property(name, value.reader().get_uint16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070077 else if (DBusAdaptor::IsUint32(value.signature()))
mukesh agrawalffa3d042011-10-06 15:26:10 -070078 store->SetUint32Property(name, value.reader().get_uint32(), &e);
Paul Stewarte18c33b2012-07-10 20:48:44 -070079 else if (DBusAdaptor::IsUint64(value.signature()))
80 store->SetUint64Property(name, value.reader().get_uint64(), &e);
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050081 else if (DBusAdaptor::IsKeyValueStore(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070082 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
Eric Shienbroodb23d4b92012-02-16 12:32:42 -050083 e.Populate(Error::kInternalError);
84 } else {
Chris Masone27c4aa52011-07-02 13:10:14 -070085 NOTREACHED() << " unknown type: " << value.signature();
mukesh agrawalffa3d042011-10-06 15:26:10 -070086 e.Populate(Error::kInternalError);
Chris Masone27c4aa52011-07-02 13:10:14 -070087 }
mukesh agrawalffa3d042011-10-06 15:26:10 -070088
89 if (error != NULL) {
90 e.ToDBusError(error);
91 }
92
93 return e.IsSuccess();
Chris Masone8fe2c7e2011-06-09 15:51:19 -070094}
95
96// static
mukesh agrawalde29fa82011-09-16 16:16:36 -070097bool DBusAdaptor::GetProperties(const PropertyStore &store,
Chris Masonea8a2c252011-06-27 22:16:30 -070098 map<string, ::DBus::Variant> *out,
mukesh agrawal1830fa12011-09-26 14:31:40 -070099 ::DBus::Error */*error*/) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700100 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800101 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700102 for ( ; !it.AtEnd(); it.Advance()) {
103 SLOG(DBus, 5) << __func__ << " serializing bool " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200104 (*out)[it.Key()] = BoolToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700105 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700106 }
107 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800108 ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700109 for ( ; !it.AtEnd(); it.Advance()) {
110 SLOG(DBus, 5) << __func__ << " serializing int16 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200111 (*out)[it.Key()] = Int16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700112 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700113 }
114 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800115 ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700116 for ( ; !it.AtEnd(); it.Advance()) {
117 SLOG(DBus, 5) << __func__ << " serializing int32 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200118 (*out)[it.Key()] = Int32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700119 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700120 }
121 {
Darin Petkov63138a92012-02-06 14:09:15 +0100122 ReadablePropertyConstIterator<KeyValueStore> it =
123 store.GetKeyValueStorePropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700124 for ( ; !it.AtEnd(); it.Advance()) {
125 SLOG(DBus, 5) << __func__ << " serializing KeyValueStore " << it.Key();
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 }
mukesh agrawal465331c2012-05-30 11:26:11 -0700140 SLOG(DBus, 5) << __func__ << " serializing RpcIdentifiers " << it.Key();
mukesh agrawal2366eed2012-03-20 18:21:50 -0700141 (*out)[it.Key()] = PathsToVariant(rpc_identifiers_as_paths);
142 }
143 }
144 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800145 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700146 for ( ; !it.AtEnd(); it.Advance()) {
147 SLOG(DBus, 5) << __func__ << " serializing string " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200148 (*out)[it.Key()] = StringToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700149 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700150 }
151 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800152 ReadablePropertyConstIterator<Stringmap> it =
153 store.GetStringmapPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700154 for ( ; !it.AtEnd(); it.Advance()) {
155 SLOG(DBus, 5) << __func__ << " serializing Stringmap " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200156 (*out)[it.Key()]= StringmapToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700157 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700158 }
159 {
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400160 ReadablePropertyConstIterator<Stringmaps> it =
161 store.GetStringmapsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700162 for ( ; !it.AtEnd(); it.Advance()) {
163 SLOG(DBus, 5) << __func__ << " serializing Stringmaps " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200164 (*out)[it.Key()]= StringmapsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700165 }
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400166 }
167 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800168 ReadablePropertyConstIterator<Strings> it =
169 store.GetStringsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700170 for ( ; !it.AtEnd(); it.Advance()) {
171 SLOG(DBus, 5) << __func__ << " serializing Strings " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200172 (*out)[it.Key()] = StringsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700173 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700174 }
175 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800176 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700177 for ( ; !it.AtEnd(); it.Advance()) {
178 SLOG(DBus, 5) << __func__ << " serializing uint8 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200179 (*out)[it.Key()] = ByteToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700180 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700181 }
182 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800183 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700184 for ( ; !it.AtEnd(); it.Advance()) {
185 SLOG(DBus, 5) << __func__ << " serializing uint16 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200186 (*out)[it.Key()] = Uint16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700187 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700188 }
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()) {
192 SLOG(DBus, 5) << __func__ << " serializing uint32 " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200193 (*out)[it.Key()] = Uint32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700194 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700195 }
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400196 {
Paul Stewarte18c33b2012-07-10 20:48:44 -0700197 ReadablePropertyConstIterator<uint64> it = store.GetUint64PropertiesIter();
198 for ( ; !it.AtEnd(); it.Advance()) {
199 SLOG(DBus, 5) << __func__ << " serializing uint64 " << it.Key();
200 (*out)[it.Key()] = Uint64ToVariant(it.value());
201 }
202 }
203 {
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400204 ReadablePropertyConstIterator<RpcIdentifier> it =
205 store.GetRpcIdentifierPropertiesIter();
206 for ( ; !it.AtEnd(); it.Advance()) {
mukesh agrawal465331c2012-05-30 11:26:11 -0700207 SLOG(DBus, 5) << __func__ << " serializing RpcIdentifier " << it.Key();
Darin Petkov4682aa82012-05-31 16:24:11 +0200208 (*out)[it.Key()] = PathToVariant(it.value());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400209 }
210 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700211 return true;
212}
213
214// static
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800215bool DBusAdaptor::ClearProperty(PropertyStore *store,
216 const string &name,
217 ::DBus::Error *error) {
218 Error e;
219 store->ClearProperty(name, &e);
220
221 if (error != NULL) {
222 e.ToDBusError(error);
223 }
224
225 return e.IsSuccess();
226}
227
228// static
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700229void DBusAdaptor::ArgsToKeyValueStore(
230 const map<string, ::DBus::Variant> &args,
231 KeyValueStore *out,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800232 Error *error) { // TODO(quiche): Should be ::DBus::Error?
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700233 for (map<string, ::DBus::Variant>::const_iterator it = args.begin();
234 it != args.end();
235 ++it) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700236 string key = it->first;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700237 DBus::type<string> string_type;
238 DBus::type<bool> bool_type;
239
240 if (it->second.signature() == string_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700241 SLOG(DBus, 5) << "Got string property " << key;
242 out->SetString(key, it->second.reader().get_string());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700243 } else if (it->second.signature() == bool_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700244 SLOG(DBus, 5) << "Got bool property " << key;
245 out->SetBool(key, it->second.reader().get_bool());
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
374::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) {
375 ::DBus::Variant v;
376 v.writer().append_uint32(value);
377 return v;
378}
379
380// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700381::DBus::Variant DBusAdaptor::Uint64ToVariant(uint64 value) {
382 ::DBus::Variant v;
383 v.writer().append_uint64(value);
384 return v;
385}
386
387// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700388bool DBusAdaptor::IsBool(::DBus::Signature signature) {
389 return signature == ::DBus::type<bool>::sig();
390}
391
392// static
393bool DBusAdaptor::IsByte(::DBus::Signature signature) {
394 return signature == ::DBus::type<uint8>::sig();
395}
396
397// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800398bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) {
399 return signature == DBusAdaptor::kByteArraysSig;
400}
401
402// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700403bool DBusAdaptor::IsInt16(::DBus::Signature signature) {
404 return signature == ::DBus::type<int16>::sig();
405}
406
407// static
408bool DBusAdaptor::IsInt32(::DBus::Signature signature) {
409 return signature == ::DBus::type<int32>::sig();
410}
411
412// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700413bool DBusAdaptor::IsPath(::DBus::Signature signature) {
414 return signature == ::DBus::type< ::DBus::Path >::sig();
415}
416
417// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700418bool DBusAdaptor::IsPaths(::DBus::Signature signature) {
419 return signature == DBusAdaptor::kPathsSig;
mukesh agrawal32399322011-09-01 10:53:43 -0700420}
421
422// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700423bool DBusAdaptor::IsString(::DBus::Signature signature) {
424 return signature == ::DBus::type<string>::sig();
425}
426
427// static
428bool DBusAdaptor::IsStringmap(::DBus::Signature signature) {
429 return signature == DBusAdaptor::kStringmapSig;
430}
431
432// static
Chris Masone27c4aa52011-07-02 13:10:14 -0700433bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) {
434 return signature == DBusAdaptor::kStringmapsSig;
435}
436
437// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700438bool DBusAdaptor::IsStrings(::DBus::Signature signature) {
439 return signature == DBusAdaptor::kStringsSig;
440}
441
442// static
443bool DBusAdaptor::IsUint16(::DBus::Signature signature) {
444 return signature == ::DBus::type<uint16>::sig();
445}
446
447// static
448bool DBusAdaptor::IsUint32(::DBus::Signature signature) {
449 return signature == ::DBus::type<uint32>::sig();
450}
451
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100452// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700453bool DBusAdaptor::IsUint64(::DBus::Signature signature) {
454 return signature == ::DBus::type<uint64>::sig();
455}
456
457// static
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500458bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) {
459 return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig();
460}
461
Eric Shienbrood9a245532012-03-07 14:20:39 -0500462void DBusAdaptor::DeferReply(const DBus::Tag *tag) {
463 return_later(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100464}
465
Eric Shienbrood9a245532012-03-07 14:20:39 -0500466void DBusAdaptor::ReplyNow(const DBus::Tag *tag) {
467 Continuation *cont = find_continuation(tag);
468 CHECK(cont);
469 return_now(cont);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100470}
471
Eric Shienbrood9a245532012-03-07 14:20:39 -0500472void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag,
473 const DBus::Error &error) {
474 Continuation *cont = find_continuation(tag);
475 CHECK(cont);
476 return_error(cont, error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100477}
478
Eric Shienbrood9a245532012-03-07 14:20:39 -0500479ResultCallback DBusAdaptor::GetMethodReplyCallback(
480 const DBus::Tag *tag) {
481 return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag));
482}
483
484void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag,
485 const Error &error,
486 DBus::Error *dberror) {
487 if (error.IsOngoing()) {
488 DeferReply(tag);
489 } else if (error.IsFailure()) {
490 error.ToDBusError(dberror);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100491 }
492}
493
Eric Shienbrood9a245532012-03-07 14:20:39 -0500494void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag,
495 const Error &error) {
496 if (error.IsFailure()) {
497 DBus::Error dberror;
498 error.ToDBusError(&dberror);
499 ReplyNowWithError(tag, dberror);
500 } else {
501 ReplyNow(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100502 }
503}
504
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700505} // namespace shill