diff --git a/include/taskGPS.h b/include/taskGPS.h index 5588a6c..f192fd8 100644 --- a/include/taskGPS.h +++ b/include/taskGPS.h @@ -2,4 +2,5 @@ #include extern TinyGPSPlus gps; -void taskGPS(void *parameter); + +[[noreturn]] void taskGPS(void *parameter); diff --git a/include/taskTNC.h b/include/taskTNC.h index c938cc5..5f2cba1 100644 --- a/include/taskTNC.h +++ b/include/taskTNC.h @@ -7,6 +7,7 @@ #endif #if defined(ENABLE_WIFI) #include "taskWebServer.h" + #include "wifi_clients.h" #endif extern QueueHandle_t tncToSendQueue; extern QueueHandle_t tncReceivedQueue; diff --git a/include/taskWebServer.h b/include/taskWebServer.h index 8ed5554..cb0dce9 100644 --- a/include/taskWebServer.h +++ b/include/taskWebServer.h @@ -12,6 +12,7 @@ extern BG_RF95 rf95; #ifdef KISS_PROTOCOL extern WiFiServer tncServer; #endif +extern WiFiServer gpsServer; typedef struct { String callsign; } tWebServerCfg; diff --git a/platformio.ini b/platformio.ini index e9f8362..96efe99 100644 --- a/platformio.ini +++ b/platformio.ini @@ -49,6 +49,7 @@ build_flags = -D 'ENABLE_OLED' -D 'ENABLE_LED_SIGNALING' -D 'NETWORK_TNC_PORT=8001' + -D 'NETWORK_GPS_PORT=10110' -D 'MAX_TIME_TO_NEXT_TX=360000L' -D 'FIX_BEACON_INTERVAL=1800000L' diff --git a/src/taskGPS.cpp b/src/taskGPS.cpp index 895797f..9847b8e 100644 --- a/src/taskGPS.cpp +++ b/src/taskGPS.cpp @@ -1,8 +1,16 @@ #include #include +#include + SFE_UBLOX_GPS myGPS; +#ifdef ENABLE_WIFI + #include "wifi_clients.h" + #define MAX_GPS_WIFI_CLIENTS 6 + WiFiClient * gps_clients[MAX_GPS_WIFI_CLIENTS]; +#endif + // Pins for GPS #ifdef T_BEAM_V1_0 static const int RXPin = 12, TXPin = 34; @@ -14,7 +22,7 @@ HardwareSerial gpsSerial(1); // TTGO has HW serial TinyGPSPlus gps; // The TinyGPS++ object bool gpsInitialized = false; -void taskGPS(void *parameter) { +[[noreturn]] void taskGPS(void *parameter) { if (!gpsInitialized){ gpsSerial.begin(GPSBaud, SERIAL_8N1, TXPin, RXPin); //Startup HW serial for GPS @@ -35,9 +43,29 @@ void taskGPS(void *parameter) { } } + String gpsDataBuffer = " "; for (;;) { + check_for_new_clients(&gpsServer, gps_clients, MAX_GPS_WIFI_CLIENTS); while (gpsSerial.available() > 0) { - gps.encode(gpsSerial.read()); + char gpsChar = (char)gpsSerial.read(); + gps.encode(gpsChar); + #ifdef ENABLE_WIFI + if (gpsChar == '$') { + gpsDataBuffer = String(gpsChar); + } else { + gpsDataBuffer += String(gpsChar); + + if (gpsChar == '\n') { + iterateWifiClients([](WiFiClient *client, int clientIdx, const String *data){ + if (client->connected()){ + client->print(*data); + client->flush(); + } + }, &gpsDataBuffer, gps_clients, MAX_GPS_WIFI_CLIENTS); + gpsDataBuffer = ""; + } + } + #endif } vTaskDelay(100 / portTICK_PERIOD_MS); } diff --git a/src/taskTNC.cpp b/src/taskTNC.cpp index 8c499c4..c77e334 100644 --- a/src/taskTNC.cpp +++ b/src/taskTNC.cpp @@ -9,25 +9,6 @@ QueueHandle_t tncReceivedQueue = nullptr; #ifdef ENABLE_WIFI #define MAX_WIFI_CLIENTS 6 WiFiClient * clients[MAX_WIFI_CLIENTS]; - - typedef void (*f_connectedClientCallback_t) (WiFiClient *, int, const String *); - - void iterateWifiClients(f_connectedClientCallback_t callback, const String *data){ - for (int i=0; iconnected()) { - callback(client, i, data); - } else { - #ifdef ENABLE_WIFI_CLIENT_DEBUG - Serial.println(String("Disconnected client ") + client->remoteIP().toString() + ":" + client->remotePort()); - #endif - delete client; - clients[i] = nullptr; - } - } - } - } #endif #ifdef ENABLE_WIFI #define IN_TNC_BUFFERS (2+MAX_WIFI_CLIENTS) @@ -67,7 +48,7 @@ void handleKISSData(char character, int bufferIndex) { client->print(*data); client->flush(); } - }, &inTNCData); + }, &inTNCData, clients, MAX_WIFI_CLIENTS); #endif #endif auto *buffer = new String(); @@ -103,45 +84,14 @@ void handleKISSData(char character, int bufferIndex) { } #endif #ifdef ENABLE_WIFI - WiFiClient new_client = tncServer.available(); - if (new_client.connected()){ - bool new_client_handled = false; - for (int i=0; i < MAX_WIFI_CLIENTS; i++) { - auto client = clients[i]; - if (client == nullptr) { - client = new WiFiClient(new_client); - clients[i] = client; - new_client_handled = true; - #ifdef ENABLE_WIFI_CLIENT_DEBUG - Serial.println(String("New client #") +String(i) + ": " + client->remoteIP().toString() + ":" + client->remotePort()); - #endif - break; - } - } - #ifdef ENABLE_WIFI_CLIENT_DEBUG - for (int i = 0; i < MAX_WIFI_CLIENTS; ++i) { - auto client = clients[i]; + check_for_new_clients(&tncServer, clients, MAX_WIFI_CLIENTS); - if (client != nullptr){ - Serial.println(String("Client #") +String(i) + ": " + client->remoteIP().toString() + ":" + client->remotePort()); - } - } - #endif - - - if (!new_client_handled){ - #ifdef ENABLE_WIFI_CLIENT_DEBUG - Serial.println(String("Refusing client ")); - #endif - new_client.stop(); - } - } iterateWifiClients([](WiFiClient * client, int clientIdx, const String * unused){ while (client->available() > 0) { char character = client->read(); handleKISSData(character, 2+clientIdx); } - }, nullptr); + }, nullptr, clients, MAX_WIFI_CLIENTS); #endif if (xQueueReceive(tncReceivedQueue, &loraReceivedFrameString, (1 / portTICK_PERIOD_MS)) == pdPASS) { @@ -158,7 +108,7 @@ void handleKISSData(char character, int bufferIndex) { client->print(*data); client->flush(); } - }, &kissEncoded); + }, &kissEncoded, clients, MAX_WIFI_CLIENTS); #endif delete loraReceivedFrameString; diff --git a/src/taskWebServer.cpp b/src/taskWebServer.cpp index 83ab67c..2a059ae 100644 --- a/src/taskWebServer.cpp +++ b/src/taskWebServer.cpp @@ -25,6 +25,7 @@ WebServer server(80); #ifdef KISS_PROTOCOL WiFiServer tncServer(NETWORK_TNC_PORT); #endif +WiFiServer gpsServer(NETWORK_GPS_PORT); #ifdef ENABLE_SYSLOG // A UDP instance to let us send and receive packets over UDP @@ -324,6 +325,7 @@ void handle_saveDeviceCfg(){ #ifdef KISS_PROTOCOL tncServer.begin(); #endif + gpsServer.begin(); if (MDNS.begin(webServerCfg->callsign.c_str())) { MDNS.setInstanceName(webServerCfg->callsign + " TTGO LoRa APRS TNC " + TXFREQ + "MHz"); MDNS.addService("http", "tcp", 80);