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

Revision 4552, 12.1 KB (checked in by jwrdegoede, 6 years ago)

add AMDSI PECI sensortype support to generic_temp_print()

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