sat-rs/fsrc-core/tests/pus_events.rs

166 lines
7.0 KiB
Rust
Raw Normal View History

2022-11-01 19:24:09 +01:00
use fsrc_core::event_man::{EventManager, MpscEventReceiver, MpscEventU32SendProvider};
2022-10-31 01:25:02 +01:00
use fsrc_core::events::{EventU32, EventU32TypedSev, Severity, SeverityInfo};
2022-11-02 11:33:59 +01:00
use fsrc_core::params::U32Pair;
use fsrc_core::params::{Params, ParamsHeapless, WritableToBeBytes};
use fsrc_core::pus::event_man::{DefaultPusMgmtBackendProvider, EventReporter, PusEventDispatcher};
2022-11-03 13:22:41 +01:00
use fsrc_core::pus::{EcssTmError, EcssTmSender};
2022-11-02 00:36:49 +01:00
use spacepackets::ecss::PusPacket;
2022-10-31 01:25:02 +01:00
use spacepackets::tm::PusTm;
2022-11-01 18:03:01 +01:00
use std::sync::mpsc::{channel, SendError, TryRecvError};
2022-11-01 19:24:09 +01:00
use std::thread;
2022-10-31 01:25:02 +01:00
const INFO_EVENT: EventU32TypedSev<SeverityInfo> =
EventU32TypedSev::<SeverityInfo>::const_new(1, 0);
const LOW_SEV_EVENT: EventU32 = EventU32::const_new(Severity::LOW, 1, 5);
const EMPTY_STAMP: [u8; 7] = [0; 7];
struct EventTmSender {
sender: std::sync::mpsc::Sender<Vec<u8>>,
}
impl EcssTmSender for EventTmSender {
type Error = SendError<Vec<u8>>;
fn send_tm(&mut self, tm: PusTm) -> Result<(), EcssTmError<Self::Error>> {
let mut vec = Vec::new();
tm.append_to_vec(&mut vec)?;
self.sender.send(vec).map_err(EcssTmError::SendError)?;
Ok(())
}
}
2022-10-24 14:03:21 +02:00
2022-10-23 22:32:30 +02:00
#[test]
2022-11-01 19:24:09 +01:00
fn test_threaded_usage() {
let (event_sender, event_man_receiver) = channel();
let event_receiver = MpscEventReceiver::new(event_man_receiver);
let mut event_man = EventManager::new(Box::new(event_receiver));
let (pus_event_man_tx, pus_event_man_rx) = channel();
let pus_event_man_send_provider = MpscEventU32SendProvider::new(1, pus_event_man_tx);
event_man.subscribe_all(pus_event_man_send_provider);
2022-10-31 01:25:02 +01:00
let (event_tx, event_rx) = channel();
2022-11-01 19:24:09 +01:00
let reporter = EventReporter::new(0x02, 128).expect("Creating event reporter failed");
let backend = DefaultPusMgmtBackendProvider::<EventU32>::default();
let mut pus_event_man = PusEventDispatcher::new(reporter, Box::new(backend));
2022-11-01 19:24:09 +01:00
// PUS + Generic event manager thread
let jh0 = thread::spawn(move || {
let mut sender = EventTmSender { sender: event_tx };
2022-11-01 20:07:02 +01:00
let mut event_cnt = 0;
2022-11-02 00:36:49 +01:00
let mut params_array: [u8; 128] = [0; 128];
2022-11-01 19:24:09 +01:00
loop {
let res = event_man.try_event_handling();
assert!(res.is_ok());
match pus_event_man_rx.try_recv() {
Ok((event, aux_data)) => {
2022-11-02 00:36:49 +01:00
let mut gen_event = |aux_data| {
pus_event_man.generate_pus_event_tm_generic(
&mut sender,
&EMPTY_STAMP,
event,
aux_data,
)
};
let res = if let Some(aux_data) = aux_data {
2022-11-01 19:24:09 +01:00
match aux_data {
2022-11-01 22:01:27 +01:00
Params::Heapless(heapless) => match heapless {
2022-11-02 00:36:49 +01:00
ParamsHeapless::Raw(raw) => {
raw.write_to_be_bytes(&mut params_array)
.expect("Writing raw parameter failed");
gen_event(Some(&params_array[0..raw.raw_len()]))
}
ParamsHeapless::EcssEnum(e) => {
e.write_to_be_bytes(&mut params_array)
.expect("Writing ECSS enum failed");
gen_event(Some(&params_array[0..e.raw_len()]))
}
ParamsHeapless::Store(_) => gen_event(None),
2022-11-01 22:01:27 +01:00
},
2022-11-02 00:36:49 +01:00
Params::Vec(vec) => gen_event(Some(vec.as_slice())),
Params::String(str) => gen_event(Some(str.as_bytes())),
2022-11-01 19:24:09 +01:00
}
2022-11-02 00:36:49 +01:00
} else {
gen_event(None)
};
2022-11-01 20:07:02 +01:00
event_cnt += 1;
2022-11-01 19:24:09 +01:00
assert!(res.is_ok());
assert!(res.unwrap());
2022-11-01 20:07:02 +01:00
if event_cnt == 2 {
break;
}
2022-11-01 19:24:09 +01:00
}
Err(e) => {
if let TryRecvError::Disconnected = e {
panic!("Event receiver disconnected!")
}
}
}
}
});
2022-10-31 01:25:55 +01:00
2022-11-01 19:24:09 +01:00
// Event sender and TM checker thread
let jh1 = thread::spawn(move || {
event_sender
.send((INFO_EVENT.into(), None))
.expect("Sending info event failed");
loop {
match event_rx.try_recv() {
// Event TM received successfully
2022-11-02 00:36:49 +01:00
Ok(event_tm) => {
let tm =
PusTm::from_bytes(event_tm.as_slice(), 7).expect("Deserializing TM failed");
assert_eq!(tm.0.service(), 5);
assert_eq!(tm.0.subservice(), 1);
let src_data = tm.0.source_data();
assert!(src_data.is_some());
let src_data = src_data.unwrap();
assert_eq!(src_data.len(), 4);
let event =
EventU32::from(u32::from_be_bytes(src_data[0..4].try_into().unwrap()));
assert_eq!(event, INFO_EVENT);
2022-11-01 20:07:02 +01:00
break;
}
2022-11-01 19:24:09 +01:00
Err(e) => {
if let TryRecvError::Disconnected = e {
panic!("Event sender disconnected!")
}
}
}
}
event_sender
.send((
LOW_SEV_EVENT.into(),
Some(Params::Heapless((2_u32, 3_u32).into())),
))
.expect("Sending low severity event failed");
loop {
match event_rx.try_recv() {
// Event TM received successfully
2022-11-02 00:36:49 +01:00
Ok(event_tm) => {
let tm =
PusTm::from_bytes(event_tm.as_slice(), 7).expect("Deserializing TM failed");
assert_eq!(tm.0.service(), 5);
assert_eq!(tm.0.subservice(), 2);
let src_data = tm.0.source_data();
assert!(src_data.is_some());
let src_data = src_data.unwrap();
assert_eq!(src_data.len(), 12);
let event =
EventU32::from(u32::from_be_bytes(src_data[0..4].try_into().unwrap()));
assert_eq!(event, LOW_SEV_EVENT);
let u32_pair: U32Pair =
src_data[4..].try_into().expect("Creating U32Pair failed");
assert_eq!(u32_pair.0, 2);
assert_eq!(u32_pair.1, 3);
2022-11-01 20:07:02 +01:00
break;
}
2022-11-01 19:24:09 +01:00
Err(e) => {
if let TryRecvError::Disconnected = e {
panic!("Event sender disconnected!")
}
}
}
}
});
jh0.join().expect("Joining manager thread failed");
jh1.join().expect("Joining creator thread failed");
2022-10-23 22:32:30 +02:00
}