Refactor MQTT bridge implementation to use dynamic allocation for the MQTTBridge instance, preventing static initialization crashes on ESP32. Update related methods to check for bridge existence before invoking operations. Enhance documentation to clarify memory limits for active connection slots based on PSRAM availability.
This commit is contained in:
1 parent
bf8a3a7a41
commit
c67fb12b44
5 files changed
+93
-72
No files matched your search
@@ -90,6 +90,12 @@ The MQTT bridge uses a slot-based architecture with up to 3 concurrent connectio
|
||||
- Slot 2: `analyzer-eu`
|
||||
- Slot 3: `none`
|
||||
|
||||
**Memory Limits:**
|
||||
- With PSRAM: All 3 slots can be active simultaneously
|
||||
- Without PSRAM: Maximum 2 active slots (each WSS/TLS connection requires ~40KB internal heap)
|
||||
- If more slots are configured than the device supports, excess slots show as `(inactive)` in `get mqtt.status`
|
||||
- Slot configurations are preserved in preferences — moving firmware to a PSRAM device activates all slots
|
||||
|
||||
### Files
|
||||
|
||||
#### Core Implementation
|
||||
@@ -338,12 +344,13 @@ Minimal raw packet data for map integration.
|
||||
## Key Features
|
||||
|
||||
### Slot-Based Preset System
|
||||
- Up to 3 concurrent MQTT connections
|
||||
- Up to 3 concurrent MQTT connections (with PSRAM), 2 without PSRAM
|
||||
- Built-in presets for LetsMesh Analyzer (US/EU) and MeshMapper
|
||||
- Custom broker support with username/password auth
|
||||
- JWT (Ed25519) authentication for preset brokers
|
||||
- Automatic reconnection with exponential backoff per slot
|
||||
- JWT token buffers only allocated for JWT-auth slots (memory efficient)
|
||||
- Deferred construction: MQTTBridge is heap-allocated in `begin()` to avoid ESP32 static init crashes
|
||||
|
||||
### Raw Radio Data Capture
|
||||
- Captures actual raw radio transmission data (including radio headers)
|
||||
|
||||
@@ -496,7 +496,7 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
#ifdef WITH_BRIDGE
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Store raw radio data for MQTT messages
|
||||
bridge.storeRawRadioData(raw, len, snr, rssi);
|
||||
if (bridge) bridge->storeRawRadioData(raw, len, snr, rssi);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -504,7 +504,7 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
#ifdef WITH_BRIDGE
|
||||
if (_prefs.bridge_pkt_src == 1) {
|
||||
bridge.onPacketReceived(pkt);
|
||||
if (bridge) bridge->onPacketReceived(pkt);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -530,7 +530,7 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
#ifdef WITH_BRIDGE
|
||||
if (_prefs.bridge_pkt_src == 0) {
|
||||
bridge.sendPacket(pkt);
|
||||
if (bridge) bridge->sendPacket(pkt);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -883,7 +883,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
#elif defined(WITH_ESPNOW_BRIDGE)
|
||||
, bridge(&_prefs, _mgr, &rtc)
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
, bridge(&_prefs, _mgr, &rtc, &self_id)
|
||||
, bridge(nullptr)
|
||||
#endif
|
||||
{
|
||||
last_millis = 0;
|
||||
@@ -984,30 +984,34 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
|
||||
#if defined(WITH_BRIDGE)
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Set device public key for MQTT topics
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
MESH_DEBUG_PRINTLN("Setting device ID: %s", device_id);
|
||||
bridge.setDeviceID(device_id);
|
||||
|
||||
// Set firmware version
|
||||
bridge.setFirmwareVersion(getFirmwareVer());
|
||||
|
||||
// Set board model
|
||||
bridge.setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
|
||||
// Set build date
|
||||
bridge.setBuildDate(getBuildDate());
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Set stats sources for automatic stats collection (optional - can be done in custom initialization)
|
||||
// This enables stats to be included in status messages automatically
|
||||
// this (Mesh*) inherits from Dispatcher, so it can be passed as Dispatcher*
|
||||
bridge.setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
// Defer construction to avoid static init crashes on ESP32 classic
|
||||
bridge = new MQTTBridge(&_prefs, _mgr, getRTCClock(), &self_id);
|
||||
#endif
|
||||
|
||||
bridge.begin();
|
||||
if (bridge) {
|
||||
// Set device public key for MQTT topics
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
MESH_DEBUG_PRINTLN("Setting device ID: %s", device_id);
|
||||
bridge->setDeviceID(device_id);
|
||||
|
||||
// Set firmware version
|
||||
bridge->setFirmwareVersion(getFirmwareVer());
|
||||
|
||||
// Set board model
|
||||
bridge->setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
|
||||
// Set build date
|
||||
bridge->setBuildDate(getBuildDate());
|
||||
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
// Set stats sources for automatic stats collection
|
||||
bridge->setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
#endif
|
||||
|
||||
bridge->begin();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1433,7 +1437,7 @@ void MyMesh::loop() {
|
||||
// To check if there is pending work
|
||||
bool MyMesh::hasPendingWork() const {
|
||||
#if defined(WITH_BRIDGE)
|
||||
if (bridge.isRunning()) return true; // bridge needs WiFi radio, can't sleep
|
||||
if (bridge && bridge->isRunning()) return true; // bridge needs WiFi radio, can't sleep
|
||||
#endif
|
||||
return _mgr->getOutboundCount(0xFFFFFFFF) > 0;
|
||||
}
|
||||
@@ -120,7 +120,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
#elif defined(WITH_ESPNOW_BRIDGE)
|
||||
ESPNowBridge bridge;
|
||||
#elif defined(WITH_MQTT_BRIDGE)
|
||||
MQTTBridge bridge;
|
||||
MQTTBridge* bridge;
|
||||
#endif
|
||||
|
||||
void putNeighbour(const mesh::Identity& id, uint32_t timestamp, float snr);
|
||||
@@ -224,47 +224,53 @@ public:
|
||||
|
||||
#if defined(WITH_BRIDGE)
|
||||
void setBridgeState(bool enable) override {
|
||||
if (enable == bridge.isRunning()) return;
|
||||
if (!bridge) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bridge = new MQTTBridge(&_prefs, _mgr, getRTCClock(), &self_id);
|
||||
#endif
|
||||
if (!bridge) return;
|
||||
}
|
||||
if (enable == bridge->isRunning()) return;
|
||||
if (enable)
|
||||
{
|
||||
// Set device metadata before starting bridge (same as in begin())
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
bridge.setDeviceID(device_id);
|
||||
bridge.setFirmwareVersion(getFirmwareVer());
|
||||
bridge.setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
bridge.setBuildDate(getBuildDate());
|
||||
bridge->setDeviceID(device_id);
|
||||
bridge->setFirmwareVersion(getFirmwareVer());
|
||||
bridge->setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
bridge->setBuildDate(getBuildDate());
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bridge.setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
bridge->setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
#endif
|
||||
bridge.begin();
|
||||
bridge->begin();
|
||||
}
|
||||
else
|
||||
else
|
||||
{
|
||||
bridge.end();
|
||||
bridge->end();
|
||||
}
|
||||
}
|
||||
|
||||
void restartBridge() override {
|
||||
if (!bridge.isRunning()) return;
|
||||
bridge.end();
|
||||
if (!bridge || !bridge->isRunning()) return;
|
||||
bridge->end();
|
||||
// Set device metadata before restarting bridge (same as in begin())
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
bridge.setDeviceID(device_id);
|
||||
bridge.setFirmwareVersion(getFirmwareVer());
|
||||
bridge.setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
bridge.setBuildDate(getBuildDate());
|
||||
bridge->setDeviceID(device_id);
|
||||
bridge->setFirmwareVersion(getFirmwareVer());
|
||||
bridge->setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
bridge->setBuildDate(getBuildDate());
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
bridge.setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
bridge->setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
#endif
|
||||
bridge.begin();
|
||||
bridge->begin();
|
||||
}
|
||||
|
||||
int getQueueSize() override {
|
||||
return bridge.getQueueSize();
|
||||
return bridge ? bridge->getQueueSize() : 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -210,7 +210,7 @@ void MyMesh::logRxRaw(float snr, float rssi, const uint8_t raw[], int len) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Store raw radio data for MQTT messages (same as repeater)
|
||||
bridge.storeRawRadioData(raw, len, snr, rssi);
|
||||
if (bridge) bridge->storeRawRadioData(raw, len, snr, rssi);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@@ -219,7 +219,7 @@ void MyMesh::logRx(mesh::Packet *pkt, int len, float score) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_prefs.bridge_enabled && _prefs.bridge_pkt_src == 1) {
|
||||
// Log received packets to MQTT (same as repeater)
|
||||
bridge.onPacketReceived(pkt);
|
||||
if (bridge) bridge->onPacketReceived(pkt);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -245,7 +245,7 @@ void MyMesh::logTx(mesh::Packet *pkt, int len) {
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
if (_prefs.bridge_enabled && _prefs.bridge_pkt_src == 0) {
|
||||
// Log transmitted packets to MQTT (same as repeater)
|
||||
bridge.sendPacket(pkt);
|
||||
if (bridge) bridge->sendPacket(pkt);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -618,7 +618,7 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc
|
||||
: mesh::Mesh(radio, ms, rng, rtc, *new StaticPoolPacketManager(32), tables),
|
||||
_cli(board, rtc, sensors, acl, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4)
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
, bridge(&_prefs, _mgr, &rtc, &self_id)
|
||||
, bridge(nullptr)
|
||||
#endif
|
||||
{
|
||||
last_millis = 0;
|
||||
@@ -718,26 +718,30 @@ void MyMesh::begin(FILESYSTEM *fs) {
|
||||
MESH_DEBUG_PRINTLN("MQTT origin set to device name: %s", _prefs.mqtt_origin);
|
||||
|
||||
if (_prefs.bridge_enabled) {
|
||||
// Set device public key for MQTT topics (same as repeater)
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
MESH_DEBUG_PRINTLN("Setting device ID: %s", device_id);
|
||||
bridge.setDeviceID(device_id);
|
||||
|
||||
// Set firmware version (same as repeater)
|
||||
bridge.setFirmwareVersion(getFirmwareVer());
|
||||
|
||||
// Set board model (same as repeater)
|
||||
bridge.setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
|
||||
// Set build date (same as repeater)
|
||||
bridge.setBuildDate(getBuildDate());
|
||||
|
||||
// Set stats sources for automatic stats collection (same as repeater)
|
||||
bridge.setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
|
||||
bridge.begin();
|
||||
// Defer construction to avoid static init crashes on ESP32 classic
|
||||
bridge = new MQTTBridge(&_prefs, _mgr, getRTCClock(), &self_id);
|
||||
if (bridge) {
|
||||
// Set device public key for MQTT topics
|
||||
char device_id[65];
|
||||
mesh::LocalIdentity self_id = getSelfId();
|
||||
mesh::Utils::toHex(device_id, self_id.pub_key, PUB_KEY_SIZE);
|
||||
MESH_DEBUG_PRINTLN("Setting device ID: %s", device_id);
|
||||
bridge->setDeviceID(device_id);
|
||||
|
||||
// Set firmware version
|
||||
bridge->setFirmwareVersion(getFirmwareVer());
|
||||
|
||||
// Set board model
|
||||
bridge->setBoardModel(_cli.getBoard()->getManufacturerName());
|
||||
|
||||
// Set build date
|
||||
bridge->setBuildDate(getBuildDate());
|
||||
|
||||
// Set stats sources for automatic stats collection
|
||||
bridge->setStatsSources(this, _radio, _cli.getBoard(), _ms);
|
||||
|
||||
bridge->begin();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ class MyMesh : public mesh::Mesh, public CommonCLICallbacks {
|
||||
uint8_t pending_cr;
|
||||
int matching_peer_indexes[MAX_CLIENTS];
|
||||
#ifdef WITH_MQTT_BRIDGE
|
||||
MQTTBridge bridge;
|
||||
MQTTBridge* bridge;
|
||||
#endif
|
||||
|
||||
void addPost(ClientInfo* client, const char* postData);
|
||||
|
||||
Reference in new issue
Block a user