1 /* 2 * Copyright (C) 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 OHOS_WIFI_CONFIG_CENTER_H 17 #define OHOS_WIFI_CONFIG_CENTER_H 18 19 #include <atomic> 20 #include <chrono> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 #include "wifi_internal_msg.h" 26 #include "wifi_settings.h" 27 #include "wifi_scan_config.h" 28 #ifndef STA_INSTANCE_MAX_NUM 29 #define STA_INSTANCE_MAX_NUM 2 30 #endif 31 #define SOFT_BUS_SERVICE_UID 1024 32 #define CAST_ENGINE_SERVICE_UID 5526 33 #define MIRACAST_SERVICE_UID 5529 34 #define MIRACAST_SERVICE_SA_ID 5527 35 #define SHARE_SERVICE_UID 5520 36 #define MOUSE_CROSS_SERVICE_UID 6699 37 namespace OHOS { 38 namespace Wifi { 39 inline const int HID2D_TIMEOUT_INTERVAL = 10 * 1000; 40 using ChannelsTable = std::map<BandType, std::vector<int32_t>>; 41 42 class WifiConfigCenter { 43 public: 44 static WifiConfigCenter &GetInstance(); 45 ~WifiConfigCenter(); 46 47 int Init(); 48 49 std::unique_ptr<WifiScanConfig>& GetWifiScanConfig(); 50 51 void SetWifiSelfcureReset(const bool isReset); 52 53 bool GetWifiSelfcureReset() const; 54 55 void SetWifiSelfcureResetEntered(const bool isReset); 56 57 bool GetWifiSelfcureResetEntered() const; 58 59 void SetLastNetworkId(const int networkId); 60 61 int GetLastNetworkId() const; 62 63 void SetSelectedCandidateNetworkId(const int networkId); 64 65 int GetSelectedCandidateNetworkId() const; 66 67 void SetWifiAllowSemiActive(bool isAllowed); 68 69 bool GetWifiAllowSemiActive() const; 70 71 void SetWifiStopState(bool state); 72 73 bool GetWifiStopState() const; 74 75 void SetStaIfaceName(const std::string &ifaceName, int instId = 0); 76 77 std::string GetStaIfaceName(int instId = 0); 78 79 int GetWifiState(int instId = 0); 80 81 int SetWifiState(int state, int instId = 0); 82 83 WifiDetailState GetWifiDetailState(int instId = 0); 84 85 int SetWifiDetailState(WifiDetailState state, int instId); 86 87 WifiOprMidState GetWifiMidState(int instId = 0); 88 89 bool SetWifiMidState(WifiOprMidState expState, WifiOprMidState state, int instId = 0); 90 91 void SetWifiMidState(WifiOprMidState state, int instId = 0); 92 93 void SetWifiStaCloseTime(int instId = 0); 94 95 double GetWifiStaInterval(int instId = 0); 96 97 bool GetWifiConnectedMode(int instId = 0); 98 99 void SetWifiConnectedMode(bool isContainerConnected, int instId = 0); 100 101 int SetChangeDeviceConfig(ConfigChange value, const WifiDeviceConfig &config); 102 103 bool GetChangeDeviceConfig(ConfigChange& value, WifiDeviceConfig &config); 104 105 int GetIpInfo(IpInfo &info, int instId = 0); 106 107 int SaveIpInfo(const IpInfo &info, int instId = 0); 108 109 int GetIpv6Info(IpV6Info &info, int instId = 0); 110 111 int SaveIpV6Info(const IpV6Info &info, int instId = 0); 112 113 std::map<int, WifiLinkedInfo> GetAllWifiLinkedInfo(); 114 115 int GetLinkedInfo(WifiLinkedInfo &info, int instId = 0); 116 117 int SaveLinkedInfo(const WifiLinkedInfo &info, int instId = 0); 118 119 int GetMloLinkedInfo(std::vector<WifiLinkedInfo> &mloInfo, int instId = 0); 120 121 int SaveMloLinkedInfo(const std::vector<WifiLinkedInfo> &mloInfo, int instId = 0); 122 123 int SetMacAddress(const std::string &macAddress, int instId = 0); 124 125 int GetMacAddress(std::string &macAddress, int instId = 0); 126 127 void SetUserLastSelectedNetworkId(int networkId, int instId = 0); 128 129 int GetUserLastSelectedNetworkId(int instId = 0); 130 131 time_t GetUserLastSelectedNetworkTimeVal(int instId = 0); 132 133 std::string GetConnectTimeoutBssid(int instId = 0); 134 135 int SetConnectTimeoutBssid(std::string &bssid, int instId = 0); 136 137 void SaveDisconnectedReason(DisconnectedReason discReason, int instId = 0); 138 139 int GetDisconnectedReason(DisconnectedReason &discReason, int instId = 0); 140 141 void InsertWifiCategoryBlackListCache(int blacklistType, const std::string currentBssid, 142 const WifiCategoryBlackListInfo wifiBlackListInfo); 143 144 void RemoveWifiCategoryBlackListCache(int blacklistType, const std::string bssid); 145 146 int GetWifiCategoryBlackListCache(int blacklistType, 147 std::map<std::string, WifiCategoryBlackListInfo> &blackListCache); 148 149 void UpdateWifiConnectFailListCache(int blacklistType, const std::string bssid, 150 const WifiCategoryConnectFailInfo wifi7ConnectFailInfo); 151 152 void RemoveWifiConnectFailListCache(const std::string bssid); 153 154 int GetWifiConnectFailListCache( 155 std::map<std::string, WifiCategoryConnectFailInfo> &connectFailCache); 156 157 bool EnableNetwork(int networkId, bool disableOthers, int instId = 0); 158 159 WifiOprMidState GetScanMidState(int instId = 0); 160 161 bool SetScanMidState(WifiOprMidState expState, WifiOprMidState state, int instId = 0); 162 163 void SetScanMidState(WifiOprMidState state, int instId = 0); 164 165 WifiOprMidState GetWifiScanOnlyMidState(int instId = 0); 166 167 bool SetWifiScanOnlyMidState(WifiOprMidState expState, WifiOprMidState state, int instId = 0); 168 169 void SetWifiScanOnlyMidState(WifiOprMidState state, int instId = 0); 170 171 int SetWifiLinkedStandardAndMaxSpeed(WifiLinkedInfo &linkInfo); 172 173 void SetMloWifiLinkedMaxSpeed(int instId = 0); 174 175 bool CheckScanOnlyAvailable(int instId = 0); 176 177 std::string GetConnectedBssid(int instId = 0); 178 179 void SetSoftapToggledState(bool state); 180 181 bool GetSoftapToggledState() const; 182 183 int SetHotspotIdleTimeout(int time); 184 185 int GetHotspotIdleTimeout() const; 186 187 void SetApIfaceName(const std::string &ifaceName); 188 189 std::string GetApIfaceName(); 190 191 WifiOprMidState GetApMidState(int id = 0); 192 193 bool SetApMidState(WifiOprMidState expState, WifiOprMidState state, int id = 0); 194 195 void SetApMidState(WifiOprMidState state, int id = 0); 196 197 int GetHotspotState(int id = 0); 198 199 int SetHotspotState(int state, int id = 0); 200 201 int SetPowerModel(const PowerModel& model, int id = 0); 202 203 int GetPowerModel(PowerModel& model, int id = 0); 204 205 int GetStationList(std::vector<StationInfo> &results, int id = 0); 206 207 int ManageStation(const StationInfo &info, int mode, int id = 0); 208 209 int ClearStationList(int id = 0); 210 211 void SetP2pIfaceName(const std::string &ifaceName); 212 213 std::string GetP2pIfaceName(); 214 215 int SetHid2dUpperScene(int uid, const Hid2dUpperScene &scene); 216 217 int GetHid2dUpperScene(int uid, Hid2dUpperScene &scene); 218 219 int SetHid2dSceneLastSetTime(int64_t setTime); 220 221 int64_t GetHid2dSceneLastSetTime(); 222 223 void ClearLocalHid2dInfo(int uid = 0); 224 225 int SetLastConnStaFreq(int freq); 226 227 int GetLastConnStaFreq(); 228 229 int SetP2pEnhanceState(int state); 230 231 int GetP2pEnhanceState(); 232 233 int SetP2pEnhanceActionListenChannel(int channel); 234 235 int GetP2pEnhanceActionListenChannel(); 236 237 int SetP2pEnhanceFreq(int freq); 238 239 int GetP2pEnhanceFreq(); 240 241 WifiOprMidState GetP2pMidState(); 242 243 bool SetP2pMidState(WifiOprMidState expState, WifiOprMidState state); 244 245 void SetP2pMidState(WifiOprMidState state); 246 247 int SetP2pState(int state); 248 249 int GetP2pState(); 250 251 int SetP2pDiscoverState(int state); 252 253 int GetP2pDiscoverState(); 254 255 int SetP2pBusinessType(const P2pBusinessType &type); 256 257 int GetP2pBusinessType(P2pBusinessType &type); 258 259 int SaveP2pCreatorUid(int uid); 260 261 int GetP2pCreatorUid(); 262 263 void SetExplicitGroup(bool isExplicit); 264 265 bool IsExplicitGroup(void); 266 267 int SaveP2pInfo(WifiP2pLinkedInfo &linkedInfo); 268 269 int GetP2pInfo(WifiP2pLinkedInfo &linkedInfo); 270 271 void SetCurrentP2pGroupInfo(const WifiP2pGroupInfo &group); 272 273 WifiP2pGroupInfo GetCurrentP2pGroupInfo(); 274 275 void SetCoexSupport(bool isSupport); 276 277 bool GetCoexSupport() const; 278 279 void SetScreenState(const int &state); 280 281 int GetScreenState() const; 282 283 void SetThermalLevel(const int &level); 284 285 int GetThermalLevel() const; 286 287 void SetPowerIdelState(const int &state); 288 289 int GetPowerIdelState() const; 290 291 void SetGnssFixState(const int &state); 292 293 int GetGnssFixState() const; 294 295 void SetScanGenieState(const int &state); 296 297 int GetScanGenieState() const; 298 299 bool SetWifiStateOnAirplaneChanged(const int &state); 300 301 int GetAirplaneModeState() const; 302 303 int GetWifiToggledEnable(int id = 0); 304 305 void SetWifiToggledState(int state, int id = 0); 306 307 void SetPowerSavingModeState(const int &state); 308 309 int GetPowerSavingModeState() const; 310 311 void SetFreezeModeState(int state); 312 313 int GetFreezeModeState() const; 314 315 void SetNoChargerPlugModeState(int state); 316 317 int GetNoChargerPlugModeState() const; 318 319 void SetThreadStatusFlag(bool state); 320 321 bool GetThreadStatusFlag(void) const; 322 323 uint64_t GetThreadStartTime(void) const; 324 325 bool StoreWifiMacAddrPairInfo(WifiMacAddrInfoType type, const std::string &realMacAddr, 326 const std::string &randomAddr); 327 328 std::string GetRandomMacAddr(WifiMacAddrInfoType type, std::string bssid); 329 330 std::string GetMacAddrPairs(WifiMacAddrInfoType type, const WifiMacAddrInfo &macAddrInfo); 331 332 void ClearMacAddrPairs(WifiMacAddrInfoType type); 333 334 std::set<int> GetAllWifiLinkedNetworkId(); 335 336 void SetPersistWifiState(int state, int instId); 337 338 int GetPersistWifiState(int instId); 339 340 bool HasWifiActive(); 341 342 void RemoveMacAddrPairInfo(WifiMacAddrInfoType type, std::string bssid, int bssidType); 343 344 void UpdateLinkedInfo(int instId = 0); 345 346 void SetSystemMode(int systemMode); 347 348 int GetSystemMode(); 349 350 void SetDeviceType(int deviceType); 351 352 bool IsAllowPopUp(); 353 354 bool IsNeedFastScan(void); 355 356 void SetFastScan(bool fastScan); 357 private: 358 WifiConfigCenter(); 359 std::string GetPairMacAddress(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap, 360 const WifiMacAddrInfo &macAddrInfo); 361 WifiMacAddrErrCode InsertMacAddrPairs(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap, 362 const WifiMacAddrInfo &macAddrInfo, std::string& randomMacAddr); 363 void DelMacAddrPairs(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap, const WifiMacAddrInfo &macAddrInfo); 364 WifiMacAddrErrCode AddMacAddrPairs(WifiMacAddrInfoType type, const WifiMacAddrInfo &macAddrInfo, 365 std::string randomMacAddr); 366 int RemoveMacAddrPairs(WifiMacAddrInfoType type, const WifiMacAddrInfo &macAddrInfo); 367 368 private: 369 // STA 370 std::mutex mStaMutex; 371 std::atomic<bool> mWifiSelfcureReset {false}; 372 std::atomic<bool> mWifiSelfcureResetEntered {false}; 373 std::atomic<int> mLastNetworkId {INVALID_NETWORK_ID}; 374 std::atomic<int> lastConnStaFreq_ {INVALID_NETWORK_ID}; 375 std::atomic<int> mSelectedCandidateNetworkId {INVALID_NETWORK_ID}; 376 std::atomic<bool> mWifiAllowSemiActive {false}; 377 std::atomic<bool> mWifiStoping {false}; 378 std::vector<std::string> mStaIfaceName = {"wlan0", "wlan1"}; 379 std::map<int, std::atomic<int>> mWifiState; 380 std::map<int, WifiDetailState> mWifiDetailState; 381 std::map<int, std::atomic<WifiOprMidState>> mStaMidState; 382 std::map<int, std::chrono::steady_clock::time_point> mWifiCloseTime; 383 std::map<int, std::atomic<bool>> mIsAncoConnected; 384 std::pair<int, WifiDeviceConfig> mLastRemoveDeviceConfig; 385 std::map<int, IpInfo> mWifiIpInfo; 386 std::map<int, IpV6Info> mWifiIpV6Info; 387 std::map<int, WifiLinkedInfo> mWifiLinkedInfo; 388 std::map<int, std::vector<WifiLinkedInfo>> mWifiMloLinkedInfo; 389 std::map<int, std::string> mMacAddress; 390 std::map<int, int> mLastSelectedNetworkId; 391 std::map<int, time_t> mLastSelectedTimeVal; 392 std::map<int, std::pair<std::string, int>> mBssidToTimeoutTime; 393 std::map<int, DisconnectedReason> mLastDiscReason; 394 std::map<int, std::map<std::string, WifiCategoryBlackListInfo>> mWifiCategoryBlackListCache; 395 std::map<std::string, WifiCategoryConnectFailInfo> mWifiConnectFailCache; 396 397 // SCAN 398 std::mutex mScanMutex; 399 std::map<int, std::atomic<WifiOprMidState>> mScanMidState; 400 std::map<int, std::atomic<WifiOprMidState>> mScanOnlyMidState; 401 std::unique_ptr<WifiScanConfig> wifiScanConfig = nullptr; 402 bool isNeedFastScan = false; 403 404 // AP 405 std::mutex mApMutex; 406 std::atomic<bool> mSoftapToggled {false}; 407 std::atomic<int> mHotspotIdleTimeout {HOTSPOT_IDLE_TIMEOUT_INTERVAL_MS}; 408 std::string mApIfaceName {"wlan0"}; 409 std::map<int, std::atomic<WifiOprMidState>> mApMidState; 410 std::map <int, std::atomic<int>> mHotspotState; 411 std::map<int, PowerModel> powerModel; 412 std::map<std::string, StationInfo> mConnectStationInfo; 413 414 // P2P 415 std::mutex mP2pMutex; 416 #ifdef HDI_CHIP_INTERFACE_SUPPORT 417 std::string mP2pIfaceName {"p2p0"}; 418 #else 419 std::string mP2pIfaceName {"p2p-dev-wlan0"}; 420 #endif 421 std::map<int, Hid2dUpperScene> mHid2dUpperScenePair; 422 std::atomic<int64_t> mHid2dSceneLastSetTime {0}; 423 std::atomic<WifiOprMidState> mP2pMidState {WifiOprMidState::CLOSED}; 424 std::atomic<int> mP2pState {static_cast<int>(P2pState::P2P_STATE_CLOSED)}; 425 std::atomic<int> p2pEnhanceState_ {0}; 426 std::atomic<int> p2pEnhanceFreq_ {0}; 427 std::atomic<int> p2pEnhanceActionListenChannel_ {0}; 428 std::atomic<int> mP2pDiscoverState {0}; 429 std::atomic<P2pBusinessType> mP2pBusinessType {P2pBusinessType::INVALID}; 430 std::atomic<int> mP2pCreatorUid {-1}; 431 std::atomic<bool> mExplicitGroup {false}; 432 WifiP2pLinkedInfo mWifiP2pInfo; 433 WifiP2pGroupInfo m_P2pGroupInfo; 434 435 // COMMON 436 std::atomic<bool> mIsSupportCoex {false}; 437 std::atomic<int> mScreenState {MODE_STATE_DEFAULT}; 438 std::atomic<int> mThermalLevel {static_cast<int>(ThermalLevel::NORMAL)}; 439 std::atomic<int> mPowerIdelState {MODE_STATE_CLOSE}; 440 std::atomic<int> mGnssFixState {MODE_STATE_CLOSE}; 441 std::atomic<int> mScanGenieState {MODE_STATE_OPEN}; 442 std::atomic<int> mAirplaneModeState {MODE_STATE_CLOSE}; 443 std::vector<int> mPersistWifiState {std::vector<int>(2, WIFI_STATE_DISABLED)}; 444 std::atomic<int> mPowerSavingModeState {MODE_STATE_CLOSE}; 445 std::atomic<int> mFreezeModeState {MODE_STATE_CLOSE}; 446 std::atomic<int> mNoChargerPlugModeState {MODE_STATE_CLOSE}; 447 std::atomic<bool> mThreadStatusFlag_ {false}; 448 std::atomic<uint64_t> mThreadStartTime {0}; 449 std::mutex mMacAddrPairMutex; 450 std::map<WifiMacAddrInfo, std::string> mWifiScanMacAddrPair; 451 std::map<WifiMacAddrInfo, std::string> mHotspotMacAddrPair; 452 std::map<WifiMacAddrInfo, std::string> mP2pDeviceMacAddrPair; 453 std::map<WifiMacAddrInfo, std::string> mP2pGroupsInfoMacAddrPair; 454 std::map<WifiMacAddrInfo, std::string> mP2pCurrentgroupMacAddrPair; 455 int systemMode_ = SystemMode::M_DEFAULT; 456 int mDeviceType = ProductDeviceType::DEFAULT; 457 }; 458 } // namespace Wifi 459 } // namespace OHOS 460 #endif 461