LoRa_APRS_iGate/src/aprs_is_utils.cpp

225 lines
9 KiB
C++
Raw Normal View History

2023-06-06 17:30:32 +02:00
#include <WiFi.h>
#include "configuration.h"
2023-06-08 06:58:10 +02:00
#include "aprs_is_utils.h"
2023-06-08 05:55:31 +02:00
#include "station_utils.h"
2023-06-08 06:58:10 +02:00
#include "query_utils.h"
#include "lora_utils.h"
#include "display.h"
2023-06-09 07:12:13 +02:00
#include "utils.h"
2023-06-06 17:30:32 +02:00
extern Configuration Config;
extern WiFiClient espClient;
extern int internalLedPin;
2023-06-08 05:55:31 +02:00
extern uint32_t lastScreenOn;
2023-06-07 23:25:50 +02:00
extern int stationMode;
2023-06-08 05:55:31 +02:00
extern String firstLine;
extern String secondLine;
2023-06-09 07:12:13 +02:00
extern String thirdLine;
extern String fourthLine;
2023-06-17 01:08:25 +02:00
extern String fifthLine;
extern String sixthLine;
extern String seventhLine;
2023-06-18 16:56:53 +02:00
2023-06-06 17:30:32 +02:00
namespace APRS_IS_Utils {
2023-10-08 14:39:44 +02:00
void connect(){
int count = 0;
String aprsauth;
Serial.print("Connecting to APRS-IS ... ");
while (!espClient.connect(Config.aprs_is.server.c_str(), Config.aprs_is.port) && count < 20) {
Serial.println("Didn't connect with server...");
delay(1000);
espClient.stop();
espClient.flush();
Serial.println("Run client.stop");
Serial.println("Trying to connect with Server: " + String(Config.aprs_is.server) + " AprsServerPort: " + String(Config.aprs_is.port));
count++;
Serial.println("Try: " + String(count));
}
if (count == 20) {
Serial.println("Tried: " + String(count) + " FAILED!");
} else {
Serial.println("Connected!\n(Server: " + String(Config.aprs_is.server) + " / Port: " + String(Config.aprs_is.port) +")");
2023-11-26 00:41:59 +01:00
aprsauth = "user " + Config.callsign + " pass " + Config.aprs_is.passcode + " vers CA2RXU_LoRa_iGate 1.2 filter t/m/" + Config.callsign + "/" + (String)Config.aprs_is.reportingDistance + "\n\r";
2023-10-08 14:39:44 +02:00
espClient.write(aprsauth.c_str());
delay(200);
}
2023-06-06 17:30:32 +02:00
}
2023-10-08 14:39:44 +02:00
void checkStatus() {
String wifiState, aprsisState;
if (WiFi.status() == WL_CONNECTED) {
wifiState = "OK";
} else {
wifiState = "--";
if (!Config.display.alwaysOn) {
display_toggle(true);
}
lastScreenOn = millis();
2023-06-06 18:37:22 +02:00
}
2023-10-08 14:39:44 +02:00
if (espClient.connected()) {
aprsisState = "OK";
} else {
aprsisState = "--";
if (!Config.display.alwaysOn) {
display_toggle(true);
}
lastScreenOn = millis();
2023-06-06 18:37:22 +02:00
}
2023-10-08 14:39:44 +02:00
secondLine = "WiFi: " + wifiState + "/ APRS-IS: " + aprsisState;
2023-06-06 18:37:22 +02:00
}
2023-10-08 14:39:44 +02:00
String createPacket(String packet) {
2023-11-28 04:57:15 +01:00
if (stationMode>1) {
2023-12-08 21:04:05 +01:00
return packet.substring(3, packet.indexOf(":`")) + ",qAR," + Config.callsign + packet.substring(packet.indexOf(":`")) + "\n";
2023-10-08 14:39:44 +02:00
} else {
2023-12-08 21:04:05 +01:00
return packet.substring(3, packet.indexOf(":`")) + ",qAO," + Config.callsign + packet.substring(packet.indexOf(":`")) + "\n";
2023-10-08 14:39:44 +02:00
}
2023-06-06 21:53:06 +02:00
}
2023-06-08 03:25:31 +02:00
2023-10-08 14:39:44 +02:00
void processLoRaPacket(String packet) {
bool queryMessage = false;
String aprsPacket, Sender, AddresseeAndMessage, Addressee, ackMessage, receivedMessage;
if (packet != "") {
#ifdef PinPointApp
Serial.println(packet.substring(3));
#else
Serial.print("Received Lora Packet : " + String(packet));
#endif
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.indexOf("TCPIP") == -1) && (packet.indexOf("NOGATE") == -1) && (packet.indexOf("RFONLY") == -1)) {
#ifndef PinPointApp
Serial.print(" ---> APRS LoRa Packet!");
#endif
Sender = packet.substring(3,packet.indexOf(">"));
if (Sender != Config.callsign) { // avoid listening yourself by digirepeating
2023-11-28 04:57:15 +01:00
if (stationMode==2 || stationMode==5) {
2023-10-08 14:39:44 +02:00
if (packet.indexOf("::") > 10) { // its a Message!
AddresseeAndMessage = packet.substring(packet.indexOf("::")+2);
Addressee = AddresseeAndMessage.substring(0,AddresseeAndMessage.indexOf(":"));
Addressee.trim();
if (Addressee == Config.callsign) { // its for me!
if (AddresseeAndMessage.indexOf("{")>0) { // ack?
ackMessage = "ack" + AddresseeAndMessage.substring(AddresseeAndMessage.indexOf("{")+1);
ackMessage.trim();
delay(4000);
//Serial.println(ackMessage);
for(int i = Sender.length(); i < 9; i++) {
Sender += ' ';
}
2023-10-10 03:12:29 +02:00
LoRa_Utils::sendNewPacket("APRS", Config.callsign + ">APLRG1,RFONLY,WIDE1-1::" + Sender + ":" + ackMessage);
2023-10-08 14:39:44 +02:00
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1, AddresseeAndMessage.indexOf("{"));
} else {
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1);
2023-06-08 05:55:31 +02:00
}
2023-10-08 14:39:44 +02:00
if (receivedMessage.indexOf("?") == 0) {
queryMessage = true;
delay(2000);
if (!Config.display.alwaysOn) {
display_toggle(true);
}
LoRa_Utils::sendNewPacket("APRS", QUERY_Utils::process(receivedMessage, Sender, "LoRa"));
lastScreenOn = millis();
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, "Callsign = " + Sender, "TYPE --> QUERY", 0);
2023-06-08 05:55:31 +02:00
}
}
}
}
2023-10-08 14:39:44 +02:00
if (!queryMessage) {
aprsPacket = createPacket(packet);
if (!Config.display.alwaysOn) {
display_toggle(true);
}
lastScreenOn = millis();
espClient.write(aprsPacket.c_str());
#ifndef PinPointApp
Serial.println(" ---> Uploaded to APRS-IS");
#endif
STATION_Utils::updateLastHeard(Sender);
Utils::typeOfPacket(aprsPacket, "LoRa-APRS");
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
2023-06-08 05:55:31 +02:00
}
2023-10-08 14:39:44 +02:00
}
} else {
#ifndef PinPointApp
Serial.println(" ---> LoRa Packet Ignored (first 3 bytes or TCPIP/NOGATE/RFONLY)\n");
#endif
}
2023-06-08 05:55:31 +02:00
}
}
2023-06-06 21:53:06 +02:00
2023-10-08 14:39:44 +02:00
void processAPRSISPacket(String packet) {
String Sender, AddresseeAndMessage, Addressee, receivedMessage;
if (!packet.startsWith("#")){
if (packet.indexOf("::")>0) {
Sender = packet.substring(0,packet.indexOf(">"));
AddresseeAndMessage = packet.substring(packet.indexOf("::")+2);
Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":"));
Addressee.trim();
if (Addressee == Config.callsign) { // its for me!
if (AddresseeAndMessage.indexOf("{")>0) { // ack?
String ackMessage = "ack" + AddresseeAndMessage.substring(AddresseeAndMessage.indexOf("{")+1);
ackMessage.trim();
delay(4000);
//Serial.println(ackMessage);
for(int i = Sender.length(); i < 9; i++) {
Sender += ' ';
}
String ackPacket = Config.callsign + ">APLRG1,TCPIP,qAC::" + Sender + ":" + ackMessage + "\n";
espClient.write(ackPacket.c_str());
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1, AddresseeAndMessage.indexOf("{"));
} else {
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":")+1);
}
if (receivedMessage.indexOf("?") == 0) {
#ifndef PinPointApp
Serial.println("Received Query APRS-IS : " + packet);
#endif
String queryAnswer = QUERY_Utils::process(receivedMessage, Sender, "APRSIS");
//Serial.println("---> QUERY Answer : " + queryAnswer.substring(0,queryAnswer.indexOf("\n")));
if (!Config.display.alwaysOn) {
display_toggle(true);
}
lastScreenOn = millis();
delay(500);
espClient.write(queryAnswer.c_str());
fifthLine = "APRS-IS ----> APRS-IS";
sixthLine = Config.callsign;
for (int j=sixthLine.length();j<9;j++) {
sixthLine += " ";
}
sixthLine += "> " + Sender;
seventhLine = "QUERY = " + receivedMessage;
2023-06-09 07:12:13 +02:00
}
} else {
2023-10-08 14:39:44 +02:00
#ifndef PinPointApp
Serial.print("Received from APRS-IS : " + packet);
#endif
if ((stationMode==2 || stationMode==5) && STATION_Utils::wasHeard(Addressee)) {
LoRa_Utils::sendNewPacket("APRS", LoRa_Utils::generatePacket(packet));
2023-06-09 07:12:13 +02:00
display_toggle(true);
2023-10-08 14:39:44 +02:00
lastScreenOn = millis();
Utils::typeOfPacket(packet, "APRS-LoRa");
2023-06-09 07:12:13 +02:00
}
}
2023-10-08 14:39:44 +02:00
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
}
}
2023-06-09 07:12:13 +02:00
}
2023-10-08 14:39:44 +02:00
void loop() {
checkStatus();
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
while (espClient.connected()) {
Utils::checkDisplayInterval();
Utils::checkBeaconInterval();
processLoRaPacket(LoRa_Utils::receivePacket());
if (espClient.available()) {
String aprsisPacket;
aprsisPacket.concat(espClient.readStringUntil('\r'));
processAPRSISPacket(aprsisPacket);
}
2023-07-31 05:53:59 +02:00
}
}
2023-06-06 17:30:32 +02:00
}