refactor: Update PTP setup to match statime API changes

Co-authored-by: aider (gemini/gemini-2.5-pro-preview-05-06) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-07-10 16:53:04 +01:00
parent 59570ecdcb
commit 7cd86ad9f4

View file

@ -2,12 +2,15 @@ use crate::config::Config;
use crate::sync_logic::LtcState; use crate::sync_logic::LtcState;
use rand::thread_rng; use rand::thread_rng;
use statime::{ use statime::{
config::{InstanceConfig, PortConfig, TimePropertiesDS}, config::{
filter::moving_average::MovingAverageFilter, AcceptAnyMaster, ClockIdentity, ClockQuality, DelayMechanism, InstanceConfig, PortConfig,
TimePropertiesDS, TimeSource,
},
filters::BasicFilter,
port::PortAction, port::PortAction,
ClockIdentity, PtpInstance, OverlayClock, PtpInstance, SharedClock,
}; };
use statime_linux::{clock::LinuxClock, udp::LinuxUdpHandles}; use statime_linux::{clock::SystemClock, net::LinuxUdpHandles};
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use tokio::time::{sleep, Instant}; use tokio::time::{sleep, Instant};
@ -60,24 +63,36 @@ async fn run_ptp_session(
let initial_interface = interface.clone(); let initial_interface = interface.clone();
// 1. Create configs // 1. Create configs
// A PTP client doesn't need a stable identity. let instance_config = InstanceConfig {
let clock_identity = ClockIdentity::new([0x00, 0x11, 0x22, 0xFF, 0xFE, 0x33, 0x44, 0x55]); clock_identity: ClockIdentity::from_mac_address(&[0x00, 0x11, 0x22, 0xFF, 0xFE, 0x33, 0x44, 0x55]),
let instance_config = InstanceConfig::new(clock_identity, 128, 128, 0); priority_1: 128,
let time_properties_ds = TimePropertiesDS::default(); priority_2: 128,
domain_number: 0,
slave_only: false,
sdo_id: Default::default(),
path_trace: false,
clock_quality: ClockQuality::default(),
};
let time_properties_ds =
TimePropertiesDS::new_arbitrary_time(false, false, TimeSource::InternalOscillator);
// 2. Create PtpInstance // 2. Create PtpInstance
let mut ptp_instance = PtpInstance::new(instance_config, time_properties_ds); let mut ptp_instance = PtpInstance::<BasicFilter>::new(instance_config, time_properties_ds);
// 3. Create PortConfig // 3. Create PortConfig
let mut port_config = PortConfig::new(interface.into()); let port_config = PortConfig {
port_config.use_hardware_timestamping = false; iface: interface.into(),
use_hardware_timestamping: false,
acceptable_master_list: AcceptAnyMaster,
delay_mechanism: DelayMechanism::EndToEnd,
};
// 4. Create Clock and Filter // 4. Create Clock and Filter
let clock = LinuxClock::new(); let clock = SharedClock::new(OverlayClock::new(SystemClock::new()));
let filter = MovingAverageFilter::new(20); let filter_config = ();
// 5. Add port and run BMCA // 5. Add port and run BMCA
let mut port = ptp_instance.add_port(port_config.clone(), clock, filter, thread_rng())?; let mut port = ptp_instance.add_port(port_config.clone(), filter_config, clock, thread_rng())?;
ptp_instance.bmca(&mut [&mut port]); ptp_instance.bmca(&mut [&mut port]);
let mut running_port = port.end_bmca()?; let mut running_port = port.end_bmca()?;
@ -124,17 +139,21 @@ async fn run_ptp_session(
for action in actions { for action in actions {
match action { match action {
PortAction::SendMessage(message) => { PortAction::Send {
let handle = if message.event { destination,
data,
event,
} => {
let handle = if event {
&mut event_handle &mut event_handle
} else { } else {
&mut general_handle &mut general_handle
}; };
if let Err(e) = handle.send(message.data, message.destination).await { if let Err(e) = handle.send(data, destination).await {
log::error!("Error sending PTP packet: {}", e); log::error!("Error sending PTP packet: {}", e);
} }
} }
PortAction::ToBmca => { PortAction::Reset => {
log::warn!("PTP port is resetting"); log::warn!("PTP port is resetting");
ptp_instance.bmca(&mut [&mut running_port.start_bmca()]); ptp_instance.bmca(&mut [&mut running_port.start_bmca()]);
running_port = running_port.start_bmca().end_bmca()?; running_port = running_port.start_bmca().end_bmca()?;