Implementation of scheduler in pus and tmtc handler #29

Merged
muellerr merged 13 commits from pus_schedule_implementation into main 2023-02-01 13:40:49 +01:00
3 changed files with 73 additions and 67 deletions
Showing only changes of commit a7aad003ab - Show all commits

View File

@ -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::<spacepackets::time::cds::TimeProvider>(&tc, &mut pool)
.unwrap();
match scheduler.insert_wrapped_tc::<spacepackets::time::cds::TimeProvider>(&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::<spacepackets::time::cds::TimeProvider>(&tc, &mut pool)
.unwrap();
match scheduler.insert_wrapped_tc::<spacepackets::time::cds::TimeProvider>(&tc, &mut pool) {
Ok(_) => {
panic!();
}
Err(e) => {
if e != ScheduleError::WrongSubservice {
panic!();
}
}
}
}
}

View File

@ -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<u32, Sender<RequestWithToken>>,
stamper: TimeProvider,
time_stamp: [u8; 7],
scheduler: Arc<Mutex<PusScheduler>>,
scheduler: Rc<RefCell<PusScheduler>>,
}
impl PusReceiver {
@ -48,7 +49,7 @@ impl PusReceiver {
tc_source: PusTcSource,
event_request_tx: Sender<EventRequestWithToken>,
request_map: HashMap<u32, Sender<RequestWithToken>>,
scheduler: Arc<Mutex<PusScheduler>>,
scheduler: Rc<RefCell<PusScheduler>>,
) -> Self {
Self {
tm_helper: PusTmWithCdsShortHelper::new(apid),
@ -286,28 +287,16 @@ impl PusReceiver {
}
fn handle_scheduled_tc(&mut self, pus_tc: &PusTc, token: VerificationToken<TcStateAccepted>) {
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::<spacepackets::time::cds::TimeProvider>(
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::<spacepackets::time::cds::TimeProvider>(
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());

View File

@ -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<StoreAddr>,
pus_receiver: &mut PusReceiver,
scheduler: Arc<Mutex<PusScheduler>>,
scheduler: Rc<RefCell<PusScheduler>>,
) {
let releaser = |enabled: bool, addr: &StoreAddr| {
tc_source.tc_source.send(*addr);
true
let releaser = |enabled: bool, addr: &StoreAddr| -> bool {
lkoester marked this conversation as resolved Outdated

unused code can be deleted

unused code can be deleted
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() {