blob: 6e2883372d8e59ea01ce1c65452817d44d984967 [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";
36
37DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path)
Chris Masoned0ceb8c2011-06-02 10:05:39 -070038 : DBus::ObjectAdaptor(*conn, object_path) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070039 SLOG(DBus, 2) << "DBusAdaptor: " << object_path;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070040}
41
42DBusAdaptor::~DBusAdaptor() {}
43
44// static
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -080045bool DBusAdaptor::SetProperty(PropertyStore *store,
46 const string &name,
47 const ::DBus::Variant &value,
48 ::DBus::Error *error) {
mukesh agrawalffa3d042011-10-06 15:26:10 -070049 Error e;
mukesh agrawalbebf1b82013-04-23 15:06:33 -070050 bool ret;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070051
52 if (DBusAdaptor::IsBool(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070053 ret = store->SetBoolProperty(name, value.reader().get_bool(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070054 else if (DBusAdaptor::IsByte(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070055 ret = store->SetUint8Property(name, value.reader().get_byte(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056 else if (DBusAdaptor::IsInt16(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070057 ret = store->SetInt16Property(name, value.reader().get_int16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070058 else if (DBusAdaptor::IsInt32(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070059 ret = store->SetInt32Property(name, value.reader().get_int32(), &e);
Chris Masone3bd3c8c2011-06-13 08:20:26 -070060 else if (DBusAdaptor::IsPath(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070061 ret = store->SetStringProperty(name, value.reader().get_path(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070062 else if (DBusAdaptor::IsString(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070063 ret = store->SetStringProperty(name, value.reader().get_string(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070064 else if (DBusAdaptor::IsStringmap(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070065 ret = store->SetStringmapProperty(name,
66 value.operator map<string, string>(),
67 &e);
mukesh agrawalffa3d042011-10-06 15:26:10 -070068 else if (DBusAdaptor::IsStringmaps(value.signature())) {
Ben Chanfad4a0b2012-04-18 15:49:59 -070069 SLOG(DBus, 1) << " can't yet handle setting type " << value.signature();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070070 ret = false;
mukesh agrawalffa3d042011-10-06 15:26:10 -070071 e.Populate(Error::kInternalError);
72 } else if (DBusAdaptor::IsStrings(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070073 ret = store->SetStringsProperty(name, value.operator vector<string>(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070074 else if (DBusAdaptor::IsUint16(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070075 ret = store->SetUint16Property(name, value.reader().get_uint16(), &e);
Chris Masone8fe2c7e2011-06-09 15:51:19 -070076 else if (DBusAdaptor::IsUint32(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070077 ret = store->SetUint32Property(name, value.reader().get_uint32(), &e);
Paul Stewarte18c33b2012-07-10 20:48:44 -070078 else if (DBusAdaptor::IsUint64(value.signature()))
mukesh agrawalbebf1b82013-04-23 15:06:33 -070079 ret = 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();
mukesh agrawalbebf1b82013-04-23 15:06:33 -070082 ret = false;
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 agrawalbebf1b82013-04-23 15:06:33 -070086 ret = false;
mukesh agrawalffa3d042011-10-06 15:26:10 -070087 e.Populate(Error::kInternalError);
Chris Masone27c4aa52011-07-02 13:10:14 -070088 }
mukesh agrawalffa3d042011-10-06 15:26:10 -070089
90 if (error != NULL) {
91 e.ToDBusError(error);
92 }
93
mukesh agrawalbebf1b82013-04-23 15:06:33 -070094 return ret;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070095}
96
97// static
mukesh agrawalde29fa82011-09-16 16:16:36 -070098bool DBusAdaptor::GetProperties(const PropertyStore &store,
Chris Masonea8a2c252011-06-27 22:16:30 -070099 map<string, ::DBus::Variant> *out,
mukesh agrawal1830fa12011-09-26 14:31:40 -0700100 ::DBus::Error */*error*/) {
Chris Masonea8a2c252011-06-27 22:16:30 -0700101 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800102 ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700103 for ( ; !it.AtEnd(); it.Advance()) {
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()) {
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()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200116 (*out)[it.Key()] = Int32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700117 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700118 }
119 {
Darin Petkov63138a92012-02-06 14:09:15 +0100120 ReadablePropertyConstIterator<KeyValueStore> it =
121 store.GetKeyValueStorePropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700122 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200123 (*out)[it.Key()] = KeyValueStoreToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700124 }
Darin Petkov63138a92012-02-06 14:09:15 +0100125 }
126 {
mukesh agrawal2366eed2012-03-20 18:21:50 -0700127 ReadablePropertyConstIterator<RpcIdentifiers> it =
128 store.GetRpcIdentifiersPropertiesIter();
129 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200130 const Strings &rpc_identifiers_as_strings = it.value();
mukesh agrawal2366eed2012-03-20 18:21:50 -0700131 vector < ::DBus::Path> rpc_identifiers_as_paths;
132 for (Strings::const_iterator in = rpc_identifiers_as_strings.begin();
133 in != rpc_identifiers_as_strings.end();
134 ++in) {
135 rpc_identifiers_as_paths.push_back(*in);
136 }
137 (*out)[it.Key()] = PathsToVariant(rpc_identifiers_as_paths);
138 }
139 }
140 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800141 ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700142 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200143 (*out)[it.Key()] = StringToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700144 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700145 }
146 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800147 ReadablePropertyConstIterator<Stringmap> it =
148 store.GetStringmapPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700149 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200150 (*out)[it.Key()]= StringmapToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700151 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700152 }
153 {
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400154 ReadablePropertyConstIterator<Stringmaps> it =
155 store.GetStringmapsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700156 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200157 (*out)[it.Key()]= StringmapsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700158 }
Eric Shienbrood30bc0ec2012-03-21 18:19:46 -0400159 }
160 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800161 ReadablePropertyConstIterator<Strings> it =
162 store.GetStringsPropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700163 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200164 (*out)[it.Key()] = StringsToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700165 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700166 }
167 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800168 ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700169 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200170 (*out)[it.Key()] = ByteToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700171 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700172 }
173 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800174 ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700175 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200176 (*out)[it.Key()] = Uint16ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700177 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700178 }
179 {
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800180 ReadablePropertyConstIterator<uint32> it = store.GetUint32PropertiesIter();
mukesh agrawal465331c2012-05-30 11:26:11 -0700181 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200182 (*out)[it.Key()] = Uint32ToVariant(it.value());
mukesh agrawal465331c2012-05-30 11:26:11 -0700183 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700184 }
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400185 {
Paul Stewarte18c33b2012-07-10 20:48:44 -0700186 ReadablePropertyConstIterator<uint64> it = store.GetUint64PropertiesIter();
187 for ( ; !it.AtEnd(); it.Advance()) {
Paul Stewarte18c33b2012-07-10 20:48:44 -0700188 (*out)[it.Key()] = Uint64ToVariant(it.value());
189 }
190 }
191 {
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400192 ReadablePropertyConstIterator<RpcIdentifier> it =
193 store.GetRpcIdentifierPropertiesIter();
194 for ( ; !it.AtEnd(); it.Advance()) {
Darin Petkov4682aa82012-05-31 16:24:11 +0200195 (*out)[it.Key()] = PathToVariant(it.value());
Jason Glasgowacdc11f2012-03-30 14:12:22 -0400196 }
197 }
Chris Masonea8a2c252011-06-27 22:16:30 -0700198 return true;
199}
200
201// static
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800202bool DBusAdaptor::ClearProperty(PropertyStore *store,
203 const string &name,
204 ::DBus::Error *error) {
205 Error e;
206 store->ClearProperty(name, &e);
207
208 if (error != NULL) {
209 e.ToDBusError(error);
210 }
211
212 return e.IsSuccess();
213}
214
215// static
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700216void DBusAdaptor::ArgsToKeyValueStore(
217 const map<string, ::DBus::Variant> &args,
218 KeyValueStore *out,
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800219 Error *error) { // TODO(quiche): Should be ::DBus::Error?
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700220 for (map<string, ::DBus::Variant>::const_iterator it = args.begin();
221 it != args.end();
222 ++it) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700223 string key = it->first;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700224 DBus::type<string> string_type;
225 DBus::type<bool> bool_type;
226
227 if (it->second.signature() == string_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700228 SLOG(DBus, 5) << "Got string property " << key;
229 out->SetString(key, it->second.reader().get_string());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700230 } else if (it->second.signature() == bool_type.sig()) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700231 SLOG(DBus, 5) << "Got bool property " << key;
232 out->SetBool(key, it->second.reader().get_bool());
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700233 } else {
mukesh agrawal06175d72012-04-23 16:46:01 -0700234 Error::PopulateAndLog(error, Error::kInternalError,
235 "unsupported type for property " + key);
Gaurav Shah1b7a6162011-11-09 11:41:01 -0800236 return; // Skip remaining args after error.
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700237 }
238 }
239}
240
241// static
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700242::DBus::Variant DBusAdaptor::BoolToVariant(bool value) {
243 ::DBus::Variant v;
244 v.writer().append_bool(value);
245 return v;
246}
247
248// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800249::DBus::Variant DBusAdaptor::ByteArraysToVariant(const ByteArrays &value) {
250 ::DBus::MessageIter writer;
251 ::DBus::Variant v;
252
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800253
254 // We have to use a local because the operator<< needs a reference
255 // to work on (the lhs) but writer() returns by-value. C++ prohibits
256 // initializing non-const references from a temporary.
257 // So:
258 // v.writer() << value;
259 // would NOT automagically promote the returned value of v.writer() to
260 // a non-const reference (if you think about it, that's almost always not what
261 // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html).
262 //
263 // One could consider changing writer() to return a reference, but then it
264 // changes writer() semantics as it can not be a const reference. writer()
265 // currently doesn't modify the original object on which it's called.
Paul Stewartced6a0b2011-11-08 15:32:04 -0800266 writer = v.writer();
267 writer << value;
268 return v;
269}
270
271// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700272::DBus::Variant DBusAdaptor::ByteToVariant(uint8 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700273 ::DBus::Variant v;
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700274 v.writer().append_byte(value);
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700275 return v;
276}
277
278// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700279::DBus::Variant DBusAdaptor::Int16ToVariant(int16 value) {
280 ::DBus::Variant v;
281 v.writer().append_int16(value);
282 return v;
283}
284
285// static
286::DBus::Variant DBusAdaptor::Int32ToVariant(int32 value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700287 ::DBus::Variant v;
288 v.writer().append_int32(value);
289 return v;
290}
291
292// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700293::DBus::Variant DBusAdaptor::PathToVariant(const ::DBus::Path &value) {
294 ::DBus::Variant v;
295 v.writer().append_path(value.c_str());
296 return v;
297}
298
299// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700300::DBus::Variant DBusAdaptor::PathsToVariant(
mukesh agrawal32399322011-09-01 10:53:43 -0700301 const vector< ::DBus::Path> &value) {
302 ::DBus::MessageIter writer;
303 ::DBus::Variant v;
304
Gaurav Shah7ad8e532011-11-11 17:14:49 -0800305 // See note above on why we need to use a local.
mukesh agrawal32399322011-09-01 10:53:43 -0700306 writer = v.writer();
307 writer << value;
308 return v;
309}
310
311// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700312::DBus::Variant DBusAdaptor::StringToVariant(const string &value) {
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700313 ::DBus::Variant v;
314 v.writer().append_string(value.c_str());
315 return v;
316}
317
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700318// static
Chris Masone889666b2011-07-03 12:58:50 -0700319::DBus::Variant DBusAdaptor::StringmapToVariant(const Stringmap &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700320 ::DBus::Variant v;
321 ::DBus::MessageIter writer = v.writer();
322 writer << value;
323 return v;
324}
325
326// static
Chris Masone889666b2011-07-03 12:58:50 -0700327::DBus::Variant DBusAdaptor::StringmapsToVariant(const Stringmaps &value) {
Chris Masoneb925cc82011-06-22 15:39:57 -0700328 ::DBus::Variant v;
329 ::DBus::MessageIter writer = v.writer();
330 writer << value;
331 return v;
332}
333
334// static
Chris Masone889666b2011-07-03 12:58:50 -0700335::DBus::Variant DBusAdaptor::StringsToVariant(const Strings &value) {
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700336 ::DBus::Variant v;
337 ::DBus::MessageIter writer = v.writer();
338 writer << value;
339 return v;
340}
341
342// static
Darin Petkov63138a92012-02-06 14:09:15 +0100343::DBus::Variant DBusAdaptor::KeyValueStoreToVariant(
344 const KeyValueStore &value) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200345 DBusPropertiesMap props;
346 DBusProperties::ConvertKeyValueStoreToMap(value, &props);
Chris Masone889666b2011-07-03 12:58:50 -0700347 ::DBus::Variant v;
348 ::DBus::MessageIter writer = v.writer();
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500349 writer << props;
Chris Masone889666b2011-07-03 12:58:50 -0700350 return v;
351}
352
353// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700354::DBus::Variant DBusAdaptor::Uint16ToVariant(uint16 value) {
355 ::DBus::Variant v;
356 v.writer().append_uint16(value);
357 return v;
358}
359
360// static
361::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) {
362 ::DBus::Variant v;
363 v.writer().append_uint32(value);
364 return v;
365}
366
367// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700368::DBus::Variant DBusAdaptor::Uint64ToVariant(uint64 value) {
369 ::DBus::Variant v;
370 v.writer().append_uint64(value);
371 return v;
372}
373
374// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700375bool DBusAdaptor::IsBool(::DBus::Signature signature) {
376 return signature == ::DBus::type<bool>::sig();
377}
378
379// static
380bool DBusAdaptor::IsByte(::DBus::Signature signature) {
381 return signature == ::DBus::type<uint8>::sig();
382}
383
384// static
Paul Stewartced6a0b2011-11-08 15:32:04 -0800385bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) {
386 return signature == DBusAdaptor::kByteArraysSig;
387}
388
389// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700390bool DBusAdaptor::IsInt16(::DBus::Signature signature) {
391 return signature == ::DBus::type<int16>::sig();
392}
393
394// static
395bool DBusAdaptor::IsInt32(::DBus::Signature signature) {
396 return signature == ::DBus::type<int32>::sig();
397}
398
399// static
Chris Masone3bd3c8c2011-06-13 08:20:26 -0700400bool DBusAdaptor::IsPath(::DBus::Signature signature) {
401 return signature == ::DBus::type< ::DBus::Path >::sig();
402}
403
404// static
mukesh agrawal2366eed2012-03-20 18:21:50 -0700405bool DBusAdaptor::IsPaths(::DBus::Signature signature) {
406 return signature == DBusAdaptor::kPathsSig;
mukesh agrawal32399322011-09-01 10:53:43 -0700407}
408
409// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700410bool DBusAdaptor::IsString(::DBus::Signature signature) {
411 return signature == ::DBus::type<string>::sig();
412}
413
414// static
415bool DBusAdaptor::IsStringmap(::DBus::Signature signature) {
416 return signature == DBusAdaptor::kStringmapSig;
417}
418
419// static
Chris Masone27c4aa52011-07-02 13:10:14 -0700420bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) {
421 return signature == DBusAdaptor::kStringmapsSig;
422}
423
424// static
Chris Masone8fe2c7e2011-06-09 15:51:19 -0700425bool DBusAdaptor::IsStrings(::DBus::Signature signature) {
426 return signature == DBusAdaptor::kStringsSig;
427}
428
429// static
430bool DBusAdaptor::IsUint16(::DBus::Signature signature) {
431 return signature == ::DBus::type<uint16>::sig();
432}
433
434// static
435bool DBusAdaptor::IsUint32(::DBus::Signature signature) {
436 return signature == ::DBus::type<uint32>::sig();
437}
438
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100439// static
Paul Stewarte18c33b2012-07-10 20:48:44 -0700440bool DBusAdaptor::IsUint64(::DBus::Signature signature) {
441 return signature == ::DBus::type<uint64>::sig();
442}
443
444// static
Eric Shienbroodb23d4b92012-02-16 12:32:42 -0500445bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) {
446 return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig();
447}
448
Eric Shienbrood9a245532012-03-07 14:20:39 -0500449void DBusAdaptor::DeferReply(const DBus::Tag *tag) {
450 return_later(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100451}
452
Eric Shienbrood9a245532012-03-07 14:20:39 -0500453void DBusAdaptor::ReplyNow(const DBus::Tag *tag) {
454 Continuation *cont = find_continuation(tag);
455 CHECK(cont);
456 return_now(cont);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100457}
458
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800459template <typename T>
460void DBusAdaptor::TypedReplyNow(const DBus::Tag *tag, const T &value) {
461 Continuation *cont = find_continuation(tag);
462 CHECK(cont);
463 cont->writer() << value;
464 return_now(cont);
465}
466
Eric Shienbrood9a245532012-03-07 14:20:39 -0500467void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag,
468 const DBus::Error &error) {
469 Continuation *cont = find_continuation(tag);
470 CHECK(cont);
471 return_error(cont, error);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100472}
473
Eric Shienbrood9a245532012-03-07 14:20:39 -0500474ResultCallback DBusAdaptor::GetMethodReplyCallback(
475 const DBus::Tag *tag) {
476 return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag));
477}
478
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800479ResultStringCallback DBusAdaptor::GetStringMethodReplyCallback(
480 const DBus::Tag *tag) {
481 return Bind(&DBusAdaptor::StringMethodReplyCallback, AsWeakPtr(), Owned(tag));
482}
483
484ResultBoolCallback DBusAdaptor::GetBoolMethodReplyCallback(
485 const DBus::Tag *tag) {
486 return Bind(&DBusAdaptor::BoolMethodReplyCallback, AsWeakPtr(), Owned(tag));
487}
488
489template<typename T>
490void DBusAdaptor::TypedMethodReplyCallback(const DBus::Tag *tag,
491 const Error &error,
492 const T &returned) {
493 if (error.IsFailure()) {
494 DBus::Error dberror;
495 error.ToDBusError(&dberror);
496 ReplyNowWithError(tag, dberror);
497 } else {
498 TypedReplyNow(tag, returned);
499 }
500}
501
Eric Shienbrood9a245532012-03-07 14:20:39 -0500502void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag,
503 const Error &error,
504 DBus::Error *dberror) {
505 if (error.IsOngoing()) {
506 DeferReply(tag);
507 } else if (error.IsFailure()) {
508 error.ToDBusError(dberror);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100509 }
510}
511
Eric Shienbrood9a245532012-03-07 14:20:39 -0500512void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag,
513 const Error &error) {
514 if (error.IsFailure()) {
515 DBus::Error dberror;
516 error.ToDBusError(&dberror);
517 ReplyNowWithError(tag, dberror);
518 } else {
519 ReplyNow(tag);
Darin Petkove5bc2cb2011-12-07 14:47:32 +0100520 }
521}
522
Christopher Wiley0d9cf0c2013-02-19 19:24:57 -0800523void DBusAdaptor::StringMethodReplyCallback(const DBus::Tag *tag,
524 const Error &error,
525 const string &returned) {
526 TypedMethodReplyCallback(tag, error, returned);
527}
528
529void DBusAdaptor::BoolMethodReplyCallback(const DBus::Tag *tag,
530 const Error &error,
531 bool returned) {
532 TypedMethodReplyCallback(tag, error, returned);
533}
534
Chris Masoned0ceb8c2011-06-02 10:05:39 -0700535} // namespace shill