Skip to main content

Embedded Lite crates and handheld FFI

Ratspeak's embedded stack brings byte-exact Reticulum and LXMF to memory-constrained microcontrollers with no_std Rust, zero heap allocation, and fixed-capacity tables. Firmware supplies clocks, cryptographic entropy, persistent storage, and radio/TCP I/O.

Architecture & Rust↔C FFI boundary​

1. rsReticulumLite (rns-lite-core)​

rns-lite-core is a no_std, allocation-free port of rsReticulum (MSRV Rust 1.87+). It implements packet encoding, signed announces, path discovery, transport forwarding, TCP client tunnel synthesis, Link handshakes, session encryption, proofs, optional IFAC, persistent ratchet/known-destination codecs, and LoRa split framing with airtime limiting and CSMA backoff.

Node profiles (MicroNode, SmallNode, LiteNode)​

ProfileIntended hardwareMemory strategy
MicroNodeTight internal-RAM targets (e.g., Cardputer Adv)Fixed small tables, compile-checked at ≤ 32 KB internal RAM (profile-micro).
SmallNodePSRAM-backed ESP32-S3 handhelds (T-Deck Plus, T-Pager, ThinkNode M9)Larger path, announce, and known-destination tables allocated in caller-owned PSRAM (profile-small).
LiteNode<...>Custom MCU integrationsGeneric const-sized tables tailored to your exact RAM budget.

Resource transfer ceiling (3,659 bytes)​

Local Reticulum Resource transfers sent or received by rns-lite-core are bounded to 3,659 bytes of data per transfer (encrypted, uncompressed, single-segment). Forwarding third-party Resource packets between other nodes is not subject to this cap.

Example: building & draining a path request​

Directly from rsReticulumLite/examples/path_request.rs (run on host with cargo +1.89.0 run --example path_request --locked):

use rns_lite_core::{LiteConfig, MicroNode, OutboundReason};

fn main() {
let mut node = MicroNode::new(LiteConfig::ESP32_LORA_TRANSPORT_MICRO, [0xa5; 16])
.expect("reviewed MicroNode profile");

let destination = [0x42; 16];
let request_tag = [0x7c; 16];
node.request_path(&destination, &request_tag, 1, 0)
.expect("request packet fits the fixed buffer");

// Select & take ownership of the queued outbound frame before handing it to a radio driver
let queued = *node
.retained_outbound_select(0, 0, [0, 1, 0, 0, 0, 0, 0])
.expect("path request was queued");
assert!(node.retained_outbound_take(queued.delivery.identity));
assert_eq!(queued.frame.reason, OutboundReason::PathRequest);
}

Pinning & cross-check verification​

rsReticulumLite is consumed from a pinned Git commit (not crates.io):

[dependencies]
rns-lite-core = { git = "https://github.com/ratspeak/rsReticulumLite.git", rev = "<pinned-commit>" }

TRUSTED_REF pins the exact upstream rsReticulum revision used by ./scripts/test-matrix.sh (host unit tests, ARM/RISC-V bare-metal builds, Clippy, rustdoc, and byte-exact parity tests against full rsReticulum).

2. rsLXMFLite (lxmf-lite-core)​

lxmf-lite-core is a no_std, zero-allocation LXMF codec built on rns-lite-core (MSRV Rust 1.87+):

  • Zero-alloc field parsing (LxmfFieldScratch): Incoming parsers take a caller-owned 1 KiB LxmfFieldScratch buffer, supporting up to 64 outer fields, max nesting depth 8, full-width integer IDs, and rejecting duplicate outer IDs. Malformed optional fields do not corrupt preserved message bytes.
  • Delivery codecs: Single-packet opportunistic encryption (seal_opportunistic + ticket_stamp), Link/Resource builders (build_link / parse_link), canonical *_with_fields builders, audio [mode, binary] fields, and client-side propagation wrapping/retrieval (does not run a propagation server or compression).
  • Resumable streaming PoW: Stamp generation and verification stream HKDF workblock preparation in bounded slices rather than allocating a multi-megabyte workblock — letting firmware keep the UI responsive while mining stamps.
  • Caller-owned policy: Signature validation does not enforce recipient stamp costs, message age, or replay policy; firmware must enforce those policies explicitly.

Directly from rsLXMFLite/examples/link_message.rs (run on host with cargo +1.96.0 run --example link_message --locked):

use lxmf_lite_core::{LxmfFieldScratch, build_link, parse_link};
use rns_lite_core::LocalIdentity;

fn main() {
let source = LocalIdentity::from_private_key(&[0x11; 64]);
let recipient = LocalIdentity::from_private_key(&[0x22; 64]);
let mut packed = [0u8; 500];
let mut build_scratch = [0u8; 500];
let mut destination = [0u8; 16];
let mut message_id = [0u8; 32];

let len = build_link(
&source,
recipient.public_key(),
1_750_000_000.0,
b"hello",
b"from a bounded MCU codec",
&mut packed,
&mut build_scratch,
&mut destination,
&mut message_id,
)
.expect("message fits the fixed buffers");

let mut parse_scratch = [0u8; 500];
let mut field_scratch = LxmfFieldScratch::new();
let view = parse_link(
&recipient,
&packed[..len],
source.public_key(),
&mut parse_scratch,
&mut field_scratch,
)
.expect("freshly built message validates");

assert_eq!(view.title, b"hello");
}

Both RNS_LITE_REF (pinned rsReticulumLite) and TRUSTED_REF (pinned rsReticulum + rsLXMF) are validated by ./scripts/test-matrix.sh.

3. ratspeak-handheld Rust↔C FFI (protocol/)​

ratspeak-handheld compiles rns-lite-core, lxmf-lite-core, lxst-embedded, and lxst-codec2 into pre-built Xtensa static archives (protocol/prebuilt/xtensa-esp32s3/{small,micro}/libratspeak_protocol.a), exposed to ESP32-S3 C++ firmware through protocol/include/ratspeak_protocol.h.

ABI & memory ownership contract​

  • Opaque context, strict ownership: Create with rs_handheld_rns_init(&ctx), destroy exactly once with rs_handheld_rns_shutdown(ctx). No Rust allocation is freed by C, and no C allocation is freed by Rust. All functions are thread-compatible but not internally synchronized (caller serializes access per context).
  • Caller-owned transport & scratch buffers: Firmware queries transport size/alignment, allocates the node buffer (SMALL in PSRAM on T-Deck Plus / T-Pager / ThinkNode M9; MICRO in ≤ 32 KB internal RAM on Cardputer Adv), and passes it to rs_handheld_rns_open_transport (which constructs the node in place without a node-sized stack temporary). Teardown order is strict: close resources → rs_handheld_rns_shutdown(ctx) → free(buf).
  • Key hygiene: Raw private keys are 64 bytes (X25519 priv 32 || Ed25519 seed 32). Rust zeroizes its internal copy on shutdown or replacement; firmware must wipe exported key and ratchet-ring buffers after durable writes.
  • Zero-unwind panic contract: Unwinding never crosses the C FFI boundary. Host test builds catch Rust panics and return RS_HANDHELD_ERR_INTERNAL (255). Firmware builds compile with panic = abort; a panic aborts and the hardware watchdog resets the device.
  • Status enum (rs_handheld_status_t): int-sized (#[repr(i32)]; do not compile with -fshort-enums) covering RS_HANDHELD_OK (0), ERR_INVALID_ARG (1), ERR_CAPACITY (2), ERR_CRYPTO (3), ERR_NOT_READY (4), ERR_UNSUPPORTED (5), ERR_RETRY (6 — transient entropy collision, retry with fresh CSPRNG bytes), ERR_SOURCE_UNKNOWN (7), RS_HANDHELD_PENDING (8 — waiting on local stamp work), and ERR_INTERNAL (255).

Resumable PoW stamp C API​

Firmware slices stamp mining across UI ticks using 8.8 fixed-point costs (256 per unit, max RS_HANDHELD_STAMP_COST_MAX_FP = 5120 = 20.0):

rs_handheld_stamp_t *job = NULL;
rs_handheld_status_t st = rs_handheld_stamp_create(
material_32, /* kind: 0=recipient, 1=propagation */ 0,
/* cost_fp (e.g. 12.0 = 3072) */ 3072, seed_32, /* max attempts */ 1000000, &job);

rs_handheld_stamp_progress_t prog;
while (st == RS_HANDHELD_OK) {
/* Bounded slice: at most 4 prep rounds or 1024 nonces per call */
st = rs_handheld_stamp_step(job, 2, 256, &prog);
if (prog.state >= 2) break; /* 2=complete, 3=exhausted, 4=cancelled */
/* yield to LVGL / radio loop */
}
rs_handheld_stamp_destroy(job);

Network destination and Link-key stamps follow the same resumable pattern via rs_handheld_network_stamp_create / _step / _destroy and rs_handheld_rns_install_destination_stamp.

Rebuilding libratspeak_protocol.a (build-xtensa.sh)​

Normal handheld firmware builds (make package DEVICE=tdeck) link the committed .a archives so firmware contributors do not need an Xtensa Rust toolchain. When modifying protocol/ or updating Lite crates, rebuild both profiles deterministically with protocol/build-xtensa.sh:

cd ratspeak-handheld/protocol
./build-xtensa.sh

build-xtensa.sh validates the pinned esp toolchain against tools/release_identity.json, remaps stage/registry/sysroot paths for reproducible builds, compiles small and micro offline with --profile xtensa (opt-level=s, fat LTO, codegen-units=1, panic=abort, -Zbuild-std=core,alloc), and writes cryptographic hashes and source commits to prebuilt/xtensa-esp32s3/PROVENANCE.txt.

Next steps​