blob: 68615bfb96378e32dafb721edfa72fc4f63e4b2c [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
Darin Petkov25665aa2012-05-21 14:08:12 +02007#include <map>
Jason Glasgow9c09e362012-04-18 15:16:29 -04008#include <string>
9#include <vector>
10
Ben Chanfad4a0b2012-04-18 15:49:59 -070011#include "shill/scope_logger.h"
Darin Petkovceb68172011-07-29 14:47:48 -070012
Darin Petkov25665aa2012-05-21 14:08:12 +020013using std::map;
Jason Glasgow9c09e362012-04-18 15:16:29 -040014using std::string;
15using std::vector;
16
Darin Petkove0a312e2011-07-20 13:45:28 -070017namespace shill {
18
19// static
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040020bool DBusProperties::GetBool(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040021 const string &key,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040022 bool *value) {
Darin Petkove0a312e2011-07-20 13:45:28 -070023 DBusPropertiesMap::const_iterator it = properties.find(key);
24 if (it == properties.end()) {
25 return false;
26 }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040027 *value = it->second.reader().get_bool();
28 SLOG(DBus, 2) << key << " = " << *value;
29 return true;
30}
31
32// static
33bool DBusProperties::GetInt32(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040034 const string &key,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040035 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 Chanfad4a0b2012-04-18 15:49:59 -070041 SLOG(DBus, 2) << key << " = " << *value;
Darin Petkove0a312e2011-07-20 13:45:28 -070042 return true;
43}
44
45// static
Nathan Williamse9840802012-04-18 18:47:40 -040046bool DBusProperties::GetObjectPath(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040047 const string &key,
48 string *value) {
Nathan Williamse9840802012-04-18 18:47:40 -040049 DBusPropertiesMap::const_iterator it = properties.find(key);
50 if (it == properties.end()) {
51 return false;
52 }
53 *value = it->second.reader().get_path();
Ben Chanfad4a0b2012-04-18 15:49:59 -070054 SLOG(DBus, 2) << key << " = " << *value;
Nathan Williamse9840802012-04-18 18:47:40 -040055 return true;
56}
57
58// static
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040059bool DBusProperties::GetString(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040060 const string &key,
61 string *value) {
Darin Petkove0a312e2011-07-20 13:45:28 -070062 DBusPropertiesMap::const_iterator it = properties.find(key);
63 if (it == properties.end()) {
64 return false;
65 }
Eric Shienbrood7fce52c2012-04-13 19:11:02 -040066 *value = it->second.reader().get_string();
Ben Chanfad4a0b2012-04-18 15:49:59 -070067 SLOG(DBus, 2) << key << " = " << *value;
Darin Petkovceb68172011-07-29 14:47:48 -070068 return true;
69}
70
71// static
Jason Glasgow9c09e362012-04-18 15:16:29 -040072bool 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 Petkovceb68172011-07-29 14:47:48 -070090bool DBusProperties::GetUint16(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -040091 const string &key,
Darin Petkovceb68172011-07-29 14:47:48 -070092 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 Chanfad4a0b2012-04-18 15:49:59 -070098 SLOG(DBus, 2) << key << " = " << *value;
Darin Petkove0a312e2011-07-20 13:45:28 -070099 return true;
100}
101
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400102// static
103bool DBusProperties::GetUint32(const DBusPropertiesMap &properties,
Jason Glasgow9c09e362012-04-18 15:16:29 -0400104 const string &key,
Eric Shienbrood7fce52c2012-04-13 19:11:02 -0400105 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 Morain610977f2012-05-04 16:03:52 -0700115// static
Darin Petkove4b27022012-05-16 13:28:50 +0200116bool DBusProperties::GetRpcIdentifiers(const DBusPropertiesMap &properties,
117 const string &key,
Darin Petkov9893d9c2012-05-17 15:27:31 -0700118 RpcIdentifiers *value) {
Darin Petkove4b27022012-05-16 13:28:50 +0200119 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 Petkov9893d9c2012-05-17 15:27:31 -0700124 ConvertPathsToRpcIdentifiers(paths, value);
Darin Petkove4b27022012-05-16 13:28:50 +0200125 SLOG(DBus, 2) << key << " = (RpcIdentfier)[" << value->size() << "]";
126 return true;
127}
128
129// static
Darin Petkov9893d9c2012-05-17 15:27:31 -0700130void DBusProperties::ConvertPathsToRpcIdentifiers(
131 const vector<DBus::Path> &dbus_paths, RpcIdentifiers *rpc_identifiers) {
Darin Petkov25665aa2012-05-21 14:08:12 +0200132 CHECK(rpc_identifiers);
Darin Petkov9893d9c2012-05-17 15:27:31 -0700133 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 Petkov25665aa2012-05-21 14:08:12 +0200141void 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 Morain610977f2012-05-04 16:03:52 -0700165std::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 Petkove0a312e2011-07-20 13:45:28 -0700176} // namespace shill