2 * Copyright (C) 2017 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
31 /* Support of the BCM2835 chip which is mostly used by by the
32 * Raspberry PI3 and provides the temperature of the chip. */
33 static const char *PROVIDER_NAME = "BCM2835";
36 * If this file exists and contains bcm2835_thermal it can be assumed
37 * that a BCM2835 chip is present.
39 static const char *SYS_THERMAL_TYPE = "/sys/class/thermal/thermal_zone0/type";
40 static const char *BCM2835_TYPE = "bcm2835_thermal";
41 /* this file contains the temperature of the chip in celcius * 1000 */
42 static char *SYS_THERMAL_TEMP = "/sys/class/thermal/thermal_zone0/temp";
44 static void log_provider_info(const char *str)
46 log_info("%s: %s", PROVIDER_NAME, str);
49 static bool is_bcm2835_present(void)
54 ret = is_file(SYS_THERMAL_TYPE);
57 log_debug("%s: %s does not exist.",
63 str = file_get_content(SYS_THERMAL_TYPE);
65 if (!str || strncmp(str, BCM2835_TYPE, strlen(BCM2835_TYPE) - 1)) {
66 log_debug("%s: type: %s.", PROVIDER_NAME, str);
78 void bcm2835_psensor_list_append(struct psensor ***sensors, int vl)
84 if (is_bcm2835_present()) {
85 log_provider_info(_("The BCM2835 (probably a Raspberry PI3) "
86 "has been detected"));
88 p = psensor_create(strdup(PROVIDER_NAME),
89 strdup(PROVIDER_NAME),
90 strdup(PROVIDER_NAME),
91 SENSOR_TYPE_BCM2835 | SENSOR_TYPE_TEMP,
94 psensor_list_append(sensors, p);
96 log_provider_info("The BCM2835 has not been detected.");
102 static double bcm2835_update_temp(struct psensor *s)
109 str = file_get_content(SYS_THERMAL_TEMP);
111 if (str[strlen(str) - 1] == '\n')
112 str[strlen(str) - 1] = '\0';
115 l = strtol(str, &end, 10);
117 log_debug("%s: found invalid value: A%sB %d.", PROVIDER_NAME, str, strlen(str));
119 psensor_set_current_value(s, l / 1000);
122 log_err(_("Failed to get content of file %s."), SYS_THERMAL_TEMP);
130 void bcm2835_psensor_list_update(struct psensor **sensors)
136 for (; *sensors; sensors++) {
139 if (s->type & SENSOR_TYPE_REMOTE)
142 if (s->type & SENSOR_TYPE_BCM2835) {
143 bcm2835_update_temp(s);
144 break; /* only one possible sensor */