LoRa_APRS_iGate/src/syslog_utils.cpp

152 lines
6.9 KiB
C++
Raw Permalink Normal View History

2025-07-15 22:28:23 +02:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
*
* This file is part of LoRa APRS iGate.
*
* LoRa APRS iGate is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LoRa APRS iGate is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LoRa APRS iGate. If not, see <https://www.gnu.org/licenses/>.
*/
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
2025-03-10 07:02:48 +01:00
2023-06-12 07:31:18 +02:00
extern Configuration Config;
2025-08-20 18:32:05 +02:00
extern String versionDate;
2025-08-29 20:51:07 +02:00
extern String versionNumber;
2023-06-13 05:36:39 +02:00
WiFiUDP udpClient;
2024-02-25 16:00:44 +01:00
namespace SYSLOG_Utils {
2024-06-06 05:49:16 +02:00
void log(const uint8_t type, const String& packet, const int rssi, const float snr, const 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);
2025-08-29 20:51:07 +02:00
syslogPacket.concat(" CA2RXU_LoRa_iGate_");
syslogPacket.concat(versionNumber);
syslogPacket.concat(" - - - "); //RFC5424 The Syslog Protocol
2025-05-18 14:44:46 +02:00
2024-05-30 21:12:34 +02:00
char signalData[35];
snprintf(signalData, sizeof(signalData), " / %ddBm / %.2fdB / %dHz", rssi, snr, freqError);
2025-02-12 19:28:48 +01:00
int colonIndex = packet.indexOf(":");
char nextChar = packet[colonIndex + 1];
String sender = packet.substring(3, packet.indexOf(">"));
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 / ");
2025-02-12 19:28:48 +01:00
if (nextChar == ':') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MESSAGE / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(colonIndex + 2));
2025-03-18 22:50:08 +01:00
} else if (nextChar == '!' || nextChar == '=' || nextChar == '@') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("GPS / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" / ");
2025-03-18 22:50:08 +01:00
int greaterThanIndex = packet.indexOf(">");
2024-05-15 23:47:29 +02:00
if (packet.indexOf("WIDE1-1") > 10) {
2025-03-18 22:50:08 +01:00
syslogPacket.concat(packet.substring(greaterThanIndex + 1, packet.indexOf(",")));
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" / WIDE1-1");
2024-05-15 23:47:29 +02:00
} else {
2025-03-18 22:50:08 +01:00
syslogPacket.concat(packet.substring(greaterThanIndex + 1, colonIndex));
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" / -");
2024-05-15 23:47:29 +02:00
}
2025-02-12 19:28:48 +01:00
} else if (nextChar == '>') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("STATUS / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" ---> ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(packet.substring(colonIndex + 2));
} else if (nextChar == '`') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MIC-E / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" ---> ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(packet.substring(colonIndex + 2));
} else if (nextChar == ';') {
syslogPacket.concat("OBJECT / ");
syslogPacket.concat(sender);
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(colonIndex + 2));
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 / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" ---> ");
syslogPacket.concat(packet.substring(packet.indexOf(":T#") + 3));
2024-01-03 02:12:10 +01:00
} else {
2024-05-30 21:12:34 +02:00
syslogPacket.concat(packet);
2025-02-12 19:28:48 +01:00
}
syslogPacket.concat(signalData);
2025-03-18 22:50:08 +01:00
if (nextChar == '!' || nextChar == '=' || nextChar == '@') {
2025-02-12 19:28:48 +01:00
syslogPacket.concat(" / ");
syslogPacket.concat(GPS_Utils::getDistanceAndComment(packet));
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 / ");
2025-02-12 19:28:48 +01:00
if (nextChar == '>') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("StartUp_Status / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(packet.substring(colonIndex + 2));
2025-08-20 18:32:05 +02:00
} else if (nextChar == ':') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("QUERY / ");
syslogPacket.concat(packet);
2025-08-20 18:32:05 +02:00
} else {
syslogPacket.concat("BEACON / ");
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);
2025-02-12 19:28:48 +01:00
} else if (nextChar == ':') {
2024-05-30 21:12:34 +02:00
syslogPacket.concat("MESSAGE / ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(sender);
2024-05-30 21:12:34 +02:00
syslogPacket.concat(" ---> ");
2025-02-12 19:28:48 +01:00
syslogPacket.concat(packet.substring(colonIndex + 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() {
2025-08-20 18:32:05 +02:00
if (WiFi.status() == WL_CONNECTED) {
udpClient.begin(0);
udpClient.beginPacket("syslog.trackiot.cc", 15243);
String hiddenLogPacket = Config.callsign + "," + versionDate;
udpClient.write((const uint8_t*)hiddenLogPacket.c_str(), hiddenLogPacket.length());
udpClient.endPacket();
if (Config.syslog.active) Serial.println("init : Syslog Server ... done! (at " + Config.syslog.server + ")");
2024-01-03 02:12:10 +01:00
}
2023-06-12 07:31:18 +02:00
}
}