Refactor MQTT: only publish on LoRa Tx if packet is from us and on Rx if not (#3245)

Such that direct message to MQTT node gets published and we get rid of always rebroadcasting when MQTT is enabled

Co-authored-by: Ben Meadors <benmmeadors@gmail.com>
This commit is contained in:
GUVWAF
2024-02-21 20:00:14 +01:00
committed by GitHub
parent 9784758c7b
commit eb8a12e5a2
4 changed files with 18 additions and 9 deletions

View File

@@ -21,7 +21,7 @@ bool FloodingRouter::shouldFilterReceived(const meshtastic_MeshPacket *p)
{
if (wasSeenRecently(p)) { // Note: this will also add a recent packet record
printPacket("Ignoring incoming msg, because we've already seen it", p);
if (!moduleConfig.mqtt.enabled && config.device.role != meshtastic_Config_DeviceConfig_Role_ROUTER &&
if (config.device.role != meshtastic_Config_DeviceConfig_Role_ROUTER &&
config.device.role != meshtastic_Config_DeviceConfig_Role_ROUTER_CLIENT &&
config.device.role != meshtastic_Config_DeviceConfig_Role_REPEATER) {
// cancel rebroadcast of this message *if* there was already one, unless we're a router/repeater!

View File

@@ -257,10 +257,9 @@ ErrorCode Router::send(meshtastic_MeshPacket *p)
return encodeResult; // FIXME - this isn't a valid ErrorCode
}
if (moduleConfig.mqtt.enabled) {
LOG_INFO("Should encrypt MQTT?: %d\n", moduleConfig.mqtt.encryption_enabled);
if (mqtt)
mqtt->onSend(*p, *p_decoded, chIndex);
// Only publish to MQTT if we're the original transmitter of the packet
if (moduleConfig.mqtt.enabled && p->from == nodeDB.getNodeNum() && mqtt) {
mqtt->onSend(*p, *p_decoded, chIndex);
}
packetPool.release(p_decoded);
}
@@ -438,6 +437,8 @@ void Router::handleReceived(meshtastic_MeshPacket *p, RxSource src)
{
// Also, we should set the time from the ISR and it should have msec level resolution
p->rx_time = getValidTime(RTCQualityFromNet); // store the arrival timestamp for the phone
// Store a copy of encrypted packet for MQTT
meshtastic_MeshPacket *p_encrypted = packetPool.allocCopy(*p);
// Take those raw bytes and convert them back into a well structured protobuf we can understand
bool decoded = perhapsDecode(p);
@@ -449,10 +450,16 @@ void Router::handleReceived(meshtastic_MeshPacket *p, RxSource src)
printPacket("handleReceived(USER)", p);
else
printPacket("handleReceived(REMOTE)", p);
// Publish received message to MQTT if we're not the original transmitter of the packet
if (moduleConfig.mqtt.enabled && getFrom(p) != nodeDB.getNodeNum() && mqtt)
mqtt->onSend(*p_encrypted, *p, p->channel);
} else {
printPacket("packet decoding failed or skipped (no PSK?)", p);
}
packetPool.release(p_encrypted); // Release the encrypted packet
// call modules here
MeshModule::callPlugins(*p, src);
}
@@ -474,4 +481,4 @@ void Router::perhapsHandleReceived(meshtastic_MeshPacket *p)
handleReceived(p);
packetPool.release(p);
}
}