2017-01-06 14:15:30 -08:00
|
|
|
#include "thermal.h"
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
# include <sys/types.h>
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <errno.h>
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__OpenBSD__)
|
|
|
|
# include <sys/param.h>
|
|
|
|
# include <sys/sysctl.h>
|
|
|
|
# include <sys/sensors.h>
|
|
|
|
# include <errno.h>
|
|
|
|
# include <err.h>
|
2017-01-06 14:15:30 -08:00
|
|
|
#endif
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
int mib[CTL_MAXNAME];
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
unsigned int miblen;
|
|
|
|
#endif
|
|
|
|
int dummy;
|
|
|
|
} Extn;
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
static const char *sources[] =
|
|
|
|
{
|
|
|
|
"hw.acpi.thermal.tz0.temperature",
|
|
|
|
"dev.cpu.0.temperature",
|
|
|
|
"dev.aibs.0.temp.0",
|
|
|
|
"dev.lm75.0.temperature",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
static struct sensor snsr;
|
|
|
|
static size_t slen = sizeof(snsr);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void
|
|
|
|
init(Tempthread *tth)
|
|
|
|
{
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
int dev, numt;
|
|
|
|
struct sensordev snsrdev;
|
|
|
|
size_t sdlen = sizeof(snsrdev);
|
|
|
|
#endif
|
2017-02-02 13:48:48 -08:00
|
|
|
|
2017-07-15 09:14:41 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
unsigned i;
|
|
|
|
size_t len;
|
|
|
|
int rc;
|
|
|
|
#endif
|
|
|
|
|
2017-02-02 13:48:48 -08:00
|
|
|
Extn *extn;
|
2017-01-06 14:15:30 -08:00
|
|
|
if (tth->initted) return;
|
|
|
|
tth->initted = EINA_TRUE;
|
|
|
|
|
|
|
|
extn = calloc(1, sizeof(Extn));
|
|
|
|
tth->extn = extn;
|
|
|
|
|
|
|
|
if ((!tth->sensor_type) ||
|
|
|
|
((!tth->sensor_name) ||
|
|
|
|
(tth->sensor_name[0] == 0)))
|
|
|
|
{
|
|
|
|
eina_stringshare_del(tth->sensor_name);
|
|
|
|
tth->sensor_name = NULL;
|
|
|
|
eina_stringshare_del(tth->sensor_path);
|
|
|
|
tth->sensor_path = NULL;
|
2017-02-02 13:48:48 -08:00
|
|
|
|
2017-07-15 09:14:41 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
for (i = 0; sources[i]; i++)
|
|
|
|
{
|
|
|
|
rc = sysctlbyname(sources[i], NULL, NULL, NULL, 0);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
tth->sensor_type = SENSOR_TYPE_FREEBSD;
|
|
|
|
tth->sensor_name = eina_stringshare_add(sources[i]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-02-02 13:48:48 -08:00
|
|
|
|
2017-01-06 14:15:30 -08:00
|
|
|
#elif defined(__OpenBSD__)
|
|
|
|
extn->mib[0] = CTL_HW;
|
|
|
|
extn->mib[1] = HW_SENSORS;
|
|
|
|
|
|
|
|
for (dev = 0;; dev++)
|
|
|
|
{
|
|
|
|
extn->mib[2] = dev;
|
|
|
|
if (sysctl(extn->mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT) /* no further sensors */
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strcmp(snsrdev.xname, "cpu0") == 0)
|
|
|
|
{
|
|
|
|
tth->sensor_type = SENSOR_TYPE_OPENBSD;
|
2017-02-02 13:48:48 -08:00
|
|
|
tth->sensor_name = eina_stringshare_add("cpu0");
|
2017-01-06 14:15:30 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (strcmp(snsrdev.xname, "km0") == 0)
|
|
|
|
{
|
|
|
|
tth->sensor_type = SENSOR_TYPE_OPENBSD;
|
2017-02-02 13:48:48 -08:00
|
|
|
tth->sensor_name = eina_stringshare_add("km0");
|
2017-01-06 14:15:30 -08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if ((tth->sensor_type) && (tth->sensor_name) && (!tth->sensor_path))
|
|
|
|
{
|
|
|
|
if (tth->sensor_type == SENSOR_TYPE_FREEBSD)
|
|
|
|
{
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
len = sizeof(extn->mib) / sizeof(extn->mib[0]);
|
|
|
|
rc = sysctlnametomib(tth->sensor_name, extn->mib, &len);
|
|
|
|
if (rc == 0)
|
|
|
|
{
|
|
|
|
extn->miblen = len;
|
|
|
|
tth->sensor_path = eina_stringshare_add(tth->sensor_name);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else if (tth->sensor_type == SENSOR_TYPE_OPENBSD)
|
|
|
|
{
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
for (numt = 0; numt < snsrdev.maxnumt[SENSOR_TEMP]; numt++)
|
|
|
|
{
|
|
|
|
extn->mib[4] = numt;
|
|
|
|
slen = sizeof(snsr);
|
|
|
|
if (sysctl(extn->mib, 5, &snsr, &slen, NULL, 0) == -1)
|
|
|
|
continue;
|
|
|
|
if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
check(Tempthread *tth)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int temp = 0;
|
2017-07-15 09:14:41 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
size_t len;
|
|
|
|
size_t ftemp = 0;
|
|
|
|
#endif
|
2017-07-15 09:14:41 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
Extn *extn = tth->extn;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* TODO: Make standard parser. Seems to be two types of temperature string:
|
|
|
|
* - Somename: <temp> C
|
|
|
|
* - <temp>
|
|
|
|
*/
|
|
|
|
if (tth->sensor_type == SENSOR_TYPE_FREEBSD)
|
|
|
|
{
|
2017-07-15 09:14:41 -07:00
|
|
|
#if defined(__FreeBSD__) || defined(__DragonFly__)
|
2017-01-06 14:15:30 -08:00
|
|
|
len = sizeof(ftemp);
|
|
|
|
if (sysctl(extn->mib, extn->miblen, &ftemp, &len, NULL, 0) == 0)
|
|
|
|
{
|
|
|
|
temp = (ftemp - 2732) / 10;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
#endif
|
2017-01-07 02:49:57 -08:00
|
|
|
}
|
2017-01-06 14:15:30 -08:00
|
|
|
else if (tth->sensor_type == SENSOR_TYPE_OPENBSD)
|
|
|
|
{
|
2017-02-02 13:48:48 -08:00
|
|
|
#if defined(__OpenBSD__)
|
2017-01-06 14:15:30 -08:00
|
|
|
if (sysctl(extn->mib, 5, &snsr, &slen, NULL, 0) != -1)
|
|
|
|
{
|
|
|
|
temp = (snsr.value - 273150000) / 1000000.0;
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
if (ret) return temp;
|
|
|
|
|
|
|
|
return -999;
|
|
|
|
error:
|
|
|
|
tth->sensor_type = SENSOR_TYPE_NONE;
|
|
|
|
eina_stringshare_del(tth->sensor_name);
|
|
|
|
tth->sensor_name = NULL;
|
|
|
|
eina_stringshare_del(tth->sensor_path);
|
|
|
|
tth->sensor_path = NULL;
|
|
|
|
return -999;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
thermal_sysctl_get(Tempthread *tth)
|
|
|
|
{
|
|
|
|
int temp;
|
|
|
|
|
|
|
|
init(tth);
|
|
|
|
temp = check(tth);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
#endif
|