From 6a923c4302385dca475e7cabad3b9387816e99e7 Mon Sep 17 00:00:00 2001 From: Asbelos Date: Mon, 29 Jun 2020 11:37:05 +0100 Subject: [PATCH] Cleaning up warnings (some of which were actually fatal) --- CVReader.ino | 3 ++- DCCEXParser.cpp | 14 +++++++------- DCCEXParser.h | 6 +++--- HTTPParser.cpp | 5 +++-- HTTPParser.h | 2 +- MemStream.h | 2 +- WiThrottle.cpp | 16 ++++++++-------- WiThrottle.h | 12 ++++++------ WifiInterface.cpp | 28 ++++++++++++++++++++++++---- WifiInterface.h | 3 ++- 10 files changed, 57 insertions(+), 34 deletions(-) diff --git a/CVReader.ino b/CVReader.ino index 37659b2..9ba6724 100644 --- a/CVReader.ino +++ b/CVReader.ino @@ -14,7 +14,8 @@ // // The filter must be enabled by calling the DCC EXParser::setFilter method, see use in setup(). -void myFilter(Stream & 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 'F': // Invent new command to call the new Loco Function API DIAG(F("Setting loco %d F%d %S"),p[0],p[1],p[2]?F("ON"):F("OFF")); diff --git a/DCCEXParser.cpp b/DCCEXParser.cpp index 4bfed24..d6b4468 100644 --- a/DCCEXParser.cpp +++ b/DCCEXParser.cpp @@ -54,18 +54,18 @@ void DCCEXParser::loop(Stream & stream) { } } - int DCCEXParser::splitValues( int result[MAX_PARAMS], char * cmd) { + int DCCEXParser::splitValues( int result[MAX_PARAMS], const byte * cmd) { byte state=1; byte parameterCount=0; int runningValue=0; - const char * remainingCmd=cmd+1; // skips the opcode + const byte * remainingCmd=cmd+1; // skips the opcode bool signNegative=false; // clear all parameters in case not enough found for (int i=0;i - if(p[2] != p[2] & 1) return; + if(p[2] != (p[2] & 1)) return; DCC::setAccessory(p[0],p[1],p[2]==1); return; @@ -286,14 +286,14 @@ bool DCCEXParser::parsef(Print & stream, int params, int p[]) { if (p[1]==222) funcmap(p[0],p[2],13,20); else if (p[1]==223) funcmap(p[0],p[2],21,28); } - // NO RESPONSE + (void)stream;// NO RESPONSE return true; } void DCCEXParser::funcmap(int cab, byte value, byte fstart, byte fstop) { for (int i=fstart;i<=fstop;i++) { DCC::setFn(cab, i, value & 1); - value>>1; + value>>=1; } } diff --git a/DCCEXParser.h b/DCCEXParser.h index 4fe4222..ed6bfb8 100644 --- a/DCCEXParser.h +++ b/DCCEXParser.h @@ -8,7 +8,7 @@ struct DCCEXParser { DCCEXParser(); void loop(Stream & pstream); - void parse(Print & stream, const char * command); + void parse(Print & stream, const byte * command); void flush(); static void setFilter(FILTER_CALLBACK filter); static const int MAX_PARAMS=10; // Must not exceed this @@ -18,8 +18,8 @@ struct DCCEXParser static const int MAX_BUFFER=50; // longest command sent in byte bufferLength=0; bool inCommandPayload=false; - char buffer[MAX_BUFFER+2]; - int splitValues( int result[MAX_PARAMS],char * command); + 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[]); diff --git a/HTTPParser.cpp b/HTTPParser.cpp index 27208f0..c3b7617 100644 --- a/HTTPParser.cpp +++ b/HTTPParser.cpp @@ -1,8 +1,9 @@ #include "HTTPParser.h" #include "StringFormatter.h" -void HTTPParser::parse(Print & stream, char * cmd) { - +void HTTPParser::parse(Print & stream, byte * cmd) { + (void)cmd; // Avoid compiler warning because this example doesnt use this parameter + // BEWARE - As soon as you start responding, the cmd buffer is trashed! // You must get everything you need from it before using StringFormatter::send! diff --git a/HTTPParser.h b/HTTPParser.h index 39a5083..655b3ba 100644 --- a/HTTPParser.h +++ b/HTTPParser.h @@ -3,6 +3,6 @@ #include class HTTPParser { public: - static void parse(Print & stream, char * cmd); + static void parse(Print & stream, byte * cmd); }; #endif diff --git a/MemStream.h b/MemStream.h index 0dbce67..444864f 100644 --- a/MemStream.h +++ b/MemStream.h @@ -46,7 +46,7 @@ public: operator const uint8_t *() const { return _buffer; } operator const char *() const { return (const char*)_buffer; } - const uint16_t current_length() const { return _pos_write; } + uint16_t current_length() const { return _pos_write; } bool listen() { return true; } void end() {} diff --git a/WiThrottle.cpp b/WiThrottle.cpp index 4f35ed7..1ca9d72 100644 --- a/WiThrottle.cpp +++ b/WiThrottle.cpp @@ -47,7 +47,7 @@ WiThrottle::WiThrottle(Print & stream, int wificlientid) { firstThrottle= this; clientid=wificlientid; for (int loco=0;loconextTurnout){ StringFormatter::send(stream,F("]\\[LT&d}|{%d}|{%d"), tt->data.id, tt->data.id, (bool)(tt->data.tStatus & STATUS_ACTIVE)); @@ -68,7 +68,7 @@ WiThrottle::~WiThrottle() { } } -void WiThrottle::parse(Print & stream, char * cmd) { +void WiThrottle::parse(Print & stream, byte * cmd) { heartBeat=millis(); DIAG(F("\nWiThrottle parse (%d) %s"),clientid, cmd); @@ -82,7 +82,7 @@ void WiThrottle::parse(Print & stream, char * cmd) { DCCWaveform::mainTrack.setPowerMode(cmd[3]=='1'?POWERMODE::ON:POWERMODE::OFF); StringFormatter::send(stream, F("PPA%c"),cmd[3]); } - else if (cmd[1]='T' && cmd[2]=='A') { // PTA accessory toggle + else if (cmd[1]=='T' && cmd[2]=='A') { // PTA accessory toggle // TODO... if we are given an address that is not a known Turnout... // should we create one or just send the DCC message. Turnout::activate(getInt(cmd+4),cmd[3]=='T'); @@ -96,7 +96,7 @@ void WiThrottle::parse(Print & stream, char * cmd) { break; } } -int WiThrottle::getInt(char * cmd) { +int WiThrottle::getInt(byte * cmd) { int i=0; while (cmd[0]>='0' && cmd[0]<='9') { i=i*10 + (cmd[0]-'0'); @@ -105,15 +105,15 @@ int WiThrottle::getInt(char * cmd) { return i; } -int WiThrottle::getLocoId(char * cmd) { +int WiThrottle::getLocoId(byte * cmd) { if (cmd[0]=='*') return -1; // match all locos if (cmd[0]!='L' && cmd[0]!='S') return 0; // should not match any locos return getInt(cmd+1); } -void WiThrottle::multithrottle(Print & stream, char* cmd){ +void WiThrottle::multithrottle(Print & stream, byte * cmd){ char throttleChar=cmd[1]; int locoid=getLocoId(cmd+3); // -1 for * - char * aval=cmd; + byte * aval=cmd; while(*aval !=';' && *aval !='\0') aval++; if (*aval) aval++; @@ -164,7 +164,7 @@ void locoAdd(String th, String actionKey, int i) { } *********/ -void WiThrottle::locoAction(Print & stream, char* aval, char throttleChar, int cab){ +void WiThrottle::locoAction(Print & stream, byte* aval, char throttleChar, int cab){ // Note cab=-1 for all cabs in the consist called throttleChar. switch (aval[0]) { diff --git a/WiThrottle.h b/WiThrottle.h index 64ecf54..f681b1f 100644 --- a/WiThrottle.h +++ b/WiThrottle.h @@ -10,7 +10,7 @@ struct MYLOCO { class WiThrottle { public: static void loop(); - void parse(Print & stream, char * cmd); + void parse(Print & stream, byte * cmd); static WiThrottle* getThrottle(Print & stream, int wifiClient); private: @@ -20,8 +20,8 @@ class WiThrottle { static const int MAX_MY_LOCO=10; static const int HEARTBEAT_TIMEOUT=10; static WiThrottle* firstThrottle; - static int getInt(char * cmd); - static int getLocoId(char * cmd); + static int getInt(byte * cmd); + static int getLocoId(byte * cmd); WiThrottle* nextThrottle; int clientid; @@ -30,9 +30,9 @@ class WiThrottle { bool heartBeatEnable; unsigned long heartBeat; - void multithrottle(Print & stream, char* cmd); - void locoAction(Print & stream, char* aval, char throttleChar, int cab); - void accessory(Print & stream, char* cmd); + void multithrottle(Print & stream, byte * cmd); + void locoAction(Print & stream, byte* aval, char throttleChar, int cab); + void accessory(Print & stream, byte* cmd); void checkHeartbeat(); }; #endif diff --git a/WifiInterface.cpp b/WifiInterface.cpp index 713a0fc..3c0551a 100644 --- a/WifiInterface.cpp +++ b/WifiInterface.cpp @@ -53,9 +53,9 @@ bool WifiInterface::setup2(Stream & wifiStream, const __FlashStringHelper* SSid, return true; } -bool WifiInterface::checkForOK(Stream & wifiStream, const int timeout, const char * waitfor, bool echo) { - long int startTime = millis(); - char *locator=waitfor; +bool WifiInterface::checkForOK(Stream & wifiStream, const unsigned int timeout, const char * waitfor, bool echo) { + unsigned long startTime = millis(); + char const *locator=waitfor; DIAG(F("\nWifi setup Check: %S\n"),waitfor); while( millis()-startTime < timeout) { while(wifiStream.available()) { @@ -75,6 +75,26 @@ bool WifiInterface::checkForOK(Stream & wifiStream, const int timeout, const cha return false; } +bool WifiInterface::isHTML() { + + // 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(Stream & wifiStream) { if (!connected) return; @@ -129,7 +149,7 @@ void WifiInterface::loop(Stream & wifiStream) { // TODO ... tell JMRI parser that callbacks are diallowed because we dont want to handle the async bool closeAfter=false; // Intercept HTTP requests - if (strstr(buffer," HTTP/1.1")) { + if (isHTML()) { HTTPParser::parse(streamer,buffer); closeAfter=true; } diff --git a/WifiInterface.h b/WifiInterface.h index aae7010..9210a46 100644 --- a/WifiInterface.h +++ b/WifiInterface.h @@ -16,7 +16,8 @@ class WifiInterface { static DCCEXParser parser; static bool setup2(Stream & wifiStream, const __FlashStringHelper* SSSid, const __FlashStringHelper* password, int port); - static bool checkForOK(Stream & wifiStream, const int timeout, const char* waitfor, bool echo); + static bool checkForOK(Stream & wifiStream, const unsigned int timeout, const char* waitfor, bool echo); + static bool isHTML(); static bool connected; static byte loopstate; static int datalength;