Add bridge management CLI

This commit is contained in:
João Brázio
2025-09-24 16:30:00 +01:00
parent ea13fa899e
commit 1d45c7ec66
11 changed files with 340 additions and 137 deletions

View File

@@ -2,6 +2,10 @@
#include <Arduino.h>
bool BridgeBase::getState() const {
return _initialized;
}
const char *BridgeBase::getLogDateTime() {
static char tmp[32];
uint32_t now = _rtc->getCurrentTime();
@@ -28,6 +32,13 @@ bool BridgeBase::validateChecksum(const uint8_t *data, size_t len, uint16_t rece
}
void BridgeBase::handleReceivedPacket(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: BRIDGE: RX packet received before initialization\n", getLogDateTime());
_mgr->free(packet);
return;
}
if (!_seen_packets.hasSeen(packet)) {
_mgr->queueInbound(packet, millis() + BRIDGE_DELAY);
} else {

View File

@@ -21,6 +21,13 @@ class BridgeBase : public AbstractBridge {
public:
virtual ~BridgeBase() = default;
/**
* @brief Gets the current state of the bridge.
*
* @return true if the bridge is initialized and running, false otherwise.
*/
bool getState() const override;
/**
* @brief Common magic number used by all bridge implementations for packet identification
*
@@ -50,6 +57,9 @@ public:
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;

View File

@@ -27,8 +27,16 @@ ESPNowBridge::ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc)
}
void ESPNowBridge::begin() {
Serial.printf("%s: ESPNOW BRIDGE: Initializing...\n", getLogDateTime());
// Initialize WiFi in station mode
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);
return;
}
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
@@ -44,13 +52,41 @@ 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 = 0;
peerInfo.channel = _channel;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.printf("%s: ESPNOW BRIDGE: Failed to add broadcast peer\n", getLogDateTime());
return;
}
// Update bridge state
_initialized = true;
}
void ESPNowBridge::end() {
Serial.printf("%s: ESPNOW BRIDGE: Stopping...\n", getLogDateTime());
// Remove broadcast peer
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
if (esp_now_del_peer(broadcastAddress) != ESP_OK) {
Serial.printf("%s: ESPNOW BRIDGE: Error removing broadcast peer\n", getLogDateTime());
}
// Unregister callbacks
esp_now_register_recv_cb(nullptr);
esp_now_register_send_cb(nullptr);
// Deinitialize ESP-NOW
if (esp_now_deinit() != ESP_OK) {
Serial.printf("%s: ESPNOW BRIDGE: Error deinitializing ESP-NOW\n", getLogDateTime());
}
// Turn off WiFi
WiFi.mode(WIFI_OFF);
// Update bridge state
_initialized = false;
}
void ESPNowBridge::loop() {
@@ -130,11 +166,13 @@ void ESPNowBridge::onDataSent(const uint8_t *mac_addr, esp_now_send_status_t sta
// Could add transmission error handling here if needed
}
void ESPNowBridge::onPacketReceived(mesh::Packet *packet) {
handleReceivedPacket(packet);
}
void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: ESPNOW BRIDGE: TX packet attempted before initialization\n", getLogDateTime());
return;
}
// First validate the packet pointer
if (!packet) {
#if MESH_PACKET_LOGGING
@@ -144,7 +182,6 @@ void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) {
}
if (!_seen_packets.hasSeen(packet)) {
// Create a temporary buffer just for size calculation and reuse for actual writing
uint8_t sizingBuffer[MAX_PAYLOAD_SIZE];
uint16_t meshPacketLen = packet->writeTo(sizingBuffer);
@@ -193,4 +230,8 @@ void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) {
}
}
void ESPNowBridge::onPacketReceived(mesh::Packet *packet) {
handleReceivedPacket(packet);
}
#endif

View File

@@ -80,6 +80,12 @@ private:
*/
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
*
@@ -130,6 +136,16 @@ public:
*/
void begin() override;
/**
* Stops the ESP-NOW bridge
*
* - Removes broadcast peer
* - Unregisters callbacks
* - Deinitializes ESP-NOW protocol
* - Turns off WiFi to release radio resources
*/
void end() override;
/**
* Main loop handler
* ESP-NOW is callback-based, so this is currently empty
@@ -151,6 +167,20 @@ 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; }
};
#endif

View File

@@ -8,6 +8,7 @@ RS232Bridge::RS232Bridge(Stream &serial, mesh::PacketManager *mgr, mesh::RTCCloc
: BridgeBase(mgr, rtc), _serial(&serial) {}
void RS232Bridge::begin() {
Serial.printf("%s: RS232 BRIDGE: Initializing...\n", getLogDateTime());
#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,52 +27,25 @@ void RS232Bridge::begin() {
#error RS232Bridge was not tested on the current platform
#endif
((HardwareSerial *)_serial)->begin(115200);
// Update bridge state
_initialized = true;
}
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) {
// First validate the packet pointer
if (!packet) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime());
#endif
return;
}
void RS232Bridge::end() {
Serial.printf("%s: RS232 BRIDGE: Stopping...\n", getLogDateTime());
((HardwareSerial *)_serial)->end();
if (!_seen_packets.hasSeen(packet)) {
uint8_t buffer[MAX_SERIAL_PACKET_SIZE];
uint16_t len = packet->writeTo(buffer + 4);
// Check if packet fits within our maximum payload size
if (len > (MAX_TRANS_UNIT + 1)) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len,
MAX_TRANS_UNIT + 1);
#endif
return;
}
// Build packet header
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; // Magic high byte
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; // Magic low byte
buffer[2] = (len >> 8) & 0xFF; // Length high byte
buffer[3] = len & 0xFF; // Length low byte
// Calculate checksum over the payload
uint16_t checksum = fletcher16(buffer + 4, len);
buffer[4 + len] = (checksum >> 8) & 0xFF; // Checksum high byte
buffer[5 + len] = checksum & 0xFF; // Checksum low byte
// Send complete packet
_serial->write(buffer, len + SERIAL_OVERHEAD);
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum);
#endif
}
// Update bridge state
_initialized = false;
}
void RS232Bridge::loop() {
// Guard against uninitialized state
if (_initialized == false) {
return;
}
while (_serial->available()) {
uint8_t b = _serial->read();
@@ -140,6 +114,55 @@ void RS232Bridge::loop() {
}
}
void RS232Bridge::onPacketTransmitted(mesh::Packet *packet) {
// Guard against uninitialized state
if (_initialized == false) {
Serial.printf("%s: ESPNOW BRIDGE: TX packet attempted before initialization\n", getLogDateTime());
return;
}
// First validate the packet pointer
if (!packet) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX invalid packet pointer\n", getLogDateTime());
#endif
return;
}
if (!_seen_packets.hasSeen(packet)) {
uint8_t buffer[MAX_SERIAL_PACKET_SIZE];
uint16_t len = packet->writeTo(buffer + 4);
// Check if packet fits within our maximum payload size
if (len > (MAX_TRANS_UNIT + 1)) {
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX packet too large (payload=%d, max=%d)\n", getLogDateTime(), len,
MAX_TRANS_UNIT + 1);
#endif
return;
}
// Build packet header
buffer[0] = (BRIDGE_PACKET_MAGIC >> 8) & 0xFF; // Magic high byte
buffer[1] = BRIDGE_PACKET_MAGIC & 0xFF; // Magic low byte
buffer[2] = (len >> 8) & 0xFF; // Length high byte
buffer[3] = len & 0xFF; // Length low byte
// Calculate checksum over the payload
uint16_t checksum = fletcher16(buffer + 4, len);
buffer[4 + len] = (checksum >> 8) & 0xFF; // Checksum high byte
buffer[5 + len] = checksum & 0xFF; // Checksum low byte
// Send complete packet
_serial->write(buffer, len + SERIAL_OVERHEAD);
#if MESH_PACKET_LOGGING
Serial.printf("%s: RS232 BRIDGE: TX, len=%d crc=0x%04x\n", getLogDateTime(), len, checksum);
#endif
}
}
void RS232Bridge::onPacketReceived(mesh::Packet *packet) {
handleReceivedPacket(packet);
}

View File

@@ -65,6 +65,12 @@ public:
*/
void begin() override;
/**
* Stops the RS232 bridge
*
*/
void end() override;
/**
* @brief Main loop handler for processing incoming serial data
*