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_scan_msg.h"
25 #include "wifi_settings.h"
26
27 namespace OHOS {
28 namespace Wifi {
29 constexpr int MAC_STRING_SIZE = 17;
30 constexpr int MIN_SSID_LEN = 1;
31 constexpr int MAX_SSID_LEN = 32;
32 constexpr int MIN_PSK_LEN = 8;
33 constexpr int MAX_PSK_LEN = 63;
34 constexpr int HEX_TYPE_LEN = 3; /* 3 hex type: 0 a A */
35 constexpr int MAX_AP_CONN = 32;
36
37 /**
38 * @Description Check valid ssid config
39 *
40 * @param cfg - HotspotConfig
41 * @return ErrCode - WIFI_OPT_SUCCESS or others
42 */
43 ErrCode CfgCheckSsid(const HotspotConfig &cfg);
44
45 /**
46 * @Description Check valid psk config
47 *
48 * @param cfg - HotspotConfig
49 * @return ErrCode - WIFI_OPT_SUCCESS or others
50 */
51 ErrCode CfgCheckPsk(const HotspotConfig &cfg);
52
53 /**
54 * @Description Check valid band config
55 *
56 * @param cfg - HotspotConfig
57 * @param bandsFromCenter - vector of BandType
58 * @return ErrCode - WIFI_OPT_SUCCESS or others
59 */
60 ErrCode CfgCheckBand(const HotspotConfig &cfg, std::vector<BandType> &bandsFromCenter);
61
62 /**
63 * @Description Check valid channel config
64 *
65 * @param cfg - HotspotConfig
66 * @param channInfoFromCenter - ChannelsTable object
67 * @return ErrCode - WIFI_OPT_SUCCESS or others
68 */
69 ErrCode CfgCheckChannel(const HotspotConfig &cfg, ChannelsTable &channInfoFromCenter);
70
71 /**
72 * @Description Check valid hotspot config
73 *
74 * @param cfg - HotspotConfig
75 * @param cfgFromCenter - Get HotspotConfig from config center
76 * @param bandsFromCenter - vector of BandType
77 * @param channInfoFromCenter - ChannelsTable object
78 * @return ErrCode - WIFI_OPT_SUCCESS or others
79 */
80 ErrCode IsValidHotspotConfig(const HotspotConfig &cfg, const HotspotConfig &cfgFromCenter,
81 std::vector<BandType> &bandsFromCenter, ChannelsTable &channInfoFromCenter);
82
83 /**
84 * @Description Get a random string
85 *
86 * @param len - Random string length
87 * @return std::string - Random String
88 */
89 std::string GetRandomStr(int len);
90
91 /**
92 * @Description If allowed scan always according the scan control policy
93 *
94 * @param info - ScanControlInfo object
95 * @return true - allowed
96 * @return false - not allowed
97 */
98 bool IsAllowScanAnyTime(const ScanControlInfo &info);
99
100 /**
101 * @Description Internal transition from OperateResState struct to ConnState
102 *
103 * @param resState - OperateResState state
104 * @param isReport - true : need report; flase : not report
105 * @return ConnState - convert output connection state
106 */
107 ConnState ConvertConnStateInternal(OperateResState resState, bool &isReport);
108
109 /**
110 * @Description Check whether the MAC address is valid
111 *
112 * @param macStr - input the mac address
113 * @return int - 0 Valid; -1 Invalid
114 */
115 int CheckMacIsValid(const std::string &macStr);
116
117 /**
118 * @Description Split string to vector accord split
119 *
120 * @param str - input string
121 * @param split - split string
122 * @param vec - return string vector
123 */
124 void SplitString(const std::string &str, const std::string &split, std::vector<std::string> &vec);
125
126 /**
127 * @Description Converts a numeric vector to a character array.
128 *
129 * @param vec - Input numeric vector.[in]
130 * @param pChar - Character array.[out]
131 * @param len - Length of character array.[out]
132 * @param memSize - Character array's memory size.[in]
133 * @return int - 0 Valid; -1 Invalid
134 */
135 template <typename T>
Vec2Char(const std::vector<T> & vec,T * pChar,int & len,int memSize)136 int Vec2Char(const std::vector<T> &vec, T *pChar, int& len, int memSize)
137 {
138 if (pChar == nullptr) {
139 len = 0;
140 return -1;
141 }
142
143 const int vecSize = static_cast<int>(vec.size());
144 if (vecSize > memSize) {
145 pChar = nullptr;
146 len = 0;
147 return -1;
148 }
149
150 for (int i = 0; i < vecSize; i++) {
151 pChar[i] = vec[i];
152 }
153 len = vecSize;
154 return 0;
155 }
156
157 /**
158 * @Description Converts a character array to a numeric vector.
159 *
160 * @param pChar - Character array.[in]
161 * @param len - Length of character array.[in]
162 * @param vec - Input numeric vector.[out]
163 * @return int - 0 Valid; -1 Invalid
164 */
165 template <typename T>
Char2Vec(const T * pChar,int len,std::vector<T> & vec)166 int Char2Vec(const T *pChar, int len, std::vector<T> &vec)
167 {
168 vec.clear();
169 if (pChar == nullptr || len < 0) {
170 return -1;
171 }
172
173 for (int i = 0; i < len; i++) {
174 vec.push_back(pChar[i]);
175 }
176
177 return 0;
178 }
179
180 /**
181 * @Description Converts a char/unsigned char/byte/int8 vector to a hexadecimal character array. A numeric
182 * value is converted to two characters. e.g. 0x3F -> '3' 'F'
183 *
184 * @param vec - Input numeric vector.
185 * @param pHexChar - Character array.
186 * @param memSize - Character array's memory size.
187 * @return int - 0 Valid; -1 Invalid
188 */
189 template<typename T>
Val2HexChar(const std::vector<T> & vec,char * pHexChar,unsigned memSize)190 int Val2HexChar(const std::vector<T> &vec, char *pHexChar, unsigned memSize)
191 {
192 unsigned size = vec.size();
193 unsigned doubleSize = (size << 1);
194 if (doubleSize >= memSize) {
195 return -1;
196 }
197 const std::string hexStr = "0123456789ABCDEF";
198 const unsigned highBit = 4;
199 int pos = 0;
200 for (unsigned i = 0; i < size; ++i) {
201 unsigned char tmp = vec[i];
202 pHexChar[pos] = hexStr[(tmp >> highBit) & 0x0F];
203 ++pos;
204 pHexChar[pos] = hexStr[tmp & 0x0F];
205 ++pos;
206 }
207 pHexChar[pos] = '\0';
208 return 0;
209 }
210
211 /**
212 * @Description Output vecChar to stream.
213 * @param prefix - prefix string[in]
214 * @param vecChar - vector char[in]
215 * @param suffix - suffix string[in]
216 */
217 std::string Vec2Stream(const std::string &prefix, const std::vector<char> &vecChar, const std::string &sufffix = "");
218
219 /**
220 * @Description Convert a hex type string to vector.
221 *
222 * @param str - input hex string, eg: 010203...
223 * @param vec - output vector result, eg: [1,2,3,...]
224 * @return int - convert result, 0 success, -1 failed
225 */
226 int HexStringToVec(const std::string &str, std::vector<char> &vec);
227
228 /**
229 * @Description Convert a hex type string to uint8_t*.
230 *
231 * @param str - input hex string, eg: 010203...
232 * @param plainText - output uint8_t* result, eg: [1,2,3,...]
233 * @param plainLength - input maxLength of uint8_t* result, eg: 256
234 * @param resultLength - output Length of uint8_t* result, eg: 16
235 * @return int - convert result, 0 success, -1 failed
236 */
237 int HexStringToVec(const std::string &str, uint8_t plainText[], int plainLength, int &resultLength);
238
239 /**
240 * @Description Convert a uint8_t* to Hex string.
241 *
242 * @param plainText - input uint8_t*, eg: [1,2,3,...]
243 * @param size - input uint8_t* size, eg: 16
244 * @return string - convert Hex string, eg: 010203...
245 */
246 std::string ConvertArrayToHex(const uint8_t plainText[], int size);
247
248 /**
249 * @Description Convert a string to validate string for write.
250 *
251 * @param str - input string
252 * @return string - validate string wrapped by ""
253 */
254 std::string ValidateString(const std::string &str);
255 /**
256 * @Description Check is a valid 5G frequency.
257 *
258 * @param freq - Frequency input
259 * @return true - valid
260 * @return false - invalid
261 */
262 bool IsValid5GHz(int freq);
263
264 /**
265 * @Description Check is a valid 2.4G frequency.
266 *
267 * @param freq - Frequency input
268 * @return true - valid
269 * @return false - invalid
270 */
271 bool IsValid24GHz(int freq);
272
273 /**
274 * @Description Convert the frequency in the container into a channel.
275 *
276 * @param freqVector - frequency vector input
277 * @param chanVector - Channel vector output
278 */
279 void TransformFrequencyIntoChannel(const std::vector<int> &freqVector, std::vector<int> &chanVector);
280 } // namespace Wifi
281 } // namespace OHOS
282 #endif