envstat.c revision 1.42.2.1 1 1.42.2.1 matt /* $NetBSD: envstat.c,v 1.42.2.1 2007/11/06 23:36:25 matt Exp $ */
2 1.1 groo
3 1.1 groo /*-
4 1.42.2.1 matt * Copyright (c) 2007 Juan Romero Pardines.
5 1.1 groo * All rights reserved.
6 1.1 groo *
7 1.1 groo * Redistribution and use in source and binary forms, with or without
8 1.1 groo * modification, are permitted provided that the following conditions
9 1.1 groo * are met:
10 1.1 groo * 1. Redistributions of source code must retain the above copyright
11 1.1 groo * notice, this list of conditions and the following disclaimer.
12 1.1 groo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 groo * notice, this list of conditions and the following disclaimer in the
14 1.1 groo * documentation and/or other materials provided with the distribution.
15 1.1 groo *
16 1.42.2.1 matt * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.42.2.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.42.2.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.42.2.1 matt * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.42.2.1 matt * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.42.2.1 matt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.42.2.1 matt * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.42.2.1 matt * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.42.2.1 matt * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.42.2.1 matt * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 groo */
27 1.1 groo
28 1.25 xtraeme /*
29 1.42.2.1 matt * TODO
30 1.25 xtraeme *
31 1.42.2.1 matt * o Some checks should be added to ensure that the user does not
32 1.42.2.1 matt * set unwanted values for the critical limits.
33 1.25 xtraeme */
34 1.1 groo
35 1.42.2.1 matt #include <sys/cdefs.h>
36 1.42.2.1 matt #ifndef lint
37 1.42.2.1 matt __RCSID("$NetBSD: envstat.c,v 1.42.2.1 2007/11/06 23:36:25 matt Exp $");
38 1.42.2.1 matt #endif /* not lint */
39 1.42.2.1 matt
40 1.1 groo #include <stdio.h>
41 1.1 groo #include <stdlib.h>
42 1.25 xtraeme #include <stdbool.h>
43 1.1 groo #include <string.h>
44 1.1 groo #include <unistd.h>
45 1.25 xtraeme #include <fcntl.h>
46 1.3 thorpej #include <err.h>
47 1.25 xtraeme #include <errno.h>
48 1.42.2.1 matt #include <syslog.h>
49 1.25 xtraeme #include <prop/proplib.h>
50 1.1 groo #include <sys/envsys.h>
51 1.1 groo
52 1.42.2.1 matt #include "envstat.h"
53 1.42.2.1 matt
54 1.25 xtraeme #define _PATH_DEV_SYSMON "/dev/sysmon"
55 1.1 groo
56 1.25 xtraeme #define ENVSYS_DFLAG 0x00000001 /* list registered devices */
57 1.25 xtraeme #define ENVSYS_FFLAG 0x00000002 /* show temp in farenheit */
58 1.25 xtraeme #define ENVSYS_LFLAG 0x00000004 /* list sensors */
59 1.25 xtraeme #define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */
60 1.42.2.1 matt #define ENVSYS_IFLAG 0x00000010 /* skips invalid sensors */
61 1.42.2.1 matt #define ENVSYS_SFLAG 0x00000020 /* removes all properties set */
62 1.25 xtraeme
63 1.25 xtraeme struct envsys_sensor {
64 1.26 xtraeme bool invalid;
65 1.25 xtraeme bool visible;
66 1.25 xtraeme bool percentage;
67 1.25 xtraeme int32_t cur_value;
68 1.25 xtraeme int32_t max_value;
69 1.25 xtraeme int32_t min_value;
70 1.25 xtraeme int32_t avg_value;
71 1.25 xtraeme int32_t critcap_value;
72 1.25 xtraeme int32_t critmin_value;
73 1.25 xtraeme int32_t critmax_value;
74 1.25 xtraeme char desc[ENVSYS_DESCLEN];
75 1.25 xtraeme char type[ENVSYS_DESCLEN];
76 1.25 xtraeme char drvstate[ENVSYS_DESCLEN];
77 1.42.2.1 matt char battcap[ENVSYS_DESCLEN];
78 1.42.2.1 matt char dvname[ENVSYS_DESCLEN];
79 1.25 xtraeme };
80 1.25 xtraeme
81 1.42.2.1 matt static unsigned int interval, flags, width;
82 1.42.2.1 matt static char *mydevname, *sensors;
83 1.25 xtraeme static struct envsys_sensor *gesen;
84 1.31 xtraeme static size_t gnelems, newsize;
85 1.25 xtraeme
86 1.25 xtraeme static int parse_dictionary(int);
87 1.42.2.1 matt static int send_dictionary(FILE *, int);
88 1.42.2.1 matt static int find_sensors(prop_array_t, const char *);
89 1.42.2.1 matt static void print_sensors(struct envsys_sensor *, size_t, const char *);
90 1.25 xtraeme static int check_sensors(struct envsys_sensor *, char *, size_t);
91 1.25 xtraeme static int usage(void);
92 1.25 xtraeme
93 1.25 xtraeme
94 1.25 xtraeme int main(int argc, char **argv)
95 1.25 xtraeme {
96 1.25 xtraeme prop_dictionary_t dict;
97 1.25 xtraeme int c, fd, rval;
98 1.42.2.1 matt char *endptr, *configfile = NULL;
99 1.42.2.1 matt FILE *cf;
100 1.25 xtraeme
101 1.25 xtraeme rval = flags = interval = width = 0;
102 1.31 xtraeme newsize = gnelems = 0;
103 1.31 xtraeme gesen = NULL;
104 1.25 xtraeme
105 1.25 xtraeme setprogname(argv[0]);
106 1.25 xtraeme
107 1.42.2.1 matt while ((c = getopt(argc, argv, "c:Dd:fIi:lrSs:w:x")) != -1) {
108 1.25 xtraeme switch (c) {
109 1.42.2.1 matt case 'c': /* configuration file */
110 1.42.2.1 matt configfile = strdup(optarg);
111 1.42.2.1 matt if (configfile == NULL)
112 1.42.2.1 matt err(EXIT_FAILURE, "strdup");
113 1.25 xtraeme break;
114 1.25 xtraeme case 'D': /* list registered devices */
115 1.25 xtraeme flags |= ENVSYS_DFLAG;
116 1.25 xtraeme break;
117 1.42.2.1 matt case 'd': /* show sensors of a specific device */
118 1.42.2.1 matt mydevname = strdup(optarg);
119 1.42.2.1 matt if (mydevname == NULL)
120 1.42.2.1 matt err(EXIT_FAILURE, "strdup");
121 1.42.2.1 matt break;
122 1.25 xtraeme case 'f': /* display temperature in Farenheit */
123 1.25 xtraeme flags |= ENVSYS_FFLAG;
124 1.25 xtraeme break;
125 1.42.2.1 matt case 'I': /* Skips invalid sensors */
126 1.42.2.1 matt flags |= ENVSYS_IFLAG;
127 1.25 xtraeme break;
128 1.42.2.1 matt case 'i': /* wait time between intervals */
129 1.42.2.1 matt interval = (unsigned int)strtoul(optarg, &endptr, 10);
130 1.25 xtraeme if (*endptr != '\0')
131 1.42.2.1 matt errx(EXIT_FAILURE, "bad interval '%s'", optarg);
132 1.6 explorer break;
133 1.42.2.1 matt case 'l': /* list sensors */
134 1.42.2.1 matt flags |= ENVSYS_LFLAG;
135 1.1 groo break;
136 1.35 xtraeme case 'r':
137 1.35 xtraeme /*
138 1.35 xtraeme * This flag doesn't do anything... it's only here for
139 1.35 xtraeme * compatibility with the old implementation.
140 1.35 xtraeme */
141 1.35 xtraeme break;
142 1.42.2.1 matt case 'S':
143 1.42.2.1 matt flags |= ENVSYS_SFLAG;
144 1.42.2.1 matt break;
145 1.25 xtraeme case 's': /* only show specified sensors */
146 1.25 xtraeme sensors = strdup(optarg);
147 1.1 groo if (sensors == NULL)
148 1.42.2.1 matt err(EXIT_FAILURE, "strdup");
149 1.1 groo break;
150 1.42.2.1 matt case 'w': /* width value for the lines */
151 1.42.2.1 matt width = strtoul(optarg, &endptr, 10);
152 1.42.2.1 matt if (*endptr != '\0')
153 1.42.2.1 matt errx(EXIT_FAILURE, "bad width '%s'", optarg);
154 1.42.2.1 matt break;
155 1.42.2.1 matt case 'x': /* print the dictionary in raw format */
156 1.42.2.1 matt flags |= ENVSYS_XFLAG;
157 1.1 groo break;
158 1.1 groo case '?':
159 1.1 groo default:
160 1.1 groo usage();
161 1.1 groo /* NOTREACHED */
162 1.1 groo }
163 1.1 groo }
164 1.1 groo
165 1.42.2.1 matt argc -= optind;
166 1.42.2.1 matt argv += optind;
167 1.42.2.1 matt
168 1.42.2.1 matt if (argc > 0)
169 1.42.2.1 matt usage();
170 1.42.2.1 matt
171 1.25 xtraeme if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
172 1.42.2.1 matt err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
173 1.1 groo
174 1.42.2.1 matt if (flags & ENVSYS_XFLAG) {
175 1.38 xtraeme rval = prop_dictionary_recv_ioctl(fd,
176 1.42.2.1 matt ENVSYS_GETDICTIONARY,
177 1.42.2.1 matt &dict);
178 1.42.2.1 matt if (rval)
179 1.42.2.1 matt errx(EXIT_FAILURE, "%s", strerror(rval));
180 1.1 groo
181 1.42.2.1 matt config_dict_dump(dict);
182 1.3 thorpej
183 1.42.2.1 matt } else if (flags & ENVSYS_SFLAG) {
184 1.42.2.1 matt (void)close(fd);
185 1.42.2.1 matt
186 1.42.2.1 matt if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1)
187 1.42.2.1 matt err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
188 1.42.2.1 matt
189 1.42.2.1 matt dict = prop_dictionary_create();
190 1.42.2.1 matt if (!dict)
191 1.42.2.1 matt err(EXIT_FAILURE, "prop_dictionary_create");
192 1.42.2.1 matt
193 1.42.2.1 matt rval = prop_dictionary_set_bool(dict,
194 1.42.2.1 matt "envsys-remove-props",
195 1.42.2.1 matt true);
196 1.42.2.1 matt if (!rval)
197 1.42.2.1 matt err(EXIT_FAILURE, "prop_dict_set_bool");
198 1.42.2.1 matt
199 1.42.2.1 matt rval = prop_dictionary_send_ioctl(dict, fd, ENVSYS_REMOVEPROPS);
200 1.42.2.1 matt if (rval)
201 1.42.2.1 matt warnx("%s", strerror(rval));
202 1.42.2.1 matt
203 1.42.2.1 matt } else if (configfile) {
204 1.42.2.1 matt /*
205 1.42.2.1 matt * Parse the configuration file.
206 1.42.2.1 matt */
207 1.42.2.1 matt if ((cf = fopen(configfile, "r")) == NULL) {
208 1.42.2.1 matt syslog(LOG_ERR, "fopen failed: %s", strerror(errno));
209 1.42.2.1 matt errx(EXIT_FAILURE, "%s", strerror(errno));
210 1.42.2.1 matt }
211 1.42.2.1 matt
212 1.42.2.1 matt rval = send_dictionary(cf, fd);
213 1.42.2.1 matt (void)fclose(cf);
214 1.42.2.1 matt
215 1.42.2.1 matt #define MISSING_FLAG() \
216 1.42.2.1 matt do { \
217 1.42.2.1 matt if (sensors && !mydevname) \
218 1.42.2.1 matt errx(EXIT_FAILURE, "-s requires -d"); \
219 1.42.2.1 matt } while (/* CONSTCOND */ 0)
220 1.25 xtraeme
221 1.25 xtraeme } else if (interval) {
222 1.42.2.1 matt MISSING_FLAG();
223 1.25 xtraeme for (;;) {
224 1.25 xtraeme rval = parse_dictionary(fd);
225 1.25 xtraeme if (rval)
226 1.42.2.1 matt break;
227 1.42.2.1 matt
228 1.25 xtraeme (void)fflush(stdout);
229 1.25 xtraeme (void)sleep(interval);
230 1.25 xtraeme }
231 1.42.2.1 matt } else {
232 1.42.2.1 matt MISSING_FLAG();
233 1.42.2.1 matt rval = parse_dictionary(fd);
234 1.42.2.1 matt }
235 1.1 groo
236 1.25 xtraeme if (sensors)
237 1.25 xtraeme free(sensors);
238 1.25 xtraeme if (mydevname)
239 1.25 xtraeme free(mydevname);
240 1.25 xtraeme (void)close(fd);
241 1.42.2.1 matt
242 1.42.2.1 matt return rval ? EXIT_FAILURE : EXIT_SUCCESS;
243 1.25 xtraeme }
244 1.25 xtraeme
245 1.25 xtraeme static int
246 1.42.2.1 matt send_dictionary(FILE *cf, int fd)
247 1.25 xtraeme {
248 1.42.2.1 matt prop_dictionary_t kdict, udict;
249 1.42.2.1 matt int error = 0;
250 1.25 xtraeme
251 1.42.2.1 matt error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &kdict);
252 1.42.2.1 matt if (error)
253 1.37 xtraeme return error;
254 1.1 groo
255 1.42.2.1 matt config_parse(cf, kdict);
256 1.25 xtraeme
257 1.42.2.1 matt /*
258 1.42.2.1 matt * Dictionary built by the parser from the configuration file.
259 1.25 xtraeme */
260 1.42.2.1 matt udict = config_dict_parsed();
261 1.1 groo
262 1.25 xtraeme /*
263 1.42.2.1 matt * Close the read only descriptor and open a new one read write.
264 1.25 xtraeme */
265 1.42.2.1 matt (void)close(fd);
266 1.36 xtraeme if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
267 1.36 xtraeme error = errno;
268 1.42.2.1 matt warn("%s", _PATH_DEV_SYSMON);
269 1.42.2.1 matt return error;
270 1.36 xtraeme }
271 1.36 xtraeme
272 1.42.2.1 matt /*
273 1.42.2.1 matt * Send our dictionary to the kernel then.
274 1.42.2.1 matt */
275 1.25 xtraeme error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
276 1.25 xtraeme if (error)
277 1.42.2.1 matt warnx("%s", strerror(error));
278 1.42.2.1 matt
279 1.25 xtraeme prop_object_release(udict);
280 1.25 xtraeme return error;
281 1.25 xtraeme }
282 1.25 xtraeme
283 1.25 xtraeme static int
284 1.25 xtraeme parse_dictionary(int fd)
285 1.25 xtraeme {
286 1.25 xtraeme prop_array_t array;
287 1.25 xtraeme prop_dictionary_t dict;
288 1.25 xtraeme prop_object_iterator_t iter;
289 1.25 xtraeme prop_object_t obj;
290 1.25 xtraeme const char *dnp = NULL;
291 1.25 xtraeme int rval = 0;
292 1.25 xtraeme
293 1.25 xtraeme /* receive dictionary from kernel */
294 1.37 xtraeme rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
295 1.37 xtraeme if (rval)
296 1.37 xtraeme return rval;
297 1.25 xtraeme
298 1.42 xtraeme if (prop_dictionary_count(dict) == 0) {
299 1.42 xtraeme warnx("no drivers registered");
300 1.42 xtraeme goto out;
301 1.42 xtraeme }
302 1.42 xtraeme
303 1.25 xtraeme if (mydevname) {
304 1.25 xtraeme obj = prop_dictionary_get(dict, mydevname);
305 1.25 xtraeme if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
306 1.25 xtraeme warnx("unknown device `%s'", mydevname);
307 1.25 xtraeme rval = EINVAL;
308 1.25 xtraeme goto out;
309 1.1 groo }
310 1.1 groo
311 1.42.2.1 matt rval = find_sensors(obj, mydevname);
312 1.25 xtraeme if (rval)
313 1.25 xtraeme goto out;
314 1.42.2.1 matt
315 1.42.2.1 matt if ((flags & ENVSYS_LFLAG) == 0)
316 1.42.2.1 matt print_sensors(gesen, gnelems, mydevname);
317 1.42.2.1 matt if (interval)
318 1.42.2.1 matt (void)printf("\n");
319 1.25 xtraeme } else {
320 1.25 xtraeme iter = prop_dictionary_iterator(dict);
321 1.25 xtraeme if (iter == NULL) {
322 1.25 xtraeme rval = EINVAL;
323 1.25 xtraeme goto out;
324 1.25 xtraeme }
325 1.6 explorer
326 1.25 xtraeme /* iterate over the dictionary returned by the kernel */
327 1.25 xtraeme while ((obj = prop_object_iterator_next(iter)) != NULL) {
328 1.1 groo
329 1.25 xtraeme array = prop_dictionary_get_keysym(dict, obj);
330 1.25 xtraeme if (prop_object_type(array) != PROP_TYPE_ARRAY) {
331 1.25 xtraeme warnx("no sensors found");
332 1.25 xtraeme rval = EINVAL;
333 1.25 xtraeme goto out;
334 1.1 groo }
335 1.1 groo
336 1.25 xtraeme dnp = prop_dictionary_keysym_cstring_nocopy(obj);
337 1.25 xtraeme
338 1.25 xtraeme if (flags & ENVSYS_DFLAG) {
339 1.31 xtraeme (void)printf("%s\n", dnp);
340 1.42.2.1 matt continue;
341 1.25 xtraeme } else {
342 1.42.2.1 matt (void)printf("[%s]\n", dnp);
343 1.42.2.1 matt rval = find_sensors(array, dnp);
344 1.25 xtraeme if (rval)
345 1.25 xtraeme goto out;
346 1.1 groo }
347 1.42.2.1 matt
348 1.42.2.1 matt if ((flags & ENVSYS_LFLAG) == 0)
349 1.42.2.1 matt print_sensors(gesen, gnelems, dnp);
350 1.42.2.1 matt if (interval)
351 1.42.2.1 matt (void)printf("\n");
352 1.1 groo }
353 1.31 xtraeme
354 1.25 xtraeme prop_object_iterator_release(iter);
355 1.33 xtraeme }
356 1.31 xtraeme
357 1.25 xtraeme out:
358 1.31 xtraeme if (gesen) {
359 1.31 xtraeme free(gesen);
360 1.31 xtraeme gesen = NULL;
361 1.31 xtraeme gnelems = 0;
362 1.31 xtraeme newsize = 0;
363 1.31 xtraeme }
364 1.25 xtraeme prop_object_release(dict);
365 1.25 xtraeme return rval;
366 1.1 groo }
367 1.1 groo
368 1.25 xtraeme static int
369 1.42.2.1 matt find_sensors(prop_array_t array, const char *dvname)
370 1.25 xtraeme {
371 1.25 xtraeme prop_object_iterator_t iter;
372 1.25 xtraeme prop_object_t obj, obj1;
373 1.25 xtraeme prop_string_t state, desc = NULL;
374 1.31 xtraeme struct envsys_sensor *esen = NULL;
375 1.31 xtraeme int rval = 0;
376 1.25 xtraeme char *str = NULL;
377 1.25 xtraeme
378 1.31 xtraeme newsize += prop_array_count(array) * sizeof(*gesen);
379 1.31 xtraeme esen = realloc(gesen, newsize);
380 1.31 xtraeme if (esen == NULL) {
381 1.31 xtraeme if (gesen)
382 1.31 xtraeme free(gesen);
383 1.31 xtraeme gesen = NULL;
384 1.42.2.1 matt return ENOMEM;
385 1.31 xtraeme }
386 1.31 xtraeme gesen = esen;
387 1.25 xtraeme
388 1.25 xtraeme iter = prop_array_iterator(array);
389 1.42.2.1 matt if (!iter)
390 1.42.2.1 matt return EINVAL;
391 1.25 xtraeme
392 1.25 xtraeme /* iterate over the array of dictionaries */
393 1.25 xtraeme while ((obj = prop_object_iterator_next(iter)) != NULL) {
394 1.42.2.1 matt
395 1.42.2.1 matt /* copy device name */
396 1.42.2.1 matt (void)strlcpy(gesen[gnelems].dvname, dvname,
397 1.42.2.1 matt sizeof(gesen[gnelems].dvname));
398 1.42.2.1 matt
399 1.31 xtraeme gesen[gnelems].visible = false;
400 1.31 xtraeme
401 1.25 xtraeme /* check sensor's state */
402 1.25 xtraeme state = prop_dictionary_get(obj, "state");
403 1.1 groo
404 1.26 xtraeme /* mark invalid sensors */
405 1.25 xtraeme if (prop_string_equals_cstring(state, "invalid"))
406 1.31 xtraeme gesen[gnelems].invalid = true;
407 1.31 xtraeme else
408 1.31 xtraeme gesen[gnelems].invalid = false;
409 1.25 xtraeme
410 1.25 xtraeme /* description string */
411 1.25 xtraeme desc = prop_dictionary_get(obj, "description");
412 1.42.2.1 matt if (desc) {
413 1.37 xtraeme /* copy description */
414 1.37 xtraeme (void)strlcpy(gesen[gnelems].desc,
415 1.37 xtraeme prop_string_cstring_nocopy(desc),
416 1.37 xtraeme sizeof(gesen[gnelems].desc));
417 1.37 xtraeme } else
418 1.37 xtraeme continue;
419 1.25 xtraeme
420 1.25 xtraeme /* type string */
421 1.25 xtraeme obj1 = prop_dictionary_get(obj, "type");
422 1.25 xtraeme /* copy type */
423 1.31 xtraeme (void)strlcpy(gesen[gnelems].type,
424 1.31 xtraeme prop_string_cstring_nocopy(obj1),
425 1.31 xtraeme sizeof(gesen[gnelems].type));
426 1.25 xtraeme
427 1.25 xtraeme /* get current drive state string */
428 1.25 xtraeme obj1 = prop_dictionary_get(obj, "drive-state");
429 1.42.2.1 matt if (obj1)
430 1.31 xtraeme (void)strlcpy(gesen[gnelems].drvstate,
431 1.25 xtraeme prop_string_cstring_nocopy(obj1),
432 1.31 xtraeme sizeof(gesen[gnelems].drvstate));
433 1.25 xtraeme
434 1.42.2.1 matt /* get current battery capacity string */
435 1.42.2.1 matt obj1 = prop_dictionary_get(obj, "battery-capacity");
436 1.42.2.1 matt if (obj1)
437 1.42.2.1 matt (void)strlcpy(gesen[gnelems].battcap,
438 1.42.2.1 matt prop_string_cstring_nocopy(obj1),
439 1.42.2.1 matt sizeof(gesen[gnelems].battcap));
440 1.42.2.1 matt
441 1.25 xtraeme /* get current value */
442 1.25 xtraeme obj1 = prop_dictionary_get(obj, "cur-value");
443 1.31 xtraeme gesen[gnelems].cur_value = prop_number_integer_value(obj1);
444 1.25 xtraeme
445 1.25 xtraeme /* get max value */
446 1.25 xtraeme obj1 = prop_dictionary_get(obj, "max-value");
447 1.42.2.1 matt if (obj1)
448 1.31 xtraeme gesen[gnelems].max_value =
449 1.31 xtraeme prop_number_integer_value(obj1);
450 1.31 xtraeme else
451 1.31 xtraeme gesen[gnelems].max_value = 0;
452 1.25 xtraeme
453 1.25 xtraeme /* get min value */
454 1.25 xtraeme obj1 = prop_dictionary_get(obj, "min-value");
455 1.42.2.1 matt if (obj1)
456 1.31 xtraeme gesen[gnelems].min_value =
457 1.31 xtraeme prop_number_integer_value(obj1);
458 1.31 xtraeme else
459 1.31 xtraeme gesen[gnelems].min_value = 0;
460 1.25 xtraeme
461 1.25 xtraeme /* get avg value */
462 1.25 xtraeme obj1 = prop_dictionary_get(obj, "avg-value");
463 1.42.2.1 matt if (obj1)
464 1.31 xtraeme gesen[gnelems].avg_value =
465 1.31 xtraeme prop_number_integer_value(obj1);
466 1.31 xtraeme else
467 1.31 xtraeme gesen[gnelems].avg_value = 0;
468 1.25 xtraeme
469 1.25 xtraeme /* get percentage flag */
470 1.25 xtraeme obj1 = prop_dictionary_get(obj, "want-percentage");
471 1.42.2.1 matt if (obj1)
472 1.31 xtraeme gesen[gnelems].percentage = prop_bool_true(obj1);
473 1.25 xtraeme
474 1.25 xtraeme /* get critical max value if available */
475 1.42.2.1 matt obj1 = prop_dictionary_get(obj, "critical-max");
476 1.42.2.1 matt if (obj1) {
477 1.31 xtraeme gesen[gnelems].critmax_value =
478 1.25 xtraeme prop_number_integer_value(obj1);
479 1.31 xtraeme } else
480 1.31 xtraeme gesen[gnelems].critmax_value = 0;
481 1.5 cgd
482 1.25 xtraeme /* get critical min value if available */
483 1.42.2.1 matt obj1 = prop_dictionary_get(obj, "critical-min");
484 1.42.2.1 matt if (obj1) {
485 1.31 xtraeme gesen[gnelems].critmin_value =
486 1.25 xtraeme prop_number_integer_value(obj1);
487 1.31 xtraeme } else
488 1.31 xtraeme gesen[gnelems].critmin_value = 0;
489 1.1 groo
490 1.25 xtraeme /* get critical capacity value if available */
491 1.25 xtraeme obj1 = prop_dictionary_get(obj, "critical-capacity");
492 1.42.2.1 matt if (obj1) {
493 1.31 xtraeme gesen[gnelems].critcap_value =
494 1.25 xtraeme prop_number_integer_value(obj1);
495 1.31 xtraeme } else
496 1.31 xtraeme gesen[gnelems].critcap_value = 0;
497 1.1 groo
498 1.31 xtraeme /* pass to the next struct and increase the counter */
499 1.31 xtraeme gnelems++;
500 1.25 xtraeme
501 1.25 xtraeme /* print sensor names if -l was given */
502 1.25 xtraeme if (flags & ENVSYS_LFLAG) {
503 1.25 xtraeme if (width)
504 1.25 xtraeme (void)printf("%*s\n", width,
505 1.25 xtraeme prop_string_cstring_nocopy(desc));
506 1.25 xtraeme else
507 1.25 xtraeme (void)printf("%s\n",
508 1.25 xtraeme prop_string_cstring_nocopy(desc));
509 1.25 xtraeme }
510 1.25 xtraeme }
511 1.1 groo
512 1.25 xtraeme /* free memory */
513 1.25 xtraeme prop_object_iterator_release(iter);
514 1.1 groo
515 1.25 xtraeme /*
516 1.25 xtraeme * if -s was specified, we need a way to mark if a sensor
517 1.25 xtraeme * was found.
518 1.25 xtraeme */
519 1.25 xtraeme if (sensors) {
520 1.25 xtraeme str = strdup(sensors);
521 1.42.2.1 matt if (!str)
522 1.25 xtraeme return ENOMEM;
523 1.6 explorer
524 1.31 xtraeme rval = check_sensors(gesen, str, gnelems);
525 1.42.2.1 matt free(str);
526 1.1 groo }
527 1.1 groo
528 1.25 xtraeme return rval;
529 1.1 groo }
530 1.1 groo
531 1.25 xtraeme static int
532 1.25 xtraeme check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
533 1.1 groo {
534 1.1 groo int i;
535 1.25 xtraeme char *sname;
536 1.25 xtraeme
537 1.25 xtraeme sname = strtok(str, ",");
538 1.42.2.1 matt while (sname) {
539 1.25 xtraeme for (i = 0; i < nelems; i++) {
540 1.25 xtraeme if (strcmp(sname, es[i].desc) == 0) {
541 1.25 xtraeme es[i].visible = true;
542 1.25 xtraeme break;
543 1.25 xtraeme }
544 1.25 xtraeme }
545 1.25 xtraeme if (i >= nelems) {
546 1.25 xtraeme if (mydevname) {
547 1.25 xtraeme warnx("unknown sensor `%s' for device `%s'",
548 1.25 xtraeme sname, mydevname);
549 1.25 xtraeme return EINVAL;
550 1.25 xtraeme }
551 1.25 xtraeme }
552 1.25 xtraeme sname = strtok(NULL, ",");
553 1.25 xtraeme }
554 1.1 groo
555 1.25 xtraeme /* check if all sensors were ok, and error out if not */
556 1.25 xtraeme for (i = 0; i < nelems; i++) {
557 1.25 xtraeme if (es[i].visible)
558 1.25 xtraeme return 0;
559 1.1 groo }
560 1.25 xtraeme
561 1.25 xtraeme warnx("no sensors selected to display");
562 1.25 xtraeme return EINVAL;
563 1.1 groo }
564 1.1 groo
565 1.25 xtraeme static void
566 1.42.2.1 matt print_sensors(struct envsys_sensor *es, size_t nelems, const char *dvname)
567 1.1 groo {
568 1.25 xtraeme size_t maxlen = 0;
569 1.25 xtraeme double temp = 0;
570 1.27 xtraeme const char *invalid = "N/A";
571 1.25 xtraeme const char *degrees = NULL;
572 1.25 xtraeme int i;
573 1.1 groo
574 1.25 xtraeme /* find the longest description */
575 1.31 xtraeme for (i = 0; i < nelems; i++) {
576 1.25 xtraeme if (strlen(es[i].desc) > maxlen)
577 1.25 xtraeme maxlen = strlen(es[i].desc);
578 1.25 xtraeme }
579 1.1 groo
580 1.25 xtraeme if (width)
581 1.25 xtraeme maxlen = width;
582 1.1 groo
583 1.25 xtraeme /* print the sensors */
584 1.31 xtraeme for (i = 0; i < nelems; i++) {
585 1.42.2.1 matt /* skip sensors that don't belong to device 'dvname' */
586 1.42.2.1 matt if (strcmp(es[i].dvname, dvname))
587 1.42.2.1 matt continue;
588 1.1 groo
589 1.25 xtraeme /* skip sensors that were not marked as visible */
590 1.25 xtraeme if (sensors && !es[i].visible)
591 1.25 xtraeme continue;
592 1.1 groo
593 1.42.2.1 matt /* Do not print invalid sensors if -I is set */
594 1.42.2.1 matt if ((flags & ENVSYS_IFLAG) && es[i].invalid)
595 1.42.2.1 matt continue;
596 1.42.2.1 matt
597 1.42.2.1 matt (void)printf("%s%*.*s", mydevname ? "" : " ", (int)maxlen,
598 1.42.2.1 matt (int)maxlen, es[i].desc);
599 1.1 groo
600 1.29 xtraeme if (es[i].invalid) {
601 1.29 xtraeme (void)printf(": %10s\n", invalid);
602 1.29 xtraeme continue;
603 1.29 xtraeme }
604 1.29 xtraeme
605 1.42.2.1 matt /*
606 1.42.2.1 matt * Indicator and Battery charge sensors.
607 1.42.2.1 matt */
608 1.42.2.1 matt if ((strcmp(es[i].type, "Indicator") == 0) ||
609 1.42.2.1 matt (strcmp(es[i].type, "Battery charge") == 0)) {
610 1.29 xtraeme
611 1.29 xtraeme (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
612 1.1 groo
613 1.25 xtraeme /* converts the value to degC or degF */
614 1.25 xtraeme #define CONVERTTEMP(a, b, c) \
615 1.25 xtraeme do { \
616 1.42.2.1 matt if (b) \
617 1.42.2.1 matt (a) = ((b) / 1000000.0) - 273.15; \
618 1.25 xtraeme if (flags & ENVSYS_FFLAG) { \
619 1.42.2.1 matt if (b) \
620 1.42.2.1 matt (a) = (9.0 / 5.0) * (a) + 32.0; \
621 1.25 xtraeme (c) = "degF"; \
622 1.25 xtraeme } else \
623 1.25 xtraeme (c) = "degC"; \
624 1.25 xtraeme } while (/* CONSTCOND */ 0)
625 1.25 xtraeme
626 1.25 xtraeme
627 1.25 xtraeme /* temperatures */
628 1.27 xtraeme } else if (strcmp(es[i].type, "Temperature") == 0) {
629 1.25 xtraeme
630 1.25 xtraeme CONVERTTEMP(temp, es[i].cur_value, degrees);
631 1.29 xtraeme (void)printf(": %10.3f %s", temp, degrees);
632 1.25 xtraeme
633 1.25 xtraeme if (es[i].critmax_value || es[i].critmin_value)
634 1.25 xtraeme (void)printf(" ");
635 1.25 xtraeme
636 1.25 xtraeme if (es[i].critmax_value) {
637 1.25 xtraeme CONVERTTEMP(temp, es[i].critmax_value, degrees);
638 1.25 xtraeme (void)printf("max: %8.3f %s ", temp, degrees);
639 1.25 xtraeme }
640 1.25 xtraeme
641 1.25 xtraeme if (es[i].critmin_value) {
642 1.25 xtraeme CONVERTTEMP(temp, es[i].critmin_value, degrees);
643 1.25 xtraeme (void)printf("min: %8.3f %s", temp, degrees);
644 1.25 xtraeme }
645 1.34 xtraeme #undef CONVERTTEMP
646 1.25 xtraeme
647 1.25 xtraeme /* fans */
648 1.25 xtraeme } else if (strcmp(es[i].type, "Fan") == 0) {
649 1.25 xtraeme
650 1.29 xtraeme (void)printf(": %10u RPM", es[i].cur_value);
651 1.25 xtraeme
652 1.25 xtraeme if (es[i].critmax_value || es[i].critmin_value)
653 1.25 xtraeme (void)printf(" ");
654 1.25 xtraeme if (es[i].critmax_value)
655 1.25 xtraeme (void)printf("max: %8u RPM ",
656 1.25 xtraeme es[i].critmax_value);
657 1.25 xtraeme if (es[i].critmin_value)
658 1.25 xtraeme (void)printf("min: %8u RPM",
659 1.25 xtraeme es[i].critmin_value);
660 1.25 xtraeme
661 1.25 xtraeme /* integers */
662 1.25 xtraeme } else if (strcmp(es[i].type, "Integer") == 0) {
663 1.25 xtraeme
664 1.29 xtraeme (void)printf(": %10d", es[i].cur_value);
665 1.25 xtraeme
666 1.42.2.1 matt /* drives */
667 1.25 xtraeme } else if (strcmp(es[i].type, "Drive") == 0) {
668 1.27 xtraeme
669 1.30 xtraeme (void)printf(": %10s", es[i].drvstate);
670 1.25 xtraeme
671 1.42.2.1 matt /* Battery capacity */
672 1.42.2.1 matt } else if (strcmp(es[i].type, "Battery capacity") == 0) {
673 1.42.2.1 matt
674 1.42.2.1 matt (void)printf(": %10s", es[i].battcap);
675 1.42.2.1 matt
676 1.25 xtraeme /* everything else */
677 1.25 xtraeme } else {
678 1.25 xtraeme const char *type;
679 1.25 xtraeme
680 1.25 xtraeme if (strcmp(es[i].type, "Voltage DC") == 0)
681 1.25 xtraeme type = "V";
682 1.25 xtraeme else if (strcmp(es[i].type, "Voltage AC") == 0)
683 1.25 xtraeme type = "VAC";
684 1.25 xtraeme else if (strcmp(es[i].type, "Ampere") == 0)
685 1.25 xtraeme type = "A";
686 1.25 xtraeme else if (strcmp(es[i].type, "Watts") == 0)
687 1.25 xtraeme type = "W";
688 1.25 xtraeme else if (strcmp(es[i].type, "Ohms") == 0)
689 1.25 xtraeme type = "Ohms";
690 1.25 xtraeme else if (strcmp(es[i].type, "Watt hour") == 0)
691 1.25 xtraeme type = "Wh";
692 1.25 xtraeme else if (strcmp(es[i].type, "Ampere hour") == 0)
693 1.25 xtraeme type = "Ah";
694 1.25 xtraeme else
695 1.25 xtraeme type = NULL;
696 1.1 groo
697 1.29 xtraeme (void)printf(": %10.3f %s",
698 1.29 xtraeme es[i].cur_value / 1000000.0, type);
699 1.29 xtraeme
700 1.29 xtraeme if (es[i].percentage && es[i].max_value) {
701 1.29 xtraeme (void)printf(" (%5.2f%%)",
702 1.29 xtraeme (es[i].cur_value * 100.0) /
703 1.29 xtraeme es[i].max_value);
704 1.25 xtraeme }
705 1.25 xtraeme
706 1.25 xtraeme if (es[i].critcap_value) {
707 1.25 xtraeme (void)printf(" critical (%5.2f%%)",
708 1.25 xtraeme (es[i].critcap_value * 100.0) /
709 1.25 xtraeme es[i].max_value);
710 1.25 xtraeme }
711 1.6 explorer
712 1.25 xtraeme if (es[i].critmax_value || es[i].critmin_value)
713 1.25 xtraeme (void)printf(" ");
714 1.25 xtraeme if (es[i].critmax_value)
715 1.25 xtraeme (void)printf("max: %8.3f %s ",
716 1.25 xtraeme es[i].critmax_value / 1000000.0,
717 1.25 xtraeme type);
718 1.25 xtraeme if (es[i].critmin_value)
719 1.25 xtraeme (void)printf("min: %8.3f %s",
720 1.25 xtraeme es[i].critmin_value / 1000000.0,
721 1.25 xtraeme type);
722 1.1 groo
723 1.25 xtraeme }
724 1.1 groo
725 1.25 xtraeme (void)printf("\n");
726 1.1 groo }
727 1.25 xtraeme }
728 1.1 groo
729 1.25 xtraeme static int
730 1.25 xtraeme usage(void)
731 1.25 xtraeme {
732 1.42.2.1 matt (void)fprintf(stderr, "Usage: %s [-DfIlrSx] ", getprogname());
733 1.42.2.1 matt (void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
734 1.42.2.1 matt (void)fprintf(stderr, "[-s sensor,...] [-w width]\n");
735 1.25 xtraeme exit(EXIT_FAILURE);
736 1.25 xtraeme /* NOTREACHED */
737 1.1 groo }
738