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