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_COMMON_UTIL_H
17 #define OHOS_WIFI_COMMON_UTIL_H
18 
19 #include <map>
20 #include <chrono>
21 #include <string>
22 #include <vector>
23 #include "securec.h"
24 #include "define.h"
25 #ifndef OHOS_ARCH_LITE
26 #include "wifi_timer.h"
27 #endif
28 #include "wifi_errcode.h"
29 
30 #ifndef WIFI_MAC_LEN
31 #define WIFI_MAC_LEN 6
32 #endif
33 
34 namespace OHOS {
35 namespace Wifi {
36 
37 #ifndef NO_SANITIZE
38 #ifdef __has_attribute
39 #if __has_attribute(no_sanitize)
40 #define NO_SANITIZE(type) __attribute__((no_sanitize(type)))
41 #endif
42 #endif
43 #endif
44 
45 #ifndef NO_SANITIZE
46 #define NO_SANITIZE(type)
47 #endif
48 
49 constexpr int INVALID_FREQ_OR_CHANNEL = -1;
50 const uint32_t DECIMAL_NOTATION = 10;
51 
52 /* StaCallBackNameEventIdMap */
53 static std::map<std::string, int> g_staCallBackNameEventIdMap = {
54     { EVENT_STA_POWER_STATE_CHANGE, WIFI_CBK_MSG_STATE_CHANGE },
55     { EVENT_STA_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECTION_CHANGE },
56     { EVENT_STA_RSSI_STATE_CHANGE, WIFI_CBK_MSG_RSSI_CHANGE },
57     { EVENT_STA_WPS_STATE_CHANGE, WIFI_CBK_MSG_WPS_STATE_CHANGE },
58     { EVENT_STREAM_CHANGE, WIFI_CBK_MSG_STREAM_DIRECTION },
59     { EVENT_STA_DEVICE_CONFIG_CHANGE, WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE },
60     { EVENT_STA_SCAN_STATE_CHANGE, WIFI_CBK_MSG_SCAN_STATE_CHANGE },
61 };
62 
63 /* ApCallBackNameEventIdMap */
64 static std::map<std::string, int> g_apCallBackNameEventIdMap = {
65     { EVENT_HOTSPOT_STATE_CHANGE, WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE },
66     { EVENT_HOTSPOT_STA_JOIN, WIFI_CBK_MSG_HOTSPOT_STATE_JOIN },
67     { EVENT_HOTSPOT_STA_LEAVE, WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE },
68 };
69 
70 /* P2PCallBackNameEventIdMap */
71 static std::map<std::string, int> g_p2pCallBackNameEventIdMap = {
72     { EVENT_P2P_STATE_CHANGE, WIFI_CBK_MSG_P2P_STATE_CHANGE },
73     { EVENT_P2P_PERSISTENT_GROUP_CHANGE, WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE },
74     { EVENT_P2P_DEVICE_STATE_CHANGE, WIFI_CBK_MSG_THIS_DEVICE_CHANGE },
75     { EVENT_P2P_PEER_DEVICE_CHANGE, WIFI_CBK_MSG_PEER_CHANGE },
76     { EVENT_P2P_SERVICES_CHANGE, WIFI_CBK_MSG_SERVICE_CHANGE },
77     { EVENT_P2P_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECT_CHANGE },
78     { EVENT_P2P_DISCOVERY_CHANGE, WIFI_CBK_MSG_DISCOVERY_CHANGE },
79     { EVENT_P2P_ACTION_RESULT, WIFI_CBK_MSG_P2P_ACTION_RESULT },
80     { EVENT_P2P_CONFIG_CHANGE, WIFI_CBK_MSG_CFG_CHANGE },
81     { EVENT_P2P_GC_JOIN_GROUP, WIFI_CBK_MSG_P2P_GC_JOIN_GROUP},
82     { EVENT_P2P_GC_LEAVE_GROUP, WIFI_CBK_MSG_P2P_GC_LEAVE_GROUP},
83     { EVENT_P2P_PRIVATE_PEER_DEVICE_CHANGE, WIFI_CBK_MSG_PRIVATE_PEER_CHANGE},
84 };
85 
86 /**
87  * @Description MAC address anonymization
88  *
89  * <p> eg: a2:7c:b0:98:e3:92 -> a2:7c:**:**:**:92
90  *
91  * @param str - Input MAC address
92  * @return std::string - Processed MAC
93  */
94 std::string MacAnonymize(const std::string str);
95 
96 /**
97  * @Description MAC address anonymization
98  *
99  * <p> eg: 192.168.0.1 -> 192.***.*.1
100  *
101  * @param str - Input MAC address
102  * @return std::string - Processed MAC
103  */
104 std::string IpAnonymize(const std::string str);
105 
106 /**
107  * @Description Ssid anonymization
108  *
109  * <p> a) Length less than or equal to 2, all bits are hidden;
110  * b) Length less than or equal to 4, hiding the middle bit;
111  * c) Length less than or equal to 8, hiding 3 bits from the second bit;
112  * d) Length greater than or equal to 9, showing the first and last three bits, the middle bits are hidden
113  * <p> eg:
114  * 1 -> *
115  * 12 -> **
116  * 123 -> 1*3
117  * 1234 -> 1**4
118  * 12345 -> 1***5
119  * 123456 -> 1***56
120  * 1234567 -> 1***567
121  * 12345678 -> 1***5678
122  * 123456789 -> 123***789
123  * 12345678910 -> 123*****910
124  *
125  * @param str - Input ssid
126  * @return std::string - Processed ssid
127  */
128 std::string SsidAnonymize(const std::string str);
129 
130 /**
131  * @Description Password anonymization
132  *
133  * Length greater than or equal to 8
134  * <p> eg: 12345678 -> 12****78
135  *
136  * @param str - Input Password
137  * @return std::string - Processed Password
138  */
139 std::string PassWordAnonymize(const std::string str);
140 
141 /**
142  * @Description Converting string MAC to a C-style MAC address
143  *
144  * @param strMac - Input MAC address
145  * @param mac - conversion result
146  * @return errno_t - EOK for success, failure for other values.
147  */
148 errno_t MacStrToArray(const std::string& strMac, unsigned char mac[WIFI_MAC_LEN]);
149 
150 /**
151  * @Description Converting C-style MAC to a string MAC
152  *
153  * @param mac - Input MAC address
154  * @return string - conversion result.
155  */
156 std::string MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN]);
157 
158 /**
159  * @Description Check whether the array of MAC address is empty
160  *
161  * @param mac - Input MAC address
162  * @return bool - true: empty, false: not empty
163  */
164 bool IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN]);
165 
166 /**
167  * @Description Converting a string IP Address to an integer IP address
168  *
169  * @param strIp - Input string IP address
170  * @return unsigned int - integer IP address
171  */
172 unsigned int Ip2Number(const std::string& strIp);
173 
174 /**
175  * @Description Converting an integer IP address to a string IP Address
176  *
177  * @param intIp - Input integer IP address
178  * @return string - string IP address
179  */
180 std::string Number2Ip(unsigned int intIp);
181 
182 /**
183  * @Description Splitting strings by delimiter
184  *
185  * @param str - Input string
186  * @param delim - Split delimiter
187  * @return std::vector<std::string> - Split result
188  */
189 std::vector<std::string> StrSplit(const std::string& str, const std::string& delim);
190 
191 /**
192  * @Description GetElapsedMicrosecondsSinceBoot
193  *
194  * @return microseconds;
195  */
196 int64_t GetElapsedMicrosecondsSinceBoot();
197 
198 #ifndef OHOS_ARCH_LITE
199 /**
200  * @Description get bundle name, it can only be obtained at the interfaces layer.
201  *
202  * @return bool - bundle name
203  */
204 std::string GetBundleName();
205 
206 std::string GetBundleAppIdByBundleName(const int userId, const std::string &bundleName);
207 /**
208  * @Description get bundle name by uid
209  *
210  * @param int - uid
211  * @param std::string - bundle name
212  * @return ErrCode - operation result
213  */
214 ErrCode GetBundleNameByUid(const int uid, std::string &bundleName);
215 
216 /**
217  * @Description get calling pid
218  *
219  * @return int - calling pid
220  */
221 int GetCallingPid();
222 
223 /**
224  * @Description get calling uid
225  *
226  * @return int - calling uid
227  */
228 int GetCallingUid();
229 
230 /**
231  * @Description get calling token id
232  *
233  * @return int - calling token id
234  */
235 int GetCallingTokenId();
236 
237 /**
238  * @Description by Process uid ,the app is a wifi broker process
239  *
240  * @param uid - Input uid
241  * @param pid - Input pid
242  * @return string - Returns processname
243  */
244 std::string GetBrokerProcessNameByPid(const int uid, const int pid);
245 
246 /**
247  * @Description set Process pid and processname
248  *
249  * @param pid - Input pid
250  * @param processName - Input processName
251  * @return void
252  */
253 void SetWifiBrokerProcess(int pid, std::string processName);
254 
255 /**
256  * @Description Time consuming statistics
257  *
258  */
259 class TimeStats final {
260 public:
261     TimeStats(const std::string desc);
262     TimeStats() = delete;
263     ~TimeStats();
264 
265 private:
266     std::string m_desc;
267     std::chrono::steady_clock::time_point m_startTime;
268 };
269 #endif
270 
271 /**
272  * @Description Convert frequency to channel
273  *
274  * @return int - channel
275  */
276 int FrequencyToChannel(int freq);
277 
278 /**
279  * @Description Convert channel to frequency
280  *
281  * @return int - frequency
282  */
283 int ChannelToFrequency(int channel);
284 bool IsOtherVapConnect();
285 int HexString2Byte(const char *hex, uint8_t *buf, size_t len);
286 void Byte2HexString(const uint8_t* byte, uint8_t bytesLen, char* hexstr, uint8_t hexstrLen);
287 bool DecodeBase64(const std::string &input, std::vector<uint8_t> &output);
288 std::string EncodeBase64(const std::vector<uint8_t> &input);
289 std::vector<std::string> getAuthInfo(const std::string &input, const std::string &delimiter);
290 std::string HexToString(const std::string &str);
291 std::string StringToHex(const std::string &data);
292 int CheckDataLegal(std::string &data, int base = DECIMAL_NOTATION);
293 int CheckDataLegalBin(const std::string &data);
294 int CheckDataLegalHex(const std::string &data);
295 unsigned int CheckDataToUint(std::string &data, int base = DECIMAL_NOTATION);
296 long long CheckDataTolonglong(std::string &data, int base = DECIMAL_NOTATION);
297 }  // namespace Wifi
298 }  // namespace OHOS
299 #endif