smbioslib/structs/types/
unknown.rs

1use crate::{Header, UndefinedStruct};
2use serde::{ser::SerializeStruct, Serialize, Serializer};
3use std::fmt;
4
5/// # OEM or Unknown Structure
6///
7/// Types 0 through 127 (7Fh) are reserved for and
8/// defined by the DMTF SMBIOS specification.
9/// Types 128 through 256 (80h to FFh) are available for
10/// system- and OEM-specific information.
11///
12/// When a structure has a type which is not defined or
13/// its type is an OEM type in the 80h to FFh range,
14/// this structure is used to represent the type.
15pub struct SMBiosUnknown<'a> {
16    parts: &'a UndefinedStruct,
17}
18
19impl<'a> SMBiosUnknown<'a> {
20    /// Creates an instance of this struct
21    pub fn new(parts: &'a UndefinedStruct) -> Self {
22        SMBiosUnknown { parts: parts }
23    }
24
25    /// Structure parts of this unknown structure
26    ///
27    /// Use this to inspect the structure in more detail.
28    pub fn parts(&self) -> &'a UndefinedStruct {
29        self.parts
30    }
31}
32
33impl fmt::Debug for SMBiosUnknown<'_> {
34    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
35        let fields = &self.parts.fields[Header::SIZE..];
36        fmt.debug_struct(std::any::type_name::<SMBiosUnknown<'_>>())
37            .field("header", &self.parts.header)
38            .field("fields", &fields)
39            .field("strings", &self.parts.strings)
40            .finish()
41    }
42}
43
44impl Serialize for SMBiosUnknown<'_> {
45    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
46    where
47        S: Serializer,
48    {
49        let fields = &self.parts.fields[Header::SIZE..];
50
51        let mut state = serializer.serialize_struct("SMBiosUnknown", 3)?;
52        state.serialize_field("header", &self.parts.header)?;
53        state.serialize_field("fields", &fields)?;
54        state.serialize_field("strings", &self.parts.strings)?;
55        state.end()
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_unknown_oem_type() {
65        // For testing we've borrowed a language information type (0x0D) structure and change its type to 0x99 (> 0x7F are OEM types)
66        let unknown_bytes = vec![
67            0x99u8, 0x16, 0x21, 0x00, // unknown data
68            0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69            0x00, 0x00, 0x00, 0x01, // "en|US|iso8859-1"
70            0x65, 0x6E, 0x7C, 0x55, 0x53, 0x7C, 0x69, 0x73, 0x6F, 0x38, 0x38, 0x35, 0x39, 0x2D,
71            0x31, 0x00, // "fr|FR|iso8859-1"
72            0x66, 0x72, 0x7C, 0x46, 0x52, 0x7C, 0x69, 0x73, 0x6F, 0x38, 0x38, 0x35, 0x39, 0x2D,
73            0x31, 0x00, // "ja|JP|unicode"
74            0x6A, 0x61, 0x7C, 0x4A, 0x50, 0x7C, 0x75, 0x6E, 0x69, 0x63, 0x6F, 0x64, 0x65, 0x00,
75            // end of structure
76            0x00,
77        ];
78
79        let parts = UndefinedStruct::new(&unknown_bytes);
80        let unknown = SMBiosUnknown::new(&parts);
81
82        // header tests
83        assert_eq!(*unknown.parts().header.handle(), 0x0021);
84        assert_eq!(unknown.parts().header.length(), 0x16);
85
86        // debug print test
87        println!("unknown structure: {:?}", unknown);
88    }
89}