use max/min of the sensor for the threshold if not overriden by the configuration
[psensor.git] / src / lib / lmsensor.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <sensors/sensors.h>
28 #include <sensors/error.h>
29
30 #include <lmsensor.h>
31
32 static int init_done;
33
34 static const char *PROVIDER_NAME = "lmsensor";
35
36 struct lmsensor_data {
37         const sensors_chip_name *chip;
38
39         const sensors_feature *feature;
40 };
41
42 static const sensors_chip_name *get_chip_name(struct psensor *s)
43 {
44         return ((struct lmsensor_data *)s->provider_data)->chip;
45 }
46
47 static const sensors_feature *get_feature(struct psensor *s)
48 {
49         return ((struct lmsensor_data *)s->provider_data)->feature;
50 }
51
52 static void lmsensor_data_set(struct psensor *s,
53                               const struct sensors_chip_name *chip,
54                               const struct sensors_feature *feature)
55 {
56         struct lmsensor_data *data;
57
58         data = malloc(sizeof(struct lmsensor_data));
59         data->chip = chip;
60         data->feature = feature;
61
62         s->provider_data = data;
63 }
64
65 static double get_value(const sensors_chip_name *name,
66                         const sensors_subfeature *sub)
67 {
68         double val;
69         int err;
70
71         err = sensors_get_value(name, sub->number, &val);
72         if (err) {
73                 log_err(_("%s: Cannot get value of subfeature %s: %s."),
74                         PROVIDER_NAME,
75                         sub->name,
76                         sensors_strerror(err));
77                 val = UNKNOWN_DBL_VALUE;
78         }
79         return val;
80 }
81
82 static double get_temp_input(struct psensor *sensor)
83 {
84         const sensors_subfeature *sf;
85
86         const sensors_chip_name *chip;
87
88         const sensors_feature *feature;
89
90         chip = get_chip_name(sensor);
91         feature = get_feature(sensor);
92
93         sf = sensors_get_subfeature(chip,
94                                     feature,
95                                     SENSORS_SUBFEATURE_TEMP_INPUT);
96         if (sf)
97                 return get_value(chip, sf);
98
99         return UNKNOWN_DBL_VALUE;
100 }
101
102 static double get_fan_input(struct psensor *sensor)
103 {
104         const sensors_chip_name *chip;
105         const sensors_feature *feature;
106
107         const sensors_subfeature *sf;
108
109         chip = get_chip_name(sensor);
110         feature = get_feature(sensor);
111
112         sf = sensors_get_subfeature(chip,
113                                     feature,
114                                     SENSORS_SUBFEATURE_FAN_INPUT);
115
116         if (sf)
117                 return get_value(chip, sf);
118
119         return UNKNOWN_DBL_VALUE;
120 }
121
122 void lmsensor_psensor_list_update(struct psensor **sensors)
123 {
124         struct psensor *s;
125         double v;
126
127         if (!init_done)
128                 return;
129
130         while (*sensors) {
131                 s = *sensors;
132
133                 if (!(s->type & SENSOR_TYPE_REMOTE)
134                     && s->type & SENSOR_TYPE_LMSENSOR) {
135
136                         if (s->type & SENSOR_TYPE_TEMP)
137                                 v = get_temp_input(s);
138                         else /* s->type & SENSOR_TYPE_RPM */
139                                 v = get_fan_input(s);
140
141                         if (v != UNKNOWN_DBL_VALUE)
142                                 psensor_set_current_value(s, v);
143                 }
144
145                 sensors++;
146         }
147 }
148
149 static struct psensor *
150 lmsensor_psensor_create(const sensors_chip_name *chip,
151                         const sensors_feature *feature,
152                         int values_max_length)
153 {
154         char name[200];
155         const sensors_subfeature *sf;
156         int type;
157         char *id, *label, *cname;
158         struct psensor *psensor;
159         sensors_subfeature_type fault_subfeature;
160
161         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
162                 return NULL;
163
164         if (feature->type == SENSORS_FEATURE_TEMP) {
165                 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
166         } else if (feature->type == SENSORS_FEATURE_FAN) {
167                 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
168         } else {
169                 log_err(_("%s: Wrong feature type."), PROVIDER_NAME);
170                 return NULL;
171         }
172
173         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
174         if (sf && get_value(chip, sf))
175                 return NULL;
176
177         label = sensors_get_label(chip, feature);
178         if (!label)
179                 return NULL;
180
181         type = SENSOR_TYPE_LMSENSOR;
182         if (feature->type == SENSORS_FEATURE_TEMP)
183                 type |= SENSOR_TYPE_TEMP;
184         else if (feature->type == SENSORS_FEATURE_FAN)
185                 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
186         else
187                 return NULL;
188
189         id = malloc(strlen(PROVIDER_NAME)
190                     + 1
191                     + strlen(name)
192                     + 1
193                     + strlen(label)
194                     + 1);
195         sprintf(id, "%s %s %s", PROVIDER_NAME, name, label);
196
197         if (!strcmp(chip->prefix, "coretemp"))
198                 cname = strdup(_("Intel CPU"));
199         else if (!strcmp(chip->prefix, "k10temp")
200                  || !strcmp(chip->prefix, "k8temp")
201                  || !strcmp(chip->prefix, "fam15h_power"))
202                 cname = strdup(_("AMD CPU"));
203         else if (!strcmp(chip->prefix, "nouveau"))
204                 cname = strdup(_("NVIDIA GPU"));
205         else if (!strcmp(chip->prefix, "via-cputemp"))
206                 cname = strdup(_("VIA CPU"));
207         else if (!strcmp(chip->prefix, "acpitz"))
208                 cname = strdup(_("ACPI"));
209         else
210                 cname = strdup(chip->prefix);
211
212         psensor = psensor_create(id, label, cname, type, values_max_length);
213
214         sf = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_MAX);
215         if (sf)
216                 psensor->max = get_value(chip, sf);
217
218         sf = sensors_get_subfeature(chip, feature, SENSORS_SUBFEATURE_TEMP_MIN);
219         if (sf)
220                 psensor->min = get_value(chip, sf);
221
222         lmsensor_data_set(psensor, chip, feature);
223
224         if (feature->type == SENSORS_FEATURE_TEMP
225             && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
226                 free(psensor);
227                 return NULL;
228         }
229
230         return psensor;
231 }
232
233 static void lmsensor_init(void)
234 {
235         int err;
236
237         err = sensors_init(NULL);
238
239         if (err) {
240                 log_err(_("%s: initialization failure: %s."),
241                         PROVIDER_NAME,
242                         sensors_strerror(err));
243                 init_done = 0;
244         } else {
245                 init_done = 1;
246         }
247 }
248
249 void lmsensor_psensor_list_append(struct psensor ***sensors, int vn)
250 {
251         const sensors_chip_name *chip;
252         int chip_nr, i;
253         const sensors_feature *feature;
254         struct psensor *s;
255
256         if (!init_done)
257                 lmsensor_init();
258
259         if (!init_done)
260                 return;
261
262         chip_nr = 0;
263         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
264
265                 i = 0;
266                 while ((feature = sensors_get_features(chip, &i))) {
267                         if (feature->type == SENSORS_FEATURE_TEMP
268                             || feature->type == SENSORS_FEATURE_FAN) {
269
270                                 s = lmsensor_psensor_create(chip, feature, vn);
271
272                                 if (s)
273                                         psensor_list_append(sensors, s);
274                         }
275                 }
276         }
277 }
278
279 void lmsensor_cleanup(void)
280 {
281         if (init_done)
282                 sensors_cleanup();
283 }