blob: f7406d7b0c3499e151b1f2dd0e1196f5c6dc653e [file] [log] [blame]
Ben Chanfad4a0b2012-04-18 15:49:59 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Darin Petkove0a312e2011-07-20 13:45:28 -07002// 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 Chan0a3a3a52013-07-10 11:34:07 -07007#include <dbus/dbus.h>
Jason Glasgow9c09e362012-04-18 15:16:29 -04008
Christopher Wileyb691efd2012-08-09 13:51:51 -07009#include "shill/logging.h"
Darin Petkovceb68172011-07-29 14:47:48 -070010
Darin Petkov25665aa2012-05-21 14:08:12 +020011using std::map;
Jason Glasgow9c09e362012-04-18 15:16:29 -040012using std::string;
13using std::vector;
14
Darin Petkove0a312e2011-07-20 13:45:28 -070015namespace shill {
16
Ben Chan0a3a3a52013-07-10 11:34:07 -070017namespace {
18
19template <typename ValueType>
20bool 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 Petkove0a312e2011-07-20 13:45:28 -070045// static
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040046bool DBusProperties::GetBool(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040047 const string &key,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040048 bool *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -070049 return GetValue<bool>(properties, key, value);
50}
51
52// static
53bool DBusProperties::GetDBusPropertiesMap(const DBusPropertiesMap &properties,
54 const string &key,
55 DBusPropertiesMap *value) {
56 return GetValue<DBusPropertiesMap>(properties, key, value);
57}
58
59// static
60bool DBusProperties::GetDouble(const DBusPropertiesMap &properties,
61 const string &key,
62 double *value) {
63 return GetValue<double>(properties, key, value);
64}
65
66// static
67bool DBusProperties::GetInt16(const DBusPropertiesMap &properties,
68 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -070069 int16_t *value) {
70 return GetValue<int16_t>(properties, key, value);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040071}
72
73// static
74bool DBusProperties::GetInt32(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040075 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -070076 int32_t *value) {
77 return GetValue<int32_t>(properties, key, value);
Ben Chan0a3a3a52013-07-10 11:34:07 -070078}
79
80// static
81bool DBusProperties::GetInt64(const DBusPropertiesMap &properties,
82 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -070083 int64_t *value) {
84 return GetValue<int64_t>(properties, key, value);
Darin Petkove0a312e2011-07-20 13:45:28 -070085}
86
87// static
Nathan Williamse9840802012-04-18 18:47:40 -040088bool DBusProperties::GetObjectPath(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040089 const string &key,
Ben Chan0a3a3a52013-07-10 11:34:07 -070090 DBus::Path *value) {
91 return GetValue<DBus::Path>(properties, key, value);
Nathan Williamse9840802012-04-18 18:47:40 -040092}
93
94// static
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040095bool DBusProperties::GetString(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040096 const string &key,
97 string *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -070098 return GetValue<string>(properties, key, value);
Darin Petkovceb68172011-07-29 14:47:48 -070099}
100
101// static
Arman Uguray631c7e42013-07-30 16:41:12 -0700102bool 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 Glasgow9c09e362012-04-18 15:16:29 -0400109bool DBusProperties::GetStrings(const DBusPropertiesMap &properties,
110 const string &key,
111 vector<string> *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700112 return GetValue<vector<string>>(properties, key, value);
113}
114
115// static
116bool DBusProperties::GetUint8(const DBusPropertiesMap &properties,
117 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -0700118 uint8_t *value) {
119 return GetValue<uint8_t>(properties, key, value);
Jason Glasgow9c09e362012-04-18 15:16:29 -0400120}
121
122// static
Darin Petkovceb68172011-07-29 14:47:48 -0700123bool DBusProperties::GetUint16(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -0400124 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -0700125 uint16_t *value) {
126 return GetValue<uint16_t>(properties, key, value);
Darin Petkove0a312e2011-07-20 13:45:28 -0700127}
128
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400129// static
130bool DBusProperties::GetUint32(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -0400131 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -0700132 uint32_t *value) {
133 return GetValue<uint32_t>(properties, key, value);
Ben Chan0a3a3a52013-07-10 11:34:07 -0700134}
135
136// static
137bool DBusProperties::GetUint64(const DBusPropertiesMap &properties,
138 const string &key,
Ben Chan7fab8972014-08-10 17:14:46 -0700139 uint64_t *value) {
140 return GetValue<uint64_t>(properties, key, value);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400141}
142
Gary Morain610977f2012-05-04 16:03:52 -0700143// static
Darin Petkove4b27022012-05-16 13:28:50 +0200144bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties,
145 const string &key,
Darin Petkov9893d9c2012-05-17 15:27:31 -0700146 RpcIdentifiers *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700147 vector<DBus::Path> paths;
148 if (GetValue<vector<DBus::Path>>(properties, key, &paths)) {
149 ConvertPathsToRpcIdentifiers(paths, value);
150 return true;
Darin Petkove4b27022012-05-16 13:28:50 +0200151 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700152 return false;
Darin Petkove4b27022012-05-16 13:28:50 +0200153}
154
155// static
Darin Petkov9893d9c2012-05-17 15:27:31 -0700156void DBusProperties::ConvertPathsToRpcIdentifiers(
157 const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200158 CHECK(rpc_identifiers);
Ben Chan0a3a3a52013-07-10 11:34:07 -0700159 rpc_identifiers->assign(dbus_paths.begin(), dbus_paths.end());
Darin Petkov9893d9c2012-05-17 15:27:31 -0700160}
161
162// static
Darin Petkov25665aa2012-05-21 14:08:12 +0200163void DBusProperties::ConvertKeyValueStoreToMap(
164 const KeyValueStore &store, DBusPropertiesMap *properties) {
165 CHECK(properties);
166 properties->clear();
Ben Chan0a3a3a52013-07-10 11:34:07 -0700167 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 Petkov25665aa2012-05-21 14:08:12 +0200170 }
Arman Uguray631c7e42013-07-30 16:41:12 -0700171 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 Chan0a3a3a52013-07-10 11:34:07 -0700175 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 Petkov25665aa2012-05-21 14:08:12 +0200178 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700179 for (const auto &key_value_pair : store.bool_properties()) {
180 (*properties)[key_value_pair.first].writer()
181 .append_bool(key_value_pair.second);
Darin Petkov25665aa2012-05-21 14:08:12 +0200182 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700183 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 Petkov25665aa2012-05-21 14:08:12 +0200190 }
191}
192
193// static
Ben Chan0a3a3a52013-07-10 11:34:07 -0700194string DBusProperties::KeysToString(const DBusPropertiesMap &properties) {
195 string keys;
196 for (const auto &key_value_pair : properties) {
Gary Morain610977f2012-05-04 16:03:52 -0700197 keys.append(" ");
Ben Chan0a3a3a52013-07-10 11:34:07 -0700198 keys.append(key_value_pair.first);
Gary Morain610977f2012-05-04 16:03:52 -0700199 }
200 return keys;
201}
202
Darin Petkove0a312e2011-07-20 13:45:28 -0700203} // namespace shill