From 85b9f6a002d088c244c7b6e51909480da9bc3a47 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Mon, 26 Feb 2024 10:20:21 +0100 Subject: [PATCH] introduce crossbeam module --- satrs/src/pus/mod.rs | 148 ++++++++++++++++++++++--------------------- 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/satrs/src/pus/mod.rs b/satrs/src/pus/mod.rs index 7e19a26..73213c4 100644 --- a/satrs/src/pus/mod.rs +++ b/satrs/src/pus/mod.rs @@ -379,7 +379,6 @@ pub mod std_mod { use crate::{ChannelId, TargetId}; use alloc::boxed::Box; use alloc::vec::Vec; - use crossbeam_channel as cb; use spacepackets::ecss::tc::PusTcReader; use spacepackets::ecss::tm::PusTmCreator; use spacepackets::ecss::{PusError, WritablePusPacket}; @@ -391,6 +390,9 @@ pub mod std_mod { use std::sync::mpsc::TryRecvError; use thiserror::Error; + #[cfg(feature = "crossbeam")] + pub use cb_mod::*; + use super::verification::VerificationReportingProvider; use super::{AcceptedEcssTcAndToken, TcInMemory}; @@ -400,21 +402,6 @@ pub mod std_mod { } } - impl From> for EcssTmtcError { - fn from(_: cb::SendError) -> Self { - Self::Send(GenericSendError::RxDisconnected) - } - } - - impl From> for EcssTmtcError { - fn from(value: cb::TrySendError) -> Self { - match value { - cb::TrySendError::Full(_) => Self::Send(GenericSendError::QueueFull(None)), - cb::TrySendError::Disconnected(_) => Self::Send(GenericSendError::RxDisconnected), - } - } - } - impl EcssTmSenderCore for mpsc::Sender { fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { match tm { @@ -439,19 +426,6 @@ pub mod std_mod { } } - #[cfg(feature = "crossbeam")] - impl EcssTmSenderCore for crossbeam_channel::Sender { - fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { - match tm { - PusTmWrapper::InStore(addr) => self - .try_send(addr) - .map_err(|e| EcssTmtcError::Send(e.into()))?, - PusTmWrapper::Direct(_) => return Err(EcssTmtcError::CantSendDirectTm), - }; - Ok(()) - } - } - impl EcssTmSenderCore for mpsc::Sender> { fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { match tm { @@ -476,19 +450,6 @@ pub mod std_mod { } } - #[cfg(feature = "crossbeam")] - impl EcssTmSenderCore for crossbeam_channel::Sender> { - fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { - match tm { - PusTmWrapper::InStore(addr) => return Err(EcssTmtcError::CantSendAddr(addr)), - PusTmWrapper::Direct(tm) => self - .send(tm.to_vec()?) - .map_err(|e| EcssTmtcError::Send(e.into()))?, - }; - Ok(()) - } - } - #[derive(Clone)] pub struct TmInSharedPoolSenderWithId { channel_id: ChannelId, @@ -542,9 +503,6 @@ pub mod std_mod { pub type TmInSharedPoolSenderWithMpsc = TmInSharedPoolSenderWithId>; pub type TmInSharedPoolSenderWithBoundedMpsc = TmInSharedPoolSenderWithId>; - #[cfg(feature = "crossbeam")] - pub type TmInSharedPoolSenderWithCrossbeam = - TmInSharedPoolSenderWithId>; /// This class can be used if frequent heap allocations during run-time are not an issue. /// PUS TM packets will be sent around as [Vec]s. Please note that the current implementation @@ -624,40 +582,88 @@ pub mod std_mod { } } - pub struct CrossbeamTcReceiver { - id: ChannelId, - name: &'static str, - receiver: cb::Receiver, - } + #[cfg(feature = "crossbeam")] + pub mod cb_mod { + use super::*; - impl CrossbeamTcReceiver { - pub fn new( + pub type TmInSharedPoolSenderWithCrossbeam = + TmInSharedPoolSenderWithId>; + + impl From> for EcssTmtcError { + fn from(_: cb::SendError) -> Self { + Self::Send(GenericSendError::RxDisconnected) + } + } + + impl From> for EcssTmtcError { + fn from(value: cb::TrySendError) -> Self { + match value { + cb::TrySendError::Full(_) => Self::Send(GenericSendError::QueueFull(None)), + cb::TrySendError::Disconnected(_) => { + Self::Send(GenericSendError::RxDisconnected) + } + } + } + } + + impl EcssTmSenderCore for cb::Sender { + fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { + match tm { + PusTmWrapper::InStore(addr) => self + .try_send(addr) + .map_err(|e| EcssTmtcError::Send(e.into()))?, + PusTmWrapper::Direct(_) => return Err(EcssTmtcError::CantSendDirectTm), + }; + Ok(()) + } + } + impl EcssTmSenderCore for cb::Sender> { + fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { + match tm { + PusTmWrapper::InStore(addr) => return Err(EcssTmtcError::CantSendAddr(addr)), + PusTmWrapper::Direct(tm) => self + .send(tm.to_vec()?) + .map_err(|e| EcssTmtcError::Send(e.into()))?, + }; + Ok(()) + } + } + + pub struct CrossbeamTcReceiver { id: ChannelId, name: &'static str, receiver: cb::Receiver, - ) -> Self { - Self { id, name, receiver } - } - } - - impl EcssChannel for CrossbeamTcReceiver { - fn channel_id(&self) -> ChannelId { - self.id } - fn name(&self) -> &'static str { - self.name + impl CrossbeamTcReceiver { + pub fn new( + id: ChannelId, + name: &'static str, + receiver: cb::Receiver, + ) -> Self { + Self { id, name, receiver } + } } - } - impl EcssTcReceiverCore for CrossbeamTcReceiver { - fn recv_tc(&self) -> Result { - self.receiver.try_recv().map_err(|e| match e { - cb::TryRecvError::Empty => TryRecvTmtcError::Empty, - cb::TryRecvError::Disconnected => { - TryRecvTmtcError::Tmtc(EcssTmtcError::from(GenericRecvError::TxDisconnected)) - } - }) + impl EcssChannel for CrossbeamTcReceiver { + fn channel_id(&self) -> ChannelId { + self.id + } + + fn name(&self) -> &'static str { + self.name + } + } + + impl EcssTcReceiverCore for CrossbeamTcReceiver { + fn recv_tc(&self) -> Result { + self.receiver.try_recv().map_err(|e| match e { + cb::TryRecvError::Empty => TryRecvTmtcError::Empty, + cb::TryRecvError::Disconnected => TryRecvTmtcError::Tmtc(EcssTmtcError::from( + GenericRecvError::TxDisconnected, + )), + }) + } } }