this is a sufficient solution

This commit is contained in:
Robin Müller 2022-09-05 00:20:32 +02:00
parent 4fa79cf7cd
commit 57a838de82
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C

View File

@ -1,22 +1,24 @@
use crate::pool::{LocalPool, StoreAddr, StoreError}; use crate::pool::{LocalPool, StoreAddr, StoreError};
use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec; use alloc::vec;
use alloc::vec::Vec; use alloc::vec::Vec;
use core::marker::PhantomData;
use core::mem::size_of; use core::mem::size_of;
use delegate::delegate;
use spacepackets::ecss::{EcssEnumeration, PusError}; use spacepackets::ecss::{EcssEnumeration, PusError};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::time::{CcsdsTimeProvider, TimestampError}; use spacepackets::time::{CcsdsTimeProvider, TimestampError};
use spacepackets::tm::{PusTm, PusTmSecondaryHeader}; use spacepackets::tm::{PusTm, PusTmSecondaryHeader};
use spacepackets::{ByteConversionError, SizeMissmatch, SpHeader}; use spacepackets::{ByteConversionError, SizeMissmatch, SpHeader};
use spacepackets::{CcsdsPacket, PacketId, PacketSequenceCtrl}; use spacepackets::{CcsdsPacket, PacketId, PacketSequenceCtrl};
use std::marker::PhantomData;
use alloc::sync::Arc;
#[cfg(feature = "std")]
use std::sync::{mpsc, Mutex};
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::sync::mpsc::SendError; use std::sync::mpsc::SendError;
#[cfg(feature = "std")]
use std::sync::MutexGuard; use std::sync::MutexGuard;
#[cfg(feature = "std")]
use std::sync::{mpsc, Mutex};
#[derive(Debug, Eq, PartialEq, Copy, Clone)] #[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub struct RequestId { pub struct RequestId {
@ -25,18 +27,6 @@ pub struct RequestId {
psc: PacketSequenceCtrl, psc: PacketSequenceCtrl,
} }
#[derive(Debug, Clone)]
pub enum VerificationError<E> {
SendError(E),
TimeStampError(TimestampError),
ByteConversionError(ByteConversionError),
PusError(PusError),
StoreError(StoreError),
}
#[derive(Debug, Clone)]
pub struct VerificationErrorWithToken<E, T>(VerificationError<E>, VerificationToken<T>);
impl RequestId { impl RequestId {
const SIZE_AS_BYTES: usize = size_of::<u32>(); const SIZE_AS_BYTES: usize = size_of::<u32>();
@ -69,71 +59,41 @@ impl RequestId {
} }
} }
#[derive(Debug, Clone)]
pub enum VerificationError<E> {
SendError(E),
TimeStampError(TimestampError),
ByteConversionError(ByteConversionError),
PusError(PusError),
}
#[derive(Debug, Clone)]
pub struct VerificationErrorWithToken<E, T>(VerificationError<E>, VerificationToken<T>);
pub trait VerificationSender<E> { pub trait VerificationSender<E> {
fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<E>>; fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<E>>;
} }
#[cfg(feature = "std")] #[derive(Debug, Clone, Copy)]
pub struct StdVerifSender { pub struct VerificationToken<STATE> {
pub ignore_poison_error: bool, state: PhantomData<STATE>,
tm_store: Arc<Mutex<LocalPool>>, req_id: RequestId,
tx: mpsc::Sender<StoreAddr>,
} }
#[cfg(feature = "std")] #[derive(Copy, Clone, Debug)]
impl StdVerifSender { pub struct StateNone;
pub fn new(tm_store: Arc<Mutex<LocalPool>>, tx: mpsc::Sender<StoreAddr>) -> Self { #[derive(Copy, Clone, Debug)]
Self { pub struct StateAccepted;
ignore_poison_error: true, #[derive(Copy, Clone, Debug)]
tx, pub struct StateStarted;
tm_store,
}
}
}
#[cfg(feature = "std")] impl<STATE> VerificationToken<STATE> {
#[derive(Debug, Eq, PartialEq)] fn new(req_id: RequestId) -> VerificationToken<StateNone> {
pub enum StdVerifSenderError { VerificationToken {
PoisonError, state: PhantomData,
SendError(SendError<StoreAddr>), req_id,
}
#[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(VerificationError::StoreError)?;
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,
))
} }
} }
}
}
}
pub struct VerificationReporter {
pub apid: u16,
pub dest_id: u16,
msg_count: u16,
source_data_buf: Vec<u8>,
} }
pub struct VerificationReporterCfg { pub struct VerificationReporterCfg {
@ -162,14 +122,14 @@ impl VerificationReporterCfg {
pub struct FailParams<'a> { pub struct FailParams<'a> {
time_stamp: &'a [u8], time_stamp: &'a [u8],
failure_code: &'a dyn EcssEnumeration, failure_code: &'a dyn EcssEnumeration,
failure_data: &'a [u8], failure_data: Option<&'a [u8]>,
} }
impl<'a> FailParams<'a> { impl<'a> FailParams<'a> {
pub fn new( pub fn new(
time_stamp: &'a [u8], time_stamp: &'a [u8],
failure_code: &'a impl EcssEnumeration, failure_code: &'a impl EcssEnumeration,
failure_data: &'a [u8], failure_data: Option<&'a [u8]>,
) -> Self { ) -> Self {
Self { Self {
time_stamp, time_stamp,
@ -188,7 +148,7 @@ impl<'a> FailParamsWithStep<'a> {
pub fn new( pub fn new(
time_stamp: &'a [u8], time_stamp: &'a [u8],
failure_code: &'a impl EcssEnumeration, failure_code: &'a impl EcssEnumeration,
failure_data: &'a [u8], failure_data: Option<&'a [u8]>,
step: &'a impl EcssEnumeration, step: &'a impl EcssEnumeration,
) -> Self { ) -> Self {
Self { Self {
@ -198,6 +158,13 @@ impl<'a> FailParamsWithStep<'a> {
} }
} }
pub struct VerificationReporter {
pub apid: u16,
pub dest_id: u16,
msg_count: u16,
source_data_buf: Vec<u8>,
}
impl VerificationReporter { impl VerificationReporter {
pub fn new(cfg: VerificationReporterCfg) -> Self { pub fn new(cfg: VerificationReporterCfg) -> Self {
Self { Self {
@ -225,7 +192,7 @@ impl VerificationReporter {
pub fn acceptance_success<E>( pub fn acceptance_success<E>(
&mut self, &mut self,
token: VerificationToken<StateNone>, token: VerificationToken<StateNone>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<VerificationToken<StateAccepted>, VerificationErrorWithToken<E, StateNone>> { ) -> Result<VerificationToken<StateAccepted>, VerificationErrorWithToken<E, StateNone>> {
let tm = self let tm = self
@ -250,7 +217,7 @@ impl VerificationReporter {
pub fn acceptance_failure<E>( pub fn acceptance_failure<E>(
mut self, mut self,
token: VerificationToken<StateNone>, token: VerificationToken<StateNone>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateNone>> { ) -> Result<(), VerificationErrorWithToken<E, StateNone>> {
let tm = self let tm = self
@ -266,7 +233,7 @@ impl VerificationReporter {
pub fn start_success<E>( pub fn start_success<E>(
&mut self, &mut self,
token: VerificationToken<StateAccepted>, token: VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<VerificationToken<StateStarted>, VerificationErrorWithToken<E, StateAccepted>> { ) -> Result<VerificationToken<StateStarted>, VerificationErrorWithToken<E, StateAccepted>> {
let tm = self let tm = self
@ -291,7 +258,7 @@ impl VerificationReporter {
pub fn start_failure<E>( pub fn start_failure<E>(
&mut self, &mut self,
token: VerificationToken<StateAccepted>, token: VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> { ) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
let tm = self let tm = self
@ -307,7 +274,7 @@ impl VerificationReporter {
pub fn step_success<E>( pub fn step_success<E>(
&mut self, &mut self,
token: &VerificationToken<StateAccepted>, token: &VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
step: impl EcssEnumeration, step: impl EcssEnumeration,
) -> Result<(), VerificationError<E>> { ) -> Result<(), VerificationError<E>> {
@ -320,7 +287,7 @@ impl VerificationReporter {
pub fn step_failure<E>( pub fn step_failure<E>(
&mut self, &mut self,
token: VerificationToken<StateAccepted>, token: VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
params: FailParamsWithStep, params: FailParamsWithStep,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> { ) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
let tm = self let tm = self
@ -336,7 +303,7 @@ impl VerificationReporter {
pub fn completion_success<E>( pub fn completion_success<E>(
&mut self, &mut self,
token: VerificationToken<StateAccepted>, token: VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
time_stamp: &[u8], time_stamp: &[u8],
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> { ) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
let tm = self let tm = self
@ -358,7 +325,7 @@ impl VerificationReporter {
pub fn completion_failure<E>( pub fn completion_failure<E>(
&mut self, &mut self,
token: VerificationToken<StateAccepted>, token: VerificationToken<StateAccepted>,
sender: &mut impl VerificationSender<E>, sender: &mut (impl VerificationSender<E> + ?Sized),
params: FailParams, params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> { ) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
let tm = self let tm = self
@ -411,12 +378,14 @@ impl VerificationReporter {
step: Option<&(impl EcssEnumeration + ?Sized)>, step: Option<&(impl EcssEnumeration + ?Sized)>,
) -> Result<PusTm, VerificationError<E>> { ) -> Result<PusTm, VerificationError<E>> {
let mut idx = 0; let mut idx = 0;
let mut source_data_len = RequestId::SIZE_AS_BYTES let mut source_data_len =
+ params.failure_code.byte_width() as usize RequestId::SIZE_AS_BYTES + params.failure_code.byte_width() as usize;
+ params.failure_data.len();
if let Some(step) = step { if let Some(step) = step {
source_data_len += step.byte_width() as usize; source_data_len += step.byte_width() as usize;
} }
if let Some(failure_data) = params.failure_data {
source_data_len += failure_data.len();
}
self.source_buffer_large_enough(source_data_len)?; self.source_buffer_large_enough(source_data_len)?;
req_id.to_bytes(&mut self.source_data_buf[0..RequestId::SIZE_AS_BYTES]); req_id.to_bytes(&mut self.source_data_buf[0..RequestId::SIZE_AS_BYTES]);
idx += RequestId::SIZE_AS_BYTES; idx += RequestId::SIZE_AS_BYTES;
@ -432,8 +401,9 @@ impl VerificationReporter {
) )
.map_err(|e| VerificationError::<E>::ByteConversionError(e))?; .map_err(|e| VerificationError::<E>::ByteConversionError(e))?;
idx += params.failure_code.byte_width() as usize; idx += params.failure_code.byte_width() as usize;
self.source_data_buf[idx..idx + params.failure_data.len()] if let Some(failure_data) = params.failure_data {
.copy_from_slice(params.failure_data); self.source_data_buf[idx..idx + failure_data.len()].copy_from_slice(failure_data);
}
let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap(); let mut sp_header = SpHeader::tm(self.apid, 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base( Ok(self.create_pus_verif_tm_base(
service, service,
@ -480,24 +450,153 @@ impl VerificationReporter {
} }
} }
#[derive(Debug, Clone, Copy)] pub struct VerificationReporterWithSender<E> {
pub struct VerificationToken<STATE> { reporter: VerificationReporter,
state: PhantomData<STATE>, sender: Box<dyn VerificationSender<E>>,
req_id: RequestId,
} }
#[derive(Copy, Clone, Debug)] impl<E> VerificationReporterWithSender<E> {
pub struct StateNone; pub fn new(cfg: VerificationReporterCfg, sender: Box<dyn VerificationSender<E>>) -> Self {
#[derive(Copy, Clone, Debug)] Self {
pub struct StateAccepted; reporter: VerificationReporter::new(cfg),
#[derive(Copy, Clone, Debug)] sender,
pub struct StateStarted; }
}
impl<STATE> VerificationToken<STATE> { delegate! {
fn new(req_id: RequestId) -> VerificationToken<StateNone> { to self.reporter {
VerificationToken { pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<StateNone>;
state: PhantomData, pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<StateNone>;
req_id, }
}
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,
token: &VerificationToken<StateAccepted>,
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,
token: VerificationToken<StateAccepted>,
params: FailParamsWithStep,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
self.reporter
.step_failure(token, self.sender.as_mut(), params)
}
pub fn completion_success(
&mut self,
token: VerificationToken<StateAccepted>,
time_stamp: &[u8],
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
self.reporter
.completion_success(token, self.sender.as_mut(), time_stamp)
}
pub fn completion_failure(
&mut self,
token: VerificationToken<StateAccepted>,
params: FailParams,
) -> Result<(), VerificationErrorWithToken<E, StateAccepted>> {
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,
))
}
}
} }
} }
} }
@ -505,11 +604,11 @@ impl<STATE> VerificationToken<STATE> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::pus::verification::{ use crate::pus::verification::{
RequestId, VerificationError, VerificationReporter, VerificationReporterCfg, FailParams, RequestId, VerificationError, VerificationReporter, VerificationReporterCfg,
VerificationSender, VerificationSender,
}; };
use alloc::vec::Vec; use alloc::vec::Vec;
use spacepackets::ecss::PusPacket; use spacepackets::ecss::{EcssEnumU16, PusPacket};
use spacepackets::tc::{PusTc, PusTcSecondaryHeader}; use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
use spacepackets::time::{CdsShortTimeProvider, TimeWriter}; use spacepackets::time::{CdsShortTimeProvider, TimeWriter};
use spacepackets::tm::{PusTm, PusTmSecondaryHeaderT}; use spacepackets::tm::{PusTm, PusTmSecondaryHeaderT};
@ -560,8 +659,9 @@ mod tests {
Ok(()) Ok(())
} }
} }
#[test] #[test]
pub fn test_basic() { pub fn test_basic_acceptance_success() {
let time_stamper = CdsShortTimeProvider::default(); let time_stamper = CdsShortTimeProvider::default();
let cfg = VerificationReporterCfg::new(time_stamper, 0x02); let cfg = VerificationReporterCfg::new(time_stamper, 0x02);
let mut reporter = VerificationReporter::new(cfg); let mut reporter = VerificationReporter::new(cfg);
@ -586,4 +686,34 @@ mod tests {
assert_eq!(info.additional_data, None); assert_eq!(info.additional_data, None);
assert_eq!(info.req_id, req_id); assert_eq!(info.req_id, req_id);
} }
#[test]
pub fn test_basic_acceptance_failure() {
let time_stamper = CdsShortTimeProvider::default();
let cfg = VerificationReporterCfg::new(time_stamper, 0x02);
let mut reporter = VerificationReporter::new(cfg);
reporter.dest_id = 5;
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, None, true);
let verif_token = reporter.add_tc(&pus_tc);
let req_id = RequestId::new(&pus_tc);
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");
assert_eq!(sender.service_queue.len(), 1);
let info = sender.service_queue.pop_front().unwrap();
assert_eq!(info.subservice, 2);
assert_eq!(info.time_stamp, stamp_buf);
assert_eq!(info.dest_id, 5);
assert_eq!(info.apid, TEST_APID);
assert_eq!(info.msg_counter, 0);
assert_eq!(info.additional_data, Some([0, 2].to_vec()));
assert_eq!(info.req_id, req_id);
}
} }