statime/clock.rs
1//! Definitions and implementations of the abstract clock types
2
3use crate::{
4 datastructures::datasets::TimePropertiesDS,
5 time::{Duration, Time},
6};
7
8/// Clock manipulation and querying interface
9///
10/// The clock trait is the primary way the PTP stack interfaces with the
11/// system's clock. It's implementation should be provided by the user of the
12/// Statime crate, and should provide information on and ways to manipulate the
13/// system's clock. An implementation of this trait for linux is provided in the
14/// statime-linux crate.
15///
16/// Note that the clock implementation is responsible for handling leap seconds.
17/// On most operating systems, this will be provided for by the OS, but on some
18/// platforms this may require extra logic.
19pub trait Clock {
20 /// Type of the error the methods of this [`Clock`] may return
21 type Error: core::fmt::Debug;
22
23 /// Get the current time of the clock
24 fn now(&self) -> Time;
25
26 /// Change the current time of the clock by offset. Returns
27 /// the time at which the change was applied.
28 ///
29 /// The applied correction should be as close as possible to
30 /// the requested correction. The reported time of the change
31 /// should be as close as possible to the time the change was
32 /// applied
33 fn step_clock(&mut self, offset: Duration) -> Result<Time, Self::Error>;
34
35 /// Set the frequency of the clock, returning the time
36 /// at which the change was applied. The value is in ppm
37 /// difference from the clocks base frequency.
38 ///
39 /// The applied correction should be as close as possible to
40 /// the requested correction. The reported time of the change
41 /// should be as close as possible to the time the change was
42 /// applied
43 fn set_frequency(&mut self, ppm: f64) -> Result<Time, Self::Error>;
44
45 /// Adjust the timescale properties of the clock, including
46 /// things like the leap indicator, to the extend supported by the
47 /// system.
48 fn set_properties(&mut self, time_properties_ds: &TimePropertiesDS) -> Result<(), Self::Error>;
49}
50
51#[cfg(feature = "std")]
52impl<T: Clock + ?Sized> Clock for std::boxed::Box<T> {
53 type Error = T::Error;
54 fn now(&self) -> Time {
55 self.as_ref().now()
56 }
57 fn step_clock(&mut self, offset: Duration) -> Result<Time, Self::Error> {
58 self.as_mut().step_clock(offset)
59 }
60 fn set_frequency(&mut self, ppm: f64) -> Result<Time, Self::Error> {
61 self.as_mut().set_frequency(ppm)
62 }
63 fn set_properties(&mut self, time_properties_ds: &TimePropertiesDS) -> Result<(), Self::Error> {
64 self.as_mut().set_properties(time_properties_ds)
65 }
66}