blob: 1f9f8221bb76f9e76ff8e34b39527435205a8fde [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
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070020namespace Logging {
21static auto kModuleLogScope = ScopeLogger::kDBus;
Paul Stewarta794cd62015-06-16 13:13:10 -070022static string ObjectID(DeviceDBusAdaptor* d) { return d->GetRpcIdentifier(); }
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070023}
24
Chris Masoned7732e42011-05-20 11:08:56 -070025// static
Chris Masonea82b7112011-05-25 15:16:29 -070026const char DeviceDBusAdaptor::kPath[] = "/device/";
Chris Masoned7732e42011-05-20 11:08:56 -070027
Paul Stewarta794cd62015-06-16 13:13:10 -070028DeviceDBusAdaptor::DeviceDBusAdaptor(DBus::Connection* conn, Device* device)
Paul Stewartf3b38402015-01-16 12:59:03 -080029 : DBusAdaptor(conn, kPath + SanitizePathElement(device->UniqueName())),
Chris Masone4e851612011-07-01 10:46:53 -070030 device_(device),
31 connection_name_(conn->unique_name()) {
Chris Masoned7732e42011-05-20 11:08:56 -070032}
Chris Masoned0ceb8c2011-06-02 10:05:39 -070033
Chris Masoneec6b18b2011-06-08 14:09:10 -070034DeviceDBusAdaptor::~DeviceDBusAdaptor() {
Ben Chancc225ef2014-09-30 13:26:51 -070035 device_ = nullptr;
Chris Masoneec6b18b2011-06-08 14:09:10 -070036}
Ben Chan3f4d4ee2014-09-09 07:41:33 -070037
Paul Stewarta794cd62015-06-16 13:13:10 -070038const string& DeviceDBusAdaptor::GetRpcIdentifier() {
Chris Masone4e851612011-07-01 10:46:53 -070039 return path();
40}
41
Paul Stewarta794cd62015-06-16 13:13:10 -070042const string& DeviceDBusAdaptor::GetRpcConnectionIdentifier() {
Chris Masone4e851612011-07-01 10:46:53 -070043 return connection_name_;
44}
Chris Masoned7732e42011-05-20 11:08:56 -070045
Paul Stewarta794cd62015-06-16 13:13:10 -070046void DeviceDBusAdaptor::EmitBoolChanged(const string& name, bool value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070047 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070048 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070049 PropertyChanged(name, DBusAdaptor::BoolToVariant(value));
50}
51
Paul Stewarta794cd62015-06-16 13:13:10 -070052void DeviceDBusAdaptor::EmitUintChanged(const string& name,
Ben Chan7fab8972014-08-10 17:14:46 -070053 uint32_t value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070054 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070055 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070056 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070057}
58
Paul Stewarta794cd62015-06-16 13:13:10 -070059void DeviceDBusAdaptor::EmitUint16Changed(const string& name, uint16_t value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070060 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070061 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080062 PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value));
63}
64
Paul Stewarta794cd62015-06-16 13:13:10 -070065void DeviceDBusAdaptor::EmitIntChanged(const string& name, int value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070066 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070067 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070068 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070069}
70
Paul Stewarta794cd62015-06-16 13:13:10 -070071void DeviceDBusAdaptor::EmitStringChanged(const string& name,
72 const string& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070073 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070074 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070075 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
76}
77
Paul Stewarta794cd62015-06-16 13:13:10 -070078void DeviceDBusAdaptor::EmitStringmapChanged(const string& name,
79 const Stringmap& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070080 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070081 << " " << name;
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080082 PropertyChanged(name, DBusAdaptor::StringmapToVariant(value));
83}
84
Paul Stewarta794cd62015-06-16 13:13:10 -070085void DeviceDBusAdaptor::EmitStringmapsChanged(const string& name,
86 const Stringmaps& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070087 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070088 << " " << name;
Darin Petkov3cfbf212011-11-21 16:02:09 +010089 PropertyChanged(name, DBusAdaptor::StringmapsToVariant(value));
90}
91
Paul Stewarta794cd62015-06-16 13:13:10 -070092void DeviceDBusAdaptor::EmitStringsChanged(const string& name,
93 const Strings& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -070094 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -070095 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080096 PropertyChanged(name, DBusAdaptor::StringsToVariant(value));
97}
98
Paul Stewarta794cd62015-06-16 13:13:10 -070099void DeviceDBusAdaptor::EmitKeyValueStoreChanged(const string& name,
100 const KeyValueStore& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700101 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -0700102 << " " << name;
Darin Petkov63138a92012-02-06 14:09:15 +0100103 PropertyChanged(name, DBusAdaptor::KeyValueStoreToVariant(value));
104}
105
Paul Stewarta794cd62015-06-16 13:13:10 -0700106void DeviceDBusAdaptor::EmitRpcIdentifierChanged(const std::string& name,
107 const std::string& value) {
Christopher Wiley674598d2014-12-12 10:21:39 -0800108 SLOG(this, 2) << __func__ << ": " << name;
109 PropertyChanged(name, DBusAdaptor::PathToVariant(value));
110}
111
Paul Stewartd4f26482014-04-25 19:12:03 -0700112void DeviceDBusAdaptor::EmitRpcIdentifierArrayChanged(
Paul Stewarta794cd62015-06-16 13:13:10 -0700113 const string& name,
114 const vector<string>& value) {
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700115 SLOG(this, 2) << __func__ << ": " << name;
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700116 vector<DBus::Path> paths;
Paul Stewarta794cd62015-06-16 13:13:10 -0700117 for (const auto& element : value) {
Paul Stewart6db7b242014-05-02 15:34:21 -0700118 paths.push_back(element);
Paul Stewartd4f26482014-04-25 19:12:03 -0700119 }
120
121 PropertyChanged(name, DBusAdaptor::PathsToVariant(paths));
122}
123
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700124map<string, DBus::Variant> DeviceDBusAdaptor::GetProperties(
Paul Stewarta794cd62015-06-16 13:13:10 -0700125 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700126 SLOG(this, 2) << __func__ << " " << device_->UniqueName();
Ben Chan3f4d4ee2014-09-09 07:41:33 -0700127 map<string, DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -0700128 DBusAdaptor::GetProperties(device_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700129 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -0700130}
131
Paul Stewarta794cd62015-06-16 13:13:10 -0700132void DeviceDBusAdaptor::SetProperty(const string& name,
133 const DBus::Variant& value,
134 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700135 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -0700136 << " " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800137 DBusAdaptor::SetProperty(device_->mutable_store(), name, value, &error);
Chris Masoned7732e42011-05-20 11:08:56 -0700138}
139
Paul Stewarta794cd62015-06-16 13:13:10 -0700140void DeviceDBusAdaptor::ClearProperty(const string& name,
141 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700142 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -0700143 << " " << name;
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800144 DBusAdaptor::ClearProperty(device_->mutable_store(), name, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700145}
146
Paul Stewarta794cd62015-06-16 13:13:10 -0700147void DeviceDBusAdaptor::Enable(DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700148 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500149 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700150 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500151 device_->SetEnabledPersistent(true, &e, GetMethodReplyCallback(tag));
152 ReturnResultOrDefer(tag, e, &error);
153}
154
Paul Stewarta794cd62015-06-16 13:13:10 -0700155void DeviceDBusAdaptor::Disable(DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700156 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500157 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700158 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500159 device_->SetEnabledPersistent(false, &e, GetMethodReplyCallback(tag));
160 ReturnResultOrDefer(tag, e, &error);
161}
162
Paul Stewarta794cd62015-06-16 13:13:10 -0700163void DeviceDBusAdaptor::ProposeScan(DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700164 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc0865312011-09-16 15:31:20 -0700165 Error e;
Wade Guthrie68d41092013-04-02 12:56:02 -0700166 // User scan requests, which are the likely source of DBus requests, probably
167 // aren't time-critical so we might as well perform a complete scan. It
168 // also provides a failsafe for progressive scan.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700169 device_->Scan(Device::kFullScan, &e, __func__);
Darin Petkovc0865312011-09-16 15:31:20 -0700170 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700171}
172
Paul Stewarta794cd62015-06-16 13:13:10 -0700173DBus::Path DeviceDBusAdaptor::AddIPConfig(const string& /*method*/,
174 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700175 SLOG(this, 2) << __func__;
Paul Stewart624b9a22012-05-21 14:17:35 -0700176 Error e(Error::kNotSupported, "This function is deprecated in shill");
177 e.ToDBusError(&error);
Darin Petkovfd164b82012-02-10 14:11:52 +0100178 return "/";
Chris Masoned7732e42011-05-20 11:08:56 -0700179}
180
Paul Stewarta794cd62015-06-16 13:13:10 -0700181void DeviceDBusAdaptor::Register(const string& network_id,
182 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700183 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -0700184 << " (" << network_id << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500185 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700186 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500187 device_->RegisterOnNetwork(network_id, &e, GetMethodReplyCallback(tag));
188 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700189}
190
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100191void DeviceDBusAdaptor::RequirePin(
Paul Stewarta794cd62015-06-16 13:13:10 -0700192 const string& pin, const bool& require, DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700193 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500194 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700195 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500196 device_->RequirePIN(pin, require, &e, GetMethodReplyCallback(tag));
197 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700198}
199
Paul Stewarta794cd62015-06-16 13:13:10 -0700200void DeviceDBusAdaptor::EnterPin(const string& pin,
201 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700202 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500203 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700204 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500205 device_->EnterPIN(pin, &e, GetMethodReplyCallback(tag));
206 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700207}
208
Paul Stewarta794cd62015-06-16 13:13:10 -0700209void DeviceDBusAdaptor::UnblockPin(const string& unblock_code,
210 const string& pin,
211 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700212 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500213 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700214 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500215 device_->UnblockPIN(unblock_code, pin, &e, GetMethodReplyCallback(tag));
216 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700217}
218
Paul Stewarta794cd62015-06-16 13:13:10 -0700219void DeviceDBusAdaptor::ChangePin(const string& old_pin,
220 const string& new_pin,
221 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700222 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc37a9c42012-09-06 15:28:22 +0200223 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700224 DBus::Tag* tag = new DBus::Tag();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500225 device_->ChangePIN(old_pin, new_pin, &e, GetMethodReplyCallback(tag));
226 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700227}
228
Paul Stewarta794cd62015-06-16 13:13:10 -0700229void DeviceDBusAdaptor::Reset(DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700230 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Ben Chanad663e12013-01-08 01:58:47 -0800231 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700232 DBus::Tag* tag = new DBus::Tag();
Ben Chanad663e12013-01-08 01:58:47 -0800233 device_->Reset(&e, GetMethodReplyCallback(tag));
234 ReturnResultOrDefer(tag, e, &error);
235}
236
Paul Stewarta794cd62015-06-16 13:13:10 -0700237string DeviceDBusAdaptor::PerformTDLSOperation(const string& operation,
238 const string& peer,
239 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700240 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
Paul Stewartc6fbad92013-11-13 14:50:52 -0800241 Error e;
242 string return_value = device_->PerformTDLSOperation(operation, peer, &e);
243 e.ToDBusError(&error);
244 return return_value;
245}
246
Paul Stewarta794cd62015-06-16 13:13:10 -0700247void DeviceDBusAdaptor::ResetByteCounters(DBus::Error& error) { // NOLINT
Paul Stewart6ff27f52012-07-11 06:51:41 -0700248 device_->ResetByteCounters();
249}
250
Paul Stewarta794cd62015-06-16 13:13:10 -0700251void DeviceDBusAdaptor::SetCarrier(const string& carrier,
252 DBus::Error& error) { // NOLINT
Rebecca Silbersteinc9c31d82014-10-21 15:01:00 -0700253 SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName()
Peter Qiu047a21c2014-04-01 11:48:50 -0700254 << "(" << carrier << ")";
Darin Petkovc37a9c42012-09-06 15:28:22 +0200255 Error e(Error::kOperationInitiated);
Paul Stewarta794cd62015-06-16 13:13:10 -0700256 DBus::Tag* tag = new DBus::Tag();
Darin Petkovc37a9c42012-09-06 15:28:22 +0200257 device_->SetCarrier(carrier, &e, GetMethodReplyCallback(tag));
258 ReturnResultOrDefer(tag, e, &error);
259}
260
Samuel Tan96bdaec2014-11-05 18:27:38 -0800261void DeviceDBusAdaptor::AddWakeOnPacketConnection(
Paul Stewarta794cd62015-06-16 13:13:10 -0700262 const string& ip_endpoint,
263 DBus::Error& error) { // NOLINT
Samuel Tan96bdaec2014-11-05 18:27:38 -0800264 SLOG(this, 2) << __func__;
265 Error e;
266 device_->AddWakeOnPacketConnection(ip_endpoint, &e);
267 e.ToDBusError(&error);
268}
269
270void DeviceDBusAdaptor::RemoveWakeOnPacketConnection(
Paul Stewarta794cd62015-06-16 13:13:10 -0700271 const string& ip_endpoint, DBus::Error& error) { // NOLINT
Samuel Tan96bdaec2014-11-05 18:27:38 -0800272 SLOG(this, 2) << __func__;
273 Error e;
274 device_->RemoveWakeOnPacketConnection(ip_endpoint, &e);
275 e.ToDBusError(&error);
276}
277
278void DeviceDBusAdaptor::RemoveAllWakeOnPacketConnections(
Paul Stewarta794cd62015-06-16 13:13:10 -0700279 DBus::Error& error) { // NOLINT
Samuel Tan96bdaec2014-11-05 18:27:38 -0800280 SLOG(this, 2) << __func__;
281 Error e;
282 device_->RemoveAllWakeOnPacketConnections(&e);
283 e.ToDBusError(&error);
284}
285
Chris Masoned7732e42011-05-20 11:08:56 -0700286} // namespace shill