Debian Codegraph @

prog/sensors/chips.c

raw
1 /*
2 chips.c - Part of sensors, a user-space program for hardware monitoring
3 Copyright (C) 1998-2003 Frodo Looijaard <[email protected]> and
4 Mark D. Studebaker <[email protected]>
5 Copyright (C) 2007-2012 Jean Delvare <[email protected]>
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., 51 Franklin Street, Fifth Floor, Boston,
20 MA 02110-1301 USA.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <math.h>
27
28 #include "main.h"
29 #include "chips.h"
30 #include "lib/sensors.h"
31 #include "lib/error.h"
32
33 #define ARRAY_SIZE(arr) (int)(sizeof(arr) / sizeof((arr)[0]))
34
35 static void scale_value(double *value, const char **prefixstr);
36
37 static inline double deg_ctof(double cel)
38 {
39 return cel * (9.0 / 5.0) + 32.0;
40 }
41
42 void print_chip_raw(const sensors_chip_name *name)
43 {
44 int a, b, err;
45 const sensors_feature *feature;
46 const sensors_subfeature *sub;
47 char *label;
48 double val;
49 int is_temp;
50
51 a = 0;
52 while ((feature = sensors_get_features(name, &a))) {
53 if (!(label = sensors_get_label(name, feature))) {
54 fprintf(stderr, "ERROR: Can't get label of feature "
55 "%s!\n", feature->name);
56 continue;
57 }
58 printf("%s:\n", label);
59
60 b = 0;
61 while ((sub = sensors_get_all_subfeatures(name, feature, &b))) {
62 if (sub->flags & SENSORS_MODE_R) {
63 is_temp = sensors_get_subfeature_quantity(sub->type) == SENSORS_QUANTITY_TEMP;
64
65 if ((err = sensors_get_value(name, sub->number,
66 &val)))
67 fprintf(stderr, "ERROR: Can't get "
68 "value of subfeature %s: %s\n",
69 sub->name,
70 sensors_strerror(err));
71 else {
72 if (is_temp && fahrenheit)
73 val = deg_ctof(val);
74 printf(" %s: %.3f\n", sub->name, val);
75 }
76 } else
77 printf("(%s)\n", label);
78 }
79 free(label);
80 }
81 }
82
83 void print_chip_json(const sensors_chip_name *name)
84 {
85 int a, b, cnt, subCnt, err;
86 const sensors_feature *feature;
87 const sensors_subfeature *sub;
88 char *label;
89 double val;
90 int is_temp;
91
92 a = 0;
93 cnt = 0;
94 while ((feature = sensors_get_features(name, &a))) {
95 if (!(label = sensors_get_label(name, feature))) {
96 fprintf(stderr, "ERROR: Can't get label of feature "
97 "%s!\n", feature->name);
98 continue;
99 }
100 if (cnt++)
101 printf(",");
102 subCnt = 0;
103 if (new_json) {
104 printf("\"%s\":{", feature->name);
105 if (strcmp(label, feature->name)) {
106 if (subCnt++)
107 printf(",");
108 printf("\"label\":\"%s\"", label);
109 }
110 } else
111 printf("\"%s\":{", label);
112
113 b = 0;
114 while ((sub = sensors_get_all_subfeatures(name, feature, &b))) {
115 char *fname = sub->name;
116 if (new_json) {
117 if (strlen(feature->name) < strlen(sub->name)) {
118 fname += strlen(feature->name);
119 if (*fname == '_') /* FIXME some sensors use other separator */
120 fname++;
121 }
122 }
123 if (sub->flags & SENSORS_MODE_R) {
124 is_temp = sensors_get_subfeature_quantity(sub->type) == SENSORS_QUANTITY_TEMP;
125
126 if ((err = sensors_get_value(name, sub->number,
127 &val))) {
128 fprintf(stderr, "ERROR: Can't get "
129 "value of subfeature %s: %s\n",
130 sub->name,
131 sensors_strerror(err));
132 } else {
133 if (subCnt++)
134 printf(",");
135 if (is_temp && fahrenheit)
136 val = deg_ctof(val);
137 if (new_json) {
138 const char *unit = sensors_get_quantity_unit(sensors_get_subfeature_quantity(sub->type));
139 const char *quantity = sensors_get_quantity_name(sensors_get_subfeature_quantity(sub->type));
140 int quaCnt = 0;
141
142 printf("\"%s\":{", fname);
143
144 if (is_temp && fahrenheit)
145 unit = "°F";
146 if (strrchr(quantity, ' '))
147 quantity = strrchr(quantity, ' ') + 1;
148
149 if (sub->type == SENSORS_SUBFEATURE_TEMP_TYPE) {
150 if (quaCnt++)
151 printf(",");
152 printf("\"label\":\"%s\"", sensors_temp_type_name(val));
153 }
154
155 if (strlen(quantity)) {
156 if (quaCnt++)
157 printf(",");
158 printf("\"quantity\":\"%s\"", quantity);
159 }
160
161 if (strlen(unit)) {
162 if (quaCnt++)
163 printf(",");
164 printf("\"unit\":\"%s\"", unit);
165 }
166
167 if (quaCnt++)
168 printf(",");
169 printf("\"value\":%.16g", val);
170 printf("}");
171
172 } else {
173 printf("\"%s\":%f", fname, val);
174 }
175 }
176
177 } else {
178 if (subCnt++)
179 printf(",");
180 printf("\"%s\":NaN", fname);
181 }
182 }
183 free(label);
184 printf("}");
185 }
186 }
187
188 static const char hyst_str[] = "hyst";
189
190 static void print_label(const char *label, int space)
191 {
192 int len = strlen(label)+1;
193 printf("%s:%*s", label, space - len, "");
194 }
195
196 static double get_value(const sensors_chip_name *name,
197 const sensors_subfeature *sub)
198 {
199 double val;
200 int err;
201
202 err = sensors_get_value(name, sub->number, &val);
203 if (err) {
204 fprintf(stderr, "ERROR: Can't get value of subfeature %s: %s\n",
205 sub->name, sensors_strerror(err));
206 val = 0;
207 }
208 return val;
209 }
210
211 /* A variant for input values, where we want to handle errors gracefully */
212 static int get_input_value(const sensors_chip_name *name,
213 const sensors_subfeature *sub,
214 double *val)
215 {
216 int err;
217
218 err = sensors_get_value(name, sub->number, val);
219 if (err && err != -SENSORS_ERR_ACCESS_R) {
220 fprintf(stderr, "ERROR: Can't get value of subfeature %s: %s\n",
221 sub->name, sensors_strerror(err));
222 }
223 return err;
224 }
225
226 static int get_label_size(const sensors_chip_name *name)
227 {
228 int i;
229 const sensors_feature *iter;
230 char *label;
231 unsigned int max_size = 11; /* 11 as minimum label width */
232
233 i = 0;
234 while ((iter = sensors_get_features(name, &i))) {
235 if ((label = sensors_get_label(name, iter)) &&
236 strlen(label) > max_size)
237 max_size = strlen(label);
238 free(label);
239 }
240
241 /* One more for the colon, and one more to guarantee at least one
242 space between that colon and the value */
243 return max_size + 2;
244 }
245
246 static void print_alarms(struct sensor_subfeature_data *alarms, int alarm_count,
247 int leading_spaces)
248 {
249 int i, printed;
250
251 printf("%*s", leading_spaces + 7, "ALARM");
252 if (alarm_count > 1 || alarms[0].name) {
253 printf(" (");
254 for (i = printed = 0; i < alarm_count; i++) {
255 if (alarms[i].name) {
256 if (printed)
257 printf(", ");
258 printf("%s", alarms[i].name);
259 printed = 1;
260 }
261 }
262 printf(")");
263 }
264 }
265
266 static void print_limits(struct sensor_subfeature_data *limits,
267 int limit_count,
268 struct sensor_subfeature_data *alarms,
269 int alarm_count, int label_size,
270 const char *fmt)
271 {
272 int i, slot, skip;
273 int alarms_printed = 0;
274
275 /*
276 * We print limits on two columns, filling lines first, except for
277 * hysteresis which must always go on the right column, with the
278 * limit it relates to being in the left column on the same line.
279 */
280 for (i = slot = 0; i < limit_count; i++, slot++) {
281 if (!(slot & 1)) {
282 if (slot)
283 printf("\n%*s", label_size + 10, "");
284 printf("(");
285 } else {
286 printf(", ");
287 }
288 printf(fmt, limits[i].name, limits[i].value,
289 limits[i].unit);
290
291 /* If needed, skip one slot to avoid hyst on first column */
292 skip = i + 2 < limit_count && limits[i + 2].name == hyst_str &&
293 !(slot & 1);
294
295 if (((slot + skip) & 1) || i == limit_count - 1) {
296 printf(")");
297 if (alarm_count && !alarms_printed) {
298 print_alarms(alarms, alarm_count,
299 (slot & 1) ? 0 : 16);
300 alarms_printed = 1;
301 }
302 }
303 slot += skip;
304 }
305 if (alarm_count && !alarms_printed)
306 print_alarms(alarms, alarm_count, 32);
307 }
308
309 /*
310 * Get sensor limit information.
311 * *num_limits and *num_alarms must be initialized by the caller.
312 */
313 static void get_sensor_limit_data(const sensors_chip_name *name,
314 const sensors_feature *feature,
315 const struct sensor_subfeature_list *sfl,
316 struct sensor_subfeature_data *limits,
317 int *num_limits,
318 struct sensor_subfeature_data *alarms,
319 int *num_alarms)
320 {
321 const sensors_subfeature *sf;
322
323 for (; sfl->subfeature >= 0; sfl++) {
324 sf = sensors_get_subfeature(name, feature, sfl->subfeature);
325 if (sf) {
326 if (sfl->alarm) {
327 /*
328 * Only queue alarm subfeatures if the alarm
329 * is active, and don't store the alarm value
330 * (it is implied to be active if queued).
331 */
332 if (get_value(name, sf)) {
333 alarms[*num_alarms].name = sfl->name;
334 (*num_alarms)++;
335 }
336 } else {
337 /*
338 * Always queue limit subfeatures with their value.
339 */
340 limits[*num_limits].value = get_value(name, sf);
341 limits[*num_limits].name = sfl->name;
342 (*num_limits)++;
343 }
344 if (sfl->exists) {
345 get_sensor_limit_data(name, feature, sfl->exists,
346 limits, num_limits,
347 alarms, num_alarms);
348 }
349 }
350 }
351 }
352
353 static const struct sensor_subfeature_list temp_min_sensors[] = {
354 { SENSORS_SUBFEATURE_TEMP_MIN_HYST, NULL, 0, hyst_str },
355 { -1, NULL, 0, NULL }
356 };
357
358 static const struct sensor_subfeature_list temp_lcrit_sensors[] = {
359 { SENSORS_SUBFEATURE_TEMP_LCRIT_HYST, NULL, 0, hyst_str },
360 { -1, NULL, 0, NULL }
361 };
362
363 static const struct sensor_subfeature_list temp_max_sensors[] = {
364 { SENSORS_SUBFEATURE_TEMP_MAX_HYST, NULL, 0, hyst_str },
365 { -1, NULL, 0, NULL }
366 };
367
368 static const struct sensor_subfeature_list temp_crit_sensors[] = {
369 { SENSORS_SUBFEATURE_TEMP_CRIT_HYST, NULL, 0, hyst_str },
370 { -1, NULL, 0, NULL }
371 };
372
373 static const struct sensor_subfeature_list temp_emergency_sensors[] = {
374 { SENSORS_SUBFEATURE_TEMP_EMERGENCY_HYST, NULL, 0,
375 hyst_str },
376 { -1, NULL, 0, NULL }
377 };
378
379 static const struct sensor_subfeature_list temp_sensors[] = {
380 { SENSORS_SUBFEATURE_TEMP_ALARM, NULL, 1, NULL },
381 { SENSORS_SUBFEATURE_TEMP_LCRIT_ALARM, NULL, 1, "LCRIT" },
382 { SENSORS_SUBFEATURE_TEMP_MIN_ALARM, NULL, 1, "LOW" },
383 { SENSORS_SUBFEATURE_TEMP_MAX_ALARM, NULL, 1, "HIGH" },
384 { SENSORS_SUBFEATURE_TEMP_CRIT_ALARM, NULL, 1, "CRIT" },
385 { SENSORS_SUBFEATURE_TEMP_EMERGENCY_ALARM, NULL, 1, "EMERGENCY" },
386 { SENSORS_SUBFEATURE_TEMP_MIN, temp_min_sensors, 0, "low" },
387 { SENSORS_SUBFEATURE_TEMP_MAX, temp_max_sensors, 0, "high" },
388 { SENSORS_SUBFEATURE_TEMP_LCRIT, temp_lcrit_sensors, 0, "crit low" },
389 { SENSORS_SUBFEATURE_TEMP_CRIT, temp_crit_sensors, 0, "crit" },
390 { SENSORS_SUBFEATURE_TEMP_EMERGENCY, temp_emergency_sensors, 0,
391 "emerg" },
392 { SENSORS_SUBFEATURE_TEMP_LOWEST, NULL, 0, "lowest" },
393 { SENSORS_SUBFEATURE_TEMP_HIGHEST, NULL, 0, "highest" },
394 { -1, NULL, 0, NULL }
395 };
396
397 #define NUM_TEMP_ALARMS 6
398 #define NUM_TEMP_SENSORS (ARRAY_SIZE(temp_sensors) \
399 + ARRAY_SIZE(temp_max_sensors) \
400 + ARRAY_SIZE(temp_crit_sensors) \
401 + ARRAY_SIZE(temp_emergency_sensors) \
402 - NUM_TEMP_ALARMS - 4)
403
404 static void print_chip_temp(const sensors_chip_name *name,
405 const sensors_feature *feature,
406 int label_size)
407 {
408 struct sensor_subfeature_data sensors[NUM_TEMP_SENSORS];
409 struct sensor_subfeature_data alarms[NUM_TEMP_ALARMS];
410 int sensor_count, alarm_count;
411 const sensors_subfeature *sf;
412 double val;
413 char *label;
414 int i;
415
416 if (!(label = sensors_get_label(name, feature))) {
417 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
418 feature->name);
419 return;
420 }
421 print_label(label, label_size);
422 free(label);
423
424 sf = sensors_get_subfeature(name, feature,
425 SENSORS_SUBFEATURE_TEMP_FAULT);
426 if (sf && get_value(name, sf)) {
427 printf(" FAULT ");
428 } else {
429 sf = sensors_get_subfeature(name, feature,
430 SENSORS_SUBFEATURE_TEMP_INPUT);
431 if (sf && get_input_value(name, sf, &val) == 0) {
432 if (fahrenheit)
433 val = deg_ctof(val);
434 printf("%+6.1f%s ", val, degstr);
435 } else
436 printf(" N/A ");
437 }
438
439 sensor_count = alarm_count = 0;
440 get_sensor_limit_data(name, feature, temp_sensors,
441 sensors, &sensor_count, alarms, &alarm_count);
442
443 for (i = 0; i < sensor_count; i++) {
444 if (fahrenheit)
445 sensors[i].value = deg_ctof(sensors[i].value);
446 sensors[i].unit = degstr;
447 }
448
449 print_limits(sensors, sensor_count, alarms, alarm_count, label_size,
450 "%-4s = %+5.1f%s");
451
452 /* print out temperature sensor info */
453 sf = sensors_get_subfeature(name, feature,
454 SENSORS_SUBFEATURE_TEMP_TYPE);
455 if (sf) {
456 int sens = (int)get_value(name, sf);
457
458 /* older kernels / drivers sometimes report a beta value for
459 thermistors */
460 if (sens > 1000)
461 sens = 4;
462
463 printf(" sensor = %s", sensors_temp_type_name(sens));
464 }
465 printf("\n");
466 }
467
468 static const struct sensor_subfeature_list voltage_sensors[] = {
469 { SENSORS_SUBFEATURE_IN_ALARM, NULL, 1, NULL },
470 { SENSORS_SUBFEATURE_IN_LCRIT_ALARM, NULL, 1, "LCRIT" },
471 { SENSORS_SUBFEATURE_IN_MIN_ALARM, NULL, 1, "MIN" },
472 { SENSORS_SUBFEATURE_IN_MAX_ALARM, NULL, 1, "MAX" },
473 { SENSORS_SUBFEATURE_IN_CRIT_ALARM, NULL, 1, "CRIT" },
474 { SENSORS_SUBFEATURE_IN_LCRIT, NULL, 0, "crit min" },
475 { SENSORS_SUBFEATURE_IN_MIN, NULL, 0, "min" },
476 { SENSORS_SUBFEATURE_IN_MAX, NULL, 0, "max" },
477 { SENSORS_SUBFEATURE_IN_CRIT, NULL, 0, "crit max" },
478 { SENSORS_SUBFEATURE_IN_AVERAGE, NULL, 0, "avg" },
479 { SENSORS_SUBFEATURE_IN_LOWEST, NULL, 0, "lowest" },
480 { SENSORS_SUBFEATURE_IN_HIGHEST, NULL, 0, "highest" },
481 { SENSORS_SUBFEATURE_IN_RATED_MIN, NULL, 0, "rated min" },
482 { SENSORS_SUBFEATURE_IN_RATED_MAX, NULL, 0, "rated max" },
483 { -1, NULL, 0, NULL }
484 };
485
486 #define NUM_IN_ALARMS 5
487 #define NUM_IN_SENSORS (ARRAY_SIZE(voltage_sensors) - NUM_IN_ALARMS - 1)
488
489 static void print_chip_in(const sensors_chip_name *name,
490 const sensors_feature *feature,
491 int label_size)
492 {
493 const sensors_subfeature *sf;
494 char *label;
495 const char *unit;
496 struct sensor_subfeature_data sensors[NUM_IN_SENSORS];
497 struct sensor_subfeature_data alarms[NUM_IN_ALARMS];
498 int sensor_count, alarm_count;
499 double val;
500
501 if (!(label = sensors_get_label(name, feature))) {
502 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
503 feature->name);
504 return;
505 }
506 print_label(label, label_size);
507 free(label);
508
509 sf = sensors_get_subfeature(name, feature,
510 SENSORS_SUBFEATURE_IN_INPUT);
511 if (sf && get_input_value(name, sf, &val) == 0) {
512 scale_value(&val, &unit);
513 printf("%6.2f %sV%*s", val, unit, 2 - (int)strlen(unit), "");
514 } else {
515 printf(" N/A ");
516 }
517
518 sensor_count = alarm_count = 0;
519 get_sensor_limit_data(name, feature, voltage_sensors,
520 sensors, &sensor_count, alarms, &alarm_count);
521
522 print_limits(sensors, sensor_count, alarms, alarm_count, label_size,
523 "%s = %+6.2f V");
524
525 printf("\n");
526 }
527
528 static void print_chip_fan(const sensors_chip_name *name,
529 const sensors_feature *feature,
530 int label_size)
531 {
532 const sensors_subfeature *sf, *sfmin, *sfmax, *sfdiv;
533 double val;
534 char *label;
535
536 if (!(label = sensors_get_label(name, feature))) {
537 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
538 feature->name);
539 return;
540 }
541 print_label(label, label_size);
542 free(label);
543
544 sf = sensors_get_subfeature(name, feature,
545 SENSORS_SUBFEATURE_FAN_FAULT);
546 if (sf && get_value(name, sf))
547 printf(" FAULT");
548 else {
549 sf = sensors_get_subfeature(name, feature,
550 SENSORS_SUBFEATURE_FAN_INPUT);
551 if (sf && get_input_value(name, sf, &val) == 0)
552 printf("%4.0f RPM", val);
553 else
554 printf(" N/A");
555 }
556
557 sfmin = sensors_get_subfeature(name, feature,
558 SENSORS_SUBFEATURE_FAN_MIN);
559 sfmax = sensors_get_subfeature(name, feature,
560 SENSORS_SUBFEATURE_FAN_MAX);
561 sfdiv = sensors_get_subfeature(name, feature,
562 SENSORS_SUBFEATURE_FAN_DIV);
563 if (sfmin || sfmax || sfdiv) {
564 printf(" (");
565 if (sfmin)
566 printf("min = %4.0f RPM",
567 get_value(name, sfmin));
568 if (sfmax)
569 printf("%smax = %4.0f RPM",
570 sfmin ? ", " : "",
571 get_value(name, sfmax));
572 if (sfdiv)
573 printf("%sdiv = %1.0f",
574 (sfmin || sfmax) ? ", " : "",
575 get_value(name, sfdiv));
576 printf(")");
577 }
578
579 sf = sensors_get_subfeature(name, feature,
580 SENSORS_SUBFEATURE_FAN_ALARM);
581 sfmin = sensors_get_subfeature(name, feature,
582 SENSORS_SUBFEATURE_FAN_MIN_ALARM);
583 sfmax = sensors_get_subfeature(name, feature,
584 SENSORS_SUBFEATURE_FAN_MAX_ALARM);
585 if ((sf && get_value(name, sf)) ||
586 (sfmin && get_value(name, sfmin)) ||
587 (sfmax && get_value(name, sfmax)))
588 printf(" ALARM");
589
590 printf("\n");
591 }
592
593 struct scale_table {
594 double upper_bound;
595 const char *unit;
596 };
597
598 static void scale_value(double *value, const char **prefixstr)
599 {
600 double abs_value = fabs(*value);
601 double divisor = 1e-9;
602 static struct scale_table prefix_scales[] = {
603 {1e-6, "n"},
604 {1e-3, "u"},
605 {1, "m"},
606 {1e3, ""},
607 {1e6, "k"},
608 {1e9, "M"},
609 {0, "G"}, /* no upper bound */
610 };
611 struct scale_table *scale = prefix_scales;
612
613 if (abs_value == 0) {
614 *prefixstr = "";
615 return;
616 }
617
618 while (scale->upper_bound && abs_value > scale->upper_bound) {
619 divisor = scale->upper_bound;
620 scale++;
621 }
622
623 *value /= divisor;
624 *prefixstr = scale->unit;
625 }
626
627 static const struct sensor_subfeature_list power_common_sensors[] = {
628 { SENSORS_SUBFEATURE_POWER_ALARM, NULL, 1, NULL },
629 { SENSORS_SUBFEATURE_POWER_MIN_ALARM, NULL, 1, "MIN" },
630 { SENSORS_SUBFEATURE_POWER_MAX_ALARM, NULL, 1, "MAX" },
631 { SENSORS_SUBFEATURE_POWER_LCRIT_ALARM, NULL, 1, "LCRIT" },
632 { SENSORS_SUBFEATURE_POWER_CRIT_ALARM, NULL, 1, "CRIT" },
633 { SENSORS_SUBFEATURE_POWER_CAP_ALARM, NULL, 1, "CAP" },
634 { SENSORS_SUBFEATURE_POWER_MAX, NULL, 0, "max" },
635 { SENSORS_SUBFEATURE_POWER_MIN, NULL, 0, "min" },
636 { SENSORS_SUBFEATURE_POWER_LCRIT, NULL, 0, "lcrit" },
637 { SENSORS_SUBFEATURE_POWER_CRIT, NULL, 0, "crit" },
638 { SENSORS_SUBFEATURE_POWER_CAP, NULL, 0, "cap" },
639 { SENSORS_SUBFEATURE_POWER_RATED_MIN, NULL, 0, "rated min" },
640 { SENSORS_SUBFEATURE_POWER_RATED_MAX, NULL, 0, "rated max" },
641 { -1, NULL, 0, NULL }
642 };
643
644 static const struct sensor_subfeature_list power_inst_sensors[] = {
645 { SENSORS_SUBFEATURE_POWER_INPUT_LOWEST, NULL, 0, "lowest" },
646 { SENSORS_SUBFEATURE_POWER_INPUT_HIGHEST, NULL, 0, "highest" },
647 { SENSORS_SUBFEATURE_POWER_AVERAGE, NULL, 0, "avg" },
648 { SENSORS_SUBFEATURE_POWER_AVERAGE_LOWEST, NULL, 0, "avg lowest" },
649 { SENSORS_SUBFEATURE_POWER_AVERAGE_HIGHEST, NULL, 0, "avg highest" },
650 { SENSORS_SUBFEATURE_POWER_AVERAGE_INTERVAL, NULL, 0,
651 "interval" },
652 { -1, NULL, 0, NULL }
653 };
654
655 static const struct sensor_subfeature_list power_avg_sensors[] = {
656 { SENSORS_SUBFEATURE_POWER_AVERAGE_LOWEST, NULL, 0, "lowest" },
657 { SENSORS_SUBFEATURE_POWER_AVERAGE_HIGHEST, NULL, 0, "highest" },
658 { SENSORS_SUBFEATURE_POWER_AVERAGE_INTERVAL, NULL, 0,
659 "interval" },
660 { -1, NULL, 0, NULL }
661 };
662
663 #define NUM_POWER_ALARMS 6
664 #define NUM_POWER_SENSORS (ARRAY_SIZE(power_common_sensors) \
665 + ARRAY_SIZE(power_inst_sensors) \
666 - NUM_POWER_ALARMS - 2)
667
668 static void print_chip_power(const sensors_chip_name *name,
669 const sensors_feature *feature,
670 int label_size)
671 {
672 double val;
673 const sensors_subfeature *sf;
674 struct sensor_subfeature_data sensors[NUM_POWER_SENSORS];
675 struct sensor_subfeature_data alarms[NUM_POWER_ALARMS];
676 int sensor_count, alarm_count;
677 char *label;
678 const char *unit;
679 int i;
680
681 if (!(label = sensors_get_label(name, feature))) {
682 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
683 feature->name);
684 return;
685 }
686 print_label(label, label_size);
687 free(label);
688
689 sensor_count = alarm_count = 0;
690
691 /*
692 * Power sensors come in 2 flavors: instantaneous and averaged.
693 * Most devices only support one flavor, so we try to display the
694 * average power if the instantaneous power attribute does not exist.
695 * If both instantaneous power and average power are supported,
696 * average power is displayed as limit.
697 */
698 sf = sensors_get_subfeature(name, feature,
699 SENSORS_SUBFEATURE_POWER_INPUT);
700 get_sensor_limit_data(name, feature,
701 sf ? power_inst_sensors : power_avg_sensors,
702 sensors, &sensor_count, alarms, &alarm_count);
703 /* Add sensors common to both flavors. */
704 get_sensor_limit_data(name, feature, power_common_sensors,
705 sensors, &sensor_count, alarms, &alarm_count);
706 if (!sf)
707 sf = sensors_get_subfeature(name, feature,
708 SENSORS_SUBFEATURE_POWER_AVERAGE);
709
710 if (sf && get_input_value(name, sf, &val) == 0) {
711 scale_value(&val, &unit);
712 printf("%6.2f %sW%*s", val, unit, 2 - (int)strlen(unit), "");
713 } else {
714 printf(" N/A ");
715 }
716
717 for (i = 0; i < sensor_count; i++) {
718 /*
719 * Unit is W and needs to be scaled for all attributes except
720 * interval, which does not need to be scaled and is reported in
721 * seconds.
722 */
723 if (strcmp(sensors[i].name, "interval")) {
724 char *tmpstr;
725
726 tmpstr = alloca(4);
727 scale_value(&sensors[i].value, &unit);
728 snprintf(tmpstr, 4, "%sW", unit);
729 sensors[i].unit = tmpstr;
730 } else {
731 sensors[i].unit = "s";
732 }
733 }
734 print_limits(sensors, sensor_count, alarms, alarm_count,
735 label_size, "%s = %6.2f %s");
736
737 printf("\n");
738 }
739
740 static void print_chip_energy(const sensors_chip_name *name,
741 const sensors_feature *feature,
742 int label_size)
743 {
744 double val;
745 const sensors_subfeature *sf;
746 char *label;
747 const char *unit;
748
749 if (!(label = sensors_get_label(name, feature))) {
750 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
751 feature->name);
752 return;
753 }
754 print_label(label, label_size);
755 free(label);
756
757 sf = sensors_get_subfeature(name, feature,
758 SENSORS_SUBFEATURE_ENERGY_INPUT);
759 if (sf && get_input_value(name, sf, &val) == 0) {
760 scale_value(&val, &unit);
761 printf("%6.2f %sJ", val, unit);
762 } else {
763 printf(" N/A");
764 }
765
766 printf("\n");
767 }
768
769 static void print_chip_vid(const sensors_chip_name *name,
770 const sensors_feature *feature,
771 int label_size)
772 {
773 char *label;
774 const sensors_subfeature *subfeature;
775 double vid;
776
777 subfeature = sensors_get_subfeature(name, feature,
778 SENSORS_SUBFEATURE_VID);
779 if (!subfeature)
780 return;
781
782 if ((label = sensors_get_label(name, feature))
783 && !sensors_get_value(name, subfeature->number, &vid)) {
784 print_label(label, label_size);
785 printf("%+6.3f V\n", vid);
786 }
787 free(label);
788 }
789
790 static void print_chip_humidity(const sensors_chip_name *name,
791 const sensors_feature *feature,
792 int label_size)
793 {
794 char *label;
795 const sensors_subfeature *subfeature;
796 double humidity;
797
798 subfeature = sensors_get_subfeature(name, feature,
799 SENSORS_SUBFEATURE_HUMIDITY_INPUT);
800 if (!subfeature)
801 return;
802
803 if ((label = sensors_get_label(name, feature))
804 && !sensors_get_value(name, subfeature->number, &humidity)) {
805 print_label(label, label_size);
806 printf("%6.1f %%RH\n", humidity);
807 }
808 free(label);
809 }
810
811 static void print_chip_pwm(const sensors_chip_name *name,
812 const sensors_feature *feature,
813 int label_size)
814 {
815 const sensors_subfeature *sfio, *sffreq, *sfenable, *sfmode;
816 char *label;
817 double val;
818
819 if (!(label = sensors_get_label(name, feature))) {
820 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
821 feature->name);
822 return;
823 }
824 print_label(label, label_size);
825 free(label);
826
827 sfio = sensors_get_subfeature(name, feature,
828 SENSORS_SUBFEATURE_PWM_IO);
829 if (sfio && !get_input_value(name, sfio, &val))
830 printf(" %3.0f%%", val);
831 else
832 printf(" N/A");
833
834 sffreq = sensors_get_subfeature(name, feature,
835 SENSORS_SUBFEATURE_PWM_FREQ);
836 sfmode = sensors_get_subfeature(name, feature,
837 SENSORS_SUBFEATURE_PWM_MODE);
838 if (sffreq || sfmode) {
839 printf(" (");
840 if (sffreq)
841 printf("freq = %.0f Hz", get_value(name, sffreq));
842
843 if (sfmode) {
844 if (!get_input_value(name, sfmode, &val))
845 printf("%smode = %s", sffreq ? ", " : "",
846 (int) val ? "pwm" : "dc");
847 else
848 printf("%smode = N/A", sffreq ? ", " : "");
849 }
850
851 printf(")");
852 }
853
854 sfenable = sensors_get_subfeature(name, feature,
855 SENSORS_SUBFEATURE_PWM_ENABLE);
856
857 if (sfenable && !get_input_value(name, sfenable, &val) &&
858 (int) val == 1)
859 printf(" MANUAL CONTROL");
860
861 printf("\n");
862 }
863
864 static void print_chip_beep_enable(const sensors_chip_name *name,
865 const sensors_feature *feature,
866 int label_size)
867 {
868 char *label;
869 const sensors_subfeature *subfeature;
870 double beep_enable;
871
872 subfeature = sensors_get_subfeature(name, feature,
873 SENSORS_SUBFEATURE_BEEP_ENABLE);
874 if (!subfeature)
875 return;
876
877 if ((label = sensors_get_label(name, feature))
878 && !sensors_get_value(name, subfeature->number, &beep_enable)) {
879 print_label(label, label_size);
880 printf("%s\n", beep_enable ? "enabled" : "disabled");
881 }
882 free(label);
883 }
884
885 static const struct sensor_subfeature_list current_sensors[] = {
886 { SENSORS_SUBFEATURE_CURR_ALARM, NULL, 1, NULL },
887 { SENSORS_SUBFEATURE_CURR_LCRIT_ALARM, NULL, 1, "LCRIT" },
888 { SENSORS_SUBFEATURE_CURR_MIN_ALARM, NULL, 1, "MIN" },
889 { SENSORS_SUBFEATURE_CURR_MAX_ALARM, NULL, 1, "MAX" },
890 { SENSORS_SUBFEATURE_CURR_CRIT_ALARM, NULL, 1, "CRIT" },
891 { SENSORS_SUBFEATURE_CURR_LCRIT, NULL, 0, "crit min" },
892 { SENSORS_SUBFEATURE_CURR_MIN, NULL, 0, "min" },
893 { SENSORS_SUBFEATURE_CURR_MAX, NULL, 0, "max" },
894 { SENSORS_SUBFEATURE_CURR_CRIT, NULL, 0, "crit max" },
895 { SENSORS_SUBFEATURE_CURR_AVERAGE, NULL, 0, "avg" },
896 { SENSORS_SUBFEATURE_CURR_LOWEST, NULL, 0, "lowest" },
897 { SENSORS_SUBFEATURE_CURR_HIGHEST, NULL, 0, "highest" },
898 { SENSORS_SUBFEATURE_CURR_RATED_MIN, NULL, 0, "rated min" },
899 { SENSORS_SUBFEATURE_CURR_RATED_MAX, NULL, 0, "rated max" },
900 { -1, NULL, 0, NULL }
901 };
902
903 #define NUM_CURR_ALARMS 5
904 #define NUM_CURR_SENSORS (ARRAY_SIZE(current_sensors) - NUM_CURR_ALARMS - 1)
905
906 static void print_chip_curr(const sensors_chip_name *name,
907 const sensors_feature *feature,
908 int label_size)
909 {
910 const sensors_subfeature *sf;
911 double val;
912 char *label;
913 const char *unit;
914 struct sensor_subfeature_data sensors[NUM_CURR_SENSORS];
915 struct sensor_subfeature_data alarms[NUM_CURR_ALARMS];
916 int sensor_count, alarm_count;
917
918 if (!(label = sensors_get_label(name, feature))) {
919 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
920 feature->name);
921 return;
922 }
923 print_label(label, label_size);
924 free(label);
925
926 sf = sensors_get_subfeature(name, feature,
927 SENSORS_SUBFEATURE_CURR_INPUT);
928 if (sf && get_input_value(name, sf, &val) == 0) {
929 scale_value(&val, &unit);
930 printf("%6.2f %sA%*s", val, unit, 2 - (int)strlen(unit), "");
931 } else {
932 printf(" N/A ");
933 }
934
935 sensor_count = alarm_count = 0;
936 get_sensor_limit_data(name, feature, current_sensors,
937 sensors, &sensor_count, alarms, &alarm_count);
938
939 print_limits(sensors, sensor_count, alarms, alarm_count, label_size,
940 "%s = %+6.2f A");
941
942 printf("\n");
943 }
944
945 static void print_chip_freq(const sensors_chip_name *name,
946 const sensors_feature *feature,
947 int label_size)
948 {
949 const sensors_subfeature *sf;
950 double val;
951 char *label;
952 const char *unit;
953
954 if (!(label = sensors_get_label(name, feature))) {
955 fprintf(stderr, "ERROR: Can't get label of feature %s!\n",
956 feature->name);
957 return;
958 }
959 print_label(label, label_size);
960 free(label);
961
962 sf = sensors_get_subfeature(name, feature,
963 SENSORS_SUBFEATURE_FREQ_INPUT);
964 if (sf && get_input_value(name, sf, &val) == 0) {
965 scale_value(&val, &unit);
966 printf("%4.0f %sHz%*s", val, unit, 2 - (int)strlen(unit), "");
967 } else {
968 printf(" N/A ");
969 }
970
971 printf("\n");
972 }
973
974 static void print_chip_intrusion(const sensors_chip_name *name,
975 const sensors_feature *feature,
976 int label_size)
977 {
978 char *label;
979 const sensors_subfeature *subfeature;
980 double alarm;
981
982 subfeature = sensors_get_subfeature(name, feature,
983 SENSORS_SUBFEATURE_INTRUSION_ALARM);
984 if (!subfeature)
985 return;
986
987 if ((label = sensors_get_label(name, feature))
988 && !sensors_get_value(name, subfeature->number, &alarm)) {
989 print_label(label, label_size);
990 printf("%s\n", alarm ? "ALARM" : "OK");
991 }
992 free(label);
993 }
994
995 void print_chip(const sensors_chip_name *name)
996 {
997 const sensors_feature *feature;
998 int i, label_size;
999
1000 label_size = get_label_size(name);
1001
1002 i = 0;
1003 while ((feature = sensors_get_features(name, &i))) {
1004 switch (feature->type) {
1005 case SENSORS_FEATURE_TEMP:
1006 print_chip_temp(name, feature, label_size);
1007 break;
1008 case SENSORS_FEATURE_IN:
1009 print_chip_in(name, feature, label_size);
1010 break;
1011 case SENSORS_FEATURE_FAN:
1012 print_chip_fan(name, feature, label_size);
1013 break;
1014 case SENSORS_FEATURE_VID:
1015 print_chip_vid(name, feature, label_size);
1016 break;
1017 case SENSORS_FEATURE_BEEP_ENABLE:
1018 print_chip_beep_enable(name, feature, label_size);
1019 break;
1020 case SENSORS_FEATURE_POWER:
1021 print_chip_power(name, feature, label_size);
1022 break;
1023 case SENSORS_FEATURE_ENERGY:
1024 print_chip_energy(name, feature, label_size);
1025 break;
1026 case SENSORS_FEATURE_CURR:
1027 print_chip_curr(name, feature, label_size);
1028 break;
1029 case SENSORS_FEATURE_INTRUSION:
1030 print_chip_intrusion(name, feature, label_size);
1031 break;
1032 case SENSORS_FEATURE_HUMIDITY:
1033 print_chip_humidity(name, feature, label_size);
1034 break;
1035 case SENSORS_FEATURE_FREQ:
1036 print_chip_freq(name, feature, label_size);
1037 break;
1038 case SENSORS_FEATURE_PWM:
1039 print_chip_pwm(name, feature, label_size);
1040 break;
1041 default:
1042 continue;
1043 }
1044 }
1045 }