1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
use crate::{SMBiosStruct, UndefinedStruct};
use std::fmt;

/// # System Reset (Type 23)
///
/// This structure describes whether Automatic System Reset functions are enabled (Status).
///
/// If the system has a watchdog timer and the timer is not reset (Timer Reset) before the Interval elapses,
/// an automatic system reset occurs. The system re-boots according to the Boot Option. This function may
/// repeat until the Limit is reached, at which time the system re-boots according to the Boot Option at Limit.
///
/// NOTE This structure type was added for version 2.2 of this specification.
///
/// Compliant with:
/// DMTF SMBIOS Reference Specification 3.4.0 (DSP0134)
/// Document Date: 2020-07-17
pub struct SMBiosSystemReset<'a> {
    parts: &'a UndefinedStruct,
}

impl<'a> SMBiosStruct<'a> for SMBiosSystemReset<'a> {
    const STRUCT_TYPE: u8 = 23u8;

    fn new(parts: &'a UndefinedStruct) -> Self {
        Self { parts }
    }

    fn parts(&self) -> &'a UndefinedStruct {
        self.parts
    }
}

impl<'a> SMBiosSystemReset<'a> {
    /// Capabilities bit-field
    ///
    /// Identifies the system-reset capabilities for the system
    pub fn capabilities(&self) -> Option<SystemResetCapabilities> {
        self.parts
            .get_field_byte(0x04)
            .map(|raw| SystemResetCapabilities::from(raw))
    }

    /// Reset count
    ///
    /// Number of automatic system resets since the last intentional
    /// reset
    pub fn reset_count(&self) -> Option<ResetCount> {
        self.parts
            .get_field_word(0x05)
            .map(|raw| ResetCount::from(raw))
    }

    /// Reset limit
    ///
    /// Number of consecutive times the system reset is attempted
    pub fn reset_limit(&self) -> Option<ResetLimit> {
        self.parts
            .get_field_word(0x07)
            .map(|raw| ResetLimit::from(raw))
    }

    /// Timer interval
    ///
    /// Number of minutes to use for the watchdog timer
    ///
    /// If the timer is not reset within this interval, the system reset
    /// timeout begins.
    pub fn timer_interval(&self) -> Option<TimerInterval> {
        self.parts
            .get_field_word(0x09)
            .map(|raw| TimerInterval::from(raw))
    }

    /// Timeout
    ///
    /// Number of minutes before the reboot is initiated
    ///
    /// It is used after a system power cycle, system reset (local or
    /// remote), and automatic system reset.
    pub fn timeout(&self) -> Option<Timeout> {
        self.parts
            .get_field_word(0x0B)
            .map(|raw| Timeout::from(raw))
    }
}

impl fmt::Debug for SMBiosSystemReset<'_> {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SMBiosSystemReset<'_>>())
            .field("header", &self.parts.header)
            .field("capabilities", &self.capabilities())
            .field("reset_count", &self.reset_count())
            .field("reset_limit", &self.reset_limit())
            .field("timer_interval", &self.timer_interval())
            .field("timeout", &self.timeout())
            .finish()
    }
}

/// # System Reset Capabilities
#[derive(PartialEq, Eq)]
pub struct SystemResetCapabilities {
    /// Raw byte of the system reset capabilities
    pub raw: u8,
}

impl From<u8> for SystemResetCapabilities {
    fn from(raw: u8) -> Self {
        SystemResetCapabilities { raw }
    }
}

impl SystemResetCapabilities {
    /// System contains a watchdog timer; either
    /// True (1) or False (0).
    pub fn has_watchdog_timer(&self) -> bool {
        self.raw & 0b0010_0000 == 0b0010_0000
    }

    /// Boot Option on Limit
    ///
    /// Identifies one of the system actions
    /// to be taken when the Reset Limit is reached.
    pub fn boot_option_on_limit(&self) -> BootOptionOnLimit {
        BootOptionOnLimit::from(self.raw)
    }

    /// Boot Option
    ///
    /// Indicates one of the following actions
    /// to be taken after a watchdog reset:
    pub fn boot_option(&self) -> BootOption {
        BootOption::from(self.raw)
    }

    /// Status
    ///
    /// Identifies whether (1) or not (0)
    /// the system reset is enabled by the user.
    pub fn reset_enabled(&self) -> bool {
        self.raw & 0b0000_0001 == 0b0000_0001
    }
}

impl fmt::Debug for SystemResetCapabilities {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt.debug_struct(std::any::type_name::<SystemResetCapabilities>())
            .field("raw", &self.raw)
            .field("has_watchdog_timer", &self.has_watchdog_timer())
            .field("boot_option_on_limit", &self.boot_option_on_limit())
            .field("boot_option", &self.boot_option())
            .field("reset_enabled", &self.reset_enabled())
            .finish()
    }
}

/// # Boot Option on Limit
///
/// Identifies one of the following system actions to
/// be taken when the Reset Limit is reached
#[derive(Debug, PartialEq, Eq)]
pub enum BootOptionOnLimit {
    /// Reserved, do not use.
    Reserved,
    /// Operating System
    OperatingSystem,
    /// System utilities
    SystemUtilities,
    /// Do not reboot
    DoNotReboot,
}

impl From<u8> for BootOptionOnLimit {
    fn from(raw: u8) -> Self {
        match raw & 0b0001_1000 {
            0b0000_0000 => BootOptionOnLimit::Reserved,
            0b0000_1000 => BootOptionOnLimit::OperatingSystem,
            0b0001_0000 => BootOptionOnLimit::SystemUtilities,
            0b0001_1000 => BootOptionOnLimit::DoNotReboot,
            _ => panic!("impossible value"),
        }
    }
}

/// # Boot Option
///
/// Indicates one of the following actions to be taken
//  after a watchdog reset
#[derive(Debug, PartialEq, Eq)]
pub enum BootOption {
    /// Reserved, do not use.
    Reserved,
    /// Operating System
    OperatingSystem,
    /// System utilities
    SystemUtilities,
    /// Do not reboot
    DoNotReboot,
}

impl From<u8> for BootOption {
    fn from(raw: u8) -> Self {
        match raw & 0b0000_0110 {
            0b0000_0000 => BootOption::Reserved,
            0b0000_0010 => BootOption::OperatingSystem,
            0b0000_0100 => BootOption::SystemUtilities,
            0b0000_0110 => BootOption::DoNotReboot,
            _ => panic!("impossible value"),
        }
    }
}

/// # Reset Count
#[derive(Debug)]
pub enum ResetCount {
    /// Number of automatic system resets since the last intentional reset
    Count(u16),
    /// Reset count is unknown.
    Unknown,
}

impl From<u16> for ResetCount {
    fn from(raw: u16) -> Self {
        match raw {
            0xFFFF => ResetCount::Unknown,
            _ => ResetCount::Count(raw),
        }
    }
}

/// # Reset Limit
#[derive(Debug)]
pub enum ResetLimit {
    /// Number of consecutive times the system reset is attempted
    Count(u16),
    /// Reset limit is unknown.
    Unknown,
}

impl From<u16> for ResetLimit {
    fn from(raw: u16) -> Self {
        match raw {
            0xFFFF => ResetLimit::Unknown,
            _ => ResetLimit::Count(raw),
        }
    }
}

/// # Timer Interval
#[derive(Debug)]
pub enum TimerInterval {
    /// Number of minutes to use for the watchdog timer
    ///
    /// If the timer is not reset within this interval,
    /// the system reset timeout begins.
    Minutes(u16),
    /// Timer interval is unknown.
    Unknown,
}

impl From<u16> for TimerInterval {
    fn from(raw: u16) -> Self {
        match raw {
            0xFFFF => TimerInterval::Unknown,
            _ => TimerInterval::Minutes(raw),
        }
    }
}

/// # Timeout
#[derive(Debug)]
pub enum Timeout {
    /// Number of minutes before the reboot is initiated
    ///
    /// It is used after a system power cycle, system reset
    // (local or remote), and automatic system reset.
    Minutes(u16),
    /// Timeout is unknown.
    Unknown,
}

impl From<u16> for Timeout {
    fn from(raw: u16) -> Self {
        match raw {
            0xFFFF => Timeout::Unknown,
            _ => Timeout::Minutes(raw),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn unit_test() {
        let struct_type23 = vec![
            0x17, 0x0D, 0x4F, 0x01, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
            0x00,
        ];

        let parts = UndefinedStruct::new(&struct_type23);
        let test_struct = SMBiosSystemReset::new(&parts);

        assert_eq!(
            test_struct.capabilities(),
            Some(SystemResetCapabilities::from(0))
        );
        match test_struct.reset_count().unwrap() {
            ResetCount::Count(_) => panic!("expected unknown"),
            ResetCount::Unknown => (),
        }
        match test_struct.reset_limit().unwrap() {
            ResetLimit::Count(_) => panic!("expected unknown"),
            ResetLimit::Unknown => (),
        }
        match test_struct.timer_interval().unwrap() {
            TimerInterval::Minutes(_) => panic!("expected unknown"),
            TimerInterval::Unknown => (),
        }
        match test_struct.timeout().unwrap() {
            Timeout::Minutes(_) => panic!("expected unknown"),
            Timeout::Unknown => (),
        }
    }
}