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_modemcdma_proxy.h"
18 
19 #include <memory>
20 
21 #include "shill/cellular/cellular_error.h"
22 #include "shill/logging.h"
23 
24 using std::string;
25 
26 namespace shill {
27 
28 namespace Logging {
29 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(const dbus::ObjectPath * p)30 static string ObjectID(const dbus::ObjectPath* p) { return p->value(); }
31 }  // namespace Logging
32 
33 namespace mm1 {
34 
ChromeosModemModemCdmaProxy(const scoped_refptr<dbus::Bus> & bus,const string & path,const string & service)35 ChromeosModemModemCdmaProxy::ChromeosModemModemCdmaProxy(
36     const scoped_refptr<dbus::Bus>& bus,
37     const string& path,
38     const string& service)
39     : proxy_(
40         new org::freedesktop::ModemManager1::Modem::ModemCdmaProxy(
41             bus, service, dbus::ObjectPath(path))) {
42   // Register signal handlers.
43   proxy_->RegisterActivationStateChangedSignalHandler(
44       base::Bind(&ChromeosModemModemCdmaProxy::ActivationStateChanged,
45                  weak_factory_.GetWeakPtr()),
46       base::Bind(&ChromeosModemModemCdmaProxy::OnSignalConnected,
47                  weak_factory_.GetWeakPtr()));
48 }
49 
~ChromeosModemModemCdmaProxy()50 ChromeosModemModemCdmaProxy::~ChromeosModemModemCdmaProxy() {}
51 
Activate(const std::string & carrier,Error * error,const ResultCallback & callback,int timeout)52 void ChromeosModemModemCdmaProxy::Activate(const std::string& carrier,
53                                            Error* error,
54                                            const ResultCallback& callback,
55                                            int timeout) {
56   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << carrier;
57   proxy_->ActivateAsync(
58       carrier,
59       base::Bind(&ChromeosModemModemCdmaProxy::OnOperationSuccess,
60                  weak_factory_.GetWeakPtr(),
61                  callback,
62                  __func__),
63       base::Bind(&ChromeosModemModemCdmaProxy::OnOperationFailure,
64                  weak_factory_.GetWeakPtr(),
65                  callback,
66                  __func__));
67 }
68 
ActivateManual(const KeyValueStore & properties,Error * error,const ResultCallback & callback,int timeout)69 void ChromeosModemModemCdmaProxy::ActivateManual(
70     const KeyValueStore& properties,
71     Error* error,
72     const ResultCallback& callback,
73     int timeout) {
74   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
75   brillo::VariantDictionary properties_dict;
76   KeyValueStore::ConvertToVariantDictionary(properties, &properties_dict);
77   proxy_->ActivateManualAsync(
78       properties_dict,
79       base::Bind(&ChromeosModemModemCdmaProxy::OnOperationSuccess,
80                  weak_factory_.GetWeakPtr(),
81                  callback,
82                  __func__),
83       base::Bind(&ChromeosModemModemCdmaProxy::OnOperationFailure,
84                  weak_factory_.GetWeakPtr(),
85                  callback,
86                  __func__));
87 }
88 
ActivationStateChanged(uint32_t activation_state,uint32_t activation_error,const brillo::VariantDictionary & status_changes)89 void ChromeosModemModemCdmaProxy::ActivationStateChanged(
90     uint32_t activation_state,
91     uint32_t activation_error,
92     const brillo::VariantDictionary& status_changes) {
93   SLOG(&proxy_->GetObjectPath(), 2) << __func__;
94   if (activation_state_callback_.is_null()) {
95     return;
96   }
97   KeyValueStore status_store;
98   KeyValueStore::ConvertFromVariantDictionary(status_changes, &status_store);
99   activation_state_callback_.Run(activation_state,
100                                  activation_error,
101                                  status_store);
102 }
103 
OnOperationSuccess(const ResultCallback & callback,const string & operation)104 void ChromeosModemModemCdmaProxy::OnOperationSuccess(
105     const ResultCallback& callback, const string& operation) {
106   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation;
107   callback.Run(Error());
108 }
109 
OnOperationFailure(const ResultCallback & callback,const string & operation,brillo::Error * dbus_error)110 void ChromeosModemModemCdmaProxy::OnOperationFailure(
111     const ResultCallback& callback,
112     const string& operation,
113     brillo::Error* dbus_error) {
114   SLOG(&proxy_->GetObjectPath(), 2) << __func__ << ": " << operation;
115   Error error;
116   CellularError::FromMM1ChromeosDBusError(dbus_error, &error);
117   callback.Run(Error());
118 }
119 
OnSignalConnected(const string & interface_name,const string & signal_name,bool success)120 void ChromeosModemModemCdmaProxy::OnSignalConnected(
121     const string& interface_name, const string& signal_name, bool success) {
122   SLOG(&proxy_->GetObjectPath(), 2) << __func__
123       << "interface: " << interface_name
124              << " signal: " << signal_name << "success: " << success;
125   if (!success) {
126     LOG(ERROR) << "Failed to connect signal " << signal_name
127         << " to interface " << interface_name;
128   }
129 }
130 
131 }  // namespace mm1
132 }  // namespace shill
133