1 /*
2 * Copyright (C) 2021-2022 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 #include "wifi_global_func.h"
16 #include <algorithm>
17 #include "wifi_log.h"
18
19 #undef LOG_TAG
20 #define LOG_TAG "WifiGlobalFunc"
21
22 namespace OHOS {
23 namespace Wifi {
24 constexpr int FREP_2G_MIN = 2412;
25 constexpr int FREP_2G_MAX = 2472;
26
27 constexpr int FREP_5G_MIN = 5170;
28 constexpr int FREP_5G_MAX = 5825;
29 constexpr int CHANNEL_14_FREP = 2484;
30 constexpr int CHANNEL_14 = 14;
31 constexpr int CENTER_FREP_DIFF = 5;
32 constexpr int CHANNEL_2G_MIN = 1;
33 constexpr int CHANNEL_5G_MIN = 34;
34
CfgCheckSsid(const HotspotConfig & cfg)35 ErrCode CfgCheckSsid(const HotspotConfig &cfg)
36 {
37 if (cfg.GetSsid().length() < MIN_SSID_LEN || cfg.GetSsid().length() > MAX_SSID_LEN) {
38 LOGE("Config ssid length is invalid!");
39 return ErrCode::WIFI_OPT_INVALID_PARAM;
40 }
41 return ErrCode::WIFI_OPT_SUCCESS;
42 }
43
CfgCheckPsk(const HotspotConfig & cfg)44 ErrCode CfgCheckPsk(const HotspotConfig &cfg)
45 {
46 size_t len = cfg.GetPreSharedKey().length();
47 if (len < MIN_PSK_LEN || len > MAX_PSK_LEN) {
48 LOGE("PreSharedKey length error! invalid len: %{public}zu", len);
49 return ErrCode::WIFI_OPT_INVALID_PARAM;
50 }
51 return ErrCode::WIFI_OPT_SUCCESS;
52 }
53
CfgCheckBand(const HotspotConfig & cfg,std::vector<BandType> & bandsFromCenter)54 ErrCode CfgCheckBand(const HotspotConfig &cfg, std::vector<BandType> &bandsFromCenter)
55 {
56 for (auto it = bandsFromCenter.begin(); it != bandsFromCenter.end(); ++it) {
57 if (cfg.GetBand() == *it) {
58 return ErrCode::WIFI_OPT_SUCCESS;
59 }
60 }
61 LOGE("Hotspot config band is invalid!");
62 return ErrCode::WIFI_OPT_INVALID_PARAM;
63 }
64
CfgCheckChannel(const HotspotConfig & cfg,ChannelsTable & channInfoFromCenter)65 ErrCode CfgCheckChannel(const HotspotConfig &cfg, ChannelsTable &channInfoFromCenter)
66 {
67 std::vector<int32_t> channels = channInfoFromCenter[static_cast<BandType>(cfg.GetBand())];
68 auto it = find(channels.begin(), channels.end(), cfg.GetChannel());
69 return ((it == channels.end()) ? ErrCode::WIFI_OPT_INVALID_PARAM : ErrCode::WIFI_OPT_SUCCESS);
70 }
71
IsValidHotspotConfig(const HotspotConfig & cfg,const HotspotConfig & cfgFromCenter,std::vector<BandType> & bandsFromCenter,ChannelsTable & channInfoFromCenter)72 ErrCode IsValidHotspotConfig(const HotspotConfig &cfg, const HotspotConfig &cfgFromCenter,
73 std::vector<BandType> &bandsFromCenter, ChannelsTable &channInfoFromCenter)
74 {
75 if (CfgCheckSsid(cfg) == ErrCode::WIFI_OPT_INVALID_PARAM) {
76 return ErrCode::WIFI_OPT_INVALID_PARAM;
77 }
78
79 if (cfg.GetSecurityType() == KeyMgmt::NONE) {
80 if (cfg.GetPreSharedKey().length() > 0) {
81 LOGE("Open hotspot PreSharedKey length is non-zero error!");
82 return ErrCode::WIFI_OPT_INVALID_PARAM;
83 }
84 } else if (cfg.GetSecurityType() == KeyMgmt::WPA_PSK || cfg.GetSecurityType() == KeyMgmt::WPA2_PSK) {
85 if (CfgCheckPsk(cfg) == ErrCode::WIFI_OPT_INVALID_PARAM) {
86 return ErrCode::WIFI_OPT_INVALID_PARAM;
87 }
88 } else {
89 LOGE("Hotspot securityType is not supported!");
90 return ErrCode::WIFI_OPT_INVALID_PARAM;
91 }
92
93 if (cfg.GetBand() != cfgFromCenter.GetBand()) {
94 if (CfgCheckBand(cfg, bandsFromCenter) == ErrCode::WIFI_OPT_INVALID_PARAM) {
95 return ErrCode::WIFI_OPT_INVALID_PARAM;
96 }
97 }
98
99 LOGD("Config channel is: %{public}d", cfg.GetChannel());
100 if (cfg.GetChannel() != cfgFromCenter.GetChannel()) {
101 if (CfgCheckChannel(cfg, channInfoFromCenter) == ErrCode::WIFI_OPT_INVALID_PARAM) {
102 LOGE("Config channel is invalid!");
103 return ErrCode::WIFI_OPT_INVALID_PARAM;
104 }
105 }
106
107 return ErrCode::WIFI_OPT_SUCCESS;
108 }
109
GetRandomStr(int len)110 std::string GetRandomStr(int len)
111 {
112 std::random_device rd;
113 std::string res;
114 char rndbuf[MAX_PSK_LEN + 1] = {0};
115 int rndnum;
116 if (len > MAX_PSK_LEN) {
117 len = MAX_PSK_LEN;
118 }
119 for (int n = 0; n < len; ++n) {
120 rndnum = std::abs((int)rd());
121 switch (rndnum % HEX_TYPE_LEN) {
122 case 0:
123 rndbuf[n] = ((rndnum % ('z' - 'a' + 1)) + 'a');
124 break;
125 case 1:
126 rndbuf[n] = ((rndnum % ('Z' - 'A' + 1)) + 'A');
127 break;
128 default:
129 rndbuf[n] = ((rndnum % ('9' - '0' + 1)) + '0');
130 break;
131 }
132 }
133 res = rndbuf;
134 return res;
135 }
136
IsAllowScanAnyTime(const ScanControlInfo & info)137 bool IsAllowScanAnyTime(const ScanControlInfo &info)
138 {
139 for (auto forbidIter = info.scanForbidList.begin(); forbidIter != info.scanForbidList.end(); forbidIter++) {
140 if (forbidIter->scanMode == ScanMode::ANYTIME_SCAN && forbidIter->scanScene == SCAN_SCENE_ALL) {
141 return false;
142 }
143 }
144 return true;
145 }
146
ConvertConnStateInternal(OperateResState resState,bool & isReport)147 ConnState ConvertConnStateInternal(OperateResState resState, bool &isReport)
148 {
149 switch (resState) {
150 case OperateResState::CONNECT_CONNECTING:
151 isReport = true;
152 return ConnState::CONNECTING;
153 case OperateResState::CONNECT_AP_CONNECTED:
154 isReport = true;
155 return ConnState::CONNECTED;
156 case OperateResState::CONNECT_NETWORK_ENABLED:
157 isReport = false;
158 return ConnState::UNKNOWN;
159 case OperateResState::CONNECT_NETWORK_DISABLED:
160 isReport = false;
161 return ConnState::UNKNOWN;
162 case OperateResState::DISCONNECT_DISCONNECTING:
163 isReport = true;
164 return ConnState::DISCONNECTING;
165 case OperateResState::DISCONNECT_DISCONNECTED:
166 isReport = true;
167 return ConnState::DISCONNECTED;
168 case OperateResState::CONNECT_PASSWORD_WRONG:
169 isReport = false;
170 return ConnState::UNKNOWN;
171 case OperateResState::CONNECT_CONNECTION_FULL:
172 isReport = false;
173 return ConnState::UNKNOWN;
174 case OperateResState::CONNECT_CONNECTION_REJECT:
175 isReport = false;
176 return ConnState::UNKNOWN;
177 case OperateResState::CONNECT_CONNECTING_TIMEOUT:
178 isReport = false;
179 return ConnState::UNKNOWN;
180 case OperateResState::CONNECT_OBTAINING_IP:
181 isReport = true;
182 return ConnState::OBTAINING_IPADDR;
183 case OperateResState::CONNECT_OBTAINING_IP_FAILED:
184 isReport = false;
185 return ConnState::UNKNOWN;
186 case OperateResState::CONNECT_ASSOCIATING:
187 isReport = false;
188 return ConnState::UNKNOWN;
189 case OperateResState::CONNECT_ASSOCIATED:
190 isReport = false;
191 return ConnState::UNKNOWN;
192 default:
193 isReport = true;
194 return ConnState::UNKNOWN;
195 }
196 }
197
IsValidHexCharAndConvert(char c)198 static int8_t IsValidHexCharAndConvert(char c)
199 {
200 if (c >= '0' && c <= '9') {
201 return c - '0';
202 }
203 if (c >= 'a' && c <= 'f') {
204 return c - 'a' + ('9' - '0' + 1);
205 }
206 if (c >= 'A' && c <= 'F') {
207 return c - 'A' + ('9' - '0' + 1);
208 }
209 return -1;
210 }
211
CheckMacIsValid(const std::string & macStr)212 int CheckMacIsValid(const std::string &macStr)
213 {
214 if (macStr.length() != MAC_STRING_SIZE) {
215 return -1;
216 }
217 /* Verification format */
218 for (int i = 0, j = 0; i < MAC_STRING_SIZE; ++i) {
219 if (j == 0 || j == 1) {
220 int8_t v = IsValidHexCharAndConvert(macStr[i]);
221 if (v < 0) {
222 return -1;
223 }
224 ++j;
225 } else {
226 if (macStr[i] != ':') {
227 return -1;
228 }
229 j = 0;
230 }
231 }
232 return 0;
233 }
234
SplitString(const std::string & str,const std::string & split,std::vector<std::string> & vec)235 void SplitString(const std::string &str, const std::string &split, std::vector<std::string> &vec)
236 {
237 if (split.empty()) {
238 vec.push_back(str);
239 return;
240 }
241 std::string::size_type begPos = 0;
242 std::string::size_type endPos = 0;
243 std::string tmpStr;
244 while ((endPos = str.find(split, begPos)) != std::string::npos) {
245 if (endPos > begPos) {
246 tmpStr = str.substr(begPos, endPos - begPos);
247 vec.push_back(tmpStr);
248 }
249 begPos = endPos + split.size();
250 }
251 tmpStr = str.substr(begPos);
252 if (!tmpStr.empty()) {
253 vec.push_back(tmpStr);
254 }
255 return;
256 }
257
Vec2Stream(const std::string & prefix,const std::vector<char> & vecChar,const std::string & sufffix)258 std::string Vec2Stream(const std::string &prefix, const std::vector<char> &vecChar, const std::string &sufffix)
259 {
260 std::ostringstream ss;
261 constexpr int hexCharLen = 2;
262 ss << prefix;
263 int temp = 0;
264 for (std::size_t i = 0; i < vecChar.size(); i++) {
265 temp = (unsigned char)(vecChar[i]);
266 ss << std::setfill('0') << std::setw(hexCharLen) << std::hex << std::uppercase << temp << " ";
267 }
268 ss << sufffix;
269 return ss.str();
270 }
271
HexStringToVec(const std::string & str,std::vector<char> & vec)272 int HexStringToVec(const std::string &str, std::vector<char> &vec)
273 {
274 unsigned len = str.length();
275 if ((len & 1) != 0) {
276 return -1;
277 }
278 const int hexShiftNum = 4;
279 for (unsigned i = 0; i + 1 < len; ++i) {
280 int8_t high = IsValidHexCharAndConvert(str[i]);
281 int8_t low = IsValidHexCharAndConvert(str[++i]);
282 if (high < 0 || low < 0) {
283 return -1;
284 }
285 char tmp = ((high << hexShiftNum) | (low & 0x0F));
286 vec.push_back(tmp);
287 }
288 return 0;
289 }
290
HexStringToVec(const std::string & str,uint8_t plainText[],int plainLength,int & resultLength)291 int HexStringToVec(const std::string &str, uint8_t plainText[], int plainLength, int &resultLength)
292 {
293 if (plainLength < 0) {
294 return false;
295 }
296
297 std::vector<char> result;
298 result.clear();
299 int ret = HexStringToVec(str, result);
300 if (ret == -1 || static_cast<int>(result.size()) > plainLength) {
301 return -1;
302 }
303 for (std::vector<char>::size_type i = 0; i < result.size(); ++i) {
304 plainText[i] = result[i];
305 }
306 resultLength = static_cast<int>(result.size());
307 return 0;
308 }
309
ConvertArrayChar(uint8_t ch)310 static char ConvertArrayChar(uint8_t ch)
311 {
312 constexpr int maxDecNum = 9;
313 constexpr int numDiffForHexAlphabet = 10;
314 if (ch >= 0 && ch <= maxDecNum) {
315 return '0' + ch;
316 }
317 if (ch >= 0xa && ch <= 0xf) {
318 return ch + 'a' - numDiffForHexAlphabet;
319 }
320 return '0';
321 }
322
ConvertArrayToHex(const uint8_t plainText[],int size)323 std::string ConvertArrayToHex(const uint8_t plainText[], int size)
324 {
325 constexpr int bitWidth = 4;
326 std::stringstream ss;
327 for (int i = 0; i < size; i++) {
328 ss << ConvertArrayChar(plainText[i] >> bitWidth) << ConvertArrayChar (plainText[i] & 0xf);
329 }
330 return ss.str();
331 }
332
ValidateChar(const char ch)333 static bool ValidateChar(const char ch)
334 {
335 if (ch > '~' || ch < ' ') {
336 return false;
337 }
338 return true;
339 }
340
ValidateString(const std::string & str)341 std::string ValidateString(const std::string &str)
342 {
343 std::stringstream ss;
344 ss << "\"";
345 for (char ch : str) {
346 if (ValidateChar(ch)) {
347 ss << ch;
348 }
349 }
350 ss << "\"";
351 return ss.str();
352 }
353
TransformFrequencyIntoChannel(const std::vector<int> & freqVector,std::vector<int> & chanVector)354 void TransformFrequencyIntoChannel(const std::vector<int> &freqVector, std::vector<int> &chanVector)
355 {
356 int channel;
357 for (size_t i = 0; i < freqVector.size(); ++i) {
358 if (freqVector[i] >= FREP_2G_MIN && freqVector[i] <= FREP_2G_MAX) {
359 channel = (freqVector[i] - FREP_2G_MIN) / CENTER_FREP_DIFF + CHANNEL_2G_MIN;
360 } else if (freqVector[i] == CHANNEL_14_FREP) {
361 channel = CHANNEL_14;
362 } else if (freqVector[i] >= FREP_5G_MIN && freqVector[i] <= FREP_5G_MAX) {
363 channel = (freqVector[i] - FREP_5G_MIN) / CENTER_FREP_DIFF + CHANNEL_5G_MIN;
364 } else {
365 LOGW("Invalid Freq:%d", freqVector[i]);
366 continue;
367 }
368 chanVector.push_back(channel);
369 }
370 }
371
IsValid24GHz(int freq)372 bool IsValid24GHz(int freq)
373 {
374 return freq > 2400 && freq < 2500;
375 }
376
IsValid5GHz(int freq)377 bool IsValid5GHz(int freq)
378 {
379 return freq > 4900 && freq < 5900;
380 }
381 } // namespace Wifi
382 } // namespace OHOS
383