someip_sd_wire/
field.rs

1//! Field offset definitions for SOME/IP-SD wire format structures.
2//!
3//! This module defines all byte offset ranges used to parse and construct SOME/IP-SD packets,
4//! entries, and options. Following the smoltcp pattern, all offsets are defined as const
5//! ranges or const functions to enable compile-time optimization.
6//!
7//! # Wire Format Structure
8//!
9//! SOME/IP-SD Packet:
10//! ```text
11//! +----------------+----------------+
12//! | FLAGS (1 byte) | RESERVED (3)   |
13//! +----------------+----------------+
14//! | ENTRIES_LENGTH (4 bytes)        |
15//! +---------------------------------+
16//! | ENTRIES_ARRAY (variable)        |
17//! +---------------------------------+
18//! | OPTIONS_LENGTH (4 bytes)        |
19//! +---------------------------------+
20//! | OPTIONS_ARRAY (variable)        |
21//! +---------------------------------+
22//! ```
23
24#![allow(non_snake_case)]
25#![allow(dead_code)]
26
27/// Type alias for a byte range (slice index range).
28pub type Field = ::core::ops::Range<usize>;
29
30/// SOME/IP-SD packet header field offsets.
31pub mod header {
32    use crate::field::Field;
33
34    /// Flags field (1 byte at offset 0).
35    ///
36    /// Contains flags for the SOME/IP-SD message (e.g., reboot, unicast flags).
37    pub const FLAGS: Field = 0..1;
38    
39    /// Reserved field (3 bytes at offset 1-3).
40    ///
41    /// Must be set to 0x000000 per SOME/IP-SD specification.
42    pub const RESERVED: Field = 1..4;
43}
44
45/// SOME/IP-SD entries and options array field offsets.
46pub mod entries {
47    use crate::field::Field;
48
49    /// Length of entries array field (4 bytes at offset 4-7).
50    ///
51    /// Specifies the length in bytes of the entries array that follows.
52    pub const LENGTH: Field = 4..8;
53    
54    /// Entries array field (variable length starting at offset 8).
55    ///
56    /// # Parameters
57    ///
58    /// * `length` - The length of the entries array in bytes (from ENTRIES_LENGTH field)
59    ///
60    /// # Returns
61    ///
62    /// Field range covering the entries array
63    pub const fn ENTRIES_ARRAY(length: usize) -> Field {
64        8..(8 + length)
65    }
66
67    /// Minimum SOME/IP-SD header length in bytes.
68    ///
69    /// This is the minimum size needed to read up to and including the ENTRIES_LENGTH field.
70    /// Value is 8 bytes (FLAGS + RESERVED + ENTRIES_LENGTH).
71    pub const MIN_HEADER_LEN: usize = LENGTH.end;
72    
73    /// Options length field (4 bytes after entries array).
74    ///
75    /// # Parameters
76    ///
77    /// * `entries_len` - The length of the entries array in bytes
78    ///
79    /// # Returns
80    ///
81    /// Field range for the 4-byte options length field
82    pub const fn OPTIONS_LENGTH(entries_len: usize) -> Field {
83        let start = ENTRIES_ARRAY(entries_len).end;
84        start..(start + 4)
85    }
86    
87    /// Options array field (variable length after options length field).
88    ///
89    /// # Parameters
90    ///
91    /// * `entries_len` - The length of the entries array in bytes
92    /// * `options_len` - The length of the options array in bytes (from OPTIONS_LENGTH field)
93    ///
94    /// # Returns
95    ///
96    /// Field range covering the options array
97    pub const fn OPTIONS_ARRAY(entries_len: usize, options_len: usize) -> Field {
98        let start = OPTIONS_LENGTH(entries_len).end;
99        start..(start + options_len)
100    }
101}
102
103/// Option-specific field offsets (relative within an option structure).
104pub mod options {
105    use crate::field::Field;
106
107    /// Length field within an option (2 bytes at offset 0-1, relative).
108    pub const LENGTH: Field = 0..4;
109    
110    /// Options array field (variable length, relative offset).
111    ///
112    /// # Parameters
113    ///
114    /// * `length` - The length of the options array
115    ///
116    /// # Returns
117    ///
118    /// Field range for options data
119    pub const fn OPTIONS_ARRAY(length: usize) -> Field {
120        4..(4 + length)
121    }
122}
123
124/// Service Entry field offsets (16 bytes total).
125///
126/// Service entries are used for FindService and OfferService messages.
127pub mod service_entry {
128    use crate::field::Field;
129
130    /// Entry type field (1 byte at offset 0).
131    ///
132    /// Values: 0x00 = FindService, 0x01 = OfferService
133    pub const TYPE: Field = 0..1;
134    
135    /// Index of first option run (1 byte at offset 1).
136    pub const INDEX_FIRST_OPTION_RUN: Field = 1..2;
137    
138    /// Index of second option run (1 byte at offset 2).
139    pub const INDEX_SECOND_OPTION_RUN: Field = 2..3;
140    
141    /// Number of options in both runs, 4-bit packed (1 byte at offset 3).
142    pub const NUMBER_OF_OPTIONS: Field = 3..4;
143    
144    /// Service ID (2 bytes at offset 4-5).
145    pub const SERVICE_ID: Field = 4..6;
146    
147    /// Instance ID (2 bytes at offset 6-7).
148    pub const INSTANCE_ID: Field = 6..8;
149    
150    /// Major version (1 byte at offset 8).
151    pub const MAJOR_VERSION: Field = 8..9;
152    
153    /// Time-To-Live in seconds (3 bytes at offset 9-11).
154    ///
155    /// Value 0xFFFFFF = infinite, 0x000000 = stop offer
156    pub const TTL: Field = 9..12;
157    
158    /// Minor version (4 bytes at offset 12-15).
159    pub const MINOR_VERSION: Field = 12..16;
160}
161
162/// EventGroup Entry field offsets (16 bytes total).
163///
164/// EventGroup entries are used for Subscribe and SubscribeAck messages.
165pub mod event_group_entry {
166    use crate::field::Field;
167
168    /// Entry type field (1 byte at offset 0).
169    ///
170    /// Values: 0x06 = Subscribe, 0x07 = SubscribeAck
171    pub const TYPE: Field = 0..1;
172    
173    /// Index of first option run (1 byte at offset 1).
174    pub const INDEX_FIRST_OPTION_RUN: Field = 1..2;
175    
176    /// Index of second option run (1 byte at offset 2).
177    pub const INDEX_SECOND_OPTION_RUN: Field = 2..3;
178    
179    /// Number of options in both runs, 4-bit packed (1 byte at offset 3).
180    pub const NUMBER_OF_OPTIONS: Field = 3..4;
181    
182    /// Service ID (2 bytes at offset 4-5).
183    pub const SERVICE_ID: Field = 4..6;
184    
185    /// Instance ID (2 bytes at offset 6-7).
186    pub const INSTANCE_ID: Field = 6..8;
187    
188    /// Major version (1 byte at offset 8).
189    pub const MAJOR_VERSION: Field = 8..9;
190    
191    /// Time-To-Live in seconds (3 bytes at offset 9-11).
192    ///
193    /// Value 0xFFFFFF = infinite, 0x000000 = stop subscribe
194    pub const TTL: Field = 9..12;
195    
196    /// Reserved (12-bit) and Counter (4-bit) packed field (2 bytes at offset 12-13).
197    pub const RESERVED_AND_COUNTER: Field = 12..14;
198    
199    /// EventGroup ID (2 bytes at offset 14-15).
200    pub const EVENTGROUP_ID: Field = 14..16;
201}
202
203/// Option header field offsets (4 bytes total).
204///
205/// All SOME/IP-SD options start with this common header.
206pub mod option_header {
207    use crate::field::Field;
208
209    /// Length of option data excluding header (2 bytes at offset 0-1).
210    pub const LENGTH: Field = 0..2;
211    
212    /// Option type field (1 byte at offset 2).
213    ///
214    /// Values: 0x01=Configuration, 0x02=LoadBalancing, 0x04=IPv4Endpoint, etc.
215    pub const TYPE: Field = 2..3;
216    
217    /// Discardable flag (1-bit) and reserved (7-bit) packed (1 byte at offset 3).
218    pub const DISCARDABLE_FLAG_AND_RESERVED: Field = 3..4;
219}
220
221/// Configuration Option field offsets (variable length).
222///
223/// Configuration options carry DNS-SD TXT record style key-value pairs.
224pub mod configuration_option {
225    use crate::field::Field;
226
227    /// Configuration string field (variable length after 3-byte header).
228    ///
229    /// # Parameters
230    ///
231    /// * `length` - Length of the configuration string in bytes
232    ///
233    /// # Returns
234    ///
235    /// Field range for the configuration data
236    pub const fn CONFIGURATION_STRING(length: usize) -> Field {
237        3..(3 + length)
238    }
239}
240
241/// Load Balancing Option field offsets (4 bytes data after header).
242pub mod load_balancing_option {
243    use crate::field::Field;
244
245    /// Priority field (2 bytes at offset 0-1).
246    pub const PRIORITY: Field = 0..2;
247    
248    /// Weight field (2 bytes at offset 2-3).
249    pub const WEIGHT: Field = 2..4;
250}
251
252/// IPv4 Endpoint Option field offsets (8 bytes data after header).
253pub mod ipv4_endpoint_option {
254    use crate::field::Field;
255
256    /// IPv4 address (4 bytes at offset 0-3).
257    pub const IPV4_ADDRESS: Field = 0..4;
258    
259    /// Reserved byte (1 byte at offset 4).
260    pub const RESERVED: Field = 4..5;
261    
262    /// Transport protocol (1 byte at offset 5).
263    ///
264    /// Values: 0x06 = TCP, 0x11 = UDP
265    pub const TRANSPORT_PROTOCOL: Field = 5..6;
266    
267    /// Port number (2 bytes at offset 6-7).
268    pub const PORT: Field = 6..8;
269}
270
271/// IPv6 Endpoint Option field offsets (20 bytes data after header).
272pub mod ipv6_endpoint_option {
273    use crate::field::Field;
274
275    /// IPv6 address (16 bytes at offset 0-15).
276    pub const IPV6_ADDRESS: Field = 0..16;
277    
278    /// Reserved byte (1 byte at offset 16).
279    pub const RESERVED: Field = 16..17;
280    
281    /// Transport protocol (1 byte at offset 17).
282    ///
283    /// Values: 0x06 = TCP, 0x11 = UDP
284    pub const TRANSPORT_PROTOCOL: Field = 17..18;
285    
286    /// Port number (2 bytes at offset 18-19).
287    pub const PORT: Field = 18..20;
288}
289
290/// IPv4 Multicast Option field offsets (8 bytes data after header).
291pub mod ipv4_multicast_option {
292    use crate::field::Field;
293
294    /// IPv4 multicast address (4 bytes at offset 0-3).
295    pub const IPV4_MULTICAST_ADDRESS: Field = 0..4;
296    
297    /// Reserved byte (1 byte at offset 4).
298    pub const RESERVED: Field = 4..5;
299    
300    /// Transport protocol (1 byte at offset 5).
301    ///
302    /// Values: 0x06 = TCP, 0x11 = UDP
303    pub const TRANSPORT_PROTOCOL: Field = 5..6;
304    
305    /// Port number (2 bytes at offset 6-7).
306    pub const PORT: Field = 6..8;
307}
308
309/// IPv6 Multicast Option field offsets (20 bytes data after header).
310pub mod ipv6_multicast_option {
311    use crate::field::Field;
312
313    /// IPv6 multicast address (16 bytes at offset 0-15).
314    pub const IPV6_MULTICAST_ADDRESS: Field = 0..16;
315    
316    /// Reserved byte (1 byte at offset 16).
317    pub const RESERVED: Field = 16..17;
318    
319    /// Transport protocol (1 byte at offset 17).
320    ///
321    /// Values: 0x06 = TCP, 0x11 = UDP
322    pub const TRANSPORT_PROTOCOL: Field = 17..18;
323    
324    /// Port number (2 bytes at offset 18-19).
325    pub const PORT: Field = 18..20;
326}
327
328/// IPv4 SD Endpoint Option field offsets (8 bytes data after header).
329pub mod ipv4_sd_endpoint_option {
330    use crate::field::Field;
331
332    /// IPv4 SD endpoint address (4 bytes at offset 0-3).
333    pub const IPV4_SD_ENDPOINT_ADDRESS: Field = 0..4;
334    
335    /// Reserved byte (1 byte at offset 4).
336    pub const RESERVED: Field = 4..5;
337    
338    /// Transport protocol (1 byte at offset 5).
339    ///
340    /// Values: 0x06 = TCP, 0x11 = UDP
341    pub const TRANSPORT_PROTOCOL: Field = 5..6;
342    
343    /// Port number (2 bytes at offset 6-7).
344    pub const PORT: Field = 6..8;
345}
346
347/// IPv6 SD Endpoint Option field offsets (20 bytes data after header).
348pub mod ipv6_sd_endpoint_option {
349    use crate::field::Field;
350
351    /// IPv6 SD endpoint address (16 bytes at offset 0-15).
352    pub const IPV6_SD_ENDPOINT_ADDRESS: Field = 0..16;
353    
354    /// Reserved byte (1 byte at offset 16).
355    pub const RESERVED: Field = 16..17;
356    
357    /// Transport protocol (1 byte at offset 17).
358    ///
359    /// Values: 0x06 = TCP, 0x11 = UDP
360    pub const TRANSPORT_PROTOCOL: Field = 17..18;
361    
362    /// Port number (2 bytes at offset 18-19).
363    pub const PORT: Field = 18..20;
364}