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 #ifndef OHOS_WIFI_MSG_H 16 #define OHOS_WIFI_MSG_H 17 18 #include <algorithm> 19 #include <cstring> 20 #include <iomanip> 21 #include <map> 22 #include <sstream> 23 #include <string> 24 #include <vector> 25 #include "ip_tools.h" 26 27 namespace OHOS { 28 namespace Wifi { 29 #define WIFI_COUNTRY_CODE_LEN 2 30 #define WEPKEYS_SIZE 4 31 #define INVALID_NETWORK_ID (-1) 32 #define IPV4_ADDRESS_TYPE 0 33 #define IPV6_ADDRESS_TYPE 1 34 35 const std::string KEY_MGMT_NONE = "NONE"; 36 const std::string KEY_MGMT_WEP = "WEP"; 37 const std::string KEY_MGMT_WPA_PSK = "WPA-PSK"; 38 const std::string KEY_MGMT_SAE = "SAE"; 39 40 enum class SupplicantState { 41 DISCONNECTED = 0, 42 INTERFACE_DISABLED = 1, 43 INACTIVE = 2, 44 SCANNING = 3, 45 AUTHENTICATING = 4, 46 ASSOCIATING = 5, 47 ASSOCIATED = 6, 48 FOUR_WAY_HANDSHAKE = 7, 49 GROUP_HANDSHAKE = 8, 50 COMPLETED = 9, 51 UNKNOWN = 10, 52 53 INVALID = 0xFF, 54 }; 55 56 enum class DetailedState { 57 AUTHENTICATING = 0, 58 BLOCKED = 1, 59 CAPTIVE_PORTAL_CHECK = 2, 60 CONNECTED = 3, 61 CONNECTING = 4, 62 DISCONNECTED = 5, 63 DISCONNECTING = 6, 64 FAILED = 7, 65 IDLE = 8, 66 OBTAINING_IPADDR = 9, 67 WORKING = 10, 68 NOTWORKING = 11, 69 SCANNING = 12, 70 SUSPENDED = 13, 71 VERIFYING_POOR_LINK = 14, 72 PASSWORD_ERROR = 15, 73 CONNECTION_REJECT = 16, 74 CONNECTION_FULL = 17, 75 CONNECTION_TIMEOUT = 18, 76 OBTAINING_IPADDR_FAIL = 19, 77 INVALID = 0xFF, 78 }; 79 80 enum ConnState { 81 /** The device is searching for an available AP. */ 82 SCANNING, 83 84 /** The Wi-Fi connection is being set up. */ 85 CONNECTING, 86 87 /** The Wi-Fi connection is being authenticated. */ 88 AUTHENTICATING, 89 90 /** The IP address of the Wi-Fi connection is being obtained. */ 91 OBTAINING_IPADDR, 92 93 /** The Wi-Fi connection has been set up. */ 94 CONNECTED, 95 96 /** The Wi-Fi connection is being torn down. */ 97 DISCONNECTING, 98 99 /** The Wi-Fi connection has been torn down. */ 100 DISCONNECTED, 101 102 /** Failed to set up the Wi-Fi connection. */ 103 UNKNOWN 104 }; 105 106 struct WifiLinkedInfo { 107 int networkId; 108 std::string ssid; 109 std::string bssid; 110 int rssi; /* signal level */ 111 int band; /* 2.4G / 5G */ 112 int frequency; 113 int linkSpeed; /* units: Mbps */ 114 std::string macAddress; 115 unsigned int ipAddress; 116 ConnState connState; 117 bool ifHiddenSSID; 118 int rxLinkSpeed; /* Downstream network speed */ 119 int txLinkSpeed; /* Upstream network speed */ 120 int chload; 121 int snr; /* Signal-to-Noise Ratio */ 122 int isDataRestricted; 123 std::string portalUrl; 124 SupplicantState supplicantState; /* wpa_supplicant state */ 125 DetailedState detailedState; /* connection state */ 126 WifiLinkedInfoWifiLinkedInfo127 WifiLinkedInfo() 128 { 129 networkId = INVALID_NETWORK_ID; 130 rssi = 0; 131 band = 0; 132 frequency = 0; 133 linkSpeed = 0; 134 ipAddress = 0; 135 connState = ConnState::UNKNOWN; 136 ifHiddenSSID = false; 137 chload = 0; 138 snr = 0; 139 isDataRestricted = 0; 140 supplicantState = SupplicantState::INVALID; 141 detailedState = DetailedState::INVALID; 142 } 143 }; 144 145 /* use WPS type */ 146 enum class SetupMethod { 147 PBC = 0, 148 DISPLAY = 1, 149 KEYPAD = 2, 150 LABEL = 3, 151 INVALID = 4, 152 }; 153 154 /* WPS config */ 155 struct WpsConfig { 156 SetupMethod setup; /* WPS type */ 157 std::string pin; /* pin code */ 158 std::string bssid; /* KEYPAD mode pin code */ 159 WpsConfigWpsConfig160 WpsConfig() 161 { 162 setup = SetupMethod::INVALID; 163 } 164 }; 165 166 enum class WifiDeviceConfigStatus { 167 ENABLED, /* enable */ 168 DISABLED, /* disabled */ 169 UNKNOWN 170 }; 171 172 enum class AssignIpMethod { DHCP, STATIC, UNASSIGNED }; 173 174 class WifiIpAddress { 175 public: 176 int family; /* ip type */ 177 unsigned int addressIpv4; /* IPv4 */ 178 std::vector<unsigned char> addressIpv6; /* IPv6 */ 179 WifiIpAddress()180 WifiIpAddress() 181 { 182 family = -1; 183 addressIpv4 = 0; 184 } 185 ~WifiIpAddress()186 ~WifiIpAddress() 187 {} 188 GetIpv4Address()189 std::string GetIpv4Address() 190 { 191 return IpTools::ConvertIpv4Address(addressIpv4); 192 } 193 SetIpv4Address(const std::string & address)194 void SetIpv4Address(const std::string &address) 195 { 196 family = IPV4_ADDRESS_TYPE; 197 addressIpv4 = IpTools::ConvertIpv4Address(address); 198 return; 199 } 200 GetIpv6Address()201 std::string GetIpv6Address() 202 { 203 return IpTools::ConvertIpv6Address(addressIpv6); 204 } 205 SetIpv6Address(const std::string & address)206 void SetIpv6Address(const std::string &address) 207 { 208 family = IPV6_ADDRESS_TYPE; 209 IpTools::ConvertIpv6Address(address, addressIpv6); 210 return; 211 } 212 }; 213 214 class WifiLinkAddress { 215 public: 216 WifiIpAddress address; /* IP address */ 217 int prefixLength; 218 int flags; 219 int scope; 220 WifiLinkAddress()221 WifiLinkAddress() 222 { 223 prefixLength = 0; 224 flags = 0; 225 scope = 0; 226 } 227 ~WifiLinkAddress()228 ~WifiLinkAddress() 229 {} 230 }; 231 232 class StaticIpAddress { 233 public: 234 WifiLinkAddress ipAddress; 235 WifiIpAddress gateway; 236 WifiIpAddress dnsServer1; /* main DNS */ 237 WifiIpAddress dnsServer2; /* backup DNS */ 238 std::string domains; 239 GetIpv4Mask()240 std::string GetIpv4Mask() 241 { 242 return IpTools::ConvertIpv4Mask(ipAddress.prefixLength); 243 } 244 GetIpv6Mask()245 std::string GetIpv6Mask() 246 { 247 return IpTools::ConvertIpv6Mask(ipAddress.prefixLength); 248 } 249 }; 250 251 class WifiIpConfig { 252 public: 253 AssignIpMethod assignMethod; 254 StaticIpAddress staticIpAddress; 255 WifiIpConfig()256 WifiIpConfig() 257 { 258 assignMethod = AssignIpMethod::DHCP; 259 } ~WifiIpConfig()260 ~WifiIpConfig() 261 {} 262 }; 263 264 class WifiEapConfig { 265 public: 266 std::string eap; /* EAP mode Encryption Mode: PEAP/TLS/TTLS/PWD/SIM/AKA/AKA */ 267 std::string identity; /* EAP mode identity */ 268 std::string password; /* EAP mode password */ 269 }; 270 271 enum class ConfigureProxyMethod { AUTOCONFIGUE, MANUALCONFIGUE, CLOSED }; 272 273 class AutoProxyConfig { 274 public: 275 std::string pacWebAddress; 276 }; 277 278 class ManualProxyConfig { 279 public: 280 std::string serverHostName; 281 int serverPort; 282 std::string exclusionObjectList; 283 GetExclusionObjectList(std::vector<std::string> & exclusionList)284 void GetExclusionObjectList(std::vector<std::string> &exclusionList) 285 { 286 IpTools::GetExclusionObjectList(exclusionObjectList, exclusionList); 287 return; 288 } 289 ManualProxyConfig()290 ManualProxyConfig() 291 { 292 serverPort = 0; 293 } ~ManualProxyConfig()294 ~ManualProxyConfig() 295 {} 296 }; 297 298 class WifiProxyConfig { 299 public: 300 ConfigureProxyMethod configureMethod; 301 AutoProxyConfig autoProxyConfig; 302 ManualProxyConfig manualProxyConfig; 303 WifiProxyConfig()304 WifiProxyConfig() 305 { 306 configureMethod = ConfigureProxyMethod::CLOSED; 307 } ~WifiProxyConfig()308 ~WifiProxyConfig() 309 {} 310 }; 311 312 enum class WifiPrivacyConfig { RANDOMMAC, DEVICEMAC }; 313 314 /* Network configuration information */ 315 struct WifiDeviceConfig { 316 int networkId; 317 /* 0: CURRENT, using 1: DISABLED 2: ENABLED */ 318 int status; 319 /* mac address */ 320 std::string bssid; 321 /* network name */ 322 std::string ssid; 323 int band; 324 int channel; 325 int frequency; 326 /* Signal strength */ 327 int rssi; 328 /** 329 * signal level, 330 * rssi<=-100 level : 0 331 * (-100, -88] level : 1 332 * (-88, -77] level : 2 333 * (-66, -55] level : 3 334 * rssi>=-55 level : 4 335 */ 336 int level; 337 /* Is Passpoint network */ 338 bool isPasspoint; 339 /* is ephemeral network */ 340 bool isEphemeral; 341 /* WPA-PSK mode pre shared key */ 342 std::string preSharedKey; 343 /* Encryption Mode */ 344 std::string keyMgmt; 345 /* WEP mode key, max size: 4 */ 346 std::string wepKeys[WEPKEYS_SIZE]; 347 /* use WEP key index */ 348 int wepTxKeyIndex; 349 /* network priority */ 350 int priority; 351 /* is hidden network */ 352 bool hiddenSSID; 353 /* Random mac address */ 354 std::string macAddress; 355 WifiIpConfig wifiIpConfig; 356 WifiEapConfig wifiEapConfig; 357 WifiProxyConfig wifiProxyconfig; 358 WifiPrivacyConfig wifiPrivacySetting; 359 WifiDeviceConfigWifiDeviceConfig360 WifiDeviceConfig() 361 { 362 networkId = INVALID_NETWORK_ID; 363 status = static_cast<int>(WifiDeviceConfigStatus::DISABLED); 364 band = 0; 365 channel = 0; 366 frequency = 0; 367 level = 0; 368 isPasspoint = false; 369 isEphemeral = false; 370 wepTxKeyIndex = 0; 371 priority = 0; 372 hiddenSSID = false; 373 wifiPrivacySetting = WifiPrivacyConfig::RANDOMMAC; 374 rssi = -100; 375 } 376 }; 377 378 enum class WifiState { DISABLING = 0, DISABLED = 1, ENABLING = 2, ENABLED = 3, UNKNOWN = 4 }; 379 380 /* wps state */ 381 enum class WpsStartState { 382 START_PBC_SUCCEED = 0, 383 START_PIN_SUCCEED = 1, 384 START_PBC_FAILED = 2, 385 PBC_STARTED_ALREADY = 3, 386 START_PIN_FAILED = 4, 387 PIN_STARTED_ALREADY = 5, 388 STOP_PBC_SUCCEED = 6, 389 STOP_PBC_FAILED = 7, 390 STOP_PIN_SUCCEED = 8, 391 STOP_PIN_FAILED = 9, 392 START_PBC_FAILED_OVERLAP = 10, 393 START_WPS_FAILED = 11, 394 WPS_TIME_OUT = 12, 395 START_AP_PIN_SUCCEED = 13, 396 START_AP_PIN_FAILED = 14, 397 STOP_AP_PIN_SUCCEED = 15, 398 STOP_AP_PIN_FAILED = 16, 399 }; 400 401 enum class StreamDirection { 402 STREAM_DIRECTION_UP = 0, 403 STREAM_DIRECTION_DOWN = 1, 404 UNKNOWN, 405 }; 406 407 /* WifiProtectType */ 408 enum class WifiProtectType { 409 WIFI_PROTECT_MULTICAST = 0, 410 WIFI_PROTECT_COMMON = 1 411 }; 412 413 /* WifiProtectMode */ 414 enum class WifiProtectMode { 415 WIFI_PROTECT_FULL = 0, 416 WIFI_PROTECT_SCAN_ONLY = 1, 417 WIFI_PROTECT_FULL_HIGH_PERF = 2, 418 WIFI_PROTECT_FULL_LOW_LATENCY = 3, 419 WIFI_PROTECT_NO_HELD = 4 420 }; 421 422 /* DHCP info */ 423 struct IpInfo { 424 unsigned int ipAddress; /* ip address */ 425 unsigned int gateway; /* gate */ 426 unsigned int netmask; /* mask */ 427 unsigned int primaryDns; /* main dns */ 428 unsigned int secondDns; /* backup dns */ 429 unsigned int serverIp; /* DHCP server's address */ 430 unsigned int leaseDuration; 431 IpInfoIpInfo432 IpInfo() 433 { 434 ipAddress = 0; 435 gateway = 0; 436 netmask = 0; 437 primaryDns = 0; 438 secondDns = 0; 439 serverIp = 0; 440 leaseDuration = 0; 441 } 442 }; 443 } // namespace Wifi 444 } // namespace OHOS 445 #endif