diff --git a/src/Identity.cpp b/src/Identity.cpp index a80a770..44dd6e8 100644 --- a/src/Identity.cpp +++ b/src/Identity.cpp @@ -298,10 +298,10 @@ Recall last heard app_data for a destination hash. std::string time_str; double save_time = OS::time() - save_start; if (save_time < 1) { - time_str = std::to_string(OS::round(save_time*1000, 2)) + "ms"; + time_str = std::to_string((int)(save_time*1000)) + " ms"; } else { - time_str = std::to_string(OS::round(save_time, 2)) + "s"; + time_str = std::to_string(OS::round(save_time, 1)) + " s"; } debug("Saved known destinations to storage in " + time_str); diff --git a/src/Reticulum.cpp b/src/Reticulum.cpp index febb3ee..13d1984 100644 --- a/src/Reticulum.cpp +++ b/src/Reticulum.cpp @@ -72,7 +72,7 @@ Reticulum::Reticulum() : _object(new Object()) { Reticulum.resourcepath = Reticulum.configdir+"/storage/resources" Reticulum.identitypath = Reticulum.configdir+"/storage/identities" */ -// CBA MOCK +// CBA TEST #ifdef ARDUINO _storagepath = ""; _cachepath = "/cache"; @@ -81,6 +81,29 @@ Reticulum::Reticulum() : _object(new Object()) { _cachepath = "./cache"; #endif +#ifdef ARDUINO + // load time offset from file if it exists + try { + std::string time_offset_path = Reticulum::_storagepath + "/time_offset"; + if (OS::file_exists(time_offset_path.c_str())) { + Bytes buf = OS::read_file(time_offset_path.c_str()); + if (buf.size() == 8) { + uint64_t offset = *(uint64_t*)buf.data(); + debug("Read time offset of " + std::to_string(offset) + " from file"); + OS::setTimeOffset(offset); + } + } + } + catch (std::exception& e) { + error("Failed to load time offset, the contained exception was: " + std::string(e.what())); + } +#endif + + // Initialize time-based variables *after* time offset update + _object->_last_data_persist = OS::time(); + _object->_last_cache_clean = 0.0; + _object->_jobs_last_run = OS::time(); + /* TODO if not os.path.isdir(Reticulum.storagepath): os.makedirs(Reticulum.storagepath) @@ -166,6 +189,7 @@ Reticulum::Reticulum() : _object(new Object()) { */ void Reticulum::start() { + info("Starting Transport..."); Transport::start(*this); } @@ -173,9 +197,9 @@ void Reticulum::loop() { assert(_object); if (!_object->_is_connected_to_shared_instance) { - if (OS::time() > (_jobs_last_run + JOB_INTERVAL)) { + if (OS::time() > (_object->_jobs_last_run + JOB_INTERVAL)) { jobs(); - _jobs_last_run = OS::time(); + _object->_jobs_last_run = OS::time(); } RNS::Transport::loop(); @@ -188,26 +212,44 @@ void Reticulum::jobs() { double now = OS::time(); - if (now > _last_cache_clean + CLEAN_INTERVAL) { + if (now > _object->_last_cache_clean + CLEAN_INTERVAL) { clean_caches(); - _last_cache_clean = OS::time(); + _object->_last_cache_clean = OS::time(); } - if (now > _last_data_persist + PERSIST_INTERVAL) { + if (now > _object->_last_data_persist + PERSIST_INTERVAL) { persist_data(); } + + debug("Available Memory: " + std::to_string(OS::freeMemory()) + " Available Storage: " + std::to_string(OS::storage_available())); } void Reticulum::should_persist_data() { - if (OS::time() > _last_data_persist + GRACIOUS_PERSIST_INTERVAL) { + if (OS::time() > _object->_last_data_persist + GRACIOUS_PERSIST_INTERVAL) { persist_data(); } } void Reticulum::persist_data() { + extreme("Persisting transport and identity data..."); Transport::persist_data(); Identity::persist_data(); - _last_data_persist = OS::time(); + +#ifdef ARDUINO + // write time offset to file + try { + std::string time_offset_path = Reticulum::_storagepath + "/time_offset"; + uint64_t offset = OS::ltime(); + debug("Writing time offset of " + std::to_string(offset) + " to file"); + Bytes buf((uint8_t*)&offset, sizeof(offset)); + OS::write_file(buf, time_offset_path.c_str()); + } + catch (std::exception& e) { + error("Failed to write time offset, the contained exception was: " + std::string(e.what())); + } +#endif + + _object->_last_data_persist = OS::time(); } void Reticulum::clean_caches() { diff --git a/src/Reticulum.h b/src/Reticulum.h index 44c3586..37552b8 100644 --- a/src/Reticulum.h +++ b/src/Reticulum.h @@ -34,23 +34,6 @@ namespace RNS { static bool __allow_probes; static bool panic_on_interface_error; - uint16_t _local_interface_port = 37428; - uint16_t _local_control_port = 37429; - bool _share_instance = true; - //p _rpc_listener = None - - //p _ifac_salt = Reticulum.IFAC_SALT - - bool _is_shared_instance = false; - bool _is_connected_to_shared_instance = false; - bool _is_standalone_instance = false; - //p _jobs_thread = None - double _last_data_persist = Utilities::OS::time(); - double _last_cache_clean = 0.0; - - // CBA - double _jobs_last_run = Utilities::OS::time(); - public: Reticulum(Type::NoneConstructor none) { mem("Reticulum NONE object created, this: " + std::to_string((uintptr_t)this) + ", data: " + std::to_string((uintptr_t)_object.get())); @@ -119,7 +102,24 @@ namespace RNS { mem("Reticulum data object destroyed, this: " + std::to_string((uintptr_t)this)); } private: + + uint16_t _local_interface_port = 37428; + uint16_t _local_control_port = 37429; + bool _share_instance = true; + //p _rpc_listener = None + + //p _ifac_salt = Reticulum.IFAC_SALT + + bool _is_shared_instance = false; bool _is_connected_to_shared_instance = false; + bool _is_standalone_instance = false; + //p _jobs_thread = None + double _last_data_persist = Utilities::OS::time(); + double _last_cache_clean = 0.0; + + // CBA + double _jobs_last_run = Utilities::OS::time(); + friend class Reticulum; }; std::shared_ptr _object; diff --git a/src/Transport.cpp b/src/Transport.cpp index 00e1b41..72e0c65 100644 --- a/src/Transport.cpp +++ b/src/Transport.cpp @@ -61,16 +61,16 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); /*static*/ uint16_t Transport::_LOCAL_CLIENT_CACHE_MAXSIZE = 512; -/*static*/ double Transport::_start_time = 0; +/*static*/ double Transport::_start_time = 0.0; /*static*/ bool Transport::_jobs_locked = false; /*static*/ bool Transport::_jobs_running = false; /*static*/ float Transport::_job_interval = 0.250; -/*static*/ double Transport::_jobs_last_run = 0; -/*static*/ double Transport::_links_last_checked = 0; +/*static*/ double Transport::_jobs_last_run = 0.0; +/*static*/ double Transport::_links_last_checked = 0.0; /*static*/ float Transport::_links_check_interval = 1.0; -/*static*/ double Transport::_receipts_last_checked = 0; +/*static*/ double Transport::_receipts_last_checked = 0.0; /*static*/ float Transport::_receipts_check_interval = 1.0; -/*static*/ double Transport::_announces_last_checked = 0; +/*static*/ double Transport::_announces_last_checked = 0.0; /*static*/ float Transport::_announces_check_interval = 1.0; /*static*/ double Transport::_tables_last_culled = 0.0; // CBA MCU @@ -101,23 +101,20 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); _jobs_running = true; _owner = reticulum_instance; + // Initialize time-based variables *after* time offset update + _jobs_last_run = OS::time(); + _links_last_checked = OS::time(); + _receipts_last_checked = OS::time(); + _announces_last_checked = OS::time(); + _tables_last_culled = OS::time(); + _last_saved = OS::time(); + // ensure required directories exist if (!OS::directory_exists(Reticulum::_cachepath.c_str())) { verbose("No cache directory, creating..."); OS::create_directory(Reticulum::_cachepath.c_str()); } -#ifdef ARDUINO - // load time offset from file if it exists - std::string time_offset_path = Reticulum::_storagepath + "/time_offset"; - if (OS::file_exists(time_offset_path.c_str())) { - Bytes buf = OS::read_file(time_offset_path.c_str()); - uint64_t offset = *(uint64_t*)buf.data(); - debug("Read time offset of " + std::to_string(offset) + " from file"); - OS::setTimeOffset(offset); - } -#endif - if (!_identity) { std::string transport_identity_path = Reticulum::_storagepath + "/transport_identity"; debug("Checking for transport identity..."); @@ -234,7 +231,7 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); //DeserializationError error = deserializeMsgPack(doc, data.data()); if (!error) { _destination_table = doc.as>(); - RNS::extreme("Transport::start: successfully deserialized path table"); + RNS::extreme("Transport::start: successfully deserialized path table with " + std::to_string(_destination_table.size()) + " entries"); // Calculate crc for dirty-checking before write _destination_table_crc = crypto_crc8(0, data.data(), data.size()); std::vector invalid_paths; @@ -731,12 +728,11 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); _tables_last_culled = OS::time(); } - // Periodically persist data - if (OS::time() > (_last_saved + _save_interval)) { - // CBA temporarily disabled for testing with empty destination table - persist_data(); - _last_saved = OS::time(); - } + // CBA Periodically persist data + //if (OS::time() > (_last_saved + _save_interval)) { + // persist_data(); + // _last_saved = OS::time(); + //} } else { // Transport jobs were locked, do nothing @@ -1124,19 +1120,19 @@ extern uint8_t crypto_crc8(uint8_t tag, const void *data, unsigned size); //z timer.start() if (wait_time < 1000) { - extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(wait_time) + "ms"); + extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string((int)wait_time) + " ms"); } else { - extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(OS::round(wait_time/1000,1)) + "s"); + extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(OS::round(wait_time/1000,1)) + " s"); } } else { double wait_time = std::max(interface.announce_allowed_at() - OS::time(), (double)0); if (wait_time < 1000) { - extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(wait_time) + "ms"); + extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string((int)wait_time) + " ms"); } else { - extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(OS::round(wait_time/1000,2)) + "s"); + extreme("Added announce to queue (height " + std::to_string(interface.announce_queue().size()) + ") on " + interface.toString() + " for processing in " + std::to_string(OS::round(wait_time/1000,1)) + " s"); } } } @@ -2744,7 +2740,7 @@ Deregisters an announce handler. bool success = RNS::Utilities::OS::remove_file(packet_cache_path.c_str()); double diff_time = OS::time() - start_time; if (diff_time < 1.0) { - debug("Remove cached packet in " + std::to_string(diff_time * 1000) + " ms"); + debug("Remove cached packet in " + std::to_string((int)(diff_time*1000)) + " ms"); } else { debug("Remove cached packet in " + std::to_string(diff_time) + " s"); @@ -3473,25 +3469,14 @@ will announce it. if (success) { double save_time = OS::time() - save_start; if (save_time < 1.0) { - //debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(OS::round(save_time * 1000, 2)) + " ms"); - debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(save_time * 1000) + " ms"); + //debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(OS::round(save_time * 1000, 1)) + " ms"); + debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string((int)(save_time*1000)) + " ms"); } else { - //debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(OS::round(save_time, 2)) + " s"); + //debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(OS::round(save_time, 1)) + " s"); debug("Saved " + std::to_string(_destination_table.size()) + " path table entries in " + std::to_string(save_time) + " s"); } } - -#ifdef ARDUINO - // write time offset to file - { - std::string time_offset_path = Reticulum::_storagepath + "/time_offset"; - uint64_t offset = OS::ltime(); - debug("Writing time offset of " + std::to_string(offset) + " to file"); - Bytes buf((uint8_t*)&offset, sizeof(offset)); - OS::write_file(buf, time_offset_path.c_str()); - } -#endif } catch (std::exception& e) { error("Could not save path table to storage, the contained exception was: " + std::string(e.what())); diff --git a/src/Type.h b/src/Type.h index 46707e9..f6d57fe 100644 --- a/src/Type.h +++ b/src/Type.h @@ -70,10 +70,12 @@ namespace RNS { namespace Type { static const uint16_t MDU = MTU - HEADER_MAXSIZE - IFAC_MIN_SIZE; - static const uint32_t RESOURCE_CACHE = 24*60*60; - static const uint16_t JOB_INTERVAL = 5*60; - static const uint16_t CLEAN_INTERVAL = 15*60; - static const uint16_t PERSIST_INTERVAL = 60*60*12; + static const uint32_t RESOURCE_CACHE = 60*60*24; + static const uint16_t JOB_INTERVAL = 60*5; + static const uint16_t CLEAN_INTERVAL = 60*15; + // CBA + //static const uint16_t PERSIST_INTERVAL = 60*60*12; + static const uint16_t PERSIST_INTERVAL = 60*60; static const uint16_t GRACIOUS_PERSIST_INTERVAL = 60*5; static const uint8_t DESTINATION_LENGTH = TRUNCATED_HASHLENGTH/8; // In bytes diff --git a/src/Utilities/OS.cpp b/src/Utilities/OS.cpp index 4255d98..08b8c16 100644 --- a/src/Utilities/OS.cpp +++ b/src/Utilities/OS.cpp @@ -2,12 +2,35 @@ #include "../Log.h" +#ifdef ARDUINO +#ifdef __arm__ +// should use uinstd.h to define sbrk but Due causes a conflict +//extern "C" char* sbrk(int incr); +#else +extern char *__brkval; +#endif +#endif + using namespace RNS; using namespace RNS::Utilities; /*static*/ Filesystem OS::filesystem = {Type::NONE}; /*static*/ uint64_t OS::timeOffset = 0; +/*static*/ int OS::freeMemory() { +#ifdef ARDUINO + char top; +#ifdef __arm__ + return &top - reinterpret_cast(sbrk(0)); +#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151) + return &top - __brkval; +#else + return __brkval ? &top - __brkval : &top - __malloc_heap_start; +#endif + return -1; +#endif +} + /*static*/ void OS::register_filesystem(Filesystem& filesystem) { extreme("Registering filesystem..."); OS::filesystem = filesystem; diff --git a/src/Utilities/OS.h b/src/Utilities/OS.h index 3102925..f066f65 100644 --- a/src/Utilities/OS.h +++ b/src/Utilities/OS.h @@ -27,6 +27,8 @@ namespace RNS { namespace Utilities { static inline uint64_t getTimeOffset() { return timeOffset; } static inline void setTimeOffset(uint64_t offset) { timeOffset = offset; } + static int freeMemory(); + #ifdef ARDUINO // return current time in milliseconds since startup static inline uint64_t ltime() { @@ -77,6 +79,14 @@ namespace RNS { namespace Utilities { static bool remove_directory(const char* directory_path); static std::list list_directory(const char* directory_path); + static inline int storage_size() { + return -1; + } + + static inline int storage_available() { + return -1; + } + }; } }