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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::nmt::states::NmtState;
use crate::frame::error::DllError;
use alloc::vec::Vec;
/// States for the Data Link Layer Cycle State Machine (DLL_CS) of a CN.
///
/// This machine tracks the expected sequence of frames within a single POWERLINK cycle.
/// (Reference: EPSG DS 301, Section 4.2.4.5.2)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DllCsState {
/// The isochronous communication is not active. Corresponds to `DLL_CS_NON_CYCLIC`.
#[default]
NonCyclic,
/// Waiting for the Start of Cycle (SoC) frame. Corresponds to `DLL_CS_WAIT_SOC`.
WaitSoc,
/// Waiting for a Poll Request (PReq) frame. Corresponds to `DLL_CS_WAIT_PREQ`.
WaitPreq,
/// Waiting for the Start of Asynchronous (SoA) frame. Corresponds to `DLL_CS_WAIT_SOA`.
WaitSoa,
}
/// Events that drive the DLL_CS, corresponding to received frames or timeouts.
/// (Reference: EPSG DS 301, Section 4.2.4.5.3)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DllCsEvent {
/// Corresponds to `DLL_CE_SOC`.
Soc,
/// Corresponds to `DLL_CE_PREQ`.
Preq,
/// Corresponds to `DLL_CE_PRES`.
Pres,
/// Corresponds to `DLL_CE_SOA`.
Soa,
/// Corresponds to `DLL_CE_ASND`.
Asnd,
/// Corresponds to `DLL_CE_SOC_TIMEOUT`.
SocTimeout,
}
/// Manages the DLL cycle state for a Controlled Node (CN).
pub struct DllCsStateMachine {
state: DllCsState,
}
impl DllCsStateMachine {
pub fn new() -> Self {
Self::default()
}
/// Processes an incoming event and transitions the state based on the current NMT state.
/// The logic follows the state diagram in Figure 30 of the specification.
pub fn process_event(&mut self, event: DllCsEvent, nmt_state: NmtState) -> Option<Vec<DllError>>{
let mut errors: Vec<DllError> = Vec::new();
// The DLL_CS is active only in specific NMT states.
match nmt_state {
NmtState::NmtPreOperational2 | NmtState::NmtReadyToOperate | NmtState::NmtOperational | NmtState::NmtCsStopped => {
let next_state = match (self.state, event) {
// --- (DLL_CT02) ---
// Process the PReq frame and send a PRes frame
(DllCsState::WaitPreq, DllCsEvent::Preq) => DllCsState::WaitSoa,
// --- (DLL_CT03) ---
// Process SoA, if allowed send an ASnd frame or a non POWERLINK frame
(DllCsState::WaitSoa, DllCsEvent::Soa) => DllCsState::WaitSoc,
// Accept the PReq frame and send a PRes frame, report error DLL_CEV_LOSS_SOC and DLL_CEV_LOSS_SOA
(DllCsState::WaitSoa, DllCsEvent::Preq) => {
errors.push(DllError::LossOfSoc);
errors.push(DllError::LossOfSoa);
DllCsState::WaitSoc
},
// Synchronise to the next SoC, report error DLL_CEV_LOSS_SOC and DLL_CEV_LOSS_SOA
(DllCsState::WaitSoa, DllCsEvent::SocTimeout) => {
errors.push(DllError::LossOfSoc);
errors.push(DllError::LossOfSoa);
DllCsState::WaitSoc
},
// --- (DLL_CT04) ---
// Process frame
(DllCsState::WaitSoc, DllCsEvent::Asnd) => DllCsState::WaitSoc,
// Respond with PRes frame, report error DLL_CEV_LOSS_SOC
(DllCsState::WaitSoc, DllCsEvent::Preq) => {
errors.push(DllError::LossOfSoc);
DllCsState::WaitSoc
},
// Report error DLL_CEV_LOSS_SOC
(DllCsState::WaitSoc, DllCsEvent::Pres) => {
errors.push(DllError::LossOfSoc);
DllCsState::WaitSoc
},
// Report error DLL_CEV_LOSS_SOC
(DllCsState::WaitSoc, DllCsEvent::Soa) => {
errors.push(DllError::LossOfSoc);
DllCsState::WaitSoc
},
// Report error DLL_CEV_LOSS_SOC
(DllCsState::WaitSoc, DllCsEvent::SocTimeout) => {
errors.push(DllError::LossOfSoc);
DllCsState::WaitSoc
},
// --- (DLL_CT07) ---
// Process PRes frames (cross traffic)
(DllCsState::WaitPreq, DllCsEvent::Pres) => DllCsState::WaitPreq,
// Report error DLL_CEV_LOSS_SOA
(DllCsState::WaitPreq, DllCsEvent::Asnd) => {
errors.push(DllError::LossOfSoa);
DllCsState::WaitPreq
},
// Synchronise to the cycle begin, report error DLL_CEV_LOSS_SOA
(DllCsState::WaitPreq, DllCsEvent::Soc) => {
errors.push(DllError::LossOfSoa);
DllCsState::WaitPreq
},
// --- (DLL_CT08) ---
// Process SoA, if invited, transmit a legal Ethernet frame
(DllCsState::WaitPreq, DllCsEvent::Soa) => {
errors.push(DllError::LossOfPreq);
DllCsState::WaitSoc
},
// Synchronise on the next SoC, report error DLL_CEV_LOSS_SOC and DLL_CEV_LOSS_SOA
(DllCsState::WaitPreq, DllCsEvent::SocTimeout) => {
errors.push(DllError::LossOfSoc);
errors.push(DllError::LossOfSoa);
DllCsState::WaitSoc
},
// --- (DLL_CT09) ---
// Synchronise on the SoC, report error DLL_CEV_LOSS_SOA
(DllCsState::WaitSoa, DllCsEvent::Soc) => {
errors.push(DllError::LossOfSoa);
DllCsState::WaitPreq
},
// --- (DLL_CT10) ---
// Process PRes frames (cross traffic)
(DllCsState::WaitSoa, DllCsEvent::Pres) => DllCsState::WaitSoa,
// Report error DLL_CEV_LOSS_SOA
(DllCsState::WaitSoa, DllCsEvent::Asnd) => {
errors.push(DllError::LossOfSoa);
DllCsState::WaitSoa
},
// --- (DLL_CT01) ---
// A SoC can be received in any state and always resets the cycle to WaitPReq.
// Synchronise the start of cycle and generate a SoC trigger to the application
(_, DllCsEvent::Soc) => DllCsState::WaitPreq,
// If an unexpected event occurs, remain in the current state.
// Error reporting would be triggered here.
(current, _) => {
errors.push(DllError::UnexpectedEventInState { state: current as u8, event: event as u8 });
current
},
};
self.state = next_state;
},
_ => {
// In all other NMT states, the Dll state machine is considered non-cyclic.
self.state = DllCsState::NonCyclic;
}
}
if errors.is_empty() {
None
} else {
Some(errors)
}
}
/// Returns the current state of the DLL state machine.
pub fn current_state(&self) -> DllCsState {
self.state
}
}
impl Default for DllCsStateMachine {
fn default() -> Self {
Self { state: DllCsState::NonCyclic }
}
}
#[cfg(test)]
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn test_happy_path() {
let mut sm = DllCsStateMachine::new();
let op_state = NmtState::NmtOperational;
assert_eq!(sm.current_state(), DllCsState::NonCyclic);
assert!(sm.process_event(DllCsEvent::Soc, op_state).is_none());
assert_eq!(sm.current_state(), DllCsState::WaitPreq);
assert!(sm.process_event(DllCsEvent::Preq, op_state).is_none());
assert_eq!(sm.current_state(), DllCsState::WaitSoa);
assert!(sm.process_event(DllCsEvent::Soa, op_state).is_none());
assert_eq!(sm.current_state(), DllCsState::WaitSoc);
}
#[test]
fn test_lost_preq() {
let mut sm = DllCsStateMachine::new();
let op_state = NmtState::NmtOperational;
sm.process_event(DllCsEvent::Soc, op_state); // -> WaitPreq
let errors = sm.process_event(DllCsEvent::Soa, op_state);
assert_eq!(errors, Some(vec![DllError::LossOfPreq]));
assert_eq!(sm.current_state(), DllCsState::WaitSoc);
}
#[test]
fn test_lost_soa_and_soc() {
let mut sm = DllCsStateMachine::new();
let op_state = NmtState::NmtOperational;
sm.process_event(DllCsEvent::Soc, op_state);
sm.process_event(DllCsEvent::Preq, op_state);
sm.process_event(DllCsEvent::Soa, op_state); // -> WaitSoc
let errors = sm.process_event(DllCsEvent::Preq, op_state);
assert_eq!(errors,Some(vec![DllError::LossOfSoc]));
assert_eq!(sm.current_state(), DllCsState::WaitSoc);
}
#[test]
fn test_soc_timeout() {
let mut sm = DllCsStateMachine::new();
let op_state = NmtState::NmtOperational;
sm.process_event(DllCsEvent::Soc, op_state); // -> WaitPreq
let errors = sm.process_event(DllCsEvent::SocTimeout, op_state);
assert_eq!(errors, Some(vec![DllError::LossOfSoc, DllError::LossOfSoa]));
assert_eq!(sm.current_state(), DllCsState::WaitSoc);
}
}