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

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

sensors: Make error messages more helpful and consistent.

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