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<IRemoteObject> obj;
61     if (!IsCellularDataSysAbilityExist(obj)) {
62         TELEPHONY_LOGE("Failed to get cellular data service");
63         return nullptr;
64     }
65     std::unique_ptr<CellularDataDeathRecipient> recipient = std::make_unique<CellularDataDeathRecipient>(*this);
66     if (recipient == nullptr) {
67         TELEPHONY_LOGE("recipient is null");
68         return nullptr;
69     }
70     sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
71     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
72         TELEPHONY_LOGE("Failed to add death recipient");
73         return nullptr;
74     }
75     proxy_ = iface_cast<ICellularDataManager>(obj);
76     deathRecipient_ = dr;
77     TELEPHONY_LOGD("Succeed to connect cellular data service %{public}d", proxy_ == nullptr);
78     return proxy_;
79 }
80 
OnRemoteDied(const wptr<IRemoteObject> & remote)81 void CellularDataClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
82 {
83     if (remote == nullptr) {
84         TELEPHONY_LOGE("remote is nullptr");
85         return;
86     }
87     std::lock_guard<std::mutex> lock(mutexProxy_);
88     if (proxy_ == nullptr) {
89         TELEPHONY_LOGE("proxy_ is nullptr");
90         return;
91     }
92     sptr<IRemoteObject> serviceRemote = proxy_->AsObject();
93     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
94         serviceRemote->RemoveDeathRecipient(deathRecipient_);
95         proxy_ = nullptr;
96         defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
97         defaultCellularDataSimId_ = 0;
98         registerStatus_ = false;
99         TELEPHONY_LOGE("on remote died");
100     }
101 }
102 
IsConnect()103 bool CellularDataClient::IsConnect()
104 {
105     sptr<ICellularDataManager> proxy = GetProxy();
106     return (proxy != nullptr);
107 }
108 
RegisterSimAccountCallback()109 void CellularDataClient::RegisterSimAccountCallback()
110 {
111     if (callback_ == nullptr) {
112         TELEPHONY_LOGE("callback_ is nullptr");
113         return;
114     }
115     if (registerStatus_) {
116         return;
117     }
118     sptr<ICellularDataManager> proxy = GetProxy();
119     if (proxy == nullptr) {
120         TELEPHONY_LOGE("proxy is null");
121         return;
122     }
123     int32_t ret = proxy->RegisterSimAccountCallback(callback_);
124     TELEPHONY_LOGD("ret:%{public}d", ret);
125     if (ret == TELEPHONY_ERR_SUCCESS) {
126         registerStatus_ = true;
127     }
128 }
129 
UnregisterSimAccountCallback()130 void CellularDataClient::UnregisterSimAccountCallback()
131 {
132     sptr<ICellularDataManager> proxy = GetProxy();
133     if (proxy == nullptr) {
134         TELEPHONY_LOGE("proxy is null");
135         return;
136     }
137     int32_t ret = proxy->UnregisterSimAccountCallback();
138     TELEPHONY_LOGD("ret:%{public}d", ret);
139 }
140 
GetDefaultCellularDataSlotId()141 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
142 {
143     RegisterSimAccountCallback();
144     if (IsValidSlotId(defaultCellularDataSlotId_)) {
145         return defaultCellularDataSlotId_;
146     }
147     sptr<ICellularDataManager> proxy = GetProxy();
148     if (proxy == nullptr) {
149         TELEPHONY_LOGE("proxy is null");
150         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
151     }
152     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
153     return defaultCellularDataSlotId_;
154 }
155 
GetDefaultCellularDataSimId(int32_t & simId)156 int32_t CellularDataClient::GetDefaultCellularDataSimId(int32_t &simId)
157 {
158     RegisterSimAccountCallback();
159     if (defaultCellularDataSimId_ > 0) {
160         simId = defaultCellularDataSimId_;
161         return TELEPHONY_ERR_SUCCESS;
162     }
163     sptr<ICellularDataManager> proxy = GetProxy();
164     if (proxy == nullptr) {
165         TELEPHONY_LOGE("proxy is null");
166         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
167     }
168     int32_t result = proxy->GetDefaultCellularDataSimId(simId);
169     if (result == TELEPHONY_ERR_SUCCESS) {
170         defaultCellularDataSimId_ = simId;
171     }
172     return result;
173 }
174 
SetDefaultCellularDataSlotId(int32_t slotId)175 int32_t CellularDataClient::SetDefaultCellularDataSlotId(int32_t slotId)
176 {
177     RegisterSimAccountCallback();
178     sptr<ICellularDataManager> proxy = GetProxy();
179     if (proxy == nullptr) {
180         TELEPHONY_LOGE("proxy is null");
181         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
182     }
183     int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
184     if (result == TELEPHONY_ERR_SUCCESS) {
185         defaultCellularDataSlotId_ = slotId;
186         int32_t simId = 0;
187         int32_t ret = proxy->GetDefaultCellularDataSimId(simId);
188         if (ret == TELEPHONY_ERR_SUCCESS) {
189             defaultCellularDataSimId_ = simId;
190         }
191     }
192     return result;
193 }
194 
UpdateDefaultCellularDataSlotId()195 int32_t CellularDataClient::UpdateDefaultCellularDataSlotId()
196 {
197     defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
198     defaultCellularDataSimId_ = 0;
199     sptr<ICellularDataManager> proxy = GetProxy();
200     if (proxy == nullptr) {
201         TELEPHONY_LOGE("proxy is null");
202         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
203     }
204     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
205     proxy->GetDefaultCellularDataSimId(defaultCellularDataSimId_);
206     return defaultCellularDataSlotId_;
207 }
208 
EnableCellularData(bool enable)209 int32_t CellularDataClient::EnableCellularData(bool enable)
210 {
211     sptr<ICellularDataManager> proxy = GetProxy();
212     if (proxy == nullptr) {
213         TELEPHONY_LOGE("proxy is null");
214         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
215     }
216     return proxy->EnableCellularData(enable);
217 }
218 
EnableIntelligenceSwitch(bool enable)219 int32_t CellularDataClient::EnableIntelligenceSwitch(bool enable)
220 {
221     sptr<ICellularDataManager> proxy = GetProxy();
222     if (proxy == nullptr) {
223         TELEPHONY_LOGE("proxy is null");
224         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
225     }
226     return proxy->EnableIntelligenceSwitch(enable);
227 }
228 
IsCellularDataEnabled(bool & dataEnabled)229 int32_t CellularDataClient::IsCellularDataEnabled(bool &dataEnabled)
230 {
231     sptr<ICellularDataManager> proxy = GetProxy();
232     if (proxy == nullptr) {
233         TELEPHONY_LOGE("proxy is null");
234         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
235     }
236     return proxy->IsCellularDataEnabled(dataEnabled);
237 }
238 
GetCellularDataState()239 int32_t CellularDataClient::GetCellularDataState()
240 {
241     sptr<ICellularDataManager> proxy = GetProxy();
242     if (proxy == nullptr) {
243         TELEPHONY_LOGE("proxy is null");
244         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
245     }
246     return proxy->GetCellularDataState();
247 }
248 
GetApnState(int32_t slotId,const std::string & apnType)249 int32_t CellularDataClient::GetApnState(int32_t slotId, const std::string &apnType)
250 {
251     sptr<ICellularDataManager> proxy = GetProxy();
252     if (proxy == nullptr) {
253         TELEPHONY_LOGE("proxy is null");
254         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
255     }
256     return proxy->GetApnState(slotId, apnType);
257 }
258 
GetDataRecoveryState()259 int32_t CellularDataClient::GetDataRecoveryState()
260 {
261     sptr<ICellularDataManager> proxy = GetProxy();
262     if (proxy == nullptr) {
263         TELEPHONY_LOGE("proxy is null");
264         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
265     }
266     return proxy->GetDataRecoveryState();
267 }
268 
IsCellularDataRoamingEnabled(int32_t slotId,bool & dataRoamingEnabled)269 int32_t CellularDataClient::IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled)
270 {
271     sptr<ICellularDataManager> proxy = GetProxy();
272     if (proxy == nullptr) {
273         TELEPHONY_LOGE("proxy is null");
274         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
275     }
276     return proxy->IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
277 }
278 
EnableCellularDataRoaming(int32_t slotId,bool enable)279 int32_t CellularDataClient::EnableCellularDataRoaming(int32_t slotId, bool enable)
280 {
281     sptr<ICellularDataManager> proxy = GetProxy();
282     if (proxy == nullptr) {
283         TELEPHONY_LOGE("proxy is null");
284         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
285     }
286     return proxy->EnableCellularDataRoaming(slotId, enable);
287 }
288 
GetCellularDataFlowType()289 int32_t CellularDataClient::GetCellularDataFlowType()
290 {
291     sptr<ICellularDataManager> proxy = GetProxy();
292     if (proxy == nullptr) {
293         TELEPHONY_LOGE("proxy is null");
294         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
295     }
296     return proxy->GetCellularDataFlowType();
297 }
298 
HasInternetCapability(int32_t slotId,int32_t cid)299 int32_t CellularDataClient::HasInternetCapability(int32_t slotId, int32_t cid)
300 {
301     sptr<ICellularDataManager> proxy = GetProxy();
302     if (proxy == nullptr) {
303         TELEPHONY_LOGE("proxy is null");
304         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
305     }
306     return proxy->HasInternetCapability(slotId, cid);
307 }
308 
ClearCellularDataConnections(int32_t slotId)309 int32_t CellularDataClient::ClearCellularDataConnections(int32_t slotId)
310 {
311     sptr<ICellularDataManager> proxy = GetProxy();
312     if (proxy == nullptr) {
313         TELEPHONY_LOGE("proxy is null");
314         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
315     }
316     return proxy->ClearCellularDataConnections(slotId);
317 }
318 
GetDataConnApnAttr(int32_t slotId,ApnItem::Attribute & apnAttr)319 int32_t CellularDataClient::GetDataConnApnAttr(int32_t slotId, ApnItem::Attribute &apnAttr)
320 {
321     sptr<ICellularDataManager> proxy = GetProxy();
322     if (proxy == nullptr) {
323         TELEPHONY_LOGE("proxy is null");
324         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
325     }
326     return proxy->GetDataConnApnAttr(slotId, apnAttr);
327 }
328 
GetDataConnIpType(int32_t slotId,std::string & ipType)329 int32_t CellularDataClient::GetDataConnIpType(int32_t slotId, std::string &ipType)
330 {
331     sptr<ICellularDataManager> proxy = GetProxy();
332     if (proxy == nullptr) {
333         TELEPHONY_LOGE("proxy is null");
334         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
335     }
336     return proxy->GetDataConnIpType(slotId, ipType);
337 }
338 
ClearAllConnections(int32_t slotId,DisConnectionReason reason)339 int32_t CellularDataClient::ClearAllConnections(int32_t slotId, DisConnectionReason reason)
340 {
341     sptr<ICellularDataManager> proxy = GetProxy();
342     if (proxy == nullptr) {
343         TELEPHONY_LOGE("proxy is null");
344         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
345     }
346     return proxy->ClearAllConnections(slotId, reason);
347 }
348 
HandleApnChanged(int32_t slotId)349 int32_t CellularDataClient::HandleApnChanged(int32_t slotId)
350 {
351     sptr<ICellularDataManager> proxy = GetProxy();
352     if (proxy == nullptr) {
353         TELEPHONY_LOGE("proxy is null");
354         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
355     }
356     return proxy->HandleApnChanged(slotId);
357 }
358 
IsNeedDoRecovery(int32_t slotId,bool needDoRecovery)359 int32_t CellularDataClient::IsNeedDoRecovery(int32_t slotId, bool needDoRecovery)
360 {
361     sptr<ICellularDataManager> proxy = GetProxy();
362     if (proxy == nullptr) {
363         TELEPHONY_LOGE("proxy is null");
364         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
365     }
366     return proxy->IsNeedDoRecovery(slotId, needDoRecovery);
367 }
368 
InitCellularDataController(int32_t slotId)369 int32_t CellularDataClient::InitCellularDataController(int32_t slotId)
370 {
371     sptr<ICellularDataManager> proxy = GetProxy();
372     if (proxy == nullptr) {
373         TELEPHONY_LOGE("proxy is null");
374         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
375     }
376     return proxy->InitCellularDataController(slotId);
377 }
378 
GetIntelligenceSwitchState(bool & switchState)379 int32_t CellularDataClient::GetIntelligenceSwitchState(bool &switchState)
380 {
381     sptr<ICellularDataManager> proxy = GetProxy();
382     if (proxy == nullptr) {
383         TELEPHONY_LOGE("proxy is null");
384         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
385     }
386     return proxy->GetIntelligenceSwitchState(switchState);
387 }
388 
IsCellularDataSysAbilityExist(sptr<IRemoteObject> & object)389 bool CellularDataClient::IsCellularDataSysAbilityExist(sptr<IRemoteObject> &object) __attribute__((no_sanitize("cfi")))
390 {
391     sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
392     if (sm == nullptr) {
393         TELEPHONY_LOGE("IsCellularDataSysAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
394         return false;
395     }
396     object = sm->CheckSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
397     if (object == nullptr) {
398         TELEPHONY_LOGE("No CesServiceAbility");
399         return false;
400     }
401     return true;
402 }
403 } // namespace Telephony
404 } // namespace OHOS
405