Merge tag 'upstream/1.2.0'
[psensor-pkg-debian.git] / src / lib / lmsensor.c
1 /*
2  * Copyright (C) 2010-2016 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 || !sensors)
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, min_subfeature,
160                 max_subfeature;
161
162         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
163                 return NULL;
164
165         if (feature->type == SENSORS_FEATURE_TEMP) {
166                 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
167                 max_subfeature = SENSORS_SUBFEATURE_TEMP_MAX;
168                 min_subfeature = SENSORS_SUBFEATURE_TEMP_MIN;
169         } else if (feature->type == SENSORS_FEATURE_FAN) {
170                 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
171                 max_subfeature = SENSORS_SUBFEATURE_FAN_MAX;
172                 min_subfeature = SENSORS_SUBFEATURE_FAN_MIN;
173         } else {
174                 log_err(_("%s: Wrong feature type."), PROVIDER_NAME);
175                 return NULL;
176         }
177
178         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
179         if (sf && get_value(chip, sf))
180                 return NULL;
181
182         label = sensors_get_label(chip, feature);
183         if (!label)
184                 return NULL;
185
186         type = SENSOR_TYPE_LMSENSOR;
187         if (feature->type == SENSORS_FEATURE_TEMP)
188                 type |= SENSOR_TYPE_TEMP;
189         else if (feature->type == SENSORS_FEATURE_FAN)
190                 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
191         else
192                 return NULL;
193
194         id = malloc(strlen(PROVIDER_NAME)
195                     + 1
196                     + strlen(name)
197                     + 1
198                     + strlen(label)
199                     + 1);
200         sprintf(id, "%s %s %s", PROVIDER_NAME, name, label);
201
202         if (!strcmp(chip->prefix, "coretemp"))
203                 cname = strdup(_("Intel CPU"));
204         else if (!strcmp(chip->prefix, "k10temp")
205                  || !strcmp(chip->prefix, "k8temp")
206                  || !strcmp(chip->prefix, "fam15h_power"))
207                 cname = strdup(_("AMD CPU"));
208         else if (!strcmp(chip->prefix, "nouveau"))
209                 cname = strdup(_("NVIDIA GPU"));
210         else if (!strcmp(chip->prefix, "via-cputemp"))
211                 cname = strdup(_("VIA CPU"));
212         else if (!strcmp(chip->prefix, "acpitz"))
213                 cname = strdup(_("ACPI"));
214         else
215                 cname = strdup(chip->prefix);
216
217         psensor = psensor_create(id, label, cname, type, values_max_length);
218
219         sf = sensors_get_subfeature(chip, feature, max_subfeature);
220         if (sf)
221                 psensor->max = get_value(chip, sf);
222
223         sf = sensors_get_subfeature(chip, feature, min_subfeature);
224         if (sf)
225                 psensor->min = get_value(chip, sf);
226
227         lmsensor_data_set(psensor, chip, feature);
228
229         if (feature->type == SENSORS_FEATURE_TEMP
230             && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
231                 free(psensor);
232                 return NULL;
233         }
234
235         return psensor;
236 }
237
238 static void lmsensor_init(void)
239 {
240         int err;
241
242         err = sensors_init(NULL);
243
244         if (err) {
245                 log_err(_("%s: initialization failure: %s."),
246                         PROVIDER_NAME,
247                         sensors_strerror(err));
248                 init_done = 0;
249         } else {
250                 init_done = 1;
251         }
252 }
253
254 void lmsensor_psensor_list_append(struct psensor ***sensors, int vn)
255 {
256         const sensors_chip_name *chip;
257         int chip_nr, i;
258         const sensors_feature *feature;
259         struct psensor *s;
260
261         if (!init_done)
262                 lmsensor_init();
263
264         if (!init_done)
265                 return;
266
267         chip_nr = 0;
268         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
269
270                 i = 0;
271                 while ((feature = sensors_get_features(chip, &i))) {
272                         if (feature->type == SENSORS_FEATURE_TEMP
273                             || feature->type == SENSORS_FEATURE_FAN) {
274
275                                 s = lmsensor_psensor_create(chip, feature, vn);
276
277                                 if (s)
278                                         psensor_list_append(sensors, s);
279                         }
280                 }
281         }
282 }
283
284 void lmsensor_cleanup(void)
285 {
286         if (init_done)
287                 sensors_cleanup();
288 }