• 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_handler.h"
17 
18 #include "cellular_data_constant.h"
19 #include "cellular_data_error.h"
20 #include "cellular_data_hisysevent.h"
21 #include "cellular_data_settings_rdb_helper.h"
22 #include "cellular_data_types.h"
23 #include "cellular_data_utils.h"
24 #include "common_event_manager.h"
25 #include "common_event_support.h"
26 #include "core_manager_inner.h"
27 #include "hitrace_meter.h"
28 #include "hril_call_parcel.h"
29 #include "net_specifier.h"
30 #include "radio_event.h"
31 #include "runner_pool.h"
32 #include "str_convert.h"
33 #include "string_ex.h"
34 #include "telephony_log_wrapper.h"
35 #include "telephony_types.h"
36 
37 namespace OHOS {
38 namespace Telephony {
39 using namespace AppExecFwk;
40 using namespace OHOS::EventFwk;
41 using namespace NetManagerStandard;
CellularDataHandler(const std::shared_ptr<AppExecFwk::EventRunner> & runner,const EventFwk::CommonEventSubscribeInfo & sp,int32_t slotId)42 CellularDataHandler::CellularDataHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner,
43     const EventFwk::CommonEventSubscribeInfo &sp, int32_t slotId)
44     : EventHandler(runner), CommonEventSubscriber(sp), slotId_(slotId)
45 {}
46 
Init()47 void CellularDataHandler::Init()
48 {
49     apnManager_ = std::make_unique<ApnManager>().release();
50     dataSwitchSettings_ = std::make_unique<DataSwitchSettings>(slotId_);
51     connectionManager_ = std::make_unique<DataConnectionManager>(GetEventRunner(), slotId_).release();
52     settingObserver_ = std::make_unique<CellularDataSettingObserver>(shared_from_this()).release();
53     roamingObserver_ = std::make_unique<CellularDataRoamingObserver>(shared_from_this(), slotId_).release();
54     if ((apnManager_ == nullptr) || (dataSwitchSettings_ == nullptr) || (connectionManager_ == nullptr)) {
55         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or dataSwitchSettings_ or connectionManager_ is null", slotId_);
56         return;
57     }
58     apnManager_->InitApnHolders();
59     apnManager_->CreateAllApnItem();
60     dataSwitchSettings_->LoadSwitchValue();
61     GetConfigurationFor5G();
62     SetRilLinkBandwidths();
63 }
64 
~CellularDataHandler()65 CellularDataHandler::~CellularDataHandler()
66 {
67     UnRegisterDataSettingObserver();
68 }
69 
ReleaseNet(const NetRequest & request)70 bool CellularDataHandler::ReleaseNet(const NetRequest &request)
71 {
72     if (apnManager_ == nullptr) {
73         TELEPHONY_LOGE("Slot%{public}d: apnManager is null.", slotId_);
74         return false;
75     }
76     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
77     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
78     if (apnHolder == nullptr) {
79         TELEPHONY_LOGE("Slot%{public}d: ReleaseNet apnHolder is null.", slotId_);
80         return false;
81     }
82     std::unique_ptr<NetRequest> netRequest = std::make_unique<NetRequest>();
83     if (netRequest == nullptr) {
84         TELEPHONY_LOGE("Netrequest is null");
85         return false;
86     }
87     netRequest->capability = request.capability;
88     netRequest->ident = request.ident;
89     AppExecFwk::InnerEvent::Pointer event =
90         InnerEvent::Get(CellularDataEventCode::MSG_REQUEST_NETWORK, netRequest, TYPE_RELEASE_NET);
91     return SendEvent(event);
92 }
93 
RequestNet(const NetRequest & request)94 bool CellularDataHandler::RequestNet(const NetRequest &request)
95 {
96     if (apnManager_ == nullptr) {
97         TELEPHONY_LOGE("Slot%{public}d: apnManager is null.", slotId_);
98         return false;
99     }
100     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
101     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
102     if (apnHolder == nullptr) {
103         TELEPHONY_LOGE("Slot%{public}d: RequestNet apnHolder is null.", slotId_);
104         return false;
105     }
106     ApnProfileState apnState = apnHolder->GetApnState();
107     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTED || apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
108         apnState == ApnProfileState::PROFILE_STATE_DISCONNECTING) {
109         TELEPHONY_LOGE("Slot%{public}d: RequestNet apn state is connected(%{public}d).", slotId_, apnState);
110         return true;
111     }
112     std::unique_ptr<NetRequest> netRequest = std::make_unique<NetRequest>();
113     if (netRequest == nullptr) {
114         TELEPHONY_LOGE("Netrequest is null");
115         return false;
116     }
117     netRequest->capability = request.capability;
118     netRequest->ident = request.ident;
119     AppExecFwk::InnerEvent::Pointer event =
120         InnerEvent::Get(CellularDataEventCode::MSG_REQUEST_NETWORK, netRequest, TYPE_REQUEST_NET);
121     return SendEvent(event);
122 }
123 
SetCellularDataEnable(bool userDataOn)124 int32_t CellularDataHandler::SetCellularDataEnable(bool userDataOn)
125 {
126     if (dataSwitchSettings_ == nullptr) {
127         TELEPHONY_LOGE("Slot%{public}d: SetCellularDataEnable dataSwitchSettings_ is null.", slotId_);
128         return TELEPHONY_ERR_LOCAL_PTR_NULL;
129     }
130     bool dataEnabled = false;
131     int32_t result = dataSwitchSettings_->IsUserDataOn(dataEnabled);
132     if (result != TELEPHONY_ERR_SUCCESS) {
133         return result;
134     }
135     if (dataEnabled == userDataOn) {
136         TELEPHONY_LOGI("Slot%{public}d: The status of the cellular data switch has not changed", slotId_);
137         return TELEPHONY_ERR_SUCCESS;
138     }
139     return dataSwitchSettings_->SetUserDataOn(userDataOn);
140 }
141 
IsCellularDataEnabled(bool & dataEnabled) const142 int32_t CellularDataHandler::IsCellularDataEnabled(bool &dataEnabled) const
143 {
144     if (dataSwitchSettings_ == nullptr) {
145         TELEPHONY_LOGE("Slot%{public}d: IsCellularDataEnabled dataSwitchSettings_ is null", slotId_);
146         return TELEPHONY_ERR_LOCAL_PTR_NULL;
147     }
148     return dataSwitchSettings_->IsUserDataOn(dataEnabled);
149 }
150 
IsCellularDataRoamingEnabled(bool & dataRoamingEnabled) const151 int32_t CellularDataHandler::IsCellularDataRoamingEnabled(bool &dataRoamingEnabled) const
152 {
153     if (dataSwitchSettings_ == nullptr) {
154         TELEPHONY_LOGE("Slot%{public}d: IsCellularDataRoamingEnabled dataSwitchSettings_ is null", slotId_);
155         return TELEPHONY_ERR_LOCAL_PTR_NULL;
156     }
157     return dataSwitchSettings_->IsUserDataRoamingOn(dataRoamingEnabled);
158 }
159 
SetCellularDataRoamingEnabled(bool dataRoamingEnabled)160 int32_t CellularDataHandler::SetCellularDataRoamingEnabled(bool dataRoamingEnabled)
161 {
162     if (dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
163         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
164         return TELEPHONY_ERR_LOCAL_PTR_NULL;
165     }
166     bool currentDataEnabled = false;
167     dataSwitchSettings_->IsUserDataRoamingOn(currentDataEnabled);
168     if (currentDataEnabled == dataRoamingEnabled) {
169         TELEPHONY_LOGI("Slot%{public}d: The roaming switch status has not changed", slotId_);
170         return TELEPHONY_ERR_SUCCESS;
171     }
172     int32_t result = dataSwitchSettings_->SetUserDataRoamingOn(dataRoamingEnabled);
173     if (result != TELEPHONY_ERR_SUCCESS) {
174         return result;
175     }
176     bool roamingState = CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0;
177     if (roamingState) {
178         ApnProfileState apnState = apnManager_->GetOverallApnState();
179         if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
180             apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
181             ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
182         }
183         EstablishAllApnsIfConnectable();
184     } else {
185         TELEPHONY_LOGI("Slot%{public}d: Not roaming(%{public}d), not doing anything", slotId_, roamingState);
186     }
187     return TELEPHONY_ERR_SUCCESS;
188 }
189 
ClearAllConnections(DisConnectionReason reason)190 void CellularDataHandler::ClearAllConnections(DisConnectionReason reason)
191 {
192     if (apnManager_ == nullptr) {
193         TELEPHONY_LOGE("Slot%{public}d: ClearAllConnections:apnManager is null", slotId_);
194         return;
195     }
196     ApnProfileState currentState = apnManager_->GetOverallApnState();
197     if (currentState == ApnProfileState::PROFILE_STATE_CONNECTED ||
198         currentState == ApnProfileState::PROFILE_STATE_CONNECTING) {
199         int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
200         CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
201         StateNotification::GetInstance().UpdateCellularDataConnectState(
202             slotId_, PROFILE_STATE_DISCONNECTING, networkType);
203     }
204     for (const sptr<ApnHolder> &apn : apnManager_->GetAllApnHolder()) {
205         ClearConnection(apn, reason);
206     }
207 
208     if (connectionManager_ == nullptr) {
209         TELEPHONY_LOGE("Slot%{public}d: in ClearAllConnections connectionManager_ is null", slotId_);
210         return;
211     }
212     connectionManager_->StopStallDetectionTimer();
213     connectionManager_->EndNetStatistics();
214 
215     if (dataSwitchSettings_ == nullptr) {
216         TELEPHONY_LOGE("Slot%{public}d: in ClearAllConnections dataSwitchSettings_ is null", slotId_);
217         return;
218     }
219     bool dataEnabled = false;
220     dataSwitchSettings_->IsUserDataOn(dataEnabled);
221     if (!dataEnabled) {
222         connectionManager_->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_NONE);
223     }
224 }
225 
ClearConnection(const sptr<ApnHolder> & apn,DisConnectionReason reason)226 void CellularDataHandler::ClearConnection(const sptr<ApnHolder> &apn, DisConnectionReason reason)
227 {
228     if (apn == nullptr) {
229         TELEPHONY_LOGE("Slot%{public}d: ClearConnection:apnHolder is null", slotId_);
230         return;
231     }
232     std::shared_ptr<CellularDataStateMachine> stateMachine = apn->GetCellularDataStateMachine();
233     if (stateMachine == nullptr) {
234         TELEPHONY_LOGE("Slot%{public}d: stateMachine is null", slotId_);
235         return;
236     }
237     TELEPHONY_LOGI("Slot%{public}d: The APN holder is of type %{public}s", slotId_, apn->GetApnType().c_str());
238     std::unique_ptr<DataDisconnectParams> object = std::make_unique<DataDisconnectParams>(apn->GetApnType(), reason);
239     if (object == nullptr) {
240         TELEPHONY_LOGE("Slot%{public}d: ClearConnection fail, object is null", slotId_);
241         return;
242     }
243     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_SM_DISCONNECT, object);
244     stateMachine->SendEvent(event);
245     apn->SetApnState(PROFILE_STATE_DISCONNECTING);
246     apn->SetCellularDataStateMachine(nullptr);
247 }
248 
GetCellularDataState() const249 ApnProfileState CellularDataHandler::GetCellularDataState() const
250 {
251     if (apnManager_ == nullptr) {
252         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
253         return ApnProfileState::PROFILE_STATE_IDLE;
254     }
255     return apnManager_->GetOverallApnState();
256 }
257 
GetCellularDataState(const std::string & apnType) const258 ApnProfileState CellularDataHandler::GetCellularDataState(const std::string &apnType) const
259 {
260     if (apnManager_ == nullptr) {
261         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
262         return ApnProfileState::PROFILE_STATE_IDLE;
263     }
264     sptr<ApnHolder> apnHolder = apnManager_->GetApnHolder(apnType);
265     if (apnHolder == nullptr) {
266         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
267         return ApnProfileState::PROFILE_STATE_IDLE;
268     }
269     return apnHolder->GetApnState();
270 }
271 
RadioPsConnectionAttached(const InnerEvent::Pointer & event)272 void CellularDataHandler::RadioPsConnectionAttached(const InnerEvent::Pointer &event)
273 {
274     TELEPHONY_LOGI("Slot%{public}d: ps attached", slotId_);
275     if (event == nullptr || apnManager_ == nullptr) {
276         TELEPHONY_LOGE("Slot%{public}d: event or apnManager_ is null", slotId_);
277         return;
278     }
279     ApnProfileState apnState = apnManager_->GetOverallApnState();
280     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
281         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
282     }
283     EstablishAllApnsIfConnectable();
284 }
285 
RadioPsConnectionDetached(const InnerEvent::Pointer & event)286 void CellularDataHandler::RadioPsConnectionDetached(const InnerEvent::Pointer &event)
287 {
288     TELEPHONY_LOGI("Slot%{public}d: ps detached", slotId_);
289     if (event == nullptr) {
290         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
291         return;
292     }
293     ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
294 }
295 
RoamingStateOn(const InnerEvent::Pointer & event)296 void CellularDataHandler::RoamingStateOn(const InnerEvent::Pointer &event)
297 {
298     TELEPHONY_LOGI("Slot%{public}d: roaming on", slotId_);
299     if (event == nullptr || dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
300         TELEPHONY_LOGE("Slot%{public}d: event or dataSwitchSettings_ or apnManager_ is null", slotId_);
301         return;
302     }
303     bool roamingState = false;
304     if (CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0) {
305         roamingState = true;
306     }
307     if (!roamingState) {
308         TELEPHONY_LOGE("Slot%{public}d: device not currently roaming state", slotId_);
309         return;
310     }
311     ApnProfileState apnState = apnManager_->GetOverallApnState();
312     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
313         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
314     }
315     EstablishAllApnsIfConnectable();
316 }
317 
RoamingStateOff(const InnerEvent::Pointer & event)318 void CellularDataHandler::RoamingStateOff(const InnerEvent::Pointer &event)
319 {
320     TELEPHONY_LOGI("Slot%{public}d: roaming off", slotId_);
321     if (event == nullptr || dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
322         TELEPHONY_LOGE("Slot%{public}d: event or dataSwitchSettings_ or apnManager_ is null", slotId_);
323         return;
324     }
325     ApnProfileState apnState = apnManager_->GetOverallApnState();
326     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
327         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
328     }
329     EstablishAllApnsIfConnectable();
330 }
331 
PsRadioEmergencyStateOpen(const InnerEvent::Pointer & event)332 void CellularDataHandler::PsRadioEmergencyStateOpen(const InnerEvent::Pointer &event)
333 {
334     TELEPHONY_LOGI("Slot%{public}d: emergency on", slotId_);
335     ApnProfileState currentState = apnManager_->GetOverallApnState();
336     if (currentState == ApnProfileState::PROFILE_STATE_CONNECTED ||
337         currentState == ApnProfileState::PROFILE_STATE_CONNECTING) {
338         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
339     }
340 }
341 
PsRadioEmergencyStateClose(const InnerEvent::Pointer & event)342 void CellularDataHandler::PsRadioEmergencyStateClose(const InnerEvent::Pointer &event)
343 {
344     TELEPHONY_LOGI("Slot%{public}d: emergency off", slotId_);
345     ApnProfileState currentState = apnManager_->GetOverallApnState();
346     if (currentState == ApnProfileState::PROFILE_STATE_IDLE ||
347         currentState == ApnProfileState::PROFILE_STATE_DISCONNECTING) {
348         EstablishAllApnsIfConnectable();
349     }
350 }
351 
EstablishAllApnsIfConnectable()352 void CellularDataHandler::EstablishAllApnsIfConnectable()
353 {
354     if (apnManager_ == nullptr) {
355         TELEPHONY_LOGE("Slot%{public}d: EstablishAllApnsIfConnectable:apnManager is null", slotId_);
356         return;
357     }
358     if (!CheckCellularDataSlotId()) {
359         return;
360     }
361     for (sptr<ApnHolder> apnHolder : apnManager_->GetSortApnHolder()) {
362         if (apnHolder == nullptr) {
363             TELEPHONY_LOGE("Slot%{public}d: apn is null", slotId_);
364             continue;
365         }
366         if (apnHolder->IsDataCallEnabled()) {
367             ApnProfileState apnState = apnHolder->GetApnState();
368             if (apnState == PROFILE_STATE_FAILED || apnState == PROFILE_STATE_RETRYING) {
369                 apnHolder->ReleaseDataConnection();
370             }
371             if (apnHolder->IsDataCallConnectable()) {
372                 AttemptEstablishDataConnection(apnHolder);
373             }
374         }
375     }
376 }
377 
CheckCellularDataSlotId()378 bool CellularDataHandler::CheckCellularDataSlotId()
379 {
380     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
381     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
382     int32_t dsdsMode = DSDS_MODE_V2;
383     coreInner.GetDsdsMode(dsdsMode);
384     if (defSlotId != slotId_ && dsdsMode != DSDS_MODE_V3) {
385         TELEPHONY_LOGI("Slot%{public}d: default:%{public}d, current:%{public}d", slotId_, defSlotId, slotId_);
386         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
387             CellularDataErrorCode::DATA_ERROR_CELLULAR_DATA_SLOT_ID_MISMATCH,
388             "Default cellular data slot id is not current slot id");
389         return false;
390     }
391     return true;
392 }
393 
CheckAttachAndSimState(sptr<ApnHolder> & apnHolder)394 bool CellularDataHandler::CheckAttachAndSimState(sptr<ApnHolder> &apnHolder)
395 {
396     if (apnHolder == nullptr) {
397         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
398         return false;
399     }
400     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
401     bool attached = coreInner.GetPsRegState(slotId_) == (int32_t)RegServiceState::REG_STATE_IN_SERVICE;
402     SimState simState = SimState::SIM_STATE_UNKNOWN;
403     coreInner.GetSimState(slotId_, simState);
404     TELEPHONY_LOGI("Slot%{public}d: attached: %{public}d simState: %{public}d", slotId_, attached, simState);
405     bool isEmergencyApn = apnHolder->IsEmergencyType();
406     if (!isEmergencyApn && !attached) {
407         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
408             CellularDataErrorCode::DATA_ERROR_PS_NOT_ATTACH, "It is not emergencyApn and not attached");
409         return false;
410     }
411     if (!isEmergencyApn && (simState != SimState::SIM_STATE_READY)) {
412         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
413             CellularDataErrorCode::DATA_ERROR_SIM_NOT_READY, "It is not emergencyApn and sim not ready");
414         return false;
415     }
416     return true;
417 }
418 
CheckRoamingState(sptr<ApnHolder> & apnHolder)419 bool CellularDataHandler::CheckRoamingState(sptr<ApnHolder> &apnHolder)
420 {
421     if (dataSwitchSettings_ == nullptr || apnHolder == nullptr) {
422         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
423         return false;
424     }
425     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
426     bool isEmergencyApn = apnHolder->IsEmergencyType();
427     bool isAllowActiveData = dataSwitchSettings_->IsAllowActiveData();
428     bool roamingState = coreInner.GetPsRoamingState(slotId_) > 0;
429     bool dataRoamingEnabled = false;
430     dataSwitchSettings_->IsUserDataRoamingOn(dataRoamingEnabled);
431     if (roamingState && !dataRoamingEnabled) {
432         isAllowActiveData = false;
433     }
434     if (isEmergencyApn) {
435         isAllowActiveData = true;
436     }
437     if (!isAllowActiveData) {
438         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
439             CellularDataErrorCode::DATA_ERROR_ROAMING_SWITCH_OFF_AND_ROAMING, "Data roaming is not on and is roaming");
440         TELEPHONY_LOGE("Slot%{public}d: AllowActiveData:%{public}d lastCallState_:%{public}d", slotId_,
441             isAllowActiveData, lastCallState_);
442         return false;
443     }
444     if (IsRestrictedMode()) {
445         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
446             CellularDataErrorCode::DATA_ERROR_CALL_AND_DATA_NOT_CONCURRENCY,
447             "CS call and data are not allowed concurrency");
448         TELEPHONY_LOGE("Slot%{public}d: AllowActiveData:%{public}d lastCallState_:%{public}d", slotId_,
449             isAllowActiveData, lastCallState_);
450         return false;
451     }
452     return true;
453 }
454 
CheckApnState(sptr<ApnHolder> & apnHolder)455 bool CellularDataHandler::CheckApnState(sptr<ApnHolder> &apnHolder)
456 {
457     if (apnManager_ == nullptr || apnHolder == nullptr) {
458         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or apnManager_ is null", slotId_);
459         return false;
460     }
461 
462     if (apnHolder->GetApnState() == PROFILE_STATE_FAILED) {
463         apnHolder->SetApnState(PROFILE_STATE_IDLE);
464     }
465 
466     if (apnHolder->GetApnState() != PROFILE_STATE_IDLE) {
467         TELEPHONY_LOGI("Slot%{public}d: APN holder is not idle", slotId_);
468         return false;
469     }
470     std::vector<sptr<ApnItem>> matchedApns = apnManager_->FilterMatchedApns(apnHolder->GetApnType());
471     if (matchedApns.empty()) {
472         TELEPHONY_LOGE("Slot%{public}d: AttemptEstablishDataConnection:matchedApns is empty", slotId_);
473         return false;
474     }
475     apnHolder->SetAllMatchedApns(matchedApns);
476     return true;
477 }
478 
AttemptEstablishDataConnection(sptr<ApnHolder> & apnHolder)479 void CellularDataHandler::AttemptEstablishDataConnection(sptr<ApnHolder> &apnHolder)
480 {
481     if (!CheckCellularDataSlotId() || !CheckAttachAndSimState(apnHolder) || !CheckRoamingState(apnHolder)) {
482         return;
483     }
484     DelayedSingleton<CellularDataHiSysEvent>::GetInstance()->SetCellularDataActivateStartTime();
485     StartTrace(HITRACE_TAG_OHOS, "ActivateCellularData");
486     if (!CheckApnState(apnHolder)) {
487         FinishTrace(HITRACE_TAG_OHOS);
488         return;
489     }
490     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
491     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
492     coreInner.GetPsRadioTech(slotId_, radioTech);
493     if (!EstablishDataConnection(apnHolder, radioTech)) {
494         TELEPHONY_LOGE("Slot%{public}d: Establish data connection fail", slotId_);
495     }
496     FinishTrace(HITRACE_TAG_OHOS);
497     DelayedSingleton<CellularDataHiSysEvent>::GetInstance()->JudgingDataActivateTimeOut(slotId_, SWITCH_ON);
498 }
499 
FindIdleCellularDataConnection() const500 std::shared_ptr<CellularDataStateMachine> CellularDataHandler::FindIdleCellularDataConnection() const
501 {
502     if (connectionManager_ == nullptr) {
503         TELEPHONY_LOGE("Slot%{public}d: in FindIdleCellularDataConnection connectionManager_ is null", slotId_);
504         return nullptr;
505     }
506     std::vector<std::shared_ptr<CellularDataStateMachine>> allMachines = connectionManager_->GetAllConnectionMachine();
507     for (const std::shared_ptr<CellularDataStateMachine> &connect : allMachines) {
508         if (connect == nullptr || apnManager_ == nullptr) {
509             TELEPHONY_LOGE("Slot%{public}d: CellularDataHandler:stateMachine or apnManager_ is null", slotId_);
510             return nullptr;
511         }
512         if (connect->IsInactiveState() && apnManager_->IsDataConnectionNotUsed(connect)) {
513             return connect;
514         }
515     }
516     return nullptr;
517 }
518 
CreateCellularDataConnect()519 std::shared_ptr<CellularDataStateMachine> CellularDataHandler::CreateCellularDataConnect()
520 {
521     if (stateMachineEventLoop_ == nullptr) {
522         stateMachineEventLoop_ = RunnerPool::GetInstance().GetCommonRunner();
523         if (stateMachineEventLoop_ == nullptr) {
524             TELEPHONY_LOGE("Slot%{public}d: failed to create EventRunner", slotId_);
525             return nullptr;
526         }
527     }
528     std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine =
529         std::make_shared<CellularDataStateMachine>(connectionManager_, shared_from_this(), stateMachineEventLoop_);
530     if (cellularDataStateMachine == nullptr) {
531         TELEPHONY_LOGE("Slot%{public}d: cellularDataStateMachine is null", slotId_);
532         return nullptr;
533     }
534     return cellularDataStateMachine;
535 }
536 
EstablishDataConnection(sptr<ApnHolder> & apnHolder,int32_t radioTech)537 bool CellularDataHandler::EstablishDataConnection(sptr<ApnHolder> &apnHolder, int32_t radioTech)
538 {
539     sptr<ApnItem> apnItem = apnHolder->GetNextRetryApn();
540     if (apnItem == nullptr) {
541         TELEPHONY_LOGE("Slot%{public}d: apnItem is null", slotId_);
542         return false;
543     }
544     if (!multipleConnectionsEnabled_) {
545         if (HasAnyHigherPriorityConnection(apnHolder)) {
546             TELEPHONY_LOGE("Slot%{public}d: has higher priority connection", slotId_);
547             return false;
548         }
549         ApnProfileState apnState = apnManager_->GetOverallApnState();
550         if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
551             apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
552             ClearAllConnections(DisConnectionReason::REASON_CHANGE_CONNECTION);
553             return false;
554         }
555     }
556     std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine = FindIdleCellularDataConnection();
557     if (cellularDataStateMachine == nullptr) {
558         cellularDataStateMachine = CreateCellularDataConnect();
559         if (cellularDataStateMachine == nullptr) {
560             TELEPHONY_LOGE("Slot%{public}d: cellularDataStateMachine is null", slotId_);
561             return false;
562         }
563         cellularDataStateMachine->Init();
564         if (connectionManager_ == nullptr) {
565             TELEPHONY_LOGE("Slot%{public}d: in EstablishDataConnection connectionManager_ is null", slotId_);
566             return false;
567         }
568         connectionManager_->AddConnectionStateMachine(cellularDataStateMachine);
569     }
570     cellularDataStateMachine->SetCapability(apnHolder->GetCapability());
571     apnHolder->SetCurrentApn(apnItem);
572     apnHolder->SetApnState(PROFILE_STATE_CONNECTING);
573     apnHolder->SetCellularDataStateMachine(cellularDataStateMachine);
574     bool roamingState = CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0;
575     bool userDataRoaming = false;
576     dataSwitchSettings_->IsUserDataRoamingOn(userDataRoaming);
577     StateNotification::GetInstance().UpdateCellularDataConnectState(slotId_, PROFILE_STATE_CONNECTING, radioTech);
578     std::unique_ptr<DataConnectionParams> object = std::make_unique<DataConnectionParams>(
579         apnHolder, apnItem->attr_.profileId_, radioTech, roamingState, userDataRoaming, true);
580     TELEPHONY_LOGI("Slot%{public}d: MSG_SM_CONNECT profileId:%{public}d type:%{public}s networkType:%{public}d",
581         slotId_, apnItem->attr_.profileId_, apnHolder->GetApnType().c_str(), radioTech);
582     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_SM_CONNECT, object);
583     cellularDataStateMachine->SendEvent(event);
584     return true;
585 }
586 
EstablishDataConnectionComplete(const InnerEvent::Pointer & event)587 void CellularDataHandler::EstablishDataConnectionComplete(const InnerEvent::Pointer &event)
588 {
589     if (event == nullptr) {
590         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
591         return;
592     }
593     std::shared_ptr<SetupDataCallResultInfo> resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
594     if ((resultInfo != nullptr) && (apnManager_ != nullptr)) {
595         sptr<ApnHolder> apnHolder = apnManager_->GetApnHolder(apnManager_->FindApnNameByApnId(resultInfo->flag));
596         if (apnHolder == nullptr) {
597             TELEPHONY_LOGE("Slot%{public}d: flag:%{public}d complete apnHolder is null", slotId_, resultInfo->flag);
598             return;
599         }
600         apnHolder->SetApnState(PROFILE_STATE_CONNECTED);
601         apnHolder->InitialApnRetryCount();
602         int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
603         CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
604         StateNotification::GetInstance().UpdateCellularDataConnectState(slotId_, PROFILE_STATE_CONNECTED, networkType);
605         std::shared_ptr<CellularDataStateMachine> stateMachine = apnHolder->GetCellularDataStateMachine();
606         if (stateMachine != nullptr) {
607             stateMachine->UpdateNetworkInfo(*resultInfo);
608         } else {
609             TELEPHONY_LOGE(
610                 "Slot%{public}d:update network info stateMachine(%{public}d) is null", slotId_, resultInfo->flag);
611         }
612         if (connectionManager_ != nullptr) {
613             connectionManager_->StartStallDetectionTimer();
614             connectionManager_->BeginNetStatistics();
615         }
616         if (!physicalConnectionActiveState_) {
617             physicalConnectionActiveState_ = true;
618             CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
619         }
620     }
621 }
622 
GetSlotId() const623 int32_t CellularDataHandler::GetSlotId() const
624 {
625     return slotId_;
626 }
627 
DisconnectDataComplete(const InnerEvent::Pointer & event)628 void CellularDataHandler::DisconnectDataComplete(const InnerEvent::Pointer &event)
629 {
630     if ((event == nullptr) || (apnManager_ == nullptr)) {
631         TELEPHONY_LOGE("Slot%{public}d: event or apnManager is null", slotId_);
632         return;
633     }
634     if (connectionManager_ == nullptr) {
635         TELEPHONY_LOGE("Slot%{public}d: in DisconnectDataComplete connectionManager_ is null", slotId_);
636         return;
637     }
638     std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
639     int32_t apnId = apnManager_->FindApnIdByApnName(object->GetApnType());
640     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(apnId);
641     if (apnHolder == nullptr) {
642         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null, apnId is %{public}d", slotId_, apnId);
643         return;
644     }
645     DisConnectionReason reason = object->GetReason();
646     apnHolder->SetApnState(PROFILE_STATE_IDLE);
647     int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
648     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
649     StateNotification::GetInstance().UpdateCellularDataConnectState(slotId_, PROFILE_STATE_IDLE, networkType);
650     TELEPHONY_LOGI("Slot%{public}d: apn type: %{public}s call:%{public}d", slotId_, apnHolder->GetApnType().c_str(),
651         apnHolder->IsDataCallEnabled());
652     bool noActiveConnection = connectionManager_->isNoActiveConnection();
653     if (noActiveConnection && physicalConnectionActiveState_) {
654         physicalConnectionActiveState_ = false;
655         CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
656     } else if (!noActiveConnection && !physicalConnectionActiveState_) {
657         physicalConnectionActiveState_ = true;
658         CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
659     }
660     if (apnHolder->IsDataCallEnabled()) {
661         if (apnHolder->GetApnState() == PROFILE_STATE_IDLE || apnHolder->GetApnState() == PROFILE_STATE_FAILED) {
662             apnHolder->SetCellularDataStateMachine(nullptr);
663         }
664         if (reason == DisConnectionReason::REASON_RETRY_CONNECTION) {
665             int64_t delayTime = apnHolder->GetRetryDelay();
666             TELEPHONY_LOGI("Slot%{public}d: Establish a data connection. The apn type is %{public}s", slotId_,
667                 apnHolder->GetApnType().c_str());
668             SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, apnId, delayTime);
669         }
670     }
671     if (connectionManager_ != nullptr && !apnManager_->HasAnyConnectedState()) {
672         connectionManager_->StopStallDetectionTimer();
673         connectionManager_->EndNetStatistics();
674     }
675     if (reason == DisConnectionReason::REASON_CHANGE_CONNECTION) {
676         HandleSortConnection();
677     }
678 }
679 
HandleSortConnection()680 void CellularDataHandler::HandleSortConnection()
681 {
682     ApnProfileState state = apnManager_->GetOverallApnState();
683     if (state == PROFILE_STATE_IDLE || state == PROFILE_STATE_FAILED) {
684         for (const sptr<ApnHolder> &sortApnHolder : apnManager_->GetSortApnHolder()) {
685             if (sortApnHolder->IsDataCallEnabled()) {
686                 int64_t delayTime = sortApnHolder->GetRetryDelay();
687                 int32_t apnId = apnManager_->FindApnIdByApnName(sortApnHolder->GetApnType());
688                 TELEPHONY_LOGI("Slot%{public}d: HandleSortConnection the apn type is %{public}s", slotId_,
689                     sortApnHolder->GetApnType().c_str());
690                 SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, apnId, delayTime);
691                 break;
692             }
693         }
694     }
695 }
696 
MsgEstablishDataConnection(const InnerEvent::Pointer & event)697 void CellularDataHandler::MsgEstablishDataConnection(const InnerEvent::Pointer &event)
698 {
699     if (apnManager_ == nullptr || event == nullptr) {
700         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or event is null", slotId_);
701         return;
702     }
703     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(event->GetParam());
704     if (apnHolder == nullptr) {
705         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
706         return;
707     }
708     TELEPHONY_LOGI("Slot%{public}d: Establish a data connection. APN holder type:%{public}s call:%{public}d", slotId_,
709         apnHolder->GetApnType().c_str(), apnHolder->IsDataCallEnabled());
710     if (apnHolder->IsDataCallEnabled()) {
711         AttemptEstablishDataConnection(apnHolder);
712     } else {
713         DisConnectionReason reason = DisConnectionReason::REASON_CHANGE_CONNECTION;
714         if (multipleConnectionsEnabled_) {
715             reason = DisConnectionReason::REASON_CLEAR_CONNECTION;
716         }
717         ClearConnection(apnHolder, reason);
718     }
719 }
720 
MsgRequestNetwork(const InnerEvent::Pointer & event)721 void CellularDataHandler::MsgRequestNetwork(const InnerEvent::Pointer &event)
722 {
723     if (apnManager_ == nullptr || event == nullptr) {
724         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or event is null", slotId_);
725         return;
726     }
727     std::unique_ptr<NetRequest> netRequest = event->GetUniqueObject<NetRequest>();
728     if (netRequest == nullptr) {
729         TELEPHONY_LOGE("Slot%{public}d: netRequest is null", slotId_);
730         return;
731     }
732     NetRequest request;
733     request.ident = netRequest->ident;
734     request.capability = netRequest->capability;
735     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
736     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
737     if (apnHolder == nullptr) {
738         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null.", slotId_);
739         return;
740     }
741     if (event->GetParam() == TYPE_REQUEST_NET) {
742         apnHolder->RequestCellularData(request);
743     } else {
744         apnHolder->ReleaseCellularData(request);
745         if (apnHolder->IsDataCallEnabled()) {
746             return;
747         }
748     }
749     InnerEvent::Pointer innerEvent = InnerEvent::Get(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, id);
750     if (!SendEvent(innerEvent)) {
751         TELEPHONY_LOGE("Slot%{public}d: send data connection event failed", slotId_);
752     }
753 }
754 
ProcessEvent(const InnerEvent::Pointer & event)755 void CellularDataHandler::ProcessEvent(const InnerEvent::Pointer &event)
756 {
757     if (event == nullptr) {
758         TELEPHONY_LOGE("Slot%{public}d: start ProcessEvent but event is null!", slotId_);
759         return;
760     }
761     uint32_t eventCode = event->GetInnerEventId();
762     std::map<uint32_t, Fun>::iterator it = eventIdMap_.find(eventCode);
763     if (it != eventIdMap_.end()) {
764         (this->*(it->second))(event);
765     }
766 }
767 
OnReceiveEvent(const EventFwk::CommonEventData & data)768 void CellularDataHandler::OnReceiveEvent(const EventFwk::CommonEventData &data)
769 {
770     const AAFwk::Want &want = data.GetWant();
771     std::string action = want.GetAction();
772     int32_t slotId = want.GetIntParam("slotId", 0);
773     TELEPHONY_LOGI("[slot%{public}d] action=%{public}s code=%{public}d", slotId_, action.c_str(), data.GetCode());
774     if (EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED == action) {
775         if (slotId_ != slotId) {
776             return;
777         }
778         int32_t state = want.GetIntParam("state", CALL_STATUS_UNKNOWN);
779         if (state == CALL_STATUS_UNKNOWN) {
780             TELEPHONY_LOGE("Slot%{public}d: unknown call state=%{public}d", slotId, state);
781             return;
782         }
783         HandleVoiceCallChanged(state);
784     } else if (action == CommonEventSupport::COMMON_EVENT_SIM_CARD_DEFAULT_DATA_SUBSCRIPTION_CHANGED) {
785         HandleDefaultDataSubscriptionChanged();
786     } else if (action == CommonEventSupport::COMMON_EVENT_OPERATOR_CONFIG_CHANGED) {
787         if (slotId_ != slotId) {
788             return;
789         }
790         GetConfigurationFor5G();
791     } else {
792         TELEPHONY_LOGI("Slot%{public}d: action=%{public}s code=%{public}d", slotId_, action.c_str(), data.GetCode());
793     }
794 }
795 
HandleSettingSwitchChanged(const InnerEvent::Pointer & event)796 void CellularDataHandler::HandleSettingSwitchChanged(const InnerEvent::Pointer &event)
797 {
798     if (event == nullptr) {
799         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
800         return;
801     }
802     bool setting_switch = event->GetParam();
803     TELEPHONY_LOGI("Slot%{public}d: setting switch = %{public}d", slotId_, setting_switch);
804 }
805 
HandleVoiceCallChanged(int32_t state)806 void CellularDataHandler::HandleVoiceCallChanged(int32_t state)
807 {
808     if (apnManager_ == nullptr || connectionManager_ == nullptr) {
809         TELEPHONY_LOGE("Slot%{public}d: apnManager or connectionManager is null!", slotId_);
810         return;
811     }
812     TELEPHONY_LOGI("Slot%{public}d: handle voice call changed lastState:%{public}d, state:%{public}d", slotId_,
813         lastCallState_, state);
814     // next to check if radio technology support voice and data at same time.
815     int32_t psRadioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
816     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, psRadioTech);
817     bool support = (psRadioTech == static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_GSM));
818     if (lastCallState_ != state) {
819         // call is busy
820         lastCallState_ = state;
821         if (state != TelCallStatus::CALL_STATUS_IDLE && state != TelCallStatus::CALL_STATUS_DISCONNECTED) {
822             if (apnManager_->HasAnyConnectedState() && support) {
823                 connectionManager_->EndNetStatistics();
824                 connectionManager_->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_DORMANT);
825                 connectionManager_->StopStallDetectionTimer();
826                 disconnectionReason_ = DisConnectionReason::REASON_GSM_AND_CALLING_ONLY;
827             }
828         } else {
829             if (apnManager_->HasAnyConnectedState() && support) {
830                 connectionManager_->StartStallDetectionTimer();
831                 connectionManager_->BeginNetStatistics();
832             }
833             disconnectionReason_ = DisConnectionReason::REASON_NORMAL;
834             TELEPHONY_LOGI("Slot%{public}d: HandleVoiceCallChanged EstablishAllApnsIfConnectable", slotId_);
835             EstablishAllApnsIfConnectable();
836         }
837         TELEPHONY_LOGI("Slot%{public}d: disconnectionReason_=%{public}d", slotId_, disconnectionReason_);
838     }
839 }
840 
HandleDefaultDataSubscriptionChanged()841 void CellularDataHandler::HandleDefaultDataSubscriptionChanged()
842 {
843     TELEPHONY_LOGI("Slot%{public}d: HandleDefaultDataSubscriptionChanged", slotId_);
844     if (CheckCellularDataSlotId()) {
845         SetDataPermitted(slotId_, true);
846     } else {
847         SetDataPermitted(slotId_, false);
848     }
849     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
850     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
851     if (defSlotId == slotId_) {
852         EstablishAllApnsIfConnectable();
853     } else {
854         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
855     }
856 }
857 
HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer & event)858 void CellularDataHandler::HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer &event)
859 {
860     if (event == nullptr) {
861         return;
862     }
863     std::u16string iccId;
864     uint32_t eventId = event->GetInnerEventId();
865     switch (eventId) {
866         case RadioEvent::RADIO_SIM_STATE_CHANGE: {
867             SimState simState = SimState::SIM_STATE_UNKNOWN;
868             CoreManagerInner::GetInstance().GetSimState(slotId_, simState);
869             TELEPHONY_LOGI("Slot%{public}d: sim state is :%{public}d", slotId_, simState);
870             if (simState != SimState::SIM_STATE_READY) {
871                 ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
872                 if (simState == SimState::SIM_STATE_NOT_PRESENT) {
873                     UnRegisterDataSettingObserver();
874                 }
875             } else {
876                 dataSwitchSettings_->LoadSwitchValue();
877                 CoreManagerInner::GetInstance().GetSimIccId(slotId_, iccId);
878                 if (lastIccId_ != u"" && lastIccId_ == iccId) {
879                     EstablishAllApnsIfConnectable();
880                 }
881             }
882             break;
883         }
884         case RadioEvent::RADIO_SIM_RECORDS_LOADED: {
885             CoreManagerInner::GetInstance().GetSimIccId(slotId_, iccId);
886             SimState simState = SimState::SIM_STATE_UNKNOWN;
887             CoreManagerInner::GetInstance().GetSimState(slotId_, simState);
888             TELEPHONY_LOGI("Slot%{public}d: sim records loaded state is :%{public}d", slotId_, simState);
889             if (simState == SimState::SIM_STATE_READY && iccId != u"") {
890                 if (iccId != lastIccId_) {
891                     dataSwitchSettings_->SetPolicyDataOn(true);
892                     GetConfigurationFor5G();
893                     lastIccId_ = iccId;
894                     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_APN_CHANGED);
895                     SendEvent(event);
896                 } else if (lastIccId_ == iccId) {
897                     TELEPHONY_LOGI("Slot%{public}d: sim state changed, but iccId not changed.", slotId_);
898                     // the sim card status has changed to ready, so try to connect
899                     EstablishAllApnsIfConnectable();
900                 }
901             }
902             break;
903         }
904         default:
905             break;
906     }
907 }
908 
HandleSimAccountLoaded(const InnerEvent::Pointer & event)909 void CellularDataHandler::HandleSimAccountLoaded(const InnerEvent::Pointer &event)
910 {
911     if (event == nullptr || dataSwitchSettings_ == nullptr) {
912         TELEPHONY_LOGE("Slot%{public}d: event or dataSwitchSettings_ is null", slotId_);
913         return;
914     }
915     CellularDataNetAgent::GetInstance().UnregisterNetSupplier(slotId_);
916     CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_);
917     if (slotId_ == 0) {
918         CellularDataNetAgent::GetInstance().UnregisterPolicyCallback();
919         CellularDataNetAgent::GetInstance().RegisterPolicyCallback();
920     }
921     RegisterDataSettingObserver();
922     dataSwitchSettings_->LoadSwitchValue();
923     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
924     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
925     if (defSlotId == slotId_) {
926         EstablishAllApnsIfConnectable();
927     } else {
928         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
929     }
930 }
931 
HandleApnChanged()932 bool CellularDataHandler::HandleApnChanged()
933 {
934     if (apnManager_ == nullptr) {
935         TELEPHONY_LOGE("apnManager is null");
936         return false;
937     }
938     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
939         TELEPHONY_LOGI("Slot%{public}d: apn type:%{public}s state:%{public}d", slotId_, apnHolder->GetApnType().c_str(),
940             apnHolder->GetApnState());
941     }
942     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_APN_CHANGED);
943     if (event == nullptr) {
944         TELEPHONY_LOGE("Slot%{public}d: get apn changed event is null", slotId_);
945         return false;
946     }
947     return SendEvent(event);
948 }
949 
HandleApnChanged(const InnerEvent::Pointer & event)950 void CellularDataHandler::HandleApnChanged(const InnerEvent::Pointer &event)
951 {
952     if (apnManager_ == nullptr) {
953         TELEPHONY_LOGE("Slot%{public}d: apnManager_ is null", slotId_);
954         return;
955     }
956     std::u16string operatorNumeric;
957     CoreManagerInner::GetInstance().GetSimOperatorNumeric(slotId_, operatorNumeric);
958     std::string numeric = Str16ToStr8(operatorNumeric);
959     int32_t result = 0;
960     for (int32_t i = 0; i < DEFAULT_READ_APN_TIME; ++i) {
961         result = apnManager_->CreateAllApnItemByDatabase(numeric);
962         if (result != 0) {
963             break;
964         }
965     }
966     if (result == 0) {
967         apnManager_->CreateAllApnItem();
968     }
969     SetRilAttachApn();
970     ApnProfileState apnState = apnManager_->GetOverallApnState();
971     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
972         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
973     }
974     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
975         if (apnHolder == nullptr) {
976             continue;
977         }
978         int32_t id = apnManager_->FindApnIdByApnName(apnHolder->GetApnType());
979         SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, id, ESTABLISH_DATA_CONNECTION_DELAY);
980     }
981 }
982 
GetCellularDataFlowType()983 int32_t CellularDataHandler::GetCellularDataFlowType()
984 {
985     if (connectionManager_ == nullptr) {
986         TELEPHONY_LOGE("Slot%{public}d: connection manager is null", slotId_);
987         return 0;
988     }
989     return connectionManager_->GetDataFlowType();
990 }
991 
HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer & event)992 void CellularDataHandler::HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
993 {
994     if (apnManager_ == nullptr || event == nullptr) {
995         TELEPHONY_LOGE("Slot%{public}d: radio off or not available apnManager or event is null!", slotId_);
996         return;
997     }
998     std::shared_ptr<HRilInt32Parcel> object = event->GetSharedObject<HRilInt32Parcel>();
999     if (object == nullptr) {
1000         TELEPHONY_LOGE("Slot%{public}d: HandleRadioStateChanged object is nullptr!", slotId_);
1001         return;
1002     }
1003     TELEPHONY_LOGI("Slot%{public}d: Radio changed with state: %{public}d", slotId_, object->data);
1004     switch (object->data) {
1005         case CORE_SERVICE_POWER_OFF:
1006         case CORE_SERVICE_POWER_NOT_AVAILABLE: {
1007             ApnProfileState apnState = apnManager_->GetOverallApnState();
1008             TELEPHONY_LOGI("Slot%{public}d: apn state is %{public}d", slotId_, apnState);
1009             if (apnState != ApnProfileState::PROFILE_STATE_IDLE) {
1010                 ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1011             }
1012             break;
1013         }
1014         case CORE_SERVICE_POWER_ON:
1015             SetRilLinkBandwidths();
1016             EstablishAllApnsIfConnectable();
1017             break;
1018         default:
1019             TELEPHONY_LOGI("Slot%{public}d: un-handle state:%{public}d", slotId_, object->data);
1020             break;
1021     }
1022 }
1023 
HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer & event)1024 void CellularDataHandler::HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer &event)
1025 {
1026     if (event == nullptr) {
1027         TELEPHONY_LOGE("Slot%{public}d: event is null!", slotId_);
1028         return;
1029     }
1030     std::shared_ptr<HRilInt32Parcel> object = event->GetSharedObject<HRilInt32Parcel>();
1031     if (object == nullptr) {
1032         TELEPHONY_LOGE("Slot%{public}d: object is null!", slotId_);
1033         return;
1034     }
1035     TELEPHONY_LOGI("Slot%{public}d: DSDS changed with mode: %{public}d", slotId_, object->data);
1036     int32_t dsdsMode = DSDS_MODE_V2;
1037     CoreManagerInner::GetInstance().GetDsdsMode(dsdsMode);
1038     if (object->data == dsdsMode) {
1039         TELEPHONY_LOGE("Slot%{public}d: DSDS mode is the same!", slotId_);
1040         return;
1041     }
1042     if (object->data != DSDS_MODE_V2 && object->data != DSDS_MODE_V3) {
1043         TELEPHONY_LOGE("Slot%{public}d: DSDS mode is illegal!", slotId_);
1044         return;
1045     }
1046     CoreManagerInner::GetInstance().SetDsdsMode(object->data);
1047     int32_t defaultSlotId = CoreManagerInner::GetInstance().GetDefaultCellularDataSlotId();
1048     int32_t simNum = CoreManagerInner::GetInstance().GetMaxSimCount();
1049     for (int32_t i = 0; i < simNum; ++i) {
1050         if (defaultSlotId != i && object->data != DSDS_MODE_V3) {
1051             SetDataPermitted(i, false);
1052         } else {
1053             SetDataPermitted(i, true);
1054         }
1055     }
1056 }
1057 
PsDataRatChanged(const InnerEvent::Pointer & event)1058 void CellularDataHandler::PsDataRatChanged(const InnerEvent::Pointer &event)
1059 {
1060     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1061     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
1062     coreInner.GetPsRadioTech(slotId_, radioTech);
1063     TELEPHONY_LOGI("Slot%{public}d: radioTech is %{public}d", slotId_, radioTech);
1064     if (event == nullptr) {
1065         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
1066         return;
1067     }
1068     bool dataEnabled = false;
1069     IsCellularDataEnabled(dataEnabled);
1070     if (!dataEnabled) {
1071         TELEPHONY_LOGE("Slot%{public}d: data enable is close", slotId_);
1072         return;
1073     }
1074     bool attached = coreInner.GetPsRegState(slotId_) == (int32_t)RegServiceState::REG_STATE_IN_SERVICE;
1075     if (!attached) {
1076         TELEPHONY_LOGE("Slot%{public}d: attached is false", slotId_);
1077         return;
1078     }
1079     ApnProfileState apnState = apnManager_->GetOverallApnState();
1080     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
1081         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
1082     }
1083     EstablishAllApnsIfConnectable();
1084 }
1085 
SetPolicyDataOn(bool enable)1086 void CellularDataHandler::SetPolicyDataOn(bool enable)
1087 {
1088     if (dataSwitchSettings_ == nullptr) {
1089         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings is null", slotId_);
1090         return;
1091     }
1092     bool policyDataOn = dataSwitchSettings_->IsPolicyDataOn();
1093     if (policyDataOn != enable) {
1094         dataSwitchSettings_->SetPolicyDataOn(enable);
1095         if (enable) {
1096             EstablishAllApnsIfConnectable();
1097         } else {
1098             ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1099         }
1100     }
1101 }
1102 
IsRestrictedMode() const1103 bool CellularDataHandler::IsRestrictedMode() const
1104 {
1105     int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
1106     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
1107     bool support = (networkType == (int32_t)RadioTech::RADIO_TECHNOLOGY_GSM);
1108     bool inCall = (lastCallState_ != TelCallStatus::CALL_STATUS_IDLE &&
1109                    lastCallState_ != TelCallStatus::CALL_STATUS_DISCONNECTED);
1110     TELEPHONY_LOGI("Slot%{public}d: radio technology is gsm only:%{public}d and call is busy:%{public}d", slotId_,
1111         support, inCall);
1112     return inCall && support;
1113 }
1114 
GetDisConnectionReason()1115 DisConnectionReason CellularDataHandler::GetDisConnectionReason()
1116 {
1117     return disconnectionReason_;
1118 }
1119 
SetDataPermitted(int32_t slotId,bool dataPermitted)1120 void CellularDataHandler::SetDataPermitted(int32_t slotId, bool dataPermitted)
1121 {
1122     TELEPHONY_LOGI("Slot%{public}d: dataPermitted is %{public}d.", slotId, dataPermitted);
1123     int32_t maxSimCount = CoreManagerInner::GetInstance().GetMaxSimCount();
1124     if (maxSimCount <= 1) {
1125         TELEPHONY_LOGE("Slot%{public}d: maxSimCount is: %{public}d", slotId_, maxSimCount);
1126         return;
1127     }
1128     CoreManagerInner::GetInstance().SetDataPermitted(
1129         slotId, CellularDataEventCode::MSG_SET_DATA_PERMITTED, dataPermitted, shared_from_this());
1130 }
1131 
SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer & event)1132 void CellularDataHandler::SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer &event)
1133 {
1134     if (event == nullptr) {
1135         TELEPHONY_LOGE("SetDataPermittedResponse:Slot%{public}d: event is null", slotId_);
1136         return;
1137     }
1138     std::shared_ptr<TelRilResponseInfo<int32_t>> rilInfo = event->GetSharedObject<TelRilResponseInfo<int32_t>>();
1139     if (rilInfo == nullptr) {
1140         TELEPHONY_LOGE("SetDataPermittedResponse:Slot%{public}d: HRilRadioResponseInfo is null", slotId_);
1141         return;
1142     }
1143     if (rilInfo->errorNo != 0) {
1144         TELEPHONY_LOGE("Slot%{public}d: SetDataPermitted error", slotId_);
1145     }
1146 }
1147 
SetRilAttachApn()1148 void CellularDataHandler::SetRilAttachApn()
1149 {
1150     sptr<ApnItem> attachApn = apnManager_->GetRilAttachApn();
1151     if (attachApn == nullptr) {
1152         TELEPHONY_LOGE("Slot%{public}d: attachApn is null", slotId_);
1153         return;
1154     }
1155     DataProfile dataProfile;
1156     dataProfile.profileId = attachApn->attr_.profileId_;
1157     dataProfile.apn = attachApn->attr_.apn_;
1158     dataProfile.protocol = attachApn->attr_.protocol_;
1159     dataProfile.verType = attachApn->attr_.authType_;
1160     dataProfile.userName = attachApn->attr_.user_;
1161     dataProfile.password = attachApn->attr_.password_;
1162     dataProfile.roamingProtocol = attachApn->attr_.roamingProtocol_;
1163     CoreManagerInner::GetInstance().SetInitApnInfo(
1164         slotId_, CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, dataProfile, shared_from_this());
1165 }
1166 
SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer & event)1167 void CellularDataHandler::SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer &event)
1168 {
1169     if (event == nullptr) {
1170         TELEPHONY_LOGE("SetRilAttachApnResponse:Slot%{public}d: event is null", slotId_);
1171         return;
1172     }
1173     std::shared_ptr<TelRilResponseInfo<int32_t>> rilInfo = event->GetSharedObject<TelRilResponseInfo<int32_t>>();
1174     if (rilInfo == nullptr) {
1175         TELEPHONY_LOGE("SetRilAttachApnResponse:Slot%{public}d: HRilRadioResponseInfo is null", slotId_);
1176         return;
1177     }
1178     if (rilInfo->errorNo != 0) {
1179         TELEPHONY_LOGE("Slot%{public}d: SetRilAttachApn error", slotId_);
1180     }
1181 }
1182 
HasAnyHigherPriorityConnection(const sptr<ApnHolder> & apnHolder)1183 bool CellularDataHandler::HasAnyHigherPriorityConnection(const sptr<ApnHolder> &apnHolder)
1184 {
1185     if (apnManager_ == nullptr) {
1186         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
1187         return false;
1188     }
1189     std::vector<sptr<ApnHolder>> sortApnHolders = apnManager_->GetSortApnHolder();
1190     if (sortApnHolders.empty()) {
1191         TELEPHONY_LOGE("Slot%{public}d: SortApnHolder is null", slotId_);
1192         return false;
1193     }
1194     for (const sptr<ApnHolder> &sortApnHolder : sortApnHolders) {
1195         if (sortApnHolder->GetPriority() > apnHolder->GetPriority()) {
1196             if (sortApnHolder->IsDataCallEnabled() &&
1197                 (sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTED ||
1198                     sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTING ||
1199                     sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_DISCONNECTING)) {
1200                 CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
1201                     CellularDataErrorCode::DATA_ERROR_HAS_HIGHER_PRIORITY_CONNECTION,
1202                     "There is higher priority connection");
1203                 return true;
1204             }
1205         }
1206     }
1207     return false;
1208 }
1209 
HasInternetCapability(const int32_t cid) const1210 bool CellularDataHandler::HasInternetCapability(const int32_t cid) const
1211 {
1212     if (connectionManager_ == nullptr) {
1213         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1214         return false;
1215     }
1216     std::shared_ptr<CellularDataStateMachine> activeStateMachine = connectionManager_->GetActiveConnectionByCid(cid);
1217     if (activeStateMachine == nullptr) {
1218         TELEPHONY_LOGE("Slot%{public}d: get activeStateMachine by cid fail", slotId_);
1219         return false;
1220     }
1221     uint64_t netCapability = activeStateMachine->GetCapability();
1222     if (netCapability == NetCap::NET_CAPABILITY_INTERNET) {
1223         return true;
1224     }
1225     return false;
1226 }
1227 
GetConfigurationFor5G()1228 void CellularDataHandler::GetConfigurationFor5G()
1229 {
1230     // get 5G configurations
1231     unMeteredAllNsaConfig_ = ParseOperatorConfig(u"allmeterednas");
1232     unMeteredNrNsaMmwaveConfig_ = ParseOperatorConfig(u"meterednrnsammware");
1233     unMeteredNrNsaSub6Config_ = ParseOperatorConfig(u"meteredNrnsasub6");
1234     unMeteredAllNrsaConfig_ = ParseOperatorConfig(u"meteredallnrsa");
1235     unMeteredNrsaMmwaveConfig_ = ParseOperatorConfig(u"meterednrsammware");
1236     unMeteredNrsaSub6Config_ = ParseOperatorConfig(u"meterednrsasub6");
1237     unMeteredRoamingConfig_ = ParseOperatorConfig(u"meteredroaming");
1238     GetDefaultConfiguration();
1239 }
1240 
ParseOperatorConfig(const std::u16string & configName)1241 bool CellularDataHandler::ParseOperatorConfig(const std::u16string &configName)
1242 {
1243     OperatorConfig configsFor5G;
1244     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, configsFor5G);
1245     if (configsFor5G.configValue.count(configName) > 0) {
1246         std::string flag = Str16ToStr8(configsFor5G.configValue[configName]);
1247         TELEPHONY_LOGI("Slot%{public}d: parse operator 5G config: %{public}s", slotId_, flag.c_str());
1248         if (flag == "true") {
1249             return true;
1250         }
1251     }
1252     return false;
1253 }
1254 
GetDefaultConfiguration()1255 void CellularDataHandler::GetDefaultConfiguration()
1256 {
1257     if (connectionManager_ == nullptr) {
1258         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1259         return;
1260     }
1261     connectionManager_->GetDefaultBandWidthsConfig();
1262     connectionManager_->GetDefaultTcpBufferConfig();
1263     GetDefaultUpLinkThresholdsConfig();
1264     GetDefaultDownLinkThresholdsConfig();
1265     defaultMobileMtuConfig_ = CellularDataUtils::GetDefaultMobileMtuConfig();
1266     TELEPHONY_LOGI("Slot%{public}d: defaultMobileMtuConfig_ = %{public}d", slotId_, defaultMobileMtuConfig_);
1267     defaultPreferApn_ = CellularDataUtils::GetDefaultPreferApnConfig();
1268     TELEPHONY_LOGI("Slot%{public}d: defaultPreferApn_ is %{public}d", slotId_, defaultPreferApn_);
1269     multipleConnectionsEnabled_ = CellularDataUtils::GetDefaultMultipleConnectionsConfig();
1270     TELEPHONY_LOGI("Slot%{public}d: multipleConnectionsEnabled_ = %{public}d", slotId_, multipleConnectionsEnabled_);
1271 }
1272 
HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer & event)1273 void CellularDataHandler::HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
1274 {
1275     if (connectionManager_ == nullptr) {
1276         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1277         return;
1278     }
1279     TELEPHONY_LOGI("Slot%{public}d: receive HandleRadioNrStateChanged event", slotId_);
1280     std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines =
1281         connectionManager_->GetAllConnectionMachine();
1282     for (const std::shared_ptr<CellularDataStateMachine> &cellularDataStateMachine : stateMachines) {
1283         InnerEvent::Pointer eventCode = InnerEvent::Get(RadioEvent::RADIO_NR_STATE_CHANGED);
1284         cellularDataStateMachine->SendEvent(eventCode);
1285     }
1286 }
1287 
HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer & event)1288 void CellularDataHandler::HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event)
1289 {
1290     if (connectionManager_ == nullptr) {
1291         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1292         return;
1293     }
1294     TELEPHONY_LOGI("Slot%{public}d: receive HandleRadioNrFrequencyChanged event", slotId_);
1295     std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines =
1296         connectionManager_->GetAllConnectionMachine();
1297     for (const std::shared_ptr<CellularDataStateMachine> &cellularDataStateMachine : stateMachines) {
1298         InnerEvent::Pointer eventCode = InnerEvent::Get(RadioEvent::RADIO_NR_FREQUENCY_CHANGED);
1299         cellularDataStateMachine->SendEvent(eventCode);
1300     }
1301 }
1302 
GetDefaultUpLinkThresholdsConfig()1303 void CellularDataHandler::GetDefaultUpLinkThresholdsConfig()
1304 {
1305     upLinkThresholds_.clear();
1306     char upLinkConfig[UP_DOWN_LINK_SIZE] = { 0 };
1307     GetParameter(CONFIG_UPLINK_THRESHOLDS, CAPACITY_THRESHOLDS_FOR_UPLINK, upLinkConfig, UP_DOWN_LINK_SIZE);
1308     TELEPHONY_LOGI("Slot%{public}d: upLinkThresholds = %{public}s", slotId_, upLinkConfig);
1309     upLinkThresholds_ = CellularDataUtils::Split(upLinkConfig, ",");
1310 }
1311 
GetDefaultDownLinkThresholdsConfig()1312 void CellularDataHandler::GetDefaultDownLinkThresholdsConfig()
1313 {
1314     downLinkThresholds_.clear();
1315     char downLinkConfig[UP_DOWN_LINK_SIZE] = { 0 };
1316     GetParameter(CONFIG_DOWNLINK_THRESHOLDS, CAPACITY_THRESHOLDS_FOR_DOWNLINK, downLinkConfig, UP_DOWN_LINK_SIZE);
1317     TELEPHONY_LOGI("Slot%{public}d: downLinkThresholds_ = %{public}s", slotId_, downLinkConfig);
1318     downLinkThresholds_ = CellularDataUtils::Split(downLinkConfig, ",");
1319 }
1320 
SetRilLinkBandwidths()1321 void CellularDataHandler::SetRilLinkBandwidths()
1322 {
1323     LinkBandwidthRule linkBandwidth;
1324     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, linkBandwidth.rat);
1325     linkBandwidth.delayMs = DELAY_SET_RIL_BANDWIDTH_MS;
1326     linkBandwidth.delayUplinkKbps = DELAY_SET_RIL_UP_DOWN_BANDWIDTH_MS;
1327     linkBandwidth.delayDownlinkKbps = DELAY_SET_RIL_UP_DOWN_BANDWIDTH_MS;
1328     for (std::string upLinkThreshold : upLinkThresholds_) {
1329         linkBandwidth.maximumUplinkKbps.push_back(atoi(upLinkThreshold.c_str()));
1330     }
1331     for (std::string downLinkThreshold : downLinkThresholds_) {
1332         linkBandwidth.maximumDownlinkKbps.push_back(atoi(downLinkThreshold.c_str()));
1333     }
1334     CoreManagerInner::GetInstance().SetLinkBandwidthReportingRule(
1335         slotId_, CellularDataEventCode::MSG_SET_RIL_BANDWIDTH, linkBandwidth, shared_from_this());
1336 }
1337 
HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer & event)1338 void CellularDataHandler::HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer &event)
1339 {
1340     if (dataSwitchSettings_ == nullptr) {
1341         TELEPHONY_LOGE("Slot%{public}d: HandleDBSettingEnableChanged dataSwitchSettings_ is null.", slotId_);
1342         return;
1343     }
1344     bool dataEnabled = false;
1345     dataSwitchSettings_->IsUserDataOn(dataEnabled);
1346     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1347     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
1348     if (dataEnabled && defSlotId == slotId_) {
1349         EstablishAllApnsIfConnectable();
1350         if (apnManager_ == nullptr) {
1351             TELEPHONY_LOGE("Slot%{public}d: apnManager is null.", slotId_);
1352             return;
1353         }
1354         const int32_t id = DATA_CONTEXT_ROLE_DEFAULT_ID;
1355         sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
1356         if (apnHolder == nullptr) {
1357             TELEPHONY_LOGE("Slot%{public}d: apnHolder is null.", slotId_);
1358             return;
1359         }
1360         if (!apnHolder->IsDataCallEnabled()) {
1361             NetRequest netRequest;
1362             netRequest.ident =
1363                 std::string(IDENT_PREFIX) + std::to_string(CoreManagerInner::GetInstance().GetSimId(slotId_));
1364             netRequest.capability = NetCap::NET_CAPABILITY_INTERNET;
1365             apnHolder->RequestCellularData(netRequest);
1366             AttemptEstablishDataConnection(apnHolder);
1367             return;
1368         }
1369     } else {
1370         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1371     }
1372 }
1373 
HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer & event)1374 void CellularDataHandler::HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer &event)
1375 {
1376     if (event == nullptr) {
1377         return;
1378     }
1379     if (dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
1380         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
1381         return;
1382     }
1383     int64_t value = event->GetParam();
1384     bool dataRoamingEnabled = false;
1385     dataSwitchSettings_->IsUserDataRoamingOn(dataRoamingEnabled);
1386     if (dataRoamingEnabled != value) {
1387         dataSwitchSettings_->SetUserDataRoamingOn(value);
1388         bool roamingState = false;
1389         if (CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0) {
1390             roamingState = true;
1391         }
1392         if (roamingState) {
1393             ApnProfileState apnState = apnManager_->GetOverallApnState();
1394             if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
1395                 apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
1396                 ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
1397             }
1398             EstablishAllApnsIfConnectable();
1399         } else {
1400             TELEPHONY_LOGI("Slot%{public}d: Not roaming(%{public}d), not doing anything", slotId_, roamingState);
1401         }
1402     } else {
1403         TELEPHONY_LOGI("Slot%{public}d: The roaming switch status has not changed", slotId_);
1404     }
1405 }
1406 
UnRegisterDataSettingObserver()1407 void CellularDataHandler::UnRegisterDataSettingObserver()
1408 {
1409     if (settingObserver_ == nullptr || roamingObserver_ == nullptr) {
1410         TELEPHONY_LOGE(
1411             "UnRegisterDataSettingObserver:Slot%{public}d: settingObserver_ or roamingObserver_ is null", slotId_);
1412         return;
1413     }
1414     std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
1415     if (settingHelper == nullptr) {
1416         TELEPHONY_LOGE("UnRegisterDataSettingObserver:Slot%{public}d: settingHelper is null", slotId_);
1417         return;
1418     }
1419     Uri dataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
1420     settingHelper->UnRegisterSettingsObserver(dataEnableUri, settingObserver_);
1421 
1422     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
1423     if (simId <= INVALID_SIM_ID) {
1424         TELEPHONY_LOGE(
1425             "UnRegisterDataSettingObserver:Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
1426         return;
1427     }
1428     Uri dataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
1429     settingHelper->UnRegisterSettingsObserver(dataRoamingUri, roamingObserver_);
1430 }
1431 
RegisterDataSettingObserver()1432 void CellularDataHandler::RegisterDataSettingObserver()
1433 {
1434     if (settingObserver_ == nullptr || roamingObserver_ == nullptr) {
1435         TELEPHONY_LOGE(
1436             "RegisterDataSettingObserver:Slot%{public}d: settingObserver_ or roamingObserver_ is null", slotId_);
1437         return;
1438     }
1439     std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
1440     if (settingHelper == nullptr) {
1441         TELEPHONY_LOGE("RegisterDataSettingObserver:Slot%{public}d: settingHelper is null", slotId_);
1442         return;
1443     }
1444     Uri dataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
1445     settingHelper->RegisterSettingsObserver(dataEnableUri, settingObserver_);
1446 
1447     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
1448     if (simId <= INVALID_SIM_ID) {
1449         TELEPHONY_LOGE(
1450             "RegisterDataSettingObserver:Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
1451         return;
1452     }
1453     Uri dataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
1454     settingHelper->RegisterSettingsObserver(dataRoamingUri, roamingObserver_);
1455 }
1456 } // namespace Telephony
1457 } // namespace OHOS
1458