1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00

Merge branch 'Stream-Reference-Fix'

This commit is contained in:
Asbelos 2020-08-01 17:00:31 +01:00
commit e01b929fd0
13 changed files with 120 additions and 102 deletions

View File

@ -35,7 +35,7 @@
//
// The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup().
void myFilter(Print & stream, byte & opcode, byte & paramCount, int p[]) {
void myFilter(Print * stream, byte & opcode, byte & paramCount, int p[]) {
(void)stream; // avoid compiler warning if we don't access this parameter
switch (opcode) {
case '!': // Create a bespoke new command to clear all loco reminders <!> or specific locos e.g <! 3 4 99>
@ -116,7 +116,7 @@ void loop() {
// Responsibility 3: Optionally handle any incoming WiFi traffic
#ifdef WIFI
WifiInterface::loop(Serial1);
WifiInterface::loop();
#endif
// Your additional code

View File

@ -20,6 +20,7 @@
#include "DCCEXParser.h"
#include "DCC.h"
#include "DCCWaveform.h"
#include "WifiInterface.h"
#include "Turnouts.h"
#include "Outputs.h"
#include "Sensors.h"
@ -39,7 +40,7 @@ const int HASH_KEYWORD_JOIN=-30750;
int DCCEXParser::stashP[MAX_PARAMS];
bool DCCEXParser::stashBusy;
Print & DCCEXParser::stashStream=DIAGSERIAL; // keep compiler happy but ovevride in constructor
Print * DCCEXParser::stashStream=NULL;
// This is a JMRI command parser, one instance per incoming stream
// It doesnt know how the string got here, nor how it gets back.
@ -67,7 +68,7 @@ void DCCEXParser::loop(Stream & stream) {
}
else if (ch == '>') {
buffer[bufferLength]='\0';
parse( stream, buffer, false); // Parse this allowing async responses
parse( & stream, buffer, false); // Parse this allowing async responses
inCommandPayload = false;
break;
} else if(inCommandPayload) {
@ -131,7 +132,7 @@ void DCCEXParser::setFilter(FILTER_CALLBACK filter) {
}
// See documentation on DCC class for info on this section
void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) {
void DCCEXParser::parse(Print * stream, byte *com, bool blocking) {
DIAG(F("\nPARSING:%s\n"),com);
(void) EEPROM; // tell compiler not to warn thi is unused
int p[MAX_PARAMS];
@ -282,6 +283,9 @@ void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) {
DCC::setFn(p[0],p[1],p[2]==1);
return;
case '+' : // Complex Wifi interface command (not usual parse)
WifiInterface::ATCommand(com);
break;
default: //anything else will diagnose and drop out to <X>
DIAG(F("\nOpcode=%c params=%d\n"),opcode,params);
@ -294,7 +298,7 @@ void DCCEXParser::parse(Print & stream, const byte *com, bool blocking) {
StringFormatter::send(stream, F("<X>"));
}
bool DCCEXParser::parseZ( Print & stream,int params, int p[]){
bool DCCEXParser::parseZ( Print * stream,int params, int p[]){
switch (params) {
@ -324,7 +328,7 @@ bool DCCEXParser::parseZ( Print & stream,int params, int p[]){
}
//===================================
bool DCCEXParser::parsef(Print & stream, int params, int p[]) {
bool DCCEXParser::parsef(Print * stream, int params, int p[]) {
// JMRI sends this info in DCC message format but it's not exactly
// convenient for other processing
if (params==2) {
@ -356,7 +360,7 @@ void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop) {
}
//===================================
bool DCCEXParser::parseT(Print & stream, int params, int p[]) {
bool DCCEXParser::parseT(Print * stream, int params, int p[]) {
switch(params){
case 0: // <T>
return (Turnout::showAll(stream)); break;
@ -381,7 +385,7 @@ bool DCCEXParser::parseT(Print & stream, int params, int p[]) {
}
}
bool DCCEXParser::parseS( Print & stream,int params, int p[]) {
bool DCCEXParser::parseS( Print * stream,int params, int p[]) {
switch(params){
@ -405,7 +409,7 @@ bool DCCEXParser::parseS( Print & stream,int params, int p[]) {
// CALLBACKS must be static
bool DCCEXParser::stashCallback(Print & stream,int p[MAX_PARAMS]) {
bool DCCEXParser::stashCallback(Print * stream,int p[MAX_PARAMS]) {
if (stashBusy || asyncBanned) return false;
stashBusy=true;
stashStream=stream;

View File

@ -20,13 +20,13 @@
#define DCCEXParser_h
#include <Arduino.h>
typedef void (*FILTER_CALLBACK)(Print & stream, byte & opcode, byte & paramCount, int p[]);
typedef void (*FILTER_CALLBACK)(Print * stream, byte & opcode, byte & paramCount, int p[]);
struct DCCEXParser
{
DCCEXParser();
void loop(Stream & pstream);
void parse(Print & stream, const byte * command, bool blocking);
void loop(Stream & stream);
void parse(Print * stream, byte * command, bool blocking);
void flush();
static void setFilter(FILTER_CALLBACK filter);
static const int MAX_PARAMS=10; // Must not exceed this
@ -40,17 +40,17 @@ struct DCCEXParser
byte buffer[MAX_BUFFER+2];
int splitValues( int result[MAX_PARAMS], const byte * command);
bool parseT(Print & stream, int params, int p[]);
bool parseZ(Print & stream, int params, int p[]);
bool parseS(Print & stream, int params, int p[]);
bool parsef(Print & stream, int params, int p[]);
bool parseT(Print * stream, int params, int p[]);
bool parseZ(Print * stream, int params, int p[]);
bool parseS(Print * stream, int params, int p[]);
bool parsef(Print * stream, int params, int p[]);
static bool stashBusy;
static Print & stashStream;
static Print * stashStream;
static int stashP[MAX_PARAMS];
bool stashCallback(Print & stream, int p[MAX_PARAMS]);
bool stashCallback(Print * stream, int p[MAX_PARAMS]);
static void callback_W(int result);
static void callback_B(int result);
static void callback_R(int result);

View File

@ -121,7 +121,7 @@ bool Output::remove(int n){
///////////////////////////////////////////////////////////////////////////////
bool Output::showAll(Print & stream){
bool Output::showAll(Print * stream){
bool gotone=false;
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
gotone=true;
@ -130,7 +130,7 @@ bool Output::showAll(Print & stream){
return gotone;
}
void Output::show(Print & stream){
void Output::show(Print * stream){
for(Output * tt=firstOutput;tt!=NULL;tt=tt->nextOutput){
StringFormatter::send(stream,F("<Y %d %d>"), tt->data.id, tt->data.oStatus);
}

View File

@ -36,8 +36,8 @@ class Output{
static void load();
static void store();
static Output *create(int, int, int, int=0);
static void show(Print & stream);
static bool showAll(Print & stream);
static void show(Print * stream);
static bool showAll(Print * stream);
private:
static Output *firstOutput;

View File

@ -73,7 +73,7 @@ decide to ignore the <q ID> return and only react to <Q ID> triggers.
///////////////////////////////////////////////////////////////////////////////
void Sensor::check(Print & stream){
void Sensor::check(Print * stream){
Sensor *tt;
for(tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
@ -148,7 +148,7 @@ bool Sensor::remove(int n){
///////////////////////////////////////////////////////////////////////////////
void Sensor::show(Print & stream){
void Sensor::show(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream, F("<Q %d %d %d>"), tt->data.snum, tt->data.pin, tt->data.pullUp);
}
@ -156,7 +156,7 @@ void Sensor::show(Print & stream){
///////////////////////////////////////////////////////////////////////////////
void Sensor::status(Print & stream){
void Sensor::status(Print * stream){
for(Sensor * tt=firstSensor;tt!=NULL;tt=tt->nextSensor){
StringFormatter::send(stream,F("<%c %d>"), tt->active?'Q':'q', tt->data.snum);
}

View File

@ -40,9 +40,9 @@ struct Sensor{
static Sensor *create(int, int, int);
static Sensor* get(int);
static bool remove(int);
static void show(Print & stream);
static void status(Print & stream);
static void check(Print & stream);
static void show(Print * stream);
static void status(Print * stream);
static void check(Print * stream);
}; // Sensor
#endif

View File

@ -22,16 +22,22 @@
void StringFormatter::print( const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send2(DIAGSERIAL,input,args);
send(& DIAGSERIAL,input,args);
}
void StringFormatter::send(Print & stream, const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send2(stream,input,args);
send(& stream,input,args);
}
void StringFormatter::send(Print * stream, const __FlashStringHelper* input...) {
va_list args;
va_start(args, input);
send(stream,input,args);
}
void StringFormatter::send2(Print & stream,const __FlashStringHelper* format, va_list args) {
void StringFormatter::send(Print * stream,const __FlashStringHelper* format, va_list args) {
// thanks to Jan Turoň https://arduino.stackexchange.com/questions/56517/formatting-strings-in-arduino-for-output
@ -39,35 +45,35 @@ void StringFormatter::send2(Print & stream,const __FlashStringHelper* format, va
for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i);
if (c=='\0') return;
if(c!='%') { stream.print(c); continue; }
if(c!='%') { stream->print(c); continue; }
i++;
c=pgm_read_byte_near(flash+i);
switch(c) {
case '%': stream.print('%'); break;
case 'c': stream.print((char) va_arg(args, int)); break;
case 's': stream.print(va_arg(args, char*)); break;
case '%': stream->print('%'); break;
case 'c': stream->print((char) va_arg(args, int)); break;
case 's': stream->print(va_arg(args, char*)); break;
case 'e': printEscapes(stream,va_arg(args, char*)); break;
case 'E': printEscapes(stream,(const __FlashStringHelper*)va_arg(args, char*)); break;
case 'S': stream.print((const __FlashStringHelper*)va_arg(args, char*)); break;
case 'd': stream.print(va_arg(args, int), DEC); break;
case 'l': stream.print(va_arg(args, long), DEC); break;
case 'b': stream.print(va_arg(args, int), BIN); break;
case 'o': stream.print(va_arg(args, int), OCT); break;
case 'x': stream.print(va_arg(args, int), HEX); break;
case 'f': stream.print(va_arg(args, double), 2); break;
case 'S': stream->print((const __FlashStringHelper*)va_arg(args, char*)); break;
case 'd': stream->print(va_arg(args, int), DEC); break;
case 'l': stream->print(va_arg(args, long), DEC); break;
case 'b': stream->print(va_arg(args, int), BIN); break;
case 'o': stream->print(va_arg(args, int), OCT); break;
case 'x': stream->print(va_arg(args, int), HEX); break;
case 'f': stream->print(va_arg(args, double), 2); break;
}
}
va_end(args);
}
void StringFormatter::printEscapes(Print & stream, char * input) {
void StringFormatter::printEscapes(Print * stream, char * input) {
for(int i=0; ; ++i) {
char c=input[i];
printEscape(stream,c);
if (c=='\0') return;
}
}
void StringFormatter::printEscapes(Print & stream, const __FlashStringHelper* input) {
void StringFormatter::printEscapes(Print * stream, const __FlashStringHelper* input) {
char* flash=(char*)input;
for(int i=0; ; ++i) {
char c=pgm_read_byte_near(flash+i);
@ -75,14 +81,14 @@ void StringFormatter::printEscapes(Print & stream, const __FlashStringHelper* in
if (c=='\0') return;
}
}
void StringFormatter::printEscape(Print & stream, char c) {
void StringFormatter::printEscape(Print * stream, char c) {
switch(c) {
case '\n': stream.print(F("\\n")); break;
case '\r': stream.print(F("\\r")); break;
case '\0': stream.print(F("\\0")); return;
case '\t': stream.print(F("\\t")); break;
case '\\': stream.print(F("\\")); break;
default: stream.print(c);
case '\n': stream->print(F("\\n")); break;
case '\r': stream->print(F("\\r")); break;
case '\0': stream->print(F("\\0")); return;
case '\t': stream->print(F("\\t")); break;
case '\\': stream->print(F("\\")); break;
default: stream->print(c);
}
}

View File

@ -33,11 +33,11 @@ class StringFormatter
public:
static void print( const __FlashStringHelper* input...);
static void send(Print & serial, const __FlashStringHelper* input...);
static void printEscapes(Print & stream, char * input);
static void printEscapes(Print & stream, const __FlashStringHelper* input);
static void printEscape(Print & stream, char c);
private:
static void send2(Print & serial, const __FlashStringHelper* input,va_list args);
static void send(Print * serial, const __FlashStringHelper* input...);
static void printEscapes(Print * stream, char * input);
static void printEscapes(Print * stream, const __FlashStringHelper* input);
static void printEscape(Print * stream, char c);
static void send(Print * serial, const __FlashStringHelper* input,va_list args);
};
#endif

View File

@ -74,7 +74,7 @@ bool Turnout::remove(int n){
///////////////////////////////////////////////////////////////////////////////
void Turnout::show(Print & stream, int n){
void Turnout::show(Print * stream, int n){
for(Turnout *tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
if (tt->data.id==n) {
StringFormatter::send(stream,F("<H %d %d>"), tt->data.id, tt->data.tStatus & STATUS_ACTIVE);
@ -83,7 +83,7 @@ void Turnout::show(Print & stream, int n){
}
}
bool Turnout::showAll(Print & stream){
bool Turnout::showAll(Print * stream){
bool gotOne=false;
for(Turnout * tt=firstTurnout;tt!=NULL;tt=tt->nextTurnout){
StringFormatter::send(stream,F("<H %d %d %d %d>"), tt->data.id, tt->data.address, tt->data.subAddress, (tt->data.tStatus & STATUS_ACTIVE)!=0);

View File

@ -47,8 +47,8 @@ class Turnout {
static Turnout *create(int id , int address , int subAddress);
static Turnout *create(int id , byte pin , int activeAngle, int inactiveAngle);
static Turnout *create(int id);
static void show(Print & stream, int n);
static bool showAll(Print & stream);
static void show(Print * stream, int n);
static bool showAll(Print * stream);
void activate(bool state);
}; // Turnout

View File

@ -37,85 +37,91 @@ int WifiInterface::datalength=0;
int WifiInterface::connectionId;
byte WifiInterface::buffer[MAX_WIFI_BUFFER];
MemStream WifiInterface::streamer(buffer,sizeof(buffer));
Stream * WifiInterface::wifiStream=NULL;
void WifiInterface::setup(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
void WifiInterface::setup(Stream & setupStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
wifiStream=&setupStream;
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
connected=setup2(wifiStream, SSid, password,hostname, servername,port);
connected=setup2( SSid, password,hostname, servername,port);
// TODO calloc the buffer and streamer and parser etc
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED"));
}
bool WifiInterface::setup2(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
bool WifiInterface::setup2(const __FlashStringHelper* SSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
int ipOK=0;
delay(1000);
StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
checkForOK(wifiStream,5000,READY_SEARCH,false); // generally not interesting to DCC
checkForOK(5000,READY_SEARCH,false); // generally not interesting to DCC
StringFormatter::send(wifiStream,F("AT+GMR\r\n")); // request AT version
checkForOK(wifiStream,2000,OK_SEARCH,true); // Makes this visible on the console
checkForOK(2000,OK_SEARCH,true); // Makes this visible on the console
StringFormatter::send(wifiStream,F("AT+CWMODE=1\r\n")); // configure as client
checkForOK(wifiStream,1000,OK_SEARCH,true); // Not always OK, sometimes "no change"
checkForOK(1000,OK_SEARCH,true); // Not always OK, sometimes "no change"
StringFormatter::send(wifiStream,F("AT+CIFSR\r\n"));
if(checkForOK(wifiStream,5000, (const char*) F("+CIFSR:STAIP"),true))
if(!checkForOK(wifiStream,1000, (const char*) F("0.0.0.0"),true))
if(checkForOK(5000, (const char*) F("+CIFSR:STAIP"),true))
if(!checkForOK(1000, (const char*) F("0.0.0.0"),true))
ipOK=1;
if(!ipOK) {
StringFormatter::send(wifiStream,F("AT+CWMODE=3\r\n")); // configure as server or access point
checkForOK(wifiStream,1000,OK_SEARCH,true); // Not always OK, sometimes "no change"
checkForOK(1000,OK_SEARCH,true); // Not always OK, sometimes "no change"
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
StringFormatter::send(wifiStream,F("AT+CWJAP?\r\n"));
if (checkForOK(wifiStream,2000,OK_SEARCH,true)) {
if (checkForOK(2000,OK_SEARCH,true)) {
// early version supports CWJAP
StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password);
checkForOK(wifiStream,20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
checkForOK(20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
}
else {
// later version supports CWJAP_CUR
StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client
checkForOK(wifiStream,2000, OK_SEARCH, true); // dont care if not supported
checkForOK(2000, OK_SEARCH, true); // dont care if not supported
StringFormatter::send(wifiStream,F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"),SSid,password);
checkForOK(wifiStream,20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
checkForOK(20000,OK_SEARCH,true); // can ignore failure as AP mode may still be ok
StringFormatter::send(wifiStream,F("AT+CIPRECVMODE=0\r\n"),port); // make sure transfer mode is correct
checkForOK(wifiStream,2000,OK_SEARCH,true);
checkForOK(2000,OK_SEARCH,true);
// StringFormatter::send(wifiStream, F("AT+MDNS=1,\"%S.local\",\"%S.local\",%d\r\n"), hostname, servername, port); // Setup mDNS for Server
// if (!checkForOK(wifiStream,5000, OK_SEARCH, true)) return false;
// if (!checkForOK(5000, OK_SEARCH, true)) return false;
(void)servername; // avoid compiler warning from commented out AT_MDNS above
}
StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
if (!checkForOK(10000,OK_SEARCH,true)) return false;
}
StringFormatter::send(wifiStream,F("AT+CIPMUX=1\r\n")); // configure for multiple connections
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
if (!checkForOK(10000,OK_SEARCH,true)) return false;
StringFormatter::send(wifiStream,F("AT+CIPSERVER=1,%d\r\n"),port); // turn on server on port
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
if (!checkForOK(10000,OK_SEARCH,true)) return false;
return true;
}
bool WifiInterface::checkForOK(Stream & wifiStream, const unsigned int timeout, const char * waitfor, bool echo) {
void WifiInterface::ATCommand(const byte * command) {
StringFormatter:: send(wifiStream,F("AT+%s\r\n"), command+1);
checkForOK(10000,OK_SEARCH,true);
}
bool WifiInterface::checkForOK( const unsigned int timeout, const char * waitfor, bool echo) {
unsigned long startTime = millis();
char const *locator=waitfor;
DIAG(F("\nWifi Check: [%E]"),waitfor);
while( millis()-startTime < timeout) {
while(wifiStream.available()) {
int ch=wifiStream.read();
if (echo) StringFormatter::printEscape(DIAGSERIAL,ch); /// THIS IS A DIAG IN DISGUISE
while(wifiStream->available()) {
int ch=wifiStream->read();
if (echo) StringFormatter::printEscape(&DIAGSERIAL,ch); /// THIS IS A DIAG IN DISGUISE
if (ch!=pgm_read_byte_near(locator)) locator=waitfor;
if (ch==pgm_read_byte_near(locator)) {
locator++;
@ -151,17 +157,17 @@ bool WifiInterface::isHTML() {
}
}
void WifiInterface::loop(Stream & wifiStream) {
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();
while (loopstate!=99 && wifiStream->available()) {
int ch=wifiStream->read();
// echo the char to the diagnostic stream in escaped format
StringFormatter::printEscape(DIAGSERIAL,ch); // DIAG in disguise
StringFormatter::printEscape(&DIAGSERIAL,ch); // DIAG in disguise
switch (loopstate) {
case 0: // looking for +IPD
@ -203,7 +209,7 @@ void WifiInterface::loop(Stream & wifiStream) {
}
if (ch=='>'){
DIAG(F("\n> [%e]\n"),buffer);
wifiStream.print((char *) buffer);
wifiStream->print((char *) buffer);
loopTimeoutStart=millis();
loopstate=closeAfter?11:0;
}
@ -238,7 +244,7 @@ void WifiInterface::loop(Stream & wifiStream) {
HTTPParser::parse(streamer,buffer);
closeAfter=true;
}
else if (buffer[0]=='<') parser.parse(streamer,buffer, true); // tell JMRI parser that ACKS are blocking because we can't handle the async
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);

View File

@ -27,16 +27,18 @@
class WifiInterface {
public:
static void setup(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
static void setup(Stream & setupStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
static void loop(Stream & wifiStream);
static void loop();
static void ATCommand(const byte * command);
private:
static Stream * wifiStream;
static DCCEXParser parser;
static bool setup2(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
static bool setup2( const __FlashStringHelper* SSSid, const __FlashStringHelper* password,
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port);
static bool checkForOK(Stream & wifiStream, const unsigned int timeout, const char* waitfor, bool echo);
static bool checkForOK(const unsigned int timeout, const char* waitfor, bool echo);
static bool isHTML();
static bool connected;
static bool closeAfter;