Refactor MQTT bridge handling in MyMesh to support RX and TX packet logging based on MQTT settings. Introduce mqtt_rx_enabled preference for default RX packet handling and update related logic in CommonCLI and MQTTBridge for improved configuration management. Enhance MQTTMessageBuilder to conditionally include SNR and RSSI for RX packets only.
This commit is contained in:
1 parent
258a0d724c
commit
47b632aaa9
11 files changed
+2739
-37
No files matched your search
Binary file not shown.
@@ -132,6 +132,9 @@ void CommonCLI::loadPrefs(FILESYSTEM* fs) {
|
||||
_prefs->bridge_pkt_src = 1; // Set to RX (logRx)
|
||||
savePrefs(fs); // Save the updated preference
|
||||
}
|
||||
// mqtt_rx_enabled: new field appended to end of MQTTPrefs. On upgrade from older firmware,
|
||||
// the shorter /mqtt_prefs file won't contain it, so it keeps the default value (1 = on)
|
||||
// set by setMQTTPrefsDefaults(). No explicit migration needed.
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -353,7 +356,8 @@ static void setMQTTPrefsDefaults(MQTTPrefs* prefs) {
|
||||
prefs->mqtt_status_enabled = 1; // enabled by default
|
||||
prefs->mqtt_packets_enabled = 1; // enabled by default
|
||||
prefs->mqtt_raw_enabled = 0; // disabled by default
|
||||
prefs->mqtt_tx_enabled = 0; // disabled by default (RX only)
|
||||
prefs->mqtt_tx_enabled = 0; // disabled by default
|
||||
prefs->mqtt_rx_enabled = 1; // RX packets enabled by default
|
||||
prefs->mqtt_status_interval = 300000; // 5 minutes default
|
||||
// Slot presets: analyzer-us and analyzer-eu enabled by default, rest = none
|
||||
strncpy(prefs->mqtt_slot_preset[0], "analyzer-us", sizeof(prefs->mqtt_slot_preset[0]) - 1);
|
||||
@@ -530,6 +534,7 @@ void CommonCLI::syncMQTTPrefsToNodePrefs() {
|
||||
_prefs->mqtt_packets_enabled = _mqtt_prefs.mqtt_packets_enabled;
|
||||
_prefs->mqtt_raw_enabled = _mqtt_prefs.mqtt_raw_enabled;
|
||||
_prefs->mqtt_tx_enabled = _mqtt_prefs.mqtt_tx_enabled;
|
||||
_prefs->mqtt_rx_enabled = _mqtt_prefs.mqtt_rx_enabled;
|
||||
_prefs->mqtt_status_interval = _mqtt_prefs.mqtt_status_interval;
|
||||
StrHelper::strncpy(_prefs->wifi_ssid, _mqtt_prefs.wifi_ssid, sizeof(_prefs->wifi_ssid));
|
||||
StrHelper::strncpy(_prefs->wifi_password, _mqtt_prefs.wifi_password, sizeof(_prefs->wifi_password));
|
||||
@@ -560,6 +565,7 @@ void CommonCLI::syncNodePrefsToMQTTPrefs() {
|
||||
_mqtt_prefs.mqtt_packets_enabled = _prefs->mqtt_packets_enabled;
|
||||
_mqtt_prefs.mqtt_raw_enabled = _prefs->mqtt_raw_enabled;
|
||||
_mqtt_prefs.mqtt_tx_enabled = _prefs->mqtt_tx_enabled;
|
||||
_mqtt_prefs.mqtt_rx_enabled = _prefs->mqtt_rx_enabled;
|
||||
_mqtt_prefs.mqtt_status_interval = _prefs->mqtt_status_interval;
|
||||
StrHelper::strncpy(_mqtt_prefs.wifi_ssid, _prefs->wifi_ssid, sizeof(_mqtt_prefs.wifi_ssid));
|
||||
StrHelper::strncpy(_mqtt_prefs.wifi_password, _prefs->wifi_password, sizeof(_mqtt_prefs.wifi_password));
|
||||
@@ -864,7 +870,10 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
} else if (memcmp(config, "mqtt.raw", 8) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_raw_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.tx", 7) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_tx_enabled ? "on" : "off");
|
||||
const char* tx_str = _prefs->mqtt_tx_enabled == 2 ? "advert" : (_prefs->mqtt_tx_enabled ? "on" : "off");
|
||||
sprintf(reply, "> %s", tx_str);
|
||||
} else if (memcmp(config, "mqtt.rx", 7) == 0) {
|
||||
sprintf(reply, "> %s", _prefs->mqtt_rx_enabled ? "on" : "off");
|
||||
} else if (memcmp(config, "mqtt.interval", 13) == 0) {
|
||||
// Display interval in minutes (rounded)
|
||||
uint32_t minutes = (_prefs->mqtt_status_interval + 29999) / 60000; // Round up
|
||||
@@ -1262,6 +1271,14 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
}
|
||||
} else if (memcmp(config, "bridge.source ", 14) == 0) {
|
||||
_prefs->bridge_pkt_src = memcmp(&config[14], "rx", 2) == 0;
|
||||
// Also update mqtt.rx/mqtt.tx for MQTT bridge compatibility
|
||||
if (_prefs->bridge_pkt_src == 1) {
|
||||
_prefs->mqtt_rx_enabled = 1;
|
||||
_prefs->mqtt_tx_enabled = 0;
|
||||
} else {
|
||||
_prefs->mqtt_rx_enabled = 0;
|
||||
_prefs->mqtt_tx_enabled = 1;
|
||||
}
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
#endif
|
||||
@@ -1321,7 +1338,15 @@ void CommonCLI::handleCommand(uint32_t sender_timestamp, const char* command, ch
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.tx ", 8) == 0) {
|
||||
_prefs->mqtt_tx_enabled = memcmp(&config[8], "on", 2) == 0;
|
||||
if (memcmp(&config[8], "advert", 6) == 0) {
|
||||
_prefs->mqtt_tx_enabled = 2;
|
||||
} else {
|
||||
_prefs->mqtt_tx_enabled = memcmp(&config[8], "on", 2) == 0 ? 1 : 0;
|
||||
}
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.rx ", 8) == 0) {
|
||||
_prefs->mqtt_rx_enabled = memcmp(&config[8], "on", 2) == 0 ? 1 : 0;
|
||||
savePrefs();
|
||||
strcpy(reply, "OK");
|
||||
} else if (memcmp(config, "mqtt.interval ", 14) == 0) {
|
||||
|
||||
@@ -66,9 +66,10 @@ struct NodePrefs { // persisted to file
|
||||
uint8_t mqtt_status_enabled; // Enable status messages
|
||||
uint8_t mqtt_packets_enabled; // Enable packet messages
|
||||
uint8_t mqtt_raw_enabled; // Enable raw messages
|
||||
uint8_t mqtt_tx_enabled; // Enable TX packet uplinking
|
||||
uint8_t mqtt_tx_enabled; // TX packet uplinking: 0=off, 1=all, 2=advert (self-originated only)
|
||||
uint32_t mqtt_status_interval; // Status publish interval (ms)
|
||||
|
||||
uint8_t mqtt_rx_enabled; // Enable RX packet uplinking (default: on)
|
||||
|
||||
// WiFi settings
|
||||
char wifi_ssid[32]; // WiFi SSID
|
||||
char wifi_password[64]; // WiFi password
|
||||
@@ -175,6 +176,9 @@ struct MQTTPrefs {
|
||||
char mqtt_slot_token[MAX_MQTT_SLOTS][48]; // Per-slot token (e.g., MeshRank account token)
|
||||
char mqtt_slot_topic[MAX_MQTT_SLOTS][96]; // Per-slot custom topic template (custom preset only)
|
||||
char mqtt_slot_audience[MAX_MQTT_SLOTS][64]; // JWT audience (non-empty enables JWT auth for custom slots)
|
||||
|
||||
// --- Appended fields (added after initial 6-slot migration) ---
|
||||
uint8_t mqtt_rx_enabled; // Enable RX packet uplinking (default: on)
|
||||
};
|
||||
|
||||
// 3-slot MQTTPrefs layout — used for migrating from 3-slot to 6-slot format.
|
||||
|
||||
@@ -122,8 +122,11 @@ int MQTTMessageBuilder::buildPacketMessage(
|
||||
root["route"] = route;
|
||||
root["payload_len"] = payload_len_str;
|
||||
root["raw"] = raw;
|
||||
root["SNR"] = snr_str;
|
||||
root["RSSI"] = rssi_str;
|
||||
// SNR and RSSI are only meaningful for RX packets (received from radio)
|
||||
if (strcmp(direction, "rx") == 0) {
|
||||
root["SNR"] = snr_str;
|
||||
root["RSSI"] = rssi_str;
|
||||
}
|
||||
root["hash"] = hash;
|
||||
|
||||
if (path && strlen(path) > 0) {
|
||||
|
||||
@@ -319,7 +319,8 @@ MQTTBridge::MQTTBridge(NodePrefs *prefs, mesh::PacketManager *mgr, mesh::RTCCloc
|
||||
_status_enabled = true;
|
||||
_packets_enabled = true;
|
||||
_raw_enabled = false;
|
||||
_tx_enabled = false;
|
||||
_rx_enabled = true;
|
||||
_tx_mode = 0;
|
||||
|
||||
// Initialize all slots to empty/disabled state
|
||||
for (int i = 0; i < RUNTIME_MQTT_SLOTS; i++) {
|
||||
@@ -439,7 +440,8 @@ void MQTTBridge::begin() {
|
||||
_status_enabled = _prefs->mqtt_status_enabled;
|
||||
_packets_enabled = _prefs->mqtt_packets_enabled;
|
||||
_raw_enabled = _prefs->mqtt_raw_enabled;
|
||||
_tx_enabled = _prefs->mqtt_tx_enabled;
|
||||
_rx_enabled = _prefs->mqtt_rx_enabled;
|
||||
_tx_mode = _prefs->mqtt_tx_enabled; // 0=off, 1=all, 2=advert
|
||||
// Set status interval to 5 minutes (300000 ms), or use preference if set and valid
|
||||
if (_prefs->mqtt_status_interval >= 1000 && _prefs->mqtt_status_interval <= 3600000) {
|
||||
_status_interval = _prefs->mqtt_status_interval;
|
||||
@@ -1764,11 +1766,11 @@ void MQTTBridge::setSlotCustomBroker(int slot_index, const char* host, uint16_t
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void MQTTBridge::checkConfigurationMismatch() {
|
||||
// Check if bridge.source is set to tx (logTx) but mqtt.tx is disabled
|
||||
if (_prefs->bridge_pkt_src == 0 && _packets_enabled && !_tx_enabled) {
|
||||
// Warn if packets are enabled but both rx and tx are off — nothing will be published
|
||||
if (_prefs->mqtt_packets_enabled && !_prefs->mqtt_rx_enabled && _prefs->mqtt_tx_enabled == 0) {
|
||||
unsigned long now = millis();
|
||||
if (_last_config_warning == 0 || (now - _last_config_warning > CONFIG_WARNING_INTERVAL)) {
|
||||
MQTT_DEBUG_PRINTLN("MQTT: Configuration mismatch detected! bridge.source=tx (logTx) but mqtt.tx=off. Packets will not be published. Run 'set bridge.source rx' or 'set mqtt.tx on' to fix.");
|
||||
MQTT_DEBUG_PRINTLN("MQTT: Both mqtt.rx and mqtt.tx are off — no packets will be published. Run 'set mqtt.rx on' or 'set mqtt.tx on' to fix.");
|
||||
_last_config_warning = now;
|
||||
}
|
||||
} else {
|
||||
@@ -2038,7 +2040,7 @@ void MQTTBridge::loop() {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled) return;
|
||||
if (!_initialized || !_prefs->mqtt_packets_enabled || !_prefs->mqtt_rx_enabled) return;
|
||||
|
||||
// Check if we have any enabled slots to send to
|
||||
bool has_valid_slots = false;
|
||||
@@ -2055,9 +2057,18 @@ void MQTTBridge::onPacketReceived(mesh::Packet *packet) {
|
||||
}
|
||||
|
||||
void MQTTBridge::sendPacket(mesh::Packet *packet) {
|
||||
if (!_initialized || !_packets_enabled || !_tx_enabled) return;
|
||||
uint8_t tx_mode = _prefs->mqtt_tx_enabled; // Read live from prefs (no restart needed)
|
||||
if (!_initialized || !_prefs->mqtt_packets_enabled || tx_mode == 0) return;
|
||||
|
||||
// Queue packet for transmission (only if TX enabled)
|
||||
// Advert mode: only queue self-originated advert packets
|
||||
if (tx_mode == 2) {
|
||||
if (packet->getPayloadType() != PAYLOAD_TYPE_ADVERT) return;
|
||||
if (packet->payload_len < PUB_KEY_SIZE) return;
|
||||
// Advert payload starts with advertiser's 32-byte public key — compare to our identity
|
||||
if (!_identity || memcmp(_identity->pub_key, packet->payload, PUB_KEY_SIZE) != 0) return;
|
||||
}
|
||||
|
||||
// Queue packet for transmission
|
||||
queuePacket(packet, true);
|
||||
}
|
||||
|
||||
|
||||
@@ -110,7 +110,8 @@ private:
|
||||
bool _status_enabled;
|
||||
bool _packets_enabled;
|
||||
bool _raw_enabled;
|
||||
bool _tx_enabled;
|
||||
bool _rx_enabled;
|
||||
uint8_t _tx_mode; // 0=off, 1=all TX, 2=self-advert only
|
||||
unsigned long _last_status_publish;
|
||||
unsigned long _status_interval;
|
||||
|
||||
|
||||
Reference in new issue
Block a user