mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-24 08:36:14 +01:00
Merge e6797d1095
into d46a6f092a
This commit is contained in:
commit
1d0b1b454c
134
Wifi_NINA.cpp
134
Wifi_NINA.cpp
|
@ -21,7 +21,7 @@
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
|
|
||||||
#ifdef WIFI_NINA
|
#ifdef WIFI_NINA
|
||||||
#include <vector>
|
//#include <vector>
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#ifndef ARDUINO_GIGA
|
#ifndef ARDUINO_GIGA
|
||||||
#include <WifiNINA.h>
|
#include <WifiNINA.h>
|
||||||
|
@ -49,13 +49,13 @@
|
||||||
#else
|
#else
|
||||||
#warning "WiFiNINA has no SPI port or pin allocations for this archiecture yet!"
|
#warning "WiFiNINA has no SPI port or pin allocations for this archiecture yet!"
|
||||||
#endif
|
#endif
|
||||||
|
#define MAX_CLIENTS 10
|
||||||
class NetworkClient {
|
/*class NetworkClient {
|
||||||
public:
|
public:
|
||||||
NetworkClient(WiFiClient c) {
|
NetworkClient(WiFiClient c) {
|
||||||
wifi = c;
|
wifi = c;
|
||||||
};
|
};
|
||||||
bool ok() {
|
bool ok() {
|
||||||
return (inUse && wifi.connected());
|
return (inUse && wifi.connected());
|
||||||
};
|
};
|
||||||
bool recycle(WiFiClient c) {
|
bool recycle(WiFiClient c) {
|
||||||
|
@ -65,22 +65,21 @@ public:
|
||||||
// return false here until we have
|
// return false here until we have
|
||||||
// implemented a LRU timer
|
// implemented a LRU timer
|
||||||
// if (LRU too recent) return false;
|
// if (LRU too recent) return false;
|
||||||
return false;
|
//return false;
|
||||||
|
|
||||||
wifi = c;
|
wifi = c;
|
||||||
inUse = true;
|
inUse = true;
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
WiFiClient wifi;
|
WiFiClient wifi;
|
||||||
bool inUse = true;
|
bool inUse = true;
|
||||||
};
|
};*/
|
||||||
|
|
||||||
static std::vector<NetworkClient> clients; // a list to hold all clients
|
//static std::vector<NetworkClient> clients; // a list to hold all clients
|
||||||
static WiFiServer *server = NULL;
|
static WiFiServer *server = NULL;
|
||||||
static RingStream *outboundRing = new RingStream(10240);
|
static RingStream *outboundRing = new RingStream(10240);
|
||||||
static bool APmode = false;
|
static bool APmode = false;
|
||||||
static IPAddress ip;
|
static IPAddress ip;
|
||||||
|
|
||||||
// #ifdef WIFI_TASK_ON_CORE0
|
// #ifdef WIFI_TASK_ON_CORE0
|
||||||
// void wifiLoop(void *){
|
// void wifiLoop(void *){
|
||||||
// for(;;){
|
// for(;;){
|
||||||
|
@ -149,7 +148,7 @@ bool WifiNINA::setup(const char *SSid,
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
// String ip_str = sprintf("%xl", WiFi.localIP());
|
// String ip_str = sprintf("%xl", WiFi.localIP());
|
||||||
DIAG(F("Wifi STA IP %d.%d.%d.%d"), WiFi.localIP()[0], WiFi.localIP()[1],WiFi.localIP()[2],WiFi.localIP()[3],WiFi.localIP()[4],WiFi.localIP()[5]);
|
DIAG(F("Wifi STA IP %d.%d.%d.%d"), WiFi.localIP()[0], WiFi.localIP()[1],WiFi.localIP()[2],WiFi.localIP()[3]);
|
||||||
wifiUp = true;
|
wifiUp = true;
|
||||||
} else {
|
} else {
|
||||||
DIAG(F("Could not connect to Wifi SSID %s"),SSid);
|
DIAG(F("Could not connect to Wifi SSID %s"),SSid);
|
||||||
|
@ -164,7 +163,7 @@ bool WifiNINA::setup(const char *SSid,
|
||||||
}
|
}
|
||||||
if (WiFi.status() == WL_CONNECTED) {
|
if (WiFi.status() == WL_CONNECTED) {
|
||||||
ip = WiFi.localIP();
|
ip = WiFi.localIP();
|
||||||
DIAG(F("Wifi STA IP 2nd try %s"), ip);
|
DIAG(F("Wifi STA IP 2nd try %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||||
wifiUp = true;
|
wifiUp = true;
|
||||||
} else {
|
} else {
|
||||||
DIAG(F("Wifi STA mode FAIL. Will revert to AP mode"));
|
DIAG(F("Wifi STA mode FAIL. Will revert to AP mode"));
|
||||||
|
@ -184,7 +183,7 @@ bool WifiNINA::setup(const char *SSid,
|
||||||
strMac += String(mac[i], HEX);
|
strMac += String(mac[i], HEX);
|
||||||
}
|
}
|
||||||
|
|
||||||
DIAG(F("MAC address: %x:%x:%x:%x:%X;%x"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
DIAG(F("MAC address: %x:%x:%x:%x:%x:%x"), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
|
||||||
|
|
||||||
strMac.remove(0,9);
|
strMac.remove(0,9);
|
||||||
strMac.replace(":","");
|
strMac.replace(":","");
|
||||||
|
@ -200,7 +199,7 @@ bool WifiNINA::setup(const char *SSid,
|
||||||
channel) == WL_AP_LISTENING) {
|
channel) == WL_AP_LISTENING) {
|
||||||
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
|
DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str());
|
||||||
ip = WiFi.localIP();
|
ip = WiFi.localIP();
|
||||||
DIAG(F("Wifi AP IP %s"),ip);
|
DIAG(F("Wifi AP IP %d.%d.%d.%d"),ip[0], ip[1], ip[2], ip[3]);
|
||||||
wifiUp = true;
|
wifiUp = true;
|
||||||
APmode = true;
|
APmode = true;
|
||||||
} else {
|
} else {
|
||||||
|
@ -248,6 +247,9 @@ bool WifiNINA::setup(const char *SSid,
|
||||||
// #else
|
// #else
|
||||||
DIAG(F("Server will be started on port %d"),port);
|
DIAG(F("Server will be started on port %d"),port);
|
||||||
// #endif
|
// #endif
|
||||||
|
ip = WiFi.localIP();
|
||||||
|
LCD(4,F("IP: %d.%d.%d.%d"), ip[0], ip[1], ip[2], ip[3]);
|
||||||
|
LCD(5,F("Port:%d"), port);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -261,7 +263,7 @@ const char *wlerror[] = {
|
||||||
"WL_DISCONNECTED"
|
"WL_DISCONNECTED"
|
||||||
};
|
};
|
||||||
|
|
||||||
void WifiNINA::loop() {
|
/*void WifiNINA::loop() {
|
||||||
int clientId; //tmp loop var
|
int clientId; //tmp loop var
|
||||||
|
|
||||||
// really no good way to check for LISTEN especially in AP mode?
|
// really no good way to check for LISTEN especially in AP mode?
|
||||||
|
@ -271,31 +273,33 @@ void WifiNINA::loop() {
|
||||||
for (clientId=0; clientId<clients.size(); clientId++){
|
for (clientId=0; clientId<clients.size(); clientId++){
|
||||||
// check if client is there and alive
|
// check if client is there and alive
|
||||||
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
|
if(clients[clientId].inUse && !clients[clientId].wifi.connected()) {
|
||||||
DIAG(F("Remove client %d"), clientId);
|
DIAG(F("Remove client %d"), clientId);
|
||||||
CommandDistributor::forget(clientId);
|
CommandDistributor::forget(clientId);
|
||||||
clients[clientId].wifi.stop();
|
clients[clientId].wifi.stop();
|
||||||
clients[clientId].inUse = false;
|
clients[clientId].inUse = false;
|
||||||
//Do NOT clients.erase(clients.begin()+clientId) as
|
|
||||||
//that would mix up clientIds for later.
|
//Do NOT clients.erase(clients.begin()+clientId) as
|
||||||
|
//that would mix up clientIds for later.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (server->available()) {
|
WiFiClient client = server->available();
|
||||||
WiFiClient client;
|
if (client) {
|
||||||
while (client = server->available()) {
|
///while (client.available() == true) {
|
||||||
for (clientId=0; clientId<clients.size(); clientId++){
|
for (clientId=0; clientId<clients.size(); clientId++){
|
||||||
if (clients[clientId].recycle(client)) {
|
if (clients[clientId].recycle(client)) {
|
||||||
ip = client.remoteIP();
|
ip = client.remoteIP();
|
||||||
DIAG(F("Recycle client %d %s"), clientId, ip);
|
DIAG(F("Recycle client %d %d.%d.%d.%d"), clientId, ip[0], ip[1], ip[2], ip[3]);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (clientId>=clients.size()) {
|
if (clientId>=clients.size()) {
|
||||||
NetworkClient nc(client);
|
NetworkClient* nc=new NetworkClient(client);
|
||||||
clients.push_back(nc);
|
clients.push_back(*nc);
|
||||||
|
//delete nc;
|
||||||
ip = client.remoteIP();
|
ip = client.remoteIP();
|
||||||
DIAG(F("New client %d, %s"), clientId, ip);
|
DIAG(F("New client %d, %d.%d.%d.%d"), clientId, ip[0], ip[1], ip[2], ip[3]);
|
||||||
}
|
}
|
||||||
}
|
///}
|
||||||
}
|
}
|
||||||
// loop over all connected clients
|
// loop over all connected clients
|
||||||
for (clientId=0; clientId<clients.size(); clientId++){
|
for (clientId=0; clientId<clients.size(); clientId++){
|
||||||
|
@ -363,5 +367,79 @@ void WifiNINA::loop() {
|
||||||
//DIAG(F("Running BT"));
|
//DIAG(F("Running BT"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
WiFiClient * clients[MAX_CLIENTS]; // nulled in setup
|
||||||
|
|
||||||
|
void WifiNINA::checkForNewClient() {
|
||||||
|
auto newClient=server->available();
|
||||||
|
if (!newClient) return;
|
||||||
|
for (byte clientId=0; clientId<MAX_CLIENTS; clientId++){
|
||||||
|
if (!clients[clientId]) {
|
||||||
|
clients[clientId]= new WiFiClient(newClient); // use this slot
|
||||||
|
DIAG(F("New client connected to slot %d"),clientId); //TJF: brought in for debugging.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WifiNINA::checkForLostClients() {
|
||||||
|
for (byte clientId=0; clientId<MAX_CLIENTS; clientId++){
|
||||||
|
auto c=clients[clientId];
|
||||||
|
if(c && !c->connected()) {
|
||||||
|
clients[clientId]->stop();
|
||||||
|
DIAG(F("Remove client %d"), clientId);
|
||||||
|
CommandDistributor::forget(clientId);
|
||||||
|
//delete c; //TJF: this causes a crash when client drops.. commenting out for now.
|
||||||
|
clients[clientId]=nullptr; // TJF: what to do... what to do...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiNINA::checkForClientInput() {
|
||||||
|
// Find a client providing input
|
||||||
|
for (byte clientId=0; clientId<MAX_CLIENTS; clientId++){
|
||||||
|
auto c=clients[clientId];
|
||||||
|
if(c) {
|
||||||
|
auto len=c->available();
|
||||||
|
if (len) {
|
||||||
|
// read data from client
|
||||||
|
byte cmd[len+1];
|
||||||
|
for(int i=0; i<len; i++) cmd[i]=c->read();
|
||||||
|
cmd[len]=0;
|
||||||
|
CommandDistributor::parse(clientId,cmd,outboundRing);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiNINA::checkForClientOutput() {
|
||||||
|
// something to write out?
|
||||||
|
auto clientId=outboundRing->read();
|
||||||
|
if (clientId < 0) return;
|
||||||
|
auto replySize=outboundRing->count();
|
||||||
|
if (replySize==0) return; // nothing to send
|
||||||
|
auto c=clients[clientId];
|
||||||
|
if (!c) {
|
||||||
|
// client is gone, throw away msg
|
||||||
|
for (int i=0;i<replySize;i++) outboundRing->read();
|
||||||
|
DIAG(F("gone, drop message.")); //TJF: only for diag
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// emit data to the client object
|
||||||
|
// This should work in theory, the
|
||||||
|
DIAG(F("send message")); //TJF: only for diag
|
||||||
|
//TJF: the old code had to add a 0x00 byte to the end to terminate the
|
||||||
|
//TJF: c string, before sending it. i take it this is not needed?
|
||||||
|
for (int i=0;i<replySize;i++) c->write(outboundRing->read());
|
||||||
|
}
|
||||||
|
|
||||||
|
void WifiNINA::loop() {
|
||||||
|
checkForLostClients(); // ***
|
||||||
|
checkForNewClient();
|
||||||
|
checkForClientInput(); // ***
|
||||||
|
WiThrottle::loop(outboundRing); // allow withrottle to broadcast if needed
|
||||||
|
checkForClientOutput();
|
||||||
|
}
|
||||||
|
|
||||||
#endif // WIFI_NINA
|
#endif // WIFI_NINA
|
|
@ -35,8 +35,12 @@ public:
|
||||||
const char *hostname,
|
const char *hostname,
|
||||||
const int port,
|
const int port,
|
||||||
const byte channel,
|
const byte channel,
|
||||||
const bool forceAP);
|
const bool forceAP);
|
||||||
static void loop();
|
static void loop();
|
||||||
private:
|
private:
|
||||||
|
static void checkForNewClient();
|
||||||
|
static void checkForLostClients();
|
||||||
|
static void checkForClientInput();
|
||||||
|
static void checkForClientOutput();
|
||||||
};
|
};
|
||||||
#endif //WifiNINA_h
|
#endif //WifiNINA_h
|
||||||
|
|
Loading…
Reference in New Issue
Block a user