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 72 constexpr char DEVICE_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/device_config.conf"; 73 constexpr char BACKUP_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/backup_config.conf"; 74 constexpr char HOTSPOT_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/hotspot_config.conf"; 75 constexpr char BLOCK_LIST_FILE_PATH[] = CONFIG_ROOR_DIR"/block_list.conf"; 76 constexpr char WIFI_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/wifi_config.conf"; 77 constexpr char WIFI_P2P_GROUP_INFO_FILE_PATH[] = CONFIG_ROOR_DIR"/p2p_groups.conf"; 78 constexpr char WIFI_P2P_VENDOR_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/p2p_vendor_config.conf"; 79 const std::string WIFI_TRUST_LIST_POLICY_FILE_PATH = CONFIG_ROOR_DIR"/trust_list_polices.conf"; 80 const std::string WIFI_MOVING_FREEZE_POLICY_FILE_PATH = CONFIG_ROOR_DIR"/moving_freeze_policy.conf"; 81 constexpr char WIFI_STA_RANDOM_MAC_FILE_PATH[] = CONFIG_ROOR_DIR"/sta_randomMac.conf"; 82 constexpr char DUAL_WIFI_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/WifiConfigStore.xml"; 83 constexpr char DUAL_SOFTAP_CONFIG_FILE_PATH[] = CONFIG_ROOR_DIR"/WifiConfigStoreSoftAp.xml"; 84 constexpr char PACKAGE_FILTER_CONFIG_FILE_PATH[] = "/system/etc/wifi/wifi_package_filter.cfg"; 85 constexpr char P2P_SUPPLICANT_CONFIG_FILE[] = CONFIG_ROOR_DIR"/wpa_supplicant/p2p_supplicant.conf"; 86 inline constexpr char WIFI_VARIABLE_PATH[] = "/system/etc/wifi/wifi_variable.cfg"; 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 int RemoveDevice(int networkId); 117 118 void ClearDeviceConfig(void); 119 120 int GetDeviceConfig(std::vector<WifiDeviceConfig> &results, int instId = 0); 121 122 int GetDeviceConfig(const int &networkId, WifiDeviceConfig &config, int instId = 0); 123 124 int GetDeviceConfig(const std::string &index, const int &indexType, WifiDeviceConfig &config, int instId = 0); 125 126 int GetDeviceConfig(const std::string &ssid, const std::string &keymgmt, WifiDeviceConfig &config, int instId = 0); 127 128 int SetDeviceState(int networkId, int state, bool bSetOther = false); 129 130 int SetDeviceEphemeral(int networkId, bool isEphemeral); 131 132 int SetDeviceAfterConnect(int networkId); 133 134 int SetDeviceRandomizedMacSuccessEver(int networkId); 135 136 int SetDeviceEverConnected(int networkId); 137 138 int SetAcceptUnvalidated(int networkId, bool state); 139 140 bool GetDeviceEverConnected(int networkId); 141 142 bool GetAcceptUnvalidated(int networkId); 143 144 int GetCandidateConfig(const int uid, const std::string &ssid, const std::string &keymgmt, 145 WifiDeviceConfig &config); 146 147 int GetCandidateConfig(const int uid, const int &networkId, WifiDeviceConfig &config); 148 149 int GetAllCandidateConfig(const int uid, std::vector<WifiDeviceConfig> &configs); 150 151 int IncreaseDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 152 153 int SetDeviceConnFailedCount(const std::string &index, const int &indexType, int count); 154 155 int SyncDeviceConfig(); 156 157 int ReloadDeviceConfig(); 158 159 int GetNextNetworkId(); 160 161 int AddWpsDeviceConfig(const WifiDeviceConfig &config); 162 163 #ifndef OHOS_ARCH_LITE 164 int OnRestore(UniqueFd &fd, const std::string &restoreInfo); 165 166 int OnBackup(UniqueFd &fd, const std::string &backupInfo); 167 168 std::string SetBackupReplyCode(int replyCode); 169 170 void RemoveBackupFile(); 171 172 int SetWifiToggleCaller(int callerPid, int instId = 0); 173 #endif 174 175 bool AddRandomMac(WifiStoreRandomMac &randomMacInfo); 176 177 bool GetRandomMac(WifiStoreRandomMac &randomMacInfo); 178 179 const std::vector<TrustListPolicy> ReloadTrustListPolicies(); 180 181 const MovingFreezePolicy ReloadMovingFreezePolicy(); 182 183 int GetPackageFilterMap(std::map<std::string, std::vector<std::string>> &filterMap); 184 185 int GetVariableMap(std::map<std::string, std::string> &variableMap); 186 187 std::string GetVariablePackageName(std::string tag); 188 189 int SyncHotspotConfig(); 190 191 int SetHotspotConfig(const HotspotConfig &config, int id = 0); 192 193 int GetHotspotConfig(HotspotConfig &config, int id = 0); 194 195 void ClearHotspotConfig(); 196 197 int GetBlockList(std::vector<StationInfo> &results, int id = 0); 198 199 int ManageBlockList(const StationInfo &info, int mode, int id = 0); 200 201 int SyncWifiP2pGroupInfoConfig(); 202 203 int SetWifiP2pGroupInfo(const std::vector<WifiP2pGroupInfo> &groups); 204 205 int RemoveWifiP2pGroupInfo(); 206 207 int RemoveWifiP2pSupplicantGroupInfo(); 208 209 int GetWifiP2pGroupInfo(std::vector<WifiP2pGroupInfo> &groups); 210 211 int SyncP2pVendorConfig(); 212 213 int SetP2pDeviceName(const std::string &deviceName); 214 215 int SetP2pVendorConfig(const P2pVendorConfig &config); 216 217 int GetP2pVendorConfig(P2pVendorConfig &config); 218 219 bool GetScanAlwaysState(int instId = 0); 220 221 int GetSignalLevel(const int &rssi, const int &band, int instId = 0); 222 223 int GetOperatorWifiType(int instId = 0); 224 225 int SetOperatorWifiType(int type, int instId = 0); 226 227 int GetLastAirplaneMode(int instId = 0); 228 229 int SetLastAirplaneMode(int mode, int instId = 0); 230 231 bool GetCanOpenStaWhenAirplaneMode(int instId = 0); 232 233 int SetWifiFlagOnAirplaneMode(bool ifOpen, int instId = 0); 234 235 bool GetWifiFlagOnAirplaneMode(int instId = 0); 236 237 bool GetWifiDisabledByAirplane(int instId = 0); 238 239 int SetWifiDisabledByAirplane(bool disabledByAirplane, int instId = 0); 240 241 int GetStaLastRunState(int instId = 0); 242 243 int SetStaLastRunState(int bRun, int instId = 0); 244 245 int GetDhcpIpType(int instId = 0); 246 247 bool GetWhetherToAllowNetworkSwitchover(int instId = 0); 248 249 int GetScoretacticsScoreSlope(int instId = 0); 250 251 int GetScoretacticsInitScore(int instId = 0); 252 253 int GetScoretacticsSameBssidScore(int instId = 0); 254 255 int GetScoretacticsSameNetworkScore(int instId = 0); 256 257 int GetScoretacticsFrequency5GHzScore(int instId = 0); 258 259 int GetScoretacticsLastSelectionScore(int instId = 0); 260 261 int GetScoretacticsSecurityScore(int instId = 0); 262 263 int GetScoretacticsNormalScore(int instId = 0); 264 265 int GetSavedDeviceAppraisalPriority(int instId = 0); 266 267 bool IsModulePreLoad(const std::string &name); 268 269 bool GetSupportHwPnoFlag(int instId = 0); 270 271 int GetMinRssi2Dot4Ghz(int instId = 0); 272 273 int GetMinRssi5Ghz(int instId = 0); 274 275 int SetRealMacAddress(const std::string &macAddress, int instId = 0); 276 277 int GetRealMacAddress(std::string &macAddress, int instId = 0); 278 279 void SetDefaultFrequenciesByCountryBand(const BandType band, std::vector<int> &frequencies, int instId = 0); 280 281 void SetScanOnlySwitchState(const int &state, int instId = 0); 282 283 int GetScanOnlySwitchState(int instId = 0); 284 285 bool EncryptionDeviceConfig(WifiDeviceConfig &config) const; 286 287 bool GetConfigValueByName(const std::string &name, std::string &value); 288 289 #ifdef SUPPORT_ClOUD_WIFI_ASSET 290 void UpdateWifiConfigFromCloud(const std::vector<WifiDeviceConfig> &newWifiDeviceConfigs, 291 const std::set<int> &wifiLinkedNetworkIds); 292 293 void UpLoadLocalDeviceConfigToCloud(); 294 #endif 295 296 private: 297 WifiSettings(); 298 int IncreaseNumRebootsSinceLastUse(); 299 void EncryptionWifiDeviceConfigOnBoot(); 300 int ReloadStaRandomMac(); 301 void InitPackageFilterConfig(); 302 void InitVariableConfig(); 303 void InitDefaultHotspotConfig(); 304 void InitHotspotConfig(); 305 int SyncBlockList(); 306 int ReloadWifiP2pGroupInfoConfig(); 307 void InitDefaultP2pVendorConfig(); 308 void InitP2pVendorConfig(); 309 int GetApMaxConnNum(); 310 void InitDefaultWifiConfig(); 311 void InitWifiConfig(); 312 int SyncWifiConfig(); 313 int RemoveExcessDeviceConfigs(std::vector<WifiDeviceConfig> &configs) const; 314 std::string FuzzyBssid(const std::string bssid); 315 #ifndef OHOS_ARCH_LITE 316 void MergeWifiConfig(); 317 void MergeSoftapConfig(); 318 void ConfigsDeduplicateAndSave(std::vector<WifiDeviceConfig> &newConfigs); 319 void ParseBackupJson(const std::string &backupInfo, std::string &key, std::string &iv, std::string &version); 320 int GetConfigbyBackupXml(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd); 321 int GetConfigbyBackupFile(std::vector<WifiDeviceConfig> &deviceConfigs, UniqueFd &fd, const std::string &key, 322 const std::string &iv); 323 #endif 324 #ifdef FEATURE_ENCRYPTION_SUPPORT 325 bool IsWifiDeviceConfigDeciphered(const WifiDeviceConfig &config) const; 326 void DecryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 327 int DecryptionDeviceConfig(WifiDeviceConfig &config); 328 bool EncryptionWapiConfig(const WifiEncryptionInfo &wifiEncryptionInfo, WifiDeviceConfig &config) const; 329 #endif 330 void SyncAfterDecryped(WifiDeviceConfig &config); 331 private: 332 // STA 333 std::mutex mStaMutex; 334 std::mutex mConfigOnBootMutex; 335 int mNetworkId; 336 std::atomic_flag deviceConfigLoadFlag = ATOMIC_FLAG_INIT; 337 std::atomic_flag mEncryptionOnBootFalg = ATOMIC_FLAG_INIT; 338 std::map<int, WifiDeviceConfig> mWifiDeviceConfig; 339 WifiConfigFileImpl<WifiDeviceConfig> mSavedDeviceConfig; 340 std::vector<WifiStoreRandomMac> mWifiStoreRandomMac; 341 WifiConfigFileImpl<WifiStoreRandomMac> mSavedWifiStoreRandomMac; 342 std::unique_ptr<WifiEventHandler> mWifiEncryptionThread = nullptr; 343 344 // SCAN 345 std::mutex mScanMutex; 346 WifiConfigFileImpl<TrustListPolicy> mTrustListPolicies; 347 WifiConfigFileImpl<MovingFreezePolicy> mMovingFreezePolicy; 348 std::map<std::string, std::vector<std::string>> mFilterMap; 349 WifiConfigFileImpl<PackageFilterConf> mPackageFilterConfig; 350 351 // AP 352 std::mutex mApMutex; 353 std::map<int, HotspotConfig> mHotspotConfig; 354 WifiConfigFileImpl<HotspotConfig> mSavedHotspotConfig; 355 std::map<std::string, StationInfo> mBlockListInfo; 356 WifiConfigFileImpl<StationInfo> mSavedBlockInfo; 357 358 // P2P 359 std::mutex mP2pMutex; 360 std::vector<WifiP2pGroupInfo> mGroupInfoList; 361 WifiConfigFileImpl<WifiP2pGroupInfo> mSavedWifiP2pGroupInfo; 362 P2pVendorConfig mP2pVendorConfig; 363 WifiConfigFileImpl<P2pVendorConfig> mSavedWifiP2pVendorConfig; 364 365 // COMMON 366 std::mutex mWifiConfigMutex; 367 std::atomic<int> mApMaxConnNum; 368 std::atomic<int> mMaxNumConfigs; 369 std::map<int, WifiConfig> mWifiConfig; 370 WifiConfigFileImpl<WifiConfig> mSavedWifiConfig; 371 372 std::mutex mVariableConfMutex; 373 WifiConfigFileImpl<VariableConf> mVariableConf; 374 std::map<std::string, std::string> mVariableMap; 375 }; 376 } // namespace Wifi 377 } // namespace OHOS 378 #endif 379