From 27433edb191778a0c610b74290c118b8ee993619 Mon Sep 17 00:00:00 2001 From: Chad Attermann Date: Mon, 9 Feb 2026 13:52:13 -0700 Subject: [PATCH] Updates for MCU builds broken by previous dev. --- README.md | 55 +++++++++ .../UniversalFileSystem.cpp | 2 +- .../UniversalFileSystem.h | 7 +- examples/link_native/main.cpp | 2 +- examples/link_native/platformio.ini | 2 +- examples/lora_announce/main.cpp | 1 + examples/lora_announce/platformio.ini | 24 ++-- examples/udp_announce/main.cpp | 3 +- examples/udp_announce/platformio.ini | 13 +- platformio.ini | 4 + src/Log.cpp | 14 ++- src/Log.h | 2 +- src/Utilities/OS.cpp | 40 +++++-- src/Utilities/OS.h | 2 +- src/Utilities/Persistence.cpp | 3 +- src/Utilities/Persistence.h | 8 +- test/common/filesystem/FileSystem.h | 2 +- test/test_persistence/test_main.cpp | 112 +++++------------- 18 files changed, 172 insertions(+), 124 deletions(-) diff --git a/README.md b/README.md index ec371de..dbc05f6 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,14 @@ This API is dependent on the following external libraries: - [ArduinoJson](https://github.com/bblanchon/ArduinoJson) - [rweather/Crypto](https://github.com/rweather/arduinolibs) +## Build Options + +- `-DRNS_MEM_LOG` Used to enable logging of low-level memory operations for debug purposes +- `-DRNS_USE_FS` Used to enable use of file system by RNS for persistence +- `-DRNS_PERSIST_PATHS` Used to enable persistence of RNS paths in file system (also requires `-DRNS_USE_FS`) +- `-DRNS_USE_TLSF=1` Enables the use of the TLSF (Two-Level Segregate Fit) dynamic memory manager for efficient management of constrained MCU memory with minimal fragmentation. Currently only required on NRF52 boards (ESP32 already uses TLSF internally). +- `-DRNS_USE_ALLOCATOR=1` Enables the replacement of default new/delete operators with custom implementations that take advantage of optimized memory managers (eg, TLSF). Currently only required on NRF52 boards (ESP32 already uses TLSF internally). + ## Building Building and uploading to hardware is simple through the VSCode PlatformIO IDE @@ -21,6 +29,53 @@ Building and uploading to hardware is simple through the VSCode PlatformIO IDE Instructions for command line builds and building of individual example applications coming soon. +## PlatformIO Command Line + +Clean all environments (boards): +``` +pio run -t clean +``` + +Full Clean (including libdeps) all environments (boards): +``` +pio run -t fullclean +``` + +Run unit tests: +``` +pio test +pio test -f test_msgpack +pio test -f test_msgpack -e native +pio test -f test_msgpack -e native17 +``` + +Build a single environment (board): +``` +pio run -e ttgo-t-beam +pio run -e wiscore_rak4631 +``` + +Build and upload a single environment (board): +``` +pio run -e ttgo-t-beam -t upload +pio run -e wiscore_rak4631 -t upload +``` + +Build and package a single environment (board): +``` +pio run -e ttgo-t-beam -t package +pio run -e wiscore_rak4631 -t package +``` + +Build all environments (boards): +``` +pio run +``` + +## Known Issues + +Use of the custom allocator (enabled by `-DRNS_USE_ALLOCATOR=1`) in conjunction with TLSF (enabled by `-DRNS_USE_TLSF=1`) is not currently working in ESP32 due to TLFS initialization failure at the time that allocator initialization takes place. This causes the allocator to fallback to using regular malloc/free instead of using TLFS as intended. + ## Roadmap - [x] Framework for simple C++ API that mimics Python reference implementation with implicit object sharing diff --git a/examples/common/universal_filesystem/UniversalFileSystem.cpp b/examples/common/universal_filesystem/UniversalFileSystem.cpp index 50ec26c..80b0ae3 100644 --- a/examples/common/universal_filesystem/UniversalFileSystem.cpp +++ b/examples/common/universal_filesystem/UniversalFileSystem.cpp @@ -287,7 +287,7 @@ void UniversalFileSystem::listDir(const char* dir) { TRACEF("open_file: successfully opened file %s", file_path); return RNS::FileStream(new UniversalFileStream(file)); #else - #warning("unsuppoprted"); + #warning("unsupported"); return RNS::FileStream(RNS::Type::NONE); #endif #else // ARDUINO diff --git a/examples/common/universal_filesystem/UniversalFileSystem.h b/examples/common/universal_filesystem/UniversalFileSystem.h index 3d00e44..eacf4d2 100644 --- a/examples/common/universal_filesystem/UniversalFileSystem.h +++ b/examples/common/universal_filesystem/UniversalFileSystem.h @@ -25,7 +25,8 @@ using namespace Adafruit_LittleFS_Namespace; class UniversalFileSystem : public RNS::FileSystemImpl { public: - UniversalFileSystem() {} + UniversalFileSystem() { TRACE("UniversalFileSystem CREATED"); } + virtual ~UniversalFileSystem() { TRACE("UniversalFileSystem DESTROYED"); } public: static void listDir(const char* dir); @@ -55,8 +56,8 @@ protected: bool _closed = false; public: - UniversalFileStream(File* file) : RNS::FileStreamImpl(), _file(file) {} - virtual ~UniversalFileStream() { if (!_closed) close(); } + UniversalFileStream(File* file) : RNS::FileStreamImpl(), _file(file) { TRACE("UniversalFileStream CREATED"); } + virtual ~UniversalFileStream() { if (!_closed) close(); TRACE("UniversalFileStream DESTROYED"); } public: inline virtual const char* name() { return _file->name(); } diff --git a/examples/link_native/main.cpp b/examples/link_native/main.cpp index e62304b..fd34d87 100644 --- a/examples/link_native/main.cpp +++ b/examples/link_native/main.cpp @@ -328,7 +328,7 @@ void cleanup_handler(int signum) { // starts up the desired program mode. int main(int argc, char *argv[]) { -#if defined(MEM_LOG) +#if defined(RNS_MEM_LOG) RNS::loglevel(RNS::LOG_MEM); #else RNS::loglevel(RNS::LOG_NOTICE); diff --git a/examples/link_native/platformio.ini b/examples/link_native/platformio.ini index bd4cfc8..3cd43b2 100644 --- a/examples/link_native/platformio.ini +++ b/examples/link_native/platformio.ini @@ -20,7 +20,7 @@ build_type = debug build_flags = -DRNS_USE_ALLOCATOR=1 -DRNS_USE_TLSF=1 - ;-DMEM_LOG + ;-DRNS_MEM_LOG lib_deps = ArduinoJson@^7.4.2 MsgPack@^0.4.2 diff --git a/examples/lora_announce/main.cpp b/examples/lora_announce/main.cpp index 98fcc21..7faba98 100644 --- a/examples/lora_announce/main.cpp +++ b/examples/lora_announce/main.cpp @@ -143,6 +143,7 @@ void reticulum_setup() { //RNS::Reticulum reticulum; reticulum = RNS::Reticulum(); reticulum.transport_enabled(true); + reticulum.probe_destination_enabled(true); reticulum.start(); //return; // 21.9% (+0.1%) diff --git a/examples/lora_announce/platformio.ini b/examples/lora_announce/platformio.ini index 96be99c..7a18e51 100644 --- a/examples/lora_announce/platformio.ini +++ b/examples/lora_announce/platformio.ini @@ -14,23 +14,21 @@ src_dir = . [env] monitor_speed = 115200 -upload_speed = 921600 +upload_speed = 460800 build_type = debug build_flags = -Wall -Wno-missing-field-initializers -Wno-format -I. - ;-DMEM_LOG=1 - -DRNS_USE_ALLOCATOR=1 - -DRNS_USE_TLSF=1 - ;-DTEST_OBJECT_DATA=1 - -DTEST_OBJECT_IMPL=1 + ;-DRNS_MEM_LOG=1 + -DRNS_USE_FS + -DRNS_PERSIST_PATHS lib_deps = - ArduinoJson@^6.21.3 + ArduinoJson@^7.4.2 + MsgPack@^0.4.2 sandeepmistry/LoRa @ ^0.8.0 https://github.com/attermann/Crypto.git - https://github.com/hideakitai/MsgPack.git microReticulum=symlink://../.. universal_filesystem=symlink://../common/universal_filesystem lora_interface=symlink://../common/lora_interface @@ -39,12 +37,12 @@ lib_deps = framework = arduino platform = espressif32 board = ttgo-lora32-v21 -upload_speed = 460800 board_build.partitions = no_ota.csv build_flags = ${env.build_flags} -Wextra -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder @@ -59,6 +57,7 @@ build_flags = ${env.build_flags} -Wextra -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder @@ -73,6 +72,7 @@ build_flags = ${env.build_flags} -Wextra -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder @@ -89,6 +89,8 @@ build_flags = -fexceptions -DBOARD_NRF52 -DRNS_USE_TLSF=1 + -DRNS_USE_ALLOCATOR=1 + -DMSGPACK_USE_BOOST=OFF build_unflags = -fno-exceptions lib_deps = ${env.lib_deps} @@ -105,6 +107,8 @@ build_flags = -fexceptions -DBOARD_NRF52 -DRNS_USE_TLSF=1 + -DRNS_USE_ALLOCATOR=1 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} @@ -119,5 +123,7 @@ build_flags = -fexceptions -DBOARD_NRF52 -DRNS_USE_TLSF=1 + -DRNS_USE_ALLOCATOR=1 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} diff --git a/examples/udp_announce/main.cpp b/examples/udp_announce/main.cpp index 76feaeb..25cf5d2 100644 --- a/examples/udp_announce/main.cpp +++ b/examples/udp_announce/main.cpp @@ -136,6 +136,7 @@ void reticulum_setup() { //RNS::Reticulum reticulum; reticulum = RNS::Reticulum(); reticulum.transport_enabled(true); + reticulum.probe_destination_enabled(true); reticulum.start(); //return; // 21.9% (+0.1%) @@ -269,7 +270,7 @@ void setup() { */ #endif -#if defined(MEM_LOG) +#if defined(RNS_MEM_LOG) RNS::loglevel(RNS::LOG_MEM); #else //RNS::loglevel(RNS::LOG_WARNING); diff --git a/examples/udp_announce/platformio.ini b/examples/udp_announce/platformio.ini index 940df75..2013779 100644 --- a/examples/udp_announce/platformio.ini +++ b/examples/udp_announce/platformio.ini @@ -15,16 +15,14 @@ src_dir = . [env] monitor_speed = 115200 -upload_speed = 921600 +upload_speed = 460800 build_type = debug build_flags = - -DRNS_USE_ALLOCATOR=1 - -DRNS_USE_TLSF=1 - ;-DMEM_LOG + ;-DRNS_MEM_LOG lib_deps = - ArduinoJson@^6.21.3 + ArduinoJson@^7.4.2 + MsgPack@^0.4.2 https://github.com/attermann/Crypto.git - https://github.com/hideakitai/MsgPack.git microReticulum=symlink://../.. universal_filesystem=symlink://../common/universal_filesystem udp_interface=symlink://../common/udp_interface @@ -106,6 +104,7 @@ build_flags = -Wno-format -I. -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder @@ -124,6 +123,7 @@ build_flags = -Wno-format -I. -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder @@ -142,6 +142,7 @@ build_flags = -Wno-format -I. -DBOARD_ESP32 + -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} monitor_filters = esp32_exception_decoder diff --git a/platformio.ini b/platformio.ini index 33936d9..6a9fb6e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -71,6 +71,7 @@ framework = arduino monitor_speed = 115200 build_flags = ${env.build_flags} + -DBOARD_ESP32 -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} @@ -84,6 +85,7 @@ framework = arduino monitor_speed = 115200 build_flags = ${env.build_flags} + -DBOARD_ESP32 -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} @@ -97,6 +99,7 @@ framework = arduino monitor_speed = 115200 build_flags = ${env.build_flags} + -DBOARD_ESP32 -DMSGPACK_USE_BOOST=OFF lib_deps = ${env.lib_deps} @@ -110,6 +113,7 @@ monitor_speed = 115200 build_flags = ${env.build_flags} -fexceptions + -DBOARD_NRF52 -DRNS_USE_ALLOCATOR=1 -DRNS_USE_TLSF=1 -DMSGPACK_USE_BOOST=OFF diff --git a/src/Log.cpp b/src/Log.cpp index d15fa5f..2da9a51 100644 --- a/src/Log.cpp +++ b/src/Log.cpp @@ -85,12 +85,14 @@ void RNS::doLog(LogLevel level, const char* msg) { return; } #ifdef ARDUINO - Serial.print(getTimeString()); - Serial.print(" ["); - Serial.print(getLevelName(level)); - Serial.print("] "); - Serial.println(msg); - Serial.flush(); + if (Serial) { + Serial.print(getTimeString()); + Serial.print(" ["); + Serial.print(getLevelName(level)); + Serial.print("] "); + Serial.println(msg); + Serial.flush(); + } #else printf("%s [%s] %s\n", getTimeString(), getLevelName(level), msg); #endif diff --git a/src/Log.h b/src/Log.h index 9026996..5caacbb 100644 --- a/src/Log.h +++ b/src/Log.h @@ -29,7 +29,7 @@ #define DEBUGF(msg, ...) (RNS::debugf(msg, __VA_ARGS__)) #define TRACE(msg) (RNS::trace(msg)) #define TRACEF(msg, ...) (RNS::tracef(msg, __VA_ARGS__)) - #if defined(MEM_LOG) + #if defined(RNS_MEM_LOG) #define MEM(msg) (RNS::mem(msg)) #define MEMF(msg, ...) (RNS::memf(msg, __VA_ARGS__)) #else diff --git a/src/Utilities/OS.cpp b/src/Utilities/OS.cpp index c46c322..f2f318c 100644 --- a/src/Utilities/OS.cpp +++ b/src/Utilities/OS.cpp @@ -23,7 +23,8 @@ using namespace RNS::Utilities; #define BUFFER_FRACTION 0 #endif -bool tlsf_init = false; +bool _tlsf_init = false; +//char _tlsf_msg[256] = ""; size_t _buffer_size = BUFFER_SIZE; size_t _contiguous_size = 0; @@ -44,29 +45,46 @@ void* operator new(size_t size) { //__attribute__((weak)) void* operator new(size_t size) { #if defined(RNS_USE_TLSF) //if (OS::_tlsf == nullptr) { - if (!tlsf_init) { - tlsf_init = true; + if (!_tlsf_init) { + _tlsf_init = true; #if defined(ESP32) + // CBA Still unknown why the call to tlsf_create_with_pool() is so flaky on ESP32 with calculated buffer size. Reuires more research and unit tests. _contiguous_size = ESP.getMaxAllocHeap(); + TRACEF("contiguous_size: %u", _contiguous_size); + if (_buffer_size == 0) { + // CBA NOTE Using fp mathhere somehow causes tlsf_create_with_pool() to fail. + //_buffer_size = (size_t)(_contiguous_size * BUFFER_FRACTION); + // Compute 80% exactly using integers + _buffer_size = (_contiguous_size * 4) / 5; + } + // Round DOWN to TLSF alignment + size_t align = tlsf_align_size(); + _buffer_size &= ~(align - 1); + void* raw_buffer = (void*)aligned_alloc(align, _buffer_size); #elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT) _contiguous_size = dbgHeapFree(); -#endif TRACEF("contiguous_size: %u", _contiguous_size); - if (_buffer_size == 0) { - _buffer_size = (size_t)((_contiguous_size * BUFFER_FRACTION) / 1024) * 1024; + _buffer_size = (size_t)(_contiguous_size * BUFFER_FRACTION); } + // For NRF52 round to kB + _buffer_size = (size_t)(_buffer_size / 1024) * 1024; TRACEF("buffer_size: %u", _buffer_size); - - //void* raw_buffer = malloc(BUFFER_SIZE); void* raw_buffer = malloc(_buffer_size); +#endif if (raw_buffer == nullptr) { ERROR("-- allocation for tlsf FAILED"); + //strcpy(_tlsf_msg, "-- allocation for tlsf FAILED!!!"); } else { #if 1 - //OS::_tlsf = tlsf_create_with_pool(raw_buffer, BUFFER_SIZE); OS::_tlsf = tlsf_create_with_pool(raw_buffer, _buffer_size); + //if (OS::_tlsf == nullptr) { + // sprintf(_tlsf_msg, "initialization of tlsf with align=%d, contiguous=%d, size=%d FAILED!!!", tlsf_align_size(), _contiguous_size, _buffer_size); + //} + //else { + // sprintf(_tlsf_msg, "initialization of tlsf with align=%d, contiguous=%d, size=%d SUCCESSFUL!!!", tlsf_align_size(), _contiguous_size, _buffer_size); + //} #else Serial.print("raw_buffer: "); Serial.println((long)raw_buffer); @@ -169,6 +187,10 @@ void dump_tlsf_stats() { _tlsf_free_count = 0; _tlsf_free_size = 0; _tlsf_free_max_size = 0; + //TRACEF("TLSF Message: %s", _tlsf_msg); + if (OS::_tlsf == nullptr) { + return; + } tlsf_walk_pool(tlsf_get_pool(OS::_tlsf), tlsf_mem_walker, nullptr); HEAD("TLSF Stats", LOG_TRACE); TRACEF("Buffer Size: %u", _buffer_size); diff --git a/src/Utilities/OS.h b/src/Utilities/OS.h index 1316dfc..0ccb9d5 100644 --- a/src/Utilities/OS.h +++ b/src/Utilities/OS.h @@ -114,7 +114,7 @@ namespace RNS { namespace Utilities { return is_big_endian() ? val : swap32(val); } - #if defined(RNS_USE_ALLOCATOR) +#if defined(RNS_USE_ALLOCATOR) static void dump_allocator_stats(); #endif diff --git a/src/Utilities/Persistence.cpp b/src/Utilities/Persistence.cpp index bdde3ca..4c835a3 100644 --- a/src/Utilities/Persistence.cpp +++ b/src/Utilities/Persistence.cpp @@ -4,5 +4,6 @@ using namespace RNS; -/*static*/ DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); +/*static*/ //DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); +/*static*/ JsonDocument _document; /*static*/ Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); diff --git a/src/Utilities/Persistence.h b/src/Utilities/Persistence.h index 8aa5ac9..8e77ae5 100644 --- a/src/Utilities/Persistence.h +++ b/src/Utilities/Persistence.h @@ -464,7 +464,8 @@ namespace RNS { namespace RNS { namespace Persistence { - static DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); + //static DynamicJsonDocument _document(Type::Persistence::DOCUMENT_MAXSIZE); + static JsonDocument _document; static Bytes _buffer(Type::Persistence::BUFFER_MAXSIZE); template size_t crc(const T& obj) { @@ -637,6 +638,11 @@ namespace RNS { namespace Persistence { 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] = ""; diff --git a/test/common/filesystem/FileSystem.h b/test/common/filesystem/FileSystem.h index b47a064..4f6fd15 100644 --- a/test/common/filesystem/FileSystem.h +++ b/test/common/filesystem/FileSystem.h @@ -312,7 +312,7 @@ public: TRACEF("open_file: successfully opened file %s", file_path); return RNS::FileStream(new FileStreamImpl(file)); #else - #warning("unsuppoprted"); + #warning("unsupported"); return RNS::FileStream(RNS::Type::NONE); #endif #else // ARDUINO diff --git a/test/test_persistence/test_main.cpp b/test/test_persistence/test_main.cpp index a135dfb..ed6d6fe 100644 --- a/test/test_persistence/test_main.cpp +++ b/test/test_persistence/test_main.cpp @@ -186,7 +186,7 @@ void testSerializeObject() { TestObject test("test", vec); TRACE("testSerializeObject: test: " + test.toString()); - StaticJsonDocument<1024> doc; + JsonDocument doc; doc.set(test); RNS::Bytes data; @@ -215,8 +215,7 @@ void testSerializeObject() { void testDeserializeObject() { - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_object_path, data) > 0) { @@ -260,7 +259,7 @@ void testSerializeVector() { vector.push_back({"two", tvec}); vector.push_back({"one", tvec}); - StaticJsonDocument<4096> doc; + JsonDocument doc; //copyArray(vector, doc.createNestedArray("vector")); //doc["vector"] = vector; //copyArray(vector, doc); @@ -312,14 +311,7 @@ void testSerializeVector() { void testDeserializeVector() { - // Compute the required size - const int capacity = - JSON_ARRAY_SIZE(2) + - 2*JSON_OBJECT_SIZE(3) + - 4*JSON_OBJECT_SIZE(1); - - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_vector_path, data) > 0) { @@ -368,7 +360,7 @@ void testSerializeSet() { set.insert({"two", tvec}); set.insert({"one", tvec}); - StaticJsonDocument<4096> doc; + JsonDocument doc; //copyArray(set, doc.createNestedArray("set")); //doc["set"] = set; //copyArray(set, doc); @@ -408,14 +400,7 @@ void testSerializeSet() { void testDeserializeSet() { - // Compute the required size - const int capacity = - JSON_ARRAY_SIZE(2) + - 2*JSON_OBJECT_SIZE(3) + - 4*JSON_OBJECT_SIZE(1); - - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_set_path, data) > 0) { @@ -465,7 +450,7 @@ void testSerializeMap() { map.insert({"two", {"two", tvec}}); map.insert({"one", {"one", tvec}}); - StaticJsonDocument<4096> doc; + JsonDocument doc; //copyArray(map, doc.createNestedArray("map")); //doc["map"] = map; //copyArray(map, doc); @@ -497,14 +482,7 @@ void testSerializeMap() { void testDeserializeMap() { - // Compute the required size - const int capacity = - JSON_ARRAY_SIZE(2) + - 2*JSON_OBJECT_SIZE(3) + - 4*JSON_OBJECT_SIZE(1); - - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_map_path, data) > 0) { @@ -594,36 +572,13 @@ void testSerializeDestinationTable() { TRACE("testSerializeDestinationTable: entry: " + hash.toHex() + " = " + test.debugString()); } - //StaticJsonDocument<1024> doc; - //doc.set(map); - TRACE("testSerializeDestinationTable: JSON_OBJECT_SIZE " + std::to_string(JSON_OBJECT_SIZE(1)) + " bytes"); - TRACE("testSerializeDestinationTable: JSON_ARRAY_SIZE " + std::to_string(JSON_ARRAY_SIZE(1)) + " bytes"); - int calcsize = 0; - for (auto& [destination_hash, destination_entry] : map) { - // entry - calcsize += JSON_ARRAY_SIZE(1); - // map key - calcsize += JSON_OBJECT_SIZE(1); - // entry (+1) - calcsize += JSON_OBJECT_SIZE(7+1); - // blobs - calcsize += JSON_ARRAY_SIZE(destination_entry._random_blobs.size()); - } - TRACE("testSerializeDestinationTable: calcsize " + std::to_string(calcsize) + " bytes"); - const int BUFFER_SIZE = (JSON_OBJECT_SIZE(9) + JSON_ARRAY_SIZE(2)) * map.size() + JSON_ARRAY_SIZE(map.size()); - TRACE("testSerializeDestinationTable: BUFFER_SIZE " + std::to_string(BUFFER_SIZE) + " bytes"); - //DynamicJsonDocument doc(32768); - //DynamicJsonDocument doc(BUFFER_SIZE); - DynamicJsonDocument doc(calcsize); + JsonDocument doc; doc.set(map); TRACE("testSerializeDestinationTable: doc size " + std::to_string(doc.size())); - TRACE("testSerializeDestinationTable: doc memory " + std::to_string(doc.memoryUsage())); RNS::Bytes data; - //size_t size = 32768; + size_t size = 32768; //size_t size = BUFFER_SIZE; - size_t size = calcsize; - //size_t size = doc.memoryUsage(); TRACE("testSerializeDestinationTable: buffer size " + std::to_string(size) + " bytes"); size_t length = serializeJson(doc, data.writable(size), size); //size_t length = serializeMsgPack(doc, data.writable(size), size); @@ -632,7 +587,7 @@ void testSerializeDestinationTable() { } TRACE("testSerializeDestinationTable: serialized " + std::to_string(length) + " bytes"); if (length > 0) { - TRACE("json: " + data.toString()); + TRACE("testSerializeDestinationTable: json: " + data.toString()); if (RNS::Utilities::OS::write_file(test_destination_table_path, data) == data.size()) { TRACE("testSerializeDestinationTable: wrote: " + std::to_string(data.size()) + " bytes"); } @@ -653,20 +608,16 @@ void testDeserializeDestinationTable() { RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_destination_table_path, data) > 0) { TRACE("testDeserializeDestinationTable: read: " + std::to_string(data.size()) + " bytes"); - //StaticJsonDocument<8192> doc; - //DynamicJsonDocument doc(1024); - int calcsize = data.size() * 2.0; - TRACE("testDeserializeDestinationTable: calcsize " + std::to_string(calcsize) + " bytes"); - DynamicJsonDocument doc(calcsize); + TRACE("testDeserializeDestinationTable: json: " + data.toString()); + JsonDocument doc; //TRACE("testDeserializeVector: data: " + data.toString()); DeserializationError error = deserializeJson(doc, data.data()); //DeserializationError error = deserializeMsgPack(doc, data.data()); TRACE("testDeserializeDestinationTable: doc size " + std::to_string(doc.size())); - TRACE("testDeserializeDestinationTable: doc memory " + std::to_string(doc.memoryUsage())); if (!error) { TRACE("testDeserializeDestinationTable: successfully deserialized " + std::to_string(data.size()) + " bytes"); - TRACE("json: " + data.toString()); + //TRACE("testDeserializeDestinationTable: json: " + data.toString()); static std::map map(doc.as>()); TEST_ASSERT_EQUAL_size_t(22, map.size()); @@ -680,7 +631,7 @@ void testDeserializeDestinationTable() { //} } else { - TRACE("testDeserializeDestinationTable: failed to deserialize"); + TRACEF("testDeserializeDestinationTable: failed to deserialize: %s", error.c_str()); TEST_FAIL(); } //assert(RNS::Utilities::OS::remove_file(test_destination_table_path)); @@ -701,20 +652,15 @@ void testDeserializeEmptyDestinationTable() { RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_empty_destination_table_path, data) > 0) { TRACE("testDeserializeEmptyDestinationTable: read: " + std::to_string(data.size()) + " bytes"); - //StaticJsonDocument<8192> doc; - //DynamicJsonDocument doc(1024); - int calcsize = data.size() * 2.0; - TRACE("testDeserializeEmptyDestinationTable: calcsize " + std::to_string(calcsize) + " bytes"); - DynamicJsonDocument doc(calcsize); + JsonDocument doc; //TRACE("testDeserializeVector: data: " + data.toString()); DeserializationError error = deserializeJson(doc, data.data()); //DeserializationError error = deserializeMsgPack(doc, data.data()); TRACE("testDeserializeEmptyDestinationTable: doc size " + std::to_string(doc.size())); - TRACE("testDeserializeEmptyDestinationTable: doc memory " + std::to_string(doc.memoryUsage())); if (!error) { TRACE("testDeserializeEmptyDestinationTable: successfully deserialized " + std::to_string(data.size()) + " bytes"); - TRACE("json: " + data.toString()); + TRACE("testDeserializeEmptyDestinationTable: json: " + data.toString()); static std::map map(doc.as>()); TEST_ASSERT_EQUAL_size_t(0, map.size()); @@ -771,7 +717,7 @@ void testJsonMsgpackSerializeObject() { TestObject test("test", vec); TRACE("testJsonMsgpackSerializeObject: test: " + test.toString()); - StaticJsonDocument<1024> doc; + JsonDocument doc; doc.set(test); RNS::Bytes data; @@ -799,8 +745,7 @@ void testJsonMsgpackSerializeObject() { void testJsonMsgpackDeserializeObject() { - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_object_path, data) > 0) { @@ -843,7 +788,7 @@ void testJsonMsgpackSerializeArray() { //const std::string two("second"); //TRACEF("testJsonMsgpackSerializeArray: time=%f one=%s two=%s", time, one.c_str(), two.c_str()); - StaticJsonDocument<1024> doc; + JsonDocument doc; JsonArray array = doc.to(); array.add(time); @@ -883,7 +828,7 @@ void testJsonMsgpackSerializeArray() { void testJsonMsgpackDeserializeArray() { - StaticJsonDocument<1024> doc; + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_array_path, data) > 0) { @@ -926,8 +871,7 @@ void testMsgpackSerializeObject() { TestObject test("test", vec); TRACE("testMsgpackSerializeObject: test: " + test.toString()); - StaticJsonDocument<1024> doc; - doc.set(test); + JsonDocument doc; RNS::Bytes data; size_t size = 1024; @@ -954,8 +898,7 @@ void testMsgpackSerializeObject() { void testMsgpackDeserializeObject() { - //StaticJsonDocument<8192> doc; - DynamicJsonDocument doc(1024); + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_object_path, data) > 0) { @@ -1027,7 +970,7 @@ void testMsgpackSerializeArray() { void testMsgpackDeserializeArray() { - StaticJsonDocument<1024> doc; + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_array_path, data) > 0) { @@ -1094,7 +1037,7 @@ void testMsgpackSerializeSeries() { void testMsgpackDeserializeSeries() { - StaticJsonDocument<1024> doc; + JsonDocument doc; RNS::Bytes data; if (RNS::Utilities::OS::read_file(test_series_path, data) > 0) { @@ -1126,6 +1069,11 @@ void testMsgpackDeserializeSeries() { void setUp(void) { // set stuff up here before each test +#ifdef ARDUINO + Serial.begin(115200); + while (!Serial) { ; } // important for USB boards + delay(1000); +#endif } void tearDown(void) {