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

94 lines
2.4 KiB
C++
Raw Normal View History

2021-02-15 21:53:37 +01:00
#include "taskTNC.h"
#ifdef ENABLE_BLUETOOTH
BluetoothSerial SerialBT;
#endif
String inTNCData = "";
QueueHandle_t tncToSendQueue = nullptr;
QueueHandle_t tncReceivedQueue = nullptr;
2021-02-18 19:34:42 +01:00
WiFiClient client;
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
if (client.connected()){
client.print(inTNCData);
client.flush();
}
#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 19:34:42 +01:00
client = tncServer.available();
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
if (!client.connected()){
client = tncServer.available();
}
if (client.connected()){
while (client.available() > 0) {
char character = client.read();
handleKISSData(character);
}
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
if (client.connected()){
client.print(kissEncoded);
client.flush();
}
#endif
2021-02-15 21:53:37 +01:00
delete loraReceivedFrameString;
}
vTaskDelay(50 / portTICK_PERIOD_MS);
}
}