diff --git a/statime-linux/src/clock/mod.rs b/statime-linux/src/clock/mod.rs index 5d2c0d7e8..36627eaf2 100644 --- a/statime-linux/src/clock/mod.rs +++ b/statime-linux/src/clock/mod.rs @@ -30,6 +30,22 @@ impl LinuxClock { }) } + // This forces the clock to start with seconds such that the current time in + // nanoseconds is representable as a u64. + pub fn init(&mut self) -> Result<(), clock_steering::unix::Error> { + use clock_steering::Clock; + + let ts = self.clock.now()?; + if ts.seconds < 0 || ts.seconds > (u64::MAX / 1000000000) as i64 { + self.clock.step_clock(TimeOffset { + seconds: -ts.seconds + 1, + nanos: 0, + })?; + } + + Ok(()) + } + pub fn open_idx(idx: u32) -> std::io::Result { let path = format!("/dev/ptp{}", idx); Self::open(path) diff --git a/statime-linux/src/main.rs b/statime-linux/src/main.rs index 7caafe762..77c264a42 100644 --- a/statime-linux/src/main.rs +++ b/statime-linux/src/main.rs @@ -253,10 +253,11 @@ async fn actual_main() { let network_mode = port_config.network_mode; let (port_clock, timestamping) = match port_config.hardware_clock { Some(idx) => { - let clock = LinuxClock::open_idx(idx).expect("Unable to open clock"); + let mut clock = LinuxClock::open_idx(idx).expect("Unable to open clock"); if let Some(id) = clock_name_map.get(&idx) { clock_port_map.push(Some(*id)); } else { + clock.init().expect("Unable to initialize clock"); let id = internal_sync_senders.len(); clock_port_map.push(Some(id)); clock_name_map.insert(idx, id);