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

Revision 4366, 13.0 KB (checked in by jwrdegoede, 7 years ago)

Generic chip support / get featuretype fixes just received from Bob Schlarmann, as the version I committed wasn't the latest version

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
25#include "chips_generic.h"
26#include "chips.h"
27
28static int get_feature_value(const sensors_chip_name *name, 
29                             const sensors_feature_data *feature, 
30                             double *val)
31{
32  return sensors_get_feature(*name, feature->number, val);
33}
34
35static const sensors_feature_data*
36sensors_get_sub_feature_by_type(const sensors_chip_name *name, 
37                                const sensors_feature_data *feature, 
38                                int i, int j,
39                                enum sensors_feature_type type)
40{
41  const sensors_feature_data *iter;
42 
43  while((iter = sensors_get_all_features(*name, &i, &j)) && 
44      iter->mapping != SENSORS_NO_MAPPING &&
45      iter->mapping == feature->number) {
46    if (sensors_feature_get_type(iter) == type)
47      return iter;
48  }
49 
50  return NULL;
51}
52
53static void sensors_get_available_features(const sensors_chip_name *name, 
54                                           const sensors_feature_data *feature, 
55                                           int i, int j, 
56                                           short *has_features, 
57                                           double *feature_vals, 
58                                           int size, 
59                                           int first_val)
60{
61  const sensors_feature_data *iter;
62 
63  while((iter = sensors_get_all_features(*name, &i, &j)) && 
64      iter->mapping != SENSORS_NO_MAPPING &&
65      iter->mapping == feature->number) {
66    sensors_feature_type type = sensors_feature_get_type(iter);
67    unsigned int index;
68   
69    if (type == SENSORS_FEATURE_UNKNOWN)
70      continue;
71   
72    index = type - first_val - 1;
73    if (index >= size) {
74      printf("ERROR: Bug in sensors: index out of bound");
75      return;
76    }
77   
78    if (get_feature_value(name, iter, &feature_vals[index]))
79      printf("ERROR: Can't get %s data!\n", iter->name);
80   
81    /* some chips don't have all the features they claim to have */
82    /* has_features[index] = (feature_vals[index] != 127); */
83    has_features[index] = 1;
84  }
85}
86
87static int sensors_get_label_size(const sensors_chip_name *name)
88{
89  int i, j, valid;
90  const sensors_feature_data *iter;
91  char *label;
92  int max_size = 11; /* Initialised to 11 as minumum label-width */
93
94  i = j = 0;
95  while((iter = sensors_get_all_features(*name, &i, &j))) {
96    if (!sensors_get_label_and_valid(*name, iter->number, &label, &valid) &&
97        valid && strlen(label) > max_size)
98      max_size = strlen(label);
99    free(label);
100  }
101  return max_size + 1;
102}
103
104#define TEMP_FEATURE(x) has_features[x - SENSORS_FEATURE_TEMP - 1]
105#define TEMP_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_TEMP - 1]
106static void print_generic_chip_temp(const sensors_chip_name *name, 
107                                    const sensors_feature_data *feature,
108                                    int i, int j, int label_size)
109{
110  double val, max, min, lim = 0.0;
111  char *label;
112  int valid, type;
113  const int size = SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP;
114  short has_features[SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP] = {0, 0, 0, 0, 0, 0, 0, 0, 0 };
115  double feature_vals[SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP] = {0.0, };
116 
117  if (sensors_get_label_and_valid(*name, feature->number, &label, &valid)) {
118    free(label);
119    printf("ERROR: Can't get temperature data!\n");
120    return;
121  } else if (!valid) {
122    free(label);
123    return; /* ignored */
124  }
125 
126  if (get_feature_value(name, feature, &val)) {
127    free(label);
128    printf("ERROR: Can't get temperature data!\n");
129    return;
130  }
131 
132  sensors_get_available_features(name, feature, i, j, has_features, 
133      feature_vals, size, SENSORS_FEATURE_TEMP);
134 
135  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX)) {
136    max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
137   
138    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) {
139      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MIN);
140     
141      if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_LIM)) {
142        lim = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_LIM);
143        type = LIM;
144      } else {
145        type = MINMAX;
146      }
147    } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT)) {
148      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
149      type = CRIT;
150    } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_HYST)) {
151      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_HYST);
152      type = HYST;
153    } else {
154      min = 0;
155      type = MAXONLY;
156    }
157  } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_HYST)) {
158    min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_HYST);
159   
160    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_OVER)) {
161      max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_OVER);
162      type = HYST;
163    } else {
164      max = min;
165      max = 0;
166      type = HYSTONLY;
167    }
168  } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_LOW)) {
169    min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_LOW);
170   
171    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_HIGH)) {
172      max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_HIGH);
173    } else { /* TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_OVER) */
174      max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_OVER);
175    }
176   
177    type = MINMAX;
178  } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_OVER)) {
179    max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_OVER);
180    min = 0;
181    type = MAXONLY;
182  } else {
183    min = max = 0;
184    type = SINGLE;
185  }
186 
187  print_label(label, label_size);
188  free(label);
189 
190  if (type == LIM) {
191    print_temp_info_real(val, max, min, lim, type, 1, 1);
192  } else {
193    print_temp_info_real(val, max, min, 0.0, type, 1, 1);
194  }
195 
196  /* print out temperature sensor info */
197  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_SENS)) {
198    int sens = (int)TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_SENS);
199    printf("sensor = %s  ", sens == 0 ? "disabled" :
200                            sens == 1 ? "diode" :
201                            sens == 2 ? "transistor" :
202                            sens == 3 ? "thermal diode" :
203                            sens == 4 ? "thermistor" :
204                            "unkown");
205  }
206 
207  /* ALARM and FAULT features */
208  if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_FAULT) &&
209      TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_FAULT) > 0.5) {
210    printf(" FAULT");
211  } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_ALARM) && 
212      TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_ALARM) > 0.5) {
213    printf(" ALARM");
214  }
215  printf("\n");
216 
217  if (type == MINMAX && TEMP_FEATURE(SENSORS_FEATURE_TEMP_CRIT))
218  {
219    const sensors_feature_data *subfeature;
220    max = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_CRIT);
221   
222    if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_HYST)) {
223      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_HYST);
224      type = HYSTONLY;
225    } else if (TEMP_FEATURE(SENSORS_FEATURE_TEMP_MAX) && 
226        !TEMP_FEATURE(SENSORS_FEATURE_TEMP_MIN)) {
227      min = TEMP_FEATURE_VAL(SENSORS_FEATURE_TEMP_MAX);
228      type = HYSTONLY;
229    } else {
230      type = SINGLE;
231      min = 0.0;
232    }
233   
234    subfeature = sensors_get_sub_feature_by_type(name, feature, i, j, 
235        SENSORS_FEATURE_TEMP_CRIT);
236    if (subfeature) {
237      sensors_get_label_and_valid(*name, subfeature->number, &label, &valid);
238     
239      if (valid) {
240        print_label(label, label_size);
241        free(label);
242        print_temp_info_real(max, min, 0, 0.0, type, 1, 0);
243        printf("\n");
244      }
245    }
246  }       
247}
248
249#define IN_FEATURE(x) has_features[x - SENSORS_FEATURE_IN - 1]
250#define IN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_IN - 1]
251static void print_generic_chip_in(const sensors_chip_name *name, 
252                                  const sensors_feature_data *feature,
253                                  int i, int j, int label_size)
254{
255  const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN;
256  int valid;
257  short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0, 0, 0, 0, 0 };
258  double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0.0, };
259  double val, alarm_max, alarm_min;
260  char *label;
261 
262  if (sensors_get_label_and_valid(*name, feature->number, &label, &valid)) {
263    free(label);
264    printf("ERROR: Can't get in data!\n");
265    return;
266  } else if (!valid) {
267    free(label);
268    return; /* ignored */
269  }
270 
271  if (get_feature_value(name, feature, &val)) {
272    printf("ERROR: Can't get %s data!\n", label);
273    return;
274  }
275 
276  sensors_get_available_features(name, feature, i, j, has_features, feature_vals,
277      size, SENSORS_FEATURE_IN);
278 
279  print_label(label, label_size);
280  free(label);
281  printf("%+6.2f V", val);
282 
283  if (IN_FEATURE(SENSORS_FEATURE_IN_MAX)) {
284    printf("  (min = %+6.2f V, max = %+6.2f V)", 
285    IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN),
286    IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX));
287   
288    if (IN_FEATURE(SENSORS_FEATURE_IN_MAX_ALARM) && 
289        IN_FEATURE(SENSORS_FEATURE_IN_MIN_ALARM)) {
290      alarm_max = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MAX_ALARM);
291      alarm_min = IN_FEATURE_VAL(SENSORS_FEATURE_IN_MIN_ALARM);
292     
293      if (alarm_min || alarm_max) {
294        printf(" ALARM (");
295       
296        if (alarm_min)
297          printf("MIN");
298        if (alarm_max)
299          printf("%sMAX", (alarm_min) ? "," : "");
300       
301        printf(")");
302      }
303    } else if (IN_FEATURE(SENSORS_FEATURE_IN_ALARM)) {
304      printf("   %s", 
305      IN_FEATURE_VAL(SENSORS_FEATURE_IN_ALARM) > 0.5 ? "ALARM" : "");
306    }
307  }
308 
309  printf("\n");
310}
311
312#define FAN_FEATURE(x) has_features[x - SENSORS_FEATURE_FAN - 1]
313#define FAN_FEATURE_VAL(x) feature_vals[x - SENSORS_FEATURE_FAN - 1]
314static void print_generic_chip_fan(const sensors_chip_name *name, 
315                                   const sensors_feature_data *feature,
316                                   int i, int j, int label_size)
317{
318  char *label;
319  int valid;
320  const int size = SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN;
321  short has_features[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0, };
322  double feature_vals[SENSORS_FEATURE_FAN_DIV - SENSORS_FEATURE_FAN] = {0.0, };
323  double val;
324 
325  if (sensors_get_label_and_valid(*name, feature->number, &label, &valid)) {
326    printf("ERROR: Can't get fan data!\n");
327    free(label);
328    return;
329  } else if (!valid) {
330    free(label);
331    return; /* ignored */
332  }
333 
334  if (get_feature_value(name, feature, &val))
335  {
336    printf("ERROR: Can't get %s data!\n", label);
337    free(label);
338    return;
339  }
340 
341  print_label(label, label_size);
342  free(label);
343  printf("%4.0f RPM", val);
344 
345  sensors_get_available_features(name, feature, i, j, has_features, feature_vals,
346      size, SENSORS_FEATURE_FAN);
347 
348  if (FAN_FEATURE(SENSORS_FEATURE_FAN_MIN)) {
349    printf("  (min = %4.0f RPM", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_MIN));
350   
351    if (FAN_FEATURE(SENSORS_FEATURE_FAN_DIV)) {
352      printf(", div = %1.0f", FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_DIV)); 
353    }
354   
355    printf(")");
356  }
357 
358  if (FAN_FEATURE(SENSORS_FEATURE_FAN_ALARM) && 
359      FAN_FEATURE_VAL(SENSORS_FEATURE_FAN_ALARM)) {
360    printf(" ALARM");
361  }       
362 
363  printf("\n");
364}
365
366static void print_generic_chip_vid(const sensors_chip_name *name, 
367                                   const sensors_feature_data *feature,
368                                   int i, int j, int label_size)
369{
370  const sensors_feature_data *vrm_feature;
371 
372  while((vrm_feature = sensors_get_all_features(*name, &i, &j))) {
373    if (sensors_feature_get_type(vrm_feature) == SENSORS_FEATURE_VRM)
374      break;
375  }
376 
377  if (vrm_feature != NULL)
378    print_vid_info_real(name, feature->number, vrm_feature->number, label_size);
379  else
380    print_vid_info_real(name, feature->number, -1, label_size);
381}
382
383void print_generic_chip(const sensors_chip_name *name)
384{
385  const sensors_feature_data *feature;
386  int i,j, label_size;
387 
388  label_size = sensors_get_label_size(name);
389 
390  i = j = 0;
391  while((feature = sensors_get_all_features(*name, &i, &j))) {
392    if (feature->mapping != SENSORS_NO_MAPPING)
393      continue;
394   
395    switch(sensors_feature_get_type(feature)) {
396      case SENSORS_FEATURE_TEMP:
397        print_generic_chip_temp(name, feature, i, j, label_size); break;
398      case SENSORS_FEATURE_IN:
399        print_generic_chip_in(name, feature, i, j, label_size); break;
400      case SENSORS_FEATURE_FAN:
401        print_generic_chip_fan(name, feature, i, j, label_size); break;
402      case SENSORS_FEATURE_VID:
403        print_generic_chip_vid(name, feature, i, j, label_size); break;
404      default: continue;
405    }
406  }
407}
Note: See TracBrowser for help on using the browser.