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