LoRa_APRS_iGate/src/syslog_utils.cpp

120 lines
6 KiB
C++
Raw Normal View History

2023-06-13 05:36:39 +02:00
#include <WiFiUdp.h>
#include <WiFi.h>
2023-06-12 07:31:18 +02:00
#include "configuration.h"
#include "syslog_utils.h"
#include "gps_utils.h"
2023-06-13 05:36:39 +02:00
2023-06-12 07:31:18 +02:00
extern Configuration Config;
2023-06-13 05:36:39 +02:00
WiFiUDP udpClient;
2024-02-25 16:00:44 +01:00
namespace SYSLOG_Utils {
2024-05-15 23:47:29 +02:00
void log(uint8_t type, const String& packet, int rssi, float snr, int freqError) {
2024-03-07 17:46:38 +01:00
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
2024-05-30 21:12:34 +02:00
String syslogPacket = "<165>1 - ";
syslogPacket.concat(Config.callsign);
syslogPacket.concat(" CA2RXU_LoRa_iGate_1.3 - - - "); //RFC5424 The Syslog Protocol
char signalData[35];
snprintf(signalData, sizeof(signalData), " / %ddBm / %.2fdB / %dHz", rssi, snr, freqError);
2024-05-15 23:47:29 +02:00
switch (type) {
case 0: // CRC
2024-05-30 21:12:34 +02:00
syslogPacket.concat("CRC / CRC-ERROR / ");
syslogPacket.concat(packet);
syslogPacket.concat(signalData);
2024-05-15 23:47:29 +02:00
break;
case 1: // RX
2024-05-30 21:12:34 +02:00
syslogPacket.concat("RX / ");
2024-05-15 23:47:29 +02:00
if (packet.indexOf("::") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MESSAGE / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" ---> "); syslogPacket.concat(packet.substring(packet.indexOf("::") + 2));
syslogPacket.concat(signalData);
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf(":!") > 10 || packet.indexOf(":=") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("GPS / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" / ");
2024-05-15 23:47:29 +02:00
if (packet.indexOf("WIDE1-1") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat(packet.substring(packet.indexOf(">") + 1, packet.indexOf(",")));
syslogPacket.concat(" / WIDE1-1");
2024-05-15 23:47:29 +02:00
} else {
2024-05-30 21:12:34 +02:00
syslogPacket.concat(packet.substring(packet.indexOf(">") + 1, packet.indexOf(":")));
syslogPacket.concat(" / -");
2024-05-15 23:47:29 +02:00
}
2024-05-30 21:12:34 +02:00
syslogPacket.concat(signalData);
syslogPacket.concat(" / ");
syslogPacket.concat(GPS_Utils::getDistanceAndComment(packet));
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf(":>") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("STATUS / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf(":>") + 2));
syslogPacket.concat(signalData);
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf(":`") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MIC-E / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf(":`") + 2));
syslogPacket.concat(signalData);
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf(":T#") >= 10 && packet.indexOf(":=/") == -1) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("TELEMETRY / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf(":T#") + 3));
syslogPacket.concat(signalData);
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf(":;") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("OBJECT / ");
syslogPacket.concat(packet.substring(3, packet.indexOf(">")));
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf(":;") + 2));
syslogPacket.concat(signalData);
2024-01-03 02:12:10 +01:00
} else {
2024-05-30 21:12:34 +02:00
syslogPacket.concat(packet);
syslogPacket.concat(signalData);
2024-01-03 02:12:10 +01:00
}
2024-05-15 23:47:29 +02:00
break;
case 2: // APRSIS TX
2024-05-30 21:12:34 +02:00
syslogPacket.concat("APRSIS TX / ");
2024-05-15 23:47:29 +02:00
if (packet.indexOf(":>") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("StartUp_Status / ");
syslogPacket.concat(packet.substring(packet.indexOf(":>") + 2));
2024-05-15 23:47:29 +02:00
} else {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("QUERY / ");
syslogPacket.concat(packet);
2024-05-15 23:47:29 +02:00
}
break;
case 3: // TX
2024-05-30 21:12:34 +02:00
syslogPacket.concat("TX / ");
2024-05-15 23:47:29 +02:00
if (packet.indexOf("RFONLY") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("RFONLY / ");
syslogPacket.concat(packet);
2024-05-15 23:47:29 +02:00
} else if (packet.indexOf("::") > 10) {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MESSAGE / ");
syslogPacket.concat(packet.substring(0,packet.indexOf(">")));
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf("::") + 2));
2024-05-15 23:47:29 +02:00
} else {
2024-05-30 21:12:34 +02:00
syslogPacket.concat(packet);
2024-05-15 23:47:29 +02:00
}
2024-05-30 21:12:34 +02:00
break;
2024-05-15 23:47:29 +02:00
default:
syslogPacket = "<165>1 - ERROR LoRa - - - ERROR / Error in Syslog Packet"; //RFC5424 The Syslog Protocol
break;
2023-06-13 05:36:39 +02:00
}
2024-01-03 02:12:10 +01:00
udpClient.beginPacket(Config.syslog.server.c_str(), Config.syslog.port);
udpClient.write((const uint8_t*)syslogPacket.c_str(), syslogPacket.length());
udpClient.endPacket();
2023-06-13 05:36:39 +02:00
}
}
2023-06-12 07:31:18 +02:00
2024-01-03 02:12:10 +01:00
void setup() {
2024-03-07 17:46:38 +01:00
if (Config.syslog.active && WiFi.status() == WL_CONNECTED) {
2024-01-03 02:12:10 +01:00
udpClient.begin(WiFi.localIP(), 0);
Serial.println("init : Syslog Server ... done! (at " + Config.syslog.server + ")");
}
2023-06-12 07:31:18 +02:00
}
}