2 * Copyright (C) 2010-2014 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)
27 #include <sensors/sensors.h>
28 #include <sensors/error.h>
34 static const char *PROVIDER_NAME = "lmsensor";
36 static double get_value(const sensors_chip_name *name,
37 const sensors_subfeature *sub)
42 err = sensors_get_value(name, sub->number, &val);
44 log_err(_("%s: Cannot get value of subfeature %s: %s."),
47 sensors_strerror(err));
48 val = UNKNOWN_DBL_VALUE;
53 static double get_temp_input(struct psensor *sensor)
55 const sensors_subfeature *sf;
57 const sensors_chip_name *chip;
59 const sensors_feature *feature;
62 feature = sensor->feature;
64 sf = sensors_get_subfeature(sensor->iname,
66 SENSORS_SUBFEATURE_TEMP_INPUT);
68 return get_value(chip, sf);
70 return UNKNOWN_DBL_VALUE;
73 static double get_fan_input(struct psensor *sensor)
75 const sensors_chip_name *chip = sensor->iname;
76 const sensors_feature *feature = sensor->feature;
78 const sensors_subfeature *sf;
80 sf = sensors_get_subfeature(chip,
82 SENSORS_SUBFEATURE_FAN_INPUT);
85 return get_value(chip, sf);
87 return UNKNOWN_DBL_VALUE;
90 void lmsensor_psensor_list_update(struct psensor **sensors)
101 if (!(s->type & SENSOR_TYPE_REMOTE)
102 && s->type & SENSOR_TYPE_LMSENSOR) {
104 if (s->type & SENSOR_TYPE_TEMP)
105 v = get_temp_input(s);
106 else /* s->type & SENSOR_TYPE_RPM */
107 v = get_fan_input(s);
109 if (v != UNKNOWN_DBL_VALUE)
110 psensor_set_current_value(s, v);
117 static struct psensor *
118 lmsensor_psensor_create(const sensors_chip_name *chip,
119 const sensors_feature *feature,
120 int values_max_length)
123 const sensors_subfeature *sf;
125 char *id, *label, *cname;
126 struct psensor *psensor;
127 sensors_subfeature_type fault_subfeature;
129 if (sensors_snprintf_chip_name(name, 200, chip) < 0)
132 if (feature->type == SENSORS_FEATURE_TEMP) {
133 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
134 } else if (feature->type == SENSORS_FEATURE_FAN) {
135 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
137 log_err(_("%s: Wrong feature type."), PROVIDER_NAME);
141 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
142 if (sf && get_value(chip, sf))
145 label = sensors_get_label(chip, feature);
149 type = SENSOR_TYPE_LMSENSOR;
150 if (feature->type == SENSORS_FEATURE_TEMP)
151 type |= SENSOR_TYPE_TEMP;
152 else if (feature->type == SENSORS_FEATURE_FAN)
153 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
157 id = malloc(strlen(PROVIDER_NAME)
163 sprintf(id, "%s %s %s", PROVIDER_NAME, name, label);
165 if (!strcmp(chip->prefix, "coretemp"))
166 cname = strdup(_("Intel CPU"));
167 else if (!strcmp(chip->prefix, "k10temp")
168 || !strcmp(chip->prefix, "k8temp")
169 || !strcmp(chip->prefix, "fam15h_power"))
170 cname = strdup(_("AMD CPU"));
171 else if (!strcmp(chip->prefix, "nouveau"))
172 cname = strdup(_("NVIDIA GPU"));
173 else if (!strcmp(chip->prefix, "via-cputemp"))
174 cname = strdup(_("VIA CPU"));
175 else if (!strcmp(chip->prefix, "acpitz"))
176 cname = strdup(_("ACPI"));
178 cname = strdup(chip->prefix);
180 psensor = psensor_create(id, label, cname, type, values_max_length);
182 psensor->iname = chip;
183 psensor->feature = feature;
185 if (feature->type == SENSORS_FEATURE_TEMP
186 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
198 err = sensors_init(NULL);
201 log_err(_("%s: initialization failure: %s."),
203 sensors_strerror(err));
210 void lmsensor_psensor_list_append(struct psensor ***sensors, int vn)
212 const sensors_chip_name *chip;
214 const sensors_feature *feature;
224 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
227 while ((feature = sensors_get_features(chip, &i))) {
228 if (feature->type == SENSORS_FEATURE_TEMP
229 || feature->type == SENSORS_FEATURE_FAN) {
231 s = lmsensor_psensor_create(chip, feature, vn);
234 psensor_list_append(sensors, s);
240 void lmsensor_cleanup()