satrs_core/tmtc/
mod.rs

1//! Telemetry and Telecommanding (TMTC) module. Contains packet routing components with special
2//! support for CCSDS and ECSS packets.
3//!
4//! The distributor modules provided by this module use trait objects provided by the user to
5//! directly dispatch received packets to packet listeners based on packet fields like the CCSDS
6//! Application Process ID (APID) or the ECSS PUS service type. This allows for fast packet
7//! routing without the overhead and complication of using message queues. However, it also requires
8#[cfg(feature = "alloc")]
9use downcast_rs::{impl_downcast, Downcast};
10use spacepackets::SpHeader;
11
12#[cfg(feature = "alloc")]
13pub mod ccsds_distrib;
14#[cfg(feature = "alloc")]
15pub mod pus_distrib;
16pub mod tm_helper;
17
18#[cfg(feature = "alloc")]
19pub use ccsds_distrib::{CcsdsDistributor, CcsdsError, CcsdsPacketHandler};
20#[cfg(feature = "alloc")]
21pub use pus_distrib::{PusDistributor, PusServiceProvider};
22
23pub type TargetId = u32;
24
25/// Generic trait for object which can receive any telecommands in form of a raw bytestream, with
26/// no assumptions about the received protocol.
27///
28/// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the
29/// [crate::tmtc::ccsds_distrib::CcsdsDistributor]  which allows to pass the respective packets in
30/// raw byte format into them.
31pub trait ReceivesTcCore {
32    type Error;
33    fn pass_tc(&mut self, tc_raw: &[u8]) -> Result<(), Self::Error>;
34}
35
36/// Extension trait of [ReceivesTcCore] which allows downcasting by implementing [Downcast] and
37/// is also sendable.
38#[cfg(feature = "alloc")]
39pub trait ReceivesTc: ReceivesTcCore + Downcast + Send {
40    // Remove this once trait upcasting coercion has been implemented.
41    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
42    fn upcast(&self) -> &dyn ReceivesTcCore<Error = Self::Error>;
43    // Remove this once trait upcasting coercion has been implemented.
44    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
45    fn upcast_mut(&mut self) -> &mut dyn ReceivesTcCore<Error = Self::Error>;
46}
47
48/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature
49/// is enabled.
50#[cfg(feature = "alloc")]
51impl<T> ReceivesTc for T
52where
53    T: ReceivesTcCore + Send + 'static,
54{
55    // Remove this once trait upcasting coercion has been implemented.
56    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
57    fn upcast(&self) -> &dyn ReceivesTcCore<Error = Self::Error> {
58        self
59    }
60    // Remove this once trait upcasting coercion has been implemented.
61    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
62    fn upcast_mut(&mut self) -> &mut dyn ReceivesTcCore<Error = Self::Error> {
63        self
64    }
65}
66
67#[cfg(feature = "alloc")]
68impl_downcast!(ReceivesTc assoc Error);
69
70/// Generic trait for object which can receive CCSDS space packets, for example ECSS PUS packets
71/// for CCSDS File Delivery Protocol (CFDP) packets.
72///
73/// This trait is implemented by both the [crate::tmtc::pus_distrib::PusDistributor] and the
74/// [crate::tmtc::ccsds_distrib::CcsdsDistributor] which allows
75/// to pass the respective packets in raw byte format or in CCSDS format into them.
76pub trait ReceivesCcsdsTc {
77    type Error;
78    fn pass_ccsds(&mut self, header: &SpHeader, tc_raw: &[u8]) -> Result<(), Self::Error>;
79}
80
81/// Generic trait for a TM packet source, with no restrictions on the type of TM.
82/// Implementors write the telemetry into the provided buffer and return the size of the telemetry.
83pub trait TmPacketSourceCore {
84    type Error;
85    fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error>;
86}
87
88/// Extension trait of [TmPacketSourceCore] which allows downcasting by implementing [Downcast] and
89/// is also sendable.
90#[cfg(feature = "alloc")]
91pub trait TmPacketSource: TmPacketSourceCore + Downcast + Send {
92    // Remove this once trait upcasting coercion has been implemented.
93    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
94    fn upcast(&self) -> &dyn TmPacketSourceCore<Error = Self::Error>;
95    // Remove this once trait upcasting coercion has been implemented.
96    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
97    fn upcast_mut(&mut self) -> &mut dyn TmPacketSourceCore<Error = Self::Error>;
98}
99
100/// Blanket implementation to automatically implement [ReceivesTc] when the [alloc] feature
101/// is enabled.
102#[cfg(feature = "alloc")]
103impl<T> TmPacketSource for T
104where
105    T: TmPacketSourceCore + Send + 'static,
106{
107    // Remove this once trait upcasting coercion has been implemented.
108    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
109    fn upcast(&self) -> &dyn TmPacketSourceCore<Error = Self::Error> {
110        self
111    }
112    // Remove this once trait upcasting coercion has been implemented.
113    // Tracking issue: https://github.com/rust-lang/rust/issues/65991
114    fn upcast_mut(&mut self) -> &mut dyn TmPacketSourceCore<Error = Self::Error> {
115        self
116    }
117}