display the name of chip in sensor preferences.
[psensor.git] / src / lib / lmsensor.c
1 /*
2  * Copyright (C) 2010-2012 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_chip_name *chip = sensor->iname;
52         const sensors_feature *feature = sensor->feature;
53
54         const sensors_subfeature *sf;
55
56         sf = sensors_get_subfeature(chip,
57                                     feature, SENSORS_SUBFEATURE_TEMP_INPUT);
58         if (sf)
59                 return get_value(chip, sf);
60         else
61                 return UNKNOWN_DBL_VALUE;
62 }
63
64 static double get_fan_input(struct psensor *sensor)
65 {
66         const sensors_chip_name *chip = sensor->iname;
67         const sensors_feature *feature = sensor->feature;
68
69         const sensors_subfeature *sf;
70
71         sf = sensors_get_subfeature(chip,
72                                     feature, SENSORS_SUBFEATURE_FAN_INPUT);
73         if (sf)
74                 return get_value(chip, sf);
75         else
76                 return UNKNOWN_DBL_VALUE;
77 }
78
79 void lmsensor_psensor_list_update(struct psensor **sensors)
80 {
81         struct psensor **s_ptr = sensors;
82
83         if (!init_done)
84                 return ;
85
86         while (*s_ptr) {
87                 struct psensor *sensor = *s_ptr;
88
89                 if (sensor->type == SENSOR_TYPE_LMSENSOR_TEMP)
90                         psensor_set_current_value
91                             (sensor, get_temp_input(sensor));
92                 else if (sensor->type == SENSOR_TYPE_LMSENSOR_FAN)
93                         psensor_set_current_value(sensor,
94                                                   get_fan_input(sensor));
95
96                 s_ptr++;
97         }
98 }
99
100 static struct psensor *
101 lmsensor_psensor_create(const sensors_chip_name *chip,
102                         const sensors_feature *feature,
103                         int values_max_length)
104 {
105         char name[200];
106         const sensors_subfeature *sf;
107         int type;
108         char *id, *label, *cname;
109         struct psensor *psensor;
110         sensors_subfeature_type fault_subfeature;
111
112         if (sensors_snprintf_chip_name(name, 200, chip) < 0)
113                 return NULL;
114
115         if (feature->type == SENSORS_FEATURE_TEMP) {
116                 fault_subfeature = SENSORS_SUBFEATURE_TEMP_FAULT;
117
118         } else if (feature->type == SENSORS_FEATURE_FAN) {
119                 fault_subfeature = SENSORS_SUBFEATURE_FAN_FAULT;
120
121         } else {
122                 log_err(_(
123 "lmsensor: lmsensor_psensor_create failure: wrong feature type."));
124                 return NULL;
125         }
126
127         sf = sensors_get_subfeature(chip, feature, fault_subfeature);
128         if (sf && get_value(chip, sf))
129                 return NULL;
130
131         label = sensors_get_label(chip, feature);
132         if (!label)
133                 return NULL;
134
135         type = 0;
136         if (feature->type == SENSORS_FEATURE_TEMP)
137                 type = SENSOR_TYPE_LMSENSOR_TEMP;
138         else if (feature->type == SENSORS_FEATURE_FAN)
139                 type = SENSOR_TYPE_LMSENSOR_FAN;
140         else
141                 return NULL;
142
143         id = malloc(strlen("lmsensor ") + 1 + strlen(name) + 1 + strlen(label) +
144                     1);
145         sprintf(id, "lmsensor %s %s", name, label);
146
147         if (!strcmp(chip->prefix, "coretemp"))
148                 cname = strdup("Intel CPU");
149         else if (!strcmp(chip->prefix, "k10temp"))
150                 cname = strdup("AMD CPU");
151         else if (!strcmp(chip->prefix, "nouveau"))
152                 cname = strdup("Nvidia GPU");
153         else
154                 cname = strdup(chip->prefix);
155
156         psensor = psensor_create(id, label, cname, type, values_max_length);
157
158         psensor->iname = chip;
159         psensor->feature = feature;
160
161         if (feature->type == SENSORS_FEATURE_TEMP
162             && (get_temp_input(psensor) == UNKNOWN_DBL_VALUE)) {
163                 free(psensor);
164                 return NULL;
165         }
166
167         return psensor;
168 }
169
170 struct psensor **lmsensor_psensor_list_add(struct psensor **sensors,
171                                            int vn)
172 {
173         const sensors_chip_name *chip;
174         int chip_nr = 0;
175         struct psensor **tmp, **result;
176         const sensors_feature *feature;
177         struct psensor *s;
178         int i;
179
180         if (!init_done)
181                 return NULL;
182
183         result = sensors;
184         while ((chip = sensors_get_detected_chips(NULL, &chip_nr))) {
185
186                 i = 0;
187                 while ((feature = sensors_get_features(chip, &i))) {
188
189                         if (feature->type == SENSORS_FEATURE_TEMP
190                             || feature->type == SENSORS_FEATURE_FAN) {
191
192                                 s = lmsensor_psensor_create(chip, feature, vn);
193
194                                 if (s) {
195                                         tmp = psensor_list_add(result, s);
196
197                                         if (tmp != sensors)
198                                                 free(result);
199
200                                         result = tmp;
201                                 }
202                         }
203                 }
204         }
205
206         return result;
207 }
208
209 void lmsensor_init()
210 {
211         int err = sensors_init(NULL);
212
213         if (err) {
214                 log_err(_("lmsensor: initialization failure: %s."),
215                         sensors_strerror(err));
216                 init_done = 0;
217         } else {
218                 init_done = 1;
219         }
220 }
221
222 void lmsensor_cleanup()
223 {
224         if (init_done)
225                 sensors_cleanup();
226 }