From 46aee8274f1358eecc31f8145873981c469d1097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 18 Oct 2022 11:16:21 +0200 Subject: [PATCH 1/2] fix JSON red herring error messages and Redirectable Print's new fixed buffer. --- src/RedirectablePrint.cpp | 9 ++++++++- src/mqtt/MQTT.cpp | 38 ++++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index d1972d2ad..4d28007d9 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -49,6 +49,13 @@ size_t RedirectablePrint::vprintf(const char *format, va_list arg) if (len < 0) return 0; + // If the resulting string is longer than sizeof(printBuf)-1 characters, the remaining characters are still counted for the return value + + if (len > sizeof(printBuf) - 1) { + len = sizeof(printBuf) - 1; + printBuf[sizeof(printBuf) - 2] = '\n'; + } + len = Print::write(printBuf, len); return len; } @@ -63,7 +70,7 @@ size_t RedirectablePrint::logDebug(const char *format, ...) va_list arg; va_start(arg, format); - // Cope with 0 len format strings, but look for new line terminator + // Cope with 0 len0 format strings, but look for new line terminator bool hasNewline = *format && format[strlen(format) - 1] == '\n'; // If we are the first message on a report, include the header diff --git a/src/mqtt/MQTT.cpp b/src/mqtt/MQTT.cpp index 020f2dbb4..5b314b577 100644 --- a/src/mqtt/MQTT.cpp +++ b/src/mqtt/MQTT.cpp @@ -27,8 +27,9 @@ void MQTT::onPublish(char *topic, byte *payload, unsigned int length) { // parsing ServiceEnvelope ServiceEnvelope e = ServiceEnvelope_init_default; - if (!pb_decode_from_bytes(payload, length, ServiceEnvelope_fields, &e) && moduleConfig.mqtt.json_enabled) { - // check if this is a json payload message + + if (moduleConfig.mqtt.json_enabled && (strncmp(topic, jsonTopic.c_str(), jsonTopic.length()) == 0)) { + // check if this is a json payload message by comparing the topic start using namespace json11; char payloadStr[length + 1]; memcpy(payloadStr, payload, length); @@ -36,36 +37,37 @@ void MQTT::onPublish(char *topic, byte *payload, unsigned int length) std::string err; auto json = Json::parse(payloadStr, err); if (err.empty()) { - DEBUG_MSG("Received json payload on MQTT, parsing..\n"); + DEBUG_MSG("JSON Received on MQTT, parsing..\n"); // check if it is a valid envelope if (json.object_items().count("sender") != 0 && json.object_items().count("payload") != 0) { // this is a valid envelope if (json["sender"].string_value().compare(owner.id) != 0) { std::string jsonPayloadStr = json["payload"].dump(); - DEBUG_MSG("Received json payload %s, length %u\n", jsonPayloadStr.c_str(), jsonPayloadStr.length()); - // FIXME Not sure we need to be doing this + DEBUG_MSG("JSON payload %s, length %u\n", jsonPayloadStr.c_str(), jsonPayloadStr.length()); + // construct protobuf data packet using TEXT_MESSAGE, send it to the mesh - // MeshPacket *p = router->allocForSending(); - // p->decoded.portnum = PortNum_TEXT_MESSAGE_APP; - // if (jsonPayloadStr.length() <= sizeof(p->decoded.payload.bytes)) { - // memcpy(p->decoded.payload.bytes, jsonPayloadStr.c_str(), jsonPayloadStr.length()); - // p->decoded.payload.size = jsonPayloadStr.length(); - // MeshPacket *packet = packetPool.allocCopy(*p); - // service.sendToMesh(packet, RX_SRC_LOCAL); - // } else { - // DEBUG_MSG("Received MQTT json payload too long, dropping\n"); - // } + MeshPacket *p = router->allocForSending(); + p->decoded.portnum = PortNum_TEXT_MESSAGE_APP; + if (jsonPayloadStr.length() <= sizeof(p->decoded.payload.bytes)) { + memcpy(p->decoded.payload.bytes, jsonPayloadStr.c_str(), jsonPayloadStr.length()); + p->decoded.payload.size = jsonPayloadStr.length(); + MeshPacket *packet = packetPool.allocCopy(*p); + service.sendToMesh(packet, RX_SRC_LOCAL); + } else { + DEBUG_MSG("Received MQTT json payload too long, dropping\n"); + } } else { - DEBUG_MSG("Ignoring downlink message we originally sent.\n"); + DEBUG_MSG("JSON Ignoring downlink message we originally sent.\n"); } } else { - DEBUG_MSG("Received json payload on MQTT but not a valid envelope\n"); + DEBUG_MSG("JSON Received payload on MQTT but not a valid envelope\n"); } } else { // no json, this is an invalid payload DEBUG_MSG("Invalid MQTT service envelope, topic %s, len %u!\n", topic, length); } } else { + pb_decode_from_bytes(payload, length, ServiceEnvelope_fields, &e); if (strcmp(e.gateway_id, owner.id) == 0) DEBUG_MSG("Ignoring downlink message we originally sent.\n"); else { @@ -243,7 +245,7 @@ void MQTT::onSend(const MeshPacket &mp, ChannelIndex chIndex) auto jsonString = this->downstreamPacketToJson((MeshPacket *)&mp); if (jsonString.length() != 0) { String topicJson = jsonTopic + channelId + "/" + owner.id; - DEBUG_MSG("publish json message to %s, %u bytes: %s\n", topicJson.c_str(), jsonString.length(), jsonString.c_str()); + DEBUG_MSG("JSON publish message to %s, %u bytes: %s\n", topicJson.c_str(), jsonString.length(), jsonString.c_str()); pubSub.publish(topicJson.c_str(), jsonString.c_str(), false); } } From 01298a7b016f7fb18d297e1a3f050a4e776d0900 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Tue, 18 Oct 2022 11:18:12 +0200 Subject: [PATCH 2/2] Update RedirectablePrint.cpp --- src/RedirectablePrint.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index 4d28007d9..de9b95027 100644 --- a/src/RedirectablePrint.cpp +++ b/src/RedirectablePrint.cpp @@ -70,7 +70,7 @@ size_t RedirectablePrint::logDebug(const char *format, ...) va_list arg; va_start(arg, format); - // Cope with 0 len0 format strings, but look for new line terminator + // Cope with 0 len format strings, but look for new line terminator bool hasNewline = *format && format[strlen(format) - 1] == '\n'; // If we are the first message on a report, include the header @@ -110,4 +110,4 @@ size_t RedirectablePrint::logDebug(const char *format, ...) } return r; -} \ No newline at end of file +}