use alloc::string::String;
use alloc::vec::Vec;
use super::apdu::{Asdu, AsduOffsets, Field, SavPdu, SmpSynch};
use crate::common::{Error, Instant, Result, UtcTime};
use crate::proto::ethernet::{FrameHeader, RESERVED1_SIMULATION};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum SmpMod {
#[default]
SamplesPerPeriod,
SamplesPerSecond,
SecondsPerSample,
}
impl SmpMod {
pub const fn to_u8(self) -> u8 {
match self {
SmpMod::SamplesPerPeriod => 0,
SmpMod::SamplesPerSecond => 1,
SmpMod::SecondsPerSample => 2,
}
}
pub const fn from_u8(v: u8) -> Option<SmpMod> {
Some(match v {
0 => SmpMod::SamplesPerPeriod,
1 => SmpMod::SamplesPerSecond,
2 => SmpMod::SecondsPerSample,
_ => return None,
})
}
pub fn parse(s: &str) -> Option<SmpMod> {
Some(match s {
"SmpPerPeriod" => SmpMod::SamplesPerPeriod,
"SmpPerSec" => SmpMod::SamplesPerSecond,
"SecPerSmp" => SmpMod::SecondsPerSample,
_ => return None,
})
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SvProfile {
pub samples_per_second: u32,
pub asdus_per_frame: u8,
pub smp_mod: Option<SmpMod>,
pub smp_rate: Option<u16>,
pub sample_len: usize,
}
impl SvProfile {
pub const LE_80_50HZ: SvProfile =
SvProfile { samples_per_second: 4000, asdus_per_frame: 1, smp_mod: None, smp_rate: None, sample_len: super::le::SAMPLE_LEN };
pub const LE_80_60HZ: SvProfile = SvProfile { samples_per_second: 4800, ..SvProfile::LE_80_50HZ };
pub const LE_256_50HZ: SvProfile = SvProfile { samples_per_second: 12_800, asdus_per_frame: 8, ..SvProfile::LE_80_50HZ };
pub const LE_256_60HZ: SvProfile = SvProfile { samples_per_second: 15_360, ..SvProfile::LE_256_50HZ };
pub const F4800S2I4U4: SvProfile = SvProfile {
samples_per_second: 4800,
asdus_per_frame: 2,
smp_mod: Some(SmpMod::SamplesPerSecond),
smp_rate: Some(4800),
sample_len: super::le::SAMPLE_LEN,
};
pub const F14400S6I4U4: SvProfile = SvProfile { samples_per_second: 14_400, asdus_per_frame: 6, smp_rate: Some(14_400), ..SvProfile::F4800S2I4U4 };
pub const fn frames_per_second(&self) -> u32 {
let n = if self.asdus_per_frame == 0 { 1 } else { self.asdus_per_frame as u32 };
self.samples_per_second / n
}
pub const fn frame_interval_nanos(&self) -> u64 {
let fps = self.frames_per_second();
if fps == 0 { 0 } else { 1_000_000_000 / fps as u64 }
}
pub const fn smp_cnt_wrap(&self) -> u32 {
super::smp_cnt_wrap(self.samples_per_second)
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct PublisherConfig {
pub header: FrameHeader,
pub sv_id: String,
pub dat_set: Option<String>,
pub conf_rev: u32,
pub profile: SvProfile,
pub simulation: bool,
pub refr_tm: bool,
pub gm_identity: bool,
}
impl PublisherConfig {
pub fn new(header: FrameHeader, sv_id: impl Into<String>, profile: SvProfile) -> Self {
PublisherConfig { header, sv_id: sv_id.into(), dat_set: None, conf_rev: 1, profile, simulation: false, refr_tm: false, gm_identity: false }
}
#[must_use]
pub fn with_conf_rev(mut self, conf_rev: u32) -> Self {
self.conf_rev = conf_rev;
self
}
#[must_use]
pub fn with_dat_set(mut self, dat_set: impl Into<String>) -> Self {
self.dat_set = Some(dat_set.into());
self
}
#[must_use]
pub fn with_time_fields(mut self, refr_tm: bool, gm_identity: bool) -> Self {
self.refr_tm = refr_tm;
self.gm_identity = gm_identity;
self
}
#[must_use]
pub fn with_simulation(mut self, on: bool) -> Self {
self.simulation = on;
self
}
}
#[derive(Debug)]
pub struct Publisher {
cfg: PublisherConfig,
frame: Vec<u8>,
at: Vec<AsduOffsets>,
smp_cnt: u16,
smp_synch: SmpSynch,
refr_tm: Option<UtcTime>,
gm_identity: Option<[u8; 8]>,
pending: bool,
next_send: Option<Instant>,
dropped: u64,
}
impl Publisher {
pub fn new(cfg: PublisherConfig) -> Result<Publisher> {
if !cfg.sv_id.is_ascii() || cfg.dat_set.as_ref().is_some_and(|s| !s.is_ascii()) {
return Err(Error::Encode("svID and datSet must be ASCII (VisibleString)"));
}
if cfg.profile.asdus_per_frame == 0 {
return Err(Error::InvalidValue("a frame must carry at least one ASDU"));
}
if cfg.profile.samples_per_second == 0 {
return Err(Error::InvalidValue("samples_per_second must not be zero"));
}
let (frame, at) = build_template(&cfg, SmpSynch::None, None, None)?;
let mut p = Publisher {
cfg,
frame,
at,
smp_cnt: 0,
smp_synch: SmpSynch::None,
refr_tm: None,
gm_identity: None,
pending: false,
next_send: Some(Instant::ZERO),
dropped: 0,
};
p.write_smp_cnt_fields();
Ok(p)
}
pub const fn config(&self) -> &PublisherConfig {
&self.cfg
}
pub const fn smp_cnt(&self) -> u16 {
self.smp_cnt
}
pub const fn dropped(&self) -> u64 {
self.dropped
}
pub const fn asdus_per_frame(&self) -> usize {
self.cfg.profile.asdus_per_frame as usize
}
pub const fn sample_len(&self) -> usize {
self.cfg.profile.sample_len
}
pub fn publish(&mut self, now: Instant, blocks: &[&[u8]]) -> Result<()> {
if blocks.len() != self.asdus_per_frame() {
return Err(Error::InvalidValue("one sample block per ASDU is required"));
}
if blocks.iter().any(|b| b.len() != self.cfg.profile.sample_len) {
return Err(Error::InvalidValue("sample block has the wrong length for this profile"));
}
for (i, block) in blocks.iter().enumerate() {
self.patch_asdu(i, block)?;
}
self.queue(now);
Ok(())
}
pub fn publish_repeating(&mut self, now: Instant, block: &[u8]) -> Result<()> {
if block.len() != self.cfg.profile.sample_len {
return Err(Error::InvalidValue("sample block has the wrong length for this profile"));
}
for i in 0..self.asdus_per_frame() {
self.patch_asdu(i, block)?;
}
self.queue(now);
Ok(())
}
fn patch_asdu(&mut self, i: usize, block: &[u8]) -> Result<()> {
let Some(&at) = self.at.get(i) else {
return Err(Error::InvalidValue("template is missing a patch point"));
};
let cnt = self.smp_cnt;
write_unsigned(&mut self.frame, at.smp_cnt, u64::from(cnt))?;
let Some(slot) = self.frame.get_mut(at.sample..at.sample + block.len()) else {
return Err(Error::InvalidValue("template offset out of range"));
};
slot.copy_from_slice(block);
self.smp_cnt = ((u32::from(self.smp_cnt) + 1) % self.cfg.profile.smp_cnt_wrap()) as u16;
Ok(())
}
fn queue(&mut self, now: Instant) {
if self.pending {
self.dropped = self.dropped.saturating_add(1);
}
self.pending = true;
self.next_send = Some(now.plus_nanos(self.cfg.profile.frame_interval_nanos()));
}
pub fn set_smp_synch(&mut self, synch: SmpSynch) -> Result<()> {
self.smp_synch = synch;
let value = u64::from(synch.to_u8());
let fits = self.at.iter().all(|a| a.smp_synch.is_none_or(|f| f.len >= crate::ber::unsigned_width(value, 1)));
if !fits {
return self.rebuild();
}
for i in 0..self.at.len() {
let Some(f) = self.at.get(i).and_then(|a| a.smp_synch) else { continue };
write_unsigned(&mut self.frame, f, value)?;
}
Ok(())
}
fn rebuild(&mut self) -> Result<()> {
let held: Vec<(u64, Vec<u8>)> = self
.at
.iter()
.map(|a| {
let count = read_unsigned(&self.frame, a.smp_cnt);
let block = self.frame.get(a.sample..a.sample + self.cfg.profile.sample_len).map(<[u8]>::to_vec).unwrap_or_default();
(count, block)
})
.collect();
let (frame, at) = build_template(&self.cfg, self.smp_synch, self.refr_tm, self.gm_identity)?;
self.frame = frame;
self.at = at;
for (i, (count, block)) in held.iter().enumerate() {
let Some(&a) = self.at.get(i) else { continue };
let _ = write_unsigned(&mut self.frame, a.smp_cnt, *count);
if block.len() != self.cfg.profile.sample_len {
continue;
}
if let Some(slot) = self.frame.get_mut(a.sample..a.sample + block.len()) {
slot.copy_from_slice(block);
}
}
Ok(())
}
fn write_smp_cnt_fields(&mut self) {
let mut cnt = u64::from(self.smp_cnt);
for i in 0..self.at.len() {
let Some(&a) = self.at.get(i) else { continue };
let _ = write_unsigned(&mut self.frame, a.smp_cnt, cnt);
cnt = (cnt + 1) % u64::from(self.cfg.profile.smp_cnt_wrap());
}
}
pub const fn smp_synch(&self) -> SmpSynch {
self.smp_synch
}
pub fn set_refr_tm(&mut self, t: UtcTime) {
self.refr_tm = Some(t);
let rate = u64::from(self.cfg.profile.samples_per_second.max(1));
let base = (u64::from(t.seconds) << 24) | u64::from(t.fraction & 0x00FF_FFFF);
for i in 0..self.at.len() {
let Some(o) = self.at.get(i).and_then(|a| a.refr_tm) else { continue };
let v = base.saturating_add(((i as u64) << 24) / rate);
let seconds = u32::try_from(v >> 24).unwrap_or(u32::MAX);
let octets = UtcTime { seconds, fraction: (v & 0x00FF_FFFF) as u32, quality: t.quality }.to_octets();
if let Some(slot) = self.frame.get_mut(o..o + 8) {
slot.copy_from_slice(&octets);
}
}
}
pub fn set_gm_identity(&mut self, id: [u8; 8]) {
self.gm_identity = Some(id);
for i in 0..self.at.len() {
let Some(o) = self.at.get(i).and_then(|a| a.gm_identity) else { continue };
if let Some(slot) = self.frame.get_mut(o..o + 8) {
slot.copy_from_slice(&id);
}
}
}
pub fn next_timeout(&self) -> Option<Instant> {
self.next_send
}
pub fn poll_transmit(&mut self) -> Option<&[u8]> {
if core::mem::take(&mut self.pending) { Some(&self.frame) } else { None }
}
pub fn set_smp_cnt(&mut self, smp_cnt: u16) {
self.smp_cnt = (u32::from(smp_cnt) % self.cfg.profile.smp_cnt_wrap()) as u16;
}
}
fn read_unsigned(frame: &[u8], field: Field) -> u64 {
frame.get(field.at..field.at + field.len).map_or(0, |o| o.iter().fold(0u64, |acc, b| (acc << 8) | u64::from(*b)))
}
fn write_unsigned(frame: &mut [u8], field: Field, value: u64) -> Result<()> {
if field.len == 0 || field.len > 8 {
return Err(Error::InvalidValue("template field has an impossible width"));
}
let Some(slot) = frame.get_mut(field.at..field.at + field.len) else {
return Err(Error::InvalidValue("template offset out of range"));
};
let bytes = value.to_be_bytes();
slot.copy_from_slice(bytes.get(8 - field.len..).unwrap_or(&bytes));
Ok(())
}
fn build_template(cfg: &PublisherConfig, smp_synch: SmpSynch, refr_tm: Option<UtcTime>, gm: Option<[u8; 8]>) -> Result<(Vec<u8>, Vec<AsduOffsets>)> {
let max_smp_cnt = u16::try_from(cfg.profile.smp_cnt_wrap().saturating_sub(1)).unwrap_or(u16::MAX);
let asdus: Vec<Asdu> = (0..cfg.profile.asdus_per_frame)
.map(|_| Asdu {
sv_id: cfg.sv_id.clone(),
dat_set: cfg.dat_set.clone(),
smp_cnt: max_smp_cnt,
conf_rev: cfg.conf_rev,
refr_tm: cfg.refr_tm.then(|| refr_tm.unwrap_or_default()),
smp_synch: Some(smp_synch),
smp_rate: cfg.profile.smp_rate,
sample: alloc::vec![0u8; cfg.profile.sample_len],
smp_mod: cfg.profile.smp_mod.map(SmpMod::to_u8),
gm_identity: cfg.gm_identity.then(|| gm.unwrap_or_default()),
})
.collect();
let apdu = SavPdu { asdus }.encode()?;
let mut link = cfg.header;
link.reserved1 = if cfg.simulation { link.reserved1 | RESERVED1_SIMULATION } else { link.reserved1 & !RESERVED1_SIMULATION };
let mut frame = alloc::vec![0u8; link.len() + apdu.len()];
link.write(&apdu, &mut frame)?;
let apdu_at = link.len();
let at = locate_fields(&frame, apdu_at, cfg)?;
Ok((frame, at))
}
fn locate_fields(frame: &[u8], apdu_at: usize, cfg: &PublisherConfig) -> Result<Vec<AsduOffsets>> {
use super::apdu::SavPduView;
use crate::common::Limits;
let apdu = frame.get(apdu_at..).ok_or(Error::Encode("frame shorter than its header"))?;
let limits = Limits { max_asdus: usize::from(cfg.profile.asdus_per_frame).max(1), ..Limits::DEFAULT };
let pdu = SavPduView::parse(apdu, &limits)?;
let mut out = Vec::with_capacity(usize::from(cfg.profile.asdus_per_frame));
for asdu in pdu.asdus() {
let a = asdu?;
out.push(AsduOffsets {
smp_cnt: Field { at: apdu_at + a.at.smp_cnt.at, len: a.at.smp_cnt.len },
smp_synch: a.at.smp_synch.map(|f| Field { at: apdu_at + f.at, len: f.len }),
refr_tm: a.at.refr_tm.map(|o| apdu_at + o),
sample: apdu_at + a.at.sample,
gm_identity: a.at.gm_identity.map(|o| apdu_at + o),
});
}
if out.len() != usize::from(cfg.profile.asdus_per_frame) {
return Err(Error::Encode("template does not hold the configured number of ASDUs"));
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::common::{Limits, Quality, TimeQuality};
use crate::proto::ethernet::{ETHERTYPE_SV, Frame, MacAddr, VlanTag};
use crate::proto::sv::le::PhsMeas1;
use crate::proto::sv::{AsduView, SavPduView, SmpSynch};
fn cfg(profile: SvProfile) -> PublisherConfig {
PublisherConfig::new(
FrameHeader {
dst: MacAddr::SV_BASE,
src: MacAddr([2, 0, 0, 0, 0, 2]),
vlan: Some(VlanTag::DEFAULT),
ethertype: ETHERTYPE_SV,
appid: 0x4000,
reserved1: 0,
reserved2: 0,
},
"MU01",
profile,
)
}
fn sample(v: i32) -> [u8; 64] {
PhsMeas1 { currents: [v; 4], current_quality: [Quality::GOOD; 4], voltages: [v; 4], voltage_quality: [Quality::GOOD; 4] }.encode()
}
#[test]
fn a_local_clock_identity_above_127_stays_a_positive_integer() {
let mut p = Publisher::new(cfg(SvProfile::LE_80_50HZ)).unwrap();
p.set_smp_synch(SmpSynch::Global).unwrap();
let frame = publish_one(&mut p, 1);
let fr = Frame::parse(&frame).unwrap();
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!(a.smp_synch, Some(SmpSynch::Global));
assert_eq!(a.at.smp_synch.unwrap().len, 1, "an ordinary smpSynch is one octet, as the vendor capture writes it");
p.set_smp_synch(SmpSynch::LocalClock(200)).unwrap();
let frame = publish_one(&mut p, 2);
let fr = Frame::parse(&frame).unwrap();
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!(a.smp_synch, Some(SmpSynch::LocalClock(200)));
assert_eq!(a.at.smp_synch.unwrap().len, 2, "200 needs the leading zero octet");
let octets = &fr.apdu[a.at.smp_synch.unwrap().at..a.at.smp_synch.unwrap().at + 2];
assert_eq!(octets, &[0x00, 0xC8]);
assert_eq!(a.sv_id, "MU01");
assert_eq!(a.sample, &sample(2)[..]);
}
#[test]
fn a_high_rate_stream_numbers_its_samples_without_going_negative() {
let profile = SvProfile { samples_per_second: 96_000, asdus_per_frame: 1, ..SvProfile::F4800S2I4U4 };
let mut p = Publisher::new(cfg(profile)).unwrap();
p.set_smp_cnt(65_535);
let frame = publish_one(&mut p, 1);
let fr = Frame::parse(&frame).unwrap();
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!(a.smp_cnt, 65_535);
assert_eq!(a.at.smp_cnt.len, 3, "the field is sized from the largest count the stream can reach");
assert_eq!(&fr.apdu[a.at.smp_cnt.at..a.at.smp_cnt.at + 3], &[0x00, 0xFF, 0xFF]);
assert_eq!(p.smp_cnt(), 0, "65 535 is the last count before the wrap");
let frame = publish_one(&mut p, 1);
let fr = Frame::parse(&frame).unwrap();
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!(a.smp_cnt, 0);
assert_eq!(a.at.smp_cnt.len, 3);
}
#[test]
fn ordinary_streams_keep_the_widths_the_field_uses() {
let mut p = Publisher::new(cfg(SvProfile::LE_80_50HZ).with_conf_rev(1)).unwrap();
let frame = publish_one(&mut p, 1);
let fr = Frame::parse(&frame).unwrap();
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!(a.at.smp_cnt.len, 2);
let at = fr.apdu.windows(6).position(|w| w == [0x83, 0x04, 0x00, 0x00, 0x00, 0x01]);
assert!(at.is_some(), "confRev keeps its four-octet field");
}
fn publish_one(p: &mut Publisher, v: i32) -> Vec<u8> {
let block = sample(v);
p.publish_repeating(Instant::ZERO, &block).unwrap();
p.poll_transmit().unwrap().to_vec()
}
#[test]
fn profiles_put_2400_frames_per_second_on_the_wire() {
assert_eq!(SvProfile::F4800S2I4U4.frames_per_second(), 2400);
assert_eq!(SvProfile::F14400S6I4U4.frames_per_second(), 2400);
assert_eq!(SvProfile::F4800S2I4U4.frame_interval_nanos(), 416_666);
assert_eq!(SvProfile::LE_80_50HZ.frames_per_second(), 4000);
assert_eq!(SvProfile::LE_256_50HZ.frames_per_second(), 1600);
assert_eq!(SvProfile::LE_256_60HZ.frames_per_second(), 1920);
}
#[test]
fn patching_the_template_yields_a_decodable_frame() {
let mut p = Publisher::new(cfg(SvProfile::LE_80_50HZ)).unwrap();
p.set_smp_synch(SmpSynch::Global).unwrap();
let frame = publish_one(&mut p, 1234);
assert!(p.poll_transmit().is_none(), "a frame is handed out once");
let fr = Frame::parse(&frame).unwrap();
assert_eq!(fr.appid, 0x4000);
let pdu = SavPduView::parse(fr.apdu, &Limits::DEFAULT).unwrap();
assert_eq!(pdu.no_asdu, 1);
let a = pdu.asdus().next().unwrap().unwrap();
assert_eq!((a.sv_id, a.smp_cnt, a.conf_rev, a.smp_synch), ("MU01", 0, 1, Some(SmpSynch::Global)));
assert_eq!(PhsMeas1::decode(a.sample).unwrap().currents[0], 1234);
assert_eq!(p.smp_cnt(), 1);
}
#[test]
fn multi_asdu_frames_number_their_samples_consecutively() {
let mut p = Publisher::new(cfg(SvProfile::F4800S2I4U4)).unwrap();
p.set_smp_synch(SmpSynch::Global).unwrap();
let (a, b) = (sample(10), sample(20));
p.publish(Instant::ZERO, &[&a, &b]).unwrap();
let frame = p.poll_transmit().unwrap().to_vec();
let pdu = SavPduView::parse(Frame::parse(&frame).unwrap().apdu, &Limits::DEFAULT).unwrap();
assert_eq!(pdu.no_asdu, 2);
let seen: Vec<(u16, i32)> = pdu.asdus().map(|a| a.unwrap()).map(|a| (a.smp_cnt, PhsMeas1::decode(a.sample).unwrap().currents[0])).collect();
assert_eq!(seen, [(0, 10), (1, 20)]);
assert_eq!(p.smp_cnt(), 2);
let first = pdu.asdus().next().unwrap().unwrap();
assert_eq!((first.smp_rate, first.smp_mod), (Some(4800), Some(1)));
}
#[test]
fn the_clock_fields_are_patched_in_place() {
let mut p = Publisher::new(cfg(SvProfile::F4800S2I4U4).with_time_fields(true, true)).unwrap();
let len = publish_one(&mut p, 0).len();
let t = UtcTime::from_unix(1_700_000_000, 500_000, TimeQuality::SYNCHRONIZED);
p.set_refr_tm(t);
p.set_gm_identity([0xAA, 1, 2, 3, 4, 5, 6, 7]);
p.set_smp_synch(SmpSynch::Global).unwrap();
let frame = publish_one(&mut p, 0);
assert_eq!(frame.len(), len, "patching must not change the frame length");
let pdu = SavPduView::parse(Frame::parse(&frame).unwrap().apdu, &Limits::DEFAULT).unwrap();
let seen: Vec<AsduView<'_>> = pdu.asdus().map(Result::unwrap).collect();
for a in &seen {
assert_eq!(a.gm_identity, Some(&[0xAA, 1, 2, 3, 4, 5, 6, 7][..]));
assert_eq!(a.smp_synch, Some(SmpSynch::Global));
}
assert_eq!(seen[0].refr_tm, Some(t));
let step = seen[1].refr_tm.unwrap().to_unix_nanos() - t.to_unix_nanos();
assert!((step as i64 - 208_333).abs() < 60, "refrTm step was {step} ns");
}
#[test]
fn refr_tm_does_not_drift_across_a_six_asdu_frame() {
let mut p = Publisher::new(cfg(SvProfile::F14400S6I4U4).with_time_fields(true, false)).unwrap();
let t = UtcTime::from_unix(1_700_000_000, 0, TimeQuality::SYNCHRONIZED);
p.set_refr_tm(t);
let blocks: Vec<[u8; 64]> = (0..6).map(|_| sample(0)).collect();
let refs: Vec<&[u8]> = blocks.iter().map(<[u8; 64]>::as_slice).collect();
p.publish(Instant::ZERO, &refs).unwrap();
let frame = p.poll_transmit().unwrap().to_vec();
let pdu = SavPduView::parse(Frame::parse(&frame).unwrap().apdu, &Limits::DEFAULT).unwrap();
for (i, a) in pdu.asdus().map(Result::unwrap).enumerate() {
let want = t.to_unix_nanos() + (i as u64 * 1_000_000_000) / 14_400;
let got = a.refr_tm.unwrap().to_unix_nanos();
assert!(got.abs_diff(want) < 60, "ASDU {i}: refrTm off by {} ns", got.abs_diff(want));
}
}
#[test]
fn smp_cnt_wraps_at_the_sample_rate() {
let mut p = Publisher::new(cfg(SvProfile::LE_80_50HZ)).unwrap();
p.set_smp_cnt(3998);
let mut seen = Vec::new();
for _ in 0..4 {
let frame = publish_one(&mut p, 0);
let pdu = SavPduView::parse(Frame::parse(&frame).unwrap().apdu, &Limits::DEFAULT).unwrap();
seen.push(pdu.asdus().next().unwrap().unwrap().smp_cnt);
}
assert_eq!(seen, [3998, 3999, 0, 1]);
}
#[test]
fn a_sample_rate_beyond_the_counter_wraps_at_the_field_width() {
let fast = SvProfile { samples_per_second: 96_000, asdus_per_frame: 4, ..SvProfile::F4800S2I4U4 };
assert_eq!(fast.smp_cnt_wrap(), 65_536);
let mut p = Publisher::new(cfg(fast)).unwrap();
p.set_smp_cnt(65_535);
let frame = publish_one(&mut p, 0);
let pdu = SavPduView::parse(Frame::parse(&frame).unwrap().apdu, &Limits::DEFAULT).unwrap();
let seen: Vec<u16> = pdu.asdus().map(|a| a.unwrap().smp_cnt).collect();
assert_eq!(seen, [65_535, 0, 1, 2]);
}
#[test]
fn the_template_is_encoded_once_and_never_grows() {
let mut p = Publisher::new(cfg(SvProfile::F14400S6I4U4)).unwrap();
let blocks: Vec<[u8; 64]> = (0..6).map(|i| sample(i * 100)).collect();
let refs: Vec<&[u8]> = blocks.iter().map(<[u8; 64]>::as_slice).collect();
let len = {
p.publish(Instant::ZERO, &refs).unwrap();
p.poll_transmit().unwrap().len()
};
for _ in 0..100 {
p.publish(Instant::ZERO, &refs).unwrap();
assert_eq!(p.poll_transmit().unwrap().len(), len, "patching must not change the frame length");
}
}
#[test]
fn scheduling_and_dropped_frames() {
let mut p = Publisher::new(cfg(SvProfile::F4800S2I4U4)).unwrap();
let (a, b) = (sample(1), sample(2));
assert_eq!(p.next_timeout(), Some(Instant::ZERO));
p.publish(Instant::ZERO, &[&a, &b]).unwrap();
assert_eq!(p.next_timeout(), Some(Instant(416_666)));
p.publish(Instant(416_666), &[&a, &b]).unwrap();
assert_eq!(p.dropped(), 1);
assert!(p.poll_transmit().is_some());
}
#[test]
fn bad_input_is_rejected_not_panicked() {
let mut p = Publisher::new(cfg(SvProfile::F4800S2I4U4)).unwrap();
let block = sample(0);
assert!(p.publish(Instant::ZERO, &[&block]).is_err(), "too few blocks");
assert!(p.publish(Instant::ZERO, &[&block[..10], &block]).is_err(), "wrong block length");
let mut bad = cfg(SvProfile::LE_80_50HZ);
bad.sv_id = "MÜ01".into();
assert!(Publisher::new(bad).is_err());
assert!(Publisher::new(cfg(SvProfile { asdus_per_frame: 0, ..SvProfile::LE_80_50HZ })).is_err());
assert!(Publisher::new(cfg(SvProfile { samples_per_second: 0, ..SvProfile::LE_80_50HZ })).is_err());
}
#[test]
fn simulation_sets_the_header_bit() {
let mut p = Publisher::new(cfg(SvProfile::LE_80_50HZ).with_simulation(true)).unwrap();
let frame = publish_one(&mut p, 0);
assert!(Frame::parse(&frame).unwrap().simulation());
}
}