Skip to content

Commit

Permalink
Implemented cross-clock synchronization for hardware timestamping.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 committed Sep 29, 2023
1 parent 56e67d2 commit 9ddda38
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 55 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions statime-linux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ rand = { version = "0.8.5", default-features = false, features = ["std", "std_rn
serde = { version = "1.0.188", features = ["derive"] }


clock-steering = { git = "https://github.com/pendulum-project/clock-steering.git", rev = "3ab6721" }
timestamped-socket = { git = "https://github.com/pendulum-project/timestamped-socket.git", rev = "021bebb", features = ["serde"] }
clock-steering = { git = "https://github.com/pendulum-project/clock-steering.git", rev = "194003b" }
timestamped-socket = { git = "https://github.com/pendulum-project/timestamped-socket.git", rev = "5ec6bd4", features = ["serde"] }
27 changes: 10 additions & 17 deletions statime-linux/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use statime::{Clock, Duration, Time, TimePropertiesDS};

#[derive(Debug, Clone)]
pub struct LinuxClock {
clock: clock_steering::unix::UnixClock,
pub clock: clock_steering::unix::UnixClock,
}

impl LinuxClock {
Expand All @@ -20,16 +20,6 @@ impl LinuxClock {

Ok(Self { clock })
}

pub fn timespec(&self) -> std::io::Result<libc::timespec> {
use clock_steering::Clock;

let now = self.clock.now()?;
Ok(libc::timespec {
tv_sec: now.seconds,
tv_nsec: now.nanos as _,
})
}
}

impl clock_steering::Clock for LinuxClock {
Expand Down Expand Up @@ -68,8 +58,10 @@ impl clock_steering::Clock for LinuxClock {
}
}

fn time_from_timestamp(timestamp: clock_steering::Timestamp) -> Time {
let seconds: u64 = timestamp.seconds.try_into().unwrap();
fn time_from_timestamp(timestamp: clock_steering::Timestamp, fallback: Time) -> Time {
let Ok(seconds): Result<u64, _> = timestamp.seconds.try_into() else {
return fallback;
};

let nanos = seconds * 1_000_000_000 + timestamp.nanos as u64;
Time::from_nanos_subnanos(nanos, 0)
Expand All @@ -82,14 +74,14 @@ impl Clock for LinuxClock {
use clock_steering::Clock;

let timestamp = self.clock.now().unwrap();
time_from_timestamp(timestamp)
time_from_timestamp(timestamp, Time::from_fixed_nanos(0))
}

fn set_frequency(&mut self, freq: f64) -> Result<Time, Self::Error> {
use clock_steering::Clock;
log::trace!("Setting clock frequency to {:e}ppm", freq);
let timestamp = self.clock.set_frequency(freq)?;
Ok(time_from_timestamp(timestamp))
Ok(time_from_timestamp(timestamp, statime::Clock::now(self)))
}

fn step_clock(&mut self, time_offset: Duration) -> Result<Time, Self::Error> {
Expand All @@ -112,7 +104,7 @@ impl Clock for LinuxClock {
);

let timestamp = self.clock.step_clock(offset)?;
Ok(time_from_timestamp(timestamp))
Ok(time_from_timestamp(timestamp, statime::Clock::now(self)))
}

fn set_properties(&mut self, time_properties: &TimePropertiesDS) -> Result<(), Self::Error> {
Expand All @@ -125,7 +117,8 @@ impl Clock for LinuxClock {
};

if time_properties.is_ptp() {
self.clock.set_leap_seconds(leap_indicator)?;
// Always do this on the system clock
UnixClock::CLOCK_REALTIME.set_leap_seconds(leap_indicator)?;
}

Ok(())
Expand Down
3 changes: 2 additions & 1 deletion statime-linux/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub struct Config {
pub domain: u8,
pub priority1: u8,
pub priority2: u8,
pub hardware_clock: Option<String>,
#[serde(rename = "port")]
pub ports: Vec<PortConfig>,
}
Expand All @@ -22,6 +21,8 @@ pub struct Config {
pub struct PortConfig {
pub interface: InterfaceName,
#[serde(default)]
pub hardware_clock: Option<String>,
#[serde(default)]
pub network_mode: NetworkMode,
pub announce_interval: i8,
pub sync_interval: i8,
Expand Down
Loading

0 comments on commit 9ddda38

Please sign in to comment.