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 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_subfeature *sf;
53 const sensors_chip_name *chip = sensor->iname;
55 const sensors_feature *feature = sensor->feature;
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 *s = *s_ptr;
90 if (!(s->type & SENSOR_TYPE_REMOTE)
91 && s->type & SENSOR_TYPE_LMSENSOR) {
92 if (s->type & SENSOR_TYPE_TEMP)
93 psensor_set_current_value(s,
95 else if (s->type & SENSOR_TYPE_RPM)
96 psensor_set_current_value(s, get_fan_input(s));
103 static struct psensor *
104 lmsensor_psensor_create(const sensors_chip_name *chip,
105 const sensors_feature *feature,
106 int values_max_length)
109 const sensors_subfeature *sf;
111 char *id, *label, *cname;
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;
120 } else if (feature->type == SENSORS_FEATURE_FAN) {
121 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
124 "lmsensor: lmsensor_psensor_create failure: 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);
136 type = SENSOR_TYPE_LMSENSOR;
137 if (feature->type == SENSORS_FEATURE_TEMP)
138 type |= SENSOR_TYPE_TEMP;
139 else if (feature->type == SENSORS_FEATURE_FAN)
140 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
144 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
146 sprintf(id, "lmsensor %s %s", name, label);
148 if (!strcmp(chip->prefix, "coretemp"))
149 cname = strdup(_("Intel CPU"));
150 else if (!strcmp(chip->prefix, "k10temp")
151 || !strcmp(chip->prefix, "k8temp")
152 || !strcmp(chip->prefix, "fam15h_power"))
153 cname = strdup(_("AMD CPU"));
154 else if (!strcmp(chip->prefix, "nouveau"))
155 cname = strdup(_("NVIDIA GPU"));
156 else if (!strcmp(chip->prefix, "via-cputemp"))
157 cname = strdup(_("VIA CPU"));
158 else if (!strcmp(chip->prefix, "acpitz"))
159 cname = strdup(_("ACPI"));
161 cname = strdup(chip->prefix);
163 psensor = psensor_create(id, label, cname, type, values_max_length);
165 psensor->iname = chip;
166 psensor->feature = feature;
168 if (feature->type == SENSORS_FEATURE_TEMP
169 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
177 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
180 const sensors_chip_name *chip;
182 struct psensor **tmp, **result;
183 const sensors_feature *feature;
191 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
194 while ((feature = sensors_get_features(chip, &i))) {
195 if (feature->type == SENSORS_FEATURE_TEMP
196 || feature->type == SENSORS_FEATURE_FAN) {
198 s = lmsensor_psensor_create(chip, feature, vn);
201 tmp = psensor_list_add(result, s);
217 int err = sensors_init(NULL);
220 log_err(_("lmsensor: initialization failure: %s."),
221 sensors_strerror(err));
228 void lmsensor_cleanup()