statime/filters/
mod.rs

1//! Definitions and implementations for the abstracted measurement filters
2
3mod basic;
4mod kalman;
5mod matrix;
6
7pub use basic::BasicFilter;
8pub use kalman::{KalmanConfiguration, KalmanFilter};
9
10use crate::{port::Measurement, time::Duration, Clock};
11
12/// Information on the result of the [`Filter`] and the actions it needs from
13/// its environment
14#[derive(Debug, Clone, Default, PartialEq, Eq)]
15pub struct FilterUpdate {
16    /// Duration until the [`Filter::update`] should be called again.
17    ///
18    /// * If **`Some`** [`Filter::update`] should be called in the given
19    ///   [`Duration`](`core::time::Duration`) from now.
20    /// * If **`None`** [`Filter::update`] the timer should not be changed.
21    pub next_update: Option<core::time::Duration>,
22    /// Mean delay measured on this link if known.
23    pub mean_delay: Option<Duration>,
24}
25
26/// Current estimate of various dataset members as generated by the filter.
27pub struct FilterEstimate {
28    /// Offset from the remote master, see also (IEEE 1588-2019 section 8.2.2.3)
29    pub offset_from_master: Duration,
30    /// Estimate of packet propagation delay, see also (IEEE 1588-2019 section
31    /// 8.2.2.4)
32    pub mean_delay: Duration,
33}
34
35/// A filter for post-processing time measurements.
36///
37/// Filters are responsible for dealing with the network noise, and should
38/// average out the input a bit so minor network variations are not immediately
39/// reflected in the synchronization of the clock.
40///
41/// This crate provides a simple [`BasicFilter`] which is
42/// suitable for most needs, but users can implement their own if desired.
43pub trait Filter {
44    /// Configuration for this [`Filter`]
45    ///
46    /// This is used to construct a new [`Filter`] instance using
47    /// [`new`](`Filter::new`).
48    type Config: Clone;
49
50    /// Create a new instance of the filter.
51    fn new(config: Self::Config) -> Self;
52
53    /// Put a new measurement in the filter.
54    /// The filter can then use this to adjust the clock
55    fn measurement<C: Clock>(&mut self, m: Measurement, clock: &mut C) -> FilterUpdate;
56
57    /// Update initiated through [FilterUpdate::next_update] timeout.
58    fn update<C: Clock>(&mut self, clock: &mut C) -> FilterUpdate;
59
60    /// Handle ending of time synchronization from the source
61    /// associated with this filter.
62    fn demobilize<C: Clock>(self, clock: &mut C);
63
64    /// Provide estimates for the Current dataset
65    /// mean delay and
66    fn current_estimates(&self) -> FilterEstimate;
67}