Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include <map> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 6 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 7 | #include <vector> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 8 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | #include <base/callback.h> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 12 | #include <dbus-c++/dbus.h> |
| 13 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 14 | #include "shill/accessor_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 15 | #include "shill/dbus_adaptor.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 16 | #include "shill/error.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 17 | #include "shill/key_value_store.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 18 | #include "shill/property_store.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 19 | #include "shill/scope_logger.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 20 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 21 | using base::Bind; |
| 22 | using base::Owned; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | using std::map; |
| 24 | using std::string; |
| 25 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 26 | |
| 27 | namespace shill { |
| 28 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 30 | const char DBusAdaptor::kByteArraysSig[] = "aay"; |
| 31 | // static |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 32 | const char DBusAdaptor::kPathsSig[] = "ao"; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 33 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 34 | const char DBusAdaptor::kStringmapSig[] = "a{ss}"; |
| 35 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 36 | const char DBusAdaptor::kStringmapsSig[] = "aa{ss}"; |
| 37 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 38 | const char DBusAdaptor::kStringsSig[] = "as"; |
| 39 | |
| 40 | DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path) |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 41 | : DBus::ObjectAdaptor(*conn, object_path) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 42 | SLOG(DBus, 2) << "DBusAdaptor: " << object_path; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DBusAdaptor::~DBusAdaptor() {} |
| 46 | |
| 47 | // static |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 48 | bool DBusAdaptor::SetProperty(PropertyStore *store, |
| 49 | const string &name, |
| 50 | const ::DBus::Variant &value, |
| 51 | ::DBus::Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 52 | Error e; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 53 | |
| 54 | if (DBusAdaptor::IsBool(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 55 | store->SetBoolProperty(name, value.reader().get_bool(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 56 | else if (DBusAdaptor::IsByte(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 57 | store->SetUint8Property(name, value.reader().get_byte(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 58 | else if (DBusAdaptor::IsInt16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 59 | store->SetInt16Property(name, value.reader().get_int16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 60 | else if (DBusAdaptor::IsInt32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 61 | store->SetInt32Property(name, value.reader().get_int32(), &e); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 62 | else if (DBusAdaptor::IsPath(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 63 | store->SetStringProperty(name, value.reader().get_path(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 64 | else if (DBusAdaptor::IsString(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 65 | store->SetStringProperty(name, value.reader().get_string(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 66 | else if (DBusAdaptor::IsStringmap(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 67 | store->SetStringmapProperty(name, |
| 68 | value.operator map<string, string>(), |
| 69 | &e); |
| 70 | else if (DBusAdaptor::IsStringmaps(value.signature())) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 71 | SLOG(DBus, 1) << " can't yet handle setting type " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 72 | e.Populate(Error::kInternalError); |
| 73 | } else if (DBusAdaptor::IsStrings(value.signature())) |
| 74 | store->SetStringsProperty(name, value.operator vector<string>(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 75 | else if (DBusAdaptor::IsUint16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 76 | store->SetUint16Property(name, value.reader().get_uint16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 77 | else if (DBusAdaptor::IsUint32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 78 | store->SetUint32Property(name, value.reader().get_uint32(), &e); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 79 | else if (DBusAdaptor::IsKeyValueStore(value.signature())) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 80 | SLOG(DBus, 1) << " can't yet handle setting type " << value.signature(); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 81 | e.Populate(Error::kInternalError); |
| 82 | } else { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 83 | NOTREACHED() << " unknown type: " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 84 | e.Populate(Error::kInternalError); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 85 | } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 86 | |
| 87 | if (error != NULL) { |
| 88 | e.ToDBusError(error); |
| 89 | } |
| 90 | |
| 91 | return e.IsSuccess(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // static |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 95 | bool DBusAdaptor::GetProperties(const PropertyStore &store, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 96 | map<string, ::DBus::Variant> *out, |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 97 | ::DBus::Error */*error*/) { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 98 | Error e; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 99 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 100 | ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 101 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 102 | (*out)[it.Key()] = BoolToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 103 | } |
| 104 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 105 | ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 106 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 107 | (*out)[it.Key()] = Int16ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 108 | } |
| 109 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 110 | ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 111 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 112 | (*out)[it.Key()] = Int32ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 113 | } |
| 114 | { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 115 | ReadablePropertyConstIterator<KeyValueStore> it = |
| 116 | store.GetKeyValueStorePropertiesIter(); |
| 117 | for ( ; !it.AtEnd(); it.Advance()) |
| 118 | (*out)[it.Key()] = KeyValueStoreToVariant(it.Value(&e)); |
| 119 | } |
| 120 | { |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 121 | ReadablePropertyConstIterator<RpcIdentifiers> it = |
| 122 | store.GetRpcIdentifiersPropertiesIter(); |
| 123 | for ( ; !it.AtEnd(); it.Advance()) { |
| 124 | Strings rpc_identifiers_as_strings = it.Value(&e); |
| 125 | vector < ::DBus::Path> rpc_identifiers_as_paths; |
| 126 | for (Strings::const_iterator in = rpc_identifiers_as_strings.begin(); |
| 127 | in != rpc_identifiers_as_strings.end(); |
| 128 | ++in) { |
| 129 | rpc_identifiers_as_paths.push_back(*in); |
| 130 | } |
| 131 | (*out)[it.Key()] = PathsToVariant(rpc_identifiers_as_paths); |
| 132 | } |
| 133 | } |
| 134 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 135 | ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 136 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 137 | (*out)[it.Key()] = StringToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 138 | } |
| 139 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 140 | ReadablePropertyConstIterator<Stringmap> it = |
| 141 | store.GetStringmapPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 142 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 143 | (*out)[it.Key()]= StringmapToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 144 | } |
| 145 | { |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 146 | ReadablePropertyConstIterator<Stringmaps> it = |
| 147 | store.GetStringmapsPropertiesIter(); |
| 148 | for ( ; !it.AtEnd(); it.Advance()) |
| 149 | (*out)[it.Key()]= StringmapsToVariant(it.Value(&e)); |
| 150 | } |
| 151 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 152 | ReadablePropertyConstIterator<Strings> it = |
| 153 | store.GetStringsPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 154 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 155 | (*out)[it.Key()] = StringsToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 156 | } |
| 157 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 158 | ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 159 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 160 | (*out)[it.Key()] = ByteToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 161 | } |
| 162 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 163 | ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 164 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 165 | (*out)[it.Key()] = Uint16ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 166 | } |
| 167 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 168 | ReadablePropertyConstIterator<uint32> it = store.GetUint32PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 169 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 170 | (*out)[it.Key()] = Uint32ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 171 | } |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 172 | { |
| 173 | ReadablePropertyConstIterator<RpcIdentifier> it = |
| 174 | store.GetRpcIdentifierPropertiesIter(); |
| 175 | for ( ; !it.AtEnd(); it.Advance()) { |
| 176 | (*out)[it.Key()] = PathToVariant(it.Value(&e)); |
| 177 | } |
| 178 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 179 | return true; |
| 180 | } |
| 181 | |
| 182 | // static |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 183 | bool DBusAdaptor::ClearProperty(PropertyStore *store, |
| 184 | const string &name, |
| 185 | ::DBus::Error *error) { |
| 186 | Error e; |
| 187 | store->ClearProperty(name, &e); |
| 188 | |
| 189 | if (error != NULL) { |
| 190 | e.ToDBusError(error); |
| 191 | } |
| 192 | |
| 193 | return e.IsSuccess(); |
| 194 | } |
| 195 | |
| 196 | // static |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 197 | void DBusAdaptor::ArgsToKeyValueStore( |
| 198 | const map<string, ::DBus::Variant> &args, |
| 199 | KeyValueStore *out, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 200 | Error *error) { // TODO(quiche): Should be ::DBus::Error? |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 201 | for (map<string, ::DBus::Variant>::const_iterator it = args.begin(); |
| 202 | it != args.end(); |
| 203 | ++it) { |
| 204 | DBus::type<string> string_type; |
| 205 | DBus::type<bool> bool_type; |
| 206 | |
| 207 | if (it->second.signature() == string_type.sig()) { |
| 208 | out->SetString(it->first, it->second.reader().get_string()); |
| 209 | } else if (it->second.signature() == bool_type.sig()) { |
| 210 | out->SetBool(it->first, it->second.reader().get_bool()); |
| 211 | } else { |
| 212 | error->Populate(Error::kInternalError); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 213 | return; // Skip remaining args after error. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // static |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 219 | ::DBus::Variant DBusAdaptor::BoolToVariant(bool value) { |
| 220 | ::DBus::Variant v; |
| 221 | v.writer().append_bool(value); |
| 222 | return v; |
| 223 | } |
| 224 | |
| 225 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 226 | ::DBus::Variant DBusAdaptor::ByteArraysToVariant(const ByteArrays &value) { |
| 227 | ::DBus::MessageIter writer; |
| 228 | ::DBus::Variant v; |
| 229 | |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 230 | |
| 231 | // We have to use a local because the operator<< needs a reference |
| 232 | // to work on (the lhs) but writer() returns by-value. C++ prohibits |
| 233 | // initializing non-const references from a temporary. |
| 234 | // So: |
| 235 | // v.writer() << value; |
| 236 | // would NOT automagically promote the returned value of v.writer() to |
| 237 | // a non-const reference (if you think about it, that's almost always not what |
| 238 | // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html). |
| 239 | // |
| 240 | // One could consider changing writer() to return a reference, but then it |
| 241 | // changes writer() semantics as it can not be a const reference. writer() |
| 242 | // currently doesn't modify the original object on which it's called. |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 243 | writer = v.writer(); |
| 244 | writer << value; |
| 245 | return v; |
| 246 | } |
| 247 | |
| 248 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 249 | ::DBus::Variant DBusAdaptor::ByteToVariant(uint8 value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 250 | ::DBus::Variant v; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 251 | v.writer().append_byte(value); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 252 | return v; |
| 253 | } |
| 254 | |
| 255 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 256 | ::DBus::Variant DBusAdaptor::Int16ToVariant(int16 value) { |
| 257 | ::DBus::Variant v; |
| 258 | v.writer().append_int16(value); |
| 259 | return v; |
| 260 | } |
| 261 | |
| 262 | // static |
| 263 | ::DBus::Variant DBusAdaptor::Int32ToVariant(int32 value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 264 | ::DBus::Variant v; |
| 265 | v.writer().append_int32(value); |
| 266 | return v; |
| 267 | } |
| 268 | |
| 269 | // static |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 270 | ::DBus::Variant DBusAdaptor::PathToVariant(const ::DBus::Path &value) { |
| 271 | ::DBus::Variant v; |
| 272 | v.writer().append_path(value.c_str()); |
| 273 | return v; |
| 274 | } |
| 275 | |
| 276 | // static |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 277 | ::DBus::Variant DBusAdaptor::PathsToVariant( |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 278 | const vector< ::DBus::Path> &value) { |
| 279 | ::DBus::MessageIter writer; |
| 280 | ::DBus::Variant v; |
| 281 | |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 282 | // See note above on why we need to use a local. |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 283 | writer = v.writer(); |
| 284 | writer << value; |
| 285 | return v; |
| 286 | } |
| 287 | |
| 288 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 289 | ::DBus::Variant DBusAdaptor::StringToVariant(const string &value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 290 | ::DBus::Variant v; |
| 291 | v.writer().append_string(value.c_str()); |
| 292 | return v; |
| 293 | } |
| 294 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 295 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 296 | ::DBus::Variant DBusAdaptor::StringmapToVariant(const Stringmap &value) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 297 | ::DBus::Variant v; |
| 298 | ::DBus::MessageIter writer = v.writer(); |
| 299 | writer << value; |
| 300 | return v; |
| 301 | } |
| 302 | |
| 303 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 304 | ::DBus::Variant DBusAdaptor::StringmapsToVariant(const Stringmaps &value) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 305 | ::DBus::Variant v; |
| 306 | ::DBus::MessageIter writer = v.writer(); |
| 307 | writer << value; |
| 308 | return v; |
| 309 | } |
| 310 | |
| 311 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 312 | ::DBus::Variant DBusAdaptor::StringsToVariant(const Strings &value) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 313 | ::DBus::Variant v; |
| 314 | ::DBus::MessageIter writer = v.writer(); |
| 315 | writer << value; |
| 316 | return v; |
| 317 | } |
| 318 | |
| 319 | // static |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 320 | ::DBus::Variant DBusAdaptor::KeyValueStoreToVariant( |
| 321 | const KeyValueStore &value) { |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 322 | ::DBus::Variant v; |
| 323 | ::DBus::MessageIter writer = v.writer(); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 324 | map<string, ::DBus::Variant> props; |
| 325 | { |
| 326 | map<string, string>::const_iterator it; |
| 327 | for (it = value.string_properties().begin(); |
| 328 | it != value.string_properties().end(); |
| 329 | ++it) { |
| 330 | ::DBus::Variant vv; |
| 331 | ::DBus::MessageIter writer = vv.writer(); |
| 332 | writer.append_string(it->second.c_str()); |
| 333 | props[it->first] = vv; |
| 334 | } |
| 335 | } |
| 336 | { |
| 337 | map<string, bool>::const_iterator it; |
| 338 | for (it = value.bool_properties().begin(); |
| 339 | it != value.bool_properties().end(); |
| 340 | ++it) { |
| 341 | ::DBus::Variant vv; |
| 342 | ::DBus::MessageIter writer = vv.writer(); |
| 343 | writer.append_bool(it->second); |
| 344 | props[it->first] = vv; |
| 345 | } |
| 346 | } |
| 347 | { |
| 348 | map<string, uint32>::const_iterator it; |
| 349 | for (it = value.uint_properties().begin(); |
| 350 | it != value.uint_properties().end(); |
| 351 | ++it) { |
| 352 | ::DBus::Variant vv; |
| 353 | ::DBus::MessageIter writer = vv.writer(); |
| 354 | writer.append_uint32(it->second); |
| 355 | props[it->first] = vv; |
| 356 | } |
| 357 | } |
| 358 | writer << props; |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 359 | return v; |
| 360 | } |
| 361 | |
| 362 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 363 | ::DBus::Variant DBusAdaptor::Uint16ToVariant(uint16 value) { |
| 364 | ::DBus::Variant v; |
| 365 | v.writer().append_uint16(value); |
| 366 | return v; |
| 367 | } |
| 368 | |
| 369 | // static |
| 370 | ::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) { |
| 371 | ::DBus::Variant v; |
| 372 | v.writer().append_uint32(value); |
| 373 | return v; |
| 374 | } |
| 375 | |
| 376 | // static |
| 377 | bool DBusAdaptor::IsBool(::DBus::Signature signature) { |
| 378 | return signature == ::DBus::type<bool>::sig(); |
| 379 | } |
| 380 | |
| 381 | // static |
| 382 | bool DBusAdaptor::IsByte(::DBus::Signature signature) { |
| 383 | return signature == ::DBus::type<uint8>::sig(); |
| 384 | } |
| 385 | |
| 386 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 387 | bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) { |
| 388 | return signature == DBusAdaptor::kByteArraysSig; |
| 389 | } |
| 390 | |
| 391 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 392 | bool DBusAdaptor::IsInt16(::DBus::Signature signature) { |
| 393 | return signature == ::DBus::type<int16>::sig(); |
| 394 | } |
| 395 | |
| 396 | // static |
| 397 | bool DBusAdaptor::IsInt32(::DBus::Signature signature) { |
| 398 | return signature == ::DBus::type<int32>::sig(); |
| 399 | } |
| 400 | |
| 401 | // static |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 402 | bool DBusAdaptor::IsPath(::DBus::Signature signature) { |
| 403 | return signature == ::DBus::type< ::DBus::Path >::sig(); |
| 404 | } |
| 405 | |
| 406 | // static |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 407 | bool DBusAdaptor::IsPaths(::DBus::Signature signature) { |
| 408 | return signature == DBusAdaptor::kPathsSig; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 412 | bool DBusAdaptor::IsString(::DBus::Signature signature) { |
| 413 | return signature == ::DBus::type<string>::sig(); |
| 414 | } |
| 415 | |
| 416 | // static |
| 417 | bool DBusAdaptor::IsStringmap(::DBus::Signature signature) { |
| 418 | return signature == DBusAdaptor::kStringmapSig; |
| 419 | } |
| 420 | |
| 421 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 422 | bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) { |
| 423 | return signature == DBusAdaptor::kStringmapsSig; |
| 424 | } |
| 425 | |
| 426 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 427 | bool DBusAdaptor::IsStrings(::DBus::Signature signature) { |
| 428 | return signature == DBusAdaptor::kStringsSig; |
| 429 | } |
| 430 | |
| 431 | // static |
| 432 | bool DBusAdaptor::IsUint16(::DBus::Signature signature) { |
| 433 | return signature == ::DBus::type<uint16>::sig(); |
| 434 | } |
| 435 | |
| 436 | // static |
| 437 | bool DBusAdaptor::IsUint32(::DBus::Signature signature) { |
| 438 | return signature == ::DBus::type<uint32>::sig(); |
| 439 | } |
| 440 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 441 | // static |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 442 | bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) { |
| 443 | return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig(); |
| 444 | } |
| 445 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 446 | void DBusAdaptor::DeferReply(const DBus::Tag *tag) { |
| 447 | return_later(tag); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 448 | } |
| 449 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 450 | void DBusAdaptor::ReplyNow(const DBus::Tag *tag) { |
| 451 | Continuation *cont = find_continuation(tag); |
| 452 | CHECK(cont); |
| 453 | return_now(cont); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 454 | } |
| 455 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 456 | void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag, |
| 457 | const DBus::Error &error) { |
| 458 | Continuation *cont = find_continuation(tag); |
| 459 | CHECK(cont); |
| 460 | return_error(cont, error); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 463 | ResultCallback DBusAdaptor::GetMethodReplyCallback( |
| 464 | const DBus::Tag *tag) { |
| 465 | return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag)); |
| 466 | } |
| 467 | |
| 468 | void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag, |
| 469 | const Error &error, |
| 470 | DBus::Error *dberror) { |
| 471 | if (error.IsOngoing()) { |
| 472 | DeferReply(tag); |
| 473 | } else if (error.IsFailure()) { |
| 474 | error.ToDBusError(dberror); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 478 | void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag, |
| 479 | const Error &error) { |
| 480 | if (error.IsFailure()) { |
| 481 | DBus::Error dberror; |
| 482 | error.ToDBusError(&dberror); |
| 483 | ReplyNowWithError(tag, dberror); |
| 484 | } else { |
| 485 | ReplyNow(tag); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 486 | } |
| 487 | } |
| 488 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 489 | } // namespace shill |