profirust 0.6.0

PROFIBUS-DP compatible communication stack in pure Rust
Documentation
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/// Status of the `LAS` (List of Active Stations)
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum LasState {
    /// We are waiting for the first token rotation to then enter discovery
    Uninitialized,
    /// We are listening to the first token rotation to discover all active stations.
    Discovery,
    /// We are listening to the second token rotation to verify correctness of the LAS.
    Verification,
    /// The LAS has been verified and is valid.  It will be updated live from now on.
    Valid,
}

impl LasState {
    /// Whether the LAS is valid.
    ///
    /// Returns `true` when `self == LasState::Valid`
    #[inline(always)]
    pub fn is_valid(self) -> bool {
        matches!(self, Self::Valid)
    }
}

/// Management of the token ring from the station's point of view
#[derive(Clone, PartialEq, Eq)]
pub struct TokenRing {
    /// `LAS` (List of Active Stations)
    active_stations: bitvec::BitArr!(for 128),

    /// Status of the `LAS`
    las_state: LasState,

    /// `TS` (This Station)
    this_station: crate::Address,

    /// `NS` (Next Station)
    ///
    /// The `next_station` is who we will forward the token to once we release it.
    ///
    /// There is always a `next_station`.  When no other active stations are known, we are our own
    /// `next_station`, so NS==TS.
    next_station: crate::Address,

    /// `PS` (Previous Station)
    ///
    /// The `previous_station` is who we will receive the token from.  At first, only a token from
    /// `previous_station` is accepted unless a station makes it clear that it is our new
    /// `previous_station` by passing the token to us a second time.
    ///
    /// There is always a `previous_station`.  When no other active stations are known, we are our
    /// own `previous_station`, so PS==TS.
    previous_station: crate::Address,
}

impl TokenRing {
    pub fn new(param: &crate::fdl::Parameters) -> Self {
        let mut active_stations = bitvec::array::BitArray::ZERO;
        // Mark ourselves in the list of active stations.
        active_stations.set(usize::from(param.address), true);

        Self {
            active_stations,
            las_state: LasState::Uninitialized,
            this_station: param.address,
            next_station: param.address,
            previous_station: param.address,
        }
    }

    pub fn iter_active_stations(
        &self,
    ) -> impl Iterator<Item = crate::Address> + DoubleEndedIterator + '_ {
        self.active_stations
            .iter_ones()
            .map(|a| u8::try_from(a).unwrap())
    }

    pub fn ready_for_ring(&self) -> bool {
        self.las_state.is_valid()
    }

    pub fn this_station(&self) -> crate::Address {
        self.this_station
    }

    pub fn next_station(&self) -> crate::Address {
        self.next_station
    }

    pub fn previous_station(&self) -> crate::Address {
        self.previous_station
    }

    fn verify_las_from_token_pass(&mut self, sa: crate::Address, da: crate::Address) -> bool {
        // SA station must be active
        if !self.active_stations[usize::from(sa)] {
            return false;
        }

        // DA station must be active
        if !self.active_stations[usize::from(da)] {
            return false;
        }

        // No stations between SA and DA must be active
        if da > sa {
            if self.active_stations[usize::from(sa + 1)..usize::from(da)].any() {
                return false;
            }
        } else {
            // Handle wrap-around
            if self.active_stations[usize::from(sa + 1)..].any() {
                return false;
            }
            if self.active_stations[..usize::from(da)].any() {
                return false;
            }
        }

        true
    }

    fn update_las_from_token_pass(&mut self, sa: crate::Address, da: crate::Address) {
        // Clear the GAP from this token pass as it does not contain any known active stations.
        if da > sa {
            self.active_stations[usize::from(sa)..usize::from(da)].fill(false);
        } else {
            self.active_stations[usize::from(sa)..].fill(false);
            self.active_stations[..usize::from(da)].fill(false);
        }

        // At this point, we only know that the source address is alive so we only enter it into
        // the LAS.  The destination will be added later, when it actively forwards the token
        // itself.
        self.active_stations.set(usize::from(sa), true);

        self.update_next_previous();
    }

    fn update_next_previous(&mut self) {
        let next_station =
            if let Some(next) = self.iter_active_stations().find(|a| *a > self.this_station) {
                next
            } else if let Some(first) = self.iter_active_stations().next() {
                first
            } else {
                self.this_station
            };

        let previous_station = if let Some(previous) = self
            .iter_active_stations()
            .rev()
            .find(|a| *a < self.this_station)
        {
            previous
        } else if let Some(last) = self.iter_active_stations().rev().next() {
            last
        } else {
            self.this_station
        };

        if self.next_station != next_station {
            log::trace!("New NS is #{next_station}");
        }
        if self.previous_station != previous_station {
            log::trace!("New PS is #{previous_station}");
        }

        self.next_station = next_station;
        self.previous_station = previous_station;
    }

    pub fn witness_token_pass(&mut self, sa: crate::Address, da: crate::Address) {
        if sa > 125 {
            log::warn!("Witnessed token pass from invalid address #{sa}->#{da}, ignoring.");
            return;
        }
        if da > 125 {
            log::warn!("Witnessed token pass to invalid address #{da}<-#{sa}, ignoring.");
            return;
        }

        match self.las_state {
            // If we see the wrap-around, start discovery
            LasState::Uninitialized => {
                if da <= sa {
                    self.las_state = LasState::Discovery;
                    log::trace!("Starting discovery of active stations...");
                }
            }
            LasState::Discovery => {
                self.update_las_from_token_pass(sa, da);
                if da <= sa {
                    self.las_state = LasState::Verification;
                    log::trace!("Starting verification of active stations list...");
                }
            }
            LasState::Verification => {
                // If verification fails, restart discovery
                if !self.verify_las_from_token_pass(sa, da) {
                    self.update_las_from_token_pass(sa, da);
                    self.las_state = LasState::Discovery;
                    log::trace!("Rediscovering active stations due to a change...");
                } else if da <= sa {
                    self.las_state = LasState::Valid;
                    log::trace!("List of active stations is complete!");
                }
            }
            LasState::Valid => {
                self.update_las_from_token_pass(sa, da);
            }
        }
    }

    pub fn claim_token(&mut self) {
        if self.las_state != LasState::Valid {
            log::trace!("Declaring list of active stations valid due to claiming the token.");
        }
        self.las_state = LasState::Valid;
    }

    pub fn set_next_station(&mut self, address: crate::Address) {
        self.active_stations.set(usize::from(address), true);
        self.update_las_from_token_pass(self.this_station, address);
    }

    pub fn remove_station(&mut self, address: crate::Address) {
        self.active_stations.set(usize::from(address), false);
        self.update_next_previous();
    }
}

impl core::fmt::Debug for TokenRing {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let mut active_stations = [0u8; 127];
        let num_stations = self.iter_active_stations().count();
        for (i, addr) in self.iter_active_stations().enumerate() {
            active_stations[i] = addr;
        }
        f.debug_struct("TokenRing")
            .field("previous_station", &self.previous_station)
            .field("this_station", &self.this_station)
            .field("next_station", &self.next_station)
            .field("las_state", &self.las_state)
            .field("active_stations", &&active_stations[..num_stations])
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    #[test]
    fn test_token_ring_add_some_stations() {
        let mut token_ring = TokenRing::new(&Default::default());

        token_ring.witness_token_pass(29, 3);
        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 29);

        dbg!(&token_ring);
    }

    #[test]
    fn test_las_initialization_bad_verification() {
        let mut token_ring = TokenRing::new(&crate::fdl::Parameters {
            address: 7,
            ..Default::default()
        });

        assert_eq!(token_ring.las_state, LasState::Uninitialized);

        token_ring.witness_token_pass(15, 29);
        token_ring.witness_token_pass(29, 3);

        assert_eq!(token_ring.las_state, LasState::Discovery);

        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 29);
        token_ring.witness_token_pass(29, 3);

        assert_eq!(token_ring.las_state, LasState::Verification);

        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 18);
        token_ring.witness_token_pass(18, 29);

        assert_eq!(token_ring.las_state, LasState::Discovery);

        token_ring.witness_token_pass(29, 3);

        assert_eq!(token_ring.las_state, LasState::Verification);

        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 29);

        assert_eq!(token_ring.las_state, LasState::Discovery);

        token_ring.witness_token_pass(29, 3);

        assert_eq!(token_ring.las_state, LasState::Verification);

        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 29);
        token_ring.witness_token_pass(29, 3);

        assert_eq!(token_ring.las_state, LasState::Valid);
        assert!(token_ring.ready_for_ring());
        assert_eq!(token_ring.next_station(), 15);
        assert_eq!(token_ring.previous_station(), 3);
    }

    #[test]
    fn next_station_correct_after_removal() {
        let mut token_ring = TokenRing::new(&Default::default());

        token_ring.witness_token_pass(29, 3);
        token_ring.witness_token_pass(3, 15);
        token_ring.witness_token_pass(15, 29);

        assert_eq!(token_ring.next_station(), 3);

        token_ring.remove_station(3);

        assert_eq!(token_ring.next_station(), 15);
    }

    #[test]
    fn token_rink_eats_bad_addresses() {
        crate::test_utils::prepare_test_logger_with_warnings(vec![
            "Witnessed token pass from invalid address #223->#7, ignoring.",
            "Witnessed token pass to invalid address #223<-#7, ignoring.",
        ]);
        let mut token_ring = TokenRing::new(&Default::default());

        token_ring.witness_token_pass(223, 7);
        token_ring.witness_token_pass(7, 223);
    }

    proptest! {
        #[test]
        fn test_las_update_correctness(
            previous in prop::collection::vec(any::<bool>(), 126),
            da in 0..126u8,
            sa in 0..126u8,
        ) {
            let mut token_ring = TokenRing::new(&crate::fdl::Parameters {
                address: 7,
                ..Default::default()
            });
            for (mut station, state) in token_ring.active_stations.iter_mut().zip(previous.iter()) {
                *station = *state;
            }

            let current = token_ring.active_stations.clone();
            let verify = token_ring.verify_las_from_token_pass(sa, da);

            token_ring.update_las_from_token_pass(sa, da);

            if !verify {
                // Also put the destination address into the LAS to make verification happy
                token_ring.active_stations.set(usize::from(da), true);
            }

            if verify {
                assert_eq!(token_ring.active_stations, current);
            }

            assert!(token_ring.verify_las_from_token_pass(sa, da));

            let current = token_ring.active_stations.clone();
            token_ring.update_las_from_token_pass(sa, da);
            assert_eq!(token_ring.active_stations, current);
        }

        #[test]
        fn test_las_initialization_happy_path(
            mut active_stations in prop::collection::vec(0..126u8, 1..16),
        ) {
            let mut token_ring = TokenRing::new(&crate::fdl::Parameters {
                address: 7,
                ..Default::default()
            });

            active_stations.sort();
            active_stations.dedup();

            assert_eq!(token_ring.las_state, LasState::Uninitialized);

            for addresses in active_stations.windows(2) {
                let prev = addresses[0];
                let next = addresses[1];
                token_ring.witness_token_pass(prev, next);
            }
            // Wrap-around
            token_ring.witness_token_pass(active_stations[active_stations.len() - 1], active_stations[0]);

            assert_eq!(token_ring.las_state, LasState::Discovery);

            for addresses in active_stations.windows(2) {
                let prev = addresses[0];
                let next = addresses[1];
                token_ring.witness_token_pass(prev, next);
            }
            // Wrap-around
            token_ring.witness_token_pass(active_stations[active_stations.len() - 1], active_stations[0]);

            assert_eq!(token_ring.las_state, LasState::Verification);

            for addresses in active_stations.windows(2) {
                let prev = addresses[0];
                let next = addresses[1];
                token_ring.witness_token_pass(prev, next);
            }
            // Wrap-around
            token_ring.witness_token_pass(active_stations[active_stations.len() - 1], active_stations[0]);

            assert_eq!(token_ring.las_state, LasState::Valid);
            assert!(token_ring.ready_for_ring());

            let known_stations = token_ring.iter_active_stations().collect::<Vec<_>>();
            assert_eq!(active_stations, known_stations);

            let next = active_stations.iter().copied().find(|a| *a > 7).or_else(|| active_stations.iter().copied().next()).unwrap();
            assert_eq!(token_ring.next_station(), next);

            let previous = active_stations.iter().rev().copied().find(|a| *a < 7).or_else(|| active_stations.iter().rev().copied().next()).unwrap();
            assert_eq!(token_ring.previous_station(), previous);
        }
    }
}