Fix auto_detect_provider to only return AutoDetect on default/unknown hardware

This commit is contained in:
NGnius (Graham) 2022-12-03 13:58:08 -05:00
parent 3766386726
commit 9ef710a966
14 changed files with 96 additions and 27 deletions

View file

@ -7,10 +7,29 @@ use limits_core::json::{Limits, BatteryLimit, CpuLimit, GpuLimit};
use crate::persist::{DriverJson, SettingsJson}; use crate::persist::{DriverJson, SettingsJson};
use crate::settings::{TGeneral, TCpus, TGpu, TBattery, Driver, General}; use crate::settings::{TGeneral, TCpus, TGpu, TBattery, Driver, General};
fn get_limits() -> limits_core::json::Base {
let limits_path = super::utility::limits_path();
match File::open(&limits_path) {
Ok(f) => {
match serde_json::from_reader(f) {
Ok(lim) => lim,
Err(e) => {
log::warn!("Failed to parse limits file `{}`, cannot use for auto_detect: {}", limits_path.display(), e);
limits_core::json::Base::default()
}
}
},
Err(e) => {
log::warn!("Failed to open limits file `{}` (trying force refresh...): {}", limits_path.display(), e);
super::limits_worker::get_limits_blocking()
}
}
}
#[inline] #[inline]
pub fn auto_detect_provider() -> DriverJson { pub fn auto_detect_provider() -> DriverJson {
let provider = auto_detect0(None, crate::utility::settings_dir().join("autodetect.json")) let provider = auto_detect0(None, crate::utility::settings_dir().join("autodetect.json"))
.general .battery
.provider(); .provider();
//log::info!("Detected device automatically, compatible driver: {:?}", provider); //log::info!("Detected device automatically, compatible driver: {:?}", provider);
provider provider
@ -27,22 +46,7 @@ pub fn auto_detect0(settings_opt: Option<SettingsJson>, json_path: std::path::Pa
let dmi_info: String = std::process::Command::new("dmidecode").output().map(|out| String::from_utf8_lossy(&out.stdout).into_owned()).unwrap_or_default(); let dmi_info: String = std::process::Command::new("dmidecode").output().map(|out| String::from_utf8_lossy(&out.stdout).into_owned()).unwrap_or_default();
log::debug!("Read dmidecode:\n{}", dmi_info); log::debug!("Read dmidecode:\n{}", dmi_info);
let limits_path = super::utility::limits_path(); let limits = get_limits();
let limits = match File::open(&limits_path) {
Ok(f) => {
match serde_json::from_reader(f) {
Ok(lim) => lim,
Err(e) => {
log::warn!("Failed to parse limits file `{}`, cannot use for auto_detect: {}", limits_path.display(), e);
limits_core::json::Base::default()
}
}
},
Err(e) => {
log::warn!("Failed to open limits file `{}` (trying force refresh...): {}", limits_path.display(), e);
super::limits_worker::get_limits_blocking()
}
};
// build driver based on limits conditions // build driver based on limits conditions
for conf in limits.configs { for conf in limits.configs {

View file

@ -37,15 +37,7 @@ pub fn spawn() -> JoinHandle<()> {
let json_res: std::io::Result<Base> = response.into_json(); let json_res: std::io::Result<Base> = response.into_json();
match json_res { match json_res {
Ok(new_base) => { Ok(new_base) => {
match std::fs::File::create(&limits_path) { save_base(&new_base, &limits_path);
Ok(f) => {
match serde_json::to_writer_pretty(f, &new_base) {
Ok(_) => log::info!("Successfully updated limits from `{}`, cached at {}", refresh, limits_path.display()),
Err(e) => log::error!("Failed to save limits json to file `{}`: {}", limits_path.display(), e),
}
},
Err(e) => log::error!("Cannot create {}: {}", limits_path.display(), e)
}
}, },
Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e), Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e),
} }
@ -98,7 +90,10 @@ pub fn get_limits_blocking() -> Base {
Ok(response) => { Ok(response) => {
let json_res: std::io::Result<Base> = response.into_json(); let json_res: std::io::Result<Base> = response.into_json();
match json_res { match json_res {
Ok(new_base) => return new_base, Ok(new_base) => {
save_base(&new_base, &limits_path);
return new_base;
},
Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e) Err(e) => log::error!("Cannot parse response from `{}`: {}", refresh, e)
} }
}, },
@ -108,3 +103,17 @@ pub fn get_limits_blocking() -> Base {
Base::default() Base::default()
} }
} }
#[cfg(feature = "online")]
fn save_base(new_base: &Base, path: impl AsRef<std::path::Path>) {
let limits_path = path.as_ref();
match std::fs::File::create(&limits_path) {
Ok(f) => {
match serde_json::to_writer_pretty(f, &new_base) {
Ok(_) => log::info!("Successfully saved new limits to {}", limits_path.display()),
Err(e) => log::error!("Failed to save limits json to file `{}`: {}", limits_path.display(), e),
}
},
Err(e) => log::error!("Cannot create {}: {}", limits_path.display(), e)
}
}

View file

@ -122,4 +122,8 @@ impl TBattery for Battery {
fn read_current_now(&self) -> Option<f64> { fn read_current_now(&self) -> Option<f64> {
None None
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Generic
}
} }

View file

@ -187,6 +187,10 @@ impl TCpus for Cpus {
fn len(&self) -> usize { fn len(&self) -> usize {
self.cpus.len() self.cpus.len()
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Generic
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -99,4 +99,8 @@ impl TGpu for Gpu {
fn slow_memory(&mut self) -> &mut bool { fn slow_memory(&mut self) -> &mut bool {
&mut self.slow_memory &mut self.slow_memory
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Generic
}
} }

View file

@ -305,4 +305,8 @@ impl TBattery for Battery {
} }
} }
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::SteamDeck
}
} }

View file

@ -151,6 +151,10 @@ impl TCpus for Cpus {
fn len(&self) -> usize { fn len(&self) -> usize {
self.cpus.len() self.cpus.len()
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::SteamDeck
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -300,6 +300,10 @@ impl TGpu for Gpu {
fn slow_memory(&mut self) -> &mut bool { fn slow_memory(&mut self) -> &mut bool {
&mut self.slow_memory &mut self.slow_memory
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::SteamDeck
}
} }
#[inline] #[inline]

View file

@ -151,6 +151,10 @@ impl TCpus for Cpus {
fn len(&self) -> usize { fn len(&self) -> usize {
self.cpus.len() self.cpus.len()
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::SteamDeckAdvance
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -298,6 +298,10 @@ impl TGpu for Gpu {
fn slow_memory(&mut self) -> &mut bool { fn slow_memory(&mut self) -> &mut bool {
&mut self.slow_memory &mut self.slow_memory
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::SteamDeckAdvance
}
} }
#[inline] #[inline]

View file

@ -29,6 +29,10 @@ pub trait TGpu: OnResume + OnSet + Debug + Send {
fn get_clock_limits(&self) -> Option<&MinMax<u64>>; fn get_clock_limits(&self) -> Option<&MinMax<u64>>;
fn slow_memory(&mut self) -> &mut bool; fn slow_memory(&mut self) -> &mut bool;
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::AutoDetect
}
} }
pub trait TCpus: OnResume + OnSet + Debug + Send { pub trait TCpus: OnResume + OnSet + Debug + Send {
@ -39,6 +43,10 @@ pub trait TCpus: OnResume + OnSet + Debug + Send {
fn cpus(&mut self) -> Vec<&mut dyn TCpu>; fn cpus(&mut self) -> Vec<&mut dyn TCpu>;
fn len(&self) -> usize; fn len(&self) -> usize;
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::AutoDetect
}
} }
pub trait TCpu: Debug + Send { pub trait TCpu: Debug + Send {
@ -91,4 +99,8 @@ pub trait TBattery: OnResume + OnSet + Debug + Send {
fn read_charge_design(&self) -> Option<f64>; fn read_charge_design(&self) -> Option<f64>;
fn read_current_now(&self) -> Option<f64>; fn read_current_now(&self) -> Option<f64>;
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::AutoDetect
}
} }

View file

@ -63,4 +63,8 @@ impl TBattery for Battery {
fn read_charge_design(&self) -> Option<f64> { None } fn read_charge_design(&self) -> Option<f64> { None }
fn read_current_now(&self) -> Option<f64> { None } fn read_current_now(&self) -> Option<f64> { None }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Unknown
}
} }

View file

@ -150,6 +150,10 @@ impl TCpus for Cpus {
fn len(&self) -> usize { fn len(&self) -> usize {
self.cpus.len() self.cpus.len()
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Unknown
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -86,4 +86,8 @@ impl TGpu for Gpu {
fn slow_memory(&mut self) -> &mut bool { fn slow_memory(&mut self) -> &mut bool {
&mut self.slow_memory &mut self.slow_memory
} }
fn provider(&self) -> crate::persist::DriverJson {
crate::persist::DriverJson::Unknown
}
} }