LoRa_APRS_iGate/src/tnc_utils.cpp

144 lines
3.9 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"
2024-03-17 12:21:11 +01:00
#include "configuration.h"
#include "station_utils.h"
2024-03-17 12:21:11 +01:00
#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 {
2024-04-04 20:54:49 +02:00
2024-03-09 16:25:23 +01:00
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 19:28:08 +01:00
void handleInputData(char character, int bufferIndex) {
String* data;
if (bufferIndex == -1) {
data = &inputSerialBuffer;
} else {
data = &inputServerBuffer[bufferIndex];
}
if (data->length() == 0 && character != (char)FEND) {
2024-03-09 16:25:23 +01:00
return;
}
2024-03-17 19:28:08 +01:00
data->concat(character);
2024-03-09 16:25:23 +01:00
2024-03-17 19:28:08 +01:00
if (character == (char)FEND && data->length() > 3) {
2024-03-09 16:25:23 +01:00
bool isDataFrame = false;
2024-03-17 19:28:08 +01:00
const String& frame = decodeKISS(*data, isDataFrame);
2024-03-09 16:25:23 +01:00
if (isDataFrame) {
2024-03-17 19:28:08 +01:00
if (bufferIndex != -1) {
Utils::print("<--- Got from TNC : ");
Utils::println(frame);
2024-03-17 12:21:11 +01:00
}
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) {
STATION_Utils::addToOutputPacketBuffer(frame);
2024-03-17 19:28:08 +01:00
} else {
Utils::println("Ignored own frame from KISS");
2024-03-17 12:21:11 +01:00
}
}
2024-03-17 19:28:08 +01:00
data->clear();
2024-03-17 12:21:11 +01:00
}
2024-03-17 19:28:08 +01:00
if (data->length() > 255) {
data->clear();
2024-03-17 12:21:11 +01:00
}
}
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 19:28:08 +01:00
handleInputData(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();
2024-03-17 19:28:08 +01:00
handleInputData(character, -1);
2024-03-17 12:21:11 +01:00
}
}
2024-05-15 22:41:07 +02:00
void sendToClients(const String& packet) {
String cleanPacket = packet.substring(3);
2024-03-09 16:25:23 +01:00
2024-05-15 22:41:07 +02:00
const String kissEncoded = encodeKISS(cleanPacket);
2024-03-09 16:25:23 +01:00
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 : ");
2024-05-15 22:41:07 +02:00
Utils::println(cleanPacket);
2024-03-17 12:21:11 +01:00
}
2024-05-15 22:41:07 +02:00
void sendToSerial(const String& packet) {
String cleanPacket = packet.substring(3);
Serial.print(encodeKISS(cleanPacket));
2024-03-17 12:21:11 +01:00
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();
readFromClients();
}
if (Config.tnc.enableSerial) {
readFromSerial();
}
2024-03-09 16:25:23 +01:00
}
}