-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathWirelessAP.cs
158 lines (135 loc) · 5.45 KB
/
WirelessAP.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Net;
using System.Net.NetworkInformation;
using Iot.Device.DhcpServer;
using nanoFramework.Runtime.Native;
namespace WifiAP
{
/// <summary>
/// Provides methods and properties to manage a wireless access point.
/// </summary>
public static class WirelessAP
{
/// <summary>
/// Gets or sets the IP address of the Soft AP.
/// </summary>
public static string SoftApIP { get; set; } = "192.168.4.1";
/// <summary>
/// Gets or sets the SSID of the Soft AP.
/// </summary>
public static string SoftApSsid { get; set; } = "MySuperSSID";
/// <summary>
/// Sets the configuration for the wireless access point.
/// </summary>
public static void SetWifiAp()
{
Wireless80211.Disable();
if (Setup() == false)
{
// Reboot device to Activate Access Point on restart
Console.WriteLine($"Setup Soft AP, Rebooting device");
Power.RebootDevice();
}
var dhcpserver = new DhcpServer
{
CaptivePortalUrl = $"http://{SoftApIP}"
};
var dhcpInitResult = dhcpserver.Start(IPAddress.Parse(SoftApIP), new IPAddress(new byte[] { 255, 255, 255, 0 }));
if (!dhcpInitResult)
{
Console.WriteLine($"Error initializing DHCP server.");
// This happens after a very freshly flashed device
Power.RebootDevice();
}
Console.WriteLine($"Running Soft AP, waiting for client to connect");
Console.WriteLine($"Soft AP IP address :{GetIP()}");
}
/// <summary>
/// Disable the Soft AP for next restart.
/// </summary>
public static void Disable()
{
WirelessAPConfiguration wapconf = GetConfiguration();
wapconf.Options = WirelessAPConfiguration.ConfigurationOptions.None;
wapconf.SaveConfiguration();
}
/// <summary>
/// Set-up the Wireless AP settings, enable and save
/// </summary>
/// <returns>True if already set-up</returns>
public static bool Setup()
{
NetworkInterface ni = GetInterface();
WirelessAPConfiguration wapconf = GetConfiguration();
// Check if already Enabled and return true
if (wapconf.Options == (WirelessAPConfiguration.ConfigurationOptions.Enable |
WirelessAPConfiguration.ConfigurationOptions.AutoStart) &&
ni.IPv4Address == SoftApIP )
{
return true;
}
// Set up IP address for Soft AP
ni.EnableStaticIPv4(SoftApIP, "255.255.255.0", SoftApIP);
// Set Options for Network Interface
//
// Enable - Enable the Soft AP ( Disable to reduce power )
// AutoStart - Start Soft AP when system boots.
// HiddenSSID- Hide the SSID
//
wapconf.Options = WirelessAPConfiguration.ConfigurationOptions.AutoStart |
WirelessAPConfiguration.ConfigurationOptions.Enable;
// Set the SSID for Access Point. If not set will use default "nano_xxxxxx"
wapconf.Ssid = SoftApSsid;
// Maximum number of simultaneous connections, reserves memory for connections
wapconf.MaxConnections = 1;
// To set-up Access point with no Authentication
wapconf.Authentication = System.Net.NetworkInformation.AuthenticationType.Open;
wapconf.Password = "";
// To set up Access point with no Authentication. Password minimum 8 chars.
//wapconf.Authentication = AuthenticationType.WPA2;
//wapconf.Password = "password";
// Save the configuration so on restart Access point will be running.
wapconf.SaveConfiguration();
return false;
}
/// <summary>
/// Find the Wireless AP configuration
/// </summary>
/// <returns>Wireless AP configuration or NUll if not available</returns>
public static WirelessAPConfiguration GetConfiguration()
{
NetworkInterface ni = GetInterface();
return WirelessAPConfiguration.GetAllWirelessAPConfigurations()[ni.SpecificConfigId];
}
/// <summary>
/// Gets the network interface for the wireless access point.
/// </summary>
/// <returns>The network interface for the wireless access point.</returns>
public static NetworkInterface GetInterface()
{
NetworkInterface[] Interfaces = NetworkInterface.GetAllNetworkInterfaces();
// Find WirelessAP interface
foreach (NetworkInterface ni in Interfaces)
{
if (ni.NetworkInterfaceType == NetworkInterfaceType.WirelessAP)
{
return ni;
}
}
return null;
}
/// <summary>
/// Returns the IP address of the Soft AP
/// </summary>
/// <returns>IP address</returns>
public static string GetIP()
{
NetworkInterface ni = GetInterface();
return ni.IPv4Address;
}
}
}