/
IWiFiNetworkAdapter.cs
182 lines (163 loc) · 7.42 KB
/
IWiFiNetworkAdapter.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using Meadow.Gateway.WiFi;
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
namespace Meadow.Hardware;
/// <summary>
/// Provides an abstraction for a wifi-enabled INetworkAdapter
/// </summary>
public interface IWiFiNetworkAdapter : IWirelessNetworkAdapter
{
/// <summary>
/// Access point the adapter is currently connected to
/// </summary>
string? Ssid { get; }
/// <summary>
/// BSSID (MAC) of the access point the ESP32 is currently connected to.
/// </summary>
PhysicalAddress Bssid { get; }
/// <summary>
/// Automatically attempt to connect to the <b>DefaultSsid</b> network after a power cycle or reset.
/// </summary>
bool AutoConnect { get; }
/// <summary>
/// Automatically try to reconnect to an access point if there is a problem / disconnection
/// </summary>
bool AutoReconnect { get; }
/// <summary>
/// Default access point to try to connect to if the network interface is started and the board
/// is configured to automatically reconnect.
/// </summary>
string DefaultSsid { get; }
/// <summary>
/// WiFi channel used for communication.
/// </summary>
int Channel { get; }
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="timeout">Timeout period for the connection attempt</param>
/// <param name="token">Cancellation token for the connection attempt</param>
/// <param name="reconnection">Should the adapter reconnect automatically?</param>
/// <exception cref="ArgumentNullException">Thrown if the ssid is null or empty or the password is null.</exception>
Task Connect(string ssid, string password, TimeSpan timeout, CancellationToken token, ReconnectionType reconnection = ReconnectionType.Automatic);
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="reconnection">Should the adapter reconnect automatically?</param>
/// <exception cref="ArgumentNullException">Thrown if the ssid is null or empty or the password is null.</exception>
async Task Connect(string ssid, string password, ReconnectionType reconnection = ReconnectionType.Automatic)
{
var src = new CancellationTokenSource();
await Connect(ssid, password, TimeSpan.Zero, src.Token, reconnection);
}
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="token">Cancellation token for the connection attempt</param>
/// <param name="reconnection">Should the adapter reconnect automatically?</param>
/// <exception cref="ArgumentNullException">Thrown if the ssid is null or empty or the password is null.</exception>
async Task Connect(string ssid, string password, CancellationToken token, ReconnectionType reconnection = ReconnectionType.Automatic)
{
await Connect(ssid, password, TimeSpan.Zero, token, reconnection);
}
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="timeout">Timeout period for the connection attempt</param>
/// <param name="reconnection">Should the adapter reconnect automatically?</param>
/// <exception cref="ArgumentNullException">Thrown if the ssid is null or empty or the password is null.</exception>
async Task Connect(string ssid, string password, TimeSpan timeout, ReconnectionType reconnection = ReconnectionType.Automatic)
{
var src = new CancellationTokenSource();
await Connect(ssid, password, timeout, src.Token, reconnection);
}
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="timeout">Timeout period for the connection attempt</param>
/// <param name="token">Cancellation token for the connection attempt</param>
async Task Connect(string ssid, string password, TimeSpan timeout, CancellationToken token)
{
await Connect(ssid, password, timeout, token, ReconnectionType.Automatic);
}
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <param name="timeout">Timeout period for the connection attempt</param>
async Task Connect(string ssid, string password, TimeSpan timeout)
{
await Connect(ssid, password, timeout, CancellationToken.None);
}
/// <summary>
/// Start a WiFi network.
/// </summary>
/// <param name="ssid">Name of the network to connect to.</param>
/// <param name="password">Password for the network.</param>
/// <returns>true if the connection was successfully made.</returns>
async Task Connect(string ssid, string password)
{
await Connect(ssid, password, TimeSpan.FromSeconds(90), CancellationToken.None);
}
/// <summary>
/// Disconnect from the currently active access point.
/// </summary>
/// <remarks>
/// Setting turnOffWiFiInterface to true will call StopWiFiInterface following
/// the disconnection from the current access point.
/// </remarks>
/// <param name="turnOffWiFiInterface">Should the WiFi interface be turned off?</param>
Task Disconnect(bool turnOffWiFiInterface);
/// <summary>
/// Connect to the default access point.
/// </summary>
/// <remarks>The access point credentials should be stored in the coprocessor memory.</remarks>
void ConnectToDefaultAccessPoint();
/// <summary>
/// Removed any stored access point information from the coprocessor memory.
/// </summary>
Task ClearStoredAccessPointInformation();
/// <summary>
/// Get the list of access points.
/// </summary>
/// <remarks>
/// The network must be started before this method can be called.
/// </remarks>
/// <returns>An `IList` (possibly empty) of access points.</returns>
public Task<IList<WifiNetwork>> Scan()
{
return Scan(TimeSpan.FromMilliseconds(-1));
}
/// <summary>
/// Get the list of access points.
/// </summary>
/// <remarks>
/// The network must be started before this method can be called.
/// </remarks>
/// <param name="token">A <see cref="CancellationToken"/> to be used if the Scan should be canceled.</param>
/// <returns>An `IList` (possibly empty) of access points.</returns>
Task<IList<WifiNetwork>> Scan(CancellationToken token);
/// <summary>
/// Get the list of access points.
/// </summary>
/// <remarks>
/// The network must be started before this method can be called.
/// </remarks>
/// <param name="timeout">A <see cref="TimeSpan"/> representing the time to search before giving up.</param>
/// <returns>An `IList` (possibly empty) of access points.</returns>
Task<IList<WifiNetwork>> Scan(TimeSpan timeout);
}