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

1632 lines
56 KiB
Rust
Raw Normal View History

2022-09-06 10:44:13 +02:00
//! # PUS Verification Service 1 Module
//!
//! This module allows packaging and sending PUS Service 1 packets. It is conforming to section
//! 8 of the PUS standard ECSS-E-ST-70-41C.
//!
2022-09-06 10:47:10 +02:00
//! The core object to report TC verification progress is the [VerificationReporter]. It exposes
2022-09-06 10:45:02 +02:00
//! an API which uses type-state programming to avoid calling the verification steps in
//! an invalid order.
2022-09-06 10:47:10 +02:00
//!
2022-09-07 15:19:16 +02:00
//! # Examples
//!
//! Basic single-threaded example where a full success sequence for a given ping telecommand is
2022-09-11 16:30:47 +02:00
//! executed. Note that the verification part could also be done in a separate thread.
//!
2022-09-07 15:19:16 +02:00
//! ```
//! use std::sync::{Arc, RwLock};
//! use std::time::Duration;
2022-09-10 13:34:04 +02:00
//! use fsrc_core::pool::{LocalPool, PoolCfg, PoolProvider, SharedPool};
2022-09-07 15:19:16 +02:00
//! use fsrc_core::pus::verification::{CrossbeamVerifSender, VerificationReporterCfg, VerificationReporterWithSender};
//! use spacepackets::ecss::PusPacket;
//! use spacepackets::SpHeader;
//! use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
//! use spacepackets::tm::PusTm;
//!
//! const EMPTY_STAMP: [u8; 7] = [0; 7];
//! const TEST_APID: u16 = 0x02;
//!
//! let pool_cfg = PoolCfg::new(vec![(10, 32), (10, 64), (10, 128), (10, 1024)]);
2022-09-10 13:34:04 +02:00
//! let shared_tm_pool: SharedPool = Arc::new(RwLock::new(Box::new(LocalPool::new(pool_cfg.clone()))));
2022-09-07 15:19:16 +02:00
//! let (verif_tx, verif_rx) = crossbeam_channel::bounded(10);
//! let sender = CrossbeamVerifSender::new(shared_tm_pool.clone(), verif_tx);
//! let cfg = VerificationReporterCfg::new(TEST_APID, 1, 2, 8);
//! let mut reporter = VerificationReporterWithSender::new(cfg , Box::new(sender));
//!
//! let mut sph = SpHeader::tc(TEST_APID, 0, 0).unwrap();
//! let tc_header = PusTcSecondaryHeader::new_simple(17, 1);
//! let pus_tc_0 = PusTc::new(&mut sph, tc_header, None, true);
//! let init_token = reporter.add_tc(&pus_tc_0);
//!
//! // Complete success sequence for a telecommand
//! let accepted_token = reporter.acceptance_success(init_token, &EMPTY_STAMP).unwrap();
//! let started_token = reporter.start_success(accepted_token, &EMPTY_STAMP).unwrap();
//! reporter.completion_success(started_token, &EMPTY_STAMP).unwrap();
//!
//! // Verify it arrives correctly on receiver end
//! let mut tm_buf: [u8; 1024] = [0; 1024];
//! let mut packet_idx = 0;
//! while packet_idx < 3 {
//! let addr = verif_rx.recv_timeout(Duration::from_millis(10)).unwrap();
//! let tm_len;
//! {
//! let mut rg = shared_tm_pool.write().expect("Error locking shared pool");
//! let store_guard = rg.read_with_guard(addr);
//! let slice = store_guard.read().expect("Error reading TM slice");
//! tm_len = slice.len();
//! tm_buf[0..tm_len].copy_from_slice(slice);
//! }
//! let (pus_tm, _) = PusTm::new_from_raw_slice(&tm_buf[0..tm_len], 7)
//! .expect("Error reading verification TM");
//! if packet_idx == 0 {
//! assert_eq!(pus_tm.subservice(), 1);
//! } else if packet_idx == 1 {
//! assert_eq!(pus_tm.subservice(), 3);
//! } else if packet_idx == 2 {
//! assert_eq!(pus_tm.subservice(), 7);
//! }
//! packet_idx += 1;
//! }
//! ```
//!
2022-09-11 16:31:38 +02:00
//! The [integration test](https://egit.irs.uni-stuttgart.de/rust/fsrc-launchpad/src/branch/main/fsrc-core/tests/verification_test.rs)
2022-09-07 15:19:16 +02:00
//! for the verification module contains examples how this module could be used in a more complex
//! context involving multiple threads
2022-09-05 00:20:32 +02:00
use alloc::boxed::Box;
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-11 14:51:25 +02:00
use core::fmt::{Display, Formatter};
2022-09-07 11:02:45 +02:00
use core::hash::{Hash, Hasher};
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-06 10:44:13 +02:00
use spacepackets::time::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")]
2022-09-10 13:34:04 +02:00
pub use stdmod::{
CrossbeamVerifSender, MpscVerifSender, SharedStdVerifReporterWithSender,
StdVerifReporterWithSender, StdVerifSenderError,
};
2022-09-06 10:44:13 +02:00
/// This is a request identifier as specified in 5.4.11.2 c. of the PUS standard
/// This field equivalent to the first two bytes of the CCSDS space packet header.
2022-09-07 11:02:45 +02:00
#[derive(Debug, Eq, Copy, Clone)]
2022-09-03 16:30:37 +02:00
pub struct RequestId {
version_number: u8,
packet_id: PacketId,
psc: PacketSequenceCtrl,
}
2022-09-11 14:51:25 +02:00
impl Display for RequestId {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
2022-09-11 14:58:51 +02:00
write!(f, "{:#08x}", self.raw())
2022-09-11 14:51:25 +02:00
}
}
2022-09-07 11:02:45 +02:00
impl Hash for RequestId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.raw().hash(state);
}
}
// Implement manually to satisfy derive_hash_xor_eq lint
impl PartialEq for RequestId {
fn eq(&self, other: &Self) -> bool {
self.version_number == other.version_number
&& self.packet_id == other.packet_id
&& self.psc == other.psc
}
}
2022-09-03 17:09:36 +02:00
impl RequestId {
2022-09-07 01:06:32 +02:00
pub const SIZE_AS_BYTES: usize = size_of::<u32>();
2022-09-03 17:09:36 +02:00
2022-09-07 01:06:32 +02:00
pub fn raw(&self) -> u32 {
((self.version_number as u32) << 29)
2022-09-03 17:09:36 +02:00
| ((self.packet_id.raw() as u32) << 16)
2022-09-07 01:06:32 +02:00
| self.psc.raw() as u32
}
pub fn to_bytes(&self, buf: &mut [u8]) {
let raw = self.raw();
2022-09-03 17:09:36 +02:00
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 {
2022-09-06 10:44:13 +02:00
/// This allows extracting the request ID from a given PUS telecommand.
2022-09-03 16:30:37 +02:00
pub fn new(tc: &PusTc) -> Self {
RequestId {
version_number: tc.ccsds_version(),
packet_id: tc.packet_id(),
psc: tc.psc(),
}
}
}
2022-09-06 10:44:13 +02:00
/// Generic error type which is also able to wrap a user send error with the user supplied type E.
2022-09-05 00:20:32 +02:00
#[derive(Debug, Clone)]
pub enum VerificationError<E> {
2022-09-06 10:44:13 +02:00
/// Errors related to sending the verification telemetry to a TM recipient
2022-09-05 00:20:32 +02:00
SendError(E),
2022-09-06 10:44:13 +02:00
/// Errors related to the time stamp format of the telemetry
2022-09-06 10:14:47 +02:00
TimestampError(TimestampError),
2022-09-06 10:44:13 +02:00
/// Errors related to byte conversion, for example unsufficient buffer size for given data
2022-09-05 00:20:32 +02:00
ByteConversionError(ByteConversionError),
2022-09-06 10:44:13 +02:00
/// Errors related to PUS packet format
2022-09-05 00:20:32 +02:00
PusError(PusError),
2022-09-03 16:30:37 +02:00
}
2022-09-10 19:57:58 +02:00
impl<E> From<ByteConversionError> for VerificationError<E> {
fn from(e: ByteConversionError) -> Self {
VerificationError::ByteConversionError(e)
}
}
2022-09-06 10:44:13 +02:00
/// If a verification operation fails, the passed token will be returned as well. This allows
/// re-trying the operation at a later point.
2022-09-05 00:20:32 +02:00
#[derive(Debug, Clone)]
pub struct VerificationErrorWithToken<E, T>(VerificationError<E>, VerificationToken<T>);
2022-09-06 10:44:13 +02:00
/// Generic trait for a user supplied sender object. This sender object is responsible for sending
/// PUS Service 1 Verification Telemetry to a verification TM recipient. The [Downcast] trait
/// is implemented to allow passing the sender as a boxed trait object and still retrieve the
/// concrete type at a later point.
2022-09-07 11:02:45 +02:00
pub trait VerificationSender<E>: Downcast + Send {
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-06 10:44:13 +02:00
/// Support token to allow type-state programming. This prevents calling the verification
/// steps in an invalid order.
2022-09-06 10:14:47 +02:00
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
2022-09-05 00:20:32 +02:00
pub struct VerificationToken<STATE> {
state: PhantomData<STATE>,
req_id: RequestId,
}
2022-09-06 10:14:47 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2022-09-05 00:20:32 +02:00
pub struct StateNone;
2022-09-06 10:14:47 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2022-09-05 00:20:32 +02:00
pub struct StateAccepted;
2022-09-06 10:14:47 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2022-09-05 00:20:32 +02:00
pub struct StateStarted;
impl<STATE> VerificationToken<STATE> {
fn new(req_id: RequestId) -> VerificationToken<StateNone> {
VerificationToken {
state: PhantomData,
req_id,
}
}
2022-09-07 11:02:45 +02:00
pub fn req_id(&self) -> RequestId {
self.req_id
}
}
2022-09-03 17:09:36 +02:00
pub struct VerificationReporterCfg {
pub apid: u16,
pub dest_id: u16,
2022-09-06 10:44:13 +02:00
pub step_field_width: usize,
pub fail_code_field_width: usize,
2022-09-03 17:09:36 +02:00
pub max_fail_data_len: usize,
}
impl VerificationReporterCfg {
2022-09-06 10:44:13 +02:00
/// Create a new configuration for the verification reporter. This includes following parameters:
///
/// 1. Destination ID and APID, which could remain constant after construction. These parameters
/// can be tweaked in the reporter after construction.
/// 2. Maximum expected field sizes. The parameters of this configuration struct will be used
/// to determine required maximum buffer sizes and there will be no addition allocation or
/// configurable buffer parameters after [VerificationReporter] construction.
///
/// This means the user has supply the maximum expected field sizes of verification messages
/// before constructing the reporter.
pub fn new(
apid: u16,
step_field_width: usize,
fail_code_field_width: usize,
max_fail_data_len: usize,
) -> Self {
2022-09-03 17:09:36 +02:00
Self {
apid,
dest_id: 0,
2022-09-06 10:44:13 +02:00
step_field_width,
fail_code_field_width,
max_fail_data_len,
2022-09-03 17:09:36 +02:00
}
}
2022-09-03 16:30:37 +02:00
}
2022-09-06 10:44:13 +02:00
/// Composite helper struct to pass failure parameters to the [VerificationReporter]
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-06 10:44:13 +02:00
/// Composite helper struct to pass step failure parameters to the [VerificationReporter]
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-06 10:44:13 +02:00
/// Primary verification handler. It provides an API to send PUS 1 verification telemetry packets
/// and verify the various steps of telecommand handling as specified in the PUS standard.
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
2022-09-06 10:44:13 +02:00
+ cfg.fail_code_field_width as usize
2022-09-03 17:09:36 +02:00
+ cfg.max_fail_data_len
],
}
}
2022-09-06 10:14:47 +02:00
pub fn allowed_source_data_len(&self) -> usize {
self.source_data_buf.capacity()
}
2022-09-06 10:44:13 +02:00
/// Initialize verification handling by passing a TC reference. This returns a token required
/// to call the acceptance functions
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<StateNone> {
self.add_tc_with_req_id(RequestId::new(pus_tc))
}
2022-09-06 10:44:13 +02:00
/// Same as [Self::add_tc] but pass a request ID instead of the direct telecommand.
/// This can be useful if the executing thread does not have full access to the telecommand.
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)
}
2022-09-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 1\] packet, see 8.1.2.1 of the PUS standard
2022-09-03 16:30:37 +02:00
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 2\] packet, see 8.1.2.2 of the PUS standard
2022-09-03 17:09:36 +02:00
pub fn acceptance_failure<E>(
&mut self,
2022-09-03 17:09:36 +02:00
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 3\] packet, see 8.1.2.3 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::acceptance_success].
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 4\] packet, see 8.1.2.4 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::acceptance_success]. It consumes
/// the token because verification handling is done.
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(())
}
2022-09-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 5\] packet, see 8.1.2.5 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::start_success].
2022-09-03 18:51:01 +02:00
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 6\] packet, see 8.1.2.6 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::start_success]. It consumes the
/// token because verification handling is done.
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 7\] packet, see 8.1.2.7 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::start_success]. It consumes the
/// token because verification handling is done.
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-06 10:44:13 +02:00
/// Package and send a PUS TM\[1, 8\] packet, see 8.1.2.8 of the PUS standard.
///
/// Requires a token previously acquired by calling [Self::start_success]. It consumes the
/// token because verification handling is done.
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-10 19:57:58 +02:00
params.failure_code.to_bytes(
&mut self.source_data_buf[idx..idx + params.failure_code.byte_width() as usize],
)?;
2022-09-04 22:24:36 +02:00
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-06 10:44:13 +02:00
/// Helper object which caches the sender passed as a trait object. Provides the same
/// API as [VerificationReporter] but without the explicit sender arguments.
2022-09-05 00:20:32 +02:00
pub struct VerificationReporterWithSender<E> {
reporter: VerificationReporter,
2022-09-07 11:02:45 +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-07 11:02:45 +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,
2022-09-07 11:02:45 +02:00
sender: Box<dyn VerificationSender<E>>,
2022-09-06 00:17:52 +02:00
) -> 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,
2022-09-05 00:20:32 +02:00
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")]
2022-09-07 11:02:45 +02:00
mod stdmod {
2022-09-10 13:34:04 +02:00
use crate::pool::{ShareablePoolProvider, SharedPool, StoreAddr, StoreError};
use crate::pus::verification::{
VerificationError, VerificationReporterWithSender, VerificationSender,
};
2022-09-07 11:02:45 +02:00
use delegate::delegate;
use spacepackets::tm::PusTm;
2022-09-10 13:34:04 +02:00
use std::sync::{mpsc, Arc, Mutex, RwLockWriteGuard};
pub type StdVerifReporterWithSender = VerificationReporterWithSender<StdVerifSenderError>;
pub type SharedStdVerifReporterWithSender = Arc<Mutex<StdVerifReporterWithSender>>;
2022-09-05 00:20:32 +02:00
2022-09-07 11:02:45 +02:00
#[derive(Debug, Eq, PartialEq)]
pub enum StdVerifSenderError {
PoisonError,
StoreError(StoreError),
RxDisconnected(StoreAddr),
}
2022-09-10 19:57:58 +02:00
impl From<StoreError> for StdVerifSenderError {
fn from(e: StoreError) -> Self {
StdVerifSenderError::StoreError(e)
}
}
impl From<StoreError> for VerificationError<StdVerifSenderError> {
fn from(e: StoreError) -> Self {
VerificationError::SendError(e.into())
}
}
2022-09-07 11:02:45 +02:00
trait SendBackend: Send {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr>;
}
struct StdSenderBase<S> {
pub ignore_poison_error: bool,
2022-09-10 13:34:04 +02:00
tm_store: SharedPool,
2022-09-07 11:02:45 +02:00
tx: S,
}
impl<S: SendBackend> StdSenderBase<S> {
2022-09-10 13:34:04 +02:00
pub fn new(tm_store: SharedPool, tx: S) -> Self {
2022-09-07 11:02:45 +02:00
Self {
ignore_poison_error: false,
tm_store,
tx,
}
2022-09-05 00:20:32 +02:00
}
}
2022-09-10 13:34:04 +02:00
unsafe impl<S: Sync> Sync for StdSenderBase<S> {}
unsafe impl<S: Send> Send for StdSenderBase<S> {}
2022-09-07 11:02:45 +02:00
impl SendBackend for mpsc::Sender<StoreAddr> {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr> {
self.send(addr).map_err(|_| addr)
}
}
2022-09-07 01:06:32 +02:00
2022-09-10 13:34:04 +02:00
pub struct MpscVerifSender {
2022-09-07 11:02:45 +02:00
base: StdSenderBase<mpsc::Sender<StoreAddr>>,
}
2022-09-05 00:20:32 +02:00
2022-09-07 14:57:44 +02:00
/// Verification sender with a [mpsc::Sender] backend.
/// It implements the [VerificationSender] trait to be used as PUS Verification TM sender.
2022-09-10 13:34:04 +02:00
impl MpscVerifSender {
pub fn new(tm_store: SharedPool, tx: mpsc::Sender<StoreAddr>) -> Self {
2022-09-07 11:02:45 +02:00
Self {
base: StdSenderBase::new(tm_store, tx),
}
}
}
//noinspection RsTraitImplementation
2022-09-10 13:34:04 +02:00
impl VerificationSender<StdVerifSenderError> for MpscVerifSender {
2022-09-07 11:02:45 +02:00
delegate!(
to self.base {
fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<StdVerifSenderError>>;
}
);
}
2022-09-10 13:34:04 +02:00
unsafe impl Sync for MpscVerifSender {}
unsafe impl Send for MpscVerifSender {}
2022-09-07 11:02:45 +02:00
impl SendBackend for crossbeam_channel::Sender<StoreAddr> {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr> {
self.send(addr).map_err(|_| addr)
}
}
2022-09-07 14:57:44 +02:00
/// Verification sender with a [crossbeam_channel::Sender] backend.
/// It implements the [VerificationSender] trait to be used as PUS Verification TM sender
2022-09-07 11:02:45 +02:00
pub struct CrossbeamVerifSender {
base: StdSenderBase<crossbeam_channel::Sender<StoreAddr>>,
}
impl CrossbeamVerifSender {
2022-09-10 13:34:04 +02:00
pub fn new(tm_store: SharedPool, tx: crossbeam_channel::Sender<StoreAddr>) -> Self {
2022-09-07 11:02:45 +02:00
Self {
base: StdSenderBase::new(tm_store, tx),
}
}
}
//noinspection RsTraitImplementation
impl VerificationSender<StdVerifSenderError> for CrossbeamVerifSender {
delegate!(
to self.base {
fn send_verification_tm(&mut self, tm: PusTm) -> Result<(), VerificationError<StdVerifSenderError>>;
}
);
}
unsafe impl Sync for CrossbeamVerifSender {}
unsafe impl Send for CrossbeamVerifSender {}
impl<S: SendBackend + 'static> VerificationSender<StdVerifSenderError> for StdSenderBase<S> {
fn send_verification_tm(
&mut self,
tm: PusTm,
) -> Result<(), VerificationError<StdVerifSenderError>> {
2022-09-10 13:34:04 +02:00
let operation = |mut mg: RwLockWriteGuard<ShareablePoolProvider>| {
2022-09-10 19:57:58 +02:00
let (addr, buf) = mg.free_element(tm.len_packed())?;
2022-09-07 11:02:45 +02:00
tm.write_to(buf).map_err(VerificationError::PusError)?;
2022-09-07 14:48:43 +02:00
drop(mg);
2022-09-07 11:02:45 +02:00
self.tx.send(addr).map_err(|_| {
VerificationError::SendError(StdVerifSenderError::RxDisconnected(addr))
})?;
Ok(())
};
match self.tm_store.write() {
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-05 00:20:32 +02:00
}
}
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;
2022-09-06 10:14:47 +02:00
use alloc::format;
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::tm::{PusTm, PusTmSecondaryHeaderT};
2022-09-06 10:14:47 +02:00
use spacepackets::{ByteConversionError, 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 10:14:47 +02:00
impl VerificationSender<()> for TestSender {
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()[0..7]);
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(())
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct DummyError {}
#[derive(Default)]
struct FallibleSender {}
impl VerificationSender<DummyError> for FallibleSender {
fn send_verification_tm(&mut self, _: PusTm) -> Result<(), VerificationError<DummyError>> {
Err(VerificationError::SendError(DummyError {}))
}
}
2022-09-06 00:17:52 +02:00
struct TestBase<'a> {
vr: VerificationReporter,
#[allow(dead_code)]
tc: PusTc<'a>,
}
impl<'a> TestBase<'a> {
fn rep(&mut self) -> &mut VerificationReporter {
&mut self.vr
}
}
2022-09-06 00:17:52 +02:00
struct TestBaseWithHelper<'a, E> {
helper: VerificationReporterWithSender<E>,
#[allow(dead_code)]
tc: PusTc<'a>,
}
impl<'a, E> TestBaseWithHelper<'a, E> {
fn rep(&mut self) -> &mut VerificationReporter {
&mut self.helper.reporter
}
}
fn base_reporter() -> VerificationReporter {
2022-09-06 10:44:13 +02:00
let cfg = VerificationReporterCfg::new(TEST_APID, 1, 2, 8);
VerificationReporter::new(cfg)
2022-09-06 00:17:52 +02:00
}
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(api_sel: bool) -> (TestBase<'static>, VerificationToken<StateNone>) {
let mut reporter = base_reporter();
2022-09-06 00:17:52 +02:00
let (tc, req_id) = base_tc_init(None);
let init_tok;
if api_sel {
init_tok = reporter.add_tc_with_req_id(req_id);
} else {
init_tok = reporter.add_tc(&tc);
}
(TestBase { vr: reporter, tc }, init_tok)
2022-09-06 00:17:52 +02:00
}
fn base_with_helper_init() -> (
TestBaseWithHelper<'static, ()>,
VerificationToken<StateNone>,
) {
let mut reporter = base_reporter();
let (tc, _) = base_tc_init(None);
2022-09-06 00:17:52 +02:00
let init_tok = reporter.add_tc(&tc);
let sender = TestSender::default();
let helper = VerificationReporterWithSender::new_from_reporter(reporter, Box::new(sender));
(TestBaseWithHelper { helper, tc }, init_tok)
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(false);
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");
acceptance_check(&mut sender, &tok.req_id);
2022-09-06 00:17:52 +02:00
}
#[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, &tok.req_id);
2022-09-03 16:30:37 +02:00
}
2022-09-05 00:20:32 +02:00
2022-09-06 10:14:47 +02:00
#[test]
fn test_acceptance_send_fails() {
let (mut b, tok) = base_init(false);
let mut faulty_sender = FallibleSender::default();
let res =
b.vr.acceptance_success(tok, &mut faulty_sender, &EMPTY_STAMP);
assert!(res.is_err());
let err = res.unwrap_err();
assert_eq!(err.1, tok);
match err.0 {
VerificationError::SendError(e) => {
assert_eq!(e, DummyError {})
}
_ => panic!("{}", format!("Unexpected error {:?}", err.0)),
}
}
fn acceptance_fail_check(sender: &mut TestSender, req_id: RequestId, stamp_buf: [u8; 7]) {
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]
fn test_basic_acceptance_failure() {
let (mut b, tok) = base_init(true);
b.rep().dest_id = 5;
let stamp_buf = [1, 2, 3, 4, 5, 6, 7];
let mut sender = TestSender::default();
let fail_code = EcssEnumU16::new(2);
let fail_params = FailParams::new(stamp_buf.as_slice(), &fail_code, None);
b.vr.acceptance_failure(tok, &mut sender, fail_params)
.expect("Sending acceptance success failed");
acceptance_fail_check(&mut sender, tok.req_id, stamp_buf);
}
#[test]
fn test_basic_acceptance_failure_with_helper() {
let (mut b, tok) = base_with_helper_init();
b.rep().dest_id = 5;
let stamp_buf = [1, 2, 3, 4, 5, 6, 7];
let fail_code = EcssEnumU16::new(2);
let fail_params = FailParams::new(stamp_buf.as_slice(), &fail_code, None);
b.helper
.acceptance_failure(tok, fail_params)
.expect("Sending acceptance success failed");
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
acceptance_fail_check(sender, tok.req_id, stamp_buf);
}
2022-09-06 10:14:47 +02:00
#[test]
fn test_acceptance_fail_data_too_large() {
let (mut b, tok) = base_with_helper_init();
b.rep().dest_id = 5;
let stamp_buf = [1, 2, 3, 4, 5, 6, 7];
let fail_code = EcssEnumU16::new(2);
let fail_data: [u8; 16] = [0; 16];
// 4 req ID + 1 byte step + 2 byte error code + 8 byte fail data
assert_eq!(b.rep().allowed_source_data_len(), 15);
let fail_params =
FailParams::new(stamp_buf.as_slice(), &fail_code, Some(fail_data.as_slice()));
let res = b.helper.acceptance_failure(tok, fail_params);
assert!(res.is_err());
let err_with_token = res.unwrap_err();
assert_eq!(err_with_token.1, tok);
match err_with_token.0 {
VerificationError::ByteConversionError(e) => match e {
ByteConversionError::ToSliceTooSmall(missmatch) => {
assert_eq!(
missmatch.expected,
fail_data.len() + RequestId::SIZE_AS_BYTES + fail_code.byte_width()
);
assert_eq!(missmatch.found, b.rep().allowed_source_data_len());
}
_ => {
panic!("{}", format!("Unexpected error {:?}", e))
}
},
_ => {
panic!("{}", format!("Unexpected error {:?}", err_with_token.0))
}
}
}
#[test]
fn test_basic_acceptance_failure_with_fail_data() {
let (mut b, tok) = base_init(false);
2022-09-06 00:17:52 +02:00
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()));
b.vr.acceptance_failure(tok, &mut sender, fail_params)
2022-09-06 00:17:52 +02:00
.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: tok.req_id,
2022-09-06 00:17:52 +02:00
};
assert_eq!(sender.service_queue.len(), 1);
let info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
}
fn start_fail_check(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) {
2022-09-06 00:17:52 +02:00
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_start_failure() {
let (mut b, tok) = base_init(false);
2022-09-06 00:17:52 +02:00
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 =
b.vr.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let empty =
b.vr.start_failure(accepted_token, &mut sender, fail_params)
.expect("Start failure failure");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
start_fail_check(&mut sender, tok.req_id, fail_data_raw);
}
#[test]
fn test_start_failure_with_helper() {
let (mut b, tok) = base_with_helper_init();
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 = b
.helper
.acceptance_success(tok, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let empty = b
.helper
.start_failure(accepted_token, fail_params)
.expect("Start failure failure");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
start_fail_check(sender, tok.req_id, fail_data_raw);
}
fn step_success_check(sender: &mut TestSender, req_id: RequestId) {
2022-09-06 00:17:52 +02:00
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_steps_success() {
let (mut b, tok) = base_init(false);
2022-09-06 00:17:52 +02:00
let mut sender = TestSender::default();
let accepted_token = b
.rep()
.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
2022-09-06 00:17:52 +02:00
.expect("Sending acceptance success failed");
let started_token = b
.rep()
2022-09-06 00:17:52 +02:00
.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let mut empty = b
.rep()
2022-09-06 00:17:52 +02:00
.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(0),
)
.expect("Sending step 0 success failed");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
empty =
b.vr.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(1),
)
.expect("Sending step 1 success failed");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
assert_eq!(sender.service_queue.len(), 4);
step_success_check(&mut sender, tok.req_id);
}
2022-09-06 00:17:52 +02:00
#[test]
fn test_steps_success_with_helper() {
let (mut b, tok) = base_with_helper_init();
let accepted_token = b
.helper
.acceptance_success(tok, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = b
.helper
.start_success(accepted_token, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let mut empty = b
.helper
.step_success(&started_token, &EMPTY_STAMP, EcssEnumU8::new(0))
.expect("Sending step 0 success failed");
assert_eq!(empty, ());
empty = b
.helper
.step_success(&started_token, &EMPTY_STAMP, EcssEnumU8::new(1))
.expect("Sending step 1 success failed");
assert_eq!(empty, ());
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
assert_eq!(sender.service_queue.len(), 4);
step_success_check(sender, tok.req_id);
}
fn check_step_failure(sender: &mut TestSender, req_id: RequestId, fail_data_raw: [u8; 4]) {
assert_eq!(sender.service_queue.len(), 4);
2022-09-06 00:17:52 +02:00
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_step_failure() {
let (mut b, tok) = base_init(false);
2022-09-06 00:17:52 +02:00
let mut sender = TestSender::default();
let req_id = tok.req_id;
2022-09-06 00:17:52 +02:00
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 =
b.vr.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token =
b.vr.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let mut empty =
b.vr.step_success(
&started_token,
&mut sender,
&EMPTY_STAMP,
EcssEnumU8::new(0),
)
.expect("Sending completion success failed");
assert_eq!(empty, ());
empty =
b.vr.step_failure(started_token, &mut sender, fail_params)
.expect("Step failure failed");
assert_eq!(empty, ());
check_step_failure(&mut sender, req_id, fail_data_raw);
}
#[test]
fn test_steps_failure_with_helper() {
let (mut b, tok) = base_with_helper_init();
let req_id = tok.req_id;
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()),
);
2022-09-06 00:17:52 +02:00
let accepted_token = b
.helper
.acceptance_success(tok, &EMPTY_STAMP)
2022-09-06 00:17:52 +02:00
.expect("Sending acceptance success failed");
let started_token = b
.helper
.start_success(accepted_token, &[0, 1, 0, 1, 0, 1, 0])
2022-09-06 00:17:52 +02:00
.expect("Sending start success failed");
let mut empty = b
.helper
.step_success(&started_token, &EMPTY_STAMP, EcssEnumU8::new(0))
.expect("Sending completion success failed");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
empty = b
.helper
.step_failure(started_token, fail_params)
.expect("Step failure failed");
assert_eq!(empty, ());
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
check_step_failure(sender, req_id, fail_data_raw);
}
2022-09-06 00:17:52 +02:00
fn completion_fail_check(sender: &mut TestSender, req_id: RequestId) {
2022-09-06 00:17:52 +02:00
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_completion_failure() {
let (mut b, tok) = base_init(false);
2022-09-06 00:17:52 +02:00
let mut sender = TestSender::default();
let req_id = tok.req_id;
let fail_code = EcssEnumU32::new(0x1020);
let fail_params = FailParams::new(&EMPTY_STAMP, &fail_code, None);
let accepted_token =
b.vr.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token =
b.vr.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let empty =
b.vr.completion_failure(started_token, &mut sender, fail_params)
.expect("Completion failure");
assert_eq!(empty, ());
completion_fail_check(&mut sender, req_id);
}
#[test]
fn test_completion_failure_with_helper() {
let (mut b, tok) = base_with_helper_init();
let req_id = tok.req_id;
let fail_code = EcssEnumU32::new(0x1020);
let fail_params = FailParams::new(&EMPTY_STAMP, &fail_code, None);
let accepted_token = b
.helper
.acceptance_success(tok, &EMPTY_STAMP)
2022-09-06 00:17:52 +02:00
.expect("Sending acceptance success failed");
let started_token = b
.helper
.start_success(accepted_token, &[0, 1, 0, 1, 0, 1, 0])
2022-09-06 00:17:52 +02:00
.expect("Sending start success failed");
let empty = b
.helper
.completion_failure(started_token, fail_params)
.expect("Completion failure");
2022-09-06 00:17:52 +02:00
assert_eq!(empty, ());
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
completion_fail_check(sender, req_id);
}
fn completion_success_check(sender: &mut TestSender, req_id: RequestId) {
2022-09-06 00:17:52 +02:00
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
}
#[test]
fn test_complete_success_sequence() {
let (mut b, tok) = base_init(false);
let mut sender = TestSender::default();
let accepted_token =
b.vr.acceptance_success(tok, &mut sender, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token =
b.vr.start_success(accepted_token, &mut sender, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let empty =
b.vr.completion_success(started_token, &mut sender, &EMPTY_STAMP)
.expect("Sending completion success failed");
assert_eq!(empty, ());
completion_success_check(&mut sender, tok.req_id);
}
#[test]
fn test_complete_success_sequence_with_helper() {
let (mut b, tok) = base_with_helper_init();
let accepted_token = b
.helper
.acceptance_success(tok, &EMPTY_STAMP)
.expect("Sending acceptance success failed");
let started_token = b
.helper
.start_success(accepted_token, &[0, 1, 0, 1, 0, 1, 0])
.expect("Sending start success failed");
let empty = b
.helper
.completion_success(started_token, &EMPTY_STAMP)
.expect("Sending completion success failed");
assert_eq!(empty, ());
let sender: &mut TestSender = b.helper.sender.downcast_mut().unwrap();
completion_success_check(sender, tok.req_id);
}
2022-09-03 16:30:37 +02:00
}