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