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