blob: 6a9863efaac697bb4ef0d818b9a907b9335f3b04 [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
49void DeviceDBusAdaptor::EmitUintChanged(const std::string& name, uint32 value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070050 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
51 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070052 PropertyChanged(name, DBusAdaptor::Uint32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070053}
54
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080055void DeviceDBusAdaptor::EmitUint16Changed(const string &name, uint16 value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070056 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
57 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080058 PropertyChanged(name, DBusAdaptor::Uint16ToVariant(value));
59}
60
Chris Masoned0ceb8c2011-06-02 10:05:39 -070061void DeviceDBusAdaptor::EmitIntChanged(const std::string& name, int value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070062 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
63 << " " << name;
Chris Masone8fe2c7e2011-06-09 15:51:19 -070064 PropertyChanged(name, DBusAdaptor::Int32ToVariant(value));
Chris Masoned0ceb8c2011-06-02 10:05:39 -070065}
66
67void DeviceDBusAdaptor::EmitStringChanged(const std::string& name,
68 const std::string& value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070069 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
70 << " " << name;
Chris Masoned0ceb8c2011-06-02 10:05:39 -070071 PropertyChanged(name, DBusAdaptor::StringToVariant(value));
72}
73
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080074void DeviceDBusAdaptor::EmitStringmapChanged(const std::string &name,
75 const Stringmap &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070076 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
77 << " " << name;
Prathmesh Prabhu9f06c872013-11-21 14:08:23 -080078 PropertyChanged(name, DBusAdaptor::StringmapToVariant(value));
79}
80
Darin Petkov3cfbf212011-11-21 16:02:09 +010081void DeviceDBusAdaptor::EmitStringmapsChanged(const std::string &name,
82 const Stringmaps &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070083 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
84 << " " << name;
Darin Petkov3cfbf212011-11-21 16:02:09 +010085 PropertyChanged(name, DBusAdaptor::StringmapsToVariant(value));
86}
87
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080088void DeviceDBusAdaptor::EmitStringsChanged(const std::string &name,
89 const Strings &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070090 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
91 << " " << name;
Prathmesh Prabhu700ff4d2014-01-16 15:59:33 -080092 PropertyChanged(name, DBusAdaptor::StringsToVariant(value));
93}
94
Darin Petkov63138a92012-02-06 14:09:15 +010095void DeviceDBusAdaptor::EmitKeyValueStoreChanged(const std::string &name,
96 const KeyValueStore &value) {
Peter Qiu047a21c2014-04-01 11:48:50 -070097 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
98 << " " << name;
Darin Petkov63138a92012-02-06 14:09:15 +010099 PropertyChanged(name, DBusAdaptor::KeyValueStoreToVariant(value));
100}
101
Paul Stewartd4f26482014-04-25 19:12:03 -0700102void DeviceDBusAdaptor::EmitRpcIdentifierArrayChanged(
103 const string &name,
104 const vector<string> &value) {
105 SLOG(DBus, 2) << __func__ << ": " << name;
106 vector< ::DBus::Path> paths;
Paul Stewart6db7b242014-05-02 15:34:21 -0700107 for (const auto &element : value) {
108 paths.push_back(element);
Paul Stewartd4f26482014-04-25 19:12:03 -0700109 }
110
111 PropertyChanged(name, DBusAdaptor::PathsToVariant(paths));
112}
113
Chris Masoned7732e42011-05-20 11:08:56 -0700114map<string, ::DBus::Variant> DeviceDBusAdaptor::GetProperties(
115 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700116 SLOG(DBus, 2) << __func__ << " " << device_->UniqueName();
Chris Masonea8a2c252011-06-27 22:16:30 -0700117 map<string, ::DBus::Variant> properties;
Chris Masone27c4aa52011-07-02 13:10:14 -0700118 DBusAdaptor::GetProperties(device_->store(), &properties, &error);
Chris Masonea8a2c252011-06-27 22:16:30 -0700119 return properties;
Chris Masoned7732e42011-05-20 11:08:56 -0700120}
121
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800122void DeviceDBusAdaptor::SetProperty(const string &name,
123 const ::DBus::Variant &value,
Chris Masoned7732e42011-05-20 11:08:56 -0700124 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700125 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
126 << " " << name;
mukesh agrawal6bb9e7c2012-01-30 14:57:54 -0800127 DBusAdaptor::SetProperty(device_->mutable_store(), name, value, &error);
Chris Masoned7732e42011-05-20 11:08:56 -0700128}
129
mukesh agrawal4d0401c2012-01-06 16:05:31 -0800130void DeviceDBusAdaptor::ClearProperty(const std::string &name,
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800131 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700132 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
133 << " " << name;
mukesh agrawal8abd2f62012-01-30 14:56:14 -0800134 DBusAdaptor::ClearProperty(device_->mutable_store(), name, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700135}
136
Eric Shienbrood9a245532012-03-07 14:20:39 -0500137void DeviceDBusAdaptor::Enable(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700138 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500139 Error e(Error::kOperationInitiated);
140 DBus::Tag *tag = new DBus::Tag();
141 device_->SetEnabledPersistent(true, &e, GetMethodReplyCallback(tag));
142 ReturnResultOrDefer(tag, e, &error);
143}
144
145void DeviceDBusAdaptor::Disable(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700146 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500147 Error e(Error::kOperationInitiated);
148 DBus::Tag *tag = new DBus::Tag();
149 device_->SetEnabledPersistent(false, &e, GetMethodReplyCallback(tag));
150 ReturnResultOrDefer(tag, e, &error);
151}
152
Chris Masoned7732e42011-05-20 11:08:56 -0700153void DeviceDBusAdaptor::ProposeScan(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700154 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc0865312011-09-16 15:31:20 -0700155 Error e;
Wade Guthrie68d41092013-04-02 12:56:02 -0700156 // User scan requests, which are the likely source of DBus requests, probably
157 // aren't time-critical so we might as well perform a complete scan. It
158 // also provides a failsafe for progressive scan.
Wade Guthrie4823f4f2013-07-25 10:03:03 -0700159 device_->Scan(Device::kFullScan, &e, __func__);
Darin Petkovc0865312011-09-16 15:31:20 -0700160 e.ToDBusError(&error);
Chris Masoned7732e42011-05-20 11:08:56 -0700161}
162
163::DBus::Path DeviceDBusAdaptor::AddIPConfig(const string& ,
Paul Stewart624b9a22012-05-21 14:17:35 -0700164 ::DBus::Error &error) {
mukesh agrawal06175d72012-04-23 16:46:01 -0700165 SLOG(DBus, 2) << __func__;
Paul Stewart624b9a22012-05-21 14:17:35 -0700166 Error e(Error::kNotSupported, "This function is deprecated in shill");
167 e.ToDBusError(&error);
Darin Petkovfd164b82012-02-10 14:11:52 +0100168 return "/";
Chris Masoned7732e42011-05-20 11:08:56 -0700169}
170
Darin Petkov9ae310f2011-08-30 15:41:13 -0700171void DeviceDBusAdaptor::Register(const string &network_id,
172 ::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700173 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
174 << " (" << network_id << ")";
Eric Shienbrood9a245532012-03-07 14:20:39 -0500175 Error e(Error::kOperationInitiated);
176 DBus::Tag *tag = new DBus::Tag();
177 device_->RegisterOnNetwork(network_id, &e, GetMethodReplyCallback(tag));
178 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700179}
180
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100181void DeviceDBusAdaptor::RequirePin(
182 const string &pin, const bool &require, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700183 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500184 Error e(Error::kOperationInitiated);
185 DBus::Tag *tag = new DBus::Tag();
186 device_->RequirePIN(pin, require, &e, GetMethodReplyCallback(tag));
187 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700188}
189
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100190void DeviceDBusAdaptor::EnterPin(const string &pin, DBus::Error &error) {
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
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100198void DeviceDBusAdaptor::UnblockPin(
199 const string &unblock_code, const string &pin, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700200 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Eric Shienbrood9a245532012-03-07 14:20:39 -0500201 Error e(Error::kOperationInitiated);
202 DBus::Tag *tag = new DBus::Tag();
203 device_->UnblockPIN(unblock_code, pin, &e, GetMethodReplyCallback(tag));
204 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700205}
206
Darin Petkovc64fe5e2012-01-11 12:46:13 +0100207void DeviceDBusAdaptor::ChangePin(
208 const string &old_pin, const string &new_pin, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700209 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Darin Petkovc37a9c42012-09-06 15:28:22 +0200210 Error e(Error::kOperationInitiated);
Eric Shienbrood9a245532012-03-07 14:20:39 -0500211 DBus::Tag *tag = new DBus::Tag();
212 device_->ChangePIN(old_pin, new_pin, &e, GetMethodReplyCallback(tag));
213 ReturnResultOrDefer(tag, e, &error);
Chris Masoneccc88812011-06-08 18:00:10 -0700214}
215
Ben Chanad663e12013-01-08 01:58:47 -0800216void DeviceDBusAdaptor::Reset(::DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700217 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Ben Chanad663e12013-01-08 01:58:47 -0800218 Error e(Error::kOperationInitiated);
219 DBus::Tag *tag = new DBus::Tag();
220 device_->Reset(&e, GetMethodReplyCallback(tag));
221 ReturnResultOrDefer(tag, e, &error);
222}
223
Paul Stewartc6fbad92013-11-13 14:50:52 -0800224string DeviceDBusAdaptor::PerformTDLSOperation(const string &operation,
225 const string &peer,
226 DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700227 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName();
Paul Stewartc6fbad92013-11-13 14:50:52 -0800228 Error e;
229 string return_value = device_->PerformTDLSOperation(operation, peer, &e);
230 e.ToDBusError(&error);
231 return return_value;
232}
233
Paul Stewart6ff27f52012-07-11 06:51:41 -0700234void DeviceDBusAdaptor::ResetByteCounters(DBus::Error &error) {
235 device_->ResetByteCounters();
236}
237
Darin Petkovc37a9c42012-09-06 15:28:22 +0200238void DeviceDBusAdaptor::SetCarrier(const string &carrier, DBus::Error &error) {
Peter Qiu047a21c2014-04-01 11:48:50 -0700239 SLOG(DBus, 2) << __func__ << ": Device " << device_->UniqueName()
240 << "(" << carrier << ")";
Darin Petkovc37a9c42012-09-06 15:28:22 +0200241 Error e(Error::kOperationInitiated);
242 DBus::Tag *tag = new DBus::Tag();
243 device_->SetCarrier(carrier, &e, GetMethodReplyCallback(tag));
244 ReturnResultOrDefer(tag, e, &error);
245}
246
Chris Masoned7732e42011-05-20 11:08:56 -0700247} // namespace shill