#pragma once // CBA NOTE If headers for classes referenced in this file are not included here, // then they MUST be included BEFORE this header is included. #include "Transport.h" #include "Type.h" #include #include #include #include #include namespace ArduinoJson { // ArduinoJSON serialization support for std::vector template struct Converter> { static void toJson(const std::vector& src, JsonVariant dst) { TRACE("<<< Serializing vector"); JsonArray array = dst.to(); for (T item : src) array.add(item); TRACE("<<< Finished serializing vector"); } static std::vector fromJson(JsonVariantConst src) { TRACE("<<< Deserializing vector"); std::vector dst; for (T item : src.as()) dst.push_back(item); TRACE("<<< Finished deserializing vector"); return dst; } static bool checkJson(JsonVariantConst src) { JsonArrayConst array = src; bool result = array; for (JsonVariantConst item : array) result &= item.is(); return result; } }; // ArduinoJSON serialization support for std::set template struct Converter> { static void toJson(const std::set& src, JsonVariant dst) { JsonArray array = dst.to(); for (T item : src) array.add(item); } static std::set fromJson(JsonVariantConst src) { std::set dst; for (T item : src.as()) dst.insert(item); return dst; } static bool checkJson(JsonVariantConst src) { JsonArrayConst array = src; bool result = array; for (JsonVariantConst item : array) result &= item.is(); return result; } }; // ArduinoJSON serialization support for std::map template struct Converter> { static void toJson(const std::map& src, JsonVariant dst) { JsonObject obj = dst.to(); for (const auto& item : src) obj[item.first] = item.second; } static std::map fromJson(JsonVariantConst src) { std::map dst; for (JsonPairConst item : src.as()) dst[item.key().c_str()] = item.value().as(); return dst; } static bool checkJson(JsonVariantConst src) { JsonObjectConst obj = src; bool result = obj; for (JsonPairConst item : obj) result &= item.value().is(); return result; } }; // ArduinoJSON serialization support for std::map template struct Converter> { static void toJson(const std::map& src, JsonVariant dst) { JsonObject obj = dst.to(); for (const auto& item : src) { //obj[item.first] = item.second; obj[item.first.toHex()] = item.second; } } static std::map fromJson(JsonVariantConst src) { std::map dst; for (JsonPairConst item : src.as()) { //dst[item.key().c_str()] = item.value().as(); RNS::Bytes key; key.assignHex(item.key().c_str()); //dst[key] = item.value().as(); dst.insert({key, item.value().as()}); } return dst; } static bool checkJson(JsonVariantConst src) { JsonObjectConst obj = src; bool result = obj; for (JsonPairConst item : obj) { result &= item.value().is(); } return result; } }; /* // ArduinoJSON serialization support for RNS::Bytes template <> struct Converter { static bool toJson(const RNS::Bytes& src, JsonVariant dst) { //TRACE("<<< Serializing Bytes"); //TRACE("<<< Finished serializing Bytes"); return true; } static RNS::Bytes fromJson(JsonVariantConst src) { Bytes bytes; //TRACE(">>> Deserializing Bytes"); //TRACE(">>> Finished deserializing Bytes"); return bytes; } static bool checkJson(JsonVariantConst src) { return src.is(); } }; */ /* // ArduinoJSON serialization support for RNS::Interface template <> struct Converter { static bool toJson(const RNS::Interface& src, JsonVariant dst) { if (!src) { return dst.set(nullptr); } //TRACEF("<<< Serializing interface hash %s", src.get_hash().toHex().c_str()); return dst.set(src.get_hash().toHex()); } static RNS::Interface fromJson(JsonVariantConst src) { if (!src.isNull()) { RNS::Bytes hash; hash.assignHex(src.as()); //TRACEF(">>> Deserialized interface hash %s", hash.toHex().c_str()); //TRACE(">>> Querying transport for interface"); // Query transport for matching interface return RNS::Transport::find_interface_from_hash(hash); } else { return {RNS::Type::NONE}; } } static bool checkJson(JsonVariantConst src) { return src.is() && strlen(src.as()) == 64; } }; */ /* // ArduinoJSON serialization support for RNS::Packet template <> struct Converter { static bool toJson(const RNS::Packet& src, JsonVariant dst) { if (!src) { return dst.set(nullptr); } //TRACEF("<<< Serializing packet hash %s", src.get_hash().toHex().c_str()); // Whenever a reference to a packet is serialized we must ensure that packet itself also gets serialized separately RNS::Transport::cache_packet(src, true); return dst.set(src.get_hash().toHex()); } static RNS::Packet fromJson(JsonVariantConst src) { if (!src.isNull()) { RNS::Bytes hash; hash.assignHex(src.as()); //TRACEF(">>> Deserialized packet hash %s", hash.toHex().c_str()); //TRACE(">>> Querying transport for cached packet"); // Query transport for matching packet return RNS::Transport::get_cached_packet(hash); } else { return {RNS::Type::NONE}; } } static bool checkJson(JsonVariantConst src) { return src.is() && strlen(src.as()) == 64; } }; */ // ArduinoJSON serialization support for RNS::Packet template <> struct Converter { static bool toJson(const RNS::Packet& src, JsonVariant dst) { //TRACE("<<< Serializing Packet"); //dst["hash"] = src.get_hash(); dst["raw"] = src.raw(); dst["sent_at"] = src.sent_at(); //TRACE("<<< Finished serializing Packet"); return true; } static RNS::Packet fromJson(JsonVariantConst src) { //TRACE(">>> Deserializing Packet"); RNS::Bytes raw = src["raw"]; //RNS::Bytes raw = src["raw"].as(); RNS::Packet packet(RNS::Destination(RNS::Type::NONE), raw); //packet.set_hash(src["hash"]); packet.sent_at(src["sent_at"]); // set cached flag since packet was read from cache packet.cached(true); //TRACE(">>> Finished deserializing Packet"); return packet; } static bool checkJson(JsonVariantConst src) { return //src["hash"].is() && src["raw"].is() && src["sent_at"].is(); } }; // ArduinoJSON serialization support for RNS::Transport::PacketEntry template <> struct Converter { static bool toJson(const RNS::Transport::PacketEntry& src, JsonVariant dst) { //TRACE("<<< Serializing Transport::PacketEntry"); //dst["hash"] = src._hash; dst["raw"] = src._raw; dst["sent_at"] = src._sent_at; dst["destination_hash"] = src._destination_hash; //TRACE("<<< Finished serializing Transport::PacketEntry"); return true; } static RNS::Transport::PacketEntry fromJson(JsonVariantConst src) { //TRACE(">>> Deserializing Transport::PacketEntry"); RNS::Transport::PacketEntry dst; //dst._hash = src["hash"]; dst._raw = src["raw"]; dst._sent_at = src["sent_at"]; dst._destination_hash = src["destination_hash"]; // set cached flag since pcket was read from cache dst._cached = true; //TRACE(">>> Finished deserializing Transport::PacketEntry"); return dst; } static bool checkJson(JsonVariantConst src) { return //src["hash"].is() && src["raw"].is() && src["sent_at"].is() && src["destination_hash"].is(); } }; #if 1 // ArduinoJSON serialization support for RNS::Transport::DestinationEntry template <> struct Converter { static bool toJson(const RNS::Transport::DestinationEntry& src, JsonVariant dst) { //TRACE("<<< Serializing Transport::DestinationEntry"); dst["timestamp"] = src._timestamp; dst["received_from"] = src._received_from; dst["announce_hops"] = src._hops; dst["expires"] = src._expires; dst["random_blobs"] = src._random_blobs; /* if (src._receiving_interface) { dst["interface_hash"] = src._receiving_interface.get_hash(); } else { dst["interface_hash"] = nullptr; } */ dst["interface_hash"] = src.receiving_interface_hash(); // CBA TODO Move packet serialization to *after* destination table serialization since packets are useless // anyway if there's no space left to write destination table. // Whenever a reference to a packet is serialized we must ensure that packet itself also gets serialized separately /* if (src._announce_packet) { dst["packet_hash"] = src._announce_packet.get_hash(); // Only cache packet if not already cached if (!src._announce_packet.cached()) { if (RNS::Transport::cache_packet(src._announce_packet, true)) { const_cast(src._announce_packet).cached(true); } } else { TRACEF("Destination announce packet %s is already cached", src._announce_packet.get_hash().toHex().c_str()); } } else { dst["packet_hash"] = nullptr; } */ dst["packet_hash"] = src.announce_packet_hash(); //TRACE("<<< Finished Serializing Transport::DestinationEntry"); return true; } static RNS::Transport::DestinationEntry fromJson(JsonVariantConst src) { //TRACE(">>> Deserializing Transport::DestinationEntry"); RNS::Transport::DestinationEntry dst; dst._timestamp = src["timestamp"]; dst._received_from = src["received_from"]; dst._hops = src["announce_hops"]; dst._expires = src["expires"]; dst._random_blobs = src["random_blobs"].as>(); /* RNS::Bytes interface_hash = src["interface_hash"]; if (interface_hash) { // Query transport for matching interface dst._receiving_interface = RNS::Transport::find_interface_from_hash(interface_hash); } */ dst.receiving_interface_hash(src["interface_hash"]); /* RNS::Bytes packet_hash = src["packet_hash"]; if (packet_hash) { // Query transport for matching cached packet dst._announce_packet = RNS::Transport::get_cached_packet(packet_hash); } */ dst.announce_packet_hash(src["packet_hash"]); /* //RNS::Transport::DestinationEntry dst(src["timestamp"], src["received_from"], src["announce_hops"], src["expires"], src["random_blobs"], src["receiving_interface"], src["packet"]); RNS::Transport::DestinationEntry dst( src["timestamp"].as(), src["received_from"].as(), src["announce_hops"].as(), src["expires"].as(), src["random_blobs"].as>(), src["receiving_interface"].as(), src["packet"].as() ); */ //TRACE(">>> Finished Deserializing Transport::DestinationEntry"); return dst; } static bool checkJson(JsonVariantConst src) { return src["timestamp"].is() && src["received_from"].is() && src["announce_hops"].is() && src["expires"].is() && src["random_blobs"].is>() && src["interface_hash"].is() && src["packet_hash"].is(); } }; #endif } #if 0 namespace RNS { inline bool convertToJson(const Transport::DestinationEntry& src, JsonVariant dst) { //TRACE("<<< NEW Serializing Transport::DestinationEntry"); dst["timestamp"] = src._timestamp; dst["received_from"] = src._received_from; dst["announce_hops"] = src._hops; dst["expires"] = src._expires; dst["random_blobs"] = src._random_blobs; /* if (src._receiving_interface) { dst["interface_hash"] = src._receiving_interface.get_hash(); } else { dst["interface_hash"] = nullptr; } */ dst["interface_hash"] = src.receiving_interface_hash(); /* // CBA TODO Move packet serialization to *after* destination table serialization since packets are useless // anyway if there's no space to left to write destination table. // Whenever a reference to a packet is serialized we must ensure that packet itself also gets serialized separately //dst["packet"] = src._announce_packet; if (src._announce_packet) { dst["packet_hash"] = src._announce_packet.get_hash(); // Only cache packet if not already cached if (!src._announce_packet.cached()) { if (RNS::Transport::cache_packet(src._announce_packet, true)) { const_cast(src._announce_packet).cached(true); } } else { TRACEF("Destination announce packet %s is already cached", src._announce_packet.get_hash().toHex().c_str()); } } else { dst["packet_hash"] = nullptr; } */ dst["packet_hash"] = src.announce_packet_hash(); //TRACE("<<< Finished Serializing Transport::DestinationEntry"); return true; } inline void convertFromJson(JsonVariantConst src, Transport::DestinationEntry& dst) { //TRACE(">>> NEW Deserializing Transport::DestinationEntry"); dst._timestamp = src["timestamp"]; dst._received_from = src["received_from"]; dst._hops = src["announce_hops"]; dst._expires = src["expires"]; dst._random_blobs = src["random_blobs"].as>(); /* RNS::Bytes interface_hash = src["interface_hash"]; if (interface_hash) { // Query transport for matching interface dst._receiving_interface = RNS::Transport::find_interface_from_hash(interface_hash); } */ dst.receiving_interface_hash(src["interface_hash"]); /* RNS::Bytes packet_hash = src["packet_hash"]; if (packet_hash) { // Query transport for matching packet dst._announce_packet = RNS::Transport::get_cached_packet(packet_hash); } */ dst.announce_packet_hash(src["packet_hash"]); /* //RNS::Transport::DestinationEntry dst(src["timestamp"], src["received_from"], src["announce_hops"], src["expires"], src["random_blobs"], src["receiving_interface"], src["packet"]); RNS::Transport::DestinationEntry dst( src["timestamp"].as(), src["received_from"].as(), src["announce_hops"].as(), src["expires"].as(), src["random_blobs"].as>(), src["receiving_interface"].as(), src["packet"].as() ); */ //TRACE(">>> Finished Deserializing Transport::DestinationEntry"); } inline bool canConvertFromJson(JsonVariantConst src, const Transport::DestinationEntry&) { TRACE("=== NEW Checking Transport::DestinationEntry"); return src["timestamp"].is() && src["received_from"].is() && src["announce_hops"].is() && src["expires"].is() && src["random_blobs"].is>() && src["interface_hash"].is() && src["packet_hash"].is(); } } #endif namespace RNS { namespace Persistence { #if defined(BOARD_HAS_PSRAM) && defined(ESP32) // PSRAM allocator for ArduinoJson — prevents deserialization failures // under heap pressure (heap can be <1KB free while PSRAM has 8+ MB). class PsramAllocator : public ArduinoJson::Allocator { public: void* allocate(size_t size) override { void* p = ps_malloc(size); if (!p) p = malloc(size); // fallback to heap if PSRAM unavailable return p; } void deallocate(void* ptr) override { free(ptr); // free() handles both heap and PSRAM on ESP32 } void* reallocate(void* ptr, size_t new_size) override { void* p = ps_realloc(ptr, new_size); if (!p) p = realloc(ptr, new_size); // fallback return p; } }; static PsramAllocator _psramAllocator; static JsonDocument _document(&_psramAllocator); #else //static DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); static JsonDocument _document; #endif static Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); template uint32_t crc(const T& obj) { //TRACE("Persistence::crc"); _document.set(obj); size_t size = _buffer.capacity(); #ifdef USE_MSGPACK size_t length = serializeMsgPack(_document, _buffer.writable(size), size); #else size_t length = serializeJson(_document, _buffer.writable(size), size); #endif if (length < size) { _buffer.resize(length); } //TRACEF("Persistence::crc: serialized %d bytes", length); return Utilities::Crc::crc32(0, _buffer.data(), _buffer.size()); } template size_t serialize(const T& obj, const char* file_path) { //TRACE("Persistence::serialize"); _document.set(obj); size_t size = _buffer.capacity(); #ifdef USE_MSGPACK size_t length = serializeMsgPack(_document, _buffer.writable(size), size); #else size_t length = serializeJson(_document, _buffer.writable(size), size); #endif if (length < size) { _buffer.resize(length); } TRACEF("Persistence::serialize: serialized %d bytes", length); size_t wrote = 0; if (length > 0) { wrote = RNS::Utilities::OS::write_file(file_path, _buffer); if (wrote == _buffer.size()) { TRACEF("Persistence::serialize: wrote %d bytes", _buffer.size()); } else { TRACE("Persistence::serialize: write failed"); return 0; } } else { TRACE("Persistence::serialize: failed to serialize"); return 0; } return wrote; } template size_t deserialize(T& obj, const char* file_path) { //TRACE("Persistence::deserialize"); size_t read = RNS::Utilities::OS::read_file(file_path, _buffer); if (read > 0) { TRACEF("Persistence::deserialize: read: %d bytes", _buffer.size()); //TRACEF("testDeserializeVector: data: %s", _buffer.toString().c_str()); #ifdef USE_MSGPACK DeserializationError error = deserializeMsgPack(_document, _buffer.data()); #else DeserializationError error = deserializeJson(_document, _buffer.data()); #endif if (!error) { TRACE("Persistence::deserialize: successfully deserialized document"); obj = _document.as(); // CBA Following obj check doesn't work when T is a collection //if (obj) { return read; //} TRACE("Persistence::deserialize: failed to compose object"); } else { TRACE("Persistence::deserialize: failed to deserialize"); } } else { TRACE("Persistence::deserialize: read failed"); } return 0; } #if 1 template uint32_t crc(std::map& map) { //TRACE("Persistence::crc>"); uint32_t crc = 0; crc = Utilities::Crc::crc32(crc, '{'); for (const auto& [key, value] : map) { crc = Utilities::Crc::crc32(crc, '"'); std::string hex = key.toHex(); crc = Utilities::Crc::crc32(crc, hex.c_str()); crc = Utilities::Crc::crc32(crc, '"'); crc = Utilities::Crc::crc32(crc, ':'); _document.set(value); size_t size = _buffer.capacity(); #ifdef USE_MSGPACK size_t length = serializeMsgPack(_document, _buffer.writable(size), size); #else size_t length = serializeJson(_document, _buffer.writable(size), size); #endif if (length < size) { _buffer.resize(length); } //TRACEF("Persistence::crc: serialized entry %d bytes", length); if (length > 0) { crc = Utilities::Crc::crc32(crc, _buffer.data(), _buffer.size()); } else { // if failed to serialize entry then write empty entry crc = Utilities::Crc::crc32(crc, "{}"); } crc = Utilities::Crc::crc32(crc, ','); } return Utilities::Crc::crc32(crc, '}'); } template size_t serialize(std::map& map, const char* file_path, uint32_t& crc) { //TRACE("Persistence::serialize>"); // CBA TODO: Use stream here instead to avoid having to buffer entire structure RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_WRITE); if (!stream) { TRACE("Persistence::serialize: failed to open write stream"); return 0; } stream.write('{'); for (const auto& [key, value] : map) { stream.write('"'); std::string hex = key.toHex(); stream.write(hex.c_str()); stream.write('"'); stream.write(':'); _document.set(value); #ifdef USE_MSGPACK size_t length = serializeMsgPack(_document, stream); #else size_t length = serializeJson(_document, stream); #endif TRACEF("Persistence::serialize: serialized entry %d bytes", length); if (length == 0) { // if failed to serialize entry then write empty entry stream.write("{}"); } stream.write(','); } stream.write('}'); stream.flush(); TRACEF("Persistence::serialize: stream size: %d bytes", stream.size()); crc = stream.crc(); return stream.size(); } template size_t serialize(std::map& map, const char* file_path) { uint32_t crc; return serialize(map, file_path, crc); } template size_t deserialize(std::map& map, const char* file_path, uint32_t& crc) { //TRACE("Persistence::deserialize>"); // CBA TODO: Use stream here instead to avoid having to buffer entire structure RNS::FileStream stream = RNS::Utilities::OS::open_file(file_path, RNS::FileStream::MODE_READ); if (!stream) { TRACE("Persistence::deserialize: failed to open read stream"); return 0; } TRACEF("Persistence::deserialize: size: %d bytes", stream.size()); map.clear(); if (stream.size() == 0) { TRACE("Persistence::deserialize: read stream is empty"); return 0; } // find opening brace if (stream.find('{')) { char key_str[RNS::Type::Reticulum::DESTINATION_LENGTH*2+1] = ""; do { key_str[0] = 0; // find map key opening quote if (stream.find('"')) { if (stream.readBytesUntil('"', key_str, sizeof(key_str)) > 0) { Bytes key; key.assignHex(key_str); TRACEF("Persistence::deserialize: key: %s", key.toHex().c_str()); if (stream.find(':')) { #ifdef USE_MSGPACK //DeserializationError error = deserializeMsgPack(_document, _buffer.data()); DeserializationError error = deserializeMsgPack(_document, stream); #else //DeserializationError error = deserializeJson(_document, _buffer.data()); DeserializationError error = deserializeJson(_document, stream); #endif if (!error) { TRACE("Persistence::deserialize: successfully deserialized entry"); T obj = _document.as(); // CBA Following obj check doesn't work when T is a collection //if (obj) { // TRACE("Persistence::deserialize: failed to compose object"); // break; //} map.insert({key, obj}); } else { TRACE("Persistence::deserialize: failed to deserialize entry"); //break; } if (!stream.find(',')) { break; } } } } } while (key_str[0] != 0); } crc = stream.crc(); return stream.size(); } template size_t deserialize(std::map& map, const char* file_path) { uint32_t crc; return deserialize(map, file_path, crc); } #endif } }