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