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