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