2020-07-03 18:35:02 +02:00
|
|
|
/*
|
|
|
|
* © 2020, Chris Harlow. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of Asbelos DCC API
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-06-12 15:28:35 +02:00
|
|
|
#include "WifiInterface.h"
|
|
|
|
#include "Config.h"
|
|
|
|
#include "DIAG.h"
|
2020-06-12 20:56:40 +02:00
|
|
|
#include "StringFormatter.h"
|
2020-06-27 16:36:32 +02:00
|
|
|
#include "WiThrottle.h"
|
2020-06-28 13:45:31 +02:00
|
|
|
#include "HTTPParser.h"
|
2020-06-13 16:53:46 +02:00
|
|
|
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[] =">";
|
2020-06-28 13:45:31 +02:00
|
|
|
const char PROGMEM SEND_OK_SEARCH[] ="\r\nSEND OK\r\n";
|
2020-07-16 14:19:29 +02:00
|
|
|
const unsigned long LOOP_TIMEOUT=2000;
|
2020-06-12 15:28:35 +02:00
|
|
|
bool WifiInterface::connected=false;
|
2020-07-16 12:52:06 +02:00
|
|
|
bool WifiInterface::closeAfter=false;
|
2020-06-13 10:36:10 +02:00
|
|
|
DCCEXParser WifiInterface::parser;
|
|
|
|
byte WifiInterface::loopstate=0;
|
2020-07-16 14:19:29 +02:00
|
|
|
unsigned long WifiInterface::loopTimeoutStart=0;
|
2020-06-13 10:36:10 +02:00
|
|
|
int WifiInterface::datalength=0;
|
|
|
|
int WifiInterface::connectionId;
|
|
|
|
byte WifiInterface::buffer[MAX_WIFI_BUFFER];
|
|
|
|
MemStream WifiInterface::streamer(buffer,sizeof(buffer));
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-07-16 12:52:06 +02:00
|
|
|
void WifiInterface::setup(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
|
|
|
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
|
2020-06-28 11:21:27 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
DIAG(F("\n++++++ Wifi Setup In Progress ++++++++\n"));
|
2020-07-16 12:52:06 +02:00
|
|
|
connected=setup2(wifiStream, SSid, password,hostname, servername,port);
|
2020-06-13 10:36:10 +02:00
|
|
|
// TODO calloc the buffer and streamer and parser etc
|
2020-06-13 16:53:46 +02:00
|
|
|
DIAG(F("\n++++++ Wifi Setup %S ++++++++\n"), connected?F("OK"):F("FAILED"));
|
2020-06-12 20:56:40 +02:00
|
|
|
}
|
|
|
|
|
2020-07-16 12:52:06 +02:00
|
|
|
bool WifiInterface::setup2(Stream & wifiStream, const __FlashStringHelper* SSid, const __FlashStringHelper* password,
|
|
|
|
const __FlashStringHelper* hostname, const __FlashStringHelper* servername, int port) {
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
delay(1000);
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-28 11:21:27 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+RST\r\n")); // reset module
|
2020-07-16 14:19:29 +02:00
|
|
|
//checkForOK(wifiStream,5000,END_DETAIL_SEARCH,true); // Show startup but ignore unreadable upto ready
|
|
|
|
checkForOK(wifiStream,5000,READY_SEARCH,true);
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-07-22 16:14:07 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CWMODE=3\r\n")); // configure as server or access point
|
2020-07-19 19:31:51 +02:00
|
|
|
checkForOK(wifiStream,1000,OK_SEARCH,true); // Not always OK, sometimes "no change"
|
2020-07-16 12:52:06 +02:00
|
|
|
|
2020-07-22 16:14:07 +02:00
|
|
|
StringFormatter::send(wifiStream, F("AT+CWHOSTNAME=\"%S\"\r\n"), hostname); // Set Host name for Wifi Client
|
|
|
|
checkForOK(wifiStream,2000, OK_SEARCH, true);
|
2020-07-16 12:52:06 +02:00
|
|
|
|
2020-07-22 16:14:07 +02:00
|
|
|
|
|
|
|
// Older ES versions have AT+CWJAP, newer ones have AT+CWJAP_CUR and AT+CWHOSTNAME
|
2020-06-28 11:21:27 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CWJAP=\"%S\",\"%S\"\r\n"),SSid,password);
|
2020-07-22 16:14:07 +02:00
|
|
|
if (!checkForOK(wifiStream,20000,OK_SEARCH,true)) {
|
|
|
|
StringFormatter::send(wifiStream,F("AT+CWJAP_CUR=\"%S\",\"%S\"\r\n"),SSid,password);
|
|
|
|
if (!checkForOK(wifiStream,20000,OK_SEARCH,true)) return false;
|
|
|
|
}
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-28 11:21:27 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CIFSR\r\n")); // get ip address //192.168.4.1
|
|
|
|
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
2020-07-16 12:52:06 +02:00
|
|
|
|
2020-06-12 20:56:40 +02:00
|
|
|
|
2020-06-28 11:21:27 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CIPMUX=1\r\n")); // configure for multiple connections
|
|
|
|
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
2020-06-12 20:56:40 +02:00
|
|
|
|
2020-06-28 11:21:27 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CIPSERVER=1,%d\r\n"),port); // turn on server on port 80
|
|
|
|
if (!checkForOK(wifiStream,10000,OK_SEARCH,true)) return false;
|
2020-06-12 20:56:40 +02:00
|
|
|
|
2020-07-16 14:19:29 +02:00
|
|
|
// 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;
|
2020-07-16 17:10:27 +02:00
|
|
|
(void)servername; // not currently in use
|
2020-06-12 20:56:40 +02:00
|
|
|
return true;
|
2020-06-12 15:28:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 12:37:05 +02:00
|
|
|
bool WifiInterface::checkForOK(Stream & wifiStream, const unsigned int timeout, const char * waitfor, bool echo) {
|
|
|
|
unsigned long startTime = millis();
|
|
|
|
char const *locator=waitfor;
|
2020-07-16 12:52:06 +02:00
|
|
|
DIAG(F("\nWifi Check: [%E]"),waitfor);
|
2020-06-28 11:21:27 +02:00
|
|
|
while( millis()-startTime < timeout) {
|
|
|
|
while(wifiStream.available()) {
|
|
|
|
int ch=wifiStream.read();
|
2020-07-03 18:35:02 +02:00
|
|
|
if (echo) StringFormatter::printEscape(Serial,ch); /// THIS IS A DIAG IN DISGUISE
|
2020-06-13 16:53:46 +02:00
|
|
|
if (ch!=pgm_read_byte_near(locator)) locator=waitfor;
|
|
|
|
if (ch==pgm_read_byte_near(locator)) {
|
2020-06-12 20:56:40 +02:00
|
|
|
locator++;
|
2020-06-13 16:53:46 +02:00
|
|
|
if (!pgm_read_byte_near(locator)) {
|
2020-07-16 12:52:06 +02:00
|
|
|
DIAG(F("\nFound in %dms"),millis()-startTime);
|
2020-06-12 20:56:40 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DIAG(F("\nTIMEOUT after %dms\n"),timeout);
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-12 15:28:35 +02:00
|
|
|
|
2020-06-29 12:37:05 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-06-13 10:36:10 +02:00
|
|
|
|
2020-06-28 11:21:27 +02:00
|
|
|
void WifiInterface::loop(Stream & wifiStream) {
|
2020-06-12 20:56:40 +02:00
|
|
|
if (!connected) return;
|
2020-06-27 16:36:32 +02:00
|
|
|
|
|
|
|
WiThrottle::loop(); // check heartbeats
|
|
|
|
|
2020-06-13 10:36:10 +02:00
|
|
|
// read anything into a buffer, collecting info on the way
|
2020-06-28 11:21:27 +02:00
|
|
|
while (loopstate!=99 && wifiStream.available()) {
|
|
|
|
int ch=wifiStream.read();
|
2020-07-16 14:19:29 +02:00
|
|
|
|
|
|
|
// echo the char to the diagnostic stream in escaped format
|
|
|
|
StringFormatter::printEscape(Serial,ch); // DIAG in disguise
|
|
|
|
|
2020-06-13 10:36:10 +02:00
|
|
|
switch (loopstate) {
|
2020-07-16 12:52:06 +02:00
|
|
|
case 0: // looking for +IPD
|
2020-06-13 10:36:10 +02:00
|
|
|
connectionId=0;
|
|
|
|
if (ch=='+') loopstate=1;
|
|
|
|
break;
|
2020-07-16 12:52:06 +02:00
|
|
|
case 1: // Looking for I in +IPD
|
2020-06-13 10:36:10 +02:00
|
|
|
loopstate= (ch=='I')?2:0;
|
|
|
|
break;
|
2020-07-16 12:52:06 +02:00
|
|
|
case 2: // Looking for P in +IPD
|
2020-06-13 10:36:10 +02:00
|
|
|
loopstate= (ch=='P')?3:0;
|
|
|
|
break;
|
2020-07-16 12:52:06 +02:00
|
|
|
case 3: // Looking for D in +IPD
|
2020-06-13 10:36:10 +02:00
|
|
|
loopstate= (ch=='D')?4:0;
|
|
|
|
break;
|
2020-07-16 12:52:06 +02:00
|
|
|
case 4: // Looking for , After +IPD
|
2020-06-13 10:36:10 +02:00
|
|
|
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');
|
2020-07-16 12:52:06 +02:00
|
|
|
streamer.flush(); // basically sets write point at start of buffer
|
2020-06-13 10:36:10 +02:00
|
|
|
break;
|
|
|
|
case 7: // reading data
|
2020-06-13 12:17:06 +02:00
|
|
|
streamer.write(ch);
|
2020-06-13 10:36:10 +02:00
|
|
|
datalength--;
|
|
|
|
if (datalength==0) loopstate=99;
|
|
|
|
break;
|
2020-07-16 12:52:06 +02:00
|
|
|
|
|
|
|
case 10: // Waiting for > so we can send reply
|
2020-07-16 14:19:29 +02:00
|
|
|
if (millis()-loopTimeoutStart > LOOP_TIMEOUT) {
|
|
|
|
DIAG(F("\nWifi TIMEOUT on wait for > prompt or ERROR\n"));
|
|
|
|
loopstate=0; // go back to +IPD
|
|
|
|
break;
|
|
|
|
}
|
2020-07-16 12:52:06 +02:00
|
|
|
if (ch=='>'){
|
|
|
|
DIAG(F("\n> [%e]\n"),buffer);
|
|
|
|
wifiStream.print((char *) buffer);
|
2020-07-16 14:19:29 +02:00
|
|
|
loopTimeoutStart=millis();
|
2020-07-16 12:52:06 +02:00
|
|
|
loopstate=closeAfter?11:0;
|
|
|
|
}
|
|
|
|
break;
|
2020-07-16 14:19:29 +02:00
|
|
|
case 11: // Waiting for SEND OK or ERROR to complete so we can closeAfter
|
|
|
|
if (millis()-loopTimeoutStart > LOOP_TIMEOUT) {
|
|
|
|
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
|
2020-07-16 12:52:06 +02:00
|
|
|
StringFormatter::send(wifiStream,F("AT+CIPCLOSE=%d\r\n"),connectionId);
|
|
|
|
loopstate=0; // wait for +IPD
|
|
|
|
}
|
|
|
|
break;
|
2020-06-13 10:36:10 +02:00
|
|
|
} // switch
|
|
|
|
} // while
|
|
|
|
if (loopstate!=99) return;
|
2020-07-16 14:19:29 +02:00
|
|
|
|
|
|
|
// AT this point we have read an incoming message into the buffer
|
2020-07-20 10:55:04 +02:00
|
|
|
streamer.print('\0'); // null the end of the buffer so we can treat it as a string
|
2020-06-13 12:17:06 +02:00
|
|
|
|
2020-07-03 18:35:02 +02:00
|
|
|
DIAG(F("\nWifiRead:%d:%e\n"),connectionId,buffer);
|
2020-06-13 12:17:06 +02:00
|
|
|
streamer.setBufferContentPosition(0,0); // reset write position to start of buffer
|
2020-06-13 10:36:10 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-07-16 12:52:06 +02:00
|
|
|
closeAfter=false;
|
2020-06-28 13:45:31 +02:00
|
|
|
// Intercept HTTP requests
|
2020-06-29 12:37:05 +02:00
|
|
|
if (isHTML()) {
|
2020-06-28 13:45:31 +02:00
|
|
|
HTTPParser::parse(streamer,buffer);
|
|
|
|
closeAfter=true;
|
|
|
|
}
|
2020-07-20 10:55:04 +02:00
|
|
|
else if (buffer[0]=='<') parser.parse(streamer,buffer, true); // tell JMRI parser that ACKS are blocking because we can't handle the async
|
2020-07-02 00:02:45 +02:00
|
|
|
|
2020-07-05 22:00:27 +02:00
|
|
|
else WiThrottle::getThrottle(connectionId)->parse(streamer, buffer);
|
2020-06-27 16:36:32 +02:00
|
|
|
|
2020-07-16 12:52:06 +02:00
|
|
|
if (streamer.available()==0) {
|
|
|
|
// No reply
|
|
|
|
if (closeAfter) StringFormatter::send(wifiStream,F("AT+CIPCLOSE=%d\r\n"),connectionId);
|
|
|
|
loopstate=0; // go back to waiting for +IPD
|
|
|
|
return;
|
2020-06-28 13:45:31 +02:00
|
|
|
}
|
2020-07-16 12:52:06 +02:00
|
|
|
// prepare to send reply
|
2020-07-20 10:55:04 +02:00
|
|
|
streamer.print('\0'); // null the end of the buffer so we can treat it as a string
|
2020-07-16 12:52:06 +02:00
|
|
|
DIAG(F("\nWiFiInterface reply c(%d) l(%d) [%e]\n"),connectionId,streamer.available()-1,buffer);
|
|
|
|
StringFormatter::send(wifiStream,F("AT+CIPSEND=%d,%d\r\n"),connectionId,streamer.available()-1);
|
2020-07-16 14:19:29 +02:00
|
|
|
loopTimeoutStart=millis();
|
2020-07-16 12:52:06 +02:00
|
|
|
loopstate=10; // non-blocking loop waits for > before sending
|
2020-06-13 10:36:10 +02:00
|
|
|
}
|