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 | |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 7 | #include <dbus/dbus.h> |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 8 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 9 | #include "shill/logging.h" |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 10 | |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 11 | using std::map; |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 12 | using std::string; |
| 13 | using std::vector; |
| 14 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 15 | namespace shill { |
| 16 | |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 17 | namespace { |
| 18 | |
| 19 | template <typename ValueType> |
| 20 | bool GetValue(const DBusPropertiesMap &properties, |
| 21 | const string &key, |
| 22 | ValueType *value) { |
| 23 | CHECK(value); |
| 24 | |
| 25 | DBusPropertiesMap::const_iterator it = properties.find(key); |
| 26 | if (it == properties.end()) { |
| 27 | SLOG(DBus, 2) << "Key '" << key << "' not found."; |
| 28 | return false; |
| 29 | } |
| 30 | |
| 31 | string actual_type = it->second.signature(); |
| 32 | string expected_type = DBus::type<ValueType>::sig(); |
| 33 | if (actual_type != expected_type) { |
| 34 | SLOG(DBus, 2) << "Key '" << key << "' type mismatch (expected '" |
| 35 | << expected_type << "', actual '" << actual_type << "')."; |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | *value = it->second.operator ValueType(); |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | } // namespace |
| 44 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 45 | // static |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 46 | bool DBusProperties::GetBool(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 47 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 48 | bool *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 49 | return GetValue<bool>(properties, key, value); |
| 50 | } |
| 51 | |
| 52 | // static |
| 53 | bool DBusProperties::GetDBusPropertiesMap(const DBusPropertiesMap &properties, |
| 54 | const string &key, |
| 55 | DBusPropertiesMap *value) { |
| 56 | return GetValue<DBusPropertiesMap>(properties, key, value); |
| 57 | } |
| 58 | |
| 59 | // static |
| 60 | bool DBusProperties::GetDouble(const DBusPropertiesMap &properties, |
| 61 | const string &key, |
| 62 | double *value) { |
| 63 | return GetValue<double>(properties, key, value); |
| 64 | } |
| 65 | |
| 66 | // static |
| 67 | bool DBusProperties::GetInt16(const DBusPropertiesMap &properties, |
| 68 | const string &key, |
| 69 | int16 *value) { |
| 70 | return GetValue<int16>(properties, key, value); |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // static |
| 74 | bool DBusProperties::GetInt32(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 75 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 76 | int32 *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 77 | return GetValue<int32>(properties, key, value); |
| 78 | } |
| 79 | |
| 80 | // static |
| 81 | bool DBusProperties::GetInt64(const DBusPropertiesMap &properties, |
| 82 | const string &key, |
| 83 | int64 *value) { |
| 84 | return GetValue<int64>(properties, key, value); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // static |
Nathan Williams | e984080 | 2012-04-18 18:47:40 -0400 | [diff] [blame] | 88 | bool DBusProperties::GetObjectPath(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 89 | const string &key, |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 90 | DBus::Path *value) { |
| 91 | return GetValue<DBus::Path>(properties, key, value); |
Nathan Williams | e984080 | 2012-04-18 18:47:40 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // static |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 95 | bool DBusProperties::GetString(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 96 | const string &key, |
| 97 | string *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 98 | return GetValue<string>(properties, key, value); |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // static |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 102 | bool DBusProperties::GetStringmap(const DBusPropertiesMap &properties, |
| 103 | const string &key, |
| 104 | map<string, string> *value) { |
| 105 | return GetValue<map<string, string>>(properties, key, value); |
| 106 | } |
| 107 | |
| 108 | // static |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 109 | bool DBusProperties::GetStrings(const DBusPropertiesMap &properties, |
| 110 | const string &key, |
| 111 | vector<string> *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 112 | return GetValue<vector<string>>(properties, key, value); |
| 113 | } |
| 114 | |
| 115 | // static |
| 116 | bool DBusProperties::GetUint8(const DBusPropertiesMap &properties, |
| 117 | const string &key, |
| 118 | uint8 *value) { |
| 119 | return GetValue<uint8>(properties, key, value); |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // static |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 123 | bool DBusProperties::GetUint16(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 124 | const string &key, |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 125 | uint16 *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 126 | return GetValue<uint16>(properties, key, value); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 129 | // static |
| 130 | bool DBusProperties::GetUint32(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 131 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 132 | uint32 *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 133 | return GetValue<uint32>(properties, key, value); |
| 134 | } |
| 135 | |
| 136 | // static |
| 137 | bool DBusProperties::GetUint64(const DBusPropertiesMap &properties, |
| 138 | const string &key, |
| 139 | uint64 *value) { |
| 140 | return GetValue<uint64>(properties, key, value); |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 141 | } |
| 142 | |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 143 | // static |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 144 | bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties, |
| 145 | const string &key, |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 146 | RpcIdentifiers *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 147 | vector<DBus::Path> paths; |
| 148 | if (GetValue<vector<DBus::Path>>(properties, key, &paths)) { |
| 149 | ConvertPathsToRpcIdentifiers(paths, value); |
| 150 | return true; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 151 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 152 | return false; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // static |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 156 | void DBusProperties::ConvertPathsToRpcIdentifiers( |
| 157 | const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) { |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 158 | CHECK(rpc_identifiers); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 159 | rpc_identifiers->assign(dbus_paths.begin(), dbus_paths.end()); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | // static |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 163 | void DBusProperties::ConvertKeyValueStoreToMap( |
| 164 | const KeyValueStore &store, DBusPropertiesMap *properties) { |
| 165 | CHECK(properties); |
| 166 | properties->clear(); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 167 | for (const auto &key_value_pair : store.string_properties()) { |
| 168 | (*properties)[key_value_pair.first].writer() |
| 169 | .append_string(key_value_pair.second.c_str()); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 170 | } |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 171 | for (const auto &key_value_pair : store.stringmap_properties()) { |
| 172 | DBus::MessageIter writer = (*properties)[key_value_pair.first].writer(); |
| 173 | writer << key_value_pair.second; |
| 174 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 175 | for (const auto &key_value_pair : store.strings_properties()) { |
| 176 | DBus::MessageIter writer = (*properties)[key_value_pair.first].writer(); |
| 177 | writer << key_value_pair.second; |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 178 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 179 | for (const auto &key_value_pair : store.bool_properties()) { |
| 180 | (*properties)[key_value_pair.first].writer() |
| 181 | .append_bool(key_value_pair.second); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 182 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 183 | for (const auto &key_value_pair : store.int_properties()) { |
| 184 | (*properties)[key_value_pair.first].writer() |
| 185 | .append_int32(key_value_pair.second); |
| 186 | } |
| 187 | for (const auto &key_value_pair : store.uint_properties()) { |
| 188 | (*properties)[key_value_pair.first].writer() |
| 189 | .append_uint32(key_value_pair.second); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | // static |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 194 | string DBusProperties::KeysToString(const DBusPropertiesMap &properties) { |
| 195 | string keys; |
| 196 | for (const auto &key_value_pair : properties) { |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 197 | keys.append(" "); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 198 | keys.append(key_value_pair.first); |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 199 | } |
| 200 | return keys; |
| 201 | } |
| 202 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 203 | } // namespace shill |