1 //
2 // Copyright (C) 2015 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "shill/dbus/chromeos_device_dbus_adaptor.h"
18 
19 #include "shill/device.h"
20 #include "shill/error.h"
21 #include "shill/logging.h"
22 
23 using brillo::dbus_utils::AsyncEventSequencer;
24 using brillo::dbus_utils::DBusObject;
25 using brillo::dbus_utils::ExportedObjectManager;
26 using std::string;
27 using std::vector;
28 
29 namespace shill {
30 
31 namespace Logging {
32 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(ChromeosDeviceDBusAdaptor * d)33 static string ObjectID(ChromeosDeviceDBusAdaptor* d) {
34   return d->GetRpcIdentifier() + " (" + d->device()->UniqueName() + ")";
35 }
36 }
37 
38 // static
39 const char ChromeosDeviceDBusAdaptor::kPath[] = "/device/";
40 
ChromeosDeviceDBusAdaptor(const scoped_refptr<dbus::Bus> & bus,Device * device)41 ChromeosDeviceDBusAdaptor::ChromeosDeviceDBusAdaptor(
42     const scoped_refptr<dbus::Bus>& bus,
43     Device* device)
44     : org::chromium::flimflam::DeviceAdaptor(this),
45       ChromeosDBusAdaptor(bus,
46                           kPath + SanitizePathElement(device->UniqueName())),
47       device_(device) {
48   // Register DBus object.
49   RegisterWithDBusObject(dbus_object());
50   dbus_object()->RegisterAndBlock();
51 }
52 
~ChromeosDeviceDBusAdaptor()53 ChromeosDeviceDBusAdaptor::~ChromeosDeviceDBusAdaptor() {
54   dbus_object()->UnregisterAsync();
55   device_ = nullptr;
56 }
57 
GetRpcIdentifier()58 const string& ChromeosDeviceDBusAdaptor::GetRpcIdentifier() {
59   return dbus_path().value();
60 }
61 
EmitBoolChanged(const string & name,bool value)62 void ChromeosDeviceDBusAdaptor::EmitBoolChanged(const string& name,
63                                                 bool value) {
64   SLOG(this, 2) << __func__ << ": " << name;
65   SendPropertyChangedSignal(name, brillo::Any(value));
66 }
67 
EmitUintChanged(const string & name,uint32_t value)68 void ChromeosDeviceDBusAdaptor::EmitUintChanged(const string& name,
69                                                 uint32_t value) {
70   SLOG(this, 2) << __func__ << ": " << name;
71   SendPropertyChangedSignal(name, brillo::Any(value));
72 }
73 
EmitUint16Changed(const string & name,uint16_t value)74 void ChromeosDeviceDBusAdaptor::EmitUint16Changed(const string& name,
75                                                   uint16_t value) {
76   SLOG(this, 2) << __func__ << ": " << name;
77   SendPropertyChangedSignal(name, brillo::Any(value));
78 }
79 
EmitIntChanged(const string & name,int value)80 void ChromeosDeviceDBusAdaptor::EmitIntChanged(const string& name, int value) {
81   SLOG(this, 2) << __func__ << ": " << name;
82   SendPropertyChangedSignal(name, brillo::Any(value));
83 }
84 
EmitStringChanged(const string & name,const string & value)85 void ChromeosDeviceDBusAdaptor::EmitStringChanged(const string& name,
86                                                   const string& value) {
87   SLOG(this, 2) << __func__ << ": " << name;
88   SendPropertyChangedSignal(name, brillo::Any(value));
89 }
90 
EmitStringmapChanged(const string & name,const Stringmap & value)91 void ChromeosDeviceDBusAdaptor::EmitStringmapChanged(const string& name,
92                                                      const Stringmap& value) {
93   SLOG(this, 2) << __func__ << ": " << name;
94   SendPropertyChangedSignal(name, brillo::Any(value));
95 }
96 
EmitStringmapsChanged(const string & name,const Stringmaps & value)97 void ChromeosDeviceDBusAdaptor::EmitStringmapsChanged(const string& name,
98                                                       const Stringmaps& value) {
99   SLOG(this, 2) << __func__ << ": " << name;
100   SendPropertyChangedSignal(name, brillo::Any(value));
101 }
102 
EmitStringsChanged(const string & name,const Strings & value)103 void ChromeosDeviceDBusAdaptor::EmitStringsChanged(const string& name,
104                                                    const Strings& value) {
105   SLOG(this, 2) << __func__ << ": " << name;
106   SendPropertyChangedSignal(name, brillo::Any(value));
107 }
108 
EmitKeyValueStoreChanged(const string & name,const KeyValueStore & value)109 void ChromeosDeviceDBusAdaptor::EmitKeyValueStoreChanged(
110     const string& name, const KeyValueStore& value) {
111   SLOG(this, 2) << __func__ << ": " << name;
112   brillo::VariantDictionary dict;
113   KeyValueStore::ConvertToVariantDictionary(value, &dict);
114   SendPropertyChangedSignal(name, brillo::Any(dict));
115 }
116 
EmitRpcIdentifierChanged(const std::string & name,const std::string & value)117 void ChromeosDeviceDBusAdaptor::EmitRpcIdentifierChanged(
118     const std::string& name, const std::string& value) {
119   SLOG(this, 2) << __func__ << ": " << name;
120   SendPropertyChangedSignal(name, brillo::Any(dbus::ObjectPath(value)));
121 }
122 
EmitRpcIdentifierArrayChanged(const string & name,const vector<string> & value)123 void ChromeosDeviceDBusAdaptor::EmitRpcIdentifierArrayChanged(
124     const string& name,
125     const vector<string>& value) {
126   SLOG(this, 2) << __func__ << ": " << name;
127   vector<dbus::ObjectPath> paths;
128   for (const auto& element : value) {
129     paths.push_back(dbus::ObjectPath(element));
130   }
131 
132   SendPropertyChangedSignal(name, brillo::Any(paths));
133 }
134 
GetProperties(brillo::ErrorPtr * error,brillo::VariantDictionary * out_properties)135 bool ChromeosDeviceDBusAdaptor::GetProperties(
136     brillo::ErrorPtr* error, brillo::VariantDictionary* out_properties) {
137   SLOG(this, 2) << __func__;
138   return ChromeosDBusAdaptor::GetProperties(device_->store(),
139                                             out_properties,
140                                             error);
141 }
142 
SetProperty(brillo::ErrorPtr * error,const string & name,const brillo::Any & value)143 bool ChromeosDeviceDBusAdaptor::SetProperty(brillo::ErrorPtr* error,
144                                             const string& name,
145                                             const brillo::Any& value) {
146   SLOG(this, 2) << __func__ << ": " << name;
147   return ChromeosDBusAdaptor::SetProperty(device_->mutable_store(),
148                                           name,
149                                           value,
150                                           error);
151 }
152 
ClearProperty(brillo::ErrorPtr * error,const string & name)153 bool ChromeosDeviceDBusAdaptor::ClearProperty(brillo::ErrorPtr* error,
154                                               const string& name) {
155   SLOG(this, 2) << __func__ << ": " << name;
156   return ChromeosDBusAdaptor::ClearProperty(device_->mutable_store(),
157                                             name,
158                                             error);
159 }
160 
Enable(DBusMethodResponsePtr<> response)161 void ChromeosDeviceDBusAdaptor::Enable(DBusMethodResponsePtr<> response) {
162   SLOG(this, 2) << __func__;
163   Error e(Error::kOperationInitiated);
164   ResultCallback callback = GetMethodReplyCallback(std::move(response));
165   device_->SetEnabledPersistent(true, &e, callback);
166   ReturnResultOrDefer(callback, e);
167 }
168 
Disable(DBusMethodResponsePtr<> response)169 void ChromeosDeviceDBusAdaptor::Disable(DBusMethodResponsePtr<> response) {
170   SLOG(this, 2) << __func__ << ": Device " << device_->UniqueName();
171   Error e(Error::kOperationInitiated);
172   ResultCallback callback = GetMethodReplyCallback(std::move(response));
173   device_->SetEnabledPersistent(false, &e, callback);
174   ReturnResultOrDefer(callback, e);
175 }
176 
ProposeScan(brillo::ErrorPtr * error)177 bool ChromeosDeviceDBusAdaptor::ProposeScan(brillo::ErrorPtr* error) {
178   SLOG(this, 2) << __func__;
179   Error e;
180   // User scan requests, which are the likely source of DBus requests, probably
181   // aren't time-critical so we might as well perform a complete scan.  It
182   // also provides a failsafe for progressive scan.
183   device_->Scan(Device::kFullScan, &e, __func__);
184 
185   return !e.ToChromeosError(error);
186 }
187 
AddIPConfig(brillo::ErrorPtr * error,const string &,dbus::ObjectPath * out_path)188 bool ChromeosDeviceDBusAdaptor::AddIPConfig(brillo::ErrorPtr* error,
189                                             const string& /*method*/,
190                                             dbus::ObjectPath* out_path) {
191   SLOG(this, 2) << __func__;
192   Error e(Error::kNotSupported, "This function is deprecated in shill");
193   return !e.ToChromeosError(error);
194 }
195 
Register(DBusMethodResponsePtr<> response,const string & network_id)196 void ChromeosDeviceDBusAdaptor::Register(DBusMethodResponsePtr<> response,
197                                          const string& network_id) {
198   SLOG(this, 2) << __func__ << ": " << network_id;
199   Error e(Error::kOperationInitiated);
200   ResultCallback callback = GetMethodReplyCallback(std::move(response));
201   device_->RegisterOnNetwork(network_id, &e, callback);
202   ReturnResultOrDefer(callback, e);
203 }
204 
RequirePin(DBusMethodResponsePtr<> response,const string & pin,bool require)205 void ChromeosDeviceDBusAdaptor::RequirePin(
206     DBusMethodResponsePtr<> response, const string& pin, bool require) {
207   SLOG(this, 2) << __func__;
208 
209   Error e(Error::kOperationInitiated);
210   ResultCallback callback = GetMethodReplyCallback(std::move(response));
211   device_->RequirePIN(pin, require, &e, callback);
212   ReturnResultOrDefer(callback, e);
213 }
214 
EnterPin(DBusMethodResponsePtr<> response,const string & pin)215 void ChromeosDeviceDBusAdaptor::EnterPin(DBusMethodResponsePtr<> response,
216                                          const string& pin) {
217   SLOG(this, 2) << __func__;
218 
219   Error e(Error::kOperationInitiated);
220   ResultCallback callback = GetMethodReplyCallback(std::move(response));
221   device_->EnterPIN(pin, &e, callback);
222   ReturnResultOrDefer(callback, e);
223 }
224 
UnblockPin(DBusMethodResponsePtr<> response,const string & unblock_code,const string & pin)225 void ChromeosDeviceDBusAdaptor::UnblockPin(DBusMethodResponsePtr<> response,
226                                            const string& unblock_code,
227                                            const string& pin) {
228   SLOG(this, 2) << __func__;
229 
230   Error e(Error::kOperationInitiated);
231   ResultCallback callback = GetMethodReplyCallback(std::move(response));
232   device_->UnblockPIN(unblock_code, pin, &e, callback);
233   ReturnResultOrDefer(callback, e);
234 }
235 
ChangePin(DBusMethodResponsePtr<> response,const string & old_pin,const string & new_pin)236 void ChromeosDeviceDBusAdaptor::ChangePin(DBusMethodResponsePtr<> response,
237                                           const string& old_pin,
238                                           const string& new_pin) {
239   SLOG(this, 2) << __func__;
240 
241   Error e(Error::kOperationInitiated);
242   ResultCallback callback = GetMethodReplyCallback(std::move(response));
243   device_->ChangePIN(old_pin, new_pin, &e, callback);
244   ReturnResultOrDefer(callback, e);
245 }
246 
Reset(DBusMethodResponsePtr<> response)247 void ChromeosDeviceDBusAdaptor::Reset(DBusMethodResponsePtr<> response) {
248   SLOG(this, 2) << __func__;
249 
250   Error e(Error::kOperationInitiated);
251   ResultCallback callback = GetMethodReplyCallback(std::move(response));
252   device_->Reset(&e, callback);
253   ReturnResultOrDefer(callback, e);
254 }
255 
PerformTDLSOperation(brillo::ErrorPtr * error,const string & operation,const string & peer,string * out_state)256 bool ChromeosDeviceDBusAdaptor::PerformTDLSOperation(brillo::ErrorPtr* error,
257                                                      const string& operation,
258                                                      const string& peer,
259                                                      string* out_state) {
260   SLOG(this, 2) << __func__;
261 
262   Error e;
263   *out_state = device_->PerformTDLSOperation(operation, peer, &e);
264   return !e.ToChromeosError(error);
265 }
266 
ResetByteCounters(brillo::ErrorPtr * error)267 bool ChromeosDeviceDBusAdaptor::ResetByteCounters(brillo::ErrorPtr* error) {
268   device_->ResetByteCounters();
269   return true;
270 }
271 
SetCarrier(DBusMethodResponsePtr<> response,const string & carrier)272 void ChromeosDeviceDBusAdaptor::SetCarrier(DBusMethodResponsePtr<> response,
273                                            const string& carrier) {
274   SLOG(this, 2) << __func__ << ": " << carrier;
275 
276   Error e(Error::kOperationInitiated);
277   ResultCallback callback = GetMethodReplyCallback(std::move(response));
278   device_->SetCarrier(carrier, &e, callback);
279   ReturnResultOrDefer(callback, e);
280 }
281 
RequestRoam(brillo::ErrorPtr * error,const std::string & addr)282 bool ChromeosDeviceDBusAdaptor::RequestRoam(brillo::ErrorPtr* error,
283                                             const std::string& addr) {
284   SLOG(this, 2) << __func__ << ": " << addr;
285   Error e;
286   device_->RequestRoam(addr, &e);
287   return !e.ToChromeosError(error);
288 }
289 
AddWakeOnPacketConnection(brillo::ErrorPtr * error,const string & ip_endpoint)290 bool ChromeosDeviceDBusAdaptor::AddWakeOnPacketConnection(
291     brillo::ErrorPtr* error, const string& ip_endpoint) {
292   SLOG(this, 2) << __func__;
293 
294   Error e;
295   device_->AddWakeOnPacketConnection(ip_endpoint, &e);
296   return !e.ToChromeosError(error);
297 }
298 
RemoveWakeOnPacketConnection(brillo::ErrorPtr * error,const string & ip_endpoint)299 bool ChromeosDeviceDBusAdaptor::RemoveWakeOnPacketConnection(
300     brillo::ErrorPtr* error, const string& ip_endpoint) {
301   SLOG(this, 2) << __func__;
302 
303   Error e;
304   device_->RemoveWakeOnPacketConnection(ip_endpoint, &e);
305   return !e.ToChromeosError(error);
306 }
307 
RemoveAllWakeOnPacketConnections(brillo::ErrorPtr * error)308 bool ChromeosDeviceDBusAdaptor::RemoveAllWakeOnPacketConnections(
309     brillo::ErrorPtr* error) {
310   SLOG(this, 2) << __func__;
311 
312   Error e;
313   device_->RemoveAllWakeOnPacketConnections(&e);
314   return !e.ToChromeosError(error);
315 }
316 
317 }  // namespace shill
318