forked from d-kato/esp32-driver
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathESP32Interface.h
More file actions
278 lines (245 loc) · 10.6 KB
/
Copy pathESP32Interface.h
File metadata and controls
278 lines (245 loc) · 10.6 KB
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
/* ESP32 implementation of NetworkInterfaceAPI
* Copyright (c) 2017 Renesas Electronics Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ESP32_INTERFACE_H
#define ESP32_INTERFACE_H
#include "mbed.h"
#include "ESP32Stack.h"
/** ESP32Interface class
* Implementation of the NetworkStack for the ESP32
*/
class ESP32Interface : public ESP32Stack, public WiFiInterface
{
public:
/** ESP32Interface lifetime
* Configuration defined in mbed_lib.json
*/
ESP32Interface();
/** ESP32Interface lifetime
* @param en EN pin (If not used this pin, please set "NC")
* @param io0 IO0 pin (If not used this pin, please set "NC")
* @param tx TX pin
* @param rx RX pin
* @param debug Enable debugging
* @param rts RTS pin
* @param cts CTS pin
* @param baudrate The baudrate of the serial port (default = 230400).
*/
ESP32Interface(PinName en, PinName io0, PinName tx, PinName rx, bool debug = false,
PinName rts = NC, PinName cts = NC, int baudrate = 230400);
/**
* @brief ESP32Interface default destructor
*/
virtual ~ESP32Interface();
/** ESP32Interface lifetime
* @param tx TX pin
* @param rx RX pin
* @param debug Enable debugging
*/
ESP32Interface(PinName tx, PinName rx, bool debug = false);
/** Set a static IP address
*
* Configures this network interface to use a static IP address.
* Implicitly disables DHCP, which can be enabled in set_dhcp.
* Requires that the network is disconnected.
*
* @param ip_address SocketAddress representation of the local IP address
* @param netmask SocketAddress representation of the local network mask
* @param gateway SocketAddress representation of the local gateway
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_network(
const SocketAddress &ip_address, const SocketAddress &netmask,
const SocketAddress &gateway) override;
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network
*
* Enables DHCP on connecting the network. Defaults to enabled unless
* a static IP address has been assigned. Requires that the network is
* disconnected.
*
* @param dhcp True to enable DHCP
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_dhcp(bool dhcp);
/** Start the interface
*
* Attempts to connect to a WiFi network. Requires ssid and passphrase to be set.
* If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.
*
* @return 0 on success, negative error code on failure
*/
virtual int connect();
/** Start the interface
*
* Attempts to connect to a WiFi network.
*
* @param ssid Name of the network to connect to
* @param pass Security passphrase to connect to the network
* @param security Type of encryption for connection (Default: NSAPI_SECURITY_NONE)
* @param channel This parameter is not supported, setting it to anything else than 0 will result in NSAPI_ERROR_UNSUPPORTED
* @return 0 on success, or error code on failure
*/
virtual int connect(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE,
uint8_t channel = 0);
/** Set the WiFi network credentials
*
* @param ssid Name of the network to connect to
* @param pass Security passphrase to connect to the network
* @param security Type of encryption for connection
* (defaults to NSAPI_SECURITY_NONE)
* @return 0 on success, or error code on failure
*/
virtual int set_credentials(const char *ssid, const char *pass, nsapi_security_t security = NSAPI_SECURITY_NONE);
/** Set the WiFi network channel - NOT SUPPORTED
*
* This function is not supported and will return NSAPI_ERROR_UNSUPPORTED
*
* @param channel Channel on which the connection is to be made, or 0 for any (Default: 0)
* @return Not supported, returns NSAPI_ERROR_UNSUPPORTED
*/
virtual int set_channel(uint8_t channel);
/** Stop the interface
* @return 0 on success, negative on failure
*/
virtual int disconnect();
/** Get the internally stored IP address
* @param sockAddr SocketAddress pointer to store the local IP address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_ip_address(SocketAddress *sockAddr);
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_ip_address();
/** Get the internally stored MAC address
* @return MAC address of the interface
*/
virtual const char *get_mac_address();
/** Get the local gateway
*
* @param sockAddr SocketAddress representation of gateway address
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_gateway(SocketAddress *sockAddr);
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_gateway();
/** Get the local network mask
*
* @param sockAddr SocketAddress representation of netmask
* @retval NSAPI_ERROR_OK on success
* @retval NSAPI_ERROR_UNSUPPORTED if this feature is not supported
* @retval NSAPI_ERROR_PARAMETER if the provided pointer is invalid
* @retval NSAPI_ERROR_NO_ADDRESS if the address cannot be obtained from stack
*/
virtual nsapi_error_t get_netmask(SocketAddress *sockAddr);
MBED_DEPRECATED_SINCE("mbed-os-5.15", "String-based APIs are deprecated")
virtual const char *get_netmask();
/** Gets the current radio signal strength for active connection
*
* @return Connection strength in dBm (negative value)
*/
virtual int8_t get_rssi();
/** Scan for available networks
*
* This function will block.
*
* @param ap Pointer to allocated array to store discovered AP
* @param count Size of allocated @a res array, or 0 to only count available AP
* @param timeout Timeout in milliseconds; 0 for no timeout (Default: 0)
* @return Number of entries in @a, or if @a count was 0 number of available networks, negative on error
* see @a nsapi_error
*/
virtual int scan(WiFiAccessPoint *res, unsigned count);
/** Translates a hostname to an IP address with specific version
*
* The hostname may be either a domain name or an IP address. If the
* hostname is an IP address, no network transactions will be performed.
*
* If no stack-specific DNS resolution is provided, the hostname
* will be resolve using a UDP socket on the stack.
*
* @param address Destination for the host SocketAddress
* @param host Hostname to resolve
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
* version is chosen by the stack (defaults to NSAPI_UNSPEC)
* @return 0 on success, negative error code on failure
*/
using NetworkInterface::gethostbyname;
/** Add a domain name server to list of servers to query
*
* @param addr Destination for the host address
* @return 0 on success, negative error code on failure
*/
using NetworkInterface::add_dns_server;
/** Register callback for status reporting
*
* The specified status callback function will be called on status changes
* on the network. The parameters on the callback are the event type and
* event-type dependent reason parameter.
*
* In ESP32 the callback will be called when processing OOB-messages via
* AT-parser. Do NOT call any ESP8266Interface -functions or do extensive
* processing in the callback.
*
* @param status_cb The callback for status changes
*/
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
/** Get the connection status
*
* @return The connection status according to ConnectionStatusType
*/
virtual nsapi_connection_status_t get_connection_status() const;
/** Provide access to the NetworkStack object
*
* @return The underlying NetworkStack object
*/
virtual NetworkStack *get_stack()
{
return this;
}
private:
// HW reset pin
class ResetPin {
public:
ResetPin(PinName rst_pin);
void rst_assert();
void rst_deassert();
bool is_connected();
private:
mbed::DigitalOut _rst_pin;
} _rst_pin;
int _initialized;
bool _dhcp;
char _ap_ssid[33]; /* 32 is what 802.11 defines as longest possible name; +1 for the \0 */
char _ap_pass[64]; /* The longest allowed passphrase */
nsapi_security_t _ap_sec;
SocketAddress _ip_address;
SocketAddress _netmask;
SocketAddress _gateway;
nsapi_connection_status_t _connection_status;
mbed::Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
void set_connection_status(nsapi_connection_status_t connection_status);
void wifi_status_cb(int8_t wifi_status);
nsapi_error_t _init(void);
nsapi_error_t _reset(void);
};
#endif