statime/datastructures/common/
clock_identity.rs

1use crate::datastructures::{WireFormat, WireFormatError};
2
3/// The identity of a PTP node.
4///
5/// All ptp clocks in a network need a unique clock identity. A common approach
6/// is to use a mac address of the device to generate a unique identifier (see
7/// [`from_mac_address`](`Self::from_mac_address`)).
8///
9/// For more details, see *IEEE1588-2019 section 7.5.2.2.2*.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct ClockIdentity(pub [u8; 8]);
13
14impl ClockIdentity {
15    /// Create a [`ClockIdentity`] from a mac address.
16    ///
17    /// Fills the first six bytes with the mac address and the rest with zeroes.
18    ///
19    /// # Example
20    /// ```
21    /// # use statime::config::ClockIdentity;
22    /// let id = ClockIdentity::from_mac_address([0xA, 0xB, 0xC, 0xD, 0xE, 0xF]);
23    /// assert_eq!(id.0, [0xA, 0xB, 0xC, 0xD, 0xE, 0xF, 0x0, 0x0]);
24    /// ```
25    pub fn from_mac_address(addr: [u8; 6]) -> Self {
26        let mut this = Self([0; 8]);
27
28        this.0[0..6].copy_from_slice(&addr);
29
30        this
31    }
32}
33
34impl WireFormat for ClockIdentity {
35    fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
36        buffer[0..8].copy_from_slice(&self.0);
37        Ok(())
38    }
39
40    fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError> {
41        Ok(Self(buffer[0..8].try_into().unwrap()))
42    }
43}
44
45#[cfg(feature = "std")]
46impl core::fmt::Display for ClockIdentity {
47    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
48        for (i, val) in self.0.iter().enumerate() {
49            if i != 0 {
50                write!(f, ":")?;
51            }
52
53            write!(f, "{:02x}", val)?;
54        }
55
56        Ok(())
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn timestamp_wireformat() {
66        let representations = [(
67            [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08u8],
68            ClockIdentity([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]),
69        )];
70
71        for (byte_representation, object_representation) in representations {
72            // Test the serialization output
73            let mut serialization_buffer = [0; 8];
74            object_representation
75                .serialize(&mut serialization_buffer)
76                .unwrap();
77            assert_eq!(serialization_buffer, byte_representation);
78
79            // Test the deserialization output
80            let deserialized_data = ClockIdentity::deserialize(&byte_representation).unwrap();
81            assert_eq!(deserialized_data, object_representation);
82        }
83    }
84
85    #[test]
86    fn from_mac() {
87        let mac = [1, 2, 3, 4, 5, 6];
88        let id = ClockIdentity::from_mac_address(mac);
89        assert_eq!(id, ClockIdentity([1, 2, 3, 4, 5, 6, 0, 0]));
90    }
91}