Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -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 | |
| 5 | #include "shill/dbus_properties.h" |
| 6 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 7 | #include <map> |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 11 | #include "shill/scope_logger.h" |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 12 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 13 | using std::map; |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 14 | using std::string; |
| 15 | using std::vector; |
| 16 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 17 | namespace shill { |
| 18 | |
| 19 | // static |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 20 | bool DBusProperties::GetBool(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 21 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 22 | bool *value) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 23 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 24 | if (it == properties.end()) { |
| 25 | return false; |
| 26 | } |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 27 | *value = it->second.reader().get_bool(); |
| 28 | SLOG(DBus, 2) << key << " = " << *value; |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | // static |
| 33 | bool DBusProperties::GetInt32(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 34 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 35 | int32 *value) { |
| 36 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 37 | if (it == properties.end()) { |
| 38 | return false; |
| 39 | } |
| 40 | *value = it->second.reader().get_int32(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 41 | SLOG(DBus, 2) << key << " = " << *value; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | // static |
Nathan Williams | e984080 | 2012-04-18 18:47:40 -0400 | [diff] [blame] | 46 | bool DBusProperties::GetObjectPath(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 47 | const string &key, |
| 48 | string *value) { |
Nathan Williams | e984080 | 2012-04-18 18:47:40 -0400 | [diff] [blame] | 49 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 50 | if (it == properties.end()) { |
| 51 | return false; |
| 52 | } |
| 53 | *value = it->second.reader().get_path(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 54 | SLOG(DBus, 2) << key << " = " << *value; |
Nathan Williams | e984080 | 2012-04-18 18:47:40 -0400 | [diff] [blame] | 55 | return true; |
| 56 | } |
| 57 | |
| 58 | // static |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 59 | bool DBusProperties::GetString(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 60 | const string &key, |
| 61 | string *value) { |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 62 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 63 | if (it == properties.end()) { |
| 64 | return false; |
| 65 | } |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 66 | *value = it->second.reader().get_string(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 67 | SLOG(DBus, 2) << key << " = " << *value; |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 68 | return true; |
| 69 | } |
| 70 | |
| 71 | // static |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 72 | bool DBusProperties::GetStrings(const DBusPropertiesMap &properties, |
| 73 | const string &key, |
| 74 | vector<string> *value) { |
| 75 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 76 | if (it == properties.end()) { |
| 77 | return false; |
| 78 | } |
| 79 | DBus::MessageIter iter(it->second.reader()); |
| 80 | value->clear(); |
| 81 | iter >> *value; |
| 82 | SLOG(DBus, 2) << key << " = " ; |
| 83 | for(vector<string>::const_iterator it = value->begin(); |
| 84 | it != value->end(); ++it) |
| 85 | SLOG(DBus, 2) << " " << *it; |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | // static |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 90 | bool DBusProperties::GetUint16(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 91 | const string &key, |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 92 | uint16 *value) { |
| 93 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 94 | if (it == properties.end()) { |
| 95 | return false; |
| 96 | } |
| 97 | *value = it->second.reader().get_uint16(); |
Ben Chan | fad4a0b | 2012-04-18 15:49:59 -0700 | [diff] [blame] | 98 | SLOG(DBus, 2) << key << " = " << *value; |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 102 | // static |
| 103 | bool DBusProperties::GetUint32(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 104 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 105 | uint32 *value) { |
| 106 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 107 | if (it == properties.end()) { |
| 108 | return false; |
| 109 | } |
| 110 | *value = it->second.reader().get_uint32(); |
| 111 | SLOG(DBus, 2) << key << " = " << *value; |
| 112 | return true; |
| 113 | } |
| 114 | |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 115 | // static |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 116 | bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties, |
| 117 | const string &key, |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 118 | RpcIdentifiers *value) { |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 119 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 120 | if (it == properties.end()) { |
| 121 | return false; |
| 122 | } |
| 123 | vector<DBus::Path> paths = it->second.operator vector<DBus::Path>(); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 124 | ConvertPathsToRpcIdentifiers(paths, value); |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 125 | SLOG(DBus, 2) << key << " = (RpcIdentfier)[" << value->size() << "]"; |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | // static |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 130 | void DBusProperties::ConvertPathsToRpcIdentifiers( |
| 131 | const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) { |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 132 | CHECK(rpc_identifiers); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 133 | rpc_identifiers->clear(); |
| 134 | for (vector<DBus::Path>::const_iterator it = dbus_paths.begin(); |
| 135 | it != dbus_paths.end(); ++it) { |
| 136 | rpc_identifiers->push_back(*it); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // static |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 141 | void DBusProperties::ConvertKeyValueStoreToMap( |
| 142 | const KeyValueStore &store, DBusPropertiesMap *properties) { |
| 143 | CHECK(properties); |
| 144 | properties->clear(); |
| 145 | for (map<string, string>::const_iterator it = |
| 146 | store.string_properties().begin(); |
| 147 | it != store.string_properties().end(); ++it) { |
| 148 | (*properties)[it->first].writer().append_string(it->second.c_str()); |
| 149 | } |
| 150 | for (map<string, bool>::const_iterator it = store.bool_properties().begin(); |
| 151 | it != store.bool_properties().end(); ++it) { |
| 152 | (*properties)[it->first].writer().append_bool(it->second); |
| 153 | } |
| 154 | for (map<string, int32>::const_iterator it = store.int_properties().begin(); |
| 155 | it != store.int_properties().end(); ++it) { |
| 156 | (*properties)[it->first].writer().append_int32(it->second); |
| 157 | } |
| 158 | for (map<string, uint32>::const_iterator it = store.uint_properties().begin(); |
| 159 | it != store.uint_properties().end(); ++it) { |
| 160 | (*properties)[it->first].writer().append_uint32(it->second); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // static |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 165 | std::string DBusProperties::KeysToString(const std::map<std::string, |
| 166 | ::DBus::Variant> &args) { |
| 167 | std::string keys; |
| 168 | for (std::map<std::string, ::DBus::Variant>:: const_iterator it = |
| 169 | args.begin(); it != args.end(); ++it) { |
| 170 | keys.append(" "); |
| 171 | keys.append(it->first); |
| 172 | } |
| 173 | return keys; |
| 174 | } |
| 175 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 176 | } // namespace shill |