diff --git a/src/interface/tcp_spp_client.rs b/src/interface/tcp_spp_client.rs index 4044dc6..52a744d 100644 --- a/src/interface/tcp_spp_client.rs +++ b/src/interface/tcp_spp_client.rs @@ -354,7 +354,39 @@ mod tests { } Err(()) } + + fn try_reading_one_packet( + &mut self, + stream: &mut TcpStream, + limit: u32, + read_buf: &mut [u8], + ) -> usize { + let mut read_data = 0; + + for _ in 0..limit { + match stream.read(read_buf) { + Ok(0) => {} + Ok(len) => { + // assert_eq!(&tm_buf, &read_buf[0..len]); + // read_bufd_expected_data = true; + read_data = len; + break; + } + Err(e) => { + if e.kind() == io::ErrorKind::WouldBlock { + continue; + } + panic!("TCP server read error: {:?}", e); + } + } + if read_data > 0 { + break; + } + } + read_data + } } + // This test just simplifies that the client properly connects to a server. #[test] fn basic_client_test() { @@ -392,7 +424,6 @@ mod tests { .write_to_be_bytes(&mut buf) .expect("writing TM failed"); let jh0 = thread::spawn(move || { - let mut read_expected_data = false; let mut read_buf: [u8; 64] = [0; 64]; let mut stream = tcp_server .attempt_connection(3) @@ -400,27 +431,12 @@ mod tests { stream .set_read_timeout(Some(Duration::from_millis(10))) .expect("setting read timeout failed"); - for _ in 0..5 { - match stream.read(&mut read_buf) { - Ok(0) => {} - Ok(len) => { - assert_eq!(&buf, &read_buf[0..len]); - read_expected_data = true; - break; - } - Err(e) => { - if e.kind() == io::ErrorKind::WouldBlock { - continue; - } - panic!("TCP server read error: {:?}", e); - } - } - if read_expected_data { - break; - } - } - if !read_expected_data { + + let read_bytes = tcp_server.try_reading_one_packet(&mut stream, 5, &mut read_buf); + if read_bytes == 0 { panic!("did not receive expected data"); + } else { + assert_eq!(&buf, &read_buf[0..read_bytes]); } }); tm_tcp_client_tx @@ -483,4 +499,63 @@ mod tests { } jh0.join().unwrap(); } + + // Test that the client can both read telecommands from the server and send back + // telemetry to the server. + #[test] + fn basic_client_tmtc_test() { + let (tc_source_tx, tc_source_rx) = mpsc::channel(); + let (tm_tcp_client_tx, tm_tcp_client_rx) = mpsc::channel(); + let mut tcp_server = TcpServerTestbench::new(); + let local_addr = tcp_server.local_addr(); + let mut tc_buf: [u8; 8] = [0; 8]; + let mut tm_buf: [u8; 8] = [0; 8]; + TEST_TC + .write_to_be_bytes(&mut tc_buf) + .expect("writing TM failed"); + TEST_TM + .write_to_be_bytes(&mut tm_buf) + .expect("writing TM failed"); + let jh0 = thread::spawn(move || { + let mut read_buf: [u8; 64] = [0; 64]; + let mut stream = tcp_server + .attempt_connection(3) + .expect("no client connection detected"); + stream + .set_read_timeout(Some(Duration::from_millis(10))) + .expect("setting read timeout failed"); + stream.write_all(&tc_buf).expect("writing TC failed"); + let read_bytes = tcp_server.try_reading_one_packet(&mut stream, 5, &mut read_buf); + if read_bytes == 0 { + panic!("did not receive expected data"); + } else { + assert_eq!(&tm_buf, &read_buf[0..read_bytes]); + } + }); + tm_tcp_client_tx + .send(PacketAsVec::new(0, tm_buf.to_vec())) + .unwrap(); + + let mut spp_client = TcpSppClientStd::new( + 1, + tc_source_tx, + tm_tcp_client_rx, + VALID_IDS, + Duration::from_millis(30), + local_addr.port(), + ) + .expect("creating TCP SPP client failed"); + let mut received_packet = false; + (0..3).for_each(|_| { + spp_client.operation().unwrap(); + if let Ok(packet) = tc_source_rx.try_recv() { + assert_eq!(packet.packet, tc_buf.to_vec()); + received_packet = true; + } + }); + if !received_packet { + panic!("did not receive expected data"); + } + jh0.join().unwrap(); + } }