1 /*
2 * Copyright (C) 2021-2023 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 "network_selection_utils.h"
17 #include "network_status_history_manager.h"
18 #include "wifi_common_util.h"
19 #include "wifi_config_center.h"
20 #include "wifi_p2p_msg.h"
21 #include "wifi_logger.h"
22 #ifndef OHOS_ARCH_LITE
23 #include "block_connect_service.h"
24 #endif
25 #ifdef FEATURE_ITNETWORK_PREFERRED_SUPPORT
26 #include "parameter.h"
27 #endif
28
29 namespace OHOS::Wifi::NetworkSelection {
30 namespace {
31 constexpr int32_t SCAN_5GHZ_BAND = 2;
32 }
33
34 DEFINE_WIFILOG_LABEL("NetworkSelectionUtils")
35
IsOpenNetwork(const NetworkCandidate & networkCandidate)36 bool NetworkSelectionUtils::IsOpenNetwork(const NetworkCandidate &networkCandidate)
37 {
38 return networkCandidate.wifiDeviceConfig.keyMgmt == KEY_MGMT_NONE;
39 };
40
IsOpenAndMaybePortal(NetworkCandidate & networkCandidate,const std::string & filterName)41 bool NetworkSelectionUtils::IsOpenAndMaybePortal(NetworkCandidate &networkCandidate,
42 const std::string &filterName)
43 {
44 auto &wifiDeviceConfig = networkCandidate.wifiDeviceConfig;
45 if (!IsOpenNetwork(networkCandidate)) {
46 networkCandidate.filtedReason[filterName].insert(FiltedReason::NOT_OPEN_NETWORK);
47 return false;
48 }
49 if (wifiDeviceConfig.noInternetAccess) {
50 networkCandidate.filtedReason[filterName].insert(FiltedReason::NO_INTERNET);
51 return false;
52 }
53 if (!NetworkStatusHistoryManager::IsEmptyNetworkStatusHistory(wifiDeviceConfig.networkStatusHistory)) {
54 networkCandidate.filtedReason[filterName].insert(FiltedReason::HAS_NETWORK_HISTORY);
55 return false;
56 }
57 return true;
58 }
59
IsScanResultForOweNetwork(const NetworkCandidate & networkCandidate)60 bool NetworkSelectionUtils::IsScanResultForOweNetwork(const NetworkCandidate &networkCandidate)
61 {
62 return networkCandidate.interScanInfo.capabilities.find("OWE") != std::string::npos;
63 }
64
IsBlackListNetwork(const NetworkCandidate & networkCandidate)65 bool NetworkSelectionUtils::IsBlackListNetwork(const NetworkCandidate &networkCandidate)
66 {
67 #ifndef OHOS_ARCH_LITE
68 return BlockConnectService::GetInstance().IsBssidMatchUnusableSet(networkCandidate.interScanInfo.bssid);
69 #else
70 return false;
71 #endif
72 }
73
GetNetworkCandidatesInfo(const std::vector<NetworkCandidate * > & networkCandidates,const std::string & filterName)74 std::string NetworkSelectionUtils::GetNetworkCandidatesInfo(const std::vector<NetworkCandidate*> &networkCandidates,
75 const std::string &filterName)
76 {
77 std::stringstream networkCandidatesInfo;
78 networkCandidatesInfo << "[";
79 for (std::size_t i = 0; i < networkCandidates.size(); i++) {
80 if (networkCandidates.at(i)->wifiDeviceConfig.networkId == INVALID_NETWORK_ID) {
81 continue;
82 }
83 networkCandidatesInfo << "\"" << networkCandidates.at(i)->ToString(filterName) << "\"";
84 if (i < networkCandidates.size() - 1) {
85 networkCandidatesInfo << ", ";
86 }
87 }
88 networkCandidatesInfo << "]";
89 return networkCandidatesInfo.str();
90 }
91
GetScoreResultsInfo(const std::vector<ScoreResult> & scoreResults)92 std::string NetworkSelectionUtils::GetScoreResultsInfo(const std::vector<ScoreResult> &scoreResults)
93 {
94 std::stringstream scoreMsg;
95 scoreMsg << "[ ";
96 for (std::size_t i = 0; i < scoreResults.size(); i++) {
97 scoreMsg << scoreResults.at(i).ToString();
98 if (i < scoreResults.size() - 1) {
99 scoreMsg << ", ";
100 }
101 }
102 scoreMsg << " ]";
103 return scoreMsg.str();
104 }
105
IsConfigOpenType(const NetworkCandidate & networkCandidate)106 bool NetworkSelectionUtils::IsConfigOpenType(const NetworkCandidate &networkCandidate)
107 {
108 return (IsOpenNetwork(networkCandidate) || NetworkSelectionUtils::IsScanResultForOweNetwork(networkCandidate)) &&
109 !HasWepKeys(networkCandidate.wifiDeviceConfig);
110 }
111
HasWepKeys(const WifiDeviceConfig & wifiConfig)112 bool NetworkSelectionUtils::HasWepKeys(const WifiDeviceConfig &wifiConfig)
113 {
114 for (int32_t i = 0; i < WEPKEYS_SIZE; i++) {
115 if (!wifiConfig.wepKeys[i].empty()) {
116 return true;
117 }
118 }
119 return false;
120 }
121
IsEnterprise(const NetworkCandidate & networkCandidate)122 bool NetworkSelectionUtils::IsEnterprise(const NetworkCandidate &networkCandidate)
123 {
124 auto &keyMgmt = networkCandidate.wifiDeviceConfig.keyMgmt;
125 bool isEnterpriseSecurityType = (keyMgmt == KEY_MGMT_EAP) || (keyMgmt == KEY_MGMT_SUITE_B_192) ||
126 (keyMgmt == KEY_MGMT_WAPI_CERT);
127 auto &eap = networkCandidate.wifiDeviceConfig.wifiEapConfig.eap;
128 return isEnterpriseSecurityType && (eap != EAP_METHOD_NONE);
129 }
130
IsConfigOpenOrEapType(const NetworkCandidate & networkCandidate)131 bool NetworkSelectionUtils::IsConfigOpenOrEapType(const NetworkCandidate &networkCandidate)
132 {
133 return IsConfigOpenType(networkCandidate) || IsEnterprise(networkCandidate);
134 }
135
IsSameFreqAsP2p(const NetworkCandidate & networkCandidate)136 bool NetworkSelectionUtils::IsSameFreqAsP2p(const NetworkCandidate &networkCandidate)
137 {
138 WifiP2pGroupInfo group = WifiConfigCenter::GetInstance().GetCurrentP2pGroupInfo();
139 auto &interScanInfo = networkCandidate.interScanInfo;
140 int32_t p2pFrequency = group.GetFrequency();
141 if (interScanInfo.band == SCAN_5GHZ_BAND && p2pFrequency == interScanInfo.frequency) {
142 return true;
143 } else {
144 WIFI_LOGI("IsSameFreqAsP2p, p2p frequency:%{public}d and scanInfo frequency:%{public}d are not same",
145 p2pFrequency, interScanInfo.frequency);
146 }
147 return false;
148 }
149 }
150