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 <cstdlib>
18 #include <iostream>
19 #include <sstream>
20 #include <random>
21 #include "wifi_common_util.h"
22 #include "wifi_log.h"
23 #ifndef OHOS_ARCH_LITE
24 #include "cJSON.h"
25 #include "wifi_country_code_define.h"
26 #endif
27 #ifdef INIT_LIB_ENABLE
28 #include "parameter.h"
29 #endif
30 #undef LOG_TAG
31 #define LOG_TAG "WifiGlobalFunc"
32
33 namespace OHOS {
34 namespace Wifi {
35 constexpr int ASCALL_NUM_START_INDEX = 48; // the range of numbers in the ascll table
36 constexpr int ASCALL_NUM_END_INDEX = 57;
37 constexpr int FREP_2G_MIN = 2412;
38 constexpr int FREP_2G_MAX = 2472;
39 constexpr int FREP_5G_MIN = 5170;
40 constexpr int FREP_5G_MAX = 5825;
41 constexpr int CHANNEL_14_FREP = 2484;
42 constexpr int CHANNEL_14 = 14;
43 constexpr int CENTER_FREP_DIFF = 5;
44 constexpr int CHANNEL_2G_MIN = 1;
45 constexpr int CHANNEL_2G_MAX = 14; // 2484
46 constexpr int CHANNEL_5G_MIN = 34;
47 constexpr int CHANNEL_5G_MAX = 165; // 5825
48 constexpr int PROP_FACTORY_RUN_MODE_LEN = 10;
49 constexpr int FACTORY_MODE_LEN = 7;
50 constexpr const char* FACTORY_RUN_MODE = "const.runmode";
51 constexpr const char* FACTORY_MODE = "factory";
52 constexpr const char* FACTORY_MODE_DEFAULT = "0";
53 constexpr int PROP_STARTUP_WIFI_ENABLE_LEN = 16;
54 constexpr int STARTUP_WIFI_ENABLE_LEN = 4;
55 constexpr const char* PROP_STARTUP_WIFI_ENABLE = "const.wifi.startup_wifi_enable";
56 constexpr const char* DEFAULT_STARTUP_WIFI_ENABLE = "false";
57 constexpr const char* STARTUP_WIFI_ENABLE = "true";
58 constexpr int PROP_PRODUCT_DEVICE_TYPE_LEN = 30;
59 constexpr int PRODUCT_DEVICE_TYPE_LEN = 5;
60 constexpr const char* PRODUCT_DEVICE_TYPE = "const.product.devicetype";
61 constexpr const char* DEFAULT_PRODUCT_DEVICE_TYPE = "default";
62 constexpr const char* PHONE_PRODUCT_DEVICE_TYPE = "phone";
63 constexpr const char* WEARABLE_PRODUCT_DEVICE_TYPE = "wearable";
64 constexpr const char* TABLET_PRODUCT_DEVICE_TYPE = "table";
65 constexpr const char* TV_PRODUCT_DEVICE_TYPE = "tv";
66 constexpr const char* PC_PRODUCT_DEVICE_TYPE = "2in1";
67 constexpr const char* VENDOR_COUNTRY_KEY = "const.cust.custPath";
68 constexpr const char* VENDOR_COUNTRY_DEFAULT = "";
69 constexpr const int32_t SYS_PARAMETER_SIZE = 256;
70 constexpr const int32_t PARAMETER_ERROR_CODE = 0;
71
72 constexpr int PROP_FSS_ENABLE_LEN = 16;
73 constexpr int FSS_ENABLE_LEN = 4;
74 constexpr const char* PROP_FSS_ENABLE = "const.wifi.hw_fss_enable";
75 constexpr const char* DEFAULT_FSS_ENABLE = "false";
76 constexpr const char* FSS_ENABLE = "true";
77 #ifndef INIT_LIB_ENABLE
78 constexpr int EC_INVALID = -9; // using sysparam_errno.h, invalid param value
79 #endif
80 constexpr int ASCALL_MINUS_SIGN_INDEX = 45;
81
GetRandomStr(int len)82 std::string GetRandomStr(int len)
83 {
84 std::random_device rd;
85 std::string res;
86 char rndbuf[MAX_PSK_LEN + 1] = {0};
87 int rndnum;
88 if (len > MAX_PSK_LEN) {
89 len = MAX_PSK_LEN;
90 }
91 for (int n = 0; n < len; ++n) {
92 rndnum = std::abs((int)rd());
93 switch (rndnum % HEX_TYPE_LEN) {
94 case 0:
95 rndbuf[n] = ((rndnum % ('z' - 'a' + 1)) + 'a');
96 break;
97 case 1:
98 rndbuf[n] = ((rndnum % ('Z' - 'A' + 1)) + 'A');
99 break;
100 default:
101 rndbuf[n] = ((rndnum % ('9' - '0' + 1)) + '0');
102 break;
103 }
104 }
105 res = rndbuf;
106 return res;
107 }
108
GetRandomInt(int start,int end)109 int GetRandomInt(int start, int end)
110 {
111 if (end <= start) {
112 return start;
113 }
114 std::random_device rd;
115 std::mt19937 e{rd()};
116 std::uniform_int_distribution<int> dist{start, end};
117 return dist(e);
118 }
119
IsAllowScanAnyTime(const ScanControlInfo & info)120 bool IsAllowScanAnyTime(const ScanControlInfo &info)
121 {
122 for (auto forbidIter = info.scanForbidList.begin(); forbidIter != info.scanForbidList.end(); forbidIter++) {
123 if (forbidIter->scanMode == ScanMode::ANYTIME_SCAN && forbidIter->scanScene == SCAN_SCENE_ALL) {
124 return false;
125 }
126 }
127 return true;
128 }
129
SplitStringToIntVector(const std::string & str,const std::string & split)130 std::vector<int> SplitStringToIntVector(const std::string &str, const std::string &split)
131 {
132 std::vector<int> res;
133 if (str.empty() || split.empty()) {
134 return res;
135 }
136 std::string::size_type begPos = 0;
137 std::string::size_type endPos = 0;
138 std::string tmpStr;
139 while ((endPos = str.find(split, begPos)) != std::string::npos) {
140 if (endPos > begPos) {
141 tmpStr = str.substr(begPos, endPos - begPos);
142 if (IsValidateNum(tmpStr)) {
143 res.push_back(CheckDataLegal(tmpStr));
144 }
145 }
146 begPos = endPos + split.size();
147 }
148 tmpStr = str.substr(begPos);
149 if (!tmpStr.empty() && IsValidateNum(tmpStr)) {
150 res.push_back(CheckDataLegal(tmpStr));
151 }
152 return res;
153 }
154
ConvertConnStateInternalExtral(OperateResState resState,bool & isReport)155 ConnState ConvertConnStateInternalExtral(OperateResState resState, bool &isReport)
156 {
157 switch (resState) {
158 case OperateResState::CONNECT_EMLSR_START:
159 case OperateResState::CONNECT_EMLSR_END:
160 isReport = false;
161 return ConnState::UNKNOWN;
162 default:
163 isReport = true;
164 return ConnState::UNKNOWN;
165 }
166 }
167
ConvertConnStateInternal(OperateResState resState,bool & isReport)168 ConnState ConvertConnStateInternal(OperateResState resState, bool &isReport)
169 {
170 switch (resState) {
171 case OperateResState::CONNECT_CONNECTING:
172 isReport = true;
173 return ConnState::CONNECTING;
174 case OperateResState::SPECIAL_CONNECTED:
175 isReport = true;
176 return ConnState::SPECIAL_CONNECT;
177 case OperateResState::CONNECT_AP_CONNECTED:
178 isReport = true;
179 return ConnState::CONNECTED;
180 case OperateResState::CONNECT_NETWORK_ENABLED:
181 case OperateResState::CONNECT_CHECK_PORTAL:
182 isReport = false;
183 return ConnState::UNKNOWN;
184 case OperateResState::CONNECT_NETWORK_DISABLED:
185 isReport = false;
186 return ConnState::UNKNOWN;
187 case OperateResState::DISCONNECT_DISCONNECTING:
188 isReport = true;
189 return ConnState::DISCONNECTING;
190 case OperateResState::DISCONNECT_DISCONNECTED:
191 isReport = true;
192 return ConnState::DISCONNECTED;
193 case OperateResState::CONNECT_PASSWORD_WRONG:
194 isReport = false;
195 return ConnState::UNKNOWN;
196 case OperateResState::CONNECT_CONNECTION_FULL:
197 isReport = false;
198 return ConnState::UNKNOWN;
199 case OperateResState::CONNECT_CONNECTION_REJECT:
200 isReport = false;
201 return ConnState::UNKNOWN;
202 case OperateResState::CONNECT_CONNECTING_TIMEOUT:
203 isReport = false;
204 return ConnState::UNKNOWN;
205 case OperateResState::CONNECT_OBTAINING_IP:
206 isReport = true;
207 return ConnState::OBTAINING_IPADDR;
208 case OperateResState::CONNECT_OBTAINING_IP_FAILED:
209 case OperateResState::CONNECT_ASSOCIATING:
210 case OperateResState::CONNECT_ASSOCIATED:
211 isReport = false;
212 return ConnState::UNKNOWN;
213 default:
214 return ConvertConnStateInternalExtral(resState, isReport);
215 }
216 }
217
IsValidHexCharAndConvert(char c)218 static int8_t IsValidHexCharAndConvert(char c)
219 {
220 if (c >= '0' && c <= '9') {
221 return c - '0';
222 }
223 if (c >= 'a' && c <= 'f') {
224 return c - 'a' + ('9' - '0' + 1);
225 }
226 if (c >= 'A' && c <= 'F') {
227 return c - 'A' + ('9' - '0' + 1);
228 }
229 return -1;
230 }
231
CheckMacIsValid(const std::string & macStr)232 int CheckMacIsValid(const std::string &macStr)
233 {
234 if (macStr.length() != MAC_STRING_SIZE) {
235 return -1;
236 }
237 /* Verification format */
238 for (int i = 0, j = 0; i < MAC_STRING_SIZE; ++i) {
239 if (j == 0 || j == 1) {
240 int8_t v = IsValidHexCharAndConvert(macStr[i]);
241 if (v < 0) {
242 return -1;
243 }
244 ++j;
245 } else {
246 if (macStr[i] != ':') {
247 return -1;
248 }
249 j = 0;
250 }
251 }
252 return 0;
253 }
254
SplitString(const std::string & str,const std::string & split,std::vector<std::string> & vec)255 void SplitString(const std::string &str, const std::string &split, std::vector<std::string> &vec)
256 {
257 if (split.empty()) {
258 vec.push_back(str);
259 return;
260 }
261 std::string::size_type begPos = 0;
262 std::string::size_type endPos = 0;
263 std::string tmpStr;
264 while ((endPos = str.find(split, begPos)) != std::string::npos) {
265 if (endPos > begPos) {
266 tmpStr = str.substr(begPos, endPos - begPos);
267 vec.push_back(tmpStr);
268 }
269 begPos = endPos + split.size();
270 }
271 tmpStr = str.substr(begPos);
272 if (!tmpStr.empty()) {
273 vec.push_back(tmpStr);
274 }
275 return;
276 }
277
Vec2Stream(const std::string & prefix,const std::vector<char> & vecChar,const std::string & sufffix)278 std::string Vec2Stream(const std::string &prefix, const std::vector<char> &vecChar, const std::string &sufffix)
279 {
280 std::ostringstream ss;
281 constexpr int hexCharLen = 2;
282 ss << prefix;
283 int temp = 0;
284 for (std::size_t i = 0; i < vecChar.size(); i++) {
285 temp = (unsigned char)(vecChar[i]);
286 ss << std::setfill('0') << std::setw(hexCharLen) << std::hex << std::uppercase << temp << " ";
287 }
288 ss << sufffix;
289 return ss.str();
290 }
291
IsValidateNum(const std::string & str)292 bool IsValidateNum(const std::string &str)
293 {
294 if (str.empty()) {
295 return false;
296 }
297 for (auto it = str.begin(); it != str.end(); ++it) {
298 if (it == str.begin() && *it == ASCALL_MINUS_SIGN_INDEX) {
299 continue;
300 } else if (*it >= ASCALL_NUM_START_INDEX && *it <= ASCALL_NUM_END_INDEX) {
301 continue;
302 } else {
303 return false;
304 }
305 }
306 return true;
307 }
308
TransformFrequencyIntoChannel(int freq)309 int TransformFrequencyIntoChannel(int freq)
310 {
311 if (freq >= FREP_2G_MIN && freq <= FREP_2G_MAX) {
312 return (freq - FREP_2G_MIN) / CENTER_FREP_DIFF + CHANNEL_2G_MIN;
313 } else if (freq == CHANNEL_14_FREP) {
314 return CHANNEL_14;
315 } else if (freq >= FREP_5G_MIN && freq <= FREP_5G_MAX) {
316 return (freq - FREP_5G_MIN) / CENTER_FREP_DIFF + CHANNEL_5G_MIN;
317 }
318 return -1;
319 }
320
HexStringToVec(const std::string & str,std::vector<char> & vec)321 int HexStringToVec(const std::string &str, std::vector<char> &vec)
322 {
323 unsigned len = str.length();
324 if ((len & 1) != 0) {
325 return -1;
326 }
327 const int hexShiftNum = 4;
328 for (unsigned i = 0; i + 1 < len;) {
329 int8_t high = IsValidHexCharAndConvert(str[i]);
330 int8_t low = IsValidHexCharAndConvert(str[i + 1]);
331 if (high < 0 || low < 0) {
332 return -1;
333 }
334 char tmp = ((static_cast<uint8_t>(high) << hexShiftNum) | (static_cast<uint8_t>(low) & 0x0F));
335 vec.push_back(tmp);
336 i += 2; //2:拼接char类型的高四位和第四位
337 }
338 return 0;
339 }
340
HexStringToVec(const std::string & str,uint8_t plainText[],uint32_t plainLength,uint32_t & resultLength)341 int HexStringToVec(const std::string &str, uint8_t plainText[], uint32_t plainLength, uint32_t &resultLength)
342 {
343 std::vector<char> result;
344 result.clear();
345 int ret = HexStringToVec(str, result);
346 if (ret == -1 || result.size() > plainLength) {
347 return -1;
348 }
349 for (std::vector<char>::size_type i = 0; i < result.size(); ++i) {
350 plainText[i] = result[i];
351 }
352 resultLength = result.size();
353 return 0;
354 }
355
ConvertArrayChar(uint8_t ch)356 static char ConvertArrayChar(uint8_t ch)
357 {
358 constexpr int maxDecNum = 9;
359 constexpr int numDiffForHexAlphabet = 10;
360 if (ch <= maxDecNum) {
361 return '0' + ch;
362 }
363 if (ch <= 0xf) {
364 return ch + 'a' - numDiffForHexAlphabet;
365 }
366 return '0';
367 }
368
ConvertArrayToHex(const uint8_t plainText[],uint32_t size)369 std::string ConvertArrayToHex(const uint8_t plainText[], uint32_t size)
370 {
371 constexpr int bitWidth = 4;
372 std::stringstream ss;
373 for (uint32_t i = 0; i < size; i++) {
374 ss << ConvertArrayChar(plainText[i] >> bitWidth) << ConvertArrayChar (plainText[i] & 0xf);
375 }
376 return ss.str();
377 }
378
ValidateChar(const char ch)379 static bool ValidateChar(const char ch)
380 {
381 if (ch == '\n' || ch == '\r') {
382 return false;
383 }
384 return true;
385 }
386
ValidateString(const std::string & str)387 std::string ValidateString(const std::string &str)
388 {
389 std::stringstream ss;
390 ss << "\"";
391 for (char ch : str) {
392 if (ValidateChar(ch)) {
393 ss << ch;
394 }
395 }
396 ss << "\"";
397 return ss.str();
398 }
399
TransformFrequencyIntoChannel(const std::vector<int> & freqVector,std::vector<int> & chanVector)400 void TransformFrequencyIntoChannel(const std::vector<int> &freqVector, std::vector<int> &chanVector)
401 {
402 int channel;
403 for (size_t i = 0; i < freqVector.size(); ++i) {
404 if (freqVector[i] >= FREP_2G_MIN && freqVector[i] <= FREP_2G_MAX) {
405 channel = (freqVector[i] - FREP_2G_MIN) / CENTER_FREP_DIFF + CHANNEL_2G_MIN;
406 } else if (freqVector[i] == CHANNEL_14_FREP) {
407 channel = CHANNEL_14;
408 } else if (freqVector[i] >= FREP_5G_MIN && freqVector[i] <= FREP_5G_MAX) {
409 channel = (freqVector[i] - FREP_5G_MIN) / CENTER_FREP_DIFF + CHANNEL_5G_MIN;
410 } else {
411 LOGW("Invalid Freq:%d", freqVector[i]);
412 continue;
413 }
414 chanVector.push_back(channel);
415 }
416 }
417
TransformFreqToBand(int freq)418 BandType TransformFreqToBand(int freq)
419 {
420 if (freq <= CHANNEL_14_FREP) {
421 return BandType::BAND_2GHZ;
422 } else if (freq <= FREP_5G_MAX) {
423 return BandType::BAND_5GHZ;
424 }
425 return BandType::BAND_NONE; // not supported currently 6/60GHZ
426 }
427
TransformChannelToBand(int channel)428 BandType TransformChannelToBand(int channel)
429 {
430 if (channel <= CHANNEL_2G_MAX) {
431 return BandType::BAND_2GHZ;
432 } else if (channel <= CHANNEL_5G_MAX) {
433 return BandType::BAND_5GHZ;
434 }
435 return BandType::BAND_NONE; // not supported currently 6/60GHZ
436 }
437
IsValid24GHz(int freq)438 bool IsValid24GHz(int freq)
439 {
440 return freq > 2400 && freq < 2500;
441 }
442
IsValid5GHz(int freq)443 bool IsValid5GHz(int freq)
444 {
445 return freq > 4900 && freq < 5900;
446 }
447
IsValid24GChannel(int channel)448 bool IsValid24GChannel(int channel)
449 {
450 return channel >= CHANNEL_2G_MIN && channel <= CHANNEL_2G_MAX;
451 }
452
IsValid5GChannel(int channel)453 bool IsValid5GChannel(int channel)
454 {
455 return channel >= CHANNEL_5G_MIN && channel <= CHANNEL_5G_MAX;
456 }
457
458 #ifndef OHOS_ARCH_LITE
IsValidCountryCode(const std::string & wifiCountryCode)459 bool IsValidCountryCode(const std::string &wifiCountryCode)
460 {
461 if (wifiCountryCode.empty()) {
462 return false;
463 }
464 for (size_t i = 0; i < std::size(MCC_TABLE); i++) {
465 if (strcasecmp(wifiCountryCode.c_str(), MCC_TABLE[i].iso) == 0) {
466 return true;
467 }
468 }
469 return false;
470 }
471
ConvertMncToIso(int mnc,std::string & wifiCountryCode)472 bool ConvertMncToIso(int mnc, std::string &wifiCountryCode)
473 {
474 unsigned int left = 0;
475 unsigned int right = static_cast<size_t>(std::size(MCC_TABLE) - 1);
476 if (MCC_TABLE[left].mnc > mnc || MCC_TABLE[right].mnc < mnc) {
477 return false;
478 }
479 while (left < right) {
480 unsigned int mid = static_cast<size_t>(left + right) >> 1;
481 if (MCC_TABLE[mid].mnc < mnc) {
482 left = mid + 1;
483 } else if (MCC_TABLE[mid].mnc > mnc) {
484 right = mid - 1;
485 } else {
486 left = mid;
487 }
488 if (MCC_TABLE[left].mnc == mnc) {
489 wifiCountryCode = MCC_TABLE[left].iso;
490 return true;
491 }
492 }
493 return false;
494 }
495 #endif
496
StrToUpper(std::string & str)497 void StrToUpper(std::string &str)
498 {
499 std::for_each(std::begin(str), std::end(str), [](auto &c) {
500 c = std::toupper(c);
501 });
502 }
503
GetParamValue(const char * key,const char * def,char * value,uint32_t len)504 int GetParamValue(const char *key, const char *def, char *value, uint32_t len)
505 {
506 #ifdef INIT_LIB_ENABLE
507 return GetParameter(key, def, value, len);
508 #else
509 return EC_INVALID;
510 #endif
511 }
512
SetParamValue(const char * key,const char * value)513 int SetParamValue(const char *key, const char *value)
514 {
515 #ifdef INIT_LIB_ENABLE
516 return SetParameter(key, value);
517 #else
518 return EC_INVALID;
519 #endif
520 }
521
WatchParamValue(const char * keyprefix,ParameterChgPtr callback,void * context)522 int WatchParamValue(const char *keyprefix, ParameterChgPtr callback, void *context)
523 {
524 #ifdef INIT_LIB_ENABLE
525 return WatchParameter(keyprefix, callback, context);
526 #else
527 return EC_INVALID;
528 #endif
529 }
530
IsFreqDbac(int freqA,int freqB)531 bool IsFreqDbac(int freqA, int freqB)
532 {
533 if (freqA == freqB) {
534 return false;
535 }
536 if (IsValid5GHz(freqA) && IsValid5GHz(freqB)) {
537 return true;
538 }
539 if (IsValid24GHz(freqA) && IsValid24GHz(freqB)) {
540 return true;
541 }
542 return false;
543 }
544
IsChannelDbac(int channelA,int channelB)545 bool IsChannelDbac(int channelA, int channelB)
546 {
547 if (channelA == channelB) {
548 return false;
549 }
550 if (IsValid5GChannel(channelA) && IsValid5GChannel(channelB)) {
551 return true;
552 }
553 if (IsValid24GChannel(channelA) && IsValid24GChannel(channelB)) {
554 return true;
555 }
556 return false;
557 }
558
IsPskEncryption(const std::string & keyMgmt)559 bool IsPskEncryption(const std::string &keyMgmt)
560 {
561 return keyMgmt == KEY_MGMT_WPA_PSK || keyMgmt == KEY_MGMT_SAE;
562 }
563
IsFactoryMode()564 bool IsFactoryMode()
565 {
566 char preValue[PROP_FACTORY_RUN_MODE_LEN] = {0};
567 int errCode = GetParamValue(FACTORY_RUN_MODE, FACTORY_MODE_DEFAULT, preValue, PROP_FACTORY_RUN_MODE_LEN);
568 if (errCode > 0) {
569 if (strncmp(preValue, FACTORY_MODE, FACTORY_MODE_LEN) == 0) {
570 return true;
571 }
572 }
573 return false;
574 }
575
GetDeviceType()576 int GetDeviceType()
577 {
578 char preValue[PROP_PRODUCT_DEVICE_TYPE_LEN] = {0};
579 int errCode = GetParamValue(
580 PRODUCT_DEVICE_TYPE, DEFAULT_PRODUCT_DEVICE_TYPE, preValue, PROP_PRODUCT_DEVICE_TYPE_LEN);
581 if (errCode > 0) {
582 if (strncmp(preValue, PHONE_PRODUCT_DEVICE_TYPE, PRODUCT_DEVICE_TYPE_LEN) == 0) {
583 return ProductDeviceType::PHONE;
584 }
585 if (strncmp(preValue, WEARABLE_PRODUCT_DEVICE_TYPE, PRODUCT_DEVICE_TYPE_LEN) == 0) {
586 return ProductDeviceType::WEARABLE;
587 }
588 if (strncmp(preValue, TABLET_PRODUCT_DEVICE_TYPE, PRODUCT_DEVICE_TYPE_LEN) == 0) {
589 return ProductDeviceType::TABLET;
590 }
591 if (strncmp(preValue, TV_PRODUCT_DEVICE_TYPE, PRODUCT_DEVICE_TYPE_LEN) == 0) {
592 return ProductDeviceType::TV;
593 }
594 if (strncmp(preValue, PC_PRODUCT_DEVICE_TYPE, PRODUCT_DEVICE_TYPE_LEN) == 0) {
595 return ProductDeviceType::PC;
596 }
597 }
598 return ProductDeviceType::DEFAULT;
599 }
600
CheckDeviceTypeByVendorCountry()601 bool CheckDeviceTypeByVendorCountry()
602 {
603 char param[SYS_PARAMETER_SIZE] = { 0 };
604 int errorCode = GetParamValue(VENDOR_COUNTRY_KEY, VENDOR_COUNTRY_DEFAULT, param, SYS_PARAMETER_SIZE);
605 if (errorCode <= PARAMETER_ERROR_CODE) {
606 LOGE("get vendor country fail, errorCode: %{public}d", errorCode);
607 return false;
608 }
609
610 LOGI("vendor country: %{public}s, errorCode: %{public}d.", param, errorCode);
611 auto iter = std::string(param).find("hwit");
612 return iter != std::string::npos;
613 }
614
IsStartUpWifiEnableSupport()615 bool IsStartUpWifiEnableSupport()
616 {
617 LOGI("Enter IsStartUpWifiEnableSupport");
618 char preValue[PROP_STARTUP_WIFI_ENABLE_LEN] = {0};
619 int errCode = GetParamValue(PROP_STARTUP_WIFI_ENABLE, DEFAULT_STARTUP_WIFI_ENABLE,
620 preValue, PROP_STARTUP_WIFI_ENABLE_LEN);
621 if (errCode > 0) {
622 if (strncmp(preValue, STARTUP_WIFI_ENABLE, STARTUP_WIFI_ENABLE_LEN) == 0) {
623 LOGI("param startup_wifi_enable is true.");
624 return true;
625 }
626 }
627 return false;
628 }
629
IsSignalSmoothingEnable()630 bool IsSignalSmoothingEnable()
631 {
632 LOGI("Enter IsSignalSmoothingEnable");
633 char preValue[PROP_FSS_ENABLE_LEN] = {0};
634 int errCode = GetParamValue(PROP_FSS_ENABLE, DEFAULT_FSS_ENABLE,
635 preValue, PROP_FSS_ENABLE_LEN);
636 if (errCode > 0) {
637 if (strncmp(preValue, FSS_ENABLE, FSS_ENABLE_LEN) == 0) {
638 LOGI("param fss_enable is true.");
639 return true;
640 }
641 }
642 return false;
643 }
644
645 #ifndef OHOS_ARCH_LITE
ParseJsonKey(const cJSON * jsonValue,const std::string & key,std::string & value)646 bool ParseJsonKey(const cJSON *jsonValue, const std::string &key, std::string &value)
647 {
648 if (!cJSON_IsArray(jsonValue)) {
649 return false;
650 }
651 int nSize = cJSON_GetArraySize(jsonValue);
652 for (int i = 0; i < nSize; ++i) {
653 cJSON *item = cJSON_GetArrayItem(jsonValue, i);
654 if (item == nullptr) {
655 return false;
656 }
657 if (!cJSON_IsObject(item)) {
658 cJSON_Delete(item);
659 return false;
660 }
661 cJSON *keyItem = cJSON_GetObjectItem(item, key.c_str());
662 if (keyItem == nullptr) {
663 return false;
664 }
665 if (cJSON_IsString(keyItem) && keyItem->valuestring != nullptr) {
666 value = keyItem->valuestring;
667 return true;
668 } else if (cJSON_IsNumber(keyItem)) {
669 value = std::to_string(keyItem->valueint);
670 return true;
671 } else {
672 return false;
673 }
674 }
675 return false;
676 }
677
ParseJson(const std::string & jsonString,const std::string & type,const std::string & key,std::string & value)678 bool ParseJson(const std::string &jsonString, const std::string &type, const std::string &key, std::string &value)
679 {
680 cJSON *root = cJSON_Parse(jsonString.c_str());
681 if (root == nullptr) {
682 LOGE("ParseJson failed to parse json data.");
683 return false;
684 }
685 if (!cJSON_IsArray(root)) {
686 cJSON_Delete(root);
687 return false;
688 }
689 int nSize = cJSON_GetArraySize(root);
690 for (int i = 0; i < nSize; i++) {
691 cJSON *item = cJSON_GetArrayItem(root, i);
692 if (item == nullptr) {
693 continue;
694 }
695 if (!cJSON_IsObject(item)) {
696 cJSON_Delete(item);
697 continue;
698 }
699 cJSON *typeItem = cJSON_GetObjectItem(item, type.c_str());
700 if (typeItem == nullptr) {
701 continue;
702 }
703 if (ParseJsonKey(typeItem, key, value)) {
704 cJSON_Delete(root);
705 return true;
706 }
707 }
708 cJSON_Delete(root);
709 return false;
710 }
711
ConvertDecStrToHexStr(const std::string & inData,std::string & outData)712 void ConvertDecStrToHexStr(const std::string &inData, std::string &outData)
713 {
714 std::stringstream ss(inData);
715 std::string token;
716 constexpr int hexCharLen = 2;
717 std::stringstream temp;
718 while (getline(ss, token, ',')) {
719 int num = CheckDataLegal(token);
720 temp << std::setfill('0') << std::setw(hexCharLen) << std::hex << num;
721 }
722 outData = temp.str();
723 }
724
SplitStringBySubstring(const std::string & inData,std::string & outData,const std::string & subBegin,const std::string & subEnd)725 void SplitStringBySubstring(const std::string &inData, std::string &outData, const std::string &subBegin,
726 const std::string &subEnd)
727 {
728 auto posBegin = inData.find(subBegin);
729 auto posEnd = inData.find(subEnd);
730 if (posBegin == std::string::npos || posEnd == std::string::npos) {
731 LOGE("SplitStringBySubstring find substring fail.");
732 return;
733 }
734 if (posEnd < posBegin + subEnd.length()) {
735 LOGE("SplitStringBySubstring data length is invaild.");
736 return;
737 }
738 outData = inData.substr(posBegin, posEnd - posBegin + subEnd.length());
739 return;
740 }
741
GetBssidCounter(const WifiDeviceConfig & config,const std::vector<WifiScanInfo> & scanResults)742 int GetBssidCounter(const WifiDeviceConfig &config, const std::vector<WifiScanInfo> &scanResults)
743 {
744 int counter = 0;
745 if (scanResults.empty()) {
746 LOGI("scanResults ie empty.");
747 return 0;
748 }
749
750 std::string currentSsid = config.ssid;
751 std::string configKey = config.keyMgmt;
752 if (currentSsid.empty() || configKey.empty()) {
753 return 0;
754 }
755 for (WifiScanInfo nextResult : scanResults) {
756 std::string scanSsid = nextResult.ssid;
757 std::string capabilities = nextResult.capabilities;
758 if (currentSsid == scanSsid && IsSameEncryptType(capabilities, configKey)) {
759 counter += 1;
760 }
761 }
762 return counter;
763 }
764
IsSameEncryptType(const std::string & scanInfoKeymgmt,const std::string & deviceKeymgmt)765 bool IsSameEncryptType(const std::string& scanInfoKeymgmt, const std::string& deviceKeymgmt)
766 {
767 if (deviceKeymgmt == "WPA-PSK") {
768 return scanInfoKeymgmt.find("PSK") != std::string::npos;
769 } else if (deviceKeymgmt == "WPA-EAP") {
770 return scanInfoKeymgmt.find("EAP") != std::string::npos;
771 } else if (deviceKeymgmt == "SAE") {
772 return scanInfoKeymgmt.find("SAE") != std::string::npos;
773 } else if (deviceKeymgmt == "NONE") {
774 return (scanInfoKeymgmt.find("PSK") == std::string::npos) &&
775 (scanInfoKeymgmt.find("EAP") == std::string::npos) && (scanInfoKeymgmt.find("SAE") == std::string::npos);
776 } else {
777 return false;
778 }
779 }
780 #endif
781 } // namespace Wifi
782 } // namespace OHOS
783