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)
32 * Support of the BCM2835 chip which is mostly used by the
33 * Raspberry PI3 and provides the temperature of the chip.
35 static const char *PROVIDER_NAME = "BCM2835";
38 * If this file exists and contains bcm2835_thermal it can be assumed
39 * that a BCM2835 chip is present.
41 static const char *SYS_THERMAL_TYPE = "/sys/class/thermal/thermal_zone0/type";
42 static const char *BCM2835_TYPE = "bcm2835_thermal";
43 /* this file contains the temperature of the chip in celcius * 1000 */
44 static char *SYS_THERMAL_TEMP = "/sys/class/thermal/thermal_zone0/temp";
46 static void log_provider_info(const char *str)
48 log_info("%s: %s", PROVIDER_NAME, str);
51 static bool is_bcm2835_present(void)
56 ret = is_file(SYS_THERMAL_TYPE);
59 log_debug("%s: %s does not exist.",
65 str = file_get_content(SYS_THERMAL_TYPE);
67 if (!str || strncmp(str, BCM2835_TYPE, strlen(BCM2835_TYPE) - 1)) {
68 log_debug("%s: type: %s.", PROVIDER_NAME, str);
80 void bcm2835_psensor_list_append(struct psensor ***sensors, int vl)
86 if (is_bcm2835_present()) {
87 log_provider_info(_("The BCM2835 (probably a Raspberry PI3) "
88 "has been detected"));
90 p = psensor_create(strdup(PROVIDER_NAME),
91 strdup(PROVIDER_NAME),
92 strdup(PROVIDER_NAME),
93 SENSOR_TYPE_BCM2835 | SENSOR_TYPE_TEMP,
96 psensor_list_append(sensors, p);
98 log_provider_info("The BCM2835 has not been detected.");
104 static double bcm2835_update_temp(struct psensor *s)
112 str = file_get_content(SYS_THERMAL_TEMP);
119 l = strtol(str, &end, 10);
121 log_debug("%s: found invalid value: %s.",
125 psensor_set_current_value(s, l / 1000);
128 log_err(_("Failed to get content of file %s."),
137 void bcm2835_psensor_list_update(struct psensor **sensors)
143 for (; *sensors; sensors++) {
146 if (s->type & SENSOR_TYPE_REMOTE)
149 if (s->type & SENSOR_TYPE_BCM2835) {
150 bcm2835_update_temp(s);
151 break; /* only one possible sensor */