Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.71.2.1
      1  1.71.2.1       mjf /*	$NetBSD: sysmon_envsys.c,v 1.71.2.1 2007/11/19 00:48:23 mjf Exp $	*/
      2      1.18   xtraeme 
      3      1.18   xtraeme /*-
      4      1.69   xtraeme  * Copyright (c) 2007 Juan Romero Pardines.
      5      1.18   xtraeme  * All rights reserved.
      6      1.18   xtraeme  *
      7      1.18   xtraeme  * Redistribution and use in source and binary forms, with or without
      8      1.18   xtraeme  * modification, are permitted provided that the following conditions
      9      1.18   xtraeme  * are met:
     10      1.18   xtraeme  * 1. Redistributions of source code must retain the above copyright
     11      1.18   xtraeme  *    notice, this list of conditions and the following disclaimer.
     12      1.18   xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.18   xtraeme  *    notice, this list of conditions and the following disclaimer in the
     14      1.18   xtraeme  *    documentation and/or other materials provided with the distribution.
     15      1.69   xtraeme  *
     16      1.69   xtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17      1.69   xtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18      1.69   xtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19      1.69   xtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20      1.69   xtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21      1.69   xtraeme  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22      1.69   xtraeme  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23      1.69   xtraeme  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24      1.69   xtraeme  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25      1.69   xtraeme  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26      1.18   xtraeme  */
     27       1.1   thorpej 
     28       1.1   thorpej /*-
     29       1.1   thorpej  * Copyright (c) 2000 Zembu Labs, Inc.
     30       1.1   thorpej  * All rights reserved.
     31       1.1   thorpej  *
     32       1.1   thorpej  * Author: Jason R. Thorpe <thorpej (at) zembu.com>
     33       1.1   thorpej  *
     34       1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     35       1.1   thorpej  * modification, are permitted provided that the following conditions
     36       1.1   thorpej  * are met:
     37       1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     38       1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     39       1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     40       1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     41       1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     42       1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     43       1.1   thorpej  *    must display the following acknowledgement:
     44       1.1   thorpej  *	This product includes software developed by Zembu Labs, Inc.
     45       1.1   thorpej  * 4. Neither the name of Zembu Labs nor the names of its employees may
     46       1.1   thorpej  *    be used to endorse or promote products derived from this software
     47       1.1   thorpej  *    without specific prior written permission.
     48       1.1   thorpej  *
     49       1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
     50       1.1   thorpej  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
     51       1.1   thorpej  * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
     52       1.1   thorpej  * CLAIMED.  IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
     53       1.1   thorpej  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     54       1.1   thorpej  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     55       1.1   thorpej  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     56       1.1   thorpej  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     57       1.1   thorpej  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     58       1.1   thorpej  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     59       1.1   thorpej  */
     60       1.1   thorpej 
     61       1.1   thorpej /*
     62      1.18   xtraeme  * Environmental sensor framework for sysmon, exported to userland
     63      1.18   xtraeme  * with proplib(3).
     64       1.1   thorpej  */
     65       1.2     lukem 
     66       1.2     lukem #include <sys/cdefs.h>
     67  1.71.2.1       mjf __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.71.2.1 2007/11/19 00:48:23 mjf Exp $");
     68       1.1   thorpej 
     69       1.1   thorpej #include <sys/param.h>
     70      1.18   xtraeme #include <sys/types.h>
     71       1.1   thorpej #include <sys/conf.h>
     72       1.1   thorpej #include <sys/errno.h>
     73      1.22   xtraeme #include <sys/fcntl.h>
     74       1.1   thorpej #include <sys/kernel.h>
     75       1.1   thorpej #include <sys/systm.h>
     76       1.1   thorpej #include <sys/proc.h>
     77      1.18   xtraeme #include <sys/mutex.h>
     78      1.18   xtraeme #include <sys/kmem.h>
     79       1.1   thorpej 
     80      1.60   xtraeme /* #define ENVSYS_DEBUG */
     81       1.1   thorpej #include <dev/sysmon/sysmonvar.h>
     82      1.18   xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     83      1.18   xtraeme #include <dev/sysmon/sysmon_taskq.h>
     84       1.1   thorpej 
     85      1.61   xtraeme /*
     86      1.61   xtraeme  * Notes about locking:
     87      1.61   xtraeme  *
     88  1.71.2.1       mjf  * The 'sme_mtx' lock is used to protect access to the sysmon_envsys
     89  1.71.2.1       mjf  * objects (devices, sensors, events) and the global counter
     90  1.71.2.1       mjf  * 'sysmon_envsys_next_sensor_index'. The 'sme_cv' condition variable
     91  1.71.2.1       mjf  * is used to wait for completion paths on these objects.
     92  1.71.2.1       mjf  *
     93  1.71.2.1       mjf  * The 'sme_events_mtx' lock is used to protect initialization and
     94  1.71.2.1       mjf  * finalization of the per device events framework (the callout(9) and
     95  1.71.2.1       mjf  * workqueue(9) that is used to check for conditions and sending events
     96  1.71.2.1       mjf  * to the powerd(8) daemon (if running)).
     97  1.71.2.1       mjf  *
     98  1.71.2.1       mjf  * The callouts are protected by the 'sme_callout_mtx' spin lock and the
     99  1.71.2.1       mjf  * 'sme_callout_cv' is used to check that the callout has finished before
    100  1.71.2.1       mjf  * unregistering a sysmon_envsys device.
    101      1.61   xtraeme  *
    102      1.61   xtraeme  */
    103      1.18   xtraeme 
    104  1.71.2.1       mjf kmutex_t sme_mtx, sme_events_mtx, sme_callout_mtx;
    105  1.71.2.1       mjf kcondvar_t sme_cv, sme_callout_cv;
    106      1.41   xtraeme 
    107      1.69   xtraeme /*
    108      1.69   xtraeme  * Types of properties that can be set via userland.
    109      1.69   xtraeme  */
    110      1.69   xtraeme enum {
    111      1.69   xtraeme 	USERPROP_DESC 		= 0x0001,
    112      1.69   xtraeme 	USERPROP_BATTCAP	= 0x0002,
    113      1.69   xtraeme 	USERPROP_CRITMAX	= 0x0004,
    114      1.69   xtraeme 	USERPROP_CRITMIN	= 0x0008,
    115      1.69   xtraeme 	USERPROP_RFACT		= 0x0010
    116      1.69   xtraeme };
    117      1.69   xtraeme 
    118      1.61   xtraeme static prop_dictionary_t sme_propd;
    119      1.56   xtraeme static uint32_t sysmon_envsys_next_sensor_index = 0;
    120      1.18   xtraeme static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    121       1.1   thorpej 
    122      1.53   xtraeme static void sysmon_envsys_destroy_plist(prop_array_t);
    123      1.69   xtraeme static void sme_remove_userprops(void);
    124  1.71.2.1       mjf static int sme_add_property_dictionary(struct sysmon_envsys *, prop_array_t,
    125  1.71.2.1       mjf 				       prop_dictionary_t);
    126       1.1   thorpej 
    127      1.18   xtraeme /*
    128      1.18   xtraeme  * sysmon_envsys_init:
    129      1.18   xtraeme  *
    130      1.18   xtraeme  * 	+ Initialize global mutexes, dictionary and the linked lists.
    131      1.18   xtraeme  */
    132      1.18   xtraeme void
    133      1.18   xtraeme sysmon_envsys_init(void)
    134      1.18   xtraeme {
    135      1.18   xtraeme 	LIST_INIT(&sysmon_envsys_list);
    136  1.71.2.1       mjf 	mutex_init(&sme_mtx, MUTEX_DEFAULT, IPL_NONE);
    137  1.71.2.1       mjf 	mutex_init(&sme_events_mtx, MUTEX_DEFAULT, IPL_NONE);
    138  1.71.2.1       mjf 	mutex_init(&sme_callout_mtx, MUTEX_SPIN, IPL_NONE);
    139  1.71.2.1       mjf 	cv_init(&sme_cv, "smeworker");
    140  1.71.2.1       mjf 	cv_init(&sme_callout_cv, "smecallout");
    141      1.18   xtraeme 	sme_propd = prop_dictionary_create();
    142      1.18   xtraeme }
    143       1.1   thorpej 
    144       1.1   thorpej /*
    145       1.1   thorpej  * sysmonopen_envsys:
    146       1.1   thorpej  *
    147      1.18   xtraeme  *	+ Open the system monitor device.
    148       1.1   thorpej  */
    149       1.1   thorpej int
    150      1.18   xtraeme sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    151       1.1   thorpej {
    152      1.18   xtraeme 	return 0;
    153       1.1   thorpej }
    154       1.1   thorpej 
    155       1.1   thorpej /*
    156       1.1   thorpej  * sysmonclose_envsys:
    157       1.1   thorpej  *
    158      1.18   xtraeme  *	+ Close the system monitor device.
    159       1.1   thorpej  */
    160       1.1   thorpej int
    161      1.18   xtraeme sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    162       1.1   thorpej {
    163      1.18   xtraeme 	return 0;
    164       1.1   thorpej }
    165       1.1   thorpej 
    166       1.1   thorpej /*
    167       1.1   thorpej  * sysmonioctl_envsys:
    168       1.1   thorpej  *
    169      1.52   xtraeme  *	+ Perform a sysmon envsys control request.
    170       1.1   thorpej  */
    171       1.1   thorpej int
    172      1.18   xtraeme sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    173       1.1   thorpej {
    174      1.18   xtraeme 	struct sysmon_envsys *sme = NULL;
    175       1.1   thorpej 	int error = 0;
    176       1.1   thorpej 	u_int oidx;
    177       1.1   thorpej 
    178       1.1   thorpej 	switch (cmd) {
    179  1.71.2.1       mjf 	/*
    180  1.71.2.1       mjf 	 * To update the global dictionary with latest data from devices.
    181  1.71.2.1       mjf 	 */
    182      1.18   xtraeme 	case ENVSYS_GETDICTIONARY:
    183      1.18   xtraeme 	    {
    184      1.18   xtraeme 		struct plistref *plist = (struct plistref *)data;
    185      1.56   xtraeme 
    186      1.18   xtraeme 		/*
    187  1.71.2.1       mjf 		 * Update dictionaries on all sysmon envsys devices
    188  1.71.2.1       mjf 		 * registered.
    189      1.18   xtraeme 		 */
    190      1.58   xtraeme 		mutex_enter(&sme_mtx);
    191      1.18   xtraeme 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    192  1.71.2.1       mjf 			sysmon_envsys_acquire(sme);
    193      1.18   xtraeme 			error = sme_update_dictionary(sme);
    194      1.18   xtraeme 			if (error) {
    195      1.18   xtraeme 				DPRINTF(("%s: sme_update_dictionary, "
    196      1.18   xtraeme 				    "error=%d\n", __func__, error));
    197  1.71.2.1       mjf 				sysmon_envsys_release(sme);
    198      1.58   xtraeme 				mutex_exit(&sme_mtx);
    199      1.18   xtraeme 				return error;
    200      1.18   xtraeme 			}
    201  1.71.2.1       mjf 			sysmon_envsys_release(sme);
    202      1.18   xtraeme 		}
    203      1.58   xtraeme 		mutex_exit(&sme_mtx);
    204      1.18   xtraeme 		/*
    205      1.18   xtraeme 		 * Copy global dictionary to userland.
    206      1.18   xtraeme 		 */
    207      1.18   xtraeme 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    208       1.1   thorpej 		break;
    209      1.18   xtraeme 	    }
    210  1.71.2.1       mjf 	/*
    211  1.71.2.1       mjf 	 * To set properties on multiple devices.
    212  1.71.2.1       mjf 	 */
    213      1.18   xtraeme 	case ENVSYS_SETDICTIONARY:
    214       1.1   thorpej 	    {
    215      1.18   xtraeme 		const struct plistref *plist = (const struct plistref *)data;
    216      1.18   xtraeme 		prop_dictionary_t udict;
    217      1.69   xtraeme 		prop_object_iterator_t iter, iter2;
    218      1.69   xtraeme 		prop_object_t obj, obj2;
    219      1.69   xtraeme 		prop_array_t array_u, array_k;
    220      1.18   xtraeme 		const char *devname = NULL;
    221      1.18   xtraeme 
    222      1.22   xtraeme 		if ((flag & FWRITE) == 0)
    223      1.22   xtraeme 			return EPERM;
    224      1.22   xtraeme 
    225      1.18   xtraeme 		/*
    226      1.18   xtraeme 		 * Get dictionary from userland.
    227      1.18   xtraeme 		 */
    228      1.18   xtraeme 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    229      1.41   xtraeme 		if (error) {
    230      1.41   xtraeme 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    231      1.41   xtraeme 			    __func__, error));
    232      1.18   xtraeme 			break;
    233      1.41   xtraeme 		}
    234       1.4  explorer 
    235      1.69   xtraeme 		iter = prop_dictionary_iterator(udict);
    236      1.69   xtraeme 		if (!iter) {
    237      1.69   xtraeme 			prop_object_release(udict);
    238      1.69   xtraeme 			return ENOMEM;
    239      1.69   xtraeme 		}
    240      1.69   xtraeme 
    241      1.18   xtraeme 		/*
    242      1.69   xtraeme 		 * Iterate over the userland dictionary and process
    243      1.69   xtraeme 		 * the list of devices.
    244      1.18   xtraeme 		 */
    245      1.69   xtraeme 		while ((obj = prop_object_iterator_next(iter))) {
    246      1.69   xtraeme 			array_u = prop_dictionary_get_keysym(udict, obj);
    247      1.69   xtraeme 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    248      1.69   xtraeme 				prop_object_iterator_release(iter);
    249      1.69   xtraeme 				prop_object_release(udict);
    250      1.69   xtraeme 				return EINVAL;
    251      1.69   xtraeme 			}
    252      1.69   xtraeme 
    253      1.69   xtraeme 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    254      1.69   xtraeme 			DPRINTF(("%s: processing the '%s' array requests\n",
    255      1.69   xtraeme 			    __func__, devname));
    256      1.69   xtraeme 
    257      1.69   xtraeme 			/*
    258      1.69   xtraeme 			 * find the correct sme device.
    259      1.69   xtraeme 			 */
    260  1.71.2.1       mjf 			mutex_enter(&sme_mtx);
    261      1.69   xtraeme 			sme = sysmon_envsys_find(devname);
    262      1.69   xtraeme 			if (!sme) {
    263  1.71.2.1       mjf 				mutex_exit(&sme_mtx);
    264      1.69   xtraeme 				DPRINTF(("%s: NULL sme\n", __func__));
    265      1.69   xtraeme 				prop_object_iterator_release(iter);
    266      1.69   xtraeme 				prop_object_release(udict);
    267      1.69   xtraeme 				return EINVAL;
    268      1.69   xtraeme 			}
    269      1.69   xtraeme 
    270      1.69   xtraeme 			/*
    271      1.69   xtraeme 			 * Find the correct array object with the string
    272      1.69   xtraeme 			 * supplied by the userland dictionary.
    273      1.69   xtraeme 			 */
    274      1.69   xtraeme 			array_k = prop_dictionary_get(sme_propd, devname);
    275      1.69   xtraeme 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    276      1.69   xtraeme 				DPRINTF(("%s: array device failed\n",
    277      1.69   xtraeme 				    __func__));
    278      1.69   xtraeme 				sysmon_envsys_release(sme);
    279  1.71.2.1       mjf 				mutex_exit(&sme_mtx);
    280      1.69   xtraeme 				prop_object_iterator_release(iter);
    281      1.69   xtraeme 				prop_object_release(udict);
    282      1.69   xtraeme 				return EINVAL;
    283      1.69   xtraeme 			}
    284      1.69   xtraeme 
    285      1.69   xtraeme 			iter2 = prop_array_iterator(array_u);
    286      1.69   xtraeme 			if (!iter2) {
    287      1.69   xtraeme 				sysmon_envsys_release(sme);
    288  1.71.2.1       mjf 				mutex_exit(&sme_mtx);
    289      1.69   xtraeme 				prop_object_iterator_release(iter);
    290      1.69   xtraeme 				prop_object_release(udict);
    291      1.69   xtraeme 				return ENOMEM;
    292      1.69   xtraeme 			}
    293      1.69   xtraeme 
    294      1.69   xtraeme 			/*
    295      1.69   xtraeme 			 * Iterate over the array of dictionaries to
    296  1.71.2.1       mjf 			 * process the list of sensors and properties.
    297      1.69   xtraeme 			 */
    298      1.69   xtraeme 			while ((obj2 = prop_object_iterator_next(iter2))) {
    299  1.71.2.1       mjf 				/*
    300  1.71.2.1       mjf 				 * do the real work now.
    301  1.71.2.1       mjf 				 */
    302      1.69   xtraeme 				error = sme_userset_dictionary(sme,
    303      1.69   xtraeme 							       obj2,
    304      1.69   xtraeme 							       array_k);
    305      1.69   xtraeme 				if (error) {
    306      1.69   xtraeme 					sysmon_envsys_release(sme);
    307  1.71.2.1       mjf 					mutex_exit(&sme_mtx);
    308      1.69   xtraeme 					prop_object_iterator_release(iter2);
    309      1.69   xtraeme 					prop_object_iterator_release(iter);
    310      1.69   xtraeme 					prop_object_release(udict);
    311  1.71.2.1       mjf 					return error;
    312      1.69   xtraeme 				}
    313      1.69   xtraeme 			}
    314      1.69   xtraeme 
    315      1.69   xtraeme 			sysmon_envsys_release(sme);
    316  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    317      1.69   xtraeme 			prop_object_iterator_release(iter2);
    318      1.18   xtraeme 		}
    319       1.4  explorer 
    320      1.69   xtraeme 		prop_object_iterator_release(iter);
    321      1.69   xtraeme 		prop_object_release(udict);
    322      1.69   xtraeme 		break;
    323      1.69   xtraeme 	    }
    324  1.71.2.1       mjf 	/*
    325  1.71.2.1       mjf 	 * To remove all properties from all devices registered.
    326  1.71.2.1       mjf 	 */
    327      1.69   xtraeme 	case ENVSYS_REMOVEPROPS:
    328      1.69   xtraeme 	    {
    329      1.69   xtraeme 		const struct plistref *plist = (const struct plistref *)data;
    330      1.69   xtraeme 		prop_dictionary_t udict;
    331      1.69   xtraeme 		prop_object_t obj;
    332      1.69   xtraeme 
    333      1.69   xtraeme 		if ((flag & FWRITE) == 0)
    334      1.69   xtraeme 			return EPERM;
    335       1.4  explorer 
    336      1.69   xtraeme 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    337      1.69   xtraeme 		if (error) {
    338      1.69   xtraeme 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    339      1.69   xtraeme 			    __func__, error));
    340       1.4  explorer 			break;
    341       1.4  explorer 		}
    342       1.1   thorpej 
    343      1.69   xtraeme 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    344      1.69   xtraeme 		if (!obj || !prop_bool_true(obj)) {
    345      1.69   xtraeme 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    346      1.69   xtraeme 			     __func__));
    347      1.69   xtraeme 			return EINVAL;
    348       1.1   thorpej 		}
    349      1.18   xtraeme 
    350  1.71.2.1       mjf 		prop_object_release(udict);
    351      1.69   xtraeme 		sme_remove_userprops();
    352      1.69   xtraeme 
    353       1.1   thorpej 		break;
    354       1.1   thorpej 	    }
    355  1.71.2.1       mjf 	/*
    356  1.71.2.1       mjf 	 * Compatibility ioctls with the old interface, only implemented
    357  1.71.2.1       mjf 	 * ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough to make old
    358  1.71.2.1       mjf 	 * applications work.
    359  1.71.2.1       mjf 	 */
    360       1.1   thorpej 	case ENVSYS_GTREDATA:
    361       1.1   thorpej 	    {
    362      1.18   xtraeme 		struct envsys_tre_data *tred = (void *)data;
    363      1.18   xtraeme 		envsys_data_t *edata = NULL;
    364  1.71.2.1       mjf 		bool found = false;
    365       1.1   thorpej 
    366       1.1   thorpej 		tred->validflags = 0;
    367       1.1   thorpej 
    368  1.71.2.1       mjf 		mutex_enter(&sme_mtx);
    369      1.18   xtraeme 		sme = sysmon_envsys_find_40(tred->sensor);
    370  1.71.2.1       mjf 		if (!sme) {
    371  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    372       1.1   thorpej 			break;
    373  1.71.2.1       mjf 		}
    374      1.18   xtraeme 
    375       1.1   thorpej 		oidx = tred->sensor;
    376       1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    377      1.18   xtraeme 
    378      1.18   xtraeme 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    379      1.18   xtraeme 		    __func__, tred->sensor, oidx, sme->sme_name,
    380      1.18   xtraeme 		    sme->sme_nsensors));
    381      1.18   xtraeme 
    382  1.71.2.1       mjf 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    383  1.71.2.1       mjf 			if (edata->sensor == tred->sensor) {
    384  1.71.2.1       mjf 				found = true;
    385  1.71.2.1       mjf 				break;
    386  1.71.2.1       mjf 			}
    387  1.71.2.1       mjf 		}
    388  1.71.2.1       mjf 
    389  1.71.2.1       mjf 		if (!found) {
    390  1.71.2.1       mjf 			sysmon_envsys_release(sme);
    391  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    392  1.71.2.1       mjf 			error = ENODEV;
    393  1.71.2.1       mjf 			break;
    394  1.71.2.1       mjf 		}
    395      1.18   xtraeme 
    396      1.18   xtraeme 		if (tred->sensor < sme->sme_nsensors) {
    397  1.71.2.1       mjf 			if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    398  1.71.2.1       mjf 				(*sme->sme_refresh)(sme, edata);
    399      1.18   xtraeme 
    400  1.71.2.1       mjf 			/*
    401  1.71.2.1       mjf 			 * copy required values to the old interface.
    402  1.71.2.1       mjf 			 */
    403      1.18   xtraeme 			tred->sensor = edata->sensor;
    404      1.18   xtraeme 			tred->cur.data_us = edata->value_cur;
    405      1.18   xtraeme 			tred->cur.data_s = edata->value_cur;
    406      1.18   xtraeme 			tred->max.data_us = edata->value_max;
    407      1.18   xtraeme 			tred->max.data_s = edata->value_max;
    408      1.18   xtraeme 			tred->min.data_us = edata->value_min;
    409      1.18   xtraeme 			tred->min.data_s = edata->value_min;
    410      1.18   xtraeme 			tred->avg.data_us = edata->value_avg;
    411      1.18   xtraeme 			tred->avg.data_s = edata->value_avg;
    412      1.71   xtraeme 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    413      1.71   xtraeme 				tred->units = ENVSYS_INDICATOR;
    414      1.71   xtraeme 			else
    415  1.71.2.1       mjf 				tred->units = edata->units;
    416      1.18   xtraeme 
    417      1.20   xtraeme 			tred->validflags |= ENVSYS_FVALID;
    418      1.20   xtraeme 			tred->validflags |= ENVSYS_FCURVALID;
    419      1.20   xtraeme 
    420      1.20   xtraeme 			if (edata->flags & ENVSYS_FPERCENT) {
    421      1.20   xtraeme 				tred->validflags |= ENVSYS_FMAXVALID;
    422      1.20   xtraeme 				tred->validflags |= ENVSYS_FFRACVALID;
    423      1.20   xtraeme 			}
    424      1.20   xtraeme 
    425  1.71.2.1       mjf 			if (edata->state == ENVSYS_SINVALID) {
    426      1.20   xtraeme 				tred->validflags &= ~ENVSYS_FCURVALID;
    427      1.18   xtraeme 				tred->cur.data_us = tred->cur.data_s = 0;
    428      1.18   xtraeme 			}
    429      1.18   xtraeme 
    430      1.18   xtraeme 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    431      1.18   xtraeme 			    __func__, edata->desc, tred->cur.data_s));
    432      1.18   xtraeme 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    433      1.18   xtraeme 			    " tred->sensor=%d\n", __func__, tred->validflags,
    434      1.18   xtraeme 			    tred->units, tred->sensor));
    435       1.3  jdolecek 		}
    436       1.1   thorpej 		tred->sensor = oidx;
    437  1.71.2.1       mjf 		sysmon_envsys_release(sme);
    438      1.58   xtraeme 		mutex_exit(&sme_mtx);
    439       1.1   thorpej 
    440       1.1   thorpej 		break;
    441       1.1   thorpej 	    }
    442       1.1   thorpej 	case ENVSYS_GTREINFO:
    443       1.1   thorpej 	    {
    444      1.18   xtraeme 		struct envsys_basic_info *binfo = (void *)data;
    445      1.18   xtraeme 		envsys_data_t *edata = NULL;
    446  1.71.2.1       mjf 		bool found = false;
    447       1.1   thorpej 
    448       1.1   thorpej 		binfo->validflags = 0;
    449       1.1   thorpej 
    450  1.71.2.1       mjf 		mutex_enter(&sme_mtx);
    451      1.18   xtraeme 		sme = sysmon_envsys_find_40(binfo->sensor);
    452  1.71.2.1       mjf 		if (!sme) {
    453  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    454       1.1   thorpej 			break;
    455  1.71.2.1       mjf 		}
    456      1.18   xtraeme 
    457       1.1   thorpej 		oidx = binfo->sensor;
    458       1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    459      1.18   xtraeme 
    460  1.71.2.1       mjf 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    461  1.71.2.1       mjf 			if (edata->sensor == binfo->sensor) {
    462  1.71.2.1       mjf 				found = true;
    463  1.71.2.1       mjf 				break;
    464  1.71.2.1       mjf 			}
    465  1.71.2.1       mjf 		}
    466  1.71.2.1       mjf 
    467  1.71.2.1       mjf 		if (!found) {
    468  1.71.2.1       mjf 			sysmon_envsys_release(sme);
    469  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    470  1.71.2.1       mjf 			error = ENODEV;
    471  1.71.2.1       mjf 			break;
    472  1.71.2.1       mjf 		}
    473      1.18   xtraeme 
    474      1.20   xtraeme 		binfo->validflags |= ENVSYS_FVALID;
    475      1.18   xtraeme 
    476      1.18   xtraeme 		if (binfo->sensor < sme->sme_nsensors) {
    477      1.71   xtraeme 			if (edata->units == ENVSYS_BATTERY_CHARGE)
    478      1.71   xtraeme 				binfo->units = ENVSYS_INDICATOR;
    479      1.71   xtraeme 			else
    480  1.71.2.1       mjf 				binfo->units = edata->units;
    481      1.70    plunky 
    482      1.70    plunky 			/*
    483      1.70    plunky 			 * previously, the ACPI sensor names included the
    484      1.70    plunky 			 * device name. Include that in compatibility code.
    485      1.70    plunky 			 */
    486      1.70    plunky 			if (strncmp(sme->sme_name, "acpi", 4) == 0)
    487      1.70    plunky 				(void)snprintf(binfo->desc, sizeof(binfo->desc),
    488      1.70    plunky 				    "%s %s", sme->sme_name, edata->desc);
    489      1.70    plunky 			else
    490      1.70    plunky 				(void)strlcpy(binfo->desc, edata->desc,
    491      1.70    plunky 				    sizeof(binfo->desc));
    492      1.18   xtraeme 		}
    493      1.18   xtraeme 
    494      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    495      1.18   xtraeme 		    __func__, binfo->units, binfo->validflags));
    496      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    497      1.18   xtraeme 		    __func__, binfo->desc, binfo->sensor));
    498      1.18   xtraeme 
    499       1.1   thorpej 		binfo->sensor = oidx;
    500  1.71.2.1       mjf 		sysmon_envsys_release(sme);
    501      1.58   xtraeme 		mutex_exit(&sme_mtx);
    502      1.18   xtraeme 
    503       1.1   thorpej 		break;
    504       1.1   thorpej 	    }
    505       1.1   thorpej 	default:
    506       1.1   thorpej 		error = ENOTTY;
    507      1.18   xtraeme 		break;
    508       1.1   thorpej 	}
    509       1.1   thorpej 
    510      1.18   xtraeme 	return error;
    511       1.1   thorpej }
    512       1.1   thorpej 
    513       1.1   thorpej /*
    514  1.71.2.1       mjf  * sysmon_envsys_create:
    515  1.71.2.1       mjf  *
    516  1.71.2.1       mjf  * 	+ Allocates a new sysmon_envsys object and initializes the
    517  1.71.2.1       mjf  * 	  stuff for sensors and events.
    518  1.71.2.1       mjf  */
    519  1.71.2.1       mjf struct sysmon_envsys *
    520  1.71.2.1       mjf sysmon_envsys_create(void)
    521  1.71.2.1       mjf {
    522  1.71.2.1       mjf 	struct sysmon_envsys *sme;
    523  1.71.2.1       mjf 
    524  1.71.2.1       mjf 	sme = kmem_zalloc(sizeof(*sme), KM_SLEEP);
    525  1.71.2.1       mjf 	TAILQ_INIT(&sme->sme_sensors_list);
    526  1.71.2.1       mjf 	LIST_INIT(&sme->sme_events_list);
    527  1.71.2.1       mjf 
    528  1.71.2.1       mjf 	return sme;
    529  1.71.2.1       mjf }
    530  1.71.2.1       mjf 
    531  1.71.2.1       mjf /*
    532  1.71.2.1       mjf  * sysmon_envsys_destroy:
    533  1.71.2.1       mjf  *
    534  1.71.2.1       mjf  * 	+ Removes all sensors from the tail queue and frees the
    535  1.71.2.1       mjf  * 	  sysmon_envsys object.
    536  1.71.2.1       mjf  */
    537  1.71.2.1       mjf void
    538  1.71.2.1       mjf sysmon_envsys_destroy(struct sysmon_envsys *sme)
    539  1.71.2.1       mjf {
    540  1.71.2.1       mjf 	envsys_data_t *edata;
    541  1.71.2.1       mjf 
    542  1.71.2.1       mjf 	KASSERT(sme != NULL);
    543  1.71.2.1       mjf 
    544  1.71.2.1       mjf 	while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    545  1.71.2.1       mjf 		edata = TAILQ_FIRST(&sme->sme_sensors_list);
    546  1.71.2.1       mjf 		TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    547  1.71.2.1       mjf 	}
    548  1.71.2.1       mjf 
    549  1.71.2.1       mjf 	kmem_free(sme, sizeof(*sme));
    550  1.71.2.1       mjf }
    551  1.71.2.1       mjf 
    552  1.71.2.1       mjf /*
    553  1.71.2.1       mjf  * sysmon_envsys_sensor_attach:
    554  1.71.2.1       mjf  *
    555  1.71.2.1       mjf  * 	+ Attachs a sensor into a sysmon_envsys device checking that units
    556  1.71.2.1       mjf  * 	  is set to a valid type and description is unique and not empty.
    557  1.71.2.1       mjf  */
    558  1.71.2.1       mjf int
    559  1.71.2.1       mjf sysmon_envsys_sensor_attach(struct sysmon_envsys *sme, envsys_data_t *edata)
    560  1.71.2.1       mjf {
    561  1.71.2.1       mjf 	const struct sme_description_table *sdt_units;
    562  1.71.2.1       mjf 	envsys_data_t *oedata;
    563  1.71.2.1       mjf 	int i;
    564  1.71.2.1       mjf 
    565  1.71.2.1       mjf 	KASSERT(sme != NULL || edata != NULL);
    566  1.71.2.1       mjf 
    567  1.71.2.1       mjf 	/*
    568  1.71.2.1       mjf 	 * Find the correct units for this sensor.
    569  1.71.2.1       mjf 	 */
    570  1.71.2.1       mjf 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    571  1.71.2.1       mjf 	for (i = 0; sdt_units[i].type != -1; i++)
    572  1.71.2.1       mjf 		if (sdt_units[i].type == edata->units)
    573  1.71.2.1       mjf 			break;
    574  1.71.2.1       mjf 
    575  1.71.2.1       mjf 	if (strcmp(sdt_units[i].desc, "unknown") == 0)
    576  1.71.2.1       mjf 		return EINVAL;
    577  1.71.2.1       mjf 
    578  1.71.2.1       mjf 	/*
    579  1.71.2.1       mjf 	 * Check that description is not empty or duplicate.
    580  1.71.2.1       mjf 	 */
    581  1.71.2.1       mjf 	if (strlen(edata->desc) == 0)
    582  1.71.2.1       mjf 		return EINVAL;
    583  1.71.2.1       mjf 
    584  1.71.2.1       mjf 	mutex_enter(&sme_mtx);
    585  1.71.2.1       mjf 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    586  1.71.2.1       mjf 		if (strcmp(oedata->desc, edata->desc) == 0) {
    587  1.71.2.1       mjf 			mutex_exit(&sme_mtx);
    588  1.71.2.1       mjf 			return EEXIST;
    589  1.71.2.1       mjf 		}
    590  1.71.2.1       mjf 	}
    591  1.71.2.1       mjf 	/*
    592  1.71.2.1       mjf 	 * Ok, the sensor has been added into the device queue.
    593  1.71.2.1       mjf 	 */
    594  1.71.2.1       mjf 	TAILQ_INSERT_TAIL(&sme->sme_sensors_list, edata, sensors_head);
    595  1.71.2.1       mjf 
    596  1.71.2.1       mjf 	/*
    597  1.71.2.1       mjf 	 * Give the sensor a index position.
    598  1.71.2.1       mjf 	 */
    599  1.71.2.1       mjf 	edata->sensor = sme->sme_nsensors;
    600  1.71.2.1       mjf 	sme->sme_nsensors++;
    601  1.71.2.1       mjf 	mutex_exit(&sme_mtx);
    602  1.71.2.1       mjf 
    603  1.71.2.1       mjf 	return 0;
    604  1.71.2.1       mjf }
    605  1.71.2.1       mjf 
    606  1.71.2.1       mjf /*
    607  1.71.2.1       mjf  * sysmon_envsys_sensor_detach:
    608  1.71.2.1       mjf  *
    609  1.71.2.1       mjf  * 	+ Detachs a sensor from a sysmon_envsys device and decrements the
    610  1.71.2.1       mjf  * 	  sensors count on success.
    611  1.71.2.1       mjf  */
    612  1.71.2.1       mjf int
    613  1.71.2.1       mjf sysmon_envsys_sensor_detach(struct sysmon_envsys *sme, envsys_data_t *edata)
    614  1.71.2.1       mjf {
    615  1.71.2.1       mjf 	envsys_data_t *oedata;
    616  1.71.2.1       mjf 	bool found = false;
    617  1.71.2.1       mjf 
    618  1.71.2.1       mjf 	KASSERT(sme != NULL || edata != NULL);
    619  1.71.2.1       mjf 
    620  1.71.2.1       mjf 	/*
    621  1.71.2.1       mjf 	 * Check the sensor is already on the list.
    622  1.71.2.1       mjf 	 */
    623  1.71.2.1       mjf 	mutex_enter(&sme_mtx);
    624  1.71.2.1       mjf 	TAILQ_FOREACH(oedata, &sme->sme_sensors_list, sensors_head) {
    625  1.71.2.1       mjf 		if (oedata->sensor == edata->sensor) {
    626  1.71.2.1       mjf 			found = true;
    627  1.71.2.1       mjf 			break;
    628  1.71.2.1       mjf 		}
    629  1.71.2.1       mjf 	}
    630  1.71.2.1       mjf 
    631  1.71.2.1       mjf 	if (!found) {
    632  1.71.2.1       mjf 		mutex_exit(&sme_mtx);
    633  1.71.2.1       mjf 		return EINVAL;
    634  1.71.2.1       mjf 	}
    635  1.71.2.1       mjf 
    636  1.71.2.1       mjf 	/*
    637  1.71.2.1       mjf 	 * remove it and decrement the sensors count.
    638  1.71.2.1       mjf 	 */
    639  1.71.2.1       mjf 	TAILQ_REMOVE(&sme->sme_sensors_list, edata, sensors_head);
    640  1.71.2.1       mjf 	sme->sme_nsensors--;
    641  1.71.2.1       mjf 	mutex_exit(&sme_mtx);
    642  1.71.2.1       mjf 
    643  1.71.2.1       mjf 	return 0;
    644  1.71.2.1       mjf }
    645  1.71.2.1       mjf 
    646  1.71.2.1       mjf 
    647  1.71.2.1       mjf /*
    648       1.1   thorpej  * sysmon_envsys_register:
    649       1.1   thorpej  *
    650      1.52   xtraeme  *	+ Register a sysmon envsys device.
    651      1.52   xtraeme  *	+ Create array of dictionaries for a device.
    652       1.1   thorpej  */
    653       1.1   thorpej int
    654       1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    655       1.1   thorpej {
    656      1.58   xtraeme 	struct sme_evdrv {
    657      1.58   xtraeme 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    658      1.58   xtraeme 		sme_event_drv_t *evdrv;
    659      1.58   xtraeme 	};
    660      1.58   xtraeme 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    661      1.58   xtraeme 	struct sme_evdrv *sme_evdrv = NULL;
    662      1.18   xtraeme 	struct sysmon_envsys *lsme;
    663  1.71.2.1       mjf 	prop_dictionary_t dict, dict2;
    664      1.47   xtraeme 	prop_array_t array;
    665      1.47   xtraeme 	envsys_data_t *edata = NULL;
    666      1.47   xtraeme 	int i, error = 0;
    667       1.1   thorpej 
    668      1.47   xtraeme 	KASSERT(sme != NULL);
    669      1.47   xtraeme 	KASSERT(sme->sme_name != NULL);
    670      1.18   xtraeme 
    671      1.47   xtraeme 	/*
    672  1.71.2.1       mjf 	 * sanity check: if SME_DISABLE_REFRESH is not set,
    673  1.71.2.1       mjf 	 * the sme_refresh function callback must be non NULL.
    674      1.18   xtraeme 	 */
    675  1.71.2.1       mjf 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
    676  1.71.2.1       mjf 		if (!sme->sme_refresh)
    677  1.71.2.1       mjf 			return EINVAL;
    678  1.71.2.1       mjf 
    679      1.18   xtraeme 	/*
    680  1.71.2.1       mjf 	 * If the list of sensors is empty, there's no point to continue...
    681      1.18   xtraeme 	 */
    682  1.71.2.1       mjf 	if (TAILQ_EMPTY(&sme->sme_sensors_list)) {
    683  1.71.2.1       mjf 		DPRINTF(("%s: sensors list empty for %s\n", __func__,
    684  1.71.2.1       mjf 		    sme->sme_name));
    685  1.71.2.1       mjf 		return ENOTSUP;
    686       1.1   thorpej 	}
    687       1.1   thorpej 
    688  1.71.2.1       mjf 	/*
    689  1.71.2.1       mjf 	 * create the device array.
    690  1.71.2.1       mjf 	 */
    691      1.47   xtraeme 	array = prop_array_create();
    692      1.69   xtraeme 	if (!array)
    693      1.47   xtraeme 		return ENOMEM;
    694      1.47   xtraeme 
    695      1.58   xtraeme 	/*
    696      1.58   xtraeme 	 * Initialize the singly linked list for driver events.
    697      1.58   xtraeme 	 */
    698      1.58   xtraeme 	SLIST_INIT(&sme_evdrv_list);
    699  1.71.2.1       mjf 
    700      1.47   xtraeme 	/*
    701  1.71.2.1       mjf 	 * Iterate over all sensors and create a dictionary per sensor.
    702  1.71.2.1       mjf 	 * We must respect the order in which the sensors were added.
    703      1.47   xtraeme 	 */
    704  1.71.2.1       mjf 	for (edata = TAILQ_FIRST(&sme->sme_sensors_list); edata;
    705  1.71.2.1       mjf 	     edata = TAILQ_NEXT(edata, sensors_head)) {
    706      1.53   xtraeme 		dict = prop_dictionary_create();
    707      1.69   xtraeme 		if (!dict) {
    708      1.53   xtraeme 			error = ENOMEM;
    709      1.53   xtraeme 			goto out2;
    710      1.53   xtraeme 		}
    711      1.53   xtraeme 
    712      1.48   xtraeme 		/*
    713      1.48   xtraeme 		 * Create all objects in sensor's dictionary.
    714      1.48   xtraeme 		 */
    715      1.58   xtraeme 		sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
    716      1.58   xtraeme 		sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
    717      1.58   xtraeme 					array, dict, edata);
    718      1.58   xtraeme 		if (sme_evdrv->evdrv)
    719      1.58   xtraeme 			SLIST_INSERT_HEAD(&sme_evdrv_list,
    720      1.58   xtraeme 					  sme_evdrv, evdrv_head);
    721      1.58   xtraeme 	}
    722      1.58   xtraeme 
    723      1.58   xtraeme 	/*
    724      1.58   xtraeme 	 * Check if requested sysmon_envsys device is valid
    725      1.58   xtraeme 	 * and does not exist already in the list.
    726      1.58   xtraeme 	 */
    727      1.58   xtraeme 	mutex_enter(&sme_mtx);
    728      1.58   xtraeme 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    729      1.58   xtraeme 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    730      1.58   xtraeme 		       error = EEXIST;
    731      1.58   xtraeme 		       goto out;
    732      1.58   xtraeme 	       }
    733      1.47   xtraeme 	}
    734      1.27   xtraeme 
    735      1.47   xtraeme 	/*
    736      1.47   xtraeme 	 * If the array does not contain any object (sensor), there's
    737      1.47   xtraeme 	 * no need to attach the driver.
    738      1.47   xtraeme 	 */
    739      1.47   xtraeme 	if (prop_array_count(array) == 0) {
    740      1.47   xtraeme 		error = EINVAL;
    741      1.47   xtraeme 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    742      1.47   xtraeme 		    sme->sme_name));
    743      1.47   xtraeme 		goto out;
    744      1.47   xtraeme 	}
    745  1.71.2.1       mjf 
    746  1.71.2.1       mjf 	/*
    747  1.71.2.1       mjf 	 * Add the dictionary for the global properties of this device.
    748  1.71.2.1       mjf 	 */
    749  1.71.2.1       mjf 	dict2 = prop_dictionary_create();
    750  1.71.2.1       mjf 	if (!dict2) {
    751  1.71.2.1       mjf 		error = ENOMEM;
    752  1.71.2.1       mjf 		goto out;
    753  1.71.2.1       mjf 	}
    754  1.71.2.1       mjf 
    755  1.71.2.1       mjf 	error = sme_add_property_dictionary(sme, array, dict2);
    756  1.71.2.1       mjf 	if (error) {
    757  1.71.2.1       mjf 		prop_object_release(dict2);
    758  1.71.2.1       mjf 		goto out;
    759  1.71.2.1       mjf 	}
    760  1.71.2.1       mjf 
    761      1.47   xtraeme 	/*
    762      1.47   xtraeme 	 * Add the array into the global dictionary for the driver.
    763      1.47   xtraeme 	 *
    764      1.47   xtraeme 	 * <dict>
    765      1.47   xtraeme 	 * 	<key>foo0</key>
    766      1.47   xtraeme 	 * 	<array>
    767      1.47   xtraeme 	 * 		...
    768      1.47   xtraeme 	 */
    769      1.47   xtraeme 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    770      1.53   xtraeme 		error = EINVAL;
    771      1.47   xtraeme 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    772      1.47   xtraeme 		    sme->sme_name));
    773      1.47   xtraeme 		goto out;
    774      1.47   xtraeme 	}
    775      1.47   xtraeme 	/*
    776      1.48   xtraeme 	 * Add the device into the list.
    777      1.47   xtraeme 	 */
    778      1.47   xtraeme 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    779      1.58   xtraeme 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    780      1.58   xtraeme 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    781      1.18   xtraeme out:
    782      1.58   xtraeme 	mutex_exit(&sme_mtx);
    783      1.56   xtraeme 
    784  1.71.2.1       mjf 	/*
    785  1.71.2.1       mjf 	 * No errors? register the events that were set in the driver.
    786  1.71.2.1       mjf 	 */
    787      1.56   xtraeme 	if (error == 0) {
    788      1.58   xtraeme 		i = 0;
    789      1.58   xtraeme 		SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
    790      1.58   xtraeme 			if (i == 0)
    791      1.58   xtraeme 				sysmon_task_queue_init();
    792      1.58   xtraeme 			sysmon_task_queue_sched(0,
    793      1.58   xtraeme 			    sme_event_drvadd, sme_evdrv->evdrv);
    794      1.58   xtraeme 		}
    795      1.56   xtraeme 		DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
    796      1.56   xtraeme 		    __func__, sme->sme_name, sme->sme_nsensors));
    797      1.56   xtraeme 	}
    798      1.62   xtraeme 
    799      1.47   xtraeme out2:
    800      1.58   xtraeme 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    801      1.58   xtraeme 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    802      1.58   xtraeme 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    803      1.58   xtraeme 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    804      1.58   xtraeme 	}
    805      1.69   xtraeme 	if (!error)
    806      1.62   xtraeme 		return 0;
    807      1.62   xtraeme 
    808  1.71.2.1       mjf 	/*
    809  1.71.2.1       mjf 	 * Ugh... something wasn't right; unregister all events and sensors
    810  1.71.2.1       mjf 	 * previously assigned and destroy the array with all its objects.
    811  1.71.2.1       mjf 	 */
    812      1.62   xtraeme 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    813      1.62   xtraeme 	    sme->sme_name, error));
    814      1.59   xtraeme 	if (error != EEXIST) {
    815      1.59   xtraeme 		mutex_enter(&sme_mtx);
    816  1.71.2.1       mjf 		sme_event_unregister_all(sme);
    817  1.71.2.1       mjf 		while (!TAILQ_EMPTY(&sme->sme_sensors_list)) {
    818  1.71.2.1       mjf 			edata = TAILQ_FIRST(&sme->sme_sensors_list);
    819  1.71.2.1       mjf 			TAILQ_REMOVE(&sme->sme_sensors_list, edata,
    820  1.71.2.1       mjf 			    sensors_head);
    821  1.71.2.1       mjf 		}
    822      1.59   xtraeme 		mutex_exit(&sme_mtx);
    823      1.59   xtraeme 	}
    824      1.53   xtraeme 	sysmon_envsys_destroy_plist(array);
    825      1.18   xtraeme 	return error;
    826       1.1   thorpej }
    827       1.1   thorpej 
    828       1.1   thorpej /*
    829      1.53   xtraeme  * sysmon_envsys_destroy_plist:
    830      1.53   xtraeme  *
    831      1.53   xtraeme  * 	+ Remove all objects from the array of dictionaries that is
    832      1.53   xtraeme  * 	  created in a sysmon envsys device.
    833      1.53   xtraeme  */
    834      1.53   xtraeme static void
    835      1.53   xtraeme sysmon_envsys_destroy_plist(prop_array_t array)
    836      1.53   xtraeme {
    837      1.63   xtraeme 	prop_object_iterator_t iter, iter2;
    838      1.53   xtraeme 	prop_dictionary_t dict;
    839      1.53   xtraeme 	prop_object_t obj;
    840      1.53   xtraeme 
    841      1.53   xtraeme 	KASSERT(array != NULL);
    842      1.53   xtraeme 
    843      1.67   xtraeme 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    844      1.63   xtraeme 	    prop_array_count(array)));
    845      1.63   xtraeme 
    846      1.53   xtraeme 	iter = prop_array_iterator(array);
    847      1.69   xtraeme 	if (!iter)
    848      1.53   xtraeme 		return;
    849      1.53   xtraeme 
    850      1.69   xtraeme 	while ((dict = prop_object_iterator_next(iter))) {
    851      1.63   xtraeme 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    852      1.53   xtraeme 		iter2 = prop_dictionary_iterator(dict);
    853      1.69   xtraeme 		if (!iter2)
    854      1.63   xtraeme 			goto out;
    855      1.67   xtraeme 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    856      1.63   xtraeme 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    857      1.67   xtraeme 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    858      1.65   xtraeme 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    859      1.63   xtraeme 			prop_dictionary_remove(dict,
    860      1.63   xtraeme 			    prop_dictionary_keysym_cstring_nocopy(obj));
    861      1.63   xtraeme 			prop_object_iterator_reset(iter2);
    862      1.63   xtraeme 		}
    863      1.63   xtraeme 		prop_object_iterator_release(iter2);
    864      1.67   xtraeme 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    865      1.63   xtraeme 		    __func__, prop_dictionary_count(dict)));
    866      1.53   xtraeme 		prop_object_release(dict);
    867      1.53   xtraeme 	}
    868      1.53   xtraeme 
    869      1.63   xtraeme out:
    870      1.53   xtraeme 	prop_object_iterator_release(iter);
    871      1.63   xtraeme 	prop_object_release(array);
    872      1.53   xtraeme }
    873      1.53   xtraeme 
    874      1.53   xtraeme /*
    875       1.1   thorpej  * sysmon_envsys_unregister:
    876       1.1   thorpej  *
    877      1.52   xtraeme  *	+ Unregister a sysmon envsys device.
    878       1.1   thorpej  */
    879       1.1   thorpej void
    880       1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    881       1.1   thorpej {
    882      1.53   xtraeme 	prop_array_t array;
    883      1.35   xtraeme 
    884      1.35   xtraeme 	KASSERT(sme != NULL);
    885       1.1   thorpej 
    886      1.58   xtraeme 	mutex_enter(&sme_mtx);
    887  1.71.2.1       mjf 	/*
    888  1.71.2.1       mjf 	 * Wait for device to be available.
    889  1.71.2.1       mjf 	 */
    890  1.71.2.1       mjf 	while (sme->sme_flags & SME_FLAG_BUSY)
    891      1.61   xtraeme 		cv_wait(&sme_cv, &sme_mtx);
    892      1.27   xtraeme 	/*
    893  1.71.2.1       mjf 	 * Wait for the callout to finish.
    894      1.27   xtraeme 	 */
    895  1.71.2.1       mjf 	mutex_enter(&sme_callout_mtx);
    896  1.71.2.1       mjf 	while (sme->sme_flags & SME_CALLOUT_BUSY)
    897  1.71.2.1       mjf 		cv_wait(&sme_callout_cv, &sme_callout_mtx);
    898  1.71.2.1       mjf 	mutex_exit(&sme_callout_mtx);
    899  1.71.2.1       mjf 	/*
    900  1.71.2.1       mjf 	 * Decrement global sensors counter (only useful for compatibility).
    901  1.71.2.1       mjf 	 */
    902  1.71.2.1       mjf 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    903      1.47   xtraeme 	/*
    904      1.47   xtraeme 	 * Unregister all events associated with this device.
    905      1.47   xtraeme 	 */
    906  1.71.2.1       mjf 	sme_event_unregister_all(sme);
    907      1.58   xtraeme 	LIST_REMOVE(sme, sme_list);
    908      1.58   xtraeme 	mutex_exit(&sme_mtx);
    909      1.47   xtraeme 	/*
    910      1.53   xtraeme 	 * Remove the device (and all its objects) from the global dictionary.
    911      1.47   xtraeme 	 */
    912      1.53   xtraeme 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    913      1.63   xtraeme 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
    914      1.63   xtraeme 		prop_dictionary_remove(sme_propd, sme->sme_name);
    915      1.53   xtraeme 		sysmon_envsys_destroy_plist(array);
    916      1.53   xtraeme 	}
    917  1.71.2.1       mjf 	/*
    918  1.71.2.1       mjf 	 * And finally destroy the sysmon_envsys object.
    919  1.71.2.1       mjf 	 */
    920  1.71.2.1       mjf 	sysmon_envsys_destroy(sme);
    921       1.1   thorpej }
    922       1.1   thorpej 
    923       1.1   thorpej /*
    924       1.1   thorpej  * sysmon_envsys_find:
    925       1.1   thorpej  *
    926  1.71.2.1       mjf  *	+ Find a sysmon envsys device and mark it as busy if found.
    927       1.1   thorpej  */
    928       1.1   thorpej struct sysmon_envsys *
    929      1.18   xtraeme sysmon_envsys_find(const char *name)
    930       1.1   thorpej {
    931       1.1   thorpej 	struct sysmon_envsys *sme;
    932       1.1   thorpej 
    933  1.71.2.1       mjf 	KASSERT(mutex_owned(&sme_mtx));
    934  1.71.2.1       mjf 
    935       1.7      yamt again:
    936      1.69   xtraeme 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    937       1.1   thorpej 	     sme = LIST_NEXT(sme, sme_list)) {
    938      1.18   xtraeme 			if (strcmp(sme->sme_name, name) == 0) {
    939      1.18   xtraeme 				if (sme->sme_flags & SME_FLAG_BUSY) {
    940      1.61   xtraeme 					cv_wait(&sme_cv, &sme_mtx);
    941      1.18   xtraeme 					goto again;
    942      1.18   xtraeme 				}
    943      1.18   xtraeme 				sme->sme_flags |= SME_FLAG_BUSY;
    944      1.18   xtraeme 				break;
    945      1.18   xtraeme 			}
    946      1.18   xtraeme 	}
    947      1.18   xtraeme 	return sme;
    948      1.18   xtraeme }
    949      1.18   xtraeme 
    950      1.18   xtraeme /*
    951  1.71.2.1       mjf  * sysmon_envsys_acquire:
    952  1.71.2.1       mjf  *
    953  1.71.2.1       mjf  * 	+ Acquire priviledge to a sysmon envsys device (locked).
    954  1.71.2.1       mjf  */
    955  1.71.2.1       mjf void
    956  1.71.2.1       mjf sysmon_envsys_acquire(struct sysmon_envsys *sme)
    957  1.71.2.1       mjf {
    958  1.71.2.1       mjf 	KASSERT(mutex_owned(&sme_mtx));
    959  1.71.2.1       mjf 
    960  1.71.2.1       mjf 	while (sme->sme_flags & SME_FLAG_BUSY)
    961  1.71.2.1       mjf 		cv_wait(&sme_cv, &sme_mtx);
    962  1.71.2.1       mjf 
    963  1.71.2.1       mjf 	sme->sme_flags |= SME_FLAG_BUSY;
    964  1.71.2.1       mjf }
    965  1.71.2.1       mjf 
    966  1.71.2.1       mjf /*
    967      1.18   xtraeme  * sysmon_envsys_release:
    968      1.18   xtraeme  *
    969  1.71.2.1       mjf  * 	+ Release a sysmon envsys device (locked).
    970      1.18   xtraeme  */
    971      1.18   xtraeme void
    972      1.18   xtraeme sysmon_envsys_release(struct sysmon_envsys *sme)
    973      1.18   xtraeme {
    974  1.71.2.1       mjf 	KASSERT(mutex_owned(&sme_mtx));
    975  1.71.2.1       mjf 
    976  1.71.2.1       mjf 	sme->sme_flags &= ~SME_FLAG_BUSY;
    977  1.71.2.1       mjf 	cv_broadcast(&sme_cv);
    978      1.18   xtraeme }
    979      1.18   xtraeme 
    980      1.18   xtraeme /* compatibility function */
    981      1.18   xtraeme struct sysmon_envsys *
    982      1.18   xtraeme sysmon_envsys_find_40(u_int idx)
    983      1.18   xtraeme {
    984      1.18   xtraeme 	struct sysmon_envsys *sme;
    985      1.18   xtraeme 
    986  1.71.2.1       mjf 	KASSERT(mutex_owned(&sme_mtx));
    987  1.71.2.1       mjf 
    988      1.69   xtraeme 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    989      1.18   xtraeme 	     sme = LIST_NEXT(sme, sme_list)) {
    990       1.1   thorpej 		if (idx >= sme->sme_fsensor &&
    991  1.71.2.1       mjf 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors)) {
    992  1.71.2.1       mjf 			sme->sme_flags |= SME_FLAG_BUSY;
    993      1.18   xtraeme 			break;
    994  1.71.2.1       mjf 		}
    995      1.18   xtraeme 	}
    996      1.18   xtraeme 	return sme;
    997      1.18   xtraeme }
    998      1.18   xtraeme 
    999      1.18   xtraeme /*
   1000      1.69   xtraeme  * sme_sensor_dictionary_get:
   1001      1.69   xtraeme  *
   1002  1.71.2.1       mjf  * 	+ Returns a dictionary of a device specified by its index
   1003  1.71.2.1       mjf  * 	  position.
   1004      1.69   xtraeme  */
   1005      1.69   xtraeme prop_dictionary_t
   1006      1.69   xtraeme sme_sensor_dictionary_get(prop_array_t array, const char *index)
   1007      1.69   xtraeme {
   1008      1.69   xtraeme 	prop_object_iterator_t iter;
   1009      1.69   xtraeme 	prop_dictionary_t dict;
   1010      1.69   xtraeme 	prop_object_t obj;
   1011      1.69   xtraeme 
   1012      1.69   xtraeme 	KASSERT(array != NULL || index != NULL);
   1013      1.69   xtraeme 
   1014      1.69   xtraeme 	iter = prop_array_iterator(array);
   1015      1.69   xtraeme 	if (!iter)
   1016      1.69   xtraeme 		return NULL;
   1017      1.69   xtraeme 
   1018      1.69   xtraeme 	while ((dict = prop_object_iterator_next(iter))) {
   1019      1.69   xtraeme 		obj = prop_dictionary_get(dict, "index");
   1020      1.69   xtraeme 		if (prop_string_equals_cstring(obj, index))
   1021      1.69   xtraeme 			break;
   1022      1.69   xtraeme 	}
   1023      1.69   xtraeme 
   1024      1.69   xtraeme 	prop_object_iterator_release(iter);
   1025      1.69   xtraeme 	return dict;
   1026      1.69   xtraeme }
   1027      1.69   xtraeme 
   1028      1.69   xtraeme /*
   1029      1.69   xtraeme  * sme_remove_userprops:
   1030      1.69   xtraeme  *
   1031      1.69   xtraeme  * 	+ Remove all properties from all devices that were set by
   1032  1.71.2.1       mjf  * 	  the ENVSYS_SETDICTIONARY ioctl.
   1033      1.69   xtraeme  */
   1034      1.69   xtraeme static void
   1035      1.69   xtraeme sme_remove_userprops(void)
   1036      1.69   xtraeme {
   1037      1.69   xtraeme 	struct sysmon_envsys *sme;
   1038      1.69   xtraeme 	prop_array_t array;
   1039      1.69   xtraeme 	prop_dictionary_t sdict;
   1040      1.69   xtraeme 	envsys_data_t *edata = NULL;
   1041      1.69   xtraeme 	char tmp[ENVSYS_DESCLEN];
   1042  1.71.2.1       mjf 	int ptype;
   1043      1.69   xtraeme 
   1044      1.69   xtraeme 	mutex_enter(&sme_mtx);
   1045      1.69   xtraeme 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
   1046  1.71.2.1       mjf 		sysmon_envsys_acquire(sme);
   1047      1.69   xtraeme 		array = prop_dictionary_get(sme_propd, sme->sme_name);
   1048      1.69   xtraeme 
   1049  1.71.2.1       mjf 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1050      1.69   xtraeme 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
   1051      1.69   xtraeme 				       edata->sensor);
   1052      1.69   xtraeme 			sdict = sme_sensor_dictionary_get(array, tmp);
   1053  1.71.2.1       mjf 			KASSERT(sdict != NULL);
   1054      1.69   xtraeme 
   1055      1.69   xtraeme 			if (edata->upropset & USERPROP_BATTCAP) {
   1056      1.69   xtraeme 				prop_dictionary_remove(sdict,
   1057      1.69   xtraeme 				    "critical-capacity");
   1058      1.69   xtraeme 				ptype = PENVSYS_EVENT_BATT_USERCAP;
   1059  1.71.2.1       mjf 				sme_event_unregister(sme, edata->desc, ptype);
   1060      1.69   xtraeme 			}
   1061      1.69   xtraeme 
   1062      1.69   xtraeme 			if (edata->upropset & USERPROP_CRITMAX) {
   1063      1.69   xtraeme 				prop_dictionary_remove(sdict,
   1064      1.69   xtraeme 				    "critical-max");
   1065      1.69   xtraeme 				ptype = PENVSYS_EVENT_USER_CRITMAX;
   1066  1.71.2.1       mjf 				sme_event_unregister(sme, edata->desc, ptype);
   1067      1.69   xtraeme 			}
   1068      1.69   xtraeme 
   1069      1.69   xtraeme 			if (edata->upropset & USERPROP_CRITMIN) {
   1070      1.69   xtraeme 				prop_dictionary_remove(sdict,
   1071      1.69   xtraeme 				    "critical-min");
   1072      1.69   xtraeme 				ptype = PENVSYS_EVENT_USER_CRITMIN;
   1073  1.71.2.1       mjf 				sme_event_unregister(sme, edata->desc, ptype);
   1074      1.69   xtraeme 			}
   1075      1.69   xtraeme 
   1076      1.69   xtraeme 			if (edata->upropset & USERPROP_RFACT) {
   1077      1.69   xtraeme 				(void)sme_sensor_upint32(sdict, "rfact", 0);
   1078      1.69   xtraeme 				edata->rfact = 0;
   1079      1.69   xtraeme 			}
   1080      1.69   xtraeme 
   1081      1.69   xtraeme 			if (edata->upropset & USERPROP_DESC)
   1082      1.69   xtraeme 				(void)sme_sensor_upstring(sdict,
   1083      1.69   xtraeme 			  	    "description", edata->desc);
   1084  1.71.2.1       mjf 
   1085  1.71.2.1       mjf 			if (edata->upropset)
   1086  1.71.2.1       mjf 				edata->upropset = 0;
   1087      1.69   xtraeme 		}
   1088      1.69   xtraeme 
   1089  1.71.2.1       mjf 		/*
   1090  1.71.2.1       mjf 		 * Restore default timeout value.
   1091  1.71.2.1       mjf 		 */
   1092  1.71.2.1       mjf 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1093  1.71.2.1       mjf 		sysmon_envsys_release(sme);
   1094      1.69   xtraeme 	}
   1095      1.69   xtraeme 	mutex_exit(&sme_mtx);
   1096      1.69   xtraeme }
   1097  1.71.2.1       mjf 
   1098      1.69   xtraeme /*
   1099  1.71.2.1       mjf  * sme_add_property_dictionary:
   1100  1.71.2.1       mjf  *
   1101  1.71.2.1       mjf  * 	+ Add global properties into a device.
   1102      1.27   xtraeme  */
   1103      1.27   xtraeme static int
   1104  1.71.2.1       mjf sme_add_property_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1105  1.71.2.1       mjf 			    prop_dictionary_t dict)
   1106      1.27   xtraeme {
   1107  1.71.2.1       mjf 	prop_dictionary_t pdict;
   1108  1.71.2.1       mjf 	int error = 0;
   1109      1.27   xtraeme 
   1110  1.71.2.1       mjf 	pdict = prop_dictionary_create();
   1111  1.71.2.1       mjf 	if (!pdict)
   1112  1.71.2.1       mjf 		return EINVAL;
   1113      1.35   xtraeme 
   1114  1.71.2.1       mjf 	/*
   1115  1.71.2.1       mjf 	 * Add the 'refresh-timeout' object into the 'device-properties'
   1116  1.71.2.1       mjf 	 * dictionary. We use by default 30 seconds.
   1117  1.71.2.1       mjf 	 *
   1118  1.71.2.1       mjf 	 * 	...
   1119  1.71.2.1       mjf 	 * 	<dict>
   1120  1.71.2.1       mjf 	 * 		<key>device-properties</key>
   1121  1.71.2.1       mjf 	 * 		<dict>
   1122  1.71.2.1       mjf 	 * 			<key>refresh-timeout</key>
   1123  1.71.2.1       mjf 	 * 			<integer>120</integer<
   1124  1.71.2.1       mjf 	 * 		</dict<
   1125  1.71.2.1       mjf 	 * 	</dict>
   1126  1.71.2.1       mjf 	 * 	...
   1127  1.71.2.1       mjf 	 *
   1128  1.71.2.1       mjf 	 */
   1129  1.71.2.1       mjf 	if (!sme->sme_events_timeout)
   1130  1.71.2.1       mjf 		sme->sme_events_timeout = SME_EVENTS_DEFTIMEOUT;
   1131  1.71.2.1       mjf 
   1132  1.71.2.1       mjf 	if (!prop_dictionary_set_uint64(pdict, "refresh-timeout",
   1133  1.71.2.1       mjf 					sme->sme_events_timeout)) {
   1134  1.71.2.1       mjf 		error = EINVAL;
   1135  1.71.2.1       mjf 		goto out;
   1136      1.27   xtraeme 	}
   1137      1.27   xtraeme 
   1138  1.71.2.1       mjf 	if (!prop_dictionary_set(dict, "device-properties", pdict)) {
   1139  1.71.2.1       mjf 		error = EINVAL;
   1140  1.71.2.1       mjf 		goto out;
   1141  1.71.2.1       mjf 	}
   1142      1.47   xtraeme 
   1143  1.71.2.1       mjf 	/*
   1144  1.71.2.1       mjf 	 * Add the device dictionary into the sysmon envsys array.
   1145  1.71.2.1       mjf 	 */
   1146  1.71.2.1       mjf 	if (!prop_array_add(array, dict))
   1147  1.71.2.1       mjf 		error = EINVAL;
   1148      1.27   xtraeme 
   1149  1.71.2.1       mjf out:
   1150  1.71.2.1       mjf 	prop_object_release(pdict);
   1151  1.71.2.1       mjf 	return error;
   1152      1.27   xtraeme }
   1153      1.27   xtraeme 
   1154      1.27   xtraeme /*
   1155      1.52   xtraeme  * sme_add_sensor_dictionary:
   1156      1.18   xtraeme  *
   1157  1.71.2.1       mjf  * 	+ Adds the sensor objects into the dictionary and returns a pointer
   1158  1.71.2.1       mjf  * 	  to a sme_event_drv_t object if a monitoring flag was set
   1159  1.71.2.1       mjf  * 	  (or NULL otherwise).
   1160      1.18   xtraeme  */
   1161      1.58   xtraeme sme_event_drv_t *
   1162      1.47   xtraeme sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
   1163      1.47   xtraeme 		    	  prop_dictionary_t dict, envsys_data_t *edata)
   1164      1.18   xtraeme {
   1165      1.55   xtraeme 	const struct sme_description_table *sdt, *sdt_units;
   1166      1.18   xtraeme 	sme_event_drv_t *sme_evdrv_t = NULL;
   1167      1.41   xtraeme 	int i, j;
   1168      1.69   xtraeme 	char indexstr[ENVSYS_DESCLEN];
   1169      1.18   xtraeme 
   1170  1.71.2.1       mjf 	/*
   1171  1.71.2.1       mjf 	 * Find the correct units for this sensor.
   1172  1.71.2.1       mjf 	 */
   1173      1.55   xtraeme 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
   1174      1.55   xtraeme 	for (i = 0; sdt_units[i].type != -1; i++)
   1175      1.55   xtraeme 		if (sdt_units[i].type == edata->units)
   1176      1.18   xtraeme 			break;
   1177      1.18   xtraeme 
   1178      1.18   xtraeme 	/*
   1179      1.69   xtraeme 	 * Add the index sensor string.
   1180      1.69   xtraeme 	 *
   1181      1.69   xtraeme 	 * 		...
   1182  1.71.2.1       mjf 	 * 		<key>index</eyr
   1183      1.69   xtraeme 	 * 		<string>sensor0</string>
   1184      1.69   xtraeme 	 * 		...
   1185      1.69   xtraeme 	 */
   1186      1.69   xtraeme 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
   1187      1.69   xtraeme 	if (sme_sensor_upstring(dict, "index", indexstr))
   1188  1.71.2.1       mjf 		goto bad;
   1189      1.69   xtraeme 
   1190      1.69   xtraeme 	/*
   1191      1.18   xtraeme 	 * 		...
   1192      1.18   xtraeme 	 * 		<key>type</key>
   1193      1.18   xtraeme 	 * 		<string>foo</string>
   1194      1.18   xtraeme 	 * 		<key>description</key>
   1195      1.18   xtraeme 	 * 		<string>blah blah</string>
   1196      1.18   xtraeme 	 * 		...
   1197      1.18   xtraeme 	 */
   1198      1.55   xtraeme 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
   1199  1.71.2.1       mjf 		goto bad;
   1200      1.30   xtraeme 
   1201      1.41   xtraeme 	if (sme_sensor_upstring(dict, "description", edata->desc))
   1202  1.71.2.1       mjf 		goto bad;
   1203      1.18   xtraeme 
   1204      1.18   xtraeme 	/*
   1205      1.18   xtraeme 	 * Add sensor's state description.
   1206      1.18   xtraeme 	 *
   1207      1.18   xtraeme 	 * 		...
   1208      1.18   xtraeme 	 * 		<key>state</key>
   1209      1.18   xtraeme 	 * 		<string>valid</string>
   1210      1.18   xtraeme 	 * 		...
   1211      1.18   xtraeme 	 */
   1212      1.55   xtraeme 	sdt = sme_get_description_table(SME_DESC_STATES);
   1213      1.55   xtraeme 	for (j = 0; sdt[j].type != -1; j++)
   1214      1.55   xtraeme 		if (sdt[j].type == edata->state)
   1215       1.7      yamt 			break;
   1216      1.18   xtraeme 
   1217      1.41   xtraeme 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
   1218      1.41   xtraeme 	    __func__, edata->desc, edata->units, edata->state));
   1219      1.41   xtraeme 
   1220      1.55   xtraeme 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
   1221  1.71.2.1       mjf 		goto bad;
   1222      1.18   xtraeme 
   1223      1.18   xtraeme 	/*
   1224      1.18   xtraeme 	 * Add the monitoring boolean object:
   1225      1.18   xtraeme 	 *
   1226      1.18   xtraeme 	 * 		...
   1227      1.18   xtraeme 	 * 		<key>monitoring-supported</key>
   1228      1.18   xtraeme 	 * 		<true/>
   1229      1.18   xtraeme 	 *		...
   1230      1.18   xtraeme 	 *
   1231      1.71   xtraeme 	 * always false on Battery {capacity,charge}, Drive and Indicator types.
   1232      1.54   xtraeme 	 * They cannot be monitored.
   1233      1.18   xtraeme 	 *
   1234      1.18   xtraeme 	 */
   1235      1.18   xtraeme 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1236      1.18   xtraeme 	    (edata->units == ENVSYS_INDICATOR) ||
   1237      1.54   xtraeme 	    (edata->units == ENVSYS_DRIVE) ||
   1238      1.71   xtraeme 	    (edata->units == ENVSYS_BATTERY_CAPACITY) ||
   1239      1.71   xtraeme 	    (edata->units == ENVSYS_BATTERY_CHARGE)) {
   1240      1.41   xtraeme 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
   1241      1.58   xtraeme 			goto out;
   1242      1.18   xtraeme 	} else {
   1243      1.41   xtraeme 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
   1244      1.58   xtraeme 			goto out;
   1245      1.18   xtraeme 	}
   1246      1.18   xtraeme 
   1247      1.18   xtraeme 	/*
   1248      1.69   xtraeme 	 * Add the percentage boolean object, true if ENVSYS_FPERCENT
   1249      1.69   xtraeme 	 * is set or false otherwise.
   1250      1.54   xtraeme 	 *
   1251      1.54   xtraeme 	 * 		...
   1252      1.55   xtraeme 	 * 		<key>want-percentage</key>
   1253      1.55   xtraeme 	 * 		<true/>
   1254      1.54   xtraeme 	 * 		...
   1255      1.54   xtraeme 	 */
   1256      1.55   xtraeme 	if (edata->flags & ENVSYS_FPERCENT)
   1257      1.55   xtraeme 		if (sme_sensor_upbool(dict, "want-percentage", true))
   1258      1.58   xtraeme 			goto out;
   1259      1.54   xtraeme 
   1260      1.54   xtraeme 	/*
   1261      1.69   xtraeme 	 * Add the allow-rfact boolean object, true if
   1262      1.69   xtraeme 	 * ENVSYS_FCHANGERFACT if set or false otherwise.
   1263      1.69   xtraeme 	 *
   1264      1.69   xtraeme 	 * 		...
   1265      1.69   xtraeme 	 * 		<key>allow-rfact</key>
   1266      1.69   xtraeme 	 * 		<true/>
   1267      1.69   xtraeme 	 * 		...
   1268      1.69   xtraeme 	 */
   1269      1.69   xtraeme 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1270      1.69   xtraeme 	    edata->units == ENVSYS_SVOLTS_AC) {
   1271      1.69   xtraeme 		if (edata->flags & ENVSYS_FCHANGERFACT) {
   1272      1.69   xtraeme 			if (sme_sensor_upbool(dict, "allow-rfact", true))
   1273      1.69   xtraeme 				goto out;
   1274      1.69   xtraeme 		} else {
   1275      1.69   xtraeme 			if (sme_sensor_upbool(dict, "allow-rfact", false))
   1276      1.69   xtraeme 				goto out;
   1277      1.69   xtraeme 		}
   1278      1.69   xtraeme 	}
   1279      1.69   xtraeme 
   1280      1.69   xtraeme 	/*
   1281      1.71   xtraeme 	 * Add the object for battery capacity sensors:
   1282      1.54   xtraeme 	 *
   1283      1.54   xtraeme 	 * 		...
   1284      1.71   xtraeme 	 * 		<key>battery-capacity</key>
   1285      1.55   xtraeme 	 * 		<string>NORMAL</string>
   1286      1.54   xtraeme 	 * 		...
   1287      1.54   xtraeme 	 */
   1288      1.71   xtraeme 	if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1289      1.71   xtraeme 		sdt = sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1290      1.55   xtraeme 		for (j = 0; sdt[j].type != -1; j++)
   1291      1.55   xtraeme 			if (sdt[j].type == edata->value_cur)
   1292      1.55   xtraeme 				break;
   1293      1.55   xtraeme 
   1294      1.71   xtraeme 		if (sme_sensor_upstring(dict, "battery-capacity", sdt[j].desc))
   1295      1.58   xtraeme 			goto out;
   1296      1.55   xtraeme 	}
   1297      1.54   xtraeme 
   1298      1.54   xtraeme 	/*
   1299      1.18   xtraeme 	 * Add the drive-state object for drive sensors:
   1300      1.18   xtraeme 	 *
   1301      1.18   xtraeme 	 * 		...
   1302      1.18   xtraeme 	 * 		<key>drive-state</key>
   1303      1.18   xtraeme 	 * 		<string>drive is online</string>
   1304      1.18   xtraeme 	 * 		...
   1305      1.18   xtraeme 	 */
   1306      1.18   xtraeme 	if (edata->units == ENVSYS_DRIVE) {
   1307      1.55   xtraeme 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1308      1.55   xtraeme 		for (j = 0; sdt[j].type != -1; j++)
   1309      1.55   xtraeme 			if (sdt[j].type == edata->value_cur)
   1310      1.18   xtraeme 				break;
   1311      1.30   xtraeme 
   1312      1.55   xtraeme 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
   1313      1.58   xtraeme 			goto out;
   1314      1.18   xtraeme 	}
   1315      1.18   xtraeme 
   1316  1.71.2.1       mjf 	/*
   1317  1.71.2.1       mjf 	 * Add the following objects if sensor is enabled...
   1318      1.54   xtraeme 	 */
   1319      1.18   xtraeme 	if (edata->state == ENVSYS_SVALID) {
   1320      1.18   xtraeme 		/*
   1321  1.71.2.1       mjf 		 * Add the following objects:
   1322  1.71.2.1       mjf 		 *
   1323      1.18   xtraeme 		 * 	...
   1324      1.18   xtraeme 		 * 	<key>rpms</key>
   1325      1.18   xtraeme 		 * 	<integer>2500</integer>
   1326      1.18   xtraeme 		 * 	<key>rfact</key>
   1327      1.18   xtraeme 		 * 	<integer>10000</integer>
   1328      1.18   xtraeme 		 * 	<key>cur-value</key>
   1329  1.71.2.1       mjf 	 	 * 	<integer>1250</integer>
   1330  1.71.2.1       mjf 	 	 * 	<key>min-value</key>
   1331  1.71.2.1       mjf 	 	 * 	<integer>800</integer>
   1332  1.71.2.1       mjf 	 	 * 	<key>max-value</integer>
   1333  1.71.2.1       mjf 	 	 * 	<integer>3000</integer>
   1334  1.71.2.1       mjf 	 	 * 	<key>avg-value</integer>
   1335  1.71.2.1       mjf 	 	 * 	<integer>1400</integer>
   1336  1.71.2.1       mjf 	 	 * 	...
   1337  1.71.2.1       mjf 	 	 */
   1338      1.41   xtraeme 		if (edata->units == ENVSYS_SFANRPM)
   1339      1.41   xtraeme 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
   1340      1.58   xtraeme 				goto out;
   1341      1.18   xtraeme 
   1342      1.41   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1343  1.71.2.1       mjf 	    	    edata->units == ENVSYS_SVOLTS_DC)
   1344      1.41   xtraeme 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
   1345      1.58   xtraeme 				goto out;
   1346      1.18   xtraeme 
   1347      1.41   xtraeme 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
   1348      1.58   xtraeme 			goto out;
   1349      1.30   xtraeme 
   1350      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1351      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1352      1.41   xtraeme 					       "min-value",
   1353      1.41   xtraeme 					       edata->value_min))
   1354  1.71.2.1       mjf 			goto out;
   1355      1.30   xtraeme 		}
   1356      1.30   xtraeme 
   1357      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1358      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1359      1.41   xtraeme 					       "max-value",
   1360      1.41   xtraeme 					       edata->value_max))
   1361  1.71.2.1       mjf 			goto out;
   1362      1.30   xtraeme 		}
   1363      1.30   xtraeme 
   1364      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1365      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1366      1.41   xtraeme 					       "avg-value",
   1367  1.71.2.1       mjf 					       edata->value_avg))
   1368  1.71.2.1       mjf 			goto out;
   1369      1.30   xtraeme 		}
   1370      1.18   xtraeme 	}
   1371      1.18   xtraeme 
   1372      1.18   xtraeme 	/*
   1373      1.18   xtraeme 	 * 	...
   1374  1.71.2.1       mjf 	 * </dict>
   1375      1.47   xtraeme 	 *
   1376      1.47   xtraeme 	 * Add the dictionary into the array.
   1377      1.47   xtraeme 	 *
   1378      1.18   xtraeme 	 */
   1379  1.71.2.1       mjf 	if (!prop_array_add(array, dict)) {
   1380      1.41   xtraeme 		DPRINTF(("%s: prop_array_add\n", __func__));
   1381  1.71.2.1       mjf 		goto bad;
   1382      1.41   xtraeme 	}
   1383      1.41   xtraeme 
   1384      1.41   xtraeme 	/*
   1385  1.71.2.1       mjf 	 * Register a new event if a monitoring flag was set.
   1386      1.41   xtraeme 	 */
   1387      1.41   xtraeme 	if (edata->monitor) {
   1388      1.58   xtraeme 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1389  1.71.2.1       mjf 		sme_evdrv_t->sed_sdict = dict;
   1390  1.71.2.1       mjf 		sme_evdrv_t->sed_edata = edata;
   1391  1.71.2.1       mjf 		sme_evdrv_t->sed_sme = sme;
   1392  1.71.2.1       mjf 		sme_evdrv_t->sed_powertype = sdt_units[i].crittype;
   1393      1.41   xtraeme 	}
   1394      1.41   xtraeme 
   1395      1.58   xtraeme out:
   1396      1.58   xtraeme 	return sme_evdrv_t;
   1397      1.41   xtraeme 
   1398  1.71.2.1       mjf bad:
   1399      1.68   xtraeme 	prop_object_release(dict);
   1400  1.71.2.1       mjf 	return NULL;
   1401      1.18   xtraeme }
   1402      1.18   xtraeme 
   1403      1.18   xtraeme /*
   1404      1.18   xtraeme  * sme_update_dictionary:
   1405      1.18   xtraeme  *
   1406      1.18   xtraeme  * 	+ Update per-sensor dictionaries with new values if there were
   1407      1.18   xtraeme  * 	  changes, otherwise the object in dictionary is untouched.
   1408      1.18   xtraeme  */
   1409      1.18   xtraeme int
   1410      1.18   xtraeme sme_update_dictionary(struct sysmon_envsys *sme)
   1411      1.18   xtraeme {
   1412      1.55   xtraeme 	const struct sme_description_table *sdt;
   1413      1.55   xtraeme 	envsys_data_t *edata;
   1414  1.71.2.1       mjf 	prop_object_t array, dict, obj, obj2;
   1415  1.71.2.1       mjf 	int j, error = 0;
   1416      1.18   xtraeme 
   1417      1.58   xtraeme 	KASSERT(mutex_owned(&sme_mtx));
   1418      1.50   xtraeme 
   1419  1.71.2.1       mjf 	/*
   1420  1.71.2.1       mjf 	 * Retrieve the array of dictionaries in device.
   1421  1.71.2.1       mjf 	 */
   1422      1.18   xtraeme 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1423      1.24   xtraeme 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1424      1.24   xtraeme 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1425      1.18   xtraeme 		return EINVAL;
   1426      1.24   xtraeme 	}
   1427      1.18   xtraeme 
   1428  1.71.2.1       mjf 	/*
   1429  1.71.2.1       mjf 	 * Get the last dictionary on the array, this contains the
   1430  1.71.2.1       mjf 	 * 'device-properties' sub-dictionary.
   1431  1.71.2.1       mjf 	 */
   1432  1.71.2.1       mjf 	obj = prop_array_get(array, prop_array_count(array) - 1);
   1433  1.71.2.1       mjf 	if (!obj || prop_object_type(obj) != PROP_TYPE_DICTIONARY) {
   1434  1.71.2.1       mjf 		DPRINTF(("%s: not a device-properties dictionary\n", __func__));
   1435  1.71.2.1       mjf 		return EINVAL;
   1436  1.71.2.1       mjf 	}
   1437  1.71.2.1       mjf 
   1438  1.71.2.1       mjf 	obj2 = prop_dictionary_get(obj, "device-properties");
   1439  1.71.2.1       mjf 	if (!obj2)
   1440  1.71.2.1       mjf 		return EINVAL;
   1441  1.71.2.1       mjf 
   1442  1.71.2.1       mjf 	/*
   1443  1.71.2.1       mjf 	 * Update the 'refresh-timeout' property.
   1444  1.71.2.1       mjf 	 */
   1445  1.71.2.1       mjf 	if (!prop_dictionary_set_uint64(obj2, "refresh-timeout",
   1446  1.71.2.1       mjf 					sme->sme_events_timeout))
   1447  1.71.2.1       mjf 		return EINVAL;
   1448  1.71.2.1       mjf 
   1449      1.18   xtraeme 	/*
   1450      1.18   xtraeme 	 * - iterate over all sensors.
   1451      1.18   xtraeme 	 * - fetch new data.
   1452      1.18   xtraeme 	 * - check if data in dictionary is different than new data.
   1453      1.18   xtraeme 	 * - update dictionary if there were changes.
   1454      1.18   xtraeme 	 */
   1455      1.56   xtraeme 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1456      1.56   xtraeme 	    sme->sme_name, sme->sme_nsensors));
   1457      1.56   xtraeme 
   1458  1.71.2.1       mjf 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1459      1.18   xtraeme 		/*
   1460  1.71.2.1       mjf 		 * refresh sensor data via sme_refresh only if the
   1461      1.18   xtraeme 		 * flag is not set.
   1462      1.18   xtraeme 		 */
   1463  1.71.2.1       mjf 		if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
   1464  1.71.2.1       mjf 			(*sme->sme_refresh)(sme, edata);
   1465      1.18   xtraeme 
   1466  1.71.2.1       mjf 		/*
   1467  1.71.2.1       mjf 		 * retrieve sensor's dictionary.
   1468  1.71.2.1       mjf 		 */
   1469  1.71.2.1       mjf 		dict = prop_array_get(array, edata->sensor);
   1470      1.24   xtraeme 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1471      1.24   xtraeme 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1472      1.24   xtraeme 			    __func__, edata->sensor, sme->sme_name));
   1473      1.18   xtraeme 			return EINVAL;
   1474      1.24   xtraeme 		}
   1475      1.18   xtraeme 
   1476  1.71.2.1       mjf 		/*
   1477  1.71.2.1       mjf 		 * update sensor's state.
   1478  1.71.2.1       mjf 		 */
   1479      1.55   xtraeme 		sdt = sme_get_description_table(SME_DESC_STATES);
   1480      1.55   xtraeme 		for (j = 0; sdt[j].type != -1; j++)
   1481      1.55   xtraeme 			if (sdt[j].type == edata->state)
   1482      1.18   xtraeme 				break;
   1483      1.18   xtraeme 
   1484      1.18   xtraeme 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1485      1.55   xtraeme 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1486      1.55   xtraeme 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1487      1.18   xtraeme 
   1488      1.55   xtraeme 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1489      1.30   xtraeme 		if (error)
   1490      1.30   xtraeme 			break;
   1491      1.18   xtraeme 
   1492  1.71.2.1       mjf 		/*
   1493  1.71.2.1       mjf 		 * update sensor's type.
   1494  1.71.2.1       mjf 		 */
   1495      1.55   xtraeme 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1496      1.55   xtraeme 		for (j = 0; sdt[j].type != -1; j++)
   1497      1.55   xtraeme 			if (sdt[j].type == edata->units)
   1498      1.45   xtraeme 				break;
   1499      1.45   xtraeme 
   1500      1.55   xtraeme 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1501      1.45   xtraeme 		if (error)
   1502      1.45   xtraeme 			break;
   1503      1.45   xtraeme 
   1504  1.71.2.1       mjf 		/*
   1505  1.71.2.1       mjf 		 * update sensor's current value.
   1506  1.71.2.1       mjf 		 */
   1507      1.34   xtraeme 		error = sme_sensor_upint32(dict,
   1508      1.30   xtraeme 					   "cur-value",
   1509      1.30   xtraeme 					   edata->value_cur);
   1510      1.30   xtraeme 		if (error)
   1511      1.30   xtraeme 			break;
   1512      1.18   xtraeme 
   1513      1.18   xtraeme 		/*
   1514      1.71   xtraeme 		 * Battery charge, Integer and Indicator types do not
   1515      1.71   xtraeme 		 * need the following objects, so skip them.
   1516      1.18   xtraeme 		 */
   1517      1.18   xtraeme 		if (edata->units == ENVSYS_INTEGER ||
   1518      1.71   xtraeme 		    edata->units == ENVSYS_INDICATOR ||
   1519      1.71   xtraeme 		    edata->units == ENVSYS_BATTERY_CHARGE)
   1520      1.18   xtraeme 			continue;
   1521      1.18   xtraeme 
   1522  1.71.2.1       mjf 		/*
   1523  1.71.2.1       mjf 		 * update sensor flags.
   1524  1.71.2.1       mjf 		 */
   1525      1.30   xtraeme 		if (edata->flags & ENVSYS_FPERCENT) {
   1526      1.34   xtraeme 			error = sme_sensor_upbool(dict,
   1527      1.30   xtraeme 						  "want-percentage",
   1528      1.30   xtraeme 						  true);
   1529      1.30   xtraeme 			if (error)
   1530      1.30   xtraeme 				break;
   1531      1.18   xtraeme 		}
   1532      1.18   xtraeme 
   1533  1.71.2.1       mjf 		/*
   1534  1.71.2.1       mjf 		 * update sensor's {avg,max,min}-value.
   1535  1.71.2.1       mjf 		 */
   1536      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1537      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1538      1.30   xtraeme 						   "max-value",
   1539      1.30   xtraeme 						   edata->value_max);
   1540      1.30   xtraeme 			if (error)
   1541      1.30   xtraeme 				break;
   1542      1.30   xtraeme 		}
   1543      1.30   xtraeme 
   1544      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1545      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1546      1.30   xtraeme 						   "min-value",
   1547      1.30   xtraeme 						   edata->value_min);
   1548      1.30   xtraeme 			if (error)
   1549      1.30   xtraeme 				break;
   1550      1.30   xtraeme 		}
   1551      1.18   xtraeme 
   1552      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1553      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1554      1.30   xtraeme 						   "avg-value",
   1555      1.30   xtraeme 						   edata->value_avg);
   1556      1.30   xtraeme 			if (error)
   1557      1.30   xtraeme 				break;
   1558      1.30   xtraeme 		}
   1559      1.18   xtraeme 
   1560  1.71.2.1       mjf 		/*
   1561  1.71.2.1       mjf 		 * update 'rpms' only for ENVSYS_SFANRPM sensors.
   1562  1.71.2.1       mjf 		 */
   1563      1.30   xtraeme 		if (edata->units == ENVSYS_SFANRPM) {
   1564      1.34   xtraeme 			error = sme_sensor_upuint32(dict,
   1565      1.30   xtraeme 						    "rpms",
   1566      1.30   xtraeme 						    edata->rpms);
   1567      1.30   xtraeme 			if (error)
   1568      1.30   xtraeme 				break;
   1569      1.30   xtraeme 		}
   1570      1.18   xtraeme 
   1571  1.71.2.1       mjf 		/*
   1572  1.71.2.1       mjf 		 * update 'rfact' only for ENVSYS_SVOLTS_[AD]C sensors.
   1573  1.71.2.1       mjf 		 */
   1574      1.18   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1575      1.18   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC) {
   1576      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1577      1.30   xtraeme 						   "rfact",
   1578      1.30   xtraeme 						   edata->rfact);
   1579      1.30   xtraeme 			if (error)
   1580      1.30   xtraeme 				break;
   1581      1.18   xtraeme 		}
   1582      1.18   xtraeme 
   1583  1.71.2.1       mjf 		/*
   1584  1.71.2.1       mjf 		 * update 'drive-state' only for ENVSYS_DRIVE sensors.
   1585  1.71.2.1       mjf 		 */
   1586      1.18   xtraeme 		if (edata->units == ENVSYS_DRIVE) {
   1587      1.55   xtraeme 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1588      1.55   xtraeme 			for (j = 0; sdt[j].type != -1; j++)
   1589      1.55   xtraeme 				if (sdt[j].type == edata->value_cur)
   1590      1.18   xtraeme 					break;
   1591      1.18   xtraeme 
   1592      1.34   xtraeme 			error = sme_sensor_upstring(dict,
   1593      1.30   xtraeme 						    "drive-state",
   1594      1.55   xtraeme 						    sdt[j].desc);
   1595      1.55   xtraeme 			if (error)
   1596      1.55   xtraeme 				break;
   1597      1.55   xtraeme 		}
   1598      1.55   xtraeme 
   1599  1.71.2.1       mjf 		/*
   1600  1.71.2.1       mjf 		 * update 'battery-capacity' only for ENVSYS_BATTERY_CAPACITY
   1601  1.71.2.1       mjf 		 * sensors.
   1602  1.71.2.1       mjf 		 */
   1603      1.71   xtraeme 		if (edata->units == ENVSYS_BATTERY_CAPACITY) {
   1604      1.55   xtraeme 			sdt =
   1605  1.71.2.1       mjf 			  sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
   1606      1.55   xtraeme 			for (j = 0; sdt[j].type != -1; j++)
   1607      1.55   xtraeme 				if (sdt[j].type == edata->value_cur)
   1608      1.55   xtraeme 					break;
   1609      1.55   xtraeme 
   1610      1.55   xtraeme 			error = sme_sensor_upstring(dict,
   1611      1.71   xtraeme 						    "battery-capacity",
   1612      1.55   xtraeme 						    sdt[j].desc);
   1613      1.32   xtraeme 			if (error)
   1614      1.32   xtraeme 				break;
   1615       1.7      yamt 		}
   1616       1.1   thorpej 	}
   1617       1.1   thorpej 
   1618      1.18   xtraeme 	return error;
   1619       1.1   thorpej }
   1620       1.1   thorpej 
   1621       1.1   thorpej /*
   1622      1.18   xtraeme  * sme_userset_dictionary:
   1623       1.1   thorpej  *
   1624  1.71.2.1       mjf  * 	+ Parse the userland dictionary and run the appropiate tasks
   1625  1.71.2.1       mjf  * 	  that were specified.
   1626       1.1   thorpej  */
   1627      1.18   xtraeme int
   1628      1.18   xtraeme sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1629      1.18   xtraeme 		       prop_array_t array)
   1630       1.1   thorpej {
   1631      1.55   xtraeme 	const struct sme_description_table *sdt;
   1632  1.71.2.1       mjf 	envsys_data_t *edata;
   1633  1.71.2.1       mjf 	prop_dictionary_t dict, tdict = NULL;
   1634  1.71.2.1       mjf 	prop_object_t obj, obj1, obj2, tobj = NULL;
   1635  1.71.2.1       mjf 	uint64_t refresh_timo = 0;
   1636      1.18   xtraeme 	int32_t critval;
   1637  1.71.2.1       mjf 	int i, error = 0;
   1638  1.71.2.1       mjf 	const char *blah;
   1639      1.18   xtraeme 	bool targetfound = false;
   1640      1.18   xtraeme 
   1641  1.71.2.1       mjf 	KASSERT(mutex_owned(&sme_mtx));
   1642      1.18   xtraeme 
   1643  1.71.2.1       mjf 	/*
   1644  1.71.2.1       mjf 	 * The user wanted to change the refresh timeout value for this
   1645  1.71.2.1       mjf 	 * device.
   1646  1.71.2.1       mjf 	 *
   1647  1.71.2.1       mjf 	 * Get the 'device-properties' object from the userland dictionary.
   1648  1.71.2.1       mjf 	 */
   1649  1.71.2.1       mjf 	obj = prop_dictionary_get(udict, "device-properties");
   1650  1.71.2.1       mjf 	if (obj && prop_object_type(obj) == PROP_TYPE_DICTIONARY) {
   1651  1.71.2.1       mjf 		/*
   1652  1.71.2.1       mjf 		 * Get the 'refresh-timeout' property for this device.
   1653  1.71.2.1       mjf 		 */
   1654  1.71.2.1       mjf 		obj1 = prop_dictionary_get(obj, "refresh-timeout");
   1655  1.71.2.1       mjf 		if (obj1 && prop_object_type(obj1) == PROP_TYPE_NUMBER) {
   1656  1.71.2.1       mjf 			targetfound = true;
   1657  1.71.2.1       mjf 			refresh_timo =
   1658  1.71.2.1       mjf 			    prop_number_unsigned_integer_value(obj1);
   1659  1.71.2.1       mjf 			if (refresh_timo < 1)
   1660  1.71.2.1       mjf 				error = EINVAL;
   1661  1.71.2.1       mjf 			else
   1662  1.71.2.1       mjf 				sme->sme_events_timeout = refresh_timo;
   1663  1.71.2.1       mjf 		}
   1664  1.71.2.1       mjf 		goto out;
   1665       1.1   thorpej 
   1666  1.71.2.1       mjf 	} else if (!obj) {
   1667      1.69   xtraeme 		/*
   1668  1.71.2.1       mjf 		 * Get sensor's index from userland dictionary.
   1669      1.69   xtraeme 		 */
   1670  1.71.2.1       mjf 		obj = prop_dictionary_get(udict, "index");
   1671  1.71.2.1       mjf 		if (!obj)
   1672  1.71.2.1       mjf 			goto out;
   1673  1.71.2.1       mjf 		if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1674  1.71.2.1       mjf 			DPRINTF(("%s: 'index' not a string\n", __func__));
   1675  1.71.2.1       mjf 			return EINVAL;
   1676      1.40   xtraeme 		}
   1677  1.71.2.1       mjf 	} else
   1678  1.71.2.1       mjf 		return EINVAL;
   1679      1.27   xtraeme 
   1680  1.71.2.1       mjf 	/*
   1681  1.71.2.1       mjf 	 * iterate over the sensors to find the right one.
   1682  1.71.2.1       mjf 	 */
   1683  1.71.2.1       mjf 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
   1684  1.71.2.1       mjf 		/*
   1685  1.71.2.1       mjf 		 * Get a dictionary and check if it's our sensor by checking
   1686  1.71.2.1       mjf 		 * at its index position.
   1687  1.71.2.1       mjf 		 */
   1688  1.71.2.1       mjf 		dict = prop_array_get(array, edata->sensor);
   1689      1.69   xtraeme 		obj1 = prop_dictionary_get(dict, "index");
   1690      1.18   xtraeme 
   1691  1.71.2.1       mjf 		/*
   1692  1.71.2.1       mjf 		 * is it our sensor?
   1693  1.71.2.1       mjf 		 */
   1694      1.18   xtraeme 		if (!prop_string_equals(obj1, obj))
   1695      1.18   xtraeme 			continue;
   1696      1.18   xtraeme 
   1697      1.18   xtraeme 		/*
   1698      1.18   xtraeme 		 * Check if a new description operation was
   1699      1.18   xtraeme 		 * requested by the user and set new description.
   1700      1.18   xtraeme 		 */
   1701  1.71.2.1       mjf 		obj2 = prop_dictionary_get(udict, "description");
   1702  1.71.2.1       mjf 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_STRING) {
   1703      1.18   xtraeme 			targetfound = true;
   1704      1.18   xtraeme 			blah = prop_string_cstring_nocopy(obj2);
   1705  1.71.2.1       mjf 
   1706  1.71.2.1       mjf 			/*
   1707  1.71.2.1       mjf 			 * Check for duplicate description.
   1708  1.71.2.1       mjf 			 */
   1709      1.23   xtraeme 			for (i = 0; i < sme->sme_nsensors; i++) {
   1710      1.23   xtraeme 				if (i == edata->sensor)
   1711      1.23   xtraeme 					continue;
   1712  1.71.2.1       mjf 				tdict = prop_array_get(array, i);
   1713  1.71.2.1       mjf 				tobj =
   1714  1.71.2.1       mjf 				    prop_dictionary_get(tdict, "description");
   1715  1.71.2.1       mjf 				if (prop_string_equals(obj2, tobj))
   1716  1.71.2.1       mjf 					return EEXIST;
   1717      1.23   xtraeme 			}
   1718      1.23   xtraeme 
   1719  1.71.2.1       mjf 			/*
   1720  1.71.2.1       mjf 			 * Update the object in dictionary.
   1721  1.71.2.1       mjf 			 */
   1722      1.34   xtraeme 			error = sme_sensor_upstring(dict,
   1723      1.30   xtraeme 						    "description",
   1724      1.30   xtraeme 						    blah);
   1725      1.18   xtraeme 			if (error)
   1726  1.71.2.1       mjf 				return error;
   1727      1.18   xtraeme 
   1728  1.71.2.1       mjf 			DPRINTF(("%s: sensor%d changed desc to: %s\n",
   1729  1.71.2.1       mjf 			    __func__, edata->sensor, blah));
   1730      1.69   xtraeme 			edata->upropset |= USERPROP_DESC;
   1731      1.18   xtraeme 		}
   1732      1.18   xtraeme 
   1733      1.69   xtraeme 		/*
   1734      1.69   xtraeme 		 * did the user want to change the rfact?
   1735      1.69   xtraeme 		 */
   1736      1.69   xtraeme 		obj2 = prop_dictionary_get(udict, "rfact");
   1737  1.71.2.1       mjf 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1738      1.18   xtraeme 			targetfound = true;
   1739      1.69   xtraeme 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1740      1.18   xtraeme 				edata->rfact = prop_number_integer_value(obj2);
   1741      1.69   xtraeme 				edata->upropset |= USERPROP_RFACT;
   1742  1.71.2.1       mjf 				DPRINTF(("%s: sensor%d changed rfact to %d\n",
   1743  1.71.2.1       mjf 				    __func__, edata->sensor, edata->rfact));
   1744  1.71.2.1       mjf 			} else
   1745  1.71.2.1       mjf 				return ENOTSUP;
   1746      1.18   xtraeme 		}
   1747      1.18   xtraeme 
   1748      1.55   xtraeme 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1749      1.55   xtraeme 		for (i = 0; sdt[i].type != -1; i++)
   1750      1.55   xtraeme 			if (sdt[i].type == edata->units)
   1751      1.18   xtraeme 				break;
   1752      1.18   xtraeme 
   1753      1.69   xtraeme 		/*
   1754      1.69   xtraeme 		 * did the user want to set a critical capacity event?
   1755      1.69   xtraeme 		 *
   1756      1.69   xtraeme 		 * NOTE: if sme_event_register returns EEXIST that means
   1757      1.69   xtraeme 		 * the object is already there, but this is not a real
   1758      1.69   xtraeme 		 * error, because the object might be updated.
   1759      1.69   xtraeme 		 */
   1760      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1761  1.71.2.1       mjf 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1762      1.18   xtraeme 			targetfound = true;
   1763      1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1764  1.71.2.1       mjf 			    (edata->flags & ENVSYS_FPERCENT) == 0)
   1765  1.71.2.1       mjf 				return ENOTSUP;
   1766      1.18   xtraeme 
   1767      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1768      1.51   xtraeme 			error = sme_event_register(dict,
   1769      1.18   xtraeme 					      edata,
   1770  1.71.2.1       mjf 					      sme,
   1771      1.18   xtraeme 					      "critical-capacity",
   1772      1.18   xtraeme 					      critval,
   1773      1.18   xtraeme 					      PENVSYS_EVENT_BATT_USERCAP,
   1774      1.55   xtraeme 					      sdt[i].crittype);
   1775      1.69   xtraeme 			if (error == EEXIST)
   1776      1.69   xtraeme 				error = 0;
   1777  1.71.2.1       mjf 			if (error)
   1778  1.71.2.1       mjf 				goto out;
   1779  1.71.2.1       mjf 			else if (!error)
   1780      1.69   xtraeme 				edata->upropset |= USERPROP_BATTCAP;
   1781      1.18   xtraeme 		}
   1782      1.18   xtraeme 
   1783      1.69   xtraeme 		/*
   1784      1.69   xtraeme 		 * did the user want to set a critical max event?
   1785      1.69   xtraeme 		 */
   1786      1.69   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-max");
   1787  1.71.2.1       mjf 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1788      1.18   xtraeme 			targetfound = true;
   1789      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1790  1.71.2.1       mjf 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1791  1.71.2.1       mjf 				return ENOTSUP;
   1792      1.18   xtraeme 
   1793      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1794      1.51   xtraeme 			error = sme_event_register(dict,
   1795      1.18   xtraeme 					      edata,
   1796  1.71.2.1       mjf 					      sme,
   1797      1.69   xtraeme 					      "critical-max",
   1798      1.18   xtraeme 					      critval,
   1799      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMAX,
   1800      1.55   xtraeme 					      sdt[i].crittype);
   1801      1.69   xtraeme 			if (error == EEXIST)
   1802      1.69   xtraeme 				error = 0;
   1803  1.71.2.1       mjf 			if (error)
   1804  1.71.2.1       mjf 				goto out;
   1805  1.71.2.1       mjf 			else if (!error)
   1806      1.69   xtraeme 				edata->upropset |= USERPROP_CRITMAX;
   1807      1.18   xtraeme 		}
   1808      1.18   xtraeme 
   1809      1.69   xtraeme 		/*
   1810      1.69   xtraeme 		 * did the user want to set a critical min event?
   1811      1.69   xtraeme 		 */
   1812      1.69   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-min");
   1813  1.71.2.1       mjf 		if (obj2 && prop_object_type(obj2) == PROP_TYPE_NUMBER) {
   1814      1.18   xtraeme 			targetfound = true;
   1815      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1816  1.71.2.1       mjf 			    edata->flags & ENVSYS_FMONNOTSUPP)
   1817  1.71.2.1       mjf 				return ENOTSUP;
   1818      1.18   xtraeme 
   1819      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1820      1.51   xtraeme 			error = sme_event_register(dict,
   1821      1.18   xtraeme 					      edata,
   1822  1.71.2.1       mjf 					      sme,
   1823      1.69   xtraeme 					      "critical-min",
   1824      1.18   xtraeme 					      critval,
   1825      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMIN,
   1826      1.55   xtraeme 					      sdt[i].crittype);
   1827      1.69   xtraeme 			if (error == EEXIST)
   1828      1.69   xtraeme 				error = 0;
   1829  1.71.2.1       mjf 			if (error)
   1830  1.71.2.1       mjf 				goto out;
   1831  1.71.2.1       mjf 			else if (!error)
   1832      1.69   xtraeme 				edata->upropset |= USERPROP_CRITMIN;
   1833      1.18   xtraeme 		}
   1834      1.69   xtraeme 
   1835      1.69   xtraeme 		/*
   1836      1.69   xtraeme 		 * All objects in dictionary were processed.
   1837      1.69   xtraeme 		 */
   1838      1.69   xtraeme 		break;
   1839      1.18   xtraeme 	}
   1840      1.18   xtraeme 
   1841  1.71.2.1       mjf out:
   1842  1.71.2.1       mjf 	/*
   1843  1.71.2.1       mjf 	 * invalid target? return the error.
   1844  1.71.2.1       mjf 	 */
   1845      1.18   xtraeme 	if (!targetfound)
   1846      1.18   xtraeme 		error = EINVAL;
   1847      1.18   xtraeme 
   1848      1.18   xtraeme 	return error;
   1849       1.1   thorpej }
   1850