sat-rs/satrs/tests/tcp_servers.rs

307 lines
11 KiB
Rust
Raw Permalink Normal View History

2023-09-20 15:04:46 +02:00
//! This serves as both an integration test and an example application showcasing all major
//! features of the TCP COBS server by performing following steps:
//!
//! 1. It defines both a TC receiver and a TM source which are [Sync].
//! 2. A telemetry packet is inserted into the TM source. The packet will be handled by the
//! TCP server after handling all TCs.
//! 3. It instantiates the TCP server on localhost with automatic port assignment and assigns
//! the TC receiver and TM source created previously.
//! 4. It moves the TCP server to a different thread and calls the
//! [TcpTmtcInCobsServer::handle_next_connection] call inside that thread
//! 5. The main threads connects to the server, sends a test telecommand and then reads back
//! the test telemetry insertd in to the TM source previously.
use core::{
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};
use std::{
io::{Read, Write},
net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream},
2024-04-16 11:04:22 +02:00
sync::{mpsc, Mutex},
2023-09-20 15:04:46 +02:00
thread,
};
2023-09-26 15:57:51 +02:00
use hashbrown::HashSet;
use satrs::{
2024-04-16 11:04:22 +02:00
encoding::{
ccsds::{SpValidity, SpacePacketValidator},
cobs::encode_packet_with_cobs,
},
2024-04-10 11:28:16 +02:00
hal::std::tcp_server::{
ConnectionResult, HandledConnectionHandler, HandledConnectionInfo, ServerConfig,
TcpSpacepacketsServer, TcpTmtcInCobsServer,
},
2024-04-16 11:04:22 +02:00
tmtc::PacketSource,
ComponentId,
2023-09-20 15:04:46 +02:00
};
2023-09-26 15:57:51 +02:00
use spacepackets::{
2023-12-07 13:29:26 +01:00
ecss::{tc::PusTcCreator, WritablePusPacket},
2024-04-16 11:04:22 +02:00
CcsdsPacket, PacketId, SpHeader,
2023-09-26 15:57:51 +02:00
};
use std::{collections::VecDeque, sync::Arc, vec::Vec};
2023-09-20 15:04:46 +02:00
2024-04-10 11:28:16 +02:00
#[derive(Default)]
pub struct ConnectionFinishedHandler {
connection_info: VecDeque<HandledConnectionInfo>,
}
impl HandledConnectionHandler for ConnectionFinishedHandler {
fn handled_connection(&mut self, info: HandledConnectionInfo) {
self.connection_info.push_back(info);
}
}
impl ConnectionFinishedHandler {
pub fn check_last_connection(&mut self, num_tms: u32, num_tcs: u32) {
let last_conn_result = self
.connection_info
.pop_back()
.expect("no connection info available");
assert_eq!(last_conn_result.num_received_tcs, num_tcs);
assert_eq!(last_conn_result.num_sent_tms, num_tms);
}
pub fn check_no_connections_left(&self) {
assert!(self.connection_info.is_empty());
}
}
2023-09-20 15:04:46 +02:00
#[derive(Default, Clone)]
struct SyncTmSource {
tm_queue: Arc<Mutex<VecDeque<Vec<u8>>>>,
}
impl SyncTmSource {
pub(crate) fn add_tm(&mut self, tm: &[u8]) {
let mut tm_queue = self.tm_queue.lock().expect("locking tm queue failec");
tm_queue.push_back(tm.to_vec());
}
}
2024-04-16 11:04:22 +02:00
impl PacketSource for SyncTmSource {
2023-09-20 15:04:46 +02:00
type Error = ();
fn retrieve_packet(&mut self, buffer: &mut [u8]) -> Result<usize, Self::Error> {
let mut tm_queue = self.tm_queue.lock().expect("locking tm queue failed");
if !tm_queue.is_empty() {
let next_vec = tm_queue.front().unwrap();
if buffer.len() < next_vec.len() {
panic!(
"provided buffer too small, must be at least {} bytes",
next_vec.len()
);
}
println!("Sending and encoding TM: {:x?}", next_vec);
let next_vec = tm_queue.pop_front().unwrap();
buffer[0..next_vec.len()].copy_from_slice(&next_vec);
return Ok(next_vec.len());
}
Ok(0)
}
}
2024-04-16 11:04:22 +02:00
const TCP_SERVER_ID: ComponentId = 0x05;
2023-09-20 15:04:46 +02:00
const SIMPLE_PACKET: [u8; 5] = [1, 2, 3, 4, 5];
const INVERTED_PACKET: [u8; 5] = [5, 4, 3, 4, 1];
2023-09-26 15:57:51 +02:00
const AUTO_PORT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
2023-09-20 15:04:46 +02:00
2023-09-26 15:57:51 +02:00
#[test]
fn test_cobs_server() {
2024-04-16 11:04:22 +02:00
let (tc_sender, tc_receiver) = mpsc::channel();
2023-09-20 15:04:46 +02:00
let mut tm_source = SyncTmSource::default();
// Insert a telemetry packet which will be read back by the client at a later stage.
tm_source.add_tm(&INVERTED_PACKET);
let mut tcp_server = TcpTmtcInCobsServer::new(
2024-04-16 11:04:22 +02:00
ServerConfig::new(
TCP_SERVER_ID,
AUTO_PORT_ADDR,
Duration::from_millis(2),
1024,
1024,
),
2023-10-01 14:32:15 +02:00
tm_source,
2024-04-16 11:04:22 +02:00
tc_sender.clone(),
2024-04-10 11:28:16 +02:00
ConnectionFinishedHandler::default(),
2024-04-09 17:21:43 +02:00
None,
2023-09-20 15:04:46 +02:00
)
.expect("TCP server generation failed");
let dest_addr = tcp_server
.local_addr()
.expect("retrieving dest addr failed");
let conn_handled: Arc<AtomicBool> = Default::default();
let set_if_done = conn_handled.clone();
// Call the connection handler in separate thread, does block.
thread::spawn(move || {
2024-04-16 11:04:22 +02:00
let result = tcp_server.handle_all_connections(Some(Duration::from_millis(400)));
2023-09-20 15:04:46 +02:00
if result.is_err() {
panic!("handling connection failed: {:?}", result.unwrap_err());
}
let conn_result = result.unwrap();
2024-04-10 11:28:16 +02:00
assert_eq!(conn_result, ConnectionResult::HandledConnections(1));
tcp_server
.generic_server
.finished_handler
.check_last_connection(1, 1);
tcp_server
.generic_server
.finished_handler
.check_no_connections_left();
2023-09-20 15:04:46 +02:00
// Signal the main thread we are done.
set_if_done.store(true, Ordering::Relaxed);
});
// Send TC to server now.
let mut encoded_buf: [u8; 16] = [0; 16];
let mut current_idx = 0;
2023-09-20 15:20:14 +02:00
encode_packet_with_cobs(&SIMPLE_PACKET, &mut encoded_buf, &mut current_idx);
2023-09-20 15:04:46 +02:00
let mut stream = TcpStream::connect(dest_addr).expect("connecting to TCP server failed");
stream
.write_all(&encoded_buf[..current_idx])
.expect("writing to TCP server failed");
// Done with writing.
stream
.shutdown(std::net::Shutdown::Write)
.expect("shutting down write failed");
let mut read_buf: [u8; 16] = [0; 16];
let read_len = stream.read(&mut read_buf).expect("read failed");
drop(stream);
// 1 byte encoding overhead, 2 sentinel bytes.
assert_eq!(read_len, 8);
assert_eq!(read_buf[0], 0);
assert_eq!(read_buf[read_len - 1], 0);
let decoded_len =
cobs::decode_in_place(&mut read_buf[1..read_len]).expect("COBS decoding failed");
assert_eq!(decoded_len, 5);
// Skip first sentinel byte.
assert_eq!(&read_buf[1..1 + INVERTED_PACKET.len()], &INVERTED_PACKET);
// A certain amount of time is allowed for the transaction to complete.
for _ in 0..3 {
if !conn_handled.load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(5));
}
}
if !conn_handled.load(Ordering::Relaxed) {
panic!("connection was not handled properly");
}
// Check that the packet was received and decoded successfully.
2024-04-16 11:04:22 +02:00
let tc_with_sender = tc_receiver.try_recv().expect("no TC received");
assert_eq!(tc_with_sender.packet, SIMPLE_PACKET);
assert_eq!(tc_with_sender.sender_id, TCP_SERVER_ID);
matches!(tc_receiver.try_recv(), Err(mpsc::TryRecvError::Empty));
2023-09-20 15:04:46 +02:00
}
2023-09-26 15:57:51 +02:00
const TEST_APID_0: u16 = 0x02;
const TEST_PACKET_ID_0: PacketId = PacketId::new_for_tc(true, TEST_APID_0);
2023-09-26 15:57:51 +02:00
2024-04-16 11:04:22 +02:00
#[derive(Default)]
pub struct SimpleVerificator {
pub valid_ids: HashSet<PacketId>,
}
impl SpacePacketValidator for SimpleVerificator {
fn validate(
&self,
sp_header: &SpHeader,
_raw_buf: &[u8],
) -> satrs::encoding::ccsds::SpValidity {
if self.valid_ids.contains(&sp_header.packet_id()) {
return SpValidity::Valid;
}
SpValidity::Skip
}
}
2023-09-26 15:57:51 +02:00
#[test]
fn test_ccsds_server() {
2024-04-16 11:04:22 +02:00
let (tc_sender, tc_receiver) = mpsc::channel();
2023-09-26 15:57:51 +02:00
let mut tm_source = SyncTmSource::default();
let sph = SpHeader::new_for_unseg_tc(TEST_APID_0, 0, 0);
let verif_tm = PusTcCreator::new_simple(sph, 1, 1, &[], true);
2023-09-26 17:26:42 +02:00
let tm_0 = verif_tm.to_vec().expect("tm generation failed");
tm_source.add_tm(&tm_0);
2024-04-16 11:04:22 +02:00
let mut packet_id_lookup = SimpleVerificator::default();
packet_id_lookup.valid_ids.insert(TEST_PACKET_ID_0);
2023-09-26 15:57:51 +02:00
let mut tcp_server = TcpSpacepacketsServer::new(
2024-04-16 11:04:22 +02:00
ServerConfig::new(
TCP_SERVER_ID,
AUTO_PORT_ADDR,
Duration::from_millis(2),
1024,
1024,
),
2023-10-01 14:32:15 +02:00
tm_source,
2024-04-16 11:04:22 +02:00
tc_sender,
packet_id_lookup,
2024-04-10 11:28:16 +02:00
ConnectionFinishedHandler::default(),
2024-04-09 17:21:43 +02:00
None,
2023-09-26 15:57:51 +02:00
)
.expect("TCP server generation failed");
let dest_addr = tcp_server
.local_addr()
.expect("retrieving dest addr failed");
let conn_handled: Arc<AtomicBool> = Default::default();
let set_if_done = conn_handled.clone();
// Call the connection handler in separate thread, does block.
thread::spawn(move || {
2024-04-16 11:04:22 +02:00
let result = tcp_server.handle_all_connections(Some(Duration::from_millis(500)));
2023-09-26 15:57:51 +02:00
if result.is_err() {
panic!("handling connection failed: {:?}", result.unwrap_err());
}
let conn_result = result.unwrap();
2024-04-10 11:28:16 +02:00
assert_eq!(conn_result, ConnectionResult::HandledConnections(1));
tcp_server
.generic_server
.finished_handler
.check_last_connection(1, 1);
tcp_server
.generic_server
.finished_handler
.check_no_connections_left();
2023-09-26 15:57:51 +02:00
set_if_done.store(true, Ordering::Relaxed);
});
let mut stream = TcpStream::connect(dest_addr).expect("connecting to TCP server failed");
stream
.set_read_timeout(Some(Duration::from_millis(10)))
.expect("setting reas timeout failed");
2023-09-26 17:26:42 +02:00
// Send ping telecommand.
let sph = SpHeader::new_for_unseg_tc(TEST_APID_0, 0, 0);
let ping_tc = PusTcCreator::new_simple(sph, 17, 1, &[], true);
2023-09-26 17:26:42 +02:00
let tc_0 = ping_tc.to_vec().expect("packet creation failed");
2023-09-26 15:57:51 +02:00
stream
2023-09-26 17:26:42 +02:00
.write_all(&tc_0)
2023-09-26 15:57:51 +02:00
.expect("writing to TCP server failed");
// Done with writing.
stream
.shutdown(std::net::Shutdown::Write)
.expect("shutting down write failed");
2023-09-26 17:26:42 +02:00
// Now read all the telemetry from the server.
2023-09-26 15:57:51 +02:00
let mut read_buf: [u8; 16] = [0; 16];
let mut read_len_total = 0;
// Timeout ensures this does not block forever.
2023-09-26 17:26:42 +02:00
while read_len_total < tm_0.len() {
2023-09-26 15:57:51 +02:00
let read_len = stream.read(&mut read_buf).expect("read failed");
read_len_total += read_len;
2023-09-26 17:26:42 +02:00
assert_eq!(read_buf[..read_len], tm_0);
2023-09-26 15:57:51 +02:00
}
drop(stream);
// A certain amount of time is allowed for the transaction to complete.
for _ in 0..3 {
if !conn_handled.load(Ordering::Relaxed) {
thread::sleep(Duration::from_millis(5));
}
}
if !conn_handled.load(Ordering::Relaxed) {
panic!("connection was not handled properly");
}
// Check that TC has arrived.
2024-04-16 11:04:22 +02:00
let tc_with_sender = tc_receiver.try_recv().expect("no TC received");
assert_eq!(tc_with_sender.packet, tc_0);
assert_eq!(tc_with_sender.sender_id, TCP_SERVER_ID);
matches!(tc_receiver.try_recv(), Err(mpsc::TryRecvError::Empty));
2023-09-26 15:57:51 +02:00
}