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_mm1_modem_proxy.h"
18 
19 #include <memory>
20 #include <tuple>
21 
22 #include "shill/cellular/cellular_error.h"
23 #include "shill/logging.h"
24 
25 using std::string;
26 
27 namespace shill {
28 
29 namespace Logging {
30 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(const dbus::ObjectPath * p)31 static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
32 }  // namespace Logging
33 
34 namespace mm1 {
35 
ChromeosModemProxy(const scoped_refptr<dbus::Bus> & bus,const string & path,const string & service)36 ChromeosModemProxy::ChromeosModemProxy(const scoped_refptr<dbus::Bus>& bus,
37                                        const string& path,
38                                        const string& service)
39     : proxy_(
40         new org::freedesktop::ModemManager1::ModemProxy(
41             bus, service, dbus::ObjectPath(path))) {
42   // Register signal handlers.
43   proxy_->RegisterStateChangedSignalHandler(
44       base::Bind(&ChromeosModemProxy::StateChanged,
45                  weak_factory_.GetWeakPtr()),
46       base::Bind(&ChromeosModemProxy::OnSignalConnected,
47                  weak_factory_.GetWeakPtr()));
48 }
49 
~ChromeosModemProxy()50 ChromeosModemProxy::~ChromeosModemProxy() {}
51 
Enable(bool enable,Error * error,const ResultCallback & callback,int timeout)52 void ChromeosModemProxy::Enable(bool enable,
53                                 Error* error,
54                                 const ResultCallback& callback,
55                                 int timeout) {
56   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << enable;
57   proxy_->EnableAsync(enable,
58                       base::Bind(&ChromeosModemProxy::OnOperationSuccess,
59                                  weak_factory_.GetWeakPtr(),
60                                  callback,
61                                  __func__),
62                       base::Bind(&ChromeosModemProxy::OnOperationFailure,
63                                  weak_factory_.GetWeakPtr(),
64                                  callback,
65                                  __func__));
66 }
67 
CreateBearer(const KeyValueStore & properties,Error * error,const RpcIdentifierCallback & callback,int timeout)68 void ChromeosModemProxy::CreateBearer(
69     const KeyValueStore& properties,
70     Error* error,
71     const RpcIdentifierCallback& callback,
72     int timeout) {
73   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
74   brillo::VariantDictionary properties_dict;
75   KeyValueStore::ConvertToVariantDictionary(properties, &properties_dict);
76   proxy_->CreateBearerAsync(
77       properties_dict,
78       base::Bind(&ChromeosModemProxy::OnCreateBearerSuccess,
79                  weak_factory_.GetWeakPtr(),
80                  callback),
81       base::Bind(&ChromeosModemProxy::OnCreateBearerFailure,
82                  weak_factory_.GetWeakPtr(),
83                  callback));
84 }
85 
DeleteBearer(const string & bearer,Error * error,const ResultCallback & callback,int timeout)86 void ChromeosModemProxy::DeleteBearer(const string& bearer,
87                                       Error* error,
88                                       const ResultCallback& callback,
89                                       int timeout) {
90   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << bearer;
91   proxy_->DeleteBearerAsync(dbus::ObjectPath(bearer),
92                             base::Bind(&ChromeosModemProxy::OnOperationSuccess,
93                                        weak_factory_.GetWeakPtr(),
94                                        callback,
95                                        __func__),
96                             base::Bind(&ChromeosModemProxy::OnOperationFailure,
97                                        weak_factory_.GetWeakPtr(),
98                                        callback,
99                                        __func__));
100 }
101 
Reset(Error * error,const ResultCallback & callback,int timeout)102 void ChromeosModemProxy::Reset(Error* error,
103                                const ResultCallback& callback,
104                                int timeout) {
105   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
106   proxy_->ResetAsync(base::Bind(&ChromeosModemProxy::OnOperationSuccess,
107                                 weak_factory_.GetWeakPtr(),
108                                 callback,
109                                 __func__),
110                      base::Bind(&ChromeosModemProxy::OnOperationFailure,
111                                 weak_factory_.GetWeakPtr(),
112                                 callback,
113                                 __func__));
114 }
115 
FactoryReset(const std::string & code,Error * error,const ResultCallback & callback,int timeout)116 void ChromeosModemProxy::FactoryReset(const std::string& code,
117                                       Error* error,
118                                       const ResultCallback& callback,
119                                       int timeout) {
120   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
121   proxy_->FactoryResetAsync(code,
122                             base::Bind(&ChromeosModemProxy::OnOperationSuccess,
123                                        weak_factory_.GetWeakPtr(),
124                                        callback,
125                                        __func__),
126                             base::Bind(&ChromeosModemProxy::OnOperationFailure,
127                                        weak_factory_.GetWeakPtr(),
128                                        callback,
129                                        __func__));
130 }
131 
SetCurrentCapabilities(uint32_t capabilities,Error * error,const ResultCallback & callback,int timeout)132 void ChromeosModemProxy::SetCurrentCapabilities(uint32_t capabilities,
133                                                 Error* error,
134                                                 const ResultCallback& callback,
135                                                 int timeout) {
136   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << capabilities;
137   proxy_->SetCurrentCapabilitiesAsync(
138       capabilities,
139       base::Bind(&ChromeosModemProxy::OnOperationSuccess,
140                  weak_factory_.GetWeakPtr(),
141                  callback,
142                  __func__),
143       base::Bind(&ChromeosModemProxy::OnOperationFailure,
144                  weak_factory_.GetWeakPtr(),
145                  callback,
146                  __func__));
147 }
148 
SetCurrentModes(uint32_t allowed_modes,uint32_t preferred_mode,Error * error,const ResultCallback & callback,int timeout)149 void ChromeosModemProxy::SetCurrentModes(uint32_t allowed_modes,
150                                          uint32_t preferred_mode,
151                                          Error* error,
152                                          const ResultCallback& callback,
153                                          int timeout) {
154   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << allowed_modes
155                                     << " " << preferred_mode;
156   std::tuple<uint32_t, uint32_t> modes { allowed_modes, preferred_mode };
157   proxy_->SetCurrentModesAsync(
158       modes,
159       base::Bind(&ChromeosModemProxy::OnOperationSuccess,
160                  weak_factory_.GetWeakPtr(),
161                  callback,
162                  __func__),
163       base::Bind(&ChromeosModemProxy::OnOperationFailure,
164                  weak_factory_.GetWeakPtr(),
165                  callback,
166                  __func__));
167 }
168 
SetCurrentBands(const std::vector<uint32_t> & bands,Error * error,const ResultCallback & callback,int timeout)169 void ChromeosModemProxy::SetCurrentBands(const std::vector<uint32_t>& bands,
170                                          Error* error,
171                                          const ResultCallback& callback,
172                                          int timeout) {
173   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
174   proxy_->SetCurrentBandsAsync(
175       bands,
176       base::Bind(&ChromeosModemProxy::OnOperationSuccess,
177                  weak_factory_.GetWeakPtr(),
178                  callback,
179                  __func__),
180       base::Bind(&ChromeosModemProxy::OnOperationFailure,
181                  weak_factory_.GetWeakPtr(),
182                  callback,
183                  __func__));
184 }
185 
Command(const std::string & cmd,uint32_t user_timeout,Error * error,const StringCallback & callback,int timeout)186 void ChromeosModemProxy::Command(const std::string& cmd,
187                                  uint32_t user_timeout,
188                                  Error* error,
189                                  const StringCallback& callback,
190                                  int timeout) {
191   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << cmd;
192   proxy_->CommandAsync(cmd,
193                        user_timeout,
194                        base::Bind(&ChromeosModemProxy::OnCommandSuccess,
195                                   weak_factory_.GetWeakPtr(),
196                                   callback),
197                        base::Bind(&ChromeosModemProxy::OnCommandFailure,
198                                   weak_factory_.GetWeakPtr(),
199                                   callback));
200 }
201 
SetPowerState(uint32_t power_state,Error * error,const ResultCallback & callback,int timeout)202 void ChromeosModemProxy::SetPowerState(uint32_t power_state,
203                                        Error* error,
204                                        const ResultCallback& callback,
205                                        int timeout) {
206   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << power_state;
207   proxy_->SetPowerStateAsync(
208       power_state,
209       base::Bind(&ChromeosModemProxy::OnOperationSuccess,
210                  weak_factory_.GetWeakPtr(),
211                  callback,
212                  __func__),
213       base::Bind(&ChromeosModemProxy::OnOperationFailure,
214                  weak_factory_.GetWeakPtr(),
215                  callback,
216                  __func__));
217 }
218 
StateChanged(int32_t old,int32_t _new,uint32_t reason)219 void ChromeosModemProxy::StateChanged(
220     int32_t old, int32_t _new, uint32_t reason) {
221   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
222   if (state_changed_callback_.is_null()) {
223     return;
224   }
225   state_changed_callback_.Run(old, _new, reason);
226 }
227 
OnCreateBearerSuccess(const RpcIdentifierCallback & callback,const dbus::ObjectPath & path)228 void ChromeosModemProxy::OnCreateBearerSuccess(
229     const RpcIdentifierCallback& callback, const dbus::ObjectPath& path) {
230   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << path.value();
231   callback.Run(path.value(), Error());
232 }
233 
OnCreateBearerFailure(const RpcIdentifierCallback & callback,brillo::Error * dbus_error)234 void ChromeosModemProxy::OnCreateBearerFailure(
235     const RpcIdentifierCallback& callback, brillo::Error* dbus_error) {
236   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
237   Error error;
238   CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
239   callback.Run("", error);
240 }
241 
OnCommandSuccess(const StringCallback & callback,const string & response)242 void ChromeosModemProxy::OnCommandSuccess(const StringCallback& callback,
243                                           const string& response) {
244   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << response;
245   callback.Run(response, Error());
246 }
247 
OnCommandFailure(const StringCallback & callback,brillo::Error * dbus_error)248 void ChromeosModemProxy::OnCommandFailure(const StringCallback& callback,
249                                           brillo::Error* dbus_error) {
250   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
251   Error error;
252   CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
253   callback.Run("", error);
254 }
255 
OnOperationSuccess(const ResultCallback & callback,const string & operation)256 void ChromeosModemProxy::OnOperationSuccess(const ResultCallback& callback,
257                                             const string& operation) {
258   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation;
259   callback.Run(Error());
260 }
261 
OnOperationFailure(const ResultCallback & callback,const string & operation,brillo::Error * dbus_error)262 void ChromeosModemProxy::OnOperationFailure(const ResultCallback& callback,
263                                             const string& operation,
264                                             brillo::Error* dbus_error) {
265   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation;
266   Error error;
267   CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
268   callback.Run(error);
269 }
270 
OnSignalConnected(const string & interface_name,const string & signal_name,bool success)271 void ChromeosModemProxy::OnSignalConnected(
272     const string& interface_name, const string& signal_name, bool success) {
273   SLOG(&proxy_->GetObjectPath(), 2) << __func__
274       << "interface: " << interface_name
275              << " signal: " << signal_name << "success: " << success;
276   if (!success) {
277     LOG(ERROR) << "Failed to connect signal " << signal_name
278         << " to interface " << interface_name;
279   }
280 }
281 
282 }  // namespace mm1
283 }  // namespace shill
284