1 /* 2 * Copyright (C) 2021-2024 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 "i_network_search.h" 30 #include "i_sim_manager.h" 31 #include "i_tel_ril_manager.h" 32 #include "iremote_stub.h" 33 #include "network_search_handler.h" 34 #include "network_search_notify.h" 35 #include "network_search_result.h" 36 #include "network_search_state.h" 37 #include "network_utils.h" 38 #include "observer_handler.h" 39 #include "radio_event.h" 40 #include "setting_utils.h" 41 42 namespace OHOS { 43 namespace Telephony { 44 enum class HandleRunningState { STATE_NOT_START, STATE_RUNNING }; 45 /** 46 * @brief inner objects for network search manager 47 * 48 */ 49 struct NetworkSearchManagerInner { 50 static const int32_t MSG_NUM = 3; 51 int32_t msgNum_ = 0; 52 static const int32_t DEFAULT_RAF = 0xffff; 53 static const int64_t SERIAL_NUMBER_DEFAULT = -1; 54 static const int64_t SERIAL_NUMBER_THRESHOLD = 1000; 55 static const int64_t SERIAL_NUMBER_EXEMPT = 1100; 56 std::shared_ptr<NetworkSearchState> networkSearchState_ = nullptr; 57 std::shared_ptr<NetworkSearchHandler> networkSearchHandler_ = nullptr; 58 std::unique_ptr<ObserverHandler> observerHandler_ = nullptr; 59 std::shared_ptr<DeviceStateHandler> deviceStateHandler_ = nullptr; 60 std::shared_ptr<DeviceStateObserver> deviceStateObserver_ = nullptr; 61 sptr<AutoTimeObserver> settingAutoTimeObserver_ = nullptr; 62 sptr<AutoTimezoneObserver> settingAutoTimezoneObserver_ = nullptr; 63 sptr<AirplaneModeObserver> airplaneModeObserver_ = nullptr; 64 HandleRunningState state_ = HandleRunningState::STATE_NOT_START; 65 std::unique_ptr<NetworkSearchResult> networkSearchResult_ = nullptr; 66 SelectionMode selection_ = SelectionMode::MODE_TYPE_UNKNOWN; 67 ModemPowerState radioState_ = ModemPowerState::CORE_SERVICE_POWER_OFF; 68 std::u16string imei_ = u""; 69 std::u16string imeiSv_ = u""; 70 std::u16string meid_ = u""; 71 std::string residentNetworkNumeric_ = ""; 72 std::string basebandVersion_ = ""; 73 NrMode nrMode_ = NrMode::NR_MODE_UNKNOWN; 74 int32_t rrcConnectionStatus_ = 0; 75 FrequencyType freqType_ = FrequencyType::FREQ_TYPE_UNKNOWN; 76 std::mutex mutex_; 77 bool isRadioFirstPowerOn_ = true; 78 bool airplaneMode_ = false; 79 int32_t preferredNetworkValue_ = PREFERRED_NETWORK_TYPE; 80 int64_t serialNum_ = SERIAL_NUMBER_DEFAULT; 81 std::mutex msgNumMutex_; 82 std::mutex serialNumMutex_; 83 bool hasCall_ = false; 84 bool skipFlag_ = false; 85 std::mutex skipFlagMutex_; 86 87 bool RegisterSetting(); 88 bool UnRegisterSetting(); 89 bool RegisterDeviceStateObserver(); 90 bool UnRegisterDeviceStateObserver(); InitNetworkSearchManagerInner91 bool Init() 92 { 93 if (networkSearchState_ != nullptr) { 94 if (!networkSearchState_->Init()) { 95 return false; 96 } 97 } 98 if (networkSearchHandler_ != nullptr) { 99 if (!networkSearchHandler_->Init()) { 100 return false; 101 } 102 if (!RegisterSetting()) { 103 return false; 104 } 105 } 106 if (deviceStateHandler_ != nullptr) { 107 if (!RegisterDeviceStateObserver()) { 108 return false; 109 } 110 } 111 state_ = HandleRunningState::STATE_RUNNING; 112 return true; 113 } InitMsgNumNetworkSearchManagerInner114 inline void InitMsgNum() 115 { 116 std::lock_guard<std::mutex> lock(msgNumMutex_); 117 msgNum_ = MSG_NUM; 118 } CheckIsNeedNotifyNetworkSearchManagerInner119 inline bool CheckIsNeedNotify() 120 { 121 std::lock_guard<std::mutex> lock(msgNumMutex_); 122 return msgNum_ == 0 ? true : false; 123 } decMsgNumNetworkSearchManagerInner124 inline void decMsgNum() 125 { 126 std::lock_guard<std::mutex> lock(msgNumMutex_); 127 msgNum_--; 128 } IncreaseSerialNumNetworkSearchManagerInner129 inline int64_t IncreaseSerialNum() 130 { 131 std::lock_guard<std::mutex> lock(serialNumMutex_); 132 if (serialNum_ >= SERIAL_NUMBER_THRESHOLD) { 133 // recycle the serial number. 134 serialNum_ = SERIAL_NUMBER_DEFAULT; 135 } 136 return ++serialNum_; 137 } GetSerialNumNetworkSearchManagerInner138 inline int64_t GetSerialNum() 139 { 140 std::lock_guard<std::mutex> lock(serialNumMutex_); 141 return serialNum_; 142 } SetSkipUnsolRptFlagNetworkSearchManagerInner143 inline void SetSkipUnsolRptFlag(bool res) 144 { 145 std::lock_guard<std::mutex> lock(skipFlagMutex_); 146 skipFlag_ = res; 147 } GetSkipUnsolRptFlagNetworkSearchManagerInner148 inline bool GetSkipUnsolRptFlag() 149 { 150 std::lock_guard<std::mutex> lock(skipFlagMutex_); 151 return skipFlag_; 152 } 153 }; 154 155 /** 156 * @brief manager class of network search module .The main entrance to the module. 157 * 158 */ 159 class NetworkSearchManager : public INetworkSearch, public std::enable_shared_from_this<NetworkSearchManager> { 160 public: 161 NetworkSearchManager(std::shared_ptr<ITelRilManager> telRilManager, std::shared_ptr<ISimManager> simManager); 162 virtual ~NetworkSearchManager(); 163 164 bool OnInit() override; 165 int32_t InitTelExtraModule(int32_t slotId) override; 166 void SetRadioState(int32_t slotId, bool isOn, int32_t rst) override; 167 int32_t SetRadioState(int32_t slotId, bool isOn, int32_t rst, NSCALLBACK &callback) override; 168 int32_t GetRadioState(int32_t slotId) override; 169 int32_t GetRadioState(int32_t slotId, NSCALLBACK &callback) override; 170 int32_t GetPsRadioTech(int32_t slotId, int32_t &psRadioTech) override; 171 int32_t GetCsRadioTech(int32_t slotId, int32_t &csRadioTech) override; 172 std::u16string GetOperatorNumeric(int32_t slotId) override; 173 int32_t GetOperatorName(int32_t slotId, std::u16string &operatorName) override; 174 int32_t GetNetworkStatus(int32_t slotId, sptr<NetworkState> &networkState) override; 175 int32_t GetSignalInfoList(int32_t slotId, std::vector<sptr<SignalInformation>> &signals) override; 176 void RegisterCoreNotify(int32_t slotId, HANDLE &handler, int32_t what) override; 177 void UnRegisterCoreNotify(int32_t slotId, HANDLE &handler, int32_t what) override; 178 void RegisterCellularDataObject(const sptr<NetworkSearchCallBackBase> &callback) override; 179 void UnRegisterCellularDataObject(const sptr<NetworkSearchCallBackBase> &callback) override; 180 void RegisterCellularCallObject(const sptr<NetworkSearchCallBackBase> &callback) override; 181 void UnRegisterCellularCallObject(const sptr<NetworkSearchCallBackBase> &callback) override; 182 int32_t GetNetworkSearchInformation(int32_t slotId, NSCALLBACK &callback) override; 183 int32_t GetNetworkSelectionMode(int32_t slotId, NSCALLBACK &callback) override; 184 int32_t SetNetworkSelectionMode(int32_t slotId, int32_t selectMode, 185 const sptr<NetworkInformation> &networkInformation, bool resumeSelection, NSCALLBACK &callback) override; 186 int32_t GetPreferredNetwork(int32_t slotId, NSCALLBACK &callback) override; 187 int32_t SetPreferredNetwork(int32_t slotId, int32_t networkMode, NSCALLBACK &callback) override; 188 bool SetForcePreferredNetwork(int32_t slotId, int32_t networkMode) override; 189 int32_t GetIsoCountryCodeForNetwork(int32_t slotId, std::u16string &countryCode) override; 190 int32_t GetImei(int32_t slotId, std::u16string &imei) override; 191 int32_t GetImeiSv(int32_t slotId, std::u16string &imeiSv) override; 192 int32_t GetPsRegState(int32_t slotId) override; 193 int32_t GetCsRegState(int32_t slotId) override; 194 int32_t GetPsRoamingState(int32_t slotId) override; 195 int32_t GetCellInfoList(int32_t slotId, std::vector<sptr<CellInformation>> &cellInfo) override; 196 int32_t GetNeighboringCellInfoList(int32_t slotId, std::vector<sptr<CellInformation>> &cellInfo) override; 197 int32_t SendUpdateCellLocationRequest(int32_t slotId) override; 198 sptr<CellLocation> GetCellLocation(int32_t slotId) override; 199 int32_t GetImsRegStatus(int32_t slotId, ImsServiceType imsSrvType, ImsRegInfo &info) override; 200 PhoneType GetPhoneType(int32_t slotId) override; 201 int32_t GetMeid(int32_t slotId, std::u16string &meid) override; 202 int32_t GetUniqueDeviceId(int32_t slotId, std::u16string &deviceId) override; 203 bool IsNrSupported(int32_t slotId) override; 204 bool IsSatelliteEnabled() override; 205 FrequencyType GetFrequencyType(int32_t slotId) override; 206 NrState GetNrState(int32_t slotId) override; 207 void DcPhysicalLinkActiveUpdate(int32_t slotId, bool isActive) override; 208 int32_t NotifyCallStatusToNetworkSearch(int32_t slotId, int32_t callStatus) override; 209 int32_t HandleNotifyStateChangeWithDelay(int32_t slotId, bool isNeedDelay) override; 210 bool IsNeedDelayNotify(int32_t slotId); 211 int32_t SetNrOptionMode(int32_t slotId, int32_t mode) override; 212 int32_t SetNrOptionMode(int32_t slotId, int32_t mode, NSCALLBACK &callback) override; 213 int32_t GetNrOptionMode(int32_t slotId, NrMode &mode) override; 214 int32_t GetNrOptionMode(int32_t slotId, NSCALLBACK &callback) override; 215 int32_t RegisterImsRegInfoCallback(int32_t slotId, ImsServiceType imsSrvType, const int32_t tokenId, 216 const sptr<ImsRegInfoCallback> &callback) override; 217 int32_t UnregisterImsRegInfoCallback( 218 int32_t slotId, ImsServiceType imsSrvType, const int32_t tokenId) override; 219 int32_t GetNetworkCapability( 220 int32_t slotId, int32_t networkCapabilityType, int32_t &networkCapabilityState) override; 221 int32_t SetNetworkCapability( 222 int32_t slotId, int32_t networkCapabilityType, int32_t networkCapabilityState) override; 223 int32_t FactoryReset(int32_t slotId) override; 224 225 void NotifyPsRoamingOpenChanged(int32_t slotId); 226 void NotifyPsRoamingCloseChanged(int32_t slotId); 227 void NotifyPsConnectionAttachedChanged(int32_t slotId); 228 void NotifyPsConnectionDetachedChanged(int32_t slotId); 229 void NotifyPsRatChanged(int32_t slotId); 230 void NotifyEmergencyOpenChanged(int32_t slotId); 231 void NotifyEmergencyCloseChanged(int32_t slotId); 232 void NotifyNrStateChanged(int32_t slotId); 233 void NotifyNrFrequencyChanged(int32_t slotId); 234 void NotifyFactoryReset(int32_t slotId); 235 std::shared_ptr<NetworkSearchState> GetNetworkSearchState(int32_t slotId); 236 void TriggerSimRefresh(int32_t slotId); 237 void TriggerTimezoneRefresh(int32_t slotId); 238 void SetNetworkSearchResultValue( 239 int32_t slotId, int32_t listSize, const std::vector<NetworkInformation> &operatorInfo); 240 sptr<NetworkSearchResult> GetNetworkSearchInformationValue(int32_t slotId); 241 int32_t GetNetworkSelectionMode(int32_t slotId); 242 bool SetNetworkSelectionMode( 243 int32_t slotId, int32_t selectMode, const sptr<NetworkInformation> &networkInformation, bool resumeSelection); 244 void SetRadioStateValue(int32_t slotId, ModemPowerState radioState); 245 void SetNetworkSelectionValue(int32_t slotId, SelectionMode selection); 246 int32_t GetPreferredNetwork(int32_t slotId); 247 int32_t SetCachePreferredNetworkValue(int32_t slotId, int32_t networkMode); 248 int32_t GetCachePreferredNetworkValue(int32_t slotId, int32_t &networkMode); 249 bool SetPreferredNetwork(int32_t slotId, int32_t networkMode) override; 250 void SavePreferredNetworkValue(int32_t slotId, int32_t networkMode); 251 int32_t GetPreferredNetworkValue(int32_t slotId) const; 252 void UpdatePhone(int32_t slotId, RadioTech csRadioTech, const RadioTech &psRadioTech); 253 void SetImei(int32_t slotId, std::u16string imei); 254 void SetImeiSv(int32_t slotId, std::u16string imeiSv); 255 void UpdateCellLocation(int32_t slotId, int32_t techType, int32_t cellId, int32_t lac); 256 void SetMeid(int32_t slotId, std::u16string meid); 257 void SetFrequencyType(int32_t slotId, FrequencyType type); 258 void GetVoiceTech(int32_t slotId); 259 std::shared_ptr<NetworkSearchManagerInner> FindManagerInner(int32_t slotId); 260 void SetLocateUpdate(int32_t slotId); 261 int32_t GetAirplaneMode(bool &airplaneMode) override; 262 void InitAirplaneMode(int32_t slotId) override; 263 int32_t ProcessNotifyStateChangeEvent(int32_t slotId); 264 bool IsRadioFirstPowerOn(int32_t slotId); 265 void SetRadioFirstPowerOn(int32_t slotId, bool isFirstPowerOn); 266 void NotifyImsRegInfoChanged(int32_t slotId, ImsServiceType imsSrvType, const ImsRegInfo &info); 267 void InitSimRadioProtocol(int32_t slotId); 268 int32_t SetLocalAirplaneMode(int32_t slotId, bool state); 269 int32_t GetLocalAirplaneMode(int32_t slotId, bool &state); 270 void SetBasebandVersion(int32_t slotId, std::string version); 271 int32_t GetBasebandVersion(int32_t slotId, std::string &version) override; 272 int32_t UpdateNrOptionMode(int32_t slotId, NrMode mode); 273 int32_t UpdateRadioOn(int32_t slotId) override; 274 int32_t HandleRrcStateChanged(int32_t slotId, int32_t status); 275 int32_t GetRrcConnectionState(int32_t slotId, int32_t &status) override; 276 int32_t UpdateRrcConnectionState(int32_t slotId, int32_t &status); 277 int32_t GetNrSsbId(int32_t slotId, const std::shared_ptr<NrSsbInformation> &nrSsbInformation) override; 278 int32_t IsGsm(int32_t slotId, bool &isGsm) override; 279 int32_t IsCdma(int32_t slotId, bool &isCdma) override; 280 std::string GetResidentNetworkNumeric(int32_t slotId) override; 281 void SetResidentNetworkNumeric(int32_t slotId, std::string operatorNumeric); 282 int32_t ProcessSignalIntensity(int32_t slotId, const Rssi &signalIntensity) override; 283 int32_t StartRadioOnState(int32_t slotId) override; 284 int32_t StartGetRilSignalIntensity(int32_t slotId) override; 285 int32_t UpdateOperatorName(int32_t slotId) override; 286 void UpdateDeviceId(int32_t slotId); 287 InitMsgNum(int32_t slotId)288 inline void InitMsgNum(int32_t slotId) 289 { 290 auto inner = FindManagerInner(slotId); 291 if (inner != nullptr) { 292 inner->InitMsgNum(); 293 } 294 } CheckIsNeedNotify(int32_t slotId)295 inline bool CheckIsNeedNotify(int32_t slotId) 296 { 297 auto inner = FindManagerInner(slotId); 298 if (inner != nullptr) { 299 return inner->CheckIsNeedNotify(); 300 } 301 return false; 302 } decMsgNum(int32_t slotId)303 inline void decMsgNum(int32_t slotId) 304 { 305 auto inner = FindManagerInner(slotId); 306 if (inner != nullptr) { 307 inner->decMsgNum(); 308 } 309 } IncreaseSerialNum(int32_t slotId)310 inline int64_t IncreaseSerialNum(int32_t slotId) 311 { 312 auto inner = FindManagerInner(slotId); 313 if (inner != nullptr) { 314 return inner->IncreaseSerialNum(); 315 } 316 return NetworkSearchManagerInner::SERIAL_NUMBER_DEFAULT; 317 } GetSerialNum(int32_t slotId)318 inline int64_t GetSerialNum(int32_t slotId) 319 { 320 auto inner = FindManagerInner(slotId); 321 if (inner != nullptr) { 322 return inner->GetSerialNum(); 323 } 324 return NetworkSearchManagerInner::SERIAL_NUMBER_DEFAULT; 325 } SetSkipUnsolRptFlag(int32_t slotId,bool res)326 inline void SetSkipUnsolRptFlag(int32_t slotId, bool res) 327 { 328 auto inner = FindManagerInner(slotId); 329 if (inner != nullptr) { 330 inner->SetSkipUnsolRptFlag(res); 331 } 332 } GetSkipUnsolRptFlag(int32_t slotId)333 inline bool GetSkipUnsolRptFlag(int32_t slotId) 334 { 335 auto inner = FindManagerInner(slotId); 336 if (inner != nullptr) { 337 return inner->GetSkipUnsolRptFlag(); 338 } 339 return false; 340 } GetCellularDataCallBack()341 inline sptr<NetworkSearchCallBackBase> GetCellularDataCallBack() 342 { 343 return cellularDataCallBack_; 344 } GetCellularCallCallBack()345 inline sptr<NetworkSearchCallBackBase> GetCellularCallCallBack() 346 { 347 return cellularCallCallBack_; 348 } GetSimManager()349 inline std::shared_ptr<ISimManager> GetSimManager() const 350 { 351 return simManager_; 352 } 353 354 public: 355 static std::mutex ctx_; 356 static bool ssbResponseReady_; 357 static std::condition_variable cv_; 358 359 private: 360 bool InitPointer(std::shared_ptr<NetworkSearchManagerInner> &inner, int32_t slotId); 361 void ClearManagerInner(); 362 void AddManagerInner(int32_t slotId, const std::shared_ptr<NetworkSearchManagerInner> &inner); 363 bool RemoveManagerInner(int32_t slotId); 364 int32_t InitModuleBySlotId(int32_t slotId); 365 int32_t GetDelayNotifyTime(); 366 int32_t RevertLastTechnology(int32_t slotId); 367 int32_t ConvertNetworkModeToCapabilityType(int32_t preferredNetwork); 368 369 private: 370 struct ImsRegInfoCallbackRecord { 371 int32_t slotId; 372 ImsServiceType imsSrvType; 373 int32_t tokenId = 0; 374 sptr<ImsRegInfoCallback> imsCallback; 375 }; 376 377 sptr<NetworkSearchCallBackBase> cellularDataCallBack_ = nullptr; 378 sptr<NetworkSearchCallBackBase> cellularCallCallBack_ = nullptr; 379 std::shared_ptr<ITelRilManager> telRilManager_ = nullptr; 380 std::shared_ptr<ISimManager> simManager_ = nullptr; 381 std::unique_ptr<EventSender> eventSender_ = nullptr; 382 std::map<int32_t, std::shared_ptr<NetworkSearchManagerInner>> mapManagerInner_; 383 std::list<ImsRegInfoCallbackRecord> listImsRegInfoCallbackRecord_; 384 std::mutex mutexInner_; 385 std::mutex mutexIms_; 386 int32_t delayTime_ = 0; 387 [[maybe_unused]] NrMode modem0EflCapability_ = NrMode::NR_MODE_UNKNOWN; 388 [[maybe_unused]] NrMode modem1EflCapability_ = NrMode::NR_MODE_UNKNOWN; 389 }; 390 } // namespace Telephony 391 } // namespace OHOS 392 #endif // NETWORK_SEARCH_INCLUDE_NETWORK_SEARCH_MANAGER_H 393