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 void SetDataPermitted(bool dataPermitted); 66 67 private: 68 std::shared_ptr<CellularDataStateMachine> CreateCellularDataConnect(); 69 std::shared_ptr<CellularDataStateMachine> FindIdleCellularDataConnection() const; 70 bool CheckCellularDataSlotId(); 71 bool CheckAttachAndSimState(sptr<ApnHolder> &apnHolder); 72 bool CheckRoamingState(sptr<ApnHolder> &apnHolder); 73 bool CheckApnState(sptr<ApnHolder> &apnHolder); 74 void AttemptEstablishDataConnection(sptr<ApnHolder> &apnHolder); 75 bool EstablishDataConnection(sptr<ApnHolder> &apnHolder, int32_t radioTech); 76 void RadioPsConnectionAttached(const AppExecFwk::InnerEvent::Pointer &event); 77 void RadioPsConnectionDetached(const AppExecFwk::InnerEvent::Pointer &event); 78 void RoamingStateOn(const AppExecFwk::InnerEvent::Pointer &event); 79 void RoamingStateOff(const AppExecFwk::InnerEvent::Pointer &event); 80 void PsRadioEmergencyStateOpen(const AppExecFwk::InnerEvent::Pointer &event); 81 void PsRadioEmergencyStateClose(const AppExecFwk::InnerEvent::Pointer &event); 82 void EstablishDataConnectionComplete(const AppExecFwk::InnerEvent::Pointer &event); 83 void DisconnectDataComplete(const AppExecFwk::InnerEvent::Pointer &event); 84 void MsgEstablishDataConnection(const AppExecFwk::InnerEvent::Pointer &event); 85 void MsgRequestNetwork(const AppExecFwk::InnerEvent::Pointer &event); 86 void HandleSettingSwitchChanged(const AppExecFwk::InnerEvent::Pointer &event); 87 void HandleVoiceCallChanged(int32_t state); 88 void HandleSimStateOrRecordsChanged(const AppExecFwk::InnerEvent::Pointer &event); 89 void HandleSimAccountLoaded(const AppExecFwk::InnerEvent::Pointer &event); 90 void HandleRadioStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 91 void PsDataRatChanged(const AppExecFwk::InnerEvent::Pointer &event); 92 void SetRilAttachApn(); 93 void SetRilAttachApnResponse(const AppExecFwk::InnerEvent::Pointer &event); 94 bool HasAnyHigherPriorityConnection(const sptr<ApnHolder> &apnHolder); 95 void GetConfigurationFor5G(); 96 void GetDefaultConfiguration(); 97 bool ParseOperatorConfig(const std::u16string &configName); 98 void HandleRadioNrStateChanged(const AppExecFwk::InnerEvent::Pointer &event); 99 void HandleRadioNrFrequencyChanged(const AppExecFwk::InnerEvent::Pointer &event); 100 void GetDefaultUpLinkThresholdsConfig(); 101 void GetDefaultDownLinkThresholdsConfig(); 102 void SetRilLinkBandwidths(); 103 void HandleDBSettingEnableChanged(const AppExecFwk::InnerEvent::Pointer &event); 104 void HandleDBSettingRoamingChanged(const AppExecFwk::InnerEvent::Pointer &event); 105 void HandleSortConnection(); 106 void SetDataPermittedResponse(const AppExecFwk::InnerEvent::Pointer &event); 107 void SyncDataPermitted(); 108 void RegisterDataSettingObserver(); 109 void UnRegisterDataSettingObserver(); 110 111 private: 112 sptr<ApnManager> apnManager_; 113 std::unique_ptr<DataSwitchSettings> dataSwitchSettings_; 114 sptr<DataConnectionManager> connectionManager_; 115 std::u16string lastIccId_; 116 int32_t lastCallState_ = (int32_t)TelCallStatus::CALL_STATUS_IDLE; 117 const int32_t slotId_; 118 DisConnectionReason disconnectionReason_ = DisConnectionReason::REASON_NORMAL; 119 std::shared_ptr<AppExecFwk::EventRunner> stateMachineEventLoop_; 120 bool unMeteredAllNsaConfig_ = false; 121 bool unMeteredNrNsaMmwaveConfig_ = false; 122 bool unMeteredNrNsaSub6Config_ = false; 123 bool unMeteredAllNrsaConfig_ = false; 124 bool unMeteredNrsaMmwaveConfig_ = false; 125 bool unMeteredNrsaSub6Config_ = false; 126 bool unMeteredRoamingConfig_ = false; 127 int defaultMobileMtuConfig_ = 0; 128 bool defaultPreferApn_ = true; 129 bool physicalConnectionActiveState_ = false; 130 bool multipleConnectionsEnabled_ = false; 131 std::vector<std::string> upLinkThresholds_; 132 std::vector<std::string> downLinkThresholds_; 133 sptr<CellularDataSettingObserver> settingObserver_; 134 sptr<CellularDataRoamingObserver> roamingObserver_; 135 136 using Fun = void (CellularDataHandler::*)(const AppExecFwk::InnerEvent::Pointer &event); 137 std::map<uint32_t, Fun> eventIdMap_ { 138 {RadioEvent::RADIO_PS_CONNECTION_ATTACHED, &CellularDataHandler::RadioPsConnectionAttached}, 139 {RadioEvent::RADIO_PS_CONNECTION_DETACHED, &CellularDataHandler::RadioPsConnectionDetached}, 140 {RadioEvent::RADIO_PS_ROAMING_OPEN, &CellularDataHandler::RoamingStateOn}, 141 {RadioEvent::RADIO_PS_ROAMING_CLOSE, &CellularDataHandler::RoamingStateOff}, 142 {RadioEvent::RADIO_EMERGENCY_STATE_OPEN, &CellularDataHandler::PsRadioEmergencyStateOpen}, 143 {RadioEvent::RADIO_EMERGENCY_STATE_CLOSE, &CellularDataHandler::PsRadioEmergencyStateClose}, 144 {CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION_COMPLETE, 145 &CellularDataHandler::EstablishDataConnectionComplete}, 146 {CellularDataEventCode::MSG_DISCONNECT_DATA_COMPLETE, &CellularDataHandler::DisconnectDataComplete}, 147 {CellularDataEventCode::MSG_ESTABLISH_DATA_CONNECTION, &CellularDataHandler::MsgEstablishDataConnection}, 148 {CellularDataEventCode::MSG_SETTING_SWITCH, &CellularDataHandler::HandleSettingSwitchChanged}, 149 {CellularDataEventCode::MSG_REQUEST_NETWORK, &CellularDataHandler::MsgRequestNetwork}, 150 {RadioEvent::RADIO_STATE_CHANGED, &CellularDataHandler::HandleRadioStateChanged}, 151 {RadioEvent::RADIO_SIM_STATE_CHANGE, &CellularDataHandler::HandleSimStateOrRecordsChanged}, 152 {RadioEvent::RADIO_SIM_RECORDS_LOADED, &CellularDataHandler::HandleSimStateOrRecordsChanged}, 153 {RadioEvent::RADIO_SIM_ACCOUNT_LOADED, &CellularDataHandler::HandleSimAccountLoaded}, 154 {RadioEvent::RADIO_PS_RAT_CHANGED, &CellularDataHandler::PsDataRatChanged}, 155 {CellularDataEventCode::MSG_APN_CHANGED, &CellularDataHandler::HandleApnChanged}, 156 {CellularDataEventCode::MSG_SET_RIL_ATTACH_APN, &CellularDataHandler::SetRilAttachApnResponse}, 157 {RadioEvent::RADIO_NR_STATE_CHANGED, &CellularDataHandler::HandleRadioNrStateChanged}, 158 {RadioEvent::RADIO_NR_FREQUENCY_CHANGED, &CellularDataHandler::HandleRadioNrFrequencyChanged}, 159 {CellularDataEventCode::MSG_DB_SETTING_ENABLE_CHANGED, &CellularDataHandler::HandleDBSettingEnableChanged}, 160 {CellularDataEventCode::MSG_DB_SETTING_ROAMING_CHANGED, &CellularDataHandler::HandleDBSettingRoamingChanged}, 161 }; 162 }; 163 } // namespace Telephony 164 } // namespace OHOS 165 #endif // CELLULAR_DATA_HANDLER_H 166