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 #ifndef CELLULAR_DATA_HANDLER_H 17 #define CELLULAR_DATA_HANDLER_H 18 19 #include <atomic> 20 #include <memory> 21 22 #include "apn_manager.h" 23 #include "cellular_data_event_code.h" 24 #include "cellular_data_incall_observer.h" 25 #include "cellular_data_rdb_observer.h" 26 #include "cellular_data_roaming_observer.h" 27 #include "cellular_data_setting_observer.h" 28 #include "cellular_data_state_machine.h" 29 #include "common_event_manager.h" 30 #include "data_switch_settings.h" 31 #include "hril_data_parcel.h" 32 #include "incall_data_state_machine.h" 33 #include "inner_event.h" 34 #include "radio_event.h" 35 #include "state_notification.h" 36 #include "tel_event_handler.h" 37 #include "tel_profile_util.h" 38 39 namespace OHOS { 40 namespace Telephony { 41 class CellularDataHandler : public TelEventHandler, public EventFwk::CommonEventSubscriber { 42 public: 43 explicit CellularDataHandler(const EventFwk::CommonEventSubscribeInfo &sp, int32_t slotId); 44 ~CellularDataHandler(); 45 void Init(); 46 bool ReleaseNet(const NetRequest &request); 47 bool RequestNet(const NetRequest &request); 48 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 49 void OnReceiveEvent(const EventFwk::CommonEventData &data) override; 50 int32_t SetCellularDataEnable(bool userDataEnabled); 51 int32_t SetIntelligenceSwitchEnable(bool userDataEnabled); 52 int32_t IsCellularDataEnabled(bool &dataEnabled) const; 53 int32_t IsCellularDataRoamingEnabled(bool &dataRoamingEnabled) const; 54 int32_t SetCellularDataRoamingEnabled(bool dataRoamingEnabled); 55 ApnProfileState GetCellularDataState() const; 56 ApnProfileState GetCellularDataState(const std::string &apnType) const; 57 void ClearConnection(const sptr<ApnHolder> &apnHolder, DisConnectionReason reason); 58 void EstablishAllApnsIfConnectable(); 59 void ClearAllConnections(DisConnectionReason reason); 60 bool ChangeConnectionForDsds(bool enable); 61 int32_t GetSlotId() const; 62 bool HandleApnChanged(); 63 void HandleApnChanged(const AppExecFwk::InnerEvent::Pointer &event); 64 int32_t GetCellularDataFlowType(); 65 void SetPolicyDataOn(bool enable); 66 bool IsRestrictedMode() const; 67 DisConnectionReason GetDisConnectionReason(); 68 bool HasInternetCapability(const int32_t cid) const; 69 void GetDataConnApnAttr(ApnItem::Attribute &apnAttr) const; 70 std::string GetDataConnIpType() const; 71 int32_t GetDataRecoveryState(); 72 void SetRilAttachApn(); 73 void IsNeedDoRecovery(bool needDoRecovery) const; 74 void RegisterDataSettingObserver(); 75 void UnRegisterDataSettingObserver(); 76 77 private: 78 std::shared_ptr<CellularDataStateMachine> CreateCellularDataConnect(); 79 std::shared_ptr<CellularDataStateMachine> FindIdleCellularDataConnection() const; 80 bool CheckCellularDataSlotId(sptr<ApnHolder> &apnHolder); 81 bool CheckAttachAndSimState(sptr<ApnHolder> &apnHolder); 82 bool CheckRoamingState(sptr<ApnHolder> &apnHolder); 83 bool CheckApnState(sptr<ApnHolder> &apnHolder); 84 void AttemptEstablishDataConnection(sptr<ApnHolder> &apnHolder); 85 bool EstablishDataConnection(sptr<ApnHolder> &apnHolder, int32_t radioTech); 86 void RadioPsConnectionAttached(const AppExecFwk::InnerEvent::Pointer &event); 87 void RadioPsConnectionDetached(const AppExecFwk::InnerEvent::Pointer &event); 88 void RoamingStateOn(const AppExecFwk::InnerEvent::Pointer &event); 89 void RoamingStateOff(const AppExecFwk::InnerEvent::Pointer &event); 90 void PsRadioEmergencyStateOpen(const AppExecFwk::InnerEvent::Pointer &event); 91 void PsRadioEmergencyStateClose(const AppExecFwk::InnerEvent::Pointer &event); 92 void EstablishDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event); 93 void DisconnectDataComplete(const AppExecFwk::InnerEvent::Pointer &event); 94 void MsgEstablishDataConnection(const AppExecFwk::InnerEvent::Pointer &event); 95 void MsgRequestNetwork(const AppExecFwk::InnerEvent::Pointer &event); 96 void HandleSettingSwitchChanged(const AppExecFwk::InnerEvent::Pointer &event); 97 void HandleVoiceCallChanged(int32_t state); 98 void HandleDefaultDataSubscriptionChanged(); 99 void HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer &event); 100 void HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer &event); 101 void HandleSimAccountLoaded(const AppExecFwk::InnerEvent::Pointer &event); 102 void HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 103 void PsDataRatChanged(const AppExecFwk::InnerEvent::Pointer &event); 104 void SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer &event); 105 bool HasAnyHigherPriorityConnection(const sptr<ApnHolder> &apnHolder); 106 void GetConfigurationFor5G(); 107 void GetDefaultConfiguration(); 108 bool ParseOperatorConfig(const std::u16string &configName); 109 void HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 110 void HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event); 111 void GetDefaultUpLinkThresholdsConfig(); 112 void GetDefaultDownLinkThresholdsConfig(); 113 void SetRilLinkBandwidths(); 114 void HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer &event); 115 void HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer &event); 116 void HandleSortConnection(); 117 void SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer &event); 118 void SetDataPermitted(int32_t slotId, bool dataPermitted); 119 bool CheckDataPermittedByDsds(); 120 bool SetDataPermittedForMms(bool dataPermittedForMms); 121 std::shared_ptr<IncallDataStateMachine> CreateIncallDataStateMachine(int32_t callState); 122 void HandleDBSettingIncallChanged(const AppExecFwk::InnerEvent::Pointer &event); 123 void HandleCallChanged(int32_t state); 124 void HandleImsCallChanged(int32_t state); 125 void IncallDataComplete(const AppExecFwk::InnerEvent::Pointer &event); 126 void ResetDataFlowType(); 127 void ClearConnectionIfRequired(); 128 bool GetEsmFlagFromOpCfg(); 129 void SetInitApnWithNullDp(); 130 void GetSinglePdpEnabledFromOpCfg(); 131 bool IsSingleConnectionEnabled(int32_t radioTech); 132 void OnRilAdapterHostDied(const AppExecFwk::InnerEvent::Pointer &event); 133 void HandleFactoryReset(const AppExecFwk::InnerEvent::Pointer &event); 134 void OnCleanAllDataConnectionsDone(const AppExecFwk::InnerEvent::Pointer &event); 135 void CreateApnItem(); 136 void UpdatePhysicalConnectionState(bool noActiveConnection); 137 138 private: 139 sptr<ApnManager> apnManager_; 140 std::unique_ptr<DataSwitchSettings> dataSwitchSettings_; 141 sptr<DataConnectionManager> connectionManager_; 142 std::u16string lastIccId_; 143 int32_t lastCallState_ = (int32_t)TelCallStatus::CALL_STATUS_IDLE; 144 const int32_t slotId_; 145 DisConnectionReason disconnectionReason_ = DisConnectionReason::REASON_NORMAL; 146 bool unMeteredAllNsaConfig_ = false; 147 bool unMeteredNrNsaMmwaveConfig_ = false; 148 bool unMeteredNrNsaSub6Config_ = false; 149 bool unMeteredAllNrsaConfig_ = false; 150 bool unMeteredNrsaMmwaveConfig_ = false; 151 bool unMeteredNrsaSub6Config_ = false; 152 bool unMeteredRoamingConfig_ = false; 153 int defaultMobileMtuConfig_ = 0; 154 bool defaultPreferApn_ = true; 155 bool physicalConnectionActiveState_ = false; 156 bool multipleConnectionsEnabled_ = false; 157 std::vector<std::string> upLinkThresholds_; 158 std::vector<std::string> downLinkThresholds_; 159 sptr<CellularDataSettingObserver> settingObserver_; 160 sptr<CellularDataRoamingObserver> roamingObserver_; 161 sptr<CellularDataIncallObserver> incallObserver_; 162 sptr<CellularDataRdbObserver> cellularDataRdbObserver_; 163 std::shared_ptr<IncallDataStateMachine> incallDataStateMachine_; 164 165 using Fun = void (CellularDataHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 166 std::map<uint32_t, Fun> eventIdMap_ { 167 { RadioEvent::RADIO_PS_CONNECTION_ATTACHED, &CellularDataHandler::RadioPsConnectionAttached }, 168 { RadioEvent::RADIO_PS_CONNECTION_DETACHED, &CellularDataHandler::RadioPsConnectionDetached }, 169 { RadioEvent::RADIO_PS_ROAMING_OPEN, &CellularDataHandler::RoamingStateOn }, 170 { RadioEvent::RADIO_PS_ROAMING_CLOSE, &CellularDataHandler::RoamingStateOff }, 171 { RadioEvent::RADIO_EMERGENCY_STATE_OPEN, &CellularDataHandler::PsRadioEmergencyStateOpen }, 172 { RadioEvent::RADIO_EMERGENCY_STATE_CLOSE, &CellularDataHandler::PsRadioEmergencyStateClose }, 173 { CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, 174 &CellularDataHandler::EstablishDataConnectionComplete }, 175 { CellularDataEventCode::MSG_DISCONNECT_DATA_COMPLETE, &CellularDataHandler::DisconnectDataComplete }, 176 { CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, &CellularDataHandler::MsgEstablishDataConnection }, 177 { CellularDataEventCode::MSG_SETTING_SWITCH, &CellularDataHandler::HandleSettingSwitchChanged }, 178 { CellularDataEventCode::MSG_REQUEST_NETWORK, &CellularDataHandler::MsgRequestNetwork }, 179 { RadioEvent::RADIO_STATE_CHANGED, &CellularDataHandler::HandleRadioStateChanged }, 180 { RadioEvent::RADIO_DSDS_MODE_CHANGED, &CellularDataHandler::HandleDsdsModeChanged }, 181 { RadioEvent::RADIO_SIM_STATE_CHANGE, &CellularDataHandler::HandleSimStateOrRecordsChanged }, 182 { RadioEvent::RADIO_SIM_RECORDS_LOADED, &CellularDataHandler::HandleSimStateOrRecordsChanged }, 183 { RadioEvent::RADIO_SIM_ACCOUNT_LOADED, &CellularDataHandler::HandleSimAccountLoaded }, 184 { RadioEvent::RADIO_PS_RAT_CHANGED, &CellularDataHandler::PsDataRatChanged }, 185 { CellularDataEventCode::MSG_APN_CHANGED, &CellularDataHandler::HandleApnChanged }, 186 { CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, &CellularDataHandler::SetRilAttachApnResponse }, 187 { RadioEvent::RADIO_NR_STATE_CHANGED, &CellularDataHandler::HandleRadioNrStateChanged }, 188 { RadioEvent::RADIO_NR_FREQUENCY_CHANGED, &CellularDataHandler::HandleRadioNrFrequencyChanged }, 189 { CellularDataEventCode::MSG_DB_SETTING_ENABLE_CHANGED, &CellularDataHandler::HandleDBSettingEnableChanged }, 190 { CellularDataEventCode::MSG_DB_SETTING_ROAMING_CHANGED, &CellularDataHandler::HandleDBSettingRoamingChanged }, 191 { CellularDataEventCode::MSG_DB_SETTING_INCALL_CHANGED, &CellularDataHandler::HandleDBSettingIncallChanged }, 192 { CellularDataEventCode::MSG_INCALL_DATA_COMPLETE, &CellularDataHandler::IncallDataComplete }, 193 { RadioEvent::RADIO_RIL_ADAPTER_HOST_DIED, &CellularDataHandler::OnRilAdapterHostDied }, 194 { RadioEvent::RADIO_FACTORY_RESET, &CellularDataHandler::HandleFactoryReset }, 195 { RadioEvent::RADIO_CLEAN_ALL_DATA_CONNECTIONS, &CellularDataHandler::OnCleanAllDataConnectionsDone }, 196 }; 197 }; 198 } // namespace Telephony 199 } // namespace OHOS 200 #endif // CELLULAR_DATA_HANDLER_H 201