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