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_modem3gpp_proxy.h"
18
19#include <memory>
20
21#include "shill/cellular/cellular_error.h"
22#include "shill/logging.h"
23
24using std::string;
25
26namespace shill {
27
28namespace Logging {
29static auto kModuleLogScope = ScopeLogger::kDBus;
30static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
31}  // namespace Logging
32
33namespace mm1 {
34
35ChromeosModemModem3gppProxy::ChromeosModemModem3gppProxy(
36    const scoped_refptr<dbus::Bus>& bus,
37    const string& path,
38    const string& service)
39    : proxy_(
40        new org::freedesktop::ModemManager1::Modem::Modem3gppProxy(
41            bus, service, dbus::ObjectPath(path))) {}
42
43ChromeosModemModem3gppProxy::~ChromeosModemModem3gppProxy() {}
44
45void ChromeosModemModem3gppProxy::Register(const std::string& operator_id,
46                                           Error* error,
47                                           const ResultCallback& callback,
48                                           int timeout) {
49  SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operator_id;
50  proxy_->RegisterAsync(
51      operator_id,
52      base::Bind(&ChromeosModemModem3gppProxy::OnRegisterSuccess,
53                 weak_factory_.GetWeakPtr(),
54                 callback),
55      base::Bind(&ChromeosModemModem3gppProxy::OnRegisterFailure,
56                 weak_factory_.GetWeakPtr(),
57                 callback));
58}
59
60void ChromeosModemModem3gppProxy::Scan(Error* error,
61                                       const KeyValueStoresCallback& callback,
62                                       int timeout) {
63  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
64  proxy_->ScanAsync(base::Bind(&ChromeosModemModem3gppProxy::OnScanSuccess,
65                               weak_factory_.GetWeakPtr(),
66                               callback),
67                    base::Bind(&ChromeosModemModem3gppProxy::OnScanFailure,
68                               weak_factory_.GetWeakPtr(),
69                               callback));
70}
71
72void ChromeosModemModem3gppProxy::OnRegisterSuccess(
73    const ResultCallback& callback) {
74  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
75  callback.Run(Error());
76}
77
78void ChromeosModemModem3gppProxy::OnRegisterFailure(
79    const ResultCallback& callback, brillo::Error* dbus_error) {
80  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
81  Error error;
82  CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
83  callback.Run(error);
84}
85
86void ChromeosModemModem3gppProxy::OnScanSuccess(
87    const KeyValueStoresCallback& callback,
88    const std::vector<brillo::VariantDictionary>& results) {
89  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
90  std::vector<KeyValueStore> result_stores;
91  for (const auto& result : results) {
92    KeyValueStore result_store;
93    KeyValueStore::ConvertFromVariantDictionary(result, &result_store);
94    result_stores.push_back(result_store);
95  }
96  callback.Run(result_stores, Error());
97}
98
99void ChromeosModemModem3gppProxy::OnScanFailure(
100    const KeyValueStoresCallback& callback, brillo::Error* dbus_error) {
101  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
102  Error error;
103  CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
104  callback.Run(std::vector<KeyValueStore>(), error);
105}
106
107}  // namespace mm1
108}  // namespace shill
109