ops-sat-rs/src/config.rs

237 lines
8.2 KiB
Rust
Raw Normal View History

2024-04-10 17:13:29 +02:00
use lazy_static::lazy_static;
2024-04-09 13:52:02 +02:00
use num_enum::{IntoPrimitive, TryFromPrimitive};
2024-04-15 12:16:01 +02:00
use satrs::spacepackets::PacketId;
2024-04-09 13:52:02 +02:00
use satrs_mib::res_code::ResultU16Info;
use satrs_mib::resultcode;
2024-04-10 14:59:34 +02:00
use std::net::Ipv4Addr;
2024-04-10 17:13:29 +02:00
use std::path::{Path, PathBuf};
2024-04-08 17:29:18 +02:00
2024-04-09 17:07:39 +02:00
pub const STOP_FILE_NAME: &str = "stop-experiment";
2024-04-19 17:40:38 +02:00
pub const CONFIG_FILE_NAME: &str = "exp278.toml";
2024-04-22 16:56:49 +02:00
pub const HOME_FOLDER_EXPERIMENT: &str = "/home/exp278";
2024-04-16 15:08:34 +02:00
pub const LOG_FOLDER: &str = "logs";
2024-04-09 17:07:39 +02:00
2024-04-08 17:29:18 +02:00
pub const OBSW_SERVER_ADDR: Ipv4Addr = Ipv4Addr::UNSPECIFIED;
pub const SERVER_PORT: u16 = 7301;
2024-04-13 15:16:53 +02:00
pub const TCP_SPP_SERVER_PORT: u16 = 4096;
2024-04-10 14:59:34 +02:00
pub const EXPERIMENT_ID: u32 = 278;
pub const EXPERIMENT_APID: u16 = 1024 + EXPERIMENT_ID as u16;
2024-04-15 12:16:01 +02:00
pub const EXPERIMENT_PACKET_ID: PacketId = PacketId::new_for_tc(true, EXPERIMENT_APID);
2024-04-16 09:59:31 +02:00
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.
2024-04-19 17:40:38 +02:00
pub const SPP_CLIENT_WIRETAPPING_RX: bool = true;
pub const SPP_CLIENT_WIRETAPPING_TX: bool = true;
2024-04-08 17:29:18 +02:00
2024-04-09 11:18:54 +02:00
#[derive(Copy, Clone, PartialEq, Eq, Debug, TryFromPrimitive, IntoPrimitive)]
#[repr(u8)]
pub enum CustomPusServiceId {
Mode = 200,
Health = 201,
}
#[derive(Debug)]
pub enum GroupId {
Tmtc = 0,
Hk = 1,
Mode = 2,
2024-04-10 15:37:24 +02:00
Action = 3,
2024-04-09 11:18:54 +02:00
}
2024-04-10 17:13:29 +02:00
lazy_static! {
pub static ref HOME_PATH: PathBuf = {
let mut home_path = PathBuf::new();
2024-04-24 16:45:38 +02:00
let home_path_default = homedir::get_my_home()
.expect("Getting home dir from OS failed.")
.expect("No home dir found.");
home_path.push(if Path::new(HOME_FOLDER_EXPERIMENT).exists() {
HOME_FOLDER_EXPERIMENT
2024-04-10 17:13:29 +02:00
} else {
2024-04-24 16:45:38 +02:00
home_path_default
.to_str()
.expect("Error converting to string.")
2024-04-10 17:13:29 +02:00
});
home_path
};
}
2024-04-19 17:40:38 +02:00
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::<toml::Table>() {
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
}
}
}
}
}
2024-04-09 11:18:54 +02:00
pub mod tmtc_err {
use super::*;
2024-04-09 13:52:02 +02:00
use satrs::res_code::ResultU16;
2024-04-09 11:18:54 +02:00
#[resultcode]
pub const INVALID_PUS_SERVICE: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 0);
#[resultcode]
pub const INVALID_PUS_SUBSERVICE: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 1);
#[resultcode]
pub const PUS_SERVICE_NOT_IMPLEMENTED: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 2);
#[resultcode]
pub const PUS_SUBSERVICE_NOT_IMPLEMENTED: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 3);
#[resultcode]
pub const UNKNOWN_TARGET_ID: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 4);
#[resultcode]
pub const ROUTING_ERROR: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 5);
#[resultcode(info = "Request timeout for targeted PUS request. P1: Request ID. P2: Target ID")]
pub const REQUEST_TIMEOUT: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 6);
#[resultcode(
2024-04-09 13:52:02 +02:00
info = "Not enough data inside the TC application data field. Optionally includes: \
2024-04-09 11:18:54 +02:00
8 bytes of failure data containing 2 failure parameters, \
P1 (u32 big endian): Expected data length, P2: Found data length"
)]
pub const NOT_ENOUGH_APP_DATA: ResultU16 = ResultU16::new(GroupId::Tmtc as u8, 2);
pub const TMTC_RESULTS: &[ResultU16Info] = &[
INVALID_PUS_SERVICE_EXT,
INVALID_PUS_SUBSERVICE_EXT,
PUS_SERVICE_NOT_IMPLEMENTED_EXT,
UNKNOWN_TARGET_ID_EXT,
ROUTING_ERROR_EXT,
NOT_ENOUGH_APP_DATA_EXT,
];
}
2024-04-10 15:37:24 +02:00
pub mod action_err {
use super::*;
use satrs::res_code::ResultU16;
#[resultcode]
pub const INVALID_ACTION_ID: ResultU16 = ResultU16::new(GroupId::Action as u8, 0);
pub const ACTION_RESULTS: &[ResultU16Info] = &[INVALID_ACTION_ID_EXT];
}
2024-04-08 17:29:18 +02:00
pub mod components {
use satrs::request::UniqueApidTargetId;
2024-04-10 14:59:34 +02:00
use super::EXPERIMENT_APID;
2024-04-08 17:29:18 +02:00
// Component IDs for components with the PUS APID.
#[derive(Copy, Clone, PartialEq, Eq)]
2024-04-10 14:59:34 +02:00
pub enum UniqueId {
Controller = 0,
PusEventManagement = 1,
PusRouting = 2,
PusTest = 3,
PusAction = 4,
PusMode = 5,
PusHk = 6,
2024-04-15 12:16:01 +02:00
UdpServer = 7,
TcpServer = 8,
TcpSppClient = 9,
CameraHandler = 10,
2024-04-08 17:29:18 +02:00
}
2024-04-10 14:59:34 +02:00
pub const CONTROLLER_ID: UniqueApidTargetId =
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::Controller as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_ACTION_SERVICE: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusAction as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_EVENT_MANAGEMENT: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusEventManagement as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_ROUTING_SERVICE: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusRouting as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_TEST_SERVICE: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusTest as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_MODE_SERVICE: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusMode as u32);
2024-04-08 17:29:18 +02:00
pub const PUS_HK_SERVICE: UniqueApidTargetId =
2024-04-10 14:59:34 +02:00
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::PusHk as u32);
2024-04-15 12:16:01 +02:00
pub const UDP_SERVER: UniqueApidTargetId =
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::UdpServer as u32);
pub const TCP_SERVER: UniqueApidTargetId =
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::TcpServer as u32);
2024-04-15 16:42:48 +02:00
pub const TCP_SPP_CLIENT: UniqueApidTargetId =
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::TcpSppClient as u32);
pub const CAMERA_HANDLER: UniqueApidTargetId =
UniqueApidTargetId::new(EXPERIMENT_APID, UniqueId::CameraHandler as u32);
2024-04-08 17:29:18 +02:00
}
pub mod tasks {
2024-04-19 17:40:38 +02:00
use std::time::Duration;
2024-04-08 17:29:18 +02:00
pub const FREQ_MS_UDP_TMTC: u64 = 200;
pub const FREQ_MS_EVENT_HANDLING: u64 = 400;
pub const FREQ_MS_AOCS: u64 = 500;
pub const FREQ_MS_PUS_STACK: u64 = 200;
2024-04-09 17:07:39 +02:00
pub const FREQ_MS_CTRL: u64 = 400;
2024-04-10 12:47:26 +02:00
2024-04-19 17:40:38 +02:00
pub const STOP_CHECK_FREQUENCY_MS: u64 = 400;
pub const STOP_CHECK_FREQUENCY: Duration = Duration::from_millis(STOP_CHECK_FREQUENCY_MS);
2024-04-08 17:29:18 +02:00
}