Files
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

137 lines
5.6 KiB
Markdown

# microReticulum
Port of Reticulum Network Stack to C++ specifically but not exclusively targeting 32-bit and better MCUs.
Note that this is a library implementing the Reticulum Network Stack and not a useful application on its own. For an actual application, see [microReticulum_Firmware](https://github.com/attermann/microReticulum_Firmware) which is a version of RNode firmware that implements a Reticulm Transport Node.
## Dependencies
Build environment is configured for use in [VSCode](https://code.visualstudio.com/) and [PlatformIO](https://platformio.org/).
This API is dependent on the following external libraries:
- [rweather/Crypto](https://github.com/rweather/arduinolibs)
- [ArduinoJson](https://github.com/bblanchon/ArduinoJson)
- [MsgPack](https://github.com/hideakitai/MsgPack)
## 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`)
## Memory Management Build Options
Two classes of memory allocator are defined; the container allocator (`RNS_CONTAINER_ALLOCATOR`) which is used for certain long-lived STL containers (e.g. path table), and the default allocator (`RNS_DEFAULT_ALLOCATOR`) which is used for all other C++ memory allocation (new/delete).
Each class of allocator can be configured to use a separate type of allocator. Available memory allocators are:
- `RNS_HEAP_ALLOCATOR`: Basic allocator that uses default system HEAP.
- `RNS_HEAP_POOL_ALLOCATOR`: Optimized TLSF managed HEAP buffer
- `RNS_PSRAM_ALLOCATOR`: Basic allocator that uses PSRAM memory (if PSRAM is available)
- `RNS_PSRAM_POOL_ALLOCATOR`: Optimized TLSF managed PSRAM buffer (if PSRAM is available)
Each class of allocator pool can have a separate buffer size:
- `RNS_HEAP_POOL_BUFFER_SIZE=N` Defines the HEAP TLSF memory pool buffer size (N bytes, default 0 which means dynamic)
- `RNS_PSRAM_POOL_BUFFER_SIZE=N` Defines the PSRAM TLSF memory pool buffer size (N bytes, default 0 which means dynamic)
"POOL" allocators use TLSF (Two-Level Segregate Fit) for efficient management of constrained MCU memory with minimal fragmentation.
The allocator for each class can be configured using build flags in `platformio.ini, for example:
```
[env]
build_flags =
-DRNS_DEFAULT_ALLOCATOR=RNS_HEAP_ALLOCATOR
-DRNS_CONTAINER_ALLOCATOR=RNS_PSRAM_POOL_ALLOCATOR
```
NOTE: Currently `-DRNS_DEFAULT_ALLOCATOR=RNS_HEAP_POOL_ALLOCATOR` is only required on NRF52 boards since ESP32 already uses TLSF internally.
## Building
Building and uploading to hardware is simple through the VSCode PlatformIO IDE
- Install VSCode and PlatformIO
- Clone this repo
- Lanch PlatformIO and load repo
- In PlatformIO, select the environment for intended board
- Build, Upload, and Monitor to observe application logging
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
- [x] Utility class to efficiently handle byte buffers
- [x] Implemenmtat basic Identity, Destination and Packet
- [x] Successful testing of Announce mechanism
- [x] Implement Ed2551 public-key signature and X25519 key exchange
- [x] Implement encryption including AES, AES-CBC, HKDF, HMAC, PKCS7, and Fernet
- [x] Successful testing of Packet encrypt/decrypt/sign/prof
- [x] Implement dynamic Interfaces
- [x] Implement UDP Interface for testing against Python reference Reticulum instances
- [x] Implement basic Transport functionality
- [x] Successful testing of end-to-end Path Finding
- [x] Implement persistence for storage of runtime data structures (config, identities, routing tables, etc.)
- [x] Support for Links and Resources in Transport
- [x] Implement Packet proofs
- [x] Successful testing of Transport of both Packets and Links
- [x] Implement memory management appropriate for memory constricted MCUs
- [x] Implement Link
- [x] Implement AES256
- [ ] Implement Ratchets
- [ ] Implement Resource
- [ ] Implement Channel and Buffer
- [x] Add example applications utilizing the C++ API
- [x] Configure PlatformIO to easily build and upload test applications to board
- [x] Add build tagets for all boards currently supported by RNode
- [ ] Implement new combination caching/storage manager for managing persistent data
Please open an Issue if you have trouble building ior using the API, and feel free to start a new Discussion for anything else.