2023-01-25 10:52:24 +01:00
|
|
|
//! # PUS support modules
|
2023-07-06 01:14:01 +02:00
|
|
|
//!
|
|
|
|
//! This module contains structures to make working with the PUS C standard easier.
|
|
|
|
//! The satrs-example application contains various usage examples of these components.
|
2023-07-08 14:57:11 +02:00
|
|
|
use crate::SenderId;
|
2023-07-09 13:23:34 +02:00
|
|
|
use core::fmt::{Display, Formatter};
|
2023-01-11 10:30:03 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use downcast_rs::{impl_downcast, Downcast};
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
use dyn_clone::DynClone;
|
|
|
|
use spacepackets::ecss::PusError;
|
2023-07-09 13:32:59 +02:00
|
|
|
use spacepackets::tc::PusTc;
|
2023-01-11 10:30:03 +01:00
|
|
|
use spacepackets::tm::PusTm;
|
|
|
|
use spacepackets::{ByteConversionError, SizeMissmatch};
|
2023-07-09 13:23:34 +02:00
|
|
|
use std::error::Error;
|
2023-01-11 10:30:03 +01:00
|
|
|
|
|
|
|
pub mod event;
|
|
|
|
pub mod event_man;
|
2023-07-05 14:25:51 +02:00
|
|
|
pub mod event_srv;
|
2023-01-11 10:30:03 +01:00
|
|
|
pub mod hk;
|
2023-02-15 11:05:32 +01:00
|
|
|
pub mod mode;
|
2023-07-05 11:25:23 +02:00
|
|
|
pub mod scheduler;
|
|
|
|
pub mod scheduler_srv;
|
2023-01-21 12:19:05 +01:00
|
|
|
#[cfg(feature = "std")]
|
2023-07-05 11:25:23 +02:00
|
|
|
pub mod test;
|
2023-01-11 10:30:03 +01:00
|
|
|
pub mod verification;
|
|
|
|
|
2023-01-25 10:52:24 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
pub use alloc_mod::*;
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
use crate::pool::{StoreAddr, StoreError};
|
2023-07-09 13:32:59 +02:00
|
|
|
use crate::pus::verification::TcStateToken;
|
2023-02-27 17:00:21 +01:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub use std_mod::*;
|
|
|
|
|
2023-07-08 14:57:11 +02:00
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
pub enum PusTmWrapper<'tm> {
|
|
|
|
InStore(StoreAddr),
|
|
|
|
Direct(PusTm<'tm>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<StoreAddr> for PusTmWrapper<'_> {
|
|
|
|
fn from(value: StoreAddr) -> Self {
|
|
|
|
Self::InStore(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tm> From<PusTm<'tm>> for PusTmWrapper<'tm> {
|
|
|
|
fn from(value: PusTm<'tm>) -> Self {
|
|
|
|
Self::Direct(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
/// Generic error type for sending something via a message queue.
|
2023-07-09 13:04:00 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2023-07-09 13:23:34 +02:00
|
|
|
pub enum GenericSendError {
|
2023-07-09 13:04:00 +02:00
|
|
|
RxDisconnected,
|
|
|
|
QueueFull(u32),
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
impl Display for GenericSendError {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
GenericSendError::RxDisconnected => {
|
|
|
|
write!(f, "rx side has disconnected")
|
|
|
|
}
|
|
|
|
GenericSendError::QueueFull(max_cap) => {
|
|
|
|
write!(f, "queue with max capacity of {max_cap} is full")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl Error for GenericSendError {}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum EcssTmtcErrorWithSend {
|
|
|
|
StoreLock,
|
|
|
|
Store(StoreError),
|
|
|
|
Pus(PusError),
|
|
|
|
CantSendAddr(StoreAddr),
|
|
|
|
Send(GenericSendError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for EcssTmtcErrorWithSend {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
|
|
|
|
match self {
|
|
|
|
EcssTmtcErrorWithSend::StoreLock => {
|
|
|
|
write!(f, "store lock error")
|
|
|
|
}
|
|
|
|
EcssTmtcErrorWithSend::Store(store) => {
|
|
|
|
write!(f, "store error: {store}")
|
|
|
|
}
|
|
|
|
EcssTmtcErrorWithSend::Pus(pus_e) => {
|
|
|
|
write!(f, "PUS error: {pus_e}")
|
|
|
|
}
|
|
|
|
EcssTmtcErrorWithSend::CantSendAddr(addr) => {
|
|
|
|
write!(f, "can not send address {addr}")
|
|
|
|
}
|
|
|
|
EcssTmtcErrorWithSend::Send(send_e) => {
|
|
|
|
write!(f, "send error {send_e}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<StoreError> for EcssTmtcErrorWithSend {
|
|
|
|
fn from(value: StoreError) -> Self {
|
|
|
|
Self::Store(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PusError> for EcssTmtcErrorWithSend {
|
|
|
|
fn from(value: PusError) -> Self {
|
|
|
|
Self::Pus(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<GenericSendError> for EcssTmtcErrorWithSend {
|
|
|
|
fn from(value: GenericSendError) -> Self {
|
|
|
|
Self::Send(value)
|
|
|
|
}
|
|
|
|
}
|
2023-01-11 10:30:03 +01:00
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl Error for EcssTmtcErrorWithSend {
|
|
|
|
fn source(&self) -> Option<&(dyn Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
EcssTmtcErrorWithSend::Store(e) => Some(e),
|
|
|
|
EcssTmtcErrorWithSend::Pus(e) => Some(e),
|
|
|
|
EcssTmtcErrorWithSend::Send(e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 00:42:20 +02:00
|
|
|
pub trait EcssSender: Send {
|
2023-02-28 14:01:40 +01:00
|
|
|
/// Each sender can have an ID associated with it
|
|
|
|
fn id(&self) -> SenderId;
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"unset"
|
|
|
|
}
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:32:59 +02:00
|
|
|
/// Generic trait for a user supplied sender object.
|
|
|
|
///
|
|
|
|
/// This sender object is responsible for sending PUS telemetry to a TM sink.
|
|
|
|
pub trait EcssTmSenderCore: EcssSender {
|
|
|
|
fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcErrorWithSend>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generic trait for a user supplied sender object.
|
|
|
|
///
|
|
|
|
/// This sender object is responsible for sending PUS telecommands to a TC recipient. Each
|
|
|
|
/// telecommand can optionally have a token which contains its verification state.
|
|
|
|
pub trait EcssTcSenderCore: EcssSender {
|
|
|
|
fn send_tc(&self, tc: PusTc, token: Option<TcStateToken>) -> Result<(), EcssTmtcErrorWithSend>;
|
|
|
|
}
|
|
|
|
|
2023-01-11 10:30:03 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
2023-01-25 10:52:24 +01:00
|
|
|
mod alloc_mod {
|
2023-01-11 10:30:03 +01:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
/// Extension trait for [EcssTmSenderCore].
|
|
|
|
///
|
|
|
|
/// It provides additional functionality, for example by implementing the [Downcast] trait
|
|
|
|
/// and the [DynClone] trait.
|
|
|
|
///
|
|
|
|
/// [Downcast] is implemented to allow passing the sender as a boxed trait object and still
|
|
|
|
/// retrieve the concrete type at a later point.
|
|
|
|
///
|
|
|
|
/// [DynClone] allows cloning the trait object as long as the boxed object implements
|
|
|
|
/// [Clone].
|
2023-01-25 10:52:24 +01:00
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
2023-01-11 10:30:03 +01:00
|
|
|
pub trait EcssTmSender: EcssTmSenderCore + Downcast + DynClone {}
|
|
|
|
|
|
|
|
/// Blanket implementation for all types which implement [EcssTmSenderCore] and are clonable.
|
|
|
|
impl<T> EcssTmSender for T where T: EcssTmSenderCore + Clone + 'static {}
|
|
|
|
|
2023-07-09 13:04:00 +02:00
|
|
|
dyn_clone::clone_trait_object!(EcssTmSender);
|
|
|
|
impl_downcast!(EcssTmSender);
|
2023-07-03 00:42:20 +02:00
|
|
|
|
|
|
|
/// Extension trait for [EcssTcSenderCore].
|
|
|
|
///
|
|
|
|
/// It provides additional functionality, for example by implementing the [Downcast] trait
|
|
|
|
/// and the [DynClone] trait.
|
|
|
|
///
|
|
|
|
/// [Downcast] is implemented to allow passing the sender as a boxed trait object and still
|
|
|
|
/// retrieve the concrete type at a later point.
|
|
|
|
///
|
|
|
|
/// [DynClone] allows cloning the trait object as long as the boxed object implements
|
|
|
|
/// [Clone].
|
|
|
|
#[cfg(feature = "alloc")]
|
|
|
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
|
|
|
pub trait EcssTcSender: EcssTcSenderCore + Downcast + DynClone {}
|
|
|
|
|
|
|
|
/// Blanket implementation for all types which implement [EcssTcSenderCore] and are clonable.
|
|
|
|
impl<T> EcssTcSender for T where T: EcssTcSenderCore + Clone + 'static {}
|
|
|
|
|
2023-07-09 13:04:00 +02:00
|
|
|
dyn_clone::clone_trait_object!(EcssTcSender);
|
|
|
|
impl_downcast!(EcssTcSender);
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
|
2023-02-27 17:00:21 +01:00
|
|
|
#[cfg(feature = "std")]
|
|
|
|
pub mod std_mod {
|
|
|
|
use crate::pool::{ShareablePoolProvider, SharedPool, StoreAddr, StoreError};
|
2023-07-05 11:25:23 +02:00
|
|
|
use crate::pus::verification::{
|
2023-07-09 13:04:00 +02:00
|
|
|
StdVerifReporterWithSender, TcStateAccepted, TcStateToken, VerificationToken,
|
2023-07-05 11:25:23 +02:00
|
|
|
};
|
2023-07-09 13:32:59 +02:00
|
|
|
use crate::pus::{
|
|
|
|
EcssSender, EcssTmSender, EcssTmSenderCore, EcssTmtcErrorWithSend, GenericSendError,
|
|
|
|
PusTmWrapper,
|
|
|
|
};
|
2023-07-05 11:25:23 +02:00
|
|
|
use crate::tmtc::tm_helper::SharedTmStore;
|
2023-02-28 14:01:40 +01:00
|
|
|
use crate::SenderId;
|
2023-07-09 13:32:59 +02:00
|
|
|
use alloc::boxed::Box;
|
2023-02-27 17:00:21 +01:00
|
|
|
use alloc::vec::Vec;
|
2023-07-05 21:08:04 +02:00
|
|
|
use spacepackets::ecss::{PusError, SerializablePusPacket};
|
2023-07-09 13:04:00 +02:00
|
|
|
use spacepackets::tc::PusTc;
|
2023-07-05 11:25:23 +02:00
|
|
|
use spacepackets::time::cds::TimeProvider;
|
|
|
|
use spacepackets::time::{StdTimestampError, TimeWriter};
|
2023-07-06 00:49:18 +02:00
|
|
|
use std::cell::RefCell;
|
2023-07-05 21:08:04 +02:00
|
|
|
use std::format;
|
2023-07-05 11:25:23 +02:00
|
|
|
use std::string::String;
|
2023-07-09 13:23:34 +02:00
|
|
|
use std::sync::mpsc::SendError;
|
2023-02-27 17:00:21 +01:00
|
|
|
use std::sync::{mpsc, RwLockWriteGuard};
|
2023-07-05 11:25:23 +02:00
|
|
|
use thiserror::Error;
|
2023-02-27 17:00:21 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2023-07-08 14:57:11 +02:00
|
|
|
pub struct MpscTmInStoreSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
id: SenderId,
|
|
|
|
name: &'static str,
|
2023-02-27 17:00:21 +01:00
|
|
|
store_helper: SharedPool,
|
|
|
|
sender: mpsc::Sender<StoreAddr>,
|
|
|
|
pub ignore_poison_errors: bool,
|
|
|
|
}
|
|
|
|
|
2023-07-08 14:57:11 +02:00
|
|
|
impl EcssSender for MpscTmInStoreSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
fn id(&self) -> SenderId {
|
|
|
|
self.id
|
|
|
|
}
|
|
|
|
|
2023-07-03 00:42:20 +02:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
impl From<SendError<StoreAddr>> for EcssTmtcErrorWithSend {
|
|
|
|
fn from(_: SendError<StoreAddr>) -> Self {
|
|
|
|
Self::Send(GenericSendError::RxDisconnected)
|
|
|
|
}
|
|
|
|
}
|
2023-07-08 14:57:11 +02:00
|
|
|
impl MpscTmInStoreSender {
|
|
|
|
pub fn send_direct_tm(
|
|
|
|
&self,
|
2023-07-03 01:33:13 +02:00
|
|
|
tmtc: impl SerializablePusPacket,
|
2023-07-09 13:04:00 +02:00
|
|
|
) -> Result<(), EcssTmtcErrorWithSend> {
|
2023-02-27 17:00:21 +01:00
|
|
|
let operation = |mut store: RwLockWriteGuard<ShareablePoolProvider>| {
|
2023-07-03 01:33:13 +02:00
|
|
|
let (addr, slice) = store.free_element(tmtc.len_packed())?;
|
|
|
|
tmtc.write_to_bytes(slice)?;
|
2023-02-27 17:00:21 +01:00
|
|
|
self.sender.send(addr)?;
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
match self.store_helper.write() {
|
|
|
|
Ok(pool) => operation(pool),
|
|
|
|
Err(e) => {
|
|
|
|
if self.ignore_poison_errors {
|
|
|
|
operation(e.into_inner())
|
|
|
|
} else {
|
2023-07-09 13:04:00 +02:00
|
|
|
Err(EcssTmtcErrorWithSend::Send(
|
2023-07-09 13:23:34 +02:00
|
|
|
GenericSendError::RxDisconnected,
|
2023-07-09 13:04:00 +02:00
|
|
|
))
|
2023-02-27 17:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-03 00:42:20 +02:00
|
|
|
}
|
2023-02-28 14:01:40 +01:00
|
|
|
|
2023-07-08 14:57:11 +02:00
|
|
|
impl EcssTmSenderCore for MpscTmInStoreSender {
|
2023-07-09 13:04:00 +02:00
|
|
|
fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcErrorWithSend> {
|
2023-07-08 14:57:11 +02:00
|
|
|
match tm {
|
2023-07-09 13:23:34 +02:00
|
|
|
PusTmWrapper::InStore(addr) => self.sender.send(addr).map_err(|e| e.into()),
|
2023-07-08 14:57:11 +02:00
|
|
|
PusTmWrapper::Direct(tm) => self.send_direct_tm(tm),
|
|
|
|
}
|
2023-02-28 14:01:40 +01:00
|
|
|
}
|
2023-02-27 17:00:21 +01:00
|
|
|
}
|
|
|
|
|
2023-07-08 14:57:11 +02:00
|
|
|
impl MpscTmInStoreSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
pub fn new(
|
|
|
|
id: SenderId,
|
|
|
|
name: &'static str,
|
|
|
|
store_helper: SharedPool,
|
|
|
|
sender: mpsc::Sender<StoreAddr>,
|
|
|
|
) -> Self {
|
2023-02-27 17:00:21 +01:00
|
|
|
Self {
|
2023-02-28 14:01:40 +01:00
|
|
|
id,
|
|
|
|
name,
|
2023-02-27 17:00:21 +01:00
|
|
|
store_helper,
|
|
|
|
sender,
|
|
|
|
ignore_poison_errors: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 14:57:11 +02:00
|
|
|
/// This class can be used if frequent heap allocations during run-time are not an issue.
|
|
|
|
/// PUS TM packets will be sent around as [Vec]s. Please note that the current implementation
|
|
|
|
/// of this class can not deal with store addresses, so it is assumed that is is always
|
|
|
|
/// going to be called with direct packets.
|
|
|
|
#[derive(Clone)]
|
2023-02-27 17:00:21 +01:00
|
|
|
pub struct MpscTmAsVecSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
id: SenderId,
|
2023-02-27 17:00:21 +01:00
|
|
|
sender: mpsc::Sender<Vec<u8>>,
|
2023-02-28 14:01:40 +01:00
|
|
|
name: &'static str,
|
2023-02-27 17:00:21 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:23:34 +02:00
|
|
|
impl From<SendError<Vec<u8>>> for EcssTmtcErrorWithSend {
|
|
|
|
fn from(_: SendError<Vec<u8>>) -> Self {
|
|
|
|
Self::Send(GenericSendError::RxDisconnected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 17:00:21 +01:00
|
|
|
impl MpscTmAsVecSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
pub fn new(id: u32, name: &'static str, sender: mpsc::Sender<Vec<u8>>) -> Self {
|
|
|
|
Self { id, sender, name }
|
2023-02-27 17:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-03 00:42:20 +02:00
|
|
|
|
|
|
|
impl EcssSender for MpscTmAsVecSender {
|
2023-02-28 14:01:40 +01:00
|
|
|
fn id(&self) -> SenderId {
|
|
|
|
self.id
|
|
|
|
}
|
2023-07-03 00:42:20 +02:00
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
self.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EcssTmSenderCore for MpscTmAsVecSender {
|
2023-07-09 13:04:00 +02:00
|
|
|
fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcErrorWithSend> {
|
2023-07-08 14:57:11 +02:00
|
|
|
match tm {
|
2023-07-09 13:04:00 +02:00
|
|
|
PusTmWrapper::InStore(addr) => Err(EcssTmtcErrorWithSend::CantSendAddr(addr)),
|
2023-07-08 14:57:11 +02:00
|
|
|
PusTmWrapper::Direct(tm) => {
|
|
|
|
let mut vec = Vec::new();
|
|
|
|
tm.append_to_vec(&mut vec)
|
2023-07-09 13:23:34 +02:00
|
|
|
.map_err(EcssTmtcErrorWithSend::Pus)?;
|
|
|
|
self.sender.send(vec)?;
|
2023-07-08 14:57:11 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-02-27 17:00:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:25:23 +02:00
|
|
|
#[derive(Debug, Clone, Error)]
|
|
|
|
pub enum PusPacketHandlingError {
|
|
|
|
#[error("Generic PUS error: {0}")]
|
|
|
|
PusError(#[from] PusError),
|
|
|
|
#[error("Wrong service number {0} for packet handler")]
|
|
|
|
WrongService(u8),
|
2023-07-05 14:25:51 +02:00
|
|
|
#[error("Invalid subservice {0}")]
|
|
|
|
InvalidSubservice(u8),
|
2023-07-05 11:25:23 +02:00
|
|
|
#[error("Not enough application data available: {0}")]
|
|
|
|
NotEnoughAppData(String),
|
2023-07-06 01:14:01 +02:00
|
|
|
#[error("Invalid application data")]
|
|
|
|
InvalidAppData(String),
|
2023-07-05 11:25:23 +02:00
|
|
|
#[error("Generic store error: {0}")]
|
|
|
|
StoreError(#[from] StoreError),
|
2023-07-05 14:25:51 +02:00
|
|
|
#[error("Error with the pool RwGuard: {0}")]
|
2023-07-05 11:25:23 +02:00
|
|
|
RwGuardError(String),
|
2023-07-05 14:25:51 +02:00
|
|
|
#[error("MQ send error: {0}")]
|
|
|
|
SendError(String),
|
|
|
|
#[error("TX message queue side has disconnected")]
|
2023-07-05 11:25:23 +02:00
|
|
|
QueueDisconnected,
|
|
|
|
#[error("Other error {0}")]
|
|
|
|
OtherError(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Error)]
|
|
|
|
pub enum PartialPusHandlingError {
|
|
|
|
#[error("Generic timestamp generation error")]
|
2023-07-09 13:32:59 +02:00
|
|
|
Time(#[from] StdTimestampError),
|
2023-07-05 11:25:23 +02:00
|
|
|
#[error("Error sending telemetry: {0}")]
|
2023-07-09 13:32:59 +02:00
|
|
|
TmSend(#[from] EcssTmtcErrorWithSend),
|
2023-07-05 11:25:23 +02:00
|
|
|
#[error("Error sending verification message")]
|
2023-07-08 13:37:27 +02:00
|
|
|
Verification,
|
2023-07-05 11:25:23 +02:00
|
|
|
}
|
|
|
|
|
2023-07-06 01:14:01 +02:00
|
|
|
/// Generic result type for handlers which can process PUS packets.
|
2023-07-05 11:25:23 +02:00
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum PusPacketHandlerResult {
|
|
|
|
RequestHandled,
|
|
|
|
RequestHandledPartialSuccess(PartialPusHandlingError),
|
2023-07-05 14:25:51 +02:00
|
|
|
SubserviceNotImplemented(u8, VerificationToken<TcStateAccepted>),
|
2023-07-05 11:58:43 +02:00
|
|
|
CustomSubservice(u8, VerificationToken<TcStateAccepted>),
|
2023-07-05 11:25:23 +02:00
|
|
|
Empty,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PartialPusHandlingError> for PusPacketHandlerResult {
|
|
|
|
fn from(value: PartialPusHandlingError) -> Self {
|
|
|
|
Self::RequestHandledPartialSuccess(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 01:14:01 +02:00
|
|
|
/// Generic abstraction for a telecommand being sent around after is has been accepted.
|
|
|
|
/// The actual telecommand is stored inside a pre-allocated pool structure.
|
2023-07-05 11:25:23 +02:00
|
|
|
pub type AcceptedTc = (StoreAddr, VerificationToken<TcStateAccepted>);
|
|
|
|
|
2023-07-06 01:14:01 +02:00
|
|
|
/// Base class for handlers which can handle PUS TC packets. Right now, the message queue
|
|
|
|
/// backend is constrained to [mpsc::channel]s and the verification reporter
|
|
|
|
/// is constrained to the [StdVerifReporterWithSender].
|
2023-07-05 11:25:23 +02:00
|
|
|
pub struct PusServiceBase {
|
2023-07-05 21:08:04 +02:00
|
|
|
pub tc_rx: mpsc::Receiver<AcceptedTc>,
|
|
|
|
pub tc_store: SharedPool,
|
2023-07-09 13:32:59 +02:00
|
|
|
pub tm_sender: Box<dyn EcssTmSender>,
|
2023-07-05 21:08:04 +02:00
|
|
|
pub tm_apid: u16,
|
2023-07-06 00:49:18 +02:00
|
|
|
/// The verification handler is wrapped in a [RefCell] to allow the interior mutability
|
|
|
|
/// pattern. This makes writing methods which are not mutable a lot easier.
|
|
|
|
pub verification_handler: RefCell<StdVerifReporterWithSender>,
|
2023-07-05 21:08:04 +02:00
|
|
|
pub pus_buf: [u8; 2048],
|
|
|
|
pub pus_size: usize,
|
2023-07-05 11:25:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PusServiceBase {
|
|
|
|
pub fn new(
|
|
|
|
receiver: mpsc::Receiver<AcceptedTc>,
|
|
|
|
tc_pool: SharedPool,
|
2023-07-09 13:32:59 +02:00
|
|
|
tm_sender: Box<dyn EcssTmSender>,
|
2023-07-05 11:25:23 +02:00
|
|
|
tm_apid: u16,
|
|
|
|
verification_handler: StdVerifReporterWithSender,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
tc_rx: receiver,
|
|
|
|
tc_store: tc_pool,
|
|
|
|
tm_apid,
|
2023-07-09 13:32:59 +02:00
|
|
|
tm_sender,
|
2023-07-06 00:49:18 +02:00
|
|
|
verification_handler: RefCell::new(verification_handler),
|
2023-07-05 11:25:23 +02:00
|
|
|
pus_buf: [0; 2048],
|
|
|
|
pus_size: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-05 21:08:04 +02:00
|
|
|
pub fn get_current_timestamp(
|
|
|
|
&self,
|
|
|
|
partial_error: &mut Option<PartialPusHandlingError>,
|
|
|
|
) -> [u8; 7] {
|
|
|
|
let mut time_stamp: [u8; 7] = [0; 7];
|
2023-07-05 11:25:23 +02:00
|
|
|
let time_provider =
|
2023-07-08 13:37:27 +02:00
|
|
|
TimeProvider::from_now_with_u16_days().map_err(PartialPusHandlingError::Time);
|
2023-07-05 11:25:23 +02:00
|
|
|
if let Ok(time_provider) = time_provider {
|
2023-07-08 14:57:11 +02:00
|
|
|
// Can't fail, we have a buffer with the exact required size.
|
2023-07-05 21:08:04 +02:00
|
|
|
time_provider.write_to_bytes(&mut time_stamp).unwrap();
|
2023-07-05 11:25:23 +02:00
|
|
|
} else {
|
2023-07-05 21:08:04 +02:00
|
|
|
*partial_error = Some(time_provider.unwrap_err());
|
2023-07-05 11:25:23 +02:00
|
|
|
}
|
2023-07-05 21:08:04 +02:00
|
|
|
time_stamp
|
2023-07-05 11:25:23 +02:00
|
|
|
}
|
2023-07-06 01:14:01 +02:00
|
|
|
|
2023-07-05 21:08:04 +02:00
|
|
|
pub fn get_current_timestamp_ignore_error(&self) -> [u8; 7] {
|
|
|
|
let mut dummy = None;
|
|
|
|
self.get_current_timestamp(&mut dummy)
|
2023-07-05 11:58:43 +02:00
|
|
|
}
|
2023-07-05 11:25:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait PusServiceHandler {
|
|
|
|
fn psb_mut(&mut self) -> &mut PusServiceBase;
|
|
|
|
fn psb(&self) -> &PusServiceBase;
|
|
|
|
fn handle_one_tc(
|
|
|
|
&mut self,
|
|
|
|
addr: StoreAddr,
|
|
|
|
token: VerificationToken<TcStateAccepted>,
|
|
|
|
) -> Result<PusPacketHandlerResult, PusPacketHandlingError>;
|
2023-07-05 21:08:04 +02:00
|
|
|
|
|
|
|
fn copy_tc_to_buf(&mut self, addr: StoreAddr) -> Result<(), PusPacketHandlingError> {
|
|
|
|
// Keep locked section as short as possible.
|
|
|
|
let psb_mut = self.psb_mut();
|
|
|
|
let mut tc_pool = psb_mut
|
|
|
|
.tc_store
|
|
|
|
.write()
|
|
|
|
.map_err(|e| PusPacketHandlingError::RwGuardError(format!("{e}")))?;
|
|
|
|
let tc_guard = tc_pool.read_with_guard(addr);
|
|
|
|
let tc_raw = tc_guard.read().unwrap();
|
|
|
|
psb_mut.pus_buf[0..tc_raw.len()].copy_from_slice(tc_raw);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:25:23 +02:00
|
|
|
fn handle_next_packet(&mut self) -> Result<PusPacketHandlerResult, PusPacketHandlingError> {
|
|
|
|
return match self.psb().tc_rx.try_recv() {
|
|
|
|
Ok((addr, token)) => self.handle_one_tc(addr, token),
|
|
|
|
Err(e) => match e {
|
|
|
|
mpsc::TryRecvError::Empty => Ok(PusPacketHandlerResult::Empty),
|
|
|
|
mpsc::TryRecvError::Disconnected => {
|
|
|
|
Err(PusPacketHandlingError::QueueDisconnected)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2023-02-15 02:12:00 +01:00
|
|
|
}
|
|
|
|
|
2023-07-09 13:04:00 +02:00
|
|
|
pub(crate) fn source_buffer_large_enough(
|
|
|
|
cap: usize,
|
|
|
|
len: usize,
|
|
|
|
) -> Result<(), EcssTmtcErrorWithSend> {
|
2023-01-11 10:30:03 +01:00
|
|
|
if len > cap {
|
2023-07-09 13:23:34 +02:00
|
|
|
return Err(
|
|
|
|
PusError::ByteConversionError(ByteConversionError::ToSliceTooSmall(SizeMissmatch {
|
2023-01-11 10:30:03 +01:00
|
|
|
found: cap,
|
|
|
|
expected: len,
|
2023-07-09 13:23:34 +02:00
|
|
|
}))
|
|
|
|
.into(),
|
|
|
|
);
|
2023-01-11 10:30:03 +01:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub(crate) mod tests {
|
|
|
|
use spacepackets::tm::{GenericPusTmSecondaryHeader, PusTm};
|
|
|
|
use spacepackets::CcsdsPacket;
|
|
|
|
|
|
|
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
|
|
|
pub(crate) struct CommonTmInfo {
|
|
|
|
pub subservice: u8,
|
|
|
|
pub apid: u16,
|
|
|
|
pub msg_counter: u16,
|
|
|
|
pub dest_id: u16,
|
|
|
|
pub time_stamp: [u8; 7],
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommonTmInfo {
|
|
|
|
pub fn new_from_tm(tm: &PusTm) -> Self {
|
|
|
|
let mut time_stamp = [0; 7];
|
2023-01-21 13:52:21 +01:00
|
|
|
time_stamp.clone_from_slice(&tm.timestamp().unwrap()[0..7]);
|
2023-01-11 10:30:03 +01:00
|
|
|
Self {
|
|
|
|
subservice: tm.subservice(),
|
|
|
|
apid: tm.apid(),
|
|
|
|
msg_counter: tm.msg_counter(),
|
|
|
|
dest_id: tm.dest_id(),
|
|
|
|
time_stamp,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|