1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2025-07-28 18:03:45 +02:00

Wifi reliability (#45)

* First pass at wifi inbound FSA
* Fixup detector loop
* Remove asyncBanned
Unused, uninitialized
* Move wifi setup loop out of .ino
Wifi auto detect Serial 1,2,3
* Correct capitalization
* Uno compiles clean
* Command distributor
Moved command execution routing out of Wifi code for future use by Ethernet interface.
Co-authored-by: Fred <fndecker@gmail.com>
This commit is contained in:
Asbelos
2020-10-05 18:42:31 +01:00
committed by GitHub
parent abd83bf7d6
commit 3f06fb08df
10 changed files with 440 additions and 219 deletions

View File

@@ -23,26 +23,75 @@
#include "DIAG.h"
#include "StringFormatter.h"
#include "WiThrottle.h"
#include "WifiInboundHandler.h"
const char PROGMEM READY_SEARCH[] = "\r\nready\r\n";
const char PROGMEM OK_SEARCH[] = "\r\nOK\r\n";
const char PROGMEM END_DETAIL_SEARCH[] = "@ 1000";
const char PROGMEM PROMPT_SEARCH[] = ">";
const char PROGMEM SEND_OK_SEARCH[] = "\r\nSEND OK\r\n";
const char PROGMEM IPD_SEARCH[] = "+IPD";
const unsigned long LOOP_TIMEOUT = 2000;
bool WifiInterface::connected = false;
bool WifiInterface::closeAfter = false;
DCCEXParser WifiInterface::parser;
byte WifiInterface::loopstate = 0;
unsigned long WifiInterface::loopTimeoutStart = 0;
int WifiInterface::datalength = 0;
int WifiInterface::connectionId;
byte WifiInterface::buffer[MAX_WIFI_BUFFER+1];
MemStream * WifiInterface::streamer;
Stream * WifiInterface::wifiStream = NULL;
HTTP_CALLBACK WifiInterface::httpCallback = 0;
Stream * WifiInterface::wifiStream;
////////////////////////////////////////////////////////////////////////////////
//
// Figure out number of serial ports depending on hardware
//
#if defined(ARDUINO_AVR_UNO)
#define NUM_SERIAL 0
#endif
#if (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560))
#define NUM_SERIAL 3
#endif
#ifndef NUM_SERIAL
#define NUM_SERIAL 1
#endif
bool WifiInterface::setup(long serial_link_speed,
const __FlashStringHelper *wifiESSID,
const __FlashStringHelper *wifiPassword,
const __FlashStringHelper *hostname,
const int port) {
bool wifiUp = false;
#if NUM_SERIAL == 0
// no warning about unused parameters.
(void) serial_link_speed;
(void) wifiESSID;
(void) wifiPassword;
(void) hostname;
(void) port;
#endif
#if NUM_SERIAL > 0
Serial1.begin(serial_link_speed);
wifiUp = setup(Serial1, wifiESSID, wifiPassword, hostname, port);
#endif
// Other serials are tried, depending on hardware.
#if NUM_SERIAL > 1
if (!wifiUp)
{
Serial2.begin(serial_link_speed);
wifiUp = setup(Serial2, wifiESSID, wifiPassword, hostname, port);
}
#endif
#if NUM_SERIAL > 2
if (!wifiUp)
{
Serial3.begin(serial_link_speed);
wifiUp = setup(Serial3, wifiESSID, wifiPassword, hostname, port);
}
#endif
return wifiUp;
}
bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, int port) {
@@ -59,9 +108,10 @@ bool WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid
StringFormatter::send(wifiStream, F("ATE0\r\n")); // turn off the echo
checkForOK(200, OK_SEARCH, true);
}
streamer=new MemStream(buffer, MAX_WIFI_BUFFER);
parser.setAtCommandCallback(ATCommand);
DCCEXParser::setAtCommandCallback(ATCommand);
WifiInboundHandler::setup(wifiStream);
DIAG(F("\n++ Wifi Setup %S ++\n"), connected ? F("OK") : F("FAILED"));
return connected;
}
@@ -78,7 +128,7 @@ bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringH
// If there is, just shortcut the setup and continue to read the data as normal.
if (checkForOK(200,IPD_SEARCH, true)) {
DIAG(F("\nPreconfigured Wifi already running with data waiting\n"));
loopstate=4; // carry on from correct place
// loopstate=4; // carry on from correct place... or not as the case may be
return true;
}
@@ -215,9 +265,7 @@ void WifiInterface::ATCommand(const byte * command) {
}
}
void WifiInterface::setHTTPCallback(HTTP_CALLBACK callback) {
httpCallback = callback;
}
bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo, bool escapeEcho) {
unsigned long startTime = millis();
@@ -244,162 +292,10 @@ bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor
return false;
}
bool WifiInterface::isHTTP() {
// POST GET PUT PATCH DELETE
// You may think a simple strstr() is better... but not when ram & time is in short supply
switch (buffer[0]) {
case 'P':
if (buffer[1] == 'U' && buffer[2] == 'T' && buffer[3] == ' ' ) return true;
if (buffer[1] == 'O' && buffer[2] == 'S' && buffer[3] == 'T' && buffer[4] == ' ') return true;
if (buffer[1] == 'A' && buffer[2] == 'T' && buffer[3] == 'C' && buffer[4] == 'H' && buffer[5] == ' ') return true;
return false;
case 'G':
if (buffer[1] == 'E' && buffer[2] == 'T' && buffer[3] == ' ' ) return true;
return false;
case 'D':
if (buffer[1] == 'E' && buffer[2] == 'L' && buffer[3] == 'E' && buffer[4] == 'T' && buffer[5] == 'E' && buffer[6] == ' ') return true;
return false;
default:
return false;
}
}
void WifiInterface::loop() {
if (!connected) return;
WiThrottle::loop(); // check heartbeats
// read anything into a buffer, collecting info on the way
while (loopstate != 99 && wifiStream->available()) {
int ch = wifiStream->read();
// echo the char to the diagnostic stream in escaped format
if (Diag::WIFI) StringFormatter::printEscape(ch); // DIAG in disguise
switch (loopstate) {
case 0: // looking for +IPD
connectionId = 0;
if (ch == '+') loopstate = 1;
break;
case 1: // Looking for I in +IPD
loopstate = (ch == 'I') ? 2 : 0;
break;
case 2: // Looking for P in +IPD
loopstate = (ch == 'P') ? 3 : 0;
break;
case 3: // Looking for D in +IPD
loopstate = (ch == 'D') ? 4 : 0;
break;
case 4: // Looking for , After +IPD
loopstate = (ch == ',') ? 5 : 0;
break;
case 5: // reading connection id
if (ch == ',') loopstate = 6;
else connectionId = 10 * connectionId + (ch - '0');
break;
case 6: // reading for length
if (ch == ':') loopstate = (datalength == 0) ? 99 : 7; // 99 is getout without reading next char
else datalength = datalength * 10 + (ch - '0');
streamer->flush(); // basically sets write point at start of buffer
break;
case 7: // reading data
streamer->write(ch); // NOTE: The MemStream will throw away bytes that do not fit in the buffer.
// This protects against buffer overflows even with things as innocent
// as a browser which send massive, irrlevent HTTP headers.
datalength--;
if (datalength == 0) {
buffer[streamer->available()]='\0'; // mark end of buffer, so it can be used as a string later
loopstate = 99;
}
break;
case 10: // Waiting for > so we can send reply
if (millis() - loopTimeoutStart > LOOP_TIMEOUT) {
if (Diag::WIFI) DIAG(F("\nWifi TIMEOUT on wait for > prompt or ERROR\n"));
loopstate = 0; // go back to +IPD
break;
}
if (ch == '>') {
// DIAG(F("\n> [%e]\n"),buffer);
wifiStream->print((char *) buffer);
loopTimeoutStart = millis();
loopstate = closeAfter ? 11 : 0;
break;
}
if (ch == '.') { // busy during send, delay and retry
loopstate = 12; // look for SEND OK finished
break;
}
break;
case 11: // Waiting for SEND OK or ERROR to complete so we can closeAfter
if (millis() - loopTimeoutStart > LOOP_TIMEOUT) {
if (Diag::WIFI) DIAG(F("\nWifi TIMEOUT on wait for SEND OK or ERROR\n"));
loopstate = 0; // go back to +IPD
break;
}
if (ch == 'K') { // assume its in SEND OK
if (Diag::WIFI) DIAG(F("\n Wifi AT+CIPCLOSE=%d\r\n"), connectionId);
StringFormatter::send(wifiStream, F("AT+CIPCLOSE=%d\r\n"), connectionId);
loopstate = 0; // wait for +IPD
}
break;
case 12: // Waiting for OK after send busy
if (ch == '+') { // Uh-oh IPD problem
if (Diag::WIFI) DIAG(F("\n\n Wifi ASYNC CLASH - LOST REPLY\n"));
connectionId = 0;
loopstate = 1;
}
if (ch == 'K') { // assume its in SEND OK
if (Diag::WIFI) DIAG(F("\n\n Wifi BUSY RETRYING.. AT+CIPSEND=%d,%d\r\n"), connectionId, streamer->available());
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer->available());
loopTimeoutStart = millis();
loopstate = 10; // non-blocking loop waits for > before sending
break;
}
break;
} // switch
} // while
if (loopstate != 99) return;
// AT this point we have read an incoming message into the buffer
if (Diag::WIFI) DIAG(F("\n%l Wifi(%d)<-[%e]\n"), millis(),connectionId, buffer);
streamer->setBufferContentPosition(0, 0); // reset write position to start of buffer
// SIDE EFFECT WARNING:::
// We know that parser will read the entire buffer before starting to write to it.
// Otherwise we would have to copy the buffer elsewhere and RAM is in short supply.
closeAfter = false;
// Intercept HTTP requests
if (isHTTP()) {
if (httpCallback) httpCallback(streamer, buffer);
else {
StringFormatter::send(streamer, F("HTTP/1.1 404 Not Found\nContent-Type: text/html\nConnnection: close\n\n"));
StringFormatter::send(streamer, F("<html><body>This is <b>not</b> a web server.<br/></body></html>"));
}
closeAfter = true;
if (connected) {
WiThrottle::loop();
WifiInboundHandler::loop();
}
else if (buffer[0] == '<') parser.parse(streamer, buffer, true); // tell JMRI parser that ACKS are blocking because we can't handle the async
else WiThrottle::getThrottle(connectionId)->parse(*streamer, buffer);
if (streamer->available() == 0) {
// No reply
if (closeAfter) {
if (Diag::WIFI) DIAG(F("AT+CIPCLOSE=%d\r\n"), connectionId);
StringFormatter::send(wifiStream, F("AT+CIPCLOSE=%d\r\n"), connectionId);
}
loopstate = 0; // go back to waiting for +IPD
return;
}
// prepare to send reply
buffer[streamer->available()]='\0'; // mark end of buffer, so it can be used as a string later
if (Diag::WIFI) DIAG(F("%l WiFi(%d)->[%e] l(%d)\n"), millis(), connectionId, buffer, streamer->available());
if (Diag::WIFI) DIAG(F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer->available());
StringFormatter::send(wifiStream, F("AT+CIPSEND=%d,%d\r\n"), connectionId, streamer->available());
loopTimeoutStart = millis();
loopstate = 10; // non-blocking loop waits for > before sending
}