From 948c42c7c25364e006cc43d7abec8b7ae8f87aec Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 18 Apr 2024 15:22:10 +0200 Subject: [PATCH] server port is configurable now --- .gitignore | 1 + Cargo.lock | 55 ++++++++++++++++++++++-- Cargo.toml | 1 + src/config.rs | 75 +++++++++++++++++++++++++++++++++ src/interface/tcp_spp_client.rs | 6 +-- src/main.rs | 5 +++ templates/exp278.toml | 2 + 7 files changed, 138 insertions(+), 7 deletions(-) create mode 100644 templates/exp278.toml diff --git a/.gitignore b/.gitignore index f9e18df..6624bed 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ # Ignore logs folder generared by application. /logs +/exp278.toml diff --git a/Cargo.lock b/Cargo.lock index 7772ac0..232bf65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -491,6 +491,7 @@ dependencies = [ "satrs-mib", "strum", "thiserror", + "toml", ] [[package]] @@ -518,7 +519,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6d37c51ca738a55da99dc0c4a34860fd675453b8b36209178c2249bb13651284" dependencies = [ - "toml_edit", + "toml_edit 0.21.1", ] [[package]] @@ -591,9 +592,9 @@ checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1" [[package]] name = "satrs" -version = "0.2.0-rc.1" +version = "0.2.0-rc.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d6f3286d35464fdc75dc846b663aaad4a81437a50e623053b1b4d481d782cd0" +checksum = "6aa9241e4d6cb0cc395927cfe653d8bc4a9cb6b2c27f28fec713d5e6ceb0ba23" dependencies = [ "bus", "cobs", @@ -680,6 +681,15 @@ dependencies = [ "syn 2.0.59", ] +[[package]] +name = "serde_spanned" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb3622f419d1296904700073ea6cc23ad690adbd66f13ea683df73298736f0c1" +dependencies = [ + "serde", +] + [[package]] name = "smallvec" version = "0.6.14" @@ -785,11 +795,26 @@ dependencies = [ "syn 2.0.59", ] +[[package]] +name = "toml" +version = "0.8.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e9dd1545e8208b4a5af1aa9bbd0b4cf7e9ea08fabc5d0a5c67fcaafa17433aa3" +dependencies = [ + "serde", + "serde_spanned", + "toml_datetime", + "toml_edit 0.22.9", +] + [[package]] name = "toml_datetime" version = "0.6.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3550f4e9685620ac18a50ed434eb3aec30db8ba93b0287467bca5826ea25baf1" +dependencies = [ + "serde", +] [[package]] name = "toml_edit" @@ -799,7 +824,20 @@ checksum = "6a8534fd7f78b5405e860340ad6575217ce99f38d4d5c8f2442cb5ecb50090e1" dependencies = [ "indexmap", "toml_datetime", - "winnow", + "winnow 0.5.40", +] + +[[package]] +name = "toml_edit" +version = "0.22.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e40bb779c5187258fd7aad0eb68cb8706a0a81fa712fbea808ab43c4b8374c4" +dependencies = [ + "indexmap", + "serde", + "serde_spanned", + "toml_datetime", + "winnow 0.6.6", ] [[package]] @@ -1037,6 +1075,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "winnow" +version = "0.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0c976aaaa0e1f90dbb21e9587cdaf1d9679a1cde8875c0d6bd83ab96a208352" +dependencies = [ + "memchr", +] + [[package]] name = "zerocopy" version = "0.7.32" diff --git a/Cargo.toml b/Cargo.toml index f724140..00db36a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] fern = "0.6" +toml = "0.8" chrono = "0.4" log = "0.4" lazy_static = "1" diff --git a/src/config.rs b/src/config.rs index 4ee0a0e..2b1a0cf 100644 --- a/src/config.rs +++ b/src/config.rs @@ -8,6 +8,7 @@ use std::net::Ipv4Addr; use std::path::{Path, PathBuf}; pub const STOP_FILE_NAME: &str = "stop-experiment"; +pub const CONFIG_FILE_NAME: &str = "exp278.toml"; pub const HOME_FOLER_EXPERIMENT: &str = "/home/exp278"; pub const LOG_FOLDER: &str = "logs"; @@ -51,6 +52,80 @@ lazy_static! { }; } +pub mod cfg_file { + use std::{ + fs::File, + io::Read, + path::{Path, PathBuf}, + }; + + use super::{CONFIG_FILE_NAME, HOME_PATH, TCP_SPP_SERVER_PORT}; + + pub const SPP_CLIENT_PORT_CFG_KEY: &str = "tcp_spp_server_port"; + + #[derive(Debug)] + pub struct AppCfg { + pub tcp_spp_server_port: u16, + } + + impl Default for AppCfg { + fn default() -> Self { + Self { + tcp_spp_server_port: TCP_SPP_SERVER_PORT, + } + } + } + + pub fn create_app_config() -> AppCfg { + let mut cfg_path = HOME_PATH.clone(); + cfg_path.push(CONFIG_FILE_NAME); + let cfg_path_home = cfg_path.as_path(); + let relevant_path = if Path::new(CONFIG_FILE_NAME).exists() { + Some(PathBuf::from(Path::new(CONFIG_FILE_NAME))) + } else if cfg_path_home.exists() { + Some(PathBuf::from(cfg_path_home)) + } else { + None + }; + + let mut app_cfg = AppCfg::default(); + if relevant_path.is_none() { + log::warn!("No config file found, using default values"); + return app_cfg; + } + let relevant_path = relevant_path.unwrap(); + match File::open(relevant_path.as_path()) { + Ok(mut file) => { + let mut toml_str = String::new(); + match file.read_to_string(&mut toml_str) { + Ok(_size) => match toml_str.parse::() { + Ok(table) => { + handle_config_file_table(table, &mut app_cfg); + } + Err(e) => log::error!("error parsing TOML config file: {e}"), + }, + Err(e) => log::error!("error reading TOML config file: {e}"), + } + } + Err(e) => log::error!("error opening TOML config file: {e}"), + } + app_cfg + } + + #[allow(clippy::collapsible_match)] + pub fn handle_config_file_table(table: toml::Table, app_cfg: &mut AppCfg) { + if let Some(value) = table.get(SPP_CLIENT_PORT_CFG_KEY) { + if let toml::Value::Integer(port) = value { + if *port < 0 { + log::warn!("invalid port value, is negative"); + } else { + app_cfg.tcp_spp_server_port = *port as u16 + } + } + } + } +} + pub mod tmtc_err { use super::*; use satrs::res_code::ResultU16; diff --git a/src/interface/tcp_spp_client.rs b/src/interface/tcp_spp_client.rs index 847f000..b926a05 100644 --- a/src/interface/tcp_spp_client.rs +++ b/src/interface/tcp_spp_client.rs @@ -6,7 +6,7 @@ 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::{SPP_CLIENT_WIRETAPPING_RX, TCP_SPP_SERVER_PORT}; +use ops_sat_rs::config::SPP_CLIENT_WIRETAPPING_RX; use satrs::encoding::ccsds::parse_buffer_for_ccsds_space_packets; use satrs::queue::GenericSendError; use satrs::spacepackets::PacketId; @@ -43,11 +43,11 @@ impl TcpSppClient { tc_source_tx: mpsc::Sender, tm_tcp_client_rx: mpsc::Receiver, valid_ids: &'static [PacketId], + port: u16, ) -> io::Result { let mut poll = Poll::new()?; let events = Events::with_capacity(128); - let server_addr = - SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 1, 1)), TCP_SPP_SERVER_PORT); + let server_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), port); let client = Self::attempt_connection(&mut poll, &server_addr); if client.is_err() { log::warn!( diff --git a/src/main.rs b/src/main.rs index afc0843..69b6e12 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use std::{ use log::info; use ops_sat_rs::config::{ + cfg_file::create_app_config, components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER}, tasks::{FREQ_MS_CTRL, FREQ_MS_PUS_STACK}, VALID_PACKET_ID_LIST, @@ -42,6 +43,9 @@ fn main() { setup_logger().expect("setting up logging with fern failed"); println!("OPS-SAT Rust Experiment OBSW"); + let app_cfg = create_app_config(); + println!("App Configuration: {:?}", app_cfg); + let stop_signal = Arc::new(AtomicBool::new(false)); let (tc_source_tx, tc_source_rx) = mpsc::channel(); @@ -169,6 +173,7 @@ fn main() { tc_source_tx, tm_tcp_client_rx, VALID_PACKET_ID_LIST, + app_cfg.tcp_spp_server_port, ) .expect("creating TCP SPP client failed"); diff --git a/templates/exp278.toml b/templates/exp278.toml new file mode 100644 index 0000000..48b4f51 --- /dev/null +++ b/templates/exp278.toml @@ -0,0 +1,2 @@ +# On the small flatsat, change this to 9999. +tcp_spp_client_port = 4096