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