• 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 <chrono>
20 #include <string>
21 #include <vector>
22 #include "securec.h"
23 
24 #ifndef WIFI_MAC_LEN
25 #define WIFI_MAC_LEN 6
26 #endif
27 
28 namespace OHOS {
29 namespace Wifi {
30 
31 constexpr int INVALID_FREQ_OR_CHANNEL = -1;
32 
33 /**
34  * @Description MAC address anonymization
35  *
36  * <p> eg: a2:7c:b0:98:e3:92 -> a2:7c:**:**:**:92
37  *
38  * @param str - Input MAC address
39  * @return std::string - Processed MAC
40  */
41 std::string MacAnonymize(const std::string str);
42 
43 /**
44  * @Description MAC address anonymization
45  *
46  * <p> eg: 192.168.0.1 -> 192.***.*.1
47  *
48  * @param str - Input MAC address
49  * @return std::string - Processed MAC
50  */
51 std::string IpAnonymize(const std::string str);
52 
53 /**
54  * @Description Ssid anonymization
55  *
56  * <p> a) Length less than or equal to 2, all bits are hidden;
57  * b) Length less than or equal to 4, hiding the middle bit;
58  * c) Length less than or equal to 8, hiding 3 bits from the second bit;
59  * d) Length greater than or equal to 9, showing the first and last three bits, the middle bits are hidden
60  * <p> eg:
61  * 1 -> *
62  * 12 -> **
63  * 123 -> 1*3
64  * 1234 -> 1**4
65  * 12345 -> 1***5
66  * 123456 -> 1***56
67  * 1234567 -> 1***567
68  * 12345678 -> 1***5678
69  * 123456789 -> 123***789
70  * 12345678910 -> 123*****910
71  *
72  * @param str - Input ssid
73  * @return std::string - Processed ssid
74  */
75 std::string SsidAnonymize(const std::string str);
76 
77 /**
78  * @Description Converting string MAC to a C-style MAC address
79  *
80  * @param strMac - Input MAC address
81  * @param mac - conversion result
82  * @return errno_t - EOK for success, failure for other values.
83  */
84 errno_t MacStrToArray(const std::string& strMac, unsigned char mac[WIFI_MAC_LEN]);
85 
86 /**
87  * @Description Converting C-style MAC to a string MAC
88  *
89  * @param mac - Input MAC address
90  * @return string - conversion result.
91  */
92 std::string MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN]);
93 
94 /**
95  * @Description Check whether the array of MAC address is empty
96  *
97  * @param mac - Input MAC address
98  * @return bool - true: empty, false: not empty
99  */
100 bool IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN]);
101 
102 /**
103  * @Description Converting a string IP Address to an integer IP address
104  *
105  * @param strIp - Input string IP address
106  * @return unsigned int - integer IP address
107  */
108 unsigned int Ip2Number(const std::string& strIp);
109 
110 /**
111  * @Description Converting an integer IP address to a string IP Address
112  *
113  * @param intIp - Input integer IP address
114  * @return string - string IP address
115  */
116 std::string Number2Ip(unsigned int intIp);
117 
118 /**
119  * @Description Splitting strings by delimiter
120  *
121  * @param str - Input string
122  * @param delim - Split delimiter
123  * @return std::vector<std::string> - Split result
124  */
125 std::vector<std::string> StrSplit(const std::string& str, const std::string& delim);
126 
127 #ifndef OHOS_ARCH_LITE
128 /**
129  * @Description get bundle name, it can only be obtained at the interfaces layer.
130  *
131  * @return bool - bundle name
132  */
133 std::string GetBundleName();
134 
135 /**
136  * @Description Check whether the app is a system app
137  *
138  * @return bool - Returns true for yes, false for no.
139  */
140 bool IsSystemApp();
141 
142 /**
143  * @Description get calling pid
144  *
145  * @return int - calling pid
146  */
147 int GetCallingPid();
148 
149 /**
150  * @Description get calling uid
151  *
152  * @return int - calling uid
153  */
154 int GetCallingUid();
155 
156 /**
157  * @Description Check uid the app is a foregroud app
158  *
159  * @param uid - Input uid
160  * @return bool - Returns true for yes, false for no.
161  */
162 bool IsForegroundApp(const int uid);
163 
164 /**
165  * @Description Time consuming statistics
166  *
167  */
168 class TimeStats final {
169 public:
170     TimeStats(const std::string desc);
171     TimeStats() = delete;
172     ~TimeStats();
173 
174 private:
175     std::string m_desc;
176     std::chrono::steady_clock::time_point m_startTime;
177 };
178 #endif
179 
180 /**
181  * @Description Convert frequency to channel
182  *
183  * @return int - channel
184  */
185 int FrequencyToChannel(int freq);
186 
187 /**
188  * @Description Convert channel to frequency
189  *
190  * @return int - frequency
191  */
192 int ChannelToFrequency(int channel);
193 }  // namespace Wifi
194 }  // namespace OHOS
195 #endif