2 Copyright (C) 2010-2011 jeanfi@gmail.com
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU 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
22 #define _(str) gettext(str)
28 #include <sensors/sensors.h>
29 #include <sensors/error.h>
33 static double get_value(const sensors_chip_name *name,
34 const sensors_subfeature *sub)
39 err = sensors_get_value(name, sub->number, &val);
42 _("ERROR: Can't get value of subfeature %s: %s\n"),
43 sub->name, sensors_strerror(err));
44 val = UNKNOWN_DBL_VALUE;
49 static double get_temp_input(struct psensor *sensor)
51 const sensors_chip_name *chip = sensor->iname;
52 const sensors_feature *feature = sensor->feature;
54 const sensors_subfeature *sf;
56 sf = sensors_get_subfeature(chip,
57 feature, SENSORS_SUBFEATURE_TEMP_INPUT);
59 return get_value(chip, sf);
61 return UNKNOWN_DBL_VALUE;
64 static double get_fan_input(struct psensor *sensor)
66 const sensors_chip_name *chip = sensor->iname;
67 const sensors_feature *feature = sensor->feature;
69 const sensors_subfeature *sf;
71 sf = sensors_get_subfeature(chip,
72 feature, SENSORS_SUBFEATURE_FAN_INPUT);
74 return get_value(chip, sf);
76 return UNKNOWN_DBL_VALUE;
79 void lmsensor_psensor_list_update(struct psensor **sensors)
81 struct psensor **s_ptr = sensors;
84 struct psensor *sensor = *s_ptr;
86 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
87 psensor_set_current_value
88 (sensor, get_temp_input(sensor));
89 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
90 psensor_set_current_value(sensor,
91 get_fan_input(sensor));
98 lmsensor_psensor_create(const sensors_chip_name *chip,
99 const sensors_feature *feature,
100 int values_max_length)
103 const sensors_subfeature *sf;
107 struct psensor *psensor;
108 sensors_subfeature_type fault_subfeature;
110 if (sensors_snprintf_chip_name(name, 200, chip) < 0)
113 if (feature->type == SENSORS_FEATURE_TEMP) {
114 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
116 } else if (feature->type == SENSORS_FEATURE_FAN) {
117 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
121 _("ERROR: create_sensor, wrong feature type\n"));
125 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
126 if (sf && get_value(chip, sf))
129 label = sensors_get_label(chip, feature);
134 if (feature->type == SENSORS_FEATURE_TEMP)
135 type = SENSOR_TYPE_LMSENSOR_TEMP;
136 else if (feature->type == SENSORS_FEATURE_FAN)
137 type = SENSOR_TYPE_LMSENSOR_FAN;
141 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
143 sprintf(id, "lmsensor %s %s", name, label);
145 psensor = psensor_create(id, label, type, values_max_length);
147 psensor->iname = chip;
148 psensor->feature = feature;
150 if (feature->type == SENSORS_FEATURE_TEMP
151 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
161 int err = sensors_init(NULL);
165 _("ERROR: lm-sensors initialization failure: %s\n"),
166 sensors_strerror(err));
173 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
176 const sensors_chip_name *chip;
178 struct psensor **tmp, **result;
179 const sensors_feature *feature;
184 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
187 while ((feature = sensors_get_features(chip, &i))) {
189 if (feature->type == SENSORS_FEATURE_TEMP
190 || feature->type == SENSORS_FEATURE_FAN) {
192 s = lmsensor_psensor_create(chip, feature, vn);
195 tmp = psensor_list_add(result, s);