• 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 #include <random>
17 #include "wifi_config_center.h"
18 #include "wifi_logger.h"
19 #include "wifi_common_util.h"
20 #include "wifi_global_func.h"
21 #include "wifi_randommac_helper.h"
22 
23 DEFINE_WIFILOG_LABEL("WifiConfigCenter");
24 
25 namespace OHOS {
26 namespace Wifi {
27 #ifdef DTFUZZ_TEST
28 static WifiConfigCenter* gWifiConfigCenter = nullptr;
29 #endif
GetInstance()30 WifiConfigCenter &WifiConfigCenter::GetInstance()
31 {
32 #ifndef DTFUZZ_TEST
33     static WifiConfigCenter gWifiConfigCenter;
34     return gWifiConfigCenter;
35 #else
36     if (gWifiConfigCenter == nullptr) {
37         gWifiConfigCenter = new (std::nothrow) WifiConfigCenter();
38     }
39     return *gWifiConfigCenter;
40 #endif
41 }
42 
WifiConfigCenter()43 WifiConfigCenter::WifiConfigCenter()
44 {
45     mWifiState.emplace(0, static_cast<int>(WifiState::DISABLED));
46     mWifiDetailState.emplace(0, WifiDetailState::STATE_INACTIVE);
47     mStaMidState.emplace(0, WifiOprMidState::CLOSED);
48     mWifiCloseTime.emplace(0, std::chrono::steady_clock::now());
49     mIsAncoConnected.emplace(0, false);
50     mWifiIpInfo.emplace(0, IpInfo());
51     mWifiIpV6Info.emplace(0, IpV6Info());
52     mWifiLinkedInfo.emplace(0, WifiLinkedInfo());
53     mWifiMloLinkedInfo.emplace(0, std::vector<WifiLinkedInfo>());
54     mLastSelectedNetworkId.emplace(0, INVALID_NETWORK_ID);
55     mLastSelectedTimeVal.emplace(0, time(NULL));
56     mBssidToTimeoutTime.emplace(0, std::make_pair("", 0));
57     mLastDiscReason.emplace(0, DisconnectedReason::DISC_REASON_DEFAULT);
58     mScanMidState.emplace(0, WifiOprMidState::CLOSED);
59     mScanOnlyMidState.emplace(0, WifiOprMidState::CLOSED);
60     mApMidState.emplace(0, WifiOprMidState::CLOSED);
61     mHotspotState.emplace(0, static_cast<int>(ApState::AP_STATE_CLOSED));
62     powerModel.emplace(0, PowerModel::GENERAL);
63 }
64 
~WifiConfigCenter()65 WifiConfigCenter::~WifiConfigCenter()
66 {}
67 
Init()68 int WifiConfigCenter::Init()
69 {
70     if (WifiSettings::GetInstance().Init() < 0) {
71         WIFI_LOGE("Init wifi settings failed!");
72         return -1;
73     }
74     wifiScanConfig = std::make_unique<WifiScanConfig>();
75     ClearLocalHid2dInfo();
76     if (systemMode_ == SystemMode::M_FACTORY_MODE) {
77         mPersistWifiState[INSTID_WLAN0] = WIFI_STATE_DISABLED;
78     } else {
79         mPersistWifiState[INSTID_WLAN0] = WifiSettings::GetInstance().GetOperatorWifiType(INSTID_WLAN0);
80     }
81     mAirplaneModeState = WifiSettings::GetInstance().GetLastAirplaneMode();
82     return 0;
83 }
84 
GetWifiScanConfig()85 std::unique_ptr<WifiScanConfig>& WifiConfigCenter::GetWifiScanConfig()
86 {
87     if (wifiScanConfig == nullptr) {
88         wifiScanConfig = std::make_unique<WifiScanConfig>();
89     }
90     return wifiScanConfig;
91 }
92 
SetWifiSelfcureReset(const bool isReset)93 void WifiConfigCenter::SetWifiSelfcureReset(const bool isReset)
94 {
95     mWifiSelfcureReset = isReset;
96 }
97 
GetWifiSelfcureReset() const98 bool WifiConfigCenter::GetWifiSelfcureReset() const
99 {
100     return mWifiSelfcureReset.load();
101 }
102 
SetWifiSelfcureResetEntered(const bool isReset)103 void WifiConfigCenter::SetWifiSelfcureResetEntered(const bool isReset)
104 {
105     mWifiSelfcureResetEntered = isReset;
106 }
107 
GetWifiSelfcureResetEntered() const108 bool WifiConfigCenter::GetWifiSelfcureResetEntered() const
109 {
110     return mWifiSelfcureResetEntered.load();
111 }
112 
SetLastNetworkId(const int networkId)113 void WifiConfigCenter::SetLastNetworkId(const int networkId)
114 {
115     mLastNetworkId = networkId;
116 }
117 
GetLastNetworkId() const118 int WifiConfigCenter::GetLastNetworkId() const
119 {
120     return mLastNetworkId.load();
121 }
122 
SetSelectedCandidateNetworkId(const int networkId)123 void WifiConfigCenter::SetSelectedCandidateNetworkId(const int networkId)
124 {
125     mSelectedCandidateNetworkId = networkId;
126 }
127 
GetSelectedCandidateNetworkId() const128 int WifiConfigCenter::GetSelectedCandidateNetworkId() const
129 {
130     return mSelectedCandidateNetworkId.load();
131 }
132 
SetWifiAllowSemiActive(bool isAllowed)133 void WifiConfigCenter::SetWifiAllowSemiActive(bool isAllowed)
134 {
135     mWifiAllowSemiActive = isAllowed;
136 }
137 
GetWifiAllowSemiActive() const138 bool WifiConfigCenter::GetWifiAllowSemiActive() const
139 {
140     if (WifiConfigCenter::GetInstance().GetSystemMode() == SystemMode::M_FACTORY_MODE) {
141         WIFI_LOGI("factory mode, not allow semi active.");
142         return false;
143     }
144     return mWifiAllowSemiActive.load();
145 }
146 
SetWifiStopState(bool state)147 void WifiConfigCenter::SetWifiStopState(bool state)
148 {
149     mWifiStoping = state;
150 }
151 
GetWifiStopState() const152 bool WifiConfigCenter::GetWifiStopState() const
153 {
154     return mWifiStoping.load();
155 }
156 
SetStaIfaceName(const std::string & ifaceName,int instId)157 void WifiConfigCenter::SetStaIfaceName(const std::string &ifaceName, int instId)
158 {
159     std::unique_lock<std::mutex> lock(mStaMutex);
160     mStaIfaceName[instId] = ifaceName;
161 }
162 
GetStaIfaceName(int instId)163 std::string WifiConfigCenter::GetStaIfaceName(int instId)
164 {
165     std::unique_lock<std::mutex> lock(mStaMutex);
166     WIFI_LOGD("GetStaIfaceName instId:%{public}d mStaIfaceName[instId]:%{public}s ",
167         instId, mStaIfaceName[instId].c_str());
168     return mStaIfaceName[instId];
169 }
170 
GetWifiState(int instId)171 int WifiConfigCenter::GetWifiState(int instId)
172 {
173     std::unique_lock<std::mutex> lock(mStaMutex);
174     auto iter = mWifiState.find(instId);
175     if (iter != mWifiState.end()) {
176         return iter->second.load();
177     }
178     mWifiState[instId] = static_cast<int>(WifiState::DISABLED);
179     return mWifiState[instId].load();
180 }
181 
SetWifiState(int state,int instId)182 int WifiConfigCenter::SetWifiState(int state, int instId)
183 {
184     std::unique_lock<std::mutex> lock(mStaMutex);
185     mWifiState[instId] = state;
186     return 0;
187 }
188 
GetWifiDetailState(int instId)189 WifiDetailState WifiConfigCenter::GetWifiDetailState(int instId)
190 {
191     std::unique_lock<std::mutex> lock(mStaMutex);
192     auto iter = mWifiDetailState.find(instId);
193     if (iter != mWifiDetailState.end()) {
194         return iter->second;
195     }
196     mWifiDetailState[instId] = WifiDetailState::STATE_UNKNOWN;
197     return mWifiDetailState[instId];
198 }
199 
SetWifiDetailState(WifiDetailState state,int instId)200 int WifiConfigCenter::SetWifiDetailState(WifiDetailState state, int instId)
201 {
202     std::unique_lock<std::mutex> lock(mStaMutex);
203     mWifiDetailState[instId] = state;
204     return 0;
205 }
206 
GetWifiMidState(int instId)207 WifiOprMidState WifiConfigCenter::GetWifiMidState(int instId)
208 {
209     std::unique_lock<std::mutex> lock(mStaMutex);
210     auto iter = mStaMidState.find(instId);
211     if (iter != mStaMidState.end()) {
212         return iter->second.load();
213     } else {
214         mStaMidState.emplace(instId, WifiOprMidState::CLOSED);
215         return mStaMidState[instId].load();
216     }
217 }
218 
SetWifiMidState(WifiOprMidState expState,WifiOprMidState state,int instId)219 bool WifiConfigCenter::SetWifiMidState(WifiOprMidState expState, WifiOprMidState state, int instId)
220 {
221     WIFI_LOGI("SetWifiMidState expState:%{public}d,state:%{public}d,instId:%{public}d",
222         (int)expState, (int)state, instId);
223     std::unique_lock<std::mutex> lock(mStaMutex);
224     auto iter = mStaMidState.find(instId);
225     if (iter != mStaMidState.end()) {
226         return iter->second.compare_exchange_strong(expState, state);
227     } else {
228         mStaMidState.emplace(instId, state);
229         return true;
230     }
231     return false;
232 }
233 
SetWifiMidState(WifiOprMidState state,int instId)234 void WifiConfigCenter::SetWifiMidState(WifiOprMidState state, int instId)
235 {
236     WIFI_LOGI("SetWifiMidState ,state:%{public}d,instId:%{public}d", (int)state, instId);
237     std::unique_lock<std::mutex> lock(mStaMutex);
238     auto ret = mStaMidState.emplace(instId, state);
239     if (!ret.second) {
240         mStaMidState[instId] = state;
241     }
242 }
243 
SetWifiStaCloseTime(int instId)244 void WifiConfigCenter::SetWifiStaCloseTime(int instId)
245 {
246     std::unique_lock<std::mutex> lock(mStaMutex);
247     mWifiCloseTime[instId] = std::chrono::steady_clock::now();
248 }
249 
GetWifiStaInterval(int instId)250 double WifiConfigCenter::GetWifiStaInterval(int instId)
251 {
252     std::unique_lock<std::mutex> lock(mStaMutex);
253     auto iter = mWifiCloseTime.find(instId);
254     if (iter != mWifiCloseTime.end()) {
255         std::chrono::steady_clock::time_point curr = std::chrono::steady_clock::now();
256         double drMs = std::chrono::duration<double, std::milli>(curr - iter->second).count();
257         return drMs;
258     }
259 
260     return 0;
261 }
262 
GetWifiConnectedMode(int instId)263 bool WifiConfigCenter::GetWifiConnectedMode(int instId)
264 {
265     std::unique_lock<std::mutex> lock(mStaMutex);
266     return mIsAncoConnected[instId].load();
267 }
268 
SetWifiConnectedMode(bool isAncoConnected,int instId)269 void WifiConfigCenter::SetWifiConnectedMode(bool isAncoConnected, int instId)
270 {
271     std::unique_lock<std::mutex> lock(mStaMutex);
272     mIsAncoConnected[instId] = isAncoConnected;
273 }
274 
275 
SetChangeDeviceConfig(ConfigChange value,const WifiDeviceConfig & config)276 int WifiConfigCenter::SetChangeDeviceConfig(ConfigChange value, const WifiDeviceConfig &config)
277 {
278     std::unique_lock<std::mutex> lock(mStaMutex);
279     mLastRemoveDeviceConfig = std::make_pair((int)value, config);
280     return WIFI_OPT_SUCCESS;
281 }
282 
GetChangeDeviceConfig(ConfigChange & value,WifiDeviceConfig & config)283 bool WifiConfigCenter::GetChangeDeviceConfig(ConfigChange& value, WifiDeviceConfig &config)
284 {
285     std::unique_lock<std::mutex> lock(mStaMutex);
286     value = (ConfigChange)mLastRemoveDeviceConfig.first;
287     config = mLastRemoveDeviceConfig.second;
288     return true;
289 }
290 
GetIpInfo(IpInfo & info,int instId)291 int WifiConfigCenter::GetIpInfo(IpInfo &info, int instId)
292 {
293     std::unique_lock<std::mutex> lock(mStaMutex);
294     auto iter = mWifiIpInfo.find(instId);
295     if (iter != mWifiIpInfo.end()) {
296         info = iter->second;
297     }
298     return 0;
299 }
300 
SaveIpInfo(const IpInfo & info,int instId)301 int WifiConfigCenter::SaveIpInfo(const IpInfo &info, int instId)
302 {
303     std::unique_lock<std::mutex> lock(mStaMutex);
304     mWifiIpInfo[instId] = info;
305     return 0;
306 }
307 
GetIpv6Info(IpV6Info & info,int instId)308 int WifiConfigCenter::GetIpv6Info(IpV6Info &info, int instId)
309 {
310     std::unique_lock<std::mutex> lock(mStaMutex);
311     auto iter = mWifiIpV6Info.find(instId);
312     if (iter != mWifiIpV6Info.end()) {
313         info = iter->second;
314     }
315     return 0;
316 }
317 
SaveIpV6Info(const IpV6Info & info,int instId)318 int WifiConfigCenter::SaveIpV6Info(const IpV6Info &info, int instId)
319 {
320     std::unique_lock<std::mutex> lock(mStaMutex);
321     mWifiIpV6Info[instId] = info;
322     return 0;
323 }
324 
GetAllWifiLinkedInfo()325 std::map<int, WifiLinkedInfo> WifiConfigCenter::GetAllWifiLinkedInfo()
326 {
327     std::unique_lock<std::mutex> lock(mStaMutex);
328     return mWifiLinkedInfo;
329 }
330 
GetLinkedInfo(WifiLinkedInfo & info,int instId)331 int WifiConfigCenter::GetLinkedInfo(WifiLinkedInfo &info, int instId)
332 {
333     std::unique_lock<std::mutex> lock(mStaMutex);
334     auto iter = mWifiLinkedInfo.find(instId);
335     if (iter != mWifiLinkedInfo.end()) {
336         info = iter->second;
337     }
338     return 0;
339 }
340 
SaveLinkedInfo(const WifiLinkedInfo & info,int instId)341 int WifiConfigCenter::SaveLinkedInfo(const WifiLinkedInfo &info, int instId)
342 {
343     std::unique_lock<std::mutex> lock(mStaMutex);
344     auto iter = mWifiLinkedInfo.find(instId);
345     if (iter != mWifiLinkedInfo.end()) {
346         WifiChannelWidth channelWidth = iter->second.channelWidth;
347         std::string bssid = iter->second.bssid;
348         iter->second = info;
349         if (bssid == info.bssid) {
350             iter->second.channelWidth = channelWidth;
351         }
352     } else {
353         mWifiLinkedInfo.emplace(instId, info);
354     }
355 
356     return 0;
357 }
358 
GetMloLinkedInfo(std::vector<WifiLinkedInfo> & mloInfo,int instId)359 int WifiConfigCenter::GetMloLinkedInfo(std::vector<WifiLinkedInfo> &mloInfo, int instId)
360 {
361     std::unique_lock<std::mutex> lock(mStaMutex);
362     auto iter = mWifiMloLinkedInfo.find(instId);
363     if (iter != mWifiMloLinkedInfo.end()) {
364         if (iter->second.size() != WIFI_MAX_MLO_LINK_NUM) {
365             return -1;
366         }
367         mloInfo = iter->second;
368     }
369     return 0;
370 }
371 
SaveMloLinkedInfo(const std::vector<WifiLinkedInfo> & mloInfo,int instId)372 int WifiConfigCenter::SaveMloLinkedInfo(const std::vector<WifiLinkedInfo> &mloInfo, int instId)
373 {
374     std::unique_lock<std::mutex> lock(mStaMutex);
375     mWifiMloLinkedInfo[instId] = mloInfo;
376     return 0;
377 }
SetMacAddress(const std::string & macAddress,int instId)378 int WifiConfigCenter::SetMacAddress(const std::string &macAddress, int instId)
379 {
380     std::unique_lock<std::mutex> lock(mStaMutex);
381     mMacAddress[instId] = macAddress;
382     return 0;
383 }
384 
GetMacAddress(std::string & macAddress,int instId)385 int WifiConfigCenter::GetMacAddress(std::string &macAddress, int instId)
386 {
387     std::unique_lock<std::mutex> lock(mStaMutex);
388     auto iter = mMacAddress.find(instId);
389     if (iter != mMacAddress.end()) {
390         macAddress = iter->second;
391     }
392     return 0;
393 }
394 
SetUserLastSelectedNetworkId(int networkId,int instId)395 void WifiConfigCenter::SetUserLastSelectedNetworkId(int networkId, int instId)
396 {
397     std::unique_lock<std::mutex> lock(mStaMutex);
398     mLastSelectedNetworkId[instId] = networkId;
399     mLastSelectedTimeVal[instId] = time(NULL);
400 }
401 
GetUserLastSelectedNetworkId(int instId)402 int WifiConfigCenter::GetUserLastSelectedNetworkId(int instId)
403 {
404     std::unique_lock<std::mutex> lock(mStaMutex);
405     auto iter = mLastSelectedNetworkId.find(instId);
406     if (iter != mLastSelectedNetworkId.end()) {
407         return iter->second;
408     }
409     return -1;
410 }
411 
GetUserLastSelectedNetworkTimeVal(int instId)412 time_t WifiConfigCenter::GetUserLastSelectedNetworkTimeVal(int instId)
413 {
414     std::unique_lock<std::mutex> lock(mStaMutex);
415     auto iter = mLastSelectedTimeVal.find(instId);
416     if (iter != mLastSelectedTimeVal.end()) {
417         return iter->second;
418     }
419     return 0;
420 }
421 
GetConnectTimeoutBssid(int instId)422 std::string WifiConfigCenter::GetConnectTimeoutBssid(int instId)
423 {
424     std::unique_lock<std::mutex> lock(mStaMutex);
425     auto iter = mBssidToTimeoutTime.find(instId);
426     if (iter != mBssidToTimeoutTime.end()) {
427         const int timeout = 30; // 30s
428         if (iter->second.second - static_cast<int>(time(NULL)) > timeout) {
429             return "";
430         }
431         return iter->second.first;
432     }
433     return "";
434 }
435 
SetConnectTimeoutBssid(std::string & bssid,int instId)436 int WifiConfigCenter::SetConnectTimeoutBssid(std::string &bssid, int instId)
437 {
438     std::unique_lock<std::mutex> lock(mStaMutex);
439     time_t now = time(nullptr);
440     if (now == static_cast<time_t>(-1)) {
441         LOGE("SetConnectTimeoutBssid: call time failed!");
442         return -1;
443     }
444     mBssidToTimeoutTime[instId] = std::make_pair(bssid, static_cast<int>(now));
445     return 0;
446 }
447 
SaveDisconnectedReason(DisconnectedReason discReason,int instId)448 void WifiConfigCenter::SaveDisconnectedReason(DisconnectedReason discReason, int instId)
449 {
450     std::unique_lock<std::mutex> lock(mStaMutex);
451     mLastDiscReason[instId] = discReason;
452 }
453 
GetDisconnectedReason(DisconnectedReason & discReason,int instId)454 int WifiConfigCenter::GetDisconnectedReason(DisconnectedReason &discReason, int instId)
455 {
456     std::unique_lock<std::mutex> lock(mStaMutex);
457     auto iter = mLastDiscReason.find(instId);
458     if (iter != mLastDiscReason.end()) {
459         discReason = iter->second;
460     }
461     return 0;
462 }
463 
InsertWifiCategoryBlackListCache(int blacklistType,const std::string currentBssid,const WifiCategoryBlackListInfo wifiBlackListInfo)464 void WifiConfigCenter::InsertWifiCategoryBlackListCache(int blacklistType, const std::string currentBssid,
465     const WifiCategoryBlackListInfo wifiBlackListInfo)
466 {
467     std::unique_lock<std::mutex> lock(mStaMutex);
468     std::map<std::string, WifiCategoryBlackListInfo> wifiBlackListCache;
469     if (mWifiCategoryBlackListCache.find(blacklistType) != mWifiCategoryBlackListCache.end()) {
470         wifiBlackListCache = mWifiCategoryBlackListCache[blacklistType];
471     }
472     auto iter = wifiBlackListCache.find(currentBssid);
473     if (iter != wifiBlackListCache.end()) {
474         iter->second = wifiBlackListInfo;
475     } else {
476         wifiBlackListCache.emplace(std::make_pair(currentBssid, wifiBlackListInfo));
477     }
478     mWifiCategoryBlackListCache[blacklistType] = wifiBlackListCache;
479 }
480 
RemoveWifiCategoryBlackListCache(int blacklistType,const std::string bssid)481 void WifiConfigCenter::RemoveWifiCategoryBlackListCache(int blacklistType, const std::string bssid)
482 {
483     std::unique_lock<std::mutex> lock(mStaMutex);
484     if (mWifiCategoryBlackListCache.find(blacklistType) == mWifiCategoryBlackListCache.end()) {
485         LOGE("%{public}s: dont exist wifi bla type", __func__);
486         return;
487     }
488     std::map<std::string, WifiCategoryBlackListInfo> wifiBlackListCache = mWifiCategoryBlackListCache[blacklistType];
489     if (wifiBlackListCache.find(bssid) != wifiBlackListCache.end()) {
490         wifiBlackListCache.erase(bssid);
491         mWifiCategoryBlackListCache[blacklistType] = wifiBlackListCache;
492     } else {
493         LOGE("%{public}s: don't exist wifi bla list, bssid: %{public}s", __func__, MacAnonymize(bssid).c_str());
494         return;
495     }
496 }
497 
GetWifiCategoryBlackListCache(int blacklistType,std::map<std::string,WifiCategoryBlackListInfo> & blackListCache)498 int WifiConfigCenter::GetWifiCategoryBlackListCache(int blacklistType,
499     std::map<std::string, WifiCategoryBlackListInfo> &blackListCache)
500 {
501     std::unique_lock<std::mutex> lock(mStaMutex);
502     if (mWifiCategoryBlackListCache.find(blacklistType) == mWifiCategoryBlackListCache.end()) {
503         LOGE("%{public}s: dont exist wifi bla type", __func__);
504         return -1;
505     }
506     blackListCache = mWifiCategoryBlackListCache[blacklistType];
507     return 0;
508 }
509 
UpdateWifiConnectFailListCache(int blacklistType,const std::string bssid,const WifiCategoryConnectFailInfo wifiConnectFailInfo)510 void WifiConfigCenter::UpdateWifiConnectFailListCache(int blacklistType, const std::string bssid,
511     const WifiCategoryConnectFailInfo wifiConnectFailInfo)
512 {
513     std::unique_lock<std::mutex> lock(mStaMutex);
514     auto iter = mWifiConnectFailCache.find(bssid);
515     if (iter != mWifiConnectFailCache.end()
516         && iter->second.actionType >= wifiConnectFailInfo.actionType) {
517         iter->second.connectFailTimes++;
518     } else {
519         mWifiConnectFailCache[bssid] = wifiConnectFailInfo;
520     }
521 }
522 
RemoveWifiConnectFailListCache(const std::string bssid)523 void WifiConfigCenter::RemoveWifiConnectFailListCache(const std::string bssid)
524 {
525     std::unique_lock<std::mutex> lock(mStaMutex);
526     if (mWifiConnectFailCache.find(bssid) != mWifiConnectFailCache.end()) {
527         mWifiConnectFailCache.erase(bssid);
528     } else {
529         LOGE("%{public}s: don't exist wifi connect fail list, bssid: %{public}s",
530             __func__, MacAnonymize(bssid).c_str());
531         return;
532     }
533 }
534 
GetWifiConnectFailListCache(std::map<std::string,WifiCategoryConnectFailInfo> & connectFailCache)535 int WifiConfigCenter::GetWifiConnectFailListCache(std::map<std::string,
536     WifiCategoryConnectFailInfo> &connectFailCache)
537 {
538     std::unique_lock<std::mutex> lock(mStaMutex);
539     connectFailCache = mWifiConnectFailCache;
540     return 0;
541 }
542 
EnableNetwork(int networkId,bool disableOthers,int instId)543 bool WifiConfigCenter::EnableNetwork(int networkId, bool disableOthers, int instId)
544 {
545     if (disableOthers) {
546         SetUserLastSelectedNetworkId(networkId, instId);
547     }
548     return true;
549 }
550 
GetScanMidState(int instId)551 WifiOprMidState WifiConfigCenter::GetScanMidState(int instId)
552 {
553     std::unique_lock<std::mutex> lock(mScanMutex);
554     auto iter = mScanMidState.find(instId);
555     if (iter != mScanMidState.end()) {
556         return iter->second.load();
557     } else {
558         mScanMidState.emplace(instId, WifiOprMidState::CLOSED);
559         return mScanMidState[instId].load();
560     }
561 }
562 
SetScanMidState(WifiOprMidState expState,WifiOprMidState state,int instId)563 bool WifiConfigCenter::SetScanMidState(WifiOprMidState expState, WifiOprMidState state, int instId)
564 {
565     std::unique_lock<std::mutex> lock(mScanMutex);
566     auto iter = mScanMidState.find(instId);
567     if (iter != mScanMidState.end()) {
568         return iter->second.compare_exchange_strong(expState, state);
569     } else {
570         mScanMidState.emplace(instId, state);
571         return true;
572     }
573     return false;
574 }
575 
SetScanMidState(WifiOprMidState state,int instId)576 void WifiConfigCenter::SetScanMidState(WifiOprMidState state, int instId)
577 {
578     std::unique_lock<std::mutex> lock(mScanMutex);
579     auto ret = mScanMidState.emplace(instId, state);
580     if (!ret.second) {
581         mScanMidState[instId] = state;
582     }
583 }
584 
GetWifiScanOnlyMidState(int instId)585 WifiOprMidState WifiConfigCenter::GetWifiScanOnlyMidState(int instId)
586 {
587     std::unique_lock<std::mutex> lock(mScanMutex);
588     auto iter = mScanOnlyMidState.find(instId);
589     if (iter != mScanOnlyMidState.end()) {
590         return iter->second.load();
591     } else {
592         mScanOnlyMidState.emplace(instId, WifiOprMidState::CLOSED);
593         return mScanOnlyMidState[instId].load();
594     }
595 }
596 
SetWifiScanOnlyMidState(WifiOprMidState expState,WifiOprMidState state,int instId)597 bool WifiConfigCenter::SetWifiScanOnlyMidState(WifiOprMidState expState, WifiOprMidState state, int instId)
598 {
599     std::unique_lock<std::mutex> lock(mScanMutex);
600     auto iter = mScanOnlyMidState.find(instId);
601     if (iter != mScanOnlyMidState.end()) {
602         return iter->second.compare_exchange_strong(expState, state);
603     } else {
604         mScanOnlyMidState.emplace(instId, state);
605         return true;
606     }
607     return false;
608 }
609 
SetWifiScanOnlyMidState(WifiOprMidState state,int instId)610 void WifiConfigCenter::SetWifiScanOnlyMidState(WifiOprMidState state, int instId)
611 {
612     std::unique_lock<std::mutex> lock(mScanMutex);
613     auto ret = mScanOnlyMidState.emplace(instId, state);
614     if (!ret.second) {
615         mScanOnlyMidState[instId] = state;
616     }
617 }
618 
SetWifiLinkedStandardAndMaxSpeed(WifiLinkedInfo & linkInfo)619 int WifiConfigCenter::SetWifiLinkedStandardAndMaxSpeed(WifiLinkedInfo &linkInfo)
620 {
621     std::vector<WifiScanInfo> wifiScanInfoList;
622     wifiScanConfig->GetScanInfoListInner(wifiScanInfoList);
623     for (auto iter = wifiScanInfoList.begin(); iter != wifiScanInfoList.end(); ++iter) {
624         if (iter->bssid == linkInfo.bssid) {
625             linkInfo.wifiStandard = iter->wifiStandard;
626             linkInfo.maxSupportedRxLinkSpeed = iter->maxSupportedRxLinkSpeed;
627             linkInfo.maxSupportedTxLinkSpeed = iter->maxSupportedTxLinkSpeed;
628             break;
629         }
630     }
631     return 0;
632 }
633 
SetMloWifiLinkedMaxSpeed(int instId)634 void WifiConfigCenter::SetMloWifiLinkedMaxSpeed(int instId)
635 {
636     std::unique_lock<std::mutex> lock(mStaMutex);
637     auto mloIter = mWifiMloLinkedInfo.find(instId);
638     if (mloIter == mWifiMloLinkedInfo.end()) {
639         return;
640     }
641     if (mloIter->second.size() != WIFI_MAX_MLO_LINK_NUM) {
642         return;
643     }
644     std::vector<WifiScanInfo> wifiScanInfoList;
645     wifiScanConfig->GetScanInfoListInner(wifiScanInfoList);
646     for (auto iter = wifiScanInfoList.begin(); iter != wifiScanInfoList.end(); ++iter) {
647         for (auto& mloInfoItem : mWifiMloLinkedInfo[instId]) {
648             if (iter->bssid == mloInfoItem.bssid) {
649                 mloInfoItem.maxSupportedRxLinkSpeed = iter->maxSupportedRxLinkSpeed;
650                 mloInfoItem.maxSupportedTxLinkSpeed = iter->maxSupportedTxLinkSpeed;
651             }
652         }
653     }
654 }
655 
CheckScanOnlyAvailable(int instId)656 bool WifiConfigCenter::CheckScanOnlyAvailable(int instId)
657 {
658     return (WifiSettings::GetInstance().GetScanOnlySwitchState(instId)) && (GetAirplaneModeState() == MODE_STATE_CLOSE);
659 }
660 
GetConnectedBssid(int instId)661 std::string WifiConfigCenter::GetConnectedBssid(int instId)
662 {
663     WifiLinkedInfo linkedInfo;
664     GetLinkedInfo(linkedInfo, instId);
665     if (linkedInfo.connState == ConnState::CONNECTED) {
666         return linkedInfo.bssid;
667     }
668     return "";
669 }
670 
SetSoftapToggledState(bool state)671 void WifiConfigCenter::SetSoftapToggledState(bool state)
672 {
673     mSoftapToggled = state;
674 }
675 
GetSoftapToggledState() const676 bool WifiConfigCenter::GetSoftapToggledState() const
677 {
678     return mSoftapToggled.load();
679 }
680 
681 
SetHotspotIdleTimeout(int time)682 int WifiConfigCenter::SetHotspotIdleTimeout(int time)
683 {
684     mHotspotIdleTimeout = time;
685     return 0;
686 }
687 
GetHotspotIdleTimeout() const688 int WifiConfigCenter::GetHotspotIdleTimeout() const
689 {
690     return mHotspotIdleTimeout.load();
691 }
692 
SetApIfaceName(const std::string & ifaceName)693 void WifiConfigCenter::SetApIfaceName(const std::string &ifaceName)
694 {
695     std::unique_lock<std::mutex> lock(mApMutex);
696     mApIfaceName = ifaceName;
697 }
698 
GetApIfaceName()699 std::string WifiConfigCenter::GetApIfaceName()
700 {
701     std::unique_lock<std::mutex> lock(mApMutex);
702     return mApIfaceName;
703 }
704 
GetApMidState(int id)705 WifiOprMidState WifiConfigCenter::GetApMidState(int id)
706 {
707     std::unique_lock<std::mutex> lock(mApMutex);
708     auto iter = mApMidState.find(id);
709     if (iter != mApMidState.end()) {
710         return iter->second.load();
711     } else {
712         mApMidState.emplace(id, WifiOprMidState::CLOSED);
713         return mApMidState[id].load();
714     }
715 }
716 
SetApMidState(WifiOprMidState expState,WifiOprMidState state,int id)717 bool WifiConfigCenter::SetApMidState(WifiOprMidState expState, WifiOprMidState state, int id)
718 {
719     std::unique_lock<std::mutex> lock(mApMutex);
720     auto iter = mApMidState.find(id);
721     if (iter != mApMidState.end()) {
722         return iter->second.compare_exchange_strong(expState, state);
723     } else {
724         mApMidState.emplace(id, state);
725         return true;
726     }
727     return false;
728 }
729 
SetApMidState(WifiOprMidState state,int id)730 void WifiConfigCenter::SetApMidState(WifiOprMidState state, int id)
731 {
732     std::unique_lock<std::mutex> lock(mApMutex);
733     auto ret = mApMidState.emplace(id, state);
734     if (!ret.second) {
735         mApMidState[id] = state;
736     }
737 }
738 
GetHotspotState(int id)739 int WifiConfigCenter::GetHotspotState(int id)
740 {
741     std::unique_lock<std::mutex> lock(mApMutex);
742     auto iter = mHotspotState.find(id);
743     if (iter != mHotspotState.end()) {
744         return iter->second.load();
745     }
746     mHotspotState[id] = static_cast<int>(ApState::AP_STATE_CLOSED);
747     return mHotspotState[id].load();
748 }
749 
SetHotspotState(int state,int id)750 int WifiConfigCenter::SetHotspotState(int state, int id)
751 {
752     std::unique_lock<std::mutex> lock(mApMutex);
753     mHotspotState[id] = state;
754     return 0;
755 }
756 
SetPowerModel(const PowerModel & model,int id)757 int WifiConfigCenter::SetPowerModel(const PowerModel& model, int id)
758 {
759     std::unique_lock<std::mutex> lock(mApMutex);
760     auto ret = powerModel.emplace(id, model);
761     if (!ret.second) {
762         powerModel[id] = model;
763     }
764     return 0;
765 }
766 
GetPowerModel(PowerModel & model,int id)767 int WifiConfigCenter::GetPowerModel(PowerModel& model, int id)
768 {
769     std::unique_lock<std::mutex> lock(mApMutex);
770     auto iter = powerModel.find(id);
771     if (iter != powerModel.end()) {
772         model = iter->second;
773     } else {
774         powerModel[id] = PowerModel::GENERAL;
775         model = powerModel[id];
776     }
777     return 0;
778 }
779 
GetStationList(std::vector<StationInfo> & results,int id)780 int WifiConfigCenter::GetStationList(std::vector<StationInfo> &results, int id)
781 {
782     std::unique_lock<std::mutex> lock(mApMutex);
783     for (auto iter = mConnectStationInfo.begin(); iter != mConnectStationInfo.end(); iter++) {
784         results.push_back(iter->second);
785     }
786     return 0;
787 }
788 
ManageStation(const StationInfo & info,int mode,int id)789 int WifiConfigCenter::ManageStation(const StationInfo &info, int mode, int id)
790 {
791     std::unique_lock<std::mutex> lock(mApMutex);
792     auto iter = mConnectStationInfo.find(info.bssid);
793     if (mode == MODE_ADD || mode == MODE_UPDATE) {
794         if (iter != mConnectStationInfo.end()) {
795             iter->second = info;
796         } else {
797             mConnectStationInfo.emplace(std::make_pair(info.bssid, info));
798         }
799 #ifdef SUPPORT_RANDOM_MAC_ADDR
800         StoreWifiMacAddrPairInfo(WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO, info.bssid, "");
801 #endif
802     } else if (mode == MODE_DEL) {
803         if (iter != mConnectStationInfo.end()) {
804             mConnectStationInfo.erase(iter);
805         }
806 #ifdef SUPPORT_RANDOM_MAC_ADDR
807         RemoveMacAddrPairInfo(WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO, info.bssid, info.bssidType);
808 #endif
809     } else {
810         return -1;
811     }
812     return 0;
813 }
814 
ClearStationList(int id)815 int WifiConfigCenter::ClearStationList(int id)
816 {
817 #ifdef SUPPORT_RANDOM_MAC_ADDR
818     ClearMacAddrPairs(WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO);
819 #endif
820     std::unique_lock<std::mutex> lock(mApMutex);
821     mConnectStationInfo.clear();
822     return 0;
823 }
824 
SetP2pIfaceName(const std::string & ifaceName)825 void WifiConfigCenter::SetP2pIfaceName(const std::string &ifaceName)
826 {
827     std::unique_lock<std::mutex> lock(mP2pMutex);
828     mP2pIfaceName = ifaceName;
829 }
830 
GetP2pIfaceName()831 std::string WifiConfigCenter::GetP2pIfaceName()
832 {
833     std::unique_lock<std::mutex> lock(mP2pMutex);
834     return mP2pIfaceName;
835 }
836 
SetHid2dUpperScene(int uid,const Hid2dUpperScene & scene)837 int WifiConfigCenter::SetHid2dUpperScene(int uid, const Hid2dUpperScene &scene)
838 {
839     LOGD("SetHid2dUpperScene uid: %{public}d", uid);
840     std::unique_lock<std::mutex> lock(mP2pMutex);
841     mHid2dUpperScenePair.insert_or_assign(uid, scene);
842     if (scene.setTime != 0) {
843         mHid2dSceneLastSetTime = scene.setTime;
844     }
845     return 0;
846 }
847 
GetHid2dUpperScene(int uid,Hid2dUpperScene & scene)848 int WifiConfigCenter::GetHid2dUpperScene(int uid, Hid2dUpperScene &scene)
849 {
850     std::unique_lock<std::mutex> lock(mP2pMutex);
851     auto iter = mHid2dUpperScenePair.find(uid);
852     if (iter != mHid2dUpperScenePair.end()) {
853         scene = iter->second;
854     }
855     return 0;
856 }
857 
SetHid2dSceneLastSetTime(int64_t setTime)858 int WifiConfigCenter::SetHid2dSceneLastSetTime(int64_t setTime)
859 {
860     mHid2dSceneLastSetTime = setTime;
861     return 0;
862 }
863 
GetHid2dSceneLastSetTime()864 int64_t WifiConfigCenter::GetHid2dSceneLastSetTime()
865 {
866     return mHid2dSceneLastSetTime.load();
867 }
868 
ClearLocalHid2dInfo(int uid)869 void WifiConfigCenter::ClearLocalHid2dInfo(int uid)
870 {
871     std::unique_lock<std::mutex> lock(mP2pMutex);
872     Hid2dUpperScene scene;
873     scene.mac = "";
874     scene.scene = 0;
875     scene.fps = 0;
876     scene.bw = 0;
877     if (uid != 0) {
878         mHid2dUpperScenePair.insert_or_assign(uid, scene);
879     } else {
880         mHid2dUpperScenePair.insert_or_assign(SOFT_BUS_SERVICE_UID, scene);
881         mHid2dUpperScenePair.insert_or_assign(CAST_ENGINE_SERVICE_UID, scene);
882         mHid2dUpperScenePair.insert_or_assign(MIRACAST_SERVICE_UID, scene);
883         mHid2dUpperScenePair.insert_or_assign(SHARE_SERVICE_UID, scene);
884         mHid2dUpperScenePair.insert_or_assign(MOUSE_CROSS_SERVICE_UID, scene);
885         SetHid2dSceneLastSetTime(0);
886     }
887 }
888 
SetLastConnStaFreq(int freq)889 int WifiConfigCenter::SetLastConnStaFreq(int freq)
890 {
891     lastConnStaFreq_.store(freq);
892     return 0;
893 }
894 
GetLastConnStaFreq()895 int WifiConfigCenter::GetLastConnStaFreq()
896 {
897     return lastConnStaFreq_.load();
898 }
899 
SetP2pEnhanceState(int state)900 int WifiConfigCenter::SetP2pEnhanceState(int state)
901 {
902     p2pEnhanceState_.store(state);
903     return 0;
904 }
905 
GetP2pEnhanceState()906 int WifiConfigCenter::GetP2pEnhanceState()
907 {
908     return p2pEnhanceState_.load();
909 }
910 
SetP2pEnhanceActionListenChannel(int channel)911 int WifiConfigCenter::SetP2pEnhanceActionListenChannel(int channel)
912 {
913     p2pEnhanceActionListenChannel_.store(channel);
914     return 0;
915 }
916 
GetP2pEnhanceActionListenChannel()917 int WifiConfigCenter::GetP2pEnhanceActionListenChannel()
918 {
919     return p2pEnhanceActionListenChannel_.load();
920 }
921 
SetP2pEnhanceFreq(int freq)922 int WifiConfigCenter::SetP2pEnhanceFreq(int freq)
923 {
924     p2pEnhanceFreq_.store(freq);
925     return 0;
926 }
927 
GetP2pEnhanceFreq()928 int WifiConfigCenter::GetP2pEnhanceFreq()
929 {
930     return p2pEnhanceFreq_.load();
931 }
932 
GetP2pMidState()933 WifiOprMidState WifiConfigCenter::GetP2pMidState()
934 {
935     return mP2pMidState.load();
936 }
937 
SetP2pMidState(WifiOprMidState expState,WifiOprMidState state)938 bool WifiConfigCenter::SetP2pMidState(WifiOprMidState expState, WifiOprMidState state)
939 {
940     return mP2pMidState.compare_exchange_strong(expState, state);
941 }
942 
SetP2pMidState(WifiOprMidState state)943 void WifiConfigCenter::SetP2pMidState(WifiOprMidState state)
944 {
945     mP2pMidState = state;
946 }
947 
SetP2pState(int state)948 int WifiConfigCenter::SetP2pState(int state)
949 {
950     mP2pState = state;
951     return 0;
952 }
953 
GetP2pState()954 int WifiConfigCenter::GetP2pState()
955 {
956     return mP2pState.load();
957 }
958 
SetP2pDiscoverState(int state)959 int WifiConfigCenter::SetP2pDiscoverState(int state)
960 {
961     mP2pDiscoverState = state;
962     return 0;
963 }
964 
GetP2pDiscoverState()965 int WifiConfigCenter::GetP2pDiscoverState()
966 {
967     return mP2pDiscoverState.load();
968 }
969 
SetP2pBusinessType(const P2pBusinessType & type)970 int WifiConfigCenter::SetP2pBusinessType(const P2pBusinessType &type)
971 {
972     mP2pBusinessType = type;
973     return 0;
974 }
975 
GetP2pBusinessType(P2pBusinessType & type)976 int WifiConfigCenter::GetP2pBusinessType(P2pBusinessType &type)
977 {
978     type = mP2pBusinessType.load();
979     return 0;
980 }
981 
SaveP2pCreatorUid(int uid)982 int WifiConfigCenter::SaveP2pCreatorUid(int uid)
983 {
984     mP2pCreatorUid = uid;
985     return 0;
986 }
987 
GetP2pCreatorUid()988 int WifiConfigCenter::GetP2pCreatorUid()
989 {
990     return mP2pCreatorUid.load();
991 }
992 
SetExplicitGroup(bool isExplicit)993 void WifiConfigCenter::SetExplicitGroup(bool isExplicit)
994 {
995     mExplicitGroup = isExplicit;
996 }
997 
IsExplicitGroup(void)998 bool WifiConfigCenter::IsExplicitGroup(void)
999 {
1000     return mExplicitGroup.load();
1001 }
1002 
GetP2pInfo(WifiP2pLinkedInfo & linkedInfo)1003 int WifiConfigCenter::GetP2pInfo(WifiP2pLinkedInfo &linkedInfo)
1004 {
1005     std::unique_lock<std::mutex> lock(mP2pMutex);
1006     linkedInfo = mWifiP2pInfo;
1007     return 0;
1008 }
1009 
SaveP2pInfo(WifiP2pLinkedInfo & linkedInfo)1010 int WifiConfigCenter::SaveP2pInfo(WifiP2pLinkedInfo &linkedInfo)
1011 {
1012     std::unique_lock<std::mutex> lock(mP2pMutex);
1013     mWifiP2pInfo = linkedInfo;
1014     return 0;
1015 }
1016 
SetCurrentP2pGroupInfo(const WifiP2pGroupInfo & group)1017 void WifiConfigCenter::SetCurrentP2pGroupInfo(const WifiP2pGroupInfo &group)
1018 {
1019     std::unique_lock<std::mutex> lock(mP2pMutex);
1020     m_P2pGroupInfo = group;
1021 }
1022 
GetCurrentP2pGroupInfo()1023 WifiP2pGroupInfo WifiConfigCenter::GetCurrentP2pGroupInfo()
1024 {
1025     std::unique_lock<std::mutex> lock(mP2pMutex);
1026     return m_P2pGroupInfo;
1027 }
1028 
SetCoexSupport(bool isSupport)1029 void WifiConfigCenter::SetCoexSupport(bool isSupport)
1030 {
1031     mIsSupportCoex = isSupport;
1032 }
1033 
GetCoexSupport() const1034 bool WifiConfigCenter::GetCoexSupport() const
1035 {
1036     return mIsSupportCoex.load();
1037 }
1038 
SetScreenState(const int & state)1039 void WifiConfigCenter::SetScreenState(const int &state)
1040 {
1041     mScreenState = state;
1042 }
1043 
GetScreenState() const1044 int WifiConfigCenter::GetScreenState() const
1045 {
1046     return mScreenState.load();
1047 }
1048 
SetThermalLevel(const int & level)1049 void WifiConfigCenter::SetThermalLevel(const int &level)
1050 {
1051     mThermalLevel = level;
1052 }
1053 
GetThermalLevel() const1054 int WifiConfigCenter::GetThermalLevel() const
1055 {
1056     return mThermalLevel.load();
1057 }
1058 
SetPowerIdelState(const int & state)1059 void WifiConfigCenter::SetPowerIdelState(const int &state)
1060 {
1061     mPowerIdelState = state;
1062 }
1063 
GetPowerIdelState() const1064 int WifiConfigCenter::GetPowerIdelState() const
1065 {
1066     return mPowerIdelState.load();
1067 }
1068 
SetGnssFixState(const int & state)1069 void WifiConfigCenter::SetGnssFixState(const int &state)
1070 {
1071     mGnssFixState = state;
1072 }
1073 
GetGnssFixState() const1074 int WifiConfigCenter::GetGnssFixState() const
1075 {
1076     return mGnssFixState.load();
1077 }
1078 
SetScanGenieState(const int & state)1079 void WifiConfigCenter::SetScanGenieState(const int &state)
1080 {
1081     mScanGenieState = state;
1082 }
1083 
GetScanGenieState() const1084 int WifiConfigCenter::GetScanGenieState() const
1085 {
1086     return mScanGenieState.load();
1087 }
1088 
SetWifiStateOnAirplaneChanged(const int & state)1089 bool WifiConfigCenter::SetWifiStateOnAirplaneChanged(const int &state)
1090 {
1091     mAirplaneModeState = state;
1092     WifiSettings::GetInstance().SetLastAirplaneMode(state);
1093     if (WifiSettings::GetInstance().GetWifiFlagOnAirplaneMode()) {
1094         if (GetPersistWifiState(INSTID_WLAN0) == WIFI_STATE_DISABLED) {
1095             return true;
1096         }
1097         if (GetPersistWifiState(INSTID_WLAN0) == WIFI_STATE_SEMI_ENABLED && state == MODE_STATE_OPEN) {
1098             SetPersistWifiState(WIFI_STATE_DISABLED, INSTID_WLAN0);
1099             return true;
1100         }
1101         return false;
1102     }
1103     if (state == MODE_STATE_OPEN) {
1104         if (GetPersistWifiState(INSTID_WLAN0) == WIFI_STATE_ENABLED) {
1105             WifiSettings::GetInstance().SetWifiDisabledByAirplane(true);
1106         }
1107         SetPersistWifiState(WIFI_STATE_DISABLED, INSTID_WLAN0);
1108     } else {
1109         if (WifiSettings::GetInstance().GetWifiDisabledByAirplane()) {
1110             SetPersistWifiState(WIFI_STATE_ENABLED, INSTID_WLAN0);
1111             WifiSettings::GetInstance().SetWifiDisabledByAirplane(false);
1112         }
1113     }
1114     return true;
1115 }
1116 
GetAirplaneModeState() const1117 int WifiConfigCenter::GetAirplaneModeState() const
1118 {
1119     return mAirplaneModeState.load();
1120 }
1121 
GetWifiToggledEnable(int id)1122 int WifiConfigCenter::GetWifiToggledEnable(int id)
1123 {
1124     if (GetAirplaneModeState() == MODE_STATE_OPEN) {
1125         if (GetPersistWifiState(id) == WIFI_STATE_ENABLED) {
1126             return WIFI_STATE_ENABLED;
1127         }
1128         return WIFI_STATE_DISABLED;
1129     }
1130     if (GetPersistWifiState(id) != WIFI_STATE_ENABLED && GetWifiAllowSemiActive()) {
1131         return WIFI_STATE_SEMI_ENABLED;
1132     }
1133     return GetPersistWifiState(id);
1134 }
1135 
SetWifiToggledState(int state,int id)1136 void WifiConfigCenter::SetWifiToggledState(int state, int id)
1137 {
1138     if (GetAirplaneModeState() == MODE_STATE_OPEN) {
1139         WifiSettings::GetInstance().SetWifiDisabledByAirplane(false);
1140         if (state == WIFI_STATE_ENABLED) {
1141             WifiSettings::GetInstance().SetWifiFlagOnAirplaneMode(true);
1142         } else {
1143             WifiSettings::GetInstance().SetWifiFlagOnAirplaneMode(false);
1144             state = WIFI_STATE_DISABLED;
1145         }
1146     }
1147     SetPersistWifiState(state, id);
1148 }
1149 
SetPowerSavingModeState(const int & state)1150 void WifiConfigCenter::SetPowerSavingModeState(const int &state)
1151 {
1152     mPowerSavingModeState = state;
1153 }
1154 
GetPowerSavingModeState() const1155 int WifiConfigCenter::GetPowerSavingModeState() const
1156 {
1157     return mPowerSavingModeState.load();
1158 }
1159 
SetFreezeModeState(int state)1160 void WifiConfigCenter::SetFreezeModeState(int state)
1161 {
1162     mFreezeModeState = state;
1163 }
1164 
GetFreezeModeState() const1165 int WifiConfigCenter::GetFreezeModeState() const
1166 {
1167     return mFreezeModeState.load();
1168 }
1169 
SetNoChargerPlugModeState(int state)1170 void WifiConfigCenter::SetNoChargerPlugModeState(int state)
1171 {
1172     mNoChargerPlugModeState = state;
1173 }
1174 
GetNoChargerPlugModeState() const1175 int WifiConfigCenter::GetNoChargerPlugModeState() const
1176 {
1177     return mNoChargerPlugModeState.load();
1178 }
1179 
SetThreadStatusFlag(bool state)1180 void WifiConfigCenter::SetThreadStatusFlag(bool state)
1181 {
1182     if (state) {
1183         mThreadStartTime = std::chrono::duration_cast<std::chrono::milliseconds>(
1184         std::chrono::steady_clock::now().time_since_epoch()).count();
1185     }
1186     mThreadStatusFlag_ = state;
1187 }
1188 
GetThreadStatusFlag(void) const1189 bool WifiConfigCenter::GetThreadStatusFlag(void) const
1190 {
1191     return mThreadStatusFlag_.load();
1192 }
1193 
GetThreadStartTime(void) const1194 uint64_t WifiConfigCenter::GetThreadStartTime(void) const
1195 {
1196     return mThreadStartTime.load();
1197 }
1198 
StoreWifiMacAddrPairInfo(WifiMacAddrInfoType type,const std::string & realMacAddr,const std::string & randomAddr)1199 bool WifiConfigCenter::StoreWifiMacAddrPairInfo(WifiMacAddrInfoType type, const std::string &realMacAddr,
1200     const std::string &randomAddr)
1201 {
1202     if (realMacAddr.empty()) {
1203         return false;
1204     }
1205 
1206     if (type >= WifiMacAddrInfoType::INVALID_MACADDR_INFO) {
1207         return false;
1208     }
1209 
1210     std::string randomMacAddr;
1211     if (randomAddr.empty()) {
1212         WifiRandomMacHelper::GenerateRandomMacAddressByBssid(realMacAddr, randomMacAddr);
1213     } else {
1214         randomMacAddr = randomAddr;
1215     }
1216     LOGD("%{public}s: type:%{public}d, address:%{private}s, randomAddr:%{private}s, randomMacAddr:%{private}s",
1217         __func__, type, realMacAddr.c_str(), randomAddr.c_str(), randomMacAddr.c_str());
1218     WifiMacAddrInfo realMacAddrInfo;
1219     realMacAddrInfo.bssid = realMacAddr;
1220     realMacAddrInfo.bssidType = REAL_DEVICE_ADDRESS;
1221     WifiMacAddrErrCode ret = AddMacAddrPairs(type, realMacAddrInfo, randomMacAddr);
1222     if (ret == WIFI_MACADDR_OPER_SUCCESS) {
1223         WifiMacAddrInfo randomMacAddrInfo;
1224         randomMacAddrInfo.bssid = randomMacAddr;
1225         randomMacAddrInfo.bssidType = RANDOM_DEVICE_ADDRESS;
1226         AddMacAddrPairs(type, randomMacAddrInfo, realMacAddr);
1227     }
1228     return true;
1229 }
1230 
GetRandomMacAddr(WifiMacAddrInfoType type,std::string bssid)1231 std::string WifiConfigCenter::GetRandomMacAddr(WifiMacAddrInfoType type, std::string bssid)
1232 {
1233     LOGD("%{public}s: query a random mac address, type:%{public}d, bssid:%{private}s",
1234         __func__, type, bssid.c_str());
1235     WifiMacAddrInfo realMacAddrInfo;
1236     realMacAddrInfo.bssid = bssid;
1237     realMacAddrInfo.bssidType = REAL_DEVICE_ADDRESS;
1238     std::string randomMacAddr = GetMacAddrPairs(type, realMacAddrInfo);
1239     if (!randomMacAddr.empty()) {
1240         LOGD("%{public}s: find the record, bssid:%{private}s, bssidType:%{public}d, randomMacAddr:%{private}s",
1241             __func__, realMacAddrInfo.bssid.c_str(), realMacAddrInfo.bssidType, randomMacAddr.c_str());
1242         return randomMacAddr;
1243     } else {
1244         WifiMacAddrInfo randomMacAddrInfo;
1245         randomMacAddrInfo.bssid = bssid;
1246         randomMacAddrInfo.bssidType = RANDOM_DEVICE_ADDRESS;
1247         randomMacAddr = GetMacAddrPairs(type, realMacAddrInfo);
1248         if (!randomMacAddr.empty()) {
1249             LOGD("%{public}s: find the record, bssid:%{private}s, bssidType:%{public}d, randomMacAddr:%{private}s",
1250                 __func__, randomMacAddrInfo.bssid.c_str(), randomMacAddrInfo.bssidType, randomMacAddr.c_str());
1251             return randomMacAddr;
1252         }
1253     }
1254     return "";
1255 }
1256 
RemoveMacAddrPairs(WifiMacAddrInfoType type,const WifiMacAddrInfo & macAddrInfo)1257 int WifiConfigCenter::RemoveMacAddrPairs(WifiMacAddrInfoType type, const WifiMacAddrInfo &macAddrInfo)
1258 {
1259     LOGD("remove a mac address pair, type:%{public}d, bssid:%{private}s, bssidType:%{public}d",
1260         type, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType);
1261     std::unique_lock<std::mutex> lock(mMacAddrPairMutex);
1262     switch (type) {
1263         case WifiMacAddrInfoType::WIFI_SCANINFO_MACADDR_INFO:
1264             DelMacAddrPairs(mWifiScanMacAddrPair, macAddrInfo);
1265             break;
1266         case WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO:
1267             DelMacAddrPairs(mHotspotMacAddrPair, macAddrInfo);
1268             break;
1269         case WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO:
1270             DelMacAddrPairs(mP2pDeviceMacAddrPair, macAddrInfo);
1271             break;
1272         case WifiMacAddrInfoType::P2P_GROUPSINFO_MACADDR_INFO:
1273             DelMacAddrPairs(mP2pGroupsInfoMacAddrPair, macAddrInfo);
1274             break;
1275         case WifiMacAddrInfoType::P2P_CURRENT_GROUP_MACADDR_INFO:
1276             DelMacAddrPairs(mP2pCurrentgroupMacAddrPair, macAddrInfo);
1277             break;
1278         default:
1279             LOGE("%{public}s: invalid mac address type, type:%{public}d", __func__, type);
1280             return -1;
1281     }
1282     return 0;
1283 }
1284 
GetMacAddrPairs(WifiMacAddrInfoType type,const WifiMacAddrInfo & macAddrInfo)1285 std::string WifiConfigCenter::GetMacAddrPairs(WifiMacAddrInfoType type, const WifiMacAddrInfo &macAddrInfo)
1286 {
1287     LOGD("get a mac address pair, type:%{public}d, bssid:%{private}s, bssidType:%{public}d",
1288         type, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType);
1289     std::unique_lock<std::mutex> lock(mMacAddrPairMutex);
1290     switch (type) {
1291         case WifiMacAddrInfoType::WIFI_SCANINFO_MACADDR_INFO:
1292             return GetPairMacAddress(mWifiScanMacAddrPair, macAddrInfo);
1293         case WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO:
1294             return GetPairMacAddress(mHotspotMacAddrPair, macAddrInfo);
1295         case WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO:
1296             return GetPairMacAddress(mP2pDeviceMacAddrPair, macAddrInfo);
1297         case WifiMacAddrInfoType::P2P_GROUPSINFO_MACADDR_INFO:
1298             return GetPairMacAddress(mP2pGroupsInfoMacAddrPair, macAddrInfo);
1299         case WifiMacAddrInfoType::P2P_CURRENT_GROUP_MACADDR_INFO:
1300             return GetPairMacAddress(mP2pCurrentgroupMacAddrPair, macAddrInfo);
1301         default:
1302             LOGE("%{public}s: invalid mac address type, type:%{public}d", __func__, type);
1303             return "";
1304     }
1305     return "";
1306 }
1307 
ClearMacAddrPairs(WifiMacAddrInfoType type)1308 void WifiConfigCenter::ClearMacAddrPairs(WifiMacAddrInfoType type)
1309 {
1310     LOGI("%{public}s type:%{public}d", __func__, type);
1311     std::unique_lock<std::mutex> lock(mMacAddrPairMutex);
1312     switch (type) {
1313         case WifiMacAddrInfoType::WIFI_SCANINFO_MACADDR_INFO:
1314             mWifiScanMacAddrPair.clear();
1315             break;
1316         case WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO:
1317             mHotspotMacAddrPair.clear();
1318             break;
1319         case WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO:
1320             mP2pDeviceMacAddrPair.clear();
1321             break;
1322         case WifiMacAddrInfoType::P2P_GROUPSINFO_MACADDR_INFO:
1323             mP2pGroupsInfoMacAddrPair.clear();
1324             break;
1325         case WifiMacAddrInfoType::P2P_CURRENT_GROUP_MACADDR_INFO:
1326             mP2pCurrentgroupMacAddrPair.clear();
1327             break;
1328         default:
1329             LOGE("%{public}s: invalid mac address type, type:%{public}d", __func__, type);
1330     }
1331     return;
1332 }
1333 
HasWifiActive()1334 bool WifiConfigCenter::HasWifiActive()
1335 {
1336     std::unique_lock<std::mutex> lock(mStaMutex);
1337     for (auto &item : mWifiState) {
1338         int state = item.second.load();
1339         if (state == static_cast<int>(WifiState::ENABLING) || state == static_cast<int>(WifiState::ENABLED)) {
1340             LOGD("HasWifiActive: one wifi is active! instId:%{public}d", item.first);
1341             return true;
1342         }
1343     }
1344     LOGD("HasWifiActive: No wifi is active!");
1345     return false;
1346 }
1347 
UpdateLinkedInfo(int instId)1348 void WifiConfigCenter::UpdateLinkedInfo(int instId)
1349 {
1350     std::vector<WifiScanInfo> wifiScanInfoList;
1351     wifiScanConfig->GetScanInfoListInner(wifiScanInfoList);
1352     for (auto iter = wifiScanInfoList.begin(); iter != wifiScanInfoList.end(); ++iter) {
1353         if (iter->bssid == mWifiLinkedInfo[instId].bssid) {
1354             if (mWifiLinkedInfo[instId].channelWidth == WifiChannelWidth::WIDTH_INVALID) {
1355                 mWifiLinkedInfo[instId].channelWidth = iter->channelWidth;
1356             }
1357             mWifiLinkedInfo[instId].isHiLinkNetwork = iter->isHiLinkNetwork;
1358             break;
1359         }
1360     }
1361     WifiCategory category = wifiScanConfig->GetWifiCategoryRecord(mWifiLinkedInfo[instId].bssid);
1362     mWifiLinkedInfo[instId].supportedWifiCategory = category;
1363     LOGD("WifiSettings UpdateLinkedInfo.");
1364 }
1365 
SetPersistWifiState(int state,int instId)1366 void WifiConfigCenter::SetPersistWifiState(int state, int instId)
1367 {
1368     if (instId < 0 || instId >= STA_INSTANCE_MAX_NUM) {
1369         LOGE("SetPersistWifiState invalid instId %{public}d", instId);
1370         return;
1371     }
1372     mPersistWifiState.at(instId) = state;
1373     WifiSettings::GetInstance().SetOperatorWifiType(state, instId);
1374     LOGI("persist wifi state is %{public}d", state);
1375 }
1376 
GetPersistWifiState(int instId)1377 int WifiConfigCenter::GetPersistWifiState(int instId)
1378 {
1379     if (instId < 0 || instId >= STA_INSTANCE_MAX_NUM) {
1380         LOGE("GetPersistWifiState invalid instId %{public}d", instId);
1381         return -1;
1382     }
1383     return mPersistWifiState.at(instId);
1384 }
1385 
GetPairMacAddress(std::map<WifiMacAddrInfo,std::string> & macAddrInfoMap,const WifiMacAddrInfo & macAddrInfo)1386 std::string WifiConfigCenter::GetPairMacAddress(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap,
1387     const WifiMacAddrInfo &macAddrInfo)
1388 {
1389     auto iter = macAddrInfoMap.find(macAddrInfo);
1390     if (iter != macAddrInfoMap.end()) {
1391         LOGD("%{public}s: find the record, realMacAddr:%{private}s, bssidType:%{public}d, randomMacAddr:%{private}s",
1392             __func__, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType, iter->second.c_str());
1393         return iter->second;
1394     } else {
1395         LOGE("%{public}s: record not found, macaddr: %{public}s", __func__, MacAnonymize(macAddrInfo.bssid).c_str());
1396     }
1397     return "";
1398 }
1399 
InsertMacAddrPairs(std::map<WifiMacAddrInfo,std::string> & macAddrInfoMap,const WifiMacAddrInfo & macAddrInfo,std::string & randomMacAddr)1400 WifiMacAddrErrCode WifiConfigCenter::InsertMacAddrPairs(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap,
1401     const WifiMacAddrInfo &macAddrInfo, std::string& randomMacAddr)
1402 {
1403     auto iter = macAddrInfoMap.find(macAddrInfo);
1404     if (iter != macAddrInfoMap.end()) {
1405         LOGD("%{public}s: the record is existed, macAddr:%{private}s, bssidType:%{public}d, value:%{private}s",
1406             __func__, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType, iter->second.c_str());
1407         return WIFI_MACADDR_HAS_EXISTED;
1408     } else {
1409         macAddrInfoMap.insert(std::make_pair(macAddrInfo, randomMacAddr));
1410         LOGD("%{public}s: add a mac address pair, bssid:%{private}s, bssidType:%{public}d, randomMacAddr:%{private}s",
1411             __func__, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType, randomMacAddr.c_str());
1412         return WIFI_MACADDR_OPER_SUCCESS;
1413     }
1414 }
1415 
DelMacAddrPairs(std::map<WifiMacAddrInfo,std::string> & macAddrInfoMap,const WifiMacAddrInfo & macAddrInfo)1416 void WifiConfigCenter::DelMacAddrPairs(std::map<WifiMacAddrInfo, std::string>& macAddrInfoMap,
1417     const WifiMacAddrInfo &macAddrInfo)
1418 {
1419     auto iter = macAddrInfoMap.find(macAddrInfo);
1420     if (iter != macAddrInfoMap.end()) {
1421         if (iter->second.empty()) {
1422             LOGI("%{public}s: invalid record, bssid:%{private}s, bssidType:%{public}d",
1423                 __func__, iter->first.bssid.c_str(), iter->first.bssidType);
1424         } else {
1425             LOGD("%{public}s:find the record, realMacAddr:%{private}s, bssidType:%{public}d, randomMacAddr:%{private}s",
1426                 __func__, macAddrInfo.bssid.c_str(), macAddrInfo.bssidType, iter->second.c_str());
1427         }
1428         macAddrInfoMap.erase(iter);
1429     }
1430 }
1431 
RemoveMacAddrPairInfo(WifiMacAddrInfoType type,std::string bssid,int bssidType)1432 void WifiConfigCenter::RemoveMacAddrPairInfo(WifiMacAddrInfoType type, std::string bssid, int bssidType)
1433 {
1434     LOGD("%{public}s: remove a mac address pair, type:%{public}d, bssid:%{private}s",
1435         __func__, type, bssid.c_str());
1436     WifiMacAddrInfo randomMacAddrInfo;
1437     randomMacAddrInfo.bssid = GetRandomMacAddr(type, bssid);
1438     randomMacAddrInfo.bssidType = REAL_DEVICE_ADDRESS == bssidType ? RANDOM_DEVICE_ADDRESS : REAL_DEVICE_ADDRESS;
1439     RemoveMacAddrPairs(type, randomMacAddrInfo);
1440 
1441     WifiMacAddrInfo realMacAddrInfo;
1442     realMacAddrInfo.bssid = bssid;
1443     realMacAddrInfo.bssidType = REAL_DEVICE_ADDRESS == bssidType ? REAL_DEVICE_ADDRESS : RANDOM_DEVICE_ADDRESS;
1444     RemoveMacAddrPairs(type, realMacAddrInfo);
1445 }
1446 
AddMacAddrPairs(WifiMacAddrInfoType type,const WifiMacAddrInfo & macAddrInfo,std::string randomMacAddr)1447 WifiMacAddrErrCode WifiConfigCenter::AddMacAddrPairs(WifiMacAddrInfoType type,
1448     const WifiMacAddrInfo &macAddrInfo, std::string randomMacAddr)
1449 {
1450     if ((type >= WifiMacAddrInfoType::INVALID_MACADDR_INFO) || macAddrInfo.bssid.empty()) {
1451         LOGE("%{public}s: invalid parameter, type:%{public}d, bssid:%{private}s",
1452             __func__, type, macAddrInfo.bssid.c_str());
1453         return WIFI_MACADDR_INVALID_PARAM;
1454     }
1455     std::unique_lock<std::mutex> lock(mMacAddrPairMutex);
1456     switch (type) {
1457         case WifiMacAddrInfoType::WIFI_SCANINFO_MACADDR_INFO:
1458             return InsertMacAddrPairs(mWifiScanMacAddrPair, macAddrInfo, randomMacAddr);
1459         case WifiMacAddrInfoType::HOTSPOT_MACADDR_INFO:
1460             return InsertMacAddrPairs(mHotspotMacAddrPair, macAddrInfo, randomMacAddr);
1461         case WifiMacAddrInfoType::P2P_DEVICE_MACADDR_INFO:
1462             return InsertMacAddrPairs(mP2pDeviceMacAddrPair, macAddrInfo, randomMacAddr);
1463         case WifiMacAddrInfoType::P2P_GROUPSINFO_MACADDR_INFO:
1464             return InsertMacAddrPairs(mP2pGroupsInfoMacAddrPair, macAddrInfo, randomMacAddr);
1465         case WifiMacAddrInfoType::P2P_CURRENT_GROUP_MACADDR_INFO:
1466             return InsertMacAddrPairs(mP2pCurrentgroupMacAddrPair, macAddrInfo, randomMacAddr);
1467         default:
1468             LOGE("%{public}s: invalid mac address type, type:%{public}d", __func__, type);
1469             break;
1470     }
1471     return WIFI_MACADDR_INVALID_PARAM;
1472 }
1473 
GetAllWifiLinkedNetworkId()1474 std::set<int> WifiConfigCenter::GetAllWifiLinkedNetworkId()
1475 {
1476     std::unique_lock<std::mutex> lock(mStaMutex);
1477     std::set<int> wifiLinkedNetworkId;
1478     for (auto iter = mWifiLinkedInfo.begin(); iter != mWifiLinkedInfo.end(); iter++) {
1479         wifiLinkedNetworkId.insert(iter->second.networkId);
1480     }
1481     return wifiLinkedNetworkId;
1482 }
1483 
SetSystemMode(int systemMode)1484 void WifiConfigCenter::SetSystemMode(int systemMode)
1485 {
1486     systemMode_ = systemMode;
1487     LOGI("SetSystemMode %{public}d", systemMode_);
1488 }
1489 
GetSystemMode()1490 int WifiConfigCenter::GetSystemMode()
1491 {
1492     LOGI("GetSystemMode %{public}d", systemMode_);
1493     return systemMode_;
1494 }
1495 
SetDeviceType(int deviceType)1496 void WifiConfigCenter::SetDeviceType(int deviceType)
1497 {
1498     mDeviceType = deviceType;
1499 }
1500 
IsAllowPopUp()1501 bool WifiConfigCenter::IsAllowPopUp()
1502 {
1503     switch (mDeviceType) {
1504         case ProductDeviceType::WEARABLE:
1505             LOGI("Not allow pop up dialog, device type:%{public}d", mDeviceType);
1506             return false;
1507         default:
1508             LOGI("Allow pop up dialog, device type:%{public}d", mDeviceType);
1509             return true;
1510     }
1511 }
1512 
IsNeedFastScan(void)1513 bool WifiConfigCenter::IsNeedFastScan(void)
1514 {
1515     std::unique_lock<std::mutex> lock(mScanMutex);
1516     return isNeedFastScan;
1517 }
1518 
SetFastScan(bool fastScan)1519 void WifiConfigCenter::SetFastScan(bool fastScan)
1520 {
1521     std::unique_lock<std::mutex> lock(mScanMutex);
1522     isNeedFastScan = fastScan;
1523 }
1524 
1525 }  // namespace Wifi
1526 }  // namespace OHOS
1527