Remove fragile JSON libraries from the firmware while retaining Meshtasticd JSON support (#10152)

This commit is contained in:
Thomas Göttgens
2026-06-03 16:47:30 +02:00
committed by GitHub
parent fe23dcfa3a
commit f86cb7781e
30 changed files with 724 additions and 2830 deletions
+166 -172
View File
@@ -1,11 +1,12 @@
#ifndef NRF52_USE_JSON
#if ARCH_PORTDUINO
#include "MeshPacketSerializer.h"
#include "JSON.h"
#include "NodeDB.h"
#include "mesh/generated/meshtastic/mqtt.pb.h"
#include "mesh/generated/meshtastic/telemetry.pb.h"
#include "modules/RoutingModule.h"
#include <DebugConfiguration.h>
#include <json/json.h>
#include <memory>
#include <mesh-pb-constants.h>
#if defined(ARCH_ESP32)
#include "../mesh/generated/meshtastic/paxcount.pb.h"
@@ -15,41 +16,49 @@
static const char *errStr = "Error decoding proto for %s message!";
static std::string writeCompact(const Json::Value &v)
{
Json::StreamWriterBuilder b;
b["indentation"] = "";
b["emitUTF8"] = true;
return Json::writeString(b, v);
}
static bool tryParseJson(const char *s, Json::Value &out)
{
Json::CharReaderBuilder b;
std::unique_ptr<Json::CharReader> reader(b.newCharReader());
std::string errs;
const char *end = s + strlen(s);
return reader->parse(s, end, &out, &errs);
}
std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp, bool shouldLog)
{
// the created jsonObj is immutable after creation, so
// we need to do the heavy lifting before assembling it.
std::string msgType;
JSONObject jsonObj;
Json::Value jsonObj(Json::objectValue);
if (mp->which_payload_variant == meshtastic_MeshPacket_decoded_tag) {
JSONObject msgPayload;
Json::Value msgPayload(Json::objectValue);
switch (mp->decoded.portnum) {
case meshtastic_PortNum_TEXT_MESSAGE_APP: {
msgType = "text";
// convert bytes to string
if (shouldLog)
LOG_DEBUG("got text message of size %u", mp->decoded.payload.size);
char payloadStr[(mp->decoded.payload.size) + 1];
memcpy(payloadStr, mp->decoded.payload.bytes, mp->decoded.payload.size);
payloadStr[mp->decoded.payload.size] = 0; // null terminated string
// check if this is a JSON payload
JSONValue *json_value = JSON::Parse(payloadStr);
if (json_value != NULL) {
payloadStr[mp->decoded.payload.size] = 0;
Json::Value parsed;
if (tryParseJson(payloadStr, parsed)) {
if (shouldLog)
LOG_INFO("text message payload is of type json");
// if it is, then we can just use the json object
jsonObj["payload"] = json_value;
jsonObj["payload"] = parsed;
} else {
// if it isn't, then we need to create a json object
// with the string as the value
if (shouldLog)
LOG_INFO("text message payload is of type plaintext");
msgPayload["text"] = new JSONValue(payloadStr);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["text"] = payloadStr;
jsonObj["payload"] = msgPayload;
}
break;
}
@@ -61,133 +70,129 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Telemetry_msg, &scratch)) {
decoded = &scratch;
if (decoded->which_variant == meshtastic_Telemetry_device_metrics_tag) {
// If battery is present, encode the battery level value
// TODO - Add a condition to send a code for a non-present value
if (decoded->variant.device_metrics.has_battery_level) {
msgPayload["battery_level"] = new JSONValue((int)decoded->variant.device_metrics.battery_level);
msgPayload["battery_level"] = (int)decoded->variant.device_metrics.battery_level;
}
msgPayload["voltage"] = new JSONValue(decoded->variant.device_metrics.voltage);
msgPayload["channel_utilization"] = new JSONValue(decoded->variant.device_metrics.channel_utilization);
msgPayload["air_util_tx"] = new JSONValue(decoded->variant.device_metrics.air_util_tx);
msgPayload["uptime_seconds"] = new JSONValue((unsigned int)decoded->variant.device_metrics.uptime_seconds);
msgPayload["voltage"] = decoded->variant.device_metrics.voltage;
msgPayload["channel_utilization"] = decoded->variant.device_metrics.channel_utilization;
msgPayload["air_util_tx"] = decoded->variant.device_metrics.air_util_tx;
msgPayload["uptime_seconds"] = (Json::UInt)decoded->variant.device_metrics.uptime_seconds;
} else if (decoded->which_variant == meshtastic_Telemetry_environment_metrics_tag) {
// Avoid sending 0s for sensors that could be 0
if (decoded->variant.environment_metrics.has_temperature) {
msgPayload["temperature"] = new JSONValue(decoded->variant.environment_metrics.temperature);
msgPayload["temperature"] = decoded->variant.environment_metrics.temperature;
}
if (decoded->variant.environment_metrics.has_relative_humidity) {
msgPayload["relative_humidity"] = new JSONValue(decoded->variant.environment_metrics.relative_humidity);
msgPayload["relative_humidity"] = decoded->variant.environment_metrics.relative_humidity;
}
if (decoded->variant.environment_metrics.has_barometric_pressure) {
msgPayload["barometric_pressure"] =
new JSONValue(decoded->variant.environment_metrics.barometric_pressure);
msgPayload["barometric_pressure"] = decoded->variant.environment_metrics.barometric_pressure;
}
if (decoded->variant.environment_metrics.has_gas_resistance) {
msgPayload["gas_resistance"] = new JSONValue(decoded->variant.environment_metrics.gas_resistance);
msgPayload["gas_resistance"] = decoded->variant.environment_metrics.gas_resistance;
}
if (decoded->variant.environment_metrics.has_voltage) {
msgPayload["voltage"] = new JSONValue(decoded->variant.environment_metrics.voltage);
msgPayload["voltage"] = decoded->variant.environment_metrics.voltage;
}
if (decoded->variant.environment_metrics.has_current) {
msgPayload["current"] = new JSONValue(decoded->variant.environment_metrics.current);
msgPayload["current"] = decoded->variant.environment_metrics.current;
}
if (decoded->variant.environment_metrics.has_lux) {
msgPayload["lux"] = new JSONValue(decoded->variant.environment_metrics.lux);
msgPayload["lux"] = decoded->variant.environment_metrics.lux;
}
if (decoded->variant.environment_metrics.has_white_lux) {
msgPayload["white_lux"] = new JSONValue(decoded->variant.environment_metrics.white_lux);
msgPayload["white_lux"] = decoded->variant.environment_metrics.white_lux;
}
if (decoded->variant.environment_metrics.has_iaq) {
msgPayload["iaq"] = new JSONValue((uint)decoded->variant.environment_metrics.iaq);
msgPayload["iaq"] = (Json::UInt)decoded->variant.environment_metrics.iaq;
}
if (decoded->variant.environment_metrics.has_distance) {
msgPayload["distance"] = new JSONValue(decoded->variant.environment_metrics.distance);
msgPayload["distance"] = decoded->variant.environment_metrics.distance;
}
if (decoded->variant.environment_metrics.has_wind_speed) {
msgPayload["wind_speed"] = new JSONValue(decoded->variant.environment_metrics.wind_speed);
msgPayload["wind_speed"] = decoded->variant.environment_metrics.wind_speed;
}
if (decoded->variant.environment_metrics.has_wind_direction) {
msgPayload["wind_direction"] = new JSONValue((uint)decoded->variant.environment_metrics.wind_direction);
msgPayload["wind_direction"] = (Json::UInt)decoded->variant.environment_metrics.wind_direction;
}
if (decoded->variant.environment_metrics.has_wind_gust) {
msgPayload["wind_gust"] = new JSONValue(decoded->variant.environment_metrics.wind_gust);
msgPayload["wind_gust"] = decoded->variant.environment_metrics.wind_gust;
}
if (decoded->variant.environment_metrics.has_wind_lull) {
msgPayload["wind_lull"] = new JSONValue(decoded->variant.environment_metrics.wind_lull);
msgPayload["wind_lull"] = decoded->variant.environment_metrics.wind_lull;
}
if (decoded->variant.environment_metrics.has_radiation) {
msgPayload["radiation"] = new JSONValue(decoded->variant.environment_metrics.radiation);
msgPayload["radiation"] = decoded->variant.environment_metrics.radiation;
}
if (decoded->variant.environment_metrics.has_ir_lux) {
msgPayload["ir_lux"] = new JSONValue(decoded->variant.environment_metrics.ir_lux);
msgPayload["ir_lux"] = decoded->variant.environment_metrics.ir_lux;
}
if (decoded->variant.environment_metrics.has_uv_lux) {
msgPayload["uv_lux"] = new JSONValue(decoded->variant.environment_metrics.uv_lux);
msgPayload["uv_lux"] = decoded->variant.environment_metrics.uv_lux;
}
if (decoded->variant.environment_metrics.has_weight) {
msgPayload["weight"] = new JSONValue(decoded->variant.environment_metrics.weight);
msgPayload["weight"] = decoded->variant.environment_metrics.weight;
}
if (decoded->variant.environment_metrics.has_rainfall_1h) {
msgPayload["rainfall_1h"] = new JSONValue(decoded->variant.environment_metrics.rainfall_1h);
msgPayload["rainfall_1h"] = decoded->variant.environment_metrics.rainfall_1h;
}
if (decoded->variant.environment_metrics.has_rainfall_24h) {
msgPayload["rainfall_24h"] = new JSONValue(decoded->variant.environment_metrics.rainfall_24h);
msgPayload["rainfall_24h"] = decoded->variant.environment_metrics.rainfall_24h;
}
if (decoded->variant.environment_metrics.has_soil_moisture) {
msgPayload["soil_moisture"] = new JSONValue((uint)decoded->variant.environment_metrics.soil_moisture);
msgPayload["soil_moisture"] = (Json::UInt)decoded->variant.environment_metrics.soil_moisture;
}
if (decoded->variant.environment_metrics.has_soil_temperature) {
msgPayload["soil_temperature"] = new JSONValue(decoded->variant.environment_metrics.soil_temperature);
msgPayload["soil_temperature"] = decoded->variant.environment_metrics.soil_temperature;
}
} else if (decoded->which_variant == meshtastic_Telemetry_air_quality_metrics_tag) {
if (decoded->variant.air_quality_metrics.has_pm10_standard) {
msgPayload["pm10"] = new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm10_standard);
msgPayload["pm10"] = (Json::UInt)decoded->variant.air_quality_metrics.pm10_standard;
}
if (decoded->variant.air_quality_metrics.has_pm25_standard) {
msgPayload["pm25"] = new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm25_standard);
msgPayload["pm25"] = (Json::UInt)decoded->variant.air_quality_metrics.pm25_standard;
}
if (decoded->variant.air_quality_metrics.has_pm100_standard) {
msgPayload["pm100"] = new JSONValue((unsigned int)decoded->variant.air_quality_metrics.pm100_standard);
msgPayload["pm100"] = (Json::UInt)decoded->variant.air_quality_metrics.pm100_standard;
}
if (decoded->variant.air_quality_metrics.has_co2) {
msgPayload["co2"] = new JSONValue((unsigned int)decoded->variant.air_quality_metrics.co2);
msgPayload["co2"] = (Json::UInt)decoded->variant.air_quality_metrics.co2;
}
if (decoded->variant.air_quality_metrics.has_co2_temperature) {
msgPayload["co2_temperature"] = new JSONValue(decoded->variant.air_quality_metrics.co2_temperature);
msgPayload["co2_temperature"] = decoded->variant.air_quality_metrics.co2_temperature;
}
if (decoded->variant.air_quality_metrics.has_co2_humidity) {
msgPayload["co2_humidity"] = new JSONValue(decoded->variant.air_quality_metrics.co2_humidity);
msgPayload["co2_humidity"] = decoded->variant.air_quality_metrics.co2_humidity;
}
if (decoded->variant.air_quality_metrics.has_form_formaldehyde) {
msgPayload["form_formaldehyde"] = new JSONValue(decoded->variant.air_quality_metrics.form_formaldehyde);
msgPayload["form_formaldehyde"] = decoded->variant.air_quality_metrics.form_formaldehyde;
}
if (decoded->variant.air_quality_metrics.has_form_temperature) {
msgPayload["form_temperature"] = new JSONValue(decoded->variant.air_quality_metrics.form_temperature);
msgPayload["form_temperature"] = decoded->variant.air_quality_metrics.form_temperature;
}
if (decoded->variant.air_quality_metrics.has_form_humidity) {
msgPayload["form_humidity"] = new JSONValue(decoded->variant.air_quality_metrics.form_humidity);
msgPayload["form_humidity"] = decoded->variant.air_quality_metrics.form_humidity;
}
} else if (decoded->which_variant == meshtastic_Telemetry_power_metrics_tag) {
if (decoded->variant.power_metrics.has_ch1_voltage) {
msgPayload["voltage_ch1"] = new JSONValue(decoded->variant.power_metrics.ch1_voltage);
msgPayload["voltage_ch1"] = decoded->variant.power_metrics.ch1_voltage;
}
if (decoded->variant.power_metrics.has_ch1_current) {
msgPayload["current_ch1"] = new JSONValue(decoded->variant.power_metrics.ch1_current);
msgPayload["current_ch1"] = decoded->variant.power_metrics.ch1_current;
}
if (decoded->variant.power_metrics.has_ch2_voltage) {
msgPayload["voltage_ch2"] = new JSONValue(decoded->variant.power_metrics.ch2_voltage);
msgPayload["voltage_ch2"] = decoded->variant.power_metrics.ch2_voltage;
}
if (decoded->variant.power_metrics.has_ch2_current) {
msgPayload["current_ch2"] = new JSONValue(decoded->variant.power_metrics.ch2_current);
msgPayload["current_ch2"] = decoded->variant.power_metrics.ch2_current;
}
if (decoded->variant.power_metrics.has_ch3_voltage) {
msgPayload["voltage_ch3"] = new JSONValue(decoded->variant.power_metrics.ch3_voltage);
msgPayload["voltage_ch3"] = decoded->variant.power_metrics.ch3_voltage;
}
if (decoded->variant.power_metrics.has_ch3_current) {
msgPayload["current_ch3"] = new JSONValue(decoded->variant.power_metrics.ch3_current);
msgPayload["current_ch3"] = decoded->variant.power_metrics.ch3_current;
}
}
jsonObj["payload"] = new JSONValue(msgPayload);
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -200,12 +205,12 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_User_msg, &scratch)) {
decoded = &scratch;
msgPayload["id"] = new JSONValue(decoded->id);
msgPayload["longname"] = new JSONValue(decoded->long_name);
msgPayload["shortname"] = new JSONValue(decoded->short_name);
msgPayload["hardware"] = new JSONValue(decoded->hw_model);
msgPayload["role"] = new JSONValue((int)decoded->role);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["id"] = decoded->id;
msgPayload["longname"] = decoded->long_name;
msgPayload["shortname"] = decoded->short_name;
msgPayload["hardware"] = (int)decoded->hw_model;
msgPayload["role"] = (int)decoded->role;
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -219,38 +224,38 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Position_msg, &scratch)) {
decoded = &scratch;
if ((int)decoded->time) {
msgPayload["time"] = new JSONValue((unsigned int)decoded->time);
msgPayload["time"] = (Json::UInt)decoded->time;
}
if ((int)decoded->timestamp) {
msgPayload["timestamp"] = new JSONValue((unsigned int)decoded->timestamp);
msgPayload["timestamp"] = (Json::UInt)decoded->timestamp;
}
msgPayload["latitude_i"] = new JSONValue((int)decoded->latitude_i);
msgPayload["longitude_i"] = new JSONValue((int)decoded->longitude_i);
msgPayload["latitude_i"] = (int)decoded->latitude_i;
msgPayload["longitude_i"] = (int)decoded->longitude_i;
if ((int)decoded->altitude) {
msgPayload["altitude"] = new JSONValue((int)decoded->altitude);
msgPayload["altitude"] = (int)decoded->altitude;
}
if ((int)decoded->ground_speed) {
msgPayload["ground_speed"] = new JSONValue((unsigned int)decoded->ground_speed);
msgPayload["ground_speed"] = (Json::UInt)decoded->ground_speed;
}
if (int(decoded->ground_track)) {
msgPayload["ground_track"] = new JSONValue((unsigned int)decoded->ground_track);
msgPayload["ground_track"] = (Json::UInt)decoded->ground_track;
}
if (int(decoded->sats_in_view)) {
msgPayload["sats_in_view"] = new JSONValue((unsigned int)decoded->sats_in_view);
msgPayload["sats_in_view"] = (Json::UInt)decoded->sats_in_view;
}
if ((int)decoded->PDOP) {
msgPayload["PDOP"] = new JSONValue((int)decoded->PDOP);
msgPayload["PDOP"] = (int)decoded->PDOP;
}
if ((int)decoded->HDOP) {
msgPayload["HDOP"] = new JSONValue((int)decoded->HDOP);
msgPayload["HDOP"] = (int)decoded->HDOP;
}
if ((int)decoded->VDOP) {
msgPayload["VDOP"] = new JSONValue((int)decoded->VDOP);
msgPayload["VDOP"] = (int)decoded->VDOP;
}
if ((int)decoded->precision_bits) {
msgPayload["precision_bits"] = new JSONValue((int)decoded->precision_bits);
msgPayload["precision_bits"] = (int)decoded->precision_bits;
}
jsonObj["payload"] = new JSONValue(msgPayload);
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -263,14 +268,14 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Waypoint_msg, &scratch)) {
decoded = &scratch;
msgPayload["id"] = new JSONValue((unsigned int)decoded->id);
msgPayload["name"] = new JSONValue(decoded->name);
msgPayload["description"] = new JSONValue(decoded->description);
msgPayload["expire"] = new JSONValue((unsigned int)decoded->expire);
msgPayload["locked_to"] = new JSONValue((unsigned int)decoded->locked_to);
msgPayload["latitude_i"] = new JSONValue((int)decoded->latitude_i);
msgPayload["longitude_i"] = new JSONValue((int)decoded->longitude_i);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["id"] = (Json::UInt)decoded->id;
msgPayload["name"] = decoded->name;
msgPayload["description"] = decoded->description;
msgPayload["expire"] = (Json::UInt)decoded->expire;
msgPayload["locked_to"] = (Json::UInt)decoded->locked_to;
msgPayload["latitude_i"] = (int)decoded->latitude_i;
msgPayload["longitude_i"] = (int)decoded->longitude_i;
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -284,26 +289,26 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_NeighborInfo_msg,
&scratch)) {
decoded = &scratch;
msgPayload["node_id"] = new JSONValue((unsigned int)decoded->node_id);
msgPayload["node_broadcast_interval_secs"] = new JSONValue((unsigned int)decoded->node_broadcast_interval_secs);
msgPayload["last_sent_by_id"] = new JSONValue((unsigned int)decoded->last_sent_by_id);
msgPayload["neighbors_count"] = new JSONValue(decoded->neighbors_count);
JSONArray neighbors;
msgPayload["node_id"] = (Json::UInt)decoded->node_id;
msgPayload["node_broadcast_interval_secs"] = (Json::UInt)decoded->node_broadcast_interval_secs;
msgPayload["last_sent_by_id"] = (Json::UInt)decoded->last_sent_by_id;
msgPayload["neighbors_count"] = (Json::UInt)decoded->neighbors_count;
Json::Value neighbors(Json::arrayValue);
for (uint8_t i = 0; i < decoded->neighbors_count; i++) {
JSONObject neighborObj;
neighborObj["node_id"] = new JSONValue((unsigned int)decoded->neighbors[i].node_id);
neighborObj["snr"] = new JSONValue((int)decoded->neighbors[i].snr);
neighbors.push_back(new JSONValue(neighborObj));
Json::Value neighborObj(Json::objectValue);
neighborObj["node_id"] = (Json::UInt)decoded->neighbors[i].node_id;
neighborObj["snr"] = (int)decoded->neighbors[i].snr;
neighbors.append(neighborObj);
}
msgPayload["neighbors"] = new JSONValue(neighbors);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["neighbors"] = neighbors;
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
break;
}
case meshtastic_PortNum_TRACEROUTE_APP: {
if (mp->decoded.request_id) { // Only report the traceroute response
if (mp->decoded.request_id) {
msgType = "traceroute";
meshtastic_RouteDiscovery scratch;
meshtastic_RouteDiscovery *decoded = NULL;
@@ -311,13 +316,12 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_RouteDiscovery_msg,
&scratch)) {
decoded = &scratch;
JSONArray route; // Route this message took
JSONArray routeBack; // Route this message took back
JSONArray snrTowards; // Snr for forward route
JSONArray snrBack; // Snr for reverse route
Json::Value route(Json::arrayValue);
Json::Value routeBack(Json::arrayValue);
Json::Value snrTowards(Json::arrayValue);
Json::Value snrBack(Json::arrayValue);
// Lambda function for adding a long name to the route
auto addToRoute = [](JSONArray *route, NodeNum num) {
auto addToRoute = [](Json::Value *r, NodeNum num) {
char long_name[40] = "Unknown";
const meshtastic_NodeInfoLite *node = nodeDB->getMeshNode(num);
bool name_known = nodeInfoLiteHasUser(node);
@@ -327,33 +331,32 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
memcpy(long_name, node->long_name, copy_len);
long_name[copy_len] = '\0';
}
route->push_back(new JSONValue(long_name));
r->append(long_name);
};
addToRoute(&route, mp->to); // Started at the original transmitter (destination of response)
addToRoute(&route, mp->to);
for (uint8_t i = 0; i < decoded->route_count; i++) {
addToRoute(&route, decoded->route[i]);
}
addToRoute(&route, mp->from); // Ended at the original destination (source of response)
addToRoute(&route, mp->from);
addToRoute(&routeBack, mp->from); // Started at the original destination (source of response)
addToRoute(&routeBack, mp->from);
for (uint8_t i = 0; i < decoded->route_back_count; i++) {
addToRoute(&routeBack, decoded->route_back[i]);
}
addToRoute(&routeBack, mp->to); // Ended at the original transmitter (destination of response)
addToRoute(&routeBack, mp->to);
for (uint8_t i = 0; i < decoded->snr_back_count; i++) {
snrBack.push_back(new JSONValue((float)decoded->snr_back[i] / 4));
snrBack.append((float)decoded->snr_back[i] / 4);
}
for (uint8_t i = 0; i < decoded->snr_towards_count; i++) {
snrTowards.push_back(new JSONValue((float)decoded->snr_towards[i] / 4));
snrTowards.append((float)decoded->snr_towards[i] / 4);
}
msgPayload["route"] = new JSONValue(route);
msgPayload["route_back"] = new JSONValue(routeBack);
msgPayload["snr_back"] = new JSONValue(snrBack);
msgPayload["snr_towards"] = new JSONValue(snrTowards);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["route"] = route;
msgPayload["route_back"] = routeBack;
msgPayload["snr_back"] = snrBack;
msgPayload["snr_towards"] = snrTowards;
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -364,9 +367,9 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
msgType = "detection";
char payloadStr[(mp->decoded.payload.size) + 1];
memcpy(payloadStr, mp->decoded.payload.bytes, mp->decoded.payload.size);
payloadStr[mp->decoded.payload.size] = 0; // null terminated string
msgPayload["text"] = new JSONValue(payloadStr);
jsonObj["payload"] = new JSONValue(msgPayload);
payloadStr[mp->decoded.payload.size] = 0;
msgPayload["text"] = payloadStr;
jsonObj["payload"] = msgPayload;
break;
}
#ifdef ARCH_ESP32
@@ -377,10 +380,10 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
memset(&scratch, 0, sizeof(scratch));
if (pb_decode_from_bytes(mp->decoded.payload.bytes, mp->decoded.payload.size, &meshtastic_Paxcount_msg, &scratch)) {
decoded = &scratch;
msgPayload["wifi_count"] = new JSONValue((unsigned int)decoded->wifi);
msgPayload["ble_count"] = new JSONValue((unsigned int)decoded->ble);
msgPayload["uptime"] = new JSONValue((unsigned int)decoded->uptime);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["wifi_count"] = (Json::UInt)decoded->wifi;
msgPayload["ble_count"] = (Json::UInt)decoded->ble;
msgPayload["uptime"] = (Json::UInt)decoded->uptime;
jsonObj["payload"] = msgPayload;
} else if (shouldLog) {
LOG_ERROR(errStr, msgType.c_str());
}
@@ -396,20 +399,19 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
decoded = &scratch;
if (decoded->type == meshtastic_HardwareMessage_Type_GPIOS_CHANGED) {
msgType = "gpios_changed";
msgPayload["gpio_value"] = new JSONValue((unsigned int)decoded->gpio_value);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["gpio_value"] = (Json::UInt)decoded->gpio_value;
jsonObj["payload"] = msgPayload;
} else if (decoded->type == meshtastic_HardwareMessage_Type_READ_GPIOS_REPLY) {
msgType = "gpios_read_reply";
msgPayload["gpio_value"] = new JSONValue((unsigned int)decoded->gpio_value);
msgPayload["gpio_mask"] = new JSONValue((unsigned int)decoded->gpio_mask);
jsonObj["payload"] = new JSONValue(msgPayload);
msgPayload["gpio_value"] = (Json::UInt)decoded->gpio_value;
msgPayload["gpio_mask"] = (Json::UInt)decoded->gpio_mask;
jsonObj["payload"] = msgPayload;
}
} else if (shouldLog) {
LOG_ERROR(errStr, "RemoteHardware");
}
break;
}
// add more packet types here if needed
default:
break;
}
@@ -417,64 +419,56 @@ std::string MeshPacketSerializer::JsonSerialize(const meshtastic_MeshPacket *mp,
LOG_WARN("Couldn't convert encrypted payload of MeshPacket to JSON");
}
jsonObj["id"] = new JSONValue((unsigned int)mp->id);
jsonObj["timestamp"] = new JSONValue((unsigned int)mp->rx_time);
jsonObj["to"] = new JSONValue((unsigned int)mp->to);
jsonObj["from"] = new JSONValue((unsigned int)mp->from);
jsonObj["channel"] = new JSONValue((unsigned int)mp->channel);
jsonObj["type"] = new JSONValue(msgType.c_str());
jsonObj["sender"] = new JSONValue(nodeDB->getNodeId().c_str());
jsonObj["id"] = (Json::UInt)mp->id;
jsonObj["timestamp"] = (Json::UInt)mp->rx_time;
jsonObj["to"] = (Json::UInt)mp->to;
jsonObj["from"] = (Json::UInt)mp->from;
jsonObj["channel"] = (Json::UInt)mp->channel;
jsonObj["type"] = msgType;
jsonObj["sender"] = nodeDB->getNodeId();
if (mp->rx_rssi != 0)
jsonObj["rssi"] = new JSONValue((int)mp->rx_rssi);
jsonObj["rssi"] = (int)mp->rx_rssi;
if (mp->rx_snr != 0)
jsonObj["snr"] = new JSONValue((float)mp->rx_snr);
jsonObj["snr"] = (float)mp->rx_snr;
const int8_t hopsAway = getHopsAway(*mp);
if (hopsAway >= 0) {
jsonObj["hops_away"] = new JSONValue((unsigned int)(hopsAway));
jsonObj["hop_start"] = new JSONValue((unsigned int)(mp->hop_start));
jsonObj["hops_away"] = (Json::UInt)(hopsAway);
jsonObj["hop_start"] = (Json::UInt)(mp->hop_start);
}
// serialize and write it to the stream
JSONValue *value = new JSONValue(jsonObj);
std::string jsonStr = value->Stringify();
std::string jsonStr = writeCompact(jsonObj);
if (shouldLog)
LOG_INFO("serialized json message: %s", jsonStr.c_str());
delete value;
return jsonStr;
}
std::string MeshPacketSerializer::JsonSerializeEncrypted(const meshtastic_MeshPacket *mp)
{
JSONObject jsonObj;
Json::Value jsonObj(Json::objectValue);
jsonObj["id"] = new JSONValue((unsigned int)mp->id);
jsonObj["time_ms"] = new JSONValue((double)millis());
jsonObj["timestamp"] = new JSONValue((unsigned int)mp->rx_time);
jsonObj["to"] = new JSONValue((unsigned int)mp->to);
jsonObj["from"] = new JSONValue((unsigned int)mp->from);
jsonObj["channel"] = new JSONValue((unsigned int)mp->channel);
jsonObj["want_ack"] = new JSONValue(mp->want_ack);
jsonObj["id"] = (Json::UInt)mp->id;
jsonObj["time_ms"] = (double)millis();
jsonObj["timestamp"] = (Json::UInt)mp->rx_time;
jsonObj["to"] = (Json::UInt)mp->to;
jsonObj["from"] = (Json::UInt)mp->from;
jsonObj["channel"] = (Json::UInt)mp->channel;
jsonObj["want_ack"] = mp->want_ack;
if (mp->rx_rssi != 0)
jsonObj["rssi"] = new JSONValue((int)mp->rx_rssi);
jsonObj["rssi"] = (int)mp->rx_rssi;
if (mp->rx_snr != 0)
jsonObj["snr"] = new JSONValue((float)mp->rx_snr);
jsonObj["snr"] = (float)mp->rx_snr;
const int8_t hopsAway = getHopsAway(*mp);
if (hopsAway >= 0) {
jsonObj["hops_away"] = new JSONValue((unsigned int)(hopsAway));
jsonObj["hop_start"] = new JSONValue((unsigned int)(mp->hop_start));
jsonObj["hops_away"] = (Json::UInt)(hopsAway);
jsonObj["hop_start"] = (Json::UInt)(mp->hop_start);
}
jsonObj["size"] = new JSONValue((unsigned int)mp->encrypted.size);
jsonObj["size"] = (Json::UInt)mp->encrypted.size;
auto encryptedStr = bytesToHex(mp->encrypted.bytes, mp->encrypted.size);
jsonObj["bytes"] = new JSONValue(encryptedStr.c_str());
jsonObj["bytes"] = encryptedStr;
// serialize and write it to the stream
JSONValue *value = new JSONValue(jsonObj);
std::string jsonStr = value->Stringify();
delete value;
return jsonStr;
return writeCompact(jsonObj);
}
#endif