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_gsm_card_proxy.h"
18
19#include <memory>
20
21#include <base/bind.h>
22#if defined(__ANDROID__)
23#include <dbus/service_constants.h>
24#else
25#include <chromeos/dbus/service_constants.h>
26#endif  // __ANDROID__
27
28#include "shill/cellular/cellular_error.h"
29#include "shill/error.h"
30#include "shill/logging.h"
31
32using std::string;
33
34namespace shill {
35
36namespace Logging {
37static auto kModuleLogScope = ScopeLogger::kDBus;
38static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
39}  // namespace Logging
40
41// static.
42const char ChromeosModemGSMCardProxy::kPropertyEnabledFacilityLocks[] =
43    "EnabledFacilityLocks";
44
45ChromeosModemGSMCardProxy::PropertySet::PropertySet(
46    dbus::ObjectProxy* object_proxy,
47    const std::string& interface_name,
48    const PropertyChangedCallback& callback)
49    : dbus::PropertySet(object_proxy, interface_name, callback) {
50  RegisterProperty(kPropertyEnabledFacilityLocks, &enabled_facility_locks);
51}
52
53ChromeosModemGSMCardProxy::ChromeosModemGSMCardProxy(
54    const scoped_refptr<dbus::Bus>& bus,
55    const string& path,
56    const string& service)
57    : proxy_(
58        new org::freedesktop::ModemManager::Modem::Gsm::CardProxy(
59            bus, service, dbus::ObjectPath(path))) {
60  // Register properties.
61  properties_.reset(
62      new PropertySet(
63          proxy_->GetObjectProxy(),
64          cromo::kModemGsmCardInterface,
65          base::Bind(&ChromeosModemGSMCardProxy::OnPropertyChanged,
66                     weak_factory_.GetWeakPtr())));
67
68  // Connect property signals and initialize cached values. Based on
69  // recommendations from src/dbus/property.h.
70  properties_->ConnectSignals();
71  properties_->GetAll();
72}
73
74ChromeosModemGSMCardProxy::~ChromeosModemGSMCardProxy() {}
75
76void ChromeosModemGSMCardProxy::GetIMEI(
77    Error* error, const GSMIdentifierCallback& callback, int timeout) {
78  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
79  proxy_->GetImeiAsync(
80      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierSuccess,
81                 weak_factory_.GetWeakPtr(),
82                 callback,
83                 "IMEI"),
84      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierFailure,
85                 weak_factory_.GetWeakPtr(),
86                 callback,
87                 "IMEI"));
88}
89
90void ChromeosModemGSMCardProxy::GetIMSI(Error* error,
91                                        const GSMIdentifierCallback& callback,
92                                        int timeout) {
93  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
94  proxy_->GetImsiAsync(
95      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierSuccess,
96                 weak_factory_.GetWeakPtr(),
97                 callback,
98                 "IMSI"),
99      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierFailure,
100                 weak_factory_.GetWeakPtr(),
101                 callback,
102                 "IMSI"));
103}
104
105void ChromeosModemGSMCardProxy::GetSPN(Error* error,
106                                       const GSMIdentifierCallback& callback,
107                                       int timeout) {
108  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
109  proxy_->GetSpnAsync(
110      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierSuccess,
111                 weak_factory_.GetWeakPtr(),
112                 callback,
113                 "SPN"),
114      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierFailure,
115                 weak_factory_.GetWeakPtr(),
116                 callback,
117                 "SPN"));
118}
119
120void ChromeosModemGSMCardProxy::GetMSISDN(Error* error,
121                                          const GSMIdentifierCallback& callback,
122                                          int timeout) {
123  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
124  proxy_->GetMsIsdnAsync(
125      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierSuccess,
126                 weak_factory_.GetWeakPtr(),
127                 callback,
128                 "MSIDN"),
129      base::Bind(&ChromeosModemGSMCardProxy::OnGetGSMIdentifierFailure,
130                 weak_factory_.GetWeakPtr(),
131                 callback,
132                 "MSIDN"));
133}
134
135void ChromeosModemGSMCardProxy::EnablePIN(const string& pin, bool enabled,
136                                          Error* error,
137                                          const ResultCallback& callback,
138                                          int timeout) {
139  // pin is intentionally not logged.
140  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << enabled;
141  proxy_->EnablePinAsync(
142      pin,
143      enabled,
144      base::Bind(&ChromeosModemGSMCardProxy::OnOperationSuccess,
145                 weak_factory_.GetWeakPtr(),
146                 callback,
147                 __func__),
148      base::Bind(&ChromeosModemGSMCardProxy::OnOperationFailure,
149                 weak_factory_.GetWeakPtr(),
150                 callback,
151                 __func__));
152}
153
154void ChromeosModemGSMCardProxy::SendPIN(const string& pin,
155                                        Error* error,
156                                        const ResultCallback& callback,
157                                        int timeout) {
158  // pin is intentionally not logged.
159  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
160  proxy_->SendPinAsync(
161      pin,
162      base::Bind(&ChromeosModemGSMCardProxy::OnOperationSuccess,
163                 weak_factory_.GetWeakPtr(),
164                 callback,
165                 __func__),
166      base::Bind(&ChromeosModemGSMCardProxy::OnOperationFailure,
167                 weak_factory_.GetWeakPtr(),
168                 callback,
169                 __func__));
170}
171
172void ChromeosModemGSMCardProxy::SendPUK(const string& puk, const string& pin,
173                                        Error* error,
174                                        const ResultCallback& callback,
175                                        int timeout) {
176  // pin is intentionally not logged.
177  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
178  proxy_->SendPukAsync(
179      puk,
180      pin,
181      base::Bind(&ChromeosModemGSMCardProxy::OnOperationSuccess,
182                 weak_factory_.GetWeakPtr(),
183                 callback,
184                 __func__),
185      base::Bind(&ChromeosModemGSMCardProxy::OnOperationFailure,
186                 weak_factory_.GetWeakPtr(),
187                 callback,
188                 __func__));
189}
190
191void ChromeosModemGSMCardProxy::ChangePIN(const string& old_pin,
192                                          const string& new_pin,
193                                          Error* error,
194                                          const ResultCallback& callback,
195                                          int timeout) {
196  // pin is intentionally not logged.
197  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
198  proxy_->SendPukAsync(
199      old_pin,
200      new_pin,
201      base::Bind(&ChromeosModemGSMCardProxy::OnOperationSuccess,
202                 weak_factory_.GetWeakPtr(),
203                 callback,
204                 __func__),
205      base::Bind(&ChromeosModemGSMCardProxy::OnOperationFailure,
206                 weak_factory_.GetWeakPtr(),
207                 callback,
208                 __func__));
209}
210
211uint32_t ChromeosModemGSMCardProxy::EnabledFacilityLocks() {
212  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
213  if (!properties_->enabled_facility_locks.GetAndBlock()) {
214    LOG(ERROR) << "Faild to get EnableFacilityLocks";
215    return 0;
216  }
217  return properties_->enabled_facility_locks.value();
218}
219
220void ChromeosModemGSMCardProxy::OnGetGSMIdentifierSuccess(
221    const GSMIdentifierCallback& callback,
222    const string& identifier_name,
223    const string& identifier_value) {
224  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << identifier_name
225                                    << " " << identifier_value;
226  callback.Run(identifier_value, Error());
227}
228
229void ChromeosModemGSMCardProxy::OnGetGSMIdentifierFailure(
230    const GSMIdentifierCallback& callback,
231    const string& identifier_name,
232    brillo::Error* dbus_error) {
233  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << identifier_name;
234  Error error;
235  CellularError::FromChromeosDBusError(dbus_error, &error);
236  callback.Run("", error);
237}
238
239void ChromeosModemGSMCardProxy::OnOperationSuccess(
240    const ResultCallback& callback,
241    const string& operation_name) {
242  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation_name;
243  callback.Run(Error());
244}
245
246void ChromeosModemGSMCardProxy::OnOperationFailure(
247    const ResultCallback& callback,
248    const string& operation_name,
249    brillo::Error* dbus_error) {
250  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation_name;
251  Error error;
252  CellularError::FromChromeosDBusError(dbus_error, &error);
253  callback.Run(error);
254}
255
256void ChromeosModemGSMCardProxy::OnPropertyChanged(
257    const string& property_name) {
258  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << property_name;
259}
260
261}  // namespace shill
262