use alloc::string::String;
use alloc::vec::Vec;
use super::apdu::{AsduView, SavPduView, SmpSynch};
use super::layout::{Channel, ChannelValue, SampleLayout};
use crate::common::{Error, EventQueue, Instant, Limits};
use crate::proto::ethernet::{ETHERTYPE_SV, Frame, FrameAddress, MacAddr};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamKey {
pub dst: MacAddr,
pub appid: u16,
pub sv_id: String,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SimulationMode {
#[default]
Off,
Preferred,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StreamConfig {
pub key: StreamKey,
pub samples_per_second: u32,
pub expected_conf_rev: Option<u32>,
pub stale_after_ms: u32,
pub simulation: SimulationMode,
pub layout: Option<SampleLayout>,
}
impl StreamConfig {
pub fn new(key: StreamKey) -> Self {
StreamConfig { key, samples_per_second: 4000, expected_conf_rev: None, stale_after_ms: 10, simulation: SimulationMode::Off, layout: None }
}
#[must_use]
pub fn with_samples_per_second(mut self, samples: u32) -> Self {
self.samples_per_second = samples;
self
}
#[must_use]
pub fn with_conf_rev(mut self, conf_rev: u32) -> Self {
self.expected_conf_rev = Some(conf_rev);
self
}
#[must_use]
pub fn with_stale_after_ms(mut self, ms: u32) -> Self {
self.stale_after_ms = ms;
self
}
#[must_use]
pub fn with_simulation(mut self, mode: SimulationMode) -> Self {
self.simulation = mode;
self
}
#[must_use]
pub fn with_layout(mut self, layout: SampleLayout) -> Self {
self.layout = Some(layout);
self
}
const fn wrap(&self) -> u32 {
super::smp_cnt_wrap(self.samples_per_second)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub struct StreamState {
pub last_smp_cnt: Option<u16>,
pub conf_rev: Option<u32>,
pub smp_synch: Option<SmpSynch>,
pub gm_identity: Option<[u8; 8]>,
pub frames: u64,
pub asdus: u64,
pub gaps: u64,
pub samples_lost: u64,
pub conf_rev_dropped: u64,
pub simulation_dropped: u64,
pub layout_mismatches: u64,
pub simulation_active: bool,
pub stale: bool,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum SubscriberEvent {
Gap {
stream: usize,
expected: u16,
received: u16,
lost: u32,
},
SyncChanged {
stream: usize,
from: Option<SmpSynch>,
to: Option<SmpSynch>,
},
GrandmasterChanged {
stream: usize,
to: Option<[u8; 8]>,
},
ConfRevMismatch {
stream: usize,
received: u32,
},
SampleLengthMismatch {
stream: usize,
expected: usize,
received: usize,
},
IgnoredSimulation {
stream: usize,
},
SimulationTakeover {
stream: usize,
},
Stale {
stream: usize,
},
Resumed {
stream: usize,
},
Malformed(Error),
}
#[derive(Clone, Copy, Debug)]
pub struct Sample<'a> {
pub stream: usize,
pub asdu: AsduView<'a>,
pub simulation: bool,
pub layout: Option<&'a SampleLayout>,
}
impl<'a> Sample<'a> {
pub fn channels(&self) -> impl Iterator<Item = (&'a Channel, ChannelValue)> + 'a {
let sample = self.asdu.sample;
self.layout.into_iter().flat_map(move |l| l.decode(sample))
}
pub fn channel(&self, i: usize) -> Option<ChannelValue> {
self.layout?.value(self.asdu.sample, i)
}
}
#[derive(Debug)]
pub struct Subscriber {
streams: Vec<Stream>,
limits: Limits,
events: EventQueue<SubscriberEvent>,
other_stream: u64,
malformed: u64,
}
#[derive(Debug)]
struct Stream {
cfg: StreamConfig,
state: StreamState,
deadline: Option<Instant>,
conf_rev_reported: bool,
simulation_reported: bool,
layout_reported: bool,
}
impl Subscriber {
pub fn new(streams: Vec<StreamConfig>) -> Subscriber {
Subscriber {
streams: streams
.into_iter()
.map(|cfg| Stream {
cfg,
state: StreamState::default(),
deadline: None,
conf_rev_reported: false,
simulation_reported: false,
layout_reported: false,
})
.collect(),
limits: Limits::DEFAULT,
events: EventQueue::new(64),
other_stream: 0,
malformed: 0,
}
}
#[must_use]
pub const fn with_limits(mut self, limits: Limits) -> Self {
self.limits = limits;
self
}
#[must_use]
pub fn with_event_capacity(mut self, capacity: usize) -> Self {
self.events = EventQueue::new(capacity);
self
}
pub fn state(&self, i: usize) -> Option<&StreamState> {
self.streams.get(i).map(|s| &s.state)
}
pub fn stream_config(&self, i: usize) -> Option<&StreamConfig> {
self.streams.get(i).map(|s| &s.cfg)
}
pub fn stream_count(&self) -> usize {
self.streams.len()
}
pub const fn other_stream(&self) -> u64 {
self.other_stream
}
pub const fn malformed(&self) -> u64 {
self.malformed
}
pub const fn events_dropped(&self) -> u64 {
self.events.dropped()
}
pub fn reset_simulation(&mut self, i: usize) {
if let Some(s) = self.streams.get_mut(i) {
s.state.simulation_active = false;
s.state.last_smp_cnt = None;
}
}
pub fn on_frame<F: FnMut(Sample<'_>)>(&mut self, now: Instant, frame: &[u8], mut consumer: F) {
self.check_stale(now);
let fr = match Frame::parse(frame) {
Ok(f) => f,
Err(e) => {
return match FrameAddress::peek(frame) {
Some(a) if a.ethertype == ETHERTYPE_SV && self.addressed(a.dst, a.appid) => self.on_malformed(e),
_ => self.other(),
};
}
};
if fr.ethertype != ETHERTYPE_SV || !self.addressed(fr.dst, fr.appid) {
return self.other();
}
let pdu = match SavPduView::parse(fr.apdu, &self.limits) {
Ok(p) => p,
Err(e) => return self.on_malformed(e),
};
let simulated = fr.simulation();
let mut matched = false;
let mut counted: Option<usize> = None;
for asdu in pdu.asdus() {
let asdu = match asdu {
Ok(a) => a,
Err(e) => return self.on_malformed(e),
};
let Some(i) = self.streams.iter().position(|s| s.cfg.key.dst == fr.dst && s.cfg.key.appid == fr.appid && s.cfg.key.sv_id == asdu.sv_id) else {
continue;
};
matched = true;
if !self.simulation_policy_admits(i, simulated) || !self.accept(i, now, &asdu) {
continue;
}
if counted != Some(i) {
counted = Some(i);
if let Some(s) = self.streams.get_mut(i) {
s.state.frames = s.state.frames.saturating_add(1);
}
}
consumer(Sample { stream: i, asdu, simulation: simulated, layout: self.streams.get(i).and_then(|s| s.cfg.layout.as_ref()) });
}
if !matched {
self.other();
}
}
pub fn on_timeout(&mut self, now: Instant) {
self.check_stale(now);
}
fn check_stale(&mut self, now: Instant) {
for i in 0..self.streams.len() {
let due = match self.streams.get(i) {
Some(s) => matches!(s.deadline, Some(d) if now >= d) && !s.state.stale,
None => false,
};
if due {
if let Some(s) = self.streams.get_mut(i) {
s.state.stale = true;
s.deadline = None;
}
self.events.push(SubscriberEvent::Stale { stream: i });
}
}
}
pub fn next_timeout(&self) -> Option<Instant> {
self.streams.iter().filter_map(|s| s.deadline).min()
}
pub fn poll_event(&mut self) -> Option<SubscriberEvent> {
self.events.pop()
}
fn simulation_policy_admits(&mut self, i: usize, simulated: bool) -> bool {
let Subscriber { streams, events, .. } = self;
let Some(s) = streams.get_mut(i) else { return false };
match (s.cfg.simulation, simulated) {
(SimulationMode::Off, false) => {
s.simulation_reported = false;
true
}
(SimulationMode::Off, true) => {
s.state.simulation_dropped = s.state.simulation_dropped.saturating_add(1);
if !s.simulation_reported {
s.simulation_reported = true;
events.push(SubscriberEvent::IgnoredSimulation { stream: i });
}
false
}
(SimulationMode::Preferred, true) => {
if !s.state.simulation_active {
s.state.simulation_active = true;
s.state.last_smp_cnt = None;
events.push(SubscriberEvent::SimulationTakeover { stream: i });
}
true
}
(SimulationMode::Preferred, false) => {
if s.state.simulation_active {
s.state.simulation_dropped = s.state.simulation_dropped.saturating_add(1);
return false;
}
true
}
}
}
fn accept(&mut self, i: usize, now: Instant, asdu: &AsduView<'_>) -> bool {
let Subscriber { streams, events, .. } = self;
let Some(s) = streams.get_mut(i) else { return false };
if let Some(expected) = s.cfg.expected_conf_rev {
if expected != asdu.conf_rev {
s.state.conf_rev_dropped = s.state.conf_rev_dropped.saturating_add(1);
if !s.conf_rev_reported {
s.conf_rev_reported = true;
events.push(SubscriberEvent::ConfRevMismatch { stream: i, received: asdu.conf_rev });
}
return false;
}
s.conf_rev_reported = false;
}
if let Some(layout) = s.cfg.layout.as_ref() {
if !layout.fits(asdu.sample.len()) {
s.state.layout_mismatches = s.state.layout_mismatches.saturating_add(1);
if !s.layout_reported {
s.layout_reported = true;
events.push(SubscriberEvent::SampleLengthMismatch { stream: i, expected: layout.len(), received: asdu.sample.len() });
}
return false;
}
s.layout_reported = false;
}
if let Some(last) = s.state.last_smp_cnt {
let wrap = s.cfg.wrap();
let expected = ((u32::from(last) + 1) % wrap) as u16;
if asdu.smp_cnt != expected {
let lost = (u32::from(asdu.smp_cnt) + wrap - u32::from(expected)) % wrap;
s.state.gaps = s.state.gaps.saturating_add(1);
s.state.samples_lost = s.state.samples_lost.saturating_add(u64::from(lost));
events.push(SubscriberEvent::Gap { stream: i, expected, received: asdu.smp_cnt, lost });
}
}
s.state.conf_rev = Some(asdu.conf_rev);
if s.state.smp_synch != asdu.smp_synch {
events.push(SubscriberEvent::SyncChanged { stream: i, from: s.state.smp_synch, to: asdu.smp_synch });
s.state.smp_synch = asdu.smp_synch;
}
let gm = asdu.gm_identity.and_then(|g| <[u8; 8]>::try_from(g).ok());
if gm != s.state.gm_identity {
s.state.gm_identity = gm;
events.push(SubscriberEvent::GrandmasterChanged { stream: i, to: gm });
}
if s.state.stale {
s.state.stale = false;
events.push(SubscriberEvent::Resumed { stream: i });
}
s.state.last_smp_cnt = Some(asdu.smp_cnt);
s.state.asdus = s.state.asdus.saturating_add(1);
s.deadline = Some(now.plus_millis(u64::from(s.cfg.stale_after_ms)));
true
}
fn addressed(&self, dst: MacAddr, appid: u16) -> bool {
self.streams.iter().any(|s| s.cfg.key.dst == dst && s.cfg.key.appid == appid)
}
fn other(&mut self) {
self.other_stream = self.other_stream.saturating_add(1);
}
fn on_malformed(&mut self, e: Error) {
self.malformed = self.malformed.saturating_add(1);
self.events.push(SubscriberEvent::Malformed(e));
}
}
#[cfg(test)]
mod tests {
use alloc::vec;
use super::*;
use crate::proto::ethernet::{FrameHeader, RESERVED1_SIMULATION};
use crate::proto::sv::{Asdu, SavPdu};
fn asdu(sv_id: &str, cnt: u16, synch: SmpSynch) -> Asdu {
Asdu {
sv_id: sv_id.into(),
dat_set: None,
smp_cnt: cnt,
conf_rev: 1,
refr_tm: None,
smp_synch: Some(synch),
smp_rate: None,
sample: vec![0; 64],
smp_mod: None,
gm_identity: None,
}
}
fn frame_of(asdus: Vec<Asdu>, simulated: bool) -> Vec<u8> {
let pdu = SavPdu { asdus };
FrameHeader {
dst: MacAddr::SV_BASE,
src: MacAddr::default(),
vlan: None,
ethertype: ETHERTYPE_SV,
appid: 0x4000,
reserved1: if simulated { RESERVED1_SIMULATION } else { 0 },
reserved2: 0,
}
.to_frame(&pdu.encode().unwrap())
.unwrap()
}
fn frame(cnt: u16, synch: SmpSynch) -> Vec<u8> {
frame_of(vec![asdu("MU01", cnt, synch)], false)
}
fn key() -> StreamKey {
StreamKey { dst: MacAddr::SV_BASE, appid: 0x4000, sv_id: "MU01".into() }
}
fn sub() -> Subscriber {
Subscriber::new(vec![StreamConfig::new(key()).with_conf_rev(1)])
}
#[test]
fn continuity_across_the_wrap() {
let mut s = sub();
let mut got = Vec::new();
for cnt in [3998u16, 3999, 0, 1, 3] {
s.on_frame(Instant::ZERO, &frame(cnt, SmpSynch::Global), |smp| got.push(smp.asdu.smp_cnt));
}
assert_eq!(got, [3998, 3999, 0, 1, 3]);
assert_eq!(s.poll_event(), Some(SubscriberEvent::SyncChanged { stream: 0, from: None, to: Some(SmpSynch::Global) }));
assert_eq!(s.poll_event(), Some(SubscriberEvent::Gap { stream: 0, expected: 2, received: 3, lost: 1 }));
assert_eq!(s.poll_event(), None);
let st = s.state(0).unwrap();
assert_eq!((st.gaps, st.samples_lost, st.frames, st.asdus), (1, 1, 5, 5));
}
#[test]
fn a_gap_across_the_wrap_counts_the_short_way() {
let mut s = sub();
s.on_frame(Instant::ZERO, &frame(3998, SmpSynch::Global), |_| {});
let _ = s.poll_event();
s.on_frame(Instant::ZERO, &frame(2, SmpSynch::Global), |_| {});
assert_eq!(s.poll_event(), Some(SubscriberEvent::Gap { stream: 0, expected: 3999, received: 2, lost: 3 }));
}
#[test]
fn a_multi_asdu_frame_counts_as_one_frame() {
let mut s = sub();
let mut seen = 0;
s.on_frame(Instant::ZERO, &frame_of(vec![asdu("MU01", 0, SmpSynch::Global), asdu("MU01", 1, SmpSynch::Global)], false), |_| seen += 1);
assert_eq!(seen, 2);
let st = s.state(0).unwrap();
assert_eq!((st.frames, st.asdus, st.gaps), (1, 2, 0));
assert_eq!(s.other_stream(), 0, "a frame of ours is never other traffic");
}
#[test]
fn conf_rev_mismatch_is_edge_triggered() {
let mut s = sub();
let mut bad = asdu("MU01", 0, SmpSynch::Global);
bad.conf_rev = 9;
for _ in 0..100 {
s.on_frame(Instant::ZERO, &frame_of(vec![bad.clone()], false), |_| unreachable!("dropped"));
}
assert_eq!(s.poll_event(), Some(SubscriberEvent::ConfRevMismatch { stream: 0, received: 9 }));
assert_eq!(s.poll_event(), None, "one event, not one per ASDU");
assert_eq!(s.state(0).unwrap().conf_rev_dropped, 100);
assert_eq!(s.state(0).unwrap().frames, 0);
assert_eq!(s.other_stream(), 0, "a stream we know is not other traffic just because we dropped it");
}
#[test]
fn staleness_is_noticed_on_arrival_as_well_as_on_a_timer() {
let mut s = sub();
s.on_frame(Instant::ZERO, &frame(0, SmpSynch::Global), |_| {});
let _ = s.poll_event();
assert_eq!(s.next_timeout(), Some(Instant::ZERO.plus_millis(10)));
s.on_timeout(Instant::ZERO.plus_millis(10));
assert_eq!(s.poll_event(), Some(SubscriberEvent::Stale { stream: 0 }));
s.on_timeout(Instant::ZERO.plus_millis(20));
assert_eq!(s.poll_event(), None, "stale is reported once");
s.on_frame(Instant::ZERO.plus_millis(21), &frame(1, SmpSynch::Global), |_| {});
assert_eq!(s.poll_event(), Some(SubscriberEvent::Resumed { stream: 0 }));
let mut s = sub();
s.on_frame(Instant::ZERO, &frame(0, SmpSynch::Global), |_| {});
while s.poll_event().is_some() {}
s.on_frame(Instant::ZERO.plus_millis(500), &frame(1, SmpSynch::Global), |_| {});
assert_eq!(s.poll_event(), Some(SubscriberEvent::Stale { stream: 0 }));
assert_eq!(s.poll_event(), Some(SubscriberEvent::Resumed { stream: 0 }));
}
#[test]
fn simulated_streams_follow_the_edition_2_rule() {
let mut s = sub();
let simulated = frame_of(vec![asdu("MU01", 0, SmpSynch::Global)], true);
s.on_frame(Instant::ZERO, &simulated, |_| unreachable!("dropped"));
assert_eq!(s.poll_event(), Some(SubscriberEvent::IgnoredSimulation { stream: 0 }));
s.on_frame(Instant::ZERO, &simulated, |_| unreachable!("dropped"));
assert_eq!(s.poll_event(), None, "reported once, not per frame");
assert_eq!(s.state(0).unwrap().simulation_dropped, 2);
let mut s = Subscriber::new(vec![StreamConfig::new(key()).with_simulation(SimulationMode::Preferred)]);
let mut seen = Vec::new();
s.on_frame(Instant::ZERO, &frame(100, SmpSynch::Global), |smp| seen.push((smp.asdu.smp_cnt, smp.simulation)));
s.on_frame(Instant::ZERO, &simulated, |smp| seen.push((smp.asdu.smp_cnt, smp.simulation)));
s.on_frame(Instant::ZERO, &frame(101, SmpSynch::Global), |smp| seen.push((smp.asdu.smp_cnt, smp.simulation)));
assert_eq!(seen, [(100, false), (0, true)], "the real stream is dropped once simulation took over");
assert!(s.state(0).unwrap().simulation_active);
while s.poll_event().is_some() {}
s.reset_simulation(0);
s.on_frame(Instant::ZERO, &frame(102, SmpSynch::Global), |smp| seen.push((smp.asdu.smp_cnt, smp.simulation)));
assert_eq!(seen.last(), Some(&(102, false)));
assert_eq!(s.poll_event(), None, "returning to the real stream is not a gap");
}
#[test]
fn a_stream_with_a_layout_hands_over_channels_and_one_without_hands_over_octets() {
use crate::proto::sv::{ChannelType, SampleLayout};
let layout = SampleLayout::new([(String::from("Ia.instMag.i"), ChannelType::Int(4)), (String::from("Ia.q"), ChannelType::Quality)]);
let mut with = Subscriber::new(vec![StreamConfig::new(key()).with_layout(layout)]);
let mut a = asdu("MU01", 0, SmpSynch::Global);
a.sample = alloc::vec![0, 0, 0x30, 0x39, 0, 0, 0, 0];
let frame = frame_of(vec![a], false);
let mut seen = Vec::new();
with.on_frame(Instant::ZERO, &frame, |s| {
seen = s.channels().map(|(c, v)| (c.name.clone(), v)).collect();
assert_eq!(s.channel(0).and_then(ChannelValue::as_i64), Some(12_345));
});
assert_eq!(seen.len(), 2);
assert_eq!(seen[0].0, "Ia.instMag.i");
assert_eq!(seen[0].1.as_i64(), Some(12_345));
assert_eq!(seen[1].1.as_quality(), Some(crate::common::Quality::GOOD));
let mut without = sub();
let mut n = 0;
without.on_frame(Instant::ZERO, &frame, |s| {
n = s.channels().count();
assert_eq!(s.asdu.sample.len(), 8);
assert_eq!(s.channel(0), None);
});
assert_eq!(n, 0);
let short = SampleLayout::new([(String::from("Ia.instMag.i"), ChannelType::Int(4))]);
let mut strict = Subscriber::new(vec![StreamConfig::new(key()).with_layout(short)]);
strict.on_frame(Instant::ZERO, &frame, |_| unreachable!("a mismatched ASDU must not reach the consumer"));
assert!(matches!(strict.poll_event(), Some(SubscriberEvent::SampleLengthMismatch { expected: 4, received: 8, .. })));
strict.on_frame(Instant::ZERO, &frame, |_| unreachable!());
assert!(strict.poll_event().is_none(), "reported once, not per ASDU");
assert_eq!(strict.state(0).unwrap().layout_mismatches, 2);
assert_eq!(strict.state(0).unwrap().asdus, 0);
}
#[test]
fn other_traffic_and_malformed_frames() {
let mut s = sub();
let mut other = frame(6, SmpSynch::None);
other[5] = 1;
s.on_frame(Instant::ZERO, &other, |_| unreachable!());
s.on_frame(Instant::ZERO, &[0u8; 40], |_| unreachable!());
assert_eq!(s.other_stream(), 2);
s.on_frame(Instant::ZERO, &frame(7, SmpSynch::None)[..30], |_| unreachable!());
assert!(matches!(s.poll_event(), Some(SubscriberEvent::Malformed(_))));
assert_eq!(s.malformed(), 1);
let mut theirs = frame(8, SmpSynch::None);
theirs[15] = 0x09; s.on_frame(Instant::ZERO, &theirs[..30], |_| unreachable!());
s.on_frame(Instant::ZERO, &theirs, |_| unreachable!());
assert_eq!((s.malformed(), s.other_stream()), (1, 4));
}
#[test]
fn the_event_queue_is_bounded() {
let mut s = Subscriber::new(vec![StreamConfig::new(key())]).with_event_capacity(4);
for cnt in (0..200u16).step_by(2) {
s.on_frame(Instant::ZERO, &frame(cnt, SmpSynch::Global), |_| {});
}
let mut n = 0;
while s.poll_event().is_some() {
n += 1;
}
assert_eq!(n, 4);
assert!(s.events_dropped() > 90);
assert_eq!(s.state(0).unwrap().gaps, 99, "dropping events must not lose counters");
}
}