blob: 92fb35489c9ea73dbfc9acbd55affba194d1d2cb [file] [log] [blame]
mukesh agrawal4d0401c2012-01-06 16:05:31 -08001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
Chris Masoned7732e42011-05-20 11:08:56 -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/device_dbus_adaptor.h"
6
7#include <map>
8#include <string>
9
Chris Masonea82b7112011-05-25 15:16:29 -070010#include "shill/device.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070011#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070012#include "shill/logging.h"
Chris Masonea82b7112011-05-25 15:16:29 -070013
Chris Masoned7732e42011-05-20 11:08:56 -070014using std::map;
15using std::string;
Paul Stewartd4f26482014-04-25 19:12:03 -070016using std::vector;
Chris Masoned7732e42011-05-20 11:08:56 -070017
18namespace shill {
19
Chris Masoned7732e42011-05-20 11:08:56 -070020// static
Chris Masonea82b7112011-05-25 15:16:29 -070021const char DeviceDBusAdaptor::kPath[] = "/device/";
Chris Masoned7732e42011-05-20 11:08:56 -070022
Ben Chan3f4d4ee2014-09-09 07:41:33 -070023DeviceDBusAdaptor::DeviceDBusAdaptor(DBus::Connection *conn, Device *device)
Chris Masonea82b7112011-05-25 15:16:29 -070024 : DBusAdaptor(conn, kPath + device->UniqueName()),
Chris Masone4e851612011-07-01 10:46:53 -070025 device_(device),
26 connection_name_(conn->unique_name()) {
Chris Masoned7732e42011-05-20 11:08:56 -070027}
Chris Masoned0ceb8c2011-06-02 10:05:39 -070028
Chris Masoneec6b18b2011-06-08 14:09:10 -070029DeviceDBusAdaptor::~DeviceDBusAdaptor() {
Ben Chancc225ef2014-09-30 13:26:51 -070030 device_ = nullptr;
Chris Masoneec6b18b2011-06-08 14:09:10 -070031}
Ben Chan3f4d4ee2014-09-09 07:41:33 -070032
33const string &DeviceDBusAdaptor::GetRpcIdentifier() {
Chris Masone4e851612011-07-01 10:46:53 -070034 return path();
35}
36
Ben Chan3f4d4ee2014-09-09 07:41:33 -070037const string &DeviceDBusAdaptor::GetRpcConnectionIdentifier() {
Chris Masone4e851612011-07-01 10:46:53 -070038 return connection_name_;
39}
Chris Masoned7732e42011-05-20 11:08:56 -070040
Ben Chan3f4d4ee2014-09-09 07:41:33 -070041void DeviceDBusAdaptor::EmitBoolChanged(const string &name, bool value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070042 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
43 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070044 PropertyChanged(name, DBusAdaptor::BoolToVariant(value));
45}
46
Ben Chan3f4d4ee2014-09-09 07:41:33 -070047void DeviceDBusAdaptor::EmitUintChanged(const string &name,
Ben Chan7fab8972014-08-10 17:14:46 -070048 uint32_t value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070049 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
50 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070051 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070052}
53
Ben Chan7fab8972014-08-10 17:14:46 -070054void DeviceDBusAdaptor::EmitUint16Changed(const string &name, uint16_t value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070055 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
56 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080057 PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value));
58}
59
Ben Chan3f4d4ee2014-09-09 07:41:33 -070060void DeviceDBusAdaptor::EmitIntChanged(const string &name, int value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070061 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
62 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070063 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070064}
65
Ben Chan3f4d4ee2014-09-09 07:41:33 -070066void DeviceDBusAdaptor::EmitStringChanged(const string &name,
67 const string &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070068 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
69 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070070 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
71}
72
Ben Chan3f4d4ee2014-09-09 07:41:33 -070073void DeviceDBusAdaptor::EmitStringmapChanged(const string &name,
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080074 const Stringmap &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070075 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
76 << " " << name;
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080077 PropertyChanged(name, DBusAdaptor::StringmapToVariant(value));
78}
79
Ben Chan3f4d4ee2014-09-09 07:41:33 -070080void DeviceDBusAdaptor::EmitStringmapsChanged(const string &name,
Darin Petkov3cfbf212011-11-21 16:02:09 +010081 const Stringmaps &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070082 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
83 << " " << name;
Darin Petkov3cfbf212011-11-21 16:02:09 +010084 PropertyChanged(name, DBusAdaptor::StringmapsToVariant(value));
85}
86
Ben Chan3f4d4ee2014-09-09 07:41:33 -070087void DeviceDBusAdaptor::EmitStringsChanged(const string &name,
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080088 const Strings &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070089 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
90 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080091 PropertyChanged(name, DBusAdaptor::StringsToVariant(value));
92}
93
Ben Chan3f4d4ee2014-09-09 07:41:33 -070094void DeviceDBusAdaptor::EmitKeyValueStoreChanged(const string &name,
Darin Petkov63138a92012-02-06 14:09:15 +010095 const KeyValueStore &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070096 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
97 << " " << name;
Darin Petkov63138a92012-02-06 14:09:15 +010098 PropertyChanged(name, DBusAdaptor::KeyValueStoreToVariant(value));
99}
100
Paul Stewartd4f26482014-04-25 19:12:03 -0700101void DeviceDBusAdaptor::EmitRpcIdentifierArrayChanged(
102 const string &name,
103 const vector<string> &value) {
104 SLOG(DBus, 2) << __func__ << ": " << name;
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700105 vector<DBus::Path> paths;
Paul Stewart6db7b242014-05-02 15:34:21 -0700106 for (const auto &element : value) {
107 paths.push_back(element);
Paul Stewartd4f26482014-04-25 19:12:03 -0700108 }
109
110 PropertyChanged(name, DBusAdaptor::PathsToVariant(paths));
111}
112
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700113map<string, DBus::Variant> DeviceDBusAdaptor::GetProperties(
114 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700115 SLOG(DBus, 2) << __func__ << " " << device_->UniqueName();
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700116 map<string, DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -0700117 DBusAdaptor::GetProperties(device_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700118 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -0700119}
120
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800121void DeviceDBusAdaptor::SetProperty(const string &name,
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700122 const DBus::Variant &value,
123 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700124 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
125 << " " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800126 DBusAdaptor::SetProperty(device_->mutable_store(), name, value, &error);
Chris Masoned7732e42011-05-20 11:08:56 -0700127}
128
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700129void DeviceDBusAdaptor::ClearProperty(const string &name,
130 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700131 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
132 << " " << name;
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800133 DBusAdaptor::ClearProperty(device_->mutable_store(), name, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700134}
135
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700136void DeviceDBusAdaptor::Enable(DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700137 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500138 Error e(Error::kOperationInitiated);
139 DBus::Tag *tag = new DBus::Tag();
140 device_->SetEnabledPersistent(true, &e, GetMethodReplyCallback(tag));
141 ReturnResultOrDefer(tag, e, &error);
142}
143
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700144void DeviceDBusAdaptor::Disable(DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700145 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500146 Error e(Error::kOperationInitiated);
147 DBus::Tag *tag = new DBus::Tag();
148 device_->SetEnabledPersistent(false, &e, GetMethodReplyCallback(tag));
149 ReturnResultOrDefer(tag, e, &error);
150}
151
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700152void DeviceDBusAdaptor::ProposeScan(DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700153 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc0865312011-09-16 15:31:20 -0700154 Error e;
Wade Guthrie68d41092013-04-02 12:56:02 -0700155 // User scan requests, which are the likely source of DBus requests, probably
156 // aren't time-critical so we might as well perform a complete scan. It
157 // also provides a failsafe for progressive scan.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700158 device_->Scan(Device::kFullScan, &e, __func__);
Darin Petkovc0865312011-09-16 15:31:20 -0700159 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700160}
161
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700162DBus::Path DeviceDBusAdaptor::AddIPConfig(const string &/*method*/,
163 DBus::Error &error) { // NOLINT
mukesh agrawal06175d72012-04-23 16:46:01 -0700164 SLOG(DBus, 2) << __func__;
Paul Stewart624b9a22012-05-21 14:17:35 -0700165 Error e(Error::kNotSupported, "This function is deprecated in shill");
166 e.ToDBusError(&error);
Darin Petkovfd164b82012-02-10 14:11:52 +0100167 return "/";
Chris Masoned7732e42011-05-20 11:08:56 -0700168}
169
Darin Petkov9ae310f2011-08-30 15:41:13 -0700170void DeviceDBusAdaptor::Register(const string &network_id,
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700171 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700172 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
173 << " (" << network_id << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500174 Error e(Error::kOperationInitiated);
175 DBus::Tag *tag = new DBus::Tag();
176 device_->RegisterOnNetwork(network_id, &e, GetMethodReplyCallback(tag));
177 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700178}
179
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100180void DeviceDBusAdaptor::RequirePin(
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700181 const string &pin, const bool &require, DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700182 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500183 Error e(Error::kOperationInitiated);
184 DBus::Tag *tag = new DBus::Tag();
185 device_->RequirePIN(pin, require, &e, GetMethodReplyCallback(tag));
186 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700187}
188
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700189void DeviceDBusAdaptor::EnterPin(const string &pin,
190 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700191 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500192 Error e(Error::kOperationInitiated);
193 DBus::Tag *tag = new DBus::Tag();
194 device_->EnterPIN(pin, &e, GetMethodReplyCallback(tag));
195 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700196}
197
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700198void DeviceDBusAdaptor::UnblockPin(const string &unblock_code,
199 const string &pin,
200 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700201 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500202 Error e(Error::kOperationInitiated);
203 DBus::Tag *tag = new DBus::Tag();
204 device_->UnblockPIN(unblock_code, pin, &e, GetMethodReplyCallback(tag));
205 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700206}
207
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700208void DeviceDBusAdaptor::ChangePin(const string &old_pin,
209 const string &new_pin,
210 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700211 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc37a9c42012-09-06 15:28:22 +0200212 Error e(Error::kOperationInitiated);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 DBus::Tag *tag = new DBus::Tag();
214 device_->ChangePIN(old_pin, new_pin, &e, GetMethodReplyCallback(tag));
215 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700216}
217
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700218void DeviceDBusAdaptor::Reset(DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700219 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Ben Chanad663e12013-01-08 01:58:47 -0800220 Error e(Error::kOperationInitiated);
221 DBus::Tag *tag = new DBus::Tag();
222 device_->Reset(&e, GetMethodReplyCallback(tag));
223 ReturnResultOrDefer(tag, e, &error);
224}
225
Paul Stewartc6fbad92013-11-13 14:50:52 -0800226string DeviceDBusAdaptor::PerformTDLSOperation(const string &operation,
227 const string &peer,
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700228 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700229 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Paul Stewartc6fbad92013-11-13 14:50:52 -0800230 Error e;
231 string return_value = device_->PerformTDLSOperation(operation, peer, &e);
232 e.ToDBusError(&error);
233 return return_value;
234}
235
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700236void DeviceDBusAdaptor::ResetByteCounters(DBus::Error &error) { // NOLINT
Paul Stewart6ff27f52012-07-11 06:51:41 -0700237 device_->ResetByteCounters();
238}
239
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700240void DeviceDBusAdaptor::SetCarrier(const string &carrier,
241 DBus::Error &error) { // NOLINT
Peter Qiu047a21c2014-04-01 11:48:50 -0700242 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
243 << "(" << carrier << ")";
Darin Petkovc37a9c42012-09-06 15:28:22 +0200244 Error e(Error::kOperationInitiated);
245 DBus::Tag *tag = new DBus::Tag();
246 device_->SetCarrier(carrier, &e, GetMethodReplyCallback(tag));
247 ReturnResultOrDefer(tag, e, &error);
248}
249
Chris Masoned7732e42011-05-20 11:08:56 -0700250} // namespace shill