statime/port/
state.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use core::fmt::{Display, Formatter};

use crate::{
    datastructures::common::PortIdentity,
    time::{Duration, Time},
};

#[derive(Debug, Default)]
#[allow(private_interfaces)]
#[allow(clippy::large_enum_variant)]
pub(crate) enum PortState {
    #[default]
    Faulty,
    Listening,
    Master,
    Passive,
    Slave(SlaveState),
}

impl Display for PortState {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            PortState::Listening => write!(f, "Listening"),
            PortState::Master => write!(f, "Master"),
            PortState::Passive => write!(f, "Passive"),
            PortState::Slave(_) => write!(f, "Slave"),
            PortState::Faulty => write!(f, "Faulty"),
        }
    }
}

#[derive(Debug)]
pub(crate) struct SlaveState {
    pub(super) remote_master: PortIdentity,

    pub(super) sync_state: SyncState,
    pub(super) delay_state: DelayState,

    pub(super) last_raw_sync_offset: Option<Duration>,
}

impl SlaveState {
    pub(crate) fn remote_master(&self) -> PortIdentity {
        self.remote_master
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum SyncState {
    Empty,
    Measuring {
        id: u16,
        send_time: Option<Time>,
        recv_time: Option<Time>,
    },
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum DelayState {
    Empty,
    Measuring {
        id: u16,
        send_time: Option<Time>,
        recv_time: Option<Time>,
    },
}

impl SlaveState {
    pub(super) fn new(remote_master: PortIdentity) -> Self {
        SlaveState {
            remote_master,
            sync_state: SyncState::Empty,
            delay_state: DelayState::Empty,
            last_raw_sync_offset: None,
        }
    }
}