Skip to main content

nros_params/
request_size.rs

1//! What a parameter-service REQUEST costs on the wire, from a node's declared
2//! parameter shape and the parameter store's capacities.
3// Issue 2702.
4//! The ONE formula for the request side. nros-node's
5//! `param_sizing::node_bound` prices every parameter-service message with it
6//! (the inbox slot and the build-time check that a stated slot fits), and
7//! `nros build` prices the same requests from the contract to suggest a slot
8//! size before any build script runs. Two readers, one function: a second
9//! spelling of this arithmetic is how one formula's inputs drift apart.
10//!
11//! Pure arithmetic over counts. It reads no `MAX_*` of this crate, so a caller
12//! that has not built the store (the CLI) passes the capacities it would build
13//! with.
14
15/// Capacity of every sequence member in the generated `rcl_interfaces` types
16/// (`heapless::Vec<_, 64>`). nros-node holds the drift guard against the
17/// generated messages (`wire_capacities_match_the_generated_messages`).
18pub const WIRE_SEQ_CAP: usize = 64;
19
20/// Capacity of every string member in the generated `rcl_interfaces` types
21/// (`heapless::String<256>`). Same drift guard.
22pub const WIRE_STRING_CAP: usize = 256;
23
24/// The CDR encapsulation header.
25pub const CDR_HEADER: usize = 4;
26/// A sequence's length word, worst-case padded.
27pub const CDR_SEQ: usize = 3 + 4;
28/// A string's length word and NUL, worst-case padded.
29pub const CDR_STR: usize = 3 + 4 + 1;
30/// An 8-byte word, worst-case padded.
31pub const CDR_WORD: usize = 7 + 8;
32/// One `ParameterValue` with every member empty.
33pub const CDR_VALUE_BASE: usize = 53;
34
35/// Field indices of one node's shape: the nine counts
36/// `nros_cli_core::entity_inventory::ParamServiceShape::token` writes, in order.
37pub const SHAPE_PARAMS: usize = 0;
38pub const SHAPE_NAME_BYTES: usize = 1;
39pub const SHAPE_PREFIXES: usize = 2;
40pub const SHAPE_PREFIX_BYTES: usize = 3;
41pub const SHAPE_STRINGS: usize = 4;
42pub const SHAPE_BYTE_ARRAYS: usize = 5;
43pub const SHAPE_BOOL_ARRAYS: usize = 6;
44pub const SHAPE_WORD_ARRAYS: usize = 7;
45pub const SHAPE_STRING_ARRAYS: usize = 8;
46
47/// The value capacities a request reaches the wire at: the store's, clamped
48/// to the `rcl_interfaces` message's own caps ([`Self::clamped`]).
49#[derive(Clone, Copy, Debug, PartialEq, Eq)]
50pub struct RequestCaps {
51    pub string: usize,
52    pub array: usize,
53    pub string_array: usize,
54    pub byte_array: usize,
55}
56
57const fn min_usize(a: usize, b: usize) -> usize {
58    if a < b { a } else { b }
59}
60
61impl RequestCaps {
62    /// The store's capacities as they reach the wire.
63    pub const fn clamped(
64        string: usize,
65        array: usize,
66        string_array: usize,
67        byte_array: usize,
68    ) -> Self {
69        Self {
70            string: min_usize(string, WIRE_STRING_CAP),
71            array: min_usize(array, WIRE_SEQ_CAP),
72            string_array: min_usize(string_array, WIRE_SEQ_CAP),
73            byte_array: min_usize(byte_array, WIRE_SEQ_CAP),
74        }
75    }
76}
77
78/// The three parameter-service requests' worst size, for one node.
79#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
80pub struct ParamRequests {
81    /// `get_parameters`, `describe_parameters`, `get_parameter_types`: the
82    /// node's every name.
83    pub names: usize,
84    /// `list_parameters`: its prefixes and a depth.
85    pub list: usize,
86    /// `set_parameters` (and the atomic one): every name with its value.
87    pub set: usize,
88}
89
90impl ParamRequests {
91    /// The largest of the three: what one inbox slot must hold.
92    pub const fn max(&self) -> usize {
93        let mut m = self.names;
94        if self.list > m {
95            m = self.list;
96        }
97        if self.set > m {
98            m = self.set;
99        }
100        m
101    }
102}
103
104/// The bytes a node's values occupy in a `set_parameters` request (and a
105/// `get_parameters` reply), at capacities `c`.
106pub const fn values_bytes(s: &[usize; 9], c: RequestCaps) -> usize {
107    s[SHAPE_PARAMS] * CDR_VALUE_BASE
108        + s[SHAPE_STRINGS] * c.string
109        + s[SHAPE_BYTE_ARRAYS] * c.byte_array
110        + s[SHAPE_BOOL_ARRAYS] * c.array
111        + s[SHAPE_WORD_ARRAYS] * (7 + 8 * c.array)
112        + s[SHAPE_STRING_ARRAYS] * c.string_array * (CDR_STR + c.string)
113}
114
115/// The bytes a node's names occupy in a request.
116pub const fn names_bytes(s: &[usize; 9]) -> usize {
117    s[SHAPE_NAME_BYTES] + s[SHAPE_PARAMS] * CDR_STR
118}
119
120/// The bytes a node's prefixes occupy in a `list_parameters` request.
121pub const fn prefixes_bytes(s: &[usize; 9]) -> usize {
122    s[SHAPE_PREFIX_BYTES] + s[SHAPE_PREFIXES] * CDR_STR
123}
124
125/// One node's three requests, at capacities `c`.
126pub const fn param_requests(s: &[usize; 9], c: RequestCaps) -> ParamRequests {
127    let head = CDR_HEADER + CDR_SEQ;
128    let names = names_bytes(s);
129    ParamRequests {
130        names: head + names,
131        list: head + prefixes_bytes(s) + CDR_WORD,
132        set: head + names + values_bytes(s, c),
133    }
134}
135
136/// The largest request any of `shapes`' nodes can produce at capacities `c`,
137/// rounded up to the 4-byte slot alignment the inbox uses.
138pub const fn worst_request_slot(shapes: &[[usize; 9]], c: RequestCaps) -> usize {
139    let mut worst = 0;
140    let mut i = 0;
141    while i < shapes.len() {
142        let m = param_requests(&shapes[i], c).max();
143        if m > worst {
144            worst = m;
145        }
146        i += 1;
147    }
148    worst.next_multiple_of(4)
149}
150
151#[cfg(test)]
152mod tests {
153    use super::*;
154
155    /// A set request carries every name and every value; a string array costs
156    /// its element count times one string each.
157    #[test]
158    fn a_set_request_carries_every_name_and_value() {
159        // 2 params (names 5 + 6 bytes), one string, one string array.
160        let shape = [2, 11, 1, 0, 1, 0, 0, 0, 1];
161        let c = RequestCaps::clamped(16, 4, 4, 8);
162        let r = param_requests(&shape, c);
163        let head = CDR_HEADER + CDR_SEQ;
164        assert_eq!(r.names, head + 11 + 2 * CDR_STR);
165        assert_eq!(
166            r.set,
167            head + 11 + 2 * CDR_STR + 2 * CDR_VALUE_BASE + 16 + 4 * (CDR_STR + 16)
168        );
169        assert_eq!(r.max(), r.set);
170        assert_eq!(worst_request_slot(&[shape], c), r.set.next_multiple_of(4));
171    }
172
173    #[test]
174    fn capacities_are_clamped_to_the_wire() {
175        let c = RequestCaps::clamped(10_000, 10_000, 3, 10_000);
176        assert_eq!(
177            c,
178            RequestCaps {
179                string: 256,
180                array: 64,
181                string_array: 3,
182                byte_array: 64
183            }
184        );
185    }
186}