-
Notifications
You must be signed in to change notification settings - Fork 8
Add clock capabilities and example binary #137
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| //! Demonstrates the clock-steering library. | ||
| //! | ||
| //! Usage: cargo run --example basic [realtime|tai|/dev/ptpN] | ||
| //! | ||
| //! Write operations (frequency, step, leap seconds, TAI) require root privileges. | ||
|
|
||
| use clock_steering::{unix::UnixClock, Clock, LeapIndicator, TimeOffset}; | ||
| use std::time::Duration; | ||
|
|
||
| fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
| #[cfg(target_os = "linux")] | ||
| let arg = std::env::args().nth(1); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| let clock: UnixClock = match arg.as_deref() { | ||
| None | Some("realtime") => UnixClock::CLOCK_REALTIME, | ||
| Some("tai") => UnixClock::CLOCK_TAI, | ||
| Some(path) if path.starts_with("/dev/") => UnixClock::open(path)?, | ||
| Some(other) => { | ||
| eprintln!("unknown clock: {other}"); | ||
| eprintln!("usage: basic [realtime|tai|/dev/ptpN]"); | ||
| std::process::exit(1); | ||
| } | ||
| }; | ||
|
|
||
| #[cfg(not(target_os = "linux"))] | ||
| let clock = UnixClock::CLOCK_REALTIME; | ||
|
|
||
| // Read-only operations | ||
|
|
||
| let now = clock.now()?; | ||
| println!("now: {}.{:09}", now.seconds, now.nanos); | ||
|
|
||
| let res = clock.resolution()?; | ||
| println!("resolution: {}ns", res.nanos); | ||
|
|
||
| let caps = clock.capabilities()?; | ||
| println!("max freq: {} ppm", caps.max_frequency_adjustment_ppm); | ||
| println!("max offset: {}ns", caps.max_offset_adjustment_ns); | ||
|
|
||
| match clock.get_frequency() { | ||
| Ok(f) => println!("frequency: {f:.6} ms/s"), | ||
| Err(e) => println!("frequency: {e}"), | ||
| } | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| match clock.get_tai() { | ||
| Ok(tai) => println!("TAI offset: {tai}s"), | ||
| Err(e) => println!("TAI offset: {e}"), | ||
| } | ||
|
|
||
| // Write operations — require root | ||
|
|
||
| println!(); | ||
|
|
||
| match clock.set_frequency(0.0) { | ||
| Ok(t) => println!("set_frequency(0.0): ok at {}.{:09}", t.seconds, t.nanos), | ||
| Err(e) => println!("set_frequency(0.0): {e}"), | ||
| } | ||
|
|
||
| match clock.step_clock(TimeOffset { | ||
| seconds: 0, | ||
| nanos: 0, | ||
| }) { | ||
| Ok(t) => println!("step_clock(0): ok at {}.{:09}", t.seconds, t.nanos), | ||
| Err(e) => println!("step_clock(0): {e}"), | ||
| } | ||
|
|
||
| match clock.set_leap_seconds(LeapIndicator::NoWarning) { | ||
| Ok(()) => println!("set_leap_seconds: ok"), | ||
| Err(e) => println!("set_leap_seconds: {e}"), | ||
| } | ||
|
|
||
| match clock.error_estimate_update(Duration::from_micros(100), Duration::from_millis(1)) { | ||
| Ok(()) => println!("error_estimate_update: ok"), | ||
| Err(e) => println!("error_estimate_update: {e}"), | ||
| } | ||
|
|
||
| match clock.disable_kernel_ntp_algorithm() { | ||
| Ok(()) => println!("disable_kernel_ntp: ok"), | ||
| Err(e) => println!("disable_kernel_ntp: {e}"), | ||
| } | ||
|
|
||
| Ok(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| //! Linux ioctl definitions for PTP clock devices. | ||
| //! | ||
| //! These definitions are derived from <linux/ptp_clock.h>. | ||
|
|
||
| use std::os::unix::io::RawFd; | ||
|
|
||
| /// PTP clock capabilities as reported by the kernel. | ||
| #[repr(C)] | ||
| pub struct PtpClockCaps { | ||
| pub max_adj: libc::c_int, // Maximum frequency adjustment in parts per billion | ||
| pub n_alarm: libc::c_int, // Number of programmable alarms | ||
| pub n_ext_ts: libc::c_int, // Number of external time stamp channels | ||
| pub n_per_out: libc::c_int, // Number of programmable periodic signals | ||
| pub pps: libc::c_int, // Whether the clock supports a PPS callback | ||
| pub n_pins: libc::c_int, // Number of input/output pins | ||
| pub cross_timestamping: libc::c_int, // Whether the clock supports precise system-device cross timestamps | ||
| pub adjust_phase: libc::c_int, // Whether the clock supports adjust phase | ||
| pub max_phase_adj: libc::c_int, // Maximum offset adjustment in nanoseconds | ||
| pub rsv: [libc::c_int; 11], // Reserved for future use | ||
| } | ||
|
|
||
| // PTP_CLOCK_GETCAPS = _IOR('=', 1, struct ptp_clock_caps) | ||
| // | ||
| // Linux _IOR encoding: (IOC_READ << 30) | (size << 16) | (type << 8) | nr | ||
| // IOC_READ = 2, type = b'=' = 0x3D, nr = 1 | ||
| const PTP_CLOCK_GETCAPS: u32 = | ||
| (2u32 << 30) | ((std::mem::size_of::<PtpClockCaps>() as u32) << 16) | ((b'=' as u32) << 8) | 1; | ||
|
|
||
| /// Query PTP clock capabilities via ioctl. | ||
| /// | ||
| /// Returns 0 on success, -1 on error (check `errno` for details). | ||
| /// | ||
| /// # Safety | ||
| /// `caps` must be a valid, writable pointer to a `PtpClockCaps`. | ||
| pub unsafe fn ptp_clock_getcaps(fd: RawFd, caps: *mut PtpClockCaps) -> libc::c_int { | ||
| libc::ioctl(fd, PTP_CLOCK_GETCAPS as _, caps) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: this needs upstreaming to libc.