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

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

Remove "generic" from all printing function names. This is the default
mode now so no need to mention it explicitly.
Rename print_vid_info to print_chip_vid and move it around for
consistency.

  • 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 sensors_get_available_features(const sensors_chip_name *name,
71                                           const sensors_feature_data *feature,
72                                           int i, short *has_features,
73                                           double *feature_vals, int size,
74                                           int first_val)
75{
76        const sensors_feature_data *iter;
77
78        while ((iter = sensors_get_all_features(name, &i)) &&
79               iter->mapping == feature->number) {
80                int indx;
81
82                indx = iter->type - first_val - 1;
83                if (indx < 0 || indx >= size) {
84                        printf("ERROR: Bug in sensors: index out of bound");
85                        return;
86                }
87
88                if (sensors_get_value(name, iter->number, &feature_vals[indx]))
89                        printf("ERROR: Can't get %s data!\n", iter->name);
90
91                has_features[indx] = 1;
92        }
93}
94
95static int sensors_get_label_size(const sensors_chip_name *name)
96{
97        int i;
98        const sensors_feature_data *iter;
99        char *label;
100        unsigned int max_size = 11;     /* 11 as minumum label width */
101
102        i = 0;
103        while ((iter = sensors_get_all_features(name, &i))) {
104                if (iter->mapping != SENSORS_NO_MAPPING)
105                        continue;
106                if ((label = sensors_get_label(name, iter->number)) &&
107                    strlen(label) > max_size)
108                        max_size = strlen(label);
109                free(label);
110        }
111        return max_size + 1;
112}
113
114static void print_temp_limits(double limit1, double limit2,
115                              const char *name1, const char *name2, int alarm)
116{
117        if (fahrenheit) {
118                limit1 = deg_ctof(limit1);
119                limit2 = deg_ctof(limit2);
120        }
121
122        if (name2) {
123                printf("(%-4s = %+5.1f%s, %-4s = %+5.1f%s)  ",
124                       name1, limit1, degstr,
125                       name2, limit2, degstr);
126        } else if (name1) {
127                printf("(%-4s = %+5.1f%s)                  ",
128                       name1, limit1, degstr);
129        } else {
130                printf("                                  ");
131        }
132
133        if (alarm)
134                printf("ALARM  ");
135}
136
137#define TEMP_FEATURE(x)         has_features[x - SENSORS_FEATURE_TEMP - 1]
138#define TEMP_FEATURE_VAL(x)     feature_vals[x - SENSORS_FEATURE_TEMP - 1]
139static void print_chip_temp(const sensors_chip_name *name,
140                            const sensors_feature_data *feature, int i,
141                            int label_size)
142{
143        double val, limit1, limit2;
144        const char *s1, *s2;
145        int alarm, crit_displayed = 0;
146        char *label;
147        const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP;
148        short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0, };
149        double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = { 0.0, };
150
151        if (!(label = sensors_get_label(name, feature->number))) {
152                printf("ERROR: Can't get temperature label!\n");
153                return;
154        }
155
156        if (sensors_get_value(name, feature->number, &val)) {
157                printf("ERROR: Can't get %s data!\n", label);
158                free(label);
159                return;
160        }
161
162        sensors_get_available_features(name, feature, i, has_features,
163                                       feature_vals, size,
164                                       SENSORS_FEATURE_TEMP);
165
166        alarm = TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) &&
167                TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM);
168        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) {
169                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_ALARM) &&
170                    TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_ALARM))
171                        alarm |= 1;
172
173                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) {
174                        limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN);
175                        s1 = "low";
176                        limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
177                        s2 = "high";
178
179                        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN_ALARM) &&
180                            TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN_ALARM))
181                                alarm |= 1;
182                } else {
183                        limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
184                        s1 = "high";
185
186                        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_HYST)) {
187                                limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_HYST);
188                                s2 = "hyst";
189                        } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
190                                limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
191                                s2 = "crit";
192
193                                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&
194                                    TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM))
195                                        alarm |= 1;
196                                crit_displayed = 1;
197                        } else {
198                                limit2 = 0;
199                                s2 = NULL;
200                        }
201                }
202        } else {
203                limit1 = limit2 = 0;
204                s1 = s2 = NULL;
205        }
206
207        print_label(label, label_size);
208        free(label);
209
210        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) &&
211            TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT)) {
212                printf("   FAULT  ");
213        } else {
214                if (fahrenheit)
215                        val = deg_ctof(val);
216                printf("%+6.1f%s  ", val, degstr);
217        }
218        print_temp_limits(limit1, limit2, s1, s2, alarm);
219
220        if (!crit_displayed && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
221                limit1 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
222                s1 = "crit";
223
224                if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_HYST)) {
225                        limit2 = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST);
226                        s2 = "hyst";
227                } else {
228                        limit2 = 0;
229                        s2 = NULL;
230                }
231
232                alarm = TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&
233                        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM);
234
235                printf("\n%*s", label_size + 10, "");
236                print_temp_limits(limit1, limit2, s1, s2, alarm);
237        }
238
239        /* print out temperature sensor info */
240        if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) {
241                int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS);
242
243                /* older kernels / drivers sometimes report a beta value for
244                   thermistors */
245                if (sens > 1000)
246                        sens = 4;
247
248                printf("sensor = %s", sens == 0 ? "disabled" :
249                       sens == 1 ? "diode" :
250                       sens == 2 ? "transistor" :
251                       sens == 3 ? "thermal diode" :
252                       sens == 4 ? "thermistor" :
253                       sens == 5 ? "AMD AMDSI" :
254                       sens == 6 ? "Intel PECI" : "unknown");
255        }
256        printf("\n");
257}
258
259#define IN_FEATURE(x)           has_features[x - SENSORS_FEATURE_IN - 1]
260#define IN_FEATURE_VAL(x)       feature_vals[x - SENSORS_FEATURE_IN - 1]
261static void print_chip_in(const sensors_chip_name *name,
262                          const sensors_feature_data *feature, int i,
263                          int label_size)
264{
265        const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN;
266        short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0, };
267        double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = { 0.0, };
268        double val, alarm_max, alarm_min;
269        char *label;
270
271        if (!(label = sensors_get_label(name, feature->number))) {
272                printf("ERROR: Can't get in label!\n");
273                return;
274        }
275
276        if (sensors_get_value(name, feature->number, &val)) {
277                printf("ERROR: Can't get %s data!\n", label);
278                free(label);
279                return;
280        }
281
282        sensors_get_available_features(name, feature, i, has_features,
283                                       feature_vals, size, SENSORS_FEATURE_IN);
284
285        print_label(label, label_size);
286        free(label);
287        printf("%+6.2f V", val);
288
289        if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) &&
290            IN_FEATURE(SENSORS_FEATURE_IN_MAX))
291                printf("  (min = %+6.2f V, max = %+6.2f V)",
292                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN),
293                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
294        else if (IN_FEATURE(SENSORS_FEATURE_IN_MIN))
295                printf("  (min = %+6.2f V)",
296                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN));
297        else if (IN_FEATURE(SENSORS_FEATURE_IN_MAX))
298                printf("  (max = %+6.2f V)",
299                       IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
300
301        if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) ||
302            IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) {
303                alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM);
304                alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM);
305
306                if (alarm_min || alarm_max) {
307                        printf(" ALARM (");
308
309                        if (alarm_min)
310                                printf("MIN");
311                        if (alarm_max)
312                                printf("%sMAX", (alarm_min) ? ", " : "");
313
314                        printf(")");
315                }
316        } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) {
317                printf("   %s",
318                IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) ? "ALARM" : "");
319        }
320
321        printf("\n");
322}
323
324#define FAN_FEATURE(x)          has_features[x - SENSORS_FEATURE_FAN - 1]
325#define FAN_FEATURE_VAL(x)      feature_vals[x - SENSORS_FEATURE_FAN - 1]
326static void print_chip_fan(const sensors_chip_name *name,
327                           const sensors_feature_data *feature, int i,
328                           int label_size)
329{
330        char *label;
331        const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN;
332        short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0, };
333        double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = { 0.0, };
334        double val;
335
336        if (!(label = sensors_get_label(name, feature->number))) {
337                printf("ERROR: Can't get fan label!\n");
338                return;
339        }
340
341        if (sensors_get_value(name, feature->number, &val)) {
342                printf("ERROR: Can't get %s data!\n", label);
343                free(label);
344                return;
345        }
346
347        print_label(label, label_size);
348        free(label);
349
350        if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) &&
351            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_FAULT))
352                printf("   FAULT");
353        else
354                printf("%4.0f RPM", val);
355
356        sensors_get_available_features(name, feature, i, has_features,
357                                       feature_vals, size, SENSORS_FEATURE_FAN);
358
359        if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN) &&
360            FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
361                printf("  (min = %4.0f RPM, div = %1.0f)",
362                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN),
363                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
364        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN))
365                printf("  (min = %4.0f RPM)",
366                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN));
367        else if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
368                printf("  (div = %1.0f)",
369                       FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
370
371        if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) &&
372            FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) {
373                printf("  ALARM");
374        }
375
376        printf("\n");
377}
378
379static void print_chip_vid(const sensors_chip_name *name, int f_vid,
380                           int label_size)
381{
382        char *label;
383        double vid;
384
385        if ((label = sensors_get_label(name, f_vid))
386         && !sensors_get_value(name, f_vid, &vid)) {
387                print_label(label, label_size);
388                printf("%+6.3f V\n", vid);
389        }
390        free(label);
391}
392
393void print_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_chip_temp(name, feature, i, label_size);
408                        break;
409                case SENSORS_FEATURE_IN:
410                        print_chip_in(name, feature, i, label_size);
411                        break;
412                case SENSORS_FEATURE_FAN:
413                        print_chip_fan(name, feature, i, label_size);
414                        break;
415                case SENSORS_FEATURE_VID:
416                        print_chip_vid(name, feature->number, label_size);
417                        break;
418                default:
419                        continue;
420                }
421        }
422}
Note: See TracBrowser for help on using the browser.