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
|
// Take a look at the license at the top of the repository in the LICENSE file.
use crate::ComponentExt;
use std::ptr::null_mut;
use winapi::shared::rpcdce::{
RPC_C_AUTHN_LEVEL_CALL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_AUTHN_WINNT, RPC_C_AUTHZ_NONE,
RPC_C_IMP_LEVEL_IMPERSONATE,
};
use winapi::shared::winerror::{FAILED, SUCCEEDED, S_FALSE, S_OK};
use winapi::shared::wtypesbase::CLSCTX_INPROC_SERVER;
use winapi::um::combaseapi::{
CoCreateInstance, CoInitializeEx, CoInitializeSecurity, CoSetProxyBlanket, CoUninitialize,
};
use winapi::um::oaidl::VARIANT;
use winapi::um::objidl::EOAC_NONE;
use winapi::um::oleauto::{SysAllocString, SysFreeString, VariantClear};
use winapi::um::wbemcli::{
CLSID_WbemLocator, IEnumWbemClassObject, IID_IWbemLocator, IWbemClassObject, IWbemLocator,
IWbemServices, WBEM_FLAG_FORWARD_ONLY, WBEM_FLAG_NONSYSTEM_ONLY, WBEM_FLAG_RETURN_IMMEDIATELY,
};
#[doc = include_str!("../../md_doc/component.md")]
pub struct Component {
temperature: f32,
max: f32,
critical: Option<f32>,
label: String,
connection: Option<Connection>,
}
impl Component {
/// Creates a new `Component` with the given information.
fn new() -> Option<Component> {
let mut c = Connection::new()
.and_then(|x| x.initialize_security())
.and_then(|x| x.create_instance())
.and_then(|x| x.connect_server())
.and_then(|x| x.set_proxy_blanket())
.and_then(|x| x.exec_query())?;
c.temperature(true)
.map(|(temperature, critical)| Component {
temperature,
label: "Computer".to_owned(),
max: temperature,
critical,
connection: Some(c),
})
}
}
impl ComponentExt for Component {
fn temperature(&self) -> f32 {
self.temperature
}
fn max(&self) -> f32 {
self.max
}
fn critical(&self) -> Option<f32> {
self.critical
}
fn label(&self) -> &str {
&self.label
}
fn refresh(&mut self) {
if self.connection.is_none() {
self.connection = Connection::new()
.and_then(|x| x.initialize_security())
.and_then(|x| x.create_instance())
.and_then(|x| x.connect_server())
.and_then(|x| x.set_proxy_blanket());
}
self.connection = if let Some(x) = self.connection.take() {
x.exec_query()
} else {
None
};
if let Some(ref mut connection) = self.connection {
if let Some((temperature, _)) = connection.temperature(false) {
self.temperature = temperature;
if self.temperature > self.max {
self.max = self.temperature;
}
}
}
}
}
pub(crate) fn get_components() -> Vec<Component> {
match Component::new() {
Some(c) => vec![c],
None => Vec::new(),
}
}
struct Instance(*mut IWbemLocator);
impl Drop for Instance {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
(*self.0).Release();
}
}
}
}
struct ServerConnection(*mut IWbemServices);
impl Drop for ServerConnection {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
(*self.0).Release();
}
}
}
}
struct Enumerator(*mut IEnumWbemClassObject);
impl Drop for Enumerator {
fn drop(&mut self) {
if !self.0.is_null() {
unsafe {
(*self.0).Release();
}
}
}
}
macro_rules! bstr {
($($x:expr),*) => {{
let x: &[u16] = &[$($x as u16),*, 0];
SysAllocString(x.as_ptr())
}}
}
struct Connection {
instance: Option<Instance>,
server_connection: Option<ServerConnection>,
enumerator: Option<Enumerator>,
initialized: bool,
}
#[allow(clippy::non_send_fields_in_send_ty)]
unsafe impl Send for Connection {}
unsafe impl Sync for Connection {}
impl Connection {
#[allow(clippy::unnecessary_wraps)]
fn new() -> Option<Connection> {
unsafe {
let val = CoInitializeEx(null_mut(), 0);
Some(Connection {
instance: None,
server_connection: None,
enumerator: None,
initialized: val == S_OK || val == S_FALSE,
})
}
}
fn initialize_security(self) -> Option<Connection> {
unsafe {
if FAILED(CoInitializeSecurity(
null_mut(),
-1,
null_mut(),
null_mut(),
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
null_mut(),
EOAC_NONE,
null_mut(),
)) {
None
} else {
Some(self)
}
}
}
fn create_instance(mut self) -> Option<Connection> {
let mut p_loc = null_mut();
unsafe {
if FAILED(CoCreateInstance(
&CLSID_WbemLocator as *const _,
null_mut(),
CLSCTX_INPROC_SERVER,
&IID_IWbemLocator as *const _,
&mut p_loc as *mut _ as *mut _,
)) {
None
} else {
self.instance = Some(Instance(p_loc));
Some(self)
}
}
}
fn connect_server(mut self) -> Option<Connection> {
let mut p_svc = null_mut();
if let Some(ref instance) = self.instance {
unsafe {
// "root\WMI"
let s = bstr!('r', 'o', 'o', 't', '\\', 'W', 'M', 'I');
let res = (*instance.0).ConnectServer(
s,
null_mut(),
null_mut(),
null_mut(),
0,
null_mut(),
null_mut(),
&mut p_svc as *mut _,
);
SysFreeString(s);
if FAILED(res) {
return None;
}
}
} else {
return None;
}
self.server_connection = Some(ServerConnection(p_svc));
Some(self)
}
fn set_proxy_blanket(self) -> Option<Connection> {
if let Some(ref server_connection) = self.server_connection {
unsafe {
if FAILED(CoSetProxyBlanket(
server_connection.0 as *mut _,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
null_mut(),
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
null_mut(),
EOAC_NONE,
)) {
return None;
}
}
} else {
return None;
}
Some(self)
}
fn exec_query(mut self) -> Option<Connection> {
let mut p_enumerator = null_mut();
if let Some(ref server_connection) = self.server_connection {
unsafe {
// "WQL"
let s = bstr!('W', 'Q', 'L'); // query kind
// "SELECT * FROM MSAcpi_ThermalZoneTemperature"
let query = bstr!(
'S', 'E', 'L', 'E', 'C', 'T', ' ', '*', ' ', 'F', 'R', 'O', 'M', ' ', 'M', 'S',
'A', 'c', 'p', 'i', '_', 'T', 'h', 'e', 'r', 'm', 'a', 'l', 'Z', 'o', 'n', 'e',
'T', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u', 'r', 'e'
);
let hres = (*server_connection.0).ExecQuery(
s,
query,
(WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY) as _,
null_mut(),
&mut p_enumerator as *mut _,
);
SysFreeString(s);
SysFreeString(query);
if FAILED(hres) {
return None;
}
}
} else {
return None;
}
self.enumerator = Some(Enumerator(p_enumerator));
Some(self)
}
fn temperature(&mut self, get_critical: bool) -> Option<(f32, Option<f32>)> {
use winapi::um::wbemcli::WBEM_INFINITE;
let p_enum = match self.enumerator.take() {
Some(x) => x,
None => {
return None;
}
};
let mut p_obj: *mut IWbemClassObject = null_mut();
let mut nb_returned = 0;
unsafe {
(*p_enum.0).Next(
WBEM_INFINITE as _, // Time out
1, // One object
&mut p_obj as *mut _,
&mut nb_returned,
);
if nb_returned == 0 {
return None; // not enough rights I suppose...
}
(*p_obj).BeginEnumeration(WBEM_FLAG_NONSYSTEM_ONLY as _);
let mut p_val = std::mem::MaybeUninit::<VARIANT>::uninit();
// "CurrentTemperature"
let temp = bstr!(
'C', 'u', 'r', 'r', 'e', 'n', 't', 'T', 'e', 'm', 'p', 'e', 'r', 'a', 't', 'u',
'r', 'e'
);
let res = (*p_obj).Get(temp, 0, p_val.as_mut_ptr(), null_mut(), null_mut());
let mut p_val = p_val.assume_init();
SysFreeString(temp);
VariantClear(&mut p_val as *mut _ as *mut _);
let temp = if SUCCEEDED(res) {
// temperature is given in tenth of degrees Kelvin
(p_val.n1.decVal().Lo64 / 10) as f32 - 273.15
} else {
(*p_obj).Release();
return None;
};
let mut critical = None;
if get_critical {
// "CriticalPoint"
let crit = bstr!(
'C', 'r', 'i', 't', 'i', 'c', 'a', 'l', 'T', 'r', 'i', 'p', 'P', 'o', 'i', 'n',
't'
);
let res = (*p_obj).Get(crit, 0, &mut p_val, null_mut(), null_mut());
SysFreeString(crit);
VariantClear(&mut p_val as *mut _ as *mut _);
if SUCCEEDED(res) {
// temperature is given in tenth of degrees Kelvin
critical = Some((p_val.n1.decVal().Lo64 / 10) as f32 - 273.15);
}
}
(*p_obj).Release();
Some((temp, critical))
}
}
}
impl Drop for Connection {
fn drop(&mut self) {
// Those three calls are here to enforce that they get dropped in the good order.
self.enumerator.take();
self.server_connection.take();
self.instance.take();
if self.initialized {
unsafe {
CoUninitialize();
}
}
}
}
|