• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef OHOS_WIFI_GLOBAL_FUNC_H
17 #define OHOS_WIFI_GLOBAL_FUNC_H
18 
19 #include <vector>
20 #include <random>
21 #include <string>
22 #include "wifi_errcode.h"
23 #include "wifi_ap_msg.h"
24 #include "wifi_internal_msg.h"
25 #include "wifi_msg.h"
26 #include "wifi_scan_msg.h"
27 
28 namespace OHOS {
29 namespace Wifi {
30 constexpr int MAC_STRING_SIZE = 17;
31 constexpr int MAX_CHAR_LEN = 128;
32 constexpr int MIN_SSID_LEN = 1;
33 constexpr int MAX_SSID_LEN = 32;
34 constexpr int MIN_PSK_LEN = 8;
35 constexpr int MAX_PSK_LEN = 63;
36 constexpr int HEX_TYPE_LEN = 3; /* 3 hex type: 0 a A */
37 constexpr int MAX_AP_CONN = 32;
38 constexpr int MAX_CONFIGS_NUM = 1000;
39 typedef void (*ParameterChgPtr)(const char *key, const char *value, void *context);
40 
41 /**
42  * @Description Get a random string
43  *
44  * @param len - Random string length
45  * @return std::string - Random String
46  */
47 std::string GetRandomStr(int len);
48 
49 /**
50  * @Description get a random num
51  *
52  * @param start - The lower limit of the range of random numbers
53  * @param end - The upper limit of the range of random numbers
54  * @return random num
55  */
56 int GetRandomInt(int start, int end);
57 
58 /**
59  * @Description If allowed scan always according the scan control policy
60  *
61  * @param info - ScanControlInfo object
62  * @return true - allowed
63  * @return false - not allowed
64  */
65 bool IsAllowScanAnyTime(const ScanControlInfo &info);
66 
67 /**
68  * @Description Internal transition from OperateResState struct to ConnState
69  *
70  * @param resState - OperateResState state
71  * @param isReport - true : need report; false : not report
72  * @return ConnState - convert output connection state
73  */
74 ConnState ConvertConnStateInternalExtral(OperateResState resState, bool &isReport);
75 
76 /**
77  * @Description Internal transition from OperateResState struct to ConnState
78  *
79  * @param resState - OperateResState state
80  * @param isReport - true : need report; false : not report
81  * @return ConnState - convert output connection state
82  */
83 ConnState ConvertConnStateInternal(OperateResState resState, bool &isReport);
84 
85 /**
86  * @Description Check whether the MAC address is valid
87  *
88  * @param macStr - input the mac address
89  * @return int - 0 Valid; -1 Invalid
90  */
91 int CheckMacIsValid(const std::string &macStr);
92 
93 /**
94  * @Description Split string to vector accord split
95  *
96  * @param str - input string
97  * @param split - split string
98  * @param vec - return string vector
99  */
100 void SplitString(const std::string &str, const std::string &split, std::vector<std::string> &vec);
101 
102 /**
103  * @Description Converts a numeric vector to a character array.
104  *
105  * @param vec - Input numeric vector.[in]
106  * @param pChar - Character array.[out]
107  * @param len - Length of character array.[out]
108  * @param memSize - Character array's memory size.[in]
109  * @return int - 0 Valid; -1 Invalid
110  */
111 template <typename T>
Vec2Char(const std::vector<T> & vec,T * pChar,int & len,int memSize)112 int Vec2Char(const std::vector<T> &vec, T *pChar, int& len, int memSize)
113 {
114     if (pChar == nullptr) {
115         len = 0;
116         return -1;
117     }
118 
119     const int vecSize = static_cast<int>(vec.size());
120     if (vecSize > memSize) {
121         pChar = nullptr;
122         len = 0;
123         return -1;
124     }
125 
126     for (int i = 0; i < vecSize; i++) {
127         pChar[i] = vec[i];
128     }
129     len = vecSize;
130     return 0;
131 }
132 
133 /**
134  * @Description Converts a character array to a numeric vector.
135  *
136  * @param pChar - Character array.[in]
137  * @param len - Length of character array.[in]
138  * @param vec - Input numeric vector.[out]
139  * @return int - 0 Valid; -1 Invalid
140  */
141 template <typename T>
Char2Vec(const T * pChar,int len,std::vector<T> & vec)142 int Char2Vec(const T *pChar, int len, std::vector<T> &vec)
143 {
144     vec.clear();
145     if (pChar == nullptr || len < 0) {
146         return -1;
147     }
148 
149     if (pChar != nullptr && len > 0 && len <= MAX_CHAR_LEN) {
150         for (int i = 0; i < len; i++) {
151             vec.push_back(pChar[i]);
152         }
153     }
154 
155     return 0;
156 }
157 
158 /**
159  * @Description Converts a char/unsigned char/byte/int8 vector to a hexadecimal character array. A numeric
160  * value is converted to two characters. e.g. 0x3F -> '3' 'F'
161  *
162  * @param vec - Input numeric vector.
163  * @param pHexChar - Character array.
164  * @param memSize - Character array's memory size.
165  * @return int - 0 Valid; -1 Invalid
166  */
167 template<typename T>
Val2HexChar(const std::vector<T> & vec,char * pHexChar,unsigned memSize)168 int Val2HexChar(const std::vector<T> &vec, char *pHexChar, unsigned memSize)
169 {
170     unsigned size = vec.size();
171     unsigned doubleSize = (size << 1);
172     if (doubleSize >= memSize) {
173         return -1;
174     }
175     const std::string hexStr = "0123456789ABCDEF";
176     const unsigned highBit = 4;
177     int pos = 0;
178     for (unsigned i = 0; i < size; ++i) {
179         unsigned char tmp = vec[i];
180         pHexChar[pos] = hexStr[(tmp >> highBit) & 0x0F];
181         ++pos;
182         pHexChar[pos] = hexStr[tmp & 0x0F];
183         ++pos;
184     }
185     pHexChar[pos] = '\0';
186     return 0;
187 }
188 
189 template <typename T>
JoinVecToString(const std::vector<T> & vec,const std::string & delimiter)190 std::string JoinVecToString(const std::vector<T> &vec, const std::string &delimiter)
191 {
192     std::stringstream ss;
193     std::copy(vec.begin(), vec.end(), std::ostream_iterator<T>(ss, delimiter.c_str()));
194     std::string joinedStr = ss.str();
195     if (joinedStr.size() > delimiter.size()) {
196         joinedStr.erase(joinedStr.size() - delimiter.size());
197     }
198     return joinedStr;
199 }
200 
201 /**
202  * @Description splitting numeric strings based on characters
203  *
204  * @param str - split string
205  * @param split - characters used for splitting
206  * @return number vector
207  */
208 std::vector<int> SplitStringToIntVector(const std::string &str, const std::string &split);
209 
210 /**
211  * @Description  Output vecChar to stream.
212  * @param prefix  - prefix string[in]
213  * @param vecChar - vector char[in]
214  * @param suffix  - suffix string[in]
215  */
216 std::string Vec2Stream(const std::string &prefix, const std::vector<char> &vecChar, const std::string &sufffix = "");
217 
218 /**
219  * @Description Convert a hex type string to vector.
220  *
221  * @param str - input hex string, eg: 010203...
222  * @param vec - output vector result, eg: [1,2,3,...]
223  * @return int - convert result, 0 success, -1 failed
224  */
225 int HexStringToVec(const std::string &str, std::vector<char> &vec);
226 
227 /**
228  * @Description Convert a hex type string to uint8_t*.
229  *
230  * @param str - input hex string, eg: 010203...
231  * @param plainText - output uint8_t* result, eg: [1,2,3,...]
232  * @param plainLength - input maxLength of uint8_t* result, eg: 256
233  * @param resultLength - output Length of uint8_t* result, eg: 16
234  * @return int - convert result, 0 success, -1 failed
235  */
236 int HexStringToVec(const std::string &str, uint8_t plainText[], uint32_t plainLength, uint32_t &resultLength);
237 
238 /**
239  * @Description Convert a uint8_t* to Hex string.
240  *
241  * @param plainText - input uint8_t*, eg: [1,2,3,...]
242  * @param size - input uint8_t* size, eg: 16
243  * @return string - convert Hex string, eg: 010203...
244  */
245 std::string ConvertArrayToHex(const uint8_t plainText[], uint32_t size);
246 
247 /**
248  * @Description Convert a string to validate string for write.
249  *
250  * @param str - input string
251  * @return string - validate string wrapped by ""
252  */
253 std::string ValidateString(const std::string  &str);
254 
255 /**
256  * @Description is unm
257  *
258  * @param str - input string
259  * @return result
260  */
261 bool IsValidateNum(const std::string &str);
262 
263 /**
264  * @Description transform freq to bandType
265  *
266  * @param freq - freq
267  * @return BandType
268  */
269 BandType TransformFreqToBand(int freq);
270 
271 /**
272  * @Description transform channel to bandType
273  *
274  * @param channel - channel
275  * @return BandType
276  */
277 BandType TransformChannelToBand(int channel);
278 
279 /**
280  * @Description Check is a valid 5G frequency.
281  *
282  * @param freq - Frequency input
283  * @return true - valid
284  * @return false - invalid
285  */
286 bool IsValid5GHz(int freq);
287 
288 /**
289  * @Description Check is a valid 2.4G frequency.
290  *
291  * @param freq - Frequency input
292  * @return true - valid
293  * @return false - invalid
294  */
295 bool IsValid24GHz(int freq);
296 
297 /**
298  * @Description Check is a valid 2.4G channel.
299  *
300  * @param channel - channel input
301  * @return true - valid
302  * @return false - invalid
303  */
304 bool IsValid24GChannel(int channel);
305 
306 /**
307  * @Description Check is a valid 5G channel.
308  *
309  * @param channel - channel input
310  * @return true - valid
311  * @return false - invalid
312  */
313 bool IsValid5GChannel(int channel);
314 
315 /**
316  * @Description  Convert frequency to channel number.
317  * @param freq - frequency to convert
318  * @return success: channel num    failed: -1
319  */
320 int TransformFrequencyIntoChannel(int freq);
321 
322 /**
323  * @Description Convert the frequency in the container into a channel.
324  *
325  * @param freqVector - frequency vector input
326  * @param chanVector - Channel vector output
327  */
328 void TransformFrequencyIntoChannel(const std::vector<int> &freqVector, std::vector<int> &chanVector);
329 
330 /**
331  * @Description transform freq to band
332  *
333  * @param freq - freq
334  * @return band
335  */
336 BandType TransformFreqToBand(int freq);
337 
338 /**
339  * @Description transform channel to band
340  *
341  * @param channel - channel
342  * @return band
343  */
344 BandType TransformChannelToBand(int channel);
345 
346 #ifndef OHOS_ARCH_LITE
347 /**
348  * @Description Check whether the country code is valid.
349  *
350  * @param wifiCountryCode - country code to be determined
351  * @return true - valid
352  * @return false - invalid
353  */
354 bool IsValidCountryCode(const std::string &wifiCountryCode);
355 
356 /**
357  * @Description Convert the country code from mnc to iso.
358  *
359  * @param wifiCountryCode - country code to be convert
360  * @return true - convert success
361  * @return false - convert fail
362  */
363 bool ConvertMncToIso(int mnc, std::string &wifiCountryCode);
364 #endif
365 
366 /**
367  * @Description Convert the letters to upper.
368  *
369  * @param str - input lowercase letters and output upper letters
370  */
371 void StrToUpper(std::string &str);
372 
373 /**
374  * @Description Obtains a system parameter matching the specified key.
375  *
376  * @param key - Indicates the key for the system parameter to query.
377  * The value can contain lowercase letters, digits, underscores (_), and dots (.).
378  * Its length cannot exceed 32 bytes (including the end-of-text character in the string).
379  * @param def - Indicates the default value to return when no query result is found.
380  * This parameter is specified by the caller.
381  * @param value - Indicates the data buffer that stores the query result.
382  * This parameter is applied for and released by the caller and can be used as an output parameter.
383  * @param len - Indicates the length of the data in the buffer.
384  * @return Returns the number of bytes of the system parameter if the operation is successful;
385  * returns -9 if a parameter is incorrect; returns -1 in other scenarios.
386  */
387 int GetParamValue(const char *key, const char *def, char *value, uint32_t len);
388 
389 /**
390  * @Description Sets or updates a system parameter.
391  *
392  * @param key Indicates the key for the parameter to set or update.
393  * The value can contain lowercase letters, digits, underscores (_), and dots (.).
394  * Its length cannot exceed 32 bytes (including the end-of-text character in the string).
395  * @param value Indicates the system parameter value.
396  * Its length cannot exceed 128 bytes (including the end-of-text character in the string).
397  * @return Returns 0 if the operation is successful;
398  * returns -9 if a parameter is incorrect; returns -1 in other scenarios.
399  */
400 int SetParamValue(const char *key, const char *value);
401 
402 /**
403  * @Description Watch for system parameter values.
404  *
405  * @param keyPrefix - Indicates the key prefix for the parameter to be watched.
406  * If keyPrefix is not a full name, "A.B." for example, it means to watch for all parameter started with "A.B.".
407  * @param callback - Indicates value change callback.
408  * If callback is NULL, it means to cancel the watch.
409  * @param context - context.
410  * @return Returns 0 if the operation is successful;
411  */
412 int WatchParamValue(const char *keyprefix, ParameterChgPtr callback, void *context);
413 
414 /**
415  * @Description are the two frequencies dbac
416  *
417  * @param freqA - one freq
418  * @param freqB - other freq
419  * @return true - dbac
420  * @return false - not dbac
421  */
422 bool IsFreqDbac(int freqA, int freqB);
423 
424 /**
425  * @Description are the two channels dbac
426  *
427  * @param freqA - one channel
428  * @param freqB - other channel
429  * @return true - dbac
430  * @return false - not dbac
431  */
432 bool IsChannelDbac(int channelA, int channelB);
433 
434 /**
435  * @Description check the keyMgmnt is Psk
436  *
437  * @param keyMgmt - in data
438  * @return true - isPsk
439  */
440 bool IsPskEncryption(const std::string &keyMgmt);
441 
442 /**
443  * @Description is factory mode
444  *
445  * @return true - factory mode
446  * @return false - not factory mode
447  */
448 bool IsFactoryMode();
449 
450 /**
451  * @Description get device type
452  *
453  * @return int - DEFAULT -1, PHONE 0, TABLET 1, WEARABLE 2, TV 3, PC 4
454  */
455 int GetDeviceType();
456 
457 /**
458  * @Description get vendor country from param to determine it is ItDevice.
459  *
460  * @return true - vendor country is ItDevice
461  */
462 bool CheckDeviceTypeByVendorCountry();
463 
464 /**
465  * @Description is wifi support opening automatically when first start up
466  *
467  * @return true - open
468  * @return false - close
469  */
470 bool IsStartUpWifiEnableSupport();
471 /**
472  * @Description is fold product supprt signal smoothe
473  *
474  * @return true - smoothe
475  * @return false - not smoothe
476  */
477 bool IsSignalSmoothingEnable();
478 #ifndef OHOS_ARCH_LITE
479 /**
480  * @Description Parse json string, find key by type, get value by key
481  *
482  * @param jsonString - json string
483  * @param type - key at group named type
484  * @param key - key
485  * @param value - value
486  * @return true - parse json success
487  */
488 bool ParseJson(const std::string &jsonString, const std::string &type, const std::string &key, std::string &value);
489 
490 /**
491  * @Description used for string(1,2,3,...,255) to hexstring
492  *
493  * @param inData - string(1,2,3,...,255)
494  * @param outData - hexstring
495  */
496 void ConvertDecStrToHexStr(const std::string &inData, std::string &outData);
497 
498 /**
499  * @Description Split string by substrings
500  *
501  * @param inData - in data
502  * @param outData - out data
503  * @param subBegin - find substring begin
504  * @param subEnd - find substring end
505  */
506 void SplitStringBySubstring(const std::string &inData, std::string &outData, const std::string &subBegin,
507     const std::string &subEnd);
508 
509 int GetBssidCounter(const WifiDeviceConfig &config, const std::vector<WifiScanInfo> &scanResults);
510 
511 bool IsSameEncryptType(const std::string& scanInfoKeymgmt, const std::string& deviceKeymgmt);
512 #endif
513 }  // namespace Wifi
514 }  // namespace OHOS
515 #endif