From 7e78e70a172ccca64146d8b41ac7abf5d88d98d5 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 2 May 2024 11:14:05 +0200 Subject: [PATCH] try unifying some direct PUS handler code --- satrs-example/src/pus/event.rs | 61 ++++++++++++++------ satrs-example/src/pus/mod.rs | 5 ++ satrs-example/src/pus/scheduler.rs | 63 +++++++++++++++------ satrs-example/src/pus/test.rs | 90 +++++++++++++++++++----------- 4 files changed, 153 insertions(+), 66 deletions(-) diff --git a/satrs-example/src/pus/event.rs b/satrs-example/src/pus/event.rs index 2f96ba9..80951a4 100644 --- a/satrs-example/src/pus/event.rs +++ b/satrs-example/src/pus/event.rs @@ -1,7 +1,6 @@ use std::sync::mpsc; use crate::pus::create_verification_reporter; -use log::{error, warn}; use satrs::pool::SharedStaticMemoryPool; use satrs::pus::event_man::EventRequestWithToken; use satrs::pus::event_srv::PusEventServiceHandler; @@ -11,10 +10,11 @@ use satrs::pus::{ EcssTcInSharedStoreConverter, EcssTcInVecConverter, EcssTmSender, MpscTcReceiver, MpscTmAsVecSender, PartialPusHandlingError, PusServiceHelper, }; +use satrs::spacepackets::ecss::PusServiceId; use satrs::tmtc::{PacketAsVec, PacketSenderWithSharedPool}; use satrs_example::config::components::PUS_EVENT_MANAGEMENT; -use super::HandlingStatus; +use super::{DirectPusService, HandlingStatus}; pub fn create_event_service_static( tm_sender: PacketSenderWithSharedPool, @@ -62,28 +62,55 @@ pub struct EventServiceWrapper, } +impl DirectPusService + for EventServiceWrapper +{ + const SERVICE_ID: u8 = PusServiceId::Event as u8; + + const SERVICE_STR: &'static str = "events"; +} + impl EventServiceWrapper { pub fn poll_and_handle_next_tc(&mut self, time_stamp: &[u8]) -> HandlingStatus { let error_handler = |partial_error: &PartialPusHandlingError| { - log::warn!("PUS 5 partial error: {:?}", partial_error); + log::warn!( + "PUS {}({}) partial error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + partial_error + ); }; - match self + let result = self .handler - .poll_and_handle_next_tc(error_handler, time_stamp) - { - Ok(result) => match result { - DirectPusPacketHandlerResult::Handled(handling_status) => return handling_status, - DirectPusPacketHandlerResult::CustomSubservice(invalid, _) => { - warn!("PUS 5 invalid subservice {invalid}"); - } - DirectPusPacketHandlerResult::SubserviceNotImplemented(subservice, _) => { - warn!("PUS 5 subservice {subservice} not implemented"); - } - }, - Err(error) => { - error!("PUS 5 packet handling error: {error:?}") + .poll_and_handle_next_tc(error_handler, time_stamp); + if let Err(e) = result { + log::warn!( + "PUS {}({}) error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + e + ); + return HandlingStatus::HandledOne; + } + match result.unwrap() { + DirectPusPacketHandlerResult::Handled(handling_status) => return handling_status, + DirectPusPacketHandlerResult::CustomSubservice(subservice, _) => { + log::warn!( + "PUS {}({}) subservice {} not implemented", + Self::SERVICE_ID, + Self::SERVICE_STR, + subservice + ); + } + DirectPusPacketHandlerResult::SubserviceNotImplemented(subservice, _) => { + log::warn!( + "PUS {}({}) subservice {} not implemented", + Self::SERVICE_ID, + Self::SERVICE_STR, + subservice + ); } } HandlingStatus::HandledOne diff --git a/satrs-example/src/pus/mod.rs b/satrs-example/src/pus/mod.rs index 25fbdf6..37367ff 100644 --- a/satrs-example/src/pus/mod.rs +++ b/satrs-example/src/pus/mod.rs @@ -524,6 +524,11 @@ pub fn generic_pus_request_timeout_handler( Ok(()) } +pub trait DirectPusService { + const SERVICE_ID: u8; + const SERVICE_STR: &'static str; +} + #[cfg(test)] pub(crate) mod tests { use std::time::Duration; diff --git a/satrs-example/src/pus/scheduler.rs b/satrs-example/src/pus/scheduler.rs index 9fa534f..0c2242d 100644 --- a/satrs-example/src/pus/scheduler.rs +++ b/satrs-example/src/pus/scheduler.rs @@ -2,7 +2,7 @@ use std::sync::mpsc; use std::time::Duration; use crate::pus::create_verification_reporter; -use log::{error, info, warn}; +use log::info; use satrs::pool::{PoolProvider, StaticMemoryPool}; use satrs::pus::scheduler::{PusScheduler, TcInfo}; use satrs::pus::scheduler_srv::PusSchedServiceHandler; @@ -12,11 +12,12 @@ use satrs::pus::{ EcssTcInSharedStoreConverter, EcssTcInVecConverter, EcssTmSender, MpscTcReceiver, MpscTmAsVecSender, PartialPusHandlingError, PusServiceHelper, }; +use satrs::spacepackets::ecss::PusServiceId; use satrs::tmtc::{PacketAsVec, PacketInPool, PacketSenderWithSharedPool}; use satrs::ComponentId; use satrs_example::config::components::PUS_SCHED_SERVICE; -use super::HandlingStatus; +use super::{DirectPusService, HandlingStatus}; pub trait TcReleaser { fn release(&mut self, sender_id: ComponentId, enabled: bool, info: &TcInfo, tc: &[u8]) -> bool; @@ -77,6 +78,13 @@ pub struct SchedulingServiceWrapper, } +impl DirectPusService + for SchedulingServiceWrapper +{ + const SERVICE_ID: u8 = PusServiceId::Verification as u8; + + const SERVICE_STR: &'static str = "verification"; +} impl SchedulingServiceWrapper @@ -106,25 +114,46 @@ impl } pub fn poll_and_handle_next_tc(&mut self, time_stamp: &[u8]) -> HandlingStatus { - let error_handler = |patial_error: &PartialPusHandlingError| { - log::warn!("PUS 11 partial error: {:?}", patial_error); + let error_handler = |partial_error: &PartialPusHandlingError| { + log::warn!( + "PUS {}({}) partial error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + partial_error + ); }; - match self.pus_11_handler.poll_and_handle_next_tc( + + let result = self.pus_11_handler.poll_and_handle_next_tc( error_handler, time_stamp, &mut self.sched_tc_pool, - ) { - Ok(result) => match result { - DirectPusPacketHandlerResult::Handled(handling_status) => return handling_status, - DirectPusPacketHandlerResult::CustomSubservice(invalid, _) => { - warn!("PUS 11 invalid subservice {invalid}"); - } - DirectPusPacketHandlerResult::SubserviceNotImplemented(subservice, _) => { - warn!("PUS 11 Subservice {subservice} not implemented"); - } - }, - Err(error) => { - error!("PUS 11 packet handling error: {error:?}") + ); + if let Err(e) = result { + log::warn!( + "PUS {}({}) error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + e + ); + return HandlingStatus::HandledOne; + } + match result.unwrap() { + DirectPusPacketHandlerResult::Handled(handling_status) => return handling_status, + DirectPusPacketHandlerResult::CustomSubservice(subservice, _) => { + log::warn!( + "PUS {}({}) subservice {} not implemented", + Self::SERVICE_ID, + Self::SERVICE_STR, + subservice + ); + } + DirectPusPacketHandlerResult::SubserviceNotImplemented(subservice, _) => { + log::warn!( + "PUS {}({}) subservice {} not implemented", + Self::SERVICE_ID, + Self::SERVICE_STR, + subservice + ); } } HandlingStatus::HandledOne diff --git a/satrs-example/src/pus/test.rs b/satrs-example/src/pus/test.rs index 933c679..372d1c7 100644 --- a/satrs-example/src/pus/test.rs +++ b/satrs-example/src/pus/test.rs @@ -1,5 +1,5 @@ use crate::pus::create_verification_reporter; -use log::{info, warn}; +use log::info; use satrs::event_man::{EventMessage, EventMessageU32}; use satrs::pool::SharedStaticMemoryPool; use satrs::pus::test::PusService17TestHandler; @@ -10,15 +10,13 @@ use satrs::pus::{ }; use satrs::pus::{EcssTcInSharedStoreConverter, PartialPusHandlingError}; use satrs::spacepackets::ecss::tc::PusTcReader; -use satrs::spacepackets::ecss::PusPacket; -use satrs::spacepackets::time::cds::CdsTime; -use satrs::spacepackets::time::TimeWriter; +use satrs::spacepackets::ecss::{PusPacket, PusServiceId}; use satrs::tmtc::{PacketAsVec, PacketSenderWithSharedPool}; use satrs_example::config::components::PUS_TEST_SERVICE; use satrs_example::config::{tmtc_err, TEST_EVENT}; use std::sync::mpsc; -use super::HandlingStatus; +use super::{DirectPusService, HandlingStatus}; pub fn create_test_service_static( tm_sender: PacketSenderWithSharedPool, @@ -64,18 +62,36 @@ pub struct TestCustomServiceWrapper, } +impl DirectPusService + for TestCustomServiceWrapper +{ + const SERVICE_ID: u8 = PusServiceId::Test as u8; + + const SERVICE_STR: &'static str = "test"; +} + impl TestCustomServiceWrapper { - pub fn poll_and_handle_next_tc(&mut self, time_stamp: &[u8]) -> HandlingStatus { - let error_handler = |patial_error: &PartialPusHandlingError| { - log::warn!("PUS 17 partial error: {:?}", patial_error); + pub fn poll_and_handle_next_tc(&mut self, timestamp: &[u8]) -> HandlingStatus { + let error_handler = |partial_error: &PartialPusHandlingError| { + log::warn!( + "PUS {}({}) partial error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + partial_error + ); }; let res = self .handler - .poll_and_handle_next_tc(error_handler, time_stamp); - if res.is_err() { - warn!("PUS 17 handler error: {:?}", res.unwrap_err()); + .poll_and_handle_next_tc(error_handler, timestamp); + if let Err(e) = res { + log::warn!( + "PUS {}({}) error: {:?}", + Self::SERVICE_ID, + Self::SERVICE_STR, + e + ); return HandlingStatus::HandledOne; } match res.unwrap() { @@ -87,7 +103,12 @@ impl return handling_status; } DirectPusPacketHandlerResult::SubserviceNotImplemented(subservice, _) => { - warn!("PUS17: Subservice {subservice} not implemented") + log::warn!( + "PUS {}({}) subservice {} not implemented", + Self::SERVICE_ID, + Self::SERVICE_STR, + subservice + ); } DirectPusPacketHandlerResult::CustomSubservice(subservice, token) => { let (tc, _) = PusTcReader::new( @@ -97,29 +118,34 @@ impl .tc_slice_raw(), ) .unwrap(); - let time_stamper = CdsTime::now_with_u16_days().unwrap(); - let mut stamp_buf: [u8; 7] = [0; 7]; - time_stamper.write_to_bytes(&mut stamp_buf).unwrap(); if subservice == 128 { - info!("Generating test event"); + info!("generating test event"); self.test_srv_event_sender .send(EventMessage::new(PUS_TEST_SERVICE.id(), TEST_EVENT.into())) .expect("Sending test event failed"); - let start_token = self - .handler - .service_helper - .verif_reporter() - .start_success(self.handler.service_helper.tm_sender(), token, &stamp_buf) - .expect("Error sending start success"); - self.handler - .service_helper - .verif_reporter() - .completion_success( - self.handler.service_helper.tm_sender(), - start_token, - &stamp_buf, - ) - .expect("Error sending completion success"); + match self.handler.service_helper.verif_reporter().start_success( + self.handler.service_helper.tm_sender(), + token, + timestamp, + ) { + Ok(started_token) => { + if let Err(e) = self + .handler + .service_helper + .verif_reporter() + .completion_success( + self.handler.service_helper.tm_sender(), + started_token, + timestamp, + ) + { + error_handler(&PartialPusHandlingError::Verification(e)); + } + } + Err(e) => { + error_handler(&PartialPusHandlingError::Verification(e)); + } + } } else { let fail_data = [tc.subservice()]; self.handler @@ -129,7 +155,7 @@ impl self.handler.service_helper.tm_sender(), token, FailParams::new( - &stamp_buf, + timestamp, &tmtc_err::INVALID_PUS_SUBSERVICE, &fail_data, ),