forked from d-kato/esp32-driver
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathESP32InterfaceAP.cpp
More file actions
253 lines (210 loc) · 6.23 KB
/
Copy pathESP32InterfaceAP.cpp
File metadata and controls
253 lines (210 loc) · 6.23 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
/* 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.
*/
#include <string.h>
#include "ESP32InterfaceAP.h"
// ESP32InterfaceAP implementation
ESP32InterfaceAP::ESP32InterfaceAP() :
ESP32Stack(MBED_CONF_ESP32_WIFI_EN, MBED_CONF_ESP32_WIFI_IO0, MBED_CONF_ESP32_WIFI_TX, MBED_CONF_ESP32_WIFI_RX, MBED_CONF_ESP32_WIFI_DEBUG,
MBED_CONF_ESP32_WIFI_RTS, MBED_CONF_ESP32_WIFI_CTS, MBED_CONF_ESP32_WIFI_BAUDRATE, 1),
_dhcp(true),
_own_ch(1),
_own_ssid(),
_own_pass(),
_own_sec(NSAPI_SECURITY_NONE),
_ip_address(),
_netmask(),
_gateway(),
_connection_status(NSAPI_STATUS_DISCONNECTED),
_connection_status_cb(NULL)
{
}
ESP32InterfaceAP::ESP32InterfaceAP(PinName en, PinName io0, PinName tx, PinName rx, bool debug,
PinName rts, PinName cts, int baudrate) :
ESP32Stack(en, io0, tx, rx, debug, rts, cts, baudrate, 1),
_dhcp(true),
_own_ch(1),
_own_ssid(),
_own_pass(),
_own_sec(NSAPI_SECURITY_NONE),
_ip_address(),
_netmask(),
_gateway(),
_connection_status(NSAPI_STATUS_DISCONNECTED),
_connection_status_cb(NULL)
{
}
ESP32InterfaceAP::ESP32InterfaceAP(PinName tx, PinName rx, bool debug) :
ESP32Stack(NC, NC, tx, rx, debug, NC, NC, 230400, 1),
_dhcp(true),
_own_ch(1),
_own_ssid(),
_own_pass(),
_own_sec(NSAPI_SECURITY_NONE),
_ip_address(),
_netmask(),
_gateway(),
_connection_status(NSAPI_STATUS_DISCONNECTED),
_connection_status_cb(NULL)
{
}
nsapi_error_t ESP32InterfaceAP::set_network(const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
{
_dhcp = false;
_ip_address = ip_address;
_netmask = netmask;
_gateway = gateway;
return NSAPI_ERROR_OK;
}
nsapi_error_t ESP32InterfaceAP::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;
_ip_address.set_ip_address(ip_address);
_netmask.set_ip_address(netmask);
_gateway.set_ip_address(gateway);
return NSAPI_ERROR_OK;
}
nsapi_error_t ESP32InterfaceAP::set_dhcp(bool dhcp)
{
_dhcp = dhcp;
return NSAPI_ERROR_OK;
}
int ESP32InterfaceAP::connect(const char *ssid, const char *pass, nsapi_security_t security,
uint8_t channel)
{
int ret;
ret = set_credentials(ssid, pass, security);
if (ret != 0) {
return ret;
}
ret = set_channel(channel);
if (ret != 0) {
return ret;
}
return connect();
}
int ESP32InterfaceAP::connect()
{
if (!_esp->set_mode(ESP32::WIFIMODE_STATION_SOFTAP)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
if (!_esp->dhcp(_dhcp, 1)) {
return NSAPI_ERROR_DHCP_FAILURE;
}
if (!_dhcp) {
if (!_esp->set_network_ap(_ip_address.get_ip_address(), _netmask.get_ip_address(), _gateway.get_ip_address())) {
return NSAPI_ERROR_DEVICE_ERROR;
}
}
if (!_esp->config_soft_ap(_own_ssid, _own_pass, _own_ch, (uint8_t)_own_sec)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
_connection_status = NSAPI_STATUS_GLOBAL_UP;
if (_connection_status_cb) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
}
return NSAPI_ERROR_OK;
}
int ESP32InterfaceAP::set_credentials(const char *ssid, const char *pass, nsapi_security_t security)
{
switch (security) {
case NSAPI_SECURITY_NONE:
case NSAPI_SECURITY_WPA:
case NSAPI_SECURITY_WPA2:
case NSAPI_SECURITY_WPA_WPA2:
_own_sec = security;
break;
case NSAPI_SECURITY_UNKNOWN:
case NSAPI_SECURITY_WEP:
default:
return NSAPI_ERROR_UNSUPPORTED;
}
memset(_own_ssid, 0, sizeof(_own_ssid));
strncpy(_own_ssid, ssid, sizeof(_own_ssid));
memset(_own_pass, 0, sizeof(_own_pass));
strncpy(_own_pass, pass, sizeof(_own_pass));
return 0;
}
int ESP32InterfaceAP::set_channel(uint8_t channel)
{
if (channel != 0) {
_own_ch = channel;
}
return 0;
}
int ESP32InterfaceAP::disconnect()
{
if (!_esp->set_mode(ESP32::WIFIMODE_STATION)) {
return NSAPI_ERROR_DEVICE_ERROR;
}
_connection_status = NSAPI_STATUS_DISCONNECTED;
if (_connection_status_cb) {
_connection_status_cb(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, _connection_status);
}
return NSAPI_ERROR_OK;
}
nsapi_error_t ESP32InterfaceAP::get_ip_address(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getIPAddress_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}
const char *ESP32InterfaceAP::get_ip_address()
{
return _esp->getIPAddress_ap();
}
const char *ESP32InterfaceAP::get_mac_address()
{
return _esp->getMACAddress_ap();
}
nsapi_error_t ESP32InterfaceAP::get_gateway(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getGateway_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}
const char *ESP32InterfaceAP::get_gateway()
{
return _esp->getGateway_ap();
}
nsapi_error_t ESP32InterfaceAP::get_netmask(SocketAddress* sockAddr)
{
if (sockAddr->set_ip_address(_esp->getNetmask_ap())) {
return NSAPI_ERROR_OK;
}
return NSAPI_ERROR_NO_ADDRESS;
}
const char *ESP32InterfaceAP::get_netmask()
{
return _esp->getNetmask_ap();
}
int8_t ESP32InterfaceAP::get_rssi()
{
return 0;
}
int ESP32InterfaceAP::scan(WiFiAccessPoint *res, unsigned count)
{
return _esp->scan(res, count);
}
void ESP32InterfaceAP::attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_connection_status_cb = status_cb;
}
nsapi_connection_status_t ESP32InterfaceAP::get_connection_status() const
{
return _connection_status;
}