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_simple_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
27using std::string;
28
29namespace shill {
30
31namespace Logging {
32static auto kModuleLogScope = ScopeLogger::kDBus;
33static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
34}
35
36ChromeosModemSimpleProxy::ChromeosModemSimpleProxy(
37    const scoped_refptr<dbus::Bus>& bus,
38    const string& path,
39    const string& service)
40    : proxy_(
41        new org::freedesktop::ModemManager::Modem::SimpleProxy(
42            bus, service, dbus::ObjectPath(path))) {}
43
44ChromeosModemSimpleProxy::~ChromeosModemSimpleProxy() {}
45
46void ChromeosModemSimpleProxy::GetModemStatus(
47    Error* error, const KeyValueStoreCallback& callback, int timeout) {
48  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
49  proxy_->GetStatusAsync(
50      base::Bind(&ChromeosModemSimpleProxy::OnGetStatusSuccess,
51                 weak_factory_.GetWeakPtr(),
52                 callback),
53      base::Bind(&ChromeosModemSimpleProxy::OnGetStatusFailure,
54                 weak_factory_.GetWeakPtr(),
55                 callback));
56}
57
58void ChromeosModemSimpleProxy::Connect(const KeyValueStore& properties,
59                                       Error* error,
60                                       const ResultCallback& callback,
61                                       int timeout) {
62  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
63  brillo::VariantDictionary properties_dict;
64  KeyValueStore::ConvertToVariantDictionary(properties, &properties_dict);
65  proxy_->ConnectAsync(
66      properties_dict,
67      base::Bind(&ChromeosModemSimpleProxy::OnConnectSuccess,
68                 weak_factory_.GetWeakPtr(),
69                 callback),
70      base::Bind(&ChromeosModemSimpleProxy::OnConnectFailure,
71                 weak_factory_.GetWeakPtr(),
72                 callback));
73}
74
75void ChromeosModemSimpleProxy::OnGetStatusSuccess(
76    const KeyValueStoreCallback& callback,
77    const brillo::VariantDictionary& props) {
78  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
79  KeyValueStore props_store;
80  KeyValueStore::ConvertFromVariantDictionary(props, &props_store);
81  callback.Run(props_store, Error());
82}
83
84void ChromeosModemSimpleProxy::OnGetStatusFailure(
85    const KeyValueStoreCallback& callback, brillo::Error* dbus_error) {
86  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
87  Error error;
88  CellularError::FromChromeosDBusError(dbus_error, &error);
89  callback.Run(KeyValueStore(), error);
90}
91
92void ChromeosModemSimpleProxy::OnConnectSuccess(
93    const ResultCallback& callback) {
94  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
95  callback.Run(Error());
96}
97
98void ChromeosModemSimpleProxy::OnConnectFailure(
99    const ResultCallback& callback, brillo::Error* dbus_error) {
100  SLOG(&proxy_->GetObjectPath(), 2) << __func__;
101  Error error;
102  CellularError::FromChromeosDBusError(dbus_error, &error);
103  callback.Run(error);
104}
105
106}  // namespace shill
107