diff --git a/Cargo.lock b/Cargo.lock index c83fe31..570d8ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -590,7 +590,7 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "satrs" version = "0.2.0-rc.0" -source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#a077c32f3c977f79c9d165e8f3bff66f1d81a669" +source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#29c0961fab78a0e192e5fc918c7e07ccf20d39a6" dependencies = [ "bus", "cobs", @@ -615,7 +615,7 @@ dependencies = [ [[package]] name = "satrs-mib" version = "0.1.1" -source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#a077c32f3c977f79c9d165e8f3bff66f1d81a669" +source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#29c0961fab78a0e192e5fc918c7e07ccf20d39a6" dependencies = [ "csv", "satrs-mib-codegen", @@ -627,7 +627,7 @@ dependencies = [ [[package]] name = "satrs-mib-codegen" version = "0.1.1" -source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#a077c32f3c977f79c9d165e8f3bff66f1d81a669" +source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#29c0961fab78a0e192e5fc918c7e07ccf20d39a6" dependencies = [ "proc-macro2", "quote", @@ -637,7 +637,7 @@ dependencies = [ [[package]] name = "satrs-shared" version = "0.1.3" -source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#a077c32f3c977f79c9d165e8f3bff66f1d81a669" +source = "git+https://egit.irs.uni-stuttgart.de/rust/sat-rs.git?branch=rework-tmtc-modules#29c0961fab78a0e192e5fc918c7e07ccf20d39a6" dependencies = [ "serde", "spacepackets", diff --git a/Cargo.toml b/Cargo.toml index f1c0533..fdfac9a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,3 +29,11 @@ branch = "rework-tmtc-modules" [dev-dependencies] env_logger = "0.11" + +# I don't think we need insane performance. If anything, a small binary is easier to upload +# to the satellite. +[profile.release] +strip = true +opt-level = "z" # Optimize for size. +lto = true +codegen-units = 1 diff --git a/src/config.rs b/src/config.rs index 4295e75..4f0ad9e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,11 @@ pub const TCP_SPP_SERVER_PORT: u16 = 4096; pub const EXPERIMENT_ID: u32 = 278; pub const EXPERIMENT_APID: u16 = 1024 + EXPERIMENT_ID as u16; pub const EXPERIMENT_PACKET_ID: PacketId = PacketId::new_for_tc(true, EXPERIMENT_APID); +pub const VALID_PACKET_ID_LIST: &[PacketId] = &[PacketId::new_for_tc(true, EXPERIMENT_APID)]; + +// TODO: Would be nice if this can be commanded as well.. +/// Can be enabled to print all SPP packets received from the SPP server on port 4096. +pub const SPP_CLIENT_WIRETAPPING_RX: bool = false; #[derive(Copy, Clone, PartialEq, Eq, Debug, TryFromPrimitive, IntoPrimitive)] #[repr(u8)] diff --git a/src/interface/mod.rs b/src/interface/mod.rs index 6439a55..5da763d 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -1,4 +1,36 @@ +use derive_new::new; +use ops_sat_rs::config::SPP_CLIENT_WIRETAPPING_RX; +use satrs::{ + encoding::ccsds::{SpValidity, SpacePacketValidator}, + spacepackets::PacketId, +}; + pub mod can; pub mod tcp_server; pub mod tcp_spp_client; pub mod udp_server; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TcpComponent { + Server, + Client, +} + +#[derive(new, Clone)] +pub struct SimpleSpValidator { + component: TcpComponent, + valid_ids: Vec, +} + +impl SpacePacketValidator for SimpleSpValidator { + fn validate(&self, sp_header: &satrs::spacepackets::SpHeader, raw_buf: &[u8]) -> SpValidity { + if SPP_CLIENT_WIRETAPPING_RX && self.component == TcpComponent::Client { + log::debug!("sp header: {:?}", sp_header); + log::debug!("raw data: {:x?}", raw_buf); + } + if self.valid_ids.contains(&sp_header.packet_id) { + return SpValidity::Valid; + } + SpValidity::Ignore + } +} diff --git a/src/interface/tcp_server.rs b/src/interface/tcp_server.rs index 42b7b40..c3275c6 100644 --- a/src/interface/tcp_server.rs +++ b/src/interface/tcp_server.rs @@ -13,6 +13,8 @@ use satrs::{ tmtc::{PacketAsVec, PacketSource}, }; +use super::{SimpleSpValidator, TcpComponent}; + #[derive(Default, Clone)] pub struct SyncTcpTmSource { tm_queue: Arc>>>, @@ -84,7 +86,7 @@ impl HandledConnectionHandler for ConnectionFinishedHandler { pub type TcpServer = TcpSpacepacketsServer< SyncTcpTmSource, mpsc::Sender, - Vec, + SimpleSpValidator, ConnectionFinishedHandler, (), GenericSendError, @@ -97,14 +99,14 @@ impl TcpTask { cfg: ServerConfig, tm_source: SyncTcpTmSource, tc_sender: mpsc::Sender, - packet_id_lookup: Vec, + valid_ids: Vec, stop_signal: Arc, ) -> Result { Ok(Self(TcpSpacepacketsServer::new( cfg, tm_source, tc_sender, - packet_id_lookup, + SimpleSpValidator::new(TcpComponent::Server, valid_ids), ConnectionFinishedHandler::default(), Some(stop_signal), )?)) @@ -113,7 +115,7 @@ impl TcpTask { pub fn periodic_operation(&mut self) { let result = self .0 - .handle_next_connection(Some(Duration::from_millis(STOP_CHECK_FREQUENCY))); + .handle_all_connections(Some(Duration::from_millis(STOP_CHECK_FREQUENCY))); match result { Ok(_conn_result) => (), Err(e) => { diff --git a/src/interface/tcp_spp_client.rs b/src/interface/tcp_spp_client.rs index 20c15ee..24bedb2 100644 --- a/src/interface/tcp_spp_client.rs +++ b/src/interface/tcp_spp_client.rs @@ -6,13 +6,16 @@ use std::time::Duration; use mio::net::TcpStream; use mio::{Events, Interest, Poll, Token}; use ops_sat_rs::config::tasks::STOP_CHECK_FREQUENCY; -use ops_sat_rs::config::{EXPERIMENT_PACKET_ID, TCP_SPP_SERVER_PORT}; +use ops_sat_rs::config::{SPP_CLIENT_WIRETAPPING_RX, TCP_SPP_SERVER_PORT}; use satrs::encoding::ccsds::parse_buffer_for_ccsds_space_packets; use satrs::queue::GenericSendError; +use satrs::spacepackets::PacketId; use satrs::tmtc::PacketAsVec; use satrs::ComponentId; use thiserror::Error; +use super::{SimpleSpValidator, TcpComponent}; + #[derive(Debug, Error)] pub enum PacketForwardingError { #[error("send error: {0}")] @@ -28,10 +31,15 @@ pub struct TcpSppClient { client: TcpStream, read_buf: [u8; 4096], tc_source_tx: mpsc::Sender, + validator: SimpleSpValidator, } impl TcpSppClient { - pub fn new(id: ComponentId, tc_source_tx: mpsc::Sender) -> io::Result { + pub fn new( + id: ComponentId, + tc_source_tx: mpsc::Sender, + valid_ids: &'static [PacketId], + ) -> io::Result { let poll = Poll::new()?; let events = Events::with_capacity(128); let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 1)), TCP_SPP_SERVER_PORT); @@ -48,6 +56,7 @@ impl TcpSppClient { client, read_buf: [0; 4096], tc_source_tx, + validator: SimpleSpValidator::new(TcpComponent::Client, valid_ids.to_vec()), }) } @@ -85,10 +94,17 @@ impl TcpSppClient { read_bytes: usize, ) -> Result<(), PacketForwardingError> { let mut dummy = 0; + if SPP_CLIENT_WIRETAPPING_RX { + log::debug!( + "received {} bytes on TCP client: {:x?}", + read_bytes, + &self.read_buf[..read_bytes] + ); + } // This parser is able to deal with broken tail packets, but we ignore those for now.. parse_buffer_for_ccsds_space_packets( &mut self.read_buf[..read_bytes], - &[EXPERIMENT_PACKET_ID].as_slice(), + &self.validator, self.id, &self.tc_source_tx, &mut dummy, diff --git a/src/main.rs b/src/main.rs index eb669d3..fb7bfc4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,13 +9,10 @@ use log::info; use ops_sat_rs::config::{ components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER}, tasks::{FREQ_MS_CTRL, FREQ_MS_PUS_STACK}, - EXPERIMENT_APID, + VALID_PACKET_ID_LIST, }; use ops_sat_rs::config::{tasks::FREQ_MS_UDP_TMTC, OBSW_SERVER_ADDR, SERVER_PORT}; -use satrs::{ - hal::std::{tcp_server::ServerConfig, udp_server::UdpTcServer}, - spacepackets::PacketId, -}; +use satrs::hal::std::{tcp_server::ServerConfig, udp_server::UdpTcServer}; use crate::tmtc::tc_source::TcSourceTaskDynamic; use crate::tmtc::tm_sink::TmFunnelDynamic; @@ -147,7 +144,7 @@ fn main() { tcp_server_cfg, sync_tm_tcp_source.clone(), tc_source_tx.clone(), - vec![PacketId::new_for_tc(true, EXPERIMENT_APID)], + VALID_PACKET_ID_LIST.to_vec(), stop_signal.clone(), ) .expect("tcp server creation failed"); @@ -165,8 +162,9 @@ fn main() { stop_signal.clone(), ); - let mut tcp_spp_client = TcpSppClient::new(TCP_SPP_CLIENT.id(), tc_source_tx) - .expect("creating TCP SPP client failed"); + let mut tcp_spp_client = + TcpSppClient::new(TCP_SPP_CLIENT.id(), tc_source_tx, VALID_PACKET_ID_LIST) + .expect("creating TCP SPP client failed"); info!("Starting CTRL task"); let ctrl_stop_signal = stop_signal.clone();