Skip to content

Commit

Permalink
continue PCDU handler
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 16, 2024
1 parent 8728c7e commit 15fcb17
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
51 changes: 49 additions & 2 deletions satrs-example/src/eps/pcdu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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<ReplyHandler: FnMut(&[u8])>(&self, f: ReplyHandler) -> Result<(), Self::Error>;
fn try_recv_replies<ReplyHandler: FnMut(&[u8])>(
&self,
f: ReplyHandler,
) -> Result<(), Self::Error>;
}

pub struct SimSerialInterface {
pub sim_request_tx: mpsc::Sender<SimRequest>,
pub sim_reply_rx: mpsc::Receiver<SimReply>,
}

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<ReplyHandler: FnMut(&[u8])>(
&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)]
Expand Down Expand Up @@ -73,7 +116,9 @@ impl<ComInterface: SerialInterface, TmSender: EcssTmSender> PcduHandler<ComInter
self.handle_composite_requests();
self.handle_mode_requests();
}
OpCode::PollAndRecvReplies => {}
OpCode::PollAndRecvReplies => {
self.poll_and_handle_replies();
}
}
}

Expand Down Expand Up @@ -138,6 +183,8 @@ impl<ComInterface: SerialInterface, TmSender: EcssTmSender> PcduHandler<ComInter
}
}
}

pub fn poll_and_handle_replies(&mut self) {}
}

impl<ComInterface: SerialInterface, TmSender: EcssTmSender> ModeProvider
Expand Down
25 changes: 25 additions & 0 deletions satrs-minisim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<SimRequest> for PcduRequest {
const TARGET: SimComponent = SimComponent::Pcdu;
}
Expand Down

0 comments on commit 15fcb17

Please sign in to comment.