statime/datastructures/common/
port_identity.rs

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
use super::clock_identity::ClockIdentity;
use crate::datastructures::{WireFormat, WireFormatError};

/// Identity of a single port of a PTP instance
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct PortIdentity {
    /// Identity of the clock this port is part of
    pub clock_identity: ClockIdentity,
    /// Index of the port (1-based).
    pub port_number: u16,
}

impl WireFormat for PortIdentity {
    fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
        self.clock_identity.serialize(&mut buffer[0..8])?;
        buffer[8..10].copy_from_slice(&self.port_number.to_be_bytes());
        Ok(())
    }

    fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError> {
        Ok(Self {
            clock_identity: ClockIdentity::deserialize(&buffer[0..8])?,
            port_number: u16::from_be_bytes(buffer[8..10].try_into().unwrap()),
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn timestamp_wireformat() {
        let representations = [
            (
                [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x15, 0xb3u8],
                PortIdentity {
                    clock_identity: ClockIdentity([0, 1, 2, 3, 4, 5, 6, 7]),
                    port_number: 5555,
                },
            ),
            (
                [0x40, 0x6d, 0x16, 0x36, 0xc4, 0x24, 0x0e, 0x38, 0x04, 0xd2u8],
                PortIdentity {
                    clock_identity: ClockIdentity([64, 109, 22, 54, 196, 36, 14, 56]),
                    port_number: 1234,
                },
            ),
        ];

        for (byte_representation, object_representation) in representations {
            // Test the serialization output
            let mut serialization_buffer = [0; 10];
            object_representation
                .serialize(&mut serialization_buffer)
                .unwrap();
            assert_eq!(serialization_buffer, byte_representation);

            // Test the deserialization output
            let deserialized_data = PortIdentity::deserialize(&byte_representation).unwrap();
            assert_eq!(deserialized_data, object_representation);
        }
    }
}