• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "cellular_data_client.h"
17 
18 #include "__mutex_base"
19 #include "cellular_data_types.h"
20 #include "i_cellular_data_manager.h"
21 #include "if_system_ability_manager.h"
22 #include "iremote_broker.h"
23 #include "iremote_object.h"
24 #include "iservice_registry.h"
25 #include "memory"
26 #include "refbase.h"
27 #include "system_ability_definition.h"
28 #include "telephony_errors.h"
29 #include "telephony_log_wrapper.h"
30 #include "telephony_types.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 int32_t CellularDataClient::defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
35 int32_t CellularDataClient::defaultCellularDataSimId_ = 0;
CellularDataClient()36 CellularDataClient::CellularDataClient()
37 {
38     if (callback_ == nullptr) {
39         callback_ = new DataSimAccountCallback();
40     }
41 }
42 
~CellularDataClient()43 CellularDataClient::~CellularDataClient()
44 {
45     UnregisterSimAccountCallback();
46 }
47 
IsValidSlotId(int32_t slotId)48 bool CellularDataClient::IsValidSlotId(int32_t slotId)
49 {
50     return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT));
51 }
52 
GetProxy()53 sptr<ICellularDataManager> CellularDataClient::GetProxy()
54 {
55     std::lock_guard<std::mutex> lock(mutexProxy_);
56     if (proxy_ != nullptr) {
57         return proxy_;
58     }
59 
60     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
61     if (sam == nullptr) {
62         TELEPHONY_LOGE("Failed to get system ability manager");
63         return nullptr;
64     }
65     sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
66     if (obj == nullptr) {
67         TELEPHONY_LOGE("Failed to get cellular data service");
68         return nullptr;
69     }
70     std::unique_ptr<CellularDataDeathRecipient> recipient = std::make_unique<CellularDataDeathRecipient>(*this);
71     if (recipient == nullptr) {
72         TELEPHONY_LOGE("recipient is null");
73         return nullptr;
74     }
75     sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
76     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
77         TELEPHONY_LOGE("Failed to add death recipient");
78         return nullptr;
79     }
80     proxy_ = iface_cast<ICellularDataManager>(obj);
81     deathRecipient_ = dr;
82     TELEPHONY_LOGD("Succeed to connect cellular data service %{public}d", proxy_ == nullptr);
83     return proxy_;
84 }
85 
OnRemoteDied(const wptr<IRemoteObject> & remote)86 void CellularDataClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
87 {
88     if (remote == nullptr) {
89         TELEPHONY_LOGE("remote is nullptr");
90         return;
91     }
92     std::lock_guard<std::mutex> lock(mutexProxy_);
93     if (proxy_ == nullptr) {
94         TELEPHONY_LOGE("proxy_ is nullptr");
95         return;
96     }
97     sptr<IRemoteObject> serviceRemote = proxy_->AsObject();
98     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
99         serviceRemote->RemoveDeathRecipient(deathRecipient_);
100         proxy_ = nullptr;
101         TELEPHONY_LOGE("on remote died");
102     }
103 }
104 
IsConnect()105 bool CellularDataClient::IsConnect()
106 {
107     sptr<ICellularDataManager> proxy = GetProxy();
108     return (proxy != nullptr);
109 }
110 
RegisterSimAccountCallback()111 void CellularDataClient::RegisterSimAccountCallback()
112 {
113     if (callback_ == nullptr) {
114         TELEPHONY_LOGE("callback_ is nullptr");
115         return;
116     }
117     if (registerStatus_) {
118         return;
119     }
120     sptr<ICellularDataManager> proxy = GetProxy();
121     if (proxy == nullptr) {
122         TELEPHONY_LOGE("proxy is null");
123         return;
124     }
125     int32_t ret = proxy->RegisterSimAccountCallback(callback_);
126     TELEPHONY_LOGD("ret:%{public}d", ret);
127     if (ret == TELEPHONY_ERR_SUCCESS) {
128         registerStatus_ = true;
129     }
130 }
131 
UnregisterSimAccountCallback()132 void CellularDataClient::UnregisterSimAccountCallback()
133 {
134     sptr<ICellularDataManager> proxy = GetProxy();
135     if (proxy == nullptr) {
136         TELEPHONY_LOGE("proxy is null");
137         return;
138     }
139     int32_t ret = proxy->UnregisterSimAccountCallback();
140     TELEPHONY_LOGD("ret:%{public}d", ret);
141 }
142 
GetDefaultCellularDataSlotId()143 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
144 {
145     RegisterSimAccountCallback();
146     if (IsValidSlotId(defaultCellularDataSlotId_)) {
147         return defaultCellularDataSlotId_;
148     }
149     sptr<ICellularDataManager> proxy = GetProxy();
150     if (proxy == nullptr) {
151         TELEPHONY_LOGE("proxy is null");
152         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
153     }
154     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
155     return defaultCellularDataSlotId_;
156 }
157 
GetDefaultCellularDataSimId(int32_t & simId)158 int32_t CellularDataClient::GetDefaultCellularDataSimId(int32_t &simId)
159 {
160     RegisterSimAccountCallback();
161     if (defaultCellularDataSimId_ > 0) {
162         simId = defaultCellularDataSimId_;
163         return TELEPHONY_ERR_SUCCESS;
164     }
165     sptr<ICellularDataManager> proxy = GetProxy();
166     if (proxy == nullptr) {
167         TELEPHONY_LOGE("proxy is null");
168         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
169     }
170     int32_t result = proxy->GetDefaultCellularDataSimId(simId);
171     if (result == TELEPHONY_ERR_SUCCESS) {
172         defaultCellularDataSimId_ = simId;
173     }
174     return result;
175 }
176 
SetDefaultCellularDataSlotId(int32_t slotId)177 int32_t CellularDataClient::SetDefaultCellularDataSlotId(int32_t slotId)
178 {
179     RegisterSimAccountCallback();
180     sptr<ICellularDataManager> proxy = GetProxy();
181     if (proxy == nullptr) {
182         TELEPHONY_LOGE("proxy is null");
183         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
184     }
185     int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
186     if (result == TELEPHONY_ERR_SUCCESS) {
187         defaultCellularDataSlotId_ = slotId;
188         int32_t simId = 0;
189         int32_t ret = proxy->GetDefaultCellularDataSimId(simId);
190         if (ret == TELEPHONY_ERR_SUCCESS) {
191             defaultCellularDataSimId_ = simId;
192         }
193     }
194     return result;
195 }
196 
UpdateDefaultCellularDataSlotId()197 int32_t CellularDataClient::UpdateDefaultCellularDataSlotId()
198 {
199     defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
200     defaultCellularDataSimId_ = 0;
201     sptr<ICellularDataManager> proxy = GetProxy();
202     if (proxy == nullptr) {
203         TELEPHONY_LOGE("proxy is null");
204         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
205     }
206     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
207     proxy->GetDefaultCellularDataSimId(defaultCellularDataSimId_);
208     return defaultCellularDataSlotId_;
209 }
210 
EnableCellularData(bool enable)211 int32_t CellularDataClient::EnableCellularData(bool enable)
212 {
213     sptr<ICellularDataManager> proxy = GetProxy();
214     if (proxy == nullptr) {
215         TELEPHONY_LOGE("proxy is null");
216         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
217     }
218     return proxy->EnableCellularData(enable);
219 }
220 
EnableIntelligenceSwitch(bool enable)221 int32_t CellularDataClient::EnableIntelligenceSwitch(bool enable)
222 {
223     sptr<ICellularDataManager> proxy = GetProxy();
224     if (proxy == nullptr) {
225         TELEPHONY_LOGE("proxy is null");
226         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
227     }
228     return proxy->EnableIntelligenceSwitch(enable);
229 }
230 
IsCellularDataEnabled(bool & dataEnabled)231 int32_t CellularDataClient::IsCellularDataEnabled(bool &dataEnabled)
232 {
233     sptr<ICellularDataManager> proxy = GetProxy();
234     if (proxy == nullptr) {
235         TELEPHONY_LOGE("proxy is null");
236         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
237     }
238     return proxy->IsCellularDataEnabled(dataEnabled);
239 }
240 
GetCellularDataState()241 int32_t CellularDataClient::GetCellularDataState()
242 {
243     sptr<ICellularDataManager> proxy = GetProxy();
244     if (proxy == nullptr) {
245         TELEPHONY_LOGE("proxy is null");
246         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
247     }
248     return proxy->GetCellularDataState();
249 }
250 
GetApnState(int32_t slotId,const std::string & apnType)251 int32_t CellularDataClient::GetApnState(int32_t slotId, const std::string &apnType)
252 {
253     sptr<ICellularDataManager> proxy = GetProxy();
254     if (proxy == nullptr) {
255         TELEPHONY_LOGE("proxy is null");
256         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
257     }
258     return proxy->GetApnState(slotId, apnType);
259 }
260 
GetDataRecoveryState()261 int32_t CellularDataClient::GetDataRecoveryState()
262 {
263     sptr<ICellularDataManager> proxy = GetProxy();
264     if (proxy == nullptr) {
265         TELEPHONY_LOGE("proxy is null");
266         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
267     }
268     return proxy->GetDataRecoveryState();
269 }
270 
IsCellularDataRoamingEnabled(int32_t slotId,bool & dataRoamingEnabled)271 int32_t CellularDataClient::IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled)
272 {
273     sptr<ICellularDataManager> proxy = GetProxy();
274     if (proxy == nullptr) {
275         TELEPHONY_LOGE("proxy is null");
276         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
277     }
278     return proxy->IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
279 }
280 
EnableCellularDataRoaming(int32_t slotId,bool enable)281 int32_t CellularDataClient::EnableCellularDataRoaming(int32_t slotId, bool enable)
282 {
283     sptr<ICellularDataManager> proxy = GetProxy();
284     if (proxy == nullptr) {
285         TELEPHONY_LOGE("proxy is null");
286         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
287     }
288     return proxy->EnableCellularDataRoaming(slotId, enable);
289 }
290 
GetCellularDataFlowType()291 int32_t CellularDataClient::GetCellularDataFlowType()
292 {
293     sptr<ICellularDataManager> proxy = GetProxy();
294     if (proxy == nullptr) {
295         TELEPHONY_LOGE("proxy is null");
296         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
297     }
298     return proxy->GetCellularDataFlowType();
299 }
300 
HasInternetCapability(int32_t slotId,int32_t cid)301 int32_t CellularDataClient::HasInternetCapability(int32_t slotId, int32_t cid)
302 {
303     sptr<ICellularDataManager> proxy = GetProxy();
304     if (proxy == nullptr) {
305         TELEPHONY_LOGE("proxy is null");
306         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
307     }
308     return proxy->HasInternetCapability(slotId, cid);
309 }
310 
ClearCellularDataConnections(int32_t slotId)311 int32_t CellularDataClient::ClearCellularDataConnections(int32_t slotId)
312 {
313     sptr<ICellularDataManager> proxy = GetProxy();
314     if (proxy == nullptr) {
315         TELEPHONY_LOGE("proxy is null");
316         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
317     }
318     return proxy->ClearCellularDataConnections(slotId);
319 }
320 
GetDataConnApnAttr(int32_t slotId,ApnItem::Attribute & apnAttr)321 int32_t CellularDataClient::GetDataConnApnAttr(int32_t slotId, ApnItem::Attribute &apnAttr)
322 {
323     sptr<ICellularDataManager> proxy = GetProxy();
324     if (proxy == nullptr) {
325         TELEPHONY_LOGE("proxy is null");
326         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
327     }
328     return proxy->GetDataConnApnAttr(slotId, apnAttr);
329 }
330 
GetDataConnIpType(int32_t slotId,std::string & ipType)331 int32_t CellularDataClient::GetDataConnIpType(int32_t slotId, std::string &ipType)
332 {
333     sptr<ICellularDataManager> proxy = GetProxy();
334     if (proxy == nullptr) {
335         TELEPHONY_LOGE("proxy is null");
336         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
337     }
338     return proxy->GetDataConnIpType(slotId, ipType);
339 }
340 
ClearAllConnections(int32_t slotId,DisConnectionReason reason)341 int32_t CellularDataClient::ClearAllConnections(int32_t slotId, DisConnectionReason reason)
342 {
343     sptr<ICellularDataManager> proxy = GetProxy();
344     if (proxy == nullptr) {
345         TELEPHONY_LOGE("proxy is null");
346         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
347     }
348     return proxy->ClearAllConnections(slotId, reason);
349 }
350 
HandleApnChanged(int32_t slotId)351 int32_t CellularDataClient::HandleApnChanged(int32_t slotId)
352 {
353     sptr<ICellularDataManager> proxy = GetProxy();
354     if (proxy == nullptr) {
355         TELEPHONY_LOGE("proxy is null");
356         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
357     }
358     return proxy->HandleApnChanged(slotId);
359 }
360 
IsNeedDoRecovery(int32_t slotId,bool needDoRecovery)361 int32_t CellularDataClient::IsNeedDoRecovery(int32_t slotId, bool needDoRecovery)
362 {
363     sptr<ICellularDataManager> proxy = GetProxy();
364     if (proxy == nullptr) {
365         TELEPHONY_LOGE("proxy is null");
366         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
367     }
368     return proxy->IsNeedDoRecovery(slotId, needDoRecovery);
369 }
370 } // namespace Telephony
371 } // namespace OHOS
372