diff --git a/src/RedirectablePrint.cpp b/src/RedirectablePrint.cpp index d1972d2ad..de9b95027 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; } @@ -103,4 +110,4 @@ size_t RedirectablePrint::logDebug(const char *format, ...) } return r; -} \ No newline at end of file +} 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); } }