6 #include <yajl/yajl_gen.h>
10 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
12 #include <sys/types.h>
13 #include <sys/sysctl.h>
15 #define TZ_KELVTOC(x) (((x) - TZ_ZEROC) / 10), abs(((x) - TZ_ZEROC) % 10)
18 static char *thermal_zone;
21 * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
22 * returns the temperature in degree celcius.
25 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format) {
28 char *outwalk = buffer;
32 asprintf(&thermal_zone, THERMAL_ZONE, zone);
38 for (walk = format; *walk != '\0'; walk++) {
44 if (BEGINS_WITH(walk+1, "degrees")) {
47 if (!slurp(path, buf, sizeof(buf)))
49 temp = strtol(buf, NULL, 10);
50 if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
53 outwalk += sprintf(outwalk, "%ld", (temp/1000));
54 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
56 size_t sysctl_size = sizeof(sysctl_rslt);
57 if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
60 outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
62 walk += strlen("degrees");
66 OUTPUT_FULL_TEXT(buffer);
70 (void)fputs("Cannot read temperature\n", stderr);