• 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 {
CellularDataClient()34 CellularDataClient::CellularDataClient()
35 {
36     defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
37     if (callback_ == nullptr) {
38         callback_ = new DataSimAccountCallback();
39     }
40 }
41 
~CellularDataClient()42 CellularDataClient::~CellularDataClient()
43 {
44     UnregisterSimAccountCallback();
45 }
46 
GetProxy()47 sptr<ICellularDataManager> CellularDataClient::GetProxy()
48 {
49     std::lock_guard<std::mutex> lock(mutexProxy_);
50     if (proxy_ != nullptr) {
51         return proxy_;
52     }
53 
54     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
55     if (sam == nullptr) {
56         TELEPHONY_LOGE("Failed to get system ability manager");
57         return nullptr;
58     }
59     sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
60     if (obj == nullptr) {
61         TELEPHONY_LOGE("Failed to get cellular data service");
62         return nullptr;
63     }
64     std::unique_ptr<CellularDataDeathRecipient> recipient = std::make_unique<CellularDataDeathRecipient>(*this);
65     if (recipient == nullptr) {
66         TELEPHONY_LOGE("recipient is null");
67         return nullptr;
68     }
69     sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
70     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
71         TELEPHONY_LOGE("Failed to add death recipient");
72         return nullptr;
73     }
74     proxy_ = iface_cast<ICellularDataManager>(obj);
75     deathRecipient_ = dr;
76     TELEPHONY_LOGI("Succeed to connect cellular data service %{public}d", proxy_ == nullptr);
77     return proxy_;
78 }
79 
OnRemoteDied(const wptr<IRemoteObject> & remote)80 void CellularDataClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
81 {
82     if (remote == nullptr) {
83         TELEPHONY_LOGE("OnRemoteDied failed, remote is nullptr");
84         return;
85     }
86     std::lock_guard<std::mutex> lock(mutexProxy_);
87     if (proxy_ == nullptr) {
88         TELEPHONY_LOGE("OnRemoteDied proxy_ is nullptr");
89         return;
90     }
91     sptr<IRemoteObject> serviceRemote = proxy_->AsObject();
92     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
93         serviceRemote->RemoveDeathRecipient(deathRecipient_);
94         proxy_ = nullptr;
95         TELEPHONY_LOGE("on remote died");
96     }
97 }
98 
IsConnect() const99 bool CellularDataClient::IsConnect() const
100 {
101     return (proxy_ != nullptr);
102 }
103 
RegisterSimAccountCallback()104 void CellularDataClient::RegisterSimAccountCallback()
105 {
106     if (callback_ == nullptr) {
107         TELEPHONY_LOGE("callback_ is nullptr");
108         return;
109     }
110     if (registerStatus_) {
111         return;
112     }
113     sptr<ICellularDataManager> proxy = GetProxy();
114     if (proxy == nullptr) {
115         TELEPHONY_LOGE("proxy is null");
116         return;
117     }
118     int32_t ret = proxy->RegisterSimAccountCallback(callback_);
119     TELEPHONY_LOGI("CellularDataClient::RegisterSimAccountCallback ret:%{public}d", ret);
120     if (ret == TELEPHONY_ERR_SUCCESS) {
121         registerStatus_ = true;
122     }
123 }
124 
UnregisterSimAccountCallback()125 void CellularDataClient::UnregisterSimAccountCallback()
126 {
127     sptr<ICellularDataManager> proxy = GetProxy();
128     if (proxy == nullptr) {
129         TELEPHONY_LOGE("proxy is null");
130         return;
131     }
132     int32_t ret = proxy->UnregisterSimAccountCallback();
133     TELEPHONY_LOGI("CellularDataClient::UnregisterSimAccountCallback ret:%{public}d", ret);
134 }
135 
GetDefaultCellularDataSlotId()136 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
137 {
138     RegisterSimAccountCallback();
139     if (defaultCellularDataSlotId_ != INVALID_MAIN_CARD_SLOTID) {
140         return defaultCellularDataSlotId_;
141     }
142     sptr<ICellularDataManager> proxy = GetProxy();
143     if (proxy == nullptr) {
144         TELEPHONY_LOGE("proxy is null");
145         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
146     }
147     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
148     return defaultCellularDataSlotId_;
149 }
150 
SetDefaultCellularDataSlotId(int32_t slotId)151 int32_t CellularDataClient::SetDefaultCellularDataSlotId(int32_t slotId)
152 {
153     RegisterSimAccountCallback();
154     sptr<ICellularDataManager> proxy = GetProxy();
155     if (proxy == nullptr) {
156         TELEPHONY_LOGE("proxy is null");
157         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
158     }
159     int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
160     if (result == TELEPHONY_ERR_SUCCESS) {
161         defaultCellularDataSlotId_ = slotId;
162     }
163     return result;
164 }
165 
UpdateDefaultCellularDataSlotId()166 int32_t CellularDataClient::UpdateDefaultCellularDataSlotId()
167 {
168     defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
169     sptr<ICellularDataManager> proxy = GetProxy();
170     if (proxy == nullptr) {
171         TELEPHONY_LOGE("proxy is null");
172         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
173     }
174     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
175     return defaultCellularDataSlotId_;
176 }
177 
EnableCellularData(bool enable)178 int32_t CellularDataClient::EnableCellularData(bool enable)
179 {
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     return proxy->EnableCellularData(enable);
186 }
187 
IsCellularDataEnabled(bool & dataEnabled)188 int32_t CellularDataClient::IsCellularDataEnabled(bool &dataEnabled)
189 {
190     sptr<ICellularDataManager> proxy = GetProxy();
191     if (proxy == nullptr) {
192         TELEPHONY_LOGE("proxy is null");
193         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
194     }
195     return proxy->IsCellularDataEnabled(dataEnabled);
196 }
197 
GetCellularDataState()198 int32_t CellularDataClient::GetCellularDataState()
199 {
200     sptr<ICellularDataManager> proxy = GetProxy();
201     if (proxy == nullptr) {
202         TELEPHONY_LOGE("proxy is null");
203         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
204     }
205     return proxy->GetCellularDataState();
206 }
207 
IsCellularDataRoamingEnabled(int32_t slotId,bool & dataRoamingEnabled)208 int32_t CellularDataClient::IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled)
209 {
210     sptr<ICellularDataManager> proxy = GetProxy();
211     if (proxy == nullptr) {
212         TELEPHONY_LOGE("proxy is null");
213         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
214     }
215     return proxy->IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
216 }
217 
EnableCellularDataRoaming(int32_t slotId,bool enable)218 int32_t CellularDataClient::EnableCellularDataRoaming(int32_t slotId, bool enable)
219 {
220     sptr<ICellularDataManager> proxy = GetProxy();
221     if (proxy == nullptr) {
222         TELEPHONY_LOGE("proxy is null");
223         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
224     }
225     return proxy->EnableCellularDataRoaming(slotId, enable);
226 }
227 
GetCellularDataFlowType()228 int32_t CellularDataClient::GetCellularDataFlowType()
229 {
230     sptr<ICellularDataManager> proxy = GetProxy();
231     if (proxy == nullptr) {
232         TELEPHONY_LOGE("proxy is null");
233         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
234     }
235     return proxy->GetCellularDataFlowType();
236 }
237 
HasInternetCapability(int32_t slotId,int32_t cid)238 int32_t CellularDataClient::HasInternetCapability(int32_t slotId, int32_t cid)
239 {
240     sptr<ICellularDataManager> proxy = GetProxy();
241     if (proxy == nullptr) {
242         TELEPHONY_LOGE("proxy is null");
243         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
244     }
245     return proxy->HasInternetCapability(slotId, cid);
246 }
247 
ClearCellularDataConnections(int32_t slotId)248 int32_t CellularDataClient::ClearCellularDataConnections(int32_t slotId)
249 {
250     sptr<ICellularDataManager> proxy = GetProxy();
251     if (proxy == nullptr) {
252         TELEPHONY_LOGE("proxy is null");
253         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
254     }
255     return proxy->ClearCellularDataConnections(slotId);
256 }
257 } // namespace Telephony
258 } // namespace OHOS