Skip to content

Commit

Permalink
Macro to trace log all MeshPackets as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
thebentern committed Jul 26, 2024
1 parent 4ee15d8 commit 9e632b3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/RedirectablePrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ size_t RedirectablePrint::write(uint8_t c)
size_t RedirectablePrint::vprintf(const char *logLevel, const char *format, va_list arg)
{
va_list copy;
#ifdef ENABLE_JSON_LOGGING
static char printBuf[512];
#else
static char printBuf[160];
#endif

va_copy(copy, arg);
size_t len = vsnprintf(printBuf, sizeof(printBuf), format, copy);
Expand Down Expand Up @@ -98,6 +102,8 @@ void RedirectablePrint::log_to_serial(const char *logLevel, const char *format,
Print::write("\u001b[33m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_ERROR) == 0)
Print::write("\u001b[31m", 6);
if (strcmp(logLevel, MESHTASTIC_LOG_LEVEL_TRACE) == 0)
Print::write("\u001b[35m", 6);
uint32_t rtc_sec = getValidTime(RTCQuality::RTCQualityDevice, true); // display local time on logfile
if (rtc_sec > 0) {
long hms = rtc_sec % SEC_PER_DAY;
Expand Down
11 changes: 11 additions & 0 deletions src/mesh/Router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
#if !MESHTASTIC_EXCLUDE_MQTT
#include "mqtt/MQTT.h"
#endif
#ifdef ENABLE_JSON_LOGGING
#include "serialization/MeshPacketSerializer.h"
#endif
/**
* Router todo
*
Expand Down Expand Up @@ -356,6 +359,9 @@ bool perhapsDecode(meshtastic_MeshPacket *p)
} */

printPacket("decoded message", p);
#ifdef ENABLE_JSON_LOGGING
LOG_TRACE("%s\n", MeshPacketSerializer::JsonSerialize(p, false).c_str());
#endif
return true;
}
}
Expand Down Expand Up @@ -491,6 +497,11 @@ void Router::handleReceived(meshtastic_MeshPacket *p, RxSource src)

void Router::perhapsHandleReceived(meshtastic_MeshPacket *p)
{
#ifdef ENABLE_JSON_LOGGING
// Even ignored packets get logged in the trace
p->rx_time = getValidTime(RTCQualityFromNet); // store the arrival timestamp for the phone
LOG_TRACE("%s\n", MeshPacketSerializer::JsonSerializeEncrypted(p).c_str());
#endif
// assert(radioConfig.has_preferences);
bool ignore = is_in_repeated(config.lora.ignore_incoming, p->from) || (config.lora.ignore_mqtt && p->via_mqtt);

Expand Down
34 changes: 33 additions & 1 deletion src/serialization/MeshPacketSerializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#endif
#include "mesh/generated/meshtastic/remote_hardware.pb.h"

std::string MeshPacketSerializer::JsonSerialize(meshtastic_MeshPacket *mp, bool shouldLog)
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.
Expand Down Expand Up @@ -312,6 +312,38 @@ std::string MeshPacketSerializer::JsonSerialize(meshtastic_MeshPacket *mp, bool
if (shouldLog)
LOG_INFO("serialized json message: %s\n", jsonStr.c_str());

delete value;
return jsonStr;
}

std::string MeshPacketSerializer::JsonSerializeEncrypted(const meshtastic_MeshPacket *mp)
{
JSONObject jsonObj;

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["want_ack"] = new JSONValue(mp->want_ack);

if (mp->rx_rssi != 0)
jsonObj["rssi"] = new JSONValue((int)mp->rx_rssi);
if (mp->rx_snr != 0)
jsonObj["snr"] = new JSONValue((float)mp->rx_snr);
if (mp->hop_start != 0 && mp->hop_limit <= mp->hop_start) {
jsonObj["hops_away"] = new JSONValue((unsigned int)(mp->hop_start - mp->hop_limit));
jsonObj["hop_start"] = new JSONValue((unsigned int)(mp->hop_start));
}
jsonObj["size"] = new JSONValue((unsigned int)mp->encrypted.size);
// jsonObj["bytes"] = new JSONValue((unsigned int)mp->encrypted.bytes);
std::string encryptedStr(reinterpret_cast<const char *>(mp->encrypted.bytes), mp->encrypted.size);
jsonObj["bytes"] = new JSONValue(encryptedStr);

// serialize and write it to the stream
JSONValue *value = new JSONValue(jsonObj);
std::string jsonStr = value->Stringify();

delete value;
return jsonStr;
}
3 changes: 2 additions & 1 deletion src/serialization/MeshPacketSerializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
class MeshPacketSerializer
{
public:
static std::string JsonSerialize(meshtastic_MeshPacket *mp, bool shouldLog = true);
static std::string JsonSerialize(const meshtastic_MeshPacket *mp, bool shouldLog = true);
static std::string JsonSerializeEncrypted(const meshtastic_MeshPacket *mp);
};

0 comments on commit 9e632b3

Please sign in to comment.