nvml_wrapper/struct_wrappers/
unit.rs

1use crate::enum_wrappers::unit::FanState;
2use crate::error::NvmlError;
3use crate::ffi::bindings::*;
4#[cfg(feature = "serde")]
5use serde_derive::{Deserialize, Serialize};
6use std::{convert::TryFrom, ffi::CStr};
7
8/// Fan information readings for an entire S-class unit.
9// Checked against local
10#[derive(Debug, Clone, Eq, PartialEq, Hash)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct FansInfo {
13    /// Number of fans in the unit.
14    pub count: u32,
15    /// Fan data for each fan.
16    pub fans: Vec<FanInfo>,
17}
18
19impl TryFrom<nvmlUnitFanSpeeds_t> for FansInfo {
20    type Error = NvmlError;
21
22    /**
23    Construct `FansInfo` from the corresponding C struct.
24
25    # Errors
26
27    * `UnexpectedVariant`, for which you can read the docs for
28    */
29    fn try_from(value: nvmlUnitFanSpeeds_t) -> Result<Self, Self::Error> {
30        let fans = value
31            .fans
32            .iter()
33            .map(|f| FanInfo::try_from(*f))
34            .collect::<Result<_, NvmlError>>()?;
35
36        Ok(FansInfo {
37            count: value.count,
38            fans,
39        })
40    }
41}
42
43/// Fan info reading for a single fan in an S-class unit.
44// Checked against local
45#[derive(Debug, Clone, Eq, PartialEq, Hash)]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47pub struct FanInfo {
48    /// Fan speed (RPM).
49    pub speed: u32,
50    /// Indicates whether a fan is working properly.
51    pub state: FanState,
52}
53
54impl TryFrom<nvmlUnitFanInfo_t> for FanInfo {
55    type Error = NvmlError;
56
57    /**
58    Construct `FanInfo` from the corresponding C struct.
59
60    # Errors
61
62    * `UnexpectedVariant`, for which you can read the docs for
63    */
64    fn try_from(value: nvmlUnitFanInfo_t) -> Result<Self, Self::Error> {
65        Ok(FanInfo {
66            speed: value.speed,
67            state: FanState::try_from(value.state)?,
68        })
69    }
70}
71
72/**
73Power usage information for an S-class unit.
74
75The power supply state is a human-readable string that equals "Normal" or contains
76a combination of "Abnormal" plus one or more of the following (aka good luck matching
77on it):
78
79* High voltage
80* Fan failure
81* Heatsink temperature
82* Current limit
83* Voltage below UV alarm threshold
84* Low-voltage
85* SI2C remote off command
86* MOD_DISABLE input
87* Short pin transition
88*/
89// Checked against local
90#[derive(Debug, Clone, Eq, PartialEq, Hash)]
91#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
92pub struct PsuInfo {
93    /// PSU current (in A)
94    pub current: u32,
95    /// PSU power draw (in W)
96    pub power_draw: u32,
97    /// Human-readable string describing the PSU state.
98    pub state: String,
99    /// PSU voltage (in V)
100    pub voltage: u32,
101}
102
103impl TryFrom<nvmlPSUInfo_t> for PsuInfo {
104    type Error = NvmlError;
105
106    /**
107    Construct `PsuInfo` from the corresponding C struct.
108
109    # Errors
110
111    * `UnexpectedVariant`, for which you can read the docs for
112    */
113    fn try_from(value: nvmlPSUInfo_t) -> Result<Self, Self::Error> {
114        unsafe {
115            let state_raw = CStr::from_ptr(value.state.as_ptr());
116            Ok(PsuInfo {
117                current: value.current,
118                power_draw: value.power,
119                state: state_raw.to_str()?.into(),
120                voltage: value.voltage,
121            })
122        }
123    }
124}
125
126/// Static S-class unit info.
127// Checked against local
128#[derive(Debug, Clone, Eq, PartialEq, Hash)]
129#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
130pub struct UnitInfo {
131    pub firmware_version: String,
132    /// Product identifier.
133    pub id: String,
134    pub name: String,
135    /// Product serial number.
136    pub serial: String,
137}
138
139impl TryFrom<nvmlUnitInfo_t> for UnitInfo {
140    type Error = NvmlError;
141
142    /**
143    Construct `UnitInfo` from the corresponding C struct.
144
145    # Errors
146
147    * `UnexpectedVariant`, for which you can read the docs for
148    */
149    fn try_from(value: nvmlUnitInfo_t) -> Result<Self, Self::Error> {
150        unsafe {
151            let version_raw = CStr::from_ptr(value.firmwareVersion.as_ptr());
152            let id_raw = CStr::from_ptr(value.id.as_ptr());
153            let name_raw = CStr::from_ptr(value.name.as_ptr());
154            let serial_raw = CStr::from_ptr(value.serial.as_ptr());
155
156            Ok(UnitInfo {
157                firmware_version: version_raw.to_str()?.into(),
158                id: id_raw.to_str()?.into(),
159                name: name_raw.to_str()?.into(),
160                serial: serial_raw.to_str()?.into(),
161            })
162        }
163    }
164}
165
166/// Description of an HWBC entry.
167// Checked against local
168#[derive(Debug, Clone, Eq, PartialEq, Hash)]
169#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
170pub struct HwbcEntry {
171    pub id: u32,
172    pub firmware_version: String,
173}
174
175impl TryFrom<nvmlHwbcEntry_t> for HwbcEntry {
176    type Error = NvmlError;
177
178    /**
179    Construct `HwbcEntry` from the corresponding C struct.
180
181    # Errors
182
183    * `UnexpectedVariant`, for which you can read the docs for
184    */
185    fn try_from(value: nvmlHwbcEntry_t) -> Result<Self, Self::Error> {
186        unsafe {
187            let version_raw = CStr::from_ptr(value.firmwareVersion.as_ptr());
188            Ok(HwbcEntry {
189                id: value.hwbcId,
190                firmware_version: version_raw.to_str()?.into(),
191            })
192        }
193    }
194}