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
use super::overall_structure::WriteBytes;
use std::io::Read;

/// This files shall only contain the types as listed in the DDSI-RTPS Version 2.5
/// Table 8.13 - Types used to define RTPS messages

type Octet = u8;
type Long = i32;
type UnsignedLong = u32;
type Short = i16;

/// ProtocolId_t
/// Enumeration used to identify the protocol.
/// The following values are reserved by the protocol: PROTOCOL_RTPS
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(non_camel_case_types)]
pub enum ProtocolId {
    PROTOCOL_RTPS,
}

impl WriteBytes for ProtocolId {
    fn write_bytes(&self, buf: &mut [u8]) -> usize {
        b"RTPS".as_slice().read(buf).unwrap()
    }
}

/// SubmessageFlag
/// Type used to specify a Submessage flag.
/// A Submessage flag takes a boolean value and affects the parsing of the Submessage by the receiver.
pub type SubmessageFlag = bool;

impl WriteBytes for [SubmessageFlag; 8] {
    fn write_bytes(&self, buf: &mut [u8]) -> usize {
        let mut flags = 0b_0000_0000_u8;
        for (i, &item) in self.iter().enumerate() {
            if item {
                flags |= 0b_0000_0001 << i
            }
        }
        buf[0] = flags;
        1
    }
}

/// SubmessageKind
/// Enumeration used to identify the kind of Submessage.
/// The following values are reserved by this version of the protocol:
/// DATA, GAP, HEARTBEAT, ACKNACK, PAD, INFO_TS, INFO_REPLY, INFO_DST, INFO_SRC, DATA_FRAG, NACK_FRAG, HEARTBEAT_FRAG
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[allow(non_camel_case_types)]
#[allow(clippy::upper_case_acronyms)]
pub enum SubmessageKind {
    DATA,
    GAP,
    HEARTBEAT,
    ACKNACK,
    PAD,
    INFO_TS,
    INFO_REPLY,
    INFO_DST,
    INFO_SRC,
    DATA_FRAG,
    NACK_FRAG,
    HEARTBEAT_FRAG,
}

pub const DATA: u8 = 0x15;
pub const GAP: u8 = 0x08;
pub const HEARTBEAT: u8 = 0x07;
pub const ACKNACK: u8 = 0x06;
pub const PAD: u8 = 0x01;
pub const INFO_TS: u8 = 0x09;
pub const INFO_REPLY: u8 = 0x0f;
pub const INFO_DST: u8 = 0x0e;
pub const INFO_SRC: u8 = 0x0c;
pub const DATA_FRAG: u8 = 0x16;
pub const NACK_FRAG: u8 = 0x12;
pub const HEARTBEAT_FRAG: u8 = 0x13;

impl WriteBytes for SubmessageKind {
    fn write_bytes(&self, buf: &mut [u8]) -> usize {
        buf[0] = match self {
            SubmessageKind::DATA => DATA,
            SubmessageKind::GAP => GAP,
            SubmessageKind::HEARTBEAT => HEARTBEAT,
            SubmessageKind::ACKNACK => ACKNACK,
            SubmessageKind::PAD => PAD,
            SubmessageKind::INFO_TS => INFO_TS,
            SubmessageKind::INFO_REPLY => INFO_REPLY,
            SubmessageKind::INFO_DST => INFO_DST,
            SubmessageKind::INFO_SRC => INFO_SRC,
            SubmessageKind::DATA_FRAG => DATA_FRAG,
            SubmessageKind::NACK_FRAG => NACK_FRAG,
            SubmessageKind::HEARTBEAT_FRAG => HEARTBEAT_FRAG,
        };
        1
    }
}

/// Time_t
/// Type used to hold a timestamp.
/// Should have at least nano-second resolution.
/// The following values are reserved by the protocol:
/// TIME_ZERO, TIME_INVALID, TIME_INFINITE

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Time {
    seconds: UnsignedLong,
    fraction: UnsignedLong,
}

impl Time {
    pub const fn new(seconds: UnsignedLong, fraction: UnsignedLong) -> Self {
        Self { seconds, fraction }
    }

    pub fn seconds(&self) -> UnsignedLong {
        self.seconds
    }

    pub fn fraction(&self) -> UnsignedLong {
        self.fraction
    }
}

#[allow(dead_code)]
pub const TIME_ZERO: Time = Time::new(0, 0);
pub const TIME_INVALID: Time = Time::new(0xffffffff, 0xffffffff);
#[allow(dead_code)]
pub const TIME_INFINITE: Time = Time::new(0xffffffff, 0xfffffffe);

impl WriteBytes for Time {
    fn write_bytes(&self, buf: &mut [u8]) -> usize {
        self.seconds.write_bytes(&mut buf[0..]) + self.fraction.write_bytes(&mut buf[4..])
    }
}

/// Count_t
/// Type used to hold a count that is incremented monotonically, used to identify message duplicates.
pub type Count = Long;

/// Checksum_t
/// Type used to hold a checksum. Used to detect RTPS message corruption by the underlying transport.
/// The following values are reserved by the protocol: CHECKSUM_INVALID.
#[allow(dead_code)]
pub type Checksum32 = [Octet; 4];

/// MessageLength_t
/// Type used to hold the length of an RTPS Message.
/// The following values are reserved by the protocol: MESSAGE_LENGTH_INVALID
#[allow(dead_code)]
struct MessageLength;

/// ParameterId_t
/// Type used to uniquely identify a parameter in a parameter list.
/// 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.
pub type ParameterId = Short;

/// FragmentNumber_t
/// Type used to hold fragment numbers.
/// Must be possible to represent using 32 bits.
pub type FragmentNumber = UnsignedLong;

/// GroupDigest_t
/// Type used to hold a digest value that uniquely identifies a group of Entities belonging to the same Participant.
#[allow(dead_code)]
pub type GroupDigest = [Octet; 4];

/// UExtension4_t
/// Type used to hold an undefined 4-byte value. It is intended to be used in future revisions of the specification.
#[allow(dead_code)]
pub type UExtension4 = [Octet; 4];

/// WExtension8_t
/// Type used to hold an undefined 8-byte value. It is intended to be used in future revisions of the specification.
#[allow(dead_code)]
pub type WExtension8 = [Octet; 8];