mirror of
https://github.com/ratspeak/microReticulum
synced 2026-11-27 13:52:14 +00:00
WIP: Filesystem support and persistence defines
This commit is contained in:
1 parent
3af5111606
commit
4a30a0a4b4
4 files changed
+58
-52
No files matched your search
+26
-52
@@ -11,9 +11,8 @@
|
||||
[platformio]
|
||||
;default_envs = native
|
||||
|
||||
[env:native]
|
||||
platform = native
|
||||
build_flags =
|
||||
[env]
|
||||
build_flags =
|
||||
-std=c++11
|
||||
-Wall
|
||||
;-Wextra
|
||||
@@ -21,29 +20,31 @@ build_flags =
|
||||
-Wno-format
|
||||
-Wno-unused-parameter
|
||||
-Isrc
|
||||
-DNATIVE
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
-DRNS_USE_FS
|
||||
-DRNS_PERSIST_PATHS
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
|
||||
[env:native]
|
||||
platform = native
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-DNATIVE
|
||||
lib_deps =
|
||||
${env.lib_deps}
|
||||
lib_compat_mode = off
|
||||
|
||||
[env:native17]
|
||||
platform = native
|
||||
build_unflags = -std=gnu++11
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-std=c++17
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Wno-unused-parameter
|
||||
-Isrc
|
||||
-DNATIVE
|
||||
;-DNDEBUG
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
${env.lib_deps}
|
||||
lib_compat_mode = off
|
||||
|
||||
[env:cubecell-gps]
|
||||
@@ -53,15 +54,9 @@ framework = arduino
|
||||
monitor_speed = 115200
|
||||
;build_flags = -Wl,-Map,output.map
|
||||
build_flags =
|
||||
-std=c++11
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
${env.build_flags}
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
${env.lib_deps}
|
||||
; CBA NOTE must undef min and max from Arduino.h
|
||||
|
||||
[env:heltec-tracker]
|
||||
@@ -73,16 +68,11 @@ upload_protocol = esp-builtin
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
${env.build_flags}
|
||||
-I variants/heltec_wireless_tracker
|
||||
-Isrc
|
||||
lib_deps =
|
||||
${env.lib_deps}
|
||||
lovyan03/LovyanGFX@^1.1.8
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
|
||||
[env:ttgo-lora32-v21]
|
||||
platform = espressif32
|
||||
@@ -91,14 +81,9 @@ board_build.partitions = no_ota.csv
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
${env.build_flags}
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
${env.lib_deps}
|
||||
|
||||
[env:ttgo-t-beam]
|
||||
platform = espressif32
|
||||
@@ -108,15 +93,9 @@ board_build.partitions = no_ota.csv
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
;-DNDEBUG
|
||||
${env.build_flags}
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
${env.lib_deps}
|
||||
|
||||
[env:wiscore_rak4631]
|
||||
platform = nordicnrf52
|
||||
@@ -125,14 +104,9 @@ board_build.partitions = no_ota.csv
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
build_flags =
|
||||
${env.build_flags}
|
||||
-fexceptions
|
||||
-Wall
|
||||
-Wextra
|
||||
-Wno-missing-field-initializers
|
||||
-Wno-format
|
||||
-Isrc
|
||||
;-DNDEBUG
|
||||
-DRNS_USE_ALLOCATOR=1
|
||||
-DRNS_USE_TLSF=1
|
||||
lib_deps =
|
||||
ArduinoJson@^6.21.3
|
||||
https://github.com/attermann/arduinolibs.git
|
||||
${env.lib_deps}
|
||||
@@ -132,6 +132,7 @@ void Identity::load_public_key(const Bytes& pub_bytes) {
|
||||
|
||||
bool Identity::load(const char* path) {
|
||||
TRACE("Reading identity key from storage...");
|
||||
#if defined(RNS_USE_FS)
|
||||
try {
|
||||
Bytes prv_bytes;
|
||||
if (OS::read_file(path, prv_bytes) > 0) {
|
||||
@@ -145,6 +146,7 @@ bool Identity::load(const char* path) {
|
||||
ERROR("Error while loading identity from " + std::string(path));
|
||||
ERRORF("The contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -158,6 +160,7 @@ communication for the identity. Be very careful with this method.
|
||||
*/
|
||||
bool Identity::to_file(const char* path) {
|
||||
TRACE("Writing identity key to storage...");
|
||||
#if defined(RNS_USE_FS)
|
||||
try {
|
||||
return (OS::write_file(path, get_private_key()) == get_private_key().size());
|
||||
}
|
||||
@@ -165,6 +168,7 @@ bool Identity::to_file(const char* path) {
|
||||
ERRORF("Error while saving identity to %s", path);
|
||||
ERRORF("The contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ Reticulum::Reticulum() : _object(new Object()) {
|
||||
#endif
|
||||
|
||||
#ifdef ARDUINO
|
||||
#if defined(RNS_USE_FS)
|
||||
// load time offset from file if it exists
|
||||
try {
|
||||
char time_offset_path[FILEPATH_MAXSIZE];
|
||||
@@ -107,6 +108,7 @@ Reticulum::Reticulum() : _object(new Object()) {
|
||||
catch (std::exception& e) {
|
||||
ERRORF("Failed to load time offset, the contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Initialize time-based variables *after* time offset update
|
||||
@@ -261,6 +263,7 @@ void Reticulum::persist_data() {
|
||||
Identity::persist_data();
|
||||
|
||||
#ifdef ARDUINO
|
||||
#if defined(RNS_USE_FS)
|
||||
// write time offset to file
|
||||
try {
|
||||
char time_offset_path[FILEPATH_MAXSIZE];
|
||||
@@ -273,6 +276,7 @@ void Reticulum::persist_data() {
|
||||
catch (std::exception& e) {
|
||||
ERRORF("Failed to write time offset, the contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
_object->_last_data_persist = OS::time();
|
||||
@@ -282,6 +286,7 @@ void Reticulum::clean_caches() {
|
||||
TRACE("Cleaning resource and packet caches...");
|
||||
double now = OS::time();
|
||||
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
/*
|
||||
// Clean resource caches
|
||||
for (auto& filename : OS::list_directory(resourcepath) {
|
||||
@@ -324,6 +329,7 @@ void Reticulum::clean_caches() {
|
||||
|
||||
// CBA
|
||||
Identity::cull_known_destinations();
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2628,6 +2628,7 @@ Deregisters an announce handler.
|
||||
// the packet cache.
|
||||
/*static*/ bool Transport::cache_packet(const Packet& packet, bool force_cache /*= false*/) {
|
||||
TRACE("Checking to see if packet " + packet.get_hash().toHex() + " should be cached");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
if (should_cache_packet(packet) || force_cache) {
|
||||
TRACE("Saving packet " + packet.get_hash().toHex() + " to storage");
|
||||
try {
|
||||
@@ -2639,11 +2640,13 @@ Deregisters an announce handler.
|
||||
ERROR("Error writing packet to cache. The contained exception was: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
/*static*/ Packet Transport::get_cached_packet(const Bytes& packet_hash) {
|
||||
TRACE("Loading packet " + packet_hash.toHex() + " from cache storage");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
try {
|
||||
/*p
|
||||
packet_hash = RNS.hexrep(packet_hash, delimit=False)
|
||||
@@ -2678,11 +2681,13 @@ Deregisters an announce handler.
|
||||
ERROR("Exception occurred while getting cached packet.");
|
||||
ERRORF("The contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
return {Type::NONE};
|
||||
}
|
||||
|
||||
/*static*/ bool Transport::clear_cached_packet(const Bytes& packet_hash) {
|
||||
TRACE("Clearing packet " + packet_hash.toHex() + " from cache storage");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
try {
|
||||
char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE];
|
||||
snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, packet_hash.toHex().c_str());
|
||||
@@ -2700,6 +2705,7 @@ Deregisters an announce handler.
|
||||
ERROR("Exception occurred while clearing cached packet.");
|
||||
ERRORF("The contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3281,6 +3287,7 @@ will announce it.
|
||||
}
|
||||
|
||||
/*static*/ void Transport::write_packet_hashlist() {
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// TODO
|
||||
/*
|
||||
if not Transport.owner.is_connected_to_shared_instance:
|
||||
@@ -3320,10 +3327,12 @@ will announce it.
|
||||
|
||||
Transport.saving_packet_hashlist = False
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ bool Transport::read_path_table() {
|
||||
DEBUG("Transport::read_path_table");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
char destination_table_path[Type::Reticulum::FILEPATH_MAXSIZE];
|
||||
snprintf(destination_table_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/destination_table", Reticulum::_storagepath);
|
||||
if (!_owner.is_connected_to_shared_instance() && OS::file_exists(destination_table_path)) {
|
||||
@@ -3422,6 +3431,7 @@ TRACE("Transport::start: buffer size " + std::to_string(Persistence::_buffer.siz
|
||||
ERRORF("Could not load destination table from storage, the contained exception was: %s", e.what());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3434,6 +3444,7 @@ TRACE("Transport::start: buffer size " + std::to_string(Persistence::_buffer.siz
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
if (_saving_path_table) {
|
||||
double wait_interval = 0.2;
|
||||
double wait_timeout = 5;
|
||||
@@ -3563,6 +3574,7 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
catch (std::exception& e) {
|
||||
ERRORF("Could not save path table to storage, the contained exception was: %s", e.what());
|
||||
}
|
||||
#endif
|
||||
|
||||
_saving_path_table = false;
|
||||
|
||||
@@ -3571,6 +3583,7 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
|
||||
/*static*/ void Transport::read_tunnel_table() {
|
||||
DEBUG("Transport::read_tunnel_table");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// TODO
|
||||
/*
|
||||
tunnel_table_path = RNS.Reticulum.storagepath+"/tunnels"
|
||||
@@ -3622,9 +3635,11 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
except Exception as e:
|
||||
RNS.log("Could not load tunnel table from storage, the contained exception was: "+str(e), RNS.LOG_ERROR)
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ void Transport::write_tunnel_table() {
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// TODO
|
||||
/*
|
||||
if not Transport.owner.is_connected_to_shared_instance:
|
||||
@@ -3701,6 +3716,7 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
|
||||
Transport.saving_tunnel_table = False
|
||||
*/
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ void Transport::persist_data() {
|
||||
@@ -3712,6 +3728,7 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
|
||||
/*static*/ void Transport::clean_caches() {
|
||||
TRACE("Transport::clean_caches()");
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// CBA Remove cached packets no longer in path list
|
||||
std::list<std::string> files = OS::list_directory(Reticulum::_cachepath);
|
||||
for (auto& file : files) {
|
||||
@@ -3730,6 +3747,7 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
OS::remove_file(packet_cache_path);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
/*static*/ void Transport::dump_stats() {
|
||||
@@ -3837,12 +3855,14 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
//if (_packet_table.erase(destination_entry._announce_packet) < 1) {
|
||||
// WARNING("Failed to remove packet " + destination_entry._announce_packet.toHex() + " from packet table");
|
||||
//}
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// Remove cached packet file
|
||||
char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE];
|
||||
snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, destination_entry._announce_packet.toHex().c_str());
|
||||
if (OS::file_exists(packet_cache_path)) {
|
||||
OS::remove_file(packet_cache_path);
|
||||
}
|
||||
#endif
|
||||
++count;
|
||||
if (_destination_table.size() <= _path_table_maxsize) {
|
||||
break;
|
||||
@@ -3871,12 +3891,14 @@ TRACE("Transport::write_path_table: buffer size " + std::to_string(Persistence::
|
||||
//if (_packet_table.erase(destination_entry._announce_packet) < 1) {
|
||||
// WARNING("Failed to remove packet " + destination_entry._announce_packet.toHex() + " from packet table");
|
||||
//}
|
||||
#if defined(RNS_USE_FS) && defined(RNS_PERSIST_PATHS)
|
||||
// Remove cached packet file
|
||||
char packet_cache_path[Type::Reticulum::FILEPATH_MAXSIZE];
|
||||
snprintf(packet_cache_path, Type::Reticulum::FILEPATH_MAXSIZE, "%s/%s", Reticulum::_cachepath, destination_entry._announce_packet.toHex().c_str());
|
||||
if (OS::file_exists(packet_cache_path)) {
|
||||
OS::remove_file(packet_cache_path);
|
||||
}
|
||||
#endif
|
||||
++count;
|
||||
if (_destination_table.size() <= _path_table_maxsize) {
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user