From 4f7a0785f1f40f30e37ddb5eaaf7a9d8b4d06185 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 27 Jan 2023 00:25:51 +0100 Subject: [PATCH] add Error impls for tmtc Error enums --- satrs-core/src/tmtc/ccsds_distrib.rs | 28 +++++++++++++++++++++++++--- satrs-core/src/tmtc/pus_distrib.rs | 22 ++++++++++++++++++++++ satrs-example/src/tmtc.rs | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/satrs-core/src/tmtc/ccsds_distrib.rs b/satrs-core/src/tmtc/ccsds_distrib.rs index 79b7b63..1e45680 100644 --- a/satrs-core/src/tmtc/ccsds_distrib.rs +++ b/satrs-core/src/tmtc/ccsds_distrib.rs @@ -86,8 +86,11 @@ //! ``` use crate::tmtc::{ReceivesCcsdsTc, ReceivesTcCore}; use alloc::boxed::Box; +use core::fmt::{Display, Formatter}; use downcast_rs::Downcast; use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader}; +#[cfg(feature = "std")] +use std::error::Error; /// Generic trait for a handler or dispatcher object handling CCSDS packets. /// @@ -134,7 +137,26 @@ pub struct CcsdsDistributor { #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum CcsdsError { CustomError(E), - PacketError(ByteConversionError), + ByteConversionError(ByteConversionError), +} + +impl Display for CcsdsError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + Self::CustomError(e) => write!(f, "{e}"), + Self::ByteConversionError(e) => write!(f, "{e}"), + } + } +} + +#[cfg(feature = "std")] +impl Error for CcsdsError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self { + Self::CustomError(e) => e.source(), + Self::ByteConversionError(e) => e.source(), + } + } } impl ReceivesCcsdsTc for CcsdsDistributor { @@ -150,7 +172,7 @@ impl ReceivesTcCore for CcsdsDistributor { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { if tc_raw.len() < 7 { - return Err(CcsdsError::PacketError( + return Err(CcsdsError::ByteConversionError( ByteConversionError::FromSliceTooSmall(SizeMissmatch { found: tc_raw.len(), expected: 7, @@ -158,7 +180,7 @@ impl ReceivesTcCore for CcsdsDistributor { )); } let (sp_header, _) = - SpHeader::from_be_bytes(tc_raw).map_err(|e| CcsdsError::PacketError(e))?; + SpHeader::from_be_bytes(tc_raw).map_err(|e| CcsdsError::ByteConversionError(e))?; self.dispatch_ccsds(&sp_header, tc_raw) } } diff --git a/satrs-core/src/tmtc/pus_distrib.rs b/satrs-core/src/tmtc/pus_distrib.rs index 9420b4c..9d2290e 100644 --- a/satrs-core/src/tmtc/pus_distrib.rs +++ b/satrs-core/src/tmtc/pus_distrib.rs @@ -62,10 +62,13 @@ //! ``` use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTcCore}; use alloc::boxed::Box; +use core::fmt::{Display, Formatter}; use downcast_rs::Downcast; use spacepackets::ecss::{PusError, PusPacket}; use spacepackets::tc::PusTc; use spacepackets::SpHeader; +#[cfg(feature = "std")] +use std::error::Error; pub trait PusServiceProvider: Downcast { type Error; @@ -104,6 +107,25 @@ pub enum PusDistribError { PusError(PusError), } +impl Display for PusDistribError { + fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { + match self { + PusDistribError::CustomError(e) => write!(f, "{e}"), + PusDistribError::PusError(e) => write!(f, "{e}"), + } + } +} + +#[cfg(feature = "std")] +impl Error for PusDistribError { + fn source(&self) -> Option<&(dyn Error + 'static)> { + match self { + Self::CustomError(e) => e.source(), + Self::PusError(e) => e.source(), + } + } +} + impl ReceivesTcCore for PusDistributor { type Error = PusDistribError; fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> { diff --git a/satrs-example/src/tmtc.rs b/satrs-example/src/tmtc.rs index 34d91a8..fc9aeba 100644 --- a/satrs-example/src/tmtc.rs +++ b/satrs-example/src/tmtc.rs @@ -222,7 +222,7 @@ fn poll_tc_server(udp_tmtc_server: &mut UdpTmtcServer) -> bool { Ok(_) => true, Err(e) => match e { ReceiveResult::ReceiverError(e) => match e { - CcsdsError::PacketError(e) => { + CcsdsError::ByteConversionError(e) => { println!("Got packet error: {e:?}"); true }