From b1f5c34ef222f5223c66dc4f16c9359bfab254fd Mon Sep 17 00:00:00 2001 From: travis-farmer Date: Fri, 3 Nov 2023 07:17:17 -0400 Subject: [PATCH] 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