Skip to content

Commit

Permalink
Ensure the phc clocks are properly initialized if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 committed Aug 23, 2024
1 parent fbe21fe commit 00692c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions statime-linux/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let path = format!("/dev/ptp{}", idx);
Self::open(path)
Expand Down
3 changes: 2 additions & 1 deletion statime-linux/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 00692c6

Please sign in to comment.