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