Skip to main content

statime_wire/messages/
p_delay_resp_follow_up.rs

1use crate::{
2    Error,
3    common::{PortIdentity, Timestamp},
4};
5
6/// Peer delay response follow up message
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub struct PDelayRespFollowUpMessage {
9    /// Precise time the peer delay response was sent.
10    pub response_origin_timestamp: Timestamp,
11    /// Port identity of the port that sent the original peer delay request.
12    pub requesting_port_identity: PortIdentity,
13}
14
15impl PDelayRespFollowUpMessage {
16    pub(crate) fn content_size(&self) -> usize {
17        20
18    }
19
20    pub(crate) fn serialize_content(&self, buffer: &mut [u8]) -> Result<(), crate::Error> {
21        if buffer.len() < 20 {
22            return Err(Error::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(buffer: &[u8]) -> Result<Self, crate::Error> {
34        if buffer.len() < 20 {
35            return Err(Error::BufferTooShort);
36        }
37        Ok(Self {
38            response_origin_timestamp: Timestamp::deserialize(&buffer[0..10])?,
39            requesting_port_identity: PortIdentity::deserialize(&buffer[10..20])?,
40        })
41    }
42}
43
44#[cfg(test)]
45mod tests {
46    use super::*;
47    use crate::common::{ClockIdentity, PortIdentity};
48
49    #[test]
50    fn timestamp_wireformat() {
51        let representations = [(
52            [
53                0x00, 0x00, 0x45, 0xb1, 0x11, 0x5a, 0x0a, 0x64, 0xfa, 0xb0, 0x01, 0x02, 0x03, 0x04,
54                0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
55            ],
56            PDelayRespFollowUpMessage {
57                response_origin_timestamp: Timestamp::new(1_169_232_218, 174_389_936).unwrap(),
58                requesting_port_identity: PortIdentity {
59                    clock_identity: ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
60                    port_number: 0x090a,
61                },
62            },
63        )];
64
65        for (byte_representation, object_representation) in representations {
66            // Test the serialization output
67            let mut serialization_buffer = [0; 20];
68            object_representation
69                .serialize_content(&mut serialization_buffer)
70                .unwrap();
71            assert_eq!(serialization_buffer, byte_representation);
72
73            // Test the deserialization output
74            let deserialized_data =
75                PDelayRespFollowUpMessage::deserialize_content(&byte_representation).unwrap();
76            assert_eq!(deserialized_data, object_representation);
77        }
78    }
79}