1use crate::timestamp::{NtpDuration, NtpTimestamp};
2
3pub trait Clock: Send + Sync {
10 fn now(&self) -> Result<NtpTimestamp, ClockError>;
12
13 fn step(&self, offset: NtpDuration) -> Result<(), ClockError>;
15
16 fn adjust_frequency(&self, ppm: f64) -> Result<(), ClockError>;
18
19 fn frequency_offset(&self) -> Result<f64, ClockError>;
21
22 fn resolution(&self) -> NtpDuration;
24
25 fn max_frequency_adjustment(&self) -> f64;
27
28 fn is_adjustable(&self) -> bool;
30}
31
32#[derive(Debug, thiserror::Error)]
33pub enum ClockError {
34 #[error("insufficient privileges for clock adjustment")]
35 PermissionDenied,
36
37 #[error("clock device not found")]
38 DeviceNotFound,
39
40 #[error("operation not supported")]
41 NotSupported,
42
43 #[error("OS error: {0}")]
44 Os(#[from] std::io::Error),
45}
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum ClockStatus {
50 Unsynchronized,
52 Synchronizing,
54 Synchronized,
56}
57
58#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
60#[repr(u8)]
61pub enum LeapIndicator {
62 #[default]
63 NoWarning = 0,
64 LastMinute61Seconds = 1,
65 LastMinute59Seconds = 2,
66 AlarmUnsynchronized = 3,
67}
68
69impl LeapIndicator {
70 pub fn from_u8(val: u8) -> Self {
71 match val {
72 0 => Self::NoWarning,
73 1 => Self::LastMinute61Seconds,
74 2 => Self::LastMinute59Seconds,
75 _ => Self::AlarmUnsynchronized,
76 }
77 }
78}