]>
Commit | Line | Data |
---|---|---|
30777a6c MT |
1 | /* |
2 | * collecty | |
3 | * Copyright (C) 2015 IPFire Team (www.ipfire.org) | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include <Python.h> | |
20 | ||
21 | #include <atasmart.h> | |
22 | #include <errno.h> | |
23 | #include <fcntl.h> | |
24 | #include <linux/hdreg.h> | |
c389fccf MT |
25 | #include <sensors/error.h> |
26 | #include <sensors/sensors.h> | |
30777a6c MT |
27 | #include <stdbool.h> |
28 | #include <string.h> | |
29 | #include <sys/ioctl.h> | |
30 | ||
31 | #define MODEL_SIZE 40 | |
32 | #define SERIAL_SIZE 20 | |
33 | ||
34 | typedef struct { | |
35 | PyObject_HEAD | |
36 | char* path; | |
37 | struct hd_driveid identity; | |
38 | SkDisk* disk; | |
39 | } BlockDevice; | |
40 | ||
41 | static void BlockDevice_dealloc(BlockDevice* self) { | |
42 | if (self->disk) | |
43 | sk_disk_free(self->disk); | |
44 | ||
45 | if (self->path) | |
46 | free(self->path); | |
47 | ||
48 | self->ob_type->tp_free((PyObject*)self); | |
49 | } | |
50 | ||
51 | static int BlockDevice_get_identity(BlockDevice* device) { | |
52 | int fd; | |
53 | ||
54 | if ((fd = open(device->path, O_RDONLY | O_NONBLOCK)) < 0) { | |
55 | return 1; | |
56 | } | |
57 | ||
58 | int r = ioctl(fd, HDIO_GET_IDENTITY, &device->identity); | |
59 | close(fd); | |
60 | ||
61 | if (r) | |
62 | return 1; | |
63 | ||
64 | return 0; | |
65 | } | |
66 | ||
67 | static int BlockDevice_smart_is_available(BlockDevice* device) { | |
68 | SkBool available = FALSE; | |
69 | ||
70 | int r = sk_disk_smart_is_available(device->disk, &available); | |
71 | if (r) | |
72 | return -1; | |
73 | ||
74 | if (available) | |
75 | return 0; | |
76 | ||
77 | return 1; | |
78 | } | |
79 | ||
80 | static int BlockDevice_check_sleep_mode(BlockDevice* device) { | |
81 | SkBool awake = FALSE; | |
82 | ||
83 | int r = sk_disk_check_sleep_mode(device->disk, &awake); | |
84 | if (r) | |
85 | return -1; | |
86 | ||
87 | if (awake) | |
88 | return 0; | |
89 | ||
90 | return 1; | |
91 | } | |
92 | ||
93 | static PyObject * BlockDevice_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
94 | BlockDevice* self = (BlockDevice*)type->tp_alloc(type, 0); | |
95 | ||
96 | if (self) { | |
97 | self->path = NULL; | |
98 | ||
99 | // libatasmart | |
100 | self->disk = NULL; | |
101 | } | |
102 | ||
103 | return (PyObject *)self; | |
104 | } | |
105 | ||
106 | static int BlockDevice_init(BlockDevice* self, PyObject* args, PyObject* kwds) { | |
107 | const char* path = NULL; | |
108 | ||
109 | if (!PyArg_ParseTuple(args, "s", &path)) | |
110 | return -1; | |
111 | ||
112 | self->path = strdup(path); | |
113 | ||
114 | int r = BlockDevice_get_identity(self); | |
115 | if (r) { | |
116 | PyErr_Format(PyExc_OSError, "Could not open block device: %s", path); | |
117 | return -1; | |
118 | } | |
119 | ||
120 | r = sk_disk_open(path, &self->disk); | |
121 | if (r == 0) { | |
122 | if (BlockDevice_smart_is_available(self) == 0) { | |
123 | if (BlockDevice_check_sleep_mode(self) == 0) { | |
124 | r = sk_disk_smart_read_data(self->disk); | |
125 | if (r) { | |
126 | PyErr_Format(PyExc_OSError, "Could not open block device %s: %s", path, | |
127 | strerror(errno)); | |
128 | return -1; | |
129 | } | |
130 | } | |
131 | } | |
132 | } else { | |
133 | PyErr_Format(PyExc_OSError, "Could not open block device %s: %s", path, | |
134 | strerror(errno)); | |
135 | return -1; | |
136 | } | |
137 | ||
138 | //sk_disk_identify_is_available | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
143 | static PyObject* BlockDevice_get_path(PyObject* self) { | |
144 | BlockDevice* device = (BlockDevice*)self; | |
145 | ||
146 | return PyString_FromString(device->path); | |
147 | } | |
148 | ||
149 | static void clean_string(char *s) { | |
150 | for (char* e = s; *e; e++) { | |
151 | if (*e < ' ' || *e >= 127) | |
152 | *e = ' '; | |
153 | } | |
154 | } | |
155 | ||
156 | static void drop_spaces(char *s) { | |
157 | char *d = s; | |
158 | bool prev_space = false; | |
159 | ||
160 | s += strspn(s, " "); | |
161 | ||
162 | for (; *s; s++) { | |
163 | if (prev_space) { | |
164 | if (*s != ' ') { | |
165 | prev_space = false; | |
166 | *(d++) = ' '; | |
167 | *(d++) = *s; | |
168 | } | |
169 | } else { | |
170 | if (*s == ' ') | |
171 | prev_space = true; | |
172 | else | |
173 | *(d++) = *s; | |
174 | } | |
175 | } | |
176 | ||
177 | *d = 0; | |
178 | } | |
179 | ||
180 | static void copy_string(char* d, const char* s, size_t n) { | |
181 | // Copy the source buffer to the destination buffer up to n | |
182 | memcpy(d, s, n); | |
183 | ||
184 | // Terminate the destination buffer with NULL | |
185 | d[n] = '\0'; | |
186 | ||
187 | // Clean up the string from non-printable characters | |
188 | clean_string(d); | |
189 | drop_spaces(d); | |
190 | } | |
191 | ||
192 | static PyObject* BlockDevice_get_model(PyObject* self) { | |
193 | BlockDevice* device = (BlockDevice*)self; | |
194 | ||
195 | char model[MODEL_SIZE + 1]; | |
196 | copy_string(model, device->identity.model, sizeof(model)); | |
197 | ||
198 | return PyString_FromString(model); | |
199 | } | |
200 | ||
201 | static PyObject* BlockDevice_get_serial(PyObject* self) { | |
202 | BlockDevice* device = (BlockDevice*)self; | |
203 | ||
204 | char serial[SERIAL_SIZE + 1]; | |
205 | copy_string(serial, device->identity.serial_no, sizeof(serial)); | |
206 | ||
207 | return PyString_FromString(serial); | |
208 | } | |
209 | ||
210 | static PyObject* BlockDevice_is_smart_supported(PyObject* self) { | |
211 | BlockDevice* device = (BlockDevice*)self; | |
212 | ||
213 | if (BlockDevice_smart_is_available(device) == 0) | |
214 | Py_RETURN_TRUE; | |
215 | ||
216 | Py_RETURN_FALSE; | |
217 | } | |
218 | ||
219 | static PyObject* BlockDevice_is_awake(PyObject* self) { | |
220 | BlockDevice* device = (BlockDevice*)self; | |
221 | ||
222 | if (BlockDevice_check_sleep_mode(device) == 0) | |
223 | Py_RETURN_TRUE; | |
224 | ||
225 | Py_RETURN_FALSE; | |
226 | } | |
227 | ||
228 | static PyObject* BlockDevice_get_bad_sectors(PyObject* self) { | |
229 | BlockDevice* device = (BlockDevice*)self; | |
230 | ||
231 | if (BlockDevice_smart_is_available(device)) { | |
232 | PyErr_Format(PyExc_OSError, "Device does not support SMART"); | |
233 | return NULL; | |
234 | } | |
235 | ||
236 | uint64_t bad_sectors; | |
237 | int r = sk_disk_smart_get_bad(device->disk, &bad_sectors); | |
238 | if (r) | |
239 | return NULL; | |
240 | ||
241 | return PyLong_FromUnsignedLongLong((unsigned long long)bad_sectors); | |
242 | } | |
243 | ||
244 | static PyObject* BlockDevice_get_temperature(PyObject* self) { | |
245 | BlockDevice* device = (BlockDevice*)self; | |
246 | ||
247 | if (BlockDevice_smart_is_available(device)) { | |
248 | PyErr_Format(PyExc_OSError, "Device does not support SMART"); | |
249 | return NULL; | |
250 | } | |
251 | ||
252 | uint64_t mkelvin; | |
253 | int r = sk_disk_smart_get_temperature(device->disk, &mkelvin); | |
254 | if (r) | |
255 | return NULL; | |
256 | ||
6f04ea33 MT |
257 | // Convert the temperature to Kelvin |
258 | return PyFloat_FromDouble((double)mkelvin / 1000.0); | |
30777a6c MT |
259 | } |
260 | ||
261 | static PyGetSetDef BlockDevice_getsetters[] = { | |
262 | {"path", (getter)BlockDevice_get_path, NULL, NULL, NULL}, | |
263 | {"model", (getter)BlockDevice_get_model, NULL, NULL, NULL}, | |
264 | {"serial", (getter)BlockDevice_get_serial, NULL, NULL, NULL}, | |
265 | }; | |
266 | ||
267 | static PyMethodDef BlockDevice_methods[] = { | |
268 | {"get_bad_sectors", (PyCFunction)BlockDevice_get_bad_sectors, METH_NOARGS, NULL}, | |
269 | {"get_temperature", (PyCFunction)BlockDevice_get_temperature, METH_NOARGS, NULL}, | |
270 | {"is_smart_supported", (PyCFunction)BlockDevice_is_smart_supported, METH_NOARGS, NULL}, | |
271 | {"is_awake", (PyCFunction)BlockDevice_is_awake, METH_NOARGS, NULL}, | |
272 | {NULL} | |
273 | }; | |
274 | ||
275 | static PyTypeObject BlockDeviceType = { | |
276 | PyObject_HEAD_INIT(NULL) | |
277 | 0, /*ob_size*/ | |
278 | "_collecty.BlockDevice", /*tp_name*/ | |
279 | sizeof(BlockDevice), /*tp_basicsize*/ | |
280 | 0, /*tp_itemsize*/ | |
281 | (destructor)BlockDevice_dealloc, /*tp_dealloc*/ | |
282 | 0, /*tp_print*/ | |
283 | 0, /*tp_getattr*/ | |
284 | 0, /*tp_setattr*/ | |
285 | 0, /*tp_compare*/ | |
286 | 0, /*tp_repr*/ | |
287 | 0, /*tp_as_number*/ | |
288 | 0, /*tp_as_sequence*/ | |
289 | 0, /*tp_as_mapping*/ | |
290 | 0, /*tp_hash */ | |
291 | 0, /*tp_call*/ | |
292 | 0, /*tp_str*/ | |
293 | 0, /*tp_getattro*/ | |
294 | 0, /*tp_setattro*/ | |
295 | 0, /*tp_as_buffer*/ | |
296 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
297 | "BlockDevice objects", /* tp_doc */ | |
298 | 0, /* tp_traverse */ | |
299 | 0, /* tp_clear */ | |
300 | 0, /* tp_richcompare */ | |
301 | 0, /* tp_weaklistoffset */ | |
302 | 0, /* tp_iter */ | |
303 | 0, /* tp_iternext */ | |
304 | BlockDevice_methods, /* tp_methods */ | |
305 | 0, /* tp_members */ | |
306 | BlockDevice_getsetters, /* tp_getset */ | |
307 | 0, /* tp_base */ | |
308 | 0, /* tp_dict */ | |
309 | 0, /* tp_descr_get */ | |
310 | 0, /* tp_descr_set */ | |
311 | 0, /* tp_dictoffset */ | |
312 | (initproc)BlockDevice_init, /* tp_init */ | |
313 | 0, /* tp_alloc */ | |
314 | BlockDevice_new, /* tp_new */ | |
315 | }; | |
316 | ||
c389fccf MT |
317 | typedef struct { |
318 | PyObject_HEAD | |
319 | const sensors_chip_name* chip; | |
320 | const sensors_feature* feature; | |
321 | } SensorObject; | |
322 | ||
323 | static void Sensor_dealloc(SensorObject* self) { | |
324 | self->ob_type->tp_free((PyObject*)self); | |
325 | } | |
326 | ||
327 | static PyObject* Sensor_new(PyTypeObject* type, PyObject* args, PyObject* kwds) { | |
328 | SensorObject* self = (SensorObject*)type->tp_alloc(type, 0); | |
329 | ||
330 | return (PyObject *)self; | |
331 | } | |
332 | ||
333 | static int Sensor_init(SensorObject* self, PyObject* args, PyObject* kwds) { | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static PyObject* Sensor_get_label(SensorObject* self) { | |
338 | char* label = sensors_get_label(self->chip, self->feature); | |
339 | ||
340 | if (label) { | |
341 | PyObject* string = PyString_FromString(label); | |
342 | free(label); | |
343 | ||
344 | return string; | |
345 | } | |
346 | ||
347 | Py_RETURN_NONE; | |
348 | } | |
349 | ||
350 | static PyObject* Sensor_get_name(SensorObject* self) { | |
351 | char chip_name[512]; | |
352 | ||
353 | int r = sensors_snprintf_chip_name(chip_name, sizeof(chip_name), self->chip); | |
354 | if (r < 0) { | |
355 | PyErr_Format(PyExc_RuntimeError, "Could not print chip name"); | |
356 | return NULL; | |
357 | } | |
358 | ||
359 | return PyString_FromString(chip_name); | |
360 | } | |
361 | ||
362 | static PyObject* Sensor_get_type(SensorObject* self) { | |
363 | const char* type = NULL; | |
364 | ||
365 | switch (self->feature->type) { | |
366 | case SENSORS_FEATURE_IN: | |
367 | type = "voltage"; | |
368 | break; | |
369 | ||
370 | case SENSORS_FEATURE_FAN: | |
371 | type = "fan"; | |
372 | break; | |
373 | ||
374 | case SENSORS_FEATURE_TEMP: | |
375 | type = "temperature"; | |
376 | break; | |
377 | ||
378 | case SENSORS_FEATURE_POWER: | |
379 | type = "power"; | |
380 | break; | |
381 | ||
382 | default: | |
383 | break; | |
384 | } | |
385 | ||
386 | if (type) | |
387 | return PyString_FromString(type); | |
388 | ||
389 | Py_RETURN_NONE; | |
390 | } | |
391 | ||
392 | static PyObject* Sensor_get_bus(SensorObject* self) { | |
393 | const char* type = NULL; | |
394 | ||
395 | switch (self->chip->bus.type) { | |
396 | case SENSORS_BUS_TYPE_I2C: | |
397 | type = "i2c"; | |
398 | break; | |
399 | ||
400 | case SENSORS_BUS_TYPE_ISA: | |
401 | type = "isa"; | |
402 | break; | |
403 | ||
404 | case SENSORS_BUS_TYPE_PCI: | |
405 | type = "pci"; | |
406 | break; | |
407 | ||
408 | case SENSORS_BUS_TYPE_SPI: | |
409 | type = "spi"; | |
410 | break; | |
411 | ||
412 | case SENSORS_BUS_TYPE_VIRTUAL: | |
413 | type = "virtual"; | |
414 | break; | |
415 | ||
416 | case SENSORS_BUS_TYPE_ACPI: | |
417 | type = "acpi"; | |
418 | break; | |
419 | ||
420 | case SENSORS_BUS_TYPE_HID: | |
421 | type = "hid"; | |
422 | break; | |
423 | ||
424 | default: | |
425 | break; | |
426 | } | |
427 | ||
428 | if (type) | |
429 | return PyString_FromString(type); | |
430 | ||
431 | Py_RETURN_NONE; | |
432 | } | |
433 | ||
434 | static const sensors_subfeature* Sensor_get_subfeature(SensorObject* sensor, sensors_subfeature_type type) { | |
435 | const sensors_subfeature* subfeature; | |
436 | int subfeature_num = 0; | |
437 | ||
438 | while ((subfeature = sensors_get_all_subfeatures(sensor->chip, sensor->feature, &subfeature_num))) { | |
439 | if (subfeature->type == type) | |
440 | break; | |
441 | } | |
442 | ||
443 | return subfeature; | |
444 | } | |
445 | ||
446 | static PyObject* Sensor_return_value(SensorObject* sensor, sensors_subfeature_type subfeature_type) { | |
447 | double value; | |
448 | ||
449 | const sensors_subfeature* subfeature = Sensor_get_subfeature(sensor, subfeature_type); | |
450 | if (!subfeature) { | |
451 | PyErr_Format(PyExc_AttributeError, "Could not find sensor of requested type"); | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | // Fetch value from the sensor | |
456 | int r = sensors_get_value(sensor->chip, subfeature->number, &value); | |
457 | if (r < 0) { | |
458 | PyErr_Format(PyExc_ValueError, "Error retrieving value from sensor: %s", | |
459 | sensors_strerror(errno)); | |
460 | return NULL; | |
461 | } | |
462 | ||
463 | // Convert all temperature values from Celcius to Kelvon | |
464 | if (sensor->feature->type == SENSORS_FEATURE_TEMP) | |
465 | value += 273.15; | |
466 | ||
467 | return PyFloat_FromDouble(value); | |
468 | } | |
469 | ||
470 | static PyObject* Sensor_no_value() { | |
471 | PyErr_Format(PyExc_ValueError, "Value not supported for this sensor type"); | |
472 | return NULL; | |
473 | } | |
474 | ||
475 | static PyObject* Sensor_get_value(SensorObject* self) { | |
476 | sensors_subfeature_type subfeature_type; | |
477 | ||
478 | switch (self->feature->type) { | |
479 | case SENSORS_FEATURE_IN: | |
480 | subfeature_type = SENSORS_SUBFEATURE_IN_INPUT; | |
481 | break; | |
482 | ||
483 | case SENSORS_FEATURE_FAN: | |
484 | subfeature_type = SENSORS_SUBFEATURE_FAN_INPUT; | |
485 | break; | |
486 | ||
487 | case SENSORS_FEATURE_TEMP: | |
488 | subfeature_type = SENSORS_SUBFEATURE_TEMP_INPUT; | |
489 | break; | |
490 | ||
491 | case SENSORS_FEATURE_POWER: | |
492 | subfeature_type = SENSORS_SUBFEATURE_POWER_INPUT; | |
493 | break; | |
494 | ||
495 | default: | |
496 | return Sensor_no_value(); | |
497 | } | |
498 | ||
499 | return Sensor_return_value(self, subfeature_type); | |
500 | } | |
501 | ||
502 | static PyObject* Sensor_get_critical(SensorObject* self) { | |
503 | sensors_subfeature_type subfeature_type; | |
504 | ||
505 | switch (self->feature->type) { | |
506 | case SENSORS_FEATURE_IN: | |
507 | subfeature_type = SENSORS_SUBFEATURE_IN_CRIT; | |
508 | break; | |
509 | ||
510 | case SENSORS_FEATURE_TEMP: | |
511 | subfeature_type = SENSORS_SUBFEATURE_TEMP_CRIT; | |
512 | break; | |
513 | ||
514 | case SENSORS_FEATURE_POWER: | |
515 | subfeature_type = SENSORS_SUBFEATURE_POWER_CRIT; | |
516 | break; | |
517 | ||
518 | default: | |
519 | return Sensor_no_value(); | |
520 | } | |
521 | ||
522 | return Sensor_return_value(self, subfeature_type); | |
523 | } | |
524 | ||
525 | static PyObject* Sensor_get_maximum(SensorObject* self) { | |
526 | sensors_subfeature_type subfeature_type; | |
527 | ||
528 | switch (self->feature->type) { | |
529 | case SENSORS_FEATURE_IN: | |
530 | subfeature_type = SENSORS_SUBFEATURE_IN_MAX; | |
531 | break; | |
532 | ||
533 | case SENSORS_FEATURE_FAN: | |
534 | subfeature_type = SENSORS_SUBFEATURE_FAN_MAX; | |
535 | break; | |
536 | ||
537 | case SENSORS_FEATURE_TEMP: | |
538 | subfeature_type = SENSORS_SUBFEATURE_TEMP_MAX; | |
539 | break; | |
540 | ||
541 | case SENSORS_FEATURE_POWER: | |
542 | subfeature_type = SENSORS_SUBFEATURE_POWER_MAX; | |
543 | break; | |
544 | ||
545 | default: | |
546 | return Sensor_no_value(); | |
547 | } | |
548 | ||
549 | return Sensor_return_value(self, subfeature_type); | |
550 | } | |
551 | ||
552 | static PyObject* Sensor_get_minimum(SensorObject* self) { | |
553 | sensors_subfeature_type subfeature_type; | |
554 | ||
555 | switch (self->feature->type) { | |
556 | case SENSORS_FEATURE_IN: | |
557 | subfeature_type = SENSORS_SUBFEATURE_IN_MIN; | |
558 | break; | |
559 | ||
560 | case SENSORS_FEATURE_FAN: | |
561 | subfeature_type = SENSORS_SUBFEATURE_FAN_MIN; | |
562 | break; | |
563 | ||
564 | case SENSORS_FEATURE_TEMP: | |
565 | subfeature_type = SENSORS_SUBFEATURE_TEMP_MIN; | |
566 | break; | |
567 | ||
568 | default: | |
569 | return Sensor_no_value(); | |
570 | } | |
571 | ||
572 | return Sensor_return_value(self, subfeature_type); | |
573 | } | |
574 | ||
575 | static PyObject* Sensor_get_high(SensorObject* self) { | |
576 | sensors_subfeature_type subfeature_type; | |
577 | ||
578 | switch (self->feature->type) { | |
579 | case SENSORS_FEATURE_TEMP: | |
580 | subfeature_type = SENSORS_SUBFEATURE_TEMP_MAX; | |
581 | break; | |
582 | ||
583 | default: | |
584 | return Sensor_no_value(); | |
585 | } | |
586 | ||
587 | return Sensor_return_value(self, subfeature_type); | |
588 | } | |
589 | ||
590 | static PyGetSetDef Sensor_getsetters[] = { | |
591 | {"bus", (getter)Sensor_get_bus, NULL, NULL, NULL}, | |
592 | {"critical", (getter)Sensor_get_critical, NULL, NULL, NULL}, | |
593 | {"high", (getter)Sensor_get_high, NULL, NULL, NULL}, | |
594 | {"label", (getter)Sensor_get_label, NULL, NULL, NULL}, | |
595 | {"maximum", (getter)Sensor_get_maximum, NULL, NULL, NULL}, | |
596 | {"minumum", (getter)Sensor_get_minimum, NULL, NULL, NULL}, | |
597 | {"name", (getter)Sensor_get_name, NULL, NULL, NULL}, | |
598 | {"type", (getter)Sensor_get_type, NULL, NULL, NULL}, | |
599 | {"value", (getter)Sensor_get_value, NULL, NULL, NULL}, | |
600 | {NULL}, | |
601 | }; | |
602 | ||
603 | static PyTypeObject SensorType = { | |
604 | PyObject_HEAD_INIT(NULL) | |
605 | 0, /*ob_size*/ | |
606 | "_collecty.Sensor", /*tp_name*/ | |
607 | sizeof(SensorObject), /*tp_basicsize*/ | |
608 | 0, /*tp_itemsize*/ | |
609 | (destructor)Sensor_dealloc, /*tp_dealloc*/ | |
610 | 0, /*tp_print*/ | |
611 | 0, /*tp_getattr*/ | |
612 | 0, /*tp_setattr*/ | |
613 | 0, /*tp_compare*/ | |
614 | 0, /*tp_repr*/ | |
615 | 0, /*tp_as_number*/ | |
616 | 0, /*tp_as_sequence*/ | |
617 | 0, /*tp_as_mapping*/ | |
618 | 0, /*tp_hash */ | |
619 | 0, /*tp_call*/ | |
620 | 0, /*tp_str*/ | |
621 | 0, /*tp_getattro*/ | |
622 | 0, /*tp_setattro*/ | |
623 | 0, /*tp_as_buffer*/ | |
624 | Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE, /*tp_flags*/ | |
625 | "Sensor objects", /* tp_doc */ | |
626 | 0, /* tp_traverse */ | |
627 | 0, /* tp_clear */ | |
628 | 0, /* tp_richcompare */ | |
629 | 0, /* tp_weaklistoffset */ | |
630 | 0, /* tp_iter */ | |
631 | 0, /* tp_iternext */ | |
632 | 0, /* tp_methods */ | |
633 | 0, /* tp_members */ | |
634 | Sensor_getsetters, /* tp_getset */ | |
635 | 0, /* tp_base */ | |
636 | 0, /* tp_dict */ | |
637 | 0, /* tp_descr_get */ | |
638 | 0, /* tp_descr_set */ | |
639 | 0, /* tp_dictoffset */ | |
640 | (initproc)Sensor_init, /* tp_init */ | |
641 | 0, /* tp_alloc */ | |
642 | Sensor_new, /* tp_new */ | |
643 | }; | |
644 | ||
645 | static SensorObject* make_sensor_object(const sensors_chip_name* chip, const sensors_feature* feature) { | |
646 | SensorObject* sensor = PyObject_New(SensorObject, &SensorType); | |
647 | if (!sensor) | |
648 | return NULL; | |
649 | ||
650 | if (!PyObject_Init((PyObject*)sensor, &SensorType)) { | |
651 | Py_DECREF(sensor); | |
652 | return NULL; | |
653 | } | |
654 | ||
655 | sensor->chip = chip; | |
656 | sensor->feature = feature; | |
657 | ||
658 | return sensor; | |
659 | } | |
660 | ||
661 | static PyObject* _collecty_sensors_init() { | |
662 | // Clean up everything first in case sensors_init was called earlier | |
663 | sensors_cleanup(); | |
664 | ||
665 | int r = sensors_init(NULL); | |
666 | if (r) { | |
667 | PyErr_Format(PyExc_OSError, "Could not initialise sensors: %s", | |
668 | sensors_strerror(errno)); | |
669 | return NULL; | |
670 | } | |
671 | ||
672 | Py_RETURN_NONE; | |
673 | } | |
674 | ||
675 | static PyObject* _collecty_sensors_cleanup() { | |
676 | sensors_cleanup(); | |
677 | Py_RETURN_NONE; | |
678 | } | |
679 | ||
680 | static PyObject* _collecty_get_detected_sensors(PyObject* o, PyObject* args) { | |
681 | const char* name = NULL; | |
682 | sensors_chip_name chip_name; | |
683 | ||
684 | if (!PyArg_ParseTuple(args, "|z", &name)) | |
685 | return NULL; | |
686 | ||
687 | if (name) { | |
688 | int r = sensors_parse_chip_name(name, &chip_name); | |
689 | if (r < 0) { | |
690 | PyErr_Format(PyExc_ValueError, "Could not parse chip name: %s", name); | |
691 | return NULL; | |
692 | } | |
693 | } | |
694 | ||
695 | PyObject* list = PyList_New(0); | |
696 | ||
697 | const sensors_chip_name* chip; | |
698 | int chip_num = 0; | |
699 | ||
700 | while ((chip = sensors_get_detected_chips((name) ? &chip_name : NULL, &chip_num))) { | |
701 | const sensors_feature* feature; | |
702 | int feature_num = 0; | |
703 | ||
704 | while ((feature = sensors_get_features(chip, &feature_num))) { | |
705 | // Skip sensors we do not want to support | |
706 | switch (feature->type) { | |
707 | case SENSORS_FEATURE_IN: | |
708 | case SENSORS_FEATURE_FAN: | |
709 | case SENSORS_FEATURE_TEMP: | |
710 | case SENSORS_FEATURE_POWER: | |
711 | break; | |
712 | ||
713 | default: | |
714 | continue; | |
715 | } | |
716 | ||
717 | SensorObject* sensor = make_sensor_object(chip, feature); | |
718 | PyList_Append(list, (PyObject*)sensor); | |
719 | } | |
720 | } | |
721 | ||
722 | return list; | |
723 | } | |
724 | ||
30777a6c | 725 | static PyMethodDef collecty_module_methods[] = { |
c389fccf MT |
726 | {"get_detected_sensors", (PyCFunction)_collecty_get_detected_sensors, METH_VARARGS, NULL}, |
727 | {"sensors_cleanup", (PyCFunction)_collecty_sensors_cleanup, METH_NOARGS, NULL}, | |
728 | {"sensors_init", (PyCFunction)_collecty_sensors_init, METH_NOARGS, NULL}, | |
30777a6c MT |
729 | {NULL}, |
730 | }; | |
731 | ||
732 | void init_collecty(void) { | |
733 | if (PyType_Ready(&BlockDeviceType) < 0) | |
734 | return; | |
735 | ||
c389fccf MT |
736 | if (PyType_Ready(&SensorType) < 0) |
737 | return; | |
738 | ||
30777a6c MT |
739 | PyObject* m = Py_InitModule("_collecty", collecty_module_methods); |
740 | ||
741 | PyModule_AddObject(m, "BlockDevice", (PyObject*)&BlockDeviceType); | |
c389fccf | 742 | PyModule_AddObject(m, "Sensor", (PyObject*)&SensorType); |
30777a6c | 743 | } |