some improvements

This commit is contained in:
Robin Müller 2022-08-14 11:13:54 +02:00
parent 75bd86c802
commit 6dcb9d719c
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 32 additions and 32 deletions

View File

@ -1,8 +1,8 @@
use std::any::Any; use std::any::Any;
/// This trait encapsulates being able to cast a trait object to its original concrete type
/// TODO: Add example code and maybe write derive macro because this code is always the same
pub trait AsAny { pub trait AsAny {
// I am not 100 % sure this is the best idea, but it allows downcasting the trait object
// to its original concrete type.
fn as_any(&self) -> &dyn Any; fn as_any(&self) -> &dyn Any;
fn as_mut_any(&mut self) -> &mut dyn Any; fn as_mut_any(&mut self) -> &mut dyn Any;
} }

View File

@ -17,8 +17,14 @@ pub struct PusDistributor {
impl ReceivesTc for PusDistributor { impl ReceivesTc for PusDistributor {
fn pass_tc(&mut self, tm_raw: &[u8]) { fn pass_tc(&mut self, tm_raw: &[u8]) {
// Convert to ccsds and call pass_ccsds // Convert to ccsds and call pass_ccsds
let sp_header = SpHeader::from_raw_slice(tm_raw).unwrap(); match SpHeader::from_raw_slice(tm_raw) {
self.pass_ccsds(&sp_header, tm_raw).unwrap(); Ok(sp_header) => {
self.pass_ccsds(&sp_header, tm_raw).unwrap();
}
Err(error) => {
// TODO: Error handling
}
}
} }
} }
@ -126,21 +132,27 @@ mod tests {
} }
} }
macro_rules! apid_handler_impl {
() => {
fn valid_apids(&self) -> &'static [u16] {
&[0x000, 0x002]
}
fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_known_apid(&sp_header, tc_raw);
self.pus_distrib
.pass_ccsds(&sp_header, tc_raw)
.expect("Passing PUS packet failed");
}
fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_unknown_apid(&sp_header, tc_raw);
}
};
}
impl ApidPacketHandler for ApidHandlerShared { impl ApidPacketHandler for ApidHandlerShared {
fn valid_apids(&self) -> &'static [u16] { apid_handler_impl!();
&[0x000, 0x002]
}
fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_known_apid(&sp_header, tc_raw);
self.pus_distrib
.pass_ccsds(&sp_header, tc_raw)
.expect("Passing PUS packet failed");
}
fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_unknown_apid(&sp_header, tc_raw);
}
} }
struct ApidHandlerOwned { struct ApidHandlerOwned {
@ -159,21 +171,9 @@ mod tests {
} }
impl ApidPacketHandler for ApidHandlerOwned { impl ApidPacketHandler for ApidHandlerOwned {
fn valid_apids(&self) -> &'static [u16] { apid_handler_impl!();
&[0x000, 0x002]
}
fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_known_apid(&sp_header, tc_raw);
self.pus_distrib
.pass_ccsds(&sp_header, tc_raw)
.expect("Passing PUS packet failed");
}
fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) {
self.handler_base.handle_unknown_apid(&sp_header, tc_raw);
}
} }
#[test] #[test]
fn test_pus_distribution() { fn test_pus_distribution() {
let known_packet_queue = Arc::new(Mutex::default()); let known_packet_queue = Arc::new(Mutex::default());