From fab1859d788c2c8fecce198141b2956ffd86310f Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 26 Apr 2024 19:18:37 +0200 Subject: [PATCH] added host feature --- Cargo.toml | 3 +++ README.md | 2 +- src/config.rs | 63 ++++++++++++++++++++++++++++++++++++----------- src/controller.rs | 14 ++++++++--- src/main.rs | 5 ++-- 5 files changed, 66 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5405a8a..42401d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,9 @@ version = ">=0.1.2, <0.2" env_logger = "0.11" tempfile = "3" +[features] +host = [] + # I don't think we need insane performance. If anything, a small binary is easier to upload # to the satellite. [profile.release] diff --git a/README.md b/README.md index 695a444..e35f4eb 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ You can use the regular cargo workflow for this. ### Running ```sh -cargo run +cargo run --features host ``` ### Testing diff --git a/src/config.rs b/src/config.rs index 0804ffc..4641073 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,5 +1,6 @@ use lazy_static::lazy_static; use num_enum::{IntoPrimitive, TryFromPrimitive}; +use once_cell::sync::OnceCell; use satrs::events::{EventU32TypedSev, SeverityInfo}; use satrs::spacepackets::PacketId; use satrs_mib::res_code::ResultU16Info; @@ -10,8 +11,8 @@ use std::path::{Path, PathBuf}; pub const STOP_FILE_NAME: &str = "stop-experiment"; pub const CONFIG_FILE_NAME: &str = "exp278.toml"; pub const HOME_FOLDER_EXPERIMENT: &str = "/home/exp278"; // also where IMS-100 images are placed -pub const TO_GROUND_FOLDER_EXPERIMENT: &str = "/home/exp278/toGround"; -pub const TO_GROUND_LP_FOLDER_EXPERIMENT: &str = "/home/exp278/toGroundLP"; +pub const TO_GROUND_FOLDER_NAME: &str = "toGround"; +pub const TO_GROUND_LP_FOLDER_NAME: &str = "toGroundLP"; pub const LOG_FOLDER: &str = "logs"; pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED; @@ -27,6 +28,9 @@ pub const VALID_PACKET_ID_LIST: &[PacketId] = &[PacketId::new_for_tc(true, EXPER pub const SPP_CLIENT_WIRETAPPING_RX: bool = false; pub const SPP_CLIENT_WIRETAPPING_TX: bool = false; +pub static TO_GROUND_FOLDER_DIR: OnceCell = OnceCell::new(); +pub static TO_GROUND_LP_FOLDER_DIR: OnceCell = OnceCell::new(); + #[derive(Copy, Clone, PartialEq, Eq, Debug, TryFromPrimitive, IntoPrimitive)] #[repr(u8)] pub enum CustomPusServiceId { @@ -66,6 +70,49 @@ lazy_static! { }; } +pub fn set_up_low_prio_ground_dir() { + #[cfg(feature = "host")] + let mut to_ground_lp_dir = std::env::current_dir().expect("getting current dir failed"); + #[cfg(not(feature = "host"))] + let mut to_ground_lp_dir = HOME_PATH.clone(); + to_ground_lp_dir.push(TO_GROUND_LP_FOLDER_NAME); + if !Path::new(&to_ground_lp_dir).exists() { + log::info!( + "creating low priority to ground directory at {:?}", + to_ground_lp_dir + ); + if std::fs::create_dir_all(&to_ground_lp_dir).is_err() { + log::error!( + "Failed to create low priority to ground directory '{:?}'", + to_ground_lp_dir + ); + } + } + TO_GROUND_LP_FOLDER_DIR + .set(to_ground_lp_dir) + .expect("attemting to set once cell twice"); +} + +pub fn set_up_ground_dir() { + #[cfg(feature = "host")] + let mut to_ground_dir = std::env::current_dir().expect("getting current dir failed"); + #[cfg(not(feature = "host"))] + let mut to_ground_dir = HOME_PATH.clone(); + to_ground_dir.push(TO_GROUND_FOLDER_NAME); + if !Path::new(&to_ground_dir).exists() { + log::info!("creating to ground directory at {:?}", to_ground_dir); + if std::fs::create_dir_all(&to_ground_dir).is_err() { + log::error!( + "Failed to create low priority to ground directory '{:?}'", + to_ground_dir + ); + } + } + TO_GROUND_FOLDER_DIR + .set(to_ground_dir) + .expect("attemting to set once cell twice"); +} + pub mod cfg_file { use std::{ fs::File, @@ -322,15 +369,3 @@ pub mod tasks { pub const STOP_CHECK_FREQUENCY_MS: u64 = 400; pub const STOP_CHECK_FREQUENCY: Duration = Duration::from_millis(STOP_CHECK_FREQUENCY_MS); } - -pub fn create_low_priority_ground_dir() { - log::info!("creating low priority to ground directory"); - if !Path::new(TO_GROUND_LP_FOLDER_EXPERIMENT).exists() - && std::fs::create_dir_all(TO_GROUND_LP_FOLDER_EXPERIMENT).is_err() - { - log::error!( - "Failed to create low priority to ground directory '{}'", - TO_GROUND_LP_FOLDER_EXPERIMENT - ); - } -} diff --git a/src/controller.rs b/src/controller.rs index 2e91915..1436253 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,9 +1,9 @@ use crate::logger::LOGFILE_PATH; use num_enum::TryFromPrimitive; +use ops_sat_rs::config::{action_err::INVALID_ACTION_ID, HOME_FOLDER_EXPERIMENT}; use ops_sat_rs::config::{ - action_err::INVALID_ACTION_ID, HOME_FOLDER_EXPERIMENT, TO_GROUND_FOLDER_EXPERIMENT, + HOME_PATH, STOP_FILE_NAME, TO_GROUND_FOLDER_DIR, TO_GROUND_LP_FOLDER_DIR, }; -use ops_sat_rs::config::{HOME_PATH, STOP_FILE_NAME, TO_GROUND_LP_FOLDER_EXPERIMENT}; use satrs::action::ActionRequestVariant; use satrs::{ action::ActionRequest, @@ -60,8 +60,14 @@ impl Default for ControllerPathCollection { Self { stop_file_home_path: home_path_stop_file, stop_file_tmp_path: tmp_path_stop_file, - to_ground_dir: PathBuf::from(TO_GROUND_FOLDER_EXPERIMENT), - to_ground_low_prio_dir: PathBuf::from(TO_GROUND_LP_FOLDER_EXPERIMENT), + to_ground_dir: TO_GROUND_FOLDER_DIR + .get() + .expect("to ground directory not set") + .clone(), + to_ground_low_prio_dir: TO_GROUND_LP_FOLDER_DIR + .get() + .expect("to ground low prio directory not set") + .clone(), } } } diff --git a/src/main.rs b/src/main.rs index c82ec91..5184c6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,8 +11,8 @@ use log::info; use ops_sat_rs::config::{ cfg_file::create_app_config, components::{CONTROLLER_ID, TCP_SERVER, TCP_SPP_CLIENT, UDP_SERVER}, - create_low_priority_ground_dir, pool::create_sched_tc_pool, + set_up_ground_dir, set_up_low_prio_ground_dir, tasks::{FREQ_MS_CAMERA_HANDLING, FREQ_MS_CTRL, FREQ_MS_PUS_STACK, STOP_CHECK_FREQUENCY}, HOME_PATH, STOP_FILE_NAME, VALID_PACKET_ID_LIST, VERSION, }; @@ -58,7 +58,8 @@ fn main() { let version_str = VERSION.unwrap_or("?"); println!("OPS-SAT Rust Experiment OBSW v{}", version_str); setup_logger().expect("setting up logging with fern failed"); - create_low_priority_ground_dir(); + set_up_low_prio_ground_dir(); + set_up_ground_dir(); let app_cfg = create_app_config(); info!("App Configuration: {:?}", app_cfg); -- 2.43.0