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 "wifi_filter_impl.h"
17 #include "network_selection_utils.h"
18 #include "network_status_history_manager.h"
19 #include "wifi_config_center.h"
20 #include "wifi_logger.h"
21 #include "wifi_settings.h"
22 #include "network_black_list_manager.h"
23 #include "wifi_ap_msg.h"
24 #ifndef OHOS_ARCH_LITE
25 #include "wifi_app_state_aware.h"
26 #include "wifi_sensor_scene.h"
27 #endif
28 namespace OHOS::Wifi::NetworkSelection {
29 DEFINE_WIFILOG_LABEL("WifiFilter")
30 namespace {
31 constexpr int RECHECK_DELAYED_SECONDS = 1 * 60 * 60;
32 constexpr int SIGNAL_LEVEL_TWO = 2;
33 constexpr int POOR_PORTAL_RECHECK_DELAYED_SECONDS = 2 * RECHECK_DELAYED_SECONDS;
34 constexpr int32_t MIN_SIGNAL_LEVEL_INTERVAL = 2;
35 constexpr int32_t SIGNAL_LEVEL_THREE = 3;
36 constexpr int32_t MIN_RSSI_INTERVAL = 8;
37 }
38
HiddenWifiFilter()39 HiddenWifiFilter::HiddenWifiFilter() : SimpleWifiFilter("notHidden") {}
40
~HiddenWifiFilter()41 HiddenWifiFilter::~HiddenWifiFilter()
42 {
43 if (!filteredNetworkCandidates.empty()) {
44 WIFI_LOGI("%{public}s: %{public}s",
45 filterName.c_str(),
46 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
47 }
48 }
49
Filter(NetworkCandidate & networkCandidate)50 bool HiddenWifiFilter::Filter(NetworkCandidate &networkCandidate)
51 {
52 if (networkCandidate.interScanInfo.ssid.empty()) {
53 networkCandidate.filtedReason[filterName].insert(FiltedReason::HIDDEN_NETWORK);
54 return false;
55 }
56 return true;
57 }
58
59
SignalStrengthWifiFilter()60 SignalStrengthWifiFilter::SignalStrengthWifiFilter(): SimpleWifiFilter("notSignalWooWeak") {}
61
~SignalStrengthWifiFilter()62 SignalStrengthWifiFilter::~SignalStrengthWifiFilter()
63 {
64 if (!filteredNetworkCandidates.empty()) {
65 WIFI_LOGI("%{public}s: %{public}s",
66 filterName.c_str(),
67 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
68 }
69 }
70
Filter(NetworkCandidate & networkCandidate)71 bool SignalStrengthWifiFilter::Filter(NetworkCandidate &networkCandidate)
72 {
73 auto &scanInfo = networkCandidate.interScanInfo;
74 #ifndef OHOS_ARCH_LITE
75 int rssiThreshold = WifiSensorScene::GetInstance().GetMinRssiThres(scanInfo.frequency);
76 #else
77 auto rssiThreshold = scanInfo.frequency < MIN_5GHZ_BAND_FREQUENCY ? MIN_RSSI_VALUE_24G : MIN_RSSI_VALUE_5G;
78 #endif
79 if (scanInfo.rssi < rssiThreshold) {
80 networkCandidate.filtedReason[filterName].insert(FiltedReason::POOR_SIGNAL);
81 return false;
82 }
83 return true;
84 }
85
SavedWifiFilter()86 SavedWifiFilter::SavedWifiFilter() : SimpleWifiFilter("savedWifiFilter") {}
87
~SavedWifiFilter()88 SavedWifiFilter::~SavedWifiFilter()
89 {
90 if (!filteredNetworkCandidates.empty()) {
91 WIFI_LOGI("%{public}s: %{public}s",
92 filterName.c_str(),
93 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
94 }
95 }
96
Filter(NetworkCandidate & networkCandidate)97 bool SavedWifiFilter::Filter(NetworkCandidate &networkCandidate)
98 {
99 if (networkCandidate.wifiDeviceConfig.networkId == INVALID_NETWORK_ID) {
100 networkCandidate.filtedReason[filterName].insert(FiltedReason::NETWORK_ID_INVALID);
101 return false;
102 }
103 if (networkCandidate.wifiDeviceConfig.uid != -1 && networkCandidate.wifiDeviceConfig.isShared == false) {
104 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_SYSTEM_NETWORK);
105 return false;
106 }
107 return true;
108 }
109
110
EphemeralWifiFilter()111 EphemeralWifiFilter::EphemeralWifiFilter() : SimpleWifiFilter("notEphemeral") {}
112
~EphemeralWifiFilter()113 EphemeralWifiFilter::~EphemeralWifiFilter()
114 {
115 if (!filteredNetworkCandidates.empty()) {
116 WIFI_LOGI("%{public}s: %{public}s",
117 filterName.c_str(),
118 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
119 }
120 }
121
Filter(NetworkCandidate & networkCandidate)122 bool EphemeralWifiFilter::Filter(NetworkCandidate &networkCandidate)
123 {
124 if (networkCandidate.wifiDeviceConfig.isEphemeral) {
125 networkCandidate.filtedReason[filterName].insert(FiltedReason::EPHEMERAL_NETWORK);
126 return false;
127 }
128 return true;
129 }
130
PassPointWifiFilter()131 PassPointWifiFilter::PassPointWifiFilter() : SimpleWifiFilter("notPassPoint") {}
132
~PassPointWifiFilter()133 PassPointWifiFilter::~PassPointWifiFilter()
134 {
135 if (!filteredNetworkCandidates.empty()) {
136 WIFI_LOGI("%{public}s: %{public}s",
137 filterName.c_str(),
138 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
139 }
140 }
141
Filter(NetworkCandidate & networkCandidate)142 bool PassPointWifiFilter::Filter(NetworkCandidate &networkCandidate)
143 {
144 if (networkCandidate.wifiDeviceConfig.isPasspoint) {
145 networkCandidate.filtedReason[filterName].insert(FiltedReason::PASSPOINT_NETWORK);
146 return false;
147 }
148 return true;
149 }
150
151
DisableWifiFilter()152 DisableWifiFilter::DisableWifiFilter() : SimpleWifiFilter("enableWifi") {}
153
~DisableWifiFilter()154 DisableWifiFilter::~DisableWifiFilter()
155 {
156 if (!filteredNetworkCandidates.empty()) {
157 WIFI_LOGI("%{public}s: %{public}s",
158 filterName.c_str(),
159 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
160 }
161 }
162
Filter(NetworkCandidate & networkCandidate)163 bool DisableWifiFilter::Filter(NetworkCandidate &networkCandidate)
164 {
165 if (networkCandidate.wifiDeviceConfig.networkSelectionStatus.status != WifiDeviceConfigStatus::ENABLED) {
166 networkCandidate.filtedReason[filterName].insert(FiltedReason::NETWORK_STATUS_DISABLE);
167 return false;
168 }
169 if (!networkCandidate.wifiDeviceConfig.isAllowAutoConnect) {
170 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_ALLOW_AUTO_CONNECT);
171 return false;
172 }
173 if (!networkCandidate.wifiDeviceConfig.isSecureWifi) {
174 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_SECURE_WIFI);
175 return false;
176 }
177 return true;
178 }
179
MatchedUserSelectBssidWifiFilter()180 MatchedUserSelectBssidWifiFilter::MatchedUserSelectBssidWifiFilter() : SimpleWifiFilter("matchUserSelect") {}
181
~MatchedUserSelectBssidWifiFilter()182 MatchedUserSelectBssidWifiFilter::~MatchedUserSelectBssidWifiFilter()
183 {
184 if (!filteredNetworkCandidates.empty()) {
185 WIFI_LOGI("%{public}s: %{public}s",
186 filterName.c_str(),
187 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
188 }
189 }
190
Filter(NetworkCandidate & networkCandidate)191 bool MatchedUserSelectBssidWifiFilter::Filter(NetworkCandidate &networkCandidate)
192 {
193 if (networkCandidate.wifiDeviceConfig.userSelectBssid.empty()) {
194 return true;
195 }
196 if (networkCandidate.interScanInfo.bssid != networkCandidate.wifiDeviceConfig.userSelectBssid) {
197 networkCandidate.filtedReason[filterName].insert(FiltedReason::UNEXPECTED_NETWORK_BY_USER);
198 return false;
199 }
200 return true;
201 }
202
HasInternetWifiFilter()203 HasInternetWifiFilter::HasInternetWifiFilter() : SimpleWifiFilter("hasInternet") {}
204
~HasInternetWifiFilter()205 HasInternetWifiFilter::~HasInternetWifiFilter()
206 {
207 if (!filteredNetworkCandidates.empty()) {
208 WIFI_LOGI("%{public}s: %{public}s",
209 filterName.c_str(),
210 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
211 }
212 }
213
Filter(NetworkCandidate & networkCandidate)214 bool HasInternetWifiFilter::Filter(NetworkCandidate &networkCandidate)
215 {
216 auto &wifiDeviceConfig = networkCandidate.wifiDeviceConfig;
217 if (wifiDeviceConfig.noInternetAccess) {
218 networkCandidate.filtedReason[filterName].insert(FiltedReason::NO_INTERNET);
219 return false;
220 }
221 if (wifiDeviceConfig.isPortal) {
222 networkCandidate.filtedReason[filterName].insert(FiltedReason::PORTAL_NETWORK);
223 return false;
224 }
225 if (NetworkStatusHistoryManager::IsInternetAccessByHistory(wifiDeviceConfig.networkStatusHistory)) {
226 return true;
227 }
228 if (NetworkSelectionUtils::IsOpenNetwork(networkCandidate)) {
229 networkCandidate.filtedReason[filterName].insert(FiltedReason::OPEN_NETWORK);
230 return false;
231 }
232 if (!NetworkStatusHistoryManager::IsEmptyNetworkStatusHistory(wifiDeviceConfig.networkStatusHistory)) {
233 networkCandidate.filtedReason[filterName].insert(FiltedReason::HAS_NETWORK_HISTORY);
234 return false;
235 }
236 return true;
237 }
238
RecoveryWifiFilter()239 RecoveryWifiFilter::RecoveryWifiFilter() : SimpleWifiFilter("recovery") {}
240
~RecoveryWifiFilter()241 RecoveryWifiFilter::~RecoveryWifiFilter()
242 {
243 if (!filteredNetworkCandidates.empty()) {
244 WIFI_LOGI("%{public}s: %{public}s",
245 filterName.c_str(),
246 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
247 }
248 }
249
Filter(NetworkCandidate & networkCandidate)250 bool RecoveryWifiFilter::Filter(NetworkCandidate &networkCandidate)
251 {
252 auto &wifiDeviceConfig = networkCandidate.wifiDeviceConfig;
253 if (NetworkStatusHistoryManager::IsEmptyNetworkStatusHistory(wifiDeviceConfig.networkStatusHistory)) {
254 InterScanInfo interScanInfo = networkCandidate.interScanInfo;
255 WIFI_LOGI("RecoveryWifiFilter, network history is 0, try reconnect, add candidate network, bssid=%{public}s",
256 MacAnonymize(interScanInfo.bssid).c_str());
257 return true;
258 }
259 if (!wifiDeviceConfig.noInternetAccess) {
260 networkCandidate.filtedReason[filterName].insert(FiltedReason::HAS_INTERNET);
261 return false;
262 }
263 if (wifiDeviceConfig.isPortal) {
264 networkCandidate.filtedReason[filterName].insert(FiltedReason::PORTAL_NETWORK);
265 return false;
266 }
267 if (!NetworkStatusHistoryManager::IsAllowRecoveryByHistory(wifiDeviceConfig.networkStatusHistory)) {
268 networkCandidate.filtedReason[filterName].insert(FiltedReason::UNRECOVERABLE_NETWORK);
269 return false;
270 }
271 return true;
272 }
273
PoorPortalWifiFilter()274 PoorPortalWifiFilter::PoorPortalWifiFilter() : SimpleWifiFilter("notPoorPortal") {}
275
~PoorPortalWifiFilter()276 PoorPortalWifiFilter::~PoorPortalWifiFilter()
277 {
278 if (!filteredNetworkCandidates.empty()) {
279 WIFI_LOGI("%{public}s: %{public}s",
280 filterName.c_str(),
281 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
282 }
283 }
284
Filter(NetworkCandidate & networkCandidate)285 bool PoorPortalWifiFilter::Filter(NetworkCandidate &networkCandidate)
286 {
287 auto &interScanInfo = networkCandidate.interScanInfo;
288 if (networkCandidate.wifiDeviceConfig.isPortal &&
289 networkCandidate.wifiDeviceConfig.noInternetAccess &&
290 !NetworkStatusHistoryManager::IsAllowRecoveryByHistory(
291 networkCandidate.wifiDeviceConfig.networkStatusHistory)) {
292 networkCandidate.filtedReason[filterName].insert({FiltedReason::PORTAL_NETWORK, FiltedReason::NO_INTERNET,
293 FiltedReason::UNRECOVERABLE_NETWORK});
294 return false;
295 }
296 int currentSignalLevel = WifiSettings::GetInstance().GetSignalLevel(interScanInfo.rssi, interScanInfo.band);
297 if (currentSignalLevel > SIGNAL_LEVEL_TWO) {
298 return true;
299 }
300 if (currentSignalLevel < SIGNAL_LEVEL_TWO) {
301 networkCandidate.filtedReason[filterName].insert(FiltedReason::POOR_SIGNAL);
302 return false;
303 }
304 auto lastHasInternetTime = networkCandidate.wifiDeviceConfig.lastHasInternetTime;
305 auto now = time(nullptr);
306 if (now < 0) {
307 WIFI_LOGW("time return invalid!\n.");
308 networkCandidate.filtedReason[filterName].insert(FiltedReason::TIME_INVALID);
309 return false;
310 }
311 if ((now - lastHasInternetTime) > POOR_PORTAL_RECHECK_DELAYED_SECONDS) {
312 networkCandidate.filtedReason[filterName].insert(FiltedReason::TIMEOUT_AND_NEED_RECHECK);
313 return false;
314 }
315 return true;
316 }
317
PortalWifiFilter()318 PortalWifiFilter::PortalWifiFilter() : SimpleWifiFilter("portalWifiFilter") {}
319
~PortalWifiFilter()320 PortalWifiFilter::~PortalWifiFilter()
321 {
322 if (!filteredNetworkCandidates.empty()) {
323 WIFI_LOGI("%{public}s: %{public}s",
324 filterName.c_str(),
325 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
326 }
327 }
328
Filter(NetworkCandidate & networkCandidate)329 bool PortalWifiFilter::Filter(NetworkCandidate &networkCandidate)
330 {
331 if (networkCandidate.wifiDeviceConfig.isPortal &&
332 networkCandidate.wifiDeviceConfig.noInternetAccess &&
333 !NetworkStatusHistoryManager::IsAllowRecoveryByHistory(
334 networkCandidate.wifiDeviceConfig.networkStatusHistory)) {
335 networkCandidate.filtedReason[filterName].insert({FiltedReason::PORTAL_NETWORK, FiltedReason::NO_INTERNET,
336 FiltedReason::UNRECOVERABLE_NETWORK});
337 return false;
338 }
339 if (!networkCandidate.wifiDeviceConfig.isPortal) {
340 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_PORTAL_NETWORK);
341 return false;
342 }
343 return true;
344 }
345
MaybePortalWifiFilter()346 MaybePortalWifiFilter::MaybePortalWifiFilter() : SimpleWifiFilter("maybePortal") {}
347
~MaybePortalWifiFilter()348 MaybePortalWifiFilter::~MaybePortalWifiFilter()
349 {
350 if (!filteredNetworkCandidates.empty()) {
351 WIFI_LOGI("%{public}s: %{public}s",
352 filterName.c_str(),
353 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
354 }
355 }
356
Filter(NetworkCandidate & networkCandidate)357 bool MaybePortalWifiFilter::Filter(NetworkCandidate &networkCandidate)
358 {
359 if (NetworkSelectionUtils::IsScanResultForOweNetwork(networkCandidate)) {
360 networkCandidate.filtedReason[filterName].insert(FiltedReason::OWE_NETWORK);
361 return false;
362 }
363 if (!NetworkSelectionUtils::IsOpenAndMaybePortal(networkCandidate, filterName)) {
364 //The filtering reason has been added to this IsOpenAndMaybePortal interface.
365 return false;
366 }
367 if (networkCandidate.wifiDeviceConfig.noInternetAccess &&
368 !NetworkStatusHistoryManager::IsAllowRecoveryByHistory(
369 networkCandidate.wifiDeviceConfig.networkStatusHistory)) {
370 networkCandidate.filtedReason[filterName].insert({FiltedReason::NO_INTERNET,
371 FiltedReason::UNRECOVERABLE_NETWORK});
372 return false;
373 }
374 return true;
375 }
376
377
NoInternetWifiFilter()378 NoInternetWifiFilter::NoInternetWifiFilter() : SimpleWifiFilter("noInternet") {}
379
~NoInternetWifiFilter()380 NoInternetWifiFilter::~NoInternetWifiFilter()
381 {
382 if (!filteredNetworkCandidates.empty()) {
383 WIFI_LOGI("%{public}s: %{public}s",
384 filterName.c_str(),
385 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
386 }
387 }
388
Filter(NetworkCandidate & networkCandidate)389 bool NoInternetWifiFilter::Filter(NetworkCandidate &networkCandidate)
390 {
391 auto &wifiDeviceConfig = networkCandidate.wifiDeviceConfig;
392 if (!NetworkStatusHistoryManager::HasInternetEverByHistory(wifiDeviceConfig.networkStatusHistory)) {
393 networkCandidate.filtedReason[filterName].insert(FiltedReason::NO_INTERNET);
394 return false;
395 }
396 return true;
397 }
398
WeakAlgorithmWifiFilter()399 WeakAlgorithmWifiFilter::WeakAlgorithmWifiFilter() : SimpleWifiFilter("noWeakAlgorithm") {}
400
~WeakAlgorithmWifiFilter()401 WeakAlgorithmWifiFilter::~WeakAlgorithmWifiFilter()
402 {
403 if (!filteredNetworkCandidates.empty()) {
404 WIFI_LOGI("%{public}s: %{public}s",
405 filterName.c_str(),
406 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates, filterName).c_str());
407 }
408 }
409
Filter(NetworkCandidate & networkCandidate)410 bool WeakAlgorithmWifiFilter::Filter(NetworkCandidate &networkCandidate)
411 {
412 auto &scanInfo = networkCandidate.interScanInfo;
413 if (scanInfo.securityType == WifiSecurity::WEP) {
414 WIFI_LOGD("WeakAlgorithm: WEP AP(%{public}s) is ignored", networkCandidate.ToString().c_str());
415 networkCandidate.filtedReason[filterName].insert(FiltedReason::WEAK_ALGORITHM_WEP_SECURITY);
416 return false;
417 } else if (scanInfo.securityType == WifiSecurity::OPEN) {
418 WIFI_LOGD("WeakAlgorithm: OPEN AP(%{public}s) is ignored", networkCandidate.ToString().c_str());
419 networkCandidate.filtedReason[filterName].insert(FiltedReason::OPEN_NETWORK);
420 return false;
421 } else if (scanInfo.securityType == WifiSecurity::PSK
422 && scanInfo.capabilities.find("TKIP") != std::string::npos) {
423 if (scanInfo.capabilities.find("CCMP") != std::string::npos) {
424 return true;
425 }
426 WIFI_LOGD("WeakAlgorithm: WPA AP(%{public}s) is ignored", networkCandidate.ToString().c_str());
427 networkCandidate.filtedReason[filterName].insert(FiltedReason::WEAK_ALGORITHM_WPA_SECURITY);
428 return false;
429 }
430 return true;
431 }
432
NotCurrentNetworkFilter()433 NotCurrentNetworkFilter::NotCurrentNetworkFilter() : SimpleWifiFilter("NotCurrentNetwork") {}
434
~NotCurrentNetworkFilter()435 NotCurrentNetworkFilter::~NotCurrentNetworkFilter()
436 {
437 if (!filteredNetworkCandidates.empty()) {
438 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
439 filterName.c_str(),
440 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
441 }
442 }
443
Filter(NetworkCandidate & networkCandidate)444 bool NotCurrentNetworkFilter::Filter(NetworkCandidate &networkCandidate)
445 {
446 WifiLinkedInfo linkedInfo;
447 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
448 if (networkCandidate.interScanInfo.bssid == linkedInfo.bssid) {
449 WIFI_LOGI("NotCurrentNetworkFilter, same bssid:%{public}s",
450 networkCandidate.ToString().c_str());
451 networkCandidate.filtedReason[filterName].insert(FiltedReason::SAME_BSSID);
452 return false;
453 }
454
455 if ((networkCandidate.interScanInfo.ssid == linkedInfo.ssid) &&
456 NetworkSelectionUtils::IsConfigOpenOrEapType(networkCandidate)) {
457 WIFI_LOGI("NotCurrentNetworkFilter, same ssid and open or eap type:%{public}s",
458 networkCandidate.ToString().c_str());
459 networkCandidate.filtedReason[filterName].insert(FiltedReason::SAME_SSID_OPENOREAP);
460 return false;
461 }
462 return true;
463 }
464
SignalLevelFilter()465 SignalLevelFilter::SignalLevelFilter() : SimpleWifiFilter("SignalLevel") {}
466
~SignalLevelFilter()467 SignalLevelFilter::~SignalLevelFilter()
468 {
469 if (!filteredNetworkCandidates.empty()) {
470 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
471 filterName.c_str(),
472 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
473 }
474 }
475
Filter(NetworkCandidate & networkCandidate)476 bool SignalLevelFilter::Filter(NetworkCandidate &networkCandidate)
477 {
478 auto &interScanInfo = networkCandidate.interScanInfo;
479 int32_t signalLevel = WifiSettings::GetInstance().GetSignalLevel(interScanInfo.rssi, interScanInfo.band);
480 if (signalLevel > SIGNAL_LEVEL_TWO) {
481 return true;
482 }
483 networkCandidate.filtedReason[filterName].insert(FiltedReason::POOR_SIGNAL);
484 return false;
485 }
486
ValidNetworkIdFilter()487 ValidNetworkIdFilter::ValidNetworkIdFilter() : SimpleWifiFilter("ValidNetworkId") {}
488
~ValidNetworkIdFilter()489 ValidNetworkIdFilter::~ValidNetworkIdFilter()
490 {
491 if (!filteredNetworkCandidates.empty()) {
492 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
493 filterName.c_str(),
494 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
495 }
496 }
497
Filter(NetworkCandidate & networkCandidate)498 bool ValidNetworkIdFilter::Filter(NetworkCandidate &networkCandidate)
499 {
500 if (networkCandidate.wifiDeviceConfig.networkId != INVALID_NETWORK_ID) {
501 return true;
502 } else {
503 networkCandidate.filtedReason[filterName].insert(FiltedReason::NETWORK_ID_INVALID);
504 return false;
505 }
506 }
507
NotNetworkBlackListFilter()508 NotNetworkBlackListFilter::NotNetworkBlackListFilter() : SimpleWifiFilter("NotNetworkBlackList") {}
509
~NotNetworkBlackListFilter()510 NotNetworkBlackListFilter::~NotNetworkBlackListFilter()
511 {
512 if (!filteredNetworkCandidates.empty()) {
513 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
514 filterName.c_str(),
515 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
516 }
517 }
518
Filter(NetworkCandidate & networkCandidate)519 bool NotNetworkBlackListFilter::Filter(NetworkCandidate &networkCandidate)
520 {
521 if (NetworkBlockListManager::GetInstance().IsInAbnormalWifiBlocklist(networkCandidate.interScanInfo.bssid)) {
522 WIFI_LOGI("NotNetworkBlockListFilter, in abnormal wifi blocklist, skip candidate:%{public}s",
523 networkCandidate.ToString().c_str());
524 networkCandidate.filtedReason[filterName].insert(FiltedReason::NETWORK_STATUS_DISABLE);
525 return false;
526 }
527
528 WifiLinkedInfo linkedInfo;
529 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
530 int32_t curSignalLevel = WifiSettings::GetInstance().GetSignalLevel(linkedInfo.rssi, linkedInfo.band);
531 auto scanInfo = networkCandidate.interScanInfo;
532 int32_t targetSignalLevel = WifiSettings::GetInstance().GetSignalLevel(scanInfo.rssi, scanInfo.band);
533 if (NetworkBlockListManager::GetInstance().IsInWifiBlocklist(networkCandidate.interScanInfo.bssid) &&
534 (targetSignalLevel <= SIGNAL_LEVEL_THREE || targetSignalLevel - curSignalLevel < MIN_SIGNAL_LEVEL_INTERVAL)) {
535 if (linkedInfo.detailedState == DetailedState::NOTWORKING && targetSignalLevel >= SIGNAL_LEVEL_THREE) {
536 WIFI_LOGI("NotNetworkBlockListFilter, ignore blocklist, targetSignalLevel >= 3, candidate:%{public}s",
537 networkCandidate.ToString().c_str());
538 return true;
539 }
540 WIFI_LOGI("NotNetworkBlackListFilter, in wifi blocklist, targetSignalLevel:%{public}d, "
541 "curSignalLevel:%{public}d, skip candidate:%{public}s",
542 targetSignalLevel, curSignalLevel, networkCandidate.ToString().c_str());
543 networkCandidate.filtedReason[filterName].insert(FiltedReason::BLOCKLIST_AP);
544 return false;
545 }
546 return true;
547 }
548
NotP2pFreqAt5gFilter()549 NotP2pFreqAt5gFilter::NotP2pFreqAt5gFilter() : SimpleWifiFilter("NotP2pFreqAt5g") {}
550
~NotP2pFreqAt5gFilter()551 NotP2pFreqAt5gFilter::~NotP2pFreqAt5gFilter()
552 {
553 if (!filteredNetworkCandidates.empty()) {
554 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
555 filterName.c_str(),
556 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
557 }
558 }
559
Filter(NetworkCandidate & networkCandidate)560 bool NotP2pFreqAt5gFilter::Filter(NetworkCandidate &networkCandidate)
561 {
562 if (networkCandidate.interScanInfo.band == static_cast<int>(BandType::BAND_2GHZ)) {
563 return true;
564 }
565
566 Hid2dUpperScene softbusScene;
567 Hid2dUpperScene castScene;
568 Hid2dUpperScene shareScene;
569 Hid2dUpperScene mouseCrossScene;
570 Hid2dUpperScene miracastScene;
571 WifiP2pLinkedInfo linkedInfo;
572 WifiConfigCenter::GetInstance().GetHid2dUpperScene(SOFT_BUS_SERVICE_UID, softbusScene);
573 WifiConfigCenter::GetInstance().GetHid2dUpperScene(CAST_ENGINE_SERVICE_UID, castScene);
574 WifiConfigCenter::GetInstance().GetHid2dUpperScene(MIRACAST_SERVICE_UID, miracastScene);
575 WifiConfigCenter::GetInstance().GetHid2dUpperScene(SHARE_SERVICE_UID, shareScene);
576 WifiConfigCenter::GetInstance().GetHid2dUpperScene(MOUSE_CROSS_SERVICE_UID, mouseCrossScene);
577 WifiConfigCenter::GetInstance().GetP2pInfo(linkedInfo);
578 if (linkedInfo.GetConnectState() == P2pConnectedState::P2P_DISCONNECTED
579 && WifiConfigCenter::GetInstance().GetP2pEnhanceState() == 0) {
580 return true;
581 }
582 // scene bit 0-2 is valid, 0x01: video, 0x02: audio, 0x04: file,
583 // scene & 0x07 > 0 means one of them takes effect.
584 bool isCastScene = false;
585 if ((softbusScene.scene & 0x07) > 0 || (castScene.scene & 0x07) > 0 || (shareScene.scene & 0x07) > 0 ||
586 (mouseCrossScene.scene & 0x07) > 0 || (miracastScene.scene & 0x07) > 0) {
587 isCastScene = true;
588 }
589
590 if (!isCastScene) {
591 return true;
592 }
593
594 if (NetworkSelectionUtils::IsSameFreqAsP2p(networkCandidate)) {
595 return true;
596 }
597 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_P2P_FREQ_AT_5G);
598 return false;
599 }
600
ValidConfigNetworkFilter()601 ValidConfigNetworkFilter::ValidConfigNetworkFilter() : SimpleWifiFilter("ValidConfigNetwork") {}
602
~ValidConfigNetworkFilter()603 ValidConfigNetworkFilter::~ValidConfigNetworkFilter()
604 {
605 if (!filteredNetworkCandidates.empty()) {
606 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
607 filterName.c_str(),
608 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
609 }
610 }
611
Filter(NetworkCandidate & networkCandidate)612 bool ValidConfigNetworkFilter::Filter(NetworkCandidate &networkCandidate)
613 {
614 // no internet filtering
615 auto &wifiDeviceConfig = networkCandidate.wifiDeviceConfig;
616 if (wifiDeviceConfig.noInternetAccess) {
617 WIFI_LOGI("ValidConfigNetworkFilter, no internet access, skip candidate:%{public}s",
618 networkCandidate.ToString().c_str());
619 networkCandidate.filtedReason[filterName].insert(FiltedReason::NO_INTERNET);
620 return false;
621 }
622
623 // status history < 80%
624 if (!NetworkStatusHistoryManager::IsInternetAccessByHistory(wifiDeviceConfig.networkStatusHistory)) {
625 WIFI_LOGI("ValidConfigNetworkFilter, current network status history is %{public}s., skip : %{public}s",
626 NetworkStatusHistoryManager::ToString(wifiDeviceConfig.networkStatusHistory).c_str(),
627 networkCandidate.ToString().c_str());
628 return false;
629 }
630
631 // portal network filtering
632 if (networkCandidate.wifiDeviceConfig.isPortal) {
633 WIFI_LOGI("ValidConfigNetworkFilter, portal network, skip candidate:%{public}s",
634 networkCandidate.ToString().c_str());
635 networkCandidate.filtedReason[filterName].insert(FiltedReason::PORTAL_NETWORK);
636 return false;
637 }
638
639 // disable network filtering
640 auto &networkSelectionStatus = networkCandidate.wifiDeviceConfig.networkSelectionStatus;
641 if (networkSelectionStatus.status != WifiDeviceConfigStatus::ENABLED ||
642 !networkCandidate.wifiDeviceConfig.isAllowAutoConnect) {
643 WIFI_LOGI("ValidConfigNetworkFilter, disable network, skip candidate:%{public}s",
644 networkCandidate.ToString().c_str());
645 networkCandidate.filtedReason[filterName].insert(FiltedReason::NETWORK_STATUS_DISABLE);
646 return false;
647 }
648
649 // empty network status history
650 if (NetworkStatusHistoryManager::IsEmptyNetworkStatusHistory(wifiDeviceConfig.networkStatusHistory)) {
651 WIFI_LOGI("ValidConfigNetworkFilter, no network status history, skip candidate:%{public}s",
652 networkCandidate.ToString().c_str());
653 networkCandidate.filtedReason[filterName].insert(FiltedReason::EMPTY_HISTORY);
654 return false;
655 }
656
657 // maybe portal network filtering
658 if (NetworkSelectionUtils::IsScanResultForOweNetwork(networkCandidate) &&
659 NetworkSelectionUtils::IsOpenAndMaybePortal(networkCandidate)) {
660 WIFI_LOGI("ValidConfigNetworkFilter, maybe portal network, skip candidate:%{public}s",
661 networkCandidate.ToString().c_str());
662 networkCandidate.filtedReason[filterName].insert(FiltedReason::MAYBE_PORTAL_NETWORK);
663 return false;
664 }
665
666 return true;
667 }
668
WifiSwitchThresholdFilter()669 WifiSwitchThresholdFilter::WifiSwitchThresholdFilter() : SimpleWifiFilter("WifiSwitchThreshold") {}
670
~WifiSwitchThresholdFilter()671 WifiSwitchThresholdFilter::~WifiSwitchThresholdFilter()
672 {
673 if (!filteredNetworkCandidates.empty()) {
674 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
675 filterName.c_str(),
676 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
677 }
678 }
679
Filter(NetworkCandidate & networkCandidate)680 bool WifiSwitchThresholdFilter::Filter(NetworkCandidate &networkCandidate)
681 {
682 WifiLinkedInfo linkedInfo;
683 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
684 auto &interScanInfo = networkCandidate.interScanInfo;
685
686 if (interScanInfo.rssi - linkedInfo.rssi < MIN_RSSI_INTERVAL) {
687 WIFI_LOGI("WifiSwitchThresholdFilter, scan rssi:%{public}d, cur rssi:%{public}d, skip candidate:%{public}s",
688 interScanInfo.rssi, linkedInfo.rssi, networkCandidate.ToString().c_str());
689 networkCandidate.filtedReason[filterName].insert(FiltedReason::LESS_THAN_8RSSI);
690 return false;
691 }
692
693 return true;
694 }
695
SuggestionNetworkWifiFilter()696 SuggestionNetworkWifiFilter::SuggestionNetworkWifiFilter() : SimpleWifiFilter("suggestionNetworkWifiFilter") {}
697
~SuggestionNetworkWifiFilter()698 SuggestionNetworkWifiFilter::~SuggestionNetworkWifiFilter()
699 {
700 if (!filteredNetworkCandidates.empty()) {
701 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
702 filterName.c_str(),
703 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
704 }
705 }
706
Filter(NetworkCandidate & networkCandidate)707 bool SuggestionNetworkWifiFilter::Filter(NetworkCandidate &networkCandidate)
708 {
709 return networkCandidate.wifiDeviceConfig.uid != WIFI_INVALID_UID &&
710 networkCandidate.wifiDeviceConfig.isShared == false;
711 }
712
WifiSwitchThresholdQoeFilter()713 WifiSwitchThresholdQoeFilter::WifiSwitchThresholdQoeFilter() : SimpleWifiFilter("WifiSwitchThresholdQoeFilter") {}
714
~WifiSwitchThresholdQoeFilter()715 WifiSwitchThresholdQoeFilter::~WifiSwitchThresholdQoeFilter()
716 {
717 if (!filteredNetworkCandidates.empty()) {
718 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
719 filterName.c_str(),
720 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
721 }
722 }
723
Filter(NetworkCandidate & networkCandidate)724 bool WifiSwitchThresholdQoeFilter::Filter(NetworkCandidate &networkCandidate)
725 {
726 WifiLinkedInfo linkedInfo;
727 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
728 auto &interScanInfo = networkCandidate.interScanInfo;
729
730 if (interScanInfo.rssi < linkedInfo.rssi) {
731 WIFI_LOGI("WifiSwitchThresholdFilter, scan info rssi:%{public}d,"
732 "cur rssi:%{public}d, skip candidate:%{public}s",
733 interScanInfo.rssi, linkedInfo.rssi, networkCandidate.ToString().c_str());
734 return false;
735 }
736
737 return true;
738 }
739
WifiSwitch5GNot2GFilter()740 WifiSwitch5GNot2GFilter::WifiSwitch5GNot2GFilter() : SimpleWifiFilter("WifiSwitch5GNot2GFilter") {}
741
~WifiSwitch5GNot2GFilter()742 WifiSwitch5GNot2GFilter::~WifiSwitch5GNot2GFilter()
743 {
744 if (!filteredNetworkCandidates.empty()) {
745 WIFI_LOGI("filteredNetworkCandidates in %{public}s: %{public}s",
746 filterName.c_str(),
747 NetworkSelectionUtils::GetNetworkCandidatesInfo(filteredNetworkCandidates).c_str());
748 }
749 }
750
Filter(NetworkCandidate & networkCandidate)751 bool WifiSwitch5GNot2GFilter::Filter(NetworkCandidate &networkCandidate)
752 {
753 WifiLinkedInfo linkedInfo;
754 WifiConfigCenter::GetInstance().GetLinkedInfo(linkedInfo);
755 auto &interScanInfo = networkCandidate.interScanInfo;
756
757 if (linkedInfo.band == static_cast<int>(BandType::BAND_5GHZ) &&
758 interScanInfo.band == static_cast<int>(BandType::BAND_2GHZ)) {
759 return false;
760 }
761
762 return true;
763 }
764 }