blob: deffa76eb57fdd3df511144ae69d5d59d749243b [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
Eric Shienbrood9a245532012-03-07 14:20:39 -050010#include <base/bind.h>
11
Chris Masonea82b7112011-05-25 15:16:29 -070012#include "shill/device.h"
Chris Masone8fe2c7e2011-06-09 15:51:19 -070013#include "shill/error.h"
Christopher Wileyb691efd2012-08-09 13:51:51 -070014#include "shill/logging.h"
Chris Masonea82b7112011-05-25 15:16:29 -070015
Eric Shienbrood9a245532012-03-07 14:20:39 -050016using base::Bind;
Chris Masoned7732e42011-05-20 11:08:56 -070017using std::map;
18using std::string;
Paul Stewartd4f26482014-04-25 19:12:03 -070019using std::vector;
Chris Masoned7732e42011-05-20 11:08:56 -070020
21namespace shill {
22
Chris Masoned7732e42011-05-20 11:08:56 -070023// static
Chris Masonea82b7112011-05-25 15:16:29 -070024const char DeviceDBusAdaptor::kPath[] = "/device/";
Chris Masoned7732e42011-05-20 11:08:56 -070025
Chris Masoneec6b18b2011-06-08 14:09:10 -070026DeviceDBusAdaptor::DeviceDBusAdaptor(DBus::Connection* conn, Device *device)
Chris Masonea82b7112011-05-25 15:16:29 -070027 : DBusAdaptor(conn, kPath + device->UniqueName()),
Chris Masone4e851612011-07-01 10:46:53 -070028 device_(device),
29 connection_name_(conn->unique_name()) {
Chris Masoned7732e42011-05-20 11:08:56 -070030}
Chris Masoned0ceb8c2011-06-02 10:05:39 -070031
Chris Masoneec6b18b2011-06-08 14:09:10 -070032DeviceDBusAdaptor::~DeviceDBusAdaptor() {
33 device_ = NULL;
34}
Chris Masone4e851612011-07-01 10:46:53 -070035const std::string &DeviceDBusAdaptor::GetRpcIdentifier() {
36 return path();
37}
38
39const std::string &DeviceDBusAdaptor::GetRpcConnectionIdentifier() {
40 return connection_name_;
41}
Chris Masoned7732e42011-05-20 11:08:56 -070042
Chris Masoned0ceb8c2011-06-02 10:05:39 -070043void DeviceDBusAdaptor::EmitBoolChanged(const std::string& name, bool value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070044 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
45 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070046 PropertyChanged(name, DBusAdaptor::BoolToVariant(value));
47}
48
Ben Chan7fab8972014-08-10 17:14:46 -070049void DeviceDBusAdaptor::EmitUintChanged(const std::string &name,
50 uint32_t value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070051 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
52 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070053 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070054}
55
Ben Chan7fab8972014-08-10 17:14:46 -070056void DeviceDBusAdaptor::EmitUint16Changed(const string &name, uint16_t value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070057 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
58 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080059 PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value));
60}
61
Chris Masoned0ceb8c2011-06-02 10:05:39 -070062void DeviceDBusAdaptor::EmitIntChanged(const std::string& name, int value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070063 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
64 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070065 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070066}
67
68void DeviceDBusAdaptor::EmitStringChanged(const std::string& name,
69 const std::string& value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070070 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
71 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070072 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
73}
74
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080075void DeviceDBusAdaptor::EmitStringmapChanged(const std::string &name,
76 const Stringmap &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070077 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
78 << " " << name;
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080079 PropertyChanged(name, DBusAdaptor::StringmapToVariant(value));
80}
81
Darin Petkov3cfbf212011-11-21 16:02:09 +010082void DeviceDBusAdaptor::EmitStringmapsChanged(const std::string &name,
83 const Stringmaps &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070084 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
85 << " " << name;
Darin Petkov3cfbf212011-11-21 16:02:09 +010086 PropertyChanged(name, DBusAdaptor::StringmapsToVariant(value));
87}
88
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080089void DeviceDBusAdaptor::EmitStringsChanged(const std::string &name,
90 const Strings &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070091 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
92 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080093 PropertyChanged(name, DBusAdaptor::StringsToVariant(value));
94}
95
Darin Petkov63138a92012-02-06 14:09:15 +010096void DeviceDBusAdaptor::EmitKeyValueStoreChanged(const std::string &name,
97 const KeyValueStore &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070098 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
99 << " " << name;
Darin Petkov63138a92012-02-06 14:09:15 +0100100 PropertyChanged(name, DBusAdaptor::KeyValueStoreToVariant(value));
101}
102
Paul Stewartd4f26482014-04-25 19:12:03 -0700103void DeviceDBusAdaptor::EmitRpcIdentifierArrayChanged(
104 const string &name,
105 const vector<string> &value) {
106 SLOG(DBus, 2) << __func__ << ": " << name;
107 vector< ::DBus::Path> paths;
Paul Stewart6db7b242014-05-02 15:34:21 -0700108 for (const auto &element : value) {
109 paths.push_back(element);
Paul Stewartd4f26482014-04-25 19:12:03 -0700110 }
111
112 PropertyChanged(name, DBusAdaptor::PathsToVariant(paths));
113}
114
Chris Masoned7732e42011-05-20 11:08:56 -0700115map<string, ::DBus::Variant> DeviceDBusAdaptor::GetProperties(
116 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700117 SLOG(DBus, 2) << __func__ << " " << device_->UniqueName();
Chris Masonea8a2c252011-06-27 22:16:30 -0700118 map<string, ::DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -0700119 DBusAdaptor::GetProperties(device_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700120 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -0700121}
122
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800123void DeviceDBusAdaptor::SetProperty(const string &name,
124 const ::DBus::Variant &value,
Chris Masoned7732e42011-05-20 11:08:56 -0700125 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700126 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
127 << " " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800128 DBusAdaptor::SetProperty(device_->mutable_store(), name, value, &error);
Chris Masoned7732e42011-05-20 11:08:56 -0700129}
130
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800131void DeviceDBusAdaptor::ClearProperty(const std::string &name,
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800132 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700133 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
134 << " " << name;
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800135 DBusAdaptor::ClearProperty(device_->mutable_store(), name, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700136}
137
Eric Shienbrood9a245532012-03-07 14:20:39 -0500138void DeviceDBusAdaptor::Enable(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700139 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500140 Error e(Error::kOperationInitiated);
141 DBus::Tag *tag = new DBus::Tag();
142 device_->SetEnabledPersistent(true, &e, GetMethodReplyCallback(tag));
143 ReturnResultOrDefer(tag, e, &error);
144}
145
146void DeviceDBusAdaptor::Disable(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700147 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500148 Error e(Error::kOperationInitiated);
149 DBus::Tag *tag = new DBus::Tag();
150 device_->SetEnabledPersistent(false, &e, GetMethodReplyCallback(tag));
151 ReturnResultOrDefer(tag, e, &error);
152}
153
Chris Masoned7732e42011-05-20 11:08:56 -0700154void DeviceDBusAdaptor::ProposeScan(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700155 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc0865312011-09-16 15:31:20 -0700156 Error e;
Wade Guthrie68d41092013-04-02 12:56:02 -0700157 // User scan requests, which are the likely source of DBus requests, probably
158 // aren't time-critical so we might as well perform a complete scan. It
159 // also provides a failsafe for progressive scan.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700160 device_->Scan(Device::kFullScan, &e, __func__);
Darin Petkovc0865312011-09-16 15:31:20 -0700161 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700162}
163
164::DBus::Path DeviceDBusAdaptor::AddIPConfig(const string& ,
Paul Stewart624b9a22012-05-21 14:17:35 -0700165 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700166 SLOG(DBus, 2) << __func__;
Paul Stewart624b9a22012-05-21 14:17:35 -0700167 Error e(Error::kNotSupported, "This function is deprecated in shill");
168 e.ToDBusError(&error);
Darin Petkovfd164b82012-02-10 14:11:52 +0100169 return "/";
Chris Masoned7732e42011-05-20 11:08:56 -0700170}
171
Darin Petkov9ae310f2011-08-30 15:41:13 -0700172void DeviceDBusAdaptor::Register(const string &network_id,
173 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700174 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
175 << " (" << network_id << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500176 Error e(Error::kOperationInitiated);
177 DBus::Tag *tag = new DBus::Tag();
178 device_->RegisterOnNetwork(network_id, &e, GetMethodReplyCallback(tag));
179 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700180}
181
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100182void DeviceDBusAdaptor::RequirePin(
183 const string &pin, const bool &require, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700184 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500185 Error e(Error::kOperationInitiated);
186 DBus::Tag *tag = new DBus::Tag();
187 device_->RequirePIN(pin, require, &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::EnterPin(const string &pin, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700192 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500193 Error e(Error::kOperationInitiated);
194 DBus::Tag *tag = new DBus::Tag();
195 device_->EnterPIN(pin, &e, GetMethodReplyCallback(tag));
196 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700197}
198
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100199void DeviceDBusAdaptor::UnblockPin(
200 const string &unblock_code, const string &pin, DBus::Error &error) {
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
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100208void DeviceDBusAdaptor::ChangePin(
209 const string &old_pin, const string &new_pin, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700210 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc37a9c42012-09-06 15:28:22 +0200211 Error e(Error::kOperationInitiated);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500212 DBus::Tag *tag = new DBus::Tag();
213 device_->ChangePIN(old_pin, new_pin, &e, GetMethodReplyCallback(tag));
214 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700215}
216
Ben Chanad663e12013-01-08 01:58:47 -0800217void DeviceDBusAdaptor::Reset(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700218 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Ben Chanad663e12013-01-08 01:58:47 -0800219 Error e(Error::kOperationInitiated);
220 DBus::Tag *tag = new DBus::Tag();
221 device_->Reset(&e, GetMethodReplyCallback(tag));
222 ReturnResultOrDefer(tag, e, &error);
223}
224
Paul Stewartc6fbad92013-11-13 14:50:52 -0800225string DeviceDBusAdaptor::PerformTDLSOperation(const string &operation,
226 const string &peer,
227 DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700228 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Paul Stewartc6fbad92013-11-13 14:50:52 -0800229 Error e;
230 string return_value = device_->PerformTDLSOperation(operation, peer, &e);
231 e.ToDBusError(&error);
232 return return_value;
233}
234
Paul Stewart6ff27f52012-07-11 06:51:41 -0700235void DeviceDBusAdaptor::ResetByteCounters(DBus::Error &error) {
236 device_->ResetByteCounters();
237}
238
Darin Petkovc37a9c42012-09-06 15:28:22 +0200239void DeviceDBusAdaptor::SetCarrier(const string &carrier, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700240 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
241 << "(" << carrier << ")";
Darin Petkovc37a9c42012-09-06 15:28:22 +0200242 Error e(Error::kOperationInitiated);
243 DBus::Tag *tag = new DBus::Tag();
244 device_->SetCarrier(carrier, &e, GetMethodReplyCallback(tag));
245 ReturnResultOrDefer(tag, e, &error);
246}
247
Chris Masoned7732e42011-05-20 11:08:56 -0700248} // namespace shill