Skip to content

Commit

Permalink
Separate fetch functions for different blocking behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
michielp1807 committed Dec 19, 2024
1 parent 1df529d commit 3b111e4
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 9 deletions.
6 changes: 4 additions & 2 deletions examples/main.rs → examples/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use pps_time::{pps, PpsDevice};
use std::path::PathBuf;

/// A simple PPS demo program
///
/// Build with `cargo build --package pps-time --example blocking`
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
println!("Example usage:");
println!("$ sudo ./target/debug/examples/main /dev/pps0");
println!("$ sudo ./target/debug/examples/blocking /dev/pps0");
return;
}

Expand Down Expand Up @@ -42,7 +44,7 @@ fn main() {
}

loop {
let data = pps.fetch(None).expect("Could not fetch!");
let data = pps.fetch_blocking().expect("Could not fetch!");
println!("{:#?}", data);
}
}
45 changes: 45 additions & 0 deletions examples/non_blocking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use pps_time::{pps, PpsDevice};
use std::path::PathBuf;

/// A simple PPS demo program
///
/// Build with `cargo build --package pps-time --example non_blocking`
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
println!("Example usage:");
println!("$ sudo ./target/debug/examples/non_blocking /dev/pps0");
return;
}

let path = PathBuf::from(&args[1]); // path to PPS device

println!("Opening PPS device {}", path.display());
let pps = PpsDevice::new(path).expect("Could not open file!");

let capabilities = pps.get_cap().expect("Could not get capabilities!");
println!("Capabilities: {:#x}", capabilities);

let mut params = pps.get_params().expect("Could not get params!");
println!("{:?}", params);

// Turn on CAPTUREASSERT if available
if capabilities & pps::PPS_CAPTUREASSERT != 0 {
params.mode |= pps::PPS_CAPTUREASSERT as i32;
} else {
println!("Cannot CAPTUREASSERT");
}
// Turn on CAPTURECLEAR if available
if capabilities & pps::PPS_CAPTURECLEAR != 0 {
params.mode |= pps::PPS_CAPTURECLEAR as i32;
} else {
println!("Cannot CAPTURECLEAR");
}

pps.set_params(&mut params).expect("Could not set params!");

loop {
let data = pps.fetch_non_blocking().expect("Could not fetch!");
println!("{:#?}", data);
}
}
45 changes: 45 additions & 0 deletions examples/timeout.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use pps_time::{pps, PpsDevice};
use std::path::PathBuf;

/// A simple PPS demo program
///
/// Build with `cargo build --package pps-time --example timeout`
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() < 2 {
println!("Example usage:");
println!("$ sudo ./target/debug/examples/timeout /dev/pps0");
return;
}

let path = PathBuf::from(&args[1]); // path to PPS device

println!("Opening PPS device {}", path.display());
let pps = PpsDevice::new(path).expect("Could not open file!");

let capabilities = pps.get_cap().expect("Could not get capabilities!");
println!("Capabilities: {:#x}", capabilities);

let mut params = pps.get_params().expect("Could not get params!");
println!("{:?}", params);

// Turn on CAPTUREASSERT if available
if capabilities & pps::PPS_CAPTUREASSERT != 0 {
params.mode |= pps::PPS_CAPTUREASSERT as i32;
} else {
println!("Cannot CAPTUREASSERT");
}
// Turn on CAPTURECLEAR if available
if capabilities & pps::PPS_CAPTURECLEAR != 0 {
params.mode |= pps::PPS_CAPTURECLEAR as i32;
} else {
println!("Cannot CAPTURECLEAR");
}

pps.set_params(&mut params).expect("Could not set params!");

loop {
let data = pps.fetch_timeout(0, 500_000_000);
println!("{:#?}", data); // half of these should be timeouts for 1PPS devices
}
}
39 changes: 32 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,7 @@ impl PpsDevice {
unsafe { self.ioctl_uninit(PPS_GETCAP) }
}

pub fn fetch(&self, timeout: Option<pps_ktime>) -> Result<pps_fdata> {
let timeout = timeout.unwrap_or(pps_ktime {
sec: 0,
nsec: 0,
flags: PPS_TIME_INVALID,
});

fn fetch(&self, timeout: pps_ktime) -> Result<pps_fdata> {
let mut data = pps_fdata {
info: Default::default(),
timeout,
Expand All @@ -72,4 +66,35 @@ impl PpsDevice {

Ok(data)
}

/// Fetch next PPS event, blocking until it arrives
///
/// Device must support PPS_CANWAIT, otherwise it will give an EOPNOTSUPP error
pub fn fetch_blocking(&self) -> Result<pps_fdata> {
self.fetch(pps_ktime {
sec: 0,
nsec: 0,
flags: PPS_TIME_INVALID,
})
}

/// Fetch next PPS event with a timeout, giving an ETIMEDOUT error if event does not come in time
///
/// Device must support PPS_CANWAIT, otherwise it will give an EOPNOTSUPP error
pub fn fetch_timeout(&self, seconds: i64, nanoseconds: i32) -> Result<pps_fdata> {
self.fetch(pps_ktime {
sec: seconds,
nsec: nanoseconds,
flags: 0,
})
}

/// Fetch newest PPS event without blocking
pub fn fetch_non_blocking(&self) -> Result<pps_fdata> {
self.fetch(pps_ktime {
sec: 0,
nsec: 0,
flags: 0,
})
}
}

0 comments on commit 3b111e4

Please sign in to comment.