From a7aad003ab5967b3f90b248a72018356d823d1da Mon Sep 17 00:00:00 2001 From: lkoester Date: Fri, 27 Jan 2023 14:02:46 +0100 Subject: [PATCH] scheduler implemented in tmtc task --- satrs-core/src/pus/scheduling.rs | 28 ++++++++--- satrs-example/src/pus.rs | 85 +++++++++++++++----------------- satrs-example/src/tmtc.rs | 27 +++++----- 3 files changed, 73 insertions(+), 67 deletions(-) diff --git a/satrs-core/src/pus/scheduling.rs b/satrs-core/src/pus/scheduling.rs index 171a2ec..a5a2083 100644 --- a/satrs-core/src/pus/scheduling.rs +++ b/satrs-core/src/pus/scheduling.rs @@ -709,7 +709,6 @@ mod tests { } #[test] - #[should_panic] fn insert_wrong_service() { let mut scheduler = PusScheduler::new(UnixTimestamp::new_only_seconds(0), Duration::from_secs(5)); @@ -719,13 +718,19 @@ mod tests { let mut buf: [u8; 32] = [0; 32]; let tc = wrong_tc_service(UnixTimestamp::new_only_seconds(100), &mut buf); - let addr = scheduler - .insert_wrapped_tc::(&tc, &mut pool) - .unwrap(); + match scheduler.insert_wrapped_tc::(&tc, &mut pool) { + Ok(_) => { + panic!(); + } + Err(e) => { + if e != ScheduleError::WrongService { + panic!(); + } + } + } } #[test] - #[should_panic] fn insert_wrong_subservice() { let mut scheduler = PusScheduler::new(UnixTimestamp::new_only_seconds(0), Duration::from_secs(5)); @@ -735,8 +740,15 @@ mod tests { let mut buf: [u8; 32] = [0; 32]; let tc = wrong_tc_subservice(UnixTimestamp::new_only_seconds(100), &mut buf); - let addr = scheduler - .insert_wrapped_tc::(&tc, &mut pool) - .unwrap(); + match scheduler.insert_wrapped_tc::(&tc, &mut pool) { + Ok(_) => { + panic!(); + } + Err(e) => { + if e != ScheduleError::WrongSubservice { + panic!(); + } + } + } } } diff --git a/satrs-example/src/pus.rs b/satrs-example/src/pus.rs index c7a8181..d33be86 100644 --- a/satrs-example/src/pus.rs +++ b/satrs-example/src/pus.rs @@ -19,6 +19,7 @@ use satrs_core::{ spacepackets::time::cds::TimeProvider, spacepackets::time::TimeWriter, spacepackets::SpHeader, }; use satrs_example::{hk_err, tmtc_err}; +use std::cell::RefCell; use std::collections::HashMap; use std::rc::Rc; use std::sync::mpsc::Sender; @@ -36,7 +37,7 @@ pub struct PusReceiver { request_map: HashMap>, stamper: TimeProvider, time_stamp: [u8; 7], - scheduler: Arc>, + scheduler: Rc>, } impl PusReceiver { @@ -48,7 +49,7 @@ impl PusReceiver { tc_source: PusTcSource, event_request_tx: Sender, request_map: HashMap>, - scheduler: Arc>, + scheduler: Rc>, ) -> Self { Self { tm_helper: PusTmWithCdsShortHelper::new(apid), @@ -286,28 +287,16 @@ impl PusReceiver { } fn handle_scheduled_tc(&mut self, pus_tc: &PusTc, token: VerificationToken) { - if pus_tc.user_data().is_none() { - self.update_time_stamp(); - self.verif_reporter - .start_failure( - token, - FailParams::new(Some(&self.time_stamp), &tmtc_err::NOT_ENOUGH_APP_DATA, None), - ) - .expect("Sending start failure TM failed"); - return; - } - self.update_time_stamp(); match pus_tc.subservice() { 1 => { - let mut scheduler = self.scheduler.lock().expect("Lock of scheduler failed"); - let start_token = self .verif_reporter .start_success(token, Some(&self.time_stamp)) .expect("Error sending start success"); + let mut scheduler = self.scheduler.borrow_mut(); scheduler.enable(); if scheduler.is_enabled() { self.verif_reporter @@ -318,11 +307,12 @@ impl PusReceiver { } } 2 => { - let mut scheduler = self.scheduler.lock().expect("Lock of scheduler failed"); let start_token = self .verif_reporter .start_success(token, Some(&self.time_stamp)) .expect("Error sending start success"); + + let mut scheduler = self.scheduler.borrow_mut(); scheduler.disable(); if !scheduler.is_enabled() { self.verif_reporter @@ -333,45 +323,50 @@ impl PusReceiver { } } 3 => { - let mut scheduler = self.scheduler.lock().expect("Lock of scheduler failed"); - let start_token = self .verif_reporter .start_success(token, Some(&self.time_stamp)) .expect("Error sending start success"); - match self.tc_source.tc_store.pool.write() { - Ok(mut pool) => { - match scheduler.reset(pool.as_mut()) { - Ok(_) => { - self.verif_reporter - .completion_success(start_token, Some(&self.time_stamp)) - .expect("Error sending completion success"); - } - Err(_) => { - // TODO - } - } - } - Err(_) => {} - } + + let mut scheduler = self.scheduler.borrow_mut(); + let mut pool = self + .tc_source + .tc_store + .pool + .write() + .expect("Locking pool failed"); + + scheduler + .reset(pool.as_mut()) + .expect("Error resetting TC Pool"); + + self.verif_reporter + .completion_success(start_token, Some(&self.time_stamp)) + .expect("Error sending completion success"); } 4 => { - let mut scheduler = self.scheduler.lock().expect("Lock of scheduler failed"); let start_token = self .verif_reporter .start_success(token, Some(&self.time_stamp)) .expect("Error sending start success"); - match self.tc_source.tc_store.pool.write() { - Ok(mut pool) => { - scheduler - .insert_wrapped_tc::( - pus_tc, - pool.as_mut(), - ) - .expect("TODO: panic message"); - } - Err(_) => {} - } + + let mut scheduler = self.scheduler.borrow_mut(); + let mut pool = self + .tc_source + .tc_store + .pool + .write() + .expect("Locking pool failed"); + scheduler + .insert_wrapped_tc::( + pus_tc, + pool.as_mut(), + ) + .expect("TODO: panic message"); + + self.verif_reporter + .completion_success(start_token, Some(&self.time_stamp)) + .expect("Error sending completion success"); //let addr = self.tc_source.tc_store.add_pus_tc().unwrap(); //let unix_time = UnixTimestamp::new_only_seconds(self.stamper.unix_seconds()); diff --git a/satrs-example/src/tmtc.rs b/satrs-example/src/tmtc.rs index 0cf8a43..00cd4f2 100644 --- a/satrs-example/src/tmtc.rs +++ b/satrs-example/src/tmtc.rs @@ -1,6 +1,7 @@ use satrs_core::events::EventU32; use satrs_core::hal::host::udp_server::{ReceiveResult, UdpTcServer}; use satrs_core::params::Params; +use std::cell::RefCell; use std::collections::HashMap; use std::error::Error; use std::fmt::{Display, Formatter}; @@ -161,7 +162,7 @@ impl ReceivesCcsdsTc for PusTcSource { } } pub fn core_tmtc_task(args: OtherArgs, mut tc_args: TcArgs, tm_args: TmArgs) { - let mut scheduler = Arc::new(Mutex::new( + let mut scheduler = Rc::new(RefCell::new( PusScheduler::new_with_current_init_time(Duration::from_secs(5)).unwrap(), )); @@ -192,7 +193,6 @@ pub fn core_tmtc_task(args: OtherArgs, mut tc_args: TcArgs, tm_args: TmArgs) { tm_store: tm_args.tm_store.pool.clone(), }; - let mut test_closure = |boolvar: bool, store_addr: &StoreAddr| true; let (mut tc_source, mut tc_receiver) = tc_args.split(); loop { @@ -213,21 +213,20 @@ fn core_tmtc_loop( tc_source: &mut PusTcSource, tc_receiver: &mut Receiver, pus_receiver: &mut PusReceiver, - scheduler: Arc>, + scheduler: Rc>, ) { - let releaser = |enabled: bool, addr: &StoreAddr| { - tc_source.tc_source.send(*addr); - true + let releaser = |enabled: bool, addr: &StoreAddr| -> bool { + match tc_source.tc_source.send(*addr) { + Ok(_) => true, + Err(_) => false, + } }; - let mut scheduler = scheduler.lock().expect("Lock of scheduler failed"); - match tc_source.tc_store.pool.write() { - Ok(mut pool) => match scheduler.release_telecommands(releaser, pool.as_mut()) { - Ok(_) => {} - Err(_) => {} - }, - Err(_) => {} - } + let mut scheduler = scheduler.borrow_mut(); + let mut pool = tc_source.tc_store.pool.write().expect("error locking pool"); + scheduler + .release_telecommands(releaser, pool.as_mut()) + .expect("error releasing tc"); while poll_tc_server(udp_tmtc_server) {} match tc_receiver.try_recv() {