Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the phc clocks are properly initialized if needed. #526

Merged
merged 1 commit into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading