• 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 #ifndef OHOS_WIFI_MSG_H
16 #define OHOS_WIFI_MSG_H
17 
18 #include <algorithm>
19 #include <cstring>
20 #include <ctime>
21 #include <iomanip>
22 #include <map>
23 #include <sstream>
24 #include <string>
25 #include <vector>
26 #include "ip_tools.h"
27 #include "wifi_scan_msg.h"
28 #include "securec.h"
29 
30 namespace OHOS {
31 namespace Wifi {
32 #define WIFI_COUNTRY_CODE_LEN 2
33 #define WEPKEYS_SIZE 4
34 #define INVALID_NETWORK_ID (-1)
35 #define WIFI_INVALID_UID (-1)
36 #define IPV4_ADDRESS_TYPE 0
37 #define IPV6_ADDRESS_TYPE 1
38 #define WIFI_PASSWORD_LEN 128
39 
40 const std::string KEY_MGMT_NONE = "NONE";
41 const std::string KEY_MGMT_WEP = "WEP";
42 const std::string KEY_MGMT_WPA_PSK = "WPA-PSK";
43 const std::string KEY_MGMT_SAE = "SAE";
44 const std::string KEY_MGMT_EAP = "WPA-EAP";
45 
46 const std::string EAP_METHOD_TLS = "TLS";
47 const std::string EAP_METHOD_TTLS = "TTLS";
48 const std::string EAP_METHOD_SIM = "SIM";
49 const std::string EAP_METHOD_PEAP = "PEAP";
50 
51 enum class SupplicantState {
52     DISCONNECTED = 0,
53     INTERFACE_DISABLED = 1,
54     INACTIVE = 2,
55     SCANNING = 3,
56     AUTHENTICATING = 4,
57     ASSOCIATING = 5,
58     ASSOCIATED = 6,
59     FOUR_WAY_HANDSHAKE = 7,
60     GROUP_HANDSHAKE = 8,
61     COMPLETED = 9,
62     UNKNOWN = 10,
63 
64     INVALID = 0xFF,
65 };
66 
67 enum class DetailedState {
68     AUTHENTICATING = 0,
69     BLOCKED = 1,
70     CAPTIVE_PORTAL_CHECK = 2,
71     CONNECTED = 3,
72     CONNECTING = 4,
73     DISCONNECTED = 5,
74     DISCONNECTING = 6,
75     FAILED = 7,
76     IDLE = 8,
77     OBTAINING_IPADDR = 9,
78     WORKING = 10,
79     NOTWORKING = 11,
80     SCANNING = 12,
81     SUSPENDED = 13,
82     VERIFYING_POOR_LINK = 14,
83     PASSWORD_ERROR = 15,
84     CONNECTION_REJECT = 16,
85     CONNECTION_FULL = 17,
86     CONNECTION_TIMEOUT = 18,
87     OBTAINING_IPADDR_FAIL = 19,
88     INVALID = 0xFF,
89 };
90 
91 enum ConnState {
92     /** The device is searching for an available AP. */
93     SCANNING,
94 
95     /** The Wi-Fi connection is being set up. */
96     CONNECTING,
97 
98     /** The Wi-Fi connection is being authenticated. */
99     AUTHENTICATING,
100 
101     /** The IP address of the Wi-Fi connection is being obtained. */
102     OBTAINING_IPADDR,
103 
104     /** The Wi-Fi connection has been set up. */
105     CONNECTED,
106 
107     /** The Wi-Fi connection is being torn down. */
108     DISCONNECTING,
109 
110     /** The Wi-Fi connection has been torn down. */
111     DISCONNECTED,
112 
113     /** Failed to set up the Wi-Fi connection. */
114     UNKNOWN
115 };
116 
117 enum class DisconnectedReason {
118     /* Default reason */
119     DISC_REASON_DEFAULT = 0,
120 
121     /* Password is wrong */
122     DISC_REASON_WRONG_PWD = 1,
123 
124     /* The number of router's connection reaches the maximum number limit */
125     DISC_REASON_CONNECTION_FULL = 2
126 };
127 
128 struct WifiLinkedInfo {
129     int networkId;
130     std::string ssid;
131     std::string bssid;
132     int rssi; /* signal level */
133     int band; /* 2.4G / 5G */
134     int frequency;
135     int linkSpeed; /* units: Mbps */
136     std::string macAddress;
137     int macType;
138     unsigned int ipAddress;
139     ConnState connState;
140     bool ifHiddenSSID;
141     int rxLinkSpeed; /* Downstream network speed */
142     int txLinkSpeed; /* Upstream network speed */
143     int chload;
144     int snr;                         /* Signal-to-Noise Ratio */
145     int isDataRestricted;
146     std::string portalUrl;
147     SupplicantState supplicantState; /* wpa_supplicant state */
148     DetailedState detailedState;     /* connection state */
149     int wifiStandard;                /* wifi standard */
150     int maxSupportedRxLinkSpeed;
151     int maxSupportedTxLinkSpeed;
152     WifiChannelWidth channelWidth; /* curr ap channel width */
153     int lastPacketDirection;
154     int lastRxPackets;
155     int lastTxPackets;
156     int retryedConnCount;
WifiLinkedInfoWifiLinkedInfo157     WifiLinkedInfo()
158     {
159         networkId = INVALID_NETWORK_ID;
160         rssi = 0;
161         band = 0;
162         frequency = 0;
163         linkSpeed = 0;
164         macType = 0;
165         ipAddress = 0;
166         connState = ConnState::UNKNOWN;
167         ifHiddenSSID = false;
168         rxLinkSpeed = 0;
169         txLinkSpeed = 0;
170         chload = 0;
171         snr = 0;
172         isDataRestricted = 0;
173         supplicantState = SupplicantState::INVALID;
174         detailedState = DetailedState::INVALID;
175         wifiStandard = 0;
176         maxSupportedRxLinkSpeed = 0;
177         maxSupportedTxLinkSpeed = 0;
178         channelWidth = WifiChannelWidth::WIDTH_INVALID;
179         lastPacketDirection = 0;
180         lastRxPackets = 0;
181         lastTxPackets = 0;
182         retryedConnCount = 0;
183     }
184 };
185 
186 /* use WPS type */
187 enum class SetupMethod {
188     PBC = 0,
189     DISPLAY = 1,
190     KEYPAD = 2,
191     LABEL = 3,
192     INVALID = 4,
193 };
194 
195 /* WPS config */
196 struct WpsConfig {
197     SetupMethod setup; /* WPS type */
198     std::string pin;   /* pin code */
199     std::string bssid; /* KEYPAD mode pin code */
200 
WpsConfigWpsConfig201     WpsConfig()
202     {
203         setup = SetupMethod::INVALID;
204     }
205 };
206 
207 enum class WifiDeviceConfigStatus {
208     ENABLED, /* enable */
209     DISABLED, /* disabled */
210     UNKNOWN
211 };
212 
213 enum class AssignIpMethod { DHCP, STATIC, UNASSIGNED };
214 
215 enum class ConfigChange {
216     CONFIG_ADD = 0,
217     CONFIG_UPDATE = 1,
218     CONFIG_REMOVE = 2,
219 };
220 
221 class WifiIpAddress {
222 public:
223     int family;                             /* ip type */
224     unsigned int addressIpv4;               /* IPv4 */
225     std::vector<unsigned char> addressIpv6; /* IPv6 */
226 
WifiIpAddress()227     WifiIpAddress()
228     {
229         family = -1;
230         addressIpv4 = 0;
231     }
232 
~WifiIpAddress()233     ~WifiIpAddress()
234     {}
235 
GetIpv4Address()236     std::string GetIpv4Address()
237     {
238         return IpTools::ConvertIpv4Address(addressIpv4);
239     }
240 
SetIpv4Address(const std::string & address)241     void SetIpv4Address(const std::string &address)
242     {
243         addressIpv4 = IpTools::ConvertIpv4Address(address);
244         if (addressIpv4 != 0) {
245             family = IPV4_ADDRESS_TYPE;
246         }
247         return;
248     }
249 
GetIpv6Address()250     std::string GetIpv6Address()
251     {
252         return IpTools::ConvertIpv6Address(addressIpv6);
253     }
254 
SetIpv6Address(const std::string & address)255     void SetIpv6Address(const std::string &address)
256     {
257         IpTools::ConvertIpv6Address(address, addressIpv6);
258         if (addressIpv6.size() != 0) {
259             family = IPV6_ADDRESS_TYPE;
260         }
261         return;
262     }
263 };
264 
265 class WifiLinkAddress {
266 public:
267     WifiIpAddress address; /* IP address */
268     int prefixLength;
269     int flags;
270     int scope;
271 
WifiLinkAddress()272     WifiLinkAddress()
273     {
274         prefixLength = 0;
275         flags = 0;
276         scope = 0;
277     }
278 
~WifiLinkAddress()279     ~WifiLinkAddress()
280     {}
281 };
282 
283 class StaticIpAddress {
284 public:
285     WifiLinkAddress ipAddress;
286     WifiIpAddress gateway;
287     WifiIpAddress dnsServer1; /* main DNS */
288     WifiIpAddress dnsServer2; /* backup DNS */
289     std::string domains;
290 
GetIpv4Mask()291     std::string GetIpv4Mask()
292     {
293         return IpTools::ConvertIpv4Mask(ipAddress.prefixLength);
294     }
295 
GetIpv6Mask()296     std::string GetIpv6Mask()
297     {
298         return IpTools::ConvertIpv6Mask(ipAddress.prefixLength);
299     }
300 };
301 
302 class WifiIpConfig {
303 public:
304     AssignIpMethod assignMethod;
305     StaticIpAddress staticIpAddress;
306 
WifiIpConfig()307     WifiIpConfig()
308     {
309         assignMethod = AssignIpMethod::DHCP;
310     }
~WifiIpConfig()311     ~WifiIpConfig()
312     {}
313 };
314 
315 enum class Phase2Method { NONE, PAP, MSCHAP, MSCHAPV2, GTC, SIM, AKA, AKA_PRIME };
316 
317 class WifiEapConfig {
318 public:
319     std::string eap;      /* EAP mode Encryption Mode: PEAP/TLS/TTLS/PWD/SIM/AKA/AKA */
320     std::string identity; /* EAP mode identity */
321     std::string password; /* EAP mode password */
322     std::string clientCert; /* EAP mode client certificate */
323     std::string privateKey; /* EAP mode client private key */
324     Phase2Method phase2Method;
325     std::vector<uint8_t> certEntry;
326     char certPassword[WIFI_PASSWORD_LEN];
327 
WifiEapConfig()328     WifiEapConfig()
329     {
330         phase2Method = Phase2Method::NONE;
331         (void) memset_s(certPassword, sizeof(certPassword), 0, sizeof(certPassword));
332     }
333     /**
334      * @Description convert Phase2Method to string
335      *
336      * @param eap - eap method
337      * @param method - phase2method
338      * @return string
339      */
340     static std::string Phase2MethodToStr(const std::string& eap, const int& method);
341 
342     /**
343      * @Description convert string to Phase2Method
344      *
345      * @param str - phase2method string
346      * @return Phase2Method
347      */
348     static Phase2Method Phase2MethodFromStr(const std::string& str);
349 };
350 
351 enum class ConfigureProxyMethod { CLOSED, AUTOCONFIGUE, MANUALCONFIGUE };
352 
353 class AutoProxyConfig {
354 public:
355     std::string pacWebAddress;
356 };
357 
358 class ManualProxyConfig {
359 public:
360     std::string serverHostName;
361     int serverPort;
362     std::string exclusionObjectList;
363 
GetExclusionObjectList(std::vector<std::string> & exclusionList)364     void GetExclusionObjectList(std::vector<std::string> &exclusionList)
365     {
366         IpTools::GetExclusionObjectList(exclusionObjectList, exclusionList);
367         return;
368     }
369 
ManualProxyConfig()370     ManualProxyConfig()
371     {
372         serverPort = 0;
373     }
~ManualProxyConfig()374     ~ManualProxyConfig()
375     {}
376 };
377 
378 class WifiProxyConfig {
379 public:
380     ConfigureProxyMethod configureMethod;
381     AutoProxyConfig autoProxyConfig;
382     ManualProxyConfig manualProxyConfig;
383 
WifiProxyConfig()384     WifiProxyConfig()
385     {
386         configureMethod = ConfigureProxyMethod::CLOSED;
387     }
~WifiProxyConfig()388     ~WifiProxyConfig()
389     {}
390 };
391 
392 enum class WifiPrivacyConfig { RANDOMMAC, DEVICEMAC };
393 
394 /* Network configuration information */
395 struct WifiDeviceConfig {
396     int networkId;
397     /* 0: CURRENT, using 1: DISABLED 2: ENABLED */
398     int status;
399     /* mac address */
400     std::string bssid;
401     /* bssid type. */
402     int bssidType;
403     /* network name */
404     std::string ssid;
405     int band;
406     int channel;
407     int frequency;
408     /* Signal strength */
409     int rssi;
410     /**
411      * signal level,
412      * rssi<=-100    level : 0
413      * (-100, -88]   level : 1
414      * (-88, -77]    level : 2
415      * (-66, -55]    level : 3
416      * rssi>=-55     level : 4
417      */
418     int level;
419     /* Is Passpoint network */
420     bool isPasspoint;
421     /* is ephemeral network */
422     bool isEphemeral;
423     /* WPA-PSK mode pre shared key */
424     std::string preSharedKey;
425     /* Encryption Mode */
426     std::string keyMgmt;
427     /* WEP mode key, max size: 4 */
428     std::string wepKeys[WEPKEYS_SIZE];
429     /* use WEP key index */
430     int wepTxKeyIndex;
431     /* network priority */
432     int priority;
433     /* is hidden network */
434     bool hiddenSSID;
435     /* Random mac address */
436     std::string macAddress;
437     int uid;
438     time_t lastConnectTime;
439     int numRebootsSinceLastUse;
440     int numAssociation;
441     int connFailedCount;
442     /* save select mac address */
443     std::string userSelectBssid;
444     WifiIpConfig wifiIpConfig;
445     WifiEapConfig wifiEapConfig;
446     WifiProxyConfig wifiProxyconfig;
447     WifiPrivacyConfig wifiPrivacySetting;
448 
WifiDeviceConfigWifiDeviceConfig449     WifiDeviceConfig()
450     {
451         networkId = INVALID_NETWORK_ID;
452         status = static_cast<int>(WifiDeviceConfigStatus::DISABLED);
453         bssidType = REAL_DEVICE_ADDRESS;
454         band = 0;
455         channel = 0;
456         frequency = 0;
457         level = 0;
458         isPasspoint = false;
459         isEphemeral = false;
460         wepTxKeyIndex = 0;
461         priority = 0;
462         hiddenSSID = false;
463         wifiPrivacySetting = WifiPrivacyConfig::RANDOMMAC;
464         rssi = -100;
465         uid = WIFI_INVALID_UID;
466         lastConnectTime = -1;
467         numRebootsSinceLastUse = 0;
468         numAssociation = 0;
469         connFailedCount = 0;
470     }
471 };
472 
473 enum class WifiState { DISABLING = 0, DISABLED = 1, ENABLING = 2, ENABLED = 3, UNKNOWN = 4 };
474 
475 /* wps state */
476 enum class WpsStartState {
477     START_PBC_SUCCEED = 0,
478     START_PIN_SUCCEED = 1,
479     START_PBC_FAILED = 2,
480     PBC_STARTED_ALREADY = 3,
481     START_PIN_FAILED = 4,
482     PIN_STARTED_ALREADY = 5,
483     STOP_PBC_SUCCEED = 6,
484     STOP_PBC_FAILED = 7,
485     STOP_PIN_SUCCEED = 8,
486     STOP_PIN_FAILED = 9,
487     START_PBC_FAILED_OVERLAP = 10,
488     START_WPS_FAILED = 11,
489     WPS_TIME_OUT = 12,
490     START_AP_PIN_SUCCEED = 13,
491     START_AP_PIN_FAILED = 14,
492     STOP_AP_PIN_SUCCEED = 15,
493     STOP_AP_PIN_FAILED = 16,
494 };
495 
496 enum class StreamDirection {
497     STREAM_DIRECTION_UP = 0,
498     STREAM_DIRECTION_DOWN = 1,
499     UNKNOWN,
500 };
501 
502 /* WifiProtectType  */
503 enum class WifiProtectType  {
504     WIFI_PROTECT_MULTICAST = 0,
505     WIFI_PROTECT_COMMON = 1
506 };
507 
508 /* WifiProtectMode  */
509 enum class WifiProtectMode {
510     WIFI_PROTECT_FULL = 0,
511     WIFI_PROTECT_SCAN_ONLY = 1,
512     WIFI_PROTECT_FULL_HIGH_PERF = 2,
513     WIFI_PROTECT_FULL_LOW_LATENCY = 3,
514     WIFI_PROTECT_NO_HELD = 4
515 };
516 
517 /* DHCP info */
518 struct IpInfo {
519     unsigned int ipAddress;     /* ip address */
520     unsigned int gateway;       /* gate */
521     unsigned int netmask;       /* mask */
522     unsigned int primaryDns;          /* main dns */
523     unsigned int secondDns;          /* backup dns */
524     unsigned int serverIp; /* DHCP server's address */
525     unsigned int leaseDuration;
526 
IpInfoIpInfo527     IpInfo()
528     {
529         ipAddress = 0;
530         gateway = 0;
531         netmask = 0;
532         primaryDns = 0;
533         secondDns = 0;
534         serverIp = 0;
535         leaseDuration = 0;
536     }
537 };
538 
539 /* DHCP IpV6Info */
540 struct IpV6Info {
541     std::string linkIpV6Address;
542     std::string globalIpV6Address;
543     std::string randGlobalIpV6Address;
544     std::string gateway;
545     std::string netmask;
546     std::string primaryDns;
547     std::string secondDns;
548 
IpV6InfoIpV6Info549     IpV6Info()
550     {
551         linkIpV6Address = "";
552         globalIpV6Address = "";
553         randGlobalIpV6Address = "";
554         gateway = "";
555         netmask = "";
556         primaryDns = "";
557         secondDns = "";
558     }
559 };
560 }  // namespace Wifi
561 }  // namespace OHOS
562 #endif
563