Simplifications for TMTC error handling #50

Merged
muellerr merged 11 commits from simplify-tm-sender-error-type into main 2023-07-10 23:20:44 +02:00
4 changed files with 44 additions and 72 deletions
Showing only changes of commit 1f4e6714ba - Show all commits

View File

@ -5,13 +5,14 @@ use crate::pus::verification::{
StdVerifReporterWithSender, TcStateAccepted, TcStateToken, VerificationToken, StdVerifReporterWithSender, TcStateAccepted, TcStateToken, VerificationToken,
}; };
use crate::pus::{ use crate::pus::{
AcceptedTc, PartialPusHandlingError, PusPacketHandlerResult, PusPacketHandlingError, AcceptedTc, EcssTmSender, PartialPusHandlingError, PusPacketHandlerResult,
PusServiceBase, PusServiceHandler, PusPacketHandlingError, PusServiceBase, PusServiceHandler,
}; };
use crate::tmtc::tm_helper::SharedTmStore; use crate::tmtc::tm_helper::SharedTmStore;
use spacepackets::ecss::event::Subservice; use spacepackets::ecss::event::Subservice;
use spacepackets::ecss::PusPacket; use spacepackets::ecss::PusPacket;
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use std::boxed::Box;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
pub struct PusService5EventHandler { pub struct PusService5EventHandler {
@ -23,21 +24,13 @@ impl PusService5EventHandler {
pub fn new( pub fn new(
receiver: Receiver<AcceptedTc>, receiver: Receiver<AcceptedTc>,
tc_pool: SharedPool, tc_pool: SharedPool,
tm_tx: Sender<StoreAddr>, tm_sender: Box<dyn EcssTmSender>,
tm_store: SharedTmStore,
tm_apid: u16, tm_apid: u16,
verification_handler: StdVerifReporterWithSender, verification_handler: StdVerifReporterWithSender,
event_request_tx: Sender<EventRequestWithToken>, event_request_tx: Sender<EventRequestWithToken>,
) -> Self { ) -> Self {
Self { Self {
psb: PusServiceBase::new( psb: PusServiceBase::new(receiver, tc_pool, tm_sender, tm_apid, verification_handler),
receiver,
tc_pool,
tm_tx,
tm_store,
tm_apid,
verification_handler,
),
event_request_tx, event_request_tx,
} }
} }

View File

@ -9,6 +9,7 @@ use downcast_rs::{impl_downcast, Downcast};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use dyn_clone::DynClone; use dyn_clone::DynClone;
use spacepackets::ecss::PusError; use spacepackets::ecss::PusError;
use spacepackets::tc::PusTc;
use spacepackets::tm::PusTm; use spacepackets::tm::PusTm;
use spacepackets::{ByteConversionError, SizeMissmatch}; use spacepackets::{ByteConversionError, SizeMissmatch};
use std::error::Error; use std::error::Error;
@ -28,6 +29,7 @@ pub mod verification;
pub use alloc_mod::*; pub use alloc_mod::*;
use crate::pool::{StoreAddr, StoreError}; use crate::pool::{StoreAddr, StoreError};
use crate::pus::verification::TcStateToken;
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub use std_mod::*; pub use std_mod::*;
@ -140,6 +142,21 @@ pub trait EcssSender: Send {
} }
} }
/// 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>;
}
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
mod alloc_mod { mod alloc_mod {
use super::*; use super::*;
@ -191,9 +208,13 @@ pub mod std_mod {
use crate::pus::verification::{ use crate::pus::verification::{
StdVerifReporterWithSender, TcStateAccepted, TcStateToken, VerificationToken, StdVerifReporterWithSender, TcStateAccepted, TcStateToken, VerificationToken,
}; };
use crate::pus::{EcssSender, EcssTmtcErrorWithSend, GenericSendError, PusTmWrapper}; use crate::pus::{
EcssSender, EcssTmSender, EcssTmSenderCore, EcssTmtcErrorWithSend, GenericSendError,
PusTmWrapper,
};
use crate::tmtc::tm_helper::SharedTmStore; use crate::tmtc::tm_helper::SharedTmStore;
use crate::SenderId; use crate::SenderId;
use alloc::boxed::Box;
use alloc::vec::Vec; use alloc::vec::Vec;
use spacepackets::ecss::{PusError, SerializablePusPacket}; use spacepackets::ecss::{PusError, SerializablePusPacket};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
@ -206,25 +227,6 @@ pub mod std_mod {
use std::sync::{mpsc, RwLockWriteGuard}; use std::sync::{mpsc, RwLockWriteGuard};
use thiserror::Error; use thiserror::Error;
/// 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>;
}
#[derive(Clone)] #[derive(Clone)]
pub struct MpscTmInStoreSender { pub struct MpscTmInStoreSender {
id: SenderId, id: SenderId,
@ -375,9 +377,9 @@ pub mod std_mod {
#[derive(Debug, Clone, Error)] #[derive(Debug, Clone, Error)]
pub enum PartialPusHandlingError { pub enum PartialPusHandlingError {
#[error("Generic timestamp generation error")] #[error("Generic timestamp generation error")]
Time(StdTimestampError), Time(#[from] StdTimestampError),
#[error("Error sending telemetry: {0}")] #[error("Error sending telemetry: {0}")]
TmSend(String), TmSend(#[from] EcssTmtcErrorWithSend),
#[error("Error sending verification message")] #[error("Error sending verification message")]
Verification, Verification,
} }
@ -408,8 +410,7 @@ pub mod std_mod {
pub struct PusServiceBase { pub struct PusServiceBase {
pub tc_rx: mpsc::Receiver<AcceptedTc>, pub tc_rx: mpsc::Receiver<AcceptedTc>,
pub tc_store: SharedPool, pub tc_store: SharedPool,
pub tm_tx: mpsc::Sender<StoreAddr>, pub tm_sender: Box<dyn EcssTmSender>,
pub tm_store: SharedTmStore,
pub tm_apid: u16, pub tm_apid: u16,
/// The verification handler is wrapped in a [RefCell] to allow the interior mutability /// 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. /// pattern. This makes writing methods which are not mutable a lot easier.
@ -422,8 +423,7 @@ pub mod std_mod {
pub fn new( pub fn new(
receiver: mpsc::Receiver<AcceptedTc>, receiver: mpsc::Receiver<AcceptedTc>,
tc_pool: SharedPool, tc_pool: SharedPool,
tm_tx: mpsc::Sender<StoreAddr>, tm_sender: Box<dyn EcssTmSender>,
tm_store: SharedTmStore,
tm_apid: u16, tm_apid: u16,
verification_handler: StdVerifReporterWithSender, verification_handler: StdVerifReporterWithSender,
) -> Self { ) -> Self {
@ -431,8 +431,7 @@ pub mod std_mod {
tc_rx: receiver, tc_rx: receiver,
tc_store: tc_pool, tc_store: tc_pool,
tm_apid, tm_apid,
tm_tx, tm_sender,
tm_store,
verification_handler: RefCell::new(verification_handler), verification_handler: RefCell::new(verification_handler),
pus_buf: [0; 2048], pus_buf: [0; 2048],
pus_size: 0, pus_size: 0,

View File

@ -2,12 +2,14 @@ use crate::pool::{SharedPool, StoreAddr};
use crate::pus::scheduler::PusScheduler; use crate::pus::scheduler::PusScheduler;
use crate::pus::verification::{StdVerifReporterWithSender, TcStateAccepted, VerificationToken}; use crate::pus::verification::{StdVerifReporterWithSender, TcStateAccepted, VerificationToken};
use crate::pus::{ use crate::pus::{
AcceptedTc, PusPacketHandlerResult, PusPacketHandlingError, PusServiceBase, PusServiceHandler, AcceptedTc, EcssTmSender, PusPacketHandlerResult, PusPacketHandlingError, PusServiceBase,
PusServiceHandler,
}; };
use crate::tmtc::tm_helper::SharedTmStore; use crate::tmtc::tm_helper::SharedTmStore;
use spacepackets::ecss::{scheduling, PusPacket}; use spacepackets::ecss::{scheduling, PusPacket};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::time::cds::TimeProvider; use spacepackets::time::cds::TimeProvider;
use std::boxed::Box;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
/// This is a helper class for [std] environments to handle generic PUS 11 (scheduling service) /// This is a helper class for [std] environments to handle generic PUS 11 (scheduling service)
@ -27,21 +29,13 @@ impl PusService11SchedHandler {
pub fn new( pub fn new(
receiver: Receiver<AcceptedTc>, receiver: Receiver<AcceptedTc>,
tc_pool: SharedPool, tc_pool: SharedPool,
tm_tx: Sender<StoreAddr>, tm_sender: Box<dyn EcssTmSender>,
tm_store: SharedTmStore,
tm_apid: u16, tm_apid: u16,
verification_handler: StdVerifReporterWithSender, verification_handler: StdVerifReporterWithSender,
scheduler: PusScheduler, scheduler: PusScheduler,
) -> Self { ) -> Self {
Self { Self {
psb: PusServiceBase::new( psb: PusServiceBase::new(receiver, tc_pool, tm_sender, tm_apid, verification_handler),
receiver,
tc_pool,
tm_tx,
tm_store,
tm_apid,
verification_handler,
),
scheduler, scheduler,
} }
} }

View File

@ -1,14 +1,15 @@
use crate::pool::{SharedPool, StoreAddr}; use crate::pool::{SharedPool, StoreAddr};
use crate::pus::verification::{StdVerifReporterWithSender, TcStateAccepted, VerificationToken}; use crate::pus::verification::{StdVerifReporterWithSender, TcStateAccepted, VerificationToken};
use crate::pus::{ use crate::pus::{
AcceptedTc, PartialPusHandlingError, PusPacketHandlerResult, PusPacketHandlingError, AcceptedTc, EcssTmSender, PartialPusHandlingError, PusPacketHandlerResult,
PusServiceBase, PusServiceHandler, PusPacketHandlingError, PusServiceBase, PusServiceHandler, PusTmWrapper,
}; };
use crate::tmtc::tm_helper::SharedTmStore; use crate::tmtc::tm_helper::SharedTmStore;
use spacepackets::ecss::PusPacket; use spacepackets::ecss::PusPacket;
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::tm::{PusTm, PusTmSecondaryHeader}; use spacepackets::tm::{PusTm, PusTmSecondaryHeader};
use spacepackets::SpHeader; use spacepackets::SpHeader;
use std::boxed::Box;
use std::format; use std::format;
use std::sync::mpsc::{Receiver, Sender}; use std::sync::mpsc::{Receiver, Sender};
@ -22,20 +23,12 @@ impl PusService17TestHandler {
pub fn new( pub fn new(
receiver: Receiver<AcceptedTc>, receiver: Receiver<AcceptedTc>,
tc_pool: SharedPool, tc_pool: SharedPool,
tm_tx: Sender<StoreAddr>, tm_sender: Box<dyn EcssTmSender>,
tm_store: SharedTmStore,
tm_apid: u16, tm_apid: u16,
verification_handler: StdVerifReporterWithSender, verification_handler: StdVerifReporterWithSender,
) -> Self { ) -> Self {
Self { Self {
psb: PusServiceBase::new( psb: PusServiceBase::new(receiver, tc_pool, tm_sender, tm_apid, verification_handler),
receiver,
tc_pool,
tm_tx,
tm_store,
tm_apid,
verification_handler,
),
} }
} }
} }
@ -77,15 +70,8 @@ impl PusServiceHandler for PusService17TestHandler {
let mut reply_header = SpHeader::tm_unseg(self.psb.tm_apid, 0, 0).unwrap(); let mut reply_header = SpHeader::tm_unseg(self.psb.tm_apid, 0, 0).unwrap();
let tc_header = PusTmSecondaryHeader::new_simple(17, 2, &time_stamp); let tc_header = PusTmSecondaryHeader::new_simple(17, 2, &time_stamp);
let ping_reply = PusTm::new(&mut reply_header, tc_header, None, true); let ping_reply = PusTm::new(&mut reply_header, tc_header, None, true);
let addr = self.psb.tm_store.add_pus_tm(&ping_reply); let result = self.psb.tm_sender.send_tm(PusTmWrapper::Direct(ping_reply));
if let Err(e) = self
.psb
.tm_tx
.send(addr)
.map_err(|e| PartialPusHandlingError::TmSend(format!("{e}")))
{
partial_error = Some(e);
}
if let Some(start_token) = start_token { if let Some(start_token) = start_token {
if self if self
.psb .psb