Skip to content

Commit

Permalink
first mod
Browse files Browse the repository at this point in the history
  • Loading branch information
richonguzman committed May 30, 2024
1 parent 12bcc76 commit 9343755
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 93 deletions.
19 changes: 12 additions & 7 deletions src/LoRa_APRS_iGate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
9 changes: 7 additions & 2 deletions src/aprs_is_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
54 changes: 30 additions & 24 deletions src/lora_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down
9 changes: 8 additions & 1 deletion src/lora_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@

#include <Arduino.h>

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(); // ???
Expand Down
113 changes: 56 additions & 57 deletions src/syslog_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <WiFi.h>
#include "configuration.h"
#include "syslog_utils.h"
#include "lora_utils.h"
#include "gps_utils.h"

extern Configuration Config;
Expand All @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion src/syslog_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
#define SYSLOG_H_

#include <Arduino.h>
#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();

}
Expand Down
7 changes: 6 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 9343755

Please sign in to comment.