diff --git a/apps/hs/Cargo.toml b/apps/hs/Cargo.toml index 54a6645..00ce86e 100644 --- a/apps/hs/Cargo.toml +++ b/apps/hs/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" path = "src/hs.rs" [features] -default = ["std"] +default = [] std = ["dep:sysinfo", "dep:watchdog-device"] [dependencies] diff --git a/apps/hs/src/hs.rs b/apps/hs/src/hs.rs index 36d1977..bf74dc1 100644 --- a/apps/hs/src/hs.rs +++ b/apps/hs/src/hs.rs @@ -5,28 +5,13 @@ use msg::{HsCmd, HsHk, HsOutData, Msg, MsgKind, TargetMsg}; use rfe::*; extern crate alloc; -use alloc::vec::Vec; mod watchdog; use utils::ManualAuto; pub use watchdog::*; -#[cfg(feature = "std")] -mod hs_std; -#[cfg(feature = "std")] -pub use hs_std::*; - -#[cfg(not(any(feature = "std")))] -mod hs_stub; -#[cfg(not(any(feature = "std")))] -pub use hs_stub::*; - -pub trait SystemInfoGrabber { - fn check_cpu_usage(&mut self) -> Vec; - fn check_mem_usage(&mut self) -> u8; - fn check_fs_usage(&mut self) -> Vec; - fn check_temps(&mut self) -> Vec; -} +mod infograbber; +pub use infograbber::*; #[derive(Debug, Default, Clone)] pub struct HsData { @@ -58,12 +43,12 @@ impl<'a> Hs<'a> { grabber: &'a mut dyn SystemInfoGrabber, mut watchdog: WatchdogRef<'a>, ) -> Self { + watchdog.set_timeout(config.watchdog_timeout); if config.watchdog_enable { watchdog.enable(); } else { watchdog.disable(); } - watchdog.set_timeout(config.watchdog_timeout); Self { data: Default::default(), config, diff --git a/apps/hs/src/hs_std.rs b/apps/hs/src/hs_std.rs deleted file mode 100644 index 8bc8196..0000000 --- a/apps/hs/src/hs_std.rs +++ /dev/null @@ -1,59 +0,0 @@ -use sysinfo::{Components, CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System}; - -use crate::SystemInfoGrabber; -extern crate alloc; -use alloc::vec::Vec; - -pub struct StdSystemInfoGrabber { - system: System, -} - -impl StdSystemInfoGrabber { - pub fn new() -> Self { - Self { - system: System::new_with_specifics( - RefreshKind::new() - .with_cpu(CpuRefreshKind::new().with_cpu_usage()) - .with_memory(MemoryRefreshKind::new().with_ram()), - ), - } - } -} - -impl SystemInfoGrabber for StdSystemInfoGrabber { - fn check_cpu_usage(&mut self) -> Vec { - self.system.refresh_cpu_usage(); - self.system - .cpus() - .iter() - .map(|x| x.cpu_usage().round() as u8) - .collect() - } - - fn check_mem_usage(&mut self) -> u8 { - self.system.refresh_memory(); - (self.system.used_memory() * 100 / self.system.total_memory()) as u8 - } - - fn check_fs_usage(&mut self) -> Vec { - let mut disks = Disks::new_with_refreshed_list(); - let disks = disks.list_mut(); - disks.sort_by(|x, y| x.name().cmp(y.name())); - - disks - .iter() - .map(|x| ((x.total_space() - x.available_space()) * 100 / x.total_space()) as u8) - .collect() - } - - fn check_temps(&mut self) -> Vec { - let mut components = Components::new_with_refreshed_list(); - let components = components.list_mut(); - components.sort_by(|x, y| x.label().cmp(y.label())); - - components - .iter() - .map(|x| x.temperature().round() as i8) - .collect() - } -} diff --git a/apps/hs/src/infograbber.rs b/apps/hs/src/infograbber.rs new file mode 100644 index 0000000..171e7c8 --- /dev/null +++ b/apps/hs/src/infograbber.rs @@ -0,0 +1,94 @@ +extern crate alloc; +use alloc::vec::Vec; + +pub trait SystemInfoGrabber { + fn check_cpu_usage(&mut self) -> Vec; + fn check_mem_usage(&mut self) -> u8; + fn check_fs_usage(&mut self) -> Vec; + fn check_temps(&mut self) -> Vec; +} + +#[cfg(any(feature = "std"))] +mod infograbber_std { + use sysinfo::{Components, CpuRefreshKind, Disks, MemoryRefreshKind, RefreshKind, System}; + + use crate::SystemInfoGrabber; + extern crate alloc; + use alloc::vec::Vec; + + pub struct StdSystemInfoGrabber { + system: System, + } + + impl StdSystemInfoGrabber { + pub fn new() -> Self { + Self { + system: System::new_with_specifics( + RefreshKind::new() + .with_cpu(CpuRefreshKind::new().with_cpu_usage()) + .with_memory(MemoryRefreshKind::new().with_ram()), + ), + } + } + } + + impl SystemInfoGrabber for StdSystemInfoGrabber { + fn check_cpu_usage(&mut self) -> Vec { + self.system.refresh_cpu_usage(); + self.system + .cpus() + .iter() + .map(|x| x.cpu_usage().round() as u8) + .collect() + } + + fn check_mem_usage(&mut self) -> u8 { + self.system.refresh_memory(); + (self.system.used_memory() * 100 / self.system.total_memory()) as u8 + } + + fn check_fs_usage(&mut self) -> Vec { + let mut disks = Disks::new_with_refreshed_list(); + let disks = disks.list_mut(); + disks.sort_by(|x, y| x.name().cmp(y.name())); + + disks + .iter() + .map(|x| ((x.total_space() - x.available_space()) * 100 / x.total_space()) as u8) + .collect() + } + + fn check_temps(&mut self) -> Vec { + let mut components = Components::new_with_refreshed_list(); + let components = components.list_mut(); + components.sort_by(|x, y| x.label().cmp(y.label())); + + components + .iter() + .map(|x| x.temperature().round() as i8) + .collect() + } + } +} + +#[cfg(any(feature = "std"))] +pub use infograbber_std::*; + +pub struct StubSystemInfoGrabber; +impl SystemInfoGrabber for StubSystemInfoGrabber { + fn check_cpu_usage(&mut self) -> Vec { + Vec::new() + } + + fn check_mem_usage(&mut self) -> u8 { + 255 + } + + fn check_fs_usage(&mut self) -> Vec { + Vec::new() + } + + fn check_temps(&mut self) -> Vec { + Vec::new() + } +} diff --git a/builds/example_build/Cargo.toml b/builds/example_build/Cargo.toml index 1dba813..ddd9e5a 100644 --- a/builds/example_build/Cargo.toml +++ b/builds/example_build/Cargo.toml @@ -10,6 +10,7 @@ rfe.features = ["std"] example.path = "../../apps/example" ds.path = "../../apps/ds" hs.path = "../../apps/hs" +hs.features = ["std"] to.path = "../../apps/to" anyhow.workspace = true hashbrown.workspace = true