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