demo/
demo.rs

1//! A simple PTP demo program
2//!
3//! Build with `cargo build --package ptp-time --example demo`
4//! Run with: `sudo ./target/debug/examples/demo /dev/ptp0`
5
6use ptp_time::PtpDevice;
7use std::path::PathBuf;
8
9fn main() {
10    let args: Vec<String> = std::env::args().collect();
11    if args.len() < 2 {
12        println!("Example usage:");
13        println!("$ sudo ./target/debug/examples/demo /dev/ptp0");
14        return;
15    }
16
17    let path = PathBuf::from(&args[1]); // path to PTP device
18
19    println!("Opening PTP device {}", path.display());
20    let device = match PtpDevice::new(path) {
21        Ok(device) => device,
22        Err(e) => {
23            eprintln!("Could not open PTP device: {}", e);
24            return;
25        }
26    };
27
28    // Get clock capabilities
29    println!("\n=== Getting Clock Capabilities ===");
30    match device.get_caps() {
31        Ok(caps) => {
32            println!("Clock capabilities:");
33            println!("  max_adj: {}", caps.max_adj);
34            println!("  n_alarm: {}", caps.n_alarm);
35            println!("  n_ext_ts: {}", caps.n_ext_ts);
36            println!("  n_per_out: {}", caps.n_per_out);
37            println!("  pps: {}", caps.pps);
38            println!("  n_pins: {}", caps.n_pins);
39            println!("  cross_timestamping: {}", caps.cross_timestamping);
40            println!("  adjust_phase: {}", caps.adjust_phase);
41            println!("  max_phase_adj: {}", caps.max_phase_adj);
42        }
43        Err(e) => {
44            eprintln!("Could not get capabilities: {}", e);
45        }
46    }
47
48    // Get system offset
49    println!("\n=== Getting System Offset ===");
50    match device.get_sys_offset() {
51        Ok(offset) => {
52            println!("System offset ({} samples):", offset.n_samples);
53            println!("  First timestamp pair: {}.{:09} -> {}.{:09}",
54                offset.ts[0].sec, offset.ts[0].nsec,
55                offset.ts[1].sec, offset.ts[1].nsec);
56        }
57        Err(e) => {
58            eprintln!("Could not get system offset: {}", e);
59        }
60    }
61
62    // Get precise system offset
63    println!("\n=== Getting Precise System Offset ===");
64    match device.get_sys_offset_precise() {
65        Ok(offset) => {
66            println!("Precise system offset:");
67            println!("  Device time: {}.{:09}", offset.device.sec, offset.device.nsec);
68            println!("  System realtime: {}.{:09}", offset.sys_realtime.sec, offset.sys_realtime.nsec);
69            println!("  System monotonic raw: {}.{:09}", offset.sys_monoraw.sec, offset.sys_monoraw.nsec);
70        }
71        Err(e) => {
72            eprintln!("Could not get precise system offset: {}", e);
73        }
74    }
75
76    // Get extended system offset
77    println!("\n=== Getting Extended System Offset ===");
78    match device.get_sys_offset_extended() {
79        Ok(offset) => {
80            println!("Extended system offset ({} samples):", offset.n_samples);
81            println!("  First timestamp triplet: {}.{:09} -> {}.{:09} -> {}.{:09}",
82                offset.ts[0][0].sec, offset.ts[0][0].nsec,
83                offset.ts[0][1].sec, offset.ts[0][1].nsec,
84                offset.ts[0][2].sec, offset.ts[0][2].nsec);
85        }
86        Err(e) => {
87            eprintln!("Could not get extended system offset: {}", e);
88        }
89    }
90
91    println!("\nDemo completed successfully!");
92}