Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 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 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 9 | #include <base/logging.h> |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 10 | #include <dbus-c++/dbus.h> |
| 11 | |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 12 | #include "shill/accessor_interface.h" |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 13 | #include "shill/dbus_adaptor.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 14 | #include "shill/error.h" |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 15 | #include "shill/key_value_store.h" |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 16 | #include "shill/property_store.h" |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 17 | |
| 18 | using std::map; |
| 19 | using std::string; |
| 20 | using std::vector; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 21 | |
| 22 | namespace shill { |
| 23 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 24 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 25 | const char DBusAdaptor::kByteArraysSig[] = "aay"; |
| 26 | // static |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 27 | const char DBusAdaptor::kPathArraySig[] = "ao"; |
| 28 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 29 | const char DBusAdaptor::kStringmapSig[] = "a{ss}"; |
| 30 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 31 | const char DBusAdaptor::kStringmapsSig[] = "aa{ss}"; |
| 32 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 33 | const char DBusAdaptor::kStringsSig[] = "as"; |
| 34 | |
| 35 | DBusAdaptor::DBusAdaptor(DBus::Connection* conn, const string &object_path) |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 36 | : DBus::ObjectAdaptor(*conn, object_path) { |
Darin Petkov | 67d8ecf | 2011-07-26 16:03:30 -0700 | [diff] [blame] | 37 | VLOG(2) << "DBusAdaptor: " << object_path; |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | DBusAdaptor::~DBusAdaptor() {} |
| 41 | |
| 42 | // static |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 43 | bool DBusAdaptor::DispatchOnType(PropertyStore *store, |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 44 | const string &name, |
| 45 | const ::DBus::Variant &value, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 46 | ::DBus::Error *error) { |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 47 | Error e; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 48 | |
| 49 | if (DBusAdaptor::IsBool(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 50 | store->SetBoolProperty(name, value.reader().get_bool(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 51 | else if (DBusAdaptor::IsByte(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 52 | store->SetUint8Property(name, value.reader().get_byte(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 53 | else if (DBusAdaptor::IsInt16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 54 | store->SetInt16Property(name, value.reader().get_int16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 55 | else if (DBusAdaptor::IsInt32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 56 | store->SetInt32Property(name, value.reader().get_int32(), &e); |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 57 | else if (DBusAdaptor::IsPath(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 58 | store->SetStringProperty(name, value.reader().get_path(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 59 | else if (DBusAdaptor::IsString(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 60 | store->SetStringProperty(name, value.reader().get_string(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 61 | else if (DBusAdaptor::IsStringmap(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 62 | store->SetStringmapProperty(name, |
| 63 | value.operator map<string, string>(), |
| 64 | &e); |
| 65 | else if (DBusAdaptor::IsStringmaps(value.signature())) { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 66 | VLOG(1) << " can't yet handle setting type " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 67 | e.Populate(Error::kInternalError); |
| 68 | } else if (DBusAdaptor::IsStrings(value.signature())) |
| 69 | store->SetStringsProperty(name, value.operator vector<string>(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 70 | else if (DBusAdaptor::IsUint16(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 71 | store->SetUint16Property(name, value.reader().get_uint16(), &e); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 72 | else if (DBusAdaptor::IsUint32(value.signature())) |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 73 | store->SetUint32Property(name, value.reader().get_uint32(), &e); |
| 74 | else { |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 75 | NOTREACHED() << " unknown type: " << value.signature(); |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 76 | e.Populate(Error::kInternalError); |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 77 | } |
mukesh agrawal | ffa3d04 | 2011-10-06 15:26:10 -0700 | [diff] [blame] | 78 | |
| 79 | if (error != NULL) { |
| 80 | e.ToDBusError(error); |
| 81 | } |
| 82 | |
| 83 | return e.IsSuccess(); |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // static |
mukesh agrawal | de29fa8 | 2011-09-16 16:16:36 -0700 | [diff] [blame] | 87 | bool DBusAdaptor::GetProperties(const PropertyStore &store, |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 88 | map<string, ::DBus::Variant> *out, |
mukesh agrawal | 1830fa1 | 2011-09-26 14:31:40 -0700 | [diff] [blame] | 89 | ::DBus::Error */*error*/) { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 90 | Error e; |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 91 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 92 | ReadablePropertyConstIterator<bool> it = store.GetBoolPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 93 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 94 | (*out)[it.Key()] = BoolToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 95 | } |
| 96 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 97 | ReadablePropertyConstIterator<int16> it = store.GetInt16PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 98 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 99 | (*out)[it.Key()] = Int16ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 100 | } |
| 101 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 102 | ReadablePropertyConstIterator<int32> it = store.GetInt32PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 103 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 104 | (*out)[it.Key()] = Int32ToVariant(it.Value(&e)); |
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<string> it = store.GetStringPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 108 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 109 | (*out)[it.Key()] = StringToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 110 | } |
| 111 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 112 | ReadablePropertyConstIterator<Stringmap> it = |
| 113 | store.GetStringmapPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 114 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 115 | (*out)[it.Key()]= StringmapToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 116 | } |
| 117 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 118 | ReadablePropertyConstIterator<Strings> it = |
| 119 | store.GetStringsPropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 120 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 121 | (*out)[it.Key()] = StringsToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 122 | } |
| 123 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 124 | ReadablePropertyConstIterator<uint8> it = store.GetUint8PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 125 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 126 | (*out)[it.Key()] = ByteToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 127 | } |
| 128 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 129 | ReadablePropertyConstIterator<uint16> it = store.GetUint16PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 130 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 131 | (*out)[it.Key()] = Uint16ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 132 | } |
| 133 | { |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 134 | ReadablePropertyConstIterator<uint32> it = store.GetUint32PropertiesIter(); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 135 | for ( ; !it.AtEnd(); it.Advance()) |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 136 | (*out)[it.Key()] = Uint32ToVariant(it.Value(&e)); |
Chris Masone | a8a2c25 | 2011-06-27 22:16:30 -0700 | [diff] [blame] | 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| 141 | // static |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 142 | void DBusAdaptor::ArgsToKeyValueStore( |
| 143 | const map<string, ::DBus::Variant> &args, |
| 144 | KeyValueStore *out, |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 145 | Error *error) { // TODO(quiche): Should be ::DBus::Error? |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 146 | for (map<string, ::DBus::Variant>::const_iterator it = args.begin(); |
| 147 | it != args.end(); |
| 148 | ++it) { |
| 149 | DBus::type<string> string_type; |
| 150 | DBus::type<bool> bool_type; |
| 151 | |
| 152 | if (it->second.signature() == string_type.sig()) { |
| 153 | out->SetString(it->first, it->second.reader().get_string()); |
| 154 | } else if (it->second.signature() == bool_type.sig()) { |
| 155 | out->SetBool(it->first, it->second.reader().get_bool()); |
| 156 | } else { |
| 157 | error->Populate(Error::kInternalError); |
Gaurav Shah | 1b7a616 | 2011-11-09 11:41:01 -0800 | [diff] [blame] | 158 | return; // Skip remaining args after error. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | // static |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 164 | ::DBus::Variant DBusAdaptor::BoolToVariant(bool value) { |
| 165 | ::DBus::Variant v; |
| 166 | v.writer().append_bool(value); |
| 167 | return v; |
| 168 | } |
| 169 | |
| 170 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 171 | ::DBus::Variant DBusAdaptor::ByteArraysToVariant(const ByteArrays &value) { |
| 172 | ::DBus::MessageIter writer; |
| 173 | ::DBus::Variant v; |
| 174 | |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 175 | |
| 176 | // We have to use a local because the operator<< needs a reference |
| 177 | // to work on (the lhs) but writer() returns by-value. C++ prohibits |
| 178 | // initializing non-const references from a temporary. |
| 179 | // So: |
| 180 | // v.writer() << value; |
| 181 | // would NOT automagically promote the returned value of v.writer() to |
| 182 | // a non-const reference (if you think about it, that's almost always not what |
| 183 | // you'd want. see: http://gcc.gnu.org/ml/gcc-help/2006-04/msg00075.html). |
| 184 | // |
| 185 | // One could consider changing writer() to return a reference, but then it |
| 186 | // changes writer() semantics as it can not be a const reference. writer() |
| 187 | // currently doesn't modify the original object on which it's called. |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 188 | writer = v.writer(); |
| 189 | writer << value; |
| 190 | return v; |
| 191 | } |
| 192 | |
| 193 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 194 | ::DBus::Variant DBusAdaptor::ByteToVariant(uint8 value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 195 | ::DBus::Variant v; |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 196 | v.writer().append_byte(value); |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 197 | return v; |
| 198 | } |
| 199 | |
| 200 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 201 | ::DBus::Variant DBusAdaptor::Int16ToVariant(int16 value) { |
| 202 | ::DBus::Variant v; |
| 203 | v.writer().append_int16(value); |
| 204 | return v; |
| 205 | } |
| 206 | |
| 207 | // static |
| 208 | ::DBus::Variant DBusAdaptor::Int32ToVariant(int32 value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 209 | ::DBus::Variant v; |
| 210 | v.writer().append_int32(value); |
| 211 | return v; |
| 212 | } |
| 213 | |
| 214 | // static |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 215 | ::DBus::Variant DBusAdaptor::PathToVariant(const ::DBus::Path &value) { |
| 216 | ::DBus::Variant v; |
| 217 | v.writer().append_path(value.c_str()); |
| 218 | return v; |
| 219 | } |
| 220 | |
| 221 | // static |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 222 | ::DBus::Variant DBusAdaptor::PathArrayToVariant( |
| 223 | const vector< ::DBus::Path> &value) { |
| 224 | ::DBus::MessageIter writer; |
| 225 | ::DBus::Variant v; |
| 226 | |
Gaurav Shah | 7ad8e53 | 2011-11-11 17:14:49 -0800 | [diff] [blame] | 227 | // See note above on why we need to use a local. |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 228 | writer = v.writer(); |
| 229 | writer << value; |
| 230 | return v; |
| 231 | } |
| 232 | |
| 233 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 234 | ::DBus::Variant DBusAdaptor::StringToVariant(const string &value) { |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 235 | ::DBus::Variant v; |
| 236 | v.writer().append_string(value.c_str()); |
| 237 | return v; |
| 238 | } |
| 239 | |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 240 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 241 | ::DBus::Variant DBusAdaptor::StringmapToVariant(const Stringmap &value) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 242 | ::DBus::Variant v; |
| 243 | ::DBus::MessageIter writer = v.writer(); |
| 244 | writer << value; |
| 245 | return v; |
| 246 | } |
| 247 | |
| 248 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 249 | ::DBus::Variant DBusAdaptor::StringmapsToVariant(const Stringmaps &value) { |
Chris Masone | b925cc8 | 2011-06-22 15:39:57 -0700 | [diff] [blame] | 250 | ::DBus::Variant v; |
| 251 | ::DBus::MessageIter writer = v.writer(); |
| 252 | writer << value; |
| 253 | return v; |
| 254 | } |
| 255 | |
| 256 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 257 | ::DBus::Variant DBusAdaptor::StringsToVariant(const Strings &value) { |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 258 | ::DBus::Variant v; |
| 259 | ::DBus::MessageIter writer = v.writer(); |
| 260 | writer << value; |
| 261 | return v; |
| 262 | } |
| 263 | |
| 264 | // static |
Chris Masone | 889666b | 2011-07-03 12:58:50 -0700 | [diff] [blame] | 265 | ::DBus::Variant DBusAdaptor::StrIntPairToVariant(const StrIntPair &value) { |
| 266 | ::DBus::Variant v; |
| 267 | ::DBus::MessageIter writer = v.writer(); |
| 268 | writer << value.string_property(); |
| 269 | writer << value.uint_property(); |
| 270 | return v; |
| 271 | } |
| 272 | |
| 273 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 274 | ::DBus::Variant DBusAdaptor::Uint16ToVariant(uint16 value) { |
| 275 | ::DBus::Variant v; |
| 276 | v.writer().append_uint16(value); |
| 277 | return v; |
| 278 | } |
| 279 | |
| 280 | // static |
| 281 | ::DBus::Variant DBusAdaptor::Uint32ToVariant(uint32 value) { |
| 282 | ::DBus::Variant v; |
| 283 | v.writer().append_uint32(value); |
| 284 | return v; |
| 285 | } |
| 286 | |
| 287 | // static |
| 288 | bool DBusAdaptor::IsBool(::DBus::Signature signature) { |
| 289 | return signature == ::DBus::type<bool>::sig(); |
| 290 | } |
| 291 | |
| 292 | // static |
| 293 | bool DBusAdaptor::IsByte(::DBus::Signature signature) { |
| 294 | return signature == ::DBus::type<uint8>::sig(); |
| 295 | } |
| 296 | |
| 297 | // static |
Paul Stewart | ced6a0b | 2011-11-08 15:32:04 -0800 | [diff] [blame] | 298 | bool DBusAdaptor::IsByteArrays(::DBus::Signature signature) { |
| 299 | return signature == DBusAdaptor::kByteArraysSig; |
| 300 | } |
| 301 | |
| 302 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 303 | bool DBusAdaptor::IsInt16(::DBus::Signature signature) { |
| 304 | return signature == ::DBus::type<int16>::sig(); |
| 305 | } |
| 306 | |
| 307 | // static |
| 308 | bool DBusAdaptor::IsInt32(::DBus::Signature signature) { |
| 309 | return signature == ::DBus::type<int32>::sig(); |
| 310 | } |
| 311 | |
| 312 | // static |
Chris Masone | 3bd3c8c | 2011-06-13 08:20:26 -0700 | [diff] [blame] | 313 | bool DBusAdaptor::IsPath(::DBus::Signature signature) { |
| 314 | return signature == ::DBus::type< ::DBus::Path >::sig(); |
| 315 | } |
| 316 | |
| 317 | // static |
mukesh agrawal | 3239932 | 2011-09-01 10:53:43 -0700 | [diff] [blame] | 318 | bool DBusAdaptor::IsPathArray(::DBus::Signature signature) { |
| 319 | return signature == DBusAdaptor::kPathArraySig; |
| 320 | } |
| 321 | |
| 322 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 323 | bool DBusAdaptor::IsString(::DBus::Signature signature) { |
| 324 | return signature == ::DBus::type<string>::sig(); |
| 325 | } |
| 326 | |
| 327 | // static |
| 328 | bool DBusAdaptor::IsStringmap(::DBus::Signature signature) { |
| 329 | return signature == DBusAdaptor::kStringmapSig; |
| 330 | } |
| 331 | |
| 332 | // static |
Chris Masone | 27c4aa5 | 2011-07-02 13:10:14 -0700 | [diff] [blame] | 333 | bool DBusAdaptor::IsStringmaps(::DBus::Signature signature) { |
| 334 | return signature == DBusAdaptor::kStringmapsSig; |
| 335 | } |
| 336 | |
| 337 | // static |
Chris Masone | 8fe2c7e | 2011-06-09 15:51:19 -0700 | [diff] [blame] | 338 | bool DBusAdaptor::IsStrings(::DBus::Signature signature) { |
| 339 | return signature == DBusAdaptor::kStringsSig; |
| 340 | } |
| 341 | |
| 342 | // static |
| 343 | bool DBusAdaptor::IsUint16(::DBus::Signature signature) { |
| 344 | return signature == ::DBus::type<uint16>::sig(); |
| 345 | } |
| 346 | |
| 347 | // static |
| 348 | bool DBusAdaptor::IsUint32(::DBus::Signature signature) { |
| 349 | return signature == ::DBus::type<uint32>::sig(); |
| 350 | } |
| 351 | |
Darin Petkov | e5bc2cb | 2011-12-07 14:47:32 +0100 | [diff] [blame] | 352 | // static |
| 353 | DBusAdaptor::Returner *DBusAdaptor::Returner::Create(DBusAdaptor *adaptor) { |
| 354 | return new Returner(adaptor); |
| 355 | } |
| 356 | |
| 357 | DBusAdaptor::Returner::Returner(DBusAdaptor *adaptor) |
| 358 | : adaptor_(adaptor), |
| 359 | state_(kStateInitialized) { |
| 360 | VLOG(2) << __func__ << " @ " << this; |
| 361 | } |
| 362 | |
| 363 | DBusAdaptor::Returner::~Returner() { |
| 364 | CHECK(state_ != kStateDestroyed); |
| 365 | VLOG(2) << "Destroying returner @ " << this << " state: " << state_; |
| 366 | adaptor_ = NULL; |
| 367 | state_ = kStateDestroyed; |
| 368 | } |
| 369 | |
| 370 | void DBusAdaptor::Returner::Return() { |
| 371 | VLOG(2) << __func__ << " @ " << this << " state: " << state_; |
| 372 | switch (state_) { |
| 373 | case kStateInitialized: |
| 374 | // Service method is returning right away, without any continuation. |
| 375 | state_ = kStateReturned; |
| 376 | return; |
| 377 | case kStateDelayed: { |
| 378 | // This return happens in the continuation. |
| 379 | DBus::ObjectAdaptor::Continuation *cont = |
| 380 | adaptor_->find_continuation(this); |
| 381 | CHECK(cont); |
| 382 | adaptor_->return_now(cont); |
| 383 | delete this; |
| 384 | return; |
| 385 | } |
| 386 | default: |
| 387 | NOTREACHED() << "Unexpected state: " << state_; |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void DBusAdaptor::Returner::ReturnError(const Error &error) { |
| 393 | VLOG(2) << __func__ << " @ " << this << " state: " << state_; |
| 394 | switch (state_) { |
| 395 | case kStateInitialized: |
| 396 | // Service method is returning right away, without any continuation. |
| 397 | error_.CopyFrom(error); |
| 398 | state_ = kStateReturned; |
| 399 | return; |
| 400 | case kStateDelayed: { |
| 401 | // This return happens in the continuation. |
| 402 | DBus::Error dbus_error; |
| 403 | error.ToDBusError(&dbus_error); |
| 404 | DBus::ObjectAdaptor::Continuation *cont = |
| 405 | adaptor_->find_continuation(this); |
| 406 | CHECK(cont); |
| 407 | adaptor_->return_error(cont, dbus_error); |
| 408 | delete this; |
| 409 | return; |
| 410 | } |
| 411 | default: |
| 412 | NOTREACHED() << "Unexpected state: " << state_; |
| 413 | break; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | void DBusAdaptor::Returner::DelayOrReturn(DBus::Error *error) { |
| 418 | VLOG(2) << __func__ << " @ " << this << " state: " << state_; |
| 419 | switch (state_) { |
| 420 | case kStateInitialized: |
| 421 | // Service method needs continuation so delay the return. |
| 422 | state_ = kStateDelayed; |
| 423 | |
| 424 | // return_later does not return. It unwinds the stack up to the dbus-c++ |
| 425 | // message handler by throwing an exception. |
| 426 | adaptor_->return_later(this); |
| 427 | return; |
| 428 | case kStateReturned: |
| 429 | // Service method has returned right away, without any continuation. |
| 430 | error_.ToDBusError(error); |
| 431 | delete this; |
| 432 | return; |
| 433 | default: |
| 434 | NOTREACHED() << "Unexpected state: " << state_; |
| 435 | break; |
| 436 | } |
| 437 | } |
| 438 | |
Chris Masone | d0ceb8c | 2011-06-02 10:05:39 -0700 | [diff] [blame] | 439 | } // namespace shill |