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
//! Module containing the power sensors and their related functionality.
use super::*;
use crate::hwmon::sync_hwmon::Hwmon;
use crate::units::{Power, Ratio, Raw};
/// Helper trait that sums up all functionality of a read-only power sensor.
pub trait PowerSensor: SyncSensor + std::fmt::Debug {
/// Reads the accuracy subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_accuracy(&self) -> Result<Ratio> {
let raw = self.read_raw(SensorSubFunctionType::Accuracy)?;
Ratio::from_raw(&raw).map_err(Error::from)
}
/// Reads the cap subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_cap(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Cap)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the cap_max subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_cap_max(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapMax)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the cap_min subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_cap_min(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapMin)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the cap_hyst subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_cap_hyst(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapHyst)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_interval subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_interval(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageInterval)?;
Duration::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_interval_max subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_interval_max(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageIntervalMax)?;
Duration::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_interval_min subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_interval_min(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageIntervalMin)?;
Duration::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_highest subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_highest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageHighest)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_lowest subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_lowest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageLowest)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_max subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_max(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageMax)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads the average_min subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn read_average_min(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageMin)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not this sensor is enabled.
/// Returns an error, if the sensor doesn't support the feature.
fn read_enable(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Enable)?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads the input subfunction of this sensor.
/// Returns an error, if this sensor doesn't support the subtype.
fn read_input(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Input)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's max value.
/// Returns an error, if this sensor doesn't support the feature.
fn read_max(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Max)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's crit value.
/// Returns an error, if this sensor doesn't support the feature.
fn read_crit(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Crit)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's average value.
/// Returns an error, if this sensor doesn't support the feature.
fn read_average(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Average)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's historically highest input.
/// Returns an error, if this sensor doesn't support the feature.
fn read_highest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Highest)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's historically lowest input.
/// Returns an error, if this sensor doesn't support the feature.
fn read_lowest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Lowest)?;
Power::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the sensor.
/// Returns an error, if the sensor doesn't support the feature.
fn read_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Alarm)?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the crit subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
fn read_crit_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::CritAlarm)?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the cap subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
fn read_cap_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::CapAlarm)?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition for the sensor also triggers beeping.
/// Returns an error, if the sensor doesn't support the feature.
fn read_beep(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Beep)?;
bool::from_raw(&raw).map_err(Error::from)
}
}
/// Struct that represents a read only power sensor.
#[derive(Debug, Clone)]
pub(crate) struct PowerSensorStruct {
hwmon_path: PathBuf,
index: u16,
}
impl Sensor for PowerSensorStruct {
fn static_base() -> &'static str where Self: Sized {
"power"
}
fn base(&self) -> &'static str {
"power"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
impl SyncSensor for PowerSensorStruct {}
impl SyncSensorExt for PowerSensorStruct {
fn parse(hwmon: &Hwmon, index: u16) -> Result<Self> {
let sensor = Self {
hwmon_path: hwmon.path().to_path_buf(),
index,
};
inspect_sensor(sensor, SensorSubFunctionType::Input)
}
}
impl PowerSensor for PowerSensorStruct {}
#[cfg(feature = "writeable")]
impl WriteableSensor for PowerSensorStruct {}
#[cfg(feature = "writeable")]
/// Helper trait that sums up all functionality of a read-write power sensor.
pub trait WriteablePowerSensor: PowerSensor + WriteableSensor {
/// Converts cap and writes it to the cap subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn write_cap(&self, cap: Power) -> Result<()> {
self.write_raw(SensorSubFunctionType::Cap, &cap.to_raw())
}
/// Converts cap_hyst and writes it to the cap_hyst subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn write_cap_hyst(&self, cap_hyst: Power) -> Result<()> {
self.write_raw(SensorSubFunctionType::CapHyst, &cap_hyst.to_raw())
}
/// Converts interval and writes it to the average_interval subfunction of this power sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
fn write_average_interval(&self, interval: Duration) -> Result<()> {
self.write_raw(SensorSubFunctionType::AverageInterval, &interval.to_raw())
}
/// Sets this sensor's enabled state.
/// Returns an error, if the sensor doesn't support the feature.
fn write_enable(&self, enable: bool) -> Result<()> {
self.write_raw(SensorSubFunctionType::Enable, &enable.to_raw())
}
/// Writes this sensor's max value.
/// Returns an error, if the sensor doesn't support the feature.
fn write_max(&self, max: Power) -> Result<()> {
self.write_raw(SensorSubFunctionType::Max, &max.to_raw())
}
/// Writes this sensor's crit value.
/// Returns an error, if the sensor doesn't support the feature.
fn write_crit(&self, crit: Power) -> Result<()> {
self.write_raw(SensorSubFunctionType::Crit, &crit.to_raw())
}
/// Sets whether or not an alarm condition for the sensor also triggers beeping.
/// Returns an error, if the sensor doesn't support the feature.
fn write_beep(&self, beep: bool) -> Result<()> {
self.write_raw(SensorSubFunctionType::Beep, &beep.to_raw())
}
}
#[cfg(feature = "writeable")]
impl WriteablePowerSensor for PowerSensorStruct {}