reduce duplicate code

This commit is contained in:
Robin Müller 2022-08-14 19:02:50 +02:00
parent e227d4f4aa
commit c62554fc1c
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 28 additions and 52 deletions

View File

@ -205,6 +205,14 @@ pub(crate) mod tests {
use std::collections::VecDeque; use std::collections::VecDeque;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
pub fn generate_ping_tc(buf: &mut [u8]) -> &[u8] {
let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
let size = pus_tc.write_to(buf).expect("Error writing TC to buffer");
assert_eq!(size, 13);
&buf[0..size]
}
pub struct BasicApidHandlerSharedQueue { pub struct BasicApidHandlerSharedQueue {
pub known_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>, pub known_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>,
pub unknown_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>, pub unknown_packet_queue: Arc<Mutex<VecDeque<(u16, Vec<u8>)>>>,
@ -290,19 +298,16 @@ pub(crate) mod tests {
let error_handler = SimpleStdErrorHandler {}; let error_handler = SimpleStdErrorHandler {};
let mut ccsds_distrib = let mut ccsds_distrib =
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler)); CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
let mut test_buf: [u8; 32] = [0; 32]; let mut test_buf: [u8; 32] = [0; 32];
pus_tc let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
.write_to(test_buf.as_mut_slice())
.expect("Error writing TC to buffer"); ccsds_distrib.pass_tc(tc_slice).expect("Passing TC failed");
ccsds_distrib.pass_tc(&test_buf).expect("Passing TC failed");
let recvd = known_packet_queue.lock().unwrap().pop_front(); let recvd = known_packet_queue.lock().unwrap().pop_front();
assert!(unknown_packet_queue.lock().unwrap().is_empty()); assert!(unknown_packet_queue.lock().unwrap().is_empty());
assert!(recvd.is_some()); assert!(recvd.is_some());
let (apid, packet) = recvd.unwrap(); let (apid, packet) = recvd.unwrap();
assert_eq!(apid, 0x002); assert_eq!(apid, 0x002);
assert_eq!(packet.as_slice(), test_buf); assert_eq!(packet, tc_slice);
} }
#[test] #[test]

View File

@ -5,8 +5,9 @@
//! 2. [PusServiceProvider] trait which should be implemented by the user-provided PUS packet //! 2. [PusServiceProvider] trait which should be implemented by the user-provided PUS packet
//! handler. //! handler.
//! //!
//! The [PusDistributor] implements the [ReceivesCcsdsTc] and [ReceivesTc] trait which allows to //! The [PusDistributor] implements the [ReceivesEcssPusTc], [ReceivesCcsdsTc] and the [ReceivesTc]
//! pass raw or CCSDS packets to it. Upon receiving a packet, it performs the following steps: //! trait which allows to pass raw packets, CCSDS packets and PUS TC packets into it.
//! Upon receiving a packet, it performs the following steps:
//! //!
//! 1. It tries to identify the target Application Process Identifier (APID) based on the //! 1. It tries to identify the target Application Process Identifier (APID) based on the
//! respective CCSDS space packet header field. If that process fails, the error //! respective CCSDS space packet header field. If that process fails, the error
@ -15,12 +16,11 @@
//! [ApidPacketHandler::valid_apids], it will pass the packet to the user provided //! [ApidPacketHandler::valid_apids], it will pass the packet to the user provided
//! [ApidPacketHandler::handle_known_apid] function. If no valid APID is found, the packet //! [ApidPacketHandler::handle_known_apid] function. If no valid APID is found, the packet
//! will be passed to the [ApidPacketHandler::handle_unknown_apid] function. //! will be passed to the [ApidPacketHandler::handle_unknown_apid] function.
use crate::error::FsrcErrorHandler;
use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTc}; use crate::tmtc::{ReceivesCcsdsTc, ReceivesEcssPusTc, ReceivesTc};
use downcast_rs::Downcast; use downcast_rs::Downcast;
use spacepackets::ecss::{PusError, PusPacket}; use spacepackets::ecss::{PusError, PusPacket};
use spacepackets::tc::PusTc; use spacepackets::tc::PusTc;
use spacepackets::{PacketError, SpHeader}; use spacepackets::SpHeader;
pub trait PusServiceProvider: Downcast { pub trait PusServiceProvider: Downcast {
type Error; type Error;
@ -35,7 +35,6 @@ downcast_rs::impl_downcast!(PusServiceProvider assoc Error);
pub struct PusDistributor<E> { pub struct PusDistributor<E> {
pub service_provider: Box<dyn PusServiceProvider<Error = E>>, pub service_provider: Box<dyn PusServiceProvider<Error = E>>,
pub error_handler: Box<dyn FsrcErrorHandler>,
} }
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
@ -48,36 +47,18 @@ impl<E> ReceivesTc for PusDistributor<E> {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> { fn pass_tc(&mut self, tm_raw: &[u8]) -> Result<(), Self::Error> {
// Convert to ccsds and call pass_ccsds // Convert to ccsds and call pass_ccsds
match SpHeader::from_raw_slice(tm_raw) { let sp_header = SpHeader::from_raw_slice(tm_raw)
Ok(sp_header) => self.pass_ccsds(&sp_header, tm_raw), .map_err(|e| PusDistribError::PusError(PusError::PacketError(e)))?;
Err(error) => { self.pass_ccsds(&sp_header, tm_raw)
// TODO: Error handling
match error {
PacketError::ToBytesSliceTooSmall(_) => {
//self.error_handler.error()
}
PacketError::FromBytesSliceTooSmall(_) => {}
PacketError::ToBytesZeroCopyError => {}
PacketError::FromBytesZeroCopyError => {}
}
Err(PusDistribError::PusError(PusError::PacketError(error)))
}
}
} }
} }
impl<E> ReceivesCcsdsTc for PusDistributor<E> { impl<E> ReceivesCcsdsTc for PusDistributor<E> {
type Error = PusDistribError<E>; type Error = PusDistribError<E>;
fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), Self::Error> { fn pass_ccsds(&mut self, header: &SpHeader, tm_raw: &[u8]) -> Result<(), Self::Error> {
// TODO: Better error handling let (tc, _) =
let (tc, _) = match PusTc::new_from_raw_slice(tm_raw) { PusTc::new_from_raw_slice(tm_raw).map_err(|e| PusDistribError::PusError(e))?;
Ok(tuple) => tuple, self.pass_pus_tc(header, &tc)
Err(e) => return Err(PusDistribError::PusError(e)),
};
self.service_provider
.handle_pus_tc_packet(tc.service(), header, &tc)
.map_err(|e| PusDistribError::CustomError(e))
} }
} }
@ -105,7 +86,7 @@ mod tests {
use super::*; use super::*;
use crate::error::SimpleStdErrorHandler; use crate::error::SimpleStdErrorHandler;
use crate::tmtc::ccsds_distrib::tests::{ use crate::tmtc::ccsds_distrib::tests::{
BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue, generate_ping_tc, BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue,
}; };
use crate::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor}; use crate::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
use spacepackets::ecss::PusError; use spacepackets::ecss::PusError;
@ -229,23 +210,18 @@ mod tests {
let error_handler = SimpleStdErrorHandler {}; let error_handler = SimpleStdErrorHandler {};
let pus_distrib = PusDistributor { let pus_distrib = PusDistributor {
service_provider: Box::new(pus_handler), service_provider: Box::new(pus_handler),
error_handler: Box::new(error_handler),
}; };
let apid_handler = ApidHandlerShared { let apid_handler = ApidHandlerShared {
pus_distrib, pus_distrib,
handler_base, handler_base,
}; };
let mut ccsds_distrib = let mut ccsds_distrib =
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler)); CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
let mut test_buf: [u8; 32] = [0; 32]; let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
.write_to(test_buf.as_mut_slice())
.expect("Error writing TC to buffer"); // Pass packet to distributor
let tc_slice = &test_buf[0..size];
ccsds_distrib ccsds_distrib
.pass_tc(tc_slice) .pass_tc(tc_slice)
.expect("Passing TC slice failed"); .expect("Passing TC slice failed");
@ -270,7 +246,6 @@ mod tests {
let error_handler = SimpleStdErrorHandler {}; let error_handler = SimpleStdErrorHandler {};
let pus_distrib = PusDistributor { let pus_distrib = PusDistributor {
service_provider: Box::new(pus_handler), service_provider: Box::new(pus_handler),
error_handler: Box::new(error_handler),
}; };
let apid_handler = ApidHandlerOwned { let apid_handler = ApidHandlerOwned {
@ -280,13 +255,9 @@ mod tests {
let mut ccsds_distrib = let mut ccsds_distrib =
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler)); CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
let mut sph = SpHeader::tc(0x002, 0x34, 0).unwrap();
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
let mut test_buf: [u8; 32] = [0; 32]; let mut test_buf: [u8; 32] = [0; 32];
let size = pus_tc let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
.write_to(test_buf.as_mut_slice())
.expect("Error writing TC to buffer");
let tc_slice = &test_buf[0..size];
ccsds_distrib ccsds_distrib
.pass_tc(tc_slice) .pass_tc(tc_slice)
.expect("Passing TC slice failed"); .expect("Passing TC slice failed");