1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-11-26 17:46:14 +01:00

Fixes for unowifi connecting, fix for baud rate and garbled text, start inbound messages using base handler already made

This commit is contained in:
dexslab 2021-10-03 00:31:45 -04:00
parent 9a9715ccbc
commit 72ced97abf
7 changed files with 316 additions and 60 deletions

3
.gitignore vendored
View File

@ -5,6 +5,5 @@ Release/*
.clang_complete
.gcc-flags.json
.pio/
.vscode/
.vscode/*
config.h
.vscode/extensions.json

View File

@ -57,7 +57,12 @@ void setup()
// Responsibility 1: Start the usb connection for diagnostics
// This is normally Serial but uses SerialUSB on a SAMD processor
// Uno Wifi Rev2 is stupid wont read past this baud rate for some reason
#ifdef ARDUINO_AVR_UNO_WIFI_REV2
Serial.begin(74880);
#else
Serial.begin(115200);
#endif
DIAG(F("DCC++ EX v%S"),F(VERSION));
CONDITIONAL_LCD_START {

View File

@ -1,5 +1,5 @@
/*
* © 2021, Chris Harlow. All rights reserved.
* © 2021, Chris Harlow, Anthony Williams. All rights reserved.
*
* This file is part of DCC-EX/CommandStation-EX
*
@ -23,41 +23,51 @@
#include "WifiInterfaceRev2.h"
#include "DIAG.h"
#include "CommandDistributor.h"
#include <SPI.h>
#include <WiFiNINA.h>
WifiInterface * WifiInterface::singleton;
WiFiServer WifiInterface::server(2560);
bool WifiInterface::connected=false;
bool WifiInterface::connected = false;
/**
* @brief Setup Wifi Connection
*
*/
bool WifiInterface::setup(long serial_link_speed,
bool WifiInterface::setup(long serial_link_speed,
const FSH *wifiESSID,
const FSH *wifiPassword,
const FSH *hostname,
const int port) {
const int port)
{
singleton = new WifiInterface(serial_link_speed, wifiESSID, wifiPassword, hostname, port);
//return singleton->connected;
}
WifiInterface::WifiInterface(long serial_link_speed,
const FSH *wifiESSID,
const FSH *wifiPassword,
const FSH *hostname,
const int port)
{
(void)serial_link_speed;
(void)port; // obsolete
(void)hostname; // To be implemented
if (WiFi.status() == WL_NO_MODULE) {
if (WiFi.status() == WL_NO_MODULE)
{
DIAG(F("Wifi- hardware failed\n"));
return false;
}
DIAG(F("Wifi Firmware=%s expected=%S"),WiFi.firmwareVersion(),F(WIFI_FIRMWARE_LATEST_VERSION));
DIAG(F("Wifi Firmware=%s expected=%S"), WiFi.firmwareVersion(), F(WIFI_FIRMWARE_LATEST_VERSION));
int status = WL_IDLE_STATUS;
int attempts = 4;
while (status != WL_CONNECTED) {
if (attempts-- <= 0) {
while (status != WL_CONNECTED)
{
if (attempts-- <= 0)
{
DIAG(F("\nFAILED - No Wifi\n"));
return false;
}
DIAG(F("\nAttempting to connect to %s\n"),wifiESSID);
DIAG(F("\nAttempting to connect to %s\n"), wifiESSID);
status = WiFi.begin(wifiESSID, wifiPassword);
// wait 10 seconds for connection:
delay(10000);
@ -66,38 +76,234 @@ bool WifiInterface::connected=false;
server.begin(); // start the server on port 2560
IPAddress ip = WiFi.localIP();
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
LCD(5,F("Port:2560"));
outboundRing=new RingStream(OUTBOUND_RING_SIZE);
LCD(4, F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
LCD(5, F("Port:2560"));
}
void WifiInterface::loop() {
singleton->loop1();
}
/**
* @brief Main loop for the WifiInterfaceRev2
*
*/
void WifiInterface::loop()
void WifiInterface::loop1()
{
if (loop2() != INBOUND_IDLE)
return;
// WiFiClient client = server.available(); // listen for incoming clients
// if (client)
// {
// // read bytes from a client
// byte buffer[MAX_NINA_BUFFER];
// int count = client.read(buffer, MAX_NINA_BUFFER - 1);
// buffer[count] = '\0'; // terminate the string properly
// if (Diag::WIFI)
// DIAG(F("WIFI:%e\n"), buffer);
// // TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
// outboundRing->mark(1);
// // TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
// CommandDistributor::parse(1, buffer, outboundRing);
// outboundRing->commit();
// int socketOut = outboundRing->read();
// if (socketOut >= 0)
// {
// int count = outboundRing->count();
// if (Diag::WIFI)
// DIAG(F("Wifi Reply count=:%d\n"), count);
// for (; count > 0; count--)
// client.write(outboundRing->read());
// client.flush(); //maybe
// }
// }
}
WifiInterface::INBOUND_STATE WifiInterface::loop2()
{
WiFiClient client = server.available(); // listen for incoming clients
if (client)
{
// read bytes from a client
byte buffer[MAX_NINA_BUFFER];
int count = client.read(buffer, MAX_NINA_BUFFER-1);
buffer[count] = '\0'; // terminate the string properly
if (Diag::WIFI) DIAG(F("WIFI:%e\n"), buffer);
// TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
outboundRing->mark(1);
// TEMPORARY - Assume all clients are client 1, this will confuse WiThrottle!
CommandDistributor::parse(1,buffer,outboundRing);
outboundRing->commit();
int socketOut=outboundRing->read();
if (socketOut>=0) {
int count=outboundRing->count();
if (Diag::WIFI) DIAG(F("Wifi Reply count=:%d\n"), count);
for(;count>0;count--) client.write(outboundRing->read());
client.flush(); //maybe
while (client.available())
{
int ch = client.read();
// echo the char to the diagnostic stream in escaped format
if (Diag::WIFI)
{
// DIAG(F(" %d/"), loopState);
StringFormatter::printEscape(ch); // DIAG in disguise
}
switch (loopState)
{
case ANYTHING: // looking for +IPD, > , busy , n,CONNECTED, n,CLOSED, ERROR, SEND OK
if (ch == '+')
{
loopState = IPD;
break;
}
if (ch == '>')
{
if (Diag::WIFI)
DIAG(F("[XMIT %d]"), currentReplySize);
// for (int i = 0; i < currentReplySize; i++)
// {
// int cout = outboundRing->read();
// client.print(cout);
// if (Diag::WIFI)
// StringFormatter::printEscape(cout); // DIAG in disguise
// }
clientPendingCIPSEND = -1;
pendingCipsend = false;
loopState = SKIPTOEND;
break;
}
if (ch == 'R')
{ // Received ... bytes
loopState = SKIPTOEND;
break;
}
if (ch == 'S')
{ // SEND OK probably
loopState = SKIPTOEND;
break;
}
if (ch == 'b')
{ // This is a busy indicator... probabaly must restart a CIPSEND
pendingCipsend = (clientPendingCIPSEND >= 0);
loopState = SKIPTOEND;
break;
}
if (ch >= '0' && ch <= '9')
{
runningClientId = ch - '0';
loopState = GOT_CLIENT_ID;
break;
}
if (ch == 'E' || ch == 'l')
{ // ERROR or "link is not valid"
if (clientPendingCIPSEND >= 0)
{
// A CIPSEND was errored... just toss it away
//purgeCurrentCIPSEND();
}
loopState = SKIPTOEND;
break;
}
break;
case IPD: // Looking for I in +IPD
loopState = (ch == 'I') ? IPD1 : SKIPTOEND;
break;
case IPD1: // Looking for P in +IPD
loopState = (ch == 'P') ? IPD2 : SKIPTOEND;
break;
case IPD2: // Looking for D in +IPD
loopState = (ch == 'D') ? IPD3 : SKIPTOEND;
break;
case IPD3: // Looking for , After +IPD
loopState = (ch == ',') ? IPD4_CLIENT : SKIPTOEND;
break;
case IPD4_CLIENT: // reading connection id
if (ch >= '0' || ch <= '9')
{
runningClientId = ch - '0';
loopState = IPD5;
}
else
loopState = SKIPTOEND;
break;
case IPD5: // Looking for , After +IPD,client
loopState = (ch == ',') ? IPD6_LENGTH : SKIPTOEND;
dataLength = 0; // ready to start collecting the length
break;
case IPD6_LENGTH: // reading for length
if (ch == ':')
{
if (dataLength == 0)
{
loopState = ANYTHING;
break;
}
if (Diag::WIFI)
DIAG(F("\nWifi inbound data(%d:%d):"), runningClientId, dataLength);
// if (server.freeSpace() <= (dataLength + 1))
// {
// // This input would overflow the inbound ring, ignore it
// loopState = IPD_IGNORE_DATA;
// if (Diag::WIFI)
// DIAG(F("\nWifi OVERFLOW IGNORING:"));
// break;
// }
// server.mark(runningClientId);
loopState = IPD_DATA;
break;
}
dataLength = dataLength * 10 + (ch - '0');
break;
case IPD_DATA: // reading data
server.write(ch);
dataLength--;
if (dataLength == 0)
{
//server.commit();
loopState = ANYTHING;
}
break;
case IPD_IGNORE_DATA: // ignoring data that would not fit in inbound ring
dataLength--;
if (dataLength == 0)
loopState = ANYTHING;
break;
case GOT_CLIENT_ID: // got x before CLOSE or CONNECTED
loopState = (ch == ',') ? GOT_CLIENT_ID2 : SKIPTOEND;
break;
case GOT_CLIENT_ID2: // got "x,"
if (ch == 'C')
{
// got "x C" before CLOSE or CONNECTED, or CONNECT FAILED
//if (runningClientId == clientPendingCIPSEND)
//purgeCurrentCIPSEND();
}
loopState = SKIPTOEND;
break;
case SKIPTOEND: // skipping for /n
if (ch == '\n')
loopState = ANYTHING;
break;
} // switch
}
} // available
return (loopState == ANYTHING) ? INBOUND_IDLE : INBOUND_BUSY;
}
// void WifiInboundHandler::purgeCurrentCIPSEND() {
// // A CIPSEND was sent but errored... or the client closed just toss it away
// if (Diag::WIFI) DIAG(F("Wifi: DROPPING CIPSEND=%d,%d\n"),clientPendingCIPSEND,currentReplySize);
// for (int i=0;i<=currentReplySize;i++) outboundRing->read();
// pendingCipsend=false;
// clientPendingCIPSEND=-1;
// }
#endif

View File

@ -36,9 +36,54 @@ public:
const FSH *hostname,
const int port = 2560); // ignored
static void loop();
private:
static WifiInterface *singleton;
WifiInterface(long serial_link_speed,
const FSH *wifiESSID,
const FSH *wifiPassword,
const FSH *hostname,
const int port);
enum INBOUND_STATE : byte
{
INBOUND_BUSY, // keep calling in loop()
INBOUND_IDLE // Nothing happening, outbound may xcall CIPSEND
};
enum LOOP_STATE : byte
{
ANYTHING, // ready for +IPD, n CLOSED, n CONNECTED, busy etc...
SKIPTOEND, // skip to newline
// +IPD,client,length:data
IPD, // got +
IPD1, // got +I
IPD2, // got +IP
IPD3, // got +IPD
IPD4_CLIENT, // got +IPD, reading cient id
IPD5, // got +IPD,c
IPD6_LENGTH, // got +IPD,c, reading length
IPD_DATA, // got +IPD,c,ll,: collecting data
IPD_IGNORE_DATA, // got +IPD,c,ll,: ignoring the data that won't fit inblound Ring
GOT_CLIENT_ID, // clientid prefix to CONNECTED / CLOSED
GOT_CLIENT_ID2 // clientid prefix to CONNECTED / CLOSED
};
LOOP_STATE loopState = ANYTHING;
int runningClientId; // latest client inbound processing data or CLOSE
int dataLength; // dataLength of +IPD
int clientPendingCIPSEND = -1;
int currentReplySize;
bool pendingCipsend;
void purgeCurrentCIPSEND();
static WiFiServer server;
static bool connected;
static RingStream * outboundRing;
//static RingStream *outboundRing;
void loop1();
INBOUND_STATE loop2();
};
#endif

View File

@ -23,7 +23,7 @@
// WIFI_ON: All prereqs for running with WIFI are met
//
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO))
#if ENABLE_WIFI && (defined(ARDUINO_AVR_MEGA) || defined(ARDUINO_AVR_MEGA2560) || defined(ARDUINO_SAMD_ZERO)) || defined(ARDUINO_AVR_UNO_WIFI_REV2)
#define WIFI_ON true
#else
#define WIFI_ON false

View File

@ -63,10 +63,11 @@ framework = arduino
lib_deps =
${env.lib_deps}
DIO2
WiFiNINA
arduino-libraries/Ethernet
SPI
marcoschwartz/LiquidCrystal_I2C
monitor_speed = 115200
monitor_speed = 74880
monitor_flags = --echo
build_flags = "-DF_CPU=16000000L -DARDUINO=10813 -DARDUINO_AVR_UNO_WIFI_DEV_ED -DARDUINO_ARCH_AVR -DESP_CH_UART -DESP_CH_UART_BR=19200"g