statime/datastructures/common/
clock_quality.rs

1use super::clock_accuracy::ClockAccuracy;
2use crate::datastructures::{WireFormat, WireFormatError};
3
4/// A description of the accuracy and type of a clock.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct ClockQuality {
8    /// The PTP clock class.
9    ///
10    /// Per the standard, 248 is the default, and a good option for most use
11    /// cases. For grandmaster clocks, this should be below 128 to ensure the
12    /// clock never takes time from another source. A value of 6 is a good
13    /// option for a node with an external time source.
14    ///
15    /// For other potential values, see *IEEE1588-2019 section 7.6.2.5*.
16    pub clock_class: u8,
17
18    /// The accuracy of the clock
19    pub clock_accuracy: ClockAccuracy,
20
21    /// 2-log of the variance (in seconds^2) of the clock when not synchronized.
22    ///
23    /// See *IEEE1588-2019 section 7.6.3.5* for more details.
24    pub offset_scaled_log_variance: u16,
25}
26
27impl Default for ClockQuality {
28    fn default() -> Self {
29        Self {
30            clock_class: 248,
31            clock_accuracy: Default::default(),
32            // See 7.6.3.3 for the description of the calculation procedure.
33            // We estimate clock variance of desktop to be no worse than
34            // 2^-23 seconds^2, based on experience from ntpd-rs
35            offset_scaled_log_variance: 0x8000 - (23 * 256),
36        }
37    }
38}
39
40impl WireFormat for ClockQuality {
41    fn serialize(&self, buffer: &mut [u8]) -> Result<(), WireFormatError> {
42        buffer[0] = self.clock_class;
43        buffer[1] = self.clock_accuracy.to_primitive();
44        buffer[2..4].copy_from_slice(&self.offset_scaled_log_variance.to_be_bytes());
45        Ok(())
46    }
47
48    fn deserialize(buffer: &[u8]) -> Result<Self, WireFormatError> {
49        Ok(Self {
50            clock_class: buffer[0],
51            clock_accuracy: ClockAccuracy::from_primitive(buffer[1]),
52            offset_scaled_log_variance: u16::from_be_bytes(buffer[2..4].try_into().unwrap()),
53        })
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::*;
60
61    #[test]
62    fn timestamp_wireformat() {
63        let representations = [(
64            [0x7a, 0x2a, 0x12, 0x34u8],
65            ClockQuality {
66                clock_class: 122,
67                clock_accuracy: ClockAccuracy::MS2_5,
68                offset_scaled_log_variance: 0x1234,
69            },
70        )];
71
72        for (byte_representation, object_representation) in representations {
73            // Test the serialization output
74            let mut serialization_buffer = [0; 4];
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 = ClockQuality::deserialize(&byte_representation).unwrap();
82            assert_eq!(deserialized_data, object_representation);
83        }
84    }
85}