2 * Copyright (C) 2010-2012 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 double get_value(const sensors_chip_name *name,
35 const sensors_subfeature *sub)
40 err = sensors_get_value(name, sub->number, &val);
42 log_err(_("lmsensor: cannot get value of subfeature %s: %s."),
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;
87 struct psensor *sensor = *s_ptr;
89 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
90 psensor_set_current_value
91 (sensor, get_temp_input(sensor));
92 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
93 psensor_set_current_value(sensor,
94 get_fan_input(sensor));
100 static struct psensor *
101 lmsensor_psensor_create(const sensors_chip_name *chip,
102 const sensors_feature *feature,
103 int values_max_length)
106 const sensors_subfeature *sf;
110 struct psensor *psensor;
111 sensors_subfeature_type fault_subfeature;
113 if (sensors_snprintf_chip_name(name, 200, chip) < 0)
116 if (feature->type == SENSORS_FEATURE_TEMP) {
117 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
119 } else if (feature->type == SENSORS_FEATURE_FAN) {
120 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
123 log_err(_("lmsensor: lmsensor_psensor_create failure:"
124 " wrong feature type."));
128 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
129 if (sf && get_value(chip, sf))
132 label = sensors_get_label(chip, feature);
137 if (feature->type == SENSORS_FEATURE_TEMP)
138 type = SENSOR_TYPE_LMSENSOR_TEMP;
139 else if (feature->type == SENSORS_FEATURE_FAN)
140 type = SENSOR_TYPE_LMSENSOR_FAN;
144 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
146 sprintf(id, "lmsensor %s %s", name, label);
148 psensor = psensor_create(id, label, type, values_max_length);
150 psensor->iname = chip;
151 psensor->feature = feature;
153 if (feature->type == SENSORS_FEATURE_TEMP
154 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
162 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
165 const sensors_chip_name *chip;
167 struct psensor **tmp, **result;
168 const sensors_feature *feature;
176 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
179 while ((feature = sensors_get_features(chip, &i))) {
181 if (feature->type == SENSORS_FEATURE_TEMP
182 || feature->type == SENSORS_FEATURE_FAN) {
184 s = lmsensor_psensor_create(chip, feature, vn);
187 tmp = psensor_list_add(result, s);
203 int err = sensors_init(NULL);
206 log_err(_("lmsensor: initialization failure: %s."),
207 sensors_strerror(err));
214 void lmsensor_cleanup()