2 * Copyright (C) 2010-2013 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 *s = *s_ptr;
89 if (!(s->type & SENSOR_TYPE_REMOTE)
90 && s->type & SENSOR_TYPE_LMSENSOR) {
91 if (s->type & SENSOR_TYPE_TEMP)
92 psensor_set_current_value(s,
94 else if (s->type & SENSOR_TYPE_RPM)
95 psensor_set_current_value(s, get_fan_input(s));
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;
110 char *id, *label, *cname;
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;
119 } else if (feature->type == SENSORS_FEATURE_FAN) {
120 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
123 "lmsensor: lmsensor_psensor_create failure: wrong feature type."));
127 sf = sensors_get_subfeature(chip, feature, fault_subfeature);
128 if (sf && get_value(chip, sf))
131 label = sensors_get_label(chip, feature);
135 type = SENSOR_TYPE_LMSENSOR;
136 if (feature->type == SENSORS_FEATURE_TEMP)
137 type |= SENSOR_TYPE_TEMP;
138 else if (feature->type == SENSORS_FEATURE_FAN)
139 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
143 id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
145 sprintf(id, "lmsensor %s %s", name, label);
147 if (!strcmp(chip->prefix, "coretemp"))
148 cname = strdup("Intel CPU");
149 else if (!strcmp(chip->prefix, "k10temp")
150 || !strcmp(chip->prefix, "k8temp")
151 || !strcmp(chip->prefix, "fam15h_power"))
152 cname = strdup("AMD CPU");
153 else if (!strcmp(chip->prefix, "nouveau"))
154 cname = strdup("Nvidia GPU");
155 else if (!strcmp(chip->prefix, "via-cputemp"))
156 cname = strdup("VIA CPU");
158 cname = strdup(chip->prefix);
160 psensor = psensor_create(id, label, cname, type, values_max_length);
162 psensor->iname = chip;
163 psensor->feature = feature;
165 if (feature->type == SENSORS_FEATURE_TEMP
166 && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
174 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
177 const sensors_chip_name *chip;
179 struct psensor **tmp, **result;
180 const sensors_feature *feature;
188 while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
191 while ((feature = sensors_get_features(chip, &i))) {
193 if (feature->type == SENSORS_FEATURE_TEMP
194 || feature->type == SENSORS_FEATURE_FAN) {
196 s = lmsensor_psensor_create(chip, feature, vn);
199 tmp = psensor_list_add(result, s);
215 int err = sensors_init(NULL);
218 log_err(_("lmsensor: initialization failure: %s."),
219 sensors_strerror(err));
226 void lmsensor_cleanup()