error handling is tricky

This commit is contained in:
Robin Müller 2022-08-08 02:03:15 +02:00
parent db78b02348
commit 51c558d7cc
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
6 changed files with 82 additions and 5 deletions

32
fsrc-core/src/error.rs Normal file
View File

@ -0,0 +1,32 @@
pub enum FsrcGroupIds {
Tmtc = 0,
}
pub struct FsrcErrorRaw {
pub group_id: u8,
pub unique_id: u8,
pub group_name: &'static str,
pub error_info: &'static str,
}
pub trait FsrcErrorHandler {
fn error(&mut self, e: FsrcErrorRaw);
fn error_with_one_param(&mut self, e: FsrcErrorRaw, _p1: u32) {
self.error(e);
}
fn error_with_two_params(&mut self, e: FsrcErrorRaw, _p1: u32, _p2: u32) {
self.error(e);
}
}
pub struct SimpleStdErrorHandler {}
#[cfg(feature = "use_std")]
impl FsrcErrorHandler for SimpleStdErrorHandler {
fn error(&mut self, e: FsrcErrorRaw) {
println!(
"Received error from group {} with ID ({},{}): {}",
e.group_name, e.group_id, e.unique_id, e.error_info
);
}
}

View File

@ -1,4 +1,5 @@
//! # Core components of the Flight Software Rust Crate (FSRC) collection
pub mod error;
pub mod event_man;
pub mod events;
pub mod executable;

View File

@ -1,18 +1,41 @@
use crate::tmtc::{ReceivesCcsds, ReceivesTc};
use spacepackets::{CcsdsPacket, SpHeader};
use crate::error::FsrcErrorHandler;
use crate::tmtc::{
ReceivesCcsds, ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR,
};
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
pub trait ApidHandler {
fn get_apid_handler(&self, apid: u16) -> Box<dyn ReceivesCcsds>;
}
struct CcsdsDistributor {
error_handler: Box<dyn FsrcErrorHandler>,
apid_handlers: Box<dyn ApidHandler>,
}
impl ReceivesTc for CcsdsDistributor {
fn pass_tc(&mut self, tm_raw: &[u8]) {
// TODO: Better error handling
let sp_header = SpHeader::from_raw_slice(tm_raw).unwrap();
let sp_header = match SpHeader::from_raw_slice(tm_raw) {
Ok(header) => header,
Err(e) => {
match e {
PacketError::FromBytesSliceTooSmall(missmatch) => {
self.error_handler.error_with_two_params(
FROM_BYTES_SLICE_TOO_SMALL_ERROR,
missmatch.found as u32,
missmatch.expected as u32,
);
}
PacketError::FromBytesZeroCopyError => {
self.error_handler.error(FROM_BYTES_ZEROCOPY_ERROR);
}
_ => {
// TODO: Unexpected error
}
}
return;
}
};
let mut handler = self.apid_handlers.get_apid_handler(sp_header.apid());
handler.pass_ccsds(&sp_header, tm_raw).unwrap();
}

View File

@ -1,3 +1,4 @@
use crate::error::{FsrcErrorRaw, FsrcGroupIds};
use spacepackets::ecss::PusError;
use spacepackets::tc::PusTc;
use spacepackets::{PacketError, SpHeader};
@ -5,6 +6,24 @@ use spacepackets::{PacketError, SpHeader};
pub mod ccsds_distrib;
pub mod pus_distrib;
const RAW_PACKET_ERROR: &str = "tmtc-raw";
const CCSDS_ERROR: &str = "tmtc-ccsds";
const PUS_ERROR: &str = "tmtc-pus";
// TODO: A macro for general and unknown errors would be nice
const FROM_BYTES_SLICE_TOO_SMALL_ERROR: FsrcErrorRaw = FsrcErrorRaw {
group_name: RAW_PACKET_ERROR,
group_id: FsrcGroupIds::Tmtc as u8,
unique_id: 0,
error_info: "FROM_BYTES_SLICE_TOO_SMALL_ERROR",
};
const FROM_BYTES_ZEROCOPY_ERROR: FsrcErrorRaw = FsrcErrorRaw {
group_name: RAW_PACKET_ERROR,
group_id: FsrcGroupIds::Tmtc as u8,
unique_id: 1,
error_info: "FROM_BYTES_ZEROCOPY_ERROR",
};
pub trait ReceivesTc {
fn pass_tc(&mut self, tc_raw: &[u8]);
}

View File

@ -1,3 +1,4 @@
use crate::error::FsrcErrorHandler;
use crate::tmtc::{ReceivesCcsds, ReceivesPus, ReceivesTc};
use spacepackets::ecss::PusPacket;
use spacepackets::tc::PusTc;
@ -9,6 +10,7 @@ pub trait PusServiceProvider {
}
pub struct PusDistributor {
error_handler: Box<dyn FsrcErrorHandler>,
service_provider: Box<dyn PusServiceProvider>,
}

@ -1 +1 @@
Subproject commit a7b97e22f5c7a183372bd30933e66b8bf3fc6a7b
Subproject commit a20955437e94af164969b0d9eec2a62fac616745