| 1 |
/* |
| 2 |
* Copyright (c) 2009, Jay Loden, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
Original code was refactored and moved from psutil/arch/freebsd/specific.c |
| 9 |
For reference, here's the git history with original(ish) implementations: |
| 10 |
- sensors_battery(): 022cf0a05d34f4274269d4f8002ee95b9f3e32d2 |
| 11 |
- sensors_cpu_temperature(): bb5d032be76980a9e110f03f1203bd35fa85a793 |
| 12 |
(patch by Alex Manuskin) |
| 13 |
*/ |
| 14 |
|
| 15 |
|
| 16 |
#include <Python.h> |
| 17 |
#include <sys/sysctl.h> |
| 18 |
|
| 19 |
#include "../../arch/all/init.h" |
| 20 |
|
| 21 |
|
| 22 |
#define DECIKELVIN_2_CELSIUS(t) (t - 2731) / 10 |
| 23 |
|
| 24 |
|
| 25 |
PyObject * |
| 26 |
psutil_sensors_battery(PyObject *self, PyObject *args) { |
| 27 |
int percent; |
| 28 |
int minsleft; |
| 29 |
int power_plugged; |
| 30 |
size_t size = sizeof(percent); |
| 31 |
|
| 32 |
if (psutil_sysctlbyname("hw.acpi.battery.life", &percent, size) != 0) |
| 33 |
goto error; |
| 34 |
if (psutil_sysctlbyname("hw.acpi.battery.time", &minsleft, size) != 0) |
| 35 |
goto error; |
| 36 |
if (psutil_sysctlbyname("hw.acpi.acline", &power_plugged, size) != 0) |
| 37 |
goto error; |
| 38 |
return Py_BuildValue("iii", percent, minsleft, power_plugged); |
| 39 |
|
| 40 |
error: |
| 41 |
// see: https://github.com/giampaolo/psutil/issues/1074 |
| 42 |
if (errno == ENOENT) |
| 43 |
PyErr_SetString(PyExc_NotImplementedError, "no battery"); |
| 44 |
else |
| 45 |
PyErr_SetFromErrno(PyExc_OSError); |
| 46 |
return NULL; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
// Return temperature information for a given CPU core number. |
| 51 |
PyObject * |
| 52 |
psutil_sensors_cpu_temperature(PyObject *self, PyObject *args) { |
| 53 |
int current; |
| 54 |
int tjmax; |
| 55 |
int core; |
| 56 |
char sensor[26]; |
| 57 |
size_t size = sizeof(current); |
| 58 |
|
| 59 |
if (! PyArg_ParseTuple(args, "i", &core)) |
| 60 |
return NULL; |
| 61 |
sprintf(sensor, "dev.cpu.%d.temperature", core); |
| 62 |
if (psutil_sysctlbyname(sensor, ¤t, size) != 0) |
| 63 |
goto error; |
| 64 |
current = DECIKELVIN_2_CELSIUS(current); |
| 65 |
|
| 66 |
// Return -273 in case of failure. |
| 67 |
sprintf(sensor, "dev.cpu.%d.coretemp.tjmax", core); |
| 68 |
if (psutil_sysctlbyname(sensor, &tjmax, size) != 0) |
| 69 |
tjmax = 0; |
| 70 |
tjmax = DECIKELVIN_2_CELSIUS(tjmax); |
| 71 |
|
| 72 |
return Py_BuildValue("ii", current, tjmax); |
| 73 |
|
| 74 |
error: |
| 75 |
if (errno == ENOENT) |
| 76 |
PyErr_SetString(PyExc_NotImplementedError, "no temperature sensors"); |
| 77 |
else |
| 78 |
PyErr_SetFromErrno(PyExc_OSError); |
| 79 |
return NULL; |
| 80 |
} |