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 #ifndef OHOS_WIFI_SETTINGS_H 16 #define OHOS_WIFI_SETTINGS_H 17 18 #include <fcntl.h> 19 #include <unistd.h> 20 #include <sys/types.h> 21 #include <sys/stat.h> 22 #include <sys/time.h> 23 #include <string> 24 #include <vector> 25 #include <map> 26 #include <atomic> 27 #include <memory> 28 #include <mutex> 29 #include <algorithm> 30 #ifndef OHOS_ARCH_LITE 31 #include "unique_fd.h" 32 #endif 33 #include "wifi_common_def.h" 34 #include "wifi_common_msg.h" 35 #include "wifi_config_file_impl.h" 36 #include "wifi_event_handler.h" 37 #include "wifi_hisysevent.h" 38 #include "wifi_common_util.h" 39 40 constexpr int RANDOM_STR_LEN = 6; 41 constexpr int RANDOM_PASSWD_LEN = 8; 42 constexpr int MSEC = 1000; 43 constexpr int FOREGROUND_SCAN_CONTROL_TIMES = 4; 44 constexpr int FOREGROUND_SCAN_CONTROL_INTERVAL = 2 * 60; 45 constexpr int BACKGROUND_SCAN_CONTROL_TIMES = 1; 46 constexpr int BACKGROUND_SCAN_CONTROL_INTERVAL = 30 * 60; 47 constexpr int FREQUENCY_CONTINUE_INTERVAL = 5; 48 constexpr int FREQUENCY_CONTINUE_COUNT = 5; 49 constexpr int FREQUENCY_BLOCKLIST_INTERVAL = 20; 50 constexpr int FREQUENCY_BLOCKLIST_COUNT = 10; 51 constexpr int PNO_SCAN_CONTROL_TIMES = 1; 52 constexpr int PNO_SCAN_CONTROL_INTERVAL = 20; 53 constexpr int SYSTEM_TIMER_SCAN_CONTROL_TIMES = 4; 54 constexpr int SYSTEM_TIMER_SCAN_CONTROL_INTERVAL = 10; 55 constexpr int MODE_ADD = 0; 56 constexpr int MODE_DEL = 1; 57 constexpr int MODE_UPDATE = 2; 58 constexpr int ASSOCIATING_SCAN_CONTROL_INTERVAL = 2; 59 constexpr int ASSOCIATED_SCAN_CONTROL_INTERVAL = 5; 60 constexpr int OBTAINING_IP_SCAN_CONTROL_INTERVAL = 5; 61 constexpr int OBTAINING_IP_SCAN_CONTROL_TIMES = 1; 62 /* Obtain the scanning result that is valid within 30s. */ 63 constexpr int WIFI_GET_SCAN_INFO_VALID_TIMESTAMP = 30 * 1000 * 1000; 64 /* Hotspot idle status auto close timeout 10min. */ 65 constexpr int HOTSPOT_IDLE_TIMEOUT_INTERVAL_MS = 10 * 60 * 1000; 66 constexpr int WIFI_DISAPPEAR_TIMES = 3; 67 constexpr int WIFI_MDM_RESTRICTED_MAX_NUM = 200; 68 constexpr uint32_t COMPARE_MAC_OFFSET = 2; 69 /* Plaintext string length */ 70 constexpr uint32_t COMPARE_MAC_LENGTH = 17 - 4; 71 72 inline constexpr char DEVICE_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/device_config.conf"; 73 inline constexpr char BACKUP_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/backup_config.conf"; 74 inline constexpr char HOTSPOT_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/hotspot_config.conf"; 75 inline constexpr char BLOCK_LIST_FILE_PATH[] = CONFIG_ROOR_DIR"/block_list.conf"; 76 inline constexpr char WIFI_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/wifi_config.conf"; 77 inline constexpr char WIFI_P2P_GROUP_INFO_FILE_PATH[] = CONFIG_ROOR_DIR"/p2p_groups.conf"; 78 inline constexpr char WIFI_P2P_VENDOR_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/p2p_vendor_config.conf"; 79 inline constexpr char WIFI_TRUST_LIST_POLICY_FILE_PATH[] = CONFIG_ROOR_DIR"/trust_list_polices.conf"; 80 inline constexpr char WIFI_MOVING_FREEZE_POLICY_FILE_PATH[] = CONFIG_ROOR_DIR"/moving_freeze_policy.conf"; 81 inline constexpr char WIFI_STA_RANDOM_MAC_FILE_PATH[] = CONFIG_ROOR_DIR"/sta_randomMac.conf"; 82 inline constexpr char DUAL_WIFI_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/WifiConfigStore.xml"; 83 inline constexpr char DUAL_SOFTAP_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/WifiConfigStoreSoftAp.xml"; 84 inline constexpr char PACKAGE_FILTER_CONFIG_FILE_PATH[] = "/system/etc/wifi/wifi_package_filter.xml"; 85 inline constexpr char P2P_SUPPLICANT_CONFIG_FILE[] = CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf"; 86 inline constexpr char WIFI_MDM_RESTRICTED_LIST[] = CONFIG_ROOR_DIR"/WifiMdmRestrictedList.conf"; 87 88 namespace OHOS { 89 namespace Wifi { 90 enum class ThermalLevel { 91 COOL = 0, 92 NORMAL = 1, 93 WARM = 2, 94 HOT = 3, 95 OVERHEATED = 4, 96 WARNING = 5, 97 EMERGENCY = 6, 98 }; 99 100 enum WifiMacAddrErrCode { 101 WIFI_MACADDR_OPER_SUCCESS = 0, 102 WIFI_MACADDR_HAS_EXISTED = 1, 103 WIFI_MACADDR_INVALID_PARAM = 2, 104 WIFI_MACADDR_BUTT 105 }; 106 107 class WifiSettings { 108 public: 109 static WifiSettings &GetInstance(); 110 ~WifiSettings(); 111 112 int Init(); 113 114 int AddDeviceConfig(const WifiDeviceConfig &config); 115 116 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 117 ErrCode AddWifiRestrictedListConfig(int uid, const WifiRestrictedInfo& wifiListInfo); 118 119 ErrCode CheckWifiMdmRestrictedList(const std::vector<WifiRestrictedInfo> &wifiRestrictedInfoList); 120 121 ErrCode ClearWifiRestrictedListConfig(int uid); 122 123 int GetMdmRestrictedBlockDeviceConfig(std::vector<WifiDeviceConfig> &results, int instId = 0); 124 125 int SyncWifiRestrictedListConfig(); 126 127 bool FindWifiBlockListConfig(const std::string &ssid, const std::string &bssid, int instId = 0); 128 129 bool FindWifiWhiteListConfig(const std::string &ssid, const std::string &bssid, int instId = 0); 130 131 bool WhetherSetWhiteListConfig(); 132 #endif 133 134 int RemoveDevice(int networkId); 135 136 void ClearDeviceConfig(void); 137 138 int GetDeviceConfig(std::vector<WifiDeviceConfig> &results, int instId = 0); 139 140 int GetDeviceConfig(const int &networkId, WifiDeviceConfig &config, int instId = 0); 141 142 int GetDeviceConfig(const std::string &index, const int &indexType, WifiDeviceConfig &config, int instId = 0); 143 144 int GetDeviceConfig(const std::string &ssid, const std::string &keymgmt, WifiDeviceConfig &config, int instId = 0); 145 146 void SetUserConnectChoice(int networkId); 147 148 void ClearAllNetworkConnectChoice(); 149 150 bool ClearNetworkConnectChoice(int networkId); 151 152 /** 153 * @Description Iterate through all the saved networks and remove the provided config from the connectChoice. 154 * This is invoked when a network is removed from records 155 * 156 * @param networkId - deviceConfig's networkId corresponding to the network that is being removed 157 */ 158 void RemoveConnectChoiceFromAllNetwork(int networkId); 159 160 bool SetNetworkConnectChoice(int networkId, int selectNetworkId, long timestamp); 161 162 /** 163 * @Description this invoked by network selector at the start of every selection procedure to clear all candidate 164 * seen flag 165 * 166 * @param networkId - deviceConfig's networkId 167 * @Return true if the network was found, false otherwise 168 */ 169 bool ClearNetworkCandidateScanResult(int networkId); 170 171 /** 172 * @Description this invoked by network selector when match deviceconfig from scanresults to update if deviceconfig 173 * can be seen for user 174 * 175 * @param networkId - deviceConfig's networkId 176 * @Return true if the network was found, false otherwise 177 */ 178 bool SetNetworkCandidateScanResult(int networkId); 179 180 int SetDeviceEphemeral(int networkId, bool isEphemeral); 181 182 int SetDeviceAfterConnect(int networkId); 183 184 int SetDeviceRandomizedMacSuccessEver(int networkId); 185 186 int SetDeviceEverConnected(int networkId); 187 188 int SetAcceptUnvalidated(int networkId, bool state); 189 190 bool GetDeviceEverConnected(int networkId); 191 192 bool GetAcceptUnvalidated(int networkId); 193 194 int GetCandidateConfigWithoutUid(const std::string &ssid, const std::string &keymgmt, 195 WifiDeviceConfig &config); 196 197 int GetCandidateConfig(const int uid, const std::string &ssid, const std::string &keymgmt, 198 WifiDeviceConfig &config); 199 200 int GetCandidateConfig(const int uid, const int &networkId, WifiDeviceConfig &config); 201 202 int GetAllCandidateConfig(const int uid, std::vector<WifiDeviceConfig> &configs); 203 204 int IncreaseDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 205 206 int SetDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 207 208 int SyncDeviceConfig(); 209 210 bool InKeyMgmtBitset(const WifiDeviceConfig& config, const std::string& keyMgmt); 211 212 void SetKeyMgmtBitset(WifiDeviceConfig &config); 213 214 void GetAllSuitableEncryption(const WifiDeviceConfig &config, const std::string &keyMgmt, 215 std::vector<std::string> &candidateKeyMgmtList); 216 217 int ReloadDeviceConfig(); 218 219 int GetNextNetworkId(); 220 221 int AddWpsDeviceConfig(const WifiDeviceConfig &config); 222 223 #ifndef OHOS_ARCH_LITE 224 int OnRestore(UniqueFd &fd, const std::string &restoreInfo); 225 226 int OnBackup(UniqueFd &fd, const std::string &backupInfo); 227 228 std::string SetBackupReplyCode(int replyCode); 229 230 void RemoveBackupFile(); 231 232 int SetWifiToggleCaller(int callerPid, int instId = 0); 233 #endif 234 235 bool AddRandomMac(WifiStoreRandomMac &randomMacInfo); 236 237 bool GetRandomMac(WifiStoreRandomMac &randomMacInfo); 238 239 const std::vector<TrustListPolicy> ReloadTrustListPolicies(); 240 241 const MovingFreezePolicy ReloadMovingFreezePolicy(); 242 243 int GetPackageInfoMap(std::map<std::string, std::vector<PackageInfo>> &packageInfoMap); 244 245 std::string GetPackageName(std::string tag); 246 247 int SyncHotspotConfig(); 248 249 int SetHotspotConfig(const HotspotConfig &config, int id = 0); 250 251 int GetHotspotConfig(HotspotConfig &config, int id = 0); 252 253 void ClearHotspotConfig(); 254 255 int GetBlockList(std::vector<StationInfo> &results, int id = 0); 256 257 int ManageBlockList(const StationInfo &info, int mode, int id = 0); 258 259 int SyncWifiP2pGroupInfoConfig(); 260 261 int SetWifiP2pGroupInfo(const std::vector<WifiP2pGroupInfo> &groups); 262 263 int RemoveWifiP2pGroupInfo(); 264 265 int RemoveWifiP2pSupplicantGroupInfo(); 266 267 int GetWifiP2pGroupInfo(std::vector<WifiP2pGroupInfo> &groups); 268 269 int SyncP2pVendorConfig(); 270 271 int SetP2pDeviceName(const std::string &deviceName); 272 273 int SetP2pVendorConfig(const P2pVendorConfig &config); 274 275 int GetP2pVendorConfig(P2pVendorConfig &config); 276 277 bool GetScanAlwaysState(int instId = 0); 278 279 int GetSignalLevel(const int &rssi, const int &band, int instId = 0); 280 281 int GetOperatorWifiType(int instId = 0); 282 283 int SetOperatorWifiType(int type, int instId = 0); 284 285 int GetLastAirplaneMode(int instId = 0); 286 287 int SetLastAirplaneMode(int mode, int instId = 0); 288 289 bool GetCanOpenStaWhenAirplaneMode(int instId = 0); 290 291 int SetWifiFlagOnAirplaneMode(bool ifOpen, int instId = 0); 292 293 bool GetWifiFlagOnAirplaneMode(int instId = 0); 294 295 bool GetWifiDisabledByAirplane(int instId = 0); 296 297 int SetWifiDisabledByAirplane(bool disabledByAirplane, int instId = 0); 298 299 int GetStaLastRunState(int instId = 0); 300 301 int SetStaLastRunState(int bRun, int instId = 0); 302 303 int GetDhcpIpType(int instId = 0); 304 305 bool GetWhetherToAllowNetworkSwitchover(int instId = 0); 306 307 int GetScoretacticsScoreSlope(int instId = 0); 308 309 int GetScoretacticsInitScore(int instId = 0); 310 311 int GetScoretacticsSameBssidScore(int instId = 0); 312 313 int GetScoretacticsSameNetworkScore(int instId = 0); 314 315 int GetScoretacticsFrequency5GHzScore(int instId = 0); 316 317 int GetScoretacticsLastSelectionScore(int instId = 0); 318 319 int GetScoretacticsSecurityScore(int instId = 0); 320 321 int GetScoretacticsNormalScore(int instId = 0); 322 323 int GetSavedDeviceAppraisalPriority(int instId = 0); 324 325 bool IsModulePreLoad(const std::string &name); 326 327 bool GetSupportHwPnoFlag(int instId = 0); 328 329 int GetMinRssi2Dot4Ghz(int instId = 0); 330 331 int GetMinRssi5Ghz(int instId = 0); 332 333 int SetRealMacAddress(const std::string &macAddress, int instId = 0); 334 335 int GetRealMacAddress(std::string &macAddress, int instId = 0); 336 337 void SetDefaultFrequenciesByCountryBand(const BandType band, std::vector<int> &frequencies, int instId = 0); 338 339 void SetScanOnlySwitchState(const int &state, int instId = 0); 340 341 int GetScanOnlySwitchState(int instId = 0); 342 343 bool EncryptionDeviceConfig(WifiDeviceConfig &config) const; 344 345 int GetPackageInfoByName(std::string name, std::vector<PackageInfo> &packageInfo); 346 #ifdef SUPPORT_ClOUD_WIFI_ASSET 347 void ApplyCloudWifiConfig(const std::vector<WifiDeviceConfig> &newWifiDeviceConfigs, 348 const std::set<int> &wifiLinkedNetworkIds, std::map<int, WifiDeviceConfig> &tempConfigs); 349 350 void UpdateWifiConfigFromCloud(const std::vector<WifiDeviceConfig> &newWifiDeviceConfigs, 351 const std::set<int> &wifiLinkedNetworkIds); 352 353 void UpLoadLocalDeviceConfigToCloud(); 354 #endif 355 std::string GetDefaultApSsid(); 356 357 private: 358 WifiSettings(); 359 int IncreaseNumRebootsSinceLastUse(); 360 void EncryptionWifiDeviceConfigOnBoot(); 361 int ReloadStaRandomMac(); 362 void InitPackageInfoConfig(); 363 void InitDefaultHotspotConfig(); 364 void InitHotspotConfig(); 365 int SyncBlockList(); 366 int ReloadWifiP2pGroupInfoConfig(); 367 void InitDefaultP2pVendorConfig(); 368 void InitP2pVendorConfig(); 369 int GetApMaxConnNum(); 370 void InitDefaultWifiConfig(); 371 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 372 void InitWifiMdmRestrictedListConfig(); 373 #endif 374 void InitWifiConfig(); 375 int SyncWifiConfig(); 376 std::vector<WifiDeviceConfig> RemoveExcessDeviceConfigs(std::vector<WifiDeviceConfig> &configs) const; 377 std::string FuzzyBssid(const std::string bssid); 378 #ifndef OHOS_ARCH_LITE 379 void MergeWifiConfig(); 380 void MergeSoftapConfig(); 381 void ConfigsDeduplicateAndSave(std::vector<WifiDeviceConfig> &newConfigs); 382 void ParseBackupJson(const std::string &backupInfo, std::string &key, std::string &iv, std::string &version); 383 int GetConfigbyBackupXml(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd); 384 int GetConfigbyBackupFile(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd, const std::string &key, 385 const std::string &iv); 386 #endif 387 #ifdef FEATURE_ENCRYPTION_SUPPORT 388 bool IsWifiDeviceConfigDeciphered(const WifiDeviceConfig &config) const; 389 void DecryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 390 int DecryptionDeviceConfig(WifiDeviceConfig &config); 391 bool EncryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 392 #endif 393 void SyncAfterDecryped(WifiDeviceConfig &config); 394 int GetAllCandidateConfigWithoutUid(std::vector<WifiDeviceConfig> &configs); 395 public: 396 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 397 std::vector<WifiRestrictedInfo> wifiRestrictedList_; 398 #endif 399 private: 400 // STA 401 std::mutex mStaMutex; 402 int mNetworkId; 403 std::atomic_flag deviceConfigLoadFlag = ATOMIC_FLAG_INIT; 404 std::atomic_flag mEncryptionOnBootFlag = ATOMIC_FLAG_INIT; 405 std::map<int, WifiDeviceConfig> mWifiDeviceConfig; 406 WifiConfigFileImpl<WifiDeviceConfig> mSavedDeviceConfig; 407 std::vector<WifiStoreRandomMac> mWifiStoreRandomMac; 408 #ifdef FEATURE_WIFI_MDM_RESTRICTED_SUPPORT 409 WifiConfigFileImpl<WifiRestrictedInfo> wifiMdmRestrictedListConfig_; 410 #endif 411 WifiConfigFileImpl<WifiStoreRandomMac> mSavedWifiStoreRandomMac; 412 std::unique_ptr<WifiEventHandler> mWifiEncryptionThread = nullptr; 413 414 // SCAN 415 std::mutex mScanMutex; 416 WifiConfigFileImpl<TrustListPolicy> mTrustListPolicies; 417 WifiConfigFileImpl<MovingFreezePolicy> mMovingFreezePolicy; 418 std::map<std::string, std::vector<std::string>> mFilterMap; 419 420 // AP 421 std::mutex mApMutex; 422 std::map<int, HotspotConfig> mHotspotConfig; 423 WifiConfigFileImpl<HotspotConfig> mSavedHotspotConfig; 424 std::map<std::string, StationInfo> mBlockListInfo; 425 WifiConfigFileImpl<StationInfo> mSavedBlockInfo; 426 427 // P2P 428 std::mutex mP2pMutex; 429 std::vector<WifiP2pGroupInfo> mGroupInfoList; 430 WifiConfigFileImpl<WifiP2pGroupInfo> mSavedWifiP2pGroupInfo; 431 P2pVendorConfig mP2pVendorConfig; 432 WifiConfigFileImpl<P2pVendorConfig> mSavedWifiP2pVendorConfig; 433 434 // COMMON 435 std::mutex mWifiConfigMutex; 436 std::mutex mSyncWifiConfigMutex; 437 std::mutex mPackageConfMutex; 438 std::atomic<int> mApMaxConnNum; 439 std::atomic<int> mMaxNumConfigs; 440 std::map<int, WifiConfig> mWifiConfig; 441 WifiConfigFileImpl<WifiConfig> mSavedWifiConfig; 442 std::map<std::string, std::vector<PackageInfo>> mPackageInfoMap; 443 }; 444 } // namespace Wifi 445 } // namespace OHOS 446 #endif 447