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 #include "wifi_msg.h" 30 31 #ifndef WIFI_MAC_LEN 32 #define WIFI_MAC_LEN 6 33 #endif 34 35 namespace OHOS { 36 namespace Wifi { 37 38 #ifndef NO_SANITIZE 39 #ifdef __has_attribute 40 #if __has_attribute(no_sanitize) 41 #define NO_SANITIZE(type) __attribute__((no_sanitize(type))) 42 #endif 43 #endif 44 #endif 45 46 #ifndef NO_SANITIZE 47 #define NO_SANITIZE(type) 48 #endif 49 50 constexpr int INVALID_FREQ_OR_CHANNEL = -1; 51 constexpr int SECOND_TO_MICROSECOND = 1000 * 1000; 52 const uint32_t DECIMAL_NOTATION = 10; 53 inline const uint8_t WIFI_SYSTEM_ID = 202; 54 inline const uint8_t P2P_SUB_SYSTEM_ID = 2; 55 inline const int SYSTEM_OFFSET = 21; 56 inline const int SUB_SYSTEM_OFFSET = 16; 57 inline const int SIGNAL_RECORD_12S = 12; 58 inline const int SIGNAL_RECORD_5S = 5; 59 inline const int BEACON_LENGTH_RSSI = 10; 60 inline const int8_t BEACON_LOST_RSSI0 = -128; 61 inline const int8_t BEACON_LOST_RSSI1 = 127; 62 inline const int SIGNAL_LEVEL_TWO = 2; 63 inline const int BEACON_ABNORMAL = 2; 64 65 enum BeaconLostType : int32_t { 66 SIGNAL_LEVEL_LOW = 0, 67 SIGNAL_LEVEL_HIGH = 1 68 }; 69 70 struct BeaconLostInfo { 71 int64_t time; 72 int cnt; 73 std::string bssid; 74 int rssi; 75 unsigned int rxBytes; 76 }; 77 78 struct BeaconAbnormalInfo { 79 int64_t time; 80 int cnt; 81 std::string bssid; 82 std::vector<uint8_t> rssiArr; 83 }; 84 85 /* StaCallBackNameEventIdMap */ 86 static std::map<std::string, int> g_staCallBackNameEventIdMap = { 87 { EVENT_STA_POWER_STATE_CHANGE, WIFI_CBK_MSG_STATE_CHANGE }, 88 { EVENT_STA_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECTION_CHANGE }, 89 { EVENT_STA_RSSI_STATE_CHANGE, WIFI_CBK_MSG_RSSI_CHANGE }, 90 { EVENT_STA_WPS_STATE_CHANGE, WIFI_CBK_MSG_WPS_STATE_CHANGE }, 91 { EVENT_STREAM_CHANGE, WIFI_CBK_MSG_STREAM_DIRECTION }, 92 { EVENT_STA_DEVICE_CONFIG_CHANGE, WIFI_CBK_MSG_DEVICE_CONFIG_CHANGE }, 93 { EVENT_STA_SCAN_STATE_CHANGE, WIFI_CBK_MSG_SCAN_STATE_CHANGE }, 94 { EVENT_STA_CANDIDATE_CONNECT_CHANGE, WIFI_CBK_MSG_CANDIDATE_CONNECT_CHANGE }, 95 }; 96 97 /* ApCallBackNameEventIdMap */ 98 static std::map<std::string, int> g_apCallBackNameEventIdMap = { 99 { EVENT_HOTSPOT_STATE_CHANGE, WIFI_CBK_MSG_HOTSPOT_STATE_CHANGE }, 100 { EVENT_HOTSPOT_STA_JOIN, WIFI_CBK_MSG_HOTSPOT_STATE_JOIN }, 101 { EVENT_HOTSPOT_STA_LEAVE, WIFI_CBK_MSG_HOTSPOT_STATE_LEAVE }, 102 }; 103 104 /* P2PCallBackNameEventIdMap */ 105 static std::map<std::string, int> g_p2pCallBackNameEventIdMap = { 106 { EVENT_P2P_STATE_CHANGE, WIFI_CBK_MSG_P2P_STATE_CHANGE }, 107 { EVENT_P2P_PERSISTENT_GROUP_CHANGE, WIFI_CBK_MSG_PERSISTENT_GROUPS_CHANGE }, 108 { EVENT_P2P_DEVICE_STATE_CHANGE, WIFI_CBK_MSG_THIS_DEVICE_CHANGE }, 109 { EVENT_P2P_PEER_DEVICE_CHANGE, WIFI_CBK_MSG_PEER_CHANGE }, 110 { EVENT_P2P_SERVICES_CHANGE, WIFI_CBK_MSG_SERVICE_CHANGE }, 111 { EVENT_P2P_CONN_STATE_CHANGE, WIFI_CBK_MSG_CONNECT_CHANGE }, 112 { EVENT_P2P_DISCOVERY_CHANGE, WIFI_CBK_MSG_DISCOVERY_CHANGE }, 113 { EVENT_P2P_ACTION_RESULT, WIFI_CBK_MSG_P2P_ACTION_RESULT }, 114 { EVENT_P2P_CONFIG_CHANGE, WIFI_CBK_MSG_CFG_CHANGE }, 115 { EVENT_P2P_GC_JOIN_GROUP, WIFI_CBK_MSG_P2P_GC_JOIN_GROUP}, 116 { EVENT_P2P_GC_LEAVE_GROUP, WIFI_CBK_MSG_P2P_GC_LEAVE_GROUP}, 117 { EVENT_P2P_PRIVATE_PEER_DEVICE_CHANGE, WIFI_CBK_MSG_PRIVATE_PEER_CHANGE}, 118 { EVENT_P2P_CHR_ERRCODE_REPORT, WIFI_CBK_MSG_P2P_CHR_ERRCODE_REPORT}, 119 }; 120 121 /** 122 * @Description MAC address anonymization 123 * 124 * <p> eg: a2:7c:b0:98:e3:92 -> a2:7c:**:**:**:92 125 * 126 * @param str - Input MAC address 127 * @return std::string - Processed MAC 128 */ 129 std::string MacAnonymize(const std::string str); 130 131 /** 132 * @Description MAC address anonymization 133 * 134 * <p> eg: 192.168.0.1 -> 192.***.*.1 135 * 136 * @param str - Input MAC address 137 * @return std::string - Processed MAC 138 */ 139 std::string IpAnonymize(const std::string str); 140 141 /** 142 * @Description Ssid anonymization 143 * 144 * <p> a) Length less than or equal to 2, all bits are hidden; 145 * b) Length less than or equal to 4, hiding the middle bit; 146 * c) Length less than or equal to 8, hiding 3 bits from the second bit; 147 * d) Length greater than or equal to 9, showing the first and last three bits, the middle bits are hidden 148 * <p> eg: 149 * 1 -> * 150 * 12 -> ** 151 * 123 -> 1*3 152 * 1234 -> 1**4 153 * 12345 -> 1***5 154 * 123456 -> 1***56 155 * 1234567 -> 1***567 156 * 12345678 -> 1***5678 157 * 123456789 -> 123***789 158 * 12345678910 -> 123*****910 159 * 160 * @param str - Input ssid 161 * @return std::string - Processed ssid 162 */ 163 std::string SsidAnonymize(const std::string str); 164 165 /** 166 * @Description Password anonymization 167 * 168 * Length greater than or equal to 8 169 * <p> eg: 12345678 -> 12****78 170 * 171 * @param str - Input Password 172 * @return std::string - Processed Password 173 */ 174 std::string PassWordAnonymize(const std::string str); 175 176 /** 177 * @Description Converting string MAC to a C-style MAC address 178 * 179 * @param strMac - Input MAC address 180 * @param mac - conversion result 181 * @return errno_t - EOK for success, failure for other values. 182 */ 183 errno_t MacStrToArray(const std::string& strMac, unsigned char mac[WIFI_MAC_LEN]); 184 185 /** 186 * @Description Converting C-style MAC to a string MAC 187 * 188 * @param mac - Input MAC address 189 * @return string - conversion result. 190 */ 191 std::string MacArrayToStr(const unsigned char mac[WIFI_MAC_LEN]); 192 193 /** 194 * @Description Check whether the array of MAC address is empty 195 * 196 * @param mac - Input MAC address 197 * @return bool - true: empty, false: not empty 198 */ 199 bool IsMacArrayEmpty(const unsigned char mac[WIFI_MAC_LEN]); 200 201 /** 202 * @Description Converting a string IP Address to an integer IP address 203 * 204 * @param strIp - Input string IP address 205 * @return unsigned int - integer IP address 206 */ 207 unsigned int Ip2Number(const std::string& strIp); 208 209 /** 210 * @Description Converting an integer IP address to a string IP Address 211 * 212 * @param intIp - Input integer IP address 213 * @return string - string IP address 214 */ 215 std::string Number2Ip(unsigned int intIp); 216 217 /** 218 * @Description Splitting strings by delimiter 219 * 220 * @param str - Input string 221 * @param delim - Split delimiter 222 * @return std::vector<std::string> - Split result 223 */ 224 std::vector<std::string> StrSplit(const std::string& str, const std::string& delim); 225 226 /** 227 * @brief Returns the time, in seconds, since 1970-01-01 00:00:00 GMT 228 * 229 * @return Returns the time, in seconds 230 */ 231 int64_t GetCurrentTimeSeconds(); 232 233 /** 234 * @Description GetCurrentTimeSeconds since 1970-01-01 00:00:00 GMT 235 * 236 * @return Returns the time, in milliseconds 237 */ 238 int64_t GetCurrentTimeMilliSeconds(); 239 /** 240 * @Description GetElapsedMicrosecondsSinceBoot 241 * 242 * @return microseconds; 243 */ 244 int64_t GetElapsedMicrosecondsSinceBoot(); 245 246 #ifndef OHOS_ARCH_LITE 247 /** 248 * @Description get bundle name, it can only be obtained at the interfaces layer. 249 * 250 * @return bool - bundle name 251 */ 252 std::string GetBundleName(); 253 254 std::string GetBundleAppIdByBundleName(const int callingUid, const std::string &bundleName); 255 256 /** 257 * @Description whether the bundle is installed. 258 * 259 * @param std::string - bundle name 260 * @return bool - true: installed 261 */ 262 bool IsBundleInstalled(const std::string &bundleName); 263 264 /** 265 * @Description get bundle name by uid 266 * 267 * @param int - uid 268 * @param std::string - bundle name 269 * @return ErrCode - operation result 270 */ 271 ErrCode GetBundleNameByUid(const int uid, std::string &bundleName); 272 273 /** 274 * @Description get bundle name by uid 275 * 276 * @param std::vector<std::string> - bundle name list 277 * @return ErrCode - operation result 278 */ 279 ErrCode GetAllBundleName(std::vector<std::string> &bundleNameList); 280 281 /** 282 * @Description get calling pid 283 * 284 * @return int - calling pid 285 */ 286 int GetCallingPid(); 287 288 /** 289 * @Description get calling uid 290 * 291 * @return int - calling uid 292 */ 293 int GetCallingUid(); 294 295 /** 296 * @Description get calling token id 297 * 298 * @return int - calling token id 299 */ 300 int GetCallingTokenId(); 301 302 /** 303 * @Description by Process uid ,the app is a wifi broker process 304 * 305 * @param uid - Input uid 306 * @param pid - Input pid 307 * @return string - Returns processname 308 */ 309 std::string GetBrokerProcessNameByPid(const int uid, const int pid); 310 311 /** 312 * @Description set Process pid and processname 313 * 314 * @param pid - Input pid 315 * @param processName - Input processName 316 * @return void 317 */ 318 void SetWifiBrokerProcess(int pid, std::string processName); 319 320 /** 321 * @Description Time consuming statistics 322 * 323 */ 324 class TimeStats final { 325 public: 326 TimeStats(const std::string desc); 327 TimeStats() = delete; 328 ~TimeStats(); 329 330 private: 331 std::string m_desc; 332 std::chrono::steady_clock::time_point m_startTime; 333 }; 334 #endif 335 336 /** 337 * @Description Convert frequency to channel 338 * 339 * @return int - channel 340 */ 341 int FrequencyToChannel(int freq); 342 343 /** 344 * @Description Convert channel to frequency 345 * 346 * @return int - frequency 347 */ 348 int ChannelToFrequency(int channel); 349 bool IsOtherVapConnect(); 350 bool IsBeaconLost(std::string bssid, WifiSignalPollInfo checkInfo); 351 bool IsBeaconAbnormal(std::string bssid, WifiSignalPollInfo checkInfo); 352 int HexString2Byte(const char *hex, uint8_t *buf, size_t len); 353 void Byte2HexString(const uint8_t* byte, uint8_t bytesLen, char* hexstr, uint8_t hexstrLen); 354 bool DecodeBase64(const std::string &input, std::vector<uint8_t> &output); 355 std::string EncodeBase64(const std::vector<uint8_t> &input); 356 std::vector<std::string> GetSplitInfo(const std::string &input, const std::string &delimiter); 357 std::string HexToString(const std::string &str); 358 std::string StringToHex(const std::string &data); 359 int CheckDataLegal(std::string &data, int base = DECIMAL_NOTATION); 360 int CheckDataLegalBin(const std::string &data); 361 int CheckDataLegalHex(const std::string &data); 362 unsigned int CheckDataToUint(std::string &data, int base = DECIMAL_NOTATION); 363 long long CheckDataTolonglong(std::string &data, int base = DECIMAL_NOTATION); 364 uint32_t GenerateStandardErrCode(uint8_t subSystem, uint16_t errCode); 365 unsigned long StringToUlong(const std::string &word); 366 float StringToFloat(const std::string &word); 367 double StringToDouble(const std::string &word); 368 /** 369 * @Description Internal HiLinkNetwork Type To Bool 370 * 371 * @return bool - isHiLinkNetwork 372 */ 373 bool InternalHiLinkNetworkToBool(int isHiLinkNetwork); 374 375 std::string Ipv4IntAnonymize(uint32_t ipInt); 376 377 std::string Ipv6Anonymize(std::string str); 378 } // namespace Wifi 379 } // namespace OHOS 380 #endif