cleanup
[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 <psensor.h>
31
32 static int init_done;
33
34 static const char *PROVIDER_NAME = "lmsensor";
35
36 static double get_value(const sensors_chip_name *name,
37                         const sensors_subfeature *sub)
38 {
39         double val;
40         int err;
41
42         err = sensors_get_value(name, sub->number, &val);
43         if (err) {
44                 log_err(_("%s: Cannot get value of subfeature %s: %s."),
45                         PROVIDER_NAME,
46                         sub->name,
47                         sensors_strerror(err));
48                 val = UNKNOWN_DBL_VALUE;
49         }
50         return val;
51 }
52
53 static double get_temp_input(struct psensor *sensor)
54 {
55         const sensors_subfeature *sf;
56
57         const sensors_chip_name *chip;
58
59         const sensors_feature *feature;
60
61         chip = sensor->iname;
62         feature = sensor->feature;
63
64         sf = sensors_get_subfeature(sensor->iname,
65                                     feature,
66                                     SENSORS_SUBFEATURE_TEMP_INPUT);
67         if (sf)
68                 return get_value(chip, sf);
69
70         return UNKNOWN_DBL_VALUE;
71 }
72
73 static double get_fan_input(struct psensor *sensor)
74 {
75         const sensors_chip_name *chip = sensor->iname;
76         const sensors_feature *feature = sensor->feature;
77
78         const sensors_subfeature *sf;
79
80         sf = sensors_get_subfeature(chip,
81                                     feature,
82                                     SENSORS_SUBFEATURE_FAN_INPUT);
83
84         if (sf)
85                 return get_value(chip, sf);
86
87         return UNKNOWN_DBL_VALUE;
88 }
89
90 void lmsensor_psensor_list_update(struct psensor **sensors)
91 {
92         struct psensor *s;
93         double v;
94
95         if (!init_done)
96                 return;
97
98         while (*sensors) {
99                 s = *sensors;
100
101                 if (!(s->type & SENSOR_TYPE_REMOTE)
102                     && s->type & SENSOR_TYPE_LMSENSOR) {
103
104                         if (s->type & SENSOR_TYPE_TEMP)
105                                 v = get_temp_input(s);
106                         else /* s->type & SENSOR_TYPE_RPM */
107                                 v = get_fan_input(s);
108
109                         if (v != UNKNOWN_DBL_VALUE)
110                                 psensor_set_current_value(s, v);
111                 }
112
113                 sensors++;
114         }
115 }
116
117 static struct psensor *
118 lmsensor_psensor_create(const sensors_chip_name *chip,
119                         const sensors_feature *feature,
120                         int values_max_length)
121 {
122         char name[200];
123         const sensors_subfeature *sf;
124         int type;
125         char *id, *label, *cname;
126         struct psensor *psensor;
127         sensors_subfeature_type fault_subfeature;
128
129         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
130                 return NULL;
131
132         if (feature->type == SENSORS_FEATURE_TEMP) {
133                 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
134         } else if (feature->type == SENSORS_FEATURE_FAN) {
135                 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
136         } else {
137                 log_err(_("%s: Wrong feature type."), PROVIDER_NAME);
138                 return NULL;
139         }
140
141         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
142         if (sf && get_value(chip, sf))
143                 return NULL;
144
145         label = sensors_get_label(chip, feature);
146         if (!label)
147                 return NULL;
148
149         type = SENSOR_TYPE_LMSENSOR;
150         if (feature->type == SENSORS_FEATURE_TEMP)
151                 type |= SENSOR_TYPE_TEMP;
152         else if (feature->type == SENSORS_FEATURE_FAN)
153                 type |= (SENSOR_TYPE_RPM|SENSOR_TYPE_FAN);
154         else
155                 return NULL;
156
157         id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
158                     1);
159         sprintf(id, "lmsensor %s %s", name, label);
160
161         if (!strcmp(chip->prefix, "coretemp"))
162                 cname = strdup(_("Intel CPU"));
163         else if (!strcmp(chip->prefix, "k10temp")
164                  || !strcmp(chip->prefix, "k8temp")
165                  || !strcmp(chip->prefix, "fam15h_power"))
166                 cname = strdup(_("AMD CPU"));
167         else if (!strcmp(chip->prefix, "nouveau"))
168                 cname = strdup(_("NVIDIA GPU"));
169         else if (!strcmp(chip->prefix, "via-cputemp"))
170                 cname = strdup(_("VIA CPU"));
171         else if (!strcmp(chip->prefix, "acpitz"))
172                 cname = strdup(_("ACPI"));
173         else
174                 cname = strdup(chip->prefix);
175
176         psensor = psensor_create(id, label, cname, type, values_max_length);
177
178         psensor->iname = chip;
179         psensor->feature = feature;
180
181         if (feature->type == SENSORS_FEATURE_TEMP
182             && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
183                 free(psensor);
184                 return NULL;
185         }
186
187         return psensor;
188 }
189
190 void lmsensor_init()
191 {
192         int err;
193
194         err = sensors_init(NULL);
195
196         if (err) {
197                 log_err(_("%s: initialization failure: %s."),
198                         PROVIDER_NAME,
199                         sensors_strerror(err));
200                 init_done = 0;
201         } else {
202                 init_done = 1;
203         }
204 }
205
206 void lmsensor_psensor_list_append(struct psensor ***sensors, int vn)
207 {
208         const sensors_chip_name *chip;
209         int chip_nr, i;
210         const sensors_feature *feature;
211         struct psensor *s;
212
213         if (!init_done)
214                 lmsensor_init();
215
216         if (!init_done)
217                 return;
218
219         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
220
221                 i = 0;
222                 while ((feature = sensors_get_features(chip, &i))) {
223                         if (feature->type == SENSORS_FEATURE_TEMP
224                             || feature->type == SENSORS_FEATURE_FAN) {
225
226                                 s = lmsensor_psensor_create(chip, feature, vn);
227
228                                 if (s)
229                                         psensor_list_append(sensors, s);
230                         }
231                 }
232         }
233 }
234
235 void lmsensor_cleanup()
236 {
237         if (init_done)
238                 sensors_cleanup();
239 }