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