sat-rs/satrs-example/src/bin/simpleclient.rs

84 lines
3.8 KiB
Rust
Raw Normal View History

2022-11-20 19:54:14 +01:00
use satrs_core::pus::verification::RequestId;
2023-01-10 17:16:57 +01:00
use satrs_core::{
2023-07-03 00:42:20 +02:00
spacepackets::ecss::{PusPacket, SerializablePusPacket},
spacepackets::tc::PusTc,
spacepackets::tm::PusTm,
2023-01-10 17:16:57 +01:00
spacepackets::SpHeader,
};
2022-11-20 19:54:14 +01:00
use satrs_example::{OBSW_SERVER_ADDR, SERVER_PORT};
2022-08-18 01:32:02 +02:00
use std::net::{IpAddr, SocketAddr, UdpSocket};
2022-08-29 00:44:34 +02:00
use std::time::Duration;
2022-08-18 01:32:02 +02:00
fn main() {
let mut buf = [0; 32];
let addr = SocketAddr::new(IpAddr::V4(OBSW_SERVER_ADDR), SERVER_PORT);
2022-12-04 20:00:20 +01:00
let mut sph = SpHeader::tc_unseg(0x02, 0, 0).unwrap();
2022-08-18 01:32:02 +02:00
let pus_tc = PusTc::new_simple(&mut sph, 17, 1, None, true);
2022-08-28 00:28:29 +02:00
let client = UdpSocket::bind("127.0.0.1:7302").expect("Connecting to UDP server failed");
2022-09-11 14:51:25 +02:00
let tc_req_id = RequestId::new(&pus_tc);
2023-01-26 23:31:09 +01:00
println!("Packing and sending PUS ping command TC[17,1] with request ID {tc_req_id}");
2022-09-11 20:51:14 +02:00
let size = pus_tc
.write_to_bytes(&mut buf)
.expect("Creating PUS TC failed");
2022-08-18 01:32:02 +02:00
client
2022-11-27 22:05:42 +01:00
.send_to(&buf[0..size], addr)
2023-01-26 23:31:09 +01:00
.unwrap_or_else(|_| panic!("Sending to {addr:?} failed"));
2022-08-29 00:44:34 +02:00
client
.set_read_timeout(Some(Duration::from_secs(2)))
.expect("Setting read timeout failed");
2022-09-11 14:51:25 +02:00
loop {
let res = client.recv(&mut buf);
match res {
Ok(_len) => {
2022-09-13 10:43:07 +02:00
let (pus_tm, size) = PusTm::from_bytes(&buf, 7).expect("Parsing PUS TM failed");
2022-09-11 14:51:25 +02:00
if pus_tm.service() == 17 && pus_tm.subservice() == 2 {
println!("Received PUS Ping Reply TM[17,2]")
} else if pus_tm.service() == 1 {
if pus_tm.source_data().is_none() {
println!("Invalid verification TM, no source data");
}
let src_data = pus_tm.source_data().unwrap();
if src_data.len() < 4 {
println!("Invalid verification TM source data, less than 4 bytes")
}
let req_id = RequestId::from_bytes(src_data).unwrap();
if pus_tm.subservice() == 1 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,1] acceptance success for request ID {req_id}")
2022-09-11 14:58:51 +02:00
} else if pus_tm.subservice() == 2 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,2] acceptance failure for request ID {req_id}")
2022-09-11 14:51:25 +02:00
} else if pus_tm.subservice() == 3 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,3] start success for request ID {req_id}")
2022-09-11 14:58:51 +02:00
} else if pus_tm.subservice() == 4 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,2] start failure for request ID {req_id}")
2022-09-11 14:51:25 +02:00
} else if pus_tm.subservice() == 5 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,5] step success for request ID {req_id}")
2022-09-11 14:58:51 +02:00
} else if pus_tm.subservice() == 6 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,6] step failure for request ID {req_id}")
2022-09-11 14:51:25 +02:00
} else if pus_tm.subservice() == 7 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,7] completion success for request ID {req_id}")
2022-09-11 14:58:51 +02:00
} else if pus_tm.subservice() == 8 {
2023-01-26 23:31:09 +01:00
println!("Received TM[1,8] completion failure for request ID {req_id}");
2022-09-11 14:51:25 +02:00
}
} else {
println!(
"Received TM[{}, {}] with {} bytes",
pus_tm.service(),
pus_tm.subservice(),
size
);
}
}
Err(ref e)
if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::TimedOut =>
{
println!("No reply received for 2 seconds");
break;
}
_ => {
println!("UDP receive error {:?}", res.unwrap_err());
}
2022-09-03 13:47:25 +02:00
}
2022-08-29 00:44:34 +02:00
}
2022-08-18 01:32:02 +02:00
}