1use std::net::SocketAddr;
2
3use crate::clock::LeapIndicator;
4use crate::timestamp::{NtpDuration, NtpTimestamp};
5
6#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8pub enum SourceId {
9 Ntp {
10 address: SocketAddr,
11 reference_id: u32,
12 },
13 Ptp {
14 clock_identity: [u8; 8],
15 port_number: u16,
16 },
17 RefClock {
18 driver: String,
19 unit: u8,
20 },
21}
22
23impl std::fmt::Display for SourceId {
24 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25 match self {
26 Self::Ntp { address, .. } => write!(f, "NTP:{}", address),
27 Self::Ptp {
28 clock_identity,
29 port_number,
30 } => {
31 write!(
32 f,
33 "PTP:{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}-{}",
34 clock_identity[0],
35 clock_identity[1],
36 clock_identity[2],
37 clock_identity[3],
38 clock_identity[4],
39 clock_identity[5],
40 clock_identity[6],
41 clock_identity[7],
42 port_number
43 )
44 }
45 Self::RefClock { driver, unit } => write!(f, "REF:{}({})", driver, unit),
46 }
47 }
48}
49
50#[derive(Clone, Debug)]
52pub struct SourceMeasurement {
53 pub id: SourceId,
54 pub offset: NtpDuration,
56 pub delay: NtpDuration,
58 pub dispersion: NtpDuration,
60 pub jitter: f64,
62 pub stratum: u8,
64 pub leap_indicator: LeapIndicator,
66 pub root_delay: NtpDuration,
68 pub root_dispersion: NtpDuration,
70 pub time: NtpTimestamp,
72}
73
74impl SourceMeasurement {
75 pub fn root_distance(&self) -> NtpDuration {
78 self.root_delay.abs() / 2 + self.root_dispersion + self.delay.abs() / 2 + self.dispersion
79 }
80}