Skip to content

Commit

Permalink
Remove MQTT JSON for all but NRF52_USE_JSON target
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern committed Dec 5, 2024
1 parent c3d6034 commit 2038edc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/mqtt/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
#include <WiFi.h>
#endif
#include "Default.h"

#if NRF52_USE_JSON
#include "serialization/JSON.h"
#include "serialization/MeshPacketSerializer.h"
#endif
#include <Throttle.h>
#include <assert.h>

Expand Down Expand Up @@ -52,6 +55,7 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
meshtastic_ServiceEnvelope e = meshtastic_ServiceEnvelope_init_default;

if (moduleConfig.mqtt.json_enabled && (strncmp(topic, jsonTopic.c_str(), jsonTopic.length()) == 0)) {
#if NRF52_USE_JSON
// check if this is a json payload message by comparing the topic start
char payloadStr[length + 1];
memcpy(payloadStr, payload, length);
Expand Down Expand Up @@ -135,6 +139,7 @@ void MQTT::onReceive(char *topic, byte *payload, size_t length)
LOG_ERROR("JSON received payload on MQTT but not a valid JSON");
}
delete json_value;
#endif
} else {
if (length == 0) {
LOG_WARN("Empty MQTT payload received, topic %s!", topic);
Expand Down Expand Up @@ -234,11 +239,15 @@ MQTT::MQTT() : concurrency::OSThread("mqtt"), mqttQueue(MAX_MQTT_QUEUE)

if (*moduleConfig.mqtt.root) {
cryptTopic = moduleConfig.mqtt.root + cryptTopic;
#if NRF52_USE_JSON
jsonTopic = moduleConfig.mqtt.root + jsonTopic;
#endif
mapTopic = moduleConfig.mqtt.root + mapTopic;
} else {
cryptTopic = "msh" + cryptTopic;
#if NRF52_USE_JSON
jsonTopic = "msh" + jsonTopic;
#endif
mapTopic = "msh" + mapTopic;
}

Expand Down Expand Up @@ -705,6 +714,7 @@ void MQTT::perhapsReportToMap()
}
}

#if NRF52_USE_JSON
bool MQTT::isValidJsonEnvelope(JSONObject &json)
{
// if "sender" is provided, avoid processing packets we uplinked
Expand All @@ -715,6 +725,7 @@ bool MQTT::isValidJsonEnvelope(JSONObject &json)
(json.find("type") != json.end()) && json["type"]->IsString() && // should specify a type
(json.find("payload") != json.end()); // should have a payload
}
#endif

bool MQTT::isPrivateIpAddress(const char address[])
{
Expand Down Expand Up @@ -780,4 +791,4 @@ bool MQTT::isPrivateIpAddress(const char address[])

int octet2Num = atoi(octet2);
return octet2Num >= 16 && octet2Num <= 31;
}
}
7 changes: 5 additions & 2 deletions src/mqtt/MQTT.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include "concurrency/OSThread.h"
#include "mesh/Channels.h"
#include "mesh/generated/meshtastic/mqtt.pb.h"
#if NRF52_USE_JSON
#include "serialization/JSON.h"
#endif
#if HAS_WIFI
#include <WiFiClient.h>
#if !defined(ARCH_PORTDUINO)
Expand Down Expand Up @@ -117,9 +119,10 @@ class MQTT : private concurrency::OSThread
// Check if we should report unencrypted information about our node for consumption by a map
void perhapsReportToMap();

#if NRF52_USE_JSON
// returns true if this is a valid JSON envelope which we accept on downlink
bool isValidJsonEnvelope(JSONObject &json);

#endif
/// Determines if the given address is a private IPv4 address, i.e. not routable on the public internet.
/// These are the ranges: 127.0.0.1, 10.0.0.0-10.255.255.255, 172.16.0.0-172.31.255.255, 192.168.0.0-192.168.255.255.
bool isPrivateIpAddress(const char address[]);
Expand All @@ -130,4 +133,4 @@ class MQTT : private concurrency::OSThread

void mqttInit();

extern MQTT *mqtt;
extern MQTT *mqtt;

0 comments on commit 2038edc

Please sign in to comment.