6 #include <yajl/yajl_gen.h>
7 #include <yajl/yajl_version.h>
11 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
13 #include <sys/types.h>
14 #include <sys/sysctl.h>
16 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
19 #if defined(__OpenBSD__)
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/sysctl.h>
23 #include <sys/sensors.h>
28 static char *thermal_zone;
31 * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
32 * returns the temperature in degree celcius.
35 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format) {
38 char *outwalk = buffer;
42 asprintf(&thermal_zone, THERMAL_ZONE, zone);
48 for (walk = format; *walk != '\0'; walk++) {
54 if (BEGINS_WITH(walk+1, "degrees")) {
57 if (!slurp(path, buf, sizeof(buf)))
59 temp = strtol(buf, NULL, 10);
60 if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
63 outwalk += sprintf(outwalk, "%ld", (temp/1000));
64 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
66 size_t sysctl_size = sizeof(sysctl_rslt);
67 if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
70 outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
71 #elif defined(__OpenBSD__)
72 struct sensordev sensordev;
75 int dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
77 sdlen = sizeof(sensordev);
78 slen = sizeof(sensor);
80 for (dev = 0; ; dev++) {
82 if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
90 * 'path' is actually the node within the full path (eg, cpu0).
91 * XXX: Extend the API to allow a string instead of just an int for path, this would
92 * allow us to have a path of 'acpitz0' for example.
94 if (strncmp(sensordev.xname, path, strlen(path)) == 0) {
96 for (numt = 0; numt < sensordev.maxnumt[SENSOR_TEMP]; numt++) {
98 if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
103 outwalk += sprintf(outwalk, "%.2f", (sensor.value - 273150000) / 1000000.0 );
108 walk += strlen("degrees");
111 OUTPUT_FULL_TEXT(buffer);
115 (void)fputs("Cannot read temperature\n", stderr);