TTGO-T-Beam-LoRa-APRS/src/taskTNC.cpp

153 lines
4.3 KiB
C++
Raw Normal View History

2021-02-15 21:53:37 +01:00
#include "taskTNC.h"
2021-02-19 01:27:14 +01:00
2021-02-15 21:53:37 +01:00
#ifdef ENABLE_BLUETOOTH
BluetoothSerial SerialBT;
#endif
String inTNCData = "";
QueueHandle_t tncToSendQueue = nullptr;
QueueHandle_t tncReceivedQueue = nullptr;
2021-02-18 23:01:15 +01:00
#ifdef ENABLE_WIFI
2021-02-19 01:27:14 +01:00
#define MAX_WIFI_CLIENTS 6
WiFiClient * clients[MAX_WIFI_CLIENTS];
2021-02-18 23:01:15 +01:00
typedef void (*f_connectedClientCallback_t) (WiFiClient *, const String *);
2021-02-19 01:27:14 +01:00
void iterateWifiClients(f_connectedClientCallback_t callback, const String *data){
for (int i=0; i<MAX_WIFI_CLIENTS; i++) {
auto client = clients[i];
if (client != nullptr) {
if (client->connected()) {
callback(client, data);
} else {
#ifdef ENABLE_WIFI_CLIENT_DEBUG
Serial.println(String("Disconnected client ") + client->remoteIP().toString() + ":" + client->remotePort());
#endif
delete client;
clients[i] = nullptr;
}
2021-02-18 23:01:15 +01:00
}
}
}
#endif
2021-02-15 21:53:37 +01:00
/**
* Handle incoming TNC KISS data character
* @param character
*/
void handleKISSData(char character) {
inTNCData.concat(character);
if (character == (char) FEND && inTNCData.length() > 3) {
const String &TNC2DataFrame = decode_kiss(inTNCData);
#ifdef LOCAL_KISS_ECHO
Serial.print(inTNCData);
#ifdef ENABLE_BLUETOOTH
if (SerialBT.hasClient()) {
SerialBT.print(inTNCData);
}
#endif
2021-02-18 19:34:42 +01:00
#ifdef ENABLE_WIFI
2021-02-19 01:27:14 +01:00
iterateWifiClients([](WiFiClient *client, const String *data){
2021-02-18 23:01:15 +01:00
if (client->connected()){
client->print(*data);
client->flush();
}
}, &inTNCData);
2021-02-18 19:34:42 +01:00
#endif
2021-02-15 21:53:37 +01:00
#endif
auto *buffer = new String();
buffer->concat(TNC2DataFrame);
if (xQueueSend(tncToSendQueue, &buffer, (1000 / portTICK_PERIOD_MS)) != pdPASS){
delete buffer;
}
inTNCData = "";
}
}
2021-02-18 19:34:42 +01:00
[[noreturn]] void taskTNC(void *parameter) {
2021-02-15 21:53:37 +01:00
tncToSendQueue = xQueueCreate(4,sizeof(String *));
tncReceivedQueue = xQueueCreate(4,sizeof(String *));
String *loraReceivedFrameString = nullptr;
2021-02-18 20:37:05 +01:00
2021-02-15 21:53:37 +01:00
while (true) {
while (Serial.available() > 0) {
char character = Serial.read();
handleKISSData(character);
}
#ifdef ENABLE_BLUETOOTH
2021-02-18 19:34:42 +01:00
if (SerialBT.hasClient()) {
while (SerialBT.available() > 0) {
char character = SerialBT.read();
handleKISSData(character);
}
}
#endif
#ifdef ENABLE_WIFI
2021-02-18 20:37:05 +01:00
WiFiClient new_client = tncServer.available();
if (new_client.connected()){
2021-02-19 01:27:14 +01:00
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];
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();
}
2021-02-18 19:34:42 +01:00
}
2021-02-19 01:27:14 +01:00
iterateWifiClients([](WiFiClient * client, const String * unused){
2021-02-18 20:37:05 +01:00
while (client->available() > 0) {
char character = client->read();
2021-02-18 19:34:42 +01:00
handleKISSData(character);
}
2021-02-18 20:37:05 +01:00
}, nullptr);
2021-02-15 21:53:37 +01:00
#endif
if (xQueueReceive(tncReceivedQueue, &loraReceivedFrameString, (1 / portTICK_PERIOD_MS)) == pdPASS) {
2021-02-18 19:34:42 +01:00
const String &kissEncoded = encode_kiss(*loraReceivedFrameString);
Serial.print(kissEncoded);
2021-02-15 21:53:37 +01:00
#ifdef ENABLE_BLUETOOTH
if (SerialBT.hasClient()){
2021-02-18 19:34:42 +01:00
SerialBT.print(kissEncoded);
2021-02-15 21:53:37 +01:00
}
#endif
2021-02-18 19:34:42 +01:00
#ifdef ENABLE_WIFI
2021-02-19 01:27:14 +01:00
iterateWifiClients([](WiFiClient *client, const String *data){
2021-02-18 20:37:05 +01:00
if (client->connected()){
client->print(*data);
client->flush();
}
}, &kissEncoded);
2021-02-18 19:34:42 +01:00
#endif
2021-02-15 21:53:37 +01:00
delete loraReceivedFrameString;
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}