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 static char *thermal_zone;
22 * Reads the CPU temperature from /sys/class/thermal/thermal_zone0/temp and
23 * returns the temperature in degree celcius.
26 void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format) {
29 char *outwalk = buffer;
33 asprintf(&thermal_zone, THERMAL_ZONE, zone);
39 for (walk = format; *walk != '\0'; walk++) {
45 if (BEGINS_WITH(walk+1, "degrees")) {
48 if (!slurp(path, buf, sizeof(buf)))
50 temp = strtol(buf, NULL, 10);
51 if (temp == LONG_MIN || temp == LONG_MAX || temp <= 0)
54 outwalk += sprintf(outwalk, "%ld", (temp/1000));
55 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
57 size_t sysctl_size = sizeof(sysctl_rslt);
58 if (sysctlbyname(path, &sysctl_rslt, &sysctl_size, NULL, 0))
61 outwalk += sprintf(outwalk, "%d.%d", TZ_KELVTOC(sysctl_rslt));
63 walk += strlen("degrees");
67 OUTPUT_FULL_TEXT(buffer);
71 (void)fputs("Cannot read temperature\n", stderr);