introduce crossbeam module

This commit is contained in:
Robin Müller 2024-02-26 10:20:21 +01:00
parent 75c687feed
commit 85b9f6a002
Signed by: muellerr
GPG Key ID: A649FB78196E3849

View File

@ -379,7 +379,6 @@ pub mod std_mod {
use crate::{ChannelId, TargetId}; use crate::{ChannelId, TargetId};
use alloc::boxed::Box; use alloc::boxed::Box;
use alloc::vec::Vec; use alloc::vec::Vec;
use crossbeam_channel as cb;
use spacepackets::ecss::tc::PusTcReader; use spacepackets::ecss::tc::PusTcReader;
use spacepackets::ecss::tm::PusTmCreator; use spacepackets::ecss::tm::PusTmCreator;
use spacepackets::ecss::{PusError, WritablePusPacket}; use spacepackets::ecss::{PusError, WritablePusPacket};
@ -391,6 +390,9 @@ pub mod std_mod {
use std::sync::mpsc::TryRecvError; use std::sync::mpsc::TryRecvError;
use thiserror::Error; use thiserror::Error;
#[cfg(feature = "crossbeam")]
pub use cb_mod::*;
use super::verification::VerificationReportingProvider; use super::verification::VerificationReportingProvider;
use super::{AcceptedEcssTcAndToken, TcInMemory}; use super::{AcceptedEcssTcAndToken, TcInMemory};
@ -400,21 +402,6 @@ pub mod std_mod {
} }
} }
impl From<cb::SendError<StoreAddr>> for EcssTmtcError {
fn from(_: cb::SendError<StoreAddr>) -> Self {
Self::Send(GenericSendError::RxDisconnected)
}
}
impl From<cb::TrySendError<StoreAddr>> for EcssTmtcError {
fn from(value: cb::TrySendError<StoreAddr>) -> Self {
match value {
cb::TrySendError::Full(_) => Self::Send(GenericSendError::QueueFull(None)),
cb::TrySendError::Disconnected(_) => Self::Send(GenericSendError::RxDisconnected),
}
}
}
impl EcssTmSenderCore for mpsc::Sender<StoreAddr> { impl EcssTmSenderCore for mpsc::Sender<StoreAddr> {
fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> {
match tm { match tm {
@ -439,19 +426,6 @@ pub mod std_mod {
} }
} }
#[cfg(feature = "crossbeam")]
impl EcssTmSenderCore for crossbeam_channel::Sender<StoreAddr> {
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<Vec<u8>> { impl EcssTmSenderCore for mpsc::Sender<Vec<u8>> {
fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> { fn send_tm(&self, tm: PusTmWrapper) -> Result<(), EcssTmtcError> {
match tm { match tm {
@ -476,19 +450,6 @@ pub mod std_mod {
} }
} }
#[cfg(feature = "crossbeam")]
impl EcssTmSenderCore for crossbeam_channel::Sender<Vec<u8>> {
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)] #[derive(Clone)]
pub struct TmInSharedPoolSenderWithId<Sender: EcssTmSenderCore> { pub struct TmInSharedPoolSenderWithId<Sender: EcssTmSenderCore> {
channel_id: ChannelId, channel_id: ChannelId,
@ -542,9 +503,6 @@ pub mod std_mod {
pub type TmInSharedPoolSenderWithMpsc = TmInSharedPoolSenderWithId<mpsc::Sender<StoreAddr>>; pub type TmInSharedPoolSenderWithMpsc = TmInSharedPoolSenderWithId<mpsc::Sender<StoreAddr>>;
pub type TmInSharedPoolSenderWithBoundedMpsc = pub type TmInSharedPoolSenderWithBoundedMpsc =
TmInSharedPoolSenderWithId<mpsc::SyncSender<StoreAddr>>; TmInSharedPoolSenderWithId<mpsc::SyncSender<StoreAddr>>;
#[cfg(feature = "crossbeam")]
pub type TmInSharedPoolSenderWithCrossbeam =
TmInSharedPoolSenderWithId<crossbeam_channel::Sender<StoreAddr>>;
/// This class can be used if frequent heap allocations during run-time are not an issue. /// 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 /// 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 { #[cfg(feature = "crossbeam")]
id: ChannelId, pub mod cb_mod {
name: &'static str, use super::*;
receiver: cb::Receiver<EcssTcAndToken>,
}
impl CrossbeamTcReceiver { pub type TmInSharedPoolSenderWithCrossbeam =
pub fn new( TmInSharedPoolSenderWithId<cb::Sender<StoreAddr>>;
impl From<cb::SendError<StoreAddr>> for EcssTmtcError {
fn from(_: cb::SendError<StoreAddr>) -> Self {
Self::Send(GenericSendError::RxDisconnected)
}
}
impl From<cb::TrySendError<StoreAddr>> for EcssTmtcError {
fn from(value: cb::TrySendError<StoreAddr>) -> Self {
match value {
cb::TrySendError::Full(_) => Self::Send(GenericSendError::QueueFull(None)),
cb::TrySendError::Disconnected(_) => {
Self::Send(GenericSendError::RxDisconnected)
}
}
}
}
impl EcssTmSenderCore for cb::Sender<StoreAddr> {
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<Vec<u8>> {
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, id: ChannelId,
name: &'static str, name: &'static str,
receiver: cb::Receiver<EcssTcAndToken>, receiver: cb::Receiver<EcssTcAndToken>,
) -> Self {
Self { id, name, receiver }
}
}
impl EcssChannel for CrossbeamTcReceiver {
fn channel_id(&self) -> ChannelId {
self.id
} }
fn name(&self) -> &'static str { impl CrossbeamTcReceiver {
self.name pub fn new(
id: ChannelId,
name: &'static str,
receiver: cb::Receiver<EcssTcAndToken>,
) -> Self {
Self { id, name, receiver }
}
} }
}
impl EcssTcReceiverCore for CrossbeamTcReceiver { impl EcssChannel for CrossbeamTcReceiver {
fn recv_tc(&self) -> Result<EcssTcAndToken, TryRecvTmtcError> { fn channel_id(&self) -> ChannelId {
self.receiver.try_recv().map_err(|e| match e { self.id
cb::TryRecvError::Empty => TryRecvTmtcError::Empty, }
cb::TryRecvError::Disconnected => {
TryRecvTmtcError::Tmtc(EcssTmtcError::from(GenericRecvError::TxDisconnected)) fn name(&self) -> &'static str {
} self.name
}) }
}
impl EcssTcReceiverCore for CrossbeamTcReceiver {
fn recv_tc(&self) -> Result<EcssTcAndToken, TryRecvTmtcError> {
self.receiver.try_recv().map_err(|e| match e {
cb::TryRecvError::Empty => TryRecvTmtcError::Empty,
cb::TryRecvError::Disconnected => TryRecvTmtcError::Tmtc(EcssTmtcError::from(
GenericRecvError::TxDisconnected,
)),
})
}
} }
} }