Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Platform and RMW APIs

Reference · Level 4: Extending nano-ros

nano-ros has two porting boundaries, and this page is the reference for both. The platform API sits between nano-ros and a concrete OS, RTOS or bare-metal target: each platform provides a clock, and optionally a heap, threading and networking. The RMW API sits between nano-ros and a concrete pub/sub transport (The RMW API, below). Both are internal: user applications use the Rust, C and C++ APIs, not either vtable directly.

nano-ros separates the platform layer (clock, sleep, allocator, threading, critical section, network, timer, …) from the rest of the code via a single canonical C ABI of free extern "C" symbols. Every supported port — POSIX, FreeRTOS, NuttX, ThreadX, Zephyr, bare-metal Cortex-M, bare-metal RISC-V — implements the same symbols against its host kernel. RMW backends, codegen output, and the nros-node runtime all link against the ABI; nobody links against a specific port’s Rust crate.

Canonical reference

The SSoT C headers in packages/platform/nros-platform-api/include/nros/ (platform.h + platform_net.h, platform_timer.h, platform_zephyr.h, platform_freertos.h; Rust consumes committed bindgen output from them) are the source of truth. Every symbol’s brief, parameter docs, ownership rules, blocking / non-blocking classification, and ISR-safe contract live in the Doxygen compiled from those headers.

SurfaceLink
platform ABI Doxygen (canonical)HTML · headers

Regenerating the Doxygen in the tree: see Internals → Build Commands.

This page does not duplicate the interface specification — read the Doxygen for that.

Surface

Three hand-written headers under packages/platform/nros-platform-cffi/include/nros/:

HeaderPurpose
platform.hCore kernel surface: clock, sleep, alloc + heap stats, threading, scheduler, time, yield, random, critical section, opaque wake primitive.
platform_net.hNetwork surface: TCP/UDP/multicast socket helpers, endpoint resolution, IVC.
platform_timer.hPeriodic timer surface (nros_platform_timer_*).

Every symbol has the prefix nros_platform_. A drift gate (scripts/check-platform-abi-mirror.sh) walks each header and asserts that the unsafe extern "C" {} mirror block in packages/platform/nros-platform-cffi/src/lib.rs declares the same set — no symbol can land in C without its Rust mirror, no Rust mirror can declare a symbol without a C header decl. CI runs the gate on every build.

Capability groups

Within platform.h, the symbols are grouped by trait. Each group is exported by one Rust trait + one C-port section + one drift-gate match. Adding a capability means adding a row to every column.

CapabilityRust traitC sectionSymbols
ClockPlatformClockclock_nsnros_platform_clock_ns
SleepPlatformSleepsleep_msnros_platform_sleep_ms
AllocPlatformAllocmalloc/realloc/freenros_platform_alloc{,_realloc,_free}
ThreadingPlatformThreadingmutex/condvar/tasknros_platform_{mutex,condvar,task}_*
Critical sectionPlatformCriticalSectionper-CPU interrupt masknros_platform_critical_section_{acquire,release}
SchedulerPlatformSchedulertask hintsnros_platform_scheduler_*
TimePlatformTimewall-clock, and what it means (synced, which source, steps since boot)nros_platform_time_now_ns, nros_platform_wall_clock_state
YieldPlatformYieldcooperative yieldnros_platform_yield_now
RandomPlatformRandombest-effort RNGnros_platform_random_*
WakePlatformThreading (wake methods)opaque binary-semaphorenros_platform_wake_{init,drop,wait_ms,signal,signal_from_isr,storage_size,storage_align}

platform_net.h covers TCP/UDP/multicast/IVC, and platform_timer.h covers periodic timers. The shapes follow the same pattern: trait → macro → header → drift gate.

How a port is built

A port can be written in either of two ways. The choice is per-port and not visible to consumers — both shapes resolve to the same symbols.

Path A — Rust trait + macro export

The Rust crate implements PlatformClock, PlatformAlloc, PlatformThreading, … on a marker type, then invokes one of the export macros:

#![allow(unused)]
fn main() {
use nros_platform_api::{
    PlatformClock, PlatformSleep, PlatformAlloc, PlatformThreading,
    PlatformCriticalSection,
    nros_platform_export, nros_platform_export_net, nros_platform_export_timer,
};

pub struct PosixPlatform;
impl PlatformClock for PosixPlatform { /* … */ }
impl PlatformSleep for PosixPlatform { /* … */ }
// … etc.

nros_platform_export!(PosixPlatform);
nros_platform_export_net!(PosixPlatform);
nros_platform_export_timer!(PosixPlatform);
}

The macros expand to the full set of #[unsafe(no_mangle)] pub extern "C" fn nros_platform_* bodies forwarding to the trait calls. This is what nros-platform-posix does today. It is also what the bare-metal board crates (nros-platform-mps2-an385, nros-platform-stm32f4) use: there is no host kernel to write idiomatic C against, so the single-task stub impls are written in Rust and exported via the macro.

Path B — pure C port

The kernel-side ports (FreeRTOS, NuttX, ThreadX, Zephyr) write the bodies directly in src/platform.c, src/net.c, src/timer.c inside each packages/platform/nros-platform-<rtos>/ directory. The Rust side has no implementation file — the build script (or parent build system: NuttX make, Zephyr west module) compiles the C sources against the kernel’s headers.

Pure-C bodies are the more natural fit when the kernel already speaks C and ships C headers (xSemaphoreCreate*, k_sem_*, tx_thread_*): the impl is one-to-one with the kernel call, no FFI dance through Rust’s calling convention. The shared crate nros-platform-critical-section is the canonical example for a single-symbol shim: the Rust side just calls the externs and registers the result with critical_section::set_impl!.

Design

Why the ABI is free symbols rather than a vtable, and why threading and sockets stay per-RTOS while the scalar services are unified, is in Internals → Platform API design.

Reference implementations

Each row is a complete worked example. The crate’s README.md walks the implementation; the source is the worked solution to copy.

CrateTargetSource
nros-platform-posixLinux / *BSDpackages/platform/nros-platform-posix
nros-platform-nuttxNuttX RTOSpackages/platform/nros-platform-nuttx
nros-platform-freertosFreeRTOSpackages/platform/nros-platform-freertos
nros-platform-threadxAzure RTOS / ThreadXpackages/platform/nros-platform-threadx
nros-platform-zephyrZephyr RTOSpackages/platform/nros-platform-zephyr
nros-platform-mps2-an385Cortex-M3 (QEMU)packages/platform/nros-platform-mps2-an385
nros-platform-stm32f4STM32F4packages/platform/nros-platform-stm32f4

The POSIX implementation is the canonical reference port.

Writing a custom platform

The RMW API

The RMW (ROS middleware) vtable is the porting boundary between nano-ros and a concrete pub/sub transport. The backends in nano-ros’s tree are listed below; a backend in your own workspace is selected the same way.

The vtable

The C vtable nros_rmw_vtable_t is the source of truth. Each function pointer’s brief, its parameters, its ownership rules (a borrowed buffer or a caller-owned one), whether it blocks, its return codes and the lifetime of loaned slots are in the Doxygen output, compiled from the header.

SurfaceLink
rmw-cffi Doxygen (canonical)HTML · header

To regenerate the Doxygen in the tree, see Internals → Build Commands. This page does not duplicate the interface specification: read the Doxygen for that.

Backends in the tree

A backend is a package whose package.xml announces <nano_ros_provides kind="rmw" name="..."/>, with an nros-rmw.toml beside it. The build finds backends by that announcement, in nano-ros’s tree and in your workspace alike, so this table is generated from the tree’s announcements rather than typed. Each package’s README.md walks its implementation, and the source is the worked example to copy.

rmw =PackageAboutLinks through
cyclonedds or rmw-cycloneddsnros_rmw_cycloneddsThe Cyclone DDS RMW backend for nano-ros — full DDS/RTPS, used where a nano-ros node must speak natively to stock ROS 2 peers.cargo crate nros-rmw-cyclonedds-sys, CMake target nros_rmw_cyclonedds
uorb or rmw-uorbnros_rmw_uorbThe uORB RMW backend for nano-ros — PX4’s in-process message bus.CMake target nros_rmw_uorb
xrce or rmw-xrcenros_rmw_xrceThe Micro XRCE-DDS RMW backend for nano-ros — client/agent transport for constrained targets that cannot host a full DDS stack.cargo crate nros-rmw-xrce-cffi, CMake target nros_rmw_xrce
zenoh or rmw-zenohnros_rmw_zenohThe zenoh RMW backend for nano-ros (zenoh-pico on embedded targets).cargo crate nros-rmw-zenoh

The zenoh-pico shim is the canonical reference port.

Writing a custom backend

Design background