statime/datastructures/messages/
p_delay_resp_follow_up.rs

1use crate::datastructures::{
2    common::{PortIdentity, WireTimestamp},
3    WireFormat, WireFormatError,
4};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub(crate) struct PDelayRespFollowUpMessage {
8    pub(crate) response_origin_timestamp: WireTimestamp,
9    pub(crate) requesting_port_identity: PortIdentity,
10}
11
12impl PDelayRespFollowUpMessage {
13    pub(crate) fn content_size(&self) -> usize {
14        20
15    }
16
17    pub(crate) fn serialize_content(
18        &self,
19        buffer: &mut [u8],
20    ) -> Result<(), crate::datastructures::WireFormatError> {
21        if buffer.len() < 20 {
22            return Err(WireFormatError::BufferTooShort);
23        }
24
25        self.response_origin_timestamp
26            .serialize(&mut buffer[0..10])?;
27        self.requesting_port_identity
28            .serialize(&mut buffer[10..20])?;
29
30        Ok(())
31    }
32
33    pub(crate) fn deserialize_content(
34        buffer: &[u8],
35    ) -> Result<Self, crate::datastructures::WireFormatError> {
36        if buffer.len() < 20 {
37            return Err(WireFormatError::BufferTooShort);
38        }
39        Ok(Self {
40            response_origin_timestamp: WireTimestamp::deserialize(&buffer[0..10])?,
41            requesting_port_identity: PortIdentity::deserialize(&buffer[10..20])?,
42        })
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::datastructures::common::{ClockIdentity, PortIdentity};
50
51    #[test]
52    fn timestamp_wireformat() {
53        let representations = [(
54            [
55                0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0, 0x01, 0x02, 0x03, 0x04,
56                0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
57            ],
58            PDelayRespFollowUpMessage {
59                response_origin_timestamp: WireTimestamp {
60                    seconds: 1169232218,
61                    nanos: 174389936,
62                },
63                requesting_port_identity: PortIdentity {
64                    clock_identity: ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
65                    port_number: 0x090a,
66                },
67            },
68        )];
69
70        for (byte_representation, object_representation) in representations {
71            // Test the serialization output
72            let mut serialization_buffer = [0; 20];
73            object_representation
74                .serialize_content(&mut serialization_buffer)
75                .unwrap();
76            assert_eq!(serialization_buffer, byte_representation);
77
78            // Test the deserialization output
79            let deserialized_data =
80                PDelayRespFollowUpMessage::deserialize_content(&byte_representation).unwrap();
81            assert_eq!(deserialized_data, object_representation);
82        }
83    }
84}