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

1987 lines
68 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
//! ```
2022-12-30 23:28:33 +01:00
//! use std::sync::{Arc, mpsc, RwLock};
2022-09-07 15:19:16 +02:00
//! use std::time::Duration;
2022-11-20 19:54:14 +01:00
//! use satrs_core::pool::{LocalPool, PoolCfg, PoolProvider, SharedPool};
2022-12-30 23:28:33 +01:00
//! use satrs_core::pus::verification::{MpscVerifSender, VerificationReporterCfg, VerificationReporterWithSender};
//! use satrs_core::seq_count::SimpleSeqCountProvider;
2022-09-07 15:19:16 +02:00
//! 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-12-30 23:28:33 +01:00
//! let (verif_tx, verif_rx) = mpsc::channel();
//! let sender = MpscVerifSender::new(shared_tm_pool.clone(), verif_tx);
//! let cfg = VerificationReporterCfg::new(TEST_APID, Box::new(SimpleSeqCountProvider::default()), 1, 2, 8).unwrap();
2022-11-21 10:28:31 +01:00
//! let mut reporter = VerificationReporterWithSender::new(&cfg , Box::new(sender));
2022-09-07 15:19:16 +02:00
//!
2022-12-04 20:00:20 +01:00
//! let mut sph = SpHeader::tc_unseg(TEST_APID, 0, 0).unwrap();
2022-09-07 15:19:16 +02:00
//! 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);
//! }
2022-09-13 10:43:07 +02:00
//! let (pus_tm, _) = PusTm::from_bytes(&tm_buf[0..tm_len], 7)
2022-09-07 15:19:16 +02:00
//! .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
2023-01-03 01:15:17 +01:00
use crate::pus::{source_buffer_large_enough, EcssTmError, EcssTmSenderBase};
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-12-30 23:09:58 +01:00
#[cfg(feature = "alloc")]
2022-09-05 00:20:32 +02:00
use delegate::delegate;
2023-01-02 23:32:31 +01:00
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
2022-09-11 20:51:14 +02:00
use spacepackets::ecss::EcssEnumeration;
2022-09-03 16:30:37 +02:00
use spacepackets::tc::PusTc;
use spacepackets::tm::{PusTm, PusTmSecondaryHeader};
use spacepackets::{CcsdsPacket, PacketId, PacketSequenceCtrl};
2022-09-11 20:51:14 +02:00
use spacepackets::{SpHeader, MAX_APID};
pub use crate::seq_count::SimpleSeqCountProvider;
2022-09-11 20:51:14 +02:00
#[cfg(feature = "alloc")]
2022-12-30 23:09:58 +01:00
pub use alloc_mod::{
2022-11-21 10:28:31 +01:00
VerificationReporterCfg, VerificationReporterWithBuf, VerificationReporterWithSender,
};
2022-11-21 10:28:31 +01:00
use crate::seq_count::SequenceCountProvider;
2023-01-03 00:22:48 +01:00
#[cfg(all(feature = "crossbeam", feature = "std"))]
2022-12-30 23:28:33 +01:00
pub use stdmod::CrossbeamVerifSender;
#[cfg(feature = "std")]
2022-09-10 13:34:04 +02:00
pub use stdmod::{
2022-12-30 23:28:33 +01:00
MpscVerifSender, SharedStdVerifReporterWithSender, StdVerifReporterWithSender,
StdVerifSenderError,
2022-09-10 13:34:04 +02:00
};
2022-09-11 21:14:19 +02:00
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
2023-01-02 23:54:59 +01:00
pub enum Subservice {
2022-09-11 21:14:19 +02:00
TmAcceptanceSuccess = 1,
TmAcceptanceFailure = 2,
TmStartSuccess = 3,
TmStartFailure = 4,
TmStepSuccess = 5,
TmStepFailure = 6,
TmCompletionSuccess = 7,
TmCompletionFailure = 8,
}
2023-01-02 23:54:59 +01:00
impl From<Subservice> for u8 {
fn from(enumeration: Subservice) -> Self {
2022-09-11 21:14:19 +02:00
enumeration as u8
}
}
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)]
2023-01-02 23:32:31 +01:00
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
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
/// 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)]
2022-09-11 20:51:14 +02:00
pub struct VerificationErrorWithToken<E, T>(EcssTmError<E>, VerificationToken<T>);
2022-09-06 00:17:52 +02:00
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-11-13 21:07:16 +01:00
pub struct TcStateNone;
2022-09-06 10:14:47 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2022-11-13 21:07:16 +01:00
pub struct TcStateAccepted;
2022-09-06 10:14:47 +02:00
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
2022-11-13 21:07:16 +01:00
pub struct TcStateStarted;
2022-10-15 19:36:13 +02:00
2022-11-12 23:13:55 +01:00
#[derive(Debug, Eq, PartialEq)]
2022-11-13 21:07:16 +01:00
pub enum TcStateToken {
None(VerificationToken<TcStateNone>),
Accepted(VerificationToken<TcStateAccepted>),
Started(VerificationToken<TcStateStarted>),
2022-10-15 19:36:13 +02:00
}
2022-09-05 00:20:32 +02:00
2022-11-13 21:07:16 +01:00
impl From<VerificationToken<TcStateNone>> for TcStateToken {
fn from(t: VerificationToken<TcStateNone>) -> Self {
TcStateToken::None(t)
2022-11-12 23:13:55 +01:00
}
}
2022-11-13 21:07:16 +01:00
impl From<VerificationToken<TcStateAccepted>> for TcStateToken {
fn from(t: VerificationToken<TcStateAccepted>) -> Self {
TcStateToken::Accepted(t)
2022-11-12 23:13:55 +01:00
}
}
2022-11-13 21:07:16 +01:00
impl From<VerificationToken<TcStateStarted>> for TcStateToken {
fn from(t: VerificationToken<TcStateStarted>) -> Self {
TcStateToken::Started(t)
2022-11-12 23:13:55 +01:00
}
}
2022-09-05 00:20:32 +02:00
impl<STATE> VerificationToken<STATE> {
2022-11-13 21:07:16 +01:00
fn new(req_id: RequestId) -> VerificationToken<TcStateNone> {
2022-09-05 00:20:32 +02:00
VerificationToken {
state: PhantomData,
req_id,
}
}
2022-09-07 11:02:45 +02:00
pub fn req_id(&self) -> RequestId {
self.req_id
}
}
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,
}
}
}
#[derive(Clone)]
2022-09-11 20:51:14 +02:00
pub struct VerificationReporterBasic {
2022-09-05 00:20:32 +02:00
pub dest_id: u16,
2022-09-11 20:51:14 +02:00
apid: u16,
2022-09-05 00:20:32 +02:00
}
2022-09-11 20:51:14 +02:00
impl VerificationReporterBasic {
pub fn new(apid: u16) -> Option<Self> {
if apid > MAX_APID {
return None;
}
2022-11-21 10:28:31 +01:00
Some(Self { apid, dest_id: 0 })
2022-09-11 20:51:14 +02:00
}
pub fn set_apid(&mut self, apid: u16) -> bool {
if apid > MAX_APID {
return false;
2022-09-03 17:09:36 +02:00
}
2022-09-11 20:51:14 +02:00
self.apid = apid;
true
2022-09-03 17:09:36 +02:00
}
2022-09-11 20:51:14 +02:00
pub fn apid(&self) -> u16 {
self.apid
}
pub fn dest_id(&self) -> u16 {
self.dest_id
}
pub fn set_dest_id(&mut self, dest_id: u16) {
self.dest_id = dest_id;
2022-09-06 10:14:47 +02:00
}
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
2022-11-13 21:07:16 +01:00
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<TcStateNone> {
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.
2022-11-13 21:07:16 +01:00
pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<TcStateNone> {
VerificationToken::<TcStateNone>::new(req_id)
2022-09-03 16:30:37 +02:00
}
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,
2022-09-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateAccepted>, VerificationErrorWithToken<E, TcStateNone>>
{
2022-09-04 22:24:36 +02:00
let tm = self
.create_pus_verif_success_tm(
2022-09-11 20:51:14 +02:00
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmAcceptanceSuccess.into(),
seq_counter.get(),
2022-09-04 22:24:36 +02:00
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
//seq_counter.get_and_increment()
2022-09-03 16:30:37 +02:00
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-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateNone>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-11 20:51:14 +02:00
.create_pus_verif_fail_tm(
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmAcceptanceFailure.into(),
seq_counter.get(),
2022-09-11 20:51:14 +02:00
&token.req_id,
None::<&dyn EcssEnumeration>,
&params,
)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 17:09:36 +02:00
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,
2022-09-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
2022-09-04 22:24:36 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateStarted>, VerificationErrorWithToken<E, TcStateAccepted>>
{
2022-09-04 22:24:36 +02:00
let tm = self
.create_pus_verif_success_tm(
2022-09-11 20:51:14 +02:00
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmStartSuccess.into(),
seq_counter.get(),
2022-09-04 22:24:36 +02:00
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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,
2022-09-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateAccepted>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-11 20:51:14 +02:00
.create_pus_verif_fail_tm(
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmStartFailure.into(),
seq_counter.get(),
2022-09-11 20:51:14 +02:00
&token.req_id,
None::<&dyn EcssEnumeration>,
&params,
)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: &VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
time_stamp: &[u8],
2022-09-03 18:51:01 +02:00
step: impl EcssEnumeration,
2022-09-11 20:51:14 +02:00
) -> Result<(), EcssTmError<E>> {
2022-09-11 21:14:19 +02:00
let tm = self.create_pus_verif_success_tm(
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmStepSuccess.into(),
seq_counter.get(),
2022-09-11 21:14:19 +02:00
&token.req_id,
time_stamp,
Some(&step),
)?;
2022-09-11 20:51:14 +02:00
sender.send_tm(tm)?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParamsWithStep,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-11 21:14:19 +02:00
.create_pus_verif_fail_tm(
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmStepFailure.into(),
seq_counter.get(),
2022-09-11 21:14:19 +02:00
&token.req_id,
Some(params.step),
&params.bp,
)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
.create_pus_verif_success_tm(
2022-09-11 20:51:14 +02:00
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmCompletionSuccess.into(),
seq_counter.get(),
2022-09-04 22:24:36 +02:00
&token.req_id,
time_stamp,
None::<&dyn EcssEnumeration>,
)
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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-11 20:51:14 +02:00
buf: &mut [u8],
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
seq_counter: &mut (impl SequenceCountProvider<u16> + ?Sized),
2022-09-04 22:24:36 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-04 22:24:36 +02:00
let tm = self
2022-09-11 20:51:14 +02:00
.create_pus_verif_fail_tm(
buf,
2023-01-02 23:54:59 +01:00
Subservice::TmCompletionFailure.into(),
seq_counter.get(),
2022-09-11 20:51:14 +02:00
&token.req_id,
None::<&dyn EcssEnumeration>,
&params,
)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
sender
2022-09-11 20:51:14 +02:00
.send_tm(tm)
2022-09-04 22:24:36 +02:00
.map_err(|e| VerificationErrorWithToken(e, token))?;
seq_counter.increment();
2022-09-03 18:51:01 +02:00
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-11 20:51:14 +02:00
buf: &'a mut [u8],
2022-09-03 18:51:01 +02:00
subservice: u8,
msg_counter: u16,
2022-09-03 18:51:01 +02:00
req_id: &RequestId,
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
step: Option<&(impl EcssEnumeration + ?Sized)>,
2022-09-11 20:51:14 +02:00
) -> Result<PusTm, EcssTmError<E>> {
2022-09-03 18:51:01 +02:00
let mut source_data_len = size_of::<u32>();
if let Some(step) = step {
2022-12-20 17:23:11 +01:00
source_data_len += step.byte_width();
2022-09-03 18:51:01 +02:00
}
2022-09-11 20:51:14 +02:00
source_buffer_large_enough(buf.len(), source_data_len)?;
2022-09-03 18:51:01 +02:00
let mut idx = 0;
2022-09-11 20:51:14 +02:00
req_id.to_bytes(&mut buf[0..RequestId::SIZE_AS_BYTES]);
2022-09-03 18:51:01 +02:00
idx += RequestId::SIZE_AS_BYTES;
if let Some(step) = step {
// Size check was done beforehand
2022-12-20 17:23:11 +01:00
step.write_to_be_bytes(&mut buf[idx..idx + step.byte_width()])
2022-09-03 18:51:01 +02:00
.unwrap();
}
2022-12-04 20:00:20 +01:00
let mut sp_header = SpHeader::tm_unseg(self.apid(), 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base(
2022-09-11 20:51:14 +02:00
buf,
subservice,
msg_counter,
&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-11 20:51:14 +02:00
buf: &'a mut [u8],
2022-09-03 17:09:36 +02:00
subservice: u8,
msg_counter: u16,
2022-09-03 17:09:36 +02:00
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-11 20:51:14 +02:00
) -> Result<PusTm, EcssTmError<E>> {
2022-09-03 18:51:01 +02:00
let mut idx = 0;
2022-12-20 17:23:11 +01:00
let mut source_data_len = RequestId::SIZE_AS_BYTES + params.failure_code.byte_width();
2022-09-03 18:51:01 +02:00
if let Some(step) = step {
2022-12-20 17:23:11 +01:00
source_data_len += step.byte_width();
2022-09-03 18:51:01 +02:00
}
2022-09-05 00:20:32 +02:00
if let Some(failure_data) = params.failure_data {
source_data_len += failure_data.len();
}
2022-09-11 20:51:14 +02:00
source_buffer_large_enough(buf.len(), source_data_len)?;
req_id.to_bytes(&mut buf[0..RequestId::SIZE_AS_BYTES]);
2022-09-03 18:51:01 +02:00
idx += RequestId::SIZE_AS_BYTES;
if let Some(step) = step {
// Size check done beforehand
2022-12-20 17:23:11 +01:00
step.write_to_be_bytes(&mut buf[idx..idx + step.byte_width()])
2022-09-03 18:51:01 +02:00
.unwrap();
2022-12-20 17:23:11 +01:00
idx += step.byte_width();
2022-09-03 18:51:01 +02:00
}
2022-09-11 20:51:14 +02:00
params
.failure_code
2022-12-20 17:23:11 +01:00
.write_to_be_bytes(&mut buf[idx..idx + params.failure_code.byte_width()])?;
idx += params.failure_code.byte_width();
2022-09-05 00:20:32 +02:00
if let Some(failure_data) = params.failure_data {
2022-09-11 20:51:14 +02:00
buf[idx..idx + failure_data.len()].copy_from_slice(failure_data);
2022-09-05 00:20:32 +02:00
}
2022-12-04 20:00:20 +01:00
let mut sp_header = SpHeader::tm_unseg(self.apid(), 0, 0).unwrap();
Ok(self.create_pus_verif_tm_base(
2022-09-11 20:51:14 +02:00
buf,
subservice,
msg_counter,
&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
}
fn create_pus_verif_tm_base<'a>(
&'a mut self,
2022-09-11 20:51:14 +02:00
buf: &'a mut [u8],
2022-09-03 18:51:01 +02:00
subservice: u8,
msg_counter: u16,
2022-09-03 18:51:01 +02:00
sp_header: &mut SpHeader,
time_stamp: &'a [u8],
2022-09-03 18:51:01 +02:00
source_data_len: usize,
) -> PusTm {
2022-09-11 21:14:19 +02:00
let tm_sec_header =
PusTmSecondaryHeader::new(1, subservice, msg_counter, self.dest_id, time_stamp);
2022-09-03 18:51:01 +02:00
PusTm::new(
sp_header,
2022-09-03 17:09:36 +02:00
tm_sec_header,
2022-09-11 20:51:14 +02:00
Some(&buf[0..source_data_len]),
2022-09-03 17:09:36 +02:00
true,
2022-09-03 18:51:01 +02:00
)
2022-09-03 16:30:37 +02:00
}
}
2022-09-11 20:51:14 +02:00
#[cfg(feature = "alloc")]
2022-12-30 23:09:58 +01:00
mod alloc_mod {
2022-09-11 20:51:14 +02:00
use super::*;
2023-01-03 01:15:17 +01:00
use crate::pus::alloc_mod::EcssTmSender;
2023-01-03 01:00:51 +01:00
use crate::seq_count::SequenceCountProviderClonable;
2022-09-11 20:51:14 +02:00
use alloc::boxed::Box;
use alloc::vec;
use alloc::vec::Vec;
2022-09-03 16:30:37 +02:00
#[derive(Clone)]
2022-09-11 20:51:14 +02:00
pub struct VerificationReporterCfg {
apid: u16,
2023-01-03 01:00:51 +01:00
seq_counter: Box<dyn SequenceCountProviderClonable<u16> + Send>,
2022-09-11 20:51:14 +02:00
pub step_field_width: usize,
pub fail_code_field_width: usize,
pub max_fail_data_len: usize,
}
impl VerificationReporterCfg {
pub fn new(
apid: u16,
2023-01-03 01:00:51 +01:00
seq_counter: Box<dyn SequenceCountProviderClonable<u16> + Send>,
2022-09-11 20:51:14 +02:00
step_field_width: usize,
fail_code_field_width: usize,
max_fail_data_len: usize,
) -> Option<Self> {
if apid > MAX_APID {
return None;
}
Some(Self {
apid,
seq_counter,
2022-09-11 20:51:14 +02:00
step_field_width,
fail_code_field_width,
max_fail_data_len,
})
}
2022-09-06 00:17:52 +02:00
}
2022-09-11 20:51:14 +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-11-21 10:28:31 +01:00
#[derive(Clone)]
pub struct VerificationReporterWithBuf {
2022-09-11 20:51:14 +02:00
source_data_buf: Vec<u8>,
2023-01-03 01:00:51 +01:00
seq_counter: Box<dyn SequenceCountProviderClonable<u16> + Send + 'static>,
2022-09-11 20:51:14 +02:00
pub reporter: VerificationReporterBasic,
2022-09-05 00:20:32 +02:00
}
2022-09-03 16:30:37 +02:00
impl VerificationReporterWithBuf {
pub fn new(cfg: &VerificationReporterCfg) -> Self {
2022-09-11 20:51:14 +02:00
let reporter = VerificationReporterBasic::new(cfg.apid).unwrap();
Self {
source_data_buf: vec![
0;
RequestId::SIZE_AS_BYTES
2022-12-20 17:23:11 +01:00
+ cfg.step_field_width
+ cfg.fail_code_field_width
2022-09-11 20:51:14 +02:00
+ cfg.max_fail_data_len
],
seq_counter: cfg.seq_counter.clone(),
2022-09-11 20:51:14 +02:00
reporter,
}
2022-09-05 00:20:32 +02:00
}
2022-09-11 20:51:14 +02:00
delegate!(
to self.reporter {
pub fn set_apid(&mut self, apid: u16) -> bool;
pub fn apid(&self) -> u16;
2022-11-13 21:07:16 +01:00
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<TcStateNone>;
pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<TcStateNone>;
2022-09-11 20:51:14 +02:00
pub fn dest_id(&self) -> u16;
pub fn set_dest_id(&mut self, dest_id: u16);
}
);
2022-09-05 00:20:32 +02:00
2022-09-11 20:51:14 +02:00
pub fn allowed_source_data_len(&self) -> usize {
self.source_data_buf.capacity()
}
2022-09-05 00:20:32 +02:00
2022-09-11 20:51:14 +02:00
/// Package and send a PUS TM\[1, 1\] packet, see 8.1.2.1 of the PUS standard
pub fn acceptance_success<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateAccepted>, VerificationErrorWithToken<E, TcStateNone>>
2022-09-11 20:51:14 +02:00
{
self.reporter.acceptance_success(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
time_stamp,
)
}
2022-09-05 00:20:32 +02:00
2022-09-11 20:51:14 +02:00
/// Package and send a PUS TM\[1, 2\] packet, see 8.1.2.2 of the PUS standard
pub fn acceptance_failure<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateNone>> {
2022-09-11 20:51:14 +02:00
self.reporter.acceptance_failure(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
params,
)
}
2022-09-05 00:20:32 +02:00
2022-09-11 20:51:14 +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].
pub fn start_success<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateStarted>, VerificationErrorWithToken<E, TcStateAccepted>>
2022-09-11 20:51:14 +02:00
{
self.reporter.start_success(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
time_stamp,
)
}
2022-09-05 00:20:32 +02:00
2022-09-11 20:51:14 +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.
pub fn start_failure<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateAccepted>> {
2022-11-21 10:28:31 +01:00
self.reporter.start_failure(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
params,
)
2022-09-11 20:51:14 +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].
pub fn step_success<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: &VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
step: impl EcssEnumeration,
) -> Result<(), EcssTmError<E>> {
self.reporter.step_success(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
time_stamp,
step,
)
}
/// 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.
pub fn step_failure<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
params: FailParamsWithStep,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-11-21 10:28:31 +01:00
self.reporter.step_failure(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
params,
)
2022-09-11 20:51:14 +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.
pub fn completion_success<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-11 20:51:14 +02:00
self.reporter.completion_success(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
time_stamp,
)
}
/// 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.
pub fn completion_failure<E>(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2023-01-03 01:15:17 +01:00
sender: &mut (impl EcssTmSenderBase<Error = E> + ?Sized),
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-11 20:51:14 +02:00
self.reporter.completion_failure(
self.source_data_buf.as_mut_slice(),
token,
sender,
self.seq_counter.as_mut(),
2022-09-11 20:51:14 +02:00
params,
)
}
2022-09-05 00:20:32 +02:00
}
2022-09-11 20:51:14 +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-11-21 10:28:31 +01:00
#[derive(Clone)]
2022-09-11 20:51:14 +02:00
pub struct VerificationReporterWithSender<E> {
pub reporter: VerificationReporterWithBuf,
2023-01-03 01:15:17 +01:00
pub sender: Box<dyn EcssTmSender<Error = E>>,
2022-09-05 00:20:32 +02:00
}
2022-09-11 20:51:14 +02:00
impl<E: 'static> VerificationReporterWithSender<E> {
2022-11-21 10:28:31 +01:00
pub fn new(
cfg: &VerificationReporterCfg,
2023-01-03 01:15:17 +01:00
sender: Box<dyn EcssTmSender<Error = E>>,
2022-11-21 10:28:31 +01:00
) -> Self {
let reporter = VerificationReporterWithBuf::new(cfg);
2022-09-11 20:51:14 +02:00
Self::new_from_reporter(reporter, sender)
}
pub fn new_from_reporter(
reporter: VerificationReporterWithBuf,
2023-01-03 01:15:17 +01:00
sender: Box<dyn EcssTmSender<Error = E>>,
2022-09-11 20:51:14 +02:00
) -> Self {
Self { reporter, sender }
}
delegate! {
to self.reporter {
pub fn set_apid(&mut self, apid: u16) -> bool;
pub fn apid(&self) -> u16;
2022-11-13 21:07:16 +01:00
pub fn add_tc(&mut self, pus_tc: &PusTc) -> VerificationToken<TcStateNone>;
pub fn add_tc_with_req_id(&mut self, req_id: RequestId) -> VerificationToken<TcStateNone>;
2022-09-11 20:51:14 +02:00
pub fn dest_id(&self) -> u16;
pub fn set_dest_id(&mut self, dest_id: u16);
}
}
pub fn acceptance_success(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateAccepted>, VerificationErrorWithToken<E, TcStateNone>>
2022-09-11 20:51:14 +02:00
{
self.reporter
.acceptance_success(token, self.sender.as_mut(), time_stamp)
}
pub fn acceptance_failure(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateNone>,
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateNone>> {
2022-09-11 20:51:14 +02:00
self.reporter
.acceptance_failure(token, self.sender.as_mut(), params)
}
pub fn start_success(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<VerificationToken<TcStateStarted>, VerificationErrorWithToken<E, TcStateAccepted>>
2022-09-11 20:51:14 +02:00
{
self.reporter
.start_success(token, self.sender.as_mut(), time_stamp)
}
pub fn start_failure(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateAccepted>,
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateAccepted>> {
2022-09-11 20:51:14 +02:00
self.reporter
.start_failure(token, self.sender.as_mut(), params)
}
pub fn step_success(
&mut self,
2022-11-13 21:07:16 +01:00
token: &VerificationToken<TcStateStarted>,
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
step: impl EcssEnumeration,
) -> Result<(), EcssTmError<E>> {
self.reporter
.step_success(token, self.sender.as_mut(), time_stamp, step)
}
pub fn step_failure(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2022-09-11 20:51:14 +02:00
params: FailParamsWithStep,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-11 20:51:14 +02:00
self.reporter
.step_failure(token, self.sender.as_mut(), params)
}
pub fn completion_success(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2022-09-11 20:51:14 +02:00
time_stamp: &[u8],
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-11 20:51:14 +02:00
self.reporter
.completion_success(token, self.sender.as_mut(), time_stamp)
}
pub fn completion_failure(
&mut self,
2022-11-13 21:07:16 +01:00
token: VerificationToken<TcStateStarted>,
2022-09-11 20:51:14 +02:00
params: FailParams,
2022-11-13 21:07:16 +01:00
) -> Result<(), VerificationErrorWithToken<E, TcStateStarted>> {
2022-09-11 20:51:14 +02:00
self.reporter
.completion_failure(token, self.sender.as_mut(), params)
}
2022-09-05 00:20:32 +02:00
}
}
#[cfg(feature = "std")]
2022-09-07 11:02:45 +02:00
mod stdmod {
2022-12-30 23:09:58 +01:00
use super::alloc_mod::VerificationReporterWithSender;
2022-09-11 20:51:14 +02:00
use super::*;
2022-09-10 13:34:04 +02:00
use crate::pool::{ShareablePoolProvider, SharedPool, StoreAddr, StoreError};
2023-01-03 01:15:17 +01:00
use crate::pus::alloc_mod::EcssTmSender;
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-11-21 10:28:31 +01:00
#[derive(Debug, Eq, PartialEq, Clone)]
2022-09-07 11:02:45 +02:00
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)
}
}
2022-09-11 20:51:14 +02:00
impl From<StoreError> for EcssTmError<StdVerifSenderError> {
2022-09-10 19:57:58 +02:00
fn from(e: StoreError) -> Self {
2022-09-11 20:51:14 +02:00
EcssTmError::SendError(e.into())
2022-09-10 19:57:58 +02:00
}
}
2022-09-07 11:02:45 +02:00
trait SendBackend: Send {
fn send(&self, addr: StoreAddr) -> Result<(), StoreAddr>;
}
2022-11-21 10:28:31 +01:00
#[derive(Clone)]
2022-09-07 11:02:45 +02:00
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-11-21 10:28:31 +01:00
#[derive(Clone)]
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.
2023-01-03 01:15:17 +01:00
/// It implements the [EcssTmSenderBase] 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
2023-01-03 01:15:17 +01:00
impl EcssTmSenderBase for MpscVerifSender {
2022-10-31 01:25:02 +01:00
type Error = StdVerifSenderError;
2022-09-07 11:02:45 +02:00
delegate!(
to self.base {
2022-09-11 20:51:14 +02:00
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<StdVerifSenderError>>;
2022-09-07 11:02:45 +02:00
}
);
}
2023-01-03 01:00:51 +01:00
2023-01-03 01:15:17 +01:00
impl EcssTmSender 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.
2023-01-03 01:15:17 +01:00
/// It implements the [EcssTmSenderBase] trait to be used as PUS Verification TM sender
2022-12-30 23:28:33 +01:00
#[cfg(feature = "crossbeam")]
2022-11-21 10:28:31 +01:00
#[derive(Clone)]
2022-09-07 11:02:45 +02:00
pub struct CrossbeamVerifSender {
base: StdSenderBase<crossbeam_channel::Sender<StoreAddr>>,
}
2022-12-30 23:28:33 +01:00
#[cfg(feature = "crossbeam")]
2022-09-07 11:02:45 +02:00
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
2022-12-30 23:28:33 +01:00
#[cfg(feature = "crossbeam")]
2023-01-03 01:15:17 +01:00
impl EcssTmSenderBase for CrossbeamVerifSender {
2022-10-31 01:25:02 +01:00
type Error = StdVerifSenderError;
2022-09-07 11:02:45 +02:00
delegate!(
to self.base {
2022-09-11 20:51:14 +02:00
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<StdVerifSenderError>>;
2022-09-07 11:02:45 +02:00
}
);
}
2022-12-30 23:28:33 +01:00
#[cfg(feature = "crossbeam")]
2023-01-03 01:15:17 +01:00
impl EcssTmSender for CrossbeamVerifSender {}
2022-09-07 11:02:45 +02:00
2023-01-03 01:15:17 +01:00
impl<S: SendBackend + Clone + 'static> EcssTmSenderBase for StdSenderBase<S> {
2022-10-31 01:25:02 +01:00
type Error = StdVerifSenderError;
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<Self::Error>> {
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-11 20:51:14 +02:00
tm.write_to_bytes(buf).map_err(EcssTmError::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(|_| {
2022-09-11 20:51:14 +02:00
EcssTmError::SendError(StdVerifSenderError::RxDisconnected(addr))
2022-09-07 11:02:45 +02:00
})?;
Ok(())
};
match self.tm_store.write() {
Ok(lock) => operation(lock),
Err(poison_error) => {
if self.ignore_poison_error {
operation(poison_error.into_inner())
} else {
2022-09-11 20:51:14 +02:00
Err(EcssTmError::SendError(StdVerifSenderError::PoisonError))
2022-09-07 11:02:45 +02:00
}
2022-09-05 00:20:32 +02:00
}
}
2022-09-03 16:30:37 +02:00
}
}
}
#[cfg(test)]
mod tests {
2023-01-03 01:00:51 +01:00
use crate::pool::{LocalPool, PoolCfg, SharedPool};
2023-01-03 01:15:17 +01:00
use crate::pus::alloc_mod::EcssTmSender;
2022-10-15 21:14:27 +02:00
use crate::pus::tests::CommonTmInfo;
use crate::pus::verification::{
2023-01-03 01:15:17 +01:00
EcssTmError, EcssTmSenderBase, FailParams, FailParamsWithStep, MpscVerifSender, RequestId,
2023-01-03 01:00:51 +01:00
TcStateNone, VerificationReporterCfg, VerificationReporterWithBuf,
VerificationReporterWithSender, VerificationToken,
};
2022-11-21 10:28:31 +01:00
use crate::seq_count::SimpleSeqCountProvider;
2022-09-06 00:17:52 +02:00
use alloc::boxed::Box;
2022-09-06 10:14:47 +02:00
use alloc::format;
2022-10-15 21:14:27 +02:00
use spacepackets::ecss::{EcssEnumU16, EcssEnumU32, EcssEnumU8, EcssEnumeration, PusPacket};
use spacepackets::tc::{PusTc, PusTcSecondaryHeader};
use spacepackets::tm::PusTm;
use spacepackets::{ByteConversionError, SpHeader};
2022-10-15 21:14:27 +02:00
use std::collections::VecDeque;
2023-01-03 01:00:51 +01:00
use std::sync::{mpsc, Arc, RwLock};
use std::vec;
2022-10-15 21:14:27 +02:00
use std::vec::Vec;
2023-01-03 01:00:51 +01:00
fn is_send<T: Send>(_: &T) {}
#[allow(dead_code)]
fn is_sync<T: Sync>(_: &T) {}
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-11-21 10:28:31 +01:00
#[derive(Debug, Eq, PartialEq, Clone)]
2022-10-15 21:14:27 +02:00
struct TmInfo {
pub common: CommonTmInfo,
pub req_id: RequestId,
pub additional_data: Option<Vec<u8>>,
}
2022-11-21 10:28:31 +01:00
#[derive(Default, Clone)]
2022-10-15 21:14:27 +02:00
struct TestSender {
pub service_queue: VecDeque<TmInfo>,
}
2023-01-03 01:15:17 +01:00
impl EcssTmSenderBase for TestSender {
2022-10-31 01:25:02 +01:00
type Error = ();
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<Self::Error>> {
2022-10-15 21:14:27 +02:00
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 {
common: CommonTmInfo::new_from_tm(&tm),
req_id,
additional_data: vec,
});
Ok(())
}
}
2023-01-03 01:15:17 +01:00
impl EcssTmSender for TestSender {}
2023-01-03 01:00:51 +01:00
2022-09-06 10:14:47 +02:00
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
struct DummyError {}
2022-11-21 10:28:31 +01:00
#[derive(Default, Clone)]
2022-09-06 10:14:47 +02:00
struct FallibleSender {}
2023-01-03 01:15:17 +01:00
impl EcssTmSenderBase for FallibleSender {
2022-11-01 18:03:01 +01:00
type Error = DummyError;
2022-09-11 20:51:14 +02:00
fn send_tm(&mut self, _: PusTm) -> Result<(), EcssTmError<DummyError>> {
Err(EcssTmError::SendError(DummyError {}))
2022-09-06 10:14:47 +02:00
}
}
2023-01-03 01:15:17 +01:00
impl EcssTmSender for FallibleSender {}
2023-01-03 01:00:51 +01:00
2022-09-06 00:17:52 +02:00
struct TestBase<'a> {
vr: VerificationReporterWithBuf,
2022-09-06 00:17:52 +02:00
#[allow(dead_code)]
tc: PusTc<'a>,
}
impl<'a> TestBase<'a> {
fn rep(&mut self) -> &mut VerificationReporterWithBuf {
&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 VerificationReporterWithBuf {
&mut self.helper.reporter
}
}
fn base_reporter() -> VerificationReporterWithBuf {
2022-11-21 10:28:31 +01:00
let cfg = VerificationReporterCfg::new(
TEST_APID,
Box::new(SimpleSeqCountProvider::default()),
1,
2,
8,
)
.unwrap();
VerificationReporterWithBuf::new(&cfg)
2022-09-06 00:17:52 +02:00
}
fn base_tc_init(app_data: Option<&[u8]>) -> (PusTc, RequestId) {
2022-12-04 20:00:20 +01:00
let mut sph = SpHeader::tc_unseg(TEST_APID, 0x34, 0).unwrap();
2022-09-06 00:17:52 +02:00
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)
}
2022-11-13 21:07:16 +01:00
fn base_init(api_sel: bool) -> (TestBase<'static>, VerificationToken<TcStateNone>) {
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, ()>,
2022-11-13 21:07:16 +01:00
VerificationToken<TcStateNone>,
2022-09-06 00:17:52 +02:00
) {
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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);
}
2023-01-03 01:00:51 +01:00
#[test]
fn test_mpsc_verif_send_sync() {
let pool = LocalPool::new(PoolCfg::new(vec![(8, 8)]));
let shared_pool: SharedPool = Arc::new(RwLock::new(Box::new(pool)));
let (tx, _) = mpsc::channel();
let mpsc_verif_sender = MpscVerifSender::new(shared_pool, tx);
is_send(&mpsc_verif_sender);
}
2022-09-11 20:51:14 +02:00
#[test]
fn test_state() {
let (mut b, _) = base_init(false);
assert_eq!(b.vr.apid(), TEST_APID);
b.vr.set_apid(TEST_APID + 1);
assert_eq!(b.vr.apid(), TEST_APID + 1);
}
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 {
2022-09-11 20:51:14 +02:00
EcssTmError::SendError(e) => {
2022-09-06 10:14:47 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 2,
apid: TEST_APID,
msg_counter: 0,
dest_id: 5,
time_stamp: stamp_buf,
},
2022-09-06 00:17:52 +02:00
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);
2022-09-11 20:51:14 +02:00
b.rep().reporter.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();
2022-09-11 20:51:14 +02:00
b.rep().reporter.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();
2022-09-11 20:51:14 +02:00
b.rep().reporter.dest_id = 5;
2022-09-06 10:14:47 +02:00
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 {
2022-09-11 20:51:14 +02:00
EcssTmError::ByteConversionError(e) => match e {
2022-09-06 10:14:47 +02:00
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];
2022-11-02 00:36:49 +01:00
fail_data.write_to_be_bytes(&mut fail_data_raw).unwrap();
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 2,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 4,
apid: TEST_APID,
msg_counter: 1,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 3,
apid: TEST_APID,
msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 5,
apid: TEST_APID,
msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: Some([0].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 5,
apid: TEST_APID,
msg_counter: 3,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 3,
apid: TEST_APID,
msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 5,
apid: TEST_APID,
msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: Some([0].to_vec()),
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 6,
apid: TEST_APID,
msg_counter: 3,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 3,
apid: TEST_APID,
msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 8,
apid: TEST_APID,
msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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 {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 1,
apid: TEST_APID,
msg_counter: 0,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
let mut info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
let cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 3,
apid: TEST_APID,
msg_counter: 1,
dest_id: 0,
time_stamp: [0, 1, 0, 1, 0, 1, 0],
},
2022-09-06 00:17:52 +02:00
additional_data: None,
req_id,
};
info = sender.service_queue.pop_front().unwrap();
assert_eq!(info, cmp_info);
let cmp_info = TmInfo {
2022-10-15 21:14:27 +02:00
common: CommonTmInfo {
subservice: 7,
apid: TEST_APID,
msg_counter: 2,
dest_id: 0,
time_stamp: EMPTY_STAMP,
},
2022-09-06 00:17:52 +02:00
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
}