LoRa_APRS_iGate/src/tnc_utils.cpp

173 lines
4.5 KiB
C++
Raw Normal View History

2024-03-09 16:25:23 +01:00
#include <WiFi.h>
#include "kiss_utils.h"
#include "kiss_protocol.h"
#include "lora_utils.h"
2024-03-17 12:21:11 +01:00
#include "configuration.h"
#include "utils.h"
extern Configuration Config;
2024-03-09 16:25:23 +01:00
#define MAX_CLIENTS 4
2024-03-17 12:21:11 +01:00
#define INPUT_BUFFER_SIZE (2 + MAX_CLIENTS)
2024-03-09 16:25:23 +01:00
#define TNC_PORT 8001
WiFiClient* clients[MAX_CLIENTS];
WiFiServer tncServer(TNC_PORT);
2024-03-17 12:21:11 +01:00
String inputServerBuffer[INPUT_BUFFER_SIZE];
String inputSerialBuffer = "";
2024-03-09 16:25:23 +01:00
namespace TNC_Utils {
void setup() {
2024-03-17 12:21:11 +01:00
if (Config.tnc.enableServer) {
tncServer.stop();
tncServer.begin();
}
2024-03-09 16:25:23 +01:00
}
void checkNewClients() {
WiFiClient new_client = tncServer.available();
if (new_client.connected()) {
for (int i = 0; i < MAX_CLIENTS; i++) {
WiFiClient* client = clients[i];
if (client == nullptr) {
clients[i] = new WiFiClient(new_client);
2024-03-17 12:21:11 +01:00
Utils::println("New TNC client connected");
2024-03-09 16:25:23 +01:00
break;
}
}
}
}
2024-03-17 12:21:11 +01:00
void handleServerInputData(char character, int bufferIndex) {
String* inTNCData = &inputServerBuffer[bufferIndex];
2024-03-09 16:25:23 +01:00
if (inTNCData->length() == 0 && character != (char)FEND) {
return;
}
inTNCData->concat(character);
if (character == (char)FEND && inTNCData->length() > 3) {
bool isDataFrame = false;
const String& frame = decodeKISS(*inTNCData, isDataFrame);
if (isDataFrame) {
2024-03-17 12:21:11 +01:00
Utils::print("<--- Got from TNC : ");
Utils::println(frame);
2024-03-09 16:25:23 +01:00
2024-03-17 12:21:11 +01:00
String sender = frame.substring(0,frame.indexOf(">"));
if (Config.tnc.acceptOwn || sender != Config.callsign) {
LoRa_Utils::sendNewPacket("APRS", frame);
} else {
Utils::println("Ignored own frame from TNC");
}
2024-03-09 16:25:23 +01:00
}
inTNCData->clear();
}
if (inTNCData->length() > 255) {
inTNCData->clear();
}
}
2024-03-17 12:21:11 +01:00
void handleSerialInputData(char character) {
if (inputSerialBuffer.length() == 0 && character != (char)FEND) {
return;
}
inputSerialBuffer.concat(character);
if (character == (char)FEND && inputSerialBuffer.length() > 3) {
bool isDataFrame = false;
const String& frame = decodeKISS(inputSerialBuffer, isDataFrame);
if (isDataFrame) {
String sender = frame.substring(0,frame.indexOf(">"));
if (Config.tnc.acceptOwn || sender != Config.callsign) {
LoRa_Utils::sendNewPacket("APRS", frame);
}
}
inputSerialBuffer.clear();
}
if (inputSerialBuffer.length() > 255) {
inputSerialBuffer.clear();
}
}
2024-03-09 16:25:23 +01:00
void readFromClients() {
for (int i = 0; i < MAX_CLIENTS; i++) {
auto client = clients[i];
if (client != nullptr) {
if (client->connected()) {
while (client->available() > 0) {
char character = client->read();
2024-03-17 12:21:11 +01:00
handleServerInputData(character, 2 + i);
2024-03-09 16:25:23 +01:00
}
} else {
delete client;
clients[i] = nullptr;
}
}
}
}
2024-03-17 12:21:11 +01:00
void readFromSerial() {
while (Serial.available() > 0) {
char character = Serial.read();
handleSerialInputData(character);
}
}
2024-03-09 16:25:23 +01:00
void sendToClients(String packet) {
packet = packet.substring(3);
const String kissEncoded = encodeKISS(packet);
for (int i = 0; i < MAX_CLIENTS; i++) {
auto client = clients[i];
if (client != nullptr) {
if (client->connected()) {
client->print(kissEncoded);
client->flush();
} else {
delete client;
clients[i] = nullptr;
}
}
}
2024-03-17 12:21:11 +01:00
Utils::print("---> Sent to TNC : ");
Utils::println(packet);
}
void sendToSerial(String packet) {
packet = packet.substring(3);
Serial.print(encodeKISS(packet));
Serial.flush();
2024-03-09 16:25:23 +01:00
}
void loop() {
2024-03-17 12:21:11 +01:00
if (Config.tnc.enableServer) {
checkNewClients();
2024-03-09 16:25:23 +01:00
2024-03-17 12:21:11 +01:00
readFromClients();
}
if (Config.tnc.enableSerial) {
readFromSerial();
}
2024-03-09 16:25:23 +01:00
}
}