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_roaming_observer.h" 25 #include "cellular_data_setting_observer.h" 26 #include "cellular_data_state_machine.h" 27 #include "common_event_manager.h" 28 #include "data_switch_settings.h" 29 #include "event_handler.h" 30 #include "hril_data_parcel.h" 31 #include "inner_event.h" 32 #include "radio_event.h" 33 #include "state_notification.h" 34 #include "tel_profile_util.h" 35 36 namespace OHOS { 37 namespace Telephony { 38 class CellularDataHandler : public AppExecFwk::EventHandler, public EventFwk::CommonEventSubscriber { 39 public: 40 explicit CellularDataHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 41 const EventFwk::CommonEventSubscribeInfo &sp, int32_t slotId); 42 ~CellularDataHandler(); 43 void Init(); 44 bool ReleaseNet(const NetRequest &request); 45 bool RequestNet(const NetRequest &request); 46 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 47 void OnReceiveEvent(const EventFwk::CommonEventData &data) override; 48 int32_t SetCellularDataEnable(bool userDataEnabled); 49 int32_t IsCellularDataEnabled(bool &dataEnabled) const; 50 int32_t IsCellularDataRoamingEnabled(bool &dataRoamingEnabled) const; 51 int32_t SetCellularDataRoamingEnabled(bool dataRoamingEnabled); 52 ApnProfileState GetCellularDataState() const; 53 ApnProfileState GetCellularDataState(const std::string &apnType) const; 54 void ClearConnection(const sptr<ApnHolder> &apnHolder, DisConnectionReason reason); 55 void EstablishAllApnsIfConnectable(); 56 void ClearAllConnections(DisConnectionReason reason); 57 int32_t GetSlotId() const; 58 bool HandleApnChanged(); 59 void HandleApnChanged(const AppExecFwk::InnerEvent::Pointer &event); 60 int32_t GetCellularDataFlowType(); 61 void SetPolicyDataOn(bool enable); 62 bool IsRestrictedMode() const; 63 DisConnectionReason GetDisConnectionReason(); 64 bool HasInternetCapability(const int32_t cid) const; 65 66 private: 67 std::shared_ptr<CellularDataStateMachine> CreateCellularDataConnect(); 68 std::shared_ptr<CellularDataStateMachine> FindIdleCellularDataConnection() const; 69 bool CheckCellularDataSlotId(); 70 bool CheckAttachAndSimState(sptr<ApnHolder> &apnHolder); 71 bool CheckRoamingState(sptr<ApnHolder> &apnHolder); 72 bool CheckApnState(sptr<ApnHolder> &apnHolder); 73 void AttemptEstablishDataConnection(sptr<ApnHolder> &apnHolder); 74 bool EstablishDataConnection(sptr<ApnHolder> &apnHolder, int32_t radioTech); 75 void RadioPsConnectionAttached(const AppExecFwk::InnerEvent::Pointer &event); 76 void RadioPsConnectionDetached(const AppExecFwk::InnerEvent::Pointer &event); 77 void RoamingStateOn(const AppExecFwk::InnerEvent::Pointer &event); 78 void RoamingStateOff(const AppExecFwk::InnerEvent::Pointer &event); 79 void PsRadioEmergencyStateOpen(const AppExecFwk::InnerEvent::Pointer &event); 80 void PsRadioEmergencyStateClose(const AppExecFwk::InnerEvent::Pointer &event); 81 void EstablishDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event); 82 void DisconnectDataComplete(const AppExecFwk::InnerEvent::Pointer &event); 83 void MsgEstablishDataConnection(const AppExecFwk::InnerEvent::Pointer &event); 84 void MsgRequestNetwork(const AppExecFwk::InnerEvent::Pointer &event); 85 void HandleSettingSwitchChanged(const AppExecFwk::InnerEvent::Pointer &event); 86 void HandleVoiceCallChanged(int32_t state); 87 void HandleDefaultDataSubscriptionChanged(); 88 void HandleDsdsModeChanged(const AppExecFwk::InnerEvent::Pointer &event); 89 void HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer &event); 90 void HandleSimAccountLoaded(const AppExecFwk::InnerEvent::Pointer &event); 91 void HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 92 void PsDataRatChanged(const AppExecFwk::InnerEvent::Pointer &event); 93 void SetRilAttachApn(); 94 void SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer &event); 95 bool HasAnyHigherPriorityConnection(const sptr<ApnHolder> &apnHolder); 96 void GetConfigurationFor5G(); 97 void GetDefaultConfiguration(); 98 bool ParseOperatorConfig(const std::u16string &configName); 99 void HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 100 void HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event); 101 void GetDefaultUpLinkThresholdsConfig(); 102 void GetDefaultDownLinkThresholdsConfig(); 103 void SetRilLinkBandwidths(); 104 void HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer &event); 105 void HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer &event); 106 void HandleSortConnection(); 107 void SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer &event); 108 void RegisterDataSettingObserver(); 109 void UnRegisterDataSettingObserver(); 110 void SetDataPermitted(int32_t slotId, bool dataPermitted); 111 112 private: 113 sptr<ApnManager> apnManager_; 114 std::unique_ptr<DataSwitchSettings> dataSwitchSettings_; 115 sptr<DataConnectionManager> connectionManager_; 116 std::u16string lastIccId_; 117 int32_t lastCallState_ = (int32_t)TelCallStatus::CALL_STATUS_IDLE; 118 const int32_t slotId_; 119 DisConnectionReason disconnectionReason_ = DisConnectionReason::REASON_NORMAL; 120 std::shared_ptr<AppExecFwk::EventRunner> stateMachineEventLoop_; 121 bool unMeteredAllNsaConfig_ = false; 122 bool unMeteredNrNsaMmwaveConfig_ = false; 123 bool unMeteredNrNsaSub6Config_ = false; 124 bool unMeteredAllNrsaConfig_ = false; 125 bool unMeteredNrsaMmwaveConfig_ = false; 126 bool unMeteredNrsaSub6Config_ = false; 127 bool unMeteredRoamingConfig_ = false; 128 int defaultMobileMtuConfig_ = 0; 129 bool defaultPreferApn_ = true; 130 bool physicalConnectionActiveState_ = false; 131 bool multipleConnectionsEnabled_ = false; 132 std::vector<std::string> upLinkThresholds_; 133 std::vector<std::string> downLinkThresholds_; 134 sptr<CellularDataSettingObserver> settingObserver_; 135 sptr<CellularDataRoamingObserver> roamingObserver_; 136 137 using Fun = void (CellularDataHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 138 std::map<uint32_t, Fun> eventIdMap_ { 139 { RadioEvent::RADIO_PS_CONNECTION_ATTACHED, &CellularDataHandler::RadioPsConnectionAttached }, 140 { RadioEvent::RADIO_PS_CONNECTION_DETACHED, &CellularDataHandler::RadioPsConnectionDetached }, 141 { RadioEvent::RADIO_PS_ROAMING_OPEN, &CellularDataHandler::RoamingStateOn }, 142 { RadioEvent::RADIO_PS_ROAMING_CLOSE, &CellularDataHandler::RoamingStateOff }, 143 { RadioEvent::RADIO_EMERGENCY_STATE_OPEN, &CellularDataHandler::PsRadioEmergencyStateOpen }, 144 { RadioEvent::RADIO_EMERGENCY_STATE_CLOSE, &CellularDataHandler::PsRadioEmergencyStateClose }, 145 { CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, 146 &CellularDataHandler::EstablishDataConnectionComplete }, 147 { CellularDataEventCode::MSG_DISCONNECT_DATA_COMPLETE, &CellularDataHandler::DisconnectDataComplete }, 148 { CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, &CellularDataHandler::MsgEstablishDataConnection }, 149 { CellularDataEventCode::MSG_SETTING_SWITCH, &CellularDataHandler::HandleSettingSwitchChanged }, 150 { CellularDataEventCode::MSG_REQUEST_NETWORK, &CellularDataHandler::MsgRequestNetwork }, 151 { RadioEvent::RADIO_STATE_CHANGED, &CellularDataHandler::HandleRadioStateChanged }, 152 { RadioEvent::RADIO_DSDS_MODE_CHANGED, &CellularDataHandler::HandleDsdsModeChanged }, 153 { RadioEvent::RADIO_SIM_STATE_CHANGE, &CellularDataHandler::HandleSimStateOrRecordsChanged }, 154 { RadioEvent::RADIO_SIM_RECORDS_LOADED, &CellularDataHandler::HandleSimStateOrRecordsChanged }, 155 { RadioEvent::RADIO_SIM_ACCOUNT_LOADED, &CellularDataHandler::HandleSimAccountLoaded }, 156 { RadioEvent::RADIO_PS_RAT_CHANGED, &CellularDataHandler::PsDataRatChanged }, 157 { CellularDataEventCode::MSG_APN_CHANGED, &CellularDataHandler::HandleApnChanged }, 158 { CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, &CellularDataHandler::SetRilAttachApnResponse }, 159 { RadioEvent::RADIO_NR_STATE_CHANGED, &CellularDataHandler::HandleRadioNrStateChanged }, 160 { RadioEvent::RADIO_NR_FREQUENCY_CHANGED, &CellularDataHandler::HandleRadioNrFrequencyChanged }, 161 { CellularDataEventCode::MSG_DB_SETTING_ENABLE_CHANGED, &CellularDataHandler::HandleDBSettingEnableChanged }, 162 { CellularDataEventCode::MSG_DB_SETTING_ROAMING_CHANGED, &CellularDataHandler::HandleDBSettingRoamingChanged }, 163 }; 164 }; 165 } // namespace Telephony 166 } // namespace OHOS 167 #endif // CELLULAR_DATA_HANDLER_H 168