/
Station.h
295 lines (249 loc) · 8.21 KB
/
Station.h
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/****
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
* Created 2015 by Skurydin Alexey
* http://github.com/SmingHub/Sming
* All files of the Sming Core are provided under the LGPL v3 license.
*
* Station.h
*
****/
#pragma once
#include <WString.h>
#include <WVector.h>
#include <IpAddress.h>
#include <MacAddress.h>
#include "BssInfo.h"
/** @defgroup wifi_sta WiFi Station Interface
* @ingroup wifi platform
* @brief Control and monitoring of WiFi station interface
* @note The WiFi station interface provides client access to a WiFi network.
Control of WiFi connection including WiFi SSID and password and
IP address, DHCP, etc.
* @see \ref wifi_ap
* @see \ref wifi_ev
* @{
*/
/// WiFi station connection states
enum StationConnectionStatus {
eSCS_Idle, ///< Connection idle
eSCS_Connecting, ///< Connecting
eSCS_WrongPassword, ///< Wrong password
eSCS_AccessPointNotFound, ///< AP not found
eSCS_ConnectionFailed, ///< Connection failed
eSCS_GotIP ///< Got IP address
};
/// Smart configuration type
enum SmartConfigType {
SCT_None = -1,
SCT_EspTouch, ///< ESP Touch
SCT_AirKiss, ///< Air Kiss
SCT_EspTouch_AirKiss, ///< ESP Touch and Air Kiss
};
/// Smart configuration event
enum SmartConfigEvent {
SCE_Wait, ///< Wait
SCE_FindChannel, ///< Find channel
SCE_GettingSsid, ///< Getting SSID & password
SCE_Link, ///< Link established
SCE_LinkOver, ///< Link-over
};
/// Smart Config callback information
struct SmartConfigEventInfo {
SmartConfigType type = SCT_None; ///< Type of configuration underway
String ssid; ///< AP SSID
String password; ///< AP Password
bool bssidSet = false; ///< true if connection should match both SSID and BSSID
MacAddress bssid; ///< AP BSSID
};
/// WiFi WPS callback status
enum WpsStatus {
eWPS_Success = 0,
eWPS_Failed,
eWPS_Timeout,
eWPS_WEP,
};
/**
* @brief Scan complete handler function
*/
typedef Delegate<void(bool success, BssList& list)> ScanCompletedDelegate;
/**
* @brief Smart configuration handler function
* @param event
* @param info
* @retval bool return true to perform default configuration
*/
typedef Delegate<bool(SmartConfigEvent event, const SmartConfigEventInfo& info)> SmartConfigDelegate;
/**
* @brief WPS configuration callback function
* @param status
* @retval bool return true to perform default configuration
*/
typedef Delegate<bool(WpsStatus status)> WPSConfigDelegate;
/** @brief WiFi station class
*/
class StationClass
{
public:
virtual ~StationClass()
{
}
/** @brief Enable / disable WiFi station
* @note Disabling WiFi station will also disable and clear the handler set with <i>waitConnection</i>.
* @param enabled True to enable station. False to disable.
* @param save True to save operational mode to flash, False to set current operational mode only
*/
virtual void enable(bool enabled, bool save = false) = 0;
/** @brief Get WiFi station enable status
* @retval bool True if WiFi station enabled
*/
virtual bool isEnabled() const = 0;
/** @brief Configure WiFi station
* @param ssid WiFi SSID
* @param password WiFi password
* @param autoConnectOnStartup True to auto connect. False for manual. (Default: True)
* @param save True to save the SSID and password in Flash. False otherwise. (Default: True)
*/
virtual bool config(const String& ssid, const String& password, bool autoConnectOnStartup = true,
bool save = true) = 0;
/** @brief Connect WiFi station to network
*/
virtual bool connect() = 0;
/** @brief Disconnect WiFi station from network
*/
virtual bool disconnect() = 0;
/** @brief Get WiFi station connectoin status
* @retval bool True if connected.
*/
bool isConnected() const;
/** @brief Get WiFi station connection failure status
* @retval bool True if connection failed
*/
bool isConnectionFailed() const;
/** @brief Get WiFi station connection status
* @retval StationConnectionStatus Connection status structure
*/
virtual StationConnectionStatus getConnectionStatus() const = 0;
/** @brief Get WiFi station connection status name
* @retval String String representing connection status
*/
String getConnectionStatusName() const;
/** @brief Get WiFi station DHCP enabled status
* @retval bool True if DHCP enabled
*/
virtual bool isEnabledDHCP() const = 0;
/** @brief Enable or disable WiFi station DHCP
* @param enable True to enable WiFi station DHCP
*/
virtual void enableDHCP(bool enable) = 0;
/** @brief Set WiFi station DHCP hostname
* @param hostname - WiFi station DHCP hostname
*/
virtual void setHostname(const String& hostname) = 0;
/** @brief Set WiFi station DHCP hostname
* @retval WiFi station DHCP hostname
*/
virtual String getHostname() const = 0;
/** @brief Get WiFi station IP address
* @retval IpAddress IP address of WiFi station
*/
virtual IpAddress getIP() const = 0;
/** @brief Get WiFi station MAC address
* @retval MacAddress
*/
virtual MacAddress getMacAddress() const = 0;
/** @brief Get WiFi station MAC address
* @param sep Optional separator between bytes (e.g. ':')
* @retval String WiFi station MAC address
*/
String getMAC(char sep = '\0') const;
/** @brief Get WiFi station network mask
* @retval IpAddress WiFi station network mask
*/
virtual IpAddress getNetworkMask() const = 0;
/** @brief Get WiFi station default gateway
* @retval IpAddress WiFi station default gateway
*/
virtual IpAddress getNetworkGateway() const = 0;
/** @brief GetWiFi station broadcast address
* @retval IpAddress WiFi station broadcast address
*/
virtual IpAddress getNetworkBroadcast() const = 0;
/** @brief Set WiFi station IP address
* @param address IP address
* @retval bool True on success
*/
bool setIP(IpAddress address);
/** @brief Set WiFi station IP parameters
* @param address IP address
* @param netmask Network mask
* @param gateway Default gatway
* @retval bool True on success
*/
virtual bool setIP(IpAddress address, IpAddress netmask, IpAddress gateway) = 0;
/** @brief Get WiFi station SSID
* @retval String WiFi station SSID
*/
virtual String getSSID() const = 0;
/** @brief Get WiFi station password
* @retval String WiFi station password
*/
virtual String getPassword() const = 0;
/** @brief Get WiFi signal strength
* @retval int8_t Value in dBm
*/
virtual int8_t getRssi() const = 0;
/** @brief Get active WiFi channel
* @retval uint8_t channel number
*/
virtual uint8_t getChannel() const = 0;
/** @brief Start WiFi station network scan
* @param scanCompleted Function to call when scan completes
* @retval bool True on success
*/
virtual bool startScan(ScanCompletedDelegate scanCompleted) = 0;
#ifdef ENABLE_SMART_CONFIG
/** @brief Start WiFi station smart configuration
* @param sctype Smart configuration type
* @param callback Function to call on WiFi staton smart configuration complete (Default: none)
* @retval true if request successfully started, false on failure
* @note If operation already in progress call will fail
*/
virtual bool smartConfigStart(SmartConfigType sctype, SmartConfigDelegate callback = nullptr) = 0;
/** @brief Stop WiFi station smart configuration
*/
virtual void smartConfigStop() = 0;
#endif
#ifdef ENABLE_WPS
/** @brief Start WiFi station by WPS method
* @param callback Function to call on WiFi WPS Events (Default: none)
*/
virtual bool wpsConfigStart(WPSConfigDelegate callback = nullptr) = 0;
/** @brief Start WiFi station by WPS method
* @deprecated Use `wpsConfigStart()`
*/
bool beginWPSConfig() SMING_DEPRECATED
{
return wpsConfigStart(nullptr);
}
/** @brief Stop WiFi station WPS configuration
*/
virtual void wpsConfigStop() = 0;
#endif
protected:
ScanCompletedDelegate scanCompletedCallback = nullptr;
#ifdef ENABLE_SMART_CONFIG
SmartConfigDelegate smartConfigCallback = nullptr;
#endif
#ifdef ENABLE_WPS
WPSConfigDelegate wpsConfigCallback = nullptr;
#endif
};
/** @brief Global instance of WiFi station object
* @note Use WifiStation.<i>function</i> to access WiFi station functions
* @note Example:
* @code if(WifiStation.config("My_WiFi", "My_Password"))
WifiStation.enable(true);
@endcode
*/
extern StationClass& WifiStation;
/** @} */