small bugfix, test works
Some checks failed
Rust/sat-rs/pipeline/pr-main There was a failure building this commit

This commit is contained in:
Robin Müller 2023-09-26 12:48:25 +02:00
parent 683ae899f5
commit a62df6dbf8
2 changed files with 56 additions and 15 deletions

View File

@ -80,7 +80,7 @@ pub fn parse_buffer_for_ccsds_space_packets<E>(
let length_field = let length_field =
u16::from_be_bytes(buf[current_idx + 4..current_idx + 6].try_into().unwrap()); u16::from_be_bytes(buf[current_idx + 4..current_idx + 6].try_into().unwrap());
let packet_size = length_field + 7; let packet_size = length_field + 7;
if (current_idx + packet_size as usize) < buf_len { if (current_idx + packet_size as usize) <= buf_len {
tc_receiver.pass_tc(&buf[current_idx..current_idx + packet_size as usize])?; tc_receiver.pass_tc(&buf[current_idx..current_idx + packet_size as usize])?;
packets_found += 1; packets_found += 1;
} else { } else {

View File

@ -1,5 +1,8 @@
use delegate::delegate; use delegate::delegate;
use std::{io::Write, net::{TcpStream, TcpListener, SocketAddr}}; use std::{
io::Write,
net::{SocketAddr, TcpListener, TcpStream},
};
use alloc::boxed::Box; use alloc::boxed::Box;
@ -105,7 +108,7 @@ impl<TmError: 'static, TcError: 'static> TcpSpacepacketsServer<TmError, TcError>
cfg: ServerConfig, cfg: ServerConfig,
tm_source: Box<dyn TmPacketSource<Error = TmError>>, tm_source: Box<dyn TmPacketSource<Error = TmError>>,
tc_receiver: Box<dyn ReceivesTc<Error = TcError>>, tc_receiver: Box<dyn ReceivesTc<Error = TcError>>,
packet_id_lookup: Box<dyn PacketIdLookup + Send> packet_id_lookup: Box<dyn PacketIdLookup + Send>,
) -> Result<Self, TcpTmtcError<TmError, TcError>> { ) -> Result<Self, TcpTmtcError<TmError, TcError>> {
Ok(Self { Ok(Self {
generic_server: TcpTmtcGenericServer::new( generic_server: TcpTmtcGenericServer::new(
@ -136,42 +139,56 @@ impl<TmError: 'static, TcError: 'static> TcpSpacepacketsServer<TmError, TcError>
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use core::time::Duration; use core::{
use std::net::{SocketAddr, IpAddr, Ipv4Addr}; sync::atomic::{AtomicBool, Ordering},
time::Duration,
};
use std::{
io::Write,
net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream},
thread,
};
use alloc::boxed::Box; use alloc::{boxed::Box, sync::Arc};
use hashbrown::HashSet; use hashbrown::HashSet;
use spacepackets::PacketId; use spacepackets::{
ecss::{tc::PusTcCreator, SerializablePusPacket},
PacketId, SpHeader,
};
use crate::hal::std::tcp_server::{tests::{SyncTmSource, SyncTcCacher}, ServerConfig}; use crate::hal::std::tcp_server::{
tests::{SyncTcCacher, SyncTmSource},
ServerConfig,
};
use super::TcpSpacepacketsServer; use super::TcpSpacepacketsServer;
const APID_0: u16 = 0x02; const TEST_APID_0: u16 = 0x02;
const PACKET_ID_0: PacketId= PacketId::const_tc(true, APID_0); const TEST_PACKET_ID_0: PacketId = PacketId::const_tc(true, TEST_APID_0);
fn generic_tmtc_server( fn generic_tmtc_server(
addr: &SocketAddr, addr: &SocketAddr,
tc_receiver: SyncTcCacher, tc_receiver: SyncTcCacher,
tm_source: SyncTmSource, tm_source: SyncTmSource,
packet_id_lookup: HashSet<PacketId> packet_id_lookup: HashSet<PacketId>,
) -> TcpSpacepacketsServer<(), ()> { ) -> TcpSpacepacketsServer<(), ()> {
TcpSpacepacketsServer::new( TcpSpacepacketsServer::new(
ServerConfig::new(*addr, Duration::from_millis(2), 1024, 1024), ServerConfig::new(*addr, Duration::from_millis(2), 1024, 1024),
Box::new(tm_source), Box::new(tm_source),
Box::new(tc_receiver), Box::new(tc_receiver),
Box::new(packet_id_lookup) Box::new(packet_id_lookup),
) )
.expect("TCP server generation failed") .expect("TCP server generation failed")
} }
#[test] #[test]
fn test_basic() { fn test_basic_tc_only() {
let auto_port_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0); let auto_port_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 0);
let tc_receiver = SyncTcCacher::default(); let tc_receiver = SyncTcCacher::default();
let tm_source = SyncTmSource::default(); let tm_source = SyncTmSource::default();
let mut packet_id_lookup = HashSet::new(); let mut packet_id_lookup = HashSet::new();
packet_id_lookup.insert(PACKET_ID_0); packet_id_lookup.insert(TEST_PACKET_ID_0);
let server = generic_tmtc_server(&auto_port_addr, tc_receiver, tm_source, packet_id_lookup); let mut tcp_server =
generic_tmtc_server(&auto_port_addr, tc_receiver.clone(), tm_source, packet_id_lookup);
let dest_addr = tcp_server let dest_addr = tcp_server
.local_addr() .local_addr()
.expect("retrieving dest addr failed"); .expect("retrieving dest addr failed");
@ -188,5 +205,29 @@ mod tests {
assert_eq!(conn_result.num_sent_tms, 0); assert_eq!(conn_result.num_sent_tms, 0);
set_if_done.store(true, Ordering::Relaxed); set_if_done.store(true, Ordering::Relaxed);
}); });
let mut sph = SpHeader::tc_unseg(TEST_APID_0, 0, 0).unwrap();
let ping_tc = PusTcCreator::new_simple(&mut sph, 17, 1, None, true);
let mut buffer: [u8; 32] = [0; 32];
let packet_len_ping = ping_tc
.write_to_bytes(&mut buffer)
.expect("writing packet failed");
let mut stream = TcpStream::connect(dest_addr).expect("connecting to TCP server failed");
stream
.write_all(&buffer[..packet_len_ping])
.expect("writing to TCP server failed");
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.
let tc_queue = tc_receiver.tc_queue.lock().unwrap();
assert_eq!(tc_queue.len(), 1);
} }
} }