From 713fe63b12bfe525040b77e3f7c76f382a804d3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Nidecki?= Date: Thu, 18 Feb 2021 20:37:05 +0100 Subject: [PATCH] Handle multiple TNC network clients --- src/taskTNC.cpp | 47 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/src/taskTNC.cpp b/src/taskTNC.cpp index a18bd0f..2b51609 100644 --- a/src/taskTNC.cpp +++ b/src/taskTNC.cpp @@ -1,3 +1,4 @@ +#include #include "taskTNC.h" #ifdef ENABLE_BLUETOOTH @@ -6,7 +7,6 @@ String inTNCData = ""; QueueHandle_t tncToSendQueue = nullptr; QueueHandle_t tncReceivedQueue = nullptr; -WiFiClient client; /** * Handle incoming TNC KISS data character @@ -40,12 +40,31 @@ void handleKISSData(char character) { } } +#ifdef ENABLE_WIFI +typedef void (*f_connectedClientCallback_t) (WiFiClient *, const String *); + +void iterateWifiClients(std::list clients, f_connectedClientCallback_t callback, const String *data){ + auto clientsIterator = clients.begin(); + while (clientsIterator != clients.end()){ + if ((*clientsIterator)->connected()){ + callback(*clientsIterator, data); + clientsIterator++; + } else { + clientsIterator = clients.erase(clientsIterator); + } + } +} +#endif + [[noreturn]] void taskTNC(void *parameter) { tncToSendQueue = xQueueCreate(4,sizeof(String *)); tncReceivedQueue = xQueueCreate(4,sizeof(String *)); String *loraReceivedFrameString = nullptr; - client = tncServer.available(); + #ifdef ENABLE_WIFI + std::list clients; + #endif + while (true) { while (Serial.available() > 0) { char character = Serial.read(); @@ -60,15 +79,17 @@ void handleKISSData(char character) { } #endif #ifdef ENABLE_WIFI - if (!client.connected()){ - client = tncServer.available(); + WiFiClient new_client = tncServer.available(); + if (new_client.connected()){ + clients.push_back(new WiFiClient(new_client)); } - if (client.connected()){ - while (client.available() > 0) { - char character = client.read(); + iterateWifiClients(clients, [](WiFiClient * client, const String * unused){ + while (client->available() > 0) { + char character = client->read(); handleKISSData(character); } - } + }, nullptr); + #endif if (xQueueReceive(tncReceivedQueue, &loraReceivedFrameString, (1 / portTICK_PERIOD_MS)) == pdPASS) { const String &kissEncoded = encode_kiss(*loraReceivedFrameString); @@ -79,10 +100,12 @@ void handleKISSData(char character) { } #endif #ifdef ENABLE_WIFI - if (client.connected()){ - client.print(kissEncoded); - client.flush(); - } + iterateWifiClients(clients, [](WiFiClient *client, const String *data){ + if (client->connected()){ + client->print(*data); + client->flush(); + } + }, &kissEncoded); #endif delete loraReceivedFrameString;