forked from DCC-EX/CommandStation-EX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WifiESP32.cpp
430 lines (394 loc) · 11.9 KB
/
WifiESP32.cpp
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/*
© 2023 Paul M. Antoine
© 2021 Harald Barth
© 2023 Nathan Kellenicki
This file is part of CommandStation-EX
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
It is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CommandStation. If not, see <https://www.gnu.org/licenses/>.
*/
#if defined(ARDUINO_ARCH_ESP32)
#include <vector>
#include "defines.h"
#include "ESPmDNS.h"
#include <WiFi.h>
#include "esp_wifi.h"
#include "WifiESP32.h"
#include "DIAG.h"
#include "RingStream.h"
#include "CommandDistributor.h"
#include "WiThrottle.h"
#include "hmi.h"
#include "LaboxModes.h"
/*
#include "soc/rtc_wdt.h"
#include "esp_task_wdt.h"
*/
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
void feedTheDog0(){
// feed dog 0
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
TIMERG0.wdt_feed=1; // feed dog
TIMERG0.wdt_wprotect=0; // write protect
// feed dog 1
//TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
//TIMERG1.wdt_feed=1; // feed dog
//TIMERG1.wdt_wprotect=0; // write protect
}
/*
void enableCoreWDT(byte core){
TaskHandle_t idle = xTaskGetIdleTaskHandleForCPU(core);
if(idle == NULL){
DIAG(F("Get idle rask on core %d failed"),core);
} else {
if(esp_task_wdt_add(idle) != ESP_OK){
DIAG(F("Failed to add Core %d IDLE task to WDT"),core);
} else {
DIAG(F("Added Core %d IDLE task to WDT"),core);
}
}
}
void disableCoreWDT(byte core){
TaskHandle_t idle = xTaskGetIdleTaskHandleForCPU(core);
if(idle == NULL || esp_task_wdt_delete(idle) != ESP_OK){
DIAG(F("Failed to remove Core %d IDLE task from WDT"),core);
}
}
*/
class NetworkClient {
public:
NetworkClient(WiFiClient c) {
wifi = c;
};
bool ok() {
return (inUse && wifi.connected());
};
bool recycle(WiFiClient c) {
if (inUse == true) return false;
// return false here until we have
// implemented a LRU timer
// if (LRU too recent) return false;
return false;
wifi = c;
inUse = true;
return true;
};
WiFiClient wifi;
bool inUse = true;
};
static std::vector<NetworkClient> clients; // a list to hold all clients
static WiFiServer *server = NULL;
static RingStream *outboundRing = new RingStream(10240);
static bool APmode = false;
#ifdef WIFI_TASK_ON_CORE0
void wifiLoop(void *){
for(;;){
WifiESP::loop();
}
}
#endif
char asciitolower(char in) {
if (in <= 'Z' && in >= 'A')
return in - ('Z' - 'z');
return in;
}
bool WifiESP::setup(const char *SSid,
const char *password,
const char *hostname,
int port,
const byte channel,
const bool forceAP) {
bool havePassword = true;
bool haveSSID = true;
bool wifiUp = false;
uint8_t tries = 40;
//#ifdef SERIAL_BT_COMMANDS
//return false;
//#endif
// tests
// enableCoreWDT(1);
// disableCoreWDT(0);
// clean start
WiFi.mode(WIFI_STA);
WiFi.disconnect(true);
// differnet settings that did not improve for haba
// WiFi.useStaticBuffers(true);
// WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SECURITY);
const char *yourNetwork = "Your network ";
if (strncmp(yourNetwork, SSid, 13) == 0 || strncmp("", SSid, 13) == 0)
haveSSID = false;
if (strncmp(yourNetwork, password, 13) == 0 || strncmp("", password, 13) == 0)
havePassword = false;
if (haveSSID && havePassword && !forceAP) {
WiFi.setHostname(hostname); // Strangely does not work unless we do it HERE!
WiFi.mode(WIFI_STA);
#ifdef SERIAL_BT_COMMANDS
WiFi.setSleep(true);
#else
WiFi.setSleep(false);
#endif
WiFi.setAutoReconnect(true);
#ifdef USE_HMI
if (!LaboxModes::progMode && !LaboxModes::silentBootMode && hmi::CurrentInterface != NULL)
{
hmi::CurrentInterface->WifiStartConnection(SSid);
hmi::CurrentInterface->HmiInterfaceUpdateDrawing();
}
#endif
WiFi.begin(SSid, password);
while (WiFi.status() != WL_CONNECTED && tries) {
Serial.print('.');
tries--;
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
wifiUp = true;
#ifdef USE_HMI
if (!LaboxModes::progMode && hmi::CurrentInterface != NULL)
{
hmi::CurrentInterface->WifiConnected(WiFi.localIP());
hmi::CurrentInterface->HmiInterfaceUpdateDrawing();
}
#endif
} else {
DIAG(F("Could not connect to Wifi SSID %s"),SSid);
DIAG(F("Forcing one more Wifi restart"));
esp_wifi_start();
esp_wifi_connect();
tries=40;
while (WiFi.status() != WL_CONNECTED && tries) {
Serial.print('.');
tries--;
delay(500);
}
if (WiFi.status() == WL_CONNECTED) {
DIAG(F("Wifi STA IP 2nd try %s"),WiFi.localIP().toString().c_str());
wifiUp = true;
#ifdef USE_HMI
if (!LaboxModes::progMode && hmi::CurrentInterface != NULL)
{
hmi::CurrentInterface->WifiConnected(WiFi.localIP());
hmi::CurrentInterface->HmiInterfaceUpdateDrawing();
}
#endif
} else {
DIAG(F("Wifi STA mode FAIL. Will revert to AP mode"));
haveSSID=false;
#ifdef USE_HMI
if (!LaboxModes::progMode && hmi::CurrentInterface != NULL)
{
hmi::CurrentInterface->WifiEndConnection(SSid);
hmi::CurrentInterface->HmiInterfaceUpdateDrawing();
}
#endif
}
}
}
if (!haveSSID || forceAP) {
// prepare all strings
String strSSID(forceAP ? SSid : "Labox_");
String strPass(forceAP ? password : "PASS_");
if (!forceAP) {
String strMac = WiFi.macAddress();
strMac.remove(0,9);
strMac.replace(":","");
strMac.replace(":","");
// convert mac addr hex chars to lower case to be compatible with AT software
std::transform(strMac.begin(), strMac.end(), strMac.begin(), asciitolower);
strSSID.concat(strMac);
strPass.concat(strMac);
}
WiFi.mode(WIFI_AP);
#ifdef SERIAL_BT_COMMANDS
WiFi.setSleep(true);
#else
WiFi.setSleep(false);
#endif
if (WiFi.softAP(strSSID.c_str(),
havePassword ? password : strPass.c_str(),
channel, false, 8)) {
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
DIAG(F("Wifi AP IP %s"),WiFi.softAPIP().toString().c_str());
wifiUp = true;
APmode = true;
#ifdef USE_HMI
if (!LaboxModes::progMode && hmi::CurrentInterface != NULL)
{
hmi::CurrentInterface->WifiConnected(WiFi.softAPIP());
hmi::CurrentInterface->HmiInterfaceUpdateDrawing();
}
#endif
} else {
DIAG(F("Could not set up AP with Wifi SSID %s"),strSSID.c_str());
}
}
if (!wifiUp) {
DIAG(F("Wifi setup all fail (STA and AP mode)"));
// no idea to go on
return false;
}
// Now Wifi is up, register the mDNS service
if(!MDNS.begin(hostname)) {
DIAG(F("Wifi setup failed to start mDNS"));
}
if(!MDNS.addService("withrottle", "tcp", 2560)) {
DIAG(F("Wifi setup failed to add withrottle service to mDNS"));
}
server = new WiFiServer(port); // start listening on tcp port
server->begin();
#ifdef WIFI_TASK_ON_CORE0
//start loop task
if (pdPASS != xTaskCreatePinnedToCore(
wifiLoop, /* Task function. */
"wifiLoop",/* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
NULL, /* Task handle to keep track of created task */
0)) { /* pin task to core 0 */
DIAG(F("Could not create wifiLoop task"));
return false;
}
// report server started after wifiLoop creation
// when everything looks good
DIAG(F("Server starting (core 0) port %d"),port);
#else
DIAG(F("Server will be started on port %d"),port);
#endif
return true;
}
const char *wlerror[] = {
"WL_IDLE_STATUS",
"WL_NO_SSID_AVAIL",
"WL_SCAN_COMPLETED",
"WL_CONNECTED",
"WL_CONNECT_FAILED",
"WL_CONNECTION_LOST",
"WL_DISCONNECTED"
};
void WifiESP::loop() {
int clientId; //tmp loop var
// really no good way to check for LISTEN especially in AP mode?
wl_status_t wlStatus;
if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) {
// loop over all clients and remove inactive
for (clientId=0; clientId<clients.size(); clientId++){
// check if client is there and alive
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
DIAG(F("Remove client %d"), clientId);
#ifdef CD_HANDLE_RING
CommandDistributor::forget(clientId);
#endif
clients[clientId].wifi.stop();
clients[clientId].inUse = false;
//Do NOT clients.erase(clients.begin()+clientId) as
//that would mix up clientIds for later.
}
}
if (server->hasClient()) {
WiFiClient client;
while (client = server->available()) {
for (clientId=0; clientId<clients.size(); clientId++){
if (clients[clientId].recycle(client)) {
DIAG(F("Recycle client %d %s"), clientId, client.remoteIP().toString().c_str());
break;
}
}
if (clientId>=clients.size()) {
NetworkClient nc(client);
clients.push_back(nc);
DIAG(F("New client %d, %s"), clientId, client.remoteIP().toString().c_str());
#ifdef USE_HMI
if (hmi::CurrentInterface != NULL)
hmi::CurrentInterface->NewClient(clientId, client.remoteIP(), 0);
#endif
}
}
}
// loop over all connected clients
for (clientId=0; clientId<clients.size(); clientId++){
if(clients[clientId].ok()) {
int len;
if ((len = clients[clientId].wifi.available()) > 0) {
// read data from client
byte cmd[len+1];
for(int i=0; i<len; i++) {
cmd[i]=clients[clientId].wifi.read();
}
cmd[len]=0;
#ifdef CD_HANDLE_RING
CommandDistributor::parse(clientId,cmd,outboundRing);
#endif
}
}
} // all clients
WiThrottle::loop(outboundRing);
// something to write out?
clientId=outboundRing->read();
if (clientId >= 0) {
// We have data to send in outboundRing
// and we have a valid clientId.
// First read it out to buffer
// and then look if it can be sent because
// we can not leave it in the ring for ever
int count=outboundRing->count();
{
char buffer[count+1]; // one extra for '\0'
for(int i=0;i<count;i++) {
int c = outboundRing->read();
if (c >= 0) // Panic check, should never be false
buffer[i] = (char)c;
else {
DIAG(F("Ringread fail at %d"),i);
break;
}
}
// buffer filled, end with '\0' so we can use it as C string
buffer[count]='\0';
if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) {
if (Diag::CMD || Diag::WITHROTTLE)
DIAG(F("SEND %d:%s"), clientId, buffer);
clients[clientId].wifi.write(buffer,count);
} else {
DIAG(F("Unsent(%d): %s"), clientId, buffer);
}
}
}
} else if (!APmode) { // in STA mode but not connected any more
// kick it again
if (wlStatus <= 6) {
DIAG(F("Wifi aborted with error %s. Kicking Wifi!"), wlerror[wlStatus]);
esp_wifi_start();
esp_wifi_connect();
uint8_t tries=40;
while (WiFi.status() != WL_CONNECTED && tries) {
Serial.print('.');
tries--;
delay(500);
}
} else {
// all well, probably
//DIAG(F("Running BT"));
}
}
// when loop() is running on core0 we must
// feed the core0 wdt ourselves as yield()
// is not necessarily yielding to a low
// prio task. On core1 this is not a problem
// as there the wdt is disabled by the
// arduio IDE startup routines.
if (xPortGetCoreID() == 0)
feedTheDog0();
yield();
}
#endif //ESP32