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