diff --git a/satrs-example/src/acs/mgm.rs b/satrs-example/src/acs/mgm.rs index 2445dd6..3f7edf3 100644 --- a/satrs-example/src/acs/mgm.rs +++ b/satrs-example/src/acs/mgm.rs @@ -61,18 +61,14 @@ pub struct SpiSimInterface { impl SpiInterface for SpiSimInterface { type Error = (); - fn transfer(&mut self, tx: &[u8], rx: &mut [u8]) -> Result<(), Self::Error> { + // Right now, we only support requesting sensor data and not configuration of the sensor. + fn transfer(&mut self, _tx: &[u8], _rx: &mut [u8]) -> Result<(), Self::Error> { let mgm_sensor_request = MgmRequestLis3Mdl::RequestSensorData; self.sim_request_tx .send(SimRequest::new_with_epoch_time(mgm_sensor_request)) .expect("failed to send request"); self.sim_reply_rx.recv().expect("reply timeout"); - /* - let mgm_req_json = serde_json::to_string(&mgm_sensor_request)?; - self.udp_socket - .send_to(mgm_req_json.as_bytes(), self.sim_addr) - .unwrap(); - */ + // TODO: Write the sensor data to the raw buffer. Ok(()) } } diff --git a/satrs-example/src/interface/sim_client_udp.rs b/satrs-example/src/interface/sim_client_udp.rs index fe596dc..0da3c3e 100644 --- a/satrs-example/src/interface/sim_client_udp.rs +++ b/satrs-example/src/interface/sim_client_udp.rs @@ -5,7 +5,10 @@ use std::{ time::Duration, }; -use satrs_minisim::{udp::SIM_CTRL_PORT, SimComponent, SimMessageProvider, SimReply, SimRequest}; +use satrs_minisim::{ + udp::SIM_CTRL_PORT, SerializableSimMsgPayload, SimComponent, SimMessageProvider, SimReply, + SimRequest, +}; use satrs_minisim::{SimCtrlReply, SimCtrlRequest}; struct SimReplyMap(pub HashMap>); @@ -19,20 +22,9 @@ pub fn create_sim_client(sim_request_rx: mpsc::Receiver) -> Option match e { - SimClientCreationResult::Io(e) => { - log::warn!("creating SIM client failed with io error {}", e); - } - SimClientCreationResult::Timeout => { - log::warn!("timeout when attempting connection to SIM client"); - } - SimClientCreationResult::InvalidPingReply(reply) => { - log::warn!( - "invalid ping reply when attempting connection to SIM client: {}", - reply - ); - } - }, + Err(e) => { + log::warn!("sim client creation error: {}", e); + } } None } @@ -44,7 +36,9 @@ pub enum SimClientCreationResult { #[error("timeout when trying to connect to sim UDP server")] Timeout, #[error("invalid ping reply when trying connection to UDP sim server")] - InvalidPingReply(#[from] serde_json::Error), + InvalidReplyJsonError(#[from] serde_json::Error), + #[error("invalid sim reply, not pong reply as expected: {0:?}")] + ReplyIsNotPong(SimReply), } pub struct SimClientUdp { @@ -68,9 +62,14 @@ impl SimClientUdp { udp_client.send_to(sim_req_json.as_bytes(), simulator_addr)?; match udp_client.recv(&mut reply_buf) { Ok(reply_len) => { - let sim_reply: SimCtrlReply = serde_json::from_slice(&reply_buf[0..reply_len])?; + let sim_reply: SimReply = serde_json::from_slice(&reply_buf[0..reply_len])?; + if sim_reply.component() != SimComponent::SimCtrl { + return Err(SimClientCreationResult::ReplyIsNotPong(sim_reply)); + } udp_client.set_read_timeout(None)?; - match sim_reply { + let sim_ctrl_reply = + SimCtrlReply::from_sim_message(&sim_reply).expect("invalid SIM reply"); + match sim_ctrl_reply { SimCtrlReply::Pong => Ok(Self { udp_client, simulator_addr, diff --git a/satrs-minisim/src/controller.rs b/satrs-minisim/src/controller.rs index c3196af..09d2772 100644 --- a/satrs-minisim/src/controller.rs +++ b/satrs-minisim/src/controller.rs @@ -16,6 +16,11 @@ use crate::{ eps::PcduModel, }; +const SIM_CTRL_REQ_WIRETAPPING: bool = true; +const MGM_REQ_WIRETAPPING: bool = true; +const PCDU_REQ_WIRETAPPING: bool = true; +const MGT_REQ_WIRETAPPING: bool = true; + // The simulation controller processes requests and drives the simulation. pub struct SimController { pub sys_clock: SystemClock, @@ -91,6 +96,9 @@ impl SimController { fn handle_ctrl_request(&mut self, request: &SimRequest) -> Result<(), SimRequestError> { let sim_ctrl_request = SimCtrlRequest::from_sim_message(request)?; + if SIM_CTRL_REQ_WIRETAPPING { + log::info!("received sim ctrl request: {:?}", sim_ctrl_request); + } match sim_ctrl_request { SimCtrlRequest::Ping => { self.reply_sender @@ -103,6 +111,9 @@ impl SimController { fn handle_mgm_request(&mut self, request: &SimRequest) -> Result<(), SimRequestError> { let mgm_request = MgmRequestLis3Mdl::from_sim_message(request)?; + if MGM_REQ_WIRETAPPING { + log::info!("received MGM request: {:?}", mgm_request); + } match mgm_request { MgmRequestLis3Mdl::RequestSensorData => { self.simulation.send_event( @@ -117,6 +128,9 @@ impl SimController { fn handle_pcdu_request(&mut self, request: &SimRequest) -> Result<(), SimRequestError> { let pcdu_request = PcduRequest::from_sim_message(request)?; + if PCDU_REQ_WIRETAPPING { + log::info!("received PCDU request: {:?}", pcdu_request); + } match pcdu_request { PcduRequest::RequestSwitchInfo => { self.simulation @@ -135,6 +149,9 @@ impl SimController { fn handle_mgt_request(&mut self, request: &SimRequest) -> Result<(), SimRequestError> { let mgt_request = MgtRequest::from_sim_message(request)?; + if MGT_REQ_WIRETAPPING { + log::info!("received MGT request: {:?}", mgt_request); + } match mgt_request { MgtRequest::ApplyTorque { duration, dipole } => self.simulation.send_event( MagnetorquerModel::apply_torque, diff --git a/satrs-minisim/src/lib.rs b/satrs-minisim/src/lib.rs index 92c73c3..1f0f1b6 100644 --- a/satrs-minisim/src/lib.rs +++ b/satrs-minisim/src/lib.rs @@ -89,7 +89,7 @@ impl SimMessageProvider for SimRequest { } /// A generic simulation reply type. Right now, the payload data is expected to be -/// JSON, which might be changed inthe future. +/// JSON, which might be changed in the future. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SimReply { inner: SimMessage,