From 175d995a0ea7129800163c54632b4174baf86a0b Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 21 Sep 2023 18:59:00 +0200 Subject: [PATCH] those impls are easy.. --- satrs-core/src/encoding/ccsds.rs | 2 +- .../src/hal/std/tcp_spacepackets_server.rs | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/satrs-core/src/encoding/ccsds.rs b/satrs-core/src/encoding/ccsds.rs index f8f775f..6bea03d 100644 --- a/satrs-core/src/encoding/ccsds.rs +++ b/satrs-core/src/encoding/ccsds.rs @@ -64,7 +64,7 @@ impl PacketIdLookup for [PacketId] { pub fn parse_buffer_for_ccsds_space_packets( buf: &mut [u8], packet_id_lookup: &(impl PacketIdLookup + ?Sized), - tc_receiver: &mut impl ReceivesTcCore, + tc_receiver: &mut (impl ReceivesTcCore + ?Sized), next_write_idx: &mut usize, ) -> Result { *next_write_idx = 0; diff --git a/satrs-core/src/hal/std/tcp_spacepackets_server.rs b/satrs-core/src/hal/std/tcp_spacepackets_server.rs index 8b13789..6c5c3c5 100644 --- a/satrs-core/src/hal/std/tcp_spacepackets_server.rs +++ b/satrs-core/src/hal/std/tcp_spacepackets_server.rs @@ -1 +1,70 @@ +use std::{io::Write, net::TcpStream}; +use alloc::boxed::Box; + +use crate::{ + encoding::{ccsds::PacketIdLookup, parse_buffer_for_ccsds_space_packets}, + tmtc::{ReceivesTc, TmPacketSource}, +}; + +use super::tcp_server::{ConnectionResult, TcpTcParser, TcpTmSender, TcpTmtcError}; + +/// Concrete [TcpTcParser] implementation for the []. +pub struct CcsdsTcParser { + packet_id_lookup: Box, +} + +impl TcpTcParser for CcsdsTcParser { + fn handle_tc_parsing( + &mut self, + tc_buffer: &mut [u8], + tc_receiver: &mut dyn ReceivesTc, + conn_result: &mut ConnectionResult, + current_write_idx: usize, + next_write_idx: &mut usize, + ) -> Result<(), TcpTmtcError> { + // Reader vec full, need to parse for packets. + conn_result.num_received_tcs += parse_buffer_for_ccsds_space_packets( + &mut tc_buffer[..current_write_idx], + self.packet_id_lookup.as_ref(), + tc_receiver.upcast_mut(), + next_write_idx, + ) + .map_err(|e| TcpTmtcError::TcError(e))?; + Ok(()) + } +} + +/// Concrete [TcpTmSender] implementation for the []. +#[derive(Default)] +pub struct CcsdsTmSender {} + +impl TcpTmSender for CcsdsTmSender { + fn handle_tm_sending( + &mut self, + tm_buffer: &mut [u8], + tm_source: &mut dyn TmPacketSource, + conn_result: &mut ConnectionResult, + stream: &mut TcpStream, + ) -> Result> { + let mut tm_was_sent = false; + loop { + // Write TM until TM source is exhausted. For now, there is no limit for the amount + // of TM written this way. + let read_tm_len = tm_source + .retrieve_packet(tm_buffer) + .map_err(|e| TcpTmtcError::TmError(e))?; + + if read_tm_len == 0 { + return Ok(tm_was_sent); + } + tm_was_sent = true; + conn_result.num_sent_tms += 1; + + stream.write_all(&tm_buffer[..read_tm_len])?; + } + } +} + +#[cfg(test)] +mod tests {}