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