Skip to content

Commit 5f31979

Browse files
authored
Merge pull request #831 from Meshcore-Portugal/jbrazio/2025_87fe0ad8
Add bridge management CLI
2 parents 5ae574b + 13a0202 commit 5f31979

File tree

33 files changed

+508
-248
lines changed

33 files changed

+508
-248
lines changed

examples/simple_repeater/MyMesh.cpp

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,12 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
331331
}
332332

333333
void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
334+
#ifdef WITH_BRIDGE
335+
if (_prefs.bridge_pkt_src == 1) {
336+
bridge.sendPacket(pkt);
337+
}
338+
#endif
339+
334340
if (_logging) {
335341
File f = openAppend(PACKET_LOG_FILE);
336342
if (f) {
@@ -352,8 +358,11 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
352358

353359
void MyMesh::logTx(mesh::Packet *pkt, int len) {
354360
#ifdef WITH_BRIDGE
355-
bridge.onPacketTransmitted(pkt);
361+
if (_prefs.bridge_pkt_src == 0) {
362+
bridge.sendPacket(pkt);
363+
}
356364
#endif
365+
357366
if (_logging) {
358367
File f = openAppend(PACKET_LOG_FILE);
359368
if (f) {
@@ -582,9 +591,10 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
582591
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
583592
_cli(board, rtc, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4)
584593
#if defined(WITH_RS232_BRIDGE)
585-
, bridge(WITH_RS232_BRIDGE, _mgr, &rtc)
586-
#elif defined(WITH_ESPNOW_BRIDGE)
587-
, bridge(_mgr, &rtc)
594+
, bridge(&_prefs, WITH_RS232_BRIDGE, _mgr, &rtc)
595+
#endif
596+
#if defined(WITH_ESPNOW_BRIDGE)
597+
, bridge(&_prefs, _mgr, &rtc)
588598
#endif
589599
{
590600
next_local_advert = next_flood_advert = 0;
@@ -614,6 +624,14 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
614624
_prefs.flood_advert_interval = 12; // 12 hours
615625
_prefs.flood_max = 64;
616626
_prefs.interference_threshold = 0; // disabled
627+
628+
// bridge defaults
629+
_prefs.bridge_enabled = 1; // enabled
630+
_prefs.bridge_delay = 500; // milliseconds
631+
_prefs.bridge_pkt_src = 0; // logTx
632+
_prefs.bridge_baud = 115200; // baud rate
633+
_prefs.bridge_channel = 1; // channel 1
634+
StrHelper::strncpy(_prefs.bridge_secret, "LVSITANOS", sizeof(_prefs.bridge_secret));
617635
}
618636

619637
void MyMesh::begin(FILESYSTEM *fs) {
@@ -624,8 +642,10 @@ void MyMesh::begin(FILESYSTEM *fs) {
624642

625643
acl.load(_fs);
626644

627-
#ifdef WITH_BRIDGE
628-
bridge.begin();
645+
#if defined(WITH_BRIDGE)
646+
if (_prefs.bridge_enabled) {
647+
bridge.begin();
648+
}
629649
#endif
630650

631651
radio_set_params(_prefs.freq, _prefs.bw, _prefs.sf, _prefs.cr);

examples/simple_repeater/MyMesh.h

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
#include <Arduino.h>
44
#include <Mesh.h>
5-
#include <helpers/CommonCLI.h>
5+
#include <RTClib.h>
6+
#include <target.h>
67

78
#if defined(NRF52_PLATFORM) || defined(STM32_PLATFORM)
89
#include <InternalFileSystem.h>
@@ -12,16 +13,6 @@
1213
#include <SPIFFS.h>
1314
#endif
1415

15-
#include <helpers/ArduinoHelpers.h>
16-
#include <helpers/StaticPoolPacketManager.h>
17-
#include <helpers/SimpleMeshTables.h>
18-
#include <helpers/IdentityStore.h>
19-
#include <helpers/AdvertDataHelpers.h>
20-
#include <helpers/TxtDataHelpers.h>
21-
#include <helpers/ClientACL.h>
22-
#include <RTClib.h>
23-
#include <target.h>
24-
2516
#ifdef WITH_RS232_BRIDGE
2617
#include "helpers/bridges/RS232Bridge.h"
2718
#define WITH_BRIDGE
@@ -32,6 +23,15 @@
3223
#define WITH_BRIDGE
3324
#endif
3425

26+
#include <helpers/AdvertDataHelpers.h>
27+
#include <helpers/ArduinoHelpers.h>
28+
#include <helpers/ClientACL.h>
29+
#include <helpers/CommonCLI.h>
30+
#include <helpers/IdentityStore.h>
31+
#include <helpers/SimpleMeshTables.h>
32+
#include <helpers/StaticPoolPacketManager.h>
33+
#include <helpers/TxtDataHelpers.h>
34+
3535
#ifdef WITH_BRIDGE
3636
extern AbstractBridge* bridge;
3737
#endif
@@ -182,4 +182,24 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
182182
void clearStats() override;
183183
void handleCommand(uint32_t sender_timestamp, char* command, char* reply);
184184
void loop();
185+
186+
#if defined(WITH_BRIDGE)
187+
void setBridgeState(bool enable) override {
188+
if (enable == bridge.isRunning()) return;
189+
if (enable)
190+
{
191+
bridge.begin();
192+
}
193+
else
194+
{
195+
bridge.end();
196+
}
197+
}
198+
199+
void restartBridge() override {
200+
if (!bridge.isRunning()) return;
201+
bridge.end();
202+
bridge.begin();
203+
}
204+
#endif
185205
};

src/MeshCore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@
2828
#define MESH_DEBUG_PRINTLN(...) {}
2929
#endif
3030

31+
#if BRIDGE_DEBUG && ARDUINO
32+
#define BRIDGE_DEBUG_PRINTLN(F, ...) Serial.printf("%s BRIDGE: " F, getLogDateTime(), ##__VA_ARGS__)
33+
#else
34+
#define BRIDGE_DEBUG_PRINTLN(...) {}
35+
#endif
36+
3137
namespace mesh {
3238

3339
#define BD_STARTUP_NORMAL 0 // getStartupReason() codes

src/helpers/AbstractBridge.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ class AbstractBridge {
1111
*/
1212
virtual void begin() = 0;
1313

14+
/**
15+
* @brief Stops the bridge.
16+
*/
17+
virtual void end() = 0;
18+
19+
/**
20+
* @brief Gets the current state of the bridge.
21+
*
22+
* @return true if the bridge is initialized and running, false otherwise.
23+
*/
24+
virtual bool isRunning() const = 0;
25+
1426
/**
1527
* @brief A method to be called on every main loop iteration.
1628
* Used for tasks like checking for incoming data.
@@ -20,14 +32,14 @@ class AbstractBridge {
2032
/**
2133
* @brief A callback that is triggered when the mesh transmits a packet.
2234
* The bridge can use this to forward the packet.
23-
*
35+
*
2436
* @param packet The packet that was transmitted.
2537
*/
26-
virtual void onPacketTransmitted(mesh::Packet* packet) = 0;
38+
virtual void sendPacket(mesh::Packet* packet) = 0;
2739

2840
/**
2941
* @brief Processes a received packet from the bridge's medium.
30-
*
42+
*
3143
* @param packet The packet that was received.
3244
*/
3345
virtual void onPacketReceived(mesh::Packet* packet) = 0;

0 commit comments

Comments
 (0)