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

Revision 4897, 11.7 KB (checked in by khali, 6 years ago)

sensors: Print error messages to stderr.

  • 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#include "lib/error.h"
30
31void print_chip_raw(const sensors_chip_name *name)
32{
33        int a, b;
34        const sensors_feature *feature;
35        const sensors_subfeature *sub;
36        char *label;
37        double val;
38
39        a = 0;
40        while ((feature = sensors_get_features(name, &a))) {
41                if (!(label = sensors_get_label(name, feature))) {
42                        fprintf(stderr, "ERROR: Can't get feature label!\n");
43                        continue;
44                }
45                printf("%s:\n", label);
46                free(label);
47
48                b = 0;
49                while ((sub = sensors_get_all_subfeatures(name, feature, &b))) {
50                        if (sub->flags & SENSORS_MODE_R) {
51                                if (sensors_get_value(name, sub->number, &val))
52                                        fprintf(stderr, "ERROR: Can't get "
53                                                "feature `%s' data!\n",
54                                                sub->name);
55                                else
56                                        printf("  %s: %.2f\n", sub->name, val);
57                        } else
58                                printf("(%s)\n", label);
59                }
60        }
61}
62
63static inline double deg_ctof(double cel)
64{
65        return cel * (9.0F / 5.0F) + 32.0F;
66}
67
68static void print_label(const char *label, int space)
69{
70        int len = strlen(label)+1;
71        printf("%s:%*s", label, space - len, "");
72}
73
74static double get_value(const sensors_chip_name *name, int subfeat_nr)
75{
76        double val;
77        int err;
78
79        err = sensors_get_value(name, subfeat_nr, &val);
80        if (err) {
81                fprintf(stderr, "ERROR: Can't get value of subfeature %d: %s\n",
82                        subfeat_nr, sensors_strerror(err));
83                val = 0;
84        }
85        return val;
86}
87
88static int get_label_size(const sensors_chip_name *name)
89{
90        int i;
91        const sensors_feature *iter;
92        char *label;
93        unsigned int max_size = 11;     /* 11 as minumum label width */
94
95        i = 0;
96        while ((iter = sensors_get_features(name, &i))) {
97                if ((label = sensors_get_label(name, iter)) &&
98                    strlen(label) > max_size)
99                        max_size = strlen(label);
100                free(label);
101        }
102        return max_size + 1;
103}
104
105static void print_temp_limits(double limit1, double limit2,
106                              const char *name1, const char *name2, int alarm)
107{
108        if (fahrenheit) {
109                limit1 = deg_ctof(limit1);
110                limit2 = deg_ctof(limit2);
111        }
112
113        if (name2) {
114                printf("(%-4s = %+5.1f%s, %-4s = %+5.1f%s)  ",
115                       name1, limit1, degstr,
116                       name2, limit2, degstr);
117        } else if (name1) {
118                printf("(%-4s = %+5.1f%s)                  ",
119                       name1, limit1, degstr);
120        } else {
121                printf("                                  ");
122        }
123
124        if (alarm)
125                printf("ALARM  ");
126}
127
128static void print_chip_temp(const sensors_chip_name *name,
129                            const sensors_feature *feature,
130                            int label_size)
131{
132        const sensors_subfeature *sf, *sfmin, *sfmax, *sfcrit, *sfhyst;
133        double val, limit1, limit2;
134        const char *s1, *s2;
135        int alarm, crit_displayed = 0;
136        char *label;
137
138        if (!(label = sensors_get_label(name, feature))) {
139                fprintf(stderr, "ERROR: Can't get temperature label!\n");
140                return;
141        }
142        print_label(label, label_size);
143        free(label);
144
145        sf = sensors_get_subfeature(name, feature,
146                                    SENSORS_SUBFEATURE_TEMP_INPUT);
147        val = sf ? get_value(name, sf->number) : 0;
148
149        sf = sensors_get_subfeature(name, feature,
150                                    SENSORS_SUBFEATURE_TEMP_ALARM);
151        alarm = sf && get_value(name, sf->number);
152
153        sfmin = sensors_get_subfeature(name, feature,
154                                       SENSORS_SUBFEATURE_TEMP_MIN);
155        sfmax = sensors_get_subfeature(name, feature,
156                                       SENSORS_SUBFEATURE_TEMP_MAX);
157        sfcrit = sensors_get_subfeature(name, feature,
158                                        SENSORS_SUBFEATURE_TEMP_CRIT);
159        if (sfmax) {
160                sf = sensors_get_subfeature(name, feature,
161                                        SENSORS_SUBFEATURE_TEMP_MAX_ALARM);
162                if (sf && get_value(name, sf->number))
163                        alarm |= 1;
164
165                if (sfmin) {
166                        limit1 = get_value(name, sfmin->number);
167                        s1 = "low";
168                        limit2 = get_value(name, sfmax->number);
169                        s2 = "high";
170
171                        sf = sensors_get_subfeature(name, feature,
172                                        SENSORS_SUBFEATURE_TEMP_MIN_ALARM);
173                        if (sf && get_value(name, sf->number))
174                                alarm |= 1;
175                } else {
176                        limit1 = get_value(name, sfmax->number);
177                        s1 = "high";
178
179                        sfhyst = sensors_get_subfeature(name, feature,
180                                        SENSORS_SUBFEATURE_TEMP_MAX_HYST);
181                        if (sfhyst) {
182                                limit2 = get_value(name, sfhyst->number);
183                                s2 = "hyst";
184                        } else if (sfcrit) {
185                                limit2 = get_value(name, sfcrit->number);
186                                s2 = "crit";
187
188                                sf = sensors_get_subfeature(name, feature,
189                                        SENSORS_SUBFEATURE_TEMP_CRIT_ALARM);
190                                if (sf && get_value(name, sf->number))
191                                        alarm |= 1;
192                                crit_displayed = 1;
193                        } else {
194                                limit2 = 0;
195                                s2 = NULL;
196                        }
197                }
198        } else if (sfcrit) {
199                limit1 = get_value(name, sfcrit->number);
200                s1 = "crit";
201
202                sfhyst = sensors_get_subfeature(name, feature,
203                                        SENSORS_SUBFEATURE_TEMP_CRIT_HYST);
204                if (sfhyst) {
205                        limit2 = get_value(name, sfhyst->number);
206                        s2 = "hyst";
207                } else {
208                        limit2 = 0;
209                        s2 = NULL;
210                }
211
212                sf = sensors_get_subfeature(name, feature,
213                                        SENSORS_SUBFEATURE_TEMP_CRIT_ALARM);
214                if (sf && get_value(name, sf->number))
215                        alarm |= 1;
216                crit_displayed = 1;
217        } else {
218                limit1 = limit2 = 0;
219                s1 = s2 = NULL;
220        }
221
222
223        sf = sensors_get_subfeature(name, feature,
224                                    SENSORS_SUBFEATURE_TEMP_FAULT);
225        if (sf && get_value(name, sf->number)) {
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 && sfcrit) {
235                limit1 = get_value(name, sfcrit->number);
236                s1 = "crit";
237
238                sfhyst = sensors_get_subfeature(name, feature,
239                                        SENSORS_SUBFEATURE_TEMP_CRIT_HYST);
240                if (sfhyst) {
241                        limit2 = get_value(name, sfhyst->number);
242                        s2 = "hyst";
243                } else {
244                        limit2 = 0;
245                        s2 = NULL;
246                }
247
248                sf = sensors_get_subfeature(name, feature,
249                                        SENSORS_SUBFEATURE_TEMP_CRIT_ALARM);
250                alarm = sf && get_value(name, sf->number);
251
252                printf("\n%*s", label_size + 10, "");
253                print_temp_limits(limit1, limit2, s1, s2, alarm);
254        }
255
256        /* print out temperature sensor info */
257        sf = sensors_get_subfeature(name, feature,
258                                    SENSORS_SUBFEATURE_TEMP_TYPE);
259        if (sf) {
260                int sens = (int)get_value(name, sf->number);
261
262                /* older kernels / drivers sometimes report a beta value for
263                   thermistors */
264                if (sens > 1000)
265                        sens = 4;
266
267                printf("sensor = %s", sens == 0 ? "disabled" :
268                       sens == 1 ? "diode" :
269                       sens == 2 ? "transistor" :
270                       sens == 3 ? "thermal diode" :
271                       sens == 4 ? "thermistor" :
272                       sens == 5 ? "AMD AMDSI" :
273                       sens == 6 ? "Intel PECI" : "unknown");
274        }
275        printf("\n");
276}
277
278static void print_chip_in(const sensors_chip_name *name,
279                          const sensors_feature *feature,
280                          int label_size)
281{
282        const sensors_subfeature *sf, *sfmin, *sfmax;
283        double val, alarm_max, alarm_min;
284        char *label;
285
286        if (!(label = sensors_get_label(name, feature))) {
287                fprintf(stderr, "ERROR: Can't get in label!\n");
288                return;
289        }
290        print_label(label, label_size);
291        free(label);
292
293        sf = sensors_get_subfeature(name, feature,
294                                    SENSORS_SUBFEATURE_IN_INPUT);
295        val = sf ? get_value(name, sf->number) : 0;
296        printf("%+6.2f V", val);
297
298        sfmin = sensors_get_subfeature(name, feature,
299                                       SENSORS_SUBFEATURE_IN_MIN);
300        sfmax = sensors_get_subfeature(name, feature,
301                                       SENSORS_SUBFEATURE_IN_MAX);
302        if (sfmin && sfmax)
303                printf("  (min = %+6.2f V, max = %+6.2f V)",
304                       get_value(name, sfmin->number),
305                       get_value(name, sfmax->number));
306        else if (sfmin)
307                printf("  (min = %+6.2f V)",
308                       get_value(name, sfmin->number));
309        else if (sfmax)
310                printf("  (max = %+6.2f V)",
311                       get_value(name, sfmax->number));
312
313        sf = sensors_get_subfeature(name, feature,
314                                    SENSORS_SUBFEATURE_IN_ALARM);
315        sfmin = sensors_get_subfeature(name, feature,
316                                       SENSORS_SUBFEATURE_IN_MIN_ALARM);
317        sfmax = sensors_get_subfeature(name, feature,
318                                       SENSORS_SUBFEATURE_IN_MAX_ALARM);
319        if (sfmin || sfmax) {
320                alarm_max = sfmax ? get_value(name, sfmax->number) : 0;
321                alarm_min = sfmin ? get_value(name, sfmin->number) : 0;
322
323                if (alarm_min || alarm_max) {
324                        printf(" ALARM (");
325
326                        if (alarm_min)
327                                printf("MIN");
328                        if (alarm_max)
329                                printf("%sMAX", (alarm_min) ? ", " : "");
330
331                        printf(")");
332                }
333        } else if (sf) {
334                printf("   %s",
335                       get_value(name, sf->number) ? "ALARM" : "");
336        }
337
338        printf("\n");
339}
340
341static void print_chip_fan(const sensors_chip_name *name,
342                           const sensors_feature *feature,
343                           int label_size)
344{
345        const sensors_subfeature *sf, *sfmin, *sfdiv;
346        char *label;
347        double val;
348
349        if (!(label = sensors_get_label(name, feature))) {
350                fprintf(stderr, "ERROR: Can't get fan label!\n");
351                return;
352        }
353        print_label(label, label_size);
354        free(label);
355
356        sf = sensors_get_subfeature(name, feature,
357                                    SENSORS_SUBFEATURE_FAN_INPUT);
358        val = sf ? get_value(name, sf->number) : 0;
359        sf = sensors_get_subfeature(name, feature,
360                                    SENSORS_SUBFEATURE_FAN_FAULT);
361        if (sf && get_value(name, sf->number))
362                printf("   FAULT");
363        else
364                printf("%4.0f RPM", val);
365
366        sfmin = sensors_get_subfeature(name, feature,
367                                       SENSORS_SUBFEATURE_FAN_MIN);
368        sfdiv = sensors_get_subfeature(name, feature,
369                                       SENSORS_SUBFEATURE_FAN_DIV);
370        if (sfmin && sfdiv)
371                printf("  (min = %4.0f RPM, div = %1.0f)",
372                       get_value(name, sfmin->number),
373                       get_value(name, sfdiv->number));
374        else if (sfmin)
375                printf("  (min = %4.0f RPM)",
376                       get_value(name, sfmin->number));
377        else if (sfdiv)
378                printf("  (div = %1.0f)",
379                       get_value(name, sfdiv->number));
380
381        sf = sensors_get_subfeature(name, feature,
382                                    SENSORS_SUBFEATURE_FAN_ALARM);
383        if (sf && get_value(name, sf->number)) {
384                printf("  ALARM");
385        }
386
387        printf("\n");
388}
389
390static void print_chip_vid(const sensors_chip_name *name,
391                           const sensors_feature *feature,
392                           int label_size)
393{
394        char *label;
395        const sensors_subfeature *subfeature;
396        double vid;
397
398        subfeature = sensors_get_subfeature(name, feature,
399                                            SENSORS_SUBFEATURE_VID);
400        if (!subfeature)
401                return;
402
403        if ((label = sensors_get_label(name, feature))
404         && !sensors_get_value(name, subfeature->number, &vid)) {
405                print_label(label, label_size);
406                printf("%+6.3f V\n", vid);
407        }
408        free(label);
409}
410
411static void print_chip_beep_enable(const sensors_chip_name *name,
412                                   const sensors_feature *feature,
413                                   int label_size)
414{
415        char *label;
416        const sensors_subfeature *subfeature;
417        double beep_enable;
418
419        subfeature = sensors_get_subfeature(name, feature,
420                                            SENSORS_SUBFEATURE_BEEP_ENABLE);
421        if (!subfeature)
422                return;
423
424        if ((label = sensors_get_label(name, feature))
425         && !sensors_get_value(name, subfeature->number, &beep_enable)) {
426                print_label(label, label_size);
427                printf("%s\n", beep_enable ? "enabled" : "disabled");
428        }
429        free(label);
430}
431
432void print_chip(const sensors_chip_name *name)
433{
434        const sensors_feature *feature;
435        int i, label_size;
436
437        label_size = get_label_size(name);
438
439        i = 0;
440        while ((feature = sensors_get_features(name, &i))) {
441                switch (feature->type) {
442                case SENSORS_FEATURE_TEMP:
443                        print_chip_temp(name, feature, label_size);
444                        break;
445                case SENSORS_FEATURE_IN:
446                        print_chip_in(name, feature, label_size);
447                        break;
448                case SENSORS_FEATURE_FAN:
449                        print_chip_fan(name, feature, label_size);
450                        break;
451                case SENSORS_FEATURE_VID:
452                        print_chip_vid(name, feature, label_size);
453                        break;
454                case SENSORS_FEATURE_BEEP_ENABLE:
455                        print_chip_beep_enable(name, feature, label_size);
456                        break;
457                default:
458                        continue;
459                }
460        }
461}
Note: See TracBrowser for help on using the browser.