added dynamic discovery of configurations

This commit is contained in:
2026-02-26 16:04:34 +01:00
parent 073414a25e
commit 07ccf7ccc7
13 changed files with 188 additions and 130 deletions

View File

@@ -1,12 +1,20 @@
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::time::Duration;
use std::time::{Duration};
use std::thread;
use std::sync::mpsc;
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict};
use std::collections::HashMap;
use crate::sal::heuristic::schema::{SensorDiscovery, ActuatorDiscovery, Conflict, Discovery};
use tracing::{debug, warn};
/// Registry of dynamically discovered paths for configs and tools.
#[derive(Debug, Clone, Default)]
pub struct PathRegistry {
pub configs: HashMap<String, PathBuf>,
pub tools: HashMap<String, PathBuf>,
}
/// Strongly-typed findings about the current system.
#[derive(Debug, Clone, Default)]
pub struct SystemFactSheet {
@@ -15,21 +23,21 @@ pub struct SystemFactSheet {
pub temp_path: Option<PathBuf>,
pub fan_paths: Vec<PathBuf>,
pub rapl_paths: Vec<PathBuf>,
pub active_conflicts: Vec<String>, // List of conflict IDs found active
pub active_conflicts: Vec<String>,
pub paths: PathRegistry,
}
/// Probes the system for hardware sensors, actuators, and service conflicts.
/// Probes the system for hardware sensors, actuators, service conflicts, and paths.
pub fn discover_facts(
sensors: &SensorDiscovery,
actuators: &ActuatorDiscovery,
discovery: &Discovery,
conflicts: &[Conflict]
) -> SystemFactSheet {
let (vendor, model) = read_dmi_info();
debug!("DMI Identity: Vendor='{}', Model='{}'", vendor, model);
let (temp_path, fan_paths) = discover_hwmon(sensors);
let rapl_paths = discover_rapl(actuators);
let (temp_path, fan_paths) = discover_hwmon(&discovery.sensors);
let rapl_paths = discover_rapl(&discovery.actuators);
let mut active_conflicts = Vec::new();
for conflict in conflicts {
@@ -37,11 +45,13 @@ pub fn discover_facts(
if is_service_active(service) {
debug!("Detected active conflict: {} (Service: {})", conflict.id, service);
active_conflicts.push(conflict.id.clone());
break; // Found one service in this conflict, move to next conflict
break;
}
}
}
let paths = discover_paths(discovery);
SystemFactSheet {
vendor,
model,
@@ -49,9 +59,42 @@ pub fn discover_facts(
fan_paths,
rapl_paths,
active_conflicts,
paths,
}
}
fn discover_paths(discovery: &Discovery) -> PathRegistry {
let mut registry = PathRegistry::default();
// 1. Discover Tools via PATH
for (id, binary_name) in &discovery.tools {
if let Ok(path) = which::which(binary_name) {
debug!("Discovered tool: {} -> {:?}", id, path);
registry.tools.insert(id.clone(), path);
}
}
// 2. Discover Configs via existence check
for (id, candidates) in &discovery.configs {
for candidate in candidates {
let path = PathBuf::from(candidate);
if path.exists() {
debug!("Discovered config: {} -> {:?}", id, path);
registry.configs.insert(id.clone(), path);
break;
}
}
// If not found, use the first one as default if any exist
if !registry.configs.contains_key(id) {
if let Some(first) = candidates.first() {
registry.configs.insert(id.clone(), PathBuf::from(first));
}
}
}
registry
}
/// Reads DMI information from sysfs with a safety timeout.
fn read_dmi_info() -> (String, String) {
let vendor = read_sysfs_with_timeout(Path::new("/sys/class/dmi/id/sys_vendor"), Duration::from_millis(100))

View File

@@ -7,13 +7,13 @@ use crate::sal::traits::PlatformSal;
use crate::sal::dell_xps_9380::DellXps9380Sal;
use crate::sal::generic_linux::GenericLinuxSal;
use crate::sal::heuristic::schema::HardwareDb;
use crate::sal::heuristic::discovery::{discover_facts};
use crate::sal::heuristic::discovery::{discover_facts, SystemFactSheet};
pub struct HeuristicEngine;
impl HeuristicEngine {
/// Loads the hardware database, probes the system, and builds the appropriate SAL.
pub fn detect_and_build() -> Result<Box<dyn PlatformSal>> {
pub fn detect_and_build() -> Result<(Box<dyn PlatformSal>, SystemFactSheet)> {
// 1. Load Hardware DB
let db_path = "assets/hardware_db.toml";
let db_content = fs::read_to_string(db_path)
@@ -24,7 +24,7 @@ impl HeuristicEngine {
.context("Failed to parse hardware_db.toml")?;
// 2. Discover Facts
let facts = discover_facts(&db.discovery.sensors, &db.discovery.actuators, &db.conflicts);
let facts = discover_facts(&db.discovery, &db.conflicts);
info!("System Identity: {} {}", facts.vendor, facts.model);
// 3. Routing Logic
@@ -32,8 +32,8 @@ impl HeuristicEngine {
// --- Special Case: Dell XPS 13 9380 ---
if is_match(&facts.vendor, "(?i)Dell.*") && is_match(&facts.model, "(?i)XPS.*13.*9380.*") {
info!("Specialized SAL Match Found: Dell XPS 13 9380");
let sal = DellXps9380Sal::init().map_err(|e| miette::miette!(e))?;
return Ok(Box::new(sal));
let sal = DellXps9380Sal::init(facts.clone()).map_err(|e| miette::miette!(e))?;
return Ok((Box::new(sal), facts));
}
// --- Fallback: Generic Linux SAL ---
@@ -47,7 +47,7 @@ impl HeuristicEngine {
return Err(miette::miette!("No RAPL power interface discovered. Generic fallback impossible."));
}
Ok(Box::new(GenericLinuxSal::new(facts, db)))
Ok((Box::new(GenericLinuxSal::new(facts.clone(), db)), facts))
}
}

View File

@@ -68,6 +68,8 @@ pub struct Quirk {
pub struct Discovery {
pub sensors: SensorDiscovery,
pub actuators: ActuatorDiscovery,
pub configs: HashMap<String, Vec<String>>,
pub tools: HashMap<String, String>,
}
#[derive(Debug, Deserialize, Clone)]