more improvements for API, tests for example event module
All checks were successful
Rust/sat-rs/pipeline/head This commit looks good

This commit is contained in:
Robin Müller 2024-04-24 18:50:08 +02:00
parent b7ce039406
commit c477739f6d
Signed by: muellerr
GPG Key ID: A649FB78196E3849
4 changed files with 87 additions and 20 deletions

View File

@ -214,16 +214,27 @@ mod tests {
use satrs::{ use satrs::{
events::EventU32, events::EventU32,
pus::verification::VerificationReporterCfg, pus::verification::VerificationReporterCfg,
spacepackets::{ecss::tm::PusTmReader, CcsdsPacket}, spacepackets::{
ecss::{tm::PusTmReader, PusPacket},
CcsdsPacket,
},
tmtc::PacketAsVec, tmtc::PacketAsVec,
}; };
use super::*; use super::*;
const TEST_CREATOR_ID: UniqueApidTargetId = UniqueApidTargetId::new(1, 2); const TEST_CREATOR_ID: UniqueApidTargetId = UniqueApidTargetId::new(1, 2);
const TEST_EVENT: EventU32 = EventU32::new(satrs::events::Severity::Info, 1, 1);
#[test] pub struct EventManagementTestbench {
fn test_basic() { pub event_tx: mpsc::SyncSender<EventMessageU32>,
pub event_manager: EventManagerWithBoundedMpsc,
pub tm_receiver: mpsc::Receiver<PacketAsVec>,
pub pus_event_handler: PusEventHandler<mpsc::Sender<PacketAsVec>>,
}
impl EventManagementTestbench {
pub fn new() -> Self {
let (event_tx, event_rx) = mpsc::sync_channel(10); let (event_tx, event_rx) = mpsc::sync_channel(10);
let (_event_req_tx, event_req_rx) = mpsc::sync_channel(10); let (_event_req_tx, event_req_rx) = mpsc::sync_channel(10);
let (tm_sender, tm_receiver) = mpsc::channel(); let (tm_sender, tm_receiver) = mpsc::channel();
@ -231,26 +242,50 @@ mod tests {
let verif_reporter = let verif_reporter =
VerificationReporter::new(PUS_EVENT_MANAGEMENT.id(), &verif_reporter_cfg); VerificationReporter::new(PUS_EVENT_MANAGEMENT.id(), &verif_reporter_cfg);
let mut event_manager = EventManagerWithBoundedMpsc::new(event_rx); let mut event_manager = EventManagerWithBoundedMpsc::new(event_rx);
let mut pus_event_handler = PusEventHandler::<mpsc::Sender<PacketAsVec>>::new( let pus_event_handler = PusEventHandler::<mpsc::Sender<PacketAsVec>>::new(
tm_sender, tm_sender,
verif_reporter, verif_reporter,
&mut event_manager, &mut event_manager,
event_req_rx, event_req_rx,
); );
event_tx Self {
event_tx,
tm_receiver,
event_manager,
pus_event_handler,
}
}
}
#[test]
fn test_basic_event_generation() {
let mut testbench = EventManagementTestbench::new();
testbench
.event_tx
.send(EventMessageU32::new( .send(EventMessageU32::new(
TEST_CREATOR_ID.id(), TEST_CREATOR_ID.id(),
EventU32::new(satrs::events::Severity::Info, 1, 1), EventU32::new(satrs::events::Severity::Info, 1, 1),
)) ))
.expect("failed to send event"); .expect("failed to send event");
pus_event_handler.handle_event_requests(); testbench.pus_event_handler.handle_event_requests();
event_manager.try_event_handling(|_, _| {}); testbench.event_manager.try_event_handling(|_, _| {});
pus_event_handler.generate_pus_event_tm(); testbench.pus_event_handler.generate_pus_event_tm();
let tm_packet = tm_receiver.try_recv().expect("failed to receive TM packet"); let tm_packet = testbench
.tm_receiver
.try_recv()
.expect("failed to receive TM packet");
assert_eq!(tm_packet.sender_id, PUS_EVENT_MANAGEMENT.id()); assert_eq!(tm_packet.sender_id, PUS_EVENT_MANAGEMENT.id());
let tm_reader = PusTmReader::new(&tm_packet.packet, 7) let tm_reader = PusTmReader::new(&tm_packet.packet, 7)
.expect("failed to create TM reader") .expect("failed to create TM reader")
.0; .0;
assert_eq!(tm_reader.apid(), TEST_CREATOR_ID.apid); assert_eq!(tm_reader.apid(), TEST_CREATOR_ID.apid);
assert_eq!(tm_reader.user_data().len(), 4);
let event_read_back = EventU32::from_be_bytes(tm_reader.user_data().try_into().unwrap());
assert_eq!(event_read_back, TEST_EVENT);
}
#[test]
fn test_basic_event_disabled() {
// TODO: Add test.
} }
} }

View File

@ -17,12 +17,17 @@ version = "1"
default-features = false default-features = false
optional = true optional = true
[dependencies.defmt]
version = "0.3"
optional = true
[dependencies.spacepackets] [dependencies.spacepackets]
version = ">0.9, <=0.11" version = ">0.9, <=0.11"
default-features = false default-features = false
[features] [features]
serde = ["dep:serde", "spacepackets/serde"] serde = ["dep:serde", "spacepackets/serde"]
spacepackets = ["dep:defmt", "spacepackets/defmt"]
[package.metadata.docs.rs] [package.metadata.docs.rs]
rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"] rustdoc-args = ["--cfg", "doc_cfg", "--generate-link-to-definition"]

View File

@ -7,6 +7,7 @@ use spacepackets::ByteConversionError;
/// Simple [u16] based result code type which also allows to group related resultcodes. /// Simple [u16] based result code type which also allows to group related resultcodes.
#[derive(Debug, Copy, Clone, PartialEq, Eq)] #[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct ResultU16 { pub struct ResultU16 {
group_id: u8, group_id: u8,
unique_id: u8, unique_id: u8,
@ -19,15 +20,28 @@ impl ResultU16 {
unique_id, unique_id,
} }
} }
pub fn raw(&self) -> u16 { pub fn raw(&self) -> u16 {
((self.group_id as u16) << 8) | self.unique_id as u16 ((self.group_id as u16) << 8) | self.unique_id as u16
} }
pub fn group_id(&self) -> u8 { pub fn group_id(&self) -> u8 {
self.group_id self.group_id
} }
pub fn unique_id(&self) -> u8 { pub fn unique_id(&self) -> u8 {
self.unique_id self.unique_id
} }
pub fn from_be_bytes(bytes: [u8; 2]) -> Self {
Self::from(u16::from_be_bytes(bytes))
}
}
impl From<u16> for ResultU16 {
fn from(value: u16) -> Self {
Self::new(((value >> 8) & 0xff) as u8, (value & 0xff) as u8)
}
} }
impl From<ResultU16> for EcssEnumU16 { impl From<ResultU16> for EcssEnumU16 {
@ -84,5 +98,14 @@ mod tests {
assert_eq!(written, 2); assert_eq!(written, 2);
assert_eq!(buf[0], 1); assert_eq!(buf[0], 1);
assert_eq!(buf[1], 1); assert_eq!(buf[1], 1);
let read_back = ResultU16::from_be_bytes(buf);
assert_eq!(read_back, result_code);
}
#[test]
fn test_from_u16() {
let result_code = ResultU16::new(1, 1);
let result_code_2 = ResultU16::from(result_code.raw());
assert_eq!(result_code, result_code_2);
} }
} }

View File

@ -794,6 +794,8 @@ mod tests {
assert!(HIGH_SEV_EVENT.write_to_be_bytes(&mut buf).is_ok()); assert!(HIGH_SEV_EVENT.write_to_be_bytes(&mut buf).is_ok());
let val_from_raw = u32::from_be_bytes(buf); let val_from_raw = u32::from_be_bytes(buf);
assert_eq!(val_from_raw, 0xFFFFFFFF); assert_eq!(val_from_raw, 0xFFFFFFFF);
let event_read_back = EventU32::from_be_bytes(buf);
assert_eq!(event_read_back, HIGH_SEV_EVENT);
} }
#[test] #[test]
@ -802,6 +804,8 @@ mod tests {
assert!(HIGH_SEV_EVENT_SMALL.write_to_be_bytes(&mut buf).is_ok()); assert!(HIGH_SEV_EVENT_SMALL.write_to_be_bytes(&mut buf).is_ok());
let val_from_raw = u16::from_be_bytes(buf); let val_from_raw = u16::from_be_bytes(buf);
assert_eq!(val_from_raw, 0xFFFF); assert_eq!(val_from_raw, 0xFFFF);
let event_read_back = EventU16::from_be_bytes(buf);
assert_eq!(event_read_back, HIGH_SEV_EVENT_SMALL);
} }
#[test] #[test]