blob: 482820baf404d3ad8ab5b150287a06470dc5f7cd [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,
69 int16 *value) {
70 return GetValue<int16>(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,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040076 int32 *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -070077 return GetValue<int32>(properties, key, value);
78}
79
80// static
81bool DBusProperties::GetInt64(const DBusPropertiesMap &properties,
82 const string &key,
83 int64 *value) {
84 return GetValue<int64>(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
Jason Glasgow9c09e362012-04-18 15:16:29 -0400102bool DBusProperties::GetStrings(const DBusPropertiesMap &properties,
103 const string &key,
104 vector<string> *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700105 return GetValue<vector<string>>(properties, key, value);
106}
107
108// static
109bool DBusProperties::GetUint8(const DBusPropertiesMap &properties,
110 const string &key,
111 uint8 *value) {
112 return GetValue<uint8>(properties, key, value);
Jason Glasgow9c09e362012-04-18 15:16:29 -0400113}
114
115// static
Darin Petkovceb68172011-07-29 14:47:48 -0700116bool DBusProperties::GetUint16(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -0400117 const string &key,
Darin Petkovceb68172011-07-29 14:47:48 -0700118 uint16 *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700119 return GetValue<uint16>(properties, key, value);
Darin Petkove0a312e2011-07-20 13:45:28 -0700120}
121
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400122// static
123bool DBusProperties::GetUint32(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -0400124 const string &key,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400125 uint32 *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700126 return GetValue<uint32>(properties, key, value);
127}
128
129// static
130bool DBusProperties::GetUint64(const DBusPropertiesMap &properties,
131 const string &key,
132 uint64 *value) {
133 return GetValue<uint64>(properties, key, value);
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400134}
135
Gary Morain610977f2012-05-04 16:03:52 -0700136// static
Darin Petkove4b27022012-05-16 13:28:50 +0200137bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties,
138 const string &key,
Darin Petkov9893d9c2012-05-17 15:27:31 -0700139 RpcIdentifiers *value) {
Ben Chan0a3a3a52013-07-10 11:34:07 -0700140 vector<DBus::Path> paths;
141 if (GetValue<vector<DBus::Path>>(properties, key, &paths)) {
142 ConvertPathsToRpcIdentifiers(paths, value);
143 return true;
Darin Petkove4b27022012-05-16 13:28:50 +0200144 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700145 return false;
Darin Petkove4b27022012-05-16 13:28:50 +0200146}
147
148// static
Darin Petkov9893d9c2012-05-17 15:27:31 -0700149void DBusProperties::ConvertPathsToRpcIdentifiers(
150 const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200151 CHECK(rpc_identifiers);
Ben Chan0a3a3a52013-07-10 11:34:07 -0700152 rpc_identifiers->assign(dbus_paths.begin(), dbus_paths.end());
Darin Petkov9893d9c2012-05-17 15:27:31 -0700153}
154
155// static
Darin Petkov25665aa2012-05-21 14:08:12 +0200156void DBusProperties::ConvertKeyValueStoreToMap(
157 const KeyValueStore &store, DBusPropertiesMap *properties) {
158 CHECK(properties);
159 properties->clear();
Ben Chan0a3a3a52013-07-10 11:34:07 -0700160 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 Petkov25665aa2012-05-21 14:08:12 +0200163 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700164 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 Petkov25665aa2012-05-21 14:08:12 +0200167 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700168 for (const auto &key_value_pair : store.bool_properties()) {
169 (*properties)[key_value_pair.first].writer()
170 .append_bool(key_value_pair.second);
Darin Petkov25665aa2012-05-21 14:08:12 +0200171 }
Ben Chan0a3a3a52013-07-10 11:34:07 -0700172 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 Petkov25665aa2012-05-21 14:08:12 +0200179 }
180}
181
182// static
Ben Chan0a3a3a52013-07-10 11:34:07 -0700183string DBusProperties::KeysToString(const DBusPropertiesMap &properties) {
184 string keys;
185 for (const auto &key_value_pair : properties) {
Gary Morain610977f2012-05-04 16:03:52 -0700186 keys.append(" ");
Ben Chan0a3a3a52013-07-10 11:34:07 -0700187 keys.append(key_value_pair.first);
Gary Morain610977f2012-05-04 16:03:52 -0700188 }
189 return keys;
190}
191
Darin Petkove0a312e2011-07-20 13:45:28 -0700192} // namespace shill