sat-rs/fsrc-core/src/pus/verification.rs

1161 lines
37 KiB
Rust
Raw Normal View History

2022-09-04 22:24:36 +02:00
use crate::pool::{LocalPool, StoreAddr, StoreError};
2022-09-05 00:20:32 +02:00
use alloc::boxed::Box;
use alloc::sync::Arc;
2022-09-03 17:09:36 +02:00
use alloc::vec;
2022-09-03 16:30:37 +02:00
use alloc::vec::Vec;
2022-09-05 00:20:32 +02:00
use core::marker::PhantomData;
2022-09-03 17:09:36 +02:00
use core::mem::size_of;
2022-09-05 00:20:32 +02:00
use delegate::delegate;
2022-09-06 00:17:52 +02:00
use downcast_rs::{impl_downcast, Downcast};
2022-09-04 22:24:36 +02:00
use spacepackets::ecss::{EcssEnumeration, PusError};
2022-09-03 16:30:37 +02:00
use spacepackets::tc::PusTc;
2022-09-04 22:24:36 +02:00
use spacepackets::time::{CcsdsTimeProvider, TimestampError};
2022-09-03 16:30:37 +02:00
use spacepackets::tm::{PusTm, PusTmSecondaryHeader};
2022-09-03 18:51:01 +02:00
use spacepackets::{ByteConversionError, SizeMissmatch, SpHeader};
2022-09-03 16:30:37 +02:00
use spacepackets::{CcsdsPacket, PacketId, PacketSequenceCtrl};
#[cfg(feature = "std")]
use std::sync::mpsc::SendError;
2022-09-05 00:20:32 +02:00
#[cfg(feature = "std")]
use std::sync::MutexGuard;
2022-09-05 00:20:32 +02:00
#[cfg(feature = "std")]
use std::sync::{mpsc, Mutex};
2022-09-03 16:30:37 +02:00
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct RequestId {
version_number: u8,
packet_id: PacketId,
psc: PacketSequenceCtrl,
}
2022-09-03 17:09:36 +02:00
impl RequestId {
const SIZE_AS_BYTES: usize = size_of::<u32>();
pub fn to_bytes(&self, buf: &mut [u8]) {
let raw = ((self.version_number as u32) << 29)
2022-09-03 17:09:36 +02:00
| ((self.packet_id.raw() as u32) << 16)
| self.psc.raw() as u32;
buf.copy_from_slice(raw.to_be_bytes().as_slice());
}
pub fn from_bytes(buf: &[u8]) -> Option<Self> {
if buf.len() < 4 {
return None;
}
let raw = u32::from_be_bytes(buf[0..Self::SIZE_AS_BYTES].try_into().unwrap());
Some(Self {
version_number: ((raw >> 29) & 0b111) as u8,
packet_id: PacketId::from(((raw >> 16) & 0xffff) as u16),
psc: PacketSequenceCtrl::from((raw & 0xffff) as u16),
})
}
2022-09-03 17:09:36 +02:00
}
2022-09-03 16:30:37 +02:00
impl RequestId {
pub fn new(tc: &PusTc) -> Self {
RequestId {
version_number: tc.ccsds_version(),
packet_id: tc.packet_id(),
psc: tc.psc(),
}
}
}
2022-09-05 00:20:32 +02:00
#[derive(Debug, Clone)]
pub enum VerificationError<E> {
SendError(E),
TimeStampError(TimestampError),
ByteConversionError(ByteConversionError),
PusError(PusError),
2022-09-03 16:30:37 +02:00
}
2022-09-05 00:20:32 +02:00
#[derive(Debug, Clone)]
pub struct VerificationErrorWithToken<E, T>(VerificationError<E>, VerificationToken<T>);
2022-09-06 00:17:52 +02:00
pub trait VerificationSender<E>: Downcast {
2022-09-05 00:20:32 +02:00
fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<E>>;
}
2022-09-06 00:17:52 +02:00
impl_downcast!(VerificationSender<E>);
2022-09-05 00:20:32 +02:00
#[derive(Debug, Clone, Copy)]
pub struct VerificationToken<STATE> {
state: PhantomData<STATE>,
req_id: RequestId,
}
2022-09-05 00:20:32 +02:00
#[derive(Copy, Clone, Debug)]
pub struct StateNone;
#[derive(Copy, Clone, Debug)]
pub struct StateAccepted;
#[derive(Copy, Clone, Debug)]
pub struct StateStarted;
impl<STATE> VerificationToken<STATE> {
fn new(req_id: RequestId) -> VerificationToken<StateNone> {
VerificationToken {
state: PhantomData,
req_id,
}
}
}
2022-09-03 17:09:36 +02:00
pub struct VerificationReporterCfg {
pub apid: u16,
pub dest_id: u16,
pub step_field_width: u8,
pub failure_code_field_width: u8,
pub max_fail_data_len: usize,
pub max_stamp_len: usize,
}
impl VerificationReporterCfg {
pub fn new(time_stamper: impl CcsdsTimeProvider, apid: u16) -> Self {
2022-09-03 17:09:36 +02:00
let max_stamp_len = time_stamper.len_as_bytes();
Self {
apid,
dest_id: 0,
step_field_width: size_of::<u8>() as u8,
failure_code_field_width: size_of::<u16>() as u8,
max_fail_data_len: 2 * size_of::<u32>(),
max_stamp_len,
}
}
2022-09-03 16:30:37 +02:00
}
2022-09-04 22:24:36 +02:00
pub struct FailParams<'a> {
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
failure_code: &'a dyn EcssEnumeration,
2022-09-05 00:20:32 +02:00
failure_data: Option<&'a [u8]>,
2022-09-03 18:51:01 +02:00
}
2022-09-04 22:24:36 +02:00
impl<'a> FailParams<'a> {
2022-09-03 18:51:01 +02:00
pub fn new(
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
failure_code: &'a impl EcssEnumeration,
2022-09-05 00:20:32 +02:00
failure_data: Option<&'a [u8]>,
2022-09-03 18:51:01 +02:00
) -> Self {
Self {
time_stamp,
2022-09-03 18:51:01 +02:00
failure_code,
failure_data,
}
}
}
2022-09-04 22:24:36 +02:00
pub struct FailParamsWithStep<'a> {
bp: FailParams<'a>,
2022-09-03 18:51:01 +02:00
step: &'a dyn EcssEnumeration,
}
2022-09-04 22:24:36 +02:00
impl<'a> FailParamsWithStep<'a> {
2022-09-03 18:51:01 +02:00
pub fn new(
time_stamp: &'a [u8],
2022-09-06 00:17:52 +02:00
step: &'a impl EcssEnumeration,
2022-09-03 18:51:01 +02:00
failure_code: &'a impl EcssEnumeration,
2022-09-05 00:20:32 +02:00
failure_data: Option<&'a [u8]>,
2022-09-03 18:51:01 +02:00
) -> Self {
Self {
2022-09-04 22:24:36 +02:00
bp: FailParams::new(time_stamp, failure_code, failure_data),
2022-09-03 18:51:01 +02:00
step,
}
}
}
2022-09-05 00:20:32 +02:00
pub struct VerificationReporter {
pub apid: u16,
pub dest_id: u16,
msg_count: u16,
source_data_buf: Vec<u8>,
}
2022-09-03 16:30:37 +02:00
impl VerificationReporter {
2022-09-03 17:09:36 +02:00
pub fn new(cfg: VerificationReporterCfg) -> Self {
Self {
apid: cfg.apid,
dest_id: cfg.dest_id,
msg_count: 0,
source_data_buf: vec![
0;
RequestId::SIZE_AS_BYTES
+ cfg.step_field_width as usize
+ cfg.failure_code_field_width as usize
+ cfg.max_fail_data_len
],
}
}
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<StateNone> {
self.add_tc_with_req_id(RequestId::new(pus_tc))
}
pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<StateNone> {
2022-09-03 16:30:37 +02:00
VerificationToken::<StateNone>::new(req_id)
}
pub fn acceptance_success<E>(
&mut self,
token: VerificationToken<StateNone>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8],
2022-09-04 22:24:36 +02:00
) -> Result<VerificationToken<StateAccepted>, VerificationErrorWithToken<E, StateNone>> {
let tm = self
.create_pus_verif_success_tm(
1,
1,
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 16:30:37 +02:00
self.msg_count += 1;
Ok(VerificationToken {
state: PhantomData,
req_id: token.req_id,
})
}
2022-09-03 17:09:36 +02:00
pub fn acceptance_failure<E>(
mut self,
token: VerificationToken<StateNone>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateNone>> {
let tm = self
2022-09-06 00:17:52 +02:00
.create_pus_verif_fail_tm(1, 2, &token.req_id, None::<&dyn EcssEnumeration>, &params)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 17:09:36 +02:00
self.msg_count += 1;
Ok(())
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
pub fn start_success<E>(
2022-09-03 16:30:37 +02:00
&mut self,
token: VerificationToken<StateAccepted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
2022-09-04 22:24:36 +02:00
time_stamp: &[u8],
) -> Result<VerificationToken<StateStarted>, VerificationErrorWithToken<E, StateAccepted>> {
let tm = self
.create_pus_verif_success_tm(
1,
3,
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 18:51:01 +02:00
self.msg_count += 1;
Ok(VerificationToken {
2022-09-03 16:30:37 +02:00
state: PhantomData,
req_id: token.req_id,
2022-09-03 18:51:01 +02:00
})
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
pub fn start_failure<E>(
&mut self,
token: VerificationToken<StateAccepted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
let tm = self
2022-09-06 00:17:52 +02:00
.create_pus_verif_fail_tm(1, 4, &token.req_id, None::<&dyn EcssEnumeration>, &params)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 18:51:01 +02:00
self.msg_count += 1;
Ok(())
}
pub fn step_success<E>(
&mut self,
2022-09-06 00:17:52 +02:00
token: &VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8],
2022-09-03 18:51:01 +02:00
step: impl EcssEnumeration,
2022-09-04 22:24:36 +02:00
) -> Result<(), VerificationError<E>> {
let tm = self.create_pus_verif_success_tm(1, 5, &token.req_id, time_stamp, Some(&step))?;
2022-09-03 18:51:01 +02:00
sender.send_verification_tm(tm)?;
self.msg_count += 1;
Ok(())
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
pub fn step_failure<E>(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParamsWithStep,
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-06 00:17:52 +02:00
.create_pus_verif_fail_tm(1, 6, &token.req_id, Some(params.step), &params.bp)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 18:51:01 +02:00
self.msg_count += 1;
Ok(())
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
pub fn completion_success<E>(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8],
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
.create_pus_verif_success_tm(
1,
7,
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 18:51:01 +02:00
self.msg_count += 1;
Ok(())
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
pub fn completion_failure<E>(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
sender: &mut (impl VerificationSender<E> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-06 00:17:52 +02:00
.create_pus_verif_fail_tm(1, 8, &token.req_id, None::<&dyn EcssEnumeration>, &params)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
.send_verification_tm(tm)
.map_err(|e| VerificationErrorWithToken(e, token))?;
2022-09-03 18:51:01 +02:00
self.msg_count += 1;
Ok(())
2022-09-03 16:30:37 +02:00
}
2022-09-03 18:51:01 +02:00
fn create_pus_verif_success_tm<'a, E>(
&'a mut self,
2022-09-03 18:51:01 +02:00
service: u8,
subservice: u8,
req_id: &RequestId,
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
step: Option<&(impl EcssEnumeration + ?Sized)>,
2022-09-04 22:24:36 +02:00
) -> Result<PusTm, VerificationError<E>> {
2022-09-03 18:51:01 +02:00
let mut source_data_len = size_of::<u32>();
if let Some(step) = step {
source_data_len += step.byte_width() as usize;
}
self.source_buffer_large_enough(source_data_len)?;
let mut idx = 0;
req_id.to_bytes(&mut self.source_data_buf[0..RequestId::SIZE_AS_BYTES]);
idx += RequestId::SIZE_AS_BYTES;
if let Some(step) = step {
// Size check was done beforehand
step.to_bytes(&mut self.source_data_buf[idx..idx + step.byte_width() as usize])
.unwrap();
}
let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base(
service,
subservice,
&mut sp_header,
time_stamp,
source_data_len,
))
2022-09-03 16:30:37 +02:00
}
fn create_pus_verif_fail_tm<'a, E>(
&'a mut self,
2022-09-03 17:09:36 +02:00
service: u8,
subservice: u8,
req_id: &RequestId,
2022-09-03 18:51:01 +02:00
step: Option<&(impl EcssEnumeration + ?Sized)>,
2022-09-06 00:17:52 +02:00
params: &'a FailParams,
2022-09-04 22:24:36 +02:00
) -> Result<PusTm, VerificationError<E>> {
2022-09-03 18:51:01 +02:00
let mut idx = 0;
2022-09-05 00:20:32 +02:00
let mut source_data_len =
RequestId::SIZE_AS_BYTES + params.failure_code.byte_width() as usize;
2022-09-03 18:51:01 +02:00
if let Some(step) = step {
source_data_len += step.byte_width() as usize;
}
2022-09-05 00:20:32 +02:00
if let Some(failure_data) = params.failure_data {
source_data_len += failure_data.len();
}
2022-09-03 18:51:01 +02:00
self.source_buffer_large_enough(source_data_len)?;
req_id.to_bytes(&mut self.source_data_buf[0..RequestId::SIZE_AS_BYTES]);
idx += RequestId::SIZE_AS_BYTES;
if let Some(step) = step {
// Size check done beforehand
step.to_bytes(&mut self.source_data_buf[idx..idx + step.byte_width() as usize])
.unwrap();
2022-09-06 00:17:52 +02:00
idx += step.byte_width() as usize;
2022-09-03 18:51:01 +02:00
}
2022-09-04 22:24:36 +02:00
params
.failure_code
.to_bytes(
&mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
)
.map_err(|e| VerificationError::<E>::ByteConversionError(e))?;
idx += params.failure_code.byte_width() as usize;
2022-09-05 00:20:32 +02:00
if let Some(failure_data) = params.failure_data {
self.source_data_buf[idx..idx + failure_data.len()].copy_from_slice(failure_data);
}
2022-09-03 16:30:37 +02:00
let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base(
service,
subservice,
&mut sp_header,
2022-09-04 22:24:36 +02:00
params.time_stamp,
source_data_len,
))
2022-09-03 18:51:01 +02:00
}
2022-09-04 22:24:36 +02:00
fn source_buffer_large_enough<E>(&self, len: usize) -> Result<(), VerificationError<E>> {
2022-09-03 18:51:01 +02:00
if len > self.source_data_buf.capacity() {
2022-09-04 22:24:36 +02:00
return Err(VerificationError::ByteConversionError(
2022-09-03 18:51:01 +02:00
ByteConversionError::ToSliceTooSmall(SizeMissmatch {
found: self.source_data_buf.capacity(),
expected: len,
}),
));
}
Ok(())
}
fn create_pus_verif_tm_base<'a>(
&'a mut self,
2022-09-03 18:51:01 +02:00
service: u8,
subservice: u8,
sp_header: &mut SpHeader,
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
source_data_len: usize,
) -> PusTm {
2022-09-03 16:30:37 +02:00
let tm_sec_header = PusTmSecondaryHeader::new(
service,
subservice,
self.msg_count,
self.dest_id,
time_stamp,
2022-09-03 16:30:37 +02:00
);
2022-09-03 18:51:01 +02:00
PusTm::new(
sp_header,
2022-09-03 17:09:36 +02:00
tm_sec_header,
Some(&self.source_data_buf[0..source_data_len]),
true,
2022-09-03 18:51:01 +02:00
)
2022-09-03 16:30:37 +02:00
}
}
2022-09-05 00:20:32 +02:00
pub struct VerificationReporterWithSender<E> {
reporter: VerificationReporter,
2022-09-06 00:17:52 +02:00
pub sender: Box<dyn VerificationSender<E>>,
2022-09-03 16:30:37 +02:00
}
2022-09-06 00:17:52 +02:00
impl<E: 'static> VerificationReporterWithSender<E> {
2022-09-05 00:20:32 +02:00
pub fn new(cfg: VerificationReporterCfg, sender: Box<dyn VerificationSender<E>>) -> Self {
2022-09-06 00:17:52 +02:00
Self::new_from_reporter(VerificationReporter::new(cfg), sender)
}
pub fn new_from_reporter(
reporter: VerificationReporter,
sender: Box<dyn VerificationSender<E>>,
) -> Self {
Self { reporter, sender }
2022-09-05 00:20:32 +02:00
}
2022-09-03 16:30:37 +02:00
2022-09-05 00:20:32 +02:00
delegate! {
to self.reporter {
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<StateNone>;
pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<StateNone>;
}
}
pub fn acceptance_success(
&mut self,
token: VerificationToken<StateNone>,
time_stamp: &[u8],
) -> Result<VerificationToken<StateAccepted>, VerificationErrorWithToken<E, StateNone>> {
self.reporter
.acceptance_success(token, self.sender.as_mut(), time_stamp)
}
pub fn acceptance_failure(
mut self,
token: VerificationToken<StateNone>,
params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateNone>> {
self.reporter
.acceptance_failure(token, self.sender.as_mut(), params)
}
pub fn start_success(
&mut self,
token: VerificationToken<StateAccepted>,
time_stamp: &[u8],
) -> Result<VerificationToken<StateStarted>, VerificationErrorWithToken<E, StateAccepted>> {
self.reporter
.start_success(token, self.sender.as_mut(), time_stamp)
}
pub fn start_failure(
&mut self,
token: VerificationToken<StateAccepted>,
params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
self.reporter
.start_failure(token, self.sender.as_mut(), params)
}
pub fn step_success(
&mut self,
2022-09-06 00:17:52 +02:00
token: &VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
time_stamp: &[u8],
step: impl EcssEnumeration,
) -> Result<(), VerificationError<E>> {
self.reporter
.step_success(token, self.sender.as_mut(), time_stamp, step)
}
pub fn step_failure(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
params: FailParamsWithStep,
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-05 00:20:32 +02:00
self.reporter
.step_failure(token, self.sender.as_mut(), params)
}
pub fn completion_success(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
time_stamp: &[u8],
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-05 00:20:32 +02:00
self.reporter
.completion_success(token, self.sender.as_mut(), time_stamp)
}
pub fn completion_failure(
&mut self,
2022-09-06 00:17:52 +02:00
token: VerificationToken<StateStarted>,
2022-09-05 00:20:32 +02:00
params: FailParams,
2022-09-06 00:17:52 +02:00
) -> Result<(), VerificationErrorWithToken<E, StateStarted>> {
2022-09-05 00:20:32 +02:00
self.reporter
.completion_failure(token, self.sender.as_mut(), params)
}
}
#[cfg(feature = "std")]
pub struct StdVerifSender {
pub ignore_poison_error: bool,
tm_store: Arc<Mutex<LocalPool>>,
tx: mpsc::Sender<StoreAddr>,
}
#[cfg(feature = "std")]
impl StdVerifSender {
pub fn new(tm_store: Arc<Mutex<LocalPool>>, tx: mpsc::Sender<StoreAddr>) -> Self {
Self {
ignore_poison_error: true,
tx,
tm_store,
}
}
}
#[cfg(feature = "std")]
#[derive(Debug, Eq, PartialEq)]
pub enum StdVerifSenderError {
PoisonError,
StoreError(StoreError),
SendError(SendError<StoreAddr>),
}
#[cfg(feature = "std")]
impl VerificationSender<StdVerifSenderError> for StdVerifSender {
fn send_verification_tm(
&mut self,
tm: PusTm,
) -> Result<(), VerificationError<StdVerifSenderError>> {
let operation = |mut mg: MutexGuard<LocalPool>| {
let (addr, buf) = mg
.free_element(tm.len_packed())
.map_err(|e| VerificationError::SendError(StdVerifSenderError::StoreError(e)))?;
tm.write_to(buf).map_err(VerificationError::PusError)?;
self.tx
.send(addr)
.map_err(|e| VerificationError::SendError(StdVerifSenderError::SendError(e)))?;
Ok(())
};
match self.tm_store.lock() {
Ok(lock) => operation(lock),
Err(poison_error) => {
if self.ignore_poison_error {
operation(poison_error.into_inner())
} else {
Err(VerificationError::SendError(
StdVerifSenderError::PoisonError,
))
}
}
2022-09-03 16:30:37 +02:00
}
}
}
#[cfg(test)]
mod tests {
use crate::pus::verification::{
2022-09-06 00:17:52 +02:00
FailParams, FailParamsWithStep, RequestId, StateNone, VerificationError,
VerificationReporter, VerificationReporterCfg, VerificationReporterWithSender,
VerificationSender, VerificationToken,
};
2022-09-06 00:17:52 +02:00
use alloc::boxed::Box;
use alloc::vec::Vec;
2022-09-06 00:17:52 +02:00
use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, EcssEnumeration, PusPacket};
use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
use spacepackets::time::{CdsShortTimeProvider, TimeWriter};
use spacepackets::tm::{PusTm, PusTmSecondaryHeaderT};
use spacepackets::{CcsdsPacket, SpHeader};
use std::collections::VecDeque;
const TEST_APID: u16 = 0x02;
2022-09-06 00:17:52 +02:00
const EMPTY_STAMP: [u8; 7] = [0; 7];
2022-09-03 16:30:37 +02:00
2022-09-06 00:17:52 +02:00
#[derive(Debug, Eq, PartialEq)]
struct TmInfo {
pub subservice: u8,
pub apid: u16,
pub msg_counter: u16,
pub dest_id: u16,
pub time_stamp: [u8; 7],
pub req_id: RequestId,
pub additional_data: Option<Vec<u8>>,
}
#[derive(Default)]
struct TestSender {
pub service_queue: VecDeque<TmInfo>,
}
2022-09-06 00:17:52 +02:00
struct TestBase<'a> {
#[allow(dead_code)]
ts: CdsShortTimeProvider,
vr: VerificationReporter,
#[allow(dead_code)]
tc: PusTc<'a>,
req_id: RequestId,
}
struct TestBaseWithHelper<'a, E> {
#[allow(dead_code)]
ts: CdsShortTimeProvider,
helper: VerificationReporterWithSender<E>,
#[allow(dead_code)]
tc: PusTc<'a>,
req_id: RequestId,
}
fn base_reporter_and_stamper() -> (CdsShortTimeProvider, VerificationReporter) {
let time_stamper = CdsShortTimeProvider::default();
let cfg = VerificationReporterCfg::new(time_stamper, 0x02);
(time_stamper, VerificationReporter::new(cfg))
}
fn base_tc_init(app_data: Option<&[u8]>) -> (PusTc, RequestId) {
let mut sph = SpHeader::tc(TEST_APID, 0x34, 0).unwrap();
let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
let pus_tc = PusTc::new(&mut sph, tc_header, app_data, true);
let req_id = RequestId::new(&pus_tc);
(pus_tc, req_id)
}
fn base_init() -> (TestBase<'static>, VerificationToken<StateNone>) {
let (ts, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let init_tok = reporter.add_tc(&tc);
(
TestBase {
ts,
vr: reporter,
tc,
req_id,
},
init_tok,
)
}
fn base_with_helper_init() -> (
TestBaseWithHelper<'static, ()>,
VerificationToken<StateNone>,
) {
let (ts, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let init_tok = reporter.add_tc(&tc);
let sender = TestSender::default();
let helper = VerificationReporterWithSender::new_from_reporter(reporter, Box::new(sender));
(
TestBaseWithHelper {
ts,
helper: helper,
tc,
req_id,
},
init_tok,
)
}
impl VerificationSender<()> for TestSender {
2022-09-04 22:24:36 +02:00
fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<()>> {
assert_eq!(PusPacket::service(&tm), 1);
assert!(tm.source_data().is_some());
let mut time_stamp = [0; 7];
time_stamp.clone_from_slice(tm.time_stamp());
let src_data = tm.source_data().unwrap();
assert!(src_data.len() >= 4);
let req_id = RequestId::from_bytes(&src_data[0..RequestId::SIZE_AS_BYTES]).unwrap();
let mut vec = None;
if src_data.len() > 4 {
let mut new_vec = Vec::new();
new_vec.extend_from_slice(&src_data[RequestId::SIZE_AS_BYTES..]);
vec = Some(new_vec);
}
self.service_queue.push_back(TmInfo {
subservice: PusPacket::subservice(&tm),
apid: tm.apid(),
msg_counter: tm.msg_counter(),
dest_id: tm.dest_id(),
time_stamp,
req_id,
additional_data: vec,
});
Ok(())
}
}
2022-09-05 00:20:32 +02:00
2022-09-06 00:17:52 +02:00
fn acceptance_check(sender: &mut TestSender, req_id: &RequestId) {
let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id: req_id.clone(),
};
assert_eq!(sender.service_queue.len(), 1);
let info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
2022-09-03 16:30:37 +02:00
#[test]
2022-09-06 00:17:52 +02:00
fn test_basic_acceptance_success() {
let (mut b, tok) = base_init();
let mut sender = TestSender::default();
2022-09-06 00:17:52 +02:00
b.vr.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
2022-09-06 00:17:52 +02:00
acceptance_check(&mut sender, &b.req_id);
}
#[test]
fn test_basic_acceptance_success_with_helper() {
let (mut b, tok) = base_with_helper_init();
b.helper
.acceptance_success(tok, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
acceptance_check(sender, &b.req_id);
2022-09-03 16:30:37 +02:00
}
2022-09-05 00:20:32 +02:00
#[test]
pub fn test_basic_acceptance_failure() {
2022-09-06 00:17:52 +02:00
let (time_stamper, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
2022-09-05 00:20:32 +02:00
reporter.dest_id = 5;
2022-09-06 00:17:52 +02:00
let verif_token = reporter.add_tc(&tc);
2022-09-05 00:20:32 +02:00
let mut stamp_buf = [1, 2, 3, 4, 5, 6, 7];
time_stamper.write_to_bytes(&mut stamp_buf).unwrap();
let mut sender = TestSender::default();
let fail_code = EcssEnumU16::new(2);
let fail_params = FailParams::new(stamp_buf.as_slice(), &fail_code, None);
reporter
.acceptance_failure(verif_token, &mut sender, fail_params)
.expect("Sending acceptance success failed");
2022-09-06 00:17:52 +02:00
let cmp_info = TmInfo {
time_stamp: stamp_buf,
subservice: 2,
dest_id: 5,
apid: TEST_APID,
msg_counter: 0,
additional_data: Some([0, 2].to_vec()),
req_id,
};
2022-09-05 00:20:32 +02:00
assert_eq!(sender.service_queue.len(), 1);
let info = sender.service_queue.pop_front().unwrap();
2022-09-06 00:17:52 +02:00
assert_eq!(info, cmp_info);
}
#[test]
pub fn test_basic_acceptance_failure_with_fail_data() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let fail_code = EcssEnumU8::new(10);
let fail_data = EcssEnumU32::new(12);
let mut fail_data_raw = [0; 4];
fail_data.to_bytes(&mut fail_data_raw).unwrap();
let fail_params = FailParams::new(&EMPTY_STAMP, &fail_code, Some(fail_data_raw.as_slice()));
reporter
.acceptance_failure(verif_token, &mut sender, fail_params)
.expect("Sending acceptance success failed");
let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 2,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: Some([10, 0, 0, 0, 12].to_vec()),
req_id,
};
assert_eq!(sender.service_queue.len(), 1);
let info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
#[test]
fn test_start_failure() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let fail_code = EcssEnumU8::new(22);
let fail_data: i32 = -12;
let mut fail_data_raw = [0; 4];
fail_data_raw.copy_from_slice(fail_data.to_be_bytes().as_slice());
let fail_params = FailParams::new(&EMPTY_STAMP, &fail_code, Some(fail_data_raw.as_slice()));
let accepted_token = reporter
.acceptance_success(verif_token, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let empty = reporter
.start_failure(accepted_token, &mut sender, fail_params)
.expect("Start failure failure");
assert_eq!(empty, ());
assert_eq!(sender.service_queue.len(), 2);
let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 4,
dest_id: 0,
apid: TEST_APID,
msg_counter: 1,
additional_data: Some([&[22], fail_data_raw.as_slice()].concat().to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
#[test]
fn test_steps_success() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let accepted_token = reporter
.acceptance_success(verif_token, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = reporter
.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let mut empty = reporter
.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(0),
)
.expect("Sending step 0 success failed");
assert_eq!(empty, ());
empty = reporter
.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(1),
)
.expect("Sending step 1 success failed");
assert_eq!(empty, ());
let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0],
subservice: 3,
dest_id: 0,
apid: TEST_APID,
msg_counter: 1,
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 5,
dest_id: 0,
apid: TEST_APID,
msg_counter: 2,
additional_data: Some([0].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 5,
dest_id: 0,
apid: TEST_APID,
msg_counter: 3,
additional_data: Some([1].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
#[test]
fn test_step_failure() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let fail_code = EcssEnumU32::new(0x1020);
let fail_data: f32 = -22.3232;
let mut fail_data_raw = [0; 4];
fail_data_raw.copy_from_slice(fail_data.to_be_bytes().as_slice());
let fail_step = EcssEnumU8::new(1);
let fail_params = FailParamsWithStep::new(
&EMPTY_STAMP,
&fail_step,
&fail_code,
Some(fail_data_raw.as_slice()),
);
let accepted_token = reporter
.acceptance_success(verif_token, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = reporter
.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let mut empty = reporter
.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(0),
)
.expect("Sending completion success failed");
assert_eq!(empty, ());
empty = reporter
.step_failure(started_token, &mut sender, fail_params)
.expect("Step failure failed");
assert_eq!(empty, ());
assert_eq!(sender.service_queue.len(), 4);
let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0],
subservice: 3,
dest_id: 0,
apid: TEST_APID,
msg_counter: 1,
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 5,
dest_id: 0,
apid: TEST_APID,
msg_counter: 2,
additional_data: Some([0].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 6,
dest_id: 0,
apid: TEST_APID,
msg_counter: 3,
additional_data: Some(
[
[1].as_slice(),
&[0, 0, 0x10, 0x20],
fail_data_raw.as_slice(),
]
.concat()
.to_vec(),
),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
#[test]
fn test_completion_failure() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let fail_code = EcssEnumU32::new(0x1020);
let fail_params = FailParams::new(&EMPTY_STAMP, &fail_code, None);
let accepted_token = reporter
.acceptance_success(verif_token, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = reporter
.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let empty = reporter
.completion_failure(started_token, &mut sender, fail_params)
.expect("Completion failure");
assert_eq!(empty, ());
assert_eq!(sender.service_queue.len(), 3);
let mut cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0],
subservice: 3,
dest_id: 0,
apid: TEST_APID,
msg_counter: 1,
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 8,
dest_id: 0,
apid: TEST_APID,
msg_counter: 2,
additional_data: Some([0, 0, 0x10, 0x20].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
#[test]
fn test_complete_success_sequence() {
let (_, mut reporter) = base_reporter_and_stamper();
let (tc, req_id) = base_tc_init(None);
let verif_token = reporter.add_tc(&tc);
let mut sender = TestSender::default();
let accepted_token = reporter
.acceptance_success(verif_token, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = reporter
.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let empty = reporter
.completion_success(started_token, &mut sender, &EMPTY_STAMP)
.expect("Sending completion success failed");
assert_eq!(empty, ());
assert_eq!(sender.service_queue.len(), 3);
let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 1,
dest_id: 0,
apid: TEST_APID,
msg_counter: 0,
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
let cmp_info = TmInfo {
time_stamp: [0, 1, 0, 1, 0, 1, 0],
subservice: 3,
dest_id: 0,
apid: TEST_APID,
msg_counter: 1,
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
let cmp_info = TmInfo {
time_stamp: EMPTY_STAMP,
subservice: 7,
dest_id: 0,
apid: TEST_APID,
msg_counter: 2,
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
2022-09-05 00:20:32 +02:00
}
2022-09-03 16:30:37 +02:00
}