Skip to main content

rns_embedded_ffi/lib_parts/
module_prelude.rs

1use alloc::{boxed::Box, string::String};
2
3use rns_embedded_core::{store::JournaledEmbeddedStore, transport::LinkState, EmbeddedError};
4
5use rns_embedded_runtime::{
6    ble::{BleShimConfig, BleShimTransport},
7    node::{CaptureDefaults, NodeLifecycleState, NodeTransportMode},
8    BleNodeBackendConfig, BroadcastOptions, EmbeddedNode, EmbeddedNodeRuntime, EventSubscription,
9    NodeBackendConfig, NodeConfig, NodeError, NodeEvent, NodeEventKind, NodeLogLevel, NodeRunState,
10    NodeStatus, PollResult, RuntimeConfig, SendOptions, TcpClientConfig, TcpServerConfig,
11};
12
13#[cfg(not(feature = "std"))]
14use core::panic::PanicInfo;
15
16#[cfg(feature = "std")]
17use std::panic::{catch_unwind, AssertUnwindSafe};
18
19#[cfg(not(feature = "std"))]
20use critical_section::RawRestoreState;
21
22#[cfg(not(feature = "std"))]
23use embedded_alloc::LlffHeap;
24
25#[cfg(not(feature = "std"))]
26#[global_allocator]
27static RNS_ALLOCATOR: LlffHeap = LlffHeap::empty();
28
29#[cfg(not(feature = "std"))]
30static mut RNS_ALLOCATOR_HEAP: [u8; 48 * 1024] = [0; 48 * 1024];
31
32#[cfg(not(feature = "std"))]
33static mut RNS_ALLOCATOR_READY: bool = false;
34
35#[cfg(not(feature = "std"))]
36#[panic_handler]
37fn panic(_info: &PanicInfo<'_>) -> ! {
38    loop {
39        core::hint::spin_loop();
40    }
41}
42
43#[cfg(not(feature = "std"))]
44struct NoopCriticalSection;
45
46#[cfg(not(feature = "std"))]
47critical_section::set_impl!(NoopCriticalSection);
48
49#[cfg(not(feature = "std"))]
50// SAFETY: this shim runs on single-threaded embedded builds where callers do not
51// rely on nested interrupt masking; the no-op critical section only gates local
52// allocator/runtime bootstrap paths in this crate.
53unsafe impl critical_section::Impl for NoopCriticalSection {
54    // SAFETY: acquire does not modify machine state and the paired restore token
55    // is the unit type, so callers can only observe the no-op contract above.
56    unsafe fn acquire() -> RawRestoreState {}
57
58    // SAFETY: release is paired with the no-op acquire implementation and has no
59    // machine state to restore for this single-threaded shim.
60    unsafe fn release(_restore_state: RawRestoreState) {}
61}
62
63#[repr(C)]
64pub struct RnsEmbeddedNodeConfig {
65    pub store_identity: [u8; 32],
66    pub lxmf_address: [u8; 16],
67    pub node_mode: RnsEmbeddedNodeMode,
68    pub announce_interval_ms: u64,
69    pub max_outbound_queue: usize,
70    pub max_events: usize,
71    pub capture_default_max_bytes: u32,
72    pub ble_mtu_hint: u16,
73    pub ble_max_inbound_frames: usize,
74    pub ble_max_outbound_frames: usize,
75    pub ble_ordered_delivery: bool,
76    pub tcp_host: [u8; 256],
77    pub tcp_port: u16,
78    pub tcp_listen_port: u16,
79}
80
81#[repr(C)]
82#[derive(Debug, Clone, Copy, Eq, PartialEq)]
83pub enum RnsEmbeddedNodeMode {
84    BleOnly = 0,
85    TcpClient = 1,
86    TcpServer = 2,
87}
88
89impl Default for RnsEmbeddedNodeConfig {
90    fn default() -> Self {
91        let runtime = RuntimeConfig::default();
92        let ble = BleShimConfig::default();
93        Self {
94            store_identity: runtime.store_identity,
95            lxmf_address: runtime.lxmf_address,
96            node_mode: RnsEmbeddedNodeMode::BleOnly,
97            announce_interval_ms: runtime.announce_interval_ms,
98            max_outbound_queue: runtime.max_outbound_queue,
99            max_events: runtime.max_events,
100            capture_default_max_bytes: runtime.capture_defaults.max_bytes,
101            ble_mtu_hint: ble.mtu_hint,
102            ble_max_inbound_frames: ble.max_inbound_frames,
103            ble_max_outbound_frames: ble.max_outbound_frames,
104            ble_ordered_delivery: ble.ordered_delivery,
105            tcp_host: [0; 256],
106            tcp_port: 0,
107            tcp_listen_port: 0,
108        }
109    }
110}
111
112#[repr(C)]
113#[derive(Debug, Clone, Copy, Eq, PartialEq)]
114pub enum RnsEmbeddedLinkState {
115    Down = 0,
116    Connecting = 1,
117    Up = 2,
118}
119
120#[repr(C)]
121#[derive(Debug, Clone, Copy, Eq, PartialEq)]
122pub enum RnsEmbeddedLifecycleState {
123    Boot = 0,
124    Unprovisioned = 1,
125    ProvisionedOffline = 2,
126    TcpOnline = 3,
127    BleRecovery = 4,
128    FailureReconnect = 5,
129}
130
131#[repr(C)]
132#[derive(Debug, Clone, Copy, Eq, PartialEq)]
133pub enum RnsEmbeddedStatus {
134    Ok = 0,
135    InvalidInput = 1,
136    InvalidArgument = 2,
137    InvalidState = 3,
138    NotFound = 4,
139    SeqGap = 5,
140    IntegrityFailure = 6,
141    ChecksumMismatch = 7,
142    IdempotencyConflict = 8,
143    ReplayRejected = 9,
144    Timeout = 10,
145    Backpressure = 11,
146    Disconnected = 12,
147    StorageCorruption = 13,
148    Unsupported = 14,
149}
150
151const RNS_EMBEDDED_V1_ABI_VERSION: u32 = 1;
152
153const RNS_EMBEDDED_V1_STRUCT_VERSION: u32 = 1;
154
155const RNS_EMBEDDED_V1_CAPABILITY_SCHEMA_VERSION: u32 = 1;
156
157const RNS_EMBEDDED_V1_CAP_MANAGED_RUNTIME: u64 = 1 << 0;
158
159const RNS_EMBEDDED_V1_CAP_BLOCKING_NEXT: u64 = 1 << 1;
160
161const RNS_EMBEDDED_V1_CAP_BROADCAST_EXPLICIT_LIST: u64 = 1 << 2;
162
163const RNS_EMBEDDED_V1_CAP_COMPAT_LEGACY_FFI: u64 = 1 << 3;
164
165const RNS_EMBEDDED_V1_CAP_EVENT_GAP_SIGNALING: u64 = 1 << 4;
166
167const RNS_EMBEDDED_V1_KNOWN_CAPABILITY_BITS: u64 = RNS_EMBEDDED_V1_CAP_MANAGED_RUNTIME
168    | RNS_EMBEDDED_V1_CAP_BLOCKING_NEXT
169    | RNS_EMBEDDED_V1_CAP_BROADCAST_EXPLICIT_LIST
170    | RNS_EMBEDDED_V1_CAP_COMPAT_LEGACY_FFI
171    | RNS_EMBEDDED_V1_CAP_EVENT_GAP_SIGNALING;
172
173const RNS_EMBEDDED_V1_DRIVER_TICK_TARGET_MS: u32 = 25;
174
175const RNS_EMBEDDED_V1_DRIVER_TICK_MAX_MS: u32 = 50;
176
177const RNS_EMBEDDED_V1_MAX_BLOCKING_TIMEOUT_MS: u64 = u32::MAX as u64;
178
179#[repr(C)]
180#[derive(Debug, Clone, Copy, Eq, PartialEq)]
181pub enum RnsEmbeddedV1RunState {
182    Stopped = 0,
183    Running = 1,
184}
185
186#[repr(C)]
187#[derive(Debug, Clone, Copy, Eq, PartialEq)]
188pub enum RnsEmbeddedV1LogLevel {
189    Error = 0,
190    Warn = 1,
191    Info = 2,
192    Debug = 3,
193    Trace = 4,
194}
195
196#[repr(C)]
197#[derive(Debug, Clone, Copy, Eq, PartialEq)]
198pub enum RnsEmbeddedV1EventKind {
199    StatusChanged = 0,
200    Log = 1,
201    Error = 2,
202    PacketReceived = 3,
203    PacketSent = 4,
204    Extension = 5,
205}
206
207#[repr(C)]
208#[derive(Debug, Clone, Copy, Eq, PartialEq)]
209pub enum RnsEmbeddedV1PollResultKind {
210    Event = 0,
211    Timeout = 1,
212    Closed = 2,
213    Gap = 3,
214    NodeStopped = 4,
215    NodeRestarted = 5,
216}