smbioslib/structs/types/
management_device_threshold_data.rs

1use crate::{SMBiosStruct, UndefinedStruct};
2use serde::{ser::SerializeStruct, Serialize, Serializer};
3use std::fmt;
4
5/// # Management Device Threshold Data (Type 36)
6///
7/// The information in this structure defines threshold information for a component (probe or cooling-unit) contained within a Management Device
8///
9/// For each threshold field present in the structure:
10/// - The threshold units (millivolts, milliamps, 1/10th degrees C, or RPMs) are as defined by the associated probe or cooling-unit component structure.
11/// - If the value is unavailable, the field is set to 0x8000.
12///
13/// NOTE This structure type was added in version 2.3 of this specification.
14///
15/// Compliant with:
16/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
17/// Document Date: 2020-07-17
18pub struct SMBiosManagementDeviceThresholdData<'a> {
19    parts: &'a UndefinedStruct,
20}
21
22impl<'a> SMBiosStruct<'a> for SMBiosManagementDeviceThresholdData<'a> {
23    const STRUCT_TYPE: u8 = 36u8;
24
25    fn new(parts: &'a UndefinedStruct) -> Self {
26        Self { parts }
27    }
28
29    fn parts(&self) -> &'a UndefinedStruct {
30        self.parts
31    }
32}
33
34impl<'a> SMBiosManagementDeviceThresholdData<'a> {
35    /// Lower non-critical threshold for this component
36    pub fn lower_threshold_non_critical(&self) -> Option<u16> {
37        self.parts.get_field_word(0x04)
38    }
39
40    /// Upper non-critical threshold for this component
41    pub fn upper_threshold_non_critical(&self) -> Option<u16> {
42        self.parts.get_field_word(0x06)
43    }
44
45    /// Lower critical threshold for this component
46    pub fn lower_threshold_critical(&self) -> Option<u16> {
47        self.parts.get_field_word(0x08)
48    }
49
50    /// Upper critical threshold for this component
51    pub fn upper_threshold_critical(&self) -> Option<u16> {
52        self.parts.get_field_word(0x0A)
53    }
54
55    /// Lower non-recoverable threshold for this component
56    pub fn lower_threshold_non_recoverable(&self) -> Option<u16> {
57        self.parts.get_field_word(0x0C)
58    }
59
60    /// Upper non-recoverable threshold for this component
61    pub fn upper_threshold_non_recoverable(&self) -> Option<u16> {
62        self.parts.get_field_word(0x0E)
63    }
64}
65
66impl fmt::Debug for SMBiosManagementDeviceThresholdData<'_> {
67    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
68        fmt.debug_struct(std::any::type_name::<SMBiosManagementDeviceThresholdData<'_>>())
69            .field("header", &self.parts.header)
70            .field(
71                "lower_threshold_non_critical",
72                &self.lower_threshold_non_critical(),
73            )
74            .field(
75                "upper_threshold_non_critical",
76                &self.upper_threshold_non_critical(),
77            )
78            .field("lower_threshold_critical", &self.lower_threshold_critical())
79            .field("upper_threshold_critical", &self.upper_threshold_critical())
80            .field(
81                "lower_threshold_non_recoverable",
82                &self.lower_threshold_non_recoverable(),
83            )
84            .field(
85                "upper_threshold_non_recoverable",
86                &self.upper_threshold_non_recoverable(),
87            )
88            .finish()
89    }
90}
91
92impl Serialize for SMBiosManagementDeviceThresholdData<'_> {
93    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94    where
95        S: Serializer,
96    {
97        let mut state = serializer.serialize_struct("SMBiosManagementDeviceThresholdData", 7)?;
98        state.serialize_field("header", &self.parts.header)?;
99        state.serialize_field(
100            "lower_threshold_non_critical",
101            &self.lower_threshold_non_critical(),
102        )?;
103        state.serialize_field(
104            "upper_threshold_non_critical",
105            &self.upper_threshold_non_critical(),
106        )?;
107        state.serialize_field("lower_threshold_critical", &self.lower_threshold_critical())?;
108        state.serialize_field("upper_threshold_critical", &self.upper_threshold_critical())?;
109        state.serialize_field(
110            "lower_threshold_non_recoverable",
111            &self.lower_threshold_non_recoverable(),
112        )?;
113        state.serialize_field(
114            "upper_threshold_non_recoverable",
115            &self.upper_threshold_non_recoverable(),
116        )?;
117        state.end()
118    }
119}
120
121#[cfg(test)]
122mod tests {
123    use super::*;
124
125    #[test]
126    fn unit_test() {
127        let struct_type36 = vec![
128            0x24, 0x10, 0x28, 0x00, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00, 0x04, 0x00, 0x05, 0x00,
129            0x06, 0x00, 0x00, 0x00,
130        ];
131
132        let parts = UndefinedStruct::new(&struct_type36);
133        let test_struct = SMBiosManagementDeviceThresholdData::new(&parts);
134
135        assert_eq!(test_struct.lower_threshold_non_critical(), Some(1));
136        assert_eq!(test_struct.upper_threshold_non_critical(), Some(2));
137        assert_eq!(test_struct.lower_threshold_critical(), Some(3));
138        assert_eq!(test_struct.upper_threshold_critical(), Some(4));
139        assert_eq!(test_struct.lower_threshold_non_recoverable(), Some(5));
140        assert_eq!(test_struct.upper_threshold_non_recoverable(), Some(6));
141    }
142}