From f1b0e1288aa68dfded65426c90e64427998c1275 Mon Sep 17 00:00:00 2001 From: "Luke D. Jones" Date: Mon, 13 Nov 2023 11:28:34 +1300 Subject: [PATCH] Add missing crates --- cpuctl/Cargo.toml | 7 +++ cpuctl/src/lib.rs | 14 ++++++ dmi-id/Cargo.toml | 8 ++++ dmi-id/src/lib.rs | 112 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 cpuctl/Cargo.toml create mode 100644 cpuctl/src/lib.rs create mode 100644 dmi-id/Cargo.toml create mode 100644 dmi-id/src/lib.rs diff --git a/cpuctl/Cargo.toml b/cpuctl/Cargo.toml new file mode 100644 index 00000000..efb33458 --- /dev/null +++ b/cpuctl/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "cpuctl" +license = "MPL-2.0" +edition = "2021" +version.workspace = true + +[dependencies] diff --git a/cpuctl/src/lib.rs b/cpuctl/src/lib.rs new file mode 100644 index 00000000..7d12d9af --- /dev/null +++ b/cpuctl/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: usize, right: usize) -> usize { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/dmi-id/Cargo.toml b/dmi-id/Cargo.toml new file mode 100644 index 00000000..bdeaff79 --- /dev/null +++ b/dmi-id/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "dmi_id" +edition = "2021" +version.workspace = true + +[dependencies] +log.workspace = true +udev.workspace = true \ No newline at end of file diff --git a/dmi-id/src/lib.rs b/dmi-id/src/lib.rs new file mode 100644 index 00000000..202a05e9 --- /dev/null +++ b/dmi-id/src/lib.rs @@ -0,0 +1,112 @@ +use log::{info, warn}; + +#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Clone)] +pub struct DMIID { + pub id_model: String, + pub dmi_family: String, + pub dmi_vendor: String, + pub board_name: String, + pub board_vendor: String, + pub bios_date: String, + pub bios_release: String, + pub bios_vendor: String, + pub bios_version: String, + pub product_family: String, + pub product_name: String, +} + +impl DMIID { + pub fn new() -> Result { + let mut enumerator = udev::Enumerator::new().map_err(|err| { + warn!("{}", err); + format!("dmi enumerator failed: {err}") + })?; + + enumerator.match_subsystem("dmi").map_err(|err| { + warn!("{}", err); + format!("dmi match_subsystem failed: {err}") + })?; + + let mut result = enumerator.scan_devices().map_err(|err| { + warn!("{}", err); + format!("dmi scan_devices failed: {err}") + })?; + + if let Some(device) = (result).next() { + info!("Found dmi ID info at {:?}", device.sysname()); + + return Ok(Self { + id_model: device + .property_value("ID_MODEL") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + dmi_family: device + .property_value("DMI_FAMILY") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + dmi_vendor: device + .property_value("DMI_VENDOR") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + board_name: device + .attribute_value("board_name") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + board_vendor: device + .attribute_value("board_vendor") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + bios_date: device + .attribute_value("bios_date") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + bios_release: device + .attribute_value("bios_release") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + bios_vendor: device + .attribute_value("bios_vendor") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + bios_version: device + .attribute_value("bios_version") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + product_family: device + .attribute_value("product_family") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + product_name: device + .attribute_value("product_name") + .map(|s| s.to_string_lossy().to_string()) + .unwrap_or("Unknown".to_string()), + }); + } + Err("dmi not found".into()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dmi_sysfs_properties_not_unknown() { + let dmi = DMIID::new().unwrap(); + + assert_ne!(dmi.id_model, "Unknown".to_string()); + dbg!(dmi.id_model); + assert_ne!(dmi.dmi_family, "Unknown".to_string()); + dbg!(dmi.dmi_family); + assert_ne!(dmi.dmi_vendor, "Unknown".to_string()); + dbg!(dmi.dmi_vendor); + assert_ne!(dmi.board_name, "Unknown".to_string()); + dbg!(dmi.board_name); + assert_ne!(dmi.board_vendor, "Unknown".to_string()); + dbg!(dmi.board_vendor); + assert_ne!(dmi.product_family, "Unknown".to_string()); + dbg!(dmi.product_family); + assert_ne!(dmi.product_name, "Unknown".to_string()); + dbg!(dmi.product_name); + } +}