use crate::{SMBiosStruct, UndefinedStruct};
use std::fmt;
pub struct SMBiosVoltageProbe<'a> {
parts: &'a UndefinedStruct,
}
impl<'a> SMBiosStruct<'a> for SMBiosVoltageProbe<'a> {
const STRUCT_TYPE: u8 = 26u8;
fn new(parts: &'a UndefinedStruct) -> Self {
Self { parts }
}
fn parts(&self) -> &'a UndefinedStruct {
self.parts
}
}
impl<'a> SMBiosVoltageProbe<'a> {
pub fn description(&self) -> Option<String> {
self.parts.get_field_string(0x04)
}
pub fn location_and_status(&self) -> Option<VoltageProbeLocationAndStatus> {
self.parts
.get_field_byte(0x05)
.map(|raw| VoltageProbeLocationAndStatus::from(raw))
}
pub fn maximum_value(&self) -> Option<ProbeVoltage> {
self.parts
.get_field_word(0x06)
.map(|raw| ProbeVoltage::from(raw))
}
pub fn minimum_value(&self) -> Option<ProbeVoltage> {
self.parts
.get_field_word(0x08)
.map(|raw| ProbeVoltage::from(raw))
}
pub fn resolution(&self) -> Option<VoltageProbeResolution> {
self.parts
.get_field_word(0x0A)
.map(|raw| VoltageProbeResolution::from(raw))
}
pub fn tolerance(&self) -> Option<ProbeVoltage> {
self.parts
.get_field_word(0x0C)
.map(|raw| ProbeVoltage::from(raw))
}
pub fn accuracy(&self) -> Option<VoltageProbeAccuracy> {
self.parts
.get_field_word(0x0E)
.map(|raw| VoltageProbeAccuracy::from(raw))
}
pub fn oem_defined(&self) -> Option<u32> {
self.parts.get_field_dword(0x10)
}
pub fn nominal_value(&self) -> Option<ProbeVoltage> {
self.parts
.get_field_word(0x14)
.map(|raw| ProbeVoltage::from(raw))
}
}
impl fmt::Debug for SMBiosVoltageProbe<'_> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(std::any::type_name::<SMBiosVoltageProbe<'_>>())
.field("header", &self.parts.header)
.field("description", &self.description())
.field("location_and_status", &self.location_and_status())
.field("maximum_value", &self.maximum_value())
.field("minimum_value", &self.minimum_value())
.field("resolution", &self.resolution())
.field("tolerance", &self.tolerance())
.field("accuracy", &self.accuracy())
.field("oem_defined", &self.oem_defined())
.field("nominal_value", &self.nominal_value())
.finish()
}
}
#[derive(PartialEq, Eq)]
pub struct VoltageProbeLocationAndStatus {
pub raw: u8,
}
impl From<u8> for VoltageProbeLocationAndStatus {
fn from(raw: u8) -> Self {
VoltageProbeLocationAndStatus { raw }
}
}
impl VoltageProbeLocationAndStatus {
pub fn location(&self) -> VoltageProbeLocation {
VoltageProbeLocation::from(self.raw)
}
pub fn status(&self) -> VoltageProbeStatus {
VoltageProbeStatus::from(self.raw)
}
}
impl fmt::Debug for VoltageProbeLocationAndStatus {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(std::any::type_name::<VoltageProbeLocationAndStatus>())
.field("raw", &self.raw)
.field("location", &self.location())
.field("status", &self.status())
.finish()
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum VoltageProbeStatus {
Other,
Unknown,
OK,
NonCritical,
Critical,
NonRecoverable,
None,
}
impl From<u8> for VoltageProbeStatus {
fn from(raw: u8) -> Self {
match raw & 0b1110_0000 {
0b0000_0000 => VoltageProbeStatus::None,
0b0010_0000 => VoltageProbeStatus::Other,
0b0100_0000 => VoltageProbeStatus::Unknown,
0b0110_0000 => VoltageProbeStatus::OK,
0b1000_0000 => VoltageProbeStatus::NonCritical,
0b1010_0000 => VoltageProbeStatus::Critical,
0b1100_0000 => VoltageProbeStatus::NonRecoverable,
0b1110_0000 => VoltageProbeStatus::None,
_ => panic!("impossible value"),
}
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum VoltageProbeLocation {
Other,
Unknown,
Processor,
Disk,
PeripheralBay,
SystemManagementModule,
Motherboard,
MemoryModule,
ProcessorModule,
PowerUnit,
AddInCard,
FrontPanelBoard,
BackPanelBoard,
PowerSystemBoard,
DriveBackPlane,
None,
}
impl From<u8> for VoltageProbeLocation {
fn from(raw: u8) -> Self {
match raw & 0b0001_1111 {
0b0000_0001 => VoltageProbeLocation::Other,
0b0000_0010 => VoltageProbeLocation::Unknown,
0b0000_0011 => VoltageProbeLocation::Processor,
0b0000_0100 => VoltageProbeLocation::Disk,
0b0000_0101 => VoltageProbeLocation::PeripheralBay,
0b0000_0110 => VoltageProbeLocation::SystemManagementModule,
0b0000_0111 => VoltageProbeLocation::Motherboard,
0b0000_1000 => VoltageProbeLocation::MemoryModule,
0b0000_1001 => VoltageProbeLocation::ProcessorModule,
0b0000_1010 => VoltageProbeLocation::PowerUnit,
0b0000_1011 => VoltageProbeLocation::AddInCard,
0b0000_1100 => VoltageProbeLocation::FrontPanelBoard,
0b0000_1101 => VoltageProbeLocation::BackPanelBoard,
0b0000_1110 => VoltageProbeLocation::PowerSystemBoard,
0b0000_1111 => VoltageProbeLocation::DriveBackPlane,
_ => VoltageProbeLocation::None,
}
}
}
#[derive(Debug)]
pub enum ProbeVoltage {
Millivolts(u16),
Unknown,
}
impl From<u16> for ProbeVoltage {
fn from(raw: u16) -> Self {
match raw {
0x8000 => ProbeVoltage::Unknown,
_ => ProbeVoltage::Millivolts(raw),
}
}
}
#[derive(Debug)]
pub enum VoltageProbeResolution {
TenthsOfMillivolts(u16),
Unknown,
}
impl From<u16> for VoltageProbeResolution {
fn from(raw: u16) -> Self {
match raw {
0x8000 => VoltageProbeResolution::Unknown,
_ => VoltageProbeResolution::TenthsOfMillivolts(raw),
}
}
}
#[derive(Debug)]
pub enum VoltageProbeAccuracy {
OneOneHundredthPercent(u16),
Unknown,
}
impl From<u16> for VoltageProbeAccuracy {
fn from(raw: u16) -> Self {
match raw {
0x8000 => VoltageProbeAccuracy::Unknown,
_ => VoltageProbeAccuracy::OneOneHundredthPercent(raw),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn unit_test() {
let struct_type26 = vec![
26, 0x16, 0x2A, 0x00, 0x01, 0x67, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x4C, 0x4D, 0x37, 0x38, 0x41, 0x00, 0x00,
];
let parts = UndefinedStruct::new(&struct_type26);
let test_struct = SMBiosVoltageProbe::new(&parts);
assert_eq!(test_struct.description(), Some("LM78A".to_string()));
assert_eq!(
test_struct.location_and_status(),
Some(VoltageProbeLocationAndStatus::from(103))
);
match test_struct.maximum_value().unwrap() {
ProbeVoltage::Millivolts(_) => panic!("expected unknown"),
ProbeVoltage::Unknown => (),
}
match test_struct.minimum_value().unwrap() {
ProbeVoltage::Millivolts(_) => panic!("expected unknown"),
ProbeVoltage::Unknown => (),
}
match test_struct.resolution().unwrap() {
VoltageProbeResolution::TenthsOfMillivolts(_) => panic!("expected unknown"),
VoltageProbeResolution::Unknown => (),
}
match test_struct.tolerance().unwrap() {
ProbeVoltage::Millivolts(_) => panic!("expected unknown"),
ProbeVoltage::Unknown => (),
}
match test_struct.accuracy().unwrap() {
VoltageProbeAccuracy::OneOneHundredthPercent(_) => panic!("expected unknown"),
VoltageProbeAccuracy::Unknown => (),
}
assert_eq!(test_struct.oem_defined(), Some(0));
match test_struct.nominal_value().unwrap() {
ProbeVoltage::Millivolts(_) => panic!("expected unknown"),
ProbeVoltage::Unknown => (),
}
}
}