-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Wireless80211.cs
142 lines (125 loc) · 4.87 KB
/
Wireless80211.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Device.Wifi;
using System.Diagnostics;
using System.Net.NetworkInformation;
using System.Threading;
using nanoFramework.Networking;
namespace WifiAP
{
class Wireless80211
{
/// <summary>
/// Checks if the wireless 802.11 interface is enabled.
/// </summary>
/// <returns>
/// Returns true if the wireless 802.11 interface is enabled (i.e., the SSID is not null or empty),
/// otherwise returns false.
/// </returns>
public static bool IsEnabled()
{
Wireless80211Configuration wconf = GetConfiguration();
return !string.IsNullOrEmpty(wconf.Ssid);
}
/// <summary>
/// Get current IP address. Only valid if successfully provisioned and connected
/// </summary>
/// <returns>IP address string</returns>
public static string GetCurrentIPAddress()
{
NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[0];
// get first NI ( Wifi on ESP32 )
return ni.IPv4Address.ToString();
}
/// <summary>
/// Coonnects to the Wifi or sets the Access Point mode.
/// </summary>
/// <returns>True if access point is setup.</returns>
public static bool ConnectOrSetAp()
{
if (IsEnabled())
{
Debug.WriteLine("Wireless client activated");
if (!WifiNetworkHelper.Reconnect(true, token: new CancellationTokenSource(30_000).Token))
{
WirelessAP.SetWifiAp();
return true;
}
}
else
{
WirelessAP.SetWifiAp();
return true;
}
return false;
}
/// <summary>
/// Disable the Wireless station interface.
/// </summary>
public static void Disable()
{
Wireless80211Configuration wconf = GetConfiguration();
wconf.Options = Wireless80211Configuration.ConfigurationOptions.None | Wireless80211Configuration.ConfigurationOptions.SmartConfig;
wconf.SaveConfiguration();
}
/// <summary>
/// Configure and enable the Wireless station interface
/// </summary>
/// <param name="ssid"></param>
/// <param name="password"></param>
/// <returns></returns>
public static bool Configure(string ssid, string password)
{
// Make sure we are disconnected before we start connecting otherwise
// ConnectDhcp will just return success instead of reconnecting.
WifiAdapter wa = WifiAdapter.FindAllAdapters()[0];
wa.Disconnect();
CancellationTokenSource cs = new(30_000);
Console.WriteLine("ConnectDHCP");
WifiNetworkHelper.Disconnect();
// Reconfigure properly the normal wifi
Wireless80211Configuration wconf = GetConfiguration();
wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable;
wconf.Ssid = ssid;
wconf.Password = password;
wconf.SaveConfiguration();
WifiNetworkHelper.Disconnect();
bool success;
success = WifiNetworkHelper.ConnectDhcp(ssid, password, WifiReconnectionKind.Automatic, true, token: cs.Token);
if (!success)
{
wa.Disconnect();
// Bug in network helper, we've most likely try to connect before, let's make it manual
var res = wa.Connect(ssid, WifiReconnectionKind.Automatic, password);
success = res.ConnectionStatus == WifiConnectionStatus.Success;
Console.WriteLine($"Connected: {res.ConnectionStatus}");
}
return success;
}
/// <summary>
/// Get the Wireless station configuration.
/// </summary>
/// <returns>Wireless80211Configuration object</returns>
public static Wireless80211Configuration GetConfiguration()
{
NetworkInterface ni = GetInterface();
return Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId];
}
public static NetworkInterface GetInterface()
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
// Find WirelessAP interface
foreach (NetworkInterface ni in Interfaces)
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211)
{
return ni;
}
}
return null;
}
}
}