Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 5 | #include <map> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 6 | #include <string> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 7 | #include <vector> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 8 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | #include <base/callback.h> |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 11 | #include <base/logging.h> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 12 | #include <dbus-c++/dbus.h> |
| 13 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 14 | #include "shill/accessor_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 15 | #include "shill/dbus_adaptor.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 16 | #include "shill/error.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 17 | #include "shill/key_value_store.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 18 | #include "shill/property_store.h" |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 19 | #include "shill/scope_logger.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 20 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 21 | using base::Bind; |
| 22 | using base::Owned; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 23 | using std::map; |
| 24 | using std::string; |
| 25 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 26 | |
| 27 | namespace shill { |
| 28 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 30 | const char DBusAdaptor::kByteArraysSig[] = "aay"; |
| 31 | // static |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 32 | const char DBusAdaptor::kPathsSig[] = "ao"; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 33 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 34 | const char DBusAdaptor::kStringmapSig[] = "a{ss}"; |
| 35 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 36 | const char DBusAdaptor::kStringmapsSig[] = "aa{ss}"; |
| 37 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 38 | const char DBusAdaptor::kStringsSig[] = "as"; |
| 39 | |
| 40 | DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path) |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 41 | : DBus::ObjectAdaptor(*conn, object_path) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 42 | SLOG(DBus, 2) << "DBusAdaptor: " << object_path; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | DBusAdaptor::~DBusAdaptor() {} |
| 46 | |
| 47 | // static |
mukesh agrawal | 6bb9e7c | 2012-01-30 14:57:54 -0800 | [diff] [blame] | 48 | bool DBusAdaptor::SetProperty(PropertyStore *store, |
| 49 | const string &name, |
| 50 | const ::DBus::Variant &value, |
| 51 | ::DBus::Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 52 | Error e; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 53 | |
| 54 | if (DBusAdaptor::IsBool(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 55 | store->SetBoolProperty(name, value.reader().get_bool(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 56 | else if (DBusAdaptor::IsByte(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 57 | store->SetUint8Property(name, value.reader().get_byte(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 58 | else if (DBusAdaptor::IsInt16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 59 | store->SetInt16Property(name, value.reader().get_int16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 60 | else if (DBusAdaptor::IsInt32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 61 | store->SetInt32Property(name, value.reader().get_int32(), &e); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 62 | else if (DBusAdaptor::IsPath(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 63 | store->SetStringProperty(name, value.reader().get_path(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 64 | else if (DBusAdaptor::IsString(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 65 | store->SetStringProperty(name, value.reader().get_string(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 66 | else if (DBusAdaptor::IsStringmap(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 67 | store->SetStringmapProperty(name, |
| 68 | value.operator map<string, string>(), |
| 69 | &e); |
| 70 | else if (DBusAdaptor::IsStringmaps(value.signature())) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 71 | SLOG(DBus, 1) << " can't yet handle setting type " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 72 | e.Populate(Error::kInternalError); |
| 73 | } else if (DBusAdaptor::IsStrings(value.signature())) |
| 74 | store->SetStringsProperty(name, value.operator vector<string>(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 75 | else if (DBusAdaptor::IsUint16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 76 | store->SetUint16Property(name, value.reader().get_uint16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 77 | else if (DBusAdaptor::IsUint32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 78 | store->SetUint32Property(name, value.reader().get_uint32(), &e); |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame^] | 79 | else if (DBusAdaptor::IsUint64(value.signature())) |
| 80 | store->SetUint64Property(name, value.reader().get_uint64(), &e); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 81 | else if (DBusAdaptor::IsKeyValueStore(value.signature())) { |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 82 | SLOG(DBus, 1) << " can't yet handle setting type " << value.signature(); |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 83 | e.Populate(Error::kInternalError); |
| 84 | } else { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 85 | NOTREACHED() << " unknown type: " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 86 | e.Populate(Error::kInternalError); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 87 | } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 88 | |
| 89 | if (error != NULL) { |
| 90 | e.ToDBusError(error); |
| 91 | } |
| 92 | |
| 93 | return e.IsSuccess(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // static |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 97 | bool DBusAdaptor::GetProperties(const PropertyStore &store, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 98 | map<string, ::DBus::Variant> *out, |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 99 | ::DBus::Error */*error*/) { |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 100 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 101 | ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 102 | for ( ; !it.AtEnd(); it.Advance()) { |
| 103 | SLOG(DBus, 5) << __func__ << " serializing bool " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 104 | (*out)[it.Key()] = BoolToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 105 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 106 | } |
| 107 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 108 | ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 109 | for ( ; !it.AtEnd(); it.Advance()) { |
| 110 | SLOG(DBus, 5) << __func__ << " serializing int16 " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 111 | (*out)[it.Key()] = Int16ToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 112 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 113 | } |
| 114 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 115 | ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 116 | for ( ; !it.AtEnd(); it.Advance()) { |
| 117 | SLOG(DBus, 5) << __func__ << " serializing int32 " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 118 | (*out)[it.Key()] = Int32ToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 119 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 120 | } |
| 121 | { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 122 | ReadablePropertyConstIterator<KeyValueStore> it = |
| 123 | store.GetKeyValueStorePropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 124 | for ( ; !it.AtEnd(); it.Advance()) { |
| 125 | SLOG(DBus, 5) << __func__ << " serializing KeyValueStore " << it.Key(); |
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 | } |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 140 | SLOG(DBus, 5) << __func__ << " serializing RpcIdentifiers " << it.Key(); |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 141 | (*out)[it.Key()] = PathsToVariant(rpc_identifiers_as_paths); |
| 142 | } |
| 143 | } |
| 144 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 145 | ReadablePropertyConstIterator<string> it = store.GetStringPropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 146 | for ( ; !it.AtEnd(); it.Advance()) { |
| 147 | SLOG(DBus, 5) << __func__ << " serializing string " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 148 | (*out)[it.Key()] = StringToVariant(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 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 152 | ReadablePropertyConstIterator<Stringmap> it = |
| 153 | store.GetStringmapPropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 154 | for ( ; !it.AtEnd(); it.Advance()) { |
| 155 | SLOG(DBus, 5) << __func__ << " serializing Stringmap " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 156 | (*out)[it.Key()]= StringmapToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 157 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 158 | } |
| 159 | { |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 160 | ReadablePropertyConstIterator<Stringmaps> it = |
| 161 | store.GetStringmapsPropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 162 | for ( ; !it.AtEnd(); it.Advance()) { |
| 163 | SLOG(DBus, 5) << __func__ << " serializing Stringmaps " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 164 | (*out)[it.Key()]= StringmapsToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 165 | } |
Eric Shienbrood | 30bc0ec | 2012-03-21 18:19:46 -0400 | [diff] [blame] | 166 | } |
| 167 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 168 | ReadablePropertyConstIterator<Strings> it = |
| 169 | store.GetStringsPropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 170 | for ( ; !it.AtEnd(); it.Advance()) { |
| 171 | SLOG(DBus, 5) << __func__ << " serializing Strings " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 172 | (*out)[it.Key()] = StringsToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 173 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 174 | } |
| 175 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 176 | ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 177 | for ( ; !it.AtEnd(); it.Advance()) { |
| 178 | SLOG(DBus, 5) << __func__ << " serializing uint8 " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 179 | (*out)[it.Key()] = ByteToVariant(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 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 183 | ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter(); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 184 | for ( ; !it.AtEnd(); it.Advance()) { |
| 185 | SLOG(DBus, 5) << __func__ << " serializing uint16 " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 186 | (*out)[it.Key()] = Uint16ToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 187 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 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()) { |
| 192 | SLOG(DBus, 5) << __func__ << " serializing uint32 " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 193 | (*out)[it.Key()] = Uint32ToVariant(it.value()); |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 194 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 195 | } |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 196 | { |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame^] | 197 | ReadablePropertyConstIterator<uint64> it = store.GetUint64PropertiesIter(); |
| 198 | for ( ; !it.AtEnd(); it.Advance()) { |
| 199 | SLOG(DBus, 5) << __func__ << " serializing uint64 " << it.Key(); |
| 200 | (*out)[it.Key()] = Uint64ToVariant(it.value()); |
| 201 | } |
| 202 | } |
| 203 | { |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 204 | ReadablePropertyConstIterator<RpcIdentifier> it = |
| 205 | store.GetRpcIdentifierPropertiesIter(); |
| 206 | for ( ; !it.AtEnd(); it.Advance()) { |
mukesh agrawal | 465331c | 2012-05-30 11:26:11 -0700 | [diff] [blame] | 207 | SLOG(DBus, 5) << __func__ << " serializing RpcIdentifier " << it.Key(); |
Darin Petkov | 4682aa8 | 2012-05-31 16:24:11 +0200 | [diff] [blame] | 208 | (*out)[it.Key()] = PathToVariant(it.value()); |
Jason Glasgow | acdc11f | 2012-03-30 14:12:22 -0400 | [diff] [blame] | 209 | } |
| 210 | } |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 211 | return true; |
| 212 | } |
| 213 | |
| 214 | // static |
mukesh agrawal | 8abd2f6 | 2012-01-30 14:56:14 -0800 | [diff] [blame] | 215 | bool DBusAdaptor::ClearProperty(PropertyStore *store, |
| 216 | const string &name, |
| 217 | ::DBus::Error *error) { |
| 218 | Error e; |
| 219 | store->ClearProperty(name, &e); |
| 220 | |
| 221 | if (error != NULL) { |
| 222 | e.ToDBusError(error); |
| 223 | } |
| 224 | |
| 225 | return e.IsSuccess(); |
| 226 | } |
| 227 | |
| 228 | // static |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 229 | void DBusAdaptor::ArgsToKeyValueStore( |
| 230 | const map<string, ::DBus::Variant> &args, |
| 231 | KeyValueStore *out, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 232 | Error *error) { // TODO(quiche): Should be ::DBus::Error? |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 233 | for (map<string, ::DBus::Variant>::const_iterator it = args.begin(); |
| 234 | it != args.end(); |
| 235 | ++it) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 236 | string key = it->first; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 237 | DBus::type<string> string_type; |
| 238 | DBus::type<bool> bool_type; |
| 239 | |
| 240 | if (it->second.signature() == string_type.sig()) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 241 | SLOG(DBus, 5) << "Got string property " << key; |
| 242 | out->SetString(key, it->second.reader().get_string()); |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 243 | } else if (it->second.signature() == bool_type.sig()) { |
mukesh agrawal | 06175d7 | 2012-04-23 16:46:01 -0700 | [diff] [blame] | 244 | SLOG(DBus, 5) << "Got bool property " << key; |
| 245 | out->SetBool(key, it->second.reader().get_bool()); |
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 |
| 374 | ::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) { |
| 375 | ::DBus::Variant v; |
| 376 | v.writer().append_uint32(value); |
| 377 | return v; |
| 378 | } |
| 379 | |
| 380 | // static |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame^] | 381 | ::DBus::Variant DBusAdaptor::Uint64ToVariant(uint64 value) { |
| 382 | ::DBus::Variant v; |
| 383 | v.writer().append_uint64(value); |
| 384 | return v; |
| 385 | } |
| 386 | |
| 387 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 388 | bool DBusAdaptor::IsBool(::DBus::Signature signature) { |
| 389 | return signature == ::DBus::type<bool>::sig(); |
| 390 | } |
| 391 | |
| 392 | // static |
| 393 | bool DBusAdaptor::IsByte(::DBus::Signature signature) { |
| 394 | return signature == ::DBus::type<uint8>::sig(); |
| 395 | } |
| 396 | |
| 397 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 398 | bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) { |
| 399 | return signature == DBusAdaptor::kByteArraysSig; |
| 400 | } |
| 401 | |
| 402 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 403 | bool DBusAdaptor::IsInt16(::DBus::Signature signature) { |
| 404 | return signature == ::DBus::type<int16>::sig(); |
| 405 | } |
| 406 | |
| 407 | // static |
| 408 | bool DBusAdaptor::IsInt32(::DBus::Signature signature) { |
| 409 | return signature == ::DBus::type<int32>::sig(); |
| 410 | } |
| 411 | |
| 412 | // static |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 413 | bool DBusAdaptor::IsPath(::DBus::Signature signature) { |
| 414 | return signature == ::DBus::type< ::DBus::Path >::sig(); |
| 415 | } |
| 416 | |
| 417 | // static |
mukesh agrawal | 2366eed | 2012-03-20 18:21:50 -0700 | [diff] [blame] | 418 | bool DBusAdaptor::IsPaths(::DBus::Signature signature) { |
| 419 | return signature == DBusAdaptor::kPathsSig; |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 423 | bool DBusAdaptor::IsString(::DBus::Signature signature) { |
| 424 | return signature == ::DBus::type<string>::sig(); |
| 425 | } |
| 426 | |
| 427 | // static |
| 428 | bool DBusAdaptor::IsStringmap(::DBus::Signature signature) { |
| 429 | return signature == DBusAdaptor::kStringmapSig; |
| 430 | } |
| 431 | |
| 432 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 433 | bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) { |
| 434 | return signature == DBusAdaptor::kStringmapsSig; |
| 435 | } |
| 436 | |
| 437 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 438 | bool DBusAdaptor::IsStrings(::DBus::Signature signature) { |
| 439 | return signature == DBusAdaptor::kStringsSig; |
| 440 | } |
| 441 | |
| 442 | // static |
| 443 | bool DBusAdaptor::IsUint16(::DBus::Signature signature) { |
| 444 | return signature == ::DBus::type<uint16>::sig(); |
| 445 | } |
| 446 | |
| 447 | // static |
| 448 | bool DBusAdaptor::IsUint32(::DBus::Signature signature) { |
| 449 | return signature == ::DBus::type<uint32>::sig(); |
| 450 | } |
| 451 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 452 | // static |
Paul Stewart | e18c33b | 2012-07-10 20:48:44 -0700 | [diff] [blame^] | 453 | bool DBusAdaptor::IsUint64(::DBus::Signature signature) { |
| 454 | return signature == ::DBus::type<uint64>::sig(); |
| 455 | } |
| 456 | |
| 457 | // static |
Eric Shienbrood | b23d4b9 | 2012-02-16 12:32:42 -0500 | [diff] [blame] | 458 | bool DBusAdaptor::IsKeyValueStore(::DBus::Signature signature) { |
| 459 | return signature == ::DBus::type<map<string, ::DBus::Variant> >::sig(); |
| 460 | } |
| 461 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 462 | void DBusAdaptor::DeferReply(const DBus::Tag *tag) { |
| 463 | return_later(tag); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 464 | } |
| 465 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 466 | void DBusAdaptor::ReplyNow(const DBus::Tag *tag) { |
| 467 | Continuation *cont = find_continuation(tag); |
| 468 | CHECK(cont); |
| 469 | return_now(cont); |
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 | void DBusAdaptor::ReplyNowWithError(const DBus::Tag *tag, |
| 473 | const DBus::Error &error) { |
| 474 | Continuation *cont = find_continuation(tag); |
| 475 | CHECK(cont); |
| 476 | return_error(cont, error); |
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 | ResultCallback DBusAdaptor::GetMethodReplyCallback( |
| 480 | const DBus::Tag *tag) { |
| 481 | return Bind(&DBusAdaptor::MethodReplyCallback, AsWeakPtr(), Owned(tag)); |
| 482 | } |
| 483 | |
| 484 | void DBusAdaptor::ReturnResultOrDefer(const DBus::Tag *tag, |
| 485 | const Error &error, |
| 486 | DBus::Error *dberror) { |
| 487 | if (error.IsOngoing()) { |
| 488 | DeferReply(tag); |
| 489 | } else if (error.IsFailure()) { |
| 490 | error.ToDBusError(dberror); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Eric Shienbrood | 9a24553 | 2012-03-07 14:20:39 -0500 | [diff] [blame] | 494 | void DBusAdaptor::MethodReplyCallback(const DBus::Tag *tag, |
| 495 | const Error &error) { |
| 496 | if (error.IsFailure()) { |
| 497 | DBus::Error dberror; |
| 498 | error.ToDBusError(&dberror); |
| 499 | ReplyNowWithError(tag, dberror); |
| 500 | } else { |
| 501 | ReplyNow(tag); |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 505 | } // namespace shill |