X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Fbcm2835.c;fp=src%2Flib%2Fbcm2835.c;h=efc8854a46b746a403027581b3daa15190c00af2;hp=0000000000000000000000000000000000000000;hb=ca30742ed33315bdd0620d7a6ef417ab7405a0ba;hpb=a2bce14963ee379e9f5b5e745304129b651e7352 diff --git a/src/lib/bcm2835.c b/src/lib/bcm2835.c new file mode 100644 index 0000000..efc8854 --- /dev/null +++ b/src/lib/bcm2835.c @@ -0,0 +1,149 @@ +/* + * Copyright (C) 2017 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ +#include +#include +#define _(str) gettext(str) + +#include +#include +#include + +#include +#include +#include + +/* Support of the BCM2835 chip which is mostly used by by the + * Raspberry PI3 and provides the temperature of the chip. */ +static const char *PROVIDER_NAME = "BCM2835"; + +/* + * If this file exists and contains bcm2835_thermal it can be assumed + * that a BCM2835 chip is present. + */ +static const char *SYS_THERMAL_TYPE = "/sys/class/thermal/thermal_zone0/type"; +static const char *BCM2835_TYPE = "bcm2835_thermal"; +/* this file contains the temperature of the chip in celcius * 1000 */ +static char *SYS_THERMAL_TEMP = "/sys/class/thermal/thermal_zone0/temp"; + +static void log_provider_info(const char *str) +{ + log_info("%s: %s", PROVIDER_NAME, str); +} + +static bool is_bcm2835_present(void) +{ + bool ret; + char *str; + + ret = is_file(SYS_THERMAL_TYPE); + + if (!ret) { + log_debug("%s: %s does not exist.", + PROVIDER_NAME, + SYS_THERMAL_TYPE); + return false; + } + + str = file_get_content(SYS_THERMAL_TYPE); + + if (!str || strncmp(str, BCM2835_TYPE, strlen(BCM2835_TYPE) - 1)) { + log_debug("%s: type: %s.", PROVIDER_NAME, str); + ret = false; + } else { + ret = true; + } + + if (str) + free(str); + + return ret; +} + +void bcm2835_psensor_list_append(struct psensor ***sensors, int vl) +{ + struct psensor *p; + + log_fct_enter(); + + if (is_bcm2835_present()) { + log_provider_info(_("The BCM2835 (probably a Raspberry PI3) " + "has been detected")); + + p = psensor_create(strdup(PROVIDER_NAME), + strdup(PROVIDER_NAME), + strdup(PROVIDER_NAME), + SENSOR_TYPE_BCM2835 | SENSOR_TYPE_TEMP, + vl); + + psensor_list_append(sensors, p); + } else { + log_provider_info("The BCM2835 has not been detected."); + } + + log_fct_exit(); +} + +static double bcm2835_update_temp(struct psensor *s) +{ + char *str, *end; + long l; + + log_fct_enter(); + + str = file_get_content(SYS_THERMAL_TEMP); + + if (str[strlen(str) - 1] == '\n') + str[strlen(str) - 1] = '\0'; + + if (str) { + l = strtol(str, &end, 10); + if (*end != '\0') + log_debug("%s: found invalid value: A%sB %d.", PROVIDER_NAME, str, strlen(str)); + else + psensor_set_current_value(s, l / 1000); + free(str); + } else { + log_err(_("Failed to get content of file %s."), SYS_THERMAL_TEMP); + } + + log_fct_exit(); + + return 0; +} + +void bcm2835_psensor_list_update(struct psensor **sensors) +{ + struct psensor *s; + + log_fct_enter(); + + for (; *sensors; sensors++) { + s = *sensors; + + if (s->type & SENSOR_TYPE_REMOTE) + continue; + + if (s->type & SENSOR_TYPE_BCM2835) { + bcm2835_update_temp(s); + break; /* only one possible sensor */ + } + } + + log_fct_exit(); +}