Macro to trace log all MeshPackets as JSON (#4336)

* Macro to trace log all MeshPackets as JSON

* Comment

* Add trace logging to file for native target

* bytes to hex

* Add time_ms

---------

Co-authored-by: Jonathan Bennett <jbennett@incomsystems.biz>
This commit is contained in:
Ben Meadors
2024-07-28 19:49:10 -05:00
committed by GitHub
parent 8b0208d1c6
commit 811a9ae261
7 changed files with 113 additions and 6 deletions

View File

@@ -1,8 +1,23 @@
#include <meshtastic/mesh.pb.h>
#include <string>
static const char hexChars[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
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);
private:
static std::string bytesToHex(const uint8_t *bytes, int len)
{
std::string result = "";
for (int i = 0; i < len; ++i) {
char const byte = bytes[i];
result += hexChars[(byte & 0xF0) >> 4];
result += hexChars[(byte & 0x0F) >> 0];
}
return result;
}
};