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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
use crate::{frame::error::DllError, nmt::states::NmtState, NodeId};
use alloc::vec::Vec;
/// States for the Data Link Layer Cycle State Machine (DLL_MS) of a MN.
/// (Reference: EPSG DS 301, Section 4.2.4.6.2)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum DllMsState {
/// The cyclic communication is not active. Corresponds to `DLL_MS_NON_CYCLIC`.
#[default]
NonCyclic,
/// Waits for the next cycle to begin. Corresponds to `DLL_MS_WAIT_SOC_TRIG`.
WaitSocTrig,
/// Waiting for a Poll Response (PRes) frame. Corresponds to `DLL_MS_WAIT_PRES`.
WaitPres,
/// Waits for the asynchronous phase to end. Corresponds to `DLL_MS_WAIT_ASND`.
WaitAsnd,
/// Waits for the asynchronous phase timeout or any Ethernet frame. Corresponds to `DLL_MS_WAIT_SOA`.
WaitSoa,
}
/// Events that drive the DLL_MS, corresponding to internal triggers or received frames.
/// (Reference: EPSG DS 301, Section 4.2.4.6.3)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DllMsEvent {
/// A PRes frame was received. Corresponds to `DLL_ME_PRES`.
Pres,
/// A PRes frame was not received in time. Corresponds to `DLL_ME_PRES_TIMEOUT`.
PresTimeout,
/// An ASnd frame or a non-POWERLINK frame was received. Corresponds to `DLL_ME_ASND`.
Asnd,
/// An ASnd frame was not received in time. Corresponds to `DLL_ME_ASND_TIMEOUT`.
AsndTimeout,
/// Triggers the emission of an SoC frame and starts a new cycle. Corresponds to `DLL_ME_SOC_TRIG`.
SocTrig,
/// Triggers a new reduced POWERLINK cycle. Corresponds to `DLL_ME_SOA_TRIG`.
SoaTrig,
}
/// Manages the DLL cycle state for a Managing Node (MN).
pub struct DllMsStateMachine {
state: DllMsState,
}
impl DllMsStateMachine {
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 diagrams in Figure 31 and 32 of the specification.
pub fn process_event(
&mut self, event: DllMsEvent, nmt_state: NmtState, response_expected: bool,
async_in: bool, async_out: bool, isochr: bool, isochr_out: bool, dest_node_id: NodeId
) -> Option<Vec<DllError>> {
let mut errors : Vec<DllError> = Vec::new();
match nmt_state {
NmtState::NmtPreOperational1 => {
let next_state = match (self.state, event) {
(DllMsState::WaitSoa, DllMsEvent::SoaTrig) => {
match (async_in, response_expected, async_out) {
// --- DLL_MT10 ---
// Send SoA with Invite
(true, false, _) => DllMsState::WaitSoa,
// Send SoA with Invite to MN and send ASnd or non POWERLINK frame
(false, _, true) => DllMsState::WaitSoa,
// Send SoA
(false, _, false) => DllMsState::WaitSoa,
// --- DLL_MT11 ---
// Send SoA with Invite
(true, true, _) => DllMsState::WaitAsnd,
}
},
(DllMsState::WaitAsnd, e @ DllMsEvent::AsndTimeout | e @ DllMsEvent::SoaTrig) => {
if e == DllMsEvent::AsndTimeout {
errors.push(DllError::MevAsndTimeout);
}
match (async_in, response_expected) {
// --- DLL_MT12 ---
// Send SoA, ASnd if available
(false, _) => DllMsState::WaitSoa,
// Send SoA with Invite
(true, false) => DllMsState::WaitSoa,
// --- DLL_MT13 ---
// Send SoA with Invite, report error DLL_MEV_ASND_TIMEOUT
(true, true) => {
errors.push(DllError::MevAsndTimeout);
DllMsState::WaitAsnd
},
}
},
// If an unexpected event occurs, remain in the current state.
(current, _) => {
errors.push(DllError::UnexpectedEventInState { state: current as u8, event: event as u8 });
current
},
};
self.state = next_state;
},
NmtState::NmtOperational | NmtState::NmtReadyToOperate | NmtState::NmtPreOperational2 => {
let next_state = match (self.state, event) {
(DllMsState::WaitSocTrig, DllMsEvent::SocTrig) => {
match(isochr, async_in, isochr_out, async_out) {
// --- DLL_MT1 ---
// Send SoC, PReq
(true, _, _, _) => DllMsState::WaitPres,
// --- DLL_MT6 ---
// Send SoC, PRes and SoA with Invite
(false, true, true, _) => DllMsState::WaitAsnd,
// Send SoC and SoA with Invite
(false, true, _, _) => DllMsState::WaitAsnd,
// --- DLL_MT7 ---
// Send SoC, PRes
(false, false, true, _) => DllMsState::WaitSocTrig,
// Send SoC, SoA and ASnd
(false, false, _, true) => DllMsState::WaitSocTrig,
// Send SoC
(false, false, _, _) => DllMsState::WaitSocTrig,
}
},
(DllMsState::WaitPres, e @ DllMsEvent::Pres | e @ DllMsEvent::PresTimeout) => {
if e == DllMsEvent::PresTimeout {
errors.push(DllError::LossOfPres { node_id: dest_node_id });
}
match(isochr, async_in, isochr_out, async_out) {
// --- DLL_MT2 --- Send next PReq
(true, _, _, _) => DllMsState::WaitPres,
// --- DLL_MT3 ---
// Send PRes and SoA
(false, false, true, _) => DllMsState::WaitSocTrig,
// Send PRes and ASnd
(false, false, _, true) => DllMsState::WaitSocTrig,
// Send PRes
(false, false, _, _) => DllMsState::WaitSocTrig,
// --- DLL_MT4 ---
// Send PRes and SoA with Invite
(false, true, true, _) => DllMsState::WaitAsnd,
// Send PRes
(false, true, _, _) => DllMsState::WaitAsnd,
}
},
(DllMsState::WaitAsnd, DllMsEvent::SocTrig) => {
match(isochr, async_in, isochr_out, async_out) {
// --- DLL_MT5 ---
// Send SoC and PRes
(false, false, true, _) => DllMsState::WaitSocTrig,
// Send SoC and SoA and ASnd
(false, false, _, true) => DllMsState::WaitSocTrig,
// Send SoC
(false, false, _, _) => DllMsState::WaitSocTrig,
// --- DLL_MT8 ---
// Send SoC and SoA with Invite
(false, true, _, _) => DllMsState::WaitAsnd,
// --- DLL_MT9 ---
// Send SoC and PReq
(true, _, _, _) => DllMsState::WaitPres,
}
},
// --- DLL_MT8 --- Process the frame
(DllMsState::WaitAsnd, DllMsEvent::Asnd) => DllMsState::WaitAsnd,
// --- DLL_MT0 --- Initial transition from NMT
(DllMsState::NonCyclic, DllMsEvent::SocTrig) => DllMsState::WaitSocTrig,
(current, _) => {
errors.push(DllError::UnexpectedEventInState { state: current as u8, event: event as u8 });
current
},
};
self.state = next_state;
},
_ => {
self.state = DllMsState::NonCyclic;
}
}
if errors.is_empty() {
None
} else {
Some(errors)
}
}
/// Returns the current state of the DLL state machine.
pub fn current_state(&self) -> DllMsState {
self.state
}
}
impl Default for DllMsStateMachine {
fn default() -> Self {
Self { state: DllMsState::NonCyclic }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dll_ms_pre_operational_1_cycle() {
let mut sm = DllMsStateMachine::new();
let preop1_state = NmtState::NmtPreOperational1;
// In PreOp1, the state machine should start in a state ready to send SoA.
// For this test, we'll assume it's already in WAIT_SOA.
sm.state = DllMsState::WaitSoa;
// Event: Trigger SoA, expect a response from a CN.
// isochr and async_out are false. async_in and response_expected are true.
sm.process_event(
DllMsEvent::SoaTrig, preop1_state, true, true,
false, false, false, NodeId(1),
);
assert_eq!(sm.current_state(), DllMsState::WaitAsnd);
// Event: Timeout waiting for ASnd. The MN should re-issue an SoA invite.
// Since the next action is to re-invite, response_expected is true.
sm.process_event(
DllMsEvent::AsndTimeout, preop1_state, true,
false, false, false, false, NodeId(1),
);
assert_eq!(sm.current_state(), DllMsState::WaitSoa);
}
#[test]
fn test_dll_ms_operational_happy_path() {
let mut sm = DllMsStateMachine::new();
let operational_state = NmtState::NmtOperational;
// The machine starts in NonCyclic. An NMT state change would trigger the first SOC.
assert_eq!(sm.current_state(), DllMsState::NonCyclic);
// (DLL_MT0) NMT signals the start of the cyclic phase.
sm.process_event(DllMsEvent::SocTrig, operational_state, false, false, false, false, false, NodeId(1),);
assert_eq!(sm.current_state(), DllMsState::WaitSocTrig);
// Event: A new cycle begins. MN sends SoC and first PReq.
// isochr is true, indicating there are isochronous frames to send.
sm.process_event(DllMsEvent::SocTrig, operational_state, false, false, false, true, false, NodeId(1), );
assert_eq!(sm.current_state(), DllMsState::WaitPres);
// Event: MN receives a PRes, sends the next PReq.
// isochr is still true.
sm.process_event(DllMsEvent::Pres, operational_state, false, false, false, true, false, NodeId(1), );
assert_eq!(sm.current_state(), DllMsState::WaitPres);
// Event: MN receives the last PRes. Isochronous phase is over. No async phase.
// isochr is now false. async_in is false.
sm.process_event(DllMsEvent::Pres, operational_state, false, false, false, false, false, NodeId(1), );
assert_eq!(sm.current_state(), DllMsState::WaitSocTrig);
}
}