Files
microReticulum/src/Interface.cpp
T
Chad Attermann 4d6f0b9de2 Added dual-class PSRAM/TLSF allocator system and refactored memory management
Introduces a flexible two-class allocator architecture (RNS_DEFAULT_ALLOCATOR and RNS_CONTAINER_ALLOCATOR) supporting heap, PSRAM, and TLSF pool variants for fine-grained control over embedded memory layout.

- New Memory.h/cpp with ContainerAllocator<T>, heap/PSRAM/pool allocator types, and heap stats reporting
- IdentityTable and PathTable now use ContainerAllocator
- Replaced RNS_USE_TLSF/RNS_USE_ALLOCATOR/RNS_USE_PSRAM flags with per-class allocator flags (RNS_DEFAULT_ALLOCATOR, RNS_CONTAINER_ALLOCATOR)
- Added ESP32 PSRAM allocator support (RNS_PSRAM_ALLOCATOR, RNS_PSRAM_POOL_ALLOCATOR)
- Added Transport::get_path()/remove_path() helpers
- Added loop callback (set_loop_callback) to enable more responsive app during long operations (like clean_cache)
- Aded guard against re-entrancy to clean_cache()
- Added callback option to list_directory() to avoid heap allocation
- Added OOM auto-restart (ESP.restart/NVIC_SystemReset) in Interface send/receive and Reticulum loop.
- Renamed setLogCallback to set_log_callback
- Made Link/Identity static members private
- Added native14 env to CI and platformio.ini
- Bumped version to 0.2.10
2026-03-06 16:43:04 -07:00

142 lines
4.2 KiB
C++

#include "Interface.h"
#include "Identity.h"
#include "Transport.h"
using namespace RNS;
using namespace RNS::Type::Interface;
/*static*/ uint8_t Interface::DISCOVER_PATHS_FOR = MODE_ACCESS_POINT | MODE_GATEWAY;
void InterfaceImpl::handle_outgoing(const Bytes& data) {
//TRACEF("InterfaceImpl.handle_outgoing: data: %s", data.toHex().c_str());
TRACE("InterfaceImpl.handle_outgoing");
_txb += data.size();
}
void InterfaceImpl::handle_incoming(const Bytes& data) {
//TRACEF("InterfaceImpl.handle_incoming: data: %s", data.toHex().c_str());
TRACE("InterfaceImpl.handle_incoming");
_rxb += data.size();
// Create temporary Interface encapsulating our own shared impl
std::shared_ptr<InterfaceImpl> self = shared_from_this();
Interface interface(self);
// Pass data on to transport for handling
Transport::inbound(data, interface);
}
void Interface::send_outgoing(const Bytes& data) {
assert(_impl);
//TRACEF("Interface.send_outgoing: data: %s", data.toHex().c_str());
//TRACE("Interface.send_outgoing");
// Catch exceptions from calls into Interface implementation
try {
_impl->send_outgoing(data);
}
catch (const std::bad_alloc&) {
ERROR("Interface::send_outgoing: bad_alloc - OUT OF MEMORY");
// Critical OOM, restarting
#if defined(ESP32)
ESP.restart();
#elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT)
NVIC_SystemReset();
#endif
}
catch (std::exception& e) {
ERRORF("Interface::send_outgoing: %s", e.what());
}
}
void Interface::handle_incoming(const Bytes& data) {
assert(_impl);
//TRACEF("Interface.handle_incoming: data: %s", data.toHex().c_str());
//TRACE("Interface.handle_incoming");
/*
_impl->_rxb += data.size();
// Pass data on to transport for handling
Transport::inbound(data, *this);
*/
// Catch exceptions from calls into Interface implementation
try {
_impl->handle_incoming(data);
}
catch (const std::bad_alloc&) {
ERROR("Interface::handle_incoming: bad_alloc - OUT OF MEMORY");
// Critical OOM, restarting
#if defined(ESP32)
ESP.restart();
#elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_NRF52_ADAFRUIT)
NVIC_SystemReset();
#endif
}
catch (std::exception& e) {
ERRORF("Interface::handle_incoming: %s", e.what());
}
}
void Interface::process_announce_queue() {
/*
if not hasattr(self, "announce_cap"):
self.announce_cap = RNS.Reticulum.ANNOUNCE_CAP
if hasattr(self, "announce_queue"):
try:
now = time.time()
stale = []
for a in self.announce_queue:
if now > a["time"]+RNS.Reticulum.QUEUED_ANNOUNCE_LIFE:
stale.append(a)
for s in stale:
if s in self.announce_queue:
self.announce_queue.remove(s)
if len(self.announce_queue) > 0:
min_hops = min(entry["hops"] for entry in self.announce_queue)
entries = list(filter(lambda e: e["hops"] == min_hops, self.announce_queue))
entries.sort(key=lambda e: e["time"])
selected = entries[0]
double now = OS::time();
uint32_t wait_time = 0;
if (_impl->_bitrate > 0 && _impl->_announce_cap > 0) {
uint32_t tx_time = (len(selected["raw"])*8) / _impl->_bitrate;
wait_time = (tx_time / _impl->_announce_cap);
}
_impl->_announce_allowed_at = now + wait_time;
self.on_outgoing(selected["raw"])
if selected in self.announce_queue:
self.announce_queue.remove(selected)
if len(self.announce_queue) > 0:
timer = threading.Timer(wait_time, self.process_announce_queue)
timer.start()
except Exception as e:
self.announce_queue = []
RNS.log("Error while processing announce queue on "+str(self)+". The contained exception was: "+str(e), RNS.LOG_ERROR)
RNS.log("The announce queue for this interface has been cleared.", RNS.LOG_ERROR)
*/
}
/*
void ArduinoJson::convertFromJson(JsonVariantConst src, RNS::Interface& dst) {
TRACE(">>> Deserializing Interface");
TRACEF(">>> Interface pre: %s", dst.debugString().c_str());
if (!src.isNull()) {
RNS::Bytes hash;
hash.assignHex(src.as<const char*>());
TRACEF(">>> Querying Transport for Interface hash %s", hash.toHex().c_str());
// Query transport for matching interface
dst = Transport::find_interface_from_hash(hash);
TRACEF(">>> Interface post: %s", dst.debugString().c_str());
}
else {
dst = {RNS::Type::NONE};
TRACEF(">>> Interface post: %s", dst.debugString().c_str());
}
}
*/