From dfa798c14938069261a3c6903a63bfe86f219b69 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Thu, 2 Nov 2023 09:12:31 -0400 Subject: [PATCH 01/10] seems to work now --- Wifi_NINA.cpp | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 8d54030..fdf496a 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -55,7 +55,7 @@ public: NetworkClient(WiFiClient c) { wifi = c; }; - bool ok() { + bool ok() { return (inUse && wifi.connected()); }; bool recycle(WiFiClient c) { @@ -65,13 +65,13 @@ public: // return false here until we have // implemented a LRU timer // if (LRU too recent) return false; - return false; + //return false; wifi = c; inUse = true; return true; }; - WiFiClient wifi; + WiFiClient wifi; bool inUse = true; }; @@ -248,6 +248,9 @@ bool WifiNINA::setup(const char *SSid, // #else DIAG(F("Server will be started on port %d"),port); // #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; } @@ -271,31 +274,33 @@ void WifiNINA::loop() { for (clientId=0; clientIdavailable()) { - WiFiClient client; - while (client = server->available()) { + WiFiClient client = server->available(); + if (client) { + ///while (!client) { for (clientId=0; clientId=clients.size()) { - NetworkClient nc(client); - clients.push_back(nc); + auto nc=new NetworkClient(client); + clients.push_back(*nc); + delete nc; 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 for (clientId=0; clientId Date: Thu, 2 Nov 2023 14:00:02 -0400 Subject: [PATCH 02/10] keeping up to date - cleaning --- Wifi_NINA.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index fdf496a..bd32bef 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -284,8 +284,8 @@ void WifiNINA::loop() { } } WiFiClient client = server->available(); - if (client) { - ///while (!client) { + if (client == true) { + ///while (client.available() == true) { for (clientId=0; clientId=clients.size()) { - auto nc=new NetworkClient(client); + NetworkClient* nc=new NetworkClient(client); clients.push_back(*nc); - delete nc; + //delete nc; ip = client.remoteIP(); DIAG(F("New client %d, %d.%d.%d.%d"), clientId, ip[0], ip[1], ip[2], ip[3]); } From b1f5c34ef222f5223c66dc4f16c9359bfab254fd Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 07:17:17 -0400 Subject: [PATCH 03/10] compiles, but crashes on client connect --- Wifi_NINA.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++++++++--- Wifi_NINA.h | 6 +++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index bd32bef..38bed4f 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -49,7 +49,7 @@ #else #warning "WiFiNINA has no SPI port or pin allocations for this archiecture yet!" #endif - +#define MAX_CLIENTS 10 class NetworkClient { public: NetworkClient(WiFiClient c) { @@ -75,7 +75,7 @@ public: bool inUse = true; }; -static std::vector clients; // a list to hold all clients +//static std::vector clients; // a list to hold all clients static WiFiServer *server = NULL; static RingStream *outboundRing = new RingStream(10240); static bool APmode = false; @@ -264,7 +264,7 @@ const char *wlerror[] = { "WL_DISCONNECTED" }; -void WifiNINA::loop() { +/*void WifiNINA::loop() { int clientId; //tmp loop var // really no good way to check for LISTEN especially in AP mode? @@ -284,7 +284,7 @@ void WifiNINA::loop() { } } WiFiClient client = server->available(); - if (client == true) { + if (client) { ///while (client.available() == true) { for (clientId=0; clientIdavailable(); + if (!newClient) return; + for (byte clientId=0; clientIdconnected()) { + DIAG(F("Remove client %d"), clientId); + CommandDistributor::forget(clientId); + delete c; // we have now finished with this client + clients[clientId]=nullptr; + } + } +} + +void WifiNINA::checkForClientInput() { + // Find a client providing input + for (byte clientId=0; clientIdavailable(); + if (len) { + // read data from client + byte cmd[len+1]; + for(int i=0; iread(); + 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;iread(); + return; + } + + // emit data to the client object + // This should work in theory, the + for (int i=0;iwrite(outboundRing->read()); + +} + +void WifiNINA::loop() { + checkForLostClients(); + checkForNewClient(); + checkForClientInput(); + WiThrottle::loop(outboundRing); // allow withrottle to broadcast if needed + checkForClientOutput(); +} + #endif // WIFI_NINA \ No newline at end of file diff --git a/Wifi_NINA.h b/Wifi_NINA.h index f841b4f..2cf5fbd 100644 --- a/Wifi_NINA.h +++ b/Wifi_NINA.h @@ -35,8 +35,12 @@ public: const char *hostname, const int port, const byte channel, - const bool forceAP); + const bool forceAP); static void loop(); private: + static void checkForNewClient(); + static void checkForLostClients(); + static void checkForClientInput(); + static void checkForClientOutput(); }; #endif //WifiNINA_h From 05db1bdd9082f2f3bf02257a95c5bdc4e5d463cd Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 07:34:05 -0400 Subject: [PATCH 04/10] cleaning --- Wifi_NINA.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 38bed4f..c662e13 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -21,7 +21,7 @@ #include "defines.h" #ifdef WIFI_NINA -#include +//#include #include #ifndef ARDUINO_GIGA #include @@ -49,8 +49,8 @@ #else #warning "WiFiNINA has no SPI port or pin allocations for this archiecture yet!" #endif -#define MAX_CLIENTS 10 -class NetworkClient { +#define MAX_CLIENTS 4 +/*class NetworkClient { public: NetworkClient(WiFiClient c) { wifi = c; @@ -73,7 +73,7 @@ public: }; WiFiClient wifi; bool inUse = true; -}; +};*/ //static std::vector clients; // a list to hold all clients static WiFiServer *server = NULL; From d95d9c193e108c26f39f4ba0edfe0fb8e827aa65 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 07:48:12 -0400 Subject: [PATCH 05/10] allows client, but immidiatly drops client --- Wifi_NINA.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index c662e13..19efc14 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -378,7 +378,7 @@ void WifiNINA::checkForNewClient() { for (byte clientId=0; clientIdconnected()) { DIAG(F("Remove client %d"), clientId); CommandDistributor::forget(clientId); - delete c; // we have now finished with this client + //delete c; // we have now finished with this client clients[clientId]=nullptr; } } From b632088b19c90d4fd0f8713337b777d83f762c13 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 09:06:42 -0400 Subject: [PATCH 06/10] cleaning... --- Wifi_NINA.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 19efc14..1ae888b 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -390,7 +390,7 @@ void WifiNINA::checkForLostClients() { if(c && !c->connected()) { DIAG(F("Remove client %d"), clientId); CommandDistributor::forget(clientId); - //delete c; // we have now finished with this client + //delete c; // this causes a crash when client drops.. commenting out for now clients[clientId]=nullptr; } } @@ -412,24 +412,22 @@ void WifiNINA::checkForClientInput() { } } } + 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;iread(); return; } - // emit data to the client object // This should work in theory, the for (int i=0;iwrite(outboundRing->read()); - } void WifiNINA::loop() { From 741771c4fe47d0097435a4bda6ecffa6940cd5b3 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 11:49:25 -0400 Subject: [PATCH 07/10] added diags for debug tracking... --- Wifi_NINA.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 1ae888b..ba57f00 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -378,7 +378,7 @@ void WifiNINA::checkForNewClient() { for (byte clientId=0; clientIdconnected()) { DIAG(F("Remove client %d"), clientId); CommandDistributor::forget(clientId); - //delete c; // this causes a crash when client drops.. commenting out for now + //delete c; //TJF: this causes a crash when client drops.. commenting out for now. clients[clientId]=nullptr; } } @@ -423,10 +423,14 @@ void WifiNINA::checkForClientOutput() { if (!c) { // client is gone, throw away msg for (int i=0;iread(); + 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;iwrite(outboundRing->read()); } From 050fed6e9af26988da9d57d554e4c432496c3ed3 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 13:40:48 -0400 Subject: [PATCH 08/10] crashes --- Wifi_NINA.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index ba57f00..46acfd4 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -184,7 +184,7 @@ bool WifiNINA::setup(const char *SSid, 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.replace(":",""); @@ -200,7 +200,7 @@ bool WifiNINA::setup(const char *SSid, channel) == WL_AP_LISTENING) { DIAG(F("Wifi AP SSID %s PASS %s"),strSSID.c_str(),havePassword ? password : strPass.c_str()); 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; APmode = true; } else { @@ -370,14 +370,14 @@ const char *wlerror[] = { } }*/ -WiFiClient * clients[MAX_CLIENTS]; // nulled in setup +WiFiClient clients[MAX_CLIENTS]; // nulled in setup void WifiNINA::checkForNewClient() { auto newClient=server->available(); if (!newClient) return; for (byte clientId=0; clientIdconnected()) { - 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; - } + if(c && !c.connected()) { + 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]=NULL; // TJF: what to do... what to do... + } } } @@ -401,11 +401,11 @@ void WifiNINA::checkForClientInput() { for (byte clientId=0; clientIdavailable(); + auto len=c.available(); if (len) { // read data from client byte cmd[len+1]; - for(int i=0; iread(); + for(int i=0; iwrite(outboundRing->read()); + for (int i=0;iread()); } void WifiNINA::loop() { From 8a813c2a1ecdc1eb40965dc4853d78bc9068a642 Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 14:11:55 -0400 Subject: [PATCH 09/10] updating current, still crashes --- Wifi_NINA.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 46acfd4..5f8048d 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -149,7 +149,7 @@ bool WifiNINA::setup(const char *SSid, } if (WiFi.status() == WL_CONNECTED) { // 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; } else { DIAG(F("Could not connect to Wifi SSID %s"),SSid); @@ -164,7 +164,7 @@ bool WifiNINA::setup(const char *SSid, } if (WiFi.status() == WL_CONNECTED) { 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; } else { DIAG(F("Wifi STA mode FAIL. Will revert to AP mode")); @@ -435,9 +435,9 @@ void WifiNINA::checkForClientOutput() { } void WifiNINA::loop() { - checkForLostClients(); + checkForLostClients(); // *** checkForNewClient(); - checkForClientInput(); + checkForClientInput(); // *** WiThrottle::loop(outboundRing); // allow withrottle to broadcast if needed checkForClientOutput(); } From e6797d1095a9ee7ee3344277187e6f56e9ce53ab Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Sat, 4 Nov 2023 07:36:23 -0400 Subject: [PATCH 10/10] Giga wifi works for 3 clients MAX --- Wifi_NINA.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Wifi_NINA.cpp b/Wifi_NINA.cpp index 5f8048d..ea970f3 100644 --- a/Wifi_NINA.cpp +++ b/Wifi_NINA.cpp @@ -49,7 +49,7 @@ #else #warning "WiFiNINA has no SPI port or pin allocations for this archiecture yet!" #endif -#define MAX_CLIENTS 4 +#define MAX_CLIENTS 10 /*class NetworkClient { public: NetworkClient(WiFiClient c) { @@ -80,7 +80,6 @@ static WiFiServer *server = NULL; static RingStream *outboundRing = new RingStream(10240); static bool APmode = false; static IPAddress ip; - // #ifdef WIFI_TASK_ON_CORE0 // void wifiLoop(void *){ // for(;;){ @@ -370,14 +369,14 @@ const char *wlerror[] = { } }*/ -WiFiClient clients[MAX_CLIENTS]; // nulled in setup +WiFiClient * clients[MAX_CLIENTS]; // nulled in setup void WifiNINA::checkForNewClient() { auto newClient=server->available(); if (!newClient) return; for (byte clientId=0; clientIdconnected()) { + 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]=NULL; // TJF: what to do... what to do... + clients[clientId]=nullptr; // TJF: what to do... what to do... } } } @@ -401,11 +401,11 @@ void WifiNINA::checkForClientInput() { for (byte clientId=0; clientIdavailable(); if (len) { // read data from client byte cmd[len+1]; - for(int i=0; iread(); cmd[len]=0; CommandDistributor::parse(clientId,cmd,outboundRing); } @@ -431,7 +431,7 @@ void WifiNINA::checkForClientOutput() { 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;iread()); + for (int i=0;iwrite(outboundRing->read()); } void WifiNINA::loop() {