cyclonedds 1.8.0

Safe Rust wrapper for Eclipse CycloneDDS
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! CDR serialization and deserialization for DDS types.
//!
//! This module provides:
//! - [`CdrSerializer`] / [`CdrDeserializer`] for serializing and deserializing
//!   DDS samples to/from CDR byte streams using the CycloneDDS CDR stream API.
//! - [`CdrSample`] for raw CDR data received via `read_cdr` / `take_cdr`.
//! - [`CdrEncoding`] for selecting XCDR version.

use crate::{DdsError, DdsResult, DdsType};
use cyclonedds_rust_sys::*;
use std::ffi::c_void;
use std::marker::PhantomData;

// ---------------------------------------------------------------------------
// CdrEncoding
// ---------------------------------------------------------------------------

/// CDR encoding version.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CdrEncoding {
    /// XCDR1 (CDR version 1, used for plain IDL types).
    #[default]
    Xcdr1,
    /// XCDR2 (CDR version 2, required for appendable/mutable types).
    Xcdr2,
}

impl CdrEncoding {
    fn as_xcdr_version(self) -> u32 {
        match self {
            Self::Xcdr1 => 1,
            Self::Xcdr2 => 2,
        }
    }
}

// ---------------------------------------------------------------------------
// CdrSample  (used by read_cdr / take_cdr)
// ---------------------------------------------------------------------------

/// A raw CDR sample obtained from a `DataReader` via `read_cdr` / `take_cdr`.
///
/// Contains the serialized CDR bytes and the associated sample info.
pub struct CdrSample {
    /// Raw CDR bytes (including the encoding header).
    pub data: Vec<u8>,
    /// Sample metadata provided by DDS.
    pub info: dds_sample_info_t,
}

// ---------------------------------------------------------------------------
// CdrSerializer
// ---------------------------------------------------------------------------

/// Serializer that writes a `DdsType` sample into a CDR byte stream.
///
/// Internally uses the CycloneDDS CDR stream writer (`dds_stream_write_sample`).
pub struct CdrSerializer<'a, T: DdsType> {
    _marker: PhantomData<&'a T>,
}

impl<'a, T: DdsType> CdrSerializer<'a, T> {
    /// Serialize `sample` to CDR bytes using the given encoding.
    ///
    /// Builds a temporary `dds_cdrstream_desc` from the type's ops/keys/flagset,
    /// writes the sample into an output stream, and returns the resulting bytes.
    pub fn serialize(sample: &T, encoding: CdrEncoding) -> DdsResult<Vec<u8>> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut os: dds_ostream_t = std::mem::zeroed();
            dds_ostream_init(
                &mut os,
                &dds_cdrstream_default_allocator,
                0,
                encoding.as_xcdr_version(),
            );

            let mut arena = crate::write_arena::WriteArena::new();
            let data_ptr = sample.write_to_native(&mut arena)?;

            let ok = dds_stream_write_sample(
                &mut os,
                &dds_cdrstream_default_allocator,
                data_ptr,
                desc.as_ptr(),
            );

            let bytes = if ok {
                let len = os.m_index as usize;
                let mut buf = vec![0u8; len];
                std::ptr::copy_nonoverlapping(os.m_buffer, buf.as_mut_ptr(), len);
                Ok(buf)
            } else {
                Err(DdsError::Unsupported("CDR serialization failed".into()))
            };

            dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
            bytes
        }
    }

    /// Serialize only the key fields of `sample`.
    pub fn serialize_key(sample: &T, encoding: CdrEncoding) -> DdsResult<Vec<u8>> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut os: dds_ostream_t = std::mem::zeroed();
            dds_ostream_init(
                &mut os,
                &dds_cdrstream_default_allocator,
                0,
                encoding.as_xcdr_version(),
            );

            let mut arena = crate::write_arena::WriteArena::new();
            let data_ptr = sample.write_to_native(&mut arena)?;

            dds_stream_write_key(
                &mut os,
                cyclonedds_rust_sys::dds_cdr_key_serialization_kind_DDS_CDR_KEY_SERIALIZATION_SAMPLE,
                &dds_cdrstream_default_allocator,
                data_ptr as *const _,
                desc.as_ptr(),
            );

            let len = os.m_index as usize;
            let mut buf = vec![0u8; len];
            std::ptr::copy_nonoverlapping(os.m_buffer, buf.as_mut_ptr(), len);

            dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
            Ok(buf)
        }
    }

    /// Serialize `sample` into a pre-allocated buffer, returning the number
    /// of bytes written.
    ///
    /// This avoids the intermediate allocation performed by `serialize`.
    /// If `buf` is too small, an error is returned.
    pub fn serialize_to_buffer(
        sample: &T,
        buf: &mut [u8],
        encoding: CdrEncoding,
    ) -> DdsResult<usize> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut os: dds_ostream_t = std::mem::zeroed();
            dds_ostream_init(
                &mut os,
                &dds_cdrstream_default_allocator,
                0,
                encoding.as_xcdr_version(),
            );

            let mut arena = crate::write_arena::WriteArena::new();
            let data_ptr = sample.write_to_native(&mut arena)?;

            let ok = dds_stream_write_sample(
                &mut os,
                &dds_cdrstream_default_allocator,
                data_ptr,
                desc.as_ptr(),
            );

            if !ok {
                dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
                return Err(DdsError::Unsupported("CDR serialization failed".into()));
            }

            let len = os.m_index as usize;
            if len > buf.len() {
                dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
                return Err(DdsError::BadParameter(format!(
                    "buffer too small: needed {} bytes, got {}",
                    len,
                    buf.len()
                )));
            }

            std::ptr::copy_nonoverlapping(os.m_buffer, buf.as_mut_ptr(), len);
            dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
            Ok(len)
        }
    }

    /// Serialize only the key fields into a pre-allocated buffer.
    pub fn serialize_key_to_buffer(
        sample: &T,
        buf: &mut [u8],
        encoding: CdrEncoding,
    ) -> DdsResult<usize> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut os: dds_ostream_t = std::mem::zeroed();
            dds_ostream_init(
                &mut os,
                &dds_cdrstream_default_allocator,
                0,
                encoding.as_xcdr_version(),
            );

            let mut arena = crate::write_arena::WriteArena::new();
            let data_ptr = sample.write_to_native(&mut arena)?;

            dds_stream_write_key(
                &mut os,
                cyclonedds_rust_sys::dds_cdr_key_serialization_kind_DDS_CDR_KEY_SERIALIZATION_SAMPLE,
                &dds_cdrstream_default_allocator,
                data_ptr as *const _,
                desc.as_ptr(),
            );

            let len = os.m_index as usize;
            if len > buf.len() {
                dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
                return Err(DdsError::BadParameter(format!(
                    "buffer too small: needed {} bytes, got {}",
                    len,
                    buf.len()
                )));
            }

            std::ptr::copy_nonoverlapping(os.m_buffer, buf.as_mut_ptr(), len);
            dds_ostream_fini(&mut os, &dds_cdrstream_default_allocator);
            Ok(len)
        }
    }
}

// ---------------------------------------------------------------------------
// CdrDeserializer
// ---------------------------------------------------------------------------

/// Deserializer that reads a `DdsType` sample from CDR bytes.
///
/// Internally uses the CycloneDDS CDR stream reader (`dds_stream_read_sample`).
pub struct CdrDeserializer<T: DdsType> {
    _marker: PhantomData<T>,
}

impl<T: DdsType> CdrDeserializer<T> {
    /// Deserialize a sample from CDR bytes using the given encoding.
    ///
    /// Allocates a zero-initialized buffer of the type's size, reads the CDR
    /// stream into it, then uses `clone_out` to produce an owned value.
    pub fn deserialize(data: &[u8], encoding: CdrEncoding) -> DdsResult<T> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut is: dds_istream_t = std::mem::zeroed();
            dds_istream_init(
                &mut is,
                data.len() as u32,
                data.as_ptr() as *const c_void,
                encoding.as_xcdr_version(),
            );

            let size = T::descriptor_size() as usize;
            let align = std::cmp::max(T::descriptor_align() as usize, 1);
            let layout = std::alloc::Layout::from_size_align(size, align)
                .map_err(|_| DdsError::BadParameter("invalid type layout".into()))?;
            let buf = std::alloc::alloc_zeroed(layout);
            if buf.is_null() {
                return Err(DdsError::OutOfMemory);
            }

            dds_stream_read_sample(
                &mut is,
                buf as *mut c_void,
                &dds_cdrstream_default_allocator,
                desc.as_ptr(),
            );

            let result = T::clone_out(buf as *const T);

            // Free any dynamically-allocated members that the read may have created.
            dds_stream_free_sample(
                buf as *mut c_void,
                &dds_cdrstream_default_allocator,
                desc.ops_ptr(),
            );
            std::alloc::dealloc(buf, layout);

            dds_istream_fini(&mut is);
            Ok(result)
        }
    }

    /// Deserialize only the key portion from CDR bytes.
    pub fn deserialize_key(data: &[u8], encoding: CdrEncoding) -> DdsResult<T> {
        let desc = CdrStreamDesc::new::<T>()?;

        unsafe {
            let mut is: dds_istream_t = std::mem::zeroed();
            dds_istream_init(
                &mut is,
                data.len() as u32,
                data.as_ptr() as *const c_void,
                encoding.as_xcdr_version(),
            );

            let size = T::descriptor_size() as usize;
            let align = std::cmp::max(T::descriptor_align() as usize, 1);
            let layout = std::alloc::Layout::from_size_align(size, align)
                .map_err(|_| DdsError::BadParameter("invalid type layout".into()))?;
            let buf = std::alloc::alloc_zeroed(layout);
            if buf.is_null() {
                return Err(DdsError::OutOfMemory);
            }

            dds_stream_read_key(
                &mut is,
                buf as *mut ::std::ffi::c_char,
                &dds_cdrstream_default_allocator,
                desc.as_ptr(),
            );

            let result = T::clone_out(buf as *const T);

            dds_stream_free_sample(
                buf as *mut ::std::ffi::c_void,
                &dds_cdrstream_default_allocator,
                desc.ops_ptr(),
            );
            std::alloc::dealloc(buf, layout);

            dds_istream_fini(&mut is);
            Ok(result)
        }
    }
}

// ---------------------------------------------------------------------------
// Helper: build a dds_cdrstream_desc from DdsType ops/keys
// ---------------------------------------------------------------------------

/// RAII wrapper around `dds_cdrstream_desc` that is initialised from a type's
/// ops array and cleaned up on drop.
struct CdrStreamDesc {
    desc: dds_cdrstream_desc,
    _ops: Vec<u32>,
    _key_names: Vec<std::ffi::CString>,
    _keys: Vec<dds_key_descriptor>,
}

impl CdrStreamDesc {
    /// Build a new CDR stream descriptor for the given `DdsType`.
    fn new<T: DdsType>() -> DdsResult<Self> {
        // Build ops array including OP_RTS and KOF entries, same as Topic::with_qos
        let mut ops = T::ops();
        if ops.last().copied() != Some(crate::topic::OP_RTS) {
            ops.push(crate::topic::OP_RTS);
        }

        let key_defs = T::keys();
        let key_names: Vec<std::ffi::CString> = key_defs
            .iter()
            .map(|k| std::ffi::CString::new(k.name.as_str()).unwrap())
            .collect();
        let mut keys: Vec<dds_key_descriptor> = Vec::with_capacity(key_defs.len());
        for (i, kd) in key_defs.iter().enumerate() {
            let offset = ops.len() as u32;
            ops.push(crate::topic::OP_KOF | (kd.ops_path.len() as u32));
            ops.extend(kd.ops_path.iter().copied());
            keys.push(dds_key_descriptor {
                m_name: key_names[i].as_ptr(),
                m_offset: offset,
                m_idx: i as u32,
            });
        }

        let post_key_ops = T::post_key_ops();
        if !post_key_ops.is_empty() {
            ops.extend(post_key_ops);
        }

        unsafe {
            let mut desc: dds_cdrstream_desc = std::mem::zeroed();
            dds_cdrstream_desc_init(
                &mut desc,
                &dds_cdrstream_default_allocator,
                T::descriptor_size(),
                T::descriptor_align(),
                T::flagset(),
                ops.as_ptr(),
                if keys.is_empty() {
                    std::ptr::null()
                } else {
                    keys.as_ptr()
                },
                keys.len() as u32,
            );

            Ok(Self {
                desc,
                _ops: ops,
                _key_names: key_names,
                _keys: keys,
            })
        }
    }

    fn as_ptr(&self) -> *const dds_cdrstream_desc {
        &self.desc
    }

    /// Returns the raw ops pointer for use with functions like `dds_stream_free_sample`.
    fn ops_ptr(&self) -> *const u32 {
        self.desc.ops.ops as *const u32
    }
}

impl Drop for CdrStreamDesc {
    fn drop(&mut self) {
        unsafe {
            dds_cdrstream_desc_fini(&mut self.desc, &dds_cdrstream_default_allocator);
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // Minimal type for testing serialization round-trip.
    // Uses the DdsType derive macro if available; otherwise we hand-roll it.
    #[derive(Debug, PartialEq, Clone)]
    #[allow(dead_code)]
    struct Point {
        x: i32,
        y: i32,
    }

    impl DdsType for Point {
        fn type_name() -> &'static str {
            "test::Point"
        }
        fn ops() -> Vec<u32> {
            use crate::topic::*;
            let mut ops = Vec::new();
            ops.extend(adr(TYPE_4BY, 0)); // x at offset 0
            ops.extend(adr(TYPE_4BY, 4)); // y at offset 4
            ops
        }
        fn descriptor_size() -> u32 {
            8
        }
        fn descriptor_align() -> u32 {
            4
        }
    }

    #[test]
    fn cdr_encoding_default_is_xcdr1() {
        assert_eq!(CdrEncoding::default(), CdrEncoding::Xcdr1);
        assert_eq!(CdrEncoding::Xcdr1.as_xcdr_version(), 1);
        assert_eq!(CdrEncoding::Xcdr2.as_xcdr_version(), 2);
    }
}