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>
35 static double get_value(const sensors_chip_name *name,
36 const sensors_subfeature *sub)
41 err = sensors_get_value(name, sub->number, &val);
44 _("ERROR: Can't get value of subfeature %s: %s\n"),
45 sub->name, sensors_strerror(err));
46 val = UNKNOWN_DBL_VALUE;
51 static double get_temp_input(struct psensor *sensor)
53 const sensors_chip_name *chip = sensor->iname;
54 const sensors_feature *feature = sensor->feature;
56 const sensors_subfeature *sf;
58 sf = sensors_get_subfeature(chip,
59 feature, SENSORS_SUBFEATURE_TEMP_INPUT);
61 return get_value(chip, sf);
63 return UNKNOWN_DBL_VALUE;
66 static double get_fan_input(struct psensor *sensor)
68 const sensors_chip_name *chip = sensor->iname;
69 const sensors_feature *feature = sensor->feature;
71 const sensors_subfeature *sf;
73 sf = sensors_get_subfeature(chip,
74 feature, SENSORS_SUBFEATURE_FAN_INPUT);
76 return get_value(chip, sf);
78 return UNKNOWN_DBL_VALUE;
81 void lmsensor_psensor_list_update(struct psensor **sensors)
83 struct psensor **s_ptr = sensors;
89 struct psensor *sensor = *s_ptr;
91 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
92 psensor_set_current_value
93 (sensor, get_temp_input(sensor));
94 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
95 psensor_set_current_value(sensor,
96 get_fan_input(sensor));
102 static struct psensor *
103 lmsensor_psensor_create(const sensors_chip_name *chip,
104 const sensors_feature *feature,
105 int values_max_length)
108 const sensors_subfeature *sf;
112 struct psensor *psensor;
113 sensors_subfeature_type fault_subfeature;
115 if (sensors_snprintf_chip_name(name, 200, chip) < 0)
118 if (feature->type == SENSORS_FEATURE_TEMP) {
119 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
121 } else if (feature->type == SENSORS_FEATURE_FAN) {
122 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
126 _("ERROR: create_sensor, wrong feature type\n"));
130 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
131 if (sf && get_value(chip, sf))
134 label = sensors_get_label(chip, feature);
139 if (feature->type == SENSORS_FEATURE_TEMP)
140 type = SENSOR_TYPE_LMSENSOR_TEMP;
141 else if (feature->type == SENSORS_FEATURE_FAN)
142 type = SENSOR_TYPE_LMSENSOR_FAN;
146 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
148 sprintf(id, "lmsensor %s %s", name, label);
150 psensor = psensor_create(id, label, type, values_max_length);
152 psensor->iname = chip;
153 psensor->feature = feature;
155 if (feature->type == SENSORS_FEATURE_TEMP
156 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
164 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
167 const sensors_chip_name *chip;
169 struct psensor **tmp, **result;
170 const sensors_feature *feature;
178 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
181 while ((feature = sensors_get_features(chip, &i))) {
183 if (feature->type == SENSORS_FEATURE_TEMP
184 || feature->type == SENSORS_FEATURE_FAN) {
186 s = lmsensor_psensor_create(chip, feature, vn);
189 tmp = psensor_list_add(result, s);
205 int err = sensors_init(NULL);
209 _("ERROR: lm-sensors initialization failure: %s\n"),
210 sensors_strerror(err));
217 void lmsensor_cleanup()