• 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_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 
26 #ifndef WIFI_MAC_LEN
27 #define WIFI_MAC_LEN 6
28 #endif
29 
30 namespace OHOS {
31 namespace Wifi {
32 
33 #ifndef NO_SANITIZE
34 #ifdef __has_attribute
35 #if __has_attribute(no_sanitize)
36 #define NO_SANITIZE(type) __attribute__((no_sanitize(type)))
37 #endif
38 #endif
39 #endif
40 
41 #ifndef NO_SANITIZE
42 #define NO_SANITIZE(type)
43 #endif
44 
45 constexpr int INVALID_FREQ_OR_CHANNEL = -1;
46 
47 /* StaCallBackNameEventIdMap */
48 static std::map<std::string, int> g_staCallBackNameEventIdMap = {
49     { EVENT_STA_POWER_STATE_CHANGE, WIFI_CBK_MSG_STATE_CHANGE },
50     { EVENT_STA_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECTION_CHANGE },
51     { EVENT_STA_RSSI_STATE_CHANGE, WIFI_CBK_MSG_RSSI_CHANGE },
52     { EVENT_STA_WPS_STATE_CHANGE, WIFI_CBK_MSG_WPS_STATE_CHANGE },
53     { EVENT_STREAM_CHANGE, WIFI_CBK_MSG_STREAM_DIRECTION },
54     { EVENT_STA_DEVICE_CONFIG_CHANGE, WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE },
55     { EVENT_STA_SCAN_STATE_CHANGE, WIFI_CBK_MSG_SCAN_STATE_CHANGE },
56 };
57 
58 /* ApCallBackNameEventIdMap */
59 static std::map<std::string, int> g_apCallBackNameEventIdMap = {
60     { EVENT_HOTSPOT_STATE_CHANGE, WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE },
61     { EVENT_HOTSPOT_STA_JOIN, WIFI_CBK_MSG_HOTSPOT_STATE_JOIN },
62     { EVENT_HOTSPOT_STA_LEAVE, WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE },
63 };
64 
65 /* P2PCallBackNameEventIdMap */
66 static std::map<std::string, int> g_p2pCallBackNameEventIdMap = {
67     { EVENT_P2P_STATE_CHANGE, WIFI_CBK_MSG_P2P_STATE_CHANGE },
68     { EVENT_P2P_PERSISTENT_GROUP_CHANGE, WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE },
69     { EVENT_P2P_DEVICE_STATE_CHANGE, WIFI_CBK_MSG_THIS_DEVICE_CHANGE },
70     { EVENT_P2P_PEER_DEVICE_CHANGE, WIFI_CBK_MSG_PEER_CHANGE },
71     { EVENT_P2P_SERVICES_CHANGE, WIFI_CBK_MSG_SERVICE_CHANGE },
72     { EVENT_P2P_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECT_CHANGE },
73     { EVENT_P2P_DISCOVERY_CHANGE, WIFI_CBK_MSG_DISCOVERY_CHANGE },
74     { EVENT_P2P_ACTION_RESULT, WIFI_CBK_MSG_P2P_ACTION_RESULT },
75     { EVENT_P2P_CONFIG_CHANGE, WIFI_CBK_MSG_CFG_CHANGE },
76 };
77 
78 /**
79  * @Description MAC address anonymization
80  *
81  * <p> eg: a2:7c:b0:98:e3:92 -> a2:7c:**:**:**:92
82  *
83  * @param str - Input MAC address
84  * @return std::string - Processed MAC
85  */
86 std::string MacAnonymize(const std::string str);
87 
88 /**
89  * @Description MAC address anonymization
90  *
91  * <p> eg: 192.168.0.1 -> 192.***.*.1
92  *
93  * @param str - Input MAC address
94  * @return std::string - Processed MAC
95  */
96 std::string IpAnonymize(const std::string str);
97 
98 /**
99  * @Description Ssid anonymization
100  *
101  * <p> a) Length less than or equal to 2, all bits are hidden;
102  * b) Length less than or equal to 4, hiding the middle bit;
103  * c) Length less than or equal to 8, hiding 3 bits from the second bit;
104  * d) Length greater than or equal to 9, showing the first and last three bits, the middle bits are hidden
105  * <p> eg:
106  * 1 -> *
107  * 12 -> **
108  * 123 -> 1*3
109  * 1234 -> 1**4
110  * 12345 -> 1***5
111  * 123456 -> 1***56
112  * 1234567 -> 1***567
113  * 12345678 -> 1***5678
114  * 123456789 -> 123***789
115  * 12345678910 -> 123*****910
116  *
117  * @param str - Input ssid
118  * @return std::string - Processed ssid
119  */
120 std::string SsidAnonymize(const std::string str);
121 
122 /**
123  * @Description Converting string MAC to a C-style MAC address
124  *
125  * @param strMac - Input MAC address
126  * @param mac - conversion result
127  * @return errno_t - EOK for success, failure for other values.
128  */
129 errno_t MacStrToArray(const std::string& strMac, unsigned char mac[WIFI_MAC_LEN]);
130 
131 /**
132  * @Description Converting C-style MAC to a string MAC
133  *
134  * @param mac - Input MAC address
135  * @return string - conversion result.
136  */
137 std::string MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN]);
138 
139 /**
140  * @Description Check whether the array of MAC address is empty
141  *
142  * @param mac - Input MAC address
143  * @return bool - true: empty, false: not empty
144  */
145 bool IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN]);
146 
147 /**
148  * @Description Converting a string IP Address to an integer IP address
149  *
150  * @param strIp - Input string IP address
151  * @return unsigned int - integer IP address
152  */
153 unsigned int Ip2Number(const std::string& strIp);
154 
155 /**
156  * @Description Converting an integer IP address to a string IP Address
157  *
158  * @param intIp - Input integer IP address
159  * @return string - string IP address
160  */
161 std::string Number2Ip(unsigned int intIp);
162 
163 /**
164  * @Description Splitting strings by delimiter
165  *
166  * @param str - Input string
167  * @param delim - Split delimiter
168  * @return std::vector<std::string> - Split result
169  */
170 std::vector<std::string> StrSplit(const std::string& str, const std::string& delim);
171 
172 #ifndef OHOS_ARCH_LITE
173 /**
174  * @Description get bundle name, it can only be obtained at the interfaces layer.
175  *
176  * @return bool - bundle name
177  */
178 std::string GetBundleName();
179 
180 /**
181  * @Description Check whether the app is a system app
182  *
183  * @return bool - Returns true for yes, false for no.
184  */
185 bool IsSystemApp();
186 
187 /**
188  * @Description get calling pid
189  *
190  * @return int - calling pid
191  */
192 int GetCallingPid();
193 
194 /**
195  * @Description get calling uid
196  *
197  * @return int - calling uid
198  */
199 int GetCallingUid();
200 
201 /**
202  * @Description get calling token id
203  *
204  * @return int - calling token id
205  */
206 int GetCallingTokenId();
207 
208 /**
209  * @Description Check uid the app is a foregroud app
210  *
211  * @param uid - Input uid
212  * @return bool - Returns true for yes, false for no.
213  */
214 bool IsForegroundApp(const int uid);
215 
216 /**
217  * @Description Time consuming statistics
218  *
219  */
220 class TimeStats final {
221 public:
222     TimeStats(const std::string desc);
223     TimeStats() = delete;
224     ~TimeStats();
225 
226 private:
227     std::string m_desc;
228     std::chrono::steady_clock::time_point m_startTime;
229 };
230 #endif
231 
232 /**
233  * @Description Convert frequency to channel
234  *
235  * @return int - channel
236  */
237 int FrequencyToChannel(int freq);
238 
239 /**
240  * @Description Convert channel to frequency
241  *
242  * @return int - frequency
243  */
244 int ChannelToFrequency(int channel);
245 }  // namespace Wifi
246 }  // namespace OHOS
247 #endif