Skip to main content

statime_wire/common/
port_identity.rs

1use super::clock_identity::ClockIdentity;
2use crate::Error;
3
4/// Identity of a single port of a PTP instance
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct PortIdentity {
8    /// Identity of the clock this port is part of
9    pub clock_identity: ClockIdentity,
10    /// Index of the port (1-based).
11    pub port_number: u16,
12}
13
14impl PortIdentity {
15    /// Serialize a port identity into a 10 byte buffer.
16    ///
17    /// # Errors
18    /// Fails when the provided buffer is too short.
19    pub fn serialize(&self, buffer: &mut [u8]) -> Result<(), Error> {
20        self.clock_identity
21            .serialize(buffer.get_mut(0..8).ok_or(Error::BufferTooShort)?)?;
22        buffer
23            .get_mut(8..10)
24            .ok_or(Error::BufferTooShort)?
25            .copy_from_slice(&self.port_number.to_be_bytes());
26        Ok(())
27    }
28
29    /// Deserialize a port identity from a 10 byte buffer.
30    ///
31    /// # Errors
32    /// Fails when the provided buffer is too short.
33    pub(crate) fn deserialize(buffer: &[u8]) -> Result<Self, Error> {
34        Ok(Self {
35            clock_identity: ClockIdentity::deserialize(
36                buffer.get(0..8).ok_or(Error::BufferTooShort)?,
37            )?,
38            port_number: u16::from_be_bytes(
39                buffer
40                    .get(8..10)
41                    .ok_or(Error::BufferTooShort)?
42                    .try_into()
43                    .unwrap(),
44            ),
45        })
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn timestamp_wireformat() {
55        let representations = [
56            (
57                [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x15, 0xb3u8],
58                PortIdentity {
59                    clock_identity: ClockIdentity([0, 1, 2, 3, 4, 5, 6, 7]),
60                    port_number: 5555,
61                },
62            ),
63            (
64                [0x40, 0x6d, 0x16, 0x36, 0xc4, 0x24, 0x0e, 0x38, 0x04, 0xd2u8],
65                PortIdentity {
66                    clock_identity: ClockIdentity([64, 109, 22, 54, 196, 36, 14, 56]),
67                    port_number: 1234,
68                },
69            ),
70        ];
71
72        for (byte_representation, object_representation) in representations {
73            // Test the serialization output
74            let mut serialization_buffer = [0; 10];
75            object_representation
76                .serialize(&mut serialization_buffer)
77                .unwrap();
78            assert_eq!(serialization_buffer, byte_representation);
79
80            // Test the deserialization output
81            let deserialized_data = PortIdentity::deserialize(&byte_representation).unwrap();
82            assert_eq!(deserialized_data, object_representation);
83        }
84    }
85}