• 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_service.h"
22 #include "cellular_data_settings_rdb_helper.h"
23 #include "cellular_data_types.h"
24 #include "cellular_data_utils.h"
25 #include "common_event_manager.h"
26 #include "common_event_support.h"
27 #include "core_manager_inner.h"
28 #include "hitrace_meter.h"
29 #include "hril_call_parcel.h"
30 #include "net_specifier.h"
31 #include "radio_event.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;
42 static const int32_t ESM_FLAG_INVALID = -1;
CellularDataHandler(const EventFwk::CommonEventSubscribeInfo & sp,int32_t slotId)43 CellularDataHandler::CellularDataHandler(const EventFwk::CommonEventSubscribeInfo &sp, int32_t slotId)
44     : TelEventHandler("CellularDataHandler"), 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>(slotId_).release();
52     settingObserver_ = new (std::nothrow) CellularDataSettingObserver(
53         std::weak_ptr<TelEventHandler>(std::static_pointer_cast<TelEventHandler>(shared_from_this())));
54     roamingObserver_ = new (std::nothrow) CellularDataRoamingObserver(
55         std::weak_ptr<TelEventHandler>(std::static_pointer_cast<TelEventHandler>(shared_from_this())), slotId_);
56     incallObserver_ = new (std::nothrow) CellularDataIncallObserver(
57         std::weak_ptr<TelEventHandler>(std::static_pointer_cast<TelEventHandler>(shared_from_this())));
58     cellularDataRdbObserver_ = new (std::nothrow) CellularDataRdbObserver(
59         std::weak_ptr<TelEventHandler>(std::static_pointer_cast<TelEventHandler>(shared_from_this())));
60     if ((apnManager_ == nullptr) || (dataSwitchSettings_ == nullptr) || (connectionManager_ == nullptr)) {
61         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or dataSwitchSettings_ or connectionManager_ is null", slotId_);
62         return;
63     }
64     connectionManager_->Init();
65     apnManager_->InitApnHolders();
66     apnManager_->CreateAllApnItem();
67     dataSwitchSettings_->LoadSwitchValue();
68     GetConfigurationFor5G();
69     SetRilLinkBandwidths();
70 }
71 
~CellularDataHandler()72 CellularDataHandler::~CellularDataHandler()
73 {
74     UnRegisterDataSettingObserver();
75 }
76 
ReleaseNet(const NetRequest & request)77 bool CellularDataHandler::ReleaseNet(const NetRequest &request)
78 {
79     if (apnManager_ == nullptr) {
80         TELEPHONY_LOGE("Slot%{public}d: apnManager is null.", slotId_);
81         return false;
82     }
83     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
84     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
85     if (apnHolder == nullptr) {
86         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null.", slotId_);
87         return false;
88     }
89     std::unique_ptr<NetRequest> netRequest = std::make_unique<NetRequest>();
90     if (netRequest == nullptr) {
91         TELEPHONY_LOGE("Netrequest is null");
92         return false;
93     }
94     netRequest->capability = request.capability;
95     netRequest->ident = request.ident;
96     AppExecFwk::InnerEvent::Pointer event =
97         InnerEvent::Get(CellularDataEventCode::MSG_REQUEST_NETWORK, netRequest, TYPE_RELEASE_NET);
98     return SendEvent(event);
99 }
100 
RequestNet(const NetRequest & request)101 bool CellularDataHandler::RequestNet(const NetRequest &request)
102 {
103     if (apnManager_ == nullptr) {
104         TELEPHONY_LOGE("Slot%{public}d: apnManager is null.", slotId_);
105         return false;
106     }
107     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
108     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
109     if (apnHolder == nullptr) {
110         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null.", slotId_);
111         return false;
112     }
113     ApnProfileState apnState = apnHolder->GetApnState();
114     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTED || apnState == ApnProfileState::PROFILE_STATE_CONNECTING) {
115         TELEPHONY_LOGD("Slot%{public}d: apn state is connected(%{public}d).", slotId_, apnState);
116         return true;
117     }
118     std::unique_ptr<NetRequest> netRequest = std::make_unique<NetRequest>();
119     if (netRequest == nullptr) {
120         TELEPHONY_LOGE("Netrequest is null");
121         return false;
122     }
123     netRequest->capability = request.capability;
124     netRequest->ident = request.ident;
125     AppExecFwk::InnerEvent::Pointer event =
126         InnerEvent::Get(CellularDataEventCode::MSG_REQUEST_NETWORK, netRequest, TYPE_REQUEST_NET);
127     return SendEvent(event);
128 }
129 
SetCellularDataEnable(bool userDataOn)130 int32_t CellularDataHandler::SetCellularDataEnable(bool userDataOn)
131 {
132     if (dataSwitchSettings_ == nullptr) {
133         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null.", slotId_);
134         return TELEPHONY_ERR_LOCAL_PTR_NULL;
135     }
136     bool dataEnabled = true;
137     int32_t result = dataSwitchSettings_->QueryUserDataStatus(dataEnabled);
138     if (result != TELEPHONY_ERR_SUCCESS) {
139         TELEPHONY_LOGE("Slot%{public}d: Query result: %{public}d", slotId_, result);
140     }
141     if (dataEnabled == userDataOn) {
142         TELEPHONY_LOGI("Slot%{public}d: The status of the cellular data switch has not changed", slotId_);
143         return TELEPHONY_ERR_SUCCESS;
144     }
145     return dataSwitchSettings_->SetUserDataOn(userDataOn);
146 }
147 
SetIntelligenceSwitchEnable(bool userSwitchOn)148 int32_t CellularDataHandler::SetIntelligenceSwitchEnable(bool userSwitchOn)
149 {
150     if (dataSwitchSettings_ == nullptr) {
151         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null.", slotId_);
152         return TELEPHONY_ERR_LOCAL_PTR_NULL;
153     }
154     bool switchEnabled = false;
155     int32_t result = dataSwitchSettings_->QueryIntelligenceSwitchStatus(switchEnabled);
156     if (result != TELEPHONY_ERR_SUCCESS) {
157         TELEPHONY_LOGE("Slot%{public}d: Query result: %{public}d", slotId_, result);
158     }
159     if (switchEnabled == userSwitchOn) {
160         TELEPHONY_LOGI("Slot%{public}d: The status of the cellular data switch has not changed", slotId_);
161         return TELEPHONY_ERR_SUCCESS;
162     }
163     return dataSwitchSettings_->SetIntelliSwitchOn(userSwitchOn);
164 }
165 
IsCellularDataEnabled(bool & dataEnabled) const166 int32_t CellularDataHandler::IsCellularDataEnabled(bool &dataEnabled) const
167 {
168     if (dataSwitchSettings_ == nullptr) {
169         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null", slotId_);
170         return TELEPHONY_ERR_LOCAL_PTR_NULL;
171     }
172     return dataSwitchSettings_->QueryUserDataStatus(dataEnabled);
173 }
174 
IsCellularDataRoamingEnabled(bool & dataRoamingEnabled) const175 int32_t CellularDataHandler::IsCellularDataRoamingEnabled(bool &dataRoamingEnabled) const
176 {
177     if (dataSwitchSettings_ == nullptr) {
178         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null", slotId_);
179         return TELEPHONY_ERR_LOCAL_PTR_NULL;
180     }
181     return dataSwitchSettings_->QueryUserDataRoamingStatus(dataRoamingEnabled);
182 }
183 
SetCellularDataRoamingEnabled(bool dataRoamingEnabled)184 int32_t CellularDataHandler::SetCellularDataRoamingEnabled(bool dataRoamingEnabled)
185 {
186     if (dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
187         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
188         return TELEPHONY_ERR_LOCAL_PTR_NULL;
189     }
190     bool currentDataEnabled = dataSwitchSettings_->IsUserDataRoamingOn();
191     if (currentDataEnabled == dataRoamingEnabled) {
192         TELEPHONY_LOGI("Slot%{public}d: The roaming switch status has not changed", slotId_);
193         return TELEPHONY_ERR_SUCCESS;
194     }
195     int32_t result = dataSwitchSettings_->SetUserDataRoamingOn(dataRoamingEnabled);
196     if (result != TELEPHONY_ERR_SUCCESS) {
197         return result;
198     }
199     bool roamingState = CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0;
200     if (roamingState) {
201         ApnProfileState apnState = apnManager_->GetOverallApnState();
202         if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
203             apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
204             ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
205         }
206         EstablishAllApnsIfConnectable();
207     } else {
208         TELEPHONY_LOGI("Slot%{public}d: Not roaming(%{public}d), not doing anything", slotId_, roamingState);
209     }
210     return TELEPHONY_ERR_SUCCESS;
211 }
212 
ClearAllConnections(DisConnectionReason reason)213 void CellularDataHandler::ClearAllConnections(DisConnectionReason reason)
214 {
215     if (apnManager_ == nullptr) {
216         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
217         return;
218     }
219     ApnProfileState currentState = apnManager_->GetOverallApnState();
220     if (currentState == ApnProfileState::PROFILE_STATE_CONNECTED ||
221         currentState == ApnProfileState::PROFILE_STATE_CONNECTING) {
222         int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
223         CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
224         StateNotification::GetInstance().UpdateCellularDataConnectState(
225             slotId_, PROFILE_STATE_DISCONNECTING, networkType);
226     }
227     for (const sptr<ApnHolder> &apn : apnManager_->GetAllApnHolder()) {
228         ClearConnection(apn, reason);
229     }
230 
231     if (connectionManager_ == nullptr) {
232         TELEPHONY_LOGE("Slot%{public}d: connectionManager_ is null", slotId_);
233         return;
234     }
235     connectionManager_->StopStallDetectionTimer();
236     connectionManager_->EndNetStatistics();
237 
238     ResetDataFlowType();
239 }
240 
ResetDataFlowType()241 void CellularDataHandler::ResetDataFlowType()
242 {
243     if (dataSwitchSettings_ == nullptr) {
244         TELEPHONY_LOGE("Slot%{public}d: in ClearAllConnections dataSwitchSettings_ is null", slotId_);
245         return;
246     }
247     bool dataEnabled = dataSwitchSettings_->IsUserDataOn();
248     if (!dataEnabled) {
249         connectionManager_->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_NONE);
250     }
251 }
252 
ClearConnection(const sptr<ApnHolder> & apn,DisConnectionReason reason)253 void CellularDataHandler::ClearConnection(const sptr<ApnHolder> &apn, DisConnectionReason reason)
254 {
255     if (apn == nullptr) {
256         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
257         return;
258     }
259     std::shared_ptr<CellularDataStateMachine> stateMachine = apn->GetCellularDataStateMachine();
260     if (stateMachine == nullptr) {
261         TELEPHONY_LOGD("Slot%{public}d: stateMachine is null", slotId_);
262         return;
263     }
264     TELEPHONY_LOGI("Slot%{public}d: The APN holder is of type %{public}s", slotId_, apn->GetApnType().c_str());
265     std::unique_ptr<DataDisconnectParams> object = std::make_unique<DataDisconnectParams>(apn->GetApnType(), reason);
266     if (object == nullptr) {
267         TELEPHONY_LOGE("Slot%{public}d: ClearConnection fail, object is null", slotId_);
268         return;
269     }
270     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_SM_DISCONNECT, object);
271     stateMachine->SendEvent(event);
272     apn->SetApnState(PROFILE_STATE_DISCONNECTING);
273     apn->SetCellularDataStateMachine(nullptr);
274 }
275 
GetCellularDataState() const276 ApnProfileState CellularDataHandler::GetCellularDataState() const
277 {
278     if (apnManager_ == nullptr) {
279         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
280         return ApnProfileState::PROFILE_STATE_IDLE;
281     }
282     return apnManager_->GetOverallApnState();
283 }
284 
GetCellularDataState(const std::string & apnType) const285 ApnProfileState CellularDataHandler::GetCellularDataState(const std::string &apnType) const
286 {
287     if (apnManager_ == nullptr) {
288         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
289         return ApnProfileState::PROFILE_STATE_IDLE;
290     }
291     sptr<ApnHolder> apnHolder = apnManager_->GetApnHolder(apnType);
292     if (apnHolder == nullptr) {
293         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
294         return ApnProfileState::PROFILE_STATE_IDLE;
295     }
296     return apnHolder->GetApnState();
297 }
298 
RadioPsConnectionAttached(const InnerEvent::Pointer & event)299 void CellularDataHandler::RadioPsConnectionAttached(const InnerEvent::Pointer &event)
300 {
301     TELEPHONY_LOGI("Slot%{public}d: ps attached", slotId_);
302     if (event == nullptr || apnManager_ == nullptr) {
303         TELEPHONY_LOGE("Slot%{public}d: event or apnManager_ is null", slotId_);
304         return;
305     }
306     EstablishAllApnsIfConnectable();
307 }
308 
RadioPsConnectionDetached(const InnerEvent::Pointer & event)309 void CellularDataHandler::RadioPsConnectionDetached(const InnerEvent::Pointer &event)
310 {
311     TELEPHONY_LOGI("Slot%{public}d: ps detached", slotId_);
312     if (event == nullptr) {
313         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
314         return;
315     }
316     ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
317 }
318 
RoamingStateOn(const InnerEvent::Pointer & event)319 void CellularDataHandler::RoamingStateOn(const InnerEvent::Pointer &event)
320 {
321     TELEPHONY_LOGI("Slot%{public}d: roaming on", slotId_);
322     if (event == nullptr || dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
323         TELEPHONY_LOGE("Slot%{public}d: event or dataSwitchSettings_ or apnManager_ is null", slotId_);
324         return;
325     }
326     bool roamingState = false;
327     if (CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0) {
328         roamingState = true;
329     }
330     if (!roamingState) {
331         TELEPHONY_LOGE("Slot%{public}d: device not currently roaming state", slotId_);
332         return;
333     }
334     ApnProfileState apnState = apnManager_->GetOverallApnState();
335     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
336         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
337     }
338     EstablishAllApnsIfConnectable();
339 }
340 
RoamingStateOff(const InnerEvent::Pointer & event)341 void CellularDataHandler::RoamingStateOff(const InnerEvent::Pointer &event)
342 {
343     TELEPHONY_LOGI("Slot%{public}d: roaming off", slotId_);
344     if (event == nullptr || dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
345         TELEPHONY_LOGE("Slot%{public}d: event or dataSwitchSettings_ or apnManager_ is null", slotId_);
346         return;
347     }
348     ApnProfileState apnState = apnManager_->GetOverallApnState();
349     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
350         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
351     }
352     EstablishAllApnsIfConnectable();
353 }
354 
PsRadioEmergencyStateOpen(const InnerEvent::Pointer & event)355 void CellularDataHandler::PsRadioEmergencyStateOpen(const InnerEvent::Pointer &event)
356 {
357     TELEPHONY_LOGI("Slot%{public}d: emergency on", slotId_);
358     ApnProfileState currentState = apnManager_->GetOverallApnState();
359     if (currentState == ApnProfileState::PROFILE_STATE_CONNECTED ||
360         currentState == ApnProfileState::PROFILE_STATE_CONNECTING) {
361         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
362     }
363 }
364 
PsRadioEmergencyStateClose(const InnerEvent::Pointer & event)365 void CellularDataHandler::PsRadioEmergencyStateClose(const InnerEvent::Pointer &event)
366 {
367     TELEPHONY_LOGI("Slot%{public}d: emergency off", slotId_);
368     ApnProfileState currentState = apnManager_->GetOverallApnState();
369     if (currentState == ApnProfileState::PROFILE_STATE_IDLE ||
370         currentState == ApnProfileState::PROFILE_STATE_DISCONNECTING) {
371         EstablishAllApnsIfConnectable();
372     }
373 }
374 
EstablishAllApnsIfConnectable()375 void CellularDataHandler::EstablishAllApnsIfConnectable()
376 {
377     if (apnManager_ == nullptr) {
378         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
379         return;
380     }
381     for (sptr<ApnHolder> apnHolder : apnManager_->GetSortApnHolder()) {
382         if (apnHolder == nullptr) {
383             TELEPHONY_LOGE("Slot%{public}d: apn is null", slotId_);
384             continue;
385         }
386         if (apnHolder->IsDataCallEnabled()) {
387             ApnProfileState apnState = apnHolder->GetApnState();
388             if (apnState == PROFILE_STATE_FAILED || apnState == PROFILE_STATE_RETRYING) {
389                 apnHolder->ReleaseDataConnection();
390             }
391             if (apnHolder->IsDataCallConnectable()) {
392                 AttemptEstablishDataConnection(apnHolder);
393             }
394         }
395     }
396 }
397 
SetDataPermittedForMms(bool dataPermittedForMms)398 bool CellularDataHandler::SetDataPermittedForMms(bool dataPermittedForMms)
399 {
400     if (incallDataStateMachine_ != nullptr) {
401         TELEPHONY_LOGI("Slot%{public}d: incall data active", slotId_);
402         return false;
403     }
404     if (CheckDataPermittedByDsds()) {
405         TELEPHONY_LOGI("Slot%{public}d: data permitted", slotId_);
406         return false;
407     }
408     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
409     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
410     SetDataPermitted(defSlotId, !dataPermittedForMms);
411     SetDataPermitted(slotId_, dataPermittedForMms);
412     DelayedRefSingleton<CellularDataService>::GetInstance().ChangeConnectionForDsds(defSlotId, !dataPermittedForMms);
413     return true;
414 }
415 
CheckDataPermittedByDsds()416 bool CellularDataHandler::CheckDataPermittedByDsds()
417 {
418     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
419     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
420     int32_t dsdsMode = DSDS_MODE_V2;
421     coreInner.GetDsdsMode(dsdsMode);
422     if (defSlotId != slotId_ && dsdsMode == DSDS_MODE_V2) {
423         TELEPHONY_LOGI("Slot%{public}d: default:%{public}d, current:%{public}d, dsdsMode:%{public}d", slotId_,
424             defSlotId, slotId_, dsdsMode);
425         return false;
426     }
427     return true;
428 }
429 
CheckCellularDataSlotId(sptr<ApnHolder> & apnHolder)430 bool CellularDataHandler::CheckCellularDataSlotId(sptr<ApnHolder> &apnHolder)
431 {
432     if (apnHolder == nullptr) {
433         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
434         return false;
435     }
436     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
437     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
438     std::string apnType = apnHolder->GetApnType();
439     if (defSlotId != slotId_ && !apnType.compare(DATA_CONTEXT_ROLE_DEFAULT)) {
440         TELEPHONY_LOGD("Slot%{public}d: default:%{public}d, current:%{public}d", slotId_, defSlotId, slotId_);
441         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
442             CellularDataErrorCode::DATA_ERROR_CELLULAR_DATA_SLOT_ID_MISMATCH,
443             "Default cellular data slot id is not current slot id");
444         return false;
445     }
446     return true;
447 }
448 
CheckAttachAndSimState(sptr<ApnHolder> & apnHolder)449 bool CellularDataHandler::CheckAttachAndSimState(sptr<ApnHolder> &apnHolder)
450 {
451     if (apnHolder == nullptr) {
452         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
453         return false;
454     }
455     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
456     bool attached = coreInner.GetPsRegState(slotId_) == (int32_t)RegServiceState::REG_STATE_IN_SERVICE;
457     SimState simState = SimState::SIM_STATE_UNKNOWN;
458     coreInner.GetSimState(slotId_, simState);
459     TELEPHONY_LOGD("Slot%{public}d: attached: %{public}d simState: %{public}d", slotId_, attached, simState);
460     bool isMmsApn = apnHolder->IsMmsType();
461     if (isMmsApn && (simState == SimState::SIM_STATE_READY)) {
462         if (SetDataPermittedForMms(true) && !attached) {
463             return false;
464         }
465     }
466     bool isEmergencyApn = apnHolder->IsEmergencyType();
467     if (!isEmergencyApn && !attached) {
468         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
469             CellularDataErrorCode::DATA_ERROR_PS_NOT_ATTACH, "It is not emergencyApn and not attached");
470         return false;
471     }
472     if (!isEmergencyApn && (simState != SimState::SIM_STATE_READY)) {
473         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
474             CellularDataErrorCode::DATA_ERROR_SIM_NOT_READY, "It is not emergencyApn and sim not ready");
475         return false;
476     }
477     return true;
478 }
479 
CheckRoamingState(sptr<ApnHolder> & apnHolder)480 bool CellularDataHandler::CheckRoamingState(sptr<ApnHolder> &apnHolder)
481 {
482     if (dataSwitchSettings_ == nullptr || apnHolder == nullptr) {
483         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
484         return false;
485     }
486     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
487     bool isEmergencyApn = apnHolder->IsEmergencyType();
488     bool isMmsApn = apnHolder->IsMmsType();
489     bool isAllowActiveData = dataSwitchSettings_->IsAllowActiveData();
490     bool roamingState = coreInner.GetPsRoamingState(slotId_) > 0;
491     bool dataRoamingEnabled = dataSwitchSettings_->IsUserDataRoamingOn();
492     if (roamingState && !dataRoamingEnabled) {
493         isAllowActiveData = false;
494     } else if (isMmsApn) {
495         isAllowActiveData = true;
496     }
497     if (isEmergencyApn) {
498         isAllowActiveData = true;
499     }
500     if (!isAllowActiveData) {
501         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
502             CellularDataErrorCode::DATA_ERROR_ROAMING_SWITCH_OFF_AND_ROAMING, "Data roaming is not on and is roaming");
503         TELEPHONY_LOGD("Slot%{public}d: AllowActiveData:%{public}d lastCallState_:%{public}d", slotId_,
504             isAllowActiveData, lastCallState_);
505         return false;
506     }
507     if (IsRestrictedMode()) {
508         CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
509             CellularDataErrorCode::DATA_ERROR_CALL_AND_DATA_NOT_CONCURRENCY,
510             "CS call and data are not allowed concurrency");
511         TELEPHONY_LOGD("Slot%{public}d: AllowActiveData:%{public}d lastCallState_:%{public}d", slotId_,
512             isAllowActiveData, lastCallState_);
513         return false;
514     }
515     return true;
516 }
517 
CheckApnState(sptr<ApnHolder> & apnHolder)518 bool CellularDataHandler::CheckApnState(sptr<ApnHolder> &apnHolder)
519 {
520     if (apnManager_ == nullptr || apnHolder == nullptr) {
521         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or apnManager_ is null", slotId_);
522         return false;
523     }
524     if (apnHolder->GetApnState() == PROFILE_STATE_DISCONNECTING &&
525         !HasInnerEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION)) {
526         TELEPHONY_LOGI("Slot%{public}d: APN holder is disconnecting", slotId_);
527         int32_t id = apnManager_->FindApnIdByApnName(apnHolder->GetApnType());
528         SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, id, ESTABLISH_DATA_CONNECTION_DELAY);
529         return false;
530     }
531     if (apnHolder->GetApnState() == PROFILE_STATE_FAILED) {
532         apnHolder->SetApnState(PROFILE_STATE_IDLE);
533     }
534 
535     if (apnHolder->GetApnState() != PROFILE_STATE_IDLE) {
536         TELEPHONY_LOGI("Slot%{public}d: APN holder is not idle", slotId_);
537         return false;
538     }
539     std::vector<sptr<ApnItem>> matchedApns = apnManager_->FilterMatchedApns(apnHolder->GetApnType());
540     if (matchedApns.empty()) {
541         TELEPHONY_LOGE("Slot%{public}d: AttemptEstablishDataConnection:matchedApns is empty", slotId_);
542         return false;
543     }
544     apnHolder->SetAllMatchedApns(matchedApns);
545     return true;
546 }
547 
AttemptEstablishDataConnection(sptr<ApnHolder> & apnHolder)548 void CellularDataHandler::AttemptEstablishDataConnection(sptr<ApnHolder> &apnHolder)
549 {
550     if (!CheckCellularDataSlotId(apnHolder) || !CheckAttachAndSimState(apnHolder) || !CheckRoamingState(apnHolder)) {
551         return;
552     }
553     DelayedSingleton<CellularDataHiSysEvent>::GetInstance()->SetCellularDataActivateStartTime();
554     StartTrace(HITRACE_TAG_OHOS, "ActivateCellularData");
555     if (!CheckApnState(apnHolder)) {
556         FinishTrace(HITRACE_TAG_OHOS);
557         return;
558     }
559     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
560     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
561     coreInner.GetPsRadioTech(slotId_, radioTech);
562     if (!EstablishDataConnection(apnHolder, radioTech)) {
563         TELEPHONY_LOGE("Slot%{public}d: Establish data connection fail", slotId_);
564     }
565     FinishTrace(HITRACE_TAG_OHOS);
566     DelayedSingleton<CellularDataHiSysEvent>::GetInstance()->JudgingDataActivateTimeOut(slotId_, SWITCH_ON);
567 }
568 
FindIdleCellularDataConnection() const569 std::shared_ptr<CellularDataStateMachine> CellularDataHandler::FindIdleCellularDataConnection() const
570 {
571     if (connectionManager_ == nullptr) {
572         TELEPHONY_LOGE("Slot%{public}d: connectionManager_ is null", slotId_);
573         return nullptr;
574     }
575     std::vector<std::shared_ptr<CellularDataStateMachine>> allMachines = connectionManager_->GetAllConnectionMachine();
576     for (const std::shared_ptr<CellularDataStateMachine> &connect : allMachines) {
577         if (connect == nullptr || apnManager_ == nullptr) {
578             TELEPHONY_LOGE("Slot%{public}d: CellularDataHandler:stateMachine or apnManager_ is null", slotId_);
579             return nullptr;
580         }
581         if (connect->IsInactiveState() && apnManager_->IsDataConnectionNotUsed(connect)) {
582             return connect;
583         }
584     }
585     return nullptr;
586 }
587 
CreateCellularDataConnect()588 std::shared_ptr<CellularDataStateMachine> CellularDataHandler::CreateCellularDataConnect()
589 {
590     std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine = std::make_shared<CellularDataStateMachine>(
591         connectionManager_, std::static_pointer_cast<TelEventHandler>(shared_from_this()));
592     if (cellularDataStateMachine == nullptr) {
593         TELEPHONY_LOGE("Slot%{public}d: cellularDataStateMachine is null", slotId_);
594         return nullptr;
595     }
596     return cellularDataStateMachine;
597 }
598 
EstablishDataConnection(sptr<ApnHolder> & apnHolder,int32_t radioTech)599 bool CellularDataHandler::EstablishDataConnection(sptr<ApnHolder> &apnHolder, int32_t radioTech)
600 {
601     sptr<ApnItem> apnItem = apnHolder->GetNextRetryApn();
602     if (apnItem == nullptr) {
603         TELEPHONY_LOGE("Slot%{public}d: apnItem is null", slotId_);
604         return false;
605     }
606     if (IsSingleConnectionEnabled(radioTech)) {
607         if (HasAnyHigherPriorityConnection(apnHolder)) {
608             TELEPHONY_LOGE("Slot%{public}d: has higher priority connection", slotId_);
609             return false;
610         }
611         ApnProfileState apnState = apnManager_->GetOverallApnState();
612         if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
613             apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
614             ClearAllConnections(DisConnectionReason::REASON_CHANGE_CONNECTION);
615             return false;
616         }
617     }
618     std::shared_ptr<CellularDataStateMachine> cellularDataStateMachine = FindIdleCellularDataConnection();
619     if (cellularDataStateMachine == nullptr) {
620         cellularDataStateMachine = CreateCellularDataConnect();
621         if (cellularDataStateMachine == nullptr) {
622             TELEPHONY_LOGE("Slot%{public}d: cellularDataStateMachine is null", slotId_);
623             return false;
624         }
625         cellularDataStateMachine->Init();
626         if (connectionManager_ == nullptr) {
627             TELEPHONY_LOGE("Slot%{public}d: connectionManager_ is null", slotId_);
628             return false;
629         }
630         connectionManager_->AddConnectionStateMachine(cellularDataStateMachine);
631     }
632     cellularDataStateMachine->SetCapability(apnHolder->GetCapability());
633     apnHolder->SetCurrentApn(apnItem);
634     apnHolder->SetApnState(PROFILE_STATE_CONNECTING);
635     apnHolder->SetCellularDataStateMachine(cellularDataStateMachine);
636     bool roamingState = CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0;
637     bool userDataRoaming = dataSwitchSettings_->IsUserDataRoamingOn();
638     if (apnHolder->GetCapability() == NetCap::NET_CAPABILITY_INTERNET) {
639         StateNotification::GetInstance().UpdateCellularDataConnectState(slotId_, PROFILE_STATE_CONNECTING, radioTech);
640     }
641     std::unique_ptr<DataConnectionParams> object = std::make_unique<DataConnectionParams>(
642         apnHolder, apnItem->attr_.profileId_, radioTech, roamingState, userDataRoaming, true);
643     TELEPHONY_LOGI("Slot%{public}d: MSG_SM_CONNECT profileId:%{public}d type:%{public}s networkType:%{public}d",
644         slotId_, apnItem->attr_.profileId_, apnHolder->GetApnType().c_str(), radioTech);
645     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_SM_CONNECT, object);
646     cellularDataStateMachine->SendEvent(event);
647     return true;
648 }
649 
EstablishDataConnectionComplete(const InnerEvent::Pointer & event)650 void CellularDataHandler::EstablishDataConnectionComplete(const InnerEvent::Pointer &event)
651 {
652     if (event == nullptr) {
653         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
654         return;
655     }
656     std::shared_ptr<SetupDataCallResultInfo> resultInfo = event->GetSharedObject<SetupDataCallResultInfo>();
657     if ((resultInfo != nullptr) && (apnManager_ != nullptr)) {
658         sptr<ApnHolder> apnHolder = apnManager_->GetApnHolder(apnManager_->FindApnNameByApnId(resultInfo->flag));
659         if (apnHolder == nullptr) {
660             TELEPHONY_LOGE("Slot%{public}d: flag:%{public}d complete apnHolder is null", slotId_, resultInfo->flag);
661             return;
662         }
663         apnHolder->SetApnState(PROFILE_STATE_CONNECTED);
664         apnHolder->InitialApnRetryCount();
665         std::shared_ptr<CellularDataStateMachine> stateMachine = apnHolder->GetCellularDataStateMachine();
666         if (stateMachine != nullptr) {
667             std::string proxyIpAddress = "";
668             sptr<ApnItem> attachApn = apnManager_->GetRilAttachApn();
669             if (attachApn != nullptr) {
670                 proxyIpAddress = attachApn->attr_.proxyIpAddress_;
671             }
672             stateMachine->UpdateHttpProxy(proxyIpAddress);
673             stateMachine->UpdateNetworkInfo(*resultInfo);
674         } else {
675             TELEPHONY_LOGE(
676                 "Slot%{public}d:update network info stateMachine(%{public}d) is null", slotId_, resultInfo->flag);
677         }
678         if (connectionManager_ != nullptr) {
679             connectionManager_->StartStallDetectionTimer();
680             connectionManager_->BeginNetStatistics();
681         }
682         if (!physicalConnectionActiveState_) {
683             physicalConnectionActiveState_ = true;
684             CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
685         }
686         if (incallDataStateMachine_ != nullptr) {
687             InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_DATA_CONNECTED);
688             incallDataStateMachine_->SendEvent(incallEvent);
689         }
690         int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
691         CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
692         if (apnHolder->GetCapability() == NetCap::NET_CAPABILITY_INTERNET) {
693             StateNotification::GetInstance().UpdateCellularDataConnectState(
694                 slotId_, PROFILE_STATE_CONNECTED, networkType);
695         }
696     }
697 }
698 
GetSlotId() const699 int32_t CellularDataHandler::GetSlotId() const
700 {
701     return slotId_;
702 }
703 
DisconnectDataComplete(const InnerEvent::Pointer & event)704 void CellularDataHandler::DisconnectDataComplete(const InnerEvent::Pointer &event)
705 {
706     if (event == nullptr || apnManager_ == nullptr || connectionManager_ == nullptr) {
707         TELEPHONY_LOGE("Slot%{public}d: event or apnManager or connectionManager_ is null", slotId_);
708         return;
709     }
710     std::unique_ptr<DataDisconnectParams> object = event->GetUniqueObject<DataDisconnectParams>();
711     int32_t apnId = apnManager_->FindApnIdByApnName(object->GetApnType());
712     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(apnId);
713     if (apnHolder == nullptr) {
714         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null, apnId is %{public}d", slotId_, apnId);
715         return;
716     }
717     DisConnectionReason reason = object->GetReason();
718     apnHolder->SetApnState(PROFILE_STATE_IDLE);
719     int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
720     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
721     if (apnHolder->GetCapability() == NetCap::NET_CAPABILITY_INTERNET) {
722         StateNotification::GetInstance().UpdateCellularDataConnectState(slotId_, PROFILE_STATE_IDLE, networkType);
723     }
724     TELEPHONY_LOGI("Slot%{public}d: apn type: %{public}s call:%{public}d", slotId_, apnHolder->GetApnType().c_str(),
725         apnHolder->IsDataCallEnabled());
726     UpdatePhysicalConnectionState(connectionManager_->isNoActiveConnection());
727     if (apnHolder->IsDataCallEnabled()) {
728         if (apnHolder->GetApnState() == PROFILE_STATE_IDLE || apnHolder->GetApnState() == PROFILE_STATE_FAILED) {
729             apnHolder->SetCellularDataStateMachine(nullptr);
730         }
731         if (reason == DisConnectionReason::REASON_RETRY_CONNECTION) {
732             int64_t delayTime = apnHolder->GetRetryDelay();
733             TELEPHONY_LOGI("Slot%{public}d: Retry apn type: %{public}s", slotId_, apnHolder->GetApnType().c_str());
734             SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, apnId, delayTime);
735         }
736     }
737     if (!apnManager_->HasAnyConnectedState()) {
738         connectionManager_->StopStallDetectionTimer();
739         connectionManager_->EndNetStatistics();
740         if (incallDataStateMachine_ != nullptr) {
741             InnerEvent::Pointer incallEvent =
742                 InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_DATA_DISCONNECTED);
743             incallDataStateMachine_->SendEvent(incallEvent);
744         }
745     }
746     if (apnHolder->IsMmsType()) {
747         SetDataPermittedForMms(false);
748     }
749     if (reason == DisConnectionReason::REASON_CHANGE_CONNECTION) {
750         HandleSortConnection();
751     }
752 }
753 
UpdatePhysicalConnectionState(bool noActiveConnection)754 void CellularDataHandler::UpdatePhysicalConnectionState(bool noActiveConnection)
755 {
756     if (noActiveConnection && physicalConnectionActiveState_) {
757         physicalConnectionActiveState_ = false;
758         CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
759     } else if (!noActiveConnection && !physicalConnectionActiveState_) {
760         physicalConnectionActiveState_ = true;
761         CoreManagerInner::GetInstance().DcPhysicalLinkActiveUpdate(slotId_, physicalConnectionActiveState_);
762     }
763 }
764 
HandleSortConnection()765 void CellularDataHandler::HandleSortConnection()
766 {
767     ApnProfileState state = apnManager_->GetOverallApnState();
768     if (state == PROFILE_STATE_IDLE || state == PROFILE_STATE_FAILED) {
769         for (const sptr<ApnHolder> &sortApnHolder : apnManager_->GetSortApnHolder()) {
770             if (sortApnHolder->IsDataCallEnabled()) {
771                 int64_t delayTime = sortApnHolder->GetRetryDelay();
772                 int32_t apnId = apnManager_->FindApnIdByApnName(sortApnHolder->GetApnType());
773                 TELEPHONY_LOGI("Slot%{public}d: HandleSortConnection the apn type is %{public}s", slotId_,
774                     sortApnHolder->GetApnType().c_str());
775                 SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, apnId, delayTime);
776                 break;
777             }
778         }
779     }
780 }
781 
MsgEstablishDataConnection(const InnerEvent::Pointer & event)782 void CellularDataHandler::MsgEstablishDataConnection(const InnerEvent::Pointer &event)
783 {
784     if (apnManager_ == nullptr || event == nullptr) {
785         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or event is null", slotId_);
786         return;
787     }
788     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(event->GetParam());
789     if (apnHolder == nullptr) {
790         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
791         return;
792     }
793     TELEPHONY_LOGD("Slot%{public}d: APN holder type:%{public}s call:%{public}d", slotId_,
794         apnHolder->GetApnType().c_str(), apnHolder->IsDataCallEnabled());
795     if (apnHolder->IsDataCallEnabled()) {
796         AttemptEstablishDataConnection(apnHolder);
797     } else {
798         DisConnectionReason reason = DisConnectionReason::REASON_CHANGE_CONNECTION;
799         int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
800         CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, radioTech);
801         if (!IsSingleConnectionEnabled(radioTech)) {
802             reason = DisConnectionReason::REASON_CLEAR_CONNECTION;
803         }
804         ClearConnection(apnHolder, reason);
805     }
806 }
807 
MsgRequestNetwork(const InnerEvent::Pointer & event)808 void CellularDataHandler::MsgRequestNetwork(const InnerEvent::Pointer &event)
809 {
810     if (apnManager_ == nullptr || event == nullptr) {
811         TELEPHONY_LOGE("Slot%{public}d: apnManager_ or event is null", slotId_);
812         return;
813     }
814     std::unique_ptr<NetRequest> netRequest = event->GetUniqueObject<NetRequest>();
815     if (netRequest == nullptr) {
816         TELEPHONY_LOGE("Slot%{public}d: netRequest is null", slotId_);
817         return;
818     }
819     NetRequest request;
820     request.ident = netRequest->ident;
821     request.capability = netRequest->capability;
822     int32_t id = ApnManager::FindApnIdByCapability(request.capability);
823     sptr<ApnHolder> apnHolder = apnManager_->FindApnHolderById(id);
824     if (apnHolder == nullptr) {
825         TELEPHONY_LOGE("Slot%{public}d: apnHolder is null.", slotId_);
826         return;
827     }
828     if (event->GetParam() == TYPE_REQUEST_NET) {
829         apnHolder->RequestCellularData(request);
830     } else {
831         apnHolder->ReleaseCellularData(request);
832         if (apnHolder->IsDataCallEnabled()) {
833             return;
834         }
835     }
836     InnerEvent::Pointer innerEvent = InnerEvent::Get(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, id);
837     if (!SendEvent(innerEvent)) {
838         TELEPHONY_LOGE("Slot%{public}d: send data connection event failed", slotId_);
839     }
840 }
841 
ProcessEvent(const InnerEvent::Pointer & event)842 void CellularDataHandler::ProcessEvent(const InnerEvent::Pointer &event)
843 {
844     if (event == nullptr) {
845         TELEPHONY_LOGE("Slot%{public}d: event is null!", slotId_);
846         return;
847     }
848     uint32_t eventCode = event->GetInnerEventId();
849     std::map<uint32_t, Fun>::iterator it = eventIdMap_.find(eventCode);
850     if (it != eventIdMap_.end()) {
851         (this->*(it->second))(event);
852     }
853 }
854 
OnReceiveEvent(const EventFwk::CommonEventData & data)855 void CellularDataHandler::OnReceiveEvent(const EventFwk::CommonEventData &data)
856 {
857     const AAFwk::Want &want = data.GetWant();
858     std::string action = want.GetAction();
859     int32_t slotId = want.GetIntParam("slotId", 0);
860     TELEPHONY_LOGI("[slot%{public}d] action=%{public}s code=%{public}d", slotId_, action.c_str(), data.GetCode());
861     if (EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED == action) {
862         if (slotId_ != slotId) {
863             return;
864         }
865         int32_t state = want.GetIntParam("state", CALL_STATUS_UNKNOWN);
866         if (state == CALL_STATUS_UNKNOWN) {
867             TELEPHONY_LOGE("Slot%{public}d: unknown call state=%{public}d", slotId, state);
868             return;
869         }
870         HandleCallChanged(state);
871     } else if (action == CommonEventSupport::COMMON_EVENT_SIM_CARD_DEFAULT_DATA_SUBSCRIPTION_CHANGED) {
872         HandleDefaultDataSubscriptionChanged();
873     } else if (action == CommonEventSupport::COMMON_EVENT_OPERATOR_CONFIG_CHANGED) {
874         if (slotId_ != slotId) {
875             return;
876         }
877         GetConfigurationFor5G();
878     } else {
879         TELEPHONY_LOGI("Slot%{public}d: action=%{public}s code=%{public}d", slotId_, action.c_str(), data.GetCode());
880     }
881 }
882 
HandleSettingSwitchChanged(const InnerEvent::Pointer & event)883 void CellularDataHandler::HandleSettingSwitchChanged(const InnerEvent::Pointer &event)
884 {
885     if (event == nullptr) {
886         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
887         return;
888     }
889     bool setting_switch = event->GetParam();
890     TELEPHONY_LOGI("Slot%{public}d: setting switch = %{public}d", slotId_, setting_switch);
891 }
892 
HandleDBSettingIncallChanged(const AppExecFwk::InnerEvent::Pointer & event)893 void CellularDataHandler::HandleDBSettingIncallChanged(const AppExecFwk::InnerEvent::Pointer &event)
894 {
895     if (event == nullptr) {
896         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
897         return;
898     }
899     if (incallDataStateMachine_ == nullptr) {
900         TELEPHONY_LOGE("Slot%{public}d: incallDataStateMachine_ is null", slotId_);
901         return;
902     }
903     int64_t value = event->GetParam();
904     if (value == static_cast<int64_t>(DataSwitchCode::CELLULAR_DATA_ENABLED)) {
905         InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_SETTINGS_ON);
906         incallDataStateMachine_->SendEvent(incallEvent);
907     } else {
908         InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_SETTINGS_OFF);
909         incallDataStateMachine_->SendEvent(incallEvent);
910     }
911 }
912 
CreateIncallDataStateMachine(int32_t callState)913 std::shared_ptr<IncallDataStateMachine> CellularDataHandler::CreateIncallDataStateMachine(int32_t callState)
914 {
915     std::shared_ptr<IncallDataStateMachine> incallDataStateMachine = std::make_shared<IncallDataStateMachine>(slotId_,
916         std::weak_ptr<TelEventHandler>(std::static_pointer_cast<TelEventHandler>(shared_from_this())), apnManager_);
917     if (incallDataStateMachine == nullptr) {
918         TELEPHONY_LOGE("Slot%{public}d: incallDataStateMachine is null", slotId_);
919         return nullptr;
920     }
921     incallDataStateMachine->Init(callState);
922     return incallDataStateMachine;
923 }
924 
IncallDataComplete(const InnerEvent::Pointer & event)925 void CellularDataHandler::IncallDataComplete(const InnerEvent::Pointer &event)
926 {
927     TELEPHONY_LOGI("Slot%{public}d: MSG_INCALL_DATA_COMPLETE", slotId_);
928     if (incallDataStateMachine_ != nullptr) {
929         incallDataStateMachine_ = nullptr;
930     }
931 }
932 
HandleCallChanged(int32_t state)933 void CellularDataHandler::HandleCallChanged(int32_t state)
934 {
935     TELEPHONY_LOGI("Slot%{public}d: lastState:%{public}d, state:%{public}d", slotId_, lastCallState_, state);
936     if (lastCallState_ == state) {
937         TELEPHONY_LOGE("Slot%{public}d: call state=%{public}d, not changed", slotId_, state);
938         return;
939     }
940     if (connectionManager_ == nullptr) {
941         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null!", slotId_);
942         return;
943     }
944     lastCallState_ = state;
945     connectionManager_->UpdateCallState(state);
946     ImsRegInfo voiceInfo;
947     CoreManagerInner::GetInstance().GetImsRegStatus(slotId_, ImsServiceType::TYPE_VOICE, voiceInfo);
948     ImsRegInfo videoInfo;
949     CoreManagerInner::GetInstance().GetImsRegStatus(slotId_, ImsServiceType::TYPE_VIDEO, videoInfo);
950     if (voiceInfo.imsRegState == ImsRegState::IMS_REGISTERED || videoInfo.imsRegState == ImsRegState::IMS_REGISTERED) {
951         HandleImsCallChanged(state);
952     } else {
953         HandleVoiceCallChanged(state);
954     }
955 }
956 
HandleImsCallChanged(int32_t state)957 void CellularDataHandler::HandleImsCallChanged(int32_t state)
958 {
959     if (state == TelCallStatus::CALL_STATUS_DIALING || state == TelCallStatus::CALL_STATUS_INCOMING) {
960         if (incallDataStateMachine_ == nullptr) {
961             incallDataStateMachine_ = CreateIncallDataStateMachine(state);
962         }
963     }
964     if (incallDataStateMachine_ == nullptr) {
965         TELEPHONY_LOGE("Slot%{public}d: incallDataStateMachine_ is null!", slotId_);
966         return;
967     }
968     incallDataStateMachine_->UpdateCallState(state);
969     if (state == TelCallStatus::CALL_STATUS_DIALING || state == TelCallStatus::CALL_STATUS_INCOMING) {
970         InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_CALL_STARTED);
971         incallDataStateMachine_->SendEvent(incallEvent);
972     }
973     if (state == TelCallStatus::CALL_STATUS_DISCONNECTED || state == TelCallStatus::CALL_STATUS_IDLE) {
974         InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_CALL_ENDED);
975         incallDataStateMachine_->SendEvent(incallEvent);
976     }
977 }
978 
HandleVoiceCallChanged(int32_t state)979 void CellularDataHandler::HandleVoiceCallChanged(int32_t state)
980 {
981     if (apnManager_ == nullptr || connectionManager_ == nullptr) {
982         TELEPHONY_LOGE("Slot%{public}d: apnManager or connectionManager is null!", slotId_);
983         return;
984     }
985     // next to check if radio technology support voice and data at same time.
986     int32_t psRadioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
987     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, psRadioTech);
988     bool support = (psRadioTech == static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_GSM));
989     if (state != TelCallStatus::CALL_STATUS_IDLE && state != TelCallStatus::CALL_STATUS_DISCONNECTED) {
990         if (apnManager_->HasAnyConnectedState() && support) {
991             connectionManager_->EndNetStatistics();
992             connectionManager_->SetDataFlowType(CellDataFlowType::DATA_FLOW_TYPE_DORMANT);
993             connectionManager_->StopStallDetectionTimer();
994             disconnectionReason_ = DisConnectionReason::REASON_GSM_AND_CALLING_ONLY;
995         }
996     } else {
997         if (apnManager_->HasAnyConnectedState() && support) {
998             connectionManager_->StartStallDetectionTimer();
999             connectionManager_->BeginNetStatistics();
1000         }
1001         disconnectionReason_ = DisConnectionReason::REASON_NORMAL;
1002         TELEPHONY_LOGI("Slot%{public}d: HandleVoiceCallChanged EstablishAllApnsIfConnectable", slotId_);
1003         EstablishAllApnsIfConnectable();
1004     }
1005     TELEPHONY_LOGI("Slot%{public}d: disconnectionReason_=%{public}d", slotId_, disconnectionReason_);
1006 }
1007 
HandleDefaultDataSubscriptionChanged()1008 void CellularDataHandler::HandleDefaultDataSubscriptionChanged()
1009 {
1010     TELEPHONY_LOGI("Slot%{public}d", slotId_);
1011     if (CheckDataPermittedByDsds()) {
1012         SetDataPermitted(slotId_, true);
1013     } else {
1014         SetDataPermitted(slotId_, false);
1015     }
1016     if (dataSwitchSettings_ != nullptr) {
1017         dataSwitchSettings_->LoadSwitchValue();
1018     }
1019     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1020     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
1021     if (defSlotId == slotId_) {
1022         EstablishAllApnsIfConnectable();
1023     } else {
1024         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1025     }
1026 }
1027 
HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer & event)1028 void CellularDataHandler::HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer &event)
1029 {
1030     if (event == nullptr) {
1031         return;
1032     }
1033     if (dataSwitchSettings_ != nullptr) {
1034         dataSwitchSettings_->LoadSwitchValue();
1035     }
1036     std::u16string iccId;
1037     uint32_t eventId = event->GetInnerEventId();
1038     switch (eventId) {
1039         case RadioEvent::RADIO_SIM_STATE_CHANGE: {
1040             SimState simState = SimState::SIM_STATE_UNKNOWN;
1041             CoreManagerInner::GetInstance().GetSimState(slotId_, simState);
1042             TELEPHONY_LOGI("Slot%{public}d: sim state is :%{public}d", slotId_, simState);
1043             if (simState != SimState::SIM_STATE_READY) {
1044                 ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1045                 if (simState == SimState::SIM_STATE_NOT_PRESENT) {
1046                     UnRegisterDataSettingObserver();
1047                 }
1048             } else {
1049                 CoreManagerInner::GetInstance().GetSimIccId(slotId_, iccId);
1050                 if (lastIccId_ != u"" && lastIccId_ == iccId) {
1051                     EstablishAllApnsIfConnectable();
1052                 }
1053             }
1054             break;
1055         }
1056         case RadioEvent::RADIO_SIM_RECORDS_LOADED: {
1057             CoreManagerInner::GetInstance().GetSimIccId(slotId_, iccId);
1058             SimState simState = SimState::SIM_STATE_UNKNOWN;
1059             CoreManagerInner::GetInstance().GetSimState(slotId_, simState);
1060             TELEPHONY_LOGI("Slot%{public}d: sim records loaded state is :%{public}d", slotId_, simState);
1061             if (simState != SimState::SIM_STATE_READY || iccId == u"") {
1062                 TELEPHONY_LOGI("sim state error or iccId nullptr");
1063                 break;
1064             }
1065             if (iccId != lastIccId_) {
1066                 if (dataSwitchSettings_ != nullptr) {
1067                     dataSwitchSettings_->SetPolicyDataOn(true);
1068                 }
1069                 lastIccId_ = iccId;
1070             } else if (lastIccId_ == iccId) {
1071                 TELEPHONY_LOGI("Slot%{public}d: sim state changed, but iccId not changed.", slotId_);
1072                 // the sim card status has changed to ready, so try to connect
1073                 EstablishAllApnsIfConnectable();
1074             }
1075             break;
1076         }
1077         default:
1078             break;
1079     }
1080 }
1081 
HandleSimAccountLoaded(const InnerEvent::Pointer & event)1082 void CellularDataHandler::HandleSimAccountLoaded(const InnerEvent::Pointer &event)
1083 {
1084     if (event == nullptr) {
1085         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
1086         return;
1087     }
1088     TELEPHONY_LOGI("Slot%{public}d", slotId_);
1089     auto slotId = event->GetParam();
1090     if (slotId == slotId_) {
1091         ClearAllConnections(DisConnectionReason::REASON_CHANGE_CONNECTION);
1092         CellularDataNetAgent::GetInstance().UnregisterNetSupplier(slotId_);
1093         CellularDataNetAgent::GetInstance().RegisterNetSupplier(slotId_);
1094         if (slotId_ == 0) {
1095             CellularDataNetAgent::GetInstance().UnregisterPolicyCallback();
1096             CellularDataNetAgent::GetInstance().RegisterPolicyCallback();
1097         }
1098         RegisterDataSettingObserver();
1099         if (dataSwitchSettings_ != nullptr) {
1100             dataSwitchSettings_->LoadSwitchValue();
1101         }
1102         GetConfigurationFor5G();
1103         CreateApnItem();
1104     }
1105     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1106     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
1107     if (defSlotId == slotId_) {
1108         EstablishAllApnsIfConnectable();
1109     } else {
1110         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1111     }
1112 }
1113 
CreateApnItem()1114 void CellularDataHandler::CreateApnItem()
1115 {
1116     if (apnManager_ == nullptr) {
1117         TELEPHONY_LOGE("Slot%{public}d: apnManager_ is null", slotId_);
1118         return;
1119     }
1120     int32_t result = 0;
1121     for (int32_t i = 0; i < DEFAULT_READ_APN_TIME; ++i) {
1122         result = apnManager_->CreateAllApnItemByDatabase(slotId_);
1123         if (result != 0) {
1124             break;
1125         }
1126     }
1127     if (result == 0) {
1128         apnManager_->CreateAllApnItem();
1129     }
1130     SetRilAttachApn();
1131 }
1132 
HandleApnChanged()1133 bool CellularDataHandler::HandleApnChanged()
1134 {
1135     if (apnManager_ == nullptr) {
1136         TELEPHONY_LOGE("apnManager is null");
1137         return false;
1138     }
1139     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
1140         TELEPHONY_LOGI("Slot%{public}d: apn type:%{public}s state:%{public}d", slotId_, apnHolder->GetApnType().c_str(),
1141             apnHolder->GetApnState());
1142     }
1143     InnerEvent::Pointer event = InnerEvent::Get(CellularDataEventCode::MSG_APN_CHANGED);
1144     if (event == nullptr) {
1145         TELEPHONY_LOGE("Slot%{public}d: get apn changed event is null", slotId_);
1146         return false;
1147     }
1148     return SendEvent(event);
1149 }
1150 
HandleApnChanged(const InnerEvent::Pointer & event)1151 void CellularDataHandler::HandleApnChanged(const InnerEvent::Pointer &event)
1152 {
1153     if (apnManager_ == nullptr) {
1154         TELEPHONY_LOGE("Slot%{public}d: apnManager_ is null", slotId_);
1155         return;
1156     }
1157     CreateApnItem();
1158     ApnProfileState apnState = apnManager_->GetOverallApnState();
1159     if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING || apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
1160         ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
1161     }
1162     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
1163         if (apnHolder == nullptr) {
1164             continue;
1165         }
1166         int32_t id = apnManager_->FindApnIdByApnName(apnHolder->GetApnType());
1167         SendEvent(CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, id, ESTABLISH_DATA_CONNECTION_DELAY);
1168     }
1169 }
1170 
GetCellularDataFlowType()1171 int32_t CellularDataHandler::GetCellularDataFlowType()
1172 {
1173     if (connectionManager_ == nullptr) {
1174         TELEPHONY_LOGE("Slot%{public}d: connection manager is null", slotId_);
1175         return 0;
1176     }
1177     return connectionManager_->GetDataFlowType();
1178 }
1179 
HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer & event)1180 void CellularDataHandler::HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
1181 {
1182     if (apnManager_ == nullptr || event == nullptr) {
1183         TELEPHONY_LOGE("Slot%{public}d: radio off or not available apnManager or event is null!", slotId_);
1184         return;
1185     }
1186     std::shared_ptr<HRilInt32Parcel> object = event->GetSharedObject<HRilInt32Parcel>();
1187     if (object == nullptr) {
1188         TELEPHONY_LOGE("Slot%{public}d: object is nullptr!", slotId_);
1189         return;
1190     }
1191     TELEPHONY_LOGI("Slot%{public}d: Radio changed with state: %{public}d", slotId_, object->data);
1192     switch (object->data) {
1193         case CORE_SERVICE_POWER_OFF:
1194         case CORE_SERVICE_POWER_NOT_AVAILABLE: {
1195             ApnProfileState apnState = apnManager_->GetOverallApnState();
1196             TELEPHONY_LOGI("Slot%{public}d: apn state is %{public}d", slotId_, apnState);
1197             if (apnState != ApnProfileState::PROFILE_STATE_IDLE) {
1198                 ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1199             }
1200             break;
1201         }
1202         case CORE_SERVICE_POWER_ON:
1203             SetRilLinkBandwidths();
1204             EstablishAllApnsIfConnectable();
1205             break;
1206         default:
1207             TELEPHONY_LOGI("Slot%{public}d: un-handle state:%{public}d", slotId_, object->data);
1208             break;
1209     }
1210 }
1211 
HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer & event)1212 void CellularDataHandler::HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer &event)
1213 {
1214     if (event == nullptr) {
1215         TELEPHONY_LOGE("Slot%{public}d: event is null!", slotId_);
1216         return;
1217     }
1218     std::shared_ptr<HRilInt32Parcel> object = event->GetSharedObject<HRilInt32Parcel>();
1219     if (object == nullptr) {
1220         TELEPHONY_LOGE("Slot%{public}d: object is null!", slotId_);
1221         return;
1222     }
1223     TELEPHONY_LOGI("Slot%{public}d: DSDS changed with mode: %{public}d", slotId_, object->data);
1224     int32_t dsdsMode = DSDS_MODE_V2;
1225     CoreManagerInner::GetInstance().GetDsdsMode(dsdsMode);
1226     if (object->data == dsdsMode) {
1227         TELEPHONY_LOGE("Slot%{public}d: DSDS mode is the same!", slotId_);
1228         return;
1229     }
1230     if (object->data < DSDS_MODE_V2) {
1231         TELEPHONY_LOGE("Slot%{public}d: DSDS mode is illegal!", slotId_);
1232         return;
1233     }
1234     CoreManagerInner::GetInstance().SetDsdsMode(object->data);
1235     int32_t defaultSlotId = CoreManagerInner::GetInstance().GetDefaultCellularDataSlotId();
1236     int32_t simNum = CoreManagerInner::GetInstance().GetMaxSimCount();
1237     for (int32_t i = 0; i < simNum; ++i) {
1238         if (defaultSlotId != i && object->data == DSDS_MODE_V2) {
1239             SetDataPermitted(i, false);
1240         } else {
1241             SetDataPermitted(i, true);
1242             DelayedRefSingleton<CellularDataService>::GetInstance().ChangeConnectionForDsds(i, true);
1243         }
1244     }
1245     if (incallDataStateMachine_ != nullptr) {
1246         InnerEvent::Pointer incallEvent = InnerEvent::Get(CellularDataEventCode::MSG_SM_INCALL_DATA_DSDS_CHANGED);
1247         incallDataStateMachine_->SendEvent(incallEvent);
1248     }
1249 }
1250 
ClearConnectionIfRequired()1251 void CellularDataHandler::ClearConnectionIfRequired()
1252 {
1253     if (apnManager_ == nullptr) {
1254         TELEPHONY_LOGE("Slot%{public}d: apnManager_ is null", slotId_);
1255         return;
1256     }
1257     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
1258         if (apnHolder == nullptr) {
1259             TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
1260             continue;
1261         }
1262         ApnProfileState apnState = apnHolder->GetApnState();
1263         std::string apnType = apnHolder->GetApnType();
1264         std::vector<sptr<ApnItem>> matchedApns = apnManager_->FilterMatchedApns(apnType);
1265         if (matchedApns.empty()) {
1266             TELEPHONY_LOGE("Slot%{public}d: matchedApns is empty", slotId_);
1267             continue;
1268         }
1269         bool roamingState = CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0;
1270         if (!apnHolder->IsSameMatchedApns(matchedApns, roamingState)) {
1271             apnHolder->SetAllMatchedApns(matchedApns);
1272             if (apnState != ApnProfileState::PROFILE_STATE_IDLE &&
1273                 apnState != ApnProfileState::PROFILE_STATE_FAILED) {
1274                 TELEPHONY_LOGI("Slot%{public}d: the connection of APN type:%{public}s will be cleared.",
1275                     slotId_, apnType.c_str());
1276                 ClearConnection(apnHolder, DisConnectionReason::REASON_RETRY_CONNECTION);
1277             }
1278         }
1279     }
1280     if (connectionManager_ == nullptr) {
1281         TELEPHONY_LOGE("Slot%{public}d: connectionManager_ is null", slotId_);
1282         return;
1283     }
1284     if (!apnManager_->HasAnyConnectedState()) {
1285         connectionManager_->StopStallDetectionTimer();
1286         connectionManager_->EndNetStatistics();
1287         ResetDataFlowType();
1288     }
1289 }
1290 
PsDataRatChanged(const InnerEvent::Pointer & event)1291 void CellularDataHandler::PsDataRatChanged(const InnerEvent::Pointer &event)
1292 {
1293     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1294     int32_t radioTech = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
1295     coreInner.GetPsRadioTech(slotId_, radioTech);
1296     TELEPHONY_LOGI("Slot%{public}d: radioTech is %{public}d", slotId_, radioTech);
1297     if (event == nullptr) {
1298         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
1299         return;
1300     }
1301     bool dataEnabled = dataSwitchSettings_->IsUserDataOn();
1302     if (!dataEnabled) {
1303         TELEPHONY_LOGE("Slot%{public}d: data enable is close", slotId_);
1304         return;
1305     }
1306     bool attached = coreInner.GetPsRegState(slotId_) == (int32_t)RegServiceState::REG_STATE_IN_SERVICE;
1307     if (!attached) {
1308         TELEPHONY_LOGE("Slot%{public}d: attached is false", slotId_);
1309         return;
1310     }
1311     ClearConnectionIfRequired();
1312     EstablishAllApnsIfConnectable();
1313 }
1314 
SetPolicyDataOn(bool enable)1315 void CellularDataHandler::SetPolicyDataOn(bool enable)
1316 {
1317     if (dataSwitchSettings_ == nullptr) {
1318         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings is null", slotId_);
1319         return;
1320     }
1321     bool policyDataOn = dataSwitchSettings_->IsPolicyDataOn();
1322     if (policyDataOn != enable) {
1323         dataSwitchSettings_->SetPolicyDataOn(enable);
1324         if (enable) {
1325             EstablishAllApnsIfConnectable();
1326         } else {
1327             ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1328         }
1329     }
1330 }
1331 
IsRestrictedMode() const1332 bool CellularDataHandler::IsRestrictedMode() const
1333 {
1334     int32_t networkType = static_cast<int32_t>(RadioTech::RADIO_TECHNOLOGY_INVALID);
1335     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, networkType);
1336     bool support = (networkType == (int32_t)RadioTech::RADIO_TECHNOLOGY_GSM);
1337     bool inCall = (lastCallState_ != TelCallStatus::CALL_STATUS_IDLE &&
1338                    lastCallState_ != TelCallStatus::CALL_STATUS_DISCONNECTED);
1339     TELEPHONY_LOGI("Slot%{public}d: radio technology is gsm only:%{public}d and call is busy:%{public}d", slotId_,
1340         support, inCall);
1341     return inCall && support;
1342 }
1343 
GetDisConnectionReason()1344 DisConnectionReason CellularDataHandler::GetDisConnectionReason()
1345 {
1346     return disconnectionReason_;
1347 }
1348 
SetDataPermitted(int32_t slotId,bool dataPermitted)1349 void CellularDataHandler::SetDataPermitted(int32_t slotId, bool dataPermitted)
1350 {
1351     TELEPHONY_LOGI("Slot%{public}d: dataPermitted is %{public}d.", slotId, dataPermitted);
1352     int32_t maxSimCount = CoreManagerInner::GetInstance().GetMaxSimCount();
1353     if (maxSimCount <= 1) {
1354         TELEPHONY_LOGE("Slot%{public}d: maxSimCount is: %{public}d", slotId_, maxSimCount);
1355         return;
1356     }
1357     bool hasSimCard = false;
1358     CoreManagerInner::GetInstance().HasSimCard(slotId, hasSimCard);
1359     if (!hasSimCard) {
1360         TELEPHONY_LOGE("Slot%{public}d: no sim :%{public}d", slotId_, slotId);
1361         return;
1362     }
1363     CoreManagerInner::GetInstance().SetDataPermitted(
1364         slotId, CellularDataEventCode::MSG_SET_DATA_PERMITTED, dataPermitted, shared_from_this());
1365 }
1366 
SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer & event)1367 void CellularDataHandler::SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer &event)
1368 {
1369     if (event == nullptr) {
1370         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
1371         return;
1372     }
1373     std::shared_ptr<TelRilResponseInfo<int32_t>> rilInfo = event->GetSharedObject<TelRilResponseInfo<int32_t>>();
1374     if (rilInfo == nullptr) {
1375         TELEPHONY_LOGE("Slot%{public}d: HRilRadioResponseInfo is null", slotId_);
1376         return;
1377     }
1378     if (rilInfo->errorNo != 0) {
1379         TELEPHONY_LOGE("Slot%{public}d: SetDataPermitted error", slotId_);
1380     }
1381 }
1382 
GetEsmFlagFromOpCfg()1383 bool CellularDataHandler::GetEsmFlagFromOpCfg()
1384 {
1385     int32_t esmFlagFromOpCfg = ESM_FLAG_INVALID;
1386     OperatorConfig configsForEsmFlag;
1387     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, configsForEsmFlag);
1388     if (configsForEsmFlag.intValue.find(KEY_PLMN_ESM_FLAG_INT) != configsForEsmFlag.intValue.end()) {
1389         esmFlagFromOpCfg = configsForEsmFlag.intValue[KEY_PLMN_ESM_FLAG_INT];
1390     }
1391     if (esmFlagFromOpCfg < 0 || esmFlagFromOpCfg > 1) {
1392         TELEPHONY_LOGE("esmFlag value is invalid");
1393     }
1394     return (esmFlagFromOpCfg != 0);
1395 }
1396 
SetInitApnWithNullDp()1397 void CellularDataHandler::SetInitApnWithNullDp()
1398 {
1399     DataProfile dataProfile;
1400     dataProfile.profileId = 0;
1401     dataProfile.apn = "";
1402     dataProfile.protocol = "";
1403     dataProfile.verType = 0;
1404     dataProfile.userName = "";
1405     dataProfile.password = "";
1406     dataProfile.roamingProtocol = "";
1407     CoreManagerInner::GetInstance().SetInitApnInfo(
1408         slotId_, CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, dataProfile, shared_from_this());
1409     return;
1410 }
1411 
SetRilAttachApn()1412 void CellularDataHandler::SetRilAttachApn()
1413 {
1414     sptr<ApnItem> attachApn = apnManager_->GetRilAttachApn();
1415     if (attachApn == nullptr) {
1416         TELEPHONY_LOGE("Slot%{public}d: attachApn is null", slotId_);
1417         return;
1418     }
1419     if (!GetEsmFlagFromOpCfg()) {
1420         SetInitApnWithNullDp();
1421         return;
1422     }
1423     DataProfile dataProfile;
1424     dataProfile.profileId = attachApn->attr_.profileId_;
1425     dataProfile.apn = attachApn->attr_.apn_;
1426     dataProfile.protocol = attachApn->attr_.protocol_;
1427     dataProfile.verType = attachApn->attr_.authType_;
1428     dataProfile.userName = attachApn->attr_.user_;
1429     dataProfile.password = attachApn->attr_.password_;
1430     dataProfile.roamingProtocol = attachApn->attr_.roamingProtocol_;
1431     TELEPHONY_LOGI("DataProfile profileId = %{public}d", dataProfile.profileId);
1432     CoreManagerInner::GetInstance().SetInitApnInfo(
1433         slotId_, CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, dataProfile, shared_from_this());
1434 }
1435 
SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer & event)1436 void CellularDataHandler::SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer &event)
1437 {
1438     if (event == nullptr) {
1439         TELEPHONY_LOGE("Slot%{public}d: event is null", slotId_);
1440         return;
1441     }
1442     std::shared_ptr<TelRilResponseInfo<int32_t>> rilInfo = event->GetSharedObject<TelRilResponseInfo<int32_t>>();
1443     if (rilInfo == nullptr) {
1444         TELEPHONY_LOGE("Slot%{public}d: HRilRadioResponseInfo is null", slotId_);
1445         return;
1446     }
1447     if (rilInfo->errorNo != 0) {
1448         TELEPHONY_LOGE("Slot%{public}d: SetRilAttachApn error", slotId_);
1449     }
1450 }
1451 
HasAnyHigherPriorityConnection(const sptr<ApnHolder> & apnHolder)1452 bool CellularDataHandler::HasAnyHigherPriorityConnection(const sptr<ApnHolder> &apnHolder)
1453 {
1454     if (apnManager_ == nullptr) {
1455         TELEPHONY_LOGE("Slot%{public}d: apnManager is null", slotId_);
1456         return false;
1457     }
1458     std::vector<sptr<ApnHolder>> sortApnHolders = apnManager_->GetSortApnHolder();
1459     if (sortApnHolders.empty()) {
1460         TELEPHONY_LOGE("Slot%{public}d: SortApnHolder is null", slotId_);
1461         return false;
1462     }
1463     for (const sptr<ApnHolder> &sortApnHolder : sortApnHolders) {
1464         if (sortApnHolder->GetPriority() > apnHolder->GetPriority()) {
1465             if (sortApnHolder->IsDataCallEnabled() &&
1466                 (sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTED ||
1467                     sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_CONNECTING ||
1468                     sortApnHolder->GetApnState() == ApnProfileState::PROFILE_STATE_DISCONNECTING)) {
1469                 CellularDataHiSysEvent::WriteDataActivateFaultEvent(slotId_, SWITCH_ON,
1470                     CellularDataErrorCode::DATA_ERROR_HAS_HIGHER_PRIORITY_CONNECTION,
1471                     "There is higher priority connection");
1472                 return true;
1473             }
1474         }
1475     }
1476     return false;
1477 }
1478 
HasInternetCapability(const int32_t cid) const1479 bool CellularDataHandler::HasInternetCapability(const int32_t cid) const
1480 {
1481     if (connectionManager_ == nullptr) {
1482         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1483         return false;
1484     }
1485     std::shared_ptr<CellularDataStateMachine> activeStateMachine = connectionManager_->GetActiveConnectionByCid(cid);
1486     if (activeStateMachine == nullptr) {
1487         TELEPHONY_LOGE("Slot%{public}d: get activeStateMachine by cid fail", slotId_);
1488         return false;
1489     }
1490     uint64_t netCapability = activeStateMachine->GetCapability();
1491     if (netCapability == NetCap::NET_CAPABILITY_INTERNET) {
1492         return true;
1493     }
1494     return false;
1495 }
1496 
ChangeConnectionForDsds(bool enable)1497 bool CellularDataHandler::ChangeConnectionForDsds(bool enable)
1498 {
1499     if (dataSwitchSettings_ == nullptr) {
1500         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null.", slotId_);
1501         return false;
1502     }
1503     if (enable) {
1504         dataSwitchSettings_->SetInternalDataOn(true);
1505         EstablishAllApnsIfConnectable();
1506     } else {
1507         dataSwitchSettings_->SetInternalDataOn(false);
1508         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1509     }
1510     return true;
1511 }
1512 
GetConfigurationFor5G()1513 void CellularDataHandler::GetConfigurationFor5G()
1514 {
1515     // get 5G configurations
1516     unMeteredAllNsaConfig_ = ParseOperatorConfig(u"allmeterednas");
1517     unMeteredNrNsaMmwaveConfig_ = ParseOperatorConfig(u"meterednrnsammware");
1518     unMeteredNrNsaSub6Config_ = ParseOperatorConfig(u"meteredNrnsasub6");
1519     unMeteredAllNrsaConfig_ = ParseOperatorConfig(u"meteredallnrsa");
1520     unMeteredNrsaMmwaveConfig_ = ParseOperatorConfig(u"meterednrsammware");
1521     unMeteredNrsaSub6Config_ = ParseOperatorConfig(u"meterednrsasub6");
1522     unMeteredRoamingConfig_ = ParseOperatorConfig(u"meteredroaming");
1523     GetDefaultConfiguration();
1524 }
1525 
ParseOperatorConfig(const std::u16string & configName)1526 bool CellularDataHandler::ParseOperatorConfig(const std::u16string &configName)
1527 {
1528     OperatorConfig configsFor5G;
1529     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, configsFor5G);
1530     if (configsFor5G.configValue.count(configName) > 0) {
1531         std::string flag = Str16ToStr8(configsFor5G.configValue[configName]);
1532         TELEPHONY_LOGI("Slot%{public}d: parse operator 5G config: %{public}s", slotId_, flag.c_str());
1533         if (flag == "true") {
1534             return true;
1535         }
1536     }
1537     return false;
1538 }
1539 
GetSinglePdpEnabledFromOpCfg()1540 void CellularDataHandler::GetSinglePdpEnabledFromOpCfg()
1541 {
1542     OperatorConfig configsForSinglePdp;
1543     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, configsForSinglePdp);
1544     if (configsForSinglePdp.boolValue.find(KEY_SINGLE_PDP_ENABLED_BOOL) != configsForSinglePdp.boolValue.end()) {
1545         multipleConnectionsEnabled_ = !configsForSinglePdp.boolValue[KEY_SINGLE_PDP_ENABLED_BOOL];
1546     }
1547     return;
1548 }
1549 
IsSingleConnectionEnabled(int32_t radioTech)1550 bool CellularDataHandler::IsSingleConnectionEnabled(int32_t radioTech)
1551 {
1552     std::vector<int32_t> singlePdpRadio;
1553     OperatorConfig configsForSinglePdpRadioType;
1554     CoreManagerInner::GetInstance().GetOperatorConfigs(slotId_, configsForSinglePdpRadioType);
1555     if (configsForSinglePdpRadioType.intArrayValue.count(KEY_SINGLE_PDP_RADIO_TYPE_INT_ARRAY) >0) {
1556         singlePdpRadio = configsForSinglePdpRadioType.intArrayValue[KEY_SINGLE_PDP_RADIO_TYPE_INT_ARRAY];
1557     }
1558     if (singlePdpRadio.empty()) {
1559         TELEPHONY_LOGI("single pdp radio type array is empty");
1560     }
1561     if (std::find(singlePdpRadio.begin(), singlePdpRadio.end(), radioTech) != singlePdpRadio.end()) {
1562         TELEPHONY_LOGI("radio type array is matched single pdp type");
1563         multipleConnectionsEnabled_ = false;
1564         return !multipleConnectionsEnabled_;
1565     }
1566     GetSinglePdpEnabledFromOpCfg();
1567     return !multipleConnectionsEnabled_;
1568 }
1569 
GetDefaultConfiguration()1570 void CellularDataHandler::GetDefaultConfiguration()
1571 {
1572     if (connectionManager_ == nullptr) {
1573         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1574         return;
1575     }
1576     connectionManager_->GetDefaultBandWidthsConfig();
1577     connectionManager_->GetDefaultTcpBufferConfig();
1578     GetDefaultUpLinkThresholdsConfig();
1579     GetDefaultDownLinkThresholdsConfig();
1580     defaultMobileMtuConfig_ = CellularDataUtils::GetDefaultMobileMtuConfig();
1581     TELEPHONY_LOGI("Slot%{public}d: defaultMobileMtuConfig_ = %{public}d", slotId_, defaultMobileMtuConfig_);
1582     defaultPreferApn_ = CellularDataUtils::GetDefaultPreferApnConfig();
1583     TELEPHONY_LOGI("Slot%{public}d: defaultPreferApn_ is %{public}d", slotId_, defaultPreferApn_);
1584     multipleConnectionsEnabled_ = CellularDataUtils::GetDefaultMultipleConnectionsConfig();
1585     GetSinglePdpEnabledFromOpCfg();
1586     TELEPHONY_LOGI("Slot%{public}d: multipleConnectionsEnabled_ = %{public}d", slotId_, multipleConnectionsEnabled_);
1587 }
1588 
HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer & event)1589 void CellularDataHandler::HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event)
1590 {
1591     if (connectionManager_ == nullptr) {
1592         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1593         return;
1594     }
1595     TELEPHONY_LOGI("Slot%{public}d: receive event", slotId_);
1596     std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines =
1597         connectionManager_->GetAllConnectionMachine();
1598     for (const std::shared_ptr<CellularDataStateMachine> &cellularDataStateMachine : stateMachines) {
1599         InnerEvent::Pointer eventCode = InnerEvent::Get(RadioEvent::RADIO_NR_STATE_CHANGED);
1600         cellularDataStateMachine->SendEvent(eventCode);
1601     }
1602 }
1603 
HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer & event)1604 void CellularDataHandler::HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event)
1605 {
1606     if (connectionManager_ == nullptr) {
1607         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1608         return;
1609     }
1610     TELEPHONY_LOGI("Slot%{public}d: receive event", slotId_);
1611     std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines =
1612         connectionManager_->GetAllConnectionMachine();
1613     for (const std::shared_ptr<CellularDataStateMachine> &cellularDataStateMachine : stateMachines) {
1614         InnerEvent::Pointer eventCode = InnerEvent::Get(RadioEvent::RADIO_NR_FREQUENCY_CHANGED);
1615         cellularDataStateMachine->SendEvent(eventCode);
1616     }
1617 }
1618 
GetDefaultUpLinkThresholdsConfig()1619 void CellularDataHandler::GetDefaultUpLinkThresholdsConfig()
1620 {
1621     upLinkThresholds_.clear();
1622     char upLinkConfig[UP_DOWN_LINK_SIZE] = { 0 };
1623     GetParameter(CONFIG_UPLINK_THRESHOLDS, CAPACITY_THRESHOLDS_FOR_UPLINK, upLinkConfig, UP_DOWN_LINK_SIZE);
1624     TELEPHONY_LOGI("Slot%{public}d: upLinkThresholds = %{public}s", slotId_, upLinkConfig);
1625     upLinkThresholds_ = CellularDataUtils::Split(upLinkConfig, ",");
1626 }
1627 
GetDefaultDownLinkThresholdsConfig()1628 void CellularDataHandler::GetDefaultDownLinkThresholdsConfig()
1629 {
1630     downLinkThresholds_.clear();
1631     char downLinkConfig[UP_DOWN_LINK_SIZE] = { 0 };
1632     GetParameter(CONFIG_DOWNLINK_THRESHOLDS, CAPACITY_THRESHOLDS_FOR_DOWNLINK, downLinkConfig, UP_DOWN_LINK_SIZE);
1633     TELEPHONY_LOGI("Slot%{public}d: downLinkThresholds_ = %{public}s", slotId_, downLinkConfig);
1634     downLinkThresholds_ = CellularDataUtils::Split(downLinkConfig, ",");
1635 }
1636 
SetRilLinkBandwidths()1637 void CellularDataHandler::SetRilLinkBandwidths()
1638 {
1639     LinkBandwidthRule linkBandwidth;
1640     CoreManagerInner::GetInstance().GetPsRadioTech(slotId_, linkBandwidth.rat);
1641     linkBandwidth.delayMs = DELAY_SET_RIL_BANDWIDTH_MS;
1642     linkBandwidth.delayUplinkKbps = DELAY_SET_RIL_UP_DOWN_BANDWIDTH_MS;
1643     linkBandwidth.delayDownlinkKbps = DELAY_SET_RIL_UP_DOWN_BANDWIDTH_MS;
1644     for (std::string upLinkThreshold : upLinkThresholds_) {
1645         linkBandwidth.maximumUplinkKbps.push_back(atoi(upLinkThreshold.c_str()));
1646     }
1647     for (std::string downLinkThreshold : downLinkThresholds_) {
1648         linkBandwidth.maximumDownlinkKbps.push_back(atoi(downLinkThreshold.c_str()));
1649     }
1650     CoreManagerInner::GetInstance().SetLinkBandwidthReportingRule(
1651         slotId_, CellularDataEventCode::MSG_SET_RIL_BANDWIDTH, linkBandwidth, shared_from_this());
1652 }
1653 
HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer & event)1654 void CellularDataHandler::HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer &event)
1655 {
1656     if (dataSwitchSettings_ == nullptr) {
1657         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ is null.", slotId_);
1658         return;
1659     }
1660     bool dataEnabled = true;
1661     dataSwitchSettings_->QueryUserDataStatus(dataEnabled);
1662     CoreManagerInner &coreInner = CoreManagerInner::GetInstance();
1663     const int32_t defSlotId = coreInner.GetDefaultCellularDataSlotId();
1664     if (dataEnabled && defSlotId == slotId_) {
1665         EstablishAllApnsIfConnectable();
1666     } else {
1667         ClearAllConnections(DisConnectionReason::REASON_CLEAR_CONNECTION);
1668     }
1669 }
1670 
HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer & event)1671 void CellularDataHandler::HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer &event)
1672 {
1673     if (event == nullptr) {
1674         return;
1675     }
1676     if (dataSwitchSettings_ == nullptr || apnManager_ == nullptr) {
1677         TELEPHONY_LOGE("Slot%{public}d: dataSwitchSettings_ or apnManager_ is null", slotId_);
1678         return;
1679     }
1680     int64_t value = event->GetParam();
1681     bool dataRoamingEnabled = false;
1682     dataSwitchSettings_->QueryUserDataRoamingStatus(dataRoamingEnabled);
1683     if (dataRoamingEnabled != value) {
1684         dataSwitchSettings_->SetUserDataRoamingOn(value);
1685         bool roamingState = false;
1686         if (CoreManagerInner::GetInstance().GetPsRoamingState(slotId_) > 0) {
1687             roamingState = true;
1688         }
1689         if (roamingState) {
1690             ApnProfileState apnState = apnManager_->GetOverallApnState();
1691             if (apnState == ApnProfileState::PROFILE_STATE_CONNECTING ||
1692                 apnState == ApnProfileState::PROFILE_STATE_CONNECTED) {
1693                 ClearAllConnections(DisConnectionReason::REASON_RETRY_CONNECTION);
1694             }
1695             EstablishAllApnsIfConnectable();
1696         } else {
1697             TELEPHONY_LOGI("Slot%{public}d: Not roaming(%{public}d), not doing anything", slotId_, roamingState);
1698         }
1699     } else {
1700         TELEPHONY_LOGI("Slot%{public}d: The roaming switch status has not changed", slotId_);
1701     }
1702 }
1703 
UnRegisterDataSettingObserver()1704 void CellularDataHandler::UnRegisterDataSettingObserver()
1705 {
1706     if (settingObserver_ == nullptr || roamingObserver_ == nullptr || incallObserver_ == nullptr ||
1707         cellularDataRdbObserver_ == nullptr) {
1708         TELEPHONY_LOGE("Slot%{public}d: settingObserver_ or roamingObserver_ or incallObserver_ or "
1709                        "cellularDataRdbObserver_ is null", slotId_);
1710         return;
1711     }
1712     std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
1713     if (settingHelper == nullptr) {
1714         TELEPHONY_LOGE("Slot%{public}d: settingHelper is null", slotId_);
1715         return;
1716     }
1717     Uri dataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
1718     settingHelper->UnRegisterSettingsObserver(dataEnableUri, settingObserver_);
1719 
1720     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
1721     if (simId <= INVALID_SIM_ID) {
1722         TELEPHONY_LOGE("Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
1723         return;
1724     }
1725     Uri dataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
1726     settingHelper->UnRegisterSettingsObserver(dataRoamingUri, roamingObserver_);
1727     Uri dataIncallUri(CELLULAR_DATA_SETTING_DATA_INCALL_URI);
1728     settingHelper->UnRegisterSettingsObserver(dataIncallUri, incallObserver_);
1729 
1730     std::shared_ptr<CellularDataRdbHelper> cellularDataRdbHelper = CellularDataRdbHelper::GetInstance();
1731     if (cellularDataRdbHelper == nullptr) {
1732         TELEPHONY_LOGE("Slot%{public}d: cellularDataRdbHelper is null", slotId_);
1733         return;
1734     }
1735     cellularDataRdbHelper->UnRegisterObserver(cellularDataRdbObserver_);
1736 }
1737 
RegisterDataSettingObserver()1738 void CellularDataHandler::RegisterDataSettingObserver()
1739 {
1740     if (settingObserver_ == nullptr || roamingObserver_ == nullptr || incallObserver_ == nullptr ||
1741         cellularDataRdbObserver_ == nullptr) {
1742         TELEPHONY_LOGE("Slot%{public}d: settingObserver_ or roamingObserver_ or incallObserver_ or "
1743                        "cellularDataRdbObserver_ is null", slotId_);
1744         return;
1745     }
1746     std::shared_ptr<CellularDataSettingsRdbHelper> settingHelper = CellularDataSettingsRdbHelper::GetInstance();
1747     if (settingHelper == nullptr) {
1748         TELEPHONY_LOGE("Slot%{public}d: settingHelper is null", slotId_);
1749         return;
1750     }
1751     Uri dataEnableUri(CELLULAR_DATA_SETTING_DATA_ENABLE_URI);
1752     settingHelper->RegisterSettingsObserver(dataEnableUri, settingObserver_);
1753 
1754     int32_t simId = CoreManagerInner::GetInstance().GetSimId(slotId_);
1755     if (simId <= INVALID_SIM_ID) {
1756         TELEPHONY_LOGE("Slot%{public}d: failed due to invalid sim id %{public}d", slotId_, simId);
1757         return;
1758     }
1759     Uri dataRoamingUri(std::string(CELLULAR_DATA_SETTING_DATA_ROAMING_URI) + std::to_string(simId));
1760     settingHelper->RegisterSettingsObserver(dataRoamingUri, roamingObserver_);
1761     Uri dataIncallUri(CELLULAR_DATA_SETTING_DATA_INCALL_URI);
1762     settingHelper->RegisterSettingsObserver(dataIncallUri, incallObserver_);
1763 
1764     std::shared_ptr<CellularDataRdbHelper> cellularDataRdbHelper = CellularDataRdbHelper::GetInstance();
1765     if (cellularDataRdbHelper == nullptr) {
1766         TELEPHONY_LOGE("Slot%{public}d: cellularDataRdbHelper is null", slotId_);
1767         return;
1768     }
1769     cellularDataRdbHelper->RegisterObserver(cellularDataRdbObserver_);
1770 }
1771 
OnRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer & event)1772 void CellularDataHandler::OnRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer &event)
1773 {
1774     if (connectionManager_ == nullptr) {
1775         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1776         return;
1777     }
1778     TELEPHONY_LOGI("Slot%{public}d: receive event", slotId_);
1779     std::vector<std::shared_ptr<CellularDataStateMachine>> stateMachines =
1780         connectionManager_->GetAllConnectionMachine();
1781     for (const std::shared_ptr<CellularDataStateMachine> &cellularDataStateMachine : stateMachines) {
1782         InnerEvent::Pointer eventCode = InnerEvent::Get(CellularDataEventCode::MSG_SM_RIL_ADAPTER_HOST_DIED);
1783         cellularDataStateMachine->SendEvent(eventCode);
1784     }
1785 }
1786 
GetDataConnApnAttr(ApnItem::Attribute & apnAttr) const1787 void CellularDataHandler::GetDataConnApnAttr(ApnItem::Attribute &apnAttr) const
1788 {
1789     if (apnManager_ == nullptr) {
1790         TELEPHONY_LOGE("Slot%{public}d: GetDataConnApnAttr:apnManager is null", slotId_);
1791         return;
1792     }
1793     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
1794         if (apnHolder == nullptr) {
1795             TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
1796             continue;
1797         }
1798         if (apnHolder->IsDataCallEnabled()) {
1799             sptr<ApnItem> apnItem = apnHolder->GetCurrentApn();
1800             if (apnItem == nullptr) {
1801                 TELEPHONY_LOGE("Slot%{public}d: apnItem is null", slotId_);
1802                 continue;
1803             }
1804             apnAttr = apnItem->attr_;
1805             return;
1806         }
1807     }
1808 }
1809 
GetDataConnIpType() const1810 std::string CellularDataHandler::GetDataConnIpType() const
1811 {
1812     if (apnManager_ == nullptr) {
1813         TELEPHONY_LOGE("Slot%{public}d: GetDataConnApnAttr:apnManager is null", slotId_);
1814         return "";
1815     }
1816     for (const sptr<ApnHolder> &apnHolder : apnManager_->GetAllApnHolder()) {
1817         if (apnHolder == nullptr) {
1818             TELEPHONY_LOGE("Slot%{public}d: apnHolder is null", slotId_);
1819             continue;
1820         }
1821         if (apnHolder->IsDataCallEnabled()) {
1822             auto stateMachine = apnHolder->GetCellularDataStateMachine();
1823             if (stateMachine == nullptr) {
1824                 TELEPHONY_LOGE("Slot%{public}d: stateMachine is null", slotId_);
1825                 continue;
1826             }
1827             return stateMachine->GetIpType();
1828         }
1829     }
1830     return "";
1831 }
1832 
GetDataRecoveryState()1833 int32_t CellularDataHandler::GetDataRecoveryState()
1834 {
1835     if (connectionManager_ == nullptr) {
1836         TELEPHONY_LOGE("Slot%{public}d: connectionManager is null", slotId_);
1837         return -1;
1838     }
1839     return connectionManager_->GetDataRecoveryState();
1840 }
1841 
HandleFactoryReset(const InnerEvent::Pointer & event)1842 void CellularDataHandler::HandleFactoryReset(const InnerEvent::Pointer &event)
1843 {
1844     TELEPHONY_LOGI("Slot%{public}d: factory reset", slotId_);
1845     SetCellularDataEnable(true);
1846     SetCellularDataRoamingEnabled(false);
1847     if (apnManager_ == nullptr) {
1848         TELEPHONY_LOGE("Slot%{public}d: apnManager_ is null", slotId_);
1849         return;
1850     }
1851     apnManager_->ResetApns();
1852 }
1853 
IsNeedDoRecovery(bool needDoRecovery) const1854 void CellularDataHandler::IsNeedDoRecovery(bool needDoRecovery) const
1855 {
1856     if (connectionManager_ == nullptr) {
1857         TELEPHONY_LOGE("Slot%{public}d: in IsNeedDoRecovery connectionManager_ is null", slotId_);
1858         return;
1859     }
1860     connectionManager_->IsNeedDoRecovery(needDoRecovery);
1861 }
1862 
OnCleanAllDataConnectionsDone(const AppExecFwk::InnerEvent::Pointer & event)1863 void CellularDataHandler::OnCleanAllDataConnectionsDone(const AppExecFwk::InnerEvent::Pointer &event)
1864 {
1865     TELEPHONY_LOGI("Slot%{public}d: receive OnCleanAllDataConnectionsDone event", slotId_);
1866 }
1867 } // namespace Telephony
1868 } // namespace OHOS
1869