Browse Source

Handle multiple TNC network clients

pull/16/head
Łukasz Nidecki 10 months ago
parent
commit
713fe63b12
  1. 47
      src/taskTNC.cpp

47
src/taskTNC.cpp

@ -1,3 +1,4 @@
#include <list>
#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<WiFiClient *> 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<WiFiClient *> 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;

Loading…
Cancel
Save