X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=blobdiff_plain;f=src%2Flib%2Flmsensor.c;h=6f3845d95bcd798e74bb5f3f1ff3ebfd8585676a;hp=5149e808e139c8eef74b98cd63c34d0340140ba5;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hpb=609664bb77874990e10f8073e54bb7f1645c8d72 diff --git a/src/lib/lmsensor.c b/src/lib/lmsensor.c index 5149e80..6f3845d 100644 --- a/src/lib/lmsensor.c +++ b/src/lib/lmsensor.c @@ -1,22 +1,21 @@ /* - Copyright (C) 2010-2011 wpitchoune@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 -*/ - + * Copyright (C) 2010-2016 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) @@ -28,128 +27,207 @@ #include #include -#include "psensor.h" +#include + +static int init_done; + +static const char *PROVIDER_NAME = "lmsensor"; + +struct lmsensor_data { + const sensors_chip_name *chip; + + const sensors_feature *feature; +}; + +static const sensors_chip_name *get_chip_name(struct psensor *s) +{ + return ((struct lmsensor_data *)s->provider_data)->chip; +} + +static const sensors_feature *get_feature(struct psensor *s) +{ + return ((struct lmsensor_data *)s->provider_data)->feature; +} + +static void lmsensor_data_set(struct psensor *s, + const struct sensors_chip_name *chip, + const struct sensors_feature *feature) +{ + struct lmsensor_data *data; + + data = malloc(sizeof(struct lmsensor_data)); + data->chip = chip; + data->feature = feature; -double -lmsensor_get_value(const sensors_chip_name *name, - const sensors_subfeature *sub) + s->provider_data = data; +} + +static double get_value(const sensors_chip_name *name, + const sensors_subfeature *sub) { double val; int err; err = sensors_get_value(name, sub->number, &val); if (err) { - fprintf(stderr, - _("ERROR: Can't get value of subfeature %s: %s\n"), - sub->name, sensors_strerror(err)); - val = UNKNOWN_VALUE; + log_err(_("%s: Cannot get value of subfeature %s: %s."), + PROVIDER_NAME, + sub->name, + sensors_strerror(err)); + val = UNKNOWN_DBL_VALUE; } return val; } -double lmsensor_get_temp_input(struct psensor *sensor) +static double get_temp_input(struct psensor *sensor) { - const sensors_chip_name *chip = sensor->iname; - const sensors_feature *feature = sensor->feature; - const sensors_subfeature *sf; + const sensors_chip_name *chip; + + const sensors_feature *feature; + + chip = get_chip_name(sensor); + feature = get_feature(sensor); + sf = sensors_get_subfeature(chip, - feature, SENSORS_SUBFEATURE_TEMP_INPUT); + feature, + SENSORS_SUBFEATURE_TEMP_INPUT); if (sf) - return lmsensor_get_value(chip, sf); - else - return UNKNOWN_VALUE; + return get_value(chip, sf); + + return UNKNOWN_DBL_VALUE; } -double lmsensor_get_fan_input(struct psensor *sensor) +static double get_fan_input(struct psensor *sensor) { - const sensors_chip_name *chip = sensor->iname; - const sensors_feature *feature = sensor->feature; + const sensors_chip_name *chip; + const sensors_feature *feature; const sensors_subfeature *sf; + chip = get_chip_name(sensor); + feature = get_feature(sensor); + sf = sensors_get_subfeature(chip, - feature, SENSORS_SUBFEATURE_FAN_INPUT); + feature, + SENSORS_SUBFEATURE_FAN_INPUT); + if (sf) - return lmsensor_get_value(chip, sf); - else - return UNKNOWN_VALUE; + return get_value(chip, sf); + + return UNKNOWN_DBL_VALUE; } void lmsensor_psensor_list_update(struct psensor **sensors) { - struct psensor **s_ptr = sensors; + struct psensor *s; + double v; + + if (!init_done || !sensors) + return; + + while (*sensors) { + s = *sensors; - while (*s_ptr) { - struct psensor *sensor = *s_ptr; + if (!(s->type & SENSOR_TYPE_REMOTE) + && s->type & SENSOR_TYPE_LMSENSOR) { - if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP) - psensor_set_current_value - (sensor, lmsensor_get_temp_input(sensor)); - else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN) - psensor_set_current_value - (sensor, lmsensor_get_fan_input(sensor)); + if (s->type & SENSOR_TYPE_TEMP) + v = get_temp_input(s); + else /* s->type & SENSOR_TYPE_RPM */ + v = get_fan_input(s); - s_ptr++; + if (v != UNKNOWN_DBL_VALUE) + psensor_set_current_value(s, v); + } + + sensors++; } } -struct psensor * +static struct psensor * lmsensor_psensor_create(const sensors_chip_name *chip, const sensors_feature *feature, int values_max_length) { char name[200]; const sensors_subfeature *sf; - char *label; int type; - char *id; + char *id, *label, *cname; struct psensor *psensor; - sensors_subfeature_type fault_subfeature; + sensors_subfeature_type fault_subfeature, min_subfeature, + max_subfeature; if (sensors_snprintf_chip_name(name, 200, chip) < 0) return NULL; if (feature->type == SENSORS_FEATURE_TEMP) { fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT; - + max_subfeature = SENSORS_SUBFEATURE_TEMP_MAX; + min_subfeature = SENSORS_SUBFEATURE_TEMP_MIN; } else if (feature->type == SENSORS_FEATURE_FAN) { fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT; - + max_subfeature = SENSORS_SUBFEATURE_FAN_MAX; + min_subfeature = SENSORS_SUBFEATURE_FAN_MIN; } else { - fprintf(stderr, - _("ERROR: create_sensor, wrong feature type\n")); + log_err(_("%s: Wrong feature type."), PROVIDER_NAME); return NULL; } sf = sensors_get_subfeature(chip, feature, fault_subfeature); - if (sf && lmsensor_get_value(chip, sf)) + if (sf && get_value(chip, sf)) return NULL; label = sensors_get_label(chip, feature); if (!label) return NULL; - type = 0; + type = SENSOR_TYPE_LMSENSOR; if (feature->type == SENSORS_FEATURE_TEMP) - type = SENSOR_TYPE_LMSENSOR_TEMP; + type |= SENSOR_TYPE_TEMP; else if (feature->type == SENSORS_FEATURE_FAN) - type = SENSOR_TYPE_LMSENSOR_FAN; + type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN); else return NULL; - id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) + - 1); - sprintf(id, "lmsensor %s %s", name, label); + id = malloc(strlen(PROVIDER_NAME) + + 1 + + strlen(name) + + 1 + + strlen(label) + + 1); + sprintf(id, "%s %s %s", PROVIDER_NAME, name, label); + + if (!strcmp(chip->prefix, "coretemp")) + cname = strdup(_("Intel CPU")); + else if (!strcmp(chip->prefix, "k10temp") + || !strcmp(chip->prefix, "k8temp") + || !strcmp(chip->prefix, "fam15h_power")) + cname = strdup(_("AMD CPU")); + else if (!strcmp(chip->prefix, "nouveau")) + cname = strdup(_("NVIDIA GPU")); + else if (!strcmp(chip->prefix, "via-cputemp")) + cname = strdup(_("VIA CPU")); + else if (!strcmp(chip->prefix, "acpitz")) + cname = strdup(_("ACPI")); + else + cname = strdup(chip->prefix); + + psensor = psensor_create(id, label, cname, type, values_max_length); - psensor = psensor_create(id, label, type, values_max_length); + sf = sensors_get_subfeature(chip, feature, max_subfeature); + if (sf) + psensor->max = get_value(chip, sf); - psensor->iname = chip; - psensor->feature = feature; + sf = sensors_get_subfeature(chip, feature, min_subfeature); + if (sf) + psensor->min = get_value(chip, sf); + + lmsensor_data_set(psensor, chip, feature); if (feature->type == SENSORS_FEATURE_TEMP - && (lmsensor_get_temp_input(psensor) == UNKNOWN_VALUE)) { + && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) { free(psensor); return NULL; } @@ -157,16 +235,54 @@ lmsensor_psensor_create(const sensors_chip_name *chip, return psensor; } -int lmsensor_init() +static void lmsensor_init(void) { - int err = sensors_init(NULL); + int err; + + err = sensors_init(NULL); if (err) { - fprintf(stderr, - _("ERROR: lm-sensors initialization failure: %s\n"), + log_err(_("%s: initialization failure: %s."), + PROVIDER_NAME, sensors_strerror(err)); - return 0; + init_done = 0; } else { - return 1; + init_done = 1; } } + +void lmsensor_psensor_list_append(struct psensor ***sensors, int vn) +{ + const sensors_chip_name *chip; + int chip_nr, i; + const sensors_feature *feature; + struct psensor *s; + + if (!init_done) + lmsensor_init(); + + if (!init_done) + return; + + chip_nr = 0; + while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) { + + i = 0; + while ((feature = sensors_get_features(chip, &i))) { + if (feature->type == SENSORS_FEATURE_TEMP + || feature->type == SENSORS_FEATURE_FAN) { + + s = lmsensor_psensor_create(chip, feature, vn); + + if (s) + psensor_list_append(sensors, s); + } + } + } +} + +void lmsensor_cleanup(void) +{ + if (init_done) + sensors_cleanup(); +}