• 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 
GetProxy()48 sptr<ICellularDataManager> CellularDataClient::GetProxy()
49 {
50     std::lock_guard<std::mutex> lock(mutexProxy_);
51     if (proxy_ != nullptr) {
52         return proxy_;
53     }
54 
55     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56     if (sam == nullptr) {
57         TELEPHONY_LOGE("Failed to get system ability manager");
58         return nullptr;
59     }
60     sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_CELLULAR_DATA_SYS_ABILITY_ID);
61     if (obj == nullptr) {
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_LOGI("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("OnRemoteDied failed, remote is nullptr");
85         return;
86     }
87     std::lock_guard<std::mutex> lock(mutexProxy_);
88     if (proxy_ == nullptr) {
89         TELEPHONY_LOGE("OnRemoteDied 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         TELEPHONY_LOGE("on remote died");
97     }
98 }
99 
IsConnect() const100 bool CellularDataClient::IsConnect() const
101 {
102     return (proxy_ != nullptr);
103 }
104 
RegisterSimAccountCallback()105 void CellularDataClient::RegisterSimAccountCallback()
106 {
107     if (callback_ == nullptr) {
108         TELEPHONY_LOGE("callback_ is nullptr");
109         return;
110     }
111     if (registerStatus_) {
112         return;
113     }
114     sptr<ICellularDataManager> proxy = GetProxy();
115     if (proxy == nullptr) {
116         TELEPHONY_LOGE("proxy is null");
117         return;
118     }
119     int32_t ret = proxy->RegisterSimAccountCallback(callback_);
120     TELEPHONY_LOGI("CellularDataClient::RegisterSimAccountCallback ret:%{public}d", ret);
121     if (ret == TELEPHONY_ERR_SUCCESS) {
122         registerStatus_ = true;
123     }
124 }
125 
UnregisterSimAccountCallback()126 void CellularDataClient::UnregisterSimAccountCallback()
127 {
128     sptr<ICellularDataManager> proxy = GetProxy();
129     if (proxy == nullptr) {
130         TELEPHONY_LOGE("proxy is null");
131         return;
132     }
133     int32_t ret = proxy->UnregisterSimAccountCallback();
134     TELEPHONY_LOGI("CellularDataClient::UnregisterSimAccountCallback ret:%{public}d", ret);
135 }
136 
GetDefaultCellularDataSlotId()137 int32_t CellularDataClient::GetDefaultCellularDataSlotId()
138 {
139     RegisterSimAccountCallback();
140     if (defaultCellularDataSlotId_ != INVALID_MAIN_CARD_SLOTID) {
141         return defaultCellularDataSlotId_;
142     }
143     sptr<ICellularDataManager> proxy = GetProxy();
144     if (proxy == nullptr) {
145         TELEPHONY_LOGE("proxy is null");
146         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
147     }
148     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
149     return defaultCellularDataSlotId_;
150 }
151 
GetDefaultCellularDataSimId(int32_t & simId)152 int32_t CellularDataClient::GetDefaultCellularDataSimId(int32_t &simId)
153 {
154     RegisterSimAccountCallback();
155     if (defaultCellularDataSimId_ > 0) {
156         simId = defaultCellularDataSimId_;
157         return TELEPHONY_ERR_SUCCESS;
158     }
159     sptr<ICellularDataManager> proxy = GetProxy();
160     if (proxy == nullptr) {
161         TELEPHONY_LOGE("proxy is null");
162         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
163     }
164     int32_t result = proxy->GetDefaultCellularDataSimId(simId);
165     if (result == TELEPHONY_ERR_SUCCESS) {
166         defaultCellularDataSimId_ = simId;
167     }
168     return result;
169 }
170 
SetDefaultCellularDataSlotId(int32_t slotId)171 int32_t CellularDataClient::SetDefaultCellularDataSlotId(int32_t slotId)
172 {
173     RegisterSimAccountCallback();
174     sptr<ICellularDataManager> proxy = GetProxy();
175     if (proxy == nullptr) {
176         TELEPHONY_LOGE("proxy is null");
177         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
178     }
179     int32_t result = proxy->SetDefaultCellularDataSlotId(slotId);
180     if (result == TELEPHONY_ERR_SUCCESS) {
181         defaultCellularDataSlotId_ = slotId;
182         int32_t simId = 0;
183         int32_t ret = proxy->GetDefaultCellularDataSimId(simId);
184         if (ret == TELEPHONY_ERR_SUCCESS) {
185             defaultCellularDataSimId_ = simId;
186         }
187     }
188     if (slotId == DEFAULT_SIM_SLOT_ID_REMOVE) {
189         UpdateDefaultCellularDataSlotId();
190     }
191     return result;
192 }
193 
UpdateDefaultCellularDataSlotId()194 int32_t CellularDataClient::UpdateDefaultCellularDataSlotId()
195 {
196     defaultCellularDataSlotId_ = INVALID_MAIN_CARD_SLOTID;
197     defaultCellularDataSimId_ = 0;
198     sptr<ICellularDataManager> proxy = GetProxy();
199     if (proxy == nullptr) {
200         TELEPHONY_LOGE("proxy is null");
201         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
202     }
203     defaultCellularDataSlotId_ = proxy->GetDefaultCellularDataSlotId();
204     proxy->GetDefaultCellularDataSimId(defaultCellularDataSimId_);
205     return defaultCellularDataSlotId_;
206 }
207 
EnableCellularData(bool enable)208 int32_t CellularDataClient::EnableCellularData(bool enable)
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->EnableCellularData(enable);
216 }
217 
IsCellularDataEnabled(bool & dataEnabled)218 int32_t CellularDataClient::IsCellularDataEnabled(bool &dataEnabled)
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->IsCellularDataEnabled(dataEnabled);
226 }
227 
GetCellularDataState()228 int32_t CellularDataClient::GetCellularDataState()
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->GetCellularDataState();
236 }
237 
IsCellularDataRoamingEnabled(int32_t slotId,bool & dataRoamingEnabled)238 int32_t CellularDataClient::IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled)
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->IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
246 }
247 
EnableCellularDataRoaming(int32_t slotId,bool enable)248 int32_t CellularDataClient::EnableCellularDataRoaming(int32_t slotId, bool enable)
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->EnableCellularDataRoaming(slotId, enable);
256 }
257 
GetCellularDataFlowType()258 int32_t CellularDataClient::GetCellularDataFlowType()
259 {
260     sptr<ICellularDataManager> proxy = GetProxy();
261     if (proxy == nullptr) {
262         TELEPHONY_LOGE("proxy is null");
263         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
264     }
265     return proxy->GetCellularDataFlowType();
266 }
267 
HasInternetCapability(int32_t slotId,int32_t cid)268 int32_t CellularDataClient::HasInternetCapability(int32_t slotId, int32_t cid)
269 {
270     sptr<ICellularDataManager> proxy = GetProxy();
271     if (proxy == nullptr) {
272         TELEPHONY_LOGE("proxy is null");
273         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
274     }
275     return proxy->HasInternetCapability(slotId, cid);
276 }
277 
ClearCellularDataConnections(int32_t slotId)278 int32_t CellularDataClient::ClearCellularDataConnections(int32_t slotId)
279 {
280     sptr<ICellularDataManager> proxy = GetProxy();
281     if (proxy == nullptr) {
282         TELEPHONY_LOGE("proxy is null");
283         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
284     }
285     return proxy->ClearCellularDataConnections(slotId);
286 }
287 } // namespace Telephony
288 } // namespace OHOS
289