root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/chips_generic.c @ 4645

Revision 4645, 11.4 KB (checked in by khali, 6 years ago)

We no longer need sensors_get_label_and_valid(), as it does hardly
more than sensors_get_label(). This allows for some cleanups.

Line 
1/*
2    chips_generic.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 <stdlib.h>
23#include <string.h>
24#include <math.h>
25
26#include "chips_generic.h"
27#include "chips.h"
28
29static int get_feature_value(const sensors_chip_name *name, 
30                             const sensors_feature_data *feature, 
31                             double *val)
32{
33  return sensors_get_feature(*name, feature->number, val);
34}
35
36static void sensors_get_available_features(const sensors_chip_name *name, 
37                                           const sensors_feature_data *feature, 
38                                           int i,
39                                           short *has_features, 
40                                           double *feature_vals, 
41                                           int size, 
42                                           int first_val)
43{
44  const sensors_feature_data *iter;
45 
46  while((iter = sensors_get_all_features(*name, &i)) &&
47      iter->mapping == feature->number) {
48    int indx;
49   
50    indx = iter->type - first_val - 1;
51    if (indx < 0 || indx >= size) {
52      printf("ERROR: Bug in sensors: index out of bound");
53      return;
54    }
55   
56    if (get_feature_value(name, iter, &feature_vals[indx]))
57      printf("ERROR: Can't get %s data!\n", iter->name);
58   
59    has_features[indx] = 1;
60  }
61}
62
63static int sensors_get_label_size(const sensors_chip_name *name)
64{
65  int i;
66  const sensors_feature_data *iter;
67  char *label;
68  unsigned int max_size = 11; /* Initialised to 11 as minumum label-width */
69
70  i = 0;
71  while((iter = sensors_get_all_features(*name, &i))) {
72    if (!sensors_get_label(*name, iter->number, &label) &&
73        strlen(label) > max_size)
74      max_size = strlen(label);
75    free(label);
76  }
77  return max_size + 1;
78}
79
80extern int fahrenheit;
81extern char degstr[5];
82
83static inline float deg_ctof(float cel)
84{
85   return ( cel * ( 9.0F / 5.0F ) + 32.0F );
86}
87
88#define TEMP_FEATURE(x) has_features[x - SENSORS_FEATURE_TEMP - 1]
89#define TEMP_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_TEMP - 1]
90static void print_generic_chip_temp(const sensors_chip_name *name, 
91                                    const sensors_feature_data *feature,
92                                    int i, int label_size)
93{
94  double val, max, min;
95  char *label;
96  int type;
97  const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP;
98  short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0, };
99  double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0.0, };
100 
101  if (sensors_get_label(*name, feature->number, &label)) {
102    free(label);
103    printf("ERROR: Can't get temperature label!\n");
104    return;
105  }
106 
107  if (get_feature_value(name, feature, &val)) {
108    printf("ERROR: Can't get %s data!\n", label);
109    free(label);
110    return;
111  }
112 
113  sensors_get_available_features(name, feature, i, has_features,
114      feature_vals, size, SENSORS_FEATURE_TEMP);
115 
116  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) {
117    max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
118   
119    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) {
120      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN);
121      type = MINMAX;
122    } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_HYST)) {
123      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_HYST);
124      type = HYST;
125    } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
126      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
127      type = CRIT;
128    } else {
129      min = 0;
130      type = MAXONLY;
131    }
132  } else {
133    min = max = 0;
134    type = SINGLE;
135  }
136 
137  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) &&
138      TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT))
139    val = HUGE_VAL;
140 
141  print_label(label, label_size);
142  free(label);
143 
144  print_temp_info(val, max, min, type, 1, 1);
145 
146  /* ALARM features */
147  if ((TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) && 
148       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM) > 0.5)
149   || (type == MINMAX &&
150       TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN_ALARM) && 
151       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN_ALARM) > 0.5)
152   || (type == MINMAX &&
153       TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX_ALARM) && 
154       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX_ALARM) > 0.5)
155   || (type == CRIT &&
156       TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) && 
157       TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM) > 0.5)) {
158    printf("ALARM  ");
159  }
160 
161  if (type != CRIT && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
162    if (fahrenheit) {
163      TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT) = deg_ctof(
164        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT));
165      TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST) = deg_ctof(
166        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST));
167    }
168   
169    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_HYST))
170      printf("\n%*s(crit = %+5.1f%s, hyst = %+5.1f%s)  ", label_size + 10, "", 
171        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), degstr,
172        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_HYST), degstr);
173    else
174      printf("\n%*s(crit = %+5.1f%s)  ", label_size + 10, "", 
175        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT), degstr);
176
177    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT_ALARM) &&
178        TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT_ALARM)) {
179      printf("ALARM  ");
180    }
181  }       
182
183  /* print out temperature sensor info */
184  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) {
185    int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS);
186   
187    /* older kernels / drivers sometimes report a beta value for thermistors */
188    if (sens > 1000) 
189      sens = 4;
190   
191    printf("sensor = %s", sens == 0 ? "disabled" :
192                          sens == 1 ? "diode" :
193                          sens == 2 ? "transistor" :
194                          sens == 3 ? "thermal diode" :
195                          sens == 4 ? "thermistor" :
196                          sens == 5 ? "AMD AMDSI" :
197                          sens == 6 ? "Intel PECI" :
198                          "unknown");
199  }
200  printf("\n");
201}
202
203#define IN_FEATURE(x) has_features[x - SENSORS_FEATURE_IN - 1]
204#define IN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_IN - 1]
205static void print_generic_chip_in(const sensors_chip_name *name, 
206                                  const sensors_feature_data *feature,
207                                  int i, int label_size)
208{
209  const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN;
210  short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0, };
211  double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0.0, };
212  double val, alarm_max, alarm_min;
213  char *label;
214 
215  if (sensors_get_label(*name, feature->number, &label)) {
216    free(label);
217    printf("ERROR: Can't get in label!\n");
218    return;
219  }
220 
221  if (get_feature_value(name, feature, &val)) {
222    printf("ERROR: Can't get %s data!\n", label);
223    free(label);
224    return;
225  }
226 
227  sensors_get_available_features(name, feature, i, has_features, feature_vals,
228      size, SENSORS_FEATURE_IN);
229 
230  print_label(label, label_size);
231  free(label);
232  printf("%+6.2f V", val);
233 
234  if (IN_FEATURE(SENSORS_FEATURE_IN_MIN) && IN_FEATURE(SENSORS_FEATURE_IN_MAX))
235    printf("  (min = %+6.2f V, max = %+6.2f V)",
236      IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN),
237      IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
238  else if (IN_FEATURE(SENSORS_FEATURE_IN_MIN))
239    printf("  (min = %+6.2f V)", IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN));
240  else if (IN_FEATURE(SENSORS_FEATURE_IN_MAX))
241    printf("  (max = %+6.2f V)", IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
242 
243  if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) ||
244      IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) {
245    alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM);
246    alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM);
247   
248    if (alarm_min || alarm_max) {
249      printf(" ALARM (");
250     
251      if (alarm_min)
252        printf("MIN");
253      if (alarm_max)
254        printf("%sMAX", (alarm_min) ? ", " : "");
255     
256      printf(")");
257    }
258  } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) {
259    printf("   %s", 
260    IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) ? "ALARM" : "");
261  }
262 
263  printf("\n");
264}
265
266#define FAN_FEATURE(x) has_features[x - SENSORS_FEATURE_FAN - 1]
267#define FAN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_FAN - 1]
268static void print_generic_chip_fan(const sensors_chip_name *name, 
269                                   const sensors_feature_data *feature,
270                                   int i, int label_size)
271{
272  char *label;
273  const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN;
274  short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0, };
275  double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0.0, };
276  double val;
277 
278  if (sensors_get_label(*name, feature->number, &label)) {
279    printf("ERROR: Can't get fan label!\n");
280    free(label);
281    return;
282  }
283 
284  if (get_feature_value(name, feature, &val))
285  {
286    printf("ERROR: Can't get %s data!\n", label);
287    free(label);
288    return;
289  }
290 
291  print_label(label, label_size);
292  free(label);
293
294  if (FAN_FEATURE(SENSORS_FEATURE_FAN_FAULT) &&
295      FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_FAULT))
296    printf("   FAULT");
297  else
298    printf("%4.0f RPM", val);
299 
300  sensors_get_available_features(name, feature, i, has_features, feature_vals,
301      size, SENSORS_FEATURE_FAN);
302 
303  if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN) &&
304      FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
305    printf("  (min = %4.0f RPM, div = %1.0f)",
306      FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN),
307      FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
308  else if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN))
309    printf("  (min = %4.0f RPM)", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN));
310  else if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV))
311    printf("  (div = %1.0f)", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV));
312 
313  if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) && 
314      FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) {
315    printf("  ALARM");
316  }       
317 
318  printf("\n");
319}
320
321void print_generic_chip(const sensors_chip_name *name)
322{
323  const sensors_feature_data *feature;
324  int i, label_size;
325 
326  label_size = sensors_get_label_size(name);
327 
328  i = 0;
329  while((feature = sensors_get_all_features(*name, &i))) {
330    if (feature->mapping != SENSORS_NO_MAPPING)
331      continue;
332   
333    switch (feature->type) {
334      case SENSORS_FEATURE_TEMP:
335        print_generic_chip_temp(name, feature, i, label_size); break;
336      case SENSORS_FEATURE_IN:
337        print_generic_chip_in(name, feature, i, label_size); break;
338      case SENSORS_FEATURE_FAN:
339        print_generic_chip_fan(name, feature, i, label_size); break;
340      case SENSORS_FEATURE_VID:
341        print_vid_info(name, feature->number, label_size); break;
342      default: continue;
343    }
344  }
345}
Note: See TracBrowser for help on using the browser.