nros_params/lib.rs
1//! Parameter server for nros
2//!
3//! This crate provides a ROS 2 compatible parameter server for embedded systems.
4//! Parameters live in storage the CALLER places and lends to the server
5//! ([`ParameterStorage`] / [`ParameterTable`]); [`MAX_PARAMETERS`] is only the
6//! default capacity of that storage.
7//!
8//! # Example
9//!
10//! ```
11//! // The store's operations are behind `ParameterServerOps`;
12//! // a `nros` user reaches parameters through its `Node` instead.
13//! use nros_params::{
14//! NodeKey, ParameterDescriptor, ParameterServer, ParameterServerOps as _, ParameterStorage,
15//! ParameterType, ParameterValue,
16//! };
17//!
18//! // Storage is CALLER-OWNED: place it (a `static`, a struct
19//! // field, a local), then lend it. `ParameterStorage` defaults to
20//! // `MAX_PARAMETERS` slots; any length works, and the server's capacity is
21//! // whatever it is handed.
22//! let mut storage = ParameterStorage::<8>::new();
23//! let mut server = ParameterServer::new_in(storage.as_table());
24//!
25//! // Every per-parameter call names the NODE it is about, so
26//! // two nodes composed onto one executor never share a name.
27//! let node = NodeKey::PRIMARY;
28//!
29//! // Declare a simple parameter
30//! server.declare(node, "max_speed", ParameterValue::Double(1.0));
31//!
32//! // Declare a parameter with constraints
33//! let desc = ParameterDescriptor::new("velocity", ParameterType::Double)
34//! .unwrap()
35//! .with_description("Maximum velocity in m/s")
36//! .with_float_range(0.0, 10.0, 0.1);
37//! server.declare_with_descriptor(node, "velocity", ParameterValue::Double(5.0), Some(desc));
38//!
39//! // Get and set parameters
40//! assert_eq!(server.get_double(node, "max_speed"), Some(1.0));
41//! server.set_double(node, "max_speed", 2.0);
42//! ```
43//!
44//! # Features
45//!
46//! - `alloc` - Enable heap allocation
47
48#![no_std]
49
50// phase-359 W10 — no `std` feature; the test harness is hosted.
51#[cfg(test)]
52extern crate std;
53
54#[cfg(feature = "alloc")]
55extern crate alloc;
56
57macro_rules! store_size_knob {
58 () => {
59 "Raise [knobs.params] max_parameters in system.toml, NROS_PARAMS_MAX_PARAMETERS or \
60 CONFIG_NROS_PARAMS_MAX_PARAMETERS."
61 };
62}
63macro_rules! store_size_derived {
64 () => {
65 "Or state none: the build derives it from what each node declares, in its code or \
66 under `params:` in its contract."
67 };
68}
69
70pub(crate) mod config;
71// phase-359 W10 / issue 0080 — `persist` is GONE. It held the parameter
72// PERSISTENCE seam (`ParamStore`, ~~`NullParamStore`~~, ~~`ParamStoreError`~~,
73// ~~`FileParamStore`~~), which 0080 ruled a non-goal in July: nano-ros does not
74// persist parameters on-device, and launch-baked defaults are the supported
75// model. Runtime get/set/describe — the `server` module — stay.
76// phase-446 W6 -- what each node's contract declares, looked up when the code
77// declares a parameter.
78pub mod declared;
79// Issue 2650 -- the probe's view of descriptor prose lengths.
80#[cfg(feature = "descriptor-text-observer")]
81pub mod descriptor_text_observer;
82// Issue 2638 -- the reading of a launch parameter's baked text.
83pub mod launch_text;
84pub mod ops;
85pub mod request_size;
86pub mod server;
87pub mod typed;
88pub mod types;
89
90// Re-export main types
91pub use declared::{DeclaredParamMismatch, DeclaredParams};
92pub use launch_text::{LaunchTextError, LaunchTextType, launch_text_extent, launch_text_type};
93pub use ops::ParameterServerOps;
94pub use server::{
95 DeclarationError, LegacyParameterBuilder, MAX_ON_SET_CALLBACKS, OnSetContext, OnSetParameterFn,
96 OnSetParameterHandle, PARAMETER_EVENT_LOG_LEN, ParameterEventKind, ParameterServer,
97 ParameterSlot, ParameterStorage, ParameterTable, PendingParameterEvent,
98};
99#[allow(deprecated)]
100pub use typed::UndeclaredParameters;
101pub use typed::{
102 MandatoryParameter, OptionalParameter, ParameterBuilder, ParameterError, Parameters,
103 RangeConvertible, ReadOnlyParameter,
104};
105/// Deprecated: the store answers `Result<(), ParameterError>`.
106#[allow(deprecated)]
107pub use types::SetParameterResult;
108pub use types::{
109 DescriptorText, FloatingPointRange, IMPLIED_STORE_SLOTS, IntegerRange, MAX_ARRAY_LEN,
110 MAX_BYTE_ARRAY_LEN, MAX_PARAM_CONSTRAINTS_LEN, MAX_PARAM_DESCRIPTION_LEN, MAX_PARAM_NAME_LEN,
111 MAX_PARAMETERS, MAX_STRING_ARRAY_LEN, MAX_STRING_VALUE_LEN, NodeFlags, NodeKey,
112 PARAM_EVENTS_DEPTH, Parameter, ParameterDescriptor, ParameterRange, ParameterType,
113 ParameterValue, ParameterVariant, STOCK_STORE_SLOTS, fit_description,
114};
115
116/// The knob that sizes the parameter store, in every spelling a user writes.
117///
118/// One of the two lines a refused declaration's report adds, each short enough
119/// for one log record (128 bytes is the smallest `NROS_LOG_BUFFER_BYTES`), so a
120/// small board does not cut the knob's name off.
121// GitHub #4018. One text, so the Rust, C and C++ reports name the knob one way.
122pub const STORE_SIZE_KNOB: &str = store_size_knob!();
123
124/// When the store's size is derived instead of stated. The second line of the
125/// report; see [`STORE_SIZE_KNOB`].
126// The derivation is `ParamDeclarations::sizing` in the CLI's entity
127// inventory (every declared name plus one `use_sim_time` per node), carried to
128// `build.rs` by the sizing descriptor.
129pub const STORE_SIZE_DERIVED: &str = store_size_derived!();
130
131/// [`STORE_SIZE_KNOB`] and [`STORE_SIZE_DERIVED`] as one text, for a report
132/// with no record limit (an error's `Display`).
133pub const STORE_SIZE_REMEDY: &str = concat!(store_size_knob!(), " ", store_size_derived!());
134
135/// The build-time refusal of an image that serves the parameter services
136/// while its build carves no store ([`IMPLIED_STORE_SLOTS`] is 0), which the
137/// `nros::main!` expansion const-asserts. It ends in [`STORE_SIZE_REMEDY`], so
138/// the build and every runtime report name the knob in the same words.
139// Issue 2777: such a build used to ask the heap for the whole crate-default
140// store in one allocation at boot.
141pub const STORE_NOT_CARVED: &str = concat!(
142 "this image serves the parameter services, but its build sets aside no parameter \
143 store, so the store would be one heap allocation at boot sized by the crate \
144 default. ",
145 store_size_knob!(),
146 " ",
147 store_size_derived!()
148);