From 7e0d3f394af309235854a62bbef22f1d0298733b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sun, 27 Nov 2022 19:24:18 +0100 Subject: [PATCH] add returncode module --- Cargo.lock | 1 + satrs-core/src/resultcode.rs | 27 +++++++++++++++++++++++++++ satrs-example/Cargo.toml | 3 +++ satrs-example/src/main.rs | 1 + satrs-example/src/pus.rs | 23 ++++++++++++----------- satrs-example/src/results.rs | 15 +++++++++++++++ satrs-macros/src/lib.rs | 6 +++++- 7 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 satrs-example/src/results.rs diff --git a/Cargo.lock b/Cargo.lock index fd2ae3e..a1eae3b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -647,6 +647,7 @@ dependencies = [ "crossbeam-channel", "delegate 0.8.0", "satrs-core", + "satrs-macros", "spacepackets", "zerocopy", ] diff --git a/satrs-core/src/resultcode.rs b/satrs-core/src/resultcode.rs index 885c7fb..5bfc598 100644 --- a/satrs-core/src/resultcode.rs +++ b/satrs-core/src/resultcode.rs @@ -1,3 +1,6 @@ +use spacepackets::{ByteConversionError, SizeMissmatch}; +use spacepackets::ecss::{EcssEnumeration, EcssEnumU16}; + #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct ResultU16 { group_id: u8, @@ -22,6 +25,30 @@ impl ResultU16 { } } +impl From for EcssEnumU16 { + fn from(v: ResultU16) -> Self { + EcssEnumU16::new(v.raw()) + } +} + +impl EcssEnumeration for ResultU16 { + fn pfc(&self) -> u8 { + 16 + } + + fn write_to_be_bytes(&self, buf: &mut [u8]) -> Result<(), ByteConversionError> { + if buf.len() < 2 { + return Err(ByteConversionError::ToSliceTooSmall(SizeMissmatch { + found: buf.len(), + expected: 2 + })); + } + buf[0] = self.group_id; + buf[1] = self.unique_id; + Ok(()) + } +} + #[derive(Debug)] pub struct ResultU16Ext { pub name: &'static str, diff --git a/satrs-example/Cargo.toml b/satrs-example/Cargo.toml index 2542451..94bf27f 100644 --- a/satrs-example/Cargo.toml +++ b/satrs-example/Cargo.toml @@ -13,5 +13,8 @@ zerocopy = "0.6" [dependencies.spacepackets] path = "../spacepackets" +[dependencies.satrs-macros] +path = "../satrs-macros" + [dependencies.satrs-core] path = "../satrs-core" diff --git a/satrs-example/src/main.rs b/satrs-example/src/main.rs index efb6628..159fe67 100644 --- a/satrs-example/src/main.rs +++ b/satrs-example/src/main.rs @@ -1,6 +1,7 @@ mod ccsds; mod pus; mod tmtc; +mod results; use crate::tmtc::{core_tmtc_task, CoreTmtcArgs, TmStore, PUS_APID}; use satrs_core::event_man::{ diff --git a/satrs-example/src/pus.rs b/satrs-example/src/pus.rs index 18a21b9..a2950aa 100644 --- a/satrs-example/src/pus.rs +++ b/satrs-example/src/pus.rs @@ -1,4 +1,5 @@ use crate::tmtc::TmStore; +use crate::results::{INVALID_PUS_SERVICE, INVALID_PUS_SUBSERVICE, NOT_ENOUGH_APP_DATA}; use satrs_core::events::EventU32; use satrs_core::pool::StoreAddr; use satrs_core::pus::event::Subservices; @@ -8,11 +9,12 @@ use satrs_core::pus::verification::{ }; use satrs_core::tmtc::tm_helper::PusTmWithCdsShortHelper; use satrs_core::tmtc::PusServiceProvider; -use spacepackets::ecss::{EcssEnumU16, PusPacket}; +use spacepackets::ecss::PusPacket; use spacepackets::tc::PusTc; use spacepackets::time::{CdsShortTimeProvider, TimeWriter}; use spacepackets::SpHeader; use std::sync::mpsc; +use satrs_core::resultcode::ResultU16; pub struct PusReceiver { pub tm_helper: PusTmWithCdsShortHelper, @@ -64,8 +66,9 @@ impl PusServiceProvider for PusReceiver { } else if service == 5 { self.handle_event_service(pus_tc, accepted_token); } else { - // TODO: Unknown service verification failure - // TODO: Unknown service returncode + self.update_time_stamp(); + self.verif_reporter.start_failure(accepted_token, FailParams::new(&self.time_stamp, &INVALID_PUS_SERVICE, None)) + .expect("Start failure verification failed") } Ok(()) } @@ -89,12 +92,11 @@ impl PusReceiver { .completion_success(start_token, &self.time_stamp) .expect("Error sending completion success"); } else { - // TODO: Unknown Subservice returncode self.update_time_stamp(); self.verif_reporter .start_failure( token, - FailParams::new(&self.time_stamp, &EcssEnumU16::new(2), None), + FailParams::new(&self.time_stamp, &INVALID_PUS_SUBSERVICE, None), ) .expect("Sending start failure TM failed"); } @@ -112,12 +114,12 @@ impl PusReceiver { fn handle_event_service(&mut self, pus_tc: &PusTc, token: VerificationToken) { let send_start_failure = |verif_reporter: &mut StdVerifReporterWithSender, timestamp: &[u8; 7], - failure_code: EcssEnumU16, + failure_code: &ResultU16, failure_data: Option<&[u8]>| { verif_reporter .start_failure( token, - FailParams::new(timestamp, &failure_code, failure_data), + FailParams::new(timestamp, failure_code, failure_data), ) .expect("Sending start failure TM failed"); }; @@ -132,7 +134,7 @@ impl PusReceiver { send_start_failure( &mut self.verif_reporter, &self.time_stamp, - EcssEnumU16::new(1), + &NOT_ENOUGH_APP_DATA, None, ); return; @@ -143,7 +145,7 @@ impl PusReceiver { send_start_failure( &mut self.verif_reporter, &self.time_stamp, - EcssEnumU16::new(1), + &NOT_ENOUGH_APP_DATA, None, ); return; @@ -171,12 +173,11 @@ impl PusReceiver { .expect("Sending event request failed"); } _ => { - // TODO: Unknown Subservice returncode self.update_time_stamp(); send_start_failure( &mut self.verif_reporter, &self.time_stamp, - EcssEnumU16::new(2), + &INVALID_PUS_SUBSERVICE, None, ); } diff --git a/satrs-example/src/results.rs b/satrs-example/src/results.rs new file mode 100644 index 0000000..e521a84 --- /dev/null +++ b/satrs-example/src/results.rs @@ -0,0 +1,15 @@ +use satrs_core::resultcode::ResultU16; +use satrs_macros::resultcode; + +#[derive(Debug)] +pub enum GroupId { + Tmtc = 0, +} + +#[resultcode] +pub const INVALID_PUS_SERVICE: ResultU16 = ResultU16::const_new(GroupId::Tmtc as u8, 0); +#[resultcode] +pub const INVALID_PUS_SUBSERVICE: ResultU16 = ResultU16::const_new(GroupId::Tmtc as u8, 1); + +#[resultcode(info="Not enough data inside the TC application data field")] +pub const NOT_ENOUGH_APP_DATA: ResultU16 = ResultU16::const_new(GroupId::Tmtc as u8, 2); diff --git a/satrs-macros/src/lib.rs b/satrs-macros/src/lib.rs index 98a6429..4d2598e 100644 --- a/satrs-macros/src/lib.rs +++ b/satrs-macros/src/lib.rs @@ -85,7 +85,11 @@ impl ResultExtGenerator { let result_code_name = self.name_str.to_owned().unwrap(); let name_as_str = result_code_name.to_string(); let gen_struct_name = format_ident!("{}_EXT", result_code_name); - let info_str = self.info_str.to_owned().unwrap(); + let info_str = if let Some(info_str) = &self.info_str { + info_str.value() + } else { + String::from("") + }; let gen_struct = quote! { const #gen_struct_name: satrs_core::resultcode::ResultU16Ext = satrs_core::resultcode::ResultU16Ext::const_new(