2021-02-15 22:53:37 +02:00
|
|
|
#include "taskTNC.h"
|
|
|
|
|
2021-02-19 02:27:14 +02:00
|
|
|
|
2021-02-15 22:53:37 +02:00
|
|
|
#ifdef ENABLE_BLUETOOTH
|
|
|
|
BluetoothSerial SerialBT;
|
|
|
|
#endif
|
2021-02-19 02:56:43 +02:00
|
|
|
|
2021-02-15 22:53:37 +02:00
|
|
|
QueueHandle_t tncReceivedQueue = nullptr;
|
2021-02-19 00:01:15 +02:00
|
|
|
#ifdef ENABLE_WIFI
|
2021-02-19 02:27:14 +02:00
|
|
|
#define MAX_WIFI_CLIENTS 6
|
|
|
|
WiFiClient * clients[MAX_WIFI_CLIENTS];
|
2021-02-19 00:01:15 +02:00
|
|
|
#endif
|
2021-02-19 02:56:43 +02:00
|
|
|
#ifdef ENABLE_WIFI
|
|
|
|
#define IN_TNC_BUFFERS (2+MAX_WIFI_CLIENTS)
|
|
|
|
#else
|
|
|
|
#define IN_TNC_BUFFERS 2
|
|
|
|
#endif
|
|
|
|
|
|
|
|
String inTNCDataBuffers[IN_TNC_BUFFERS];
|
|
|
|
|
|
|
|
QueueHandle_t tncToSendQueue = nullptr;
|
2021-02-19 00:01:15 +02:00
|
|
|
|
2021-02-15 22:53:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle incoming TNC KISS data character
|
|
|
|
* @param character
|
|
|
|
*/
|
2021-02-19 02:56:43 +02:00
|
|
|
void handleKISSData(char character, int bufferIndex) {
|
|
|
|
String *inTNCData = &inTNCDataBuffers[bufferIndex];
|
2021-09-13 21:11:10 +03:00
|
|
|
if (inTNCData->length() == 0 && character != (char) FEND)
|
|
|
|
{
|
2021-02-19 02:56:43 +02:00
|
|
|
// kiss frame begins with C0
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
inTNCData->concat(character);
|
2021-09-16 11:51:02 +03:00
|
|
|
if (character == (char) FEND && inTNCData->length() > 3)
|
|
|
|
{
|
2021-09-06 20:15:00 +03:00
|
|
|
bool isDataFrame = false;
|
2021-09-12 22:17:16 +03:00
|
|
|
bool isPacketFrame = false;
|
2021-09-16 11:51:02 +03:00
|
|
|
|
2021-09-14 11:18:21 +03:00
|
|
|
const String &TNC2DataFrame = decode_kiss(*inTNCData, isDataFrame, isPacketFrame);
|
2021-09-14 22:46:46 +03:00
|
|
|
if (isDataFrame && isPacketFrame) {
|
|
|
|
auto *buffer = new String();
|
|
|
|
buffer->concat(TNC2DataFrame);
|
2021-09-16 11:51:02 +03:00
|
|
|
if (xQueueSend(tncToSendQueue, &buffer, (1000 / portTICK_PERIOD_MS)) != pdPASS)
|
|
|
|
{
|
2021-09-14 22:46:46 +03:00
|
|
|
delete buffer;
|
|
|
|
}
|
2021-09-16 11:51:02 +03:00
|
|
|
inTNCData->clear();
|
|
|
|
}
|
|
|
|
else if (character == (char) FEND && inTNCData->length() > 3)
|
|
|
|
{
|
|
|
|
const String &TNC2DataFrame = decode_kiss_pkt(*inTNCData, isDataFrame, isPacketFrame);
|
|
|
|
if (isDataFrame && !isPacketFrame) {
|
2021-09-13 21:11:10 +03:00
|
|
|
auto *buffer = new String();
|
|
|
|
buffer->concat(TNC2DataFrame);
|
2021-09-16 11:51:02 +03:00
|
|
|
if (xQueueSend(tncToSendQueue, &buffer, (1000 / portTICK_PERIOD_MS)) != pdPASS)
|
|
|
|
{
|
2021-09-13 21:11:10 +03:00
|
|
|
delete buffer;
|
|
|
|
}
|
2021-02-15 22:53:37 +02:00
|
|
|
}
|
2021-02-19 02:56:43 +02:00
|
|
|
inTNCData->clear();
|
|
|
|
}
|
2021-09-16 11:51:02 +03:00
|
|
|
}
|
2021-02-19 02:56:43 +02:00
|
|
|
if (inTNCData->length() > 255){
|
|
|
|
// just in case of garbage input reset data
|
|
|
|
inTNCData->clear();
|
2021-02-15 22:53:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-02-18 20:34:42 +02:00
|
|
|
[[noreturn]] void taskTNC(void *parameter) {
|
2021-02-15 22:53:37 +02:00
|
|
|
tncToSendQueue = xQueueCreate(4,sizeof(String *));
|
|
|
|
tncReceivedQueue = xQueueCreate(4,sizeof(String *));
|
|
|
|
String *loraReceivedFrameString = nullptr;
|
2021-02-18 21:37:05 +02:00
|
|
|
|
2021-02-15 22:53:37 +02:00
|
|
|
while (true) {
|
|
|
|
while (Serial.available() > 0) {
|
|
|
|
char character = Serial.read();
|
2021-02-19 02:56:43 +02:00
|
|
|
handleKISSData(character, 0);
|
2021-02-15 22:53:37 +02:00
|
|
|
}
|
|
|
|
#ifdef ENABLE_BLUETOOTH
|
2021-02-18 20:34:42 +02:00
|
|
|
if (SerialBT.hasClient()) {
|
|
|
|
while (SerialBT.available() > 0) {
|
|
|
|
char character = SerialBT.read();
|
2021-02-19 02:56:43 +02:00
|
|
|
handleKISSData(character, 1);
|
2021-02-18 20:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_WIFI
|
2021-09-13 21:11:10 +03:00
|
|
|
check_for_new_clients(&tncServer, clients, MAX_WIFI_CLIENTS);
|
2021-02-19 02:27:14 +02:00
|
|
|
|
2021-02-19 02:56:43 +02:00
|
|
|
iterateWifiClients([](WiFiClient * client, int clientIdx, const String * unused){
|
2021-02-18 21:37:05 +02:00
|
|
|
while (client->available() > 0) {
|
|
|
|
char character = client->read();
|
2021-09-13 21:11:10 +03:00
|
|
|
handleKISSData(character, 1);
|
2021-02-18 20:34:42 +02:00
|
|
|
}
|
2021-09-13 21:11:10 +03:00
|
|
|
}, nullptr, clients, MAX_WIFI_CLIENTS);
|
2021-02-15 22:53:37 +02:00
|
|
|
#endif
|
2021-09-13 21:11:10 +03:00
|
|
|
|
2021-02-15 22:53:37 +02:00
|
|
|
if (xQueueReceive(tncReceivedQueue, &loraReceivedFrameString, (1 / portTICK_PERIOD_MS)) == pdPASS) {
|
2021-09-13 21:11:10 +03:00
|
|
|
bool isPacketFrame = false;
|
2021-09-16 11:51:02 +03:00
|
|
|
const String &kissEncoded_pkt = encode_kiss_pkt(*loraReceivedFrameString, isPacketFrame);
|
2021-09-14 11:18:21 +03:00
|
|
|
const String &kissEncoded = encode_kiss(*loraReceivedFrameString, isPacketFrame);
|
2021-09-16 11:51:02 +03:00
|
|
|
if (isPacketFrame) {
|
|
|
|
Serial.print(kissEncoded);
|
2021-02-15 22:53:37 +02:00
|
|
|
#ifdef ENABLE_BLUETOOTH
|
|
|
|
if (SerialBT.hasClient()){
|
2021-02-18 20:34:42 +02:00
|
|
|
SerialBT.print(kissEncoded);
|
2021-02-15 22:53:37 +02:00
|
|
|
}
|
|
|
|
#endif
|
2021-02-18 20:34:42 +02:00
|
|
|
#ifdef ENABLE_WIFI
|
2021-02-19 02:56:43 +02:00
|
|
|
iterateWifiClients([](WiFiClient *client, int clientIdx, const String *data){
|
2021-02-18 21:37:05 +02:00
|
|
|
if (client->connected()){
|
|
|
|
client->print(*data);
|
|
|
|
client->flush();
|
|
|
|
}
|
2021-09-13 21:11:10 +03:00
|
|
|
}, &kissEncoded, clients, MAX_WIFI_CLIENTS);
|
2021-02-18 20:34:42 +02:00
|
|
|
#endif
|
2021-09-16 11:51:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Serial.print(kissEncoded_pkt);
|
|
|
|
#ifdef ENABLE_BLUETOOTH
|
|
|
|
if (SerialBT.hasClient()){
|
|
|
|
SerialBT.print(kissEncoded_pkt);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef ENABLE_WIFI
|
|
|
|
iterateWifiClients([](WiFiClient *client, int clientIdx, const String *data){
|
|
|
|
if (client->connected()){
|
|
|
|
client->print(*data);
|
|
|
|
client->flush();
|
|
|
|
}
|
|
|
|
}, &kissEncoded_pkt, clients, MAX_WIFI_CLIENTS);
|
|
|
|
#endif
|
|
|
|
}
|
2021-02-15 22:53:37 +02:00
|
|
|
delete loraReceivedFrameString;
|
|
|
|
}
|
|
|
|
vTaskDelay(50 / portTICK_PERIOD_MS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|