• 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_controller.h"
17 
18 #include "cellular_data_constant.h"
19 #include "cellular_data_settings_rdb_helper.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "core_manager_inner.h"
23 #include "network_search_callback.h"
24 #include "radio_event.h"
25 #include "telephony_log_wrapper.h"
26 #include "uri.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 using namespace NetManagerStandard;
31 using namespace OHOS::EventFwk;
32 
CellularDataController(std::shared_ptr<AppExecFwk::EventRunner> & runner,int32_t slotId)33 CellularDataController::CellularDataController(std::shared_ptr<AppExecFwk::EventRunner> &runner, int32_t slotId)
34     : AppExecFwk::EventHandler(runner), slotId_(slotId)
35 {}
36 
~CellularDataController()37 CellularDataController::~CellularDataController()
38 {
39     UnRegisterEvents();
40     UnRegisterDatabaseObserver();
41     if (netManagerListener_ != nullptr) {
42         auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
43         if (samgrProxy != nullptr) {
44             samgrProxy->UnSubscribeSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, netManagerListener_);
45             samgrProxy->UnSubscribeSystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, netManagerListener_);
46             netManagerListener_ = nullptr;
47         }
48     }
49 }
50 
Init()51 void CellularDataController::Init()
52 {
53     EventFwk::MatchingSkills matchingSkills;
54     matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
55     EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
56     cellularDataHandler_ = std::make_shared<CellularDataHandler>(GetEventRunner(), subscriberInfo, slotId_);
57     if (cellularDataHandler_ == nullptr) {
58         TELEPHONY_LOGE("Slot%{public}d: cellularDataHandler_ is null", slotId_);
59         return;
60     }
61     cellularDataHandler_->Init();
62     RegisterDatabaseObserver();
63     auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
64     if (samgrProxy == nullptr) {
65         TELEPHONY_LOGE("samgrProxy is nullptr");
66         return;
67     }
68     netManagerListener_ = new (std::nothrow) SystemAbilityStatusChangeListener(slotId_, cellularDataHandler_);
69     if (netManagerListener_ == nullptr) {
70         TELEPHONY_LOGE("netManagerListener_ is nullptr");
71         return;
72     }
73     samgrProxy->SubscribeSystemAbility(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID, netManagerListener_);
74     samgrProxy->SubscribeSystemAbility(COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID, netManagerListener_);
75 }
76 
SetCellularDataEnable(bool userDataEnabled)77 int32_t CellularDataController::SetCellularDataEnable(bool userDataEnabled)
78 {
79     if (cellularDataHandler_ == nullptr) {
80         TELEPHONY_LOGE("Slot%{public}d: SetCellularDataEnable cellularDataHandler_ is null", slotId_);
81         return TELEPHONY_ERR_LOCAL_PTR_NULL;
82     }
83     return cellularDataHandler_->SetCellularDataEnable(userDataEnabled);
84 }
85 
IsCellularDataEnabled(bool & dataEnabled) const86 int32_t CellularDataController::IsCellularDataEnabled(bool &dataEnabled) const
87 {
88     if (cellularDataHandler_ == nullptr) {
89         TELEPHONY_LOGE("Slot%{public}d: IsCellularDataEnabled cellularDataHandler_ is null", slotId_);
90         return TELEPHONY_ERR_LOCAL_PTR_NULL;
91     }
92     return cellularDataHandler_->IsCellularDataEnabled(dataEnabled);
93 }
94 
GetCellularDataState() const95 ApnProfileState CellularDataController::GetCellularDataState() const
96 {
97     if (cellularDataHandler_ == nullptr) {
98         TELEPHONY_LOGE("Slot%{public}d: GetCellularDataState cellularDataHandler_ is null", slotId_);
99         return ApnProfileState::PROFILE_STATE_FAILED;
100     }
101     return cellularDataHandler_->GetCellularDataState();
102 }
103 
GetCellularDataState(const std::string & apnType) const104 ApnProfileState CellularDataController::GetCellularDataState(const std::string &apnType) const
105 {
106     if (cellularDataHandler_ == nullptr) {
107         TELEPHONY_LOGE("Slot%{public}d: GetCellularDataState cellularDataHandler_ is null", slotId_);
108         return ApnProfileState::PROFILE_STATE_FAILED;
109     }
110     return cellularDataHandler_->GetCellularDataState(apnType);
111 }
112 
IsCellularDataRoamingEnabled(bool & dataRoamingEnabled) const113 int32_t CellularDataController::IsCellularDataRoamingEnabled(bool &dataRoamingEnabled) const
114 {
115     if (cellularDataHandler_ == nullptr) {
116         TELEPHONY_LOGE("Slot%{public}d: IsCellularDataRoamingEnabled cellularDataHandler_ is null", slotId_);
117         return TELEPHONY_ERR_LOCAL_PTR_NULL;
118     }
119     return cellularDataHandler_->IsCellularDataRoamingEnabled(dataRoamingEnabled);
120 }
121 
SetCellularDataRoamingEnabled(bool dataRoamingEnabled)122 int32_t CellularDataController::SetCellularDataRoamingEnabled(bool dataRoamingEnabled)
123 {
124     if (cellularDataHandler_ == nullptr) {
125         TELEPHONY_LOGE("Slot%{public}d: SetCellularDataRoamingEnabled cellularDataHandler is null", slotId_);
126         return TELEPHONY_ERR_LOCAL_PTR_NULL;
127     }
128     return cellularDataHandler_->SetCellularDataRoamingEnabled(dataRoamingEnabled);
129 }
130 
SetDataPermitted(bool dataPermitted) const131 void CellularDataController::SetDataPermitted(bool dataPermitted) const
132 {
133     if (cellularDataHandler_ == nullptr) {
134         TELEPHONY_LOGE("Slot%{public}d: SetDataPermitted cellularDataHandler is null", slotId_);
135         return;
136     }
137     cellularDataHandler_->SetDataPermitted(dataPermitted);
138 }
139 
ReleaseNet(const NetRequest & request)140 bool CellularDataController::ReleaseNet(const NetRequest &request)
141 {
142     if (cellularDataHandler_ == nullptr) {
143         TELEPHONY_LOGE("Slot%{public}d: ReleaseNet cellularDataHandler_ is null", slotId_);
144         return false;
145     }
146     return cellularDataHandler_->ReleaseNet(request);
147 }
148 
RequestNet(const NetRequest & request)149 bool CellularDataController::RequestNet(const NetRequest &request)
150 {
151     if (cellularDataHandler_ == nullptr) {
152         TELEPHONY_LOGE("Slot%{public}d: RequestNet cellularDataHandler_ is null", slotId_);
153         return false;
154     }
155     return cellularDataHandler_->RequestNet(request);
156 }
157 
AsynchronousRegister()158 void CellularDataController::AsynchronousRegister()
159 {
160     if (CoreManagerInner::GetInstance().IsInitFinished()) {
161         TELEPHONY_LOGI("Slot%{public}d: core inited", slotId_);
162         Init();
163         RegisterEvents();
164         return;
165     }
166     SendEvent(CellularDataEventCode::MSG_ASYNCHRONOUS_REGISTER_EVENT_ID, CORE_INIT_DELAY_TIME, Priority::HIGH);
167 }
168 
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)169 void CellularDataController::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
170 {
171     if (event == nullptr) {
172         TELEPHONY_LOGE("Slot%{public}d: ProcessEvent event is null.", slotId_);
173         return;
174     }
175     size_t eventId = event->GetInnerEventId();
176     switch (eventId) {
177         case CellularDataEventCode::MSG_ASYNCHRONOUS_REGISTER_EVENT_ID:
178             AsynchronousRegister();
179             break;
180         default:
181             TELEPHONY_LOGE("Slot%{public}d: ProcessEvent nothing to do", slotId_);
182             break;
183     }
184 }
185 
RegisterEvents()186 void CellularDataController::RegisterEvents()
187 {
188     if (cellularDataHandler_ == nullptr) {
189         TELEPHONY_LOGE("Slot%{public}d: core is null or cellularDataHandler is null", slotId_);
190         return;
191     }
192     TELEPHONY_LOGI("Slot%{public}d: RegisterEvents start", slotId_);
193     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
194     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_STATE_CHANGE, nullptr);
195     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_RECORDS_LOADED, nullptr);
196     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_ACCOUNT_LOADED, nullptr);
197     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_ATTACHED, nullptr);
198     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_DETACHED, nullptr);
199     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_OPEN, nullptr);
200     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_CLOSE, nullptr);
201     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_STATE_CHANGED, nullptr);
202     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_RAT_CHANGED, nullptr);
203     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_CALL_STATUS_INFO, nullptr);
204     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_OPEN, nullptr);
205     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_CLOSE, nullptr);
206     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_STATE_CHANGED, nullptr);
207     coreInner.RegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_FREQUENCY_CHANGED, nullptr);
208     if (slotId_ == 0) {
209         sptr<NetworkSearchCallback> networkSearchCallback = std::make_unique<NetworkSearchCallback>().release();
210         if (networkSearchCallback != nullptr) {
211             coreInner.RegisterCellularDataObject(networkSearchCallback);
212         } else {
213             TELEPHONY_LOGE("Slot%{public}d: networkSearchCallback is null", slotId_);
214         }
215     }
216 }
217 
UnRegisterEvents()218 void CellularDataController::UnRegisterEvents()
219 {
220     if (cellularDataHandler_ == nullptr) {
221         TELEPHONY_LOGE("Slot%{public}d: UnRegisterEvents cellularDataHandler_ is null", slotId_);
222         return;
223     }
224     TELEPHONY_LOGI("Slot%{public}d: UnRegisterEvents start", slotId_);
225     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
226     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_STATE_CHANGE);
227     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_RECORDS_LOADED);
228     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_SIM_ACCOUNT_LOADED);
229     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_ATTACHED);
230     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_CONNECTION_DETACHED);
231     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_OPEN);
232     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_ROAMING_CLOSE);
233     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_STATE_CHANGED);
234     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_PS_RAT_CHANGED);
235     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_CALL_STATUS_INFO);
236     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_OPEN);
237     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_EMERGENCY_STATE_CLOSE);
238     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_STATE_CHANGED);
239     coreInner.UnRegisterCoreNotify(slotId_, cellularDataHandler_, RadioEvent::RADIO_NR_FREQUENCY_CHANGED);
240     TELEPHONY_LOGI("Slot%{public}d: UnRegisterEvents end", slotId_);
241 }
242 
UnRegisterDatabaseObserver()243 void CellularDataController::UnRegisterDatabaseObserver()
244 {
245     if (cellularDataRdbObserver_ == nullptr) {
246         TELEPHONY_LOGE("Slot%{public}d: cellularDataRdbObserver_ is null", slotId_);
247         return;
248     }
249     std::shared_ptr<CellularDataRdbHelper> cellularDataDbHelper = CellularDataRdbHelper::GetInstance();
250     cellularDataDbHelper->UnRegisterObserver(cellularDataRdbObserver_);
251 }
252 
RegisterDatabaseObserver()253 void CellularDataController::RegisterDatabaseObserver()
254 {
255     cellularDataRdbObserver_ = std::make_unique<CellularDataRdbObserver>(cellularDataHandler_).release();
256     if (cellularDataRdbObserver_ == nullptr) {
257         TELEPHONY_LOGE("Slot%{public}d: cellularDataRdbObserver_ is null", slotId_);
258         return;
259     }
260     std::shared_ptr<CellularDataRdbHelper> cellularDataDbHelper = CellularDataRdbHelper::GetInstance();
261     cellularDataDbHelper->RegisterObserver(cellularDataRdbObserver_);
262 }
263 
HandleApnChanged()264 bool CellularDataController::HandleApnChanged()
265 {
266     if (cellularDataHandler_ == nullptr) {
267         TELEPHONY_LOGE("Slot%{public}d: ApnChanged cellularDataHandler_ is null", slotId_);
268         return static_cast<int32_t>(DataRespondCode::SET_FAILED);
269     }
270     return cellularDataHandler_->HandleApnChanged();
271 }
272 
GetCellularDataFlowType()273 int32_t CellularDataController::GetCellularDataFlowType()
274 {
275     if (cellularDataHandler_ == nullptr) {
276         TELEPHONY_LOGE("Slot%{public}d: cellular data handler is null", slotId_);
277         return static_cast<int32_t>(CellDataFlowType::DATA_FLOW_TYPE_NONE);
278     }
279     return cellularDataHandler_->GetCellularDataFlowType();
280 }
281 
EstablishDataConnection()282 void CellularDataController::EstablishDataConnection()
283 {
284     if (cellularDataHandler_ != nullptr) {
285         cellularDataHandler_->EstablishAllApnsIfConnectable();
286     }
287 }
288 
SetPolicyDataOn(bool enable)289 int32_t CellularDataController::SetPolicyDataOn(bool enable)
290 {
291     if (cellularDataHandler_ != nullptr) {
292         cellularDataHandler_->SetPolicyDataOn(enable);
293     }
294     return static_cast<int32_t>(DataRespondCode::SET_SUCCESS);
295 }
296 
IsRestrictedMode() const297 bool CellularDataController::IsRestrictedMode() const
298 {
299     if (cellularDataHandler_ != nullptr) {
300         return cellularDataHandler_->IsRestrictedMode();
301     }
302     return false;
303 }
304 
GetDisConnectionReason()305 DisConnectionReason CellularDataController::GetDisConnectionReason()
306 {
307     if (cellularDataHandler_ != nullptr) {
308         return cellularDataHandler_->GetDisConnectionReason();
309     }
310     return DisConnectionReason::REASON_NORMAL;
311 }
312 
HasInternetCapability(const int32_t cid) const313 bool CellularDataController::HasInternetCapability(const int32_t cid) const
314 {
315     if (cellularDataHandler_ == nullptr) {
316         TELEPHONY_LOGE("Slot%{public}d: cellularDataHandler_ is null", slotId_);
317         return false;
318     }
319     return cellularDataHandler_->HasInternetCapability(cid);
320 }
321 
ClearAllConnections(DisConnectionReason reason) const322 bool CellularDataController::ClearAllConnections(DisConnectionReason reason) const
323 {
324     if (cellularDataHandler_ == nullptr) {
325         TELEPHONY_LOGE("Slot%{public}d: cellularDataHandler is null", slotId_);
326         return false;
327     }
328     cellularDataHandler_->ClearAllConnections(reason);
329     return true;
330 }
331 
SystemAbilityStatusChangeListener(int32_t slotId,std::shared_ptr<CellularDataHandler> handler)332 CellularDataController::SystemAbilityStatusChangeListener::SystemAbilityStatusChangeListener(
333     int32_t slotId, std::shared_ptr<CellularDataHandler> handler)
334     : slotId_(slotId), handler_(handler)
335 {}
336 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)337 void CellularDataController::SystemAbilityStatusChangeListener::OnAddSystemAbility(
338     int32_t systemAbilityId, const std::string &deviceId)
339 {
340     switch (systemAbilityId) {
341         case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID:
342             TELEPHONY_LOGI("COMM_NET_CONN_MANAGER_SYS_ABILITY_ID running");
343             if (isNetStopped_ && handler_ != nullptr) {
344                 handler_->ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
345                 CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_);
346                 handler_->EstablishAllApnsIfConnectable();
347                 isNetStopped_ = false;
348             } else {
349                 CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_);
350             }
351             break;
352         case COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID:
353             TELEPHONY_LOGI("COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID running");
354             if (slotId_ == 0) {
355                 CellularDataNetAgent::GetInstance().RegisterPolicyCallback();
356             }
357             break;
358         default:
359             TELEPHONY_LOGE("systemAbilityId is invalid");
360             break;
361     }
362 }
363 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)364 void CellularDataController::SystemAbilityStatusChangeListener::OnRemoveSystemAbility(
365     int32_t systemAbilityId, const std::string &deviceId)
366 {
367     switch (systemAbilityId) {
368         case COMM_NET_CONN_MANAGER_SYS_ABILITY_ID:
369             TELEPHONY_LOGE("COMM_NET_CONN_MANAGER_SYS_ABILITY_ID stopped");
370             isNetStopped_ = true;
371             break;
372         case COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID:
373             TELEPHONY_LOGE("COMM_NET_POLICY_MANAGER_SYS_ABILITY_ID stopped");
374             break;
375         default:
376             TELEPHONY_LOGE("systemAbilityId is invalid");
377             break;
378     }
379 }
380 } // namespace Telephony
381 } // namespace OHOS
382