firmware/src/serialization/MeshPacketSerializer.h
Ben Meadors 811a9ae261
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>
2024-07-28 19:49:10 -05:00

23 lines
721 B
C++

#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(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;
}
};