remove dependency on error handler
This commit is contained in:
parent
287f67a477
commit
8418085fb1
@ -18,14 +18,19 @@
|
|||||||
//! # Example
|
//! # Example
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use fsrc_core::error::SimpleStdErrorHandler;
|
|
||||||
//! use fsrc_core::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
|
//! use fsrc_core::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
|
||||||
//! use fsrc_core::tmtc::ReceivesTc;
|
//! use fsrc_core::tmtc::ReceivesTc;
|
||||||
//! use spacepackets::{CcsdsPacket, SpHeader};
|
//! use spacepackets::{CcsdsPacket, SpHeader};
|
||||||
//! use spacepackets::tc::PusTc;
|
//! use spacepackets::tc::PusTc;
|
||||||
//!
|
//!
|
||||||
|
//! #[derive (Default)]
|
||||||
//! struct ConcreteApidHandler {
|
//! struct ConcreteApidHandler {
|
||||||
//! custom_field: u32
|
//! known_call_count: u32,
|
||||||
|
//! unknown_call_count: u32
|
||||||
|
//! }
|
||||||
|
//!
|
||||||
|
//! impl ConcreteApidHandler {
|
||||||
|
//! fn mutable_foo(&mut self) {}
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! impl ApidPacketHandler for ConcreteApidHandler {
|
//! impl ApidPacketHandler for ConcreteApidHandler {
|
||||||
@ -34,20 +39,19 @@
|
|||||||
//! fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
//! fn handle_known_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
||||||
//! assert_eq!(sp_header.apid(), 0x002);
|
//! assert_eq!(sp_header.apid(), 0x002);
|
||||||
//! assert_eq!(tc_raw.len(), 13);
|
//! assert_eq!(tc_raw.len(), 13);
|
||||||
|
//! self.known_call_count += 1;
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
//! fn handle_unknown_apid(&mut self, sp_header: &SpHeader, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
||||||
//! assert_eq!(sp_header.apid(), 0x003);
|
//! assert_eq!(sp_header.apid(), 0x003);
|
||||||
//! assert_eq!(tc_raw.len(), 13);
|
//! assert_eq!(tc_raw.len(), 13);
|
||||||
|
//! self.unknown_call_count += 1;
|
||||||
//! Ok(())
|
//! Ok(())
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! let apid_handler = ConcreteApidHandler {
|
//! let apid_handler = ConcreteApidHandler::default();
|
||||||
//! custom_field: 0x42
|
//! let mut ccsds_distributor = CcsdsDistributor::new(Box::new(apid_handler));
|
||||||
//! };
|
|
||||||
//! let err_handler = SimpleStdErrorHandler::default();
|
|
||||||
//! let mut ccsds_distributor = CcsdsDistributor::new(Box::new(apid_handler), Box::new(err_handler));
|
|
||||||
//!
|
//!
|
||||||
//! // Create and pass PUS telecommand with a valid APID
|
//! // Create and pass PUS telecommand with a valid APID
|
||||||
//! let mut space_packet_header = SpHeader::tc(0x002, 0x34, 0).unwrap();
|
//! let mut space_packet_header = SpHeader::tc(0x002, 0x34, 0).unwrap();
|
||||||
@ -71,12 +75,16 @@
|
|||||||
//! let concrete_handler_ref: &ConcreteApidHandler = ccsds_distributor
|
//! let concrete_handler_ref: &ConcreteApidHandler = ccsds_distributor
|
||||||
//! .apid_handler_ref()
|
//! .apid_handler_ref()
|
||||||
//! .expect("Casting back to concrete type failed");
|
//! .expect("Casting back to concrete type failed");
|
||||||
//! assert_eq!(concrete_handler_ref.custom_field, 0x42);
|
//! assert_eq!(concrete_handler_ref.known_call_count, 1);
|
||||||
|
//! assert_eq!(concrete_handler_ref.unknown_call_count, 1);
|
||||||
|
//!
|
||||||
|
//! // It's also possible to retrieve a mutable reference
|
||||||
|
//! let mutable_ref: &mut ConcreteApidHandler = ccsds_distributor
|
||||||
|
//! .apid_handler_mut()
|
||||||
|
//! .expect("Casting back to concrete type failed");
|
||||||
|
//! mutable_ref.mutable_foo();
|
||||||
//! ```
|
//! ```
|
||||||
use crate::error::FsrcErrorHandler;
|
use crate::tmtc::{ReceivesCcsdsTc, ReceivesTc};
|
||||||
use crate::tmtc::{
|
|
||||||
ReceivesCcsdsTc, ReceivesTc, FROM_BYTES_SLICE_TOO_SMALL_ERROR, FROM_BYTES_ZEROCOPY_ERROR,
|
|
||||||
};
|
|
||||||
use downcast_rs::Downcast;
|
use downcast_rs::Downcast;
|
||||||
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
|
use spacepackets::{CcsdsPacket, PacketError, SpHeader};
|
||||||
|
|
||||||
@ -111,7 +119,6 @@ pub struct CcsdsDistributor<E> {
|
|||||||
/// It can be cast back to the original concrete type using the [Self::apid_handler_ref] or
|
/// It can be cast back to the original concrete type using the [Self::apid_handler_ref] or
|
||||||
/// the [Self::apid_handler_mut] method.
|
/// the [Self::apid_handler_mut] method.
|
||||||
pub apid_handler: Box<dyn ApidPacketHandler<Error = E>>,
|
pub apid_handler: Box<dyn ApidPacketHandler<Error = E>>,
|
||||||
pub error_handler: Box<dyn FsrcErrorHandler>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone, PartialEq)]
|
#[derive(Debug, Copy, Clone, PartialEq)]
|
||||||
@ -132,48 +139,25 @@ impl<E: 'static> ReceivesTc for CcsdsDistributor<E> {
|
|||||||
type Error = CcsdsError<E>;
|
type Error = CcsdsError<E>;
|
||||||
|
|
||||||
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error> {
|
||||||
let sp_header = match SpHeader::from_raw_slice(tc_raw) {
|
let sp_header = SpHeader::from_raw_slice(tc_raw).map_err(|e| CcsdsError::PacketError(e))?;
|
||||||
Ok(header) => header,
|
|
||||||
Err(e) => {
|
|
||||||
match e {
|
|
||||||
PacketError::FromBytesSliceTooSmall(missmatch) => {
|
|
||||||
self.error_handler.error_with_two_params(
|
|
||||||
FROM_BYTES_SLICE_TOO_SMALL_ERROR,
|
|
||||||
missmatch.found as u32,
|
|
||||||
missmatch.expected as u32,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
PacketError::FromBytesZeroCopyError => {
|
|
||||||
self.error_handler.error(FROM_BYTES_ZEROCOPY_ERROR);
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
// TODO: Unexpected error
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Err(CcsdsError::PacketError(e));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
self.dispatch_ccsds(&sp_header, tc_raw)
|
self.dispatch_ccsds(&sp_header, tc_raw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: 'static> CcsdsDistributor<E> {
|
impl<E: 'static> CcsdsDistributor<E> {
|
||||||
pub fn new(
|
pub fn new(apid_handler: Box<dyn ApidPacketHandler<Error = E>>) -> Self {
|
||||||
apid_handler: Box<dyn ApidPacketHandler<Error = E>>,
|
CcsdsDistributor { apid_handler }
|
||||||
error_handler: Box<dyn FsrcErrorHandler>,
|
|
||||||
) -> Self {
|
|
||||||
CcsdsDistributor {
|
|
||||||
apid_handler,
|
|
||||||
error_handler,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// This function can be used to retrieve the concrete instance of the APID handler
|
/// This function can be used to retrieve a reference to the concrete instance of the APID
|
||||||
/// after it was passed to the distributor.
|
/// handler after it was passed to the distributor. See the
|
||||||
|
/// [module documentation][crate::tmtc::ccsds_distrib] for an example.
|
||||||
pub fn apid_handler_ref<T: ApidPacketHandler<Error = E>>(&self) -> Option<&T> {
|
pub fn apid_handler_ref<T: ApidPacketHandler<Error = E>>(&self) -> Option<&T> {
|
||||||
self.apid_handler.downcast_ref::<T>()
|
self.apid_handler.downcast_ref::<T>()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// This function can be used to retrieve a mutable reference to the concrete instance of the
|
||||||
|
/// APID handler after it was passed to the distributor.
|
||||||
pub fn apid_handler_mut<T: ApidPacketHandler<Error = E>>(&mut self) -> Option<&mut T> {
|
pub fn apid_handler_mut<T: ApidPacketHandler<Error = E>>(&mut self) -> Option<&mut T> {
|
||||||
self.apid_handler.downcast_mut::<T>()
|
self.apid_handler.downcast_mut::<T>()
|
||||||
}
|
}
|
||||||
@ -198,7 +182,6 @@ impl<E: 'static> CcsdsDistributor<E> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub(crate) mod tests {
|
pub(crate) mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::error::SimpleStdErrorHandler;
|
|
||||||
use crate::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
|
use crate::tmtc::ccsds_distrib::{ApidPacketHandler, CcsdsDistributor};
|
||||||
use spacepackets::tc::PusTc;
|
use spacepackets::tc::PusTc;
|
||||||
use spacepackets::CcsdsPacket;
|
use spacepackets::CcsdsPacket;
|
||||||
@ -295,9 +278,7 @@ pub(crate) mod tests {
|
|||||||
known_packet_queue: known_packet_queue.clone(),
|
known_packet_queue: known_packet_queue.clone(),
|
||||||
unknown_packet_queue: unknown_packet_queue.clone(),
|
unknown_packet_queue: unknown_packet_queue.clone(),
|
||||||
};
|
};
|
||||||
let error_handler = SimpleStdErrorHandler {};
|
let mut ccsds_distrib = CcsdsDistributor::new(Box::new(apid_handler));
|
||||||
let mut ccsds_distrib =
|
|
||||||
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
|
|
||||||
let mut test_buf: [u8; 32] = [0; 32];
|
let mut test_buf: [u8; 32] = [0; 32];
|
||||||
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
||||||
|
|
||||||
@ -318,9 +299,7 @@ pub(crate) mod tests {
|
|||||||
known_packet_queue: known_packet_queue.clone(),
|
known_packet_queue: known_packet_queue.clone(),
|
||||||
unknown_packet_queue: unknown_packet_queue.clone(),
|
unknown_packet_queue: unknown_packet_queue.clone(),
|
||||||
};
|
};
|
||||||
let error_handler = SimpleStdErrorHandler {};
|
let mut ccsds_distrib = CcsdsDistributor::new(Box::new(apid_handler));
|
||||||
let mut ccsds_distrib =
|
|
||||||
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
|
|
||||||
let mut sph = SpHeader::tc(0x004, 0x34, 0).unwrap();
|
let mut sph = SpHeader::tc(0x004, 0x34, 0).unwrap();
|
||||||
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
|
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];
|
||||||
|
@ -7,22 +7,22 @@ use spacepackets::SpHeader;
|
|||||||
pub mod ccsds_distrib;
|
pub mod ccsds_distrib;
|
||||||
pub mod pus_distrib;
|
pub mod pus_distrib;
|
||||||
|
|
||||||
const RAW_PACKET_ERROR: &str = "raw-tmtc";
|
const _RAW_PACKET_ERROR: &str = "raw-tmtc";
|
||||||
const _CCSDS_ERROR: &str = "ccsds-tmtc";
|
const _CCSDS_ERROR: &str = "ccsds-tmtc";
|
||||||
const _PUS_ERROR: &str = "pus-tmtc";
|
const _PUS_ERROR: &str = "pus-tmtc";
|
||||||
|
|
||||||
// TODO: A macro for general and unknown errors would be nice
|
// TODO: A macro for general and unknown errors would be nice
|
||||||
const FROM_BYTES_SLICE_TOO_SMALL_ERROR: FsrcErrorRaw = FsrcErrorRaw::new(
|
const _FROM_BYTES_SLICE_TOO_SMALL_ERROR: FsrcErrorRaw = FsrcErrorRaw::new(
|
||||||
FsrcGroupIds::Tmtc as u8,
|
FsrcGroupIds::Tmtc as u8,
|
||||||
0,
|
0,
|
||||||
RAW_PACKET_ERROR,
|
_RAW_PACKET_ERROR,
|
||||||
"FROM_BYTES_SLICE_TOO_SMALL_ERROR",
|
"FROM_BYTES_SLICE_TOO_SMALL_ERROR",
|
||||||
);
|
);
|
||||||
|
|
||||||
const FROM_BYTES_ZEROCOPY_ERROR: FsrcErrorRaw = FsrcErrorRaw::new(
|
const _FROM_BYTES_ZEROCOPY_ERROR: FsrcErrorRaw = FsrcErrorRaw::new(
|
||||||
FsrcGroupIds::Tmtc as u8,
|
FsrcGroupIds::Tmtc as u8,
|
||||||
1,
|
1,
|
||||||
RAW_PACKET_ERROR,
|
_RAW_PACKET_ERROR,
|
||||||
"FROM_BYTES_ZEROCOPY_ERROR",
|
"FROM_BYTES_ZEROCOPY_ERROR",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -134,7 +134,6 @@ impl<E: 'static> PusDistributor<E> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::error::SimpleStdErrorHandler;
|
|
||||||
use crate::tmtc::ccsds_distrib::tests::{
|
use crate::tmtc::ccsds_distrib::tests::{
|
||||||
generate_ping_tc, BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue,
|
generate_ping_tc, BasicApidHandlerOwnedQueue, BasicApidHandlerSharedQueue,
|
||||||
};
|
};
|
||||||
@ -257,7 +256,6 @@ mod tests {
|
|||||||
unknown_packet_queue: unknown_packet_queue.clone(),
|
unknown_packet_queue: unknown_packet_queue.clone(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let error_handler = SimpleStdErrorHandler {};
|
|
||||||
let pus_distrib = PusDistributor {
|
let pus_distrib = PusDistributor {
|
||||||
service_provider: Box::new(pus_handler),
|
service_provider: Box::new(pus_handler),
|
||||||
};
|
};
|
||||||
@ -266,8 +264,7 @@ mod tests {
|
|||||||
pus_distrib,
|
pus_distrib,
|
||||||
handler_base,
|
handler_base,
|
||||||
};
|
};
|
||||||
let mut ccsds_distrib =
|
let mut ccsds_distrib = CcsdsDistributor::new(Box::new(apid_handler));
|
||||||
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
|
|
||||||
let mut test_buf: [u8; 32] = [0; 32];
|
let mut test_buf: [u8; 32] = [0; 32];
|
||||||
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
||||||
|
|
||||||
@ -293,7 +290,6 @@ mod tests {
|
|||||||
fn test_as_any_cast() {
|
fn test_as_any_cast() {
|
||||||
let pus_handler = PusHandlerOwnedQueue::default();
|
let pus_handler = PusHandlerOwnedQueue::default();
|
||||||
let handler_base = BasicApidHandlerOwnedQueue::default();
|
let handler_base = BasicApidHandlerOwnedQueue::default();
|
||||||
let error_handler = SimpleStdErrorHandler {};
|
|
||||||
let pus_distrib = PusDistributor {
|
let pus_distrib = PusDistributor {
|
||||||
service_provider: Box::new(pus_handler),
|
service_provider: Box::new(pus_handler),
|
||||||
};
|
};
|
||||||
@ -302,8 +298,7 @@ mod tests {
|
|||||||
pus_distrib,
|
pus_distrib,
|
||||||
handler_base,
|
handler_base,
|
||||||
};
|
};
|
||||||
let mut ccsds_distrib =
|
let mut ccsds_distrib = CcsdsDistributor::new(Box::new(apid_handler));
|
||||||
CcsdsDistributor::new(Box::new(apid_handler), Box::new(error_handler));
|
|
||||||
|
|
||||||
let mut test_buf: [u8; 32] = [0; 32];
|
let mut test_buf: [u8; 32] = [0; 32];
|
||||||
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
let tc_slice = generate_ping_tc(test_buf.as_mut_slice());
|
||||||
|
Loading…
Reference in New Issue
Block a user