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);
43 _("ERROR: Can't get value of subfeature %s: %s\n"),
44 sub->name, sensors_strerror(err));
45 val = UNKNOWN_DBL_VALUE;
50 static double get_temp_input(struct psensor *sensor)
52 const sensors_chip_name *chip = sensor->iname;
53 const sensors_feature *feature = sensor->feature;
55 const sensors_subfeature *sf;
57 sf = sensors_get_subfeature(chip,
58 feature, SENSORS_SUBFEATURE_TEMP_INPUT);
60 return get_value(chip, sf);
62 return UNKNOWN_DBL_VALUE;
65 static double get_fan_input(struct psensor *sensor)
67 const sensors_chip_name *chip = sensor->iname;
68 const sensors_feature *feature = sensor->feature;
70 const sensors_subfeature *sf;
72 sf = sensors_get_subfeature(chip,
73 feature, SENSORS_SUBFEATURE_FAN_INPUT);
75 return get_value(chip, sf);
77 return UNKNOWN_DBL_VALUE;
80 void lmsensor_psensor_list_update(struct psensor **sensors)
82 struct psensor **s_ptr = sensors;
88 struct psensor *sensor = *s_ptr;
90 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
91 psensor_set_current_value
92 (sensor, get_temp_input(sensor));
93 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
94 psensor_set_current_value(sensor,
95 get_fan_input(sensor));
101 static struct psensor *
102 lmsensor_psensor_create(const sensors_chip_name *chip,
103 const sensors_feature *feature,
104 int values_max_length)
107 const sensors_subfeature *sf;
111 struct psensor *psensor;
112 sensors_subfeature_type fault_subfeature;
114 if (sensors_snprintf_chip_name(name, 200, chip) < 0)
117 if (feature->type == SENSORS_FEATURE_TEMP) {
118 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
120 } else if (feature->type == SENSORS_FEATURE_FAN) {
121 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
125 _("ERROR: create_sensor, wrong feature type\n"));
129 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
130 if (sf && get_value(chip, sf))
133 label = sensors_get_label(chip, feature);
138 if (feature->type == SENSORS_FEATURE_TEMP)
139 type = SENSOR_TYPE_LMSENSOR_TEMP;
140 else if (feature->type == SENSORS_FEATURE_FAN)
141 type = SENSOR_TYPE_LMSENSOR_FAN;
145 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
147 sprintf(id, "lmsensor %s %s", name, label);
149 psensor = psensor_create(id, label, type, values_max_length);
151 psensor->iname = chip;
152 psensor->feature = feature;
154 if (feature->type == SENSORS_FEATURE_TEMP
155 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
163 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
166 const sensors_chip_name *chip;
168 struct psensor **tmp, **result;
169 const sensors_feature *feature;
177 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
180 while ((feature = sensors_get_features(chip, &i))) {
182 if (feature->type == SENSORS_FEATURE_TEMP
183 || feature->type == SENSORS_FEATURE_FAN) {
185 s = lmsensor_psensor_create(chip, feature, vn);
188 tmp = psensor_list_add(result, s);
204 int err = sensors_init(NULL);
208 _("ERROR: lm-sensors initialization failure: %s\n"),
209 sensors_strerror(err));
216 void lmsensor_cleanup()