diff --git a/fsrc-core/src/pus/event.rs b/fsrc-core/src/pus/event.rs index a4867e6..10e0618 100644 --- a/fsrc-core/src/pus/event.rs +++ b/fsrc-core/src/pus/event.rs @@ -25,6 +25,37 @@ impl From for u8 { } } +impl TryFrom for Subservices { + type Error = (); + + fn try_from(value: u8) -> Result { + match value { + x if x == Subservices::TmInfoReport as u8 => Ok(Subservices::TmInfoReport), + x if x == Subservices::TmLowSeverityReport as u8 => { + Ok(Subservices::TmLowSeverityReport) + } + x if x == Subservices::TmMediumSeverityReport as u8 => { + Ok(Subservices::TmMediumSeverityReport) + } + x if x == Subservices::TmHighSeverityReport as u8 => { + Ok(Subservices::TmHighSeverityReport) + } + x if x == Subservices::TcEnableEventGeneration as u8 => { + Ok(Subservices::TcEnableEventGeneration) + } + x if x == Subservices::TcDisableEventGeneration as u8 => { + Ok(Subservices::TcDisableEventGeneration) + } + x if x == Subservices::TcReportDisabledList as u8 => { + Ok(Subservices::TcReportDisabledList) + } + x if x == Subservices::TmDisabledEventsReport as u8 => { + Ok(Subservices::TmDisabledEventsReport) + } + _ => Err(()), + } + } +} pub struct EventReporterBase { msg_count: u16, apid: u16, diff --git a/fsrc-example/src/bin/obsw/pus.rs b/fsrc-example/src/bin/obsw/pus.rs index c0a0c68..5caaf17 100644 --- a/fsrc-example/src/bin/obsw/pus.rs +++ b/fsrc-example/src/bin/obsw/pus.rs @@ -1,11 +1,15 @@ use crate::tmtc::TmStore; +use fsrc_core::events::EventU32; use fsrc_core::pool::StoreAddr; +use fsrc_core::pus::event::Subservices; +use fsrc_core::pus::event_man::{EventRequest, EventRequestWithToken}; use fsrc_core::pus::verification::{ - SharedStdVerifReporterWithSender, TcStateAccepted, VerificationToken, + FailParams, SharedStdVerifReporterWithSender, TcStateAccepted, VerificationToken, }; use fsrc_core::tmtc::tm_helper::PusTmWithCdsShortHelper; use fsrc_core::tmtc::PusServiceProvider; -use spacepackets::tc::{PusTc, PusTcSecondaryHeaderT}; +use spacepackets::ecss::{EcssEnumU16, PusPacket}; +use spacepackets::tc::PusTc; use spacepackets::time::{CdsShortTimeProvider, TimeWriter}; use spacepackets::SpHeader; use std::sync::mpsc; @@ -15,6 +19,7 @@ pub struct PusReceiver { pub tm_tx: mpsc::Sender, pub tm_store: TmStore, pub verif_reporter: SharedStdVerifReporterWithSender, + event_request_tx: mpsc::Sender, stamper: CdsShortTimeProvider, time_stamp: [u8; 7], } @@ -25,12 +30,14 @@ impl PusReceiver { tm_tx: mpsc::Sender, tm_store: TmStore, verif_reporter: SharedStdVerifReporterWithSender, + event_request_tx: mpsc::Sender, ) -> Self { Self { tm_helper: PusTmWithCdsShortHelper::new(apid), tm_tx, tm_store, verif_reporter, + event_request_tx, stamper: CdsShortTimeProvider::default(), time_stamp: [0; 7], } @@ -64,7 +71,7 @@ impl PusServiceProvider for PusReceiver { if service == 17 { self.handle_test_service(pus_tc, accepted_token); } else if service == 5 { - // TODO: Send message to event manager here + self.handle_event_service(pus_tc, accepted_token); } Ok(()) } @@ -92,4 +99,90 @@ impl PusReceiver { .expect("Error sending completion success"); } } + + fn update_time_stamp(&mut self) { + self.stamper + .update_from_now() + .expect("Updating timestamp failed"); + self.stamper + .write_to_bytes(&mut self.time_stamp) + .expect("Writing timestamp failed"); + } + + fn handle_event_service(&mut self, pus_tc: &PusTc, token: VerificationToken) { + let send_start_failure = |verif_reporter: &mut SharedStdVerifReporterWithSender, + timestamp: &[u8; 7], + failure_code: EcssEnumU16, + failure_data: Option<&[u8]>| { + verif_reporter + .lock() + .expect("Locking verification reporter failed") + .start_failure( + token, + FailParams::new(timestamp, &failure_code, failure_data), + ) + .expect("Sending start failure TM failed"); + }; + let send_start_acceptance = |verif_reporter: &mut SharedStdVerifReporterWithSender, + timestamp: &[u8; 7]| { + verif_reporter + .lock() + .expect("Locking verification reporter failed") + .start_success(token, timestamp) + .expect("Sending start success TM failed") + }; + if pus_tc.user_data().is_none() { + self.update_time_stamp(); + send_start_failure( + &mut self.verif_reporter, + &self.time_stamp, + EcssEnumU16::new(1), + None, + ); + return; + } + let app_data = pus_tc.user_data().unwrap(); + if app_data.len() < 4 { + self.update_time_stamp(); + send_start_failure( + &mut self.verif_reporter, + &self.time_stamp, + EcssEnumU16::new(1), + None, + ); + return; + } + let event_id = EventU32::from(u32::from_be_bytes(app_data.try_into().unwrap())); + match PusPacket::subservice(pus_tc).try_into() { + Ok(Subservices::TcEnableEventGeneration) => { + self.update_time_stamp(); + let start_token = send_start_acceptance(&mut self.verif_reporter, &self.time_stamp); + self.event_request_tx + .send(EventRequestWithToken { + request: EventRequest::Enable(event_id), + token: start_token, + }) + .expect("Sending event request failed"); + } + Ok(Subservices::TcDisableEventGeneration) => { + self.update_time_stamp(); + let start_token = send_start_acceptance(&mut self.verif_reporter, &self.time_stamp); + self.event_request_tx + .send(EventRequestWithToken { + request: EventRequest::Disable(event_id), + token: start_token, + }) + .expect("Sending event request failed"); + } + _ => { + self.update_time_stamp(); + send_start_failure( + &mut self.verif_reporter, + &self.time_stamp, + EcssEnumU16::new(2), + None, + ); + } + } + } } diff --git a/fsrc-example/src/bin/obsw/tmtc.rs b/fsrc-example/src/bin/obsw/tmtc.rs index 3ff05f4..7806347 100644 --- a/fsrc-example/src/bin/obsw/tmtc.rs +++ b/fsrc-example/src/bin/obsw/tmtc.rs @@ -52,6 +52,7 @@ pub fn core_tmtc_task( args.tm_sender, args.tm_store.clone(), verif_reporter, + args.event_request_tx, ); let pus_distributor = PusDistributor::new(Box::new(pus_receiver)); let ccsds_receiver = CcsdsReceiver {