mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-22 23:10:38 +00:00
WIP: Logging optimizations and memory management
Changed a few key logs to use printf-style instead of string concatenation to avoid dynamic memory allocation. Still need to move all remaining logging over to printf-style. Added option (-DRNS_USE_ALLOCATOR=1) to override new/delete operators for both allocation metrics and the ability to implement an alternate memory allocator. Added option (-DRNS_USE_TLSF=1) to use the alternate TLSF allocator (must also define RNS_USE_ALLOCATOR). This allocator is well known for reducing fragmentation and providing constant, O(1)-time allocation and deallocation for real-time embedded systems. This TLSF allocator appears to already be the default allocator for ESP32 boards, but the allocator used by NRF52 is unknown at this time so the TLSF allocator internal to microReticulum is only being used for NRF52 boards at this time. Future memory management improvements will include custom allocators for long-lived structures like the path table that will segregate them from the general pool of memory that is constantly cycling, thereby further reducing fragmentation. It will also allow these structures to use alternate memory like PSRAM present on T-Beam boards.
This commit is contained in:
1 parent
25741cf235
commit
3af5111606
30 files changed
+2428
-355
No files matched your search
@@ -208,7 +208,7 @@ void reticulum_setup() {
|
||||
HEAD("Ready!", RNS::LOG_TRACE);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_setup: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_setup: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,7 +227,7 @@ void reticulum_teardown() {
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_teardown: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_teardown: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+3
-3
@@ -135,7 +135,7 @@ void run_tests() {
|
||||
//return;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in test: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in test: %s", e.what());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -275,7 +275,7 @@ void reticulum_setup() {
|
||||
HEAD("Ready!", RNS::LOG_TRACE);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_setup: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_setup: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +302,7 @@ void reticulum_teardown() {
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_teardown: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_teardown: %s", e.what());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -20,7 +20,7 @@ using namespace Adafruit_LittleFS_Namespace;
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO
|
||||
void listDir(const char* dir){
|
||||
void Filesystem::listDir(const char* dir) {
|
||||
Serial.print("DIR: ");
|
||||
Serial.println(dir);
|
||||
#ifdef BOARD_ESP32
|
||||
@@ -30,8 +30,13 @@ void listDir(const char* dir){
|
||||
return;
|
||||
}
|
||||
File file = root.openNextFile();
|
||||
while(file){
|
||||
Serial.print(" FILE: ");
|
||||
while (file) {
|
||||
if (file.isDirectory()) {
|
||||
Serial.print(" DIR: ");
|
||||
}
|
||||
else {
|
||||
Serial.print(" FILE: ");
|
||||
}
|
||||
Serial.println(file.name());
|
||||
file.close();
|
||||
file = root.openNextFile();
|
||||
@@ -44,8 +49,13 @@ void listDir(const char* dir){
|
||||
return;
|
||||
}
|
||||
File file = root.openNextFile();
|
||||
while(file){
|
||||
Serial.print(" FILE: ");
|
||||
while (file) {
|
||||
if (file.isDirectory()) {
|
||||
Serial.print(" DIR: ");
|
||||
}
|
||||
else {
|
||||
Serial.print(" FILE: ");
|
||||
}
|
||||
Serial.println(file.name());
|
||||
file.close();
|
||||
file = root.openNextFile();
|
||||
@@ -53,6 +63,9 @@ void listDir(const char* dir){
|
||||
root.close();
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
void Filesystem::listDir(const char* dir) {
|
||||
}
|
||||
#endif
|
||||
|
||||
bool Filesystem::init() {
|
||||
@@ -75,8 +88,8 @@ bool Filesystem::init() {
|
||||
Serial.print(used);
|
||||
Serial.println(" MB");
|
||||
// ensure filesystem is writable and format if not
|
||||
RNS::Bytes test;
|
||||
if (!write_file("/test", test)) {
|
||||
RNS::Bytes test("test");
|
||||
if (write_file("/test", test) < 4) {
|
||||
INFO("SPIFFS filesystem is being formatted, please wait...");
|
||||
SPIFFS.format();
|
||||
}
|
||||
@@ -90,7 +103,6 @@ bool Filesystem::init() {
|
||||
InternalFS.begin();
|
||||
INFO("InternalFS filesystem is ready");
|
||||
#endif
|
||||
listDir("/");
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
@@ -157,7 +169,7 @@ bool Filesystem::init() {
|
||||
//size_t read = fread(data.writable(size), size, 1, file);
|
||||
size_t read = fread(data.writable(size), 1, size, file);
|
||||
#endif
|
||||
TRACE("read_file: read " + std::to_string(read) + " RNS::Bytes from file " + std::string(file_path));
|
||||
TRACE("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path));
|
||||
if (read != size) {
|
||||
ERROR("read_file: failed to read file " + std::string(file_path));
|
||||
data.clear();
|
||||
@@ -181,6 +193,8 @@ bool Filesystem::init() {
|
||||
}
|
||||
|
||||
/*virtual*/ size_t Filesystem::write_file(const char* file_path, const RNS::Bytes& data) {
|
||||
// CBA TODO Replace remove with working truncation
|
||||
remove_file(file_path);
|
||||
size_t wrote = 0;
|
||||
#ifdef ARDUINO
|
||||
#ifdef BOARD_ESP32
|
||||
@@ -201,7 +215,7 @@ bool Filesystem::init() {
|
||||
//size_t wrote = fwrite(data.data(), data.size(), 1, file);
|
||||
size_t wrote = fwrite(data.data(), 1, data.size(), file);
|
||||
#endif
|
||||
TRACE("write_file: wrote " + std::to_string(wrote) + " RNS::Bytes to file " + std::string(file_path));
|
||||
TRACE("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path));
|
||||
if (wrote < data.size()) {
|
||||
WARNING("write_file: not all data was written to file " + std::string(file_path));
|
||||
}
|
||||
|
||||
@@ -8,6 +8,9 @@ class Filesystem : public RNS::Filesystem {
|
||||
public:
|
||||
bool init();
|
||||
|
||||
public:
|
||||
static void listDir(const char* dir);
|
||||
|
||||
protected:
|
||||
virtual bool file_exists(const char* file_path);
|
||||
virtual size_t read_file(const char* file_path, RNS::Bytes& data);
|
||||
|
||||
+143
-64
@@ -140,6 +140,7 @@ void testBytesMain() {
|
||||
}
|
||||
|
||||
// test resize
|
||||
HEAD("TestBytes: resize", RNS::LOG_TRACE);
|
||||
{
|
||||
RNS::Bytes shrink(bytes);
|
||||
shrink.resize(5);
|
||||
@@ -148,60 +149,6 @@ void testBytesMain() {
|
||||
assert(memcmp(shrink.data(), "Hello", shrink.size()) == 0);
|
||||
}
|
||||
|
||||
// stream into empty bytes
|
||||
{
|
||||
RNS::Bytes strmbuf;
|
||||
strmbuf << prebuf << postbuf;
|
||||
TRACE("stream prebuf: " + prebuf.toString());
|
||||
TRACE("stream postbuf: " + postbuf.toString());
|
||||
TRACE("stream strmbuf: " + strmbuf.toString());
|
||||
assert(strmbuf.size() == 11);
|
||||
assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
}
|
||||
|
||||
// stream into populated bytes
|
||||
{
|
||||
RNS::Bytes strmbuf("Stream ");
|
||||
assert(strmbuf);
|
||||
assert(strmbuf.size() == 7);
|
||||
assert(memcmp(strmbuf.data(), "Stream ", strmbuf.size()) == 0);
|
||||
|
||||
strmbuf << prebuf << postbuf;
|
||||
TRACE("stream prebuf: " + prebuf.toString());
|
||||
TRACE("stream postbuf: " + postbuf.toString());
|
||||
TRACE("stream strmbuf: " + strmbuf.toString());
|
||||
assert(strmbuf.size() == 18);
|
||||
assert(memcmp(strmbuf.data(), "Stream Hello World", strmbuf.size()) == 0);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
}
|
||||
|
||||
// stream with assignment
|
||||
// (side effect of also updating pre - this is a known and correct but perhaps unexpected and non-intuitive side-effect of assignment with stream)
|
||||
{
|
||||
// NOTE pre must be volatile in order for below stream with assignment to work (since it gets modified)
|
||||
RNS::Bytes pre("Hello");
|
||||
assert(pre.size() == 5);
|
||||
const RNS::Bytes post(" World");
|
||||
assert(post.size() == 6);
|
||||
RNS::Bytes strmbuf = pre << post;
|
||||
TRACE("stream pre: " + prebuf.toString());
|
||||
TRACE("stream post: " + postbuf.toString());
|
||||
TRACE("stream strmbuf: " + strmbuf.toString());
|
||||
assert(strmbuf.size() == 11);
|
||||
assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0);
|
||||
assert(pre.size() == 11);
|
||||
assert(memcmp(pre.data(), "Hello World", pre.size()) == 0);
|
||||
assert(post.size() == 6);
|
||||
assert(memcmp(post.data(), " World", post.size()) == 0);
|
||||
}
|
||||
|
||||
// test creating bytes from default
|
||||
{
|
||||
RNS::Bytes bytes;
|
||||
@@ -235,11 +182,11 @@ void testBytesMain() {
|
||||
}
|
||||
|
||||
// function default argument
|
||||
TRACE("TestBytes: function default argument");
|
||||
HEAD("TestBytes: function default argument", RNS::LOG_TRACE);
|
||||
testBytesDefault();
|
||||
|
||||
// function reference return
|
||||
TRACE("TestBytes: function reference return");
|
||||
HEAD("TestBytes: function reference return", RNS::LOG_TRACE);
|
||||
{
|
||||
RNS::Bytes test = testBytesReference();
|
||||
TRACE("returned");
|
||||
@@ -326,12 +273,14 @@ void testBytesConversion() {
|
||||
assert(text.length() == 11);
|
||||
assert(text.compare("Hello World") == 0);
|
||||
|
||||
// overwrite
|
||||
bytes.assignHex(hex.c_str());
|
||||
text = bytes.toString();
|
||||
TRACE("hex: \"" + hex + "\" text: \"" + text + "\"");
|
||||
assert(text.length() == 11);
|
||||
assert(text.compare("Hello World") == 0);
|
||||
|
||||
// apend
|
||||
bytes.appendHex(hex.c_str());
|
||||
text = bytes.toString();
|
||||
TRACE("hex: \"" + hex + "\" text: \"" + text + "\"");
|
||||
@@ -362,22 +311,152 @@ void testBytesResize() {
|
||||
assert(bytes.capacity() == 1024);
|
||||
}
|
||||
|
||||
void testBytesStream() {
|
||||
|
||||
const uint8_t prestr[] = "Hello";
|
||||
const uint8_t poststr[] = " World";
|
||||
|
||||
const RNS::Bytes prebuf(prestr, 5);
|
||||
assert(prebuf);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
|
||||
const RNS::Bytes postbuf(poststr, 6);
|
||||
assert(postbuf);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
|
||||
// stream into empty bytes
|
||||
HEAD("TestBytes: stream into empty bytes", RNS::LOG_TRACE);
|
||||
{
|
||||
RNS::Bytes strmbuf;
|
||||
strmbuf << prebuf << postbuf;
|
||||
TRACE("Results:");
|
||||
TRACEF("stream prebuf: %s", prebuf.toString().c_str());
|
||||
TRACEF("stream postbuf: %s", postbuf.toString().c_str());
|
||||
TRACEF("stream strmbuf: %s", strmbuf.toString().c_str());
|
||||
assert(strmbuf.size() == 11);
|
||||
assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
}
|
||||
|
||||
// stream into reserved empty bytes
|
||||
HEAD("TestBytes: stream into reserved empty bytes", RNS::LOG_TRACE);
|
||||
{
|
||||
RNS::Bytes strmbuf(256);
|
||||
//strmbuf << prebuf << postbuf;
|
||||
strmbuf << prebuf;
|
||||
strmbuf << postbuf;
|
||||
TRACE("Results:");
|
||||
//TRACEF("stream prebuf: %s", prebuf.toString().c_str());
|
||||
//TRACEF("stream postbuf: %s", postbuf.toString().c_str());
|
||||
//TRACEF("stream strmbuf: %s", strmbuf.toString().c_str());
|
||||
assert(strmbuf.size() == 11);
|
||||
assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
}
|
||||
|
||||
// stream into populated bytes
|
||||
HEAD("TestBytes: stream into populated bytes", RNS::LOG_TRACE);
|
||||
{
|
||||
RNS::Bytes strmbuf("Stream ");
|
||||
assert(strmbuf);
|
||||
assert(strmbuf.size() == 7);
|
||||
assert(memcmp(strmbuf.data(), "Stream ", strmbuf.size()) == 0);
|
||||
|
||||
strmbuf << prebuf << postbuf;
|
||||
TRACE("Results:");
|
||||
TRACEF("stream prebuf: %s", prebuf.toString().c_str());
|
||||
TRACEF("stream postbuf: %s", postbuf.toString().c_str());
|
||||
TRACEF("stream strmbuf: %s", strmbuf.toString().c_str());
|
||||
assert(strmbuf.size() == 18);
|
||||
assert(memcmp(strmbuf.data(), "Stream Hello World", strmbuf.size()) == 0);
|
||||
assert(prebuf.size() == 5);
|
||||
assert(memcmp(prebuf.data(), "Hello", prebuf.size()) == 0);
|
||||
assert(postbuf.size() == 6);
|
||||
assert(memcmp(postbuf.data(), " World", postbuf.size()) == 0);
|
||||
}
|
||||
|
||||
// stream with assignment
|
||||
HEAD("TestBytes: stream with assignment", RNS::LOG_TRACE);
|
||||
// (side effect of also updating pre - this is a known and correct but perhaps unexpected and non-intuitive side-effect of assignment with stream)
|
||||
{
|
||||
// NOTE pre must be volatile in order for below stream with assignment to work (since it gets modified)
|
||||
RNS::Bytes pre("Hello");
|
||||
assert(pre.size() == 5);
|
||||
const RNS::Bytes post(" World");
|
||||
assert(post.size() == 6);
|
||||
RNS::Bytes strmbuf = pre << post;
|
||||
TRACE("Results:");
|
||||
TRACEF("stream pre: %s", prebuf.toString().c_str());
|
||||
TRACEF("stream post: %s", postbuf.toString().c_str());
|
||||
TRACEF("stream strmbuf: %s", strmbuf.toString().c_str());
|
||||
assert(strmbuf.size() == 11);
|
||||
assert(memcmp(strmbuf.data(), "Hello World", strmbuf.size()) == 0);
|
||||
assert(pre.size() == 11);
|
||||
assert(memcmp(pre.data(), "Hello World", pre.size()) == 0);
|
||||
assert(post.size() == 6);
|
||||
assert(memcmp(post.data(), " World", post.size()) == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void testBytesReserve() {
|
||||
|
||||
RNS::Bytes src("Hello");
|
||||
uint8_t hops = 32;
|
||||
|
||||
{
|
||||
RNS::Bytes dst = src.left(1);
|
||||
dst << hops;
|
||||
dst << src.mid(2);
|
||||
TRACEF("non-reserve: %s", dst.toString().c_str());
|
||||
assert(dst.size() == 5);
|
||||
assert(memcmp(dst.data(), "H llo", dst.size()) == 0);
|
||||
}
|
||||
|
||||
{
|
||||
RNS::Bytes dst(512);
|
||||
dst << src.left(1);
|
||||
dst << hops;
|
||||
dst << src.mid(2);
|
||||
TRACEF("reserve: %s", dst.toString().c_str());
|
||||
assert(dst.size() == 5);
|
||||
assert(memcmp(dst.data(), "H llo", dst.size()) == 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void testBytes() {
|
||||
HEAD("Running testBytes...", RNS::LOG_TRACE);
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACE("testBytes: pre-mem: " + std::to_string(pre_memory));
|
||||
RNS::Utilities::OS::dump_heap_stats();
|
||||
|
||||
testBytesMain();
|
||||
testCowBytes();
|
||||
testBytesConversion();
|
||||
testBytesResize();
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testBytes: pre-mem: %u", pre_memory);
|
||||
|
||||
//testBytesMain();
|
||||
//testCowBytes();
|
||||
//testBytesConversion();
|
||||
//testBytesResize();
|
||||
//testBytesStream();
|
||||
testBytesReserve();
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACE("testBytes: post-mem: " + std::to_string(post_memory));
|
||||
TRACE("testBytes: diff-mem: " + std::to_string(diff_memory));
|
||||
TRACEF("testBytes: post-mem: %u", post_memory);
|
||||
TRACEF("testBytes: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
RNS::Utilities::OS::dump_allocator_stats();
|
||||
|
||||
RNS::Utilities::OS::dump_heap_stats();
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,37 +1,170 @@
|
||||
//#include <unity.h>
|
||||
|
||||
#include "Filesystem.h"
|
||||
|
||||
#include <Utilities/OS.h>
|
||||
#include "Log.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
using namespace RNS;
|
||||
using namespace RNS::Utilities;
|
||||
Filesystem test_filesystem;
|
||||
|
||||
void testListDirectory() {
|
||||
|
||||
size_t pre_memory = OS::heap_available();
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
INFO("testListDirectory: pre-mem: " + std::to_string(pre_memory));
|
||||
|
||||
for (int i = 0; i < 1; i++) {
|
||||
std::list<std::string> files;
|
||||
files = OS::list_directory("/");
|
||||
for (auto& file : files) {
|
||||
INFO("FILE: " + file);
|
||||
{
|
||||
for (int i = 0; i < 1; i++) {
|
||||
std::list<std::string> files;
|
||||
files = RNS::Utilities::OS::list_directory("/");
|
||||
for (auto& file : files) {
|
||||
INFO("FILE: " + file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t post_memory = OS::heap_available();
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
INFO("testListDirectory: post-mem: " + std::to_string(post_memory));
|
||||
INFO("testListDirectory: diff-mem: " + std::to_string(diff_memory));
|
||||
assert(diff_memory == 0);
|
||||
}
|
||||
|
||||
void testWriteFile(const char* file_path) {
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testWriteFile: pre-mem: %u", pre_memory);
|
||||
|
||||
{
|
||||
RNS::Bytes data("test");
|
||||
size_t wrote = RNS::Utilities::OS::write_file(file_path, data);
|
||||
//assert(wrote == 4);
|
||||
}
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testWriteFile: post-mem: %u", post_memory);
|
||||
TRACEF("testWriteFile: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
}
|
||||
|
||||
void testReadFile(const char* file_path) {
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testReadFile: pre-mem: %u", pre_memory);
|
||||
|
||||
{
|
||||
RNS::Bytes data;
|
||||
size_t read = RNS::Utilities::OS::read_file(file_path, data);
|
||||
assert(read == 4);
|
||||
}
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testReadFile: post-mem: %u", post_memory);
|
||||
TRACEF("testReadFile: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
}
|
||||
|
||||
void testRemoveFile(const char* file_path) {
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testRemoveFile: pre-mem: %u", pre_memory);
|
||||
|
||||
assert(RNS::Utilities::OS::remove_file(file_path));
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testRemoveFile: post-mem: %u", post_memory);
|
||||
TRACEF("testRemoveFile: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
}
|
||||
|
||||
void testReadNonexistantFile() {
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testReadNonexistantFile: pre-mem: %u", pre_memory);
|
||||
|
||||
{
|
||||
RNS::Bytes data;
|
||||
size_t read = RNS::Utilities::OS::read_file("/foo", data);
|
||||
assert(read == 0);
|
||||
}
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testReadNonexistantFile: post-mem: %u", post_memory);
|
||||
TRACEF("testReadNonexistantFile: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
}
|
||||
|
||||
void testWriteFail() {
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testWriteFail: pre-mem: %u", pre_memory);
|
||||
|
||||
{
|
||||
RNS::Bytes data("test");
|
||||
size_t wrote = RNS::Utilities::OS::write_file("", data);
|
||||
assert(wrote == 4);
|
||||
}
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testWriteFail: post-mem: %u", post_memory);
|
||||
TRACEF("testWriteFail: diff-mem: %u", diff_memory);
|
||||
assert(diff_memory == 0);
|
||||
|
||||
}
|
||||
|
||||
void testFilesystem() {
|
||||
HEAD("Running testFilesystem...", RNS::LOG_TRACE);
|
||||
|
||||
test_filesystem.init();
|
||||
test_filesystem.listDir("/");
|
||||
test_filesystem.listDir("/cache");
|
||||
RNS::Utilities::OS::register_filesystem(test_filesystem);
|
||||
|
||||
|
||||
size_t pre_memory = RNS::Utilities::OS::heap_available();
|
||||
TRACEF("testFilesystem: pre-mem: %u", pre_memory);
|
||||
|
||||
// CBA Seems that first call to write_file allocates some memory (at least with NRF52 LittleFS) so get that out of the way first
|
||||
{
|
||||
RNS::Bytes data("foo");
|
||||
RNS::Utilities::OS::write_file("/foo", data);
|
||||
RNS::Utilities::OS::remove_file("/foo");
|
||||
}
|
||||
|
||||
size_t post_memory = RNS::Utilities::OS::heap_available();
|
||||
size_t diff_memory = (int)pre_memory - (int)post_memory;
|
||||
TRACEF("testFilesystem: post-mem: %u", post_memory);
|
||||
TRACEF("testFilesystem: diff-mem: %u", diff_memory);
|
||||
|
||||
|
||||
testListDirectory();
|
||||
testWriteFile("/test1");
|
||||
testReadFile("/test1");
|
||||
//testRemoveFile("/test1");
|
||||
testReadNonexistantFile();
|
||||
//testWriteFail();
|
||||
|
||||
// CBA Attempt to reproduce failure to open file for write that is leaking mmeory
|
||||
if (!RNS::Utilities::OS::directory_exists("/cache")) {
|
||||
RNS::Utilities::OS::create_directory("/cache");
|
||||
}
|
||||
testWriteFile("/cache/test");
|
||||
testWriteFile("/cache/45c50662af11f1b26889efaab547942b");
|
||||
testWriteFile("/cache/40fe4ab8105b591cca1ef159d476a7b4");
|
||||
testWriteFile("/cache/c755609f4d0ab75b5119905a032eeb33");
|
||||
testWriteFile("/cache/426f66f9aadf866933358b71295f59d0");
|
||||
|
||||
RNS::Utilities::OS::deregister_filesystem();
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
//#include <unity.h>
|
||||
|
||||
#include "Filesystem.h"
|
||||
|
||||
#include "Interface.h"
|
||||
#include "Transport.h"
|
||||
#include "Log.h"
|
||||
@@ -17,6 +19,7 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
Filesystem persistence_filesystem;
|
||||
|
||||
class TestInterface : public RNS::Interface {
|
||||
|
||||
@@ -240,7 +243,6 @@ void testDeserializeObject() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
#ifdef ARDUINO
|
||||
const char test_vector_path[] = "/test_vector";
|
||||
#else
|
||||
@@ -684,9 +686,20 @@ void testDeserializeDestinationTable() {
|
||||
|
||||
}
|
||||
|
||||
void testSerializeTimeOffset() {
|
||||
uint64_t offset = RNS::Utilities::OS::ltime();
|
||||
TRACEF("Writing time offset of %llu to file /time_offset", offset);
|
||||
RNS::Bytes buf((uint8_t*)&offset, sizeof(offset));
|
||||
RNS::Utilities::OS::write_file("/time_offset", buf);
|
||||
}
|
||||
|
||||
|
||||
void testPersistence() {
|
||||
HEAD("Running testPersistence...", RNS::LOG_TRACE);
|
||||
|
||||
persistence_filesystem.init();
|
||||
RNS::Utilities::OS::register_filesystem(persistence_filesystem);
|
||||
|
||||
testWrite();
|
||||
testRead();
|
||||
testSerializeObject();
|
||||
@@ -697,8 +710,12 @@ void testPersistence() {
|
||||
testDeserializeSet();
|
||||
testSerializeMap();
|
||||
testDeserializeMap();
|
||||
testSerializeDestinationTable();
|
||||
testDeserializeDestinationTable();
|
||||
//testSerializeDestinationTable();
|
||||
//testDeserializeDestinationTable();
|
||||
testSerializeTimeOffset();
|
||||
|
||||
RNS::Utilities::OS::deregister_filesystem();
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -6,21 +6,181 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
RNS::Reticulum reticulum = {RNS::Type::NONE};
|
||||
class TestInterface : public RNS::Interface {
|
||||
|
||||
void testDestinationTable() {
|
||||
public:
|
||||
TestInterface(const char* name = "TestInterface") : Interface(name) {}
|
||||
virtual ~TestInterface() {}
|
||||
|
||||
virtual inline std::string toString() const { return "TestInterface[" + name() + "]"; }
|
||||
|
||||
private:
|
||||
virtual void on_incoming(const RNS::Bytes& data) {
|
||||
RNS::debug(toString() + ".on_incoming: data: " + data.toHex());
|
||||
Interface::on_incoming(data);
|
||||
}
|
||||
virtual void on_outgoing(const RNS::Bytes& data) {
|
||||
RNS::debug(toString() + ".on_outgoing: data: " + data.toHex());
|
||||
Interface::on_outgoing(data);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class InInterface : public RNS::Interface {
|
||||
public:
|
||||
InInterface() : RNS::Interface("InInterface") {
|
||||
OUT(false);
|
||||
IN(true);
|
||||
}
|
||||
InInterface(const char *name) : RNS::Interface(name) {
|
||||
OUT(false);
|
||||
IN(true);
|
||||
}
|
||||
virtual ~InInterface() {
|
||||
name("(deleted)");
|
||||
}
|
||||
virtual void on_incoming(const RNS::Bytes &data) {
|
||||
HEAD("InInterface.on_incoming: data: " + data.toHex(), RNS::LOG_TRACE);
|
||||
RNS::Interface::on_incoming(data);
|
||||
}
|
||||
virtual inline std::string toString() const { return "InInterface[" + name() + "]"; }
|
||||
};
|
||||
|
||||
class OutInterface : public RNS::Interface {
|
||||
public:
|
||||
OutInterface(InInterface& in_interface) : RNS::Interface("OutInterface"), _in_interface(in_interface) {
|
||||
OUT(true);
|
||||
IN(false);
|
||||
}
|
||||
OutInterface(InInterface& in_interface, const char *name) : RNS::Interface(name), _in_interface(in_interface) {
|
||||
OUT(true);
|
||||
IN(false);
|
||||
}
|
||||
virtual ~OutInterface() {
|
||||
name("(deleted)");
|
||||
}
|
||||
virtual void on_outgoing(const RNS::Bytes &data) {
|
||||
HEAD("OutInterface.on_outgoing: data: " + data.toHex(), RNS::LOG_TRACE);
|
||||
_in_interface.on_incoming(data);
|
||||
RNS::Interface::on_outgoing(data);
|
||||
}
|
||||
virtual inline std::string toString() const { return "OutInterface[" + name() + "]"; }
|
||||
private:
|
||||
InInterface& _in_interface;
|
||||
};
|
||||
|
||||
// Test AnnounceHandler
|
||||
class ExampleAnnounceHandler : public RNS::AnnounceHandler {
|
||||
public:
|
||||
ExampleAnnounceHandler(const char* aspect_filter = nullptr) : AnnounceHandler(aspect_filter) {}
|
||||
virtual ~ExampleAnnounceHandler() {}
|
||||
virtual void received_announce(const RNS::Bytes& destination_hash, const RNS::Identity& announced_identity, const RNS::Bytes& app_data) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
INFO("ExampleAnnounceHandler: destination hash: " + destination_hash.toHex());
|
||||
if (announced_identity) {
|
||||
INFO("ExampleAnnounceHandler: announced identity hash: " + announced_identity.hash().toHex());
|
||||
INFO("ExampleAnnounceHandler: announced identity app data: " + announced_identity.app_data().toHex());
|
||||
}
|
||||
if (app_data) {
|
||||
INFO("ExampleAnnounceHandler: app data text: \"" + app_data.toString() + "\"");
|
||||
}
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
};
|
||||
|
||||
//ExampleAnnounceHandler announce_handler((const char*)"example_utilities.announcesample.fruits");
|
||||
//RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler("example_utilities.announcesample.fruits"));
|
||||
RNS::HAnnounceHandler announce_handler(new ExampleAnnounceHandler());
|
||||
|
||||
// Test packet receive callback
|
||||
void onPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
INFO("onPacket: data: " + data.toHex());
|
||||
INFO("onPacket: text: \"" + data.toString() + "\"");
|
||||
//TRACE("onPacket: " + packet.debugString());
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
// Ping packet receive callback
|
||||
void onPingPacket(const RNS::Bytes& data, const RNS::Packet& packet) {
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
INFO("onPingPacket: data: " + data.toHex());
|
||||
INFO("onPingPacket: text: \"" + data.toString() + "\"");
|
||||
//TRACE("onPingPacket: " + packet.debugString());
|
||||
INFO("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
|
||||
}
|
||||
|
||||
|
||||
RNS::Reticulum reticulum({RNS::Type::NONE});
|
||||
RNS::Identity identity({RNS::Type::NONE});
|
||||
RNS::Destination destination({RNS::Type::NONE});
|
||||
InInterface in_interface;
|
||||
OutInterface out_interface(in_interface);
|
||||
|
||||
void testReticulum() {
|
||||
HEAD("Running testReticulum...", RNS::LOG_TRACE);
|
||||
|
||||
HEAD("Registering Interface instances with Transport...", RNS::LOG_TRACE);
|
||||
RNS::Transport::register_interface(in_interface);
|
||||
RNS::Transport::register_interface(out_interface);
|
||||
|
||||
RNS::Bytes transport_prv_bytes;
|
||||
#ifdef ARDUINO
|
||||
transport_prv_bytes.assignHex("CAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFECAFE");
|
||||
#else
|
||||
transport_prv_bytes.assignHex("BABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABEBABE");
|
||||
#endif
|
||||
RNS::Identity transport_identity(false);
|
||||
transport_identity.load_private_key(transport_prv_bytes);
|
||||
RNS::Transport::identity(transport_identity);
|
||||
|
||||
HEAD("Creating Reticulum instance...", RNS::LOG_TRACE);
|
||||
reticulum = RNS::Reticulum();
|
||||
reticulum.transport_enabled(true);
|
||||
reticulum.probe_destination_enabled(true);
|
||||
reticulum.start();
|
||||
|
||||
testDestinationTable();
|
||||
HEAD("Creating Identity instance...", RNS::LOG_TRACE);
|
||||
// new identity
|
||||
identity = RNS::Identity(false);
|
||||
RNS::Bytes prv_bytes;
|
||||
#ifdef ARDUINO
|
||||
prv_bytes.assignHex("78E7D93E28D55871608FF13329A226CABC3903A357388A035B360162FF6321570B092E0583772AB80BC425F99791DF5CA2CA0A985FF0415DAB419BBC64DDFAE8");
|
||||
#else
|
||||
prv_bytes.assignHex("E0D43398EDC974EBA9F4A83463691A08F4D306D4E56BA6B275B8690A2FBD9852E9EBE7C03BC45CAEC9EF8E78C830037210BFB9986F6CA2DEE2B5C28D7B4DE6B0");
|
||||
#endif
|
||||
identity.load_private_key(prv_bytes);
|
||||
// 22.6% (+0.7%)
|
||||
|
||||
HEAD("Creating Destination instance...", RNS::LOG_TRACE);
|
||||
//RNS::Destination destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
destination = RNS::Destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "app", "aspects");
|
||||
// 23.0% (+0.4%)
|
||||
|
||||
// Register DATA packet callback
|
||||
HEAD("Registering packet callback with Destination...", RNS::LOG_TRACE);
|
||||
destination.set_packet_callback(onPacket);
|
||||
destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
|
||||
{
|
||||
// Register PING packet callback
|
||||
HEAD("Creating PING Destination instance...", RNS::LOG_TRACE);
|
||||
RNS::Destination ping_destination(identity, RNS::Type::Destination::IN, RNS::Type::Destination::SINGLE, "example_utilities", "echo.request");
|
||||
|
||||
HEAD("Registering packet callback with PING Destination...", RNS::LOG_TRACE);
|
||||
ping_destination.set_packet_callback(onPingPacket);
|
||||
ping_destination.set_proof_strategy(RNS::Type::Destination::PROVE_ALL);
|
||||
}
|
||||
|
||||
HEAD("Registering announce handler with Transport...", RNS::LOG_TRACE);
|
||||
RNS::Transport::register_announce_handler(announce_handler);
|
||||
|
||||
HEAD("Announcing destination...", RNS::LOG_TRACE);
|
||||
// test path
|
||||
//destination.announce(RNS::bytesFromString("test"), true, nullptr, RNS::bytesFromString("test_tag"));
|
||||
// test packet send
|
||||
destination.announce(RNS::bytesFromString("test"));
|
||||
// 23.9% (+0.8%)
|
||||
|
||||
reticulum = {RNS::Type::NONE};
|
||||
}
|
||||
|
||||
+3
-12
@@ -1,7 +1,5 @@
|
||||
//#define NDEBUG
|
||||
|
||||
#include "Filesystem.h"
|
||||
|
||||
#include "Test.h"
|
||||
|
||||
#include "Log.h"
|
||||
@@ -22,8 +20,6 @@
|
||||
#include <map>
|
||||
#include <functional>
|
||||
|
||||
Filesystem filesystem;
|
||||
|
||||
void setup() {
|
||||
|
||||
#ifdef ARDUINO
|
||||
@@ -38,18 +34,13 @@ void setup() {
|
||||
RNS::loglevel(RNS::LOG_TRACE);
|
||||
//RNS::loglevel(RNS::LOG_MEM);
|
||||
|
||||
if (filesystem) {
|
||||
INFO("Filesystem exists");
|
||||
filesystem.init();
|
||||
}
|
||||
RNS::Utilities::OS::register_filesystem(filesystem);
|
||||
|
||||
try {
|
||||
|
||||
TRACE("Running tests...");
|
||||
RNS::LogLevel loglevel = RNS::loglevel();
|
||||
//RNS::loglevel(RNS::LOG_WARNING);
|
||||
RNS::loglevel(RNS::LOG_TRACE);
|
||||
//RNS::loglevel(RNS::LOG_MEM);
|
||||
|
||||
// all tests
|
||||
//test();
|
||||
@@ -57,9 +48,9 @@ void setup() {
|
||||
// individiual tests
|
||||
//testReference();
|
||||
//testCrypto();
|
||||
//testPersistence();
|
||||
testBytes();
|
||||
//testFilesystem();
|
||||
//testPersistence();
|
||||
//testReticulum();
|
||||
|
||||
RNS::loglevel(loglevel);
|
||||
@@ -67,7 +58,7 @@ void setup() {
|
||||
//return;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in test: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in test: %s", e.what());
|
||||
}
|
||||
|
||||
#ifdef ARDUINO
|
||||
|
||||
@@ -16,9 +16,9 @@ include_dir = .
|
||||
src_dir = .
|
||||
|
||||
[env]
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
upload_speed = 921600
|
||||
build_type = debug
|
||||
|
||||
[env:native]
|
||||
platform = native
|
||||
@@ -61,9 +61,11 @@ lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
microReticulum=symlink://../..
|
||||
lib_extra_dirs = ../
|
||||
lib_compat_mode = off
|
||||
|
||||
[env:cubecell-gps]
|
||||
framework = arduino
|
||||
platform = heltec-cubecell
|
||||
board = cubecell_gps
|
||||
;build_flags = -Wl,-Map,output.map
|
||||
@@ -82,6 +84,7 @@ lib_deps =
|
||||
; CBA NOTE must undef min and max from Arduino.h
|
||||
|
||||
[env:heltec-tracker]
|
||||
framework = arduino
|
||||
platform = espressif32
|
||||
board = heltec_wireless_tracker
|
||||
upload_protocol = esp-builtin
|
||||
@@ -102,8 +105,10 @@ lib_deps =
|
||||
microReticulum=symlink://../..
|
||||
|
||||
[env:ttgo-lora32-v21]
|
||||
framework = arduino
|
||||
platform = espressif32
|
||||
board = ttgo-lora32-v21
|
||||
upload_speed = 460800
|
||||
board_build.partitions = no_ota.csv
|
||||
build_flags =
|
||||
-Wall
|
||||
@@ -116,8 +121,10 @@ lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
microReticulum=symlink://../..
|
||||
monitor_filters = esp32_exception_decoder
|
||||
|
||||
[env:ttgo-t-beam]
|
||||
framework = arduino
|
||||
platform = espressif32
|
||||
board = ttgo-t-beam
|
||||
board_build.partitions = no_ota.csv
|
||||
@@ -133,8 +140,10 @@ lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
microReticulum=symlink://../..
|
||||
monitor_filters = esp32_exception_decoder
|
||||
|
||||
[env:wiscore_rak4631]
|
||||
framework = arduino
|
||||
platform = nordicnrf52
|
||||
board = wiscore_rak4631
|
||||
board_build.partitions = no_ota.csv
|
||||
@@ -147,8 +156,22 @@ build_flags =
|
||||
-Wno-format
|
||||
-I.
|
||||
-DBOARD_NRF52
|
||||
-DRNS_USE_TLSF=1
|
||||
build_unflags = -fno-exceptions
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
microReticulum=symlink://../..
|
||||
|
||||
[env:nrf52840_dk_adafruit]
|
||||
framework = arduino
|
||||
platform = nordicnrf52
|
||||
board = nrf52840_dk_adafruit
|
||||
monitor_speed = 115200
|
||||
|
||||
[env:nrf52_dk]
|
||||
framework = arduino
|
||||
;framework = zephyr
|
||||
platform = nordicnrf52
|
||||
board = nrf52_dk
|
||||
|
||||
@@ -209,7 +209,7 @@ void reticulum_setup() {
|
||||
HEAD("Ready!", RNS::LOG_TRACE);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_setup: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_setup: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,7 +228,7 @@ void reticulum_teardown() {
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ERROR(std::string("!!! Exception in reticulum_teardown: ") + e.what() + " !!!");
|
||||
ERRORF("!!! Exception in reticulum_teardown: %s", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user