2021-10-05 10:39:08 +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/>.
|
|
|
|
*/
|
|
|
|
|
2022-07-31 00:23:19 +02:00
|
|
|
#if defined(ARDUINO_ARCH_ESP32)
|
2021-11-14 13:10:16 +01:00
|
|
|
#include <vector>
|
2021-10-05 10:39:08 +02:00
|
|
|
#include "defines.h"
|
|
|
|
#include <WiFi.h>
|
2022-07-31 08:47:58 +02:00
|
|
|
#include "esp_wifi.h"
|
2021-10-05 10:39:08 +02:00
|
|
|
#include "WifiESP32.h"
|
|
|
|
#include "DIAG.h"
|
|
|
|
#include "RingStream.h"
|
|
|
|
#include "CommandDistributor.h"
|
2022-08-06 00:34:35 +02:00
|
|
|
#include "WiThrottle.h"
|
2021-10-24 12:09:54 +02:00
|
|
|
/*
|
|
|
|
#include "soc/rtc_wdt.h"
|
|
|
|
#include "esp_task_wdt.h"
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "soc/timer_group_struct.h"
|
|
|
|
#include "soc/timer_group_reg.h"
|
|
|
|
void feedTheDog0(){
|
|
|
|
// feed dog 0
|
|
|
|
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
|
|
|
|
TIMERG0.wdt_feed=1; // feed dog
|
|
|
|
TIMERG0.wdt_wprotect=0; // write protect
|
|
|
|
// feed dog 1
|
|
|
|
//TIMERG1.wdt_wprotect=TIMG_WDT_WKEY_VALUE; // write enable
|
|
|
|
//TIMERG1.wdt_feed=1; // feed dog
|
|
|
|
//TIMERG1.wdt_wprotect=0; // write protect
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void enableCoreWDT(byte core){
|
|
|
|
TaskHandle_t idle = xTaskGetIdleTaskHandleForCPU(core);
|
|
|
|
if(idle == NULL){
|
|
|
|
DIAG(F("Get idle rask on core %d failed"),core);
|
|
|
|
} else {
|
|
|
|
if(esp_task_wdt_add(idle) != ESP_OK){
|
|
|
|
DIAG(F("Failed to add Core %d IDLE task to WDT"),core);
|
|
|
|
} else {
|
|
|
|
DIAG(F("Added Core %d IDLE task to WDT"),core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void disableCoreWDT(byte core){
|
|
|
|
TaskHandle_t idle = xTaskGetIdleTaskHandleForCPU(core);
|
|
|
|
if(idle == NULL || esp_task_wdt_delete(idle) != ESP_OK){
|
|
|
|
DIAG(F("Failed to remove Core %d IDLE task from WDT"),core);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2021-10-05 10:39:08 +02:00
|
|
|
|
2022-08-07 01:24:41 +02:00
|
|
|
class NetworkClient {
|
|
|
|
public:
|
|
|
|
NetworkClient(WiFiClient c) {
|
|
|
|
wifi = c;
|
|
|
|
};
|
|
|
|
bool ok() {
|
|
|
|
return (inUse && wifi.connected());
|
|
|
|
};
|
|
|
|
bool recycle(WiFiClient c) {
|
2022-08-07 20:11:31 +02:00
|
|
|
|
2022-08-07 01:24:41 +02:00
|
|
|
if (inUse == true) return false;
|
2022-08-07 20:11:31 +02:00
|
|
|
|
|
|
|
// return false here until we have
|
|
|
|
// implemented a LRU timer
|
|
|
|
// if (LRU too recent) return false;
|
|
|
|
return false;
|
|
|
|
|
2022-08-07 01:24:41 +02:00
|
|
|
wifi = c;
|
|
|
|
inUse = true;
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
WiFiClient wifi;
|
|
|
|
bool inUse = true;
|
|
|
|
};
|
|
|
|
|
|
|
|
static std::vector<NetworkClient> clients; // a list to hold all clients
|
2021-10-05 21:53:02 +02:00
|
|
|
static WiFiServer *server = NULL;
|
2022-08-16 12:43:38 +02:00
|
|
|
static RingStream *outboundRing = new RingStream(10240);
|
2021-10-31 00:10:58 +02:00
|
|
|
static bool APmode = false;
|
2021-10-05 21:53:02 +02:00
|
|
|
|
2022-08-16 12:25:49 +02:00
|
|
|
#ifdef WIFI_TASK_ON_CORE0
|
2021-11-06 02:40:49 +01:00
|
|
|
void wifiLoop(void *){
|
|
|
|
for(;;){
|
|
|
|
WifiESP::loop();
|
|
|
|
}
|
|
|
|
}
|
2022-08-16 12:25:49 +02:00
|
|
|
#endif
|
2021-11-06 02:40:49 +01:00
|
|
|
|
2021-10-05 10:39:08 +02:00
|
|
|
bool WifiESP::setup(const char *SSid,
|
|
|
|
const char *password,
|
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
const byte channel) {
|
2021-10-05 21:53:02 +02:00
|
|
|
bool havePassword = true;
|
|
|
|
bool haveSSID = true;
|
|
|
|
bool wifiUp = false;
|
2021-11-02 17:50:32 +01:00
|
|
|
uint8_t tries = 40;
|
2021-10-05 21:53:02 +02:00
|
|
|
|
2022-08-28 22:54:26 +02:00
|
|
|
//#ifdef SERIAL_BT_COMMANDS
|
|
|
|
//return false;
|
|
|
|
//#endif
|
|
|
|
|
2021-10-24 12:09:54 +02:00
|
|
|
// tests
|
|
|
|
// enableCoreWDT(1);
|
|
|
|
// disableCoreWDT(0);
|
|
|
|
|
2022-07-31 13:35:25 +02:00
|
|
|
// clean start
|
2022-08-07 20:11:31 +02:00
|
|
|
WiFi.mode(WIFI_STA);
|
2022-07-31 13:35:25 +02:00
|
|
|
WiFi.disconnect(true);
|
2022-08-28 19:44:07 +02:00
|
|
|
// differnet settings that did not improve for haba
|
|
|
|
// WiFi.useStaticBuffers(true);
|
|
|
|
// WiFi.setScanMethod(WIFI_ALL_CHANNEL_SCAN);
|
|
|
|
// WiFi.setSortMethod(WIFI_CONNECT_AP_BY_SECURITY);
|
2022-07-31 13:35:25 +02:00
|
|
|
|
2021-10-05 21:53:02 +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);
|
2022-08-28 22:54:26 +02:00
|
|
|
#ifdef SERIAL_BT_COMMANDS
|
|
|
|
WiFi.setSleep(true);
|
|
|
|
#else
|
2022-07-31 09:20:34 +02:00
|
|
|
WiFi.setSleep(false);
|
2022-08-28 22:54:26 +02:00
|
|
|
#endif
|
2021-10-05 21:53:02 +02:00
|
|
|
WiFi.setAutoReconnect(true);
|
|
|
|
WiFi.begin(SSid, password);
|
2021-11-02 17:50:32 +01:00
|
|
|
while (WiFi.status() != WL_CONNECTED && tries) {
|
2021-10-05 21:53:02 +02:00
|
|
|
Serial.print('.');
|
2021-11-02 17:50:32 +01:00
|
|
|
tries--;
|
2021-10-05 21:53:02 +02:00
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
DIAG(F("Wifi STA IP %s"),WiFi.localIP().toString().c_str());
|
|
|
|
wifiUp = true;
|
2021-10-31 00:40:35 +02:00
|
|
|
} else {
|
|
|
|
DIAG(F("Could not connect to Wifi SSID %s"),SSid);
|
2022-07-31 08:47:58 +02:00
|
|
|
DIAG(F("Forcing one more Wifi restart"));
|
|
|
|
esp_wifi_start();
|
|
|
|
esp_wifi_connect();
|
|
|
|
tries=40;
|
|
|
|
while (WiFi.status() != WL_CONNECTED && tries) {
|
|
|
|
Serial.print('.');
|
|
|
|
tries--;
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
|
|
|
DIAG(F("Wifi STA IP 2nd try %s"),WiFi.localIP().toString().c_str());
|
|
|
|
wifiUp = true;
|
|
|
|
} else {
|
2022-08-28 19:44:07 +02:00
|
|
|
DIAG(F("Wifi STA mode FAIL. Will revert to AP mode"));
|
|
|
|
haveSSID=false;
|
2022-07-31 08:47:58 +02:00
|
|
|
}
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!haveSSID) {
|
|
|
|
// prepare all strings
|
2023-06-23 01:47:20 +02:00
|
|
|
String strSSID("DCCEX_");
|
2021-10-05 21:53:02 +02:00
|
|
|
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);
|
2022-08-28 22:54:26 +02:00
|
|
|
#ifdef SERIAL_BT_COMMANDS
|
|
|
|
WiFi.setSleep(true);
|
|
|
|
#else
|
2022-08-05 16:14:51 +02:00
|
|
|
WiFi.setSleep(false);
|
2022-08-28 22:54:26 +02:00
|
|
|
#endif
|
2021-10-05 21:53:02 +02:00
|
|
|
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;
|
2021-10-31 00:10:58 +02:00
|
|
|
APmode = true;
|
2021-10-31 00:40:35 +02:00
|
|
|
} else {
|
|
|
|
DIAG(F("Could not set up AP with Wifi SSID %s"),strSSID.c_str());
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!wifiUp) {
|
2021-10-31 00:40:35 +02:00
|
|
|
DIAG(F("Wifi setup all fail (STA and AP mode)"));
|
2021-10-05 21:53:02 +02:00
|
|
|
// no idea to go on
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
server = new WiFiServer(port); // start listening on tcp port
|
|
|
|
server->begin();
|
2021-11-06 02:59:57 +01:00
|
|
|
// server started here
|
|
|
|
|
2022-08-16 12:25:49 +02:00
|
|
|
#ifdef WIFI_TASK_ON_CORE0
|
2021-11-06 02:59:57 +01:00
|
|
|
//start loop task
|
|
|
|
if (pdPASS != xTaskCreatePinnedToCore(
|
|
|
|
wifiLoop, /* Task function. */
|
|
|
|
"wifiLoop",/* name of task. */
|
|
|
|
10000, /* Stack size of task */
|
|
|
|
NULL, /* parameter of the task */
|
|
|
|
1, /* priority of the task */
|
|
|
|
NULL, /* Task handle to keep track of created task */
|
|
|
|
0)) { /* pin task to core 0 */
|
|
|
|
DIAG(F("Could not create wifiLoop task"));
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-05 21:53:02 +02:00
|
|
|
|
2021-11-06 02:59:57 +01:00
|
|
|
// report server started after wifiLoop creation
|
|
|
|
// when everything looks good
|
2022-08-16 12:25:49 +02:00
|
|
|
DIAG(F("Server starting (core 0) port %d"),port);
|
|
|
|
#else
|
|
|
|
DIAG(F("Server will be started on port %d"),port);
|
|
|
|
#endif
|
2021-10-05 21:53:02 +02:00
|
|
|
return true;
|
2021-10-05 10:39:08 +02:00
|
|
|
}
|
|
|
|
|
2022-08-28 19:44:07 +02:00
|
|
|
const char *wlerror[] = {
|
|
|
|
"WL_IDLE_STATUS",
|
|
|
|
"WL_NO_SSID_AVAIL",
|
|
|
|
"WL_SCAN_COMPLETED",
|
|
|
|
"WL_CONNECTED",
|
|
|
|
"WL_CONNECT_FAILED",
|
|
|
|
"WL_CONNECTION_LOST",
|
|
|
|
"WL_DISCONNECTED"
|
|
|
|
};
|
|
|
|
|
2021-10-05 10:39:08 +02:00
|
|
|
void WifiESP::loop() {
|
2021-10-05 21:53:02 +02:00
|
|
|
int clientId; //tmp loop var
|
|
|
|
|
2021-10-31 00:10:58 +02:00
|
|
|
// really no good way to check for LISTEN especially in AP mode?
|
2022-08-28 19:44:07 +02:00
|
|
|
wl_status_t wlStatus;
|
|
|
|
if (APmode || (wlStatus = WiFi.status()) == WL_CONNECTED) {
|
2022-08-05 19:54:01 +02:00
|
|
|
// loop over all clients and remove inactive
|
|
|
|
for (clientId=0; clientId<clients.size(); clientId++){
|
|
|
|
// check if client is there and alive
|
2022-08-07 01:24:41 +02:00
|
|
|
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
|
|
|
|
DIAG(F("Remove client %d"), clientId);
|
2022-08-05 19:54:01 +02:00
|
|
|
CommandDistributor::forget(clientId);
|
2022-08-07 01:24:41 +02:00
|
|
|
clients[clientId].wifi.stop();
|
|
|
|
clients[clientId].inUse = false;
|
|
|
|
//Do NOT clients.erase(clients.begin()+clientId) as
|
|
|
|
//that would mix up clientIds for later.
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
2022-08-05 19:54:01 +02:00
|
|
|
}
|
|
|
|
if (server->hasClient()) {
|
2021-10-05 21:53:02 +02:00
|
|
|
WiFiClient client;
|
|
|
|
while (client = server->available()) {
|
2022-08-07 01:24:41 +02:00
|
|
|
for (clientId=0; clientId<clients.size(); clientId++){
|
|
|
|
if (clients[clientId].recycle(client)) {
|
|
|
|
DIAG(F("Recycle client %d %s"), clientId, client.remoteIP().toString().c_str());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (clientId>=clients.size()) {
|
|
|
|
NetworkClient nc(client);
|
|
|
|
clients.push_back(nc);
|
|
|
|
DIAG(F("New client %d, %s"), clientId, client.remoteIP().toString().c_str());
|
|
|
|
}
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// loop over all connected clients
|
|
|
|
for (clientId=0; clientId<clients.size(); clientId++){
|
2022-08-07 01:24:41 +02:00
|
|
|
if(clients[clientId].ok()) {
|
2021-10-05 21:53:02 +02:00
|
|
|
int len;
|
2022-08-07 01:24:41 +02:00
|
|
|
if ((len = clients[clientId].wifi.available()) > 0) {
|
2021-10-05 21:53:02 +02:00
|
|
|
// read data from client
|
|
|
|
byte cmd[len+1];
|
|
|
|
for(int i=0; i<len; i++) {
|
2022-08-07 01:24:41 +02:00
|
|
|
cmd[i]=clients[clientId].wifi.read();
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
|
|
|
cmd[len]=0;
|
|
|
|
CommandDistributor::parse(clientId,cmd,outboundRing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // all clients
|
2021-10-05 10:39:08 +02:00
|
|
|
|
2022-08-06 00:34:35 +02:00
|
|
|
WiThrottle::loop(outboundRing);
|
|
|
|
|
2021-10-05 21:53:02 +02:00
|
|
|
// something to write out?
|
2022-08-08 13:00:53 +02:00
|
|
|
clientId=outboundRing->read();
|
2021-10-05 21:53:02 +02:00
|
|
|
if (clientId >= 0) {
|
2022-08-09 15:25:29 +02:00
|
|
|
// We have data to send in outboundRing
|
|
|
|
// and we have a valid clientId.
|
|
|
|
// First read it out to buffer
|
|
|
|
// and then look if it can be sent because
|
|
|
|
// we can not leave it in the ring for ever
|
|
|
|
int count=outboundRing->count();
|
|
|
|
{
|
|
|
|
char buffer[count+1]; // one extra for '\0'
|
|
|
|
for(int i=0;i<count;i++) {
|
|
|
|
int c = outboundRing->read();
|
|
|
|
if (c >= 0) // Panic check, should never be false
|
|
|
|
buffer[i] = (char)c;
|
|
|
|
else {
|
|
|
|
DIAG(F("Ringread fail at %d"),i);
|
|
|
|
break;
|
2022-08-06 19:07:35 +02:00
|
|
|
}
|
|
|
|
}
|
2022-08-09 15:25:29 +02:00
|
|
|
// buffer filled, end with '\0' so we can use it as C string
|
|
|
|
buffer[count]='\0';
|
|
|
|
if((unsigned int)clientId <= clients.size() && clients[clientId].ok()) {
|
2022-08-20 11:45:29 +02:00
|
|
|
if (Diag::CMD || Diag::WITHROTTLE)
|
|
|
|
DIAG(F("SEND %d:%s"), clientId, buffer);
|
2022-08-09 15:25:29 +02:00
|
|
|
clients[clientId].wifi.write(buffer,count);
|
|
|
|
} else {
|
|
|
|
DIAG(F("Unsent(%d): %s"), clientId, buffer);
|
2021-10-05 21:53:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-28 19:44:07 +02:00
|
|
|
} else if (!APmode) { // in STA mode but not connected any more
|
|
|
|
// kick it again
|
2022-08-28 22:54:26 +02:00
|
|
|
if (wlStatus <= 6) {
|
|
|
|
DIAG(F("Wifi aborted with error %s. Kicking Wifi!"), wlerror[wlStatus]);
|
|
|
|
esp_wifi_start();
|
|
|
|
esp_wifi_connect();
|
|
|
|
uint8_t tries=40;
|
|
|
|
while (WiFi.status() != WL_CONNECTED && tries) {
|
|
|
|
Serial.print('.');
|
|
|
|
tries--;
|
|
|
|
delay(500);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// all well, probably
|
|
|
|
//DIAG(F("Running BT"));
|
2022-08-28 19:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 13:10:16 +01:00
|
|
|
|
2021-10-24 12:09:54 +02:00
|
|
|
// when loop() is running on core0 we must
|
|
|
|
// feed the core0 wdt ourselves as yield()
|
|
|
|
// is not necessarily yielding to a low
|
|
|
|
// prio task. On core1 this is not a problem
|
|
|
|
// as there the wdt is disabled by the
|
|
|
|
// arduio IDE startup routines.
|
|
|
|
if (xPortGetCoreID() == 0)
|
|
|
|
feedTheDog0();
|
2021-11-14 13:10:16 +01:00
|
|
|
yield();
|
2021-10-05 10:39:08 +02:00
|
|
|
}
|
|
|
|
#endif //ESP32
|