enable/disable of lmsensor provider
[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 double get_value(const sensors_chip_name *name,
35                         const sensors_subfeature *sub)
36 {
37         double val;
38         int err;
39
40         err = sensors_get_value(name, sub->number, &val);
41         if (err) {
42                 log_err(_("lmsensor: cannot get value of subfeature %s: %s."),
43                         sub->name, sensors_strerror(err));
44                 val = UNKNOWN_DBL_VALUE;
45         }
46         return val;
47 }
48
49 static double get_temp_input(struct psensor *sensor)
50 {
51         const sensors_subfeature *sf;
52
53         const sensors_chip_name *chip = sensor->iname;
54
55         const sensors_feature *feature = sensor->feature;
56
57         sf = sensors_get_subfeature(chip,
58                                     feature, SENSORS_SUBFEATURE_TEMP_INPUT);
59         if (sf)
60                 return get_value(chip, sf);
61
62         return UNKNOWN_DBL_VALUE;
63 }
64
65 static double get_fan_input(struct psensor *sensor)
66 {
67         const sensors_chip_name *chip = sensor->iname;
68         const sensors_feature *feature = sensor->feature;
69
70         const sensors_subfeature *sf;
71
72         sf = sensors_get_subfeature(chip,
73                                     feature, SENSORS_SUBFEATURE_FAN_INPUT);
74         if (sf)
75                 return get_value(chip, sf);
76
77         return UNKNOWN_DBL_VALUE;
78 }
79
80 void lmsensor_psensor_list_update(struct psensor **sensors)
81 {
82         struct psensor **s_ptr = sensors;
83
84         if (!init_done)
85                 return;
86
87         while (*s_ptr) {
88                 struct psensor *s = *s_ptr;
89
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,
94                                                           get_temp_input(s));
95                         else if (s->type & SENSOR_TYPE_RPM)
96                                 psensor_set_current_value(s, get_fan_input(s));
97                 }
98
99                 s_ptr++;
100         }
101 }
102
103 static struct psensor *
104 lmsensor_psensor_create(const sensors_chip_name *chip,
105                         const sensors_feature *feature,
106                         int values_max_length)
107 {
108         char name[200];
109         const sensors_subfeature *sf;
110         int type;
111         char *id, *label, *cname;
112         struct psensor *psensor;
113         sensors_subfeature_type fault_subfeature;
114
115         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
116                 return NULL;
117
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;
122         } else {
123                 log_err(_(
124 "lmsensor: lmsensor_psensor_create failure: wrong feature type."));
125                 return NULL;
126         }
127
128         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
129         if (sf && get_value(chip, sf))
130                 return NULL;
131
132         label = sensors_get_label(chip, feature);
133         if (!label)
134                 return NULL;
135
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);
141         else
142                 return NULL;
143
144         id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
145                     1);
146         sprintf(id, "lmsensor %s %s", name, label);
147
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"));
160         else
161                 cname = strdup(chip->prefix);
162
163         psensor = psensor_create(id, label, cname, type, values_max_length);
164
165         psensor->iname = chip;
166         psensor->feature = feature;
167
168         if (feature->type == SENSORS_FEATURE_TEMP
169             && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
170                 free(psensor);
171                 return NULL;
172         }
173
174         return psensor;
175 }
176
177 void lmsensor_init()
178 {
179         int err = sensors_init(NULL);
180
181         if (err) {
182                 log_err(_("lmsensor: initialization failure: %s."),
183                         sensors_strerror(err));
184                 init_done = 0;
185         } else {
186                 init_done = 1;
187         }
188 }
189
190 void lmsensor_psensor_list_append(struct psensor ***sensors, int vn)
191 {
192         const sensors_chip_name *chip;
193         int chip_nr = 0;
194         const sensors_feature *feature;
195         struct psensor *s;
196         int i;
197
198         if (!init_done)
199                 lmsensor_init();
200
201         if (!init_done)
202                 return;
203
204         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
205
206                 i = 0;
207                 while ((feature = sensors_get_features(chip, &i))) {
208                         if (feature->type == SENSORS_FEATURE_TEMP
209                             || feature->type == SENSORS_FEATURE_FAN) {
210
211                                 s = lmsensor_psensor_create(chip, feature, vn);
212
213                                 if (s)
214                                         psensor_list_append(sensors, s);
215                         }
216                 }
217         }
218 }
219
220 void lmsensor_cleanup()
221 {
222         if (init_done)
223                 sensors_cleanup();
224 }