Bridge: enhance CLI configuration options

This commit is contained in:
João Brázio 2025-10-03 00:20:09 +01:00
parent aa946bbe36
commit 8edcb46a28
No known key found for this signature in database
GPG key ID: 56A1490716A324DD
11 changed files with 162 additions and 118 deletions

View file

@ -40,7 +40,8 @@ void BridgeBase::handleReceivedPacket(mesh::Packet *packet) {
}
if (!_seen_packets.hasSeen(packet)) {
_mgr->queueInbound(packet, millis() + BRIDGE_DELAY);
// bridge_delay provides a buffer to prevent immediate processing conflicts in the mesh network.
_mgr->queueInbound(packet, millis() + _prefs->bridge_delay);
} else {
_mgr->free(packet);
}

View file

@ -1,6 +1,7 @@
#pragma once
#include "helpers/AbstractBridge.h"
#include "helpers/CommonCLI.h"
#include "helpers/SimpleMeshTables.h"
#include <RTClib.h>
@ -48,34 +49,31 @@ public:
static constexpr uint16_t BRIDGE_LENGTH_SIZE = sizeof(uint16_t);
static constexpr uint16_t BRIDGE_CHECKSUM_SIZE = sizeof(uint16_t);
/**
* @brief Default delay in milliseconds for scheduling inbound packet processing
*
* It provides a buffer to prevent immediate processing conflicts in the mesh network.
* Used in handleReceivedPacket() as: millis() + BRIDGE_DELAY
*/
static constexpr uint16_t BRIDGE_DELAY = 500; // TODO: maybe too high ?
protected:
/** Tracks bridge state */
bool _initialized = false;
/** Packet manager for allocating and queuing mesh packets */
mesh::PacketManager *_mgr;
/** RTC clock for timestamping debug messages */
mesh::RTCClock *_rtc;
/** Node preferences for configuration settings */
NodePrefs *_prefs;
/** Tracks seen packets to prevent loops in broadcast communications */
SimpleMeshTables _seen_packets;
/**
* @brief Constructs a BridgeBase instance
*
* @param prefs Node preferences for configuration settings
* @param mgr PacketManager for allocating and queuing packets
* @param rtc RTCClock for timestamping debug messages
*/
BridgeBase(mesh::PacketManager *mgr, mesh::RTCClock *rtc) : _mgr(mgr), _rtc(rtc) {}
BridgeBase(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: _prefs(prefs), _mgr(mgr), _rtc(rtc) {}
/**
* @brief Gets formatted date/time string for logging

View file

@ -21,8 +21,8 @@ void ESPNowBridge::send_cb(const uint8_t *mac, esp_now_send_status_t status) {
}
}
ESPNowBridge::ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: BridgeBase(mgr, rtc), _rx_buffer_pos(0) {
ESPNowBridge::ESPNowBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: BridgeBase(prefs, mgr, rtc), _rx_buffer_pos(0) {
_instance = this;
}
@ -33,8 +33,8 @@ void ESPNowBridge::begin() {
WiFi.mode(WIFI_STA);
// Set wifi channel
if (esp_wifi_set_channel(_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
Serial.printf("%s: ESPNOW BRIDGE: Error setting WIFI channel to %d\n", getLogDateTime(), _channel);
if (esp_wifi_set_channel(_prefs->bridge_channel, WIFI_SECOND_CHAN_NONE) != ESP_OK) {
Serial.printf("%s: ESPNOW BRIDGE: Error setting WIFI channel to %d\n", getLogDateTime(), _prefs->bridge_channel);
return;
}
@ -52,7 +52,7 @@ void ESPNowBridge::begin() {
esp_now_peer_info_t peerInfo = {};
memset(&peerInfo, 0, sizeof(peerInfo));
memset(peerInfo.peer_addr, 0xFF, ESP_NOW_ETH_ALEN); // Broadcast address
peerInfo.channel = _channel;
peerInfo.channel = _prefs->bridge_channel;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
@ -94,9 +94,9 @@ void ESPNowBridge::loop() {
}
void ESPNowBridge::xorCrypt(uint8_t *data, size_t len) {
size_t keyLen = strlen(_secret);
size_t keyLen = strlen(_prefs->bridge_secret);
for (size_t i = 0; i < len; i++) {
data[i] ^= _secret[i % keyLen];
data[i] ^= _prefs->bridge_secret[i % keyLen];
}
}
@ -166,10 +166,9 @@ void ESPNowBridge::onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sta
// Could add transmission error handling here if needed
}
void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) {
void ESPNowBridge::sendPacket(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: ESPNOW BRIDGE: TX packet attempted before initialization\n", getLogDateTime());
return;
}

View file

@ -73,22 +73,10 @@ private:
/** Current position in receive buffer */
size_t _rx_buffer_pos;
/**
* Network encryption key from build define
* Must be defined with WITH_ESPNOW_BRIDGE_SECRET
* Used for XOR encryption to isolate different mesh networks
*/
const char *_secret = WITH_ESPNOW_BRIDGE_SECRET;
/**
* Channel for ESP-NOW communication
* Valid 2.4GHz channels: 1-14
*/
int _channel = 0;
/**
* Performs XOR encryption/decryption of data
*
* Used to isolate different mesh networks
*
* Uses WITH_ESPNOW_BRIDGE_SECRET as the key in a simple XOR operation.
* The same operation is used for both encryption and decryption.
* While not cryptographically secure, it provides basic network isolation.
@ -121,10 +109,11 @@ public:
/**
* Constructs an ESPNowBridge instance
*
* @param prefs Node preferences for configuration settings
* @param mgr PacketManager for allocating and queuing packets
* @param rtc RTCClock for timestamping debug messages
*/
ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc);
ESPNowBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCClock *rtc);
/**
* Initializes the ESP-NOW bridge
@ -166,21 +155,7 @@ public:
*
* @param packet The mesh packet to transmit
*/
void onPacketTransmitted(mesh::Packet *packet) override;
/**
* Gets the current channel
*
* @return The current channel (0 = AUTO, 1-14 = valid channel)
*/
int getChannel() const { return _channel; }
/**
* Sets the channel for ESP-NOW communication
*
* @param ch The channel to set (0 = AUTO, 1-14 = valid channel)
*/
void setChannel(int ch) { _channel = ch; }
void sendPacket(mesh::Packet *packet) override;
};
#endif

View file

@ -4,11 +4,11 @@
#ifdef WITH_RS232_BRIDGE
RS232Bridge::RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: BridgeBase(mgr, rtc), _serial(&serial) {}
RS232Bridge::RS232Bridge(NodePrefs *prefs, Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc)
: BridgeBase(prefs, mgr, rtc), _serial(&serial) {}
void RS232Bridge::begin() {
Serial.printf("%s: RS232 BRIDGE: Initializing...\n", getLogDateTime());
Serial.printf("%s: RS232 BRIDGE: Initializing at %d baud...\n", getLogDateTime(), _prefs->bridge_baud);
#if !defined(WITH_RS232_BRIDGE_RX) || !defined(WITH_RS232_BRIDGE_TX)
#error "WITH_RS232_BRIDGE_RX and WITH_RS232_BRIDGE_TX must be defined"
#endif
@ -26,7 +26,7 @@ void RS232Bridge::begin() {
#else
#error RS232Bridge was not tested on the current platform
#endif
((HardwareSerial *)_serial)->begin(115200);
((HardwareSerial *)_serial)->begin(_prefs->bridge_baud);
// Update bridge state
_initialized = true;
@ -114,10 +114,9 @@ void RS232Bridge::loop() {
}
}
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) {
void RS232Bridge::sendPacket(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: ESPNOW BRIDGE: TX packet attempted before initialization\n", getLogDateTime());
return;
}

View file

@ -49,11 +49,12 @@ public:
/**
* @brief Constructs an RS232Bridge instance
*
* @param prefs Node preferences for configuration settings
* @param serial The hardware serial port to use
* @param mgr PacketManager for allocating and queuing packets
* @param rtc RTCClock for timestamping debug messages
*/
RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc);
RS232Bridge(NodePrefs *prefs, Stream &serial, mesh::PacketManager *mgr, mesh::RTCClock *rtc);
/**
* Initializes the RS232 bridge
@ -96,7 +97,7 @@ public:
*
* @param packet The mesh packet to transmit
*/
void onPacketTransmitted(mesh::Packet *packet) override;
void sendPacket(mesh::Packet *packet) override;
/**
* @brief Called when a complete valid packet has been received from serial