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_modem_proxy.h"
18 
19 #include <memory>
20 
21 #include <base/bind.h>
22 
23 #include "shill/cellular/cellular_error.h"
24 #include "shill/error.h"
25 #include "shill/logging.h"
26 
27 using std::string;
28 
29 namespace shill {
30 
31 namespace Logging {
32 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(const dbus::ObjectPath * p)33 static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
34 }  // namespace Logging
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::ModemManager::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(
53     bool enable, Error* error, const ResultCallback& callback, int timeout) {
54   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << enable;
55   proxy_->EnableAsync(enable,
56                       base::Bind(&ChromeosModemProxy::OnEnableSuccess,
57                                  weak_factory_.GetWeakPtr(),
58                                  callback),
59                       base::Bind(&ChromeosModemProxy::OnEnableFailure,
60                                  weak_factory_.GetWeakPtr(),
61                                  callback));
62 }
63 
Disconnect(Error * error,const ResultCallback & callback,int timeout)64 void ChromeosModemProxy::Disconnect(
65     Error* error, const ResultCallback& callback, int timeout) {
66   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
67   proxy_->DisconnectAsync(base::Bind(&ChromeosModemProxy::OnDisconnectSuccess,
68                                      weak_factory_.GetWeakPtr(),
69                                      callback),
70                           base::Bind(&ChromeosModemProxy::OnDisconnectFailure,
71                                      weak_factory_.GetWeakPtr(),
72                                      callback));
73 }
74 
GetModemInfo(Error * error,const ModemInfoCallback & callback,int timeout)75 void ChromeosModemProxy::GetModemInfo(
76     Error* error, const ModemInfoCallback& callback, int timeout) {
77   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
78   proxy_->GetInfoAsync(base::Bind(&ChromeosModemProxy::OnGetInfoSuccess,
79                                   weak_factory_.GetWeakPtr(),
80                                   callback),
81                        base::Bind(&ChromeosModemProxy::OnGetInfoFailure,
82                                   weak_factory_.GetWeakPtr(),
83                                   callback));
84 }
85 
StateChanged(uint32_t old,uint32_t _new,uint32_t reason)86 void ChromeosModemProxy::StateChanged(
87     uint32_t old, uint32_t _new, uint32_t reason) {
88   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << "(" << old
89       << ", " << _new << ", " << reason << ")";
90   if (state_changed_callback_.is_null()) {
91     return;
92   }
93   state_changed_callback_.Run(old, _new, reason);
94 }
95 
OnEnableSuccess(const ResultCallback & callback)96 void ChromeosModemProxy::OnEnableSuccess(const ResultCallback& callback) {
97   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
98   callback.Run(Error());
99 }
100 
OnEnableFailure(const ResultCallback & callback,brillo::Error * dbus_error)101 void ChromeosModemProxy::OnEnableFailure(const ResultCallback& callback,
102                                          brillo::Error* dbus_error) {
103   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
104   Error error;
105   CellularError::FromChromeosDBusError(dbus_error, &error);
106   callback.Run(error);
107 }
108 
OnGetInfoSuccess(const ModemInfoCallback & callback,const ModemHardwareInfo & info)109 void ChromeosModemProxy::OnGetInfoSuccess(const ModemInfoCallback& callback,
110                                           const ModemHardwareInfo& info) {
111   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
112   callback.Run(
113       std::get<0>(info), std::get<1>(info), std::get<2>(info), Error());
114 }
115 
OnGetInfoFailure(const ModemInfoCallback & callback,brillo::Error * dbus_error)116 void ChromeosModemProxy::OnGetInfoFailure(const ModemInfoCallback& callback,
117                                           brillo::Error* dbus_error) {
118   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
119   Error error;
120   CellularError::FromChromeosDBusError(dbus_error, &error);
121   callback.Run("", "", "", error);
122 }
123 
OnDisconnectSuccess(const ResultCallback & callback)124 void ChromeosModemProxy::OnDisconnectSuccess(const ResultCallback& callback) {
125   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
126   callback.Run(Error());
127 }
128 
OnDisconnectFailure(const ResultCallback & callback,brillo::Error * dbus_error)129 void ChromeosModemProxy::OnDisconnectFailure(const ResultCallback& callback,
130                                              brillo::Error* dbus_error) {
131   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
132   Error error;
133   CellularError::FromChromeosDBusError(dbus_error, &error);
134   callback.Run(error);
135 }
136 
OnSignalConnected(const string & interface_name,const string & signal_name,bool success)137 void ChromeosModemProxy::OnSignalConnected(
138     const string& interface_name, const string& signal_name, bool success) {
139   SLOG(&proxy_->GetObjectPath(), 2) << __func__
140       << "interface: " << interface_name
141              << " signal: " << signal_name << "success: " << success;
142   if (!success) {
143     LOG(ERROR) << "Failed to connect signal " << signal_name
144         << " to interface " << interface_name;
145   }
146 }
147 
148 }  // namespace shill
149