diff --git a/satrs-example/src/eps/pcdu.rs b/satrs-example/src/eps/pcdu.rs index ad331a0..2b20520 100644 --- a/satrs-example/src/eps/pcdu.rs +++ b/satrs-example/src/eps/pcdu.rs @@ -13,6 +13,7 @@ use satrs::{ request::{GenericMessage, MessageMetadata, UniqueApidTargetId}, }; use satrs_example::{config::components::PUS_MODE_SERVICE, DeviceMode, TimestampHelper}; +use satrs_minisim::{SimReply, SimRequest}; use crate::{acs::mgm::MpscModeLeafInterface, pus::hk::HkReply, requests::CompositeRequest}; @@ -22,7 +23,49 @@ pub trait SerialInterface { fn send(&self, data: &[u8]) -> Result<(), Self::Error>; /// Receive all replies received on the serial interface so far. This function takes a closure /// and call its for each received packet, passing the received packet into it. - fn recv_replies(&self, f: ReplyHandler) -> Result<(), Self::Error>; + fn try_recv_replies( + &self, + f: ReplyHandler, + ) -> Result<(), Self::Error>; +} + +pub struct SimSerialInterface { + pub sim_request_tx: mpsc::Sender, + pub sim_reply_rx: mpsc::Receiver, +} + +impl SerialInterface for SimSerialInterface { + type Error = (); + + fn send(&self, data: &[u8]) -> Result<(), Self::Error> { + let request: SimRequest = serde_json::from_slice(data).unwrap(); + self.sim_request_tx + .send(request) + .expect("failed to send request to simulation"); + Ok(()) + } + + fn try_recv_replies( + &self, + mut f: ReplyHandler, + ) -> Result<(), Self::Error> { + loop { + match self.sim_reply_rx.try_recv() { + Ok(reply) => { + let reply = serde_json::to_string(&reply).unwrap(); + f(reply.as_bytes()); + } + Err(e) => match e { + mpsc::TryRecvError::Empty => break, + mpsc::TryRecvError::Disconnected => { + log::warn!("sim reply sender has disconnected"); + break; + } + }, + } + } + Ok(()) + } } #[derive(Debug, Copy, Clone, PartialEq, Eq)] @@ -73,7 +116,9 @@ impl PcduHandler {} + OpCode::PollAndRecvReplies => { + self.poll_and_handle_replies(); + } } } @@ -138,6 +183,8 @@ impl PcduHandler ModeProvider diff --git a/satrs-minisim/src/lib.rs b/satrs-minisim/src/lib.rs index 124ef32..c4b952c 100644 --- a/satrs-minisim/src/lib.rs +++ b/satrs-minisim/src/lib.rs @@ -172,6 +172,13 @@ pub mod eps { Mgt = 1, } + #[derive(Debug, Copy, Clone)] + #[repr(u8)] + pub enum PcduRequestId { + SwitchDevice = 0, + RequestSwitchInfo = 1, + } + #[derive(Debug, Copy, Clone, Serialize, Deserialize)] pub enum PcduRequest { SwitchDevice { @@ -181,6 +188,24 @@ pub mod eps { RequestSwitchInfo, } + /* + impl PcduRequest { + /// The sole purpose of this method + pub fn write_to_be_bytes(&self, buf: &mut [u8]) { + match self { + PcduRequest::SwitchDevice { switch, state } => { + buf[0] = PcduRequestId::SwitchDevice as u8; + buf[1..3].copy_from_slice(&(*switch as u16).to_be_bytes()); + buf[4] = *state as u8; + } + PcduRequest::RequestSwitchInfo => { + buf[0] = PcduRequestId::RequestSwitchInfo as u8; + } + } + } + } + */ + impl SerializableSimMsgPayload for PcduRequest { const TARGET: SimComponent = SimComponent::Pcdu; }