add Error impls for tmtc Error enums

This commit is contained in:
Robin Müller 2023-01-27 00:25:51 +01:00
parent 40120dc83f
commit 4f7a0785f1
No known key found for this signature in database
GPG Key ID: BE6480244DFE612C
3 changed files with 48 additions and 4 deletions

View File

@ -86,8 +86,11 @@
//! ``` //! ```
use crate::tmtc::{ReceivesCcsdsTc, ReceivesTcCore}; use crate::tmtc::{ReceivesCcsdsTc, ReceivesTcCore};
use alloc::boxed::Box; use alloc::boxed::Box;
use core::fmt::{Display, Formatter};
use downcast_rs::Downcast; use downcast_rs::Downcast;
use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader}; use spacepackets::{ByteConversionError, CcsdsPacket, SizeMissmatch, SpHeader};
#[cfg(feature = "std")]
use std::error::Error;
/// Generic trait for a handler or dispatcher object handling CCSDS packets. /// Generic trait for a handler or dispatcher object handling CCSDS packets.
/// ///
@ -134,7 +137,26 @@ pub struct CcsdsDistributor<E> {
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum CcsdsError<E> { pub enum CcsdsError<E> {
CustomError(E), CustomError(E),
PacketError(ByteConversionError), ByteConversionError(ByteConversionError),
}
impl<E: Display> Display for CcsdsError<E> {
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<E: Error> Error for CcsdsError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::CustomError(e) => e.source(),
Self::ByteConversionError(e) => e.source(),
}
}
} }
impl<E: 'static> ReceivesCcsdsTc for CcsdsDistributor<E> { impl<E: 'static> ReceivesCcsdsTc for CcsdsDistributor<E> {
@ -150,7 +172,7 @@ impl<E: 'static> ReceivesTcCore for CcsdsDistributor<E> {
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
if tc_raw.len() < 7 { if tc_raw.len() < 7 {
return Err(CcsdsError::PacketError( return Err(CcsdsError::ByteConversionError(
ByteConversionError::FromSliceTooSmall(SizeMissmatch { ByteConversionError::FromSliceTooSmall(SizeMissmatch {
found: tc_raw.len(), found: tc_raw.len(),
expected: 7, expected: 7,
@ -158,7 +180,7 @@ impl<E: 'static> ReceivesTcCore for CcsdsDistributor<E> {
)); ));
} }
let (sp_header, _) = 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) self.dispatch_ccsds(&sp_header, tc_raw)
} }
} }

View File

@ -62,10 +62,13 @@
//! ``` //! ```
use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTcCore}; use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTcCore};
use alloc::boxed::Box; use alloc::boxed::Box;
use core::fmt::{Display, Formatter};
use downcast_rs::Downcast; use downcast_rs::Downcast;
use spacepackets::ecss::{PusError, PusPacket}; use spacepackets::ecss::{PusError, PusPacket};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::SpHeader; use spacepackets::SpHeader;
#[cfg(feature = "std")]
use std::error::Error;
pub trait PusServiceProvider: Downcast { pub trait PusServiceProvider: Downcast {
type Error; type Error;
@ -104,6 +107,25 @@ pub enum PusDistribError<E> {
PusError(PusError), PusError(PusError),
} }
impl<E: Display> Display for PusDistribError<E> {
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<E: Error> Error for PusDistribError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::CustomError(e) => e.source(),
Self::PusError(e) => e.source(),
}
}
}
impl<E: 'static> ReceivesTcCore for PusDistributor<E> { impl<E: 'static> ReceivesTcCore for PusDistributor<E> {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> {

View File

@ -222,7 +222,7 @@ fn poll_tc_server(udp_tmtc_server: &mut UdpTmtcServer) -> bool {
Ok(_) => true, Ok(_) => true,
Err(e) => match e { Err(e) => match e {
ReceiveResult::ReceiverError(e) => match e { ReceiveResult::ReceiverError(e) => match e {
CcsdsError::PacketError(e) => { CcsdsError::ByteConversionError(e) => {
println!("Got packet error: {e:?}"); println!("Got packet error: {e:?}");
true true
} }