mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
fix(safety,openxr,dvr,config): harden RC/firmware failsafes, OpenXR lifecycle, DVR StreamCopy, relay & persistent config
This commit is contained in:
1 parent
8028680f07
commit
d82a65d9e6
24 files changed
+2246
-638
No files matched your search
+310
-1
@@ -1,4 +1,8 @@
|
||||
use clap::Parser;
|
||||
use anyhow::{Context, Result};
|
||||
use clap::{parser::ValueSource, CommandFactory, FromArgMatches, Parser};
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
#[command(author, version, about)]
|
||||
@@ -192,3 +196,308 @@ pub struct AppConfig {
|
||||
#[arg(long, default_value = "steamframe_fpv.log")]
|
||||
pub log_file: String,
|
||||
}
|
||||
|
||||
impl AppConfig {
|
||||
/// Retourne le chemin XDG du fichier de configuration persistant (`~/.config/steamframe-fpv/config.toml`).
|
||||
pub fn default_config_path() -> Option<PathBuf> {
|
||||
if let Ok(xdg) = std::env::var("XDG_CONFIG_HOME") {
|
||||
let trimmed = xdg.trim();
|
||||
if !trimmed.is_empty() {
|
||||
return Some(PathBuf::from(trimmed).join("steamframe-fpv/config.toml"));
|
||||
}
|
||||
}
|
||||
std::env::var("HOME")
|
||||
.ok()
|
||||
.filter(|h| !h.trim().is_empty())
|
||||
.map(|h| PathBuf::from(h).join(".config/steamframe-fpv/config.toml"))
|
||||
}
|
||||
|
||||
/// Parse les arguments CLI en appliquant les réglages persistés de `config.toml`
|
||||
/// uniquement pour les options non explicitement fournies en ligne de commande.
|
||||
pub fn parse_with_saved() -> Self {
|
||||
let matches = Self::command().get_matches();
|
||||
let mut cfg = Self::from_arg_matches(&matches).unwrap_or_else(|e| e.exit());
|
||||
if let Some(path) = Self::default_config_path() {
|
||||
if path.exists() {
|
||||
if let Ok(raw) = fs::read_to_string(&path) {
|
||||
let map = Self::parse_toml_kv(&raw);
|
||||
cfg.apply_saved_map(&map, |id| {
|
||||
matches.value_source(id) == Some(ValueSource::CommandLine)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
cfg
|
||||
}
|
||||
|
||||
/// Sérialise les réglages pilote au format TOML propre et lisible.
|
||||
pub fn to_toml_string(&self) -> String {
|
||||
fn esc(s: &str) -> String {
|
||||
s.replace('\\', "\\\\").replace('"', "\\\"")
|
||||
}
|
||||
format!(
|
||||
"# SteamFrame FPV — Configuration persistante (auto-générée)\n\
|
||||
# Les arguments en ligne de commande ont toujours priorité sur ce fichier.\n\n\
|
||||
usb_video_mode = \"{}\"\n\
|
||||
uvc_device = \"{}\"\n\
|
||||
usb_bind_ip = \"{}\"\n\
|
||||
radxa_vrx_ip = \"{}\"\n\
|
||||
wifi_channel = {}\n\
|
||||
vrx_video_profile = \"{}\"\n\
|
||||
video_udp_port = {}\n\
|
||||
codec = \"{}\"\n\
|
||||
stream_transport = \"{}\"\n\
|
||||
video_width = {}\n\
|
||||
video_height = {}\n\n\
|
||||
projection_mode = \"{}\"\n\
|
||||
camera_fov_deg = {:.2}\n\
|
||||
lens_k1 = {:.4}\n\
|
||||
lens_k2 = {:.4}\n\
|
||||
screen_distance_m = {:.3}\n\
|
||||
screen_width_m = {:.3}\n\n\
|
||||
telemetry_enabled = {}\n\
|
||||
telemetry_udp_port = {}\n\
|
||||
head_tracking_enabled = {}\n\
|
||||
nomad_ip = \"{}\"\n\
|
||||
nomad_port = {}\n\
|
||||
ht_protocol = \"{}\"\n\
|
||||
max_pan_deg = {:.2}\n\
|
||||
max_tilt_deg = {:.2}\n\
|
||||
max_roll_deg = {:.2}\n\n\
|
||||
g29_enabled = {}\n\
|
||||
g29_udp_port = {}\n\
|
||||
control_source = \"{}\"\n\
|
||||
controller_device = \"{}\"\n\
|
||||
controller_expo = {:.3}\n\
|
||||
controller_deadzone = {:.3}\n\
|
||||
controller_invert_steering = {}\n\
|
||||
controller_vibration = {}\n\
|
||||
gaz_avant_min = {}\n\
|
||||
gear_1_max = {}\n\
|
||||
gear_2_max = {}\n\
|
||||
gear_3_max = {}\n\
|
||||
gaz_arriere_min = {}\n\
|
||||
gear_reverse_max = {}\n\n\
|
||||
dvr_dir = \"{}\"\n\
|
||||
dvr_format = \"{}\"\n\
|
||||
dvr_mode = \"{}\"\n",
|
||||
esc(&self.usb_video_mode),
|
||||
esc(&self.uvc_device),
|
||||
esc(&self.usb_bind_ip),
|
||||
esc(&self.radxa_vrx_ip),
|
||||
self.wifi_channel,
|
||||
esc(&self.vrx_video_profile),
|
||||
self.video_udp_port,
|
||||
esc(&self.codec),
|
||||
esc(&self.stream_transport),
|
||||
self.video_width,
|
||||
self.video_height,
|
||||
esc(&self.projection_mode),
|
||||
self.camera_fov_deg,
|
||||
self.lens_k1,
|
||||
self.lens_k2,
|
||||
self.screen_distance_m,
|
||||
self.screen_width_m,
|
||||
self.telemetry_enabled,
|
||||
self.telemetry_udp_port,
|
||||
self.head_tracking_enabled,
|
||||
esc(&self.nomad_ip),
|
||||
self.nomad_port,
|
||||
esc(&self.ht_protocol),
|
||||
self.max_pan_deg,
|
||||
self.max_tilt_deg,
|
||||
self.max_roll_deg,
|
||||
self.g29_enabled,
|
||||
self.g29_udp_port,
|
||||
esc(&self.control_source),
|
||||
esc(&self.controller_device),
|
||||
self.controller_expo,
|
||||
self.controller_deadzone,
|
||||
self.controller_invert_steering,
|
||||
self.controller_vibration,
|
||||
self.gaz_avant_min,
|
||||
self.gear_1_max,
|
||||
self.gear_2_max,
|
||||
self.gear_3_max,
|
||||
self.gaz_arriere_min,
|
||||
self.gear_reverse_max,
|
||||
esc(&self.dvr_dir),
|
||||
esc(&self.dvr_format),
|
||||
esc(&self.dvr_mode),
|
||||
)
|
||||
}
|
||||
|
||||
/// Sauvegarde atomique dans `~/.config/steamframe-fpv/config.toml` (fichier temporaire + rename).
|
||||
pub fn save_to_disk(&self) -> Result<PathBuf> {
|
||||
let path = Self::default_config_path()
|
||||
.ok_or_else(|| anyhow::anyhow!("Impossible de déterminer XDG_CONFIG_HOME / HOME"))?;
|
||||
self.save_to_path(&path)?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn save_to_path(&self, path: &Path) -> Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).with_context(|| {
|
||||
format!("Impossible de créer le dossier de config {:?}", parent)
|
||||
})?;
|
||||
}
|
||||
let tmp_path = path.with_extension("toml.tmp");
|
||||
fs::write(&tmp_path, self.to_toml_string())
|
||||
.with_context(|| format!("Échec écriture config temporaire {:?}", tmp_path))?;
|
||||
fs::rename(&tmp_path, path)
|
||||
.with_context(|| format!("Échec remplacement config {:?}", path))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Met à jour uniquement un sous-ensemble de clés dans `config.toml` existant (ou crée le fichier)
|
||||
/// — utilisé par le Menu VR pour persister les réglages en vol sans écraser les autres options.
|
||||
pub fn patch_saved_fields(&self) {
|
||||
if let Err(e) = self.save_to_disk() {
|
||||
log::warn!("[Config] Impossible de sauvegarder config.toml : {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_toml_kv(raw: &str) -> HashMap<String, String> {
|
||||
let mut map = HashMap::new();
|
||||
for line in raw.lines() {
|
||||
let trimmed = line.trim();
|
||||
if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with('[') {
|
||||
continue;
|
||||
}
|
||||
if let Some((k, v)) = trimmed.split_once('=') {
|
||||
let key = k.trim().to_string();
|
||||
let mut val = v.trim();
|
||||
// Retirer éventuel commentaire en fin de ligne hors guillemets
|
||||
if !val.starts_with('"') {
|
||||
if let Some((before, _)) = val.split_once('#') {
|
||||
val = before.trim();
|
||||
}
|
||||
map.insert(key, val.to_string());
|
||||
} else if let Some(inner) = val.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
|
||||
{
|
||||
let unescaped = inner.replace("\\\"", "\"").replace("\\\\", "\\");
|
||||
map.insert(key, unescaped);
|
||||
}
|
||||
}
|
||||
}
|
||||
map
|
||||
}
|
||||
|
||||
fn apply_saved_map<F>(&mut self, map: &HashMap<String, String>, is_cli_set: F)
|
||||
where
|
||||
F: Fn(&str) -> bool,
|
||||
{
|
||||
macro_rules! load_str {
|
||||
($field:ident) => {
|
||||
if !is_cli_set(stringify!($field)) {
|
||||
if let Some(v) = map.get(stringify!($field)) {
|
||||
self.$field = v.clone();
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
macro_rules! load_parse {
|
||||
($field:ident) => {
|
||||
if !is_cli_set(stringify!($field)) {
|
||||
if let Some(v) = map.get(stringify!($field)) {
|
||||
if let Ok(parsed) = v.parse() {
|
||||
self.$field = parsed;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
load_str!(usb_video_mode);
|
||||
load_str!(uvc_device);
|
||||
load_str!(usb_bind_ip);
|
||||
load_str!(radxa_vrx_ip);
|
||||
load_parse!(wifi_channel);
|
||||
load_str!(vrx_video_profile);
|
||||
load_parse!(video_udp_port);
|
||||
load_str!(codec);
|
||||
load_str!(stream_transport);
|
||||
load_parse!(video_width);
|
||||
load_parse!(video_height);
|
||||
|
||||
load_str!(projection_mode);
|
||||
load_parse!(camera_fov_deg);
|
||||
load_parse!(lens_k1);
|
||||
load_parse!(lens_k2);
|
||||
load_parse!(screen_distance_m);
|
||||
load_parse!(screen_width_m);
|
||||
|
||||
load_parse!(telemetry_enabled);
|
||||
load_parse!(telemetry_udp_port);
|
||||
load_parse!(head_tracking_enabled);
|
||||
load_str!(nomad_ip);
|
||||
load_parse!(nomad_port);
|
||||
load_str!(ht_protocol);
|
||||
load_parse!(max_pan_deg);
|
||||
load_parse!(max_tilt_deg);
|
||||
load_parse!(max_roll_deg);
|
||||
|
||||
load_parse!(g29_enabled);
|
||||
load_parse!(g29_udp_port);
|
||||
load_str!(control_source);
|
||||
load_str!(controller_device);
|
||||
load_parse!(controller_expo);
|
||||
load_parse!(controller_deadzone);
|
||||
load_parse!(controller_invert_steering);
|
||||
load_parse!(controller_vibration);
|
||||
load_parse!(gaz_avant_min);
|
||||
load_parse!(gear_1_max);
|
||||
load_parse!(gear_2_max);
|
||||
load_parse!(gear_3_max);
|
||||
load_parse!(gaz_arriere_min);
|
||||
load_parse!(gear_reverse_max);
|
||||
|
||||
load_str!(dvr_dir);
|
||||
load_str!(dvr_format);
|
||||
load_str!(dvr_mode);
|
||||
|
||||
// Sécurité bornes numériques
|
||||
self.video_width = self.video_width.clamp(320, 3840);
|
||||
self.video_height = self.video_height.clamp(240, 2160);
|
||||
self.camera_fov_deg = self.camera_fov_deg.clamp(60.0, 180.0);
|
||||
self.lens_k1 = self.lens_k1.clamp(-0.50, 0.25);
|
||||
self.screen_distance_m = self.screen_distance_m.clamp(0.5, 5.0);
|
||||
self.controller_vibration = self.controller_vibration.min(100);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_config_toml_roundtrip_and_cli_override() {
|
||||
let mut cfg = AppConfig::parse_from(["steamframe-fpv"]);
|
||||
cfg.projection_mode = "cylinder".to_string();
|
||||
cfg.screen_distance_m = 2.2;
|
||||
cfg.lens_k1 = -0.22;
|
||||
cfg.camera_fov_deg = 120.0;
|
||||
cfg.control_source = "steam".to_string();
|
||||
cfg.controller_invert_steering = true;
|
||||
cfg.gear_3_max = 1350;
|
||||
cfg.wifi_channel = 157;
|
||||
cfg.telemetry_enabled = true;
|
||||
|
||||
let toml_text = cfg.to_toml_string();
|
||||
let map = AppConfig::parse_toml_kv(&toml_text);
|
||||
|
||||
let mut loaded = AppConfig::parse_from(["steamframe-fpv", "--wifi-channel", "149"]);
|
||||
loaded.apply_saved_map(&map, |id| id == "wifi_channel");
|
||||
|
||||
assert_eq!(loaded.projection_mode, "cylinder");
|
||||
assert!((loaded.screen_distance_m - 2.2).abs() < 0.01);
|
||||
assert!((loaded.lens_k1 - (-0.22)).abs() < 0.001);
|
||||
assert!((loaded.camera_fov_deg - 120.0).abs() < 0.01);
|
||||
assert_eq!(loaded.control_source, "steam");
|
||||
assert!(loaded.controller_invert_steering);
|
||||
assert_eq!(loaded.gear_3_max, 1350);
|
||||
assert!(loaded.telemetry_enabled);
|
||||
// L'argument CLI explicite doit garder priorité sur config.toml
|
||||
assert_eq!(loaded.wifi_channel, 149);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user