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
32namespace OHOS {
33namespace Telephony {
34int32_t CellularDataClient::defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
35int32_t CellularDataClient::defaultCellularDataSimId_ = 0;
36CellularDataClient::CellularDataClient()
37{
38    if (callback_ == nullptr) {
39        callback_ = new DataSimAccountCallback();
40    }
41}
42
43CellularDataClient::~CellularDataClient()
44{
45    UnregisterSimAccountCallback();
46}
47
48bool CellularDataClient::IsValidSlotId(int32_t slotId)
49{
50    return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT));
51}
52
53sptr<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
81void 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
103bool CellularDataClient::IsConnect()
104{
105    sptr<ICellularDataManager> proxy = GetProxy();
106    return (proxy != nullptr);
107}
108
109void 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
130void 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
141int32_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
156int32_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
175int32_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
195int32_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
209int32_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
219int32_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
229int32_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
239int32_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
249int32_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
259int32_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
269int32_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
279int32_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
289int32_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
299int32_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
309int32_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
319int32_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
329int32_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
339int32_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
349int32_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
359int32_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
369int32_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
379int32_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
389bool 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
404int32_t CellularDataClient::EstablishAllApnsIfConnectable(int32_t slotId)
405{
406    sptr<ICellularDataManager> proxy = GetProxy();
407    if (proxy == nullptr) {
408        TELEPHONY_LOGE("proxy is null");
409        return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
410    }
411    return proxy->EstablishAllApnsIfConnectable(slotId);
412}
413
414int32_t CellularDataClient::ReleaseCellularDataConnection(int32_t slotId)
415{
416    sptr<ICellularDataManager> proxy = GetProxy();
417    if (proxy == nullptr) {
418        TELEPHONY_LOGE("proxy is null");
419        return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
420    }
421    return proxy->ReleaseCellularDataConnection(slotId);
422}
423
424int32_t CellularDataClient::GetCellularDataSupplierId(int32_t slotId, uint64_t capability, uint32_t &supplierId)
425{
426    sptr<ICellularDataManager> proxy = GetProxy();
427    if (proxy == nullptr) {
428        TELEPHONY_LOGE("proxy is null");
429        return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
430    }
431    return proxy->GetCellularDataSupplierId(slotId, capability, supplierId);
432}
433
434int32_t CellularDataClient::CorrectNetSupplierNoAvailable(int32_t slotId)
435{
436    sptr<ICellularDataManager> proxy = GetProxy();
437    if (proxy == nullptr) {
438        TELEPHONY_LOGE("proxy is null");
439        return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
440    }
441    return proxy->CorrectNetSupplierNoAvailable(slotId);
442}
443
444int32_t CellularDataClient::GetSupplierRegisterState(uint32_t supplierId, int32_t &regState)
445{
446    sptr<ICellularDataManager> proxy = GetProxy();
447    if (proxy == nullptr) {
448        TELEPHONY_LOGE("proxy is null");
449        return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
450    }
451    return proxy->GetSupplierRegisterState(supplierId, regState);
452}
453} // namespace Telephony
454} // namespace OHOS
455