1 /*
2 * Copyright (C) 2024-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 <ctime>
17 #include <vector>
18 #include "wifi_timer.h"
19 #include "network_black_list_manager.h"
20 #include "wifi_logger.h"
21 #include "wifi_common_util.h"
22
23 namespace OHOS {
24 namespace Wifi {
25 DEFINE_WIFILOG_LABEL("NetworkBlockListManager");
26 namespace {
27 constexpr int32_t MAX_CONNECT_FAILED_TIMES = 2;
28 }
29
NetworkBlockListManager()30 NetworkBlockListManager::NetworkBlockListManager()
31 {}
32
~NetworkBlockListManager()33 NetworkBlockListManager::~NetworkBlockListManager()
34 {}
35
GetInstance()36 NetworkBlockListManager &NetworkBlockListManager::GetInstance()
37 {
38 static NetworkBlockListManager gNetworkBlockListManager;
39 return gNetworkBlockListManager;
40 }
41
AddWifiBlocklist(const std::string & bssid)42 void NetworkBlockListManager::AddWifiBlocklist(const std::string &bssid)
43 {
44 if (bssid.empty()) {
45 WIFI_LOGI("AddWifiBlocklist, bssid is invalid");
46 return;
47 }
48
49 WIFI_LOGI("AddWifiBlocklist, bssid:%{public}s", MacAnonymize(bssid).c_str());
50 std::lock_guard<std::mutex> lock(mutex_);
51 wifiBlockSet_.insert(bssid);
52 }
53
RemoveWifiBlocklist(const std::string & bssid)54 void NetworkBlockListManager::RemoveWifiBlocklist(const std::string &bssid)
55 {
56 WIFI_LOGI("Enter RemoveWifiBlocklist");
57 std::lock_guard<std::mutex> lock(mutex_);
58 if (wifiBlockSet_.empty()) {
59 WIFI_LOGI("RemoveWifiBlocklist, wifiBlockSet is empty");
60 return;
61 }
62
63 if (wifiBlockSet_.find(bssid) != wifiBlockSet_.end()) {
64 WIFI_LOGI("RemoveWifiBlocklist, bssid:%{public}s", MacAnonymize(bssid).c_str());
65 wifiBlockSet_.erase(bssid);
66 }
67 }
68
IsInWifiBlocklist(const std::string & bssid)69 bool NetworkBlockListManager::IsInWifiBlocklist(const std::string &bssid)
70 {
71 if (bssid.empty()) {
72 return false;
73 }
74
75 std::lock_guard<std::mutex> lock(mutex_);
76 if (wifiBlockSet_.empty()) {
77 return false;
78 }
79
80 auto iter = wifiBlockSet_.find(bssid);
81 return iter != wifiBlockSet_.end();
82 }
83
AddAbnormalWifiBlocklist(const std::string & bssid)84 void NetworkBlockListManager::AddAbnormalWifiBlocklist(const std::string &bssid)
85 {
86 if (bssid.empty()) {
87 WIFI_LOGI("AddAbnormalWifiBlocklist, bssid is invalid");
88 return;
89 }
90 std::lock_guard<std::mutex> lock(mutex_);
91 WIFI_LOGI("AddAbnormalWifiBlocklist, bssid:%{public}s", MacAnonymize(bssid).c_str());
92 abnormalWifiBlockSet_.insert(bssid);
93 }
94
CleanAbnormalWifiBlocklist()95 void NetworkBlockListManager::CleanAbnormalWifiBlocklist()
96 {
97 std::lock_guard<std::mutex> lock(mutex_);
98 WIFI_LOGI("CleanAbnormalWifiBlocklist");
99 abnormalWifiBlockSet_.clear();
100 }
101
IsInAbnormalWifiBlocklist(const std::string & bssid)102 bool NetworkBlockListManager::IsInAbnormalWifiBlocklist(const std::string &bssid)
103 {
104 if (abnormalWifiBlockSet_.empty()) {
105 return false;
106 }
107
108 std::lock_guard<std::mutex> lock(mutex_);
109 return abnormalWifiBlockSet_.find(bssid) != abnormalWifiBlockSet_.end();
110 }
111
CleanTempWifiBlockList()112 void NetworkBlockListManager::CleanTempWifiBlockList()
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 std::map<std::string, int32_t> tempMap;
116 tempWifiBlockMap_.swap(tempMap);
117 }
118
IsInTempWifiBlockList(const std::string & bssid)119 bool NetworkBlockListManager::IsInTempWifiBlockList(const std::string &bssid)
120 {
121 if (tempWifiBlockMap_.empty()) {
122 return false;
123 }
124
125 std::lock_guard<std::mutex> lock(mutex_);
126 return tempWifiBlockMap_.find(bssid) != tempWifiBlockMap_.end();
127 }
128
IsFailedMultiTimes(const std::string & bssid)129 bool NetworkBlockListManager::IsFailedMultiTimes(const std::string &bssid)
130 {
131 if (bssid.empty()) {
132 return false;
133 }
134
135 std::lock_guard<std::mutex> lock(mutex_);
136 int32_t counter = 1;
137 auto iter = tempWifiBlockMap_.find(bssid);
138 if (iter != tempWifiBlockMap_.end()) {
139 counter++;
140 iter->second = counter;
141 } else {
142 tempWifiBlockMap_[bssid] = counter;
143 }
144
145 return counter >= MAX_CONNECT_FAILED_TIMES;
146 }
147
148 } // namespace Wifi
149 } // namespace OHOS