root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/chips.c @ 4726

Revision 4726, 11.8 KB (checked in by khali, 6 years ago)

Fix a memory leak on error in raw print mode.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    chips.c - Part of sensors, a user-space program for hardware monitoring
3    Copyright (c) 1998-2003 Frodo Looijaard <frodol@dds.nl>
4                            and Mark D. Studebaker <mdsxyz123@yahoo.com>
5    Copyright (c) 2003-2006 The lm_sensors team
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "main.h"
27#include "chips.h"
28#include "lib/sensors.h"
29
30void print_chip_raw(const sensors_chip_name *name)
31{
32        int a;
33        const sensors_feature_data *data;
34        char *label;
35        double val;
36
37        a = 0;
38        while ((data = sensors_get_all_features(name, &a))) {
39                if (!(label = sensors_get_label(name, data->number))) {
40                        printf("ERROR: Can't get feature `%s' label!\n",
41                               data->name);
42                        continue;
43                }
44                if (data->mode & SENSORS_MODE_R) {
45                        if (sensors_get_value(name, data->number, &val))
46                                printf("ERROR: Can't get feature `%s' data!\n",
47                                       data->name);
48                        else if (data->mapping != SENSORS_NO_MAPPING)
49                                printf("  %s: %.2f\n", label, val);
50                        else
51                                printf("%s: %.2f (%s)\n", label, val,
52                                       data->name);
53                } else
54                        printf("(%s)\n", label);
55                free(label);
56        }
57}
58
59static inline double deg_ctof(double cel)
60{
61        return cel * (9.0F / 5.0F) + 32.0F;
62}
63
64static void print_label(const char *label, int space)
65{
66        int len = strlen(label)+1;
67        printf("%s:%*s", label, space - len, "");
68}
69
70static void print_vid_info(const sensors_chip_name *name, int f_vid,
71                           int label_size)
72{
73        char *label;
74        double vid;
75
76        if ((label = sensors_get_label(name, f_vid))
77         && !sensors_get_value(name, f_vid, &vid)) {
78                print_label(label, label_size);
79                printf("%+6.3f V\n", vid);
80        }
81        free(label);
82}
83
84static void sensors_get_available_features(const sensors_chip_name *name,
85                                           const sensors_feature_data *feature,
86                                           int i, short *has_features,
87                                           double *feature_vals, int size,
88                                           int first_val)
89{
90        const sensors_feature_data *iter;
91
92        while ((iter = sensors_get_all_features(name, &i)) &&
93               iter->mapping == feature->number) {
94                int indx;
95
96                indx = iter->type - first_val - 1;
97                if (indx < 0 || indx >= size) {
98                        printf("ERROR: Bug in sensors: index out of bound");
99                        return;
100                }
101
102                if (sensors_get_value(name, iter->number, &feature_vals[indx]))
103                        printf("ERROR: Can't get %s data!\n", iter->name);
104
105                has_features[indx] = 1;
106        }
107}
108
109static int sensors_get_label_size(const sensors_chip_name *name)
110{
111        int i;
112        const sensors_feature_data *iter;
113        char *label;
114        unsigned int max_size = 11;     /* 11 as minumum label width */
115
116        i = 0;
117        while ((iter = sensors_get_all_features(name, &i))) {
118                if (iter->mapping != SENSORS_NO_MAPPING)
119                        continue;
120                if ((label = sensors_get_label(name, iter->number)) &&
121                    strlen(label) > max_size)
122                        max_size = strlen(label);
123                free(label);
124        }
125        return max_size + 1;
126}
127
128static void print_temp_limits(double limit1, double limit2,
129                              const char *name1, const char *name2, int alarm)
130{
131        if (fahrenheit) {
132                limit1 = deg_ctof(limit1);
133                limit2 = deg_ctof(limit2);
134        }
135
136        if (name2) {
137                printf("(%-4s = %+5.1f%s, %-4s = %+5.1f%s)  ",
138                       name1, limit1, degstr,
139                       name2, limit2, degstr);
140        } else if (name1) {
141                printf("(%-4s = %+5.1f%s)                  ",
142                       name1, limit1, degstr);
143        } else {
144                printf("                                  ");
145        }
146
147        if (alarm)
148                printf("ALARM  ");
149}
150
151#define TEMP_FEATURE(x)         has_features[x - SENSORS_FEATURE_TEMP - 1]
152#define TEMP_FEATURE_VAL(x)     feature_vals[x - SENSORS_FEATURE_TEMP - 1]
153static void print_generic_chip_temp(const sensors_chip_name *name,
154                                    const sensors_feature_data *feature,
155                                    int i, int label_size)
156{
157        double val, limit1, limit2;
158        const char *s1, *s2;
159        int alarm, crit_displayed = 0;
160        char *label;
161        const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP;
162        short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0, };
163        double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0.0, };
164
165        if (!(label = sensors_get_label(name, feature->number))) {
166                printf("ERROR: Can't get temperature label!\n");
167                return;
168        }
169
170        if (sensors_get_value(name, feature->number, &val)) {
171                printf("ERROR: Can't get %s data!\n", label);
172                free(label);
173                return;
174        }
175
176        sensors_get_available_features(name, feature, i, has_features,
177                                       feature_vals, size,
178                                       SENSORS_FEATURE_TEMP);
179
180        alarm = TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) &&
181                TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM);
182        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) {
183                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_ALARM) &&
184                    TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_ALARM))
185                        alarm |= 1;
186
187                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) {
188                        limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN);
189                        s1 = "low";
190                        limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
191                        s2 = "high";
192
193                        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN_ALARM) &&
194                            TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN_ALARM))
195                                alarm |= 1;
196                } else {
197                        limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
198                        s1 = "high";
199
200                        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_HYST)) {
201                                limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_HYST);
202                                s2 = "hyst";
203                        } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
204                                limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
205                                s2 = "crit";
206
207                                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&
208                                    TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM))
209                                        alarm |= 1;
210                                crit_displayed = 1;
211                        } else {
212                                limit2 = 0;
213                                s2 = NULL;
214                        }
215                }
216        } else {
217                limit1 = limit2 = 0;
218                s1 = s2 = NULL;
219        }
220
221        print_label(label, label_size);
222        free(label);
223
224        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) &&
225            TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT)) {
226                printf("   FAULT  ");
227        } else {
228                if (fahrenheit)
229                        val = deg_ctof(val);
230                printf("%+6.1f%s  ", val, degstr);
231        }
232        print_temp_limits(limit1, limit2, s1, s2, alarm);
233
234        if (!crit_displayed && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
235                limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
236                s1 = "crit";
237
238                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_HYST)) {
239                        limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST);
240                        s2 = "hyst";
241                } else {
242                        limit2 = 0;
243                        s2 = NULL;
244                }
245
246                alarm = TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&
247                        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM);
248
249                printf("\n%*s", label_size + 10, "");
250                print_temp_limits(limit1, limit2, s1, s2, alarm);
251        }
252
253        /* print out temperature sensor info */
254        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) {
255                int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS);
256
257                /* older kernels / drivers sometimes report a beta value for
258                   thermistors */
259                if (sens > 1000)
260                        sens = 4;
261
262                printf("sensor = %s", sens == 0 ? "disabled" :
263                       sens == 1 ? "diode" :
264                       sens == 2 ? "transistor" :
265                       sens == 3 ? "thermal diode" :
266                       sens == 4 ? "thermistor" :
267                       sens == 5 ? "AMD AMDSI" :
268                       sens == 6 ? "Intel PECI" : "unknown");
269        }
270        printf("\n");
271}
272
273#define IN_FEATURE(x)           has_features[x - SENSORS_FEATURE_IN - 1]
274#define IN_FEATURE_VAL(x)       feature_vals[x - SENSORS_FEATURE_IN - 1]
275static void print_generic_chip_in(const sensors_chip_name *name,
276                                  const sensors_feature_data *feature,
277                                  int i, int label_size)
278{
279        const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN;
280        short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0, };
281        double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0.0, };
282        double val, alarm_max, alarm_min;
283        char *label;
284
285        if (!(label = sensors_get_label(name, feature->number))) {
286                printf("ERROR: Can't get in label!\n");
287                return;
288        }
289
290        if (sensors_get_value(name, feature->number, &val)) {
291                printf("ERROR: Can't get %s data!\n", label);
292                free(label);
293                return;
294        }
295
296        sensors_get_available_features(name, feature, i, has_features,
297                                       feature_vals, size, SENSORS_FEATURE_IN);
298
299        print_label(label, label_size);
300        free(label);
301        printf("%+6.2f V", val);
302
303        if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) &&
304            IN_FEATURE(SENSORS_FEATURE_IN_MAX))
305                printf("  (min = %+6.2f V, max = %+6.2f V)",
306                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN),
307                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
308        else if (IN_FEATURE(SENSORS_FEATURE_IN_MIN))
309                printf("  (min = %+6.2f V)",
310                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN));
311        else if (IN_FEATURE(SENSORS_FEATURE_IN_MAX))
312                printf("  (max = %+6.2f V)",
313                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
314
315        if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) ||
316            IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) {
317                alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM);
318                alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM);
319
320                if (alarm_min || alarm_max) {
321                        printf(" ALARM (");
322
323                        if (alarm_min)
324                                printf("MIN");
325                        if (alarm_max)
326                                printf("%sMAX", (alarm_min) ? ", " : "");
327
328                        printf(")");
329                }
330        } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) {
331                printf("   %s",
332                IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) ? "ALARM" : "");
333        }
334
335        printf("\n");
336}
337
338#define FAN_FEATURE(x)          has_features[x - SENSORS_FEATURE_FAN - 1]
339#define FAN_FEATURE_VAL(x)      feature_vals[x - SENSORS_FEATURE_FAN - 1]
340static void print_generic_chip_fan(const sensors_chip_name *name,
341                                   const sensors_feature_data *feature,
342                                   int i, int label_size)
343{
344        char *label;
345        const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN;
346        short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0, };
347        double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0.0, };
348        double val;
349
350        if (!(label = sensors_get_label(name, feature->number))) {
351                printf("ERROR: Can't get fan label!\n");
352                return;
353        }
354
355        if (sensors_get_value(name, feature->number, &val)) {
356                printf("ERROR: Can't get %s data!\n", label);
357                free(label);
358                return;
359        }
360
361        print_label(label, label_size);
362        free(label);
363
364        if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) &&
365            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_FAULT))
366                printf("   FAULT");
367        else
368                printf("%4.0f RPM", val);
369
370        sensors_get_available_features(name, feature, i, has_features,
371                                       feature_vals, size, SENSORS_FEATURE_FAN);
372
373        if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN) &&
374            FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
375                printf("  (min = %4.0f RPM, div = %1.0f)",
376                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN),
377                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
378        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN))
379                printf("  (min = %4.0f RPM)",
380                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN));
381        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
382                printf("  (div = %1.0f)",
383                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
384
385        if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) &&
386            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) {
387                printf("  ALARM");
388        }
389
390        printf("\n");
391}
392
393void print_generic_chip(const sensors_chip_name *name)
394{
395        const sensors_feature_data *feature;
396        int i, label_size;
397
398        label_size = sensors_get_label_size(name);
399
400        i = 0;
401        while ((feature = sensors_get_all_features(name, &i))) {
402                if (feature->mapping != SENSORS_NO_MAPPING)
403                        continue;
404
405                switch (feature->type) {
406                case SENSORS_FEATURE_TEMP:
407                        print_generic_chip_temp(name, feature, i, label_size);
408                        break;
409                case SENSORS_FEATURE_IN:
410                        print_generic_chip_in(name, feature, i, label_size);
411                        break;
412                case SENSORS_FEATURE_FAN:
413                        print_generic_chip_fan(name, feature, i, label_size);
414                        break;
415                case SENSORS_FEATURE_VID:
416                        print_vid_info(name, feature->number, label_size);
417                        break;
418                default:
419                        continue;
420                }
421        }
422}
Note: See TracBrowser for help on using the browser.