use crate::timestamp::{NtpDuration, NtpTimestamp};
pub trait Clock: Send + Sync {
fn now(&self) -> Result<NtpTimestamp, ClockError>;
fn step(&self, offset: NtpDuration) -> Result<(), ClockError>;
fn adjust_frequency(&self, ppm: f64) -> Result<(), ClockError>;
fn frequency_offset(&self) -> Result<f64, ClockError>;
fn resolution(&self) -> NtpDuration;
fn max_frequency_adjustment(&self) -> f64;
fn is_adjustable(&self) -> bool;
}
#[derive(Debug, thiserror::Error)]
pub enum ClockError {
#[error("insufficient privileges for clock adjustment")]
PermissionDenied,
#[error("clock device not found")]
DeviceNotFound,
#[error("operation not supported")]
NotSupported,
#[error("OS error: {0}")]
Os(#[from] std::io::Error),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ClockStatus {
Unsynchronized,
Synchronizing,
Synchronized,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u8)]
pub enum LeapIndicator {
#[default]
NoWarning = 0,
LastMinute61Seconds = 1,
LastMinute59Seconds = 2,
AlarmUnsynchronized = 3,
}
impl LeapIndicator {
pub fn from_u8(val: u8) -> Self {
match val {
0 => Self::NoWarning,
1 => Self::LastMinute61Seconds,
2 => Self::LastMinute59Seconds,
_ => Self::AlarmUnsynchronized,
}
}
}