From ae86e721e15c4fd85f834bc11dc768d48f8fa959 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 6 Sep 2024 14:55:45 -0500 Subject: [PATCH 1/8] Don't filter PKI packets just for being encrypted. --- src/mqtt/MQTT.cpp | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 797fc7dc3a..fde9b34372 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -514,19 +514,20 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & return; // no channels have an uplink enabled auto &ch = channels.getByIndex(chIndex); - if (mp_decoded.which_payload_variant != meshtastic_MeshPacket_decoded_tag) { - LOG_CRIT("MQTT::onSend(): mp_decoded isn't actually decoded\n"); - return; - } + if (!mp.pki_encrypted) { + if (mp_decoded.which_payload_variant != meshtastic_MeshPacket_decoded_tag) { + LOG_CRIT("MQTT::onSend(): mp_decoded isn't actually decoded\n"); + return; + } - if (strcmp(moduleConfig.mqtt.address, default_mqtt_address) == 0 && - (mp_decoded.decoded.portnum == meshtastic_PortNum_RANGE_TEST_APP || - mp_decoded.decoded.portnum == meshtastic_PortNum_DETECTION_SENSOR_APP)) { - LOG_DEBUG("MQTT onSend - Ignoring range test or detection sensor message on public mqtt\n"); - return; + if (strcmp(moduleConfig.mqtt.address, default_mqtt_address) == 0 && + (mp_decoded.decoded.portnum == meshtastic_PortNum_RANGE_TEST_APP || + mp_decoded.decoded.portnum == meshtastic_PortNum_DETECTION_SENSOR_APP)) { + LOG_DEBUG("MQTT onSend - Ignoring range test or detection sensor message on public mqtt\n"); + return; + } } - - if (ch.settings.uplink_enabled || mp.pki_encrypted) { + if (mp.pki_encrypted || ch.settings.uplink_enabled) { const char *channelId = mp.pki_encrypted ? "PKI" : channels.getGlobalId(chIndex); meshtastic_ServiceEnvelope *env = mqttPool.allocZeroed(); @@ -537,7 +538,8 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & if (moduleConfig.mqtt.encryption_enabled) { env->packet = (meshtastic_MeshPacket *)∓ LOG_DEBUG("encrypted message\n"); - } else { + } else if (mp_decoded.which_payload_variant == + meshtastic_MeshPacket_decoded_tag) { // Don't upload a still-encrypted PKI packet env->packet = (meshtastic_MeshPacket *)&mp_decoded; LOG_DEBUG("portnum %i message\n", env->packet->decoded.portnum); } From 9f849a60d07d4d2c37fe50ed435b0a8fba1450d4 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 6 Sep 2024 14:58:12 -0500 Subject: [PATCH 2/8] Add ok_to_mqtt config and bit --- src/mesh/Channels.cpp | 4 ---- src/mesh/Channels.h | 10 +++++++++- src/mesh/NodeDB.cpp | 1 + src/mesh/ProtobufModule.h | 3 ++- src/mesh/Router.cpp | 3 +++ src/modules/AtakPluginModule.cpp | 2 ++ src/modules/PositionModule.cpp | 4 ++++ src/modules/SerialModule.cpp | 2 ++ src/modules/esp32/StoreForwardModule.cpp | 3 ++- src/mqtt/MQTT.cpp | 7 +++++++ 10 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/mesh/Channels.cpp b/src/mesh/Channels.cpp index bba7571d26..47c0134438 100644 --- a/src/mesh/Channels.cpp +++ b/src/mesh/Channels.cpp @@ -13,10 +13,6 @@ #include "mqtt/MQTT.h" #endif -/// 16 bytes of random PSK for our _public_ default channel that all devices power up on (AES128) -static const uint8_t defaultpsk[] = {0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, - 0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0x01}; - Channels channels; const char *Channels::adminChannel = "admin"; diff --git a/src/mesh/Channels.h b/src/mesh/Channels.h index 4f87cb309a..e5a750f715 100644 --- a/src/mesh/Channels.h +++ b/src/mesh/Channels.h @@ -129,4 +129,12 @@ class Channels }; /// Singleton channel table -extern Channels channels; \ No newline at end of file +extern Channels channels; + +/// 16 bytes of random PSK for our _public_ default channel that all devices power up on (AES128) +static const uint8_t defaultpsk[] = {0xd4, 0xf1, 0xbb, 0x3a, 0x20, 0x29, 0x07, 0x59, + 0xf0, 0xbc, 0xff, 0xab, 0xcf, 0x4e, 0x69, 0x01}; + +static const uint8_t eventpsk[] = {0x38, 0x4b, 0xbc, 0xc0, 0x1d, 0xc0, 0x22, 0xd1, 0x81, 0xbf, 0x36, + 0xb8, 0x61, 0x21, 0xe1, 0xfb, 0x96, 0xb7, 0x2e, 0x55, 0xbf, 0x74, + 0x22, 0x7e, 0x9d, 0x6a, 0xfb, 0x48, 0xd6, 0x4c, 0xb1, 0xa1}; \ No newline at end of file diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 6613ad3c34..06180310a9 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -285,6 +285,7 @@ void NodeDB::installDefaultConfig() config.lora.tx_enabled = true; // FIXME: maybe false in the future, and setting region to enable it. (unset region forces it off) config.lora.override_duty_cycle = false; + config.lora.config_ok_to_mqtt = false; #ifdef CONFIG_LORA_REGION_USERPREFS config.lora.region = CONFIG_LORA_REGION_USERPREFS; #else diff --git a/src/mesh/ProtobufModule.h b/src/mesh/ProtobufModule.h index 3b438ebebd..56bf71e725 100644 --- a/src/mesh/ProtobufModule.h +++ b/src/mesh/ProtobufModule.h @@ -44,7 +44,8 @@ template class ProtobufModule : protected SinglePortModule { // Update our local node info with our position (even if we don't decide to update anyone else) meshtastic_MeshPacket *p = allocDataPacket(); - + p->decoded.has_ok_to_mqtt = true; + p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), fields, &payload); // LOG_DEBUG("did encode\n"); diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index d8e578db16..a7315fa727 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -424,6 +424,9 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p) // If the packet is not yet encrypted, do so now if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) { + p->decoded.has_ok_to_mqtt = true; + p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded); /* Not actually used, so save the cycles diff --git a/src/modules/AtakPluginModule.cpp b/src/modules/AtakPluginModule.cpp index 437a341db6..2664d978db 100644 --- a/src/modules/AtakPluginModule.cpp +++ b/src/modules/AtakPluginModule.cpp @@ -116,6 +116,8 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast LOG_DEBUG("Compressed chat to_callsign: %d bytes\n", length); } } + mp.decoded.has_ok_to_mqtt = true; + mp.decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), meshtastic_TAKPacket_fields, &compressed); LOG_DEBUG("Final payload: %d bytes\n", mp.decoded.payload.size); diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index cb6a58b2e0..ca3959a120 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -118,6 +118,8 @@ void PositionModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtastic p->latitude_i += (1 << (31 - precision)); p->longitude_i += (1 << (31 - precision)); + mp.decoded.has_ok_to_mqtt = true; + mp.decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), &meshtastic_Position_msg, p); } @@ -270,6 +272,8 @@ meshtastic_MeshPacket *PositionModule::allocAtakPli() LOG_INFO("Sending TAK PLI packet\n"); meshtastic_MeshPacket *mp = allocDataPacket(); mp->decoded.portnum = meshtastic_PortNum_ATAK_PLUGIN; + mp->decoded.has_ok_to_mqtt = true; + mp->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; meshtastic_TAKPacket takPacket = {.is_compressed = true, .has_contact = true, diff --git a/src/modules/SerialModule.cpp b/src/modules/SerialModule.cpp index f0ba64f65a..3a4e2fdd1d 100644 --- a/src/modules/SerialModule.cpp +++ b/src/modules/SerialModule.cpp @@ -235,6 +235,8 @@ void SerialModule::sendTelemetry(meshtastic_Telemetry m) pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_Telemetry_msg, &m); p->to = NODENUM_BROADCAST; p->decoded.want_response = false; + p->decoded.has_ok_to_mqtt = true; + p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; p->priority = meshtastic_MeshPacket_Priority_RELIABLE; service->sendToMesh(p, RX_SRC_LOCAL, true); } diff --git a/src/modules/esp32/StoreForwardModule.cpp b/src/modules/esp32/StoreForwardModule.cpp index c696d342ac..6a10fbfcbb 100644 --- a/src/modules/esp32/StoreForwardModule.cpp +++ b/src/modules/esp32/StoreForwardModule.cpp @@ -260,7 +260,8 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } else { sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; } - + p->decoded.has_ok_to_mqtt = true; + p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_StoreAndForward_msg, &sf); } diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index fde9b34372..ef07281a0e 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -520,6 +520,13 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & return; } + if (mp_decoded.decoded.has_ok_to_mqtt && !mp_decoded.decoded.ok_to_mqtt && + (ch.settings.psk.size < 2 || (ch.settings.psk.size == 16 && memcmp(ch.settings.psk.bytes, defaultpsk, 16)) || + (ch.settings.psk.size == 32 && memcmp(ch.settings.psk.bytes, eventpsk, 32)))) { + LOG_INFO("MQTT onSend - Not forwarding packet with ok_to_mqtt set false\n"); + return; + } + if (strcmp(moduleConfig.mqtt.address, default_mqtt_address) == 0 && (mp_decoded.decoded.portnum == meshtastic_PortNum_RANGE_TEST_APP || mp_decoded.decoded.portnum == meshtastic_PortNum_DETECTION_SENSOR_APP)) { From 16c0b06971534219106df572cfbe84b46b92695f Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 6 Sep 2024 18:27:36 -0500 Subject: [PATCH 3/8] Bitfield --- src/mesh/ProtobufModule.h | 4 ++-- src/mesh/Router.cpp | 4 ++-- src/modules/AtakPluginModule.cpp | 4 ++-- src/modules/PositionModule.cpp | 8 ++++---- src/modules/SerialModule.cpp | 4 ++-- src/modules/esp32/StoreForwardModule.cpp | 4 ++-- src/mqtt/MQTT.cpp | 5 +++-- 7 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/mesh/ProtobufModule.h b/src/mesh/ProtobufModule.h index 56bf71e725..862526a32e 100644 --- a/src/mesh/ProtobufModule.h +++ b/src/mesh/ProtobufModule.h @@ -44,8 +44,8 @@ template class ProtobufModule : protected SinglePortModule { // Update our local node info with our position (even if we don't decide to update anyone else) meshtastic_MeshPacket *p = allocDataPacket(); - p->decoded.has_ok_to_mqtt = true; - p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + p->decoded.has_bitfield = true; + p->decoded.bitfield |= config.lora.config_ok_to_mqtt; p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), fields, &payload); // LOG_DEBUG("did encode\n"); diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index a7315fa727..8ca1eeede3 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -424,8 +424,8 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p) // If the packet is not yet encrypted, do so now if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) { - p->decoded.has_ok_to_mqtt = true; - p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + p->decoded.has_bitfield = true; + p->decoded.bitfield |= config.lora.config_ok_to_mqtt; size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded); diff --git a/src/modules/AtakPluginModule.cpp b/src/modules/AtakPluginModule.cpp index 2664d978db..dbf7b3f248 100644 --- a/src/modules/AtakPluginModule.cpp +++ b/src/modules/AtakPluginModule.cpp @@ -116,8 +116,8 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast LOG_DEBUG("Compressed chat to_callsign: %d bytes\n", length); } } - mp.decoded.has_ok_to_mqtt = true; - mp.decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + mp.decoded.has_bitfield = true; + mp.decoded.bitfield |= config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), meshtastic_TAKPacket_fields, &compressed); LOG_DEBUG("Final payload: %d bytes\n", mp.decoded.payload.size); diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index ca3959a120..8e329a69a9 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -118,8 +118,8 @@ void PositionModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtastic p->latitude_i += (1 << (31 - precision)); p->longitude_i += (1 << (31 - precision)); - mp.decoded.has_ok_to_mqtt = true; - mp.decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + mp.decoded.has_bitfield = true; + mp.decoded.bitfield |= config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), &meshtastic_Position_msg, p); } @@ -272,8 +272,8 @@ meshtastic_MeshPacket *PositionModule::allocAtakPli() LOG_INFO("Sending TAK PLI packet\n"); meshtastic_MeshPacket *mp = allocDataPacket(); mp->decoded.portnum = meshtastic_PortNum_ATAK_PLUGIN; - mp->decoded.has_ok_to_mqtt = true; - mp->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + mp->decoded.has_bitfield = true; + mp->decoded.bitfield |= config.lora.config_ok_to_mqtt; meshtastic_TAKPacket takPacket = {.is_compressed = true, .has_contact = true, diff --git a/src/modules/SerialModule.cpp b/src/modules/SerialModule.cpp index 3a4e2fdd1d..4f263f417c 100644 --- a/src/modules/SerialModule.cpp +++ b/src/modules/SerialModule.cpp @@ -235,8 +235,8 @@ void SerialModule::sendTelemetry(meshtastic_Telemetry m) pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_Telemetry_msg, &m); p->to = NODENUM_BROADCAST; p->decoded.want_response = false; - p->decoded.has_ok_to_mqtt = true; - p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + p->decoded.has_bitfield = true; + p->decoded.bitfield |= config.lora.config_ok_to_mqtt; p->priority = meshtastic_MeshPacket_Priority_RELIABLE; service->sendToMesh(p, RX_SRC_LOCAL, true); } diff --git a/src/modules/esp32/StoreForwardModule.cpp b/src/modules/esp32/StoreForwardModule.cpp index 6a10fbfcbb..b03bfa239a 100644 --- a/src/modules/esp32/StoreForwardModule.cpp +++ b/src/modules/esp32/StoreForwardModule.cpp @@ -260,8 +260,8 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } else { sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; } - p->decoded.has_ok_to_mqtt = true; - p->decoded.ok_to_mqtt = config.lora.config_ok_to_mqtt; + p->decoded.has_bitfield = true; + p->decoded.bitfield |= config.lora.config_ok_to_mqtt; p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_StoreAndForward_msg, &sf); } diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index ef07281a0e..a82d3dfad8 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -520,10 +520,11 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & return; } - if (mp_decoded.decoded.has_ok_to_mqtt && !mp_decoded.decoded.ok_to_mqtt && + // check for the lowest bit of the data bitfield set false, and the use of one of the default keys. + if (mp_decoded.decoded.has_bitfield && !(mp_decoded.decoded.bitfield | 1) && (ch.settings.psk.size < 2 || (ch.settings.psk.size == 16 && memcmp(ch.settings.psk.bytes, defaultpsk, 16)) || (ch.settings.psk.size == 32 && memcmp(ch.settings.psk.bytes, eventpsk, 32)))) { - LOG_INFO("MQTT onSend - Not forwarding packet with ok_to_mqtt set false\n"); + LOG_INFO("MQTT onSend - Not forwarding packet due to DontMqttMeBro flag\n"); return; } From 8832b4f226226e2a7e051b006f36759576c42ead Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Fri, 6 Sep 2024 18:58:31 -0500 Subject: [PATCH 4/8] Adjust dontmqttmebro logic. --- src/mqtt/MQTT.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index a82d3dfad8..555bbb2c68 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -521,7 +521,7 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & } // check for the lowest bit of the data bitfield set false, and the use of one of the default keys. - if (mp_decoded.decoded.has_bitfield && !(mp_decoded.decoded.bitfield | 1) && + if (mp_decoded.from != nodeDB->getNodeNum() && mp_decoded.decoded.has_bitfield && !(mp_decoded.decoded.bitfield & 1) && (ch.settings.psk.size < 2 || (ch.settings.psk.size == 16 && memcmp(ch.settings.psk.bytes, defaultpsk, 16)) || (ch.settings.psk.size == 32 && memcmp(ch.settings.psk.bytes, eventpsk, 32)))) { LOG_INFO("MQTT onSend - Not forwarding packet due to DontMqttMeBro flag\n"); From ce406cee194226a81a88fc653ce0ac1bd10f2be5 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 7 Sep 2024 14:14:18 -0500 Subject: [PATCH 5/8] Manipulate bitfield only in router.cpp --- src/mesh/ProtobufModule.h | 3 +-- src/mesh/Router.cpp | 9 +++++++-- src/modules/AtakPluginModule.cpp | 2 -- src/modules/PositionModule.cpp | 4 ---- src/modules/SerialModule.cpp | 2 -- src/modules/esp32/StoreForwardModule.cpp | 3 +-- 6 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/mesh/ProtobufModule.h b/src/mesh/ProtobufModule.h index 862526a32e..3b438ebebd 100644 --- a/src/mesh/ProtobufModule.h +++ b/src/mesh/ProtobufModule.h @@ -44,8 +44,7 @@ template class ProtobufModule : protected SinglePortModule { // Update our local node info with our position (even if we don't decide to update anyone else) meshtastic_MeshPacket *p = allocDataPacket(); - p->decoded.has_bitfield = true; - p->decoded.bitfield |= config.lora.config_ok_to_mqtt; + p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), fields, &payload); // LOG_DEBUG("did encode\n"); diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 8ca1eeede3..9da6981594 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -378,6 +378,8 @@ bool perhapsDecode(meshtastic_MeshPacket *p) // parsing was successful p->which_payload_variant = meshtastic_MeshPacket_decoded_tag; // change type to decoded p->channel = chIndex; // change to store the index instead of the hash + if (p->decoded.has_bitfield) + p->want_ack |= p->decoded.bitfield & 1 << 1; /* Not actually ever used. // Decompress if needed. jm @@ -424,8 +426,11 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p) // If the packet is not yet encrypted, do so now if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) { - p->decoded.has_bitfield = true; - p->decoded.bitfield |= config.lora.config_ok_to_mqtt; + if (p->from == nodeDB->getNodeNum()) { + p->decoded.has_bitfield = true; + p->decoded.bitfield |= config.lora.config_ok_to_mqtt; + p->decoded.bitfield |= (p->want_ack << 1); + } size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded); diff --git a/src/modules/AtakPluginModule.cpp b/src/modules/AtakPluginModule.cpp index dbf7b3f248..437a341db6 100644 --- a/src/modules/AtakPluginModule.cpp +++ b/src/modules/AtakPluginModule.cpp @@ -116,8 +116,6 @@ void AtakPluginModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtast LOG_DEBUG("Compressed chat to_callsign: %d bytes\n", length); } } - mp.decoded.has_bitfield = true; - mp.decoded.bitfield |= config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), meshtastic_TAKPacket_fields, &compressed); LOG_DEBUG("Final payload: %d bytes\n", mp.decoded.payload.size); diff --git a/src/modules/PositionModule.cpp b/src/modules/PositionModule.cpp index 8e329a69a9..cb6a58b2e0 100644 --- a/src/modules/PositionModule.cpp +++ b/src/modules/PositionModule.cpp @@ -118,8 +118,6 @@ void PositionModule::alterReceivedProtobuf(meshtastic_MeshPacket &mp, meshtastic p->latitude_i += (1 << (31 - precision)); p->longitude_i += (1 << (31 - precision)); - mp.decoded.has_bitfield = true; - mp.decoded.bitfield |= config.lora.config_ok_to_mqtt; mp.decoded.payload.size = pb_encode_to_bytes(mp.decoded.payload.bytes, sizeof(mp.decoded.payload.bytes), &meshtastic_Position_msg, p); } @@ -272,8 +270,6 @@ meshtastic_MeshPacket *PositionModule::allocAtakPli() LOG_INFO("Sending TAK PLI packet\n"); meshtastic_MeshPacket *mp = allocDataPacket(); mp->decoded.portnum = meshtastic_PortNum_ATAK_PLUGIN; - mp->decoded.has_bitfield = true; - mp->decoded.bitfield |= config.lora.config_ok_to_mqtt; meshtastic_TAKPacket takPacket = {.is_compressed = true, .has_contact = true, diff --git a/src/modules/SerialModule.cpp b/src/modules/SerialModule.cpp index 4f263f417c..f0ba64f65a 100644 --- a/src/modules/SerialModule.cpp +++ b/src/modules/SerialModule.cpp @@ -235,8 +235,6 @@ void SerialModule::sendTelemetry(meshtastic_Telemetry m) pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_Telemetry_msg, &m); p->to = NODENUM_BROADCAST; p->decoded.want_response = false; - p->decoded.has_bitfield = true; - p->decoded.bitfield |= config.lora.config_ok_to_mqtt; p->priority = meshtastic_MeshPacket_Priority_RELIABLE; service->sendToMesh(p, RX_SRC_LOCAL, true); } diff --git a/src/modules/esp32/StoreForwardModule.cpp b/src/modules/esp32/StoreForwardModule.cpp index b03bfa239a..c696d342ac 100644 --- a/src/modules/esp32/StoreForwardModule.cpp +++ b/src/modules/esp32/StoreForwardModule.cpp @@ -260,8 +260,7 @@ meshtastic_MeshPacket *StoreForwardModule::preparePayload(NodeNum dest, uint32_t } else { sf.rr = meshtastic_StoreAndForward_RequestResponse_ROUTER_TEXT_DIRECT; } - p->decoded.has_bitfield = true; - p->decoded.bitfield |= config.lora.config_ok_to_mqtt; + p->decoded.payload.size = pb_encode_to_bytes(p->decoded.payload.bytes, sizeof(p->decoded.payload.bytes), &meshtastic_StoreAndForward_msg, &sf); } From 983c2a015adab7fab5fc880e55787223a19daa37 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 7 Sep 2024 14:19:52 -0500 Subject: [PATCH 6/8] Want_ack is not want_response --- src/mesh/Router.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 9da6981594..f5182050ff 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -379,7 +379,7 @@ bool perhapsDecode(meshtastic_MeshPacket *p) p->which_payload_variant = meshtastic_MeshPacket_decoded_tag; // change type to decoded p->channel = chIndex; // change to store the index instead of the hash if (p->decoded.has_bitfield) - p->want_ack |= p->decoded.bitfield & 1 << 1; + p->decoded.want_response |= p->decoded.bitfield & 1 << 1; /* Not actually ever used. // Decompress if needed. jm @@ -429,7 +429,7 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p) if (p->from == nodeDB->getNodeNum()) { p->decoded.has_bitfield = true; p->decoded.bitfield |= config.lora.config_ok_to_mqtt; - p->decoded.bitfield |= (p->want_ack << 1); + p->decoded.bitfield |= (p->decoded.want_response << 1); } size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded); From 77d74de2bdec4fcfc12e0fbb04155a8a95441036 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 7 Sep 2024 14:37:49 -0500 Subject: [PATCH 7/8] Bitfield macros --- src/mesh/Router.cpp | 6 +++--- src/mesh/Router.h | 5 +++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index f5182050ff..b222872fa6 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -379,7 +379,7 @@ bool perhapsDecode(meshtastic_MeshPacket *p) p->which_payload_variant = meshtastic_MeshPacket_decoded_tag; // change type to decoded p->channel = chIndex; // change to store the index instead of the hash if (p->decoded.has_bitfield) - p->decoded.want_response |= p->decoded.bitfield & 1 << 1; + p->decoded.want_response |= p->decoded.bitfield & BITFIELD_WANT_RESPONSE_MASK; /* Not actually ever used. // Decompress if needed. jm @@ -428,8 +428,8 @@ meshtastic_Routing_Error perhapsEncode(meshtastic_MeshPacket *p) if (p->which_payload_variant == meshtastic_MeshPacket_decoded_tag) { if (p->from == nodeDB->getNodeNum()) { p->decoded.has_bitfield = true; - p->decoded.bitfield |= config.lora.config_ok_to_mqtt; - p->decoded.bitfield |= (p->decoded.want_response << 1); + p->decoded.bitfield |= (config.lora.config_ok_to_mqtt << BITFIELD_OK_TO_MQTT_SHIFT); + p->decoded.bitfield |= (p->decoded.want_response << BITFIELD_WANT_RESPONSE_SHIFT); } size_t numbytes = pb_encode_to_bytes(bytes, sizeof(bytes), &meshtastic_Data_msg, &p->decoded); diff --git a/src/mesh/Router.h b/src/mesh/Router.h index 98486745b0..fd4b0ccf92 100644 --- a/src/mesh/Router.h +++ b/src/mesh/Router.h @@ -148,3 +148,8 @@ extern Router *router; /// Generate a unique packet id // FIXME, move this someplace better PacketId generatePacketId(); + +#define BITFIELD_WANT_RESPONSE_SHIFT 1 +#define BITFIELD_OK_TO_MQTT_SHIFT 0 +#define BITFIELD_WANT_RESPONSE_MASK (1 << BITFIELD_WANT_RESPONSE_SHIFT) +#define BITFIELD_OK_TO_MQTT_MASK (1 << BITFIELD_OK_TO_MQTT_SHIFT) \ No newline at end of file From 148386374ab05fd4da612804099e0f284ffbe8f5 Mon Sep 17 00:00:00 2001 From: Jonathan Bennett Date: Sat, 7 Sep 2024 14:55:22 -0500 Subject: [PATCH 8/8] Use new Bitfield macro in MQTT.cpp --- src/mqtt/MQTT.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 555bbb2c68..d14c7a9237 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -521,7 +521,8 @@ void MQTT::onSend(const meshtastic_MeshPacket &mp, const meshtastic_MeshPacket & } // check for the lowest bit of the data bitfield set false, and the use of one of the default keys. - if (mp_decoded.from != nodeDB->getNodeNum() && mp_decoded.decoded.has_bitfield && !(mp_decoded.decoded.bitfield & 1) && + if (mp_decoded.from != nodeDB->getNodeNum() && mp_decoded.decoded.has_bitfield && + !(mp_decoded.decoded.bitfield & BITFIELD_OK_TO_MQTT_MASK) && (ch.settings.psk.size < 2 || (ch.settings.psk.size == 16 && memcmp(ch.settings.psk.bytes, defaultpsk, 16)) || (ch.settings.psk.size == 32 && memcmp(ch.settings.psk.bytes, eventpsk, 32)))) { LOG_INFO("MQTT onSend - Not forwarding packet due to DontMqttMeBro flag\n");