1 /* $OpenBSD: sensors.c,v 1.11 2007/03/23 14:48:22 ckuethe Exp $ */
2 /* $DragonFly: src/usr.bin/systat/sensors.c,v 1.2 2008/11/10 04:59:45 swildner Exp $ */
5 * Copyright (c) 2007 Deanna Phillips <deanna@openbsd.org>
6 * Copyright (c) 2003 Henning Brauer <henning@openbsd.org>
7 * Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/cdefs.h>
23 #include <sys/param.h>
24 #include <sys/sysctl.h>
25 #include <sys/sensors.h>
36 struct sensordev sensordev
;
39 static char * fmttime(double);
44 return (subwin(stdscr
, LINES
-5-1, 0, 5, 0));
48 closesensors(WINDOW
*w
)
62 mvwaddstr(wnd
, 0, 0, "Sensor");
63 mvwaddstr(wnd
, 0, 34, "Value");
64 mvwaddstr(wnd
, 0, 45, "Status");
65 mvwaddstr(wnd
, 0, 58, "Description");
71 enum sensor_type type
;
73 int mib
[5], dev
, numt
;
77 slen
= sizeof(struct sensor
);
78 sdlen
= sizeof(struct sensordev
);
86 for (dev
= 0; dev
< MAXSENSORDEVICES
; dev
++) {
88 if (sysctl(mib
, 3, &sensordev
, &sdlen
, NULL
, 0) == -1) {
93 for (type
= 0; type
< SENSOR_MAX_TYPES
; type
++) {
95 for (numt
= 0; numt
< sensordev
.maxnumt
[type
]; numt
++) {
97 if (sysctl(mib
, 5, &sensor
, &slen
, NULL
, 0)
103 if (sensor
.flags
& SENSOR_FINVALID
)
112 const char *drvstat
[] = {
114 "empty", "ready", "powerup", "online", "idle", "active",
115 "rebuild", "powerdown", "fail", "pfail"
122 mvwaddstr(wnd
, row
, 0, "No sensors found.");
134 mvwprintw(wnd
, row
, 0, "%s.%s%d", sensordev
.xname
,
135 sensor_type_s
[sensor
.type
], sensor
.numt
);
136 switch (sensor
.type
) {
138 mvwprintw(wnd
, row
, 24, "%10.2f degC",
139 (sensor
.value
- 273150000) / 1000000.0);
142 mvwprintw(wnd
, row
, 24, "%11lld RPM", sensor
.value
);
144 case SENSOR_VOLTS_DC
:
145 mvwprintw(wnd
, row
, 24, "%10.2f V DC",
146 sensor
.value
/ 1000000.0);
149 mvwprintw(wnd
, row
, 24, "%10.2f A", sensor
.value
/ 1000000.0);
151 case SENSOR_INDICATOR
:
152 mvwprintw(wnd
, row
, 24, "%15s", sensor
.value
? "On" : "Off");
155 mvwprintw(wnd
, row
, 24, "%11lld raw", sensor
.value
);
158 mvwprintw(wnd
, row
, 24, "%14.2f%%", sensor
.value
/ 1000.0);
161 mvwprintw(wnd
, row
, 24, "%15.2f lx", sensor
.value
/ 1000000.0);
164 if (0 < sensor
.value
&&
165 (size_t)sensor
.value
< sizeof(drvstat
)/sizeof(drvstat
[0])) {
166 mvwprintw(wnd
, row
, 24, "%15s", drvstat
[sensor
.value
]);
170 case SENSOR_TIMEDELTA
:
171 mvwprintw(wnd
, row
, 24, "%15s", fmttime(sensor
.value
/ 1000000000.0));
173 case SENSOR_WATTHOUR
:
174 mvwprintw(wnd
, row
, 24, "%12.2f Wh", sensor
.value
/ 1000000.0);
177 mvwprintw(wnd
, row
, 24, "%10.2f Ah", sensor
.value
/ 1000000.0);
180 mvwprintw(wnd
, row
, 24, "%10lld", sensor
.value
);
183 if (sensor
.desc
[0] != '\0')
184 mvwprintw(wnd
, row
, 58, "(%s)", sensor
.desc
);
186 switch (sensor
.status
) {
187 case SENSOR_S_UNSPEC
:
189 case SENSOR_S_UNKNOWN
:
190 mvwaddstr(wnd
, row
, 45, "unknown");
193 mvwaddstr(wnd
, row
, 45, "WARNING");
196 mvwaddstr(wnd
, row
, 45, "CRITICAL");
199 mvwaddstr(wnd
, row
, 45, "OK");
205 #define SECS_PER_DAY 86400
206 #define SECS_PER_HOUR 3600
207 #define SECS_PER_MIN 60
216 static char outbuf
[LEN
];
223 if (in
>= SECS_PER_DAY
){
226 } else if (in
>= SECS_PER_HOUR
){
229 } else if (in
>= SECS_PER_MIN
){
232 } else if (in
>= 1 ){
234 /* in *= 1; */ /* no op */
235 } else if (in
== 0 ){ /* direct comparisons to floats are scary */
237 } else if (in
>= 1e-3 ){
240 } else if (in
>= 1e-6 ){
243 } else if (in
>= 1e-9 ){
253 snprintf(outbuf
, LEN
,
254 tiny
? "%s%lf %s" : "%s%.3lf %s",
255 signbit
== -1 ? "-" : "", in
, unit
);