Skip to content

Commit

Permalink
first connection success
Browse files Browse the repository at this point in the history
  • Loading branch information
robamu committed May 8, 2024
1 parent c20163b commit fe4126f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
10 changes: 3 additions & 7 deletions satrs-example/src/acs/mgm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
Expand Down
35 changes: 17 additions & 18 deletions satrs-example/src/interface/sim_client_udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimComponent, mpsc::Sender<SimReply>>);
Expand All @@ -19,20 +22,9 @@ pub fn create_sim_client(sim_request_rx: mpsc::Receiver<SimRequest>) -> Option<S
log::info!("simulator client connection success");
return Some(sim_client);
}
Err(e) => 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
}
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions satrs-minisim/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion satrs-minisim/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fe4126f

Please sign in to comment.