From 93437558eedd941b764bc7ee7980bdf62ad1d9a4 Mon Sep 17 00:00:00 2001 From: richonguzman Date: Thu, 30 May 2024 09:11:09 -0400 Subject: [PATCH] first mod --- src/LoRa_APRS_iGate.cpp | 19 ++++--- src/aprs_is_utils.cpp | 9 +++- src/lora_utils.cpp | 54 ++++++++++--------- src/lora_utils.h | 9 +++- src/syslog_utils.cpp | 113 ++++++++++++++++++++-------------------- src/syslog_utils.h | 3 +- src/utils.cpp | 7 ++- 7 files changed, 121 insertions(+), 93 deletions(-) diff --git a/src/LoRa_APRS_iGate.cpp b/src/LoRa_APRS_iGate.cpp index 547586be..c7e907d7 100644 --- a/src/LoRa_APRS_iGate.cpp +++ b/src/LoRa_APRS_iGate.cpp @@ -152,25 +152,30 @@ void loop() { APRS_IS_Utils::checkStatus(); // Need that to update display, maybe split this and send APRSIS status to display func? - String packet = ""; + ReceivedLoRaPacket receivedLoRaPacket; + receivedLoRaPacket.packet = ""; if (Config.loramodule.rxActive) { - packet = LoRa_Utils::receivePacket(); // We need to fetch LoRa packet above APRSIS and Digi + receivedLoRaPacket = LoRa_Utils::receivePacket(); // We need to fetch LoRa packet above APRSIS and Digi } - if (packet != "") { + if (receivedLoRaPacket.packet != "") { if (Config.aprs_is.active) { // If APRSIS enabled - APRS_IS_Utils::processLoRaPacket(packet); // Send received packet to APRSIS + APRS_IS_Utils::processLoRaPacket(receivedLoRaPacket.packet); // Send received packet to APRSIS + } + + if (Config.syslog.active && WiFi.status() == WL_CONNECTED) { + SYSLOG_Utils::log(1, receivedLoRaPacket); // RX } if (Config.digi.mode == 2 || backUpDigiMode) { // If Digi enabled - DIGI_Utils::processLoRaPacket(packet); // Send received packet to Digi + DIGI_Utils::processLoRaPacket(receivedLoRaPacket.packet); // Send received packet to Digi } if (Config.tnc.enableServer) { // If TNC server enabled - TNC_Utils::sendToClients(packet); // Send received packet to TNC KISS + TNC_Utils::sendToClients(receivedLoRaPacket.packet); // Send received packet to TNC KISS } if (Config.tnc.enableSerial) { // If Serial KISS enabled - TNC_Utils::sendToSerial(packet); // Send received packet to Serial KISS + TNC_Utils::sendToSerial(receivedLoRaPacket.packet); // Send received packet to Serial KISS } } diff --git a/src/aprs_is_utils.cpp b/src/aprs_is_utils.cpp index 4b22fbe5..14952e32 100644 --- a/src/aprs_is_utils.cpp +++ b/src/aprs_is_utils.cpp @@ -261,8 +261,13 @@ namespace APRS_IS_Utils { A7670_Utils::uploadToAPRSIS(queryAnswer); #else upload(queryAnswer); - #endif - SYSLOG_Utils::log(2, queryAnswer, 0, 0, 0); // APRSIS TX + #endif + ReceivedLoRaPacket queryTx; + queryTx.packet = queryAnswer; + queryTx.rssi = 0; + queryTx.snr = 0; + queryTx.freqError = 0; + SYSLOG_Utils::log(2, queryTx); // APRSIS TX fifthLine = "APRS-IS ----> APRS-IS"; sixthLine = Config.callsign; for (int j = sixthLine.length();j < 9;j++) { diff --git a/src/lora_utils.cpp b/src/lora_utils.cpp index f77bd792..cb1b94e3 100644 --- a/src/lora_utils.cpp +++ b/src/lora_utils.cpp @@ -4,6 +4,7 @@ #include "aprs_is_utils.h" #include "boards_pinout.h" #include "syslog_utils.h" +#include "lora_utils.h" #include "display.h" #include "utils.h" @@ -118,7 +119,12 @@ namespace LoRa_Utils { transmitFlag = true; if (state == RADIOLIB_ERR_NONE) { if (Config.syslog.active && WiFi.status() == WL_CONNECTED) { - SYSLOG_Utils::log(3, newPacket, 0, 0, 0); // TX + ReceivedLoRaPacket txPacket; + txPacket.packet = newPacket; + txPacket.rssi = 0; + txPacket.snr = 0; + txPacket.freqError = 0; + SYSLOG_Utils::log(3, txPacket); // TX } Utils::print("---> LoRa Packet Tx : "); Utils::println(newPacket); @@ -152,58 +158,58 @@ namespace LoRa_Utils { radio.startReceive(); } - String receivePacket() { - String packet = ""; + ReceivedLoRaPacket receivePacket() { + ReceivedLoRaPacket receivedLoraPacket; + String rxPacket = ""; if (operationDone) { operationDone = false; if (transmitFlag) { radio.startReceive(); transmitFlag = false; } else { - int state = radio.readData(packet); + int state = radio.readData(rxPacket); if (state == RADIOLIB_ERR_NONE) { - if (packet != "") { - rssi = radio.getRSSI(); - snr = radio.getSNR(); - freqError = radio.getFrequencyError(); - Utils::println("<--- LoRa Packet Rx : " + packet.substring(3)); - Utils::println("(RSSI:" + String(rssi) + " / SNR:" + String(snr) + " / FreqErr:" + String(freqError) + ")"); + if(!rxPacket.isEmpty()) { + receivedLoraPacket.packet = rxPacket; + receivedLoraPacket.rssi = radio.getRSSI(); + receivedLoraPacket.snr = radio.getSNR(); + receivedLoraPacket.freqError = radio.getFrequencyError(); + Utils::println("<--- LoRa Packet Rx : " + rxPacket.substring(3)); + Utils::println("(RSSI:" + String(receivedLoraPacket.rssi) + " / SNR:" + String(receivedLoraPacket.snr) + " / FreqErr:" + String(receivedLoraPacket.freqError) + ")"); if (!Config.lowPowerMode) { ReceivedPacket receivedPacket; receivedPacket.millis = millis(); - receivedPacket.packet = packet.substring(3); - receivedPacket.RSSI = rssi; - receivedPacket.SNR = snr; + receivedPacket.packet = rxPacket.substring(3); + receivedPacket.RSSI = receivedLoraPacket.rssi; + receivedPacket.SNR = receivedLoraPacket.snr; if (receivedPackets.size() >= 20) { receivedPackets.erase(receivedPackets.begin()); } receivedPackets.push_back(receivedPacket); } - if (Config.syslog.active && WiFi.status() == WL_CONNECTED) { - SYSLOG_Utils::log(1, packet, rssi, snr, freqError); // RX - } lastRxTime = millis(); - return packet; + return receivedLoraPacket; } } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { - rssi = radio.getRSSI(); - snr = radio.getSNR(); - freqError = radio.getFrequencyError(); + receivedLoraPacket.packet = rxPacket.substring(3); + receivedLoraPacket.rssi = radio.getRSSI(); + receivedLoraPacket.snr = radio.getSNR(); + receivedLoraPacket.freqError = radio.getFrequencyError(); Utils::println(F("CRC error!")); if (Config.syslog.active && WiFi.status() == WL_CONNECTED) { - SYSLOG_Utils::log(0, packet, rssi, snr, freqError); // CRC + SYSLOG_Utils::log(0, receivedLoraPacket); // CRC } - packet = ""; + receivedLoraPacket.packet = ""; } else { Utils::print(F("failed, code ")); Utils::println(String(state)); - packet = ""; + receivedLoraPacket.packet = ""; } } } - return packet; + return receivedLoraPacket; } void sleepRadio() { diff --git a/src/lora_utils.h b/src/lora_utils.h index dc5378df..9a98628a 100644 --- a/src/lora_utils.h +++ b/src/lora_utils.h @@ -3,13 +3,20 @@ #include +struct ReceivedLoRaPacket { + String packet; + int rssi; + float snr; + int freqError; +}; + namespace LoRa_Utils { void setup(); void sendNewPacket(const String& newPacket); String packetSanitization(const String& packet); - String receivePacket(); + ReceivedLoRaPacket receivePacket(); void changeFreqTx(); void changeFreqRx(); void startReceive(); // ??? diff --git a/src/syslog_utils.cpp b/src/syslog_utils.cpp index c5d67705..dea18e62 100644 --- a/src/syslog_utils.cpp +++ b/src/syslog_utils.cpp @@ -2,6 +2,7 @@ #include #include "configuration.h" #include "syslog_utils.h" +#include "lora_utils.h" #include "gps_utils.h" extern Configuration Config; @@ -11,67 +12,65 @@ WiFiUDP udpClient; namespace SYSLOG_Utils { - void log(uint8_t type, const String& packet, int rssi, float snr, int freqError) { + void log(uint8_t type, ReceivedLoRaPacket rxPacket) { String syslogPacket = "<165>1 - " + Config.callsign + " CA2RXU_LoRa_iGate_1.3" + " - - - "; //RFC5424 The Syslog Protocol - if (Config.syslog.active && WiFi.status() == WL_CONNECTED) { - switch (type) { - case 0: // CRC - syslogPacket += type + " / CRC-ERROR / " + packet; - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - break; - case 1: // RX - if (packet.indexOf("::") > 10) { - syslogPacket += type + " / MESSAGE / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf("::")+2); - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } else if (packet.indexOf(":!") > 10 || packet.indexOf(":=") > 10) { - syslogPacket += type + " / GPS / " + packet.substring(3,packet.indexOf(">")) + " / "; - if (packet.indexOf("WIDE1-1") > 10) { - syslogPacket += packet.substring(packet.indexOf(">")+1,packet.indexOf(",")) + " / WIDE1-1 / "; - } else { - syslogPacket += packet.substring(packet.indexOf(">")+1,packet.indexOf(":")) + " / - / "; - } - syslogPacket += String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz / " + GPS_Utils::getDistanceAndComment(packet); - } else if (packet.indexOf(":>") > 10) { - syslogPacket += type + " / STATUS / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":>")+2); - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } else if (packet.indexOf(":`") > 10) { - syslogPacket += type + " / MIC-E / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":`")+2); - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } else if (packet.indexOf(":T#") >= 10 && packet.indexOf(":=/") == -1) { - syslogPacket += type + " / TELEMETRY / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":T#")+3); - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } else if (packet.indexOf(":;") > 10) { - syslogPacket += type + " / OBJECT / " + packet.substring(3,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf(":;")+2); - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } else { - syslogPacket += type + " / " + packet; - syslogPacket += " / " + String(rssi) + "dBm / " + String(snr) + "dB / " + String(freqError) + "Hz"; - } - break; - case 2: // APRSIS TX - if (packet.indexOf(":>") > 10) { - syslogPacket += type + " / StartUp_Status / " + packet.substring(packet.indexOf(":>")+2); - } else { - syslogPacket += type + " / QUERY / " + packet; - } - break; - case 3: // TX - if (packet.indexOf("RFONLY") > 10) { - syslogPacket += type + " / RFONLY / " + packet; - } else if (packet.indexOf("::") > 10) { - syslogPacket += type + " / MESSAGE / " + packet.substring(0,packet.indexOf(">")) + " ---> " + packet.substring(packet.indexOf("::")+2); + switch (type) { + case 0: // CRC + syslogPacket += type + " / CRC-ERROR / " + rxPacket.packet; + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + break; + case 1: // RX + if (rxPacket.packet.indexOf("::") > 10) { + syslogPacket += type + " / MESSAGE / " + rxPacket.packet.substring(3, rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf("::")+2); + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } else if (rxPacket.packet.indexOf(":!") > 10 || rxPacket.packet.indexOf(":=") > 10) { + syslogPacket += type + " / GPS / " + rxPacket.packet.substring(3,rxPacket.packet.indexOf(">")) + " / "; + if (rxPacket.packet.indexOf("WIDE1-1") > 10) { + syslogPacket += rxPacket.packet.substring(rxPacket.packet.indexOf(">")+1,rxPacket.packet.indexOf(",")) + " / WIDE1-1 / "; } else { - syslogPacket += type + " / " + packet; + syslogPacket += rxPacket.packet.substring(rxPacket.packet.indexOf(">")+1,rxPacket.packet.indexOf(":")) + " / - / "; } - break; - default: - syslogPacket = "<165>1 - ERROR LoRa - - - ERROR / Error in Syslog Packet"; //RFC5424 The Syslog Protocol - break; - } - udpClient.beginPacket(Config.syslog.server.c_str(), Config.syslog.port); - udpClient.write((const uint8_t*)syslogPacket.c_str(), syslogPacket.length()); - udpClient.endPacket(); + syslogPacket += String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz / " + GPS_Utils::getDistanceAndComment(rxPacket.packet); + } else if (rxPacket.packet.indexOf(":>") > 10) { + syslogPacket += type + " / STATUS / " + rxPacket.packet.substring(3,rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf(":>")+2); + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } else if (rxPacket.packet.indexOf(":`") > 10) { + syslogPacket += type + " / MIC-E / " + rxPacket.packet.substring(3,rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf(":`")+2); + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } else if (rxPacket.packet.indexOf(":T#") >= 10 && rxPacket.packet.indexOf(":=/") == -1) { + syslogPacket += type + " / TELEMETRY / " + rxPacket.packet.substring(3,rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf(":T#")+3); + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } else if (rxPacket.packet.indexOf(":;") > 10) { + syslogPacket += type + " / OBJECT / " + rxPacket.packet.substring(3,rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf(":;")+2); + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } else { + syslogPacket += type + " / " + rxPacket.packet; + syslogPacket += " / " + String(rxPacket.rssi) + "dBm / " + String(rxPacket.snr) + "dB / " + String(rxPacket.freqError) + "Hz"; + } + break; + case 2: // APRSIS TX + if (rxPacket.packet.indexOf(":>") > 10) { + syslogPacket += type + " / StartUp_Status / " + rxPacket.packet.substring(rxPacket.packet.indexOf(":>")+2); + } else { + syslogPacket += type + " / QUERY / " + rxPacket.packet; + } + break; + case 3: // TX + if (rxPacket.packet.indexOf("RFONLY") > 10) { + syslogPacket += type + " / RFONLY / " + rxPacket.packet; + } else if (rxPacket.packet.indexOf("::") > 10) { + syslogPacket += type + " / MESSAGE / " + rxPacket.packet.substring(0,rxPacket.packet.indexOf(">")) + " ---> " + rxPacket.packet.substring(rxPacket.packet.indexOf("::")+2); + } else { + syslogPacket += type + " / " + rxPacket.packet; + } + break; + default: + syslogPacket = "<165>1 - ERROR LoRa - - - ERROR / Error in Syslog Packet"; //RFC5424 The Syslog Protocol + break; } + udpClient.beginPacket(Config.syslog.server.c_str(), Config.syslog.port); + udpClient.write((const uint8_t*)syslogPacket.c_str(), syslogPacket.length()); + udpClient.endPacket(); } void setup() { diff --git a/src/syslog_utils.h b/src/syslog_utils.h index a60d0a24..0bfd44af 100644 --- a/src/syslog_utils.h +++ b/src/syslog_utils.h @@ -2,11 +2,12 @@ #define SYSLOG_H_ #include +#include "lora_utils.h" namespace SYSLOG_Utils { - void log(uint8_t type ,const String& packet, int rssi, float snr, int freqError); + void log(uint8_t type, ReceivedLoRaPacket rxPacket); void setup(); } diff --git a/src/utils.cpp b/src/utils.cpp index fbb6ffd4..1b338e0d 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -51,7 +51,12 @@ namespace Utils { delay(1000); status += ",qAC:>https://github.com/richonguzman/LoRa_APRS_iGate " + versionDate; APRS_IS_Utils::upload(status); - SYSLOG_Utils::log(2, status,0,0,0); // APRSIS TX + ReceivedLoRaPacket txPacket; + txPacket.packet = status; + txPacket.rssi = 0; + txPacket.snr = 0; + txPacket.freqError = 0; + SYSLOG_Utils::log(2, txPacket); // APRSIS TX statusAfterBoot = false; } if (statusAfterBoot && !Config.beacon.sendViaAPRSIS && Config.beacon.sendViaRF) {