2021-09-21 09:23:52 +02:00
|
|
|
/*
|
|
|
|
© 2021, Harald Barth.
|
|
|
|
|
|
|
|
This file is part of CommandStation-EX
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-01 09:09:30 +02:00
|
|
|
#include "defines.h"
|
|
|
|
#ifdef ESP_FAMILY
|
2021-09-25 23:18:10 +02:00
|
|
|
#include <ESP8266WiFi.h>
|
|
|
|
#include <ESPAsyncTCP.h>
|
|
|
|
#include <vector>
|
2021-10-01 11:32:09 +02:00
|
|
|
#include <string>
|
2021-09-25 23:18:10 +02:00
|
|
|
|
2021-09-21 09:23:52 +02:00
|
|
|
#include "WifiESP.h"
|
|
|
|
#include "DIAG.h"
|
2021-09-25 23:18:10 +02:00
|
|
|
#include "RingStream.h"
|
|
|
|
#include "CommandDistributor.h"
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static std::vector<AsyncClient*> clients; // a list to hold all clients
|
|
|
|
static AsyncServer *server;
|
|
|
|
|
|
|
|
static RingStream *outboundRing = new RingStream(2048);
|
|
|
|
|
|
|
|
static void handleError(void* arg, AsyncClient* client, int8_t error) {
|
|
|
|
DIAG(F("connection error %s from client %s"), client->errorToString(error), client->remoteIP().toString().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void handleData(void* arg, AsyncClient* client, void *data, size_t len) {
|
2021-09-28 21:08:41 +02:00
|
|
|
//DIAG(F("data received from client %s"), client->remoteIP().toString().c_str());
|
2021-09-25 23:18:10 +02:00
|
|
|
uint8_t clientId;
|
|
|
|
for (clientId=0; clientId<clients.size(); clientId++){
|
|
|
|
if (clients[clientId] == client) break;
|
|
|
|
}
|
|
|
|
if (clientId < clients.size()) {
|
|
|
|
byte cmd[len+1];
|
|
|
|
memcpy(cmd,data,len);
|
|
|
|
cmd[len]=0;
|
2021-09-26 08:37:59 +02:00
|
|
|
outboundRing->mark(clientId);
|
2021-09-25 23:18:10 +02:00
|
|
|
CommandDistributor::parse(clientId,cmd,outboundRing);
|
2021-09-26 08:37:59 +02:00
|
|
|
outboundRing->commit();
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
2021-09-26 08:37:59 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 22:55:14 +02:00
|
|
|
//static AsyncClient *debugclient = NULL;
|
|
|
|
|
|
|
|
bool sendData(AsyncClient *client, char* data, size_t count) {
|
2021-09-28 17:20:44 +02:00
|
|
|
size_t willsend = 0;
|
2021-09-26 08:37:59 +02:00
|
|
|
|
2021-09-25 23:18:10 +02:00
|
|
|
// reply to client
|
2021-09-28 17:20:44 +02:00
|
|
|
if (client->canSend()) {
|
|
|
|
while (count > 0) {
|
2021-09-30 22:55:14 +02:00
|
|
|
if (client->connected())
|
|
|
|
willsend = client->add(data, count); // add checks for space()
|
|
|
|
else
|
|
|
|
willsend = 0;
|
2021-09-28 17:20:44 +02:00
|
|
|
if (willsend < count) {
|
|
|
|
DIAG(F("Willsend %d of count %d"), willsend, count);
|
|
|
|
}
|
2021-09-30 22:55:14 +02:00
|
|
|
if (client->connected() && client->send()) {
|
2021-09-28 17:20:44 +02:00
|
|
|
count = count - willsend;
|
|
|
|
data = data + willsend;
|
|
|
|
} else {
|
|
|
|
DIAG(F("Could not send promised %d"), count);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Did send all bytes we wanted
|
2021-09-26 08:37:59 +02:00
|
|
|
return true;
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
2021-09-28 17:20:44 +02:00
|
|
|
DIAG(F("Aborting: Busy or space=0"));
|
2021-09-26 08:37:59 +02:00
|
|
|
return false;
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
2021-09-30 22:55:14 +02:00
|
|
|
static void deleteClient(AsyncClient* client) {
|
|
|
|
uint8_t clientId;
|
|
|
|
for (clientId=0; clientId<clients.size(); clientId++){
|
|
|
|
if (clients[clientId] == client) break;
|
|
|
|
}
|
|
|
|
if (clientId < clients.size()) {
|
|
|
|
clients[clientId] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 23:18:10 +02:00
|
|
|
static void handleDisconnect(void* arg, AsyncClient* client) {
|
2021-10-01 09:09:30 +02:00
|
|
|
DIAG(F("Client disconnected"));
|
2021-09-30 22:55:14 +02:00
|
|
|
deleteClient(client);
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void handleTimeOut(void* arg, AsyncClient* client, uint32_t time) {
|
|
|
|
DIAG(F("client ACK timeout ip: %s"), client->remoteIP().toString().c_str());
|
2021-09-30 22:55:14 +02:00
|
|
|
deleteClient(client);
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void handleNewClient(void* arg, AsyncClient* client) {
|
2021-10-01 09:09:30 +02:00
|
|
|
DIAG(F("New client %s"), client->remoteIP().toString().c_str());
|
2021-09-25 23:18:10 +02:00
|
|
|
|
|
|
|
// add to list
|
|
|
|
clients.push_back(client);
|
2021-10-01 09:09:30 +02:00
|
|
|
|
2021-09-25 23:18:10 +02:00
|
|
|
// register events
|
|
|
|
client->onData(&handleData, NULL);
|
|
|
|
client->onError(&handleError, NULL);
|
|
|
|
client->onDisconnect(&handleDisconnect, NULL);
|
|
|
|
client->onTimeout(&handleTimeOut, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-28 17:20:44 +02:00
|
|
|
/* Things one _might_ want to do:
|
|
|
|
Disable soft watchdog: ESP.wdtDisable()
|
2021-10-01 09:09:30 +02:00
|
|
|
Enable soft watchdog: ESP.wdtEnable(X) ignores the value of X and enables it for fixed
|
2021-09-28 17:20:44 +02:00
|
|
|
time at least in version 3.0.2 of the esp8266 package.
|
|
|
|
|
|
|
|
Internet says:
|
|
|
|
|
|
|
|
I manage to complety disable the hardware watchdog on ESP8266 in order to run the benchmark CoreMark.
|
|
|
|
|
|
|
|
void hw_wdt_disable(){
|
|
|
|
*((volatile uint32_t*) 0x60000900) &= ~(1); // Hardware WDT OFF
|
|
|
|
}
|
|
|
|
|
|
|
|
void hw_wdt_enable(){
|
|
|
|
*((volatile uint32_t*) 0x60000900) |= 1; // Hardware WDT ON
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2021-10-01 11:32:09 +02:00
|
|
|
bool WifiESP::setup(const char *SSid,
|
|
|
|
const char *password,
|
2021-09-25 23:18:10 +02:00
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
const byte channel) {
|
2021-10-01 11:32:09 +02:00
|
|
|
bool havePassword = true;
|
|
|
|
bool haveSSID = true;
|
|
|
|
bool wifiUp = false;
|
|
|
|
|
2021-10-01 09:09:30 +02:00
|
|
|
// We are server and should not sleep
|
2021-09-25 23:18:10 +02:00
|
|
|
wifi_set_sleep_type(NONE_SLEEP_T);
|
2021-10-01 09:09:30 +02:00
|
|
|
// connects to access point
|
2021-10-01 11:32:09 +02:00
|
|
|
|
|
|
|
const char *yourNetwork = "Your network ";
|
|
|
|
if (strncmp(yourNetwork, SSid, 13) == 0 || strncmp("", SSid, 13) == 0)
|
|
|
|
haveSSID = false;
|
|
|
|
if (strncmp(yourNetwork, password, 13) == 0 || strncmp("", password, 13) == 0)
|
|
|
|
havePassword = false;
|
|
|
|
|
|
|
|
if (haveSSID && havePassword) {
|
|
|
|
WiFi.mode(WIFI_STA);
|
|
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
WiFi.begin(SSid, password);
|
|
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
|
|
Serial.print('.');
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
|
|
|
|
wifiUp = true;
|
|
|
|
}
|
2021-09-25 23:18:10 +02:00
|
|
|
}
|
2021-10-01 11:32:09 +02:00
|
|
|
if (!haveSSID) {
|
|
|
|
// prepare all strings
|
|
|
|
String strSSID("DCC_");
|
|
|
|
String strPass("PASS_");
|
|
|
|
String strMac = WiFi.macAddress();
|
|
|
|
strMac.remove(0,9);
|
|
|
|
strMac.replace(":","");
|
|
|
|
strMac.replace(":","");
|
|
|
|
strSSID.concat(strMac);
|
|
|
|
strPass.concat(strMac);
|
|
|
|
|
|
|
|
WiFi.mode(WIFI_AP);
|
|
|
|
if (WiFi.softAP(strSSID.c_str(),
|
|
|
|
havePassword ? password : strPass.c_str(),
|
|
|
|
channel, false, 8)) {
|
|
|
|
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
|
|
|
|
DIAG(F("Wifi AP IP %s"),WiFi.softAPIP().toString().c_str());
|
|
|
|
wifiUp = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!wifiUp) {
|
|
|
|
DIAG(F("Wifi all fail"));
|
2021-10-01 09:09:30 +02:00
|
|
|
// no idea to go on
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-25 23:18:10 +02:00
|
|
|
|
|
|
|
server = new AsyncServer(port); // start listening on tcp port
|
|
|
|
|
|
|
|
server->onClient(&handleNewClient, server);
|
|
|
|
server->begin();
|
2021-10-01 09:09:30 +02:00
|
|
|
DIAG(F("Server up port %d"),port);
|
2021-09-21 09:23:52 +02:00
|
|
|
|
2021-09-25 23:18:10 +02:00
|
|
|
return true;
|
2021-09-21 09:23:52 +02:00
|
|
|
}
|
2021-09-26 08:37:59 +02:00
|
|
|
|
2021-09-21 09:23:52 +02:00
|
|
|
void WifiESP::loop() {
|
2021-09-30 22:55:14 +02:00
|
|
|
AsyncClient *client = NULL;
|
2021-09-26 08:37:59 +02:00
|
|
|
// Do something with outboundRing
|
|
|
|
// call sendData
|
2021-09-30 22:55:14 +02:00
|
|
|
int clientId=outboundRing->peek();
|
|
|
|
if (clientId >= 0) {
|
|
|
|
if (clientId > clients.size()) {
|
|
|
|
// something is wrong with the ringbuffer position
|
|
|
|
outboundRing->info();
|
|
|
|
client = NULL;
|
|
|
|
} else {
|
|
|
|
client = clients[clientId];
|
|
|
|
}
|
|
|
|
// if (client != debugclient) {
|
|
|
|
// DIAG(F("new client pointer = %x from id %d"), client, clientId);
|
|
|
|
// debugclient = client;
|
|
|
|
// }
|
|
|
|
} else {
|
|
|
|
client = NULL;
|
|
|
|
}
|
|
|
|
if (clientId>=0 && client && client->connected() && client->canSend()) {
|
|
|
|
outboundRing->read();
|
2021-09-26 10:59:07 +02:00
|
|
|
int count=outboundRing->count();
|
2021-09-28 21:08:41 +02:00
|
|
|
//DIAG(F("Wifi reply client=%d, count=%d"), clientId,count);
|
2021-09-26 10:59:07 +02:00
|
|
|
{
|
2021-09-28 17:20:44 +02:00
|
|
|
char buffer[count+1];
|
2021-09-28 21:08:41 +02:00
|
|
|
for(int i=0;i<count;i++) {
|
2021-09-28 17:31:12 +02:00
|
|
|
int c = outboundRing->read();
|
|
|
|
if (c >= 0)
|
|
|
|
buffer[i] = (char)c;
|
|
|
|
else {
|
|
|
|
DIAG(F("Ringread fail at %d"),i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-28 17:20:44 +02:00
|
|
|
buffer[count]=0;
|
2021-09-28 21:08:41 +02:00
|
|
|
//DIAG(F("SEND:%s COUNT:%d"),buffer,count);
|
2021-09-30 22:55:14 +02:00
|
|
|
uint8_t tries = 3;
|
|
|
|
while (! sendData(client, buffer, count)) {
|
2021-09-28 21:08:41 +02:00
|
|
|
DIAG(F("senData fail"));
|
|
|
|
yield();
|
2021-09-30 22:55:14 +02:00
|
|
|
if (tries == 0) break;
|
2021-09-28 21:08:41 +02:00
|
|
|
}
|
2021-09-26 10:59:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 09:09:30 +02:00
|
|
|
#ifdef ESP_DEBUG
|
2021-09-25 23:18:10 +02:00
|
|
|
static unsigned long last = 0;
|
|
|
|
if (millis() - last > 60000) {
|
|
|
|
last = millis();
|
|
|
|
DIAG(F("+"));
|
|
|
|
}
|
2021-10-01 09:09:30 +02:00
|
|
|
#endif
|
2021-09-25 23:18:10 +02:00
|
|
|
ESP.wdtFeed();
|
2021-09-21 09:23:52 +02:00
|
|
|
}
|
2021-10-01 09:09:30 +02:00
|
|
|
#endif //ESP_FAMILY
|