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 |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 102 | bool DBusProperties::GetStrings(const DBusPropertiesMap &properties, |
| 103 | const string &key, |
| 104 | vector<string> *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 105 | return GetValue<vector<string>>(properties, key, value); |
| 106 | } |
| 107 | |
| 108 | // static |
| 109 | bool DBusProperties::GetUint8(const DBusPropertiesMap &properties, |
| 110 | const string &key, |
| 111 | uint8 *value) { |
| 112 | return GetValue<uint8>(properties, key, value); |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // static |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 116 | bool DBusProperties::GetUint16(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 117 | const string &key, |
Darin Petkov | ceb6817 | 2011-07-29 14:47:48 -0700 | [diff] [blame] | 118 | uint16 *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 119 | return GetValue<uint16>(properties, key, value); |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 122 | // static |
| 123 | bool DBusProperties::GetUint32(const DBusPropertiesMap &properties, |
Jason Glasgow | 9c09e36 | 2012-04-18 15:16:29 -0400 | [diff] [blame] | 124 | const string &key, |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 125 | uint32 *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 126 | return GetValue<uint32>(properties, key, value); |
| 127 | } |
| 128 | |
| 129 | // static |
| 130 | bool DBusProperties::GetUint64(const DBusPropertiesMap &properties, |
| 131 | const string &key, |
| 132 | uint64 *value) { |
| 133 | return GetValue<uint64>(properties, key, value); |
Eric Shienbrood | 7fce52c | 2012-04-13 19:11:02 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 136 | // static |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 137 | bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties, |
| 138 | const string &key, |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 139 | RpcIdentifiers *value) { |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 140 | vector<DBus::Path> paths; |
| 141 | if (GetValue<vector<DBus::Path>>(properties, key, &paths)) { |
| 142 | ConvertPathsToRpcIdentifiers(paths, value); |
| 143 | return true; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 144 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 145 | return false; |
Darin Petkov | e4b2702 | 2012-05-16 13:28:50 +0200 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | // static |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 149 | void DBusProperties::ConvertPathsToRpcIdentifiers( |
| 150 | const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) { |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 151 | CHECK(rpc_identifiers); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 152 | rpc_identifiers->assign(dbus_paths.begin(), dbus_paths.end()); |
Darin Petkov | 9893d9c | 2012-05-17 15:27:31 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | // static |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 156 | void DBusProperties::ConvertKeyValueStoreToMap( |
| 157 | const KeyValueStore &store, DBusPropertiesMap *properties) { |
| 158 | CHECK(properties); |
| 159 | properties->clear(); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 160 | for (const auto &key_value_pair : store.string_properties()) { |
| 161 | (*properties)[key_value_pair.first].writer() |
| 162 | .append_string(key_value_pair.second.c_str()); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 163 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 164 | for (const auto &key_value_pair : store.strings_properties()) { |
| 165 | DBus::MessageIter writer = (*properties)[key_value_pair.first].writer(); |
| 166 | writer << key_value_pair.second; |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 167 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 168 | for (const auto &key_value_pair : store.bool_properties()) { |
| 169 | (*properties)[key_value_pair.first].writer() |
| 170 | .append_bool(key_value_pair.second); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 171 | } |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 172 | for (const auto &key_value_pair : store.int_properties()) { |
| 173 | (*properties)[key_value_pair.first].writer() |
| 174 | .append_int32(key_value_pair.second); |
| 175 | } |
| 176 | for (const auto &key_value_pair : store.uint_properties()) { |
| 177 | (*properties)[key_value_pair.first].writer() |
| 178 | .append_uint32(key_value_pair.second); |
Darin Petkov | 25665aa | 2012-05-21 14:08:12 +0200 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
| 182 | // static |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 183 | string DBusProperties::KeysToString(const DBusPropertiesMap &properties) { |
| 184 | string keys; |
| 185 | for (const auto &key_value_pair : properties) { |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 186 | keys.append(" "); |
Ben Chan | 0a3a3a5 | 2013-07-10 11:34:07 -0700 | [diff] [blame] | 187 | keys.append(key_value_pair.first); |
Gary Morain | 610977f | 2012-05-04 16:03:52 -0700 | [diff] [blame] | 188 | } |
| 189 | return keys; |
| 190 | } |
| 191 | |
Darin Petkov | e0a312e | 2011-07-20 13:45:28 -0700 | [diff] [blame] | 192 | } // namespace shill |