ntp_proto/clock.rs
1use crate::{
2 packet::NtpLeapIndicator,
3 time_types::{NtpDuration, NtpTimestamp},
4};
5
6/// Interface for a clock settable by the ntp implementation.
7/// This needs to be a trait as a single system can have multiple clocks
8/// which need different implementation for steering and/or now.
9pub trait NtpClock: Clone + Send + 'static {
10 type Error: std::error::Error + Send + Sync;
11
12 // Get current time
13 fn now(&self) -> Result<NtpTimestamp, Self::Error>;
14
15 // Change the frequency of the clock, returning the time
16 // at which the change was applied.
17 fn set_frequency(&self, freq: f64) -> Result<NtpTimestamp, Self::Error>;
18
19 // Get the frequency of the clock
20 fn get_frequency(&self) -> Result<f64, Self::Error>;
21
22 // Change the current time of the clock by offset. Returns
23 // the time at which the change was applied.
24 fn step_clock(&self, offset: NtpDuration) -> Result<NtpTimestamp, Self::Error>;
25
26 // A clock can have a built in NTP clock discipline algorithm
27 // that does more processing on the offsets it receives. This
28 // functions disables that discipline.
29 fn disable_ntp_algorithm(&self) -> Result<(), Self::Error>;
30
31 // Provide the system with our current best estimates for
32 // the statistical error of the clock (est_error), and
33 // the maximum deviation due to frequency error and
34 // distance to the root clock.
35 fn error_estimate_update(
36 &self,
37 est_error: NtpDuration,
38 max_error: NtpDuration,
39 ) -> Result<(), Self::Error>;
40 // Change the indicators for upcoming leap seconds and
41 // the clocks synchronization status.
42 fn status_update(&self, leap_status: NtpLeapIndicator) -> Result<(), Self::Error>;
43}