]>
Commit | Line | Data |
---|---|---|
923072b8 FG |
1 | // Take a look at the license at the top of the repository in the LICENSE file. |
2 | ||
3 | use super::utils::get_sys_value_by_name; | |
4 | use crate::ComponentExt; | |
5 | ||
6 | #[doc = include_str!("../../md_doc/component.md")] | |
7 | pub struct Component { | |
8 | id: Vec<u8>, | |
9 | label: String, | |
10 | temperature: f32, | |
11 | max: f32, | |
12 | } | |
13 | ||
14 | impl ComponentExt for Component { | |
15 | fn temperature(&self) -> f32 { | |
16 | self.temperature | |
17 | } | |
18 | ||
19 | fn max(&self) -> f32 { | |
20 | self.max | |
21 | } | |
22 | ||
23 | fn critical(&self) -> Option<f32> { | |
24 | None | |
25 | } | |
26 | ||
27 | fn label(&self) -> &str { | |
28 | &self.label | |
29 | } | |
30 | ||
31 | fn refresh(&mut self) { | |
32 | unsafe { | |
33 | if let Some(temperature) = refresh_component(&self.id) { | |
34 | self.temperature = temperature; | |
35 | if self.temperature > self.max { | |
36 | self.max = self.temperature; | |
37 | } | |
38 | } | |
39 | } | |
40 | } | |
41 | } | |
42 | ||
43 | unsafe fn refresh_component(id: &[u8]) -> Option<f32> { | |
44 | let mut temperature: libc::c_int = 0; | |
45 | if !get_sys_value_by_name(id, &mut temperature) { | |
46 | None | |
47 | } else { | |
48 | // convert from Kelvin (x 10 -> 273.2 x 10) to Celsius | |
49 | Some((temperature - 2732) as f32 / 10.) | |
50 | } | |
51 | } | |
52 | ||
53 | pub unsafe fn get_components(nb_cpus: usize) -> Vec<Component> { | |
54 | // For now, we only have temperature for CPUs... | |
55 | let mut components = Vec::with_capacity(nb_cpus); | |
56 | ||
57 | for core in 0..nb_cpus { | |
487cf647 | 58 | let id = format!("dev.cpu.{core}.temperature\0").as_bytes().to_vec(); |
923072b8 FG |
59 | if let Some(temperature) = refresh_component(&id) { |
60 | components.push(Component { | |
61 | id, | |
62 | label: format!("CPU {}", core + 1), | |
63 | temperature, | |
64 | max: temperature, | |
65 | }); | |
66 | } | |
67 | } | |
68 | components | |
69 | } |