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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::device::Device;
use crate::enum_wrappers::unit::LedColor;
use crate::enums::unit::{LedState, TemperatureReading};
use crate::error::{nvml_sym, nvml_try, NvmlError};
use crate::ffi::bindings::*;
use crate::struct_wrappers::unit::{FansInfo, PsuInfo, UnitInfo};
use crate::Nvml;
use static_assertions::assert_impl_all;
use std::mem;
use std::{convert::TryFrom, os::raw::c_uint};

/**
Struct that represents a unit.

Obtain a `Unit` with the various methods available to you on the `Nvml`
struct.

Lifetimes are used to enforce that each `Unit` instance cannot be used after
the `Nvml` instance it was obtained from is dropped:

```compile_fail
use nvml_wrapper::Nvml;
# use nvml_wrapper::error::*;

# fn main() -> Result<(), NvmlError> {
let nvml = Nvml::init()?;
let unit = nvml.unit_by_index(0)?;

drop(nvml);

// This won't compile
let unit_devices = unit.devices()?;
# Ok(())
# }
```

Note that I cannot test any `Unit` methods myself as I do not have access to
such hardware. **Test the functionality in this module before you use it**.
*/
#[derive(Debug)]
pub struct Unit<'nvml> {
    unit: nvmlUnit_t,
    nvml: &'nvml Nvml,
}

unsafe impl<'nvml> Send for Unit<'nvml> {}
unsafe impl<'nvml> Sync for Unit<'nvml> {}

assert_impl_all!(Unit: Send, Sync);

impl<'nvml> Unit<'nvml> {
    /**
    Create a new `Unit` wrapper.

    You will most likely never need to call this; see the methods available to you
    on the `Nvml` struct to get one.

    # Safety

    It is your responsibility to ensure that the given `nvmlUnit_t` pointer
    is valid.
    */
    // Clippy bug, see https://github.com/rust-lang/rust-clippy/issues/5593
    #[allow(clippy::missing_safety_doc)]
    pub unsafe fn new(unit: nvmlUnit_t, nvml: &'nvml Nvml) -> Self {
        Self { unit, nvml }
    }

    /// Access the `NVML` reference this struct wraps
    pub fn nvml(&self) -> &'nvml Nvml {
        self.nvml
    }

    /// Get the raw unit handle contained in this struct
    ///
    /// Sometimes necessary for C interop.
    ///
    /// # Safety
    ///
    /// This is unsafe to prevent it from being used without care.
    pub unsafe fn handle(&self) -> nvmlUnit_t {
        self.unit
    }

    /**
    Gets the set of GPU devices that are attached to this `Unit`.

    **I do not have the hardware to test this call. Verify for yourself that it
    works before you use it**. If it works, please let me know; if it doesn't,
    I would love a PR. If NVML is sane this should work, but NVIDIA's docs
    on this call are _anything_ but clear.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetDevices")]
    pub fn devices(&self) -> Result<Vec<Device>, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetDevices.as_ref())?;

        unsafe {
            let mut count: c_uint = match self.device_count()? {
                0 => return Ok(vec![]),
                value => value,
            };
            let mut devices: Vec<nvmlDevice_t> = vec![mem::zeroed(); count as usize];

            nvml_try(sym(self.unit, &mut count, devices.as_mut_ptr()))?;

            Ok(devices
                .into_iter()
                .map(|d| Device::new(d, self.nvml))
                .collect())
        }
    }

    /**
    Gets the count of GPU devices that are attached to this `Unit`.

    **I do not have the hardware to test this call. Verify for yourself that it
    works before you use it**. If it works, please let me know; if it doesn't,
    I would love a PR. If NVML is sane this should work, but NVIDIA's docs
    on this call are _anything_ but clear.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // Tested as part of the above
    #[doc(alias = "nvmlUnitGetDevices")]
    pub fn device_count(&self) -> Result<u32, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetDevices.as_ref())?;

        unsafe {
            /*
            NVIDIA doesn't even say that `count` will be set to the count if
            `InsufficientSize` is returned. But we can assume sanity, right?

            The idea here is:
            If there are 0 devices, NVML_SUCCESS is returned, `count` is set
              to 0. We return count, all good.
            If there is 1 device, NVML_SUCCESS is returned, `count` is set to
              1. We return count, all good.
            If there are >= 2 devices, NVML_INSUFFICIENT_SIZE is returned.
             `count` is theoretically set to the actual count, and we
              return it.
            */
            let mut count: c_uint = 1;
            let mut devices: [nvmlDevice_t; 1] = [mem::zeroed()];

            match sym(self.unit, &mut count, devices.as_mut_ptr()) {
                nvmlReturn_enum_NVML_SUCCESS | nvmlReturn_enum_NVML_ERROR_INSUFFICIENT_SIZE => {
                    Ok(count)
                }
                // We know that this will be an error
                other => nvml_try(other).map(|_| 0),
            }
        }
    }

    /**
    Gets fan information for this `Unit` (fan count and state + speed for each).

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `NotSupported`, if this is not an S-class product
    * `UnexpectedVariant`, for which you can read the docs for
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetFanSpeedInfo")]
    pub fn fan_info(&self) -> Result<FansInfo, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetFanSpeedInfo.as_ref())?;

        unsafe {
            let mut fans_info: nvmlUnitFanSpeeds_t = mem::zeroed();
            nvml_try(sym(self.unit, &mut fans_info))?;

            FansInfo::try_from(fans_info)
        }
    }

    /**
    Gets the LED state associated with this `Unit`.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `NotSupported`, if this is not an S-class product
    * `Utf8Error`, if the string obtained from the C function is not valid Utf8
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetLedState")]
    pub fn led_state(&self) -> Result<LedState, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetLedState.as_ref())?;

        unsafe {
            let mut state: nvmlLedState_t = mem::zeroed();
            nvml_try(sym(self.unit, &mut state))?;

            LedState::try_from(state)
        }
    }

    /**
    Gets the PSU stats for this `Unit`.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `NotSupported`, if this is not an S-class product
    * `Utf8Error`, if the string obtained from the C function is not valid Utf8
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetPsuInfo")]
    pub fn psu_info(&self) -> Result<PsuInfo, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetPsuInfo.as_ref())?;
        unsafe {
            let mut info: nvmlPSUInfo_t = mem::zeroed();
            nvml_try(sym(self.unit, &mut info))?;

            PsuInfo::try_from(info)
        }
    }

    /**
    Gets the temperature for the specified `UnitTemperatureReading`, in °C.

    Available readings depend on the product.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `NotSupported`, if this is not an S-class product
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products. Available readings depend on the product.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetTemperature")]
    pub fn temperature(&self, reading_type: TemperatureReading) -> Result<u32, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetTemperature.as_ref())?;

        unsafe {
            let mut temp: c_uint = mem::zeroed();

            nvml_try(sym(self.unit, reading_type as c_uint, &mut temp))?;

            Ok(temp)
        }
    }

    /**
    Gets the static information associated with this `Unit`.

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `Utf8Error`, if the string obtained from the C function is not valid Utf8

    # Device Support

    For S-class products.
    */
    // Checked against local
    // Tested
    #[doc(alias = "nvmlUnitGetUnitInfo")]
    pub fn info(&self) -> Result<UnitInfo, NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitGetUnitInfo.as_ref())?;

        unsafe {
            let mut info: nvmlUnitInfo_t = mem::zeroed();
            nvml_try(sym(self.unit, &mut info))?;

            UnitInfo::try_from(info)
        }
    }

    // Unit commands starting here

    /**
    Sets the LED color for this `Unit`.

    Requires root/admin permissions. This operation takes effect immediately.

    Note: Current S-class products don't provide unique LEDs for each unit. As such,
    both front and back LEDs will be toggled in unison regardless of which unit is
    specified with this method (aka the `Unit` represented by this struct).

    # Errors

    * `Uninitialized`, if the library has not been successfully initialized
    * `InvalidArg`, if the unit is invalid
    * `NotSupported`, if this is not an S-class product
    * `NoPermission`, if the user doesn't have permission to perform this operation
    * `Unknown`, on any unexpected error

    # Device Support

    For S-class products.
    */
    // checked against local
    // Tested (no-run)
    #[doc(alias = "nvmlUnitSetLedState")]
    pub fn set_led_color(&mut self, color: LedColor) -> Result<(), NvmlError> {
        let sym = nvml_sym(self.nvml.lib.nvmlUnitSetLedState.as_ref())?;

        unsafe { nvml_try(sym(self.unit, color.as_c())) }
    }
}

// I do not have access to this hardware and cannot test anything
#[cfg(test)]
#[deny(unused_mut)]
mod test {
    use crate::enum_wrappers::unit::LedColor;
    use crate::enums::unit::TemperatureReading;
    use crate::test_utils::*;

    #[test]
    #[ignore = "my machine does not support this call"]
    fn devices() {
        let nvml = nvml();
        let unit = unit(&nvml);
        unit.devices().expect("devices");
    }

    #[test]
    #[ignore = "my machine does not support this call"]
    fn fan_info() {
        let nvml = nvml();
        test_with_unit(3, &nvml, |unit| unit.fan_info())
    }

    #[test]
    #[ignore = "my machine does not support this call"]
    fn led_state() {
        let nvml = nvml();
        test_with_unit(3, &nvml, |unit| unit.led_state())
    }

    #[test]
    #[ignore = "my machine does not support this call"]
    fn psu_info() {
        let nvml = nvml();
        test_with_unit(3, &nvml, |unit| unit.psu_info())
    }

    #[test]
    #[ignore = "my machine does not support this call"]
    fn temperature() {
        let nvml = nvml();
        test_with_unit(3, &nvml, |unit| unit.temperature(TemperatureReading::Board))
    }

    #[test]
    #[ignore = "my machine does not support this call"]
    fn info() {
        let nvml = nvml();
        test_with_unit(3, &nvml, |unit| unit.info())
    }

    // This modifies unit state, so we don't want to actually run the test
    #[allow(dead_code)]
    fn set_led_color() {
        let nvml = nvml();
        let mut unit = unit(&nvml);

        unit.set_led_color(LedColor::Amber).expect("set to true")
    }
}