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_DEVICE_CONFIG_MAX_MUN = 1000; 68 constexpr uint32_t COMPARE_MAC_OFFSET = 2; 69 /* Plaintext string length */ 70 constexpr uint32_t COMPARE_MAC_LENGTH = 17 - 4; 71 inline constexpr char WIFI_VARIABLE_PATH[] = "/system/etc/wifi/wifi_variable.cfg"; 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 87 namespace OHOS { 88 namespace Wifi { 89 enum class ThermalLevel { 90 COOL = 0, 91 NORMAL = 1, 92 WARM = 2, 93 HOT = 3, 94 OVERHEATED = 4, 95 WARNING = 5, 96 EMERGENCY = 6, 97 }; 98 99 enum WifiMacAddrErrCode { 100 WIFI_MACADDR_OPER_SUCCESS = 0, 101 WIFI_MACADDR_HAS_EXISTED = 1, 102 WIFI_MACADDR_INVALID_PARAM = 2, 103 WIFI_MACADDR_BUTT 104 }; 105 106 class WifiSettings { 107 public: 108 static WifiSettings &GetInstance(); 109 ~WifiSettings(); 110 111 int Init(); 112 113 int AddDeviceConfig(const WifiDeviceConfig &config); 114 115 int RemoveDevice(int networkId); 116 117 void ClearDeviceConfig(void); 118 119 int GetDeviceConfig(std::vector<WifiDeviceConfig> &results, int instId = 0); 120 121 int GetDeviceConfig(const int &networkId, WifiDeviceConfig &config, int instId = 0); 122 123 int GetDeviceConfig(const std::string &index, const int &indexType, WifiDeviceConfig &config, int instId = 0); 124 125 int GetDeviceConfig(const std::string &ssid, const std::string &keymgmt, WifiDeviceConfig &config, int instId = 0); 126 127 void SetUserConnectChoice(int networkId); 128 129 void ClearAllNetworkConnectChoice(); 130 131 bool ClearNetworkConnectChoice(int networkId); 132 133 /** 134 * @Description Iterate through all the saved networks and remove the provided config from the connectChoice. 135 * This is invoked when a network is removed from records 136 * 137 * @param networkId - deviceConfig's networkId corresponding to the network that is being removed 138 */ 139 void RemoveConnectChoiceFromAllNetwork(int networkId); 140 141 bool SetNetworkConnectChoice(int networkId, int selectNetworkId, long timestamp); 142 143 /** 144 * @Description this invoked by network selector at the start of every selection procedure to clear all candidate 145 * seen flag 146 * 147 * @param networkId - deviceConfig's networkId 148 * @Return true if the network was found, false otherwise 149 */ 150 bool ClearNetworkCandidateScanResult(int networkId); 151 152 /** 153 * @Description this invoked by network selector when match deviceconfig from scanresults to update if deviceconfig 154 * can be seen for user 155 * 156 * @param networkId - deviceConfig's networkId 157 * @Return true if the network was found, false otherwise 158 */ 159 bool SetNetworkCandidateScanResult(int networkId); 160 161 int SetDeviceEphemeral(int networkId, bool isEphemeral); 162 163 int SetDeviceAfterConnect(int networkId); 164 165 int SetDeviceRandomizedMacSuccessEver(int networkId); 166 167 int SetDeviceEverConnected(int networkId); 168 169 int SetAcceptUnvalidated(int networkId, bool state); 170 171 bool GetDeviceEverConnected(int networkId); 172 173 bool GetAcceptUnvalidated(int networkId); 174 175 int GetCandidateConfigWithoutUid(const std::string &ssid, const std::string &keymgmt, 176 WifiDeviceConfig &config); 177 178 int GetCandidateConfig(const int uid, const std::string &ssid, const std::string &keymgmt, 179 WifiDeviceConfig &config); 180 181 int GetCandidateConfig(const int uid, const int &networkId, WifiDeviceConfig &config); 182 183 int GetAllCandidateConfig(const int uid, std::vector<WifiDeviceConfig> &configs); 184 185 int IncreaseDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 186 187 int SetDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 188 189 int SyncDeviceConfig(); 190 191 bool InKeyMgmtBitset(const WifiDeviceConfig& config, const std::string& keyMgmt); 192 193 void SetKeyMgmtBitset(WifiDeviceConfig &config); 194 195 void GetAllSuitableEncryption(const WifiDeviceConfig &config, const std::string &keyMgmt, 196 std::vector<std::string> &candidateKeyMgmtList); 197 198 int ReloadDeviceConfig(); 199 200 int GetNextNetworkId(); 201 202 int AddWpsDeviceConfig(const WifiDeviceConfig &config); 203 204 #ifndef OHOS_ARCH_LITE 205 int OnRestore(UniqueFd &fd, const std::string &restoreInfo); 206 207 int OnBackup(UniqueFd &fd, const std::string &backupInfo); 208 209 std::string SetBackupReplyCode(int replyCode); 210 211 void RemoveBackupFile(); 212 213 int SetWifiToggleCaller(int callerPid, int instId = 0); 214 #endif 215 216 bool AddRandomMac(WifiStoreRandomMac &randomMacInfo); 217 218 bool GetRandomMac(WifiStoreRandomMac &randomMacInfo); 219 220 const std::vector<TrustListPolicy> ReloadTrustListPolicies(); 221 222 const MovingFreezePolicy ReloadMovingFreezePolicy(); 223 224 int GetPackageInfoMap(std::map<std::string, std::vector<PackageInfo>> &packageInfoMap); 225 226 std::string GetPackageName(std::string tag); 227 228 int SyncHotspotConfig(); 229 230 int SetHotspotConfig(const HotspotConfig &config, int id = 0); 231 232 int GetHotspotConfig(HotspotConfig &config, int id = 0); 233 234 void ClearHotspotConfig(); 235 236 int GetBlockList(std::vector<StationInfo> &results, int id = 0); 237 238 int ManageBlockList(const StationInfo &info, int mode, int id = 0); 239 240 int SyncWifiP2pGroupInfoConfig(); 241 242 int SetWifiP2pGroupInfo(const std::vector<WifiP2pGroupInfo> &groups); 243 244 int RemoveWifiP2pGroupInfo(); 245 246 int RemoveWifiP2pSupplicantGroupInfo(); 247 248 int GetWifiP2pGroupInfo(std::vector<WifiP2pGroupInfo> &groups); 249 250 int SyncP2pVendorConfig(); 251 252 int SetP2pDeviceName(const std::string &deviceName); 253 254 int SetP2pVendorConfig(const P2pVendorConfig &config); 255 256 int GetP2pVendorConfig(P2pVendorConfig &config); 257 258 bool GetScanAlwaysState(int instId = 0); 259 260 int GetSignalLevel(const int &rssi, const int &band, int instId = 0); 261 262 int GetOperatorWifiType(int instId = 0); 263 264 int SetOperatorWifiType(int type, int instId = 0); 265 266 int GetLastAirplaneMode(int instId = 0); 267 268 int SetLastAirplaneMode(int mode, int instId = 0); 269 270 bool GetCanOpenStaWhenAirplaneMode(int instId = 0); 271 272 int SetWifiFlagOnAirplaneMode(bool ifOpen, int instId = 0); 273 274 bool GetWifiFlagOnAirplaneMode(int instId = 0); 275 276 bool GetWifiDisabledByAirplane(int instId = 0); 277 278 int SetWifiDisabledByAirplane(bool disabledByAirplane, int instId = 0); 279 280 int GetStaLastRunState(int instId = 0); 281 282 int SetStaLastRunState(int bRun, int instId = 0); 283 284 int GetDhcpIpType(int instId = 0); 285 286 bool GetWhetherToAllowNetworkSwitchover(int instId = 0); 287 288 int GetScoretacticsScoreSlope(int instId = 0); 289 290 int GetScoretacticsInitScore(int instId = 0); 291 292 int GetScoretacticsSameBssidScore(int instId = 0); 293 294 int GetScoretacticsSameNetworkScore(int instId = 0); 295 296 int GetScoretacticsFrequency5GHzScore(int instId = 0); 297 298 int GetScoretacticsLastSelectionScore(int instId = 0); 299 300 int GetScoretacticsSecurityScore(int instId = 0); 301 302 int GetScoretacticsNormalScore(int instId = 0); 303 304 int GetSavedDeviceAppraisalPriority(int instId = 0); 305 306 bool IsModulePreLoad(const std::string &name); 307 308 bool GetSupportHwPnoFlag(int instId = 0); 309 310 int GetMinRssi2Dot4Ghz(int instId = 0); 311 312 int GetMinRssi5Ghz(int instId = 0); 313 314 int SetRealMacAddress(const std::string &macAddress, int instId = 0); 315 316 int GetRealMacAddress(std::string &macAddress, int instId = 0); 317 318 void SetDefaultFrequenciesByCountryBand(const BandType band, std::vector<int> &frequencies, int instId = 0); 319 320 void SetScanOnlySwitchState(const int &state, int instId = 0); 321 322 int GetScanOnlySwitchState(int instId = 0); 323 324 bool EncryptionDeviceConfig(WifiDeviceConfig &config) const; 325 326 #ifdef SUPPORT_ClOUD_WIFI_ASSET 327 void ApplyCloudWifiConfig(const std::vector<WifiDeviceConfig> &newWifiDeviceConfigs, 328 const std::set<int> &wifiLinkedNetworkIds, std::map<int, WifiDeviceConfig> &tempConfigs); 329 330 void UpdateWifiConfigFromCloud(const std::vector<WifiDeviceConfig> &newWifiDeviceConfigs, 331 const std::set<int> &wifiLinkedNetworkIds); 332 333 void UpLoadLocalDeviceConfigToCloud(); 334 #endif 335 336 private: 337 WifiSettings(); 338 int IncreaseNumRebootsSinceLastUse(); 339 void EncryptionWifiDeviceConfigOnBoot(); 340 int ReloadStaRandomMac(); 341 void InitPackageInfoConfig(); 342 void InitDefaultHotspotConfig(); 343 void InitHotspotConfig(); 344 int SyncBlockList(); 345 int ReloadWifiP2pGroupInfoConfig(); 346 void InitDefaultP2pVendorConfig(); 347 void InitP2pVendorConfig(); 348 int GetApMaxConnNum(); 349 void InitDefaultWifiConfig(); 350 void InitWifiConfig(); 351 int SyncWifiConfig(); 352 int RemoveExcessDeviceConfigs(std::vector<WifiDeviceConfig> &configs) const; 353 std::string FuzzyBssid(const std::string bssid); 354 #ifndef OHOS_ARCH_LITE 355 void MergeWifiConfig(); 356 void MergeSoftapConfig(); 357 void ConfigsDeduplicateAndSave(std::vector<WifiDeviceConfig> &newConfigs); 358 void ParseBackupJson(const std::string &backupInfo, std::string &key, std::string &iv, std::string &version); 359 int GetConfigbyBackupXml(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd); 360 int GetConfigbyBackupFile(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd, const std::string &key, 361 const std::string &iv); 362 #endif 363 #ifdef FEATURE_ENCRYPTION_SUPPORT 364 bool IsWifiDeviceConfigDeciphered(const WifiDeviceConfig &config) const; 365 void DecryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 366 int DecryptionDeviceConfig(WifiDeviceConfig &config); 367 bool EncryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 368 #endif 369 void SyncAfterDecryped(WifiDeviceConfig &config); 370 int GetAllCandidateConfigWithoutUid(std::vector<WifiDeviceConfig> &configs); 371 private: 372 // STA 373 std::mutex mStaMutex; 374 std::mutex mConfigOnBootMutex; 375 int mNetworkId; 376 std::atomic_flag deviceConfigLoadFlag = ATOMIC_FLAG_INIT; 377 std::atomic_flag mEncryptionOnBootFalg = ATOMIC_FLAG_INIT; 378 std::map<int, WifiDeviceConfig> mWifiDeviceConfig; 379 WifiConfigFileImpl<WifiDeviceConfig> mSavedDeviceConfig; 380 std::vector<WifiStoreRandomMac> mWifiStoreRandomMac; 381 WifiConfigFileImpl<WifiStoreRandomMac> mSavedWifiStoreRandomMac; 382 std::unique_ptr<WifiEventHandler> mWifiEncryptionThread = nullptr; 383 384 // SCAN 385 std::mutex mScanMutex; 386 WifiConfigFileImpl<TrustListPolicy> mTrustListPolicies; 387 WifiConfigFileImpl<MovingFreezePolicy> mMovingFreezePolicy; 388 std::map<std::string, std::vector<std::string>> mFilterMap; 389 390 // AP 391 std::mutex mApMutex; 392 std::map<int, HotspotConfig> mHotspotConfig; 393 WifiConfigFileImpl<HotspotConfig> mSavedHotspotConfig; 394 std::map<std::string, StationInfo> mBlockListInfo; 395 WifiConfigFileImpl<StationInfo> mSavedBlockInfo; 396 397 // P2P 398 std::mutex mP2pMutex; 399 std::vector<WifiP2pGroupInfo> mGroupInfoList; 400 WifiConfigFileImpl<WifiP2pGroupInfo> mSavedWifiP2pGroupInfo; 401 P2pVendorConfig mP2pVendorConfig; 402 WifiConfigFileImpl<P2pVendorConfig> mSavedWifiP2pVendorConfig; 403 404 // COMMON 405 std::mutex mWifiConfigMutex; 406 std::mutex mPackageConfMutex; 407 std::atomic<int> mApMaxConnNum; 408 std::atomic<int> mMaxNumConfigs; 409 std::map<int, WifiConfig> mWifiConfig; 410 WifiConfigFileImpl<WifiConfig> mSavedWifiConfig; 411 std::map<std::string, std::vector<PackageInfo>> mPackageInfoMap; 412 }; 413 } // namespace Wifi 414 } // namespace OHOS 415 #endif 416