lets just push through with one concrete example
Some checks failed
Rust/sat-rs/pipeline/head There was a failure building this commit

and work from there
This commit is contained in:
Robin Müller 2024-02-21 17:47:15 +01:00
parent bfedd9572e
commit 0bd492038d
Signed by: muellerr
GPG Key ID: A649FB78196E3849
3 changed files with 171 additions and 26 deletions

View File

@ -5,7 +5,7 @@ use hashbrown::HashMap;
use crate::{ use crate::{
mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode}, mode::{Mode, ModeAndSubmode, ModeReply, ModeRequest, Submode},
queue::GenericSendError, queue::{GenericReceiveError, GenericSendError},
ChannelId, ChannelId,
}; };
@ -51,6 +51,7 @@ pub type ModeTable = HashMap<Mode, ModeTableMapValue>;
pub enum ModeMessagingError { pub enum ModeMessagingError {
TargetDoesNotExist(ChannelId), TargetDoesNotExist(ChannelId),
Send(GenericSendError), Send(GenericSendError),
Receive(GenericReceiveError),
} }
impl From<GenericSendError> for ModeMessagingError { impl From<GenericSendError> for ModeMessagingError {
fn from(value: GenericSendError) -> Self { fn from(value: GenericSendError) -> Self {
@ -58,7 +59,13 @@ impl From<GenericSendError> for ModeMessagingError {
} }
} }
pub trait ModeReplyHandler { impl From<GenericReceiveError> for ModeMessagingError {
fn from(value: GenericReceiveError) -> Self {
Self::Receive(value)
}
}
pub trait ModeReplySendProvider {
fn local_channel_id(&self) -> ChannelId; fn local_channel_id(&self) -> ChannelId;
fn send_mode_reply( fn send_mode_reply(
@ -68,12 +75,12 @@ pub trait ModeReplyHandler {
) -> Result<(), ModeMessagingError>; ) -> Result<(), ModeMessagingError>;
} }
pub struct StandardModeReplyMap { pub struct StandardModeReplySender {
pub local_id: ChannelId, pub local_id: ChannelId,
pub reply_sender_map: HashMap<ChannelId, mpsc::Sender<ModeReply>>, pub reply_sender_map: HashMap<ChannelId, mpsc::Sender<ModeReply>>,
} }
impl ModeReplyHandler for StandardModeReplyMap { impl ModeReplySendProvider for StandardModeReplySender {
fn send_mode_reply( fn send_mode_reply(
&mut self, &mut self,
target_channel_id: ChannelId, target_channel_id: ChannelId,
@ -95,7 +102,80 @@ impl ModeReplyHandler for StandardModeReplyMap {
} }
} }
pub trait ModeRequestSender { impl StandardModeReplySender {
pub fn new(local_id: ChannelId) -> Self {
Self {
local_id,
reply_sender_map: HashMap::new(),
}
}
pub fn add_reply_target(
&mut self,
target_id: ChannelId,
reply_sender: mpsc::Sender<ModeReply>,
) {
self.reply_sender_map.insert(target_id, reply_sender);
}
}
pub struct ModeReplyTuple {
pub sender_id: ChannelId,
pub reply: ModeReply,
}
pub trait ModeReplyReceiver {
fn try_recv_mode_reply(&self) -> Result<Option<ModeReplyTuple>, ModeMessagingError>;
}
pub struct StandardModeReplyReceiver {
pub sender_id: ChannelId,
pub receiver: mpsc::Receiver<ModeReply>,
}
#[derive(Default)]
pub struct StandardModeReplyReceiverList {
pub reply_receiver_vec: Vec<StandardModeReplyReceiver>,
}
impl StandardModeReplyReceiverList {
pub fn add_reply_receiver(
&mut self,
sender_id: ChannelId,
receiver: mpsc::Receiver<ModeReply>,
) {
self.reply_receiver_vec.push(StandardModeReplyReceiver {
sender_id,
receiver,
});
}
}
impl ModeReplyReceiver for StandardModeReplyReceiverList {
fn try_recv_mode_reply(&self) -> Result<Option<ModeReplyTuple>, ModeMessagingError> {
for reply_receiver in &self.reply_receiver_vec {
match reply_receiver.receiver.try_recv() {
Ok(reply) => {
return Ok(Some(ModeReplyTuple {
sender_id: reply_receiver.sender_id,
reply,
}))
}
Err(e) => {
if e == mpsc::TryRecvError::Disconnected {
return Err(GenericReceiveError::TxDisconnected(Some(
reply_receiver.sender_id,
))
.into());
}
}
}
}
Ok(None)
}
}
pub trait ModeRequestSendProvider {
fn local_channel_id(&self) -> Option<ChannelId>; fn local_channel_id(&self) -> Option<ChannelId>;
fn send_mode_request( fn send_mode_request(
&mut self, &mut self,
@ -104,12 +184,12 @@ pub trait ModeRequestSender {
) -> Result<(), ModeMessagingError>; ) -> Result<(), ModeMessagingError>;
} }
pub struct StandardModeRequestMap { pub struct StandardModeRequestSender {
pub local_channel_id: ChannelId, pub local_channel_id: ChannelId,
pub request_sender_map: HashMap<ChannelId, mpsc::Sender<ModeRequestWithSenderId>>, pub request_sender_map: HashMap<ChannelId, mpsc::Sender<ModeRequestWithSenderId>>,
} }
impl ModeRequestSender for StandardModeRequestMap { impl ModeRequestSendProvider for StandardModeRequestSender {
fn send_mode_request( fn send_mode_request(
&mut self, &mut self,
target_id: ChannelId, target_id: ChannelId,
@ -134,6 +214,23 @@ impl ModeRequestSender for StandardModeRequestMap {
} }
} }
impl StandardModeRequestSender {
pub fn new(local_channel_id: ChannelId) -> Self {
Self {
local_channel_id,
request_sender_map: HashMap::new(),
}
}
pub fn add_request_target(
&mut self,
target_id: ChannelId,
request_sender: mpsc::Sender<ModeRequestWithSenderId>,
) {
self.request_sender_map.insert(target_id, request_sender);
}
}
pub trait ModeProvider { pub trait ModeProvider {
fn mode_and_submode(&self) -> ModeAndSubmode; fn mode_and_submode(&self) -> ModeAndSubmode;
} }
@ -154,22 +251,25 @@ pub trait ModeRequestHandler: ModeProvider {
mod tests { mod tests {
use std::{println, sync::mpsc}; use std::{println, sync::mpsc};
use hashbrown::HashMap;
use crate::{ use crate::{
mode::{ModeAndSubmode, ModeReply, ModeRequest}, mode::{ModeAndSubmode, ModeReply, ModeRequest},
mode_tree::ModeRequestSender, mode_tree::ModeRequestSendProvider,
ChannelId, ChannelId,
}; };
use super::{ use super::{
ModeMessagingError, ModeProvider, ModeReplyHandler, ModeRequestHandler, ModeMessagingError, ModeProvider, ModeReplySendProvider, ModeRequestHandler,
ModeRequestWithSenderId, StandardModeReplyMap, StandardModeRequestMap, ModeRequestWithSenderId, StandardModeReplyReceiverList, StandardModeReplySender,
StandardModeRequestSender,
}; };
struct TestDevice { struct TestDevice {
/// One receiver handle for all mode requests. /// One receiver handle for all mode requests.
pub mode_req_receiver: mpsc::Receiver<ModeRequestWithSenderId>, pub mode_req_receiver: mpsc::Receiver<ModeRequestWithSenderId>,
/// This structure contains all handles to send mode replies. /// This structure contains all handles to send mode replies.
pub mode_reply_sender: StandardModeReplyMap, pub mode_reply_sender: StandardModeReplySender,
pub last_mode_sender: Option<ChannelId>, pub last_mode_sender: Option<ChannelId>,
pub mode_and_submode: ModeAndSubmode, pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>, pub target_mode_and_submode: Option<ModeAndSubmode>,
@ -179,9 +279,10 @@ mod tests {
/// One receiver handle for all mode requests. /// One receiver handle for all mode requests.
pub mode_req_receiver: mpsc::Receiver<ModeRequestWithSenderId>, pub mode_req_receiver: mpsc::Receiver<ModeRequestWithSenderId>,
/// This structure contains all handles to send mode replies. /// This structure contains all handles to send mode replies.
pub mode_reply_sender: StandardModeReplyMap, pub mode_reply_sender: StandardModeReplySender,
/// This structure contains all handles to send mode requests to its children. /// This structure contains all handles to send mode requests to its children.
pub mode_children_map: StandardModeRequestMap, pub mode_children_map: StandardModeRequestSender,
pub mode_reply_receiver_list: StandardModeReplyReceiverList,
pub last_mode_sender: Option<ChannelId>, pub last_mode_sender: Option<ChannelId>,
pub mode_and_submode: ModeAndSubmode, pub mode_and_submode: ModeAndSubmode,
pub target_mode_and_submode: Option<ModeAndSubmode>, pub target_mode_and_submode: Option<ModeAndSubmode>,
@ -194,6 +295,10 @@ mod tests {
} }
impl TestAssembly { impl TestAssembly {
pub fn run(&mut self) {
self.check_mode_requests()
}
pub fn check_mode_requests(&mut self) { pub fn check_mode_requests(&mut self) {
match self.mode_req_receiver.try_recv() { match self.mode_req_receiver.try_recv() {
Ok(ModeRequestWithSenderId { sender_id, request }) => { Ok(ModeRequestWithSenderId { sender_id, request }) => {
@ -219,6 +324,7 @@ mod tests {
}; };
} }
} }
impl ModeRequestHandler for TestAssembly { impl ModeRequestHandler for TestAssembly {
fn start_transition( fn start_transition(
&mut self, &mut self,
@ -258,4 +364,43 @@ mod tests {
Ok(()) Ok(())
} }
} }
pub enum TestChannelId {
Device1 = 1,
Device2 = 2,
Assembly = 3,
}
#[test]
fn basic_test() {
let (dev1_sender, dev1_receiver) = mpsc::channel();
let (dev2_sender, dev2_receiver) = mpsc::channel();
let mut mode_reply_sender_dev1 =
StandardModeReplySender::new(TestChannelId::Device1 as u32);
let mut mode_reply_sender_dev2 =
StandardModeReplySender::new(TestChannelId::Device2 as u32);
let mut mode_request_map_assy =
StandardModeRequestSender::new(TestChannelId::Assembly as u32);
let mut mode_reply_map_assy = StandardModeReplySender::new(TestChannelId::Assembly as u32);
let (dev1_reply_sender, assy_reply_receiver_dev1) = mpsc::channel();
let (dev2_reply_sender, assy_reply_receiver_dev2) = mpsc::channel();
mode_reply_sender_dev1.add_reply_target(TestChannelId::Assembly as u32, dev1_reply_sender);
mode_reply_sender_dev2.add_reply_target(TestChannelId::Assembly as u32, dev2_reply_sender);
//mode_reply_map_assy.add_reply_target(target_id, reply_sender)
let device1 = TestDevice {
mode_req_receiver: dev1_receiver,
mode_reply_sender: mode_reply_sender_dev1,
last_mode_sender: None,
mode_and_submode: ModeAndSubmode::new(0, 0),
target_mode_and_submode: None,
};
let device2 = TestDevice {
mode_req_receiver: dev2_receiver,
mode_reply_sender: mode_reply_sender_dev2,
last_mode_sender: None,
mode_and_submode: ModeAndSubmode::new(0, 0),
target_mode_and_submode: None,
};
}
} }

View File

@ -2,7 +2,7 @@
//! //!
//! This module contains structures to make working with the PUS C standard easier. //! This module contains structures to make working with the PUS C standard easier.
//! The satrs-example application contains various usage examples of these components. //! The satrs-example application contains various usage examples of these components.
use crate::queue::{GenericRecvError, GenericSendError}; use crate::queue::{GenericReceiveError, GenericSendError};
use crate::ChannelId; use crate::ChannelId;
use core::fmt::{Display, Formatter}; use core::fmt::{Display, Formatter};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
@ -64,7 +64,7 @@ pub enum EcssTmtcError {
Pus(PusError), Pus(PusError),
CantSendAddr(StoreAddr), CantSendAddr(StoreAddr),
Send(GenericSendError), Send(GenericSendError),
Recv(GenericRecvError), Recv(GenericReceiveError),
} }
impl Display for EcssTmtcError { impl Display for EcssTmtcError {
@ -110,8 +110,8 @@ impl From<GenericSendError> for EcssTmtcError {
} }
} }
impl From<GenericRecvError> for EcssTmtcError { impl From<GenericReceiveError> for EcssTmtcError {
fn from(value: GenericRecvError) -> Self { fn from(value: GenericReceiveError) -> Self {
Self::Recv(value) Self::Recv(value)
} }
} }
@ -367,7 +367,7 @@ pub mod std_mod {
use crate::pus::verification::{TcStateAccepted, VerificationToken}; use crate::pus::verification::{TcStateAccepted, VerificationToken};
use crate::pus::{ use crate::pus::{
EcssChannel, EcssTcAndToken, EcssTcReceiver, EcssTcReceiverCore, EcssTmSender, EcssChannel, EcssTcAndToken, EcssTcReceiver, EcssTcReceiverCore, EcssTmSender,
EcssTmSenderCore, EcssTmtcError, GenericRecvError, GenericSendError, PusTmWrapper, EcssTmSenderCore, EcssTmtcError, GenericReceiveError, GenericSendError, PusTmWrapper,
TryRecvTmtcError, TryRecvTmtcError,
}; };
use crate::tmtc::tm_helper::SharedTmPool; use crate::tmtc::tm_helper::SharedTmPool;
@ -486,7 +486,7 @@ pub mod std_mod {
self.receiver.try_recv().map_err(|e| match e { self.receiver.try_recv().map_err(|e| match e {
TryRecvError::Empty => TryRecvTmtcError::Empty, TryRecvError::Empty => TryRecvTmtcError::Empty,
TryRecvError::Disconnected => { TryRecvError::Disconnected => {
TryRecvTmtcError::Error(EcssTmtcError::from(GenericRecvError::TxDisconnected)) TryRecvTmtcError::Error(EcssTmtcError::from(GenericReceiveError::TxDisconnected(Some(self.id()))))
} }
}) })
} }
@ -626,7 +626,7 @@ pub mod std_mod {
self.receiver.try_recv().map_err(|e| match e { self.receiver.try_recv().map_err(|e| match e {
cb::TryRecvError::Empty => TryRecvTmtcError::Empty, cb::TryRecvError::Empty => TryRecvTmtcError::Empty,
cb::TryRecvError::Disconnected => { cb::TryRecvError::Disconnected => {
TryRecvTmtcError::Error(EcssTmtcError::from(GenericRecvError::TxDisconnected)) TryRecvTmtcError::Error(EcssTmtcError::from(GenericReceiveError::TxDisconnected(Some(self.id()))))
} }
}) })
} }

View File

@ -30,16 +30,16 @@ impl Error for GenericSendError {}
/// Generic error type for sending something via a message queue. /// Generic error type for sending something via a message queue.
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub enum GenericRecvError { pub enum GenericReceiveError {
Empty, Empty,
TxDisconnected, TxDisconnected(Option<ChannelId>)
} }
impl Display for GenericRecvError { impl Display for GenericReceiveError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
match self { match self {
Self::TxDisconnected => { Self::TxDisconnected(channel_id) => {
write!(f, "tx side has disconnected") write!(f, "tx side with id {channel_id:?} has disconnected")
} }
Self::Empty => { Self::Empty => {
write!(f, "nothing to receive") write!(f, "nothing to receive")
@ -49,4 +49,4 @@ impl Display for GenericRecvError {
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
impl Error for GenericRecvError {} impl Error for GenericReceiveError {}