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"
|
2024-01-22 13:11:01 +01:00
|
|
|
#include "syslog_utils.h"
|
2023-06-08 06:58:10 +02:00
|
|
|
#include "query_utils.h"
|
2024-01-12 05:36:04 +01:00
|
|
|
#include "digi_utils.h"
|
2023-06-08 06:58:10 +02:00
|
|
|
#include "display.h"
|
2023-06-09 07:12:13 +02:00
|
|
|
#include "utils.h"
|
2023-06-06 17:30:32 +02:00
|
|
|
|
2024-04-20 15:27:20 +02:00
|
|
|
extern Configuration Config;
|
|
|
|
|
extern WiFiClient espClient;
|
|
|
|
|
extern uint32_t lastScreenOn;
|
|
|
|
|
extern String firstLine;
|
|
|
|
|
extern String secondLine;
|
|
|
|
|
extern String thirdLine;
|
|
|
|
|
extern String fourthLine;
|
|
|
|
|
extern String fifthLine;
|
|
|
|
|
extern String sixthLine;
|
|
|
|
|
extern String seventhLine;
|
|
|
|
|
|
|
|
|
|
extern std::vector<String> outputPacketBuffer;
|
2024-04-20 15:37:22 +02:00
|
|
|
extern uint32_t lastRxTime;
|
2023-06-18 16:56:53 +02:00
|
|
|
|
2023-06-06 17:30:32 +02:00
|
|
|
|
|
|
|
|
namespace APRS_IS_Utils {
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void upload(String line) {
|
|
|
|
|
espClient.print(line + "\r\n");
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-06 17:30:32 +02:00
|
|
|
|
2024-03-17 12:21:11 +01:00
|
|
|
void connect() {
|
2024-03-27 19:28:27 +01:00
|
|
|
uint8_t count = 0;
|
2024-02-24 14:09:05 +01:00
|
|
|
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!");
|
2024-03-17 12:21:11 +01:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Serial.println("Connected!\n(Server: " + String(Config.aprs_is.server) + " / Port: " + String(Config.aprs_is.port) + ")");
|
2024-03-07 17:46:38 +01:00
|
|
|
|
|
|
|
|
// String filter = "t/m/" + Config.callsign + "/" + (String)Config.aprs_is.reportingDistance;
|
|
|
|
|
|
2024-03-18 14:38:16 +01:00
|
|
|
aprsauth = "user " + Config.callsign + " pass " + Config.aprs_is.passcode + " vers CA2RXU_LoRa_iGate 1.3 filter " + Config.aprs_is.filter;
|
2024-02-24 14:09:05 +01:00
|
|
|
upload(aprsauth);
|
|
|
|
|
delay(200);
|
|
|
|
|
}
|
2023-06-06 18:37:22 +02:00
|
|
|
}
|
2024-02-24 14:09:05 +01:00
|
|
|
|
|
|
|
|
void checkStatus() {
|
|
|
|
|
String wifiState, aprsisState;
|
|
|
|
|
if (WiFi.status() == WL_CONNECTED) {
|
2024-03-17 12:21:11 +01:00
|
|
|
wifiState = "OK";
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-02-24 14:09:05 +01:00
|
|
|
wifiState = "AP";
|
2024-03-07 17:46:38 +01:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
if (!Config.display.alwaysOn) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
2024-03-07 17:46:38 +01:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
lastScreenOn = millis();
|
|
|
|
|
}
|
2024-03-07 17:46:38 +01:00
|
|
|
|
|
|
|
|
if (!Config.aprs_is.active) {
|
2024-03-17 12:21:11 +01:00
|
|
|
aprsisState = "OFF";
|
|
|
|
|
}
|
|
|
|
|
else if (espClient.connected()) {
|
|
|
|
|
aprsisState = "OK";
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-02-24 14:09:05 +01:00
|
|
|
aprsisState = "--";
|
2024-03-07 17:46:38 +01:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
if (!Config.display.alwaysOn) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
2024-03-07 17:46:38 +01:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
lastScreenOn = millis();
|
|
|
|
|
}
|
2024-03-18 14:38:16 +01:00
|
|
|
|
2024-03-07 17:46:38 +01:00
|
|
|
secondLine = "WiFi: " + wifiState + " APRS-IS: " + aprsisState;
|
2023-06-06 18:37:22 +02:00
|
|
|
}
|
|
|
|
|
|
2024-04-20 15:27:20 +02:00
|
|
|
String buildPacketToUpload(String packet) {
|
2024-03-07 17:46:38 +01:00
|
|
|
if (!(Config.aprs_is.active && Config.digi.mode == 0)) { // Check if NOT only IGate
|
2024-03-18 14:38:16 +01:00
|
|
|
return packet.substring(3, packet.indexOf(":")) + ",qAR," + Config.callsign + packet.substring(packet.indexOf(":"));
|
2024-03-17 12:21:11 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2024-03-18 14:38:16 +01:00
|
|
|
return packet.substring(3, packet.indexOf(":")) + ",qAO," + Config.callsign + packet.substring(packet.indexOf(":"));
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-08 03:25:31 +02:00
|
|
|
|
2024-04-20 15:27:20 +02:00
|
|
|
String buildPacketToTx(String aprsisPacket) {
|
|
|
|
|
String firstPart, messagePart;
|
|
|
|
|
aprsisPacket.trim();
|
|
|
|
|
firstPart = aprsisPacket.substring(0, aprsisPacket.indexOf(","));
|
|
|
|
|
messagePart = aprsisPacket.substring(aprsisPacket.indexOf("::") + 2);
|
|
|
|
|
return firstPart + ",TCPIP,WIDE1-1," + Config.callsign + "::" + messagePart;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-18 18:48:56 +01:00
|
|
|
bool processReceivedLoRaMessage(String sender, String packet) {
|
|
|
|
|
String ackMessage, receivedMessage;
|
2024-03-17 12:21:11 +01:00
|
|
|
if (packet.indexOf("{") > 0) { // ack?
|
|
|
|
|
ackMessage = "ack" + packet.substring(packet.indexOf("{") + 1);
|
2024-03-18 18:48:56 +01:00
|
|
|
ackMessage.trim();
|
|
|
|
|
//Serial.println(ackMessage);
|
2024-03-17 12:21:11 +01:00
|
|
|
for (int i = sender.length(); i < 9; i++) {
|
2024-03-18 18:48:56 +01:00
|
|
|
sender += ' ';
|
|
|
|
|
}
|
2024-04-04 20:54:49 +02:00
|
|
|
if (Config.beacon.path == "") {
|
2024-04-20 15:27:20 +02:00
|
|
|
STATION_Utils::addToOutputPacketBuffer(Config.callsign + ">APLRG1,RFONLY::" + sender + ":" + ackMessage);
|
2024-04-04 20:54:49 +02:00
|
|
|
} else {
|
2024-04-20 15:27:20 +02:00
|
|
|
STATION_Utils::addToOutputPacketBuffer(Config.callsign + ">APLRG1,RFONLY," + Config.beacon.path + "::" + sender + ":" + ackMessage);
|
2024-04-04 20:54:49 +02:00
|
|
|
}
|
2024-04-20 15:27:20 +02:00
|
|
|
|
2024-03-17 12:21:11 +01:00
|
|
|
receivedMessage = packet.substring(packet.indexOf(":") + 1, packet.indexOf("{"));
|
2024-04-20 15:27:20 +02:00
|
|
|
} else {
|
2024-03-17 12:21:11 +01:00
|
|
|
receivedMessage = packet.substring(packet.indexOf(":") + 1);
|
2024-03-18 18:48:56 +01:00
|
|
|
}
|
|
|
|
|
if (receivedMessage.indexOf("?") == 0) {
|
|
|
|
|
delay(2000);
|
|
|
|
|
if (!Config.display.alwaysOn) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
2024-04-20 15:27:20 +02:00
|
|
|
STATION_Utils::addToOutputPacketBuffer(QUERY_Utils::process(receivedMessage, sender, "LoRa"));
|
2024-03-18 18:48:56 +01:00
|
|
|
lastScreenOn = millis();
|
2024-03-17 12:21:11 +01:00
|
|
|
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, "Callsign = " + sender, "TYPE --> QUERY", 0);
|
2024-03-18 18:48:56 +01:00
|
|
|
return true;
|
2024-03-17 12:21:11 +01:00
|
|
|
}
|
|
|
|
|
else {
|
2024-03-18 18:48:56 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void processLoRaPacket(String packet) {
|
2024-03-27 15:26:02 +01:00
|
|
|
if (espClient.connected()) {
|
|
|
|
|
bool queryMessage = false;
|
|
|
|
|
String aprsPacket, Sender, AddresseeAndMessage, Addressee;
|
|
|
|
|
if (packet != "") {
|
|
|
|
|
if ((packet.substring(0, 3) == "\x3c\xff\x01") && (packet.indexOf("TCPIP") == -1) && (packet.indexOf("NOGATE") == -1) && (packet.indexOf("RFONLY") == -1)) {
|
|
|
|
|
Sender = packet.substring(3, packet.indexOf(">"));
|
|
|
|
|
STATION_Utils::updateLastHeard(Sender);
|
|
|
|
|
Utils::typeOfPacket(aprsPacket, "LoRa-APRS");
|
|
|
|
|
if (Sender != Config.callsign) { // avoid listening yourself by digirepeating
|
|
|
|
|
AddresseeAndMessage = packet.substring(packet.indexOf("::") + 2);
|
|
|
|
|
Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":"));
|
|
|
|
|
Addressee.trim();
|
|
|
|
|
|
|
|
|
|
if (packet.indexOf("::") > 10 && Addressee == Config.callsign) { // its a message for me!
|
|
|
|
|
queryMessage = processReceivedLoRaMessage(Sender, AddresseeAndMessage);
|
|
|
|
|
}
|
|
|
|
|
if (!queryMessage) {
|
2024-04-20 15:27:20 +02:00
|
|
|
aprsPacket = buildPacketToUpload(packet);
|
2024-03-27 15:26:02 +01:00
|
|
|
if (!Config.display.alwaysOn) {
|
|
|
|
|
display_toggle(true);
|
|
|
|
|
}
|
|
|
|
|
lastScreenOn = millis();
|
|
|
|
|
upload(aprsPacket);
|
|
|
|
|
Utils::println("---> Uploaded to APRS-IS");
|
|
|
|
|
STATION_Utils::updateLastHeard(Sender);
|
|
|
|
|
Utils::typeOfPacket(aprsPacket, "LoRa-APRS");
|
|
|
|
|
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 12:21:11 +01:00
|
|
|
}
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
2023-06-08 05:55:31 +02:00
|
|
|
}
|
2023-06-06 21:53:06 +02:00
|
|
|
|
2024-02-24 14:09:05 +01:00
|
|
|
void processAPRSISPacket(String packet) {
|
|
|
|
|
String Sender, AddresseeAndMessage, Addressee, receivedMessage;
|
2024-03-17 12:21:11 +01:00
|
|
|
if (!packet.startsWith("#")) {
|
|
|
|
|
if (packet.indexOf("::") > 0) {
|
|
|
|
|
Sender = packet.substring(0, packet.indexOf(">"));
|
|
|
|
|
AddresseeAndMessage = packet.substring(packet.indexOf("::") + 2);
|
2024-02-24 14:09:05 +01:00
|
|
|
Addressee = AddresseeAndMessage.substring(0, AddresseeAndMessage.indexOf(":"));
|
|
|
|
|
Addressee.trim();
|
|
|
|
|
if (Addressee == Config.callsign) { // its for me!
|
2024-03-17 12:21:11 +01:00
|
|
|
if (AddresseeAndMessage.indexOf("{") > 0) { // ack?
|
|
|
|
|
String ackMessage = "ack" + AddresseeAndMessage.substring(AddresseeAndMessage.indexOf("{") + 1);
|
2024-02-24 14:09:05 +01:00
|
|
|
ackMessage.trim();
|
|
|
|
|
delay(4000);
|
2024-03-17 12:21:11 +01:00
|
|
|
for (int i = Sender.length(); i < 9; i++) {
|
2024-02-24 14:09:05 +01:00
|
|
|
Sender += ' ';
|
|
|
|
|
}
|
|
|
|
|
String ackPacket = Config.callsign + ">APLRG1,TCPIP,qAC::" + Sender + ":" + ackMessage;// + "\n";
|
|
|
|
|
upload(ackPacket);
|
2024-03-17 12:21:11 +01:00
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":") + 1, AddresseeAndMessage.indexOf("{"));
|
2024-03-27 15:26:02 +01:00
|
|
|
} else {
|
2024-03-17 12:21:11 +01:00
|
|
|
receivedMessage = AddresseeAndMessage.substring(AddresseeAndMessage.indexOf(":") + 1);
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
|
|
|
|
if (receivedMessage.indexOf("?") == 0) {
|
2024-03-17 12:21:11 +01:00
|
|
|
Utils::println("Received Query APRS-IS : " + packet);
|
2024-02-24 14:09:05 +01:00
|
|
|
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);
|
|
|
|
|
upload(queryAnswer);
|
2024-03-17 12:21:11 +01:00
|
|
|
SYSLOG_Utils::log("APRSIS Tx", queryAnswer, 0, 0, 0);
|
2024-02-24 14:09:05 +01:00
|
|
|
fifthLine = "APRS-IS ----> APRS-IS";
|
|
|
|
|
sixthLine = Config.callsign;
|
2024-03-17 12:21:11 +01:00
|
|
|
for (int j = sixthLine.length();j < 9;j++) {
|
2024-02-24 14:09:05 +01:00
|
|
|
sixthLine += " ";
|
|
|
|
|
}
|
|
|
|
|
sixthLine += "> " + Sender;
|
|
|
|
|
seventhLine = "QUERY = " + receivedMessage;
|
|
|
|
|
}
|
2024-03-27 15:26:02 +01:00
|
|
|
} else {
|
2024-03-17 12:21:11 +01:00
|
|
|
Utils::print("Received from APRS-IS : " + packet);
|
2024-03-07 17:46:38 +01:00
|
|
|
|
|
|
|
|
if (Config.aprs_is.toRF && STATION_Utils::wasHeard(Addressee)) {
|
2024-04-20 15:27:20 +02:00
|
|
|
STATION_Utils::addToOutputPacketBuffer(buildPacketToTx(packet));
|
2024-02-24 14:09:05 +01:00
|
|
|
display_toggle(true);
|
|
|
|
|
lastScreenOn = millis();
|
|
|
|
|
Utils::typeOfPacket(packet, "APRS-LoRa");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
show_display(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, seventhLine, 0);
|
2024-03-18 14:38:16 +01:00
|
|
|
}
|
2023-06-09 07:12:13 +02:00
|
|
|
}
|
2023-10-08 14:39:44 +02:00
|
|
|
}
|
2023-06-09 07:12:13 +02:00
|
|
|
|
2024-03-27 15:26:02 +01:00
|
|
|
void listenAPRSIS() {
|
2024-03-07 17:46:38 +01:00
|
|
|
if (espClient.connected()) {
|
2024-02-24 14:09:05 +01:00
|
|
|
if (espClient.available()) {
|
2024-03-07 17:46:38 +01:00
|
|
|
String aprsisPacket = espClient.readStringUntil('\r');
|
|
|
|
|
// Serial.println(aprsisPacket);
|
2024-02-24 14:09:05 +01:00
|
|
|
processAPRSISPacket(aprsisPacket);
|
2024-04-20 15:37:22 +02:00
|
|
|
lastRxTime = millis();
|
2024-02-24 14:09:05 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-07-31 05:53:59 +02:00
|
|
|
}
|
2024-02-24 14:09:05 +01:00
|
|
|
|
2023-06-06 17:30:32 +02:00
|
|
|
}
|