Skip to content

Commit

Permalink
Add ok_to_mqtt config and bit
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett authored and caveman99 committed Sep 6, 2024
1 parent 268d414 commit d966658
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 7 deletions.
4 changes: 0 additions & 4 deletions src/mesh/Channels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 9 additions & 1 deletion src/mesh/Channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,12 @@ class Channels
};

/// Singleton channel table
extern Channels channels;
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};
1 change: 1 addition & 0 deletions src/mesh/NodeDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/ProtobufModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ template <class T> 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");
Expand Down
3 changes: 3 additions & 0 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/modules/AtakPluginModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/modules/PositionModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/modules/SerialModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/modules/esp32/StoreForwardModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
7 changes: 7 additions & 0 deletions src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit d966658

Please sign in to comment.