2025-09-08 21:46:19 +10:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <Mesh.h>
|
2025-09-24 16:30:00 +01:00
|
|
|
#include <RTClib.h>
|
|
|
|
|
#include <target.h>
|
2025-09-08 21:46:19 +10:00
|
|
|
|
|
|
|
|
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
|
|
|
|
|
#include <InternalFileSystem.h>
|
|
|
|
|
#elif defined(RP2040_PLATFORM)
|
|
|
|
|
#include <LittleFS.h>
|
|
|
|
|
#elif defined(ESP32)
|
|
|
|
|
#include <SPIFFS.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-09-09 18:02:05 +10:00
|
|
|
#ifdef WITH_RS232_BRIDGE
|
|
|
|
|
#include "helpers/bridges/RS232Bridge.h"
|
|
|
|
|
#define WITH_BRIDGE
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef WITH_ESPNOW_BRIDGE
|
|
|
|
|
#include "helpers/bridges/ESPNowBridge.h"
|
|
|
|
|
#define WITH_BRIDGE
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-09-24 16:30:00 +01:00
|
|
|
#include <helpers/AdvertDataHelpers.h>
|
|
|
|
|
#include <helpers/ArduinoHelpers.h>
|
|
|
|
|
#include <helpers/ClientACL.h>
|
|
|
|
|
#include <helpers/CommonCLI.h>
|
|
|
|
|
#include <helpers/IdentityStore.h>
|
|
|
|
|
#include <helpers/SimpleMeshTables.h>
|
|
|
|
|
#include <helpers/StaticPoolPacketManager.h>
|
2025-10-07 09:45:45 -07:00
|
|
|
#include <helpers/StatsFormatHelper.h>
|
2025-09-24 16:30:00 +01:00
|
|
|
#include <helpers/TxtDataHelpers.h>
|
2025-11-03 14:23:32 +11:00
|
|
|
#include <helpers/RegionMap.h>
|
2025-11-06 20:15:01 +11:00
|
|
|
#include "RateLimiter.h"
|
2025-09-24 16:30:00 +01:00
|
|
|
|
2025-09-09 18:02:05 +10:00
|
|
|
#ifdef WITH_BRIDGE
|
|
|
|
|
extern AbstractBridge* bridge;
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-09-08 21:46:19 +10:00
|
|
|
struct RepeaterStats {
|
|
|
|
|
uint16_t batt_milli_volts;
|
|
|
|
|
uint16_t curr_tx_queue_len;
|
|
|
|
|
int16_t noise_floor;
|
|
|
|
|
int16_t last_rssi;
|
|
|
|
|
uint32_t n_packets_recv;
|
|
|
|
|
uint32_t n_packets_sent;
|
|
|
|
|
uint32_t total_air_time_secs;
|
|
|
|
|
uint32_t total_up_time_secs;
|
|
|
|
|
uint32_t n_sent_flood, n_sent_direct;
|
|
|
|
|
uint32_t n_recv_flood, n_recv_direct;
|
|
|
|
|
uint16_t err_events; // was 'n_full_events'
|
|
|
|
|
int16_t last_snr; // x 4
|
|
|
|
|
uint16_t n_direct_dups, n_flood_dups;
|
|
|
|
|
uint32_t total_rx_air_time_secs;
|
2026-01-24 20:06:29 -08:00
|
|
|
uint32_t n_recv_errors;
|
2025-09-08 21:46:19 +10:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifndef MAX_CLIENTS
|
|
|
|
|
#define MAX_CLIENTS 32
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
struct NeighbourInfo {
|
|
|
|
|
mesh::Identity id;
|
|
|
|
|
uint32_t advert_timestamp;
|
|
|
|
|
uint32_t heard_timestamp;
|
|
|
|
|
int8_t snr; // multiplied by 4, user should divide to get float value
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#ifndef FIRMWARE_BUILD_DATE
|
2026-03-20 12:32:41 +11:00
|
|
|
#define FIRMWARE_BUILD_DATE "20 Mar 2026"
|
2025-09-08 21:46:19 +10:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifndef FIRMWARE_VERSION
|
2026-03-20 12:32:41 +11:00
|
|
|
#define FIRMWARE_VERSION "v1.14.1"
|
2025-09-08 21:46:19 +10:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#define FIRMWARE_ROLE "repeater"
|
|
|
|
|
|
|
|
|
|
#define PACKET_LOG_FILE "/packet_log"
|
|
|
|
|
|
|
|
|
|
class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
|
|
|
|
FILESYSTEM* _fs;
|
2025-10-23 23:24:40 +13:00
|
|
|
uint32_t last_millis;
|
|
|
|
|
uint64_t uptime_millis;
|
2025-09-08 21:46:19 +10:00
|
|
|
unsigned long next_local_advert, next_flood_advert;
|
|
|
|
|
bool _logging;
|
|
|
|
|
NodePrefs _prefs;
|
2026-01-25 01:31:53 +11:00
|
|
|
ClientACL acl;
|
2025-09-08 21:46:19 +10:00
|
|
|
CommonCLI _cli;
|
|
|
|
|
uint8_t reply_data[MAX_PACKET_PAYLOAD];
|
2026-01-08 13:20:52 +11:00
|
|
|
uint8_t reply_path[MAX_PATH_SIZE];
|
|
|
|
|
int8_t reply_path_len;
|
2026-02-19 14:37:51 +11:00
|
|
|
uint8_t reply_path_hash_size;
|
2025-11-03 14:23:32 +11:00
|
|
|
TransportKeyStore key_store;
|
2025-11-03 22:53:14 +11:00
|
|
|
RegionMap region_map, temp_map;
|
|
|
|
|
RegionEntry* load_stack[8];
|
2025-11-06 12:27:25 +11:00
|
|
|
RegionEntry* recv_pkt_region;
|
2026-04-14 17:51:34 +10:00
|
|
|
TransportKey default_scope;
|
2026-01-09 11:07:31 +11:00
|
|
|
RateLimiter discover_limiter, anon_limiter;
|
2026-02-17 01:04:14 +00:00
|
|
|
uint32_t pending_discover_tag;
|
|
|
|
|
unsigned long pending_discover_until;
|
2025-11-03 22:53:14 +11:00
|
|
|
bool region_load_active;
|
2025-09-13 19:37:15 +10:00
|
|
|
unsigned long dirty_contacts_expiry;
|
2025-09-08 21:46:19 +10:00
|
|
|
#if MAX_NEIGHBOURS
|
|
|
|
|
NeighbourInfo neighbours[MAX_NEIGHBOURS];
|
|
|
|
|
#endif
|
|
|
|
|
CayenneLPP telemetry;
|
|
|
|
|
unsigned long set_radio_at, revert_radio_at;
|
|
|
|
|
float pending_freq;
|
|
|
|
|
float pending_bw;
|
|
|
|
|
uint8_t pending_sf;
|
|
|
|
|
uint8_t pending_cr;
|
|
|
|
|
int matching_peer_indexes[MAX_CLIENTS];
|
2025-09-09 19:02:23 +10:00
|
|
|
#if defined(WITH_RS232_BRIDGE)
|
|
|
|
|
RS232Bridge bridge;
|
|
|
|
|
#elif defined(WITH_ESPNOW_BRIDGE)
|
|
|
|
|
ESPNowBridge bridge;
|
|
|
|
|
#endif
|
2025-09-08 21:46:19 +10:00
|
|
|
|
|
|
|
|
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
2025-11-24 22:56:55 +11:00
|
|
|
uint8_t handleLoginReq(const mesh::Identity& sender, const uint8_t* secret, uint32_t sender_timestamp, const uint8_t* data, bool is_flood);
|
2026-01-03 12:02:15 +11:00
|
|
|
uint8_t handleAnonRegionsReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
|
2026-01-12 16:58:35 +11:00
|
|
|
uint8_t handleAnonOwnerReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
|
|
|
|
|
uint8_t handleAnonClockReq(const mesh::Identity& sender, uint32_t sender_timestamp, const uint8_t* data);
|
2025-09-08 21:46:19 +10:00
|
|
|
int handleRequest(ClientInfo* sender, uint32_t sender_timestamp, uint8_t* payload, size_t payload_len);
|
|
|
|
|
mesh::Packet* createSelfAdvert();
|
|
|
|
|
|
|
|
|
|
File openAppend(const char* fname);
|
2026-03-05 16:26:09 +11:00
|
|
|
bool isLooped(const mesh::Packet* packet, const uint8_t max_counters[]);
|
2025-09-08 21:46:19 +10:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
float getAirtimeBudgetFactor() const override {
|
|
|
|
|
return _prefs.airtime_factor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool allowPacketForward(const mesh::Packet* packet) override;
|
|
|
|
|
const char* getLogDateTime() override;
|
|
|
|
|
void logRxRaw(float snr, float rssi, const uint8_t raw[], int len) override;
|
|
|
|
|
|
|
|
|
|
void logRx(mesh::Packet* pkt, int len, float score) override;
|
|
|
|
|
void logTx(mesh::Packet* pkt, int len) override;
|
|
|
|
|
void logTxFail(mesh::Packet* pkt, int len) override;
|
|
|
|
|
int calcRxDelay(float score, uint32_t air_time) const override;
|
|
|
|
|
|
|
|
|
|
uint32_t getRetransmitDelay(const mesh::Packet* packet) override;
|
|
|
|
|
uint32_t getDirectRetransmitDelay(const mesh::Packet* packet) override;
|
|
|
|
|
|
|
|
|
|
int getInterferenceThreshold() const override {
|
|
|
|
|
return _prefs.interference_threshold;
|
|
|
|
|
}
|
|
|
|
|
int getAGCResetInterval() const override {
|
|
|
|
|
return ((int)_prefs.agc_reset_interval) * 4000; // milliseconds
|
|
|
|
|
}
|
|
|
|
|
uint8_t getExtraAckTransmitCount() const override {
|
|
|
|
|
return _prefs.multi_acks;
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-06 15:30:18 +02:00
|
|
|
#if ENV_INCLUDE_GPS == 1
|
|
|
|
|
void applyGpsPrefs() {
|
2025-10-18 23:37:58 +02:00
|
|
|
sensors.setSettingValue("gps", _prefs.gps_enabled?"1":"0");
|
2025-10-06 15:30:18 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-11-05 14:34:44 +11:00
|
|
|
bool filterRecvFloodPacket(mesh::Packet* pkt) override;
|
2025-11-03 14:23:32 +11:00
|
|
|
|
2025-09-08 21:46:19 +10:00
|
|
|
void onAnonDataRecv(mesh::Packet* packet, const uint8_t* secret, const mesh::Identity& sender, uint8_t* data, size_t len) override;
|
|
|
|
|
int searchPeersByHash(const uint8_t* hash) override;
|
|
|
|
|
void getPeerSharedSecret(uint8_t* dest_secret, int peer_idx) override;
|
|
|
|
|
void onAdvertRecv(mesh::Packet* packet, const mesh::Identity& id, uint32_t timestamp, const uint8_t* app_data, size_t app_data_len);
|
|
|
|
|
void onPeerDataRecv(mesh::Packet* packet, uint8_t type, int sender_idx, const uint8_t* secret, uint8_t* data, size_t len) override;
|
|
|
|
|
bool onPeerPathRecv(mesh::Packet* packet, int sender_idx, const uint8_t* secret, uint8_t* path, uint8_t path_len, uint8_t extra_type, uint8_t* extra, uint8_t extra_len) override;
|
2025-11-06 00:56:54 +11:00
|
|
|
void onControlDataRecv(mesh::Packet* packet) override;
|
2025-09-08 21:46:19 +10:00
|
|
|
|
2026-04-14 17:51:34 +10:00
|
|
|
void sendFloodReply(mesh::Packet* packet, unsigned long delay_millis, uint8_t path_hash_size);
|
|
|
|
|
|
2025-09-08 21:46:19 +10:00
|
|
|
public:
|
|
|
|
|
MyMesh(mesh::MainBoard& board, mesh::Radio& radio, mesh::MillisecondClock& ms, mesh::RNG& rng, mesh::RTCClock& rtc, mesh::MeshTables& tables);
|
|
|
|
|
|
|
|
|
|
void begin(FILESYSTEM* fs);
|
2026-03-30 22:35:05 +03:00
|
|
|
void sendNodeDiscoverReq();
|
2025-09-08 21:46:19 +10:00
|
|
|
const char* getFirmwareVer() override { return FIRMWARE_VERSION; }
|
|
|
|
|
const char* getBuildDate() override { return FIRMWARE_BUILD_DATE; }
|
|
|
|
|
const char* getRole() override { return FIRMWARE_ROLE; }
|
|
|
|
|
const char* getNodeName() { return _prefs.node_name; }
|
|
|
|
|
NodePrefs* getNodePrefs() {
|
|
|
|
|
return &_prefs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void savePrefs() override {
|
|
|
|
|
_cli.savePrefs(_fs);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-14 17:51:34 +10:00
|
|
|
void sendFloodScoped(const TransportKey& scope, mesh::Packet* pkt, uint32_t delay_millis, uint8_t path_hash_size);
|
2026-04-15 13:32:49 +10:00
|
|
|
|
|
|
|
|
// CommonCLICallbacks
|
2025-09-08 21:46:19 +10:00
|
|
|
void applyTempRadioParams(float freq, float bw, uint8_t sf, uint8_t cr, int timeout_mins) override;
|
|
|
|
|
bool formatFileSystem() override;
|
2026-01-26 22:20:36 +13:00
|
|
|
void sendSelfAdvertisement(int delay_millis, bool flood) override;
|
2025-09-08 21:46:19 +10:00
|
|
|
void updateAdvertTimer() override;
|
|
|
|
|
void updateFloodAdvertTimer() override;
|
|
|
|
|
|
|
|
|
|
void setLoggingOn(bool enable) override { _logging = enable; }
|
|
|
|
|
|
|
|
|
|
void eraseLogFile() override {
|
|
|
|
|
_fs->remove(PACKET_LOG_FILE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dumpLogFile() override;
|
2026-01-03 20:35:14 +01:00
|
|
|
void setTxPower(int8_t power_dbm) override;
|
2025-09-08 21:46:19 +10:00
|
|
|
void formatNeighborsReply(char *reply) override;
|
|
|
|
|
void removeNeighbor(const uint8_t* pubkey, int key_len) override;
|
2025-10-07 09:45:45 -07:00
|
|
|
void formatStatsReply(char *reply) override;
|
|
|
|
|
void formatRadioStatsReply(char *reply) override;
|
|
|
|
|
void formatPacketStatsReply(char *reply) override;
|
2026-04-15 13:32:49 +10:00
|
|
|
void startRegionsLoad() override;
|
|
|
|
|
bool saveRegions() override;
|
|
|
|
|
void onDefaultRegionChanged(const RegionEntry* r) override;
|
2025-09-08 21:46:19 +10:00
|
|
|
|
|
|
|
|
mesh::LocalIdentity& getSelfId() override { return self_id; }
|
|
|
|
|
|
|
|
|
|
void saveIdentity(const mesh::LocalIdentity& new_id) override;
|
|
|
|
|
void clearStats() override;
|
2026-04-15 13:32:49 +10:00
|
|
|
|
2025-09-08 21:46:19 +10:00
|
|
|
void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
|
|
|
|
|
void loop();
|
2025-10-03 00:20:09 +01:00
|
|
|
|
|
|
|
|
#if defined(WITH_BRIDGE)
|
|
|
|
|
void setBridgeState(bool enable) override {
|
2025-10-06 12:57:04 +01:00
|
|
|
if (enable == bridge.isRunning()) return;
|
|
|
|
|
if (enable)
|
|
|
|
|
{
|
|
|
|
|
bridge.begin();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
bridge.end();
|
|
|
|
|
}
|
2025-10-03 00:20:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void restartBridge() override {
|
2025-10-06 12:57:04 +01:00
|
|
|
if (!bridge.isRunning()) return;
|
2025-10-03 00:20:09 +01:00
|
|
|
bridge.end();
|
|
|
|
|
bridge.begin();
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2025-12-23 12:48:08 +07:00
|
|
|
|
2025-12-24 11:00:34 +07:00
|
|
|
// To check if there is pending work
|
|
|
|
|
bool hasPendingWork() const;
|
2026-02-10 14:56:20 +00:00
|
|
|
|
|
|
|
|
#if defined(USE_SX1262) || defined(USE_SX1268)
|
2026-03-05 18:48:40 +00:00
|
|
|
void setRxBoostedGain(bool enable) override;
|
2026-02-10 14:56:20 +00:00
|
|
|
#endif
|
2025-09-08 21:46:19 +10:00
|
|
|
};
|