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
//! Module containing the temp sensors and their related functionality.
use super::*;
use crate::hwmon::async_hwmon::Hwmon;
use crate::units::{Raw, TempType, Temperature};
use std::path::{Path, PathBuf};
#[async_trait]
/// Helper trait that sums up all functionality of a read-only temp sensor.
pub trait AsyncTempSensor: AsyncSensor + std::fmt::Debug {
/// Reads the type subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_type(&self) -> Result<TempType> {
let raw = self.read_raw(SensorSubFunctionType::Type).await?;
TempType::from_raw(&raw).map_err(Error::from)
}
/// Reads the offset subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_offset(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::Offset).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the max_hyst subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_max_hyst(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::MaxHyst).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the min_hyst subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_min_hyst(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::MinHyst).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the crit_hyst subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_crit_hyst(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::CritHyst).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the emergency subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_emergency(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::Emergency).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the emergency_hyst subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_emergency_hyst(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::EmergencyHyst).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's lcrit value.
/// Returns an error, if this sensor doesn't support the feature.
async fn read_lcrit(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::LowCrit).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads the lcrit_hyst subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn read_lcrit_hyst(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::LowCritHyst).await?;
Temperature::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.
async fn read_enable(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Enable).await?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads the input subfunction of this temp sensor.
/// Returns an error, if this sensor doesn't support the subtype.
async fn read_input(&self) -> Result<Temperature> {
if self.read_faulty().await.unwrap_or(false) {
return Err(Error::FaultySensor);
}
let raw = self.read_raw(SensorSubFunctionType::Input).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's min value.
/// Returns an error, if this sensor doesn't support the feature.
async fn read_min(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::Min).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's max value.
/// Returns an error, if this sensor doesn't support the feature.
async fn read_max(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::Max).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads this sensor's crit value.
/// Returns an error, if this sensor doesn't support the feature.
async fn read_crit(&self) -> Result<Temperature> {
let raw = self.read_raw(SensorSubFunctionType::Crit).await?;
Temperature::from_raw(&raw).map_err(Error::from)
}
/// Reads whether this sensor is faulty or not.
/// Returns an error, if this sensor doesn't support the feature.
async fn read_faulty(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Fault).await?;
bool::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.
async fn read_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Alarm).await?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the min subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
async fn read_min_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::MinAlarm).await?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the max subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
async fn read_max_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::MaxAlarm).await?;
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.
async fn read_crit_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::CritAlarm).await?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the lcrit subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
async fn read_lcrit_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::LowCritAlarm).await?;
bool::from_raw(&raw).map_err(Error::from)
}
/// Reads whether or not an alarm condition exists for the emergency subfunction of the sensor.
/// Returns an error, if the sensor doesn't support the feature.
async fn read_emergency_alarm(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::EmergencyAlarm).await?;
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.
async fn read_beep(&self) -> Result<bool> {
let raw = self.read_raw(SensorSubFunctionType::Beep).await?;
bool::from_raw(&raw).map_err(Error::from)
}
}
/// Struct that represents a read only temp sensor.
#[derive(Debug, Clone)]
pub(crate) struct TempSensorStruct {
hwmon_path: PathBuf,
index: u16,
}
impl Sensor for TempSensorStruct {
fn static_base() -> &'static str where Self: Sized {
"temp"
}
fn base(&self) -> &'static str {
"temp"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
impl AsyncSensor for TempSensorStruct {}
#[async_trait]
impl AsyncSensorExt for TempSensorStruct {
async fn parse(hwmon: &Hwmon, index: u16) -> Result<Self> {
let sensor = Self {
hwmon_path: hwmon.path().to_path_buf(),
index,
};
inspect_sensor(sensor, SensorSubFunctionType::Input).await
}
}
impl AsyncTempSensor for TempSensorStruct {}
#[cfg(feature = "writeable")]
impl AsyncWriteableSensor for TempSensorStruct {}
#[cfg(feature = "writeable")]
#[async_trait]
/// Helper trait that sums up all functionality of a read-write temp sensor.
pub trait AsyncWriteableTempSensor: AsyncTempSensor + AsyncWriteableSensor {
/// Converts offset and writes it to this temp's offset subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_offset(&self, offset: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::Offset, &offset.to_raw())
.await
}
/// Converts max_hyst and writes it to this temp's max_hyst subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_max_hyst(&self, max_hyst: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::MaxHyst, &max_hyst.to_raw())
.await
}
/// Converts min_hyst and writes it to this temp's min_hyst subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_min_hyst(&self, min_hyst: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::MinHyst, &min_hyst.to_raw())
.await
}
/// Converts crit_hyst and writes it to this temp's crit_hyst subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_crit_hyst(&self, crit_hyst: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::CritHyst, &crit_hyst.to_raw())
.await
}
/// Converts emergency and writes it to this temp's emergency subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_emergency(&self, emergency: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::Emergency, &emergency.to_raw())
.await
}
/// Converts emergency_hyst and writes it to this temp's emergency_hyst subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_emergency_hyst(&self, emergency_hyst: Temperature) -> Result<()> {
self.write_raw(
SensorSubFunctionType::EmergencyHyst,
&emergency_hyst.to_raw(),
)
.await
}
/// Writes this sensor's lcrit value.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_lcrit(&self, lcrit: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::LowCrit, &lcrit.to_raw())
.await
}
/// Converts lcrit_hyst and writes it to this temp's lcrit_hyst subfunction.
/// Returns an error, if this sensor doesn't support the subfunction.
async fn write_lcrit_hyst(&self, lcrit_hyst: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::LowCritHyst, &lcrit_hyst.to_raw())
.await
}
/// Sets this sensor's enabled state.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_enable(&self, enable: bool) -> Result<()> {
self.write_raw(SensorSubFunctionType::Enable, &enable.to_raw())
.await
}
/// Writes this sensor's min value.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_min(&self, min: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::Min, &min.to_raw())
.await
}
/// Writes this sensor's max value.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_max(&self, max: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::Max, &max.to_raw())
.await
}
/// Writes this sensor's crit value.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_crit(&self, crit: Temperature) -> Result<()> {
self.write_raw(SensorSubFunctionType::Crit, &crit.to_raw())
.await
}
/// Sets whether or not an alarm condition for the sensor also triggers beeping.
/// Returns an error, if the sensor doesn't support the feature.
async fn write_beep(&self, beep: bool) -> Result<()> {
self.write_raw(SensorSubFunctionType::Beep, &beep.to_raw())
.await
}
}
#[cfg(feature = "writeable")]
impl AsyncWriteableTempSensor for TempSensorStruct {}