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
use std::ffi::CString;
use std::ptr;
use std::os::raw::{c_char, c_int, c_uint, c_void};

use api;
use assert_size;
use from_c_string;

use DeviceHandle;
use Error;
use IntoResult;

#[derive(Debug, PartialEq)]
pub enum ThermalThrottlingLevel {
    Normal,
    TemperatureLimitLowerReached,
    TemperatureLimitHigherReached,
}

pub struct Device {
    handle: *const c_void,
}

impl Device {
    /// Gets the device name at given zero-based `index` or None if there is no device there.
    pub fn get_name(index: usize) -> Option<String> {
        let mut buf = [0; api::MVNC_MAX_NAME_SIZE];
        unsafe {
            api::mvncGetDeviceName(
                index as c_int,
                buf.as_mut_ptr() as *mut _ as *mut c_char,
                buf.len() as c_uint,
            )
        }.into_result()
            .and_then(|()| from_c_string(&buf))
            .ok()
    }

    /// Opens the device with the given `name`.
    pub fn open(name: &str) -> Result<Device, Error> {
        let cstr = CString::new(name).unwrap();
        let mut handle = ptr::null::<c_void>();
        unsafe { api::mvncOpenDevice(cstr.as_ptr(), &mut handle).into_result() }
            .map(|()| Device { handle })
    }

    /// Gets the thermal throttling level.
    pub fn get_thermal_throttling_level(&self) -> Result<ThermalThrottlingLevel, Error> {
        let mut thermal_throttling_level = -1;
        let expected_size = api::SIZEOF_C_INT;
        let mut size = expected_size;

        unsafe {
            api::mvncGetDeviceOption(
                self.handle,
                api::MVNC_THERMAL_THROTTLING_LEVEL,
                &mut thermal_throttling_level as *mut _ as *mut c_void,
                &mut size,
            )
        }.into_result()
            .and_then(|()| {
                assert_size("throttling level", expected_size, size);
                match thermal_throttling_level {
                    api::MVNC_TEMP_LIM_NORMAL => Ok(ThermalThrottlingLevel::Normal),
                    api::MVNC_TEMP_LIM_LOWER => {
                        Ok(ThermalThrottlingLevel::TemperatureLimitLowerReached)
                    }
                    api::MVNC_TEMP_LIM_HIGHER => {
                        Ok(ThermalThrottlingLevel::TemperatureLimitHigherReached)
                    }
                    _ => Err(Error::ApiError),
                }
            })
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        if let Err(e) = unsafe { api::mvncCloseDevice(self.handle) }.into_result() {
            eprintln!("::mvnc::device::Device::drop: Err({:?})", e);
        }
    }
}

impl DeviceHandle for Device {
    fn handle(&self) -> *const c_void {
        self.handle
    }
}