1 /* 2 * Copyright (C) 2021-2022 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 NETWORK_SEARCH_INCLUDE_NETWORK_SEARCH_MANAGER_H 17 #define NETWORK_SEARCH_INCLUDE_NETWORK_SEARCH_MANAGER_H 18 19 #include <any> 20 #include <cinttypes> 21 #include <list> 22 #include <map> 23 #include <mutex> 24 #include <string> 25 #include <tuple> 26 27 #include "device_state_handler.h" 28 #include "device_state_observer.h" 29 #include "event_handler.h" 30 #include "i_network_search.h" 31 #include "i_sim_manager.h" 32 #include "i_tel_ril_manager.h" 33 #include "iremote_stub.h" 34 #include "network_search_handler.h" 35 #include "network_search_notify.h" 36 #include "network_search_result.h" 37 #include "network_search_state.h" 38 #include "network_utils.h" 39 #include "observer_handler.h" 40 #include "radio_event.h" 41 #include "setting_utils.h" 42 43 namespace OHOS { 44 namespace Telephony { 45 enum class HandleRunningState { STATE_NOT_START, STATE_RUNNING }; 46 /** 47 * @brief inner objects for network search manager 48 * 49 */ 50 struct NetworkSearchManagerInner { 51 static const int32_t MSG_NUM = 3; 52 int32_t msgNum_ = MSG_NUM; 53 static const int32_t DEFAULT_RAF = 0xffff; 54 std::shared_ptr<NetworkSearchState> networkSearchState_ = nullptr; 55 std::shared_ptr<NetworkSearchHandler> networkSearchHandler_ = nullptr; 56 std::shared_ptr<AppExecFwk::EventRunner> eventLoop_ = nullptr; 57 std::unique_ptr<ObserverHandler> observerHandler_ = nullptr; 58 std::shared_ptr<DeviceStateHandler> deviceStateHandler_ = nullptr; 59 std::shared_ptr<DeviceStateObserver> deviceStateObserver_ = nullptr; 60 sptr<AutoTimeObserver> settingAutoTimeObserver_; 61 sptr<AutoTimezoneObserver> settingAutoTimezoneObserver_; 62 sptr<AirplaneModeObserver> airplaneModeObserver_; 63 HandleRunningState state_ = HandleRunningState::STATE_NOT_START; 64 std::unique_ptr<NetworkSearchResult> networkSearchResult_ = nullptr; 65 SelectionMode selection_ = SelectionMode::MODE_TYPE_UNKNOWN; 66 ModemPowerState radioState_ = ModemPowerState::CORE_SERVICE_POWER_OFF; 67 std::u16string imei_; 68 std::u16string meid_; 69 NrMode nrMode_ = NrMode::NR_MODE_UNKNOWN; 70 FrequencyType freqType_ = FrequencyType::FREQ_TYPE_UNKNOWN; 71 std::mutex mutex_; 72 bool isRadioFirstPowerOn_ = true; 73 74 bool RegisterSetting(); 75 bool UnRegisterSetting(); 76 bool RegisterDeviceStateObserver(); 77 bool UnRegisterDeviceStateObserver(); InitNetworkSearchManagerInner78 bool Init() 79 { 80 if (networkSearchState_ != nullptr) { 81 if (!networkSearchState_->Init()) { 82 return false; 83 } 84 } 85 if (networkSearchHandler_ != nullptr) { 86 if (!networkSearchHandler_->Init()) { 87 return false; 88 } 89 if (!RegisterSetting()) { 90 return false; 91 } 92 } 93 if (deviceStateHandler_ != nullptr) { 94 if (!RegisterDeviceStateObserver()) { 95 return false; 96 } 97 } 98 if (eventLoop_ != nullptr) { 99 eventLoop_->Run(); 100 } 101 state_ = HandleRunningState::STATE_RUNNING; 102 return true; 103 } InitMsgNumNetworkSearchManagerInner104 inline void InitMsgNum() 105 { 106 msgNum_ = MSG_NUM; 107 } CheckIsNeedNotifyNetworkSearchManagerInner108 inline bool CheckIsNeedNotify() 109 { 110 return msgNum_ == 0 ? true : false; 111 } decMsgNumNetworkSearchManagerInner112 inline void decMsgNum() 113 { 114 msgNum_--; 115 } 116 }; 117 118 /** 119 * @brief manager class of network search module .The main entrance to the module. 120 * 121 */ 122 class NetworkSearchManager : public INetworkSearch, public std::enable_shared_from_this<NetworkSearchManager> { 123 public: 124 NetworkSearchManager(std::shared_ptr<ITelRilManager> telRilManager, std::shared_ptr<ISimManager> simManager); 125 virtual ~NetworkSearchManager(); 126 127 bool OnInit() override; 128 void SetRadioState(int32_t slotId, bool isOn, int32_t rst) override; 129 int32_t SetRadioState(int32_t slotId, bool isOn, int32_t rst, NSCALLBACK &callback) override; 130 int32_t GetRadioState(int32_t slotId) override; 131 int32_t GetRadioState(int32_t slotId, NSCALLBACK &callback) override; 132 int32_t GetPsRadioTech(int32_t slotId, int32_t &psRadioTech) override; 133 int32_t GetCsRadioTech(int32_t slotId, int32_t &csRadioTech) override; 134 std::u16string GetOperatorNumeric(int32_t slotId) override; 135 int32_t GetOperatorName(int32_t slotId, std::u16string &operatorName) override; 136 int32_t GetNetworkStatus(int32_t slotId, sptr<NetworkState> &networkState) override; 137 int32_t GetSignalInfoList(int32_t slotId, std::vector<sptr<SignalInformation>> &signals) override; 138 void RegisterCoreNotify(int32_t slotId, HANDLE &handler, int32_t what) override; 139 void UnRegisterCoreNotify(int32_t slotId, HANDLE &handler, int32_t what) override; 140 void RegisterCellularDataObject(const sptr<NetworkSearchCallBackBase> &callback) override; 141 void UnRegisterCellularDataObject(const sptr<NetworkSearchCallBackBase> &callback) override; 142 void RegisterCellularCallObject(const sptr<NetworkSearchCallBackBase> &callback) override; 143 void UnRegisterCellularCallObject(const sptr<NetworkSearchCallBackBase> &callback) override; 144 int32_t GetNetworkSearchInformation(int32_t slotId, NSCALLBACK &callback) override; 145 int32_t GetNetworkSelectionMode(int32_t slotId, NSCALLBACK &callback) override; 146 int32_t SetNetworkSelectionMode(int32_t slotId, int32_t selectMode, 147 const sptr<NetworkInformation> &networkInformation, bool resumeSelection, NSCALLBACK &callback) override; 148 int32_t GetPreferredNetwork(int32_t slotId, NSCALLBACK &callback) override; 149 int32_t SetPreferredNetwork(int32_t slotId, int32_t networkMode, NSCALLBACK &callback) override; 150 int32_t GetIsoCountryCodeForNetwork(int32_t slotId, std::u16string &countryCode) override; 151 int32_t GetImei(int32_t slotId, std::u16string &imei) override; 152 int32_t GetPsRegState(int32_t slotId) override; 153 int32_t GetCsRegState(int32_t slotId) override; 154 int32_t GetPsRoamingState(int32_t slotId) override; 155 int32_t GetCellInfoList(int32_t slotId, std::vector<sptr<CellInformation>> &cellInfo) override; 156 int32_t SendUpdateCellLocationRequest(int32_t slotId) override; 157 sptr<CellLocation> GetCellLocation(int32_t slotId) override; 158 int32_t GetImsRegStatus(int32_t slotId, ImsServiceType imsSrvType, ImsRegInfo &info) override; 159 PhoneType GetPhoneType(int32_t slotId) override; 160 int32_t GetMeid(int32_t slotId, std::u16string &meid) override; 161 int32_t GetUniqueDeviceId(int32_t slotId, std::u16string &deviceId) override; 162 bool IsNrSupported(int32_t slotId) override; 163 FrequencyType GetFrequencyType(int32_t slotId) override; 164 NrState GetNrState(int32_t slotId) override; 165 void DcPhysicalLinkActiveUpdate(int32_t slotId, bool isActive) override; 166 int32_t GetNrOptionMode(int32_t slotId, NrMode &mode) override; 167 int32_t RegisterImsRegInfoCallback(int32_t slotId, ImsServiceType imsSrvType, const std::string &bundleName, 168 const sptr<ImsRegInfoCallback> &callback) override; 169 int32_t UnregisterImsRegInfoCallback( 170 int32_t slotId, ImsServiceType imsSrvType, const std::string &bundleName) override; 171 172 void NotifyPsRoamingOpenChanged(int32_t slotId); 173 void NotifyPsRoamingCloseChanged(int32_t slotId); 174 void NotifyPsConnectionAttachedChanged(int32_t slotId); 175 void NotifyPsConnectionDetachedChanged(int32_t slotId); 176 void NotifyPsRatChanged(int32_t slotId); 177 void NotifyEmergencyOpenChanged(int32_t slotId); 178 void NotifyEmergencyCloseChanged(int32_t slotId); 179 void NotifyNrStateChanged(int32_t slotId); 180 void NotifyNrFrequencyChanged(int32_t slotId); 181 std::shared_ptr<NetworkSearchState> GetNetworkSearchState(int32_t slotId); 182 void TriggerSimRefresh(int32_t slotId); 183 void TriggerTimezoneRefresh(int32_t slotId); 184 void SetNetworkSearchResultValue( 185 int32_t slotId, int32_t listSize, const std::vector<NetworkInformation> &operatorInfo); 186 sptr<NetworkSearchResult> GetNetworkSearchInformationValue(int32_t slotId); 187 int32_t GetNetworkSelectionMode(int32_t slotId); 188 bool SetNetworkSelectionMode( 189 int32_t slotId, int32_t selectMode, const sptr<NetworkInformation> &networkInformation, bool resumeSelection); 190 void SetRadioStateValue(int32_t slotId, ModemPowerState radioState); 191 void SetNetworkSelectionValue(int32_t slotId, SelectionMode selection); 192 bool GetPreferredNetwork(int32_t slotId); 193 bool SetPreferredNetwork(int32_t slotId, int32_t networkMode); 194 void SavePreferredNetworkValue(int32_t slotId, int32_t networkMode); 195 int32_t GetPreferredNetworkValue(int32_t slotId) const; 196 void UpdatePhone(int32_t slotId, RadioTech csRadioTech, const RadioTech &psRadioTech); 197 void SetImei(int32_t slotId, std::u16string imei); 198 void UpdateCellLocation(int32_t slotId, int32_t techType, int32_t cellId, int32_t lac); 199 void SetMeid(int32_t slotId, std::u16string meid); 200 void SetNrOptionMode(int32_t slotId, NrMode mode); 201 void SetFrequencyType(int32_t slotId, FrequencyType type); 202 void GetVoiceTech(int32_t slotId); 203 std::shared_ptr<NetworkSearchManagerInner> FindManagerInner(int32_t slotId); 204 void SetLocateUpdate(int32_t slotId); 205 bool GetAirplaneMode(); 206 bool IsRadioFirstPowerOn(int32_t slotId); 207 void SetRadioFirstPowerOn(int32_t slotId, bool isFirstPowerOn); 208 void NotifyImsRegInfoChanged(int32_t slotId, ImsServiceType imsSrvType, const ImsRegInfo &info); 209 void InitSimRadioProtocol(int32_t slotId); 210 InitMsgNum(int32_t slotId)211 inline void InitMsgNum(int32_t slotId) 212 { 213 auto inner = FindManagerInner(slotId); 214 if (inner != nullptr) { 215 inner->InitMsgNum(); 216 } 217 } CheckIsNeedNotify(int32_t slotId)218 inline bool CheckIsNeedNotify(int32_t slotId) 219 { 220 auto inner = FindManagerInner(slotId); 221 if (inner != nullptr) { 222 return inner->CheckIsNeedNotify(); 223 } 224 return false; 225 } decMsgNum(int32_t slotId)226 inline void decMsgNum(int32_t slotId) 227 { 228 auto inner = FindManagerInner(slotId); 229 if (inner != nullptr) { 230 inner->decMsgNum(); 231 } 232 } GetCellularDataCallBack()233 inline sptr<NetworkSearchCallBackBase> GetCellularDataCallBack() 234 { 235 return cellularDataCallBack_; 236 } GetCellularCallCallBack()237 inline sptr<NetworkSearchCallBackBase> GetCellularCallCallBack() 238 { 239 return cellularCallCallBack_; 240 } GetSimManager()241 inline std::shared_ptr<ISimManager> GetSimManager() const 242 { 243 return simManager_; 244 } 245 246 private: 247 bool InitPointer(std::shared_ptr<NetworkSearchManagerInner> &inner, int32_t slotId); 248 void ClearManagerInner(); 249 void AddManagerInner(int32_t slotId, const std::shared_ptr<NetworkSearchManagerInner> &inner); 250 bool RemoveManagerInner(int32_t slotId); 251 252 private: 253 struct ImsRegInfoCallbackRecord { 254 int32_t slotId; 255 ImsServiceType imsSrvType; 256 std::string bundleName; 257 sptr<ImsRegInfoCallback> imsCallback; 258 }; 259 260 bool AirplaneMode_ = false; 261 sptr<NetworkSearchCallBackBase> cellularDataCallBack_ = nullptr; 262 sptr<NetworkSearchCallBackBase> cellularCallCallBack_ = nullptr; 263 std::shared_ptr<ITelRilManager> telRilManager_ = nullptr; 264 std::shared_ptr<ISimManager> simManager_ = nullptr; 265 std::unique_ptr<EventSender> eventSender_ = nullptr; 266 std::map<int32_t, std::shared_ptr<NetworkSearchManagerInner>> mapManagerInner_; 267 std::list<ImsRegInfoCallbackRecord> listImsRegInfoCallbackRecord_; 268 std::mutex mutexInner_; 269 }; 270 } // namespace Telephony 271 } // namespace OHOS 272 #endif // NETWORK_SEARCH_INCLUDE_NETWORK_SEARCH_MANAGER_H 273