LoRa_APRS_iGate/src/utils.cpp

484 lines
20 KiB
C++
Raw Normal View History

2025-07-15 22:28:23 +02:00
/* Copyright (C) 2025 Ricardo Guzman - CA2RXU
2026-02-20 14:03:01 +01:00
*
2025-07-15 22:28:23 +02:00
* This file is part of LoRa APRS iGate.
2026-02-20 14:03:01 +01:00
*
2025-07-15 22:28:23 +02:00
* 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
2026-02-20 14:03:01 +01:00
* the Free Software Foundation, either version 3 of the License, or
2025-07-15 22:28:23 +02:00
* (at your option) any later version.
2026-02-20 14:03:01 +01:00
*
2025-07-15 22:28:23 +02:00
* 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.
2026-02-20 14:03:01 +01:00
*
2025-07-15 22:28:23 +02:00
* 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/>.
*/
2025-11-30 14:15:01 +01:00
#include <APRSPacketLib.h>
2024-10-14 22:04:28 +02:00
#include <TinyGPS++.h>
2023-06-04 20:54:11 +02:00
#include <WiFi.h>
2025-08-27 00:33:58 +02:00
#include "telemetry_utils.h"
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-06-18 00:28:40 +02:00
#include "station_utils.h"
2023-07-06 07:01:10 +02:00
#include "battery_utils.h"
2023-07-30 23:12:50 +02:00
#include "aprs_is_utils.h"
2024-11-05 22:20:44 +01:00
#include "board_pinout.h"
2023-06-13 05:36:39 +02:00
#include "syslog_utils.h"
2024-04-23 19:57:21 +02:00
#include "A7670_utils.h"
2024-05-29 21:43:23 +02:00
#include "lora_utils.h"
2023-06-07 23:25:50 +02:00
#include "wifi_utils.h"
2023-06-20 01:44:55 +02:00
#include "gps_utils.h"
2024-10-05 13:48:08 +02:00
#include "wx_utils.h"
2023-06-08 05:55:31 +02:00
#include "display.h"
2023-06-04 20:18:30 +02:00
#include "utils.h"
2023-06-11 02:17:10 +02:00
2026-02-11 03:01:49 +01:00
#define DAY_MS (24UL * 60UL * 60UL * 1000UL)
2024-10-14 22:04:28 +02:00
2023-06-17 17:10:44 +02:00
extern Configuration Config;
2024-10-14 22:04:28 +02:00
extern TinyGPSPlus gps;
2023-06-17 17:10:44 +02:00
extern String versionDate;
extern String firstLine;
extern String secondLine;
extern String thirdLine;
extern String fourthLine;
extern String fifthLine;
extern String sixthLine;
extern String seventhLine;
extern String iGateBeaconPacket;
extern String iGateLoRaBeaconPacket;
2023-06-20 01:44:55 +02:00
extern int rssi;
extern float snr;
extern int freqError;
extern String distance;
2024-02-24 14:09:05 +01:00
extern bool WiFiConnected;
2024-05-13 20:00:07 +02:00
extern int wxModuleType;
2026-01-21 02:44:03 +01:00
extern bool backupDigiMode;
2024-05-24 20:24:40 +02:00
extern bool shouldSleepLowVoltage;
2024-05-29 21:46:07 +02:00
extern bool transmitFlag;
2024-12-06 16:03:37 +01:00
extern bool passcodeValid;
2026-02-24 11:55:32 +01:00
extern bool sendEUP; // Equations Units Parameters
2023-06-04 20:18:30 +02:00
extern std::vector<LastHeardStation> lastHeardStations;
2026-02-25 16:25:40 +01:00
bool statusUpdate = true;
2026-02-20 14:03:01 +01:00
bool beaconUpdate = false;
uint32_t lastBeaconTx = 0;
uint32_t lastScreenOn = millis();
uint32_t lastStatusTx = 0;
bool stationCallsignIsValid = false;
2024-10-21 13:28:35 +02:00
String beaconPacket;
String secondaryBeaconPacket;
2024-05-14 05:30:15 +02:00
2023-12-20 04:53:04 +01:00
2023-06-12 07:31:18 +02:00
namespace Utils {
2023-06-04 20:18:30 +02:00
2023-10-08 15:16:12 +02:00
void processStatus() {
2026-02-25 16:25:40 +01:00
bool sendOverAPRSIS = Config.beacon.sendViaAPRSIS && Config.aprs_is.active && WiFi.status() == WL_CONNECTED;
bool sendOverRF = !Config.beacon.sendViaAPRSIS && Config.beacon.sendViaRF;
if (!sendOverAPRSIS && !sendOverRF) {
statusUpdate = false;
return;
2024-04-09 18:20:31 +02:00
}
2026-02-25 16:25:40 +01:00
String statusPacket = APRSPacketLib::generateBasePacket(Config.callsign, "APLRG1", Config.beacon.path);
statusPacket += sendOverAPRSIS ? ",qAC:>" : ":>";
statusPacket += Config.beacon.statusPacket;
if (sendOverAPRSIS) {
APRS_IS_Utils::upload(statusPacket);
SYSLOG_Utils::log(2, statusPacket, 0, 0.0, 0); // APRSIS TX
} else {
STATION_Utils::addToOutputPacketBuffer(statusPacket, true); // treated also as beacon on Tx Freq
2023-06-08 23:09:05 +02:00
}
2026-02-25 16:25:40 +01:00
statusUpdate = false;
2026-02-11 03:01:49 +01:00
lastStatusTx = millis();
}
void checkStatusInterval() {
2026-02-25 16:25:40 +01:00
if (lastStatusTx == 0 || millis() - lastStatusTx > DAY_MS) statusUpdate = true;
2023-06-08 06:44:13 +02:00
}
2023-06-17 01:08:25 +02:00
2024-06-08 20:12:20 +02:00
String getLocalIP() {
2025-04-24 14:07:29 +02:00
if (Config.digi.ecoMode == 1 || Config.digi.ecoMode == 2) {
2024-10-07 04:23:22 +02:00
return "** WiFi AP Killed **";
} else if (!WiFiConnected) {
2024-02-25 16:00:44 +01:00
return "IP : 192.168.4.1";
2026-01-21 02:44:03 +01:00
} else if (backupDigiMode) {
2024-05-22 21:41:25 +02:00
return "- BACKUP DIGI MODE -";
2024-02-24 14:09:05 +01:00
} else {
return "IP : " + String(WiFi.localIP()[0]) + "." + String(WiFi.localIP()[1]) + "." + String(WiFi.localIP()[2]) + "." + String(WiFi.localIP()[3]);
2024-11-06 16:47:42 +01:00
}
2023-06-19 06:52:40 +02:00
}
2023-10-08 15:16:12 +02:00
void setupDisplay() {
2025-04-25 01:32:46 +02:00
if (Config.digi.ecoMode != 1) displaySetup();
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2024-05-11 18:59:07 +02:00
digitalWrite(INTERNAL_LED_PIN,HIGH);
#endif
2024-02-24 14:09:05 +01:00
Serial.println("\nStarting Station: " + Config.callsign + " Version: " + versionDate);
2025-04-24 14:07:29 +02:00
Serial.print("(DigiEcoMode: ");
if (Config.digi.ecoMode == 0) {
Serial.println("OFF)");
} else if (Config.digi.ecoMode == 1) {
Serial.println("ON)");
} else {
Serial.println("ON / Only Serial Output)");
}
2024-08-14 18:32:34 +02:00
displayShow(" LoRa APRS", "", "", " ( iGATE & DIGI )", "", "" , " CA2RXU " + versionDate, 4000);
2024-04-24 04:25:20 +02:00
#ifdef INTERNAL_LED_PIN
2024-05-11 18:59:07 +02:00
digitalWrite(INTERNAL_LED_PIN,LOW);
#endif
2026-01-07 18:30:32 +01:00
if (Config.tacticalCallsign != "") {
firstLine = Config.tacticalCallsign;
} else {
firstLine = Config.callsign;
}
2023-10-08 15:16:12 +02:00
seventhLine = " listening...";
2023-06-08 05:55:31 +02:00
}
2023-10-08 15:16:12 +02:00
2025-10-11 23:27:06 +02:00
void showActiveStations() {
2025-02-12 18:15:36 +01:00
char buffer[30]; // Adjust size as needed
sprintf(buffer, "Stations (%dmin) = %2d", Config.rememberStationTime, lastHeardStations.size());
fourthLine = buffer;
2023-10-08 15:16:12 +02:00
}
void checkBeaconInterval() {
uint32_t lastTx = millis() - lastBeaconTx;
2025-01-07 18:19:06 +01:00
if (lastBeaconTx == 0 || lastTx >= Config.beacon.interval * 60 * 1000) {
2025-05-18 14:44:46 +02:00
beaconUpdate = true;
2023-07-06 07:08:01 +02:00
}
2024-03-07 17:46:38 +01:00
2024-10-21 13:28:35 +02:00
#ifdef HAS_GPS
2024-10-21 16:16:31 +02:00
if (Config.beacon.gpsActive && gps.location.lat() == 0.0 && gps.location.lng() == 0.0 && Config.beacon.latitude == 0.0 && Config.beacon.longitude == 0.0) {
2024-10-21 13:28:35 +02:00
GPS_Utils::getData();
beaconUpdate = false;
}
#endif
2023-10-08 15:16:12 +02:00
if (beaconUpdate) {
2025-01-07 18:19:06 +01:00
if (!Config.display.alwaysOn && Config.display.timeout != 0) displayToggle(true);
2024-09-22 18:34:13 +02:00
2026-02-24 11:55:32 +01:00
TELEMETRY_Utils::checkEUPInterval();
if (sendEUP &&
Config.battery.sendVoltageAsTelemetry &&
2026-02-20 14:03:01 +01:00
!Config.wxsensor.active &&
(Config.battery.sendInternalVoltage || Config.battery.sendExternalVoltage) &&
(lastBeaconTx > 0)) {
2025-08-27 00:33:58 +02:00
TELEMETRY_Utils::sendEquationsUnitsParameters();
2024-09-22 18:34:13 +02:00
}
2024-11-06 16:47:42 +01:00
2023-10-08 15:16:12 +02:00
STATION_Utils::deleteNotHeard();
2024-03-07 17:46:38 +01:00
2025-10-11 23:27:06 +02:00
showActiveStations();
2024-03-07 17:46:38 +01:00
2025-12-11 17:44:06 +01:00
beaconPacket = iGateBeaconPacket;
secondaryBeaconPacket = iGateLoRaBeaconPacket;
2024-10-14 22:04:28 +02:00
#ifdef HAS_GPS
2025-04-24 14:07:29 +02:00
if (Config.beacon.gpsActive && Config.digi.ecoMode == 0) {
2024-10-14 22:04:28 +02:00
GPS_Utils::getData();
2024-10-21 13:28:35 +02:00
if (gps.location.isUpdated() && gps.location.lat() != 0.0 && gps.location.lng() != 0.0) {
2025-11-30 14:15:01 +01:00
String basePacket = APRSPacketLib::generateBasePacket(Config.callsign, "APLRG1", Config.beacon.path);
2025-11-30 14:47:12 +01:00
String encodedGPS = APRSPacketLib::encodeGPSIntoBase91(gps.location.lat(),gps.location.lng(), 0, 0, Config.beacon.symbol, false, 0, true, Config.beacon.ambiguityLevel);
2025-11-30 14:15:01 +01:00
beaconPacket = basePacket;
beaconPacket += ",qAC:!";
beaconPacket += Config.beacon.overlay;
beaconPacket += encodedGPS;
secondaryBeaconPacket = basePacket;
secondaryBeaconPacket += ":=";
secondaryBeaconPacket += Config.beacon.overlay;
secondaryBeaconPacket += encodedGPS;
2024-10-14 22:04:28 +02:00
}
2024-10-21 13:28:35 +02:00
}
#endif
2024-10-14 22:04:28 +02:00
2025-02-12 18:15:36 +01:00
if (Config.wxsensor.active) {
2025-05-17 17:43:54 +02:00
String sensorData = (wxModuleType == 0) ? ".../...g...t..." : WX_Utils::readDataSensor();
2025-02-12 18:15:36 +01:00
beaconPacket += sensorData;
secondaryBeaconPacket += sensorData;
2023-06-19 06:52:40 +02:00
}
beaconPacket += Config.beacon.comment;
secondaryBeaconPacket += Config.beacon.comment;
2026-02-20 14:03:01 +01:00
if (stationCallsignIsValid && Config.tacticalCallsign != "") {
2026-01-11 15:46:23 +01:00
beaconPacket += " de ";
beaconPacket += Config.callsign;
secondaryBeaconPacket += " de ";
secondaryBeaconPacket += Config.callsign;
}
2024-03-07 17:46:38 +01:00
2024-05-27 17:46:14 +02:00
#if defined(BATTERY_PIN) || defined(HAS_AXP192) || defined(HAS_AXP2101)
2024-05-24 20:24:40 +02:00
if (Config.battery.sendInternalVoltage || Config.battery.monitorInternalVoltage) {
2024-05-24 19:05:28 +02:00
float internalVoltage = BATTERY_Utils::checkInternalVoltage();
2024-09-22 18:34:13 +02:00
if (Config.battery.monitorInternalVoltage && internalVoltage < Config.battery.internalSleepVoltage) {
beaconPacket += " **IntBatWarning:SLEEP**";
secondaryBeaconPacket += " **IntBatWarning:SLEEP**";
shouldSleepLowVoltage = true;
}
2024-05-24 20:24:40 +02:00
if (Config.battery.sendInternalVoltage) {
2025-02-12 18:15:36 +01:00
char internalVoltageInfo[10]; // Enough to hold "xx.xxV\0"
snprintf(internalVoltageInfo, sizeof(internalVoltageInfo), "%.2fV", internalVoltage);
char sixthLineBuffer[25]; // Enough to hold " (Batt=xx.xxV)"
snprintf(sixthLineBuffer, sizeof(sixthLineBuffer), " (Batt=%s)", internalVoltageInfo);
sixthLine = sixthLineBuffer;
2024-09-22 18:34:13 +02:00
if (!Config.battery.sendVoltageAsTelemetry) {
beaconPacket += " Batt=";
beaconPacket += internalVoltageInfo;
secondaryBeaconPacket += " Batt=";
secondaryBeaconPacket += internalVoltageInfo;
}
2024-11-06 16:47:42 +01:00
}
2024-05-11 18:59:07 +02:00
}
2023-12-26 21:27:36 +01:00
#endif
2024-03-07 17:46:38 +01:00
2025-09-02 03:32:28 +02:00
#ifndef HELTEC_WP_V1
2024-08-19 16:41:46 +02:00
if (Config.battery.sendExternalVoltage || Config.battery.monitorExternalVoltage) {
float externalVoltage = BATTERY_Utils::checkExternalVoltage();
if (Config.battery.monitorExternalVoltage && externalVoltage < Config.battery.externalSleepVoltage) {
beaconPacket += " **ExtBatWarning:SLEEP**";
secondaryBeaconPacket += " **ExtBatWarning:SLEEP**";
shouldSleepLowVoltage = true;
}
2024-09-22 18:34:13 +02:00
if (Config.battery.sendExternalVoltage) {
2025-02-12 18:15:36 +01:00
char externalVoltageInfo[10]; // "xx.xxV\0" (max 7 chars)
snprintf(externalVoltageInfo, sizeof(externalVoltageInfo), "%.2fV", externalVoltage);
2026-02-20 14:03:01 +01:00
2025-02-12 18:15:36 +01:00
char sixthLineBuffer[25]; // Ensure enough space
snprintf(sixthLineBuffer, sizeof(sixthLineBuffer), " (Ext V=%s)", externalVoltageInfo);
sixthLine = sixthLineBuffer;
2024-09-22 18:34:13 +02:00
if (!Config.battery.sendVoltageAsTelemetry) {
beaconPacket += " Ext=";
beaconPacket += externalVoltageInfo;
secondaryBeaconPacket += " Ext=";
secondaryBeaconPacket += externalVoltageInfo;
2024-11-06 16:47:42 +01:00
}
}
2024-05-24 19:05:28 +02:00
}
2024-08-19 16:41:46 +02:00
#endif
2024-03-07 17:46:38 +01:00
2024-10-05 13:48:08 +02:00
if (Config.battery.sendVoltageAsTelemetry && !Config.wxsensor.active && (Config.battery.sendInternalVoltage || Config.battery.sendExternalVoltage)){
2025-08-27 00:33:58 +02:00
String encodedTelemetry = TELEMETRY_Utils::generateEncodedTelemetry();
2024-09-22 18:34:13 +02:00
beaconPacket += encodedTelemetry;
secondaryBeaconPacket += encodedTelemetry;
}
2026-01-21 02:44:03 +01:00
if (Config.beacon.sendViaAPRSIS && Config.aprs_is.active && passcodeValid && !backupDigiMode) {
Utils::println("-- Sending Beacon to APRSIS --");
2024-11-06 16:47:42 +01:00
displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, "SENDING IGATE BEACON", 0);
2023-07-30 23:12:50 +02:00
seventhLine = " listening...";
2024-06-19 01:55:33 +02:00
#ifdef HAS_A7670
2024-05-11 18:59:07 +02:00
A7670_Utils::uploadToAPRSIS(beaconPacket);
2024-04-23 19:57:21 +02:00
#else
2024-05-11 18:59:07 +02:00
APRS_IS_Utils::upload(beaconPacket);
2024-04-23 19:57:21 +02:00
#endif
2025-08-20 19:01:36 +02:00
if (Config.syslog.logBeaconOverTCPIP) SYSLOG_Utils::log(1, "tcp" + beaconPacket, 0, 0.0, 0); // APRSIS TX
2024-03-07 17:46:38 +01:00
}
2026-01-21 02:44:03 +01:00
if (Config.beacon.sendViaRF || backupDigiMode) {
Utils::println("-- Sending Beacon to RF --");
2024-08-14 18:32:34 +02:00
displayShow(firstLine, secondLine, thirdLine, fourthLine, fifthLine, sixthLine, "SENDING DIGI BEACON", 0);
seventhLine = " listening...";
2025-10-11 23:27:06 +02:00
STATION_Utils::addToOutputPacketBuffer(secondaryBeaconPacket, true);
2023-09-21 07:08:10 +02:00
}
2024-03-07 17:46:38 +01:00
2023-10-08 15:16:12 +02:00
lastBeaconTx = millis();
lastScreenOn = millis();
beaconUpdate = false;
2023-06-08 05:55:31 +02:00
}
2024-03-07 17:46:38 +01:00
2026-02-11 03:01:49 +01:00
checkStatusInterval();
2026-02-25 16:25:40 +01:00
if (statusUpdate && Config.beacon.statusActive && !Config.beacon.statusPacket.isEmpty()) processStatus();
2023-06-08 05:55:31 +02:00
}
2023-10-08 15:16:12 +02:00
void checkDisplayInterval() {
uint32_t lastDisplayTime = millis() - lastScreenOn;
2024-04-28 15:30:56 +02:00
if (!Config.display.alwaysOn && lastDisplayTime >= Config.display.timeout * 1000) {
2024-08-14 18:32:34 +02:00
displayToggle(false);
2023-10-08 15:16:12 +02:00
}
2023-07-31 00:21:06 +02:00
}
2024-03-07 17:46:38 +01:00
void validateFreqs() {
if (Config.loramodule.txFreq != Config.loramodule.rxFreq && abs(Config.loramodule.txFreq - Config.loramodule.rxFreq) < 125000) {
Serial.println("Tx Freq less than 125kHz from Rx Freq ---> NOT VALID");
2024-08-14 18:32:34 +02:00
displayShow("Tx Freq is less than ", "125kHz from Rx Freq", "device will autofix", "and then reboot", 1000);
2024-03-07 17:46:38 +01:00
Config.loramodule.txFreq = Config.loramodule.rxFreq; // Inform about that but then change the TX QRG to RX QRG and reset the device
2025-10-15 19:18:43 +02:00
Config.beacon.beaconFreq = 1; // return to LoRa Tx Beacon Freq
2024-03-07 17:46:38 +01:00
Config.writeFile();
ESP.restart();
2023-06-17 17:10:44 +02:00
}
}
2023-10-08 15:16:12 +02:00
2024-06-06 05:49:16 +02:00
void typeOfPacket(const String& packet, const uint8_t packetType) {
2024-05-15 23:47:29 +02:00
switch (packetType) {
case 0: // LoRa-APRS
fifthLine = "LoRa Rx ----> APRS-IS";
break;
case 1: // APRS-LoRa
fifthLine = "APRS-IS ----> LoRa Tx";
break;
2024-11-06 16:47:42 +01:00
case 2: // Digipeater
2024-05-15 23:47:29 +02:00
fifthLine = "LoRa Rx ----> LoRa Tx";
break;
2023-06-17 17:10:44 +02:00
}
2025-02-12 18:15:36 +01:00
int firstColonIndex = packet.indexOf(":");
char nextChar = packet[firstColonIndex + 1];
2026-02-20 21:03:39 +01:00
String sender = packet.substring(0,packet.indexOf(">"));
2024-03-27 19:28:27 +01:00
for (int i = sender.length(); i < 9; i++) {
2023-10-08 15:16:12 +02:00
sender += " ";
2023-06-20 01:44:55 +02:00
}
2024-05-30 22:27:07 +02:00
sixthLine = sender;
2025-02-12 18:15:36 +01:00
if (nextChar == ':') {
2024-05-30 22:27:07 +02:00
sixthLine += "> MESSAGE";
2025-02-12 18:15:36 +01:00
} else if (nextChar == '>') {
2024-05-30 22:27:07 +02:00
sixthLine += "> NEW STATUS";
2025-02-12 18:15:36 +01:00
} else if (nextChar == '!' || nextChar == '=' || nextChar == '@') {
2024-05-30 22:27:07 +02:00
sixthLine += "> GPS BEACON";
2025-02-12 18:15:36 +01:00
if (!Config.syslog.active) GPS_Utils::getDistanceAndComment(packet); // to be checked!!!
2024-05-30 22:27:07 +02:00
seventhLine = "RSSI:";
seventhLine += String(rssi);
seventhLine += "dBm";
2025-02-12 18:15:36 +01:00
seventhLine += (rssi <= -100) ? " " : " ";
if (distance.indexOf(".") == 1) seventhLine += " ";
2024-05-30 22:27:07 +02:00
seventhLine += "D:";
seventhLine += distance;
seventhLine += "km";
2025-02-12 18:15:36 +01:00
} else if (nextChar == '`' || nextChar == '\'') {
2024-05-30 22:27:07 +02:00
sixthLine += "> MIC-E";
2025-02-12 18:15:36 +01:00
} else if (nextChar == ';') {
2024-05-30 22:27:07 +02:00
sixthLine += "> OBJECT";
2024-06-24 17:02:38 +02:00
} else if (packet.indexOf(":T#") >= 10 && packet.indexOf(":=/") == -1) {
sixthLine += "> TELEMETRY";
2023-10-08 15:16:12 +02:00
} else {
2024-05-30 22:27:07 +02:00
sixthLine += "> ??????????";
2025-02-12 18:15:36 +01:00
}
if (nextChar != '!' && nextChar != '=' && nextChar != '@') { // Common assignment for non-GPS cases
seventhLine = "RSSI:";
seventhLine += String(rssi);
seventhLine += "dBm SNR: ";
seventhLine += String(snr);
seventhLine += "dBm";
2023-06-20 05:06:41 +02:00
}
2023-06-09 07:12:13 +02:00
}
2024-05-15 23:47:29 +02:00
void print(const String& text) {
2024-03-17 12:21:11 +01:00
if (!Config.tnc.enableSerial) {
Serial.print(text);
}
}
2024-05-15 23:47:29 +02:00
void println(const String& text) {
2024-03-17 12:21:11 +01:00
if (!Config.tnc.enableSerial) {
Serial.println(text);
}
}
2024-05-23 01:19:25 +02:00
void checkRebootMode() {
if (Config.rebootMode && Config.rebootModeTime > 0) {
Serial.println("(Reboot Time Set to " + String(Config.rebootModeTime) + " hours)");
}
}
void checkRebootTime() {
if (Config.rebootMode && Config.rebootModeTime > 0) {
if (millis() > Config.rebootModeTime * 60 * 60 * 1000) {
Serial.println("\n*** Automatic Reboot Time Restart! ****\n");
ESP.restart();
}
}
}
2024-05-24 20:24:40 +02:00
void checkSleepByLowBatteryVoltage(uint8_t mode) {
if (shouldSleepLowVoltage) {
2024-08-28 23:11:50 +02:00
if (mode == 0) { // at startup
2024-05-24 20:24:40 +02:00
delay(3000);
}
Serial.println("\n\n*** Sleeping Low Battey Voltage ***\n\n");
esp_sleep_enable_timer_wakeup(30 * 60 * 1000000); // sleep 30 min
2024-08-28 23:11:50 +02:00
if (mode == 1) { // low voltage detected after a while
2024-08-14 18:32:34 +02:00
displayToggle(false);
2024-05-24 20:24:40 +02:00
}
2026-03-04 16:24:16 +01:00
#ifdef VEXT_CTRL_PIN
2024-05-24 20:24:40 +02:00
#ifndef HELTEC_WSL_V3
2026-03-04 16:24:16 +01:00
digitalWrite(VEXT_CTRL_PIN, LOW);
2024-05-24 20:24:40 +02:00
#endif
#endif
2024-05-29 21:43:23 +02:00
LoRa_Utils::sleepRadio();
2024-05-29 21:46:07 +02:00
transmitFlag = true;
2024-05-29 21:43:23 +02:00
delay(100);
2024-05-24 20:24:40 +02:00
esp_deep_sleep_start();
2024-05-24 19:05:28 +02:00
}
}
2026-02-20 14:03:01 +01:00
bool callsignIsValid(const String& callsign) {
2024-06-08 20:39:15 +02:00
if (callsign == "WLNK-1") return true;
2026-02-25 01:26:20 +01:00
int totalCallsignLength = callsign.length();
if (totalCallsignLength < 4) return false;
int hyphenIndex = callsign.indexOf("-");
int baseCallsignLength = (hyphenIndex > 0) ? hyphenIndex : totalCallsignLength;
if (hyphenIndex > 0) { // SSID Validation
if (hyphenIndex < 4) return false; // base Callsign must have at least 4 characters
int ssidStart = hyphenIndex + 1;
int ssidLength = totalCallsignLength - ssidStart;
if (ssidLength == 0 || ssidLength > 2) return false;
if (callsign.indexOf('-', ssidStart) != -1) return false; // avoid another "-" in ssid
if (ssidLength == 2 && callsign[ssidStart] == '0') return false; // ssid can't start with "0"
for (int i = ssidStart; i < totalCallsignLength; i++) {
if (!isDigit(callsign[i])) return false;
2024-06-07 23:29:55 +02:00
}
}
2024-06-10 17:33:26 +02:00
2026-02-25 01:26:20 +01:00
if (baseCallsignLength < 4 || baseCallsignLength > 6) return false;
2024-06-07 23:29:55 +02:00
2026-02-25 01:26:20 +01:00
bool padded = false; // Callsigns with 4 characters like A0AA are padded into 5 characters for further analisis : A0AA --> _A0AA
if (baseCallsignLength == 4 && isAlpha(callsign[0]) && isDigit(callsign[1]) && isAlpha(callsign[2]) && isAlpha(callsign[3])) padded = true;
char c0, c1, c2, c3;
if (padded) {
c0 = ' ';
c1 = callsign[0];
c2 = callsign[1];
c3 = callsign[2];
} else {
c0 = callsign[0];
c1 = callsign[1];
c2 = callsign[2];
c3 = callsign[3];
2024-06-07 23:29:55 +02:00
}
2026-02-25 01:26:20 +01:00
if (!isDigit(c2) || !isAlpha(c3)) { // __0A__ must be validated
if (c0 != 'R' && !isDigit(c1) && !isAlpha(c2)) return false; // to accepto R0A___
}
2024-06-10 17:33:26 +02:00
2026-02-25 01:26:20 +01:00
bool isValid =
((isAlphaNumeric(c0) || c0 == ' ') && isAlpha(c1)) || // AA0A (+A+A) + _A0AA (+A) + 0A0A (+A+A)
(isAlpha(c0) && isDigit(c1)) || // A00A (+A+A)
(c0 == 'R' && baseCallsignLength == 6 && isDigit(c1) && isAlpha(c2) && isAlpha(c3) && isAlpha(callsign[4])); // R0AA (+A+A)
if (!isValid) return false; // also 00__ avoided
2024-06-10 17:33:26 +02:00
2026-02-25 01:26:20 +01:00
if (baseCallsignLength > 4 ) { // to validate ____AA
for (int i = 4; i < baseCallsignLength; i++) {
if (!isAlpha(callsign[i])) return false;
2024-06-10 17:33:26 +02:00
}
}
2024-06-07 23:29:55 +02:00
return true;
}
2025-10-12 19:21:51 +02:00
void startupDelay() {
if (Config.startupDelay > 0) {
displayShow("", " STARTUP DELAY ...", "", "", 0);
delay(Config.startupDelay * 60 * 1000);
}
}
2023-06-04 20:18:30 +02:00
}