rtps_parser/rtps/messages/
types.rs

1use super::overall_structure::WriteBytes;
2use std::io::Read;
3
4/// This files shall only contain the types as listed in the DDSI-RTPS Version 2.5
5/// Table 8.13 - Types used to define RTPS messages
6
7type Octet = u8;
8type Long = i32;
9type UnsignedLong = u32;
10type Short = i16;
11
12/// ProtocolId_t
13/// Enumeration used to identify the protocol.
14/// The following values are reserved by the protocol: PROTOCOL_RTPS
15#[derive(Clone, Copy, PartialEq, Eq, Debug)]
16#[allow(non_camel_case_types)]
17pub enum ProtocolId {
18    PROTOCOL_RTPS,
19}
20
21impl WriteBytes for ProtocolId {
22    fn write_bytes(&self, buf: &mut [u8]) -> usize {
23        b"RTPS".as_slice().read(buf).unwrap()
24    }
25}
26
27/// SubmessageFlag
28/// Type used to specify a Submessage flag.
29/// A Submessage flag takes a boolean value and affects the parsing of the Submessage by the receiver.
30pub type SubmessageFlag = bool;
31
32impl WriteBytes for [SubmessageFlag; 8] {
33    fn write_bytes(&self, buf: &mut [u8]) -> usize {
34        let mut flags = 0b_0000_0000_u8;
35        for (i, &item) in self.iter().enumerate() {
36            if item {
37                flags |= 0b_0000_0001 << i
38            }
39        }
40        buf[0] = flags;
41        1
42    }
43}
44
45/// SubmessageKind
46/// Enumeration used to identify the kind of Submessage.
47/// The following values are reserved by this version of the protocol:
48/// DATA, GAP, HEARTBEAT, ACKNACK, PAD, INFO_TS, INFO_REPLY, INFO_DST, INFO_SRC, DATA_FRAG, NACK_FRAG, HEARTBEAT_FRAG
49#[derive(Clone, Copy, PartialEq, Eq, Debug)]
50#[allow(non_camel_case_types)]
51#[allow(clippy::upper_case_acronyms)]
52pub enum SubmessageKind {
53    DATA,
54    GAP,
55    HEARTBEAT,
56    ACKNACK,
57    PAD,
58    INFO_TS,
59    INFO_REPLY,
60    INFO_DST,
61    INFO_SRC,
62    DATA_FRAG,
63    NACK_FRAG,
64    HEARTBEAT_FRAG,
65}
66
67pub const DATA: u8 = 0x15;
68pub const GAP: u8 = 0x08;
69pub const HEARTBEAT: u8 = 0x07;
70pub const ACKNACK: u8 = 0x06;
71pub const PAD: u8 = 0x01;
72pub const INFO_TS: u8 = 0x09;
73pub const INFO_REPLY: u8 = 0x0f;
74pub const INFO_DST: u8 = 0x0e;
75pub const INFO_SRC: u8 = 0x0c;
76pub const DATA_FRAG: u8 = 0x16;
77pub const NACK_FRAG: u8 = 0x12;
78pub const HEARTBEAT_FRAG: u8 = 0x13;
79
80impl WriteBytes for SubmessageKind {
81    fn write_bytes(&self, buf: &mut [u8]) -> usize {
82        buf[0] = match self {
83            SubmessageKind::DATA => DATA,
84            SubmessageKind::GAP => GAP,
85            SubmessageKind::HEARTBEAT => HEARTBEAT,
86            SubmessageKind::ACKNACK => ACKNACK,
87            SubmessageKind::PAD => PAD,
88            SubmessageKind::INFO_TS => INFO_TS,
89            SubmessageKind::INFO_REPLY => INFO_REPLY,
90            SubmessageKind::INFO_DST => INFO_DST,
91            SubmessageKind::INFO_SRC => INFO_SRC,
92            SubmessageKind::DATA_FRAG => DATA_FRAG,
93            SubmessageKind::NACK_FRAG => NACK_FRAG,
94            SubmessageKind::HEARTBEAT_FRAG => HEARTBEAT_FRAG,
95        };
96        1
97    }
98}
99
100/// Time_t
101/// Type used to hold a timestamp.
102/// Should have at least nano-second resolution.
103/// The following values are reserved by the protocol:
104/// TIME_ZERO, TIME_INVALID, TIME_INFINITE
105
106#[derive(Clone, Copy, Debug, PartialEq, Eq)]
107pub struct Time {
108    seconds: UnsignedLong,
109    fraction: UnsignedLong,
110}
111
112impl Time {
113    pub const fn new(seconds: UnsignedLong, fraction: UnsignedLong) -> Self {
114        Self { seconds, fraction }
115    }
116
117    pub fn seconds(&self) -> UnsignedLong {
118        self.seconds
119    }
120
121    pub fn fraction(&self) -> UnsignedLong {
122        self.fraction
123    }
124}
125
126#[allow(dead_code)]
127pub const TIME_ZERO: Time = Time::new(0, 0);
128pub const TIME_INVALID: Time = Time::new(0xffffffff, 0xffffffff);
129#[allow(dead_code)]
130pub const TIME_INFINITE: Time = Time::new(0xffffffff, 0xfffffffe);
131
132impl WriteBytes for Time {
133    fn write_bytes(&self, buf: &mut [u8]) -> usize {
134        self.seconds.write_bytes(&mut buf[0..]) + self.fraction.write_bytes(&mut buf[4..])
135    }
136}
137
138/// Count_t
139/// Type used to hold a count that is incremented monotonically, used to identify message duplicates.
140pub type Count = Long;
141
142/// Checksum_t
143/// Type used to hold a checksum. Used to detect RTPS message corruption by the underlying transport.
144/// The following values are reserved by the protocol: CHECKSUM_INVALID.
145#[allow(dead_code)]
146pub type Checksum32 = [Octet; 4];
147
148/// MessageLength_t
149/// Type used to hold the length of an RTPS Message.
150/// The following values are reserved by the protocol: MESSAGE_LENGTH_INVALID
151#[allow(dead_code)]
152struct MessageLength;
153
154/// ParameterId_t
155/// Type used to uniquely identify a parameter in a parameter list.
156/// Used extensively by the Discovery Module mainly to define QoS Parameters. A range of values is reserved for protocol-defined parameters, while another range can be used for vendor-defined parameters, see 8.3.5.9.
157pub type ParameterId = Short;
158
159/// FragmentNumber_t
160/// Type used to hold fragment numbers.
161/// Must be possible to represent using 32 bits.
162pub type FragmentNumber = UnsignedLong;
163
164/// GroupDigest_t
165/// Type used to hold a digest value that uniquely identifies a group of Entities belonging to the same Participant.
166#[allow(dead_code)]
167pub type GroupDigest = [Octet; 4];
168
169/// UExtension4_t
170/// Type used to hold an undefined 4-byte value. It is intended to be used in future revisions of the specification.
171#[allow(dead_code)]
172pub type UExtension4 = [Octet; 4];
173
174/// WExtension8_t
175/// Type used to hold an undefined 8-byte value. It is intended to be used in future revisions of the specification.
176#[allow(dead_code)]
177pub type WExtension8 = [Octet; 8];