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_service_dbus_adaptor.h"
18 
19 #include <map>
20 #include <string>
21 
22 #include "shill/error.h"
23 #include "shill/logging.h"
24 #include "shill/service.h"
25 
26 using brillo::dbus_utils::AsyncEventSequencer;
27 using brillo::dbus_utils::ExportedObjectManager;
28 using std::map;
29 using std::string;
30 using std::vector;
31 
32 namespace shill {
33 
34 namespace Logging {
35 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(ChromeosServiceDBusAdaptor * s)36 static string ObjectID(ChromeosServiceDBusAdaptor* s) {
37   return s->GetRpcIdentifier() + " (" + s->service()->unique_name() + ")";
38 }
39 }
40 
41 // static
42 const char ChromeosServiceDBusAdaptor::kPath[] = "/service/";
43 
ChromeosServiceDBusAdaptor(const scoped_refptr<dbus::Bus> & bus,Service * service)44 ChromeosServiceDBusAdaptor::ChromeosServiceDBusAdaptor(
45     const scoped_refptr<dbus::Bus>& bus,
46     Service* service)
47     : org::chromium::flimflam::ServiceAdaptor(this),
48       ChromeosDBusAdaptor(bus, kPath + service->unique_name()),
49       service_(service) {
50   // Register DBus object.
51   RegisterWithDBusObject(dbus_object());
52   dbus_object()->RegisterAndBlock();
53 }
54 
~ChromeosServiceDBusAdaptor()55 ChromeosServiceDBusAdaptor::~ChromeosServiceDBusAdaptor() {
56   dbus_object()->UnregisterAsync();
57   service_ = nullptr;
58 }
59 
EmitBoolChanged(const string & name,bool value)60 void ChromeosServiceDBusAdaptor::EmitBoolChanged(const string& name,
61                                                  bool value) {
62   SLOG(this, 2) << __func__ << ": " << name;
63   SendPropertyChangedSignal(name, brillo::Any(value));
64 }
65 
EmitUint8Changed(const string & name,uint8_t value)66 void ChromeosServiceDBusAdaptor::EmitUint8Changed(const string& name,
67                                                   uint8_t value) {
68   SLOG(this, 2) << __func__ << ": " << name;
69   SendPropertyChangedSignal(name, brillo::Any(value));
70 }
71 
EmitUint16Changed(const string & name,uint16_t value)72 void ChromeosServiceDBusAdaptor::EmitUint16Changed(const string& name,
73                                                    uint16_t value) {
74   SLOG(this, 2) << __func__ << ": " << name;
75   SendPropertyChangedSignal(name, brillo::Any(value));
76 }
77 
EmitUint16sChanged(const string & name,const Uint16s & value)78 void ChromeosServiceDBusAdaptor::EmitUint16sChanged(const string& name,
79                                                     const Uint16s& value) {
80   SLOG(this, 2) << __func__ << ": " << name;
81   SendPropertyChangedSignal(name, brillo::Any(value));
82 }
83 
EmitUintChanged(const string & name,uint32_t value)84 void ChromeosServiceDBusAdaptor::EmitUintChanged(const string& name,
85                                                  uint32_t value) {
86   SLOG(this, 2) << __func__ << ": " << name;
87   SendPropertyChangedSignal(name, brillo::Any(value));
88 }
89 
EmitIntChanged(const string & name,int value)90 void ChromeosServiceDBusAdaptor::EmitIntChanged(const string& name, int value) {
91   SLOG(this, 2) << __func__ << ": " << name;
92   SendPropertyChangedSignal(name, brillo::Any(value));
93 }
94 
EmitRpcIdentifierChanged(const string & name,const string & value)95 void ChromeosServiceDBusAdaptor::EmitRpcIdentifierChanged(const string& name,
96                                                           const string& value) {
97   SLOG(this, 2) << __func__ << ": " << name;
98   SendPropertyChangedSignal(name, brillo::Any(dbus::ObjectPath(value)));
99 }
100 
EmitStringChanged(const string & name,const string & value)101 void ChromeosServiceDBusAdaptor::EmitStringChanged(const string& name,
102                                                    const string& value) {
103   SLOG(this, 2) << __func__ << ": " << name;
104   SendPropertyChangedSignal(name, brillo::Any(value));
105 }
106 
EmitStringmapChanged(const string & name,const Stringmap & value)107 void ChromeosServiceDBusAdaptor::EmitStringmapChanged(const string& name,
108                                                       const Stringmap& value) {
109   SLOG(this, 2) << __func__ << ": " << name;
110   SendPropertyChangedSignal(name, brillo::Any(value));
111 }
112 
GetProperties(brillo::ErrorPtr * error,brillo::VariantDictionary * properties)113 bool ChromeosServiceDBusAdaptor::GetProperties(
114     brillo::ErrorPtr* error, brillo::VariantDictionary* properties) {
115   SLOG(this, 2) << __func__;
116   return ChromeosDBusAdaptor::GetProperties(service_->store(),
117                                             properties,
118                                             error);
119 }
120 
SetProperty(brillo::ErrorPtr * error,const string & name,const brillo::Any & value)121 bool ChromeosServiceDBusAdaptor::SetProperty(
122     brillo::ErrorPtr* error, const string& name, const brillo::Any& value) {
123   SLOG(this, 2) << __func__ << ": " << name;
124   return ChromeosDBusAdaptor::SetProperty(service_->mutable_store(),
125                                           name,
126                                           value,
127                                           error);
128 }
129 
SetProperties(brillo::ErrorPtr * error,const brillo::VariantDictionary & args)130 bool ChromeosServiceDBusAdaptor::SetProperties(
131     brillo::ErrorPtr* error, const brillo::VariantDictionary& args) {
132   SLOG(this, 2) << __func__;
133   KeyValueStore args_store;
134   KeyValueStore::ConvertFromVariantDictionary(args, &args_store);
135   Error configure_error;
136   service_->Configure(args_store, &configure_error);
137   return !configure_error.ToChromeosError(error);
138 }
139 
ClearProperty(brillo::ErrorPtr * error,const string & name)140 bool ChromeosServiceDBusAdaptor::ClearProperty(brillo::ErrorPtr* error,
141                                               const string& name) {
142   SLOG(this, 2) << __func__ << ": " << name;
143   bool status = ChromeosDBusAdaptor::ClearProperty(service_->mutable_store(),
144                                                    name,
145                                                    error);
146   if (status) {
147     service_->OnPropertyChanged(name);
148   }
149   return status;
150 }
151 
ClearProperties(brillo::ErrorPtr *,const vector<string> & names,vector<bool> * results)152 bool ChromeosServiceDBusAdaptor::ClearProperties(brillo::ErrorPtr* /*error*/,
153                                                  const vector<string>& names,
154                                                  vector<bool>* results) {
155   SLOG(this, 2) << __func__;
156   vector<string>::const_iterator it;
157   for (it = names.begin(); it != names.end(); ++it) {
158     results->push_back(ClearProperty(nullptr, *it));
159   }
160   return true;
161 }
162 
Connect(brillo::ErrorPtr * error)163 bool ChromeosServiceDBusAdaptor::Connect(brillo::ErrorPtr* error) {
164   SLOG(this, 2) << __func__;
165   Error e;
166   service_->UserInitiatedConnect(&e);
167   return !e.ToChromeosError(error);
168 }
169 
Disconnect(brillo::ErrorPtr * error)170 bool ChromeosServiceDBusAdaptor::Disconnect(brillo::ErrorPtr* error) {
171   SLOG(this, 2) << __func__;
172   Error e;
173   service_->UserInitiatedDisconnect(&e);
174   return !e.ToChromeosError(error);
175 }
176 
Remove(brillo::ErrorPtr * error)177 bool ChromeosServiceDBusAdaptor::Remove(brillo::ErrorPtr* error) {
178   SLOG(this, 2) << __func__;
179   Error e;
180   service_->Remove(&e);
181   return !e.ToChromeosError(error);
182 }
183 
ActivateCellularModem(DBusMethodResponsePtr<> response,const string & carrier)184 void ChromeosServiceDBusAdaptor::ActivateCellularModem(
185     DBusMethodResponsePtr<> response, const string& carrier) {
186   SLOG(this, 2) << __func__;
187   Error e(Error::kOperationInitiated);
188   ResultCallback callback = GetMethodReplyCallback(std::move(response));
189   service_->ActivateCellularModem(carrier, &e, callback);
190   ReturnResultOrDefer(callback, e);
191 }
192 
CompleteCellularActivation(brillo::ErrorPtr * error)193 bool ChromeosServiceDBusAdaptor::CompleteCellularActivation(
194     brillo::ErrorPtr* error) {
195   SLOG(this, 2) << __func__;
196   Error e;
197   service_->CompleteCellularActivation(&e);
198   return !e.ToChromeosError(error);
199 }
200 
GetLoadableProfileEntries(brillo::ErrorPtr *,map<dbus::ObjectPath,string> * entries)201 bool ChromeosServiceDBusAdaptor::GetLoadableProfileEntries(
202     brillo::ErrorPtr* /*error*/, map<dbus::ObjectPath, string>* entries) {
203   SLOG(this, 2) << __func__;
204   map<string, string> profile_entry_strings =
205       service_->GetLoadableProfileEntries();
206   for (const auto& entry : profile_entry_strings) {
207     entries->insert(
208         std::make_pair(dbus::ObjectPath(entry.first), entry.second));
209   }
210   return true;
211 }
212 
213 }  // namespace shill
214