oxictl 0.1.1

Pure Rust Real-Time Control Systems Framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! SEDP builtin topic data: publications and subscriptions.
//!
//! `PublicationBuiltinTopicData` and `SubscriptionBuiltinTopicData` carry the endpoint
//! metadata exchanged over SEDP.  Both encode/decode as CDR PL_CDR_LE ParameterList
//! payloads, identical to SPDP participant data.

use heapless::{String as HString, Vec as HVec};

use crate::protocol::dds::byte_cursor::{ByteCursor, ByteWriter, Endianness};
use crate::protocol::dds::error::RtpsError;
use crate::protocol::dds::types::guid::{Guid, GUID_UNKNOWN};
use crate::protocol::dds::types::locator::Locator;
use crate::protocol::dds::types::parameter::{
    ParameterList, PID_DEADLINE, PID_DURABILITY, PID_ENDPOINT_GUID, PID_HISTORY, PID_LIVELINESS,
    PID_MULTICAST_LOCATOR, PID_RELIABILITY, PID_SENTINEL, PID_TOPIC_NAME, PID_TYPE_NAME,
    PID_UNICAST_LOCATOR,
};

use super::qos::{
    DeadlineQosPolicy, DurabilityQosPolicy, HistoryQosPolicy, LivelinessQosPolicy,
    ReliabilityQosPolicy,
};

// ─── PublicationBuiltinTopicData ─────────────────────────────────────────────

/// SEDP builtin topic data for a publication (writer) endpoint.
///
/// Serialized as a CDR PL_CDR_LE ParameterList in DATA submessage payloads
/// on the builtin publications topic.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PublicationBuiltinTopicData {
    pub endpoint_guid: Guid,
    pub topic_name: HString<256>,
    pub type_name: HString<256>,
    pub unicast_locators: HVec<Locator, 4>,
    pub multicast_locators: HVec<Locator, 4>,
    pub reliability: ReliabilityQosPolicy,
    pub history: HistoryQosPolicy,
    pub durability: DurabilityQosPolicy,
    pub liveliness: LivelinessQosPolicy,
    pub deadline: DeadlineQosPolicy,
}

impl Default for PublicationBuiltinTopicData {
    fn default() -> Self {
        Self {
            endpoint_guid: GUID_UNKNOWN,
            topic_name: HString::new(),
            type_name: HString::new(),
            unicast_locators: HVec::new(),
            multicast_locators: HVec::new(),
            reliability: ReliabilityQosPolicy::default(),
            history: HistoryQosPolicy::default(),
            durability: DurabilityQosPolicy::default(),
            liveliness: LivelinessQosPolicy::default(),
            deadline: DeadlineQosPolicy::default(),
        }
    }
}

impl PublicationBuiltinTopicData {
    /// Serialize this data into a CDR PL_CDR_LE payload.
    ///
    /// Writes a 4-byte CDR encapsulation header `[0x00, 0x03, 0x00, 0x00]` (PL_CDR_LE)
    /// followed by the ParameterList entries and a PID_SENTINEL terminator.
    ///
    /// Returns the total number of bytes written into `buf`.
    pub fn serialize_to_payload(&self, buf: &mut [u8]) -> Result<usize, RtpsError> {
        if buf.len() < 4 {
            return Err(RtpsError::BufferTooSmall);
        }
        buf[0] = 0x00;
        buf[1] = 0x03;
        buf[2] = 0x00;
        buf[3] = 0x00;

        let mut w = ByteWriter::new(&mut buf[4..], Endianness::Little);

        write_param_guid(&mut w, PID_ENDPOINT_GUID, &self.endpoint_guid)?;
        write_param_string(&mut w, PID_TOPIC_NAME, self.topic_name.as_str())?;
        write_param_string(&mut w, PID_TYPE_NAME, self.type_name.as_str())?;

        for loc in &self.unicast_locators {
            write_param_locator(&mut w, PID_UNICAST_LOCATOR, loc)?;
        }
        for loc in &self.multicast_locators {
            write_param_locator(&mut w, PID_MULTICAST_LOCATOR, loc)?;
        }

        // Reliability (12 bytes)
        let mut rbuf = [0u8; 12];
        {
            let mut rw = ByteWriter::new(&mut rbuf, Endianness::Little);
            self.reliability.serialize(&mut rw)?;
        }
        write_param_raw(&mut w, PID_RELIABILITY, &rbuf)?;

        // History (8 bytes)
        let mut hbuf = [0u8; 8];
        {
            let mut hw = ByteWriter::new(&mut hbuf, Endianness::Little);
            self.history.serialize(&mut hw)?;
        }
        write_param_raw(&mut w, PID_HISTORY, &hbuf)?;

        // Durability (4 bytes)
        let mut dbuf = [0u8; 4];
        {
            let mut dw = ByteWriter::new(&mut dbuf, Endianness::Little);
            self.durability.serialize(&mut dw)?;
        }
        write_param_raw(&mut w, PID_DURABILITY, &dbuf)?;

        // Liveliness (12 bytes)
        let mut lbuf = [0u8; 12];
        {
            let mut lw = ByteWriter::new(&mut lbuf, Endianness::Little);
            self.liveliness.serialize(&mut lw)?;
        }
        write_param_raw(&mut w, PID_LIVELINESS, &lbuf)?;

        // Deadline (8 bytes)
        let mut dlbuf = [0u8; 8];
        {
            let mut dlw = ByteWriter::new(&mut dlbuf, Endianness::Little);
            self.deadline.serialize(&mut dlw)?;
        }
        write_param_raw(&mut w, PID_DEADLINE, &dlbuf)?;

        // PID_SENTINEL
        w.write_u16(PID_SENTINEL)?;
        w.write_u16(0)?;

        Ok(4 + w.position())
    }

    /// Parse publication endpoint data from a CDR PL_CDR_LE or PL_CDR_BE payload.
    ///
    /// Detects endianness from the CDR encapsulation header. Unknown PIDs are silently
    /// skipped for forward compatibility.
    pub fn parse_from_payload(payload: &[u8]) -> Result<Self, RtpsError> {
        if payload.len() < 4 {
            return Err(RtpsError::TruncatedHeader);
        }
        let endianness = if payload[1] == 0x02 {
            Endianness::Big
        } else {
            Endianness::Little
        };

        let param_bytes = &payload[4..];
        let mut cur = ByteCursor::new(param_bytes, endianness);
        let list = ParameterList::parse(&mut cur)?;

        let mut data = Self::default();
        for param in list.iter() {
            match param.pid {
                PID_ENDPOINT_GUID if param.value.len() >= 16 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.endpoint_guid = Guid::parse(&mut vc)?;
                }
                PID_TOPIC_NAME => {
                    data.topic_name = read_param_cdr_string::<256>(param.value, endianness)?;
                }
                PID_TYPE_NAME => {
                    data.type_name = read_param_cdr_string::<256>(param.value, endianness)?;
                }
                PID_UNICAST_LOCATOR if param.value.len() >= 24 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    let loc = Locator::parse(&mut vc)?;
                    let _ = data.unicast_locators.push(loc);
                }
                PID_MULTICAST_LOCATOR if param.value.len() >= 24 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    let loc = Locator::parse(&mut vc)?;
                    let _ = data.multicast_locators.push(loc);
                }
                PID_RELIABILITY if param.value.len() >= 12 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.reliability = ReliabilityQosPolicy::parse(&mut vc)?;
                }
                PID_HISTORY if param.value.len() >= 8 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.history = HistoryQosPolicy::parse(&mut vc)?;
                }
                PID_DURABILITY if param.value.len() >= 4 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.durability = DurabilityQosPolicy::parse(&mut vc)?;
                }
                PID_LIVELINESS if param.value.len() >= 12 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.liveliness = LivelinessQosPolicy::parse(&mut vc)?;
                }
                PID_DEADLINE if param.value.len() >= 8 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.deadline = DeadlineQosPolicy::parse(&mut vc)?;
                }
                _ => {} // unknown PIDs silently skipped
            }
        }
        Ok(data)
    }
}

// ─── SubscriptionBuiltinTopicData ────────────────────────────────────────────

/// SEDP builtin topic data for a subscription (reader) endpoint.
///
/// Serialized as a CDR PL_CDR_LE ParameterList in DATA submessage payloads
/// on the builtin subscriptions topic.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SubscriptionBuiltinTopicData {
    pub endpoint_guid: Guid,
    pub topic_name: HString<256>,
    pub type_name: HString<256>,
    pub unicast_locators: HVec<Locator, 4>,
    pub multicast_locators: HVec<Locator, 4>,
    pub reliability: ReliabilityQosPolicy,
    pub history: HistoryQosPolicy,
    pub durability: DurabilityQosPolicy,
    pub liveliness: LivelinessQosPolicy,
    pub deadline: DeadlineQosPolicy,
}

impl Default for SubscriptionBuiltinTopicData {
    fn default() -> Self {
        Self {
            endpoint_guid: GUID_UNKNOWN,
            topic_name: HString::new(),
            type_name: HString::new(),
            unicast_locators: HVec::new(),
            multicast_locators: HVec::new(),
            reliability: ReliabilityQosPolicy::default(),
            history: HistoryQosPolicy::default(),
            durability: DurabilityQosPolicy::default(),
            liveliness: LivelinessQosPolicy::default(),
            deadline: DeadlineQosPolicy::default(),
        }
    }
}

impl SubscriptionBuiltinTopicData {
    /// Serialize this data into a CDR PL_CDR_LE payload.
    pub fn serialize_to_payload(&self, buf: &mut [u8]) -> Result<usize, RtpsError> {
        if buf.len() < 4 {
            return Err(RtpsError::BufferTooSmall);
        }
        buf[0] = 0x00;
        buf[1] = 0x03;
        buf[2] = 0x00;
        buf[3] = 0x00;

        let mut w = ByteWriter::new(&mut buf[4..], Endianness::Little);

        write_param_guid(&mut w, PID_ENDPOINT_GUID, &self.endpoint_guid)?;
        write_param_string(&mut w, PID_TOPIC_NAME, self.topic_name.as_str())?;
        write_param_string(&mut w, PID_TYPE_NAME, self.type_name.as_str())?;

        for loc in &self.unicast_locators {
            write_param_locator(&mut w, PID_UNICAST_LOCATOR, loc)?;
        }
        for loc in &self.multicast_locators {
            write_param_locator(&mut w, PID_MULTICAST_LOCATOR, loc)?;
        }

        let mut rbuf = [0u8; 12];
        {
            let mut rw = ByteWriter::new(&mut rbuf, Endianness::Little);
            self.reliability.serialize(&mut rw)?;
        }
        write_param_raw(&mut w, PID_RELIABILITY, &rbuf)?;

        let mut hbuf = [0u8; 8];
        {
            let mut hw = ByteWriter::new(&mut hbuf, Endianness::Little);
            self.history.serialize(&mut hw)?;
        }
        write_param_raw(&mut w, PID_HISTORY, &hbuf)?;

        let mut dbuf = [0u8; 4];
        {
            let mut dw = ByteWriter::new(&mut dbuf, Endianness::Little);
            self.durability.serialize(&mut dw)?;
        }
        write_param_raw(&mut w, PID_DURABILITY, &dbuf)?;

        let mut lbuf = [0u8; 12];
        {
            let mut lw = ByteWriter::new(&mut lbuf, Endianness::Little);
            self.liveliness.serialize(&mut lw)?;
        }
        write_param_raw(&mut w, PID_LIVELINESS, &lbuf)?;

        let mut dlbuf = [0u8; 8];
        {
            let mut dlw = ByteWriter::new(&mut dlbuf, Endianness::Little);
            self.deadline.serialize(&mut dlw)?;
        }
        write_param_raw(&mut w, PID_DEADLINE, &dlbuf)?;

        w.write_u16(PID_SENTINEL)?;
        w.write_u16(0)?;

        Ok(4 + w.position())
    }

    /// Parse subscription endpoint data from a CDR PL_CDR_LE or PL_CDR_BE payload.
    pub fn parse_from_payload(payload: &[u8]) -> Result<Self, RtpsError> {
        if payload.len() < 4 {
            return Err(RtpsError::TruncatedHeader);
        }
        let endianness = if payload[1] == 0x02 {
            Endianness::Big
        } else {
            Endianness::Little
        };

        let param_bytes = &payload[4..];
        let mut cur = ByteCursor::new(param_bytes, endianness);
        let list = ParameterList::parse(&mut cur)?;

        let mut data = Self::default();
        for param in list.iter() {
            match param.pid {
                PID_ENDPOINT_GUID if param.value.len() >= 16 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.endpoint_guid = Guid::parse(&mut vc)?;
                }
                PID_TOPIC_NAME => {
                    data.topic_name = read_param_cdr_string::<256>(param.value, endianness)?;
                }
                PID_TYPE_NAME => {
                    data.type_name = read_param_cdr_string::<256>(param.value, endianness)?;
                }
                PID_UNICAST_LOCATOR if param.value.len() >= 24 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    let loc = Locator::parse(&mut vc)?;
                    let _ = data.unicast_locators.push(loc);
                }
                PID_MULTICAST_LOCATOR if param.value.len() >= 24 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    let loc = Locator::parse(&mut vc)?;
                    let _ = data.multicast_locators.push(loc);
                }
                PID_RELIABILITY if param.value.len() >= 12 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.reliability = ReliabilityQosPolicy::parse(&mut vc)?;
                }
                PID_HISTORY if param.value.len() >= 8 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.history = HistoryQosPolicy::parse(&mut vc)?;
                }
                PID_DURABILITY if param.value.len() >= 4 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.durability = DurabilityQosPolicy::parse(&mut vc)?;
                }
                PID_LIVELINESS if param.value.len() >= 12 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.liveliness = LivelinessQosPolicy::parse(&mut vc)?;
                }
                PID_DEADLINE if param.value.len() >= 8 => {
                    let mut vc = ByteCursor::new(param.value, endianness);
                    data.deadline = DeadlineQosPolicy::parse(&mut vc)?;
                }
                _ => {}
            }
        }
        Ok(data)
    }
}

// ─── Private serialization helpers ───────────────────────────────────────────

/// Write one parameter entry: pid(u16 LE) + aligned_length(u16 LE) + value_bytes + zero padding.
fn write_param_raw(w: &mut ByteWriter<'_>, pid: u16, value_bytes: &[u8]) -> Result<(), RtpsError> {
    let aligned_len = (value_bytes.len() + 3) & !3;
    w.write_u16(pid)?;
    w.write_u16(aligned_len as u16)?;
    w.write_bytes(value_bytes)?;
    let pad = aligned_len - value_bytes.len();
    if pad > 0 {
        let zeros = [0u8; 3];
        w.write_bytes(&zeros[..pad])?;
    }
    Ok(())
}

/// Write a CDR string parameter entry.
///
/// The CDR string value = `[u32 length (includes null)][bytes][null][zero padding to 4-byte boundary]`.
/// Total parameter value length = `4 + aligned(len(s)+1, 4)`.
fn write_param_string(w: &mut ByteWriter<'_>, pid: u16, s: &str) -> Result<(), RtpsError> {
    let with_null = s.len() + 1; // content + null terminator
    let aligned_str = (with_null + 3) & !3; // aligned byte count for the string part
    let value_len = 4 + aligned_str; // u32 length prefix + aligned string content

    w.write_u16(pid)?;
    w.write_u16(value_len as u16)?;
    w.write_cdr_string(s)
}

/// Write a 16-byte GUID as a raw parameter.
fn write_param_guid(w: &mut ByteWriter<'_>, pid: u16, guid: &Guid) -> Result<(), RtpsError> {
    let mut buf = [0u8; 16];
    {
        let mut gw = ByteWriter::new(&mut buf, Endianness::Little);
        guid.serialize(&mut gw)?;
    }
    write_param_raw(w, pid, &buf)
}

/// Write a 24-byte Locator as a raw parameter.
fn write_param_locator(w: &mut ByteWriter<'_>, pid: u16, loc: &Locator) -> Result<(), RtpsError> {
    let mut buf = [0u8; 24];
    {
        let mut lw = ByteWriter::new(&mut buf, Endianness::Little);
        loc.serialize(&mut lw)?;
    }
    write_param_raw(w, pid, &buf)
}

// ─── Private parse helpers ────────────────────────────────────────────────────

/// Parse a CDR string from a raw parameter value slice.
///
/// Format: `[u32 length (includes null)][string bytes][null terminator][zero padding]`.
/// The returned string has the null terminator trimmed.
fn read_param_cdr_string<const N: usize>(
    value: &[u8],
    endianness: Endianness,
) -> Result<HString<N>, RtpsError> {
    let mut cur = ByteCursor::new(value, endianness);
    let s = cur.read_cdr_string()?;
    let mut result = HString::<N>::new();
    result.push_str(s).map_err(|_| RtpsError::BufferTooSmall)?;
    Ok(result)
}