Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys.c revision 1.45.4.4
      1  1.45.4.4     joerg /*	$NetBSD: sysmon_envsys.c,v 1.45.4.4 2007/10/07 13:25:05 joerg Exp $	*/
      2      1.18   xtraeme 
      3      1.18   xtraeme /*-
      4  1.45.4.4     joerg  * 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.45.4.4     joerg  *
     16  1.45.4.4     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.45.4.4     joerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.45.4.4     joerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.45.4.4     joerg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.45.4.4     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.45.4.4     joerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.45.4.4     joerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.45.4.4     joerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.45.4.4     joerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.45.4.4     joerg  * 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.45.4.4     joerg __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.45.4.4 2007/10/07 13:25:05 joerg 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.45.4.3     joerg /* #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.45.4.3     joerg /*
     86  1.45.4.3     joerg  * Notes about locking:
     87  1.45.4.3     joerg  *
     88  1.45.4.3     joerg  * There's a global lock 'sme_mtx' to protect access to 'sysmon_envsys_list'
     89  1.45.4.3     joerg  * (devices linked list), 'struct sysmon_envsys' (device), 'sme_events_list'
     90  1.45.4.3     joerg  * (events linked list), 'sme_event_t' (event) and the global counter
     91  1.45.4.3     joerg  * 'sysmon_envsys_next_sensor_index'.
     92  1.45.4.3     joerg  *
     93  1.45.4.3     joerg  * Another lock 'sme_init_mtx' is used to protect initialization and
     94  1.45.4.3     joerg  * finalization of the events framework (the callout(9) and workqueue(9)
     95  1.45.4.3     joerg  * that is used to check for conditions and sending events to powerd(8)).
     96  1.45.4.3     joerg  *
     97  1.45.4.3     joerg  * The global 'sme_cv' condition variable is used to wait for state changes
     98  1.45.4.3     joerg  * on the 'sysmon_envsys_list' and 'sme_events_list' linked lists.
     99  1.45.4.3     joerg  *
    100  1.45.4.3     joerg  */
    101       1.1   thorpej 
    102  1.45.4.3     joerg kmutex_t sme_mtx, sme_event_init_mtx;
    103  1.45.4.3     joerg kcondvar_t sme_cv;
    104      1.18   xtraeme 
    105  1.45.4.4     joerg /*
    106  1.45.4.4     joerg  * Types of properties that can be set via userland.
    107  1.45.4.4     joerg  */
    108  1.45.4.4     joerg enum {
    109  1.45.4.4     joerg 	USERPROP_DESC 		= 0x0001,
    110  1.45.4.4     joerg 	USERPROP_BATTCAP	= 0x0002,
    111  1.45.4.4     joerg 	USERPROP_CRITMAX	= 0x0004,
    112  1.45.4.4     joerg 	USERPROP_CRITMIN	= 0x0008,
    113  1.45.4.4     joerg 	USERPROP_RFACT		= 0x0010
    114  1.45.4.4     joerg };
    115  1.45.4.4     joerg 
    116      1.18   xtraeme static prop_dictionary_t sme_propd;
    117  1.45.4.3     joerg static uint32_t sysmon_envsys_next_sensor_index = 0;
    118      1.18   xtraeme static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
    119       1.1   thorpej 
    120      1.18   xtraeme static void sysmon_envsys_release(struct sysmon_envsys *);
    121  1.45.4.2  jmcneill static void sysmon_envsys_destroy_plist(prop_array_t);
    122  1.45.4.2  jmcneill static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
    123  1.45.4.4     joerg static void sme_remove_userprops(void);
    124       1.1   thorpej 
    125      1.18   xtraeme /*
    126      1.18   xtraeme  * sysmon_envsys_init:
    127      1.18   xtraeme  *
    128      1.18   xtraeme  * 	+ Initialize global mutexes, dictionary and the linked lists.
    129      1.18   xtraeme  */
    130      1.18   xtraeme void
    131      1.18   xtraeme sysmon_envsys_init(void)
    132      1.18   xtraeme {
    133      1.18   xtraeme 	LIST_INIT(&sysmon_envsys_list);
    134      1.18   xtraeme 	LIST_INIT(&sme_events_list);
    135  1.45.4.3     joerg 	mutex_init(&sme_mtx, MUTEX_DRIVER, IPL_NONE);
    136      1.18   xtraeme 	mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
    137  1.45.4.3     joerg 	cv_init(&sme_cv, "smework");
    138      1.18   xtraeme 	sme_propd = prop_dictionary_create();
    139      1.18   xtraeme }
    140       1.1   thorpej 
    141       1.1   thorpej /*
    142       1.1   thorpej  * sysmonopen_envsys:
    143       1.1   thorpej  *
    144      1.18   xtraeme  *	+ Open the system monitor device.
    145       1.1   thorpej  */
    146       1.1   thorpej int
    147      1.18   xtraeme sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    148       1.1   thorpej {
    149      1.18   xtraeme 	return 0;
    150       1.1   thorpej }
    151       1.1   thorpej 
    152       1.1   thorpej /*
    153       1.1   thorpej  * sysmonclose_envsys:
    154       1.1   thorpej  *
    155      1.18   xtraeme  *	+ Close the system monitor device.
    156       1.1   thorpej  */
    157       1.1   thorpej int
    158      1.18   xtraeme sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
    159       1.1   thorpej {
    160       1.3  jdolecek 	/* Nothing to do */
    161      1.18   xtraeme 	return 0;
    162       1.1   thorpej }
    163       1.1   thorpej 
    164       1.1   thorpej /*
    165       1.1   thorpej  * sysmonioctl_envsys:
    166       1.1   thorpej  *
    167  1.45.4.2  jmcneill  *	+ Perform a sysmon envsys control request.
    168       1.1   thorpej  */
    169       1.1   thorpej int
    170      1.18   xtraeme sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    171       1.1   thorpej {
    172      1.18   xtraeme 	struct sysmon_envsys *sme = NULL;
    173       1.1   thorpej 	int error = 0;
    174       1.1   thorpej 	u_int oidx;
    175       1.1   thorpej 
    176       1.1   thorpej 	switch (cmd) {
    177      1.18   xtraeme 	case ENVSYS_GETDICTIONARY:
    178      1.18   xtraeme 	    {
    179      1.18   xtraeme 		struct plistref *plist = (struct plistref *)data;
    180  1.45.4.3     joerg 
    181      1.18   xtraeme 		/*
    182      1.18   xtraeme 		 * Update all sysmon envsys devices dictionaries with
    183      1.18   xtraeme 		 * new data if it's different than we have currently
    184      1.18   xtraeme 		 * in the dictionary.
    185      1.18   xtraeme 		 */
    186  1.45.4.3     joerg 		mutex_enter(&sme_mtx);
    187      1.18   xtraeme 		LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    188  1.45.4.2  jmcneill 			sme->sme_flags |= SME_FLAG_BUSY;
    189      1.18   xtraeme 			error = sme_update_dictionary(sme);
    190      1.18   xtraeme 			if (error) {
    191      1.18   xtraeme 				DPRINTF(("%s: sme_update_dictionary, "
    192      1.18   xtraeme 				    "error=%d\n", __func__, error));
    193  1.45.4.2  jmcneill 				sme->sme_flags &= ~SME_FLAG_BUSY;
    194  1.45.4.3     joerg 				mutex_exit(&sme_mtx);
    195      1.18   xtraeme 				return error;
    196      1.18   xtraeme 			}
    197  1.45.4.2  jmcneill 			sme->sme_flags &= ~SME_FLAG_BUSY;
    198      1.18   xtraeme 		}
    199  1.45.4.3     joerg 		mutex_exit(&sme_mtx);
    200      1.18   xtraeme 		/*
    201      1.18   xtraeme 		 * Copy global dictionary to userland.
    202      1.18   xtraeme 		 */
    203      1.18   xtraeme 		error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
    204       1.1   thorpej 		break;
    205      1.18   xtraeme 	    }
    206      1.18   xtraeme 	case ENVSYS_SETDICTIONARY:
    207       1.1   thorpej 	    {
    208      1.18   xtraeme 		const struct plistref *plist = (const struct plistref *)data;
    209      1.18   xtraeme 		prop_dictionary_t udict;
    210  1.45.4.4     joerg 		prop_object_iterator_t iter, iter2;
    211  1.45.4.4     joerg 		prop_object_t obj, obj2;
    212  1.45.4.4     joerg 		prop_array_t array_u, array_k;
    213      1.18   xtraeme 		const char *devname = NULL;
    214      1.18   xtraeme 
    215      1.22   xtraeme 		if ((flag & FWRITE) == 0)
    216      1.22   xtraeme 			return EPERM;
    217      1.22   xtraeme 
    218      1.18   xtraeme 		/*
    219      1.18   xtraeme 		 * Get dictionary from userland.
    220      1.18   xtraeme 		 */
    221      1.18   xtraeme 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    222      1.41   xtraeme 		if (error) {
    223      1.41   xtraeme 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    224      1.41   xtraeme 			    __func__, error));
    225      1.18   xtraeme 			break;
    226      1.41   xtraeme 		}
    227       1.4  explorer 
    228  1.45.4.4     joerg 		iter = prop_dictionary_iterator(udict);
    229  1.45.4.4     joerg 		if (!iter) {
    230  1.45.4.4     joerg 			prop_object_release(udict);
    231  1.45.4.4     joerg 			return ENOMEM;
    232  1.45.4.4     joerg 		}
    233  1.45.4.4     joerg 
    234      1.18   xtraeme 		/*
    235  1.45.4.4     joerg 		 * Iterate over the userland dictionary and process
    236  1.45.4.4     joerg 		 * the list of devices.
    237      1.18   xtraeme 		 */
    238  1.45.4.4     joerg 		while ((obj = prop_object_iterator_next(iter))) {
    239  1.45.4.4     joerg 			array_u = prop_dictionary_get_keysym(udict, obj);
    240  1.45.4.4     joerg 			if (prop_object_type(array_u) != PROP_TYPE_ARRAY) {
    241  1.45.4.4     joerg 				prop_object_iterator_release(iter);
    242  1.45.4.4     joerg 				prop_object_release(udict);
    243  1.45.4.4     joerg 				return EINVAL;
    244  1.45.4.4     joerg 			}
    245  1.45.4.4     joerg 
    246  1.45.4.4     joerg 			devname = prop_dictionary_keysym_cstring_nocopy(obj);
    247  1.45.4.4     joerg 			DPRINTF(("%s: processing the '%s' array requests\n",
    248  1.45.4.4     joerg 			    __func__, devname));
    249  1.45.4.4     joerg 
    250  1.45.4.4     joerg 			/*
    251  1.45.4.4     joerg 			 * find the correct sme device.
    252  1.45.4.4     joerg 			 */
    253  1.45.4.4     joerg 			sme = sysmon_envsys_find(devname);
    254  1.45.4.4     joerg 			if (!sme) {
    255  1.45.4.4     joerg 				DPRINTF(("%s: NULL sme\n", __func__));
    256  1.45.4.4     joerg 				prop_object_iterator_release(iter);
    257  1.45.4.4     joerg 				prop_object_release(udict);
    258  1.45.4.4     joerg 				return EINVAL;
    259  1.45.4.4     joerg 			}
    260  1.45.4.4     joerg 
    261  1.45.4.4     joerg 			/*
    262  1.45.4.4     joerg 			 * Find the correct array object with the string
    263  1.45.4.4     joerg 			 * supplied by the userland dictionary.
    264  1.45.4.4     joerg 			 */
    265  1.45.4.4     joerg 			array_k = prop_dictionary_get(sme_propd, devname);
    266  1.45.4.4     joerg 			if (prop_object_type(array_k) != PROP_TYPE_ARRAY) {
    267  1.45.4.4     joerg 				DPRINTF(("%s: array device failed\n",
    268  1.45.4.4     joerg 				    __func__));
    269  1.45.4.4     joerg 				sysmon_envsys_release(sme);
    270  1.45.4.4     joerg 				prop_object_iterator_release(iter);
    271  1.45.4.4     joerg 				prop_object_release(udict);
    272  1.45.4.4     joerg 				return EINVAL;
    273  1.45.4.4     joerg 			}
    274  1.45.4.4     joerg 
    275  1.45.4.4     joerg 			iter2 = prop_array_iterator(array_u);
    276  1.45.4.4     joerg 			if (!iter2) {
    277  1.45.4.4     joerg 				sysmon_envsys_release(sme);
    278  1.45.4.4     joerg 				prop_object_iterator_release(iter);
    279  1.45.4.4     joerg 				prop_object_release(udict);
    280  1.45.4.4     joerg 				return ENOMEM;
    281  1.45.4.4     joerg 			}
    282  1.45.4.4     joerg 
    283  1.45.4.4     joerg 			/*
    284  1.45.4.4     joerg 			 * Iterate over the array of dictionaries to
    285  1.45.4.4     joerg 			 * process the list of sensors.
    286  1.45.4.4     joerg 			 */
    287  1.45.4.4     joerg 			while ((obj2 = prop_object_iterator_next(iter2))) {
    288  1.45.4.4     joerg 				/* do the real work now */
    289  1.45.4.4     joerg 				error = sme_userset_dictionary(sme,
    290  1.45.4.4     joerg 							       obj2,
    291  1.45.4.4     joerg 							       array_k);
    292  1.45.4.4     joerg 				if (error) {
    293  1.45.4.4     joerg 					sysmon_envsys_release(sme);
    294  1.45.4.4     joerg 					prop_object_iterator_release(iter2);
    295  1.45.4.4     joerg 					prop_object_iterator_release(iter);
    296  1.45.4.4     joerg 					prop_object_release(udict);
    297  1.45.4.4     joerg 					return EINVAL;
    298  1.45.4.4     joerg 				}
    299  1.45.4.4     joerg 			}
    300  1.45.4.4     joerg 
    301  1.45.4.4     joerg 			sysmon_envsys_release(sme);
    302  1.45.4.4     joerg 			prop_object_iterator_release(iter2);
    303      1.18   xtraeme 		}
    304       1.4  explorer 
    305  1.45.4.4     joerg 		prop_object_iterator_release(iter);
    306  1.45.4.4     joerg 		prop_object_release(udict);
    307  1.45.4.4     joerg 		break;
    308  1.45.4.4     joerg 	    }
    309  1.45.4.4     joerg 	case ENVSYS_REMOVEPROPS:
    310  1.45.4.4     joerg 	    {
    311  1.45.4.4     joerg 		const struct plistref *plist = (const struct plistref *)data;
    312  1.45.4.4     joerg 		prop_dictionary_t udict;
    313  1.45.4.4     joerg 		prop_object_t obj;
    314       1.4  explorer 
    315  1.45.4.4     joerg 		if ((flag & FWRITE) == 0)
    316  1.45.4.4     joerg 			return EPERM;
    317  1.45.4.4     joerg 
    318  1.45.4.4     joerg 		error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
    319  1.45.4.4     joerg 		if (error) {
    320  1.45.4.4     joerg 			DPRINTF(("%s: copyin_ioctl error=%d\n",
    321  1.45.4.4     joerg 			    __func__, error));
    322       1.4  explorer 			break;
    323       1.4  explorer 		}
    324       1.1   thorpej 
    325  1.45.4.4     joerg 		obj = prop_dictionary_get(udict, "envsys-remove-props");
    326  1.45.4.4     joerg 		if (!obj || !prop_bool_true(obj)) {
    327  1.45.4.4     joerg 			DPRINTF(("%s: invalid 'envsys-remove-props'\n",
    328  1.45.4.4     joerg 			     __func__));
    329  1.45.4.4     joerg 			return EINVAL;
    330       1.1   thorpej 		}
    331      1.18   xtraeme 
    332  1.45.4.4     joerg 		sme_remove_userprops();
    333  1.45.4.4     joerg 
    334  1.45.4.2  jmcneill 		prop_object_release(udict);
    335       1.1   thorpej 		break;
    336       1.1   thorpej 	    }
    337  1.45.4.4     joerg 
    338      1.18   xtraeme 	    /*
    339      1.18   xtraeme 	     * Compatibility functions with the old interface, only
    340      1.18   xtraeme 	     * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
    341      1.18   xtraeme 	     * to make old applications work.
    342      1.18   xtraeme 	     */
    343       1.1   thorpej 	case ENVSYS_GTREDATA:
    344       1.1   thorpej 	    {
    345      1.18   xtraeme 		struct envsys_tre_data *tred = (void *)data;
    346      1.18   xtraeme 		envsys_data_t *edata = NULL;
    347       1.1   thorpej 
    348       1.1   thorpej 		tred->validflags = 0;
    349       1.1   thorpej 
    350      1.18   xtraeme 		sme = sysmon_envsys_find_40(tred->sensor);
    351  1.45.4.4     joerg 		if (!sme)
    352       1.1   thorpej 			break;
    353      1.18   xtraeme 
    354  1.45.4.3     joerg 		mutex_enter(&sme_mtx);
    355       1.1   thorpej 		oidx = tred->sensor;
    356       1.1   thorpej 		tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
    357      1.18   xtraeme 
    358      1.18   xtraeme 		DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
    359      1.18   xtraeme 		    __func__, tred->sensor, oidx, sme->sme_name,
    360      1.18   xtraeme 		    sme->sme_nsensors));
    361      1.18   xtraeme 
    362      1.18   xtraeme 		edata = &sme->sme_sensor_data[tred->sensor];
    363      1.18   xtraeme 
    364      1.18   xtraeme 		if (tred->sensor < sme->sme_nsensors) {
    365      1.18   xtraeme 			if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    366      1.18   xtraeme 				error = (*sme->sme_gtredata)(sme, edata);
    367      1.18   xtraeme 				if (error) {
    368      1.18   xtraeme 					DPRINTF(("%s: sme_gtredata failed\n",
    369      1.18   xtraeme 				    	    __func__));
    370  1.45.4.3     joerg 					mutex_exit(&sme_mtx);
    371      1.18   xtraeme 					return error;
    372      1.18   xtraeme 				}
    373      1.18   xtraeme 			}
    374      1.18   xtraeme 
    375      1.18   xtraeme 			/* copy required values to the old interface */
    376      1.18   xtraeme 			tred->sensor = edata->sensor;
    377      1.18   xtraeme 			tred->cur.data_us = edata->value_cur;
    378      1.18   xtraeme 			tred->cur.data_s = edata->value_cur;
    379      1.18   xtraeme 			tred->max.data_us = edata->value_max;
    380      1.18   xtraeme 			tred->max.data_s = edata->value_max;
    381      1.18   xtraeme 			tred->min.data_us = edata->value_min;
    382      1.18   xtraeme 			tred->min.data_s = edata->value_min;
    383      1.18   xtraeme 			tred->avg.data_us = edata->value_avg;
    384      1.18   xtraeme 			tred->avg.data_s = edata->value_avg;
    385      1.18   xtraeme 			tred->units = edata->units;
    386      1.18   xtraeme 
    387      1.20   xtraeme 			tred->validflags |= ENVSYS_FVALID;
    388      1.20   xtraeme 			tred->validflags |= ENVSYS_FCURVALID;
    389      1.20   xtraeme 
    390      1.20   xtraeme 			if (edata->flags & ENVSYS_FPERCENT) {
    391      1.20   xtraeme 				tred->validflags |= ENVSYS_FMAXVALID;
    392      1.20   xtraeme 				tred->validflags |= ENVSYS_FFRACVALID;
    393      1.20   xtraeme 			}
    394      1.20   xtraeme 
    395      1.37   xtraeme 			if (edata->state == ENVSYS_SINVALID ||
    396      1.41   xtraeme 			    edata->flags & ENVSYS_FNOTVALID) {
    397      1.20   xtraeme 				tred->validflags &= ~ENVSYS_FCURVALID;
    398      1.18   xtraeme 				tred->cur.data_us = tred->cur.data_s = 0;
    399      1.18   xtraeme 			}
    400      1.18   xtraeme 
    401      1.18   xtraeme 			DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
    402      1.18   xtraeme 			    __func__, edata->desc, tred->cur.data_s));
    403      1.18   xtraeme 			DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
    404      1.18   xtraeme 			    " tred->sensor=%d\n", __func__, tred->validflags,
    405      1.18   xtraeme 			    tred->units, tred->sensor));
    406       1.3  jdolecek 		}
    407       1.1   thorpej 		tred->sensor = oidx;
    408  1.45.4.3     joerg 		mutex_exit(&sme_mtx);
    409       1.1   thorpej 
    410       1.1   thorpej 		break;
    411       1.1   thorpej 	    }
    412       1.1   thorpej 	case ENVSYS_GTREINFO:
    413       1.1   thorpej 	    {
    414      1.18   xtraeme 		struct envsys_basic_info *binfo = (void *)data;
    415      1.18   xtraeme 		envsys_data_t *edata = NULL;
    416       1.1   thorpej 
    417       1.1   thorpej 		binfo->validflags = 0;
    418       1.1   thorpej 
    419      1.18   xtraeme 		sme = sysmon_envsys_find_40(binfo->sensor);
    420  1.45.4.4     joerg 		if (!sme)
    421       1.1   thorpej 			break;
    422      1.18   xtraeme 
    423  1.45.4.3     joerg 		mutex_enter(&sme_mtx);
    424       1.1   thorpej 		oidx = binfo->sensor;
    425       1.1   thorpej 		binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
    426      1.18   xtraeme 
    427      1.18   xtraeme 		edata = &sme->sme_sensor_data[binfo->sensor];
    428      1.18   xtraeme 
    429      1.20   xtraeme 		binfo->validflags |= ENVSYS_FVALID;
    430      1.18   xtraeme 
    431      1.18   xtraeme 		if (binfo->sensor < sme->sme_nsensors) {
    432      1.18   xtraeme 			binfo->units = edata->units;
    433      1.18   xtraeme 			(void)strlcpy(binfo->desc, edata->desc,
    434      1.18   xtraeme 			    sizeof(binfo->desc));
    435      1.18   xtraeme 		}
    436      1.18   xtraeme 
    437      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
    438      1.18   xtraeme 		    __func__, binfo->units, binfo->validflags));
    439      1.18   xtraeme 		DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
    440      1.18   xtraeme 		    __func__, binfo->desc, binfo->sensor));
    441      1.18   xtraeme 
    442       1.1   thorpej 		binfo->sensor = oidx;
    443  1.45.4.3     joerg 		mutex_exit(&sme_mtx);
    444      1.18   xtraeme 
    445       1.1   thorpej 		break;
    446       1.1   thorpej 	    }
    447       1.1   thorpej 	default:
    448       1.1   thorpej 		error = ENOTTY;
    449      1.18   xtraeme 		break;
    450       1.1   thorpej 	}
    451       1.1   thorpej 
    452      1.18   xtraeme 	return error;
    453       1.1   thorpej }
    454       1.1   thorpej 
    455       1.1   thorpej /*
    456       1.1   thorpej  * sysmon_envsys_register:
    457       1.1   thorpej  *
    458  1.45.4.2  jmcneill  *	+ Register a sysmon envsys device.
    459  1.45.4.2  jmcneill  *	+ Create array of dictionaries for a device.
    460       1.1   thorpej  */
    461       1.1   thorpej int
    462       1.1   thorpej sysmon_envsys_register(struct sysmon_envsys *sme)
    463       1.1   thorpej {
    464  1.45.4.3     joerg 	struct sme_evdrv {
    465  1.45.4.3     joerg 		SLIST_ENTRY(sme_evdrv) evdrv_head;
    466  1.45.4.3     joerg 		sme_event_drv_t *evdrv;
    467  1.45.4.3     joerg 	};
    468  1.45.4.3     joerg 	SLIST_HEAD(, sme_evdrv) sme_evdrv_list;
    469  1.45.4.3     joerg 	struct sme_evdrv *sme_evdrv = NULL;
    470      1.18   xtraeme 	struct sysmon_envsys *lsme;
    471  1.45.4.2  jmcneill 	prop_dictionary_t dict;
    472  1.45.4.2  jmcneill 	prop_array_t array;
    473  1.45.4.2  jmcneill 	envsys_data_t *edata = NULL;
    474  1.45.4.2  jmcneill 	int i, error = 0;
    475       1.1   thorpej 
    476  1.45.4.2  jmcneill 	KASSERT(sme != NULL);
    477  1.45.4.2  jmcneill 	KASSERT(sme->sme_name != NULL);
    478  1.45.4.2  jmcneill 	KASSERT(sme->sme_sensor_data != NULL);
    479      1.18   xtraeme 
    480  1.45.4.2  jmcneill 	/*
    481  1.45.4.2  jmcneill 	 * sme_nsensors is mandatory...
    482      1.18   xtraeme 	 */
    483  1.45.4.2  jmcneill 	if (!sme->sme_nsensors)
    484      1.18   xtraeme 		return EINVAL;
    485  1.45.4.2  jmcneill 
    486      1.18   xtraeme 	/*
    487  1.45.4.2  jmcneill 	 * sanity check: if SME_DISABLE_GTREDATA is not set,
    488      1.18   xtraeme 	 * the sme_gtredata function callback must be non NULL.
    489      1.18   xtraeme 	 */
    490      1.18   xtraeme 	if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
    491  1.45.4.4     joerg 		if (!sme->sme_gtredata)
    492      1.18   xtraeme 			return EINVAL;
    493       1.1   thorpej 	}
    494       1.1   thorpej 
    495  1.45.4.3     joerg 
    496  1.45.4.2  jmcneill 	/* create the sysmon envsys device array. */
    497  1.45.4.2  jmcneill 	array = prop_array_create();
    498  1.45.4.4     joerg 	if (!array)
    499  1.45.4.2  jmcneill 		return ENOMEM;
    500  1.45.4.2  jmcneill 
    501  1.45.4.2  jmcneill 	/*
    502  1.45.4.2  jmcneill 	 * Initialize the singly linked list for sensor descriptions.
    503  1.45.4.2  jmcneill 	 */
    504  1.45.4.2  jmcneill 	SLIST_INIT(&sme->sme_names_list);
    505  1.45.4.3     joerg 
    506  1.45.4.3     joerg 	/*
    507  1.45.4.3     joerg 	 * Initialize the singly linked list for driver events.
    508  1.45.4.3     joerg 	 */
    509  1.45.4.3     joerg 	SLIST_INIT(&sme_evdrv_list);
    510  1.45.4.2  jmcneill 	/*
    511  1.45.4.2  jmcneill 	 * Iterate over all sensors and create a dictionary per sensor,
    512  1.45.4.2  jmcneill 	 * checking firstly if sensor description is unique.
    513  1.45.4.2  jmcneill 	 */
    514  1.45.4.2  jmcneill 	for (i = 0; i < sme->sme_nsensors; i++) {
    515  1.45.4.2  jmcneill 		edata = &sme->sme_sensor_data[i];
    516  1.45.4.2  jmcneill 		/*
    517  1.45.4.2  jmcneill 		 * Check if sensor description is unique.
    518  1.45.4.2  jmcneill 		 */
    519  1.45.4.2  jmcneill 		if (sme_register_sensorname(sme, edata))
    520  1.45.4.2  jmcneill 			continue;
    521  1.45.4.2  jmcneill 
    522  1.45.4.2  jmcneill 		dict = prop_dictionary_create();
    523  1.45.4.4     joerg 		if (!dict) {
    524  1.45.4.2  jmcneill 			error = ENOMEM;
    525  1.45.4.2  jmcneill 			goto out2;
    526  1.45.4.2  jmcneill 		}
    527  1.45.4.2  jmcneill 
    528  1.45.4.2  jmcneill 		/*
    529  1.45.4.2  jmcneill 		 * Create all objects in sensor's dictionary.
    530  1.45.4.2  jmcneill 		 */
    531  1.45.4.3     joerg 		sme_evdrv = kmem_zalloc(sizeof(*sme_evdrv), KM_SLEEP);
    532  1.45.4.3     joerg 		sme_evdrv->evdrv = sme_add_sensor_dictionary(sme,
    533  1.45.4.3     joerg 					array, dict, edata);
    534  1.45.4.3     joerg 		if (sme_evdrv->evdrv)
    535  1.45.4.3     joerg 			SLIST_INSERT_HEAD(&sme_evdrv_list,
    536  1.45.4.3     joerg 					  sme_evdrv, evdrv_head);
    537  1.45.4.2  jmcneill 	}
    538  1.45.4.2  jmcneill 
    539      1.18   xtraeme 	/*
    540  1.45.4.2  jmcneill 	 * Check if requested sysmon_envsys device is valid
    541  1.45.4.2  jmcneill 	 * and does not exist already in the list.
    542      1.18   xtraeme 	 */
    543  1.45.4.3     joerg 	mutex_enter(&sme_mtx);
    544  1.45.4.2  jmcneill 	sme->sme_flags |= SME_FLAG_BUSY;
    545      1.18   xtraeme 	LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
    546      1.18   xtraeme 	       if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
    547      1.18   xtraeme 		       error = EEXIST;
    548      1.18   xtraeme 		       goto out;
    549      1.18   xtraeme 	       }
    550      1.18   xtraeme 	}
    551  1.45.4.3     joerg 
    552  1.45.4.2  jmcneill 	/*
    553  1.45.4.2  jmcneill 	 * If the array does not contain any object (sensor), there's
    554  1.45.4.2  jmcneill 	 * no need to attach the driver.
    555  1.45.4.2  jmcneill 	 */
    556  1.45.4.2  jmcneill 	if (prop_array_count(array) == 0) {
    557  1.45.4.2  jmcneill 		error = EINVAL;
    558  1.45.4.2  jmcneill 		DPRINTF(("%s: empty array for '%s'\n", __func__,
    559  1.45.4.2  jmcneill 		    sme->sme_name));
    560  1.45.4.2  jmcneill 		goto out;
    561  1.45.4.2  jmcneill 	}
    562  1.45.4.2  jmcneill 	/*
    563  1.45.4.2  jmcneill 	 * Add the array into the global dictionary for the driver.
    564  1.45.4.2  jmcneill 	 *
    565  1.45.4.2  jmcneill 	 * <dict>
    566  1.45.4.2  jmcneill 	 * 	<key>foo0</key>
    567  1.45.4.2  jmcneill 	 * 	<array>
    568  1.45.4.2  jmcneill 	 * 		...
    569  1.45.4.2  jmcneill 	 */
    570  1.45.4.2  jmcneill 	if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
    571  1.45.4.2  jmcneill 		error = EINVAL;
    572  1.45.4.2  jmcneill 		DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
    573  1.45.4.2  jmcneill 		    sme->sme_name));
    574  1.45.4.2  jmcneill 		goto out;
    575  1.45.4.2  jmcneill 	}
    576  1.45.4.2  jmcneill 	/*
    577  1.45.4.2  jmcneill 	 * Add the device into the list.
    578  1.45.4.2  jmcneill 	 */
    579  1.45.4.3     joerg 	LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
    580       1.1   thorpej 	sme->sme_fsensor = sysmon_envsys_next_sensor_index;
    581       1.1   thorpej 	sysmon_envsys_next_sensor_index += sme->sme_nsensors;
    582      1.18   xtraeme out:
    583  1.45.4.2  jmcneill 	sme->sme_uniqsensors = 0;
    584  1.45.4.3     joerg 	sme->sme_flags &= ~SME_FLAG_BUSY;
    585  1.45.4.3     joerg 	mutex_exit(&sme_mtx);
    586  1.45.4.3     joerg 
    587  1.45.4.3     joerg 	if (error == 0) {
    588  1.45.4.3     joerg 		i = 0;
    589  1.45.4.3     joerg 		SLIST_FOREACH(sme_evdrv, &sme_evdrv_list, evdrv_head) {
    590  1.45.4.3     joerg 			if (i == 0)
    591  1.45.4.3     joerg 				sysmon_task_queue_init();
    592  1.45.4.3     joerg 			sysmon_task_queue_sched(0,
    593  1.45.4.3     joerg 			    sme_event_drvadd, sme_evdrv->evdrv);
    594  1.45.4.3     joerg 		}
    595  1.45.4.3     joerg 		DPRINTF(("%s: driver '%s' registered (nsens=%d)\n",
    596  1.45.4.3     joerg 		    __func__, sme->sme_name, sme->sme_nsensors));
    597  1.45.4.3     joerg 	}
    598  1.45.4.3     joerg 
    599  1.45.4.3     joerg out2:
    600  1.45.4.3     joerg 	while (!SLIST_EMPTY(&sme_evdrv_list)) {
    601  1.45.4.3     joerg 		sme_evdrv = SLIST_FIRST(&sme_evdrv_list);
    602  1.45.4.3     joerg 		SLIST_REMOVE_HEAD(&sme_evdrv_list, evdrv_head);
    603  1.45.4.3     joerg 		kmem_free(sme_evdrv, sizeof(*sme_evdrv));
    604  1.45.4.3     joerg 	}
    605  1.45.4.4     joerg 	if (!error)
    606  1.45.4.2  jmcneill 		return 0;
    607  1.45.4.3     joerg 
    608  1.45.4.2  jmcneill 	DPRINTF(("%s: failed to register '%s' (%d)\n", __func__,
    609  1.45.4.2  jmcneill 	    sme->sme_name, error));
    610  1.45.4.3     joerg 	if (error != EEXIST) {
    611  1.45.4.3     joerg 		mutex_enter(&sme_mtx);
    612  1.45.4.3     joerg 		sme_event_unregister_all(sme->sme_name);
    613  1.45.4.3     joerg 		mutex_exit(&sme_mtx);
    614  1.45.4.3     joerg 	}
    615  1.45.4.2  jmcneill 	sysmon_envsys_destroy_plist(array);
    616      1.18   xtraeme 	return error;
    617       1.1   thorpej }
    618       1.1   thorpej 
    619       1.1   thorpej /*
    620  1.45.4.2  jmcneill  * sysmon_envsys_destroy_plist:
    621  1.45.4.2  jmcneill  *
    622  1.45.4.2  jmcneill  * 	+ Remove all objects from the array of dictionaries that is
    623  1.45.4.2  jmcneill  * 	  created in a sysmon envsys device.
    624  1.45.4.2  jmcneill  */
    625  1.45.4.2  jmcneill static void
    626  1.45.4.2  jmcneill sysmon_envsys_destroy_plist(prop_array_t array)
    627  1.45.4.2  jmcneill {
    628  1.45.4.2  jmcneill 	prop_object_iterator_t iter, iter2;
    629  1.45.4.3     joerg 	prop_dictionary_t dict;
    630  1.45.4.2  jmcneill 	prop_object_t obj;
    631  1.45.4.2  jmcneill 
    632  1.45.4.2  jmcneill 	KASSERT(array != NULL);
    633  1.45.4.2  jmcneill 
    634  1.45.4.3     joerg 	DPRINTFOBJ(("%s: objects in array=%d\n", __func__,
    635  1.45.4.3     joerg 	    prop_array_count(array)));
    636  1.45.4.3     joerg 
    637  1.45.4.2  jmcneill 	iter = prop_array_iterator(array);
    638  1.45.4.4     joerg 	if (!iter)
    639  1.45.4.2  jmcneill 		return;
    640  1.45.4.2  jmcneill 
    641  1.45.4.4     joerg 	while ((dict = prop_object_iterator_next(iter))) {
    642  1.45.4.3     joerg 		KASSERT(prop_object_type(dict) == PROP_TYPE_DICTIONARY);
    643  1.45.4.2  jmcneill 		iter2 = prop_dictionary_iterator(dict);
    644  1.45.4.4     joerg 		if (!iter2)
    645  1.45.4.3     joerg 			goto out;
    646  1.45.4.3     joerg 		DPRINTFOBJ(("%s: iterating over dictionary\n", __func__));
    647  1.45.4.3     joerg 		while ((obj = prop_object_iterator_next(iter2)) != NULL) {
    648  1.45.4.3     joerg 			DPRINTFOBJ(("%s: obj=%s\n", __func__,
    649  1.45.4.3     joerg 			    prop_dictionary_keysym_cstring_nocopy(obj)));
    650  1.45.4.3     joerg 			prop_dictionary_remove(dict,
    651  1.45.4.3     joerg 			    prop_dictionary_keysym_cstring_nocopy(obj));
    652  1.45.4.3     joerg 			prop_object_iterator_reset(iter2);
    653  1.45.4.3     joerg 		}
    654  1.45.4.3     joerg 		prop_object_iterator_release(iter2);
    655  1.45.4.3     joerg 		DPRINTFOBJ(("%s: objects in dictionary:%d\n",
    656  1.45.4.3     joerg 		    __func__, prop_dictionary_count(dict)));
    657  1.45.4.2  jmcneill 		prop_object_release(dict);
    658  1.45.4.2  jmcneill 	}
    659  1.45.4.2  jmcneill 
    660  1.45.4.3     joerg out:
    661  1.45.4.2  jmcneill 	prop_object_iterator_release(iter);
    662  1.45.4.3     joerg 	prop_object_release(array);
    663  1.45.4.2  jmcneill }
    664  1.45.4.2  jmcneill 
    665  1.45.4.2  jmcneill /*
    666       1.1   thorpej  * sysmon_envsys_unregister:
    667       1.1   thorpej  *
    668  1.45.4.2  jmcneill  *	+ Unregister a sysmon envsys device.
    669       1.1   thorpej  */
    670       1.1   thorpej void
    671       1.1   thorpej sysmon_envsys_unregister(struct sysmon_envsys *sme)
    672       1.1   thorpej {
    673      1.27   xtraeme 	struct sme_sensor_names *snames;
    674  1.45.4.2  jmcneill 	prop_array_t array;
    675      1.35   xtraeme 
    676      1.35   xtraeme 	KASSERT(sme != NULL);
    677       1.1   thorpej 
    678  1.45.4.3     joerg 	mutex_enter(&sme_mtx);
    679       1.7      yamt 	while (sme->sme_flags & SME_FLAG_BUSY) {
    680       1.8      yamt 		sme->sme_flags |= SME_FLAG_WANTED;
    681  1.45.4.3     joerg 		cv_wait(&sme_cv, &sme_mtx);
    682      1.18   xtraeme 	}
    683  1.45.4.3     joerg 	sysmon_envsys_next_sensor_index -= sme->sme_nsensors;
    684      1.27   xtraeme 	/*
    685  1.45.4.2  jmcneill 	 * Remove all sensor descriptions from the singly linked list.
    686  1.45.4.2  jmcneill 	 */
    687  1.45.4.2  jmcneill 	while (!SLIST_EMPTY(&sme->sme_names_list)) {
    688  1.45.4.2  jmcneill 		snames = SLIST_FIRST(&sme->sme_names_list);
    689  1.45.4.2  jmcneill 		SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
    690  1.45.4.2  jmcneill 		kmem_free(snames, sizeof(*snames));
    691  1.45.4.2  jmcneill 	}
    692  1.45.4.2  jmcneill 	/*
    693      1.27   xtraeme 	 * Unregister all events associated with this device.
    694      1.27   xtraeme 	 */
    695  1.45.4.2  jmcneill 	sme_event_unregister_all(sme->sme_name);
    696  1.45.4.3     joerg 	LIST_REMOVE(sme, sme_list);
    697  1.45.4.3     joerg 	mutex_exit(&sme_mtx);
    698      1.27   xtraeme 	/*
    699  1.45.4.2  jmcneill 	 * Remove the device (and all its objects) from the global dictionary.
    700      1.27   xtraeme 	 */
    701  1.45.4.2  jmcneill 	array = prop_dictionary_get(sme_propd, sme->sme_name);
    702  1.45.4.3     joerg 	if (array && prop_object_type(array) == PROP_TYPE_ARRAY) {
    703  1.45.4.2  jmcneill 		prop_dictionary_remove(sme_propd, sme->sme_name);
    704  1.45.4.3     joerg 		sysmon_envsys_destroy_plist(array);
    705      1.27   xtraeme 	}
    706       1.1   thorpej }
    707       1.1   thorpej 
    708       1.1   thorpej /*
    709       1.1   thorpej  * sysmon_envsys_find:
    710       1.1   thorpej  *
    711  1.45.4.2  jmcneill  *	+ Find a sysmon envsys device.
    712       1.1   thorpej  */
    713       1.1   thorpej struct sysmon_envsys *
    714      1.18   xtraeme sysmon_envsys_find(const char *name)
    715       1.1   thorpej {
    716       1.1   thorpej 	struct sysmon_envsys *sme;
    717       1.1   thorpej 
    718  1.45.4.3     joerg 	mutex_enter(&sme_mtx);
    719       1.7      yamt again:
    720  1.45.4.4     joerg 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    721       1.1   thorpej 	     sme = LIST_NEXT(sme, sme_list)) {
    722      1.18   xtraeme 			if (strcmp(sme->sme_name, name) == 0) {
    723      1.18   xtraeme 				if (sme->sme_flags & SME_FLAG_BUSY) {
    724      1.18   xtraeme 					sme->sme_flags |= SME_FLAG_WANTED;
    725  1.45.4.3     joerg 					cv_wait(&sme_cv, &sme_mtx);
    726      1.18   xtraeme 					goto again;
    727      1.18   xtraeme 				}
    728      1.18   xtraeme 				sme->sme_flags |= SME_FLAG_BUSY;
    729      1.18   xtraeme 				break;
    730      1.18   xtraeme 			}
    731      1.18   xtraeme 	}
    732  1.45.4.3     joerg 	mutex_exit(&sme_mtx);
    733      1.18   xtraeme 	return sme;
    734      1.18   xtraeme }
    735      1.18   xtraeme 
    736      1.18   xtraeme /*
    737      1.18   xtraeme  * sysmon_envsys_release:
    738      1.18   xtraeme  *
    739  1.45.4.2  jmcneill  * 	+ Release a sysmon envsys device.
    740      1.18   xtraeme  */
    741      1.18   xtraeme void
    742      1.18   xtraeme sysmon_envsys_release(struct sysmon_envsys *sme)
    743      1.18   xtraeme {
    744  1.45.4.3     joerg 	mutex_enter(&sme_mtx);
    745      1.18   xtraeme 	if (sme->sme_flags & SME_FLAG_WANTED)
    746  1.45.4.3     joerg 		cv_broadcast(&sme_cv);
    747      1.18   xtraeme 	sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
    748  1.45.4.3     joerg 	mutex_exit(&sme_mtx);
    749      1.18   xtraeme }
    750      1.18   xtraeme 
    751      1.18   xtraeme /* compatibility function */
    752      1.18   xtraeme struct sysmon_envsys *
    753      1.18   xtraeme sysmon_envsys_find_40(u_int idx)
    754      1.18   xtraeme {
    755      1.18   xtraeme 	struct sysmon_envsys *sme;
    756      1.18   xtraeme 
    757  1.45.4.3     joerg 	mutex_enter(&sme_mtx);
    758  1.45.4.4     joerg 	for (sme = LIST_FIRST(&sysmon_envsys_list); sme;
    759      1.18   xtraeme 	     sme = LIST_NEXT(sme, sme_list)) {
    760       1.1   thorpej 		if (idx >= sme->sme_fsensor &&
    761      1.18   xtraeme 	    	    idx < (sme->sme_fsensor + sme->sme_nsensors))
    762      1.18   xtraeme 			break;
    763      1.18   xtraeme 	}
    764  1.45.4.3     joerg 	mutex_exit(&sme_mtx);
    765      1.18   xtraeme 	return sme;
    766      1.18   xtraeme }
    767      1.18   xtraeme 
    768      1.18   xtraeme /*
    769  1.45.4.4     joerg  * sme_sensor_dictionary_get:
    770  1.45.4.4     joerg  *
    771  1.45.4.4     joerg  * 	+ Returns a dictionary of a device specified by 'index'.
    772  1.45.4.4     joerg  */
    773  1.45.4.4     joerg prop_dictionary_t
    774  1.45.4.4     joerg sme_sensor_dictionary_get(prop_array_t array, const char *index)
    775  1.45.4.4     joerg {
    776  1.45.4.4     joerg 	prop_object_iterator_t iter;
    777  1.45.4.4     joerg 	prop_dictionary_t dict;
    778  1.45.4.4     joerg 	prop_object_t obj;
    779  1.45.4.4     joerg 
    780  1.45.4.4     joerg 	KASSERT(array != NULL || index != NULL);
    781  1.45.4.4     joerg 
    782  1.45.4.4     joerg 	iter = prop_array_iterator(array);
    783  1.45.4.4     joerg 	if (!iter)
    784  1.45.4.4     joerg 		return NULL;
    785  1.45.4.4     joerg 
    786  1.45.4.4     joerg 	while ((dict = prop_object_iterator_next(iter))) {
    787  1.45.4.4     joerg 		obj = prop_dictionary_get(dict, "index");
    788  1.45.4.4     joerg 		if (prop_string_equals_cstring(obj, index))
    789  1.45.4.4     joerg 			break;
    790  1.45.4.4     joerg 	}
    791  1.45.4.4     joerg 
    792  1.45.4.4     joerg 	prop_object_iterator_release(iter);
    793  1.45.4.4     joerg 	return dict;
    794  1.45.4.4     joerg }
    795  1.45.4.4     joerg 
    796  1.45.4.4     joerg /*
    797  1.45.4.4     joerg  * sme_remove_userprops:
    798  1.45.4.4     joerg  *
    799  1.45.4.4     joerg  * 	+ Remove all properties from all devices that were set by
    800  1.45.4.4     joerg  * 	  the userland.
    801  1.45.4.4     joerg  */
    802  1.45.4.4     joerg static void
    803  1.45.4.4     joerg sme_remove_userprops(void)
    804  1.45.4.4     joerg {
    805  1.45.4.4     joerg 	struct sysmon_envsys *sme;
    806  1.45.4.4     joerg 	prop_array_t array;
    807  1.45.4.4     joerg 	prop_dictionary_t sdict;
    808  1.45.4.4     joerg 	envsys_data_t *edata = NULL;
    809  1.45.4.4     joerg 	char tmp[ENVSYS_DESCLEN];
    810  1.45.4.4     joerg 	int i, ptype;
    811  1.45.4.4     joerg 
    812  1.45.4.4     joerg 	mutex_enter(&sme_mtx);
    813  1.45.4.4     joerg 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    814  1.45.4.4     joerg 		sme->sme_flags |= SME_FLAG_BUSY;
    815  1.45.4.4     joerg 		array = prop_dictionary_get(sme_propd, sme->sme_name);
    816  1.45.4.4     joerg 
    817  1.45.4.4     joerg 		for (i = 0; i < sme->sme_nsensors; i++) {
    818  1.45.4.4     joerg 			edata = &sme->sme_sensor_data[i];
    819  1.45.4.4     joerg 			(void)snprintf(tmp, sizeof(tmp), "sensor%d",
    820  1.45.4.4     joerg 				       edata->sensor);
    821  1.45.4.4     joerg 			sdict = sme_sensor_dictionary_get(array, tmp);
    822  1.45.4.4     joerg 
    823  1.45.4.4     joerg 			if (edata->upropset & USERPROP_BATTCAP) {
    824  1.45.4.4     joerg 				prop_dictionary_remove(sdict,
    825  1.45.4.4     joerg 				    "critical-capacity");
    826  1.45.4.4     joerg 				ptype = PENVSYS_EVENT_BATT_USERCAP;
    827  1.45.4.4     joerg 				sme_event_unregister(edata->desc, ptype);
    828  1.45.4.4     joerg 			}
    829  1.45.4.4     joerg 
    830  1.45.4.4     joerg 			if (edata->upropset & USERPROP_CRITMAX) {
    831  1.45.4.4     joerg 				prop_dictionary_remove(sdict,
    832  1.45.4.4     joerg 				    "critical-max");
    833  1.45.4.4     joerg 				ptype = PENVSYS_EVENT_USER_CRITMAX;
    834  1.45.4.4     joerg 				sme_event_unregister(edata->desc, ptype);
    835  1.45.4.4     joerg 			}
    836  1.45.4.4     joerg 
    837  1.45.4.4     joerg 			if (edata->upropset & USERPROP_CRITMIN) {
    838  1.45.4.4     joerg 				prop_dictionary_remove(sdict,
    839  1.45.4.4     joerg 				    "critical-min");
    840  1.45.4.4     joerg 				ptype = PENVSYS_EVENT_USER_CRITMIN;
    841  1.45.4.4     joerg 				sme_event_unregister(edata->desc, ptype);
    842  1.45.4.4     joerg 			}
    843  1.45.4.4     joerg 
    844  1.45.4.4     joerg 			if (edata->upropset & USERPROP_RFACT) {
    845  1.45.4.4     joerg 				(void)sme_sensor_upint32(sdict, "rfact", 0);
    846  1.45.4.4     joerg 				edata->rfact = 0;
    847  1.45.4.4     joerg 			}
    848  1.45.4.4     joerg 
    849  1.45.4.4     joerg 			if (edata->upropset & USERPROP_DESC)
    850  1.45.4.4     joerg 				(void)sme_sensor_upstring(sdict,
    851  1.45.4.4     joerg 			  	    "description", edata->desc);
    852  1.45.4.4     joerg 		}
    853  1.45.4.4     joerg 
    854  1.45.4.4     joerg 		if (edata->upropset)
    855  1.45.4.4     joerg 			edata->upropset = 0;
    856  1.45.4.4     joerg 
    857  1.45.4.4     joerg 		sme->sme_flags &= ~SME_FLAG_BUSY;
    858  1.45.4.4     joerg 	}
    859  1.45.4.4     joerg 	mutex_exit(&sme_mtx);
    860  1.45.4.4     joerg }
    861  1.45.4.4     joerg /*
    862      1.27   xtraeme  * sme_register_sensorname:
    863      1.27   xtraeme  *
    864  1.45.4.2  jmcneill  * 	+ Register a sensor description into the list maintained per device.
    865      1.27   xtraeme  */
    866      1.27   xtraeme static int
    867  1.45.4.2  jmcneill sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
    868      1.27   xtraeme {
    869      1.27   xtraeme 	struct sme_sensor_names *snames, *snames2 = NULL;
    870      1.27   xtraeme 
    871      1.35   xtraeme 	KASSERT(edata != NULL);
    872      1.35   xtraeme 
    873  1.45.4.2  jmcneill 	SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
    874      1.41   xtraeme 		/*
    875      1.41   xtraeme 		 * Match sensors with empty and duplicate description.
    876      1.41   xtraeme 		 */
    877      1.41   xtraeme 		if (strlen(edata->desc) == 0 ||
    878      1.41   xtraeme 		    strcmp(snames2->desc, edata->desc) == 0)
    879      1.34   xtraeme 			if (snames2->assigned) {
    880      1.41   xtraeme 				edata->flags |= ENVSYS_FNOTVALID;
    881  1.45.4.3     joerg 				DPRINTF(("%s: wrong sensor name='%s'\n",
    882  1.45.4.3     joerg 				    sme->sme_name, edata->desc));
    883      1.34   xtraeme 				return EEXIST;
    884      1.34   xtraeme 			}
    885      1.27   xtraeme 	}
    886      1.27   xtraeme 
    887  1.45.4.2  jmcneill 	snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
    888  1.45.4.4     joerg 	if (!snames)
    889  1.45.4.2  jmcneill 		return ENOMEM;
    890  1.45.4.2  jmcneill 
    891      1.27   xtraeme 	snames->assigned = true;
    892      1.27   xtraeme 	(void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
    893  1.45.4.3     joerg 	DPRINTF(("%s: registering sensor name='%s'\n",
    894  1.45.4.3     joerg 	    sme->sme_name, edata->desc));
    895  1.45.4.2  jmcneill 	SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
    896  1.45.4.2  jmcneill 	sme->sme_uniqsensors++;
    897      1.27   xtraeme 
    898      1.27   xtraeme 	return 0;
    899      1.27   xtraeme }
    900      1.27   xtraeme 
    901      1.27   xtraeme /*
    902  1.45.4.2  jmcneill  * sme_add_sensor_dictionary:
    903      1.18   xtraeme  *
    904  1.45.4.2  jmcneill  * 	+ Add the objects into the dictionary.
    905      1.18   xtraeme  */
    906  1.45.4.3     joerg sme_event_drv_t *
    907  1.45.4.2  jmcneill sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
    908  1.45.4.2  jmcneill 		    	  prop_dictionary_t dict, envsys_data_t *edata)
    909      1.18   xtraeme {
    910  1.45.4.3     joerg 	const struct sme_description_table *sdt, *sdt_units;
    911      1.18   xtraeme 	sme_event_drv_t *sme_evdrv_t = NULL;
    912      1.41   xtraeme 	int i, j;
    913  1.45.4.4     joerg 	char indexstr[ENVSYS_DESCLEN];
    914      1.18   xtraeme 
    915      1.41   xtraeme 	i = j = 0;
    916      1.27   xtraeme 
    917      1.18   xtraeme 	/* find the correct unit for this sensor. */
    918  1.45.4.3     joerg 	sdt_units = sme_get_description_table(SME_DESC_UNITS);
    919  1.45.4.3     joerg 	for (i = 0; sdt_units[i].type != -1; i++)
    920  1.45.4.3     joerg 		if (sdt_units[i].type == edata->units)
    921      1.18   xtraeme 			break;
    922      1.18   xtraeme 
    923  1.45.4.3     joerg 	if (strcmp(sdt_units[i].desc, "unknown") == 0) {
    924      1.42   xtraeme 		DPRINTF(("%s: invalid units type for sensor=%d\n",
    925      1.42   xtraeme 		    __func__, edata->sensor));
    926      1.41   xtraeme 		goto invalidate_sensor;
    927      1.41   xtraeme 	}
    928      1.41   xtraeme 
    929      1.18   xtraeme 	/*
    930  1.45.4.4     joerg 	 * Add the index sensor string.
    931  1.45.4.4     joerg 	 *
    932  1.45.4.4     joerg 	 * 		...
    933  1.45.4.4     joerg 	 * 		<key>index</key>
    934  1.45.4.4     joerg 	 * 		<string>sensor0</string>
    935  1.45.4.4     joerg 	 * 		...
    936  1.45.4.4     joerg 	 */
    937  1.45.4.4     joerg 	(void)snprintf(indexstr, sizeof(indexstr), "sensor%d", edata->sensor);
    938  1.45.4.4     joerg 	if (sme_sensor_upstring(dict, "index", indexstr))
    939  1.45.4.4     joerg 		goto invalidate_sensor;
    940  1.45.4.4     joerg 
    941  1.45.4.4     joerg 	/*
    942      1.18   xtraeme 	 * 		...
    943      1.18   xtraeme 	 * 		<key>type</key>
    944      1.18   xtraeme 	 * 		<string>foo</string>
    945      1.18   xtraeme 	 * 		<key>description</key>
    946      1.18   xtraeme 	 * 		<string>blah blah</string>
    947      1.18   xtraeme 	 * 		...
    948      1.18   xtraeme 	 */
    949  1.45.4.3     joerg 	if (sme_sensor_upstring(dict, "type", sdt_units[i].desc))
    950      1.41   xtraeme 		goto invalidate_sensor;
    951      1.41   xtraeme 
    952      1.41   xtraeme 	if (strlen(edata->desc) == 0) {
    953      1.42   xtraeme 		DPRINTF(("%s: invalid description for sensor=%d\n",
    954      1.42   xtraeme 		    __func__, edata->sensor));
    955      1.41   xtraeme 		goto invalidate_sensor;
    956      1.41   xtraeme 	}
    957      1.30   xtraeme 
    958      1.41   xtraeme 	if (sme_sensor_upstring(dict, "description", edata->desc))
    959      1.41   xtraeme 		goto invalidate_sensor;
    960      1.18   xtraeme 
    961      1.18   xtraeme 	/*
    962      1.18   xtraeme 	 * Add sensor's state description.
    963      1.18   xtraeme 	 *
    964      1.18   xtraeme 	 * 		...
    965      1.18   xtraeme 	 * 		<key>state</key>
    966      1.18   xtraeme 	 * 		<string>valid</string>
    967      1.18   xtraeme 	 * 		...
    968      1.18   xtraeme 	 */
    969  1.45.4.3     joerg 	sdt = sme_get_description_table(SME_DESC_STATES);
    970  1.45.4.3     joerg 	for (j = 0; sdt[j].type != -1; j++)
    971  1.45.4.3     joerg 		if (sdt[j].type == edata->state)
    972       1.7      yamt 			break;
    973      1.18   xtraeme 
    974  1.45.4.3     joerg 	if (strcmp(sdt[j].desc, "unknown") == 0) {
    975      1.42   xtraeme 		DPRINTF(("%s: invalid state for sensor=%d\n",
    976      1.42   xtraeme 		    __func__, edata->sensor));
    977      1.41   xtraeme 		goto invalidate_sensor;
    978      1.41   xtraeme 	}
    979      1.41   xtraeme 
    980      1.41   xtraeme 	DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
    981      1.41   xtraeme 	    __func__, edata->desc, edata->units, edata->state));
    982      1.41   xtraeme 
    983  1.45.4.3     joerg 	if (sme_sensor_upstring(dict, "state", sdt[j].desc))
    984      1.41   xtraeme 		goto invalidate_sensor;
    985      1.18   xtraeme 
    986      1.18   xtraeme 	/*
    987      1.18   xtraeme 	 * Add the monitoring boolean object:
    988      1.18   xtraeme 	 *
    989      1.18   xtraeme 	 * 		...
    990      1.18   xtraeme 	 * 		<key>monitoring-supported</key>
    991      1.18   xtraeme 	 * 		<true/>
    992      1.18   xtraeme 	 *		...
    993      1.18   xtraeme 	 *
    994  1.45.4.3     joerg 	 * always false on Battery state, Drive and Indicator types.
    995  1.45.4.2  jmcneill 	 * They cannot be monitored.
    996      1.18   xtraeme 	 *
    997      1.18   xtraeme 	 */
    998      1.18   xtraeme 	if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
    999      1.18   xtraeme 	    (edata->units == ENVSYS_INDICATOR) ||
   1000  1.45.4.2  jmcneill 	    (edata->units == ENVSYS_DRIVE) ||
   1001  1.45.4.3     joerg 	    (edata->units == ENVSYS_BATTERY_STATE)) {
   1002      1.41   xtraeme 		if (sme_sensor_upbool(dict, "monitoring-supported", false))
   1003  1.45.4.3     joerg 			goto out;
   1004      1.18   xtraeme 	} else {
   1005      1.41   xtraeme 		if (sme_sensor_upbool(dict, "monitoring-supported", true))
   1006  1.45.4.3     joerg 			goto out;
   1007  1.45.4.2  jmcneill 	}
   1008  1.45.4.2  jmcneill 
   1009  1.45.4.2  jmcneill 	/*
   1010  1.45.4.4     joerg 	 * Add the percentage boolean object, true if ENVSYS_FPERCENT
   1011  1.45.4.4     joerg 	 * is set or false otherwise.
   1012  1.45.4.2  jmcneill 	 *
   1013  1.45.4.2  jmcneill 	 * 		...
   1014  1.45.4.3     joerg 	 * 		<key>want-percentage</key>
   1015  1.45.4.3     joerg 	 * 		<true/>
   1016  1.45.4.2  jmcneill 	 * 		...
   1017  1.45.4.2  jmcneill 	 */
   1018  1.45.4.3     joerg 	if (edata->flags & ENVSYS_FPERCENT)
   1019  1.45.4.3     joerg 		if (sme_sensor_upbool(dict, "want-percentage", true))
   1020  1.45.4.3     joerg 			goto out;
   1021      1.18   xtraeme 
   1022      1.18   xtraeme 	/*
   1023  1.45.4.4     joerg 	 * Add the allow-rfact boolean object, true if
   1024  1.45.4.4     joerg 	 * ENVSYS_FCHANGERFACT if set or false otherwise.
   1025  1.45.4.4     joerg 	 *
   1026  1.45.4.4     joerg 	 * 		...
   1027  1.45.4.4     joerg 	 * 		<key>allow-rfact</key>
   1028  1.45.4.4     joerg 	 * 		<true/>
   1029  1.45.4.4     joerg 	 * 		...
   1030  1.45.4.4     joerg 	 */
   1031  1.45.4.4     joerg 	if (edata->units == ENVSYS_SVOLTS_DC ||
   1032  1.45.4.4     joerg 	    edata->units == ENVSYS_SVOLTS_AC) {
   1033  1.45.4.4     joerg 		if (edata->flags & ENVSYS_FCHANGERFACT) {
   1034  1.45.4.4     joerg 			if (sme_sensor_upbool(dict, "allow-rfact", true))
   1035  1.45.4.4     joerg 				goto out;
   1036  1.45.4.4     joerg 		} else {
   1037  1.45.4.4     joerg 			if (sme_sensor_upbool(dict, "allow-rfact", false))
   1038  1.45.4.4     joerg 				goto out;
   1039  1.45.4.4     joerg 		}
   1040  1.45.4.4     joerg 	}
   1041  1.45.4.4     joerg 
   1042  1.45.4.4     joerg 
   1043  1.45.4.4     joerg 	/*
   1044  1.45.4.3     joerg 	 * Add the battery-state object for battery state sensors:
   1045  1.45.4.2  jmcneill 	 *
   1046  1.45.4.2  jmcneill 	 * 		...
   1047  1.45.4.3     joerg 	 * 		<key>battery-state</key>
   1048  1.45.4.3     joerg 	 * 		<string>NORMAL</string>
   1049  1.45.4.2  jmcneill 	 * 		...
   1050  1.45.4.2  jmcneill 	 */
   1051  1.45.4.3     joerg 	if (edata->units == ENVSYS_BATTERY_STATE) {
   1052  1.45.4.3     joerg 		sdt = sme_get_description_table(SME_DESC_BATTERY_STATES);
   1053  1.45.4.3     joerg 		for (j = 0; sdt[j].type != -1; j++)
   1054  1.45.4.3     joerg 			if (sdt[j].type == edata->value_cur)
   1055  1.45.4.3     joerg 				break;
   1056  1.45.4.3     joerg 
   1057  1.45.4.3     joerg 		if (sme_sensor_upstring(dict, "battery-state", sdt[j].desc))
   1058  1.45.4.3     joerg 			goto out;
   1059  1.45.4.3     joerg 	}
   1060  1.45.4.2  jmcneill 
   1061  1.45.4.2  jmcneill 	/*
   1062      1.18   xtraeme 	 * Add the drive-state object for drive sensors:
   1063      1.18   xtraeme 	 *
   1064      1.18   xtraeme 	 * 		...
   1065      1.18   xtraeme 	 * 		<key>drive-state</key>
   1066      1.18   xtraeme 	 * 		<string>drive is online</string>
   1067      1.18   xtraeme 	 * 		...
   1068      1.18   xtraeme 	 */
   1069      1.18   xtraeme 	if (edata->units == ENVSYS_DRIVE) {
   1070  1.45.4.3     joerg 		sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1071  1.45.4.3     joerg 		for (j = 0; sdt[j].type != -1; j++)
   1072  1.45.4.3     joerg 			if (sdt[j].type == edata->value_cur)
   1073      1.18   xtraeme 				break;
   1074      1.30   xtraeme 
   1075  1.45.4.3     joerg 		if (sme_sensor_upstring(dict, "drive-state", sdt[j].desc))
   1076  1.45.4.3     joerg 			goto out;
   1077      1.18   xtraeme 	}
   1078      1.18   xtraeme 
   1079  1.45.4.2  jmcneill 	/*
   1080  1.45.4.2  jmcneill 	 * if sensor is enabled, add the following properties...
   1081  1.45.4.2  jmcneill 	 */
   1082      1.18   xtraeme 	if (edata->state == ENVSYS_SVALID) {
   1083      1.18   xtraeme 		/*
   1084      1.18   xtraeme 		 * 	...
   1085      1.18   xtraeme 		 * 	<key>rpms</key>
   1086      1.18   xtraeme 		 * 	<integer>2500</integer>
   1087      1.18   xtraeme 		 * 	<key>rfact</key>
   1088      1.18   xtraeme 		 * 	<integer>10000</integer>
   1089      1.18   xtraeme 		 * 	<key>cur-value</key>
   1090      1.18   xtraeme 		 * 	<integer>1250</integer>
   1091      1.18   xtraeme 		 * 	<key>min-value</key>
   1092      1.18   xtraeme 		 * 	<integer>800</integer>
   1093      1.18   xtraeme 		 * 	<key>max-value</integer>
   1094      1.18   xtraeme 		 * 	<integer>3000</integer>
   1095      1.18   xtraeme 		 * 	<key>avg-value</integer>
   1096      1.18   xtraeme 		 * 	<integer>1400</integer>
   1097      1.18   xtraeme 		 * </dict>
   1098      1.18   xtraeme 		 */
   1099      1.41   xtraeme 		if (edata->units == ENVSYS_SFANRPM)
   1100      1.41   xtraeme 			if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
   1101  1.45.4.3     joerg 				goto out;
   1102      1.18   xtraeme 
   1103      1.41   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1104      1.41   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC)
   1105      1.41   xtraeme 			if (sme_sensor_upint32(dict, "rfact", edata->rfact))
   1106  1.45.4.3     joerg 				goto out;
   1107      1.18   xtraeme 
   1108      1.41   xtraeme 		if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
   1109  1.45.4.3     joerg 			goto out;
   1110      1.30   xtraeme 
   1111      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1112      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1113      1.41   xtraeme 					       "min-value",
   1114      1.41   xtraeme 					       edata->value_min))
   1115  1.45.4.3     joerg 				goto out;
   1116      1.30   xtraeme 		}
   1117      1.30   xtraeme 
   1118      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1119      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1120      1.41   xtraeme 					       "max-value",
   1121      1.41   xtraeme 					       edata->value_max))
   1122  1.45.4.3     joerg 				goto out;
   1123      1.30   xtraeme 		}
   1124      1.30   xtraeme 
   1125      1.41   xtraeme 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1126      1.41   xtraeme 			if (sme_sensor_upint32(dict,
   1127      1.41   xtraeme 					       "avg-value",
   1128      1.41   xtraeme 					        edata->value_avg))
   1129  1.45.4.3     joerg 				goto out;
   1130      1.30   xtraeme 		}
   1131      1.18   xtraeme 	}
   1132      1.18   xtraeme 
   1133      1.18   xtraeme 	/*
   1134      1.18   xtraeme 	 * 	...
   1135      1.18   xtraeme 	 * </array>
   1136  1.45.4.2  jmcneill 	 *
   1137  1.45.4.2  jmcneill 	 * Add the dictionary into the array.
   1138  1.45.4.2  jmcneill 	 *
   1139      1.18   xtraeme 	 */
   1140      1.41   xtraeme 
   1141  1.45.4.2  jmcneill 	if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
   1142      1.41   xtraeme 		DPRINTF(("%s: prop_array_add\n", __func__));
   1143      1.41   xtraeme 		goto invalidate_sensor;
   1144      1.41   xtraeme 	}
   1145      1.41   xtraeme 
   1146      1.41   xtraeme 	/*
   1147      1.41   xtraeme 	 * Add a new event if a monitoring flag was set.
   1148      1.41   xtraeme 	 */
   1149      1.41   xtraeme 	if (edata->monitor) {
   1150  1.45.4.3     joerg 		sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_SLEEP);
   1151      1.41   xtraeme 		sme_evdrv_t->sdict = dict;
   1152      1.41   xtraeme 		sme_evdrv_t->edata = edata;
   1153      1.41   xtraeme 		sme_evdrv_t->sme = sme;
   1154  1.45.4.3     joerg 		sme_evdrv_t->powertype = sdt_units[i].crittype;
   1155      1.41   xtraeme 	}
   1156      1.41   xtraeme 
   1157  1.45.4.3     joerg out:
   1158  1.45.4.3     joerg 	return sme_evdrv_t;
   1159      1.41   xtraeme 
   1160      1.41   xtraeme invalidate_sensor:
   1161      1.41   xtraeme 	edata->flags |= ENVSYS_FNOTVALID;
   1162  1.45.4.3     joerg 	prop_object_release(dict);
   1163  1.45.4.3     joerg 	return sme_evdrv_t;
   1164      1.18   xtraeme }
   1165      1.18   xtraeme 
   1166      1.18   xtraeme /*
   1167      1.18   xtraeme  * sme_update_dictionary:
   1168      1.18   xtraeme  *
   1169      1.18   xtraeme  * 	+ Update per-sensor dictionaries with new values if there were
   1170      1.18   xtraeme  * 	  changes, otherwise the object in dictionary is untouched.
   1171      1.18   xtraeme  */
   1172      1.18   xtraeme int
   1173      1.18   xtraeme sme_update_dictionary(struct sysmon_envsys *sme)
   1174      1.18   xtraeme {
   1175  1.45.4.3     joerg 	const struct sme_description_table *sdt;
   1176  1.45.4.3     joerg 	envsys_data_t *edata;
   1177      1.34   xtraeme 	prop_object_t array, dict;
   1178      1.41   xtraeme 	int i, j, error, invalid;
   1179      1.18   xtraeme 
   1180  1.45.4.3     joerg 	KASSERT(mutex_owned(&sme_mtx));
   1181  1.45.4.2  jmcneill 
   1182      1.41   xtraeme 	error = invalid = 0;
   1183      1.34   xtraeme 	array = dict = NULL;
   1184      1.30   xtraeme 
   1185      1.18   xtraeme 	/* retrieve the array of dictionaries in device. */
   1186      1.18   xtraeme 	array = prop_dictionary_get(sme_propd, sme->sme_name);
   1187      1.24   xtraeme 	if (prop_object_type(array) != PROP_TYPE_ARRAY) {
   1188      1.24   xtraeme 		DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
   1189      1.18   xtraeme 		return EINVAL;
   1190      1.24   xtraeme 	}
   1191      1.18   xtraeme 
   1192      1.18   xtraeme 	/*
   1193      1.18   xtraeme 	 * - iterate over all sensors.
   1194      1.18   xtraeme 	 * - fetch new data.
   1195      1.18   xtraeme 	 * - check if data in dictionary is different than new data.
   1196      1.18   xtraeme 	 * - update dictionary if there were changes.
   1197      1.18   xtraeme 	 */
   1198  1.45.4.3     joerg 	DPRINTF(("%s: updating '%s' with nsensors=%d\n", __func__,
   1199  1.45.4.3     joerg 	    sme->sme_name, sme->sme_nsensors));
   1200  1.45.4.3     joerg 
   1201      1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
   1202      1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
   1203      1.41   xtraeme 		/* skip invalid sensors */
   1204      1.41   xtraeme 		if (edata->flags & ENVSYS_FNOTVALID) {
   1205  1.45.4.3     joerg 			DPRINTF(("%s: invalid sensor=%s idx=%d\n",
   1206  1.45.4.3     joerg 			    __func__, edata->desc, edata->sensor));
   1207      1.41   xtraeme 			invalid++;
   1208      1.23   xtraeme 			continue;
   1209      1.24   xtraeme 		}
   1210      1.23   xtraeme 
   1211      1.18   xtraeme 		/*
   1212      1.18   xtraeme 		 * refresh sensor data via sme_gtredata only if the
   1213      1.18   xtraeme 		 * flag is not set.
   1214      1.18   xtraeme 		 */
   1215      1.18   xtraeme 		if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
   1216      1.18   xtraeme 			error = (*sme->sme_gtredata)(sme, edata);
   1217      1.18   xtraeme 			if (error) {
   1218      1.18   xtraeme 				DPRINTF(("%s: gtredata[%d] failed\n",
   1219      1.18   xtraeme 				    __func__, i));
   1220      1.18   xtraeme 				return error;
   1221      1.18   xtraeme 			}
   1222      1.18   xtraeme 		}
   1223      1.18   xtraeme 
   1224      1.18   xtraeme 		/* retrieve sensor's dictionary. */
   1225      1.41   xtraeme 		dict = prop_array_get(array, i - invalid);
   1226      1.24   xtraeme 		if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
   1227      1.24   xtraeme 			DPRINTF(("%s: not a dictionary (%d:%s)\n",
   1228      1.24   xtraeme 			    __func__, edata->sensor, sme->sme_name));
   1229      1.18   xtraeme 			return EINVAL;
   1230      1.24   xtraeme 		}
   1231      1.18   xtraeme 
   1232  1.45.4.3     joerg 		/* update sensor's state */
   1233  1.45.4.3     joerg 		sdt = sme_get_description_table(SME_DESC_STATES);
   1234  1.45.4.3     joerg 		for (j = 0; sdt[j].type != -1; j++)
   1235  1.45.4.3     joerg 			if (sdt[j].type == edata->state)
   1236      1.18   xtraeme 				break;
   1237      1.18   xtraeme 
   1238      1.18   xtraeme 		DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
   1239  1.45.4.3     joerg 		    "units=%d sensor=%d\n", __func__, sdt[j].desc,
   1240  1.45.4.3     joerg 		    sdt[j].type, edata->flags, edata->units, edata->sensor));
   1241      1.18   xtraeme 
   1242      1.18   xtraeme 		/* update sensor state */
   1243  1.45.4.3     joerg 		error = sme_sensor_upstring(dict, "state", sdt[j].desc);
   1244      1.30   xtraeme 		if (error)
   1245      1.30   xtraeme 			break;
   1246      1.18   xtraeme 
   1247  1.45.4.3     joerg 		/* update sensor type */
   1248  1.45.4.3     joerg 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1249  1.45.4.3     joerg 		for (j = 0; sdt[j].type != -1; j++)
   1250  1.45.4.3     joerg 			if (sdt[j].type == edata->units)
   1251      1.45   xtraeme 				break;
   1252      1.45   xtraeme 
   1253  1.45.4.3     joerg 		error = sme_sensor_upstring(dict, "type", sdt[j].desc);
   1254      1.45   xtraeme 		if (error)
   1255      1.45   xtraeme 			break;
   1256      1.45   xtraeme 
   1257      1.18   xtraeme 		/* update sensor current value */
   1258      1.34   xtraeme 		error = sme_sensor_upint32(dict,
   1259      1.30   xtraeme 					   "cur-value",
   1260      1.30   xtraeme 					   edata->value_cur);
   1261      1.30   xtraeme 		if (error)
   1262      1.30   xtraeme 			break;
   1263      1.18   xtraeme 
   1264      1.18   xtraeme 		/*
   1265      1.18   xtraeme 		 * Integer and Indicator types do not the following
   1266      1.18   xtraeme 		 * values, so skip them.
   1267      1.18   xtraeme 		 */
   1268      1.18   xtraeme 		if (edata->units == ENVSYS_INTEGER ||
   1269      1.18   xtraeme 		    edata->units == ENVSYS_INDICATOR)
   1270      1.18   xtraeme 			continue;
   1271      1.18   xtraeme 
   1272      1.18   xtraeme 		/* update sensor flags */
   1273      1.30   xtraeme 		if (edata->flags & ENVSYS_FPERCENT) {
   1274      1.34   xtraeme 			error = sme_sensor_upbool(dict,
   1275      1.30   xtraeme 						  "want-percentage",
   1276      1.30   xtraeme 						  true);
   1277      1.30   xtraeme 			if (error)
   1278      1.30   xtraeme 				break;
   1279      1.18   xtraeme 		}
   1280      1.18   xtraeme 
   1281      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_MAX) {
   1282      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1283      1.30   xtraeme 						   "max-value",
   1284      1.30   xtraeme 						   edata->value_max);
   1285      1.30   xtraeme 			if (error)
   1286      1.30   xtraeme 				break;
   1287      1.30   xtraeme 		}
   1288      1.30   xtraeme 
   1289      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_MIN) {
   1290      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1291      1.30   xtraeme 						   "min-value",
   1292      1.30   xtraeme 						   edata->value_min);
   1293      1.30   xtraeme 			if (error)
   1294      1.30   xtraeme 				break;
   1295      1.30   xtraeme 		}
   1296      1.18   xtraeme 
   1297      1.30   xtraeme 		if (edata->flags & ENVSYS_FVALID_AVG) {
   1298      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1299      1.30   xtraeme 						   "avg-value",
   1300      1.30   xtraeme 						   edata->value_avg);
   1301      1.30   xtraeme 			if (error)
   1302      1.30   xtraeme 				break;
   1303      1.30   xtraeme 		}
   1304      1.18   xtraeme 
   1305      1.18   xtraeme 		/* update 'rpms' only in ENVSYS_SFANRPM. */
   1306      1.30   xtraeme 		if (edata->units == ENVSYS_SFANRPM) {
   1307      1.34   xtraeme 			error = sme_sensor_upuint32(dict,
   1308      1.30   xtraeme 						    "rpms",
   1309      1.30   xtraeme 						    edata->rpms);
   1310      1.30   xtraeme 			if (error)
   1311      1.30   xtraeme 				break;
   1312      1.30   xtraeme 		}
   1313      1.18   xtraeme 
   1314      1.18   xtraeme 		/* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
   1315      1.18   xtraeme 		if (edata->units == ENVSYS_SVOLTS_AC ||
   1316      1.18   xtraeme 		    edata->units == ENVSYS_SVOLTS_DC) {
   1317      1.34   xtraeme 			error = sme_sensor_upint32(dict,
   1318      1.30   xtraeme 						   "rfact",
   1319      1.30   xtraeme 						   edata->rfact);
   1320      1.30   xtraeme 			if (error)
   1321      1.30   xtraeme 				break;
   1322      1.18   xtraeme 		}
   1323      1.18   xtraeme 
   1324      1.18   xtraeme 		/* update 'drive-state' only in ENVSYS_DRIVE. */
   1325      1.18   xtraeme 		if (edata->units == ENVSYS_DRIVE) {
   1326  1.45.4.3     joerg 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
   1327  1.45.4.3     joerg 			for (j = 0; sdt[j].type != -1; j++)
   1328  1.45.4.3     joerg 				if (sdt[j].type == edata->value_cur)
   1329      1.18   xtraeme 					break;
   1330      1.18   xtraeme 
   1331      1.34   xtraeme 			error = sme_sensor_upstring(dict,
   1332      1.30   xtraeme 						    "drive-state",
   1333  1.45.4.3     joerg 						    sdt[j].desc);
   1334  1.45.4.3     joerg 			if (error)
   1335  1.45.4.3     joerg 				break;
   1336  1.45.4.3     joerg 		}
   1337  1.45.4.3     joerg 
   1338  1.45.4.3     joerg 		/* update 'battery-state' only in ENVSYS_BATTERY_STATE. */
   1339  1.45.4.3     joerg 		if (edata->units == ENVSYS_BATTERY_STATE) {
   1340  1.45.4.3     joerg 			sdt =
   1341  1.45.4.3     joerg 			    sme_get_description_table(SME_DESC_BATTERY_STATES);
   1342  1.45.4.3     joerg 			for (j = 0; sdt[j].type != -1; j++)
   1343  1.45.4.3     joerg 				if (sdt[j].type == edata->value_cur)
   1344  1.45.4.3     joerg 					break;
   1345  1.45.4.3     joerg 
   1346  1.45.4.3     joerg 			error = sme_sensor_upstring(dict,
   1347  1.45.4.3     joerg 						    "battery-state",
   1348  1.45.4.3     joerg 						    sdt[j].desc);
   1349      1.32   xtraeme 			if (error)
   1350      1.32   xtraeme 				break;
   1351       1.7      yamt 		}
   1352       1.1   thorpej 	}
   1353       1.1   thorpej 
   1354      1.18   xtraeme 	return error;
   1355       1.1   thorpej }
   1356       1.1   thorpej 
   1357       1.1   thorpej /*
   1358      1.18   xtraeme  * sme_userset_dictionary:
   1359       1.1   thorpej  *
   1360  1.45.4.4     joerg  * 	+ Parse the userland sensor's dictionary and run the appropiate
   1361  1.45.4.4     joerg  * 	  tasks that was requested.
   1362       1.1   thorpej  */
   1363      1.18   xtraeme int
   1364      1.18   xtraeme sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
   1365      1.18   xtraeme 		       prop_array_t array)
   1366       1.1   thorpej {
   1367  1.45.4.3     joerg 	const struct sme_description_table *sdt;
   1368      1.23   xtraeme 	envsys_data_t *edata, *nedata;
   1369      1.18   xtraeme 	prop_dictionary_t dict;
   1370      1.18   xtraeme 	prop_object_t obj, obj1, obj2;
   1371      1.18   xtraeme 	int32_t critval;
   1372      1.41   xtraeme 	int i, invalid, error;
   1373      1.18   xtraeme 	const char *blah, *sname;
   1374      1.18   xtraeme 	bool targetfound = false;
   1375      1.18   xtraeme 
   1376      1.41   xtraeme 	error = invalid = 0;
   1377      1.18   xtraeme 	blah = sname = NULL;
   1378      1.18   xtraeme 
   1379  1.45.4.4     joerg 	/* get sensor's index from userland dictionary. */
   1380  1.45.4.4     joerg 	obj = prop_dictionary_get(udict, "index");
   1381      1.18   xtraeme 	if (prop_object_type(obj) != PROP_TYPE_STRING) {
   1382      1.18   xtraeme 		DPRINTF(("%s: sensor-name failed\n", __func__));
   1383      1.18   xtraeme 		return EINVAL;
   1384      1.18   xtraeme 	}
   1385       1.1   thorpej 
   1386      1.18   xtraeme 	/* iterate over the sensors to find the right one */
   1387      1.18   xtraeme 	for (i = 0; i < sme->sme_nsensors; i++) {
   1388      1.18   xtraeme 		edata = &sme->sme_sensor_data[i];
   1389  1.45.4.4     joerg 		/*
   1390  1.45.4.4     joerg 		 * skip invalid sensors.
   1391  1.45.4.4     joerg 		 */
   1392      1.41   xtraeme 		if (edata->flags & ENVSYS_FNOTVALID) {
   1393      1.41   xtraeme 			invalid++;
   1394      1.27   xtraeme 			continue;
   1395      1.40   xtraeme 		}
   1396      1.27   xtraeme 
   1397      1.41   xtraeme 		dict = prop_array_get(array, i - invalid);
   1398  1.45.4.4     joerg 		obj1 = prop_dictionary_get(dict, "index");
   1399      1.18   xtraeme 
   1400      1.18   xtraeme 		/* is it our sensor? */
   1401      1.18   xtraeme 		if (!prop_string_equals(obj1, obj))
   1402      1.18   xtraeme 			continue;
   1403      1.18   xtraeme 
   1404      1.18   xtraeme 		/*
   1405      1.18   xtraeme 		 * Check if a new description operation was
   1406      1.18   xtraeme 		 * requested by the user and set new description.
   1407      1.18   xtraeme 		 */
   1408  1.45.4.4     joerg 		if ((obj2 = prop_dictionary_get(udict, "description"))) {
   1409      1.18   xtraeme 			targetfound = true;
   1410      1.18   xtraeme 			blah = prop_string_cstring_nocopy(obj2);
   1411      1.23   xtraeme 
   1412      1.23   xtraeme 			for (i = 0; i < sme->sme_nsensors; i++) {
   1413      1.23   xtraeme 				if (i == edata->sensor)
   1414      1.23   xtraeme 					continue;
   1415      1.23   xtraeme 
   1416      1.23   xtraeme 				nedata = &sme->sme_sensor_data[i];
   1417      1.23   xtraeme 				if (strcmp(blah, nedata->desc) == 0) {
   1418      1.23   xtraeme 					error = EEXIST;
   1419      1.30   xtraeme 					break;
   1420      1.23   xtraeme 				}
   1421      1.23   xtraeme 			}
   1422      1.23   xtraeme 
   1423      1.31   xtraeme 			if (error)
   1424      1.31   xtraeme 				break;
   1425      1.31   xtraeme 
   1426      1.34   xtraeme 			error = sme_sensor_upstring(dict,
   1427      1.30   xtraeme 						    "description",
   1428      1.30   xtraeme 						    blah);
   1429      1.18   xtraeme 			if (error)
   1430      1.18   xtraeme 				break;
   1431      1.18   xtraeme 
   1432  1.45.4.4     joerg 			edata->upropset |= USERPROP_DESC;
   1433      1.18   xtraeme 		}
   1434      1.18   xtraeme 
   1435  1.45.4.4     joerg 		/*
   1436  1.45.4.4     joerg 		 * did the user want to change the rfact?
   1437  1.45.4.4     joerg 		 */
   1438  1.45.4.4     joerg 		obj2 = prop_dictionary_get(udict, "rfact");
   1439  1.45.4.4     joerg 		if (obj2) {
   1440      1.18   xtraeme 			targetfound = true;
   1441  1.45.4.4     joerg 			if (edata->flags & ENVSYS_FCHANGERFACT) {
   1442      1.18   xtraeme 				edata->rfact = prop_number_integer_value(obj2);
   1443  1.45.4.4     joerg 				edata->upropset |= USERPROP_RFACT;
   1444  1.45.4.4     joerg 			} else {
   1445      1.18   xtraeme 				error = ENOTSUP;
   1446  1.45.4.4     joerg 				break;
   1447  1.45.4.4     joerg 			}
   1448      1.18   xtraeme 		}
   1449      1.18   xtraeme 
   1450  1.45.4.3     joerg 		sdt = sme_get_description_table(SME_DESC_UNITS);
   1451  1.45.4.3     joerg 		for (i = 0; sdt[i].type != -1; i++)
   1452  1.45.4.3     joerg 			if (sdt[i].type == edata->units)
   1453      1.18   xtraeme 				break;
   1454      1.18   xtraeme 
   1455  1.45.4.4     joerg 
   1456  1.45.4.4     joerg 		/*
   1457  1.45.4.4     joerg 		 * did the user want to set a critical capacity event?
   1458  1.45.4.4     joerg 		 *
   1459  1.45.4.4     joerg 		 * NOTE: if sme_event_register returns EEXIST that means
   1460  1.45.4.4     joerg 		 * the object is already there, but this is not a real
   1461  1.45.4.4     joerg 		 * error, because the object might be updated.
   1462  1.45.4.4     joerg 		 */
   1463      1.18   xtraeme 		obj2 = prop_dictionary_get(udict, "critical-capacity");
   1464  1.45.4.4     joerg 		if (obj2) {
   1465      1.18   xtraeme 			targetfound = true;
   1466      1.18   xtraeme 			if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
   1467      1.18   xtraeme 			    (edata->flags & ENVSYS_FPERCENT) == 0) {
   1468      1.18   xtraeme 				error = ENOTSUP;
   1469      1.18   xtraeme 				break;
   1470      1.18   xtraeme 			}
   1471      1.18   xtraeme 
   1472      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1473  1.45.4.2  jmcneill 			error = sme_event_register(dict,
   1474      1.18   xtraeme 					      edata,
   1475      1.18   xtraeme 					      sme->sme_name,
   1476      1.18   xtraeme 					      "critical-capacity",
   1477      1.18   xtraeme 					      critval,
   1478      1.18   xtraeme 					      PENVSYS_EVENT_BATT_USERCAP,
   1479  1.45.4.3     joerg 					      sdt[i].crittype);
   1480  1.45.4.4     joerg 			if (error == EEXIST)
   1481  1.45.4.4     joerg 				error = 0;
   1482  1.45.4.4     joerg 			if (!error)
   1483  1.45.4.4     joerg 				edata->upropset |= USERPROP_BATTCAP;
   1484      1.18   xtraeme 		}
   1485      1.18   xtraeme 
   1486  1.45.4.4     joerg 		/*
   1487  1.45.4.4     joerg 		 * did the user want to set a critical max event?
   1488  1.45.4.4     joerg 		 */
   1489  1.45.4.4     joerg 		obj2 = prop_dictionary_get(udict, "critical-max");
   1490  1.45.4.4     joerg 		if (obj2) {
   1491      1.18   xtraeme 			targetfound = true;
   1492      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1493      1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1494      1.18   xtraeme 				error = ENOTSUP;
   1495      1.18   xtraeme 				break;
   1496      1.18   xtraeme 			}
   1497      1.18   xtraeme 
   1498      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1499  1.45.4.2  jmcneill 			error = sme_event_register(dict,
   1500      1.18   xtraeme 					      edata,
   1501      1.18   xtraeme 					      sme->sme_name,
   1502  1.45.4.4     joerg 					      "critical-max",
   1503      1.18   xtraeme 					      critval,
   1504      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMAX,
   1505  1.45.4.3     joerg 					      sdt[i].crittype);
   1506  1.45.4.4     joerg 			if (error == EEXIST)
   1507  1.45.4.4     joerg 				error = 0;
   1508  1.45.4.4     joerg 			if (!error)
   1509  1.45.4.4     joerg 				edata->upropset |= USERPROP_CRITMAX;
   1510      1.18   xtraeme 		}
   1511      1.18   xtraeme 
   1512  1.45.4.4     joerg 		/*
   1513  1.45.4.4     joerg 		 * did the user want to set a critical min event?
   1514  1.45.4.4     joerg 		 */
   1515  1.45.4.4     joerg 		obj2 = prop_dictionary_get(udict, "critical-min");
   1516  1.45.4.4     joerg 		if (obj2) {
   1517      1.18   xtraeme 			targetfound = true;
   1518      1.18   xtraeme 			if (edata->units == ENVSYS_INDICATOR ||
   1519      1.18   xtraeme 			    edata->flags & ENVSYS_FMONNOTSUPP) {
   1520      1.18   xtraeme 				error = ENOTSUP;
   1521      1.18   xtraeme 				break;
   1522      1.18   xtraeme 			}
   1523      1.18   xtraeme 
   1524      1.18   xtraeme 			critval = prop_number_integer_value(obj2);
   1525  1.45.4.2  jmcneill 			error = sme_event_register(dict,
   1526      1.18   xtraeme 					      edata,
   1527      1.18   xtraeme 					      sme->sme_name,
   1528  1.45.4.4     joerg 					      "critical-min",
   1529      1.18   xtraeme 					      critval,
   1530      1.18   xtraeme 					      PENVSYS_EVENT_USER_CRITMIN,
   1531  1.45.4.3     joerg 					      sdt[i].crittype);
   1532  1.45.4.4     joerg 			if (error == EEXIST)
   1533  1.45.4.4     joerg 				error = 0;
   1534  1.45.4.4     joerg 			if (!error)
   1535  1.45.4.4     joerg 				edata->upropset |= USERPROP_CRITMIN;
   1536      1.18   xtraeme 		}
   1537  1.45.4.4     joerg 
   1538  1.45.4.4     joerg 		/*
   1539  1.45.4.4     joerg 		 * All objects in dictionary were processed.
   1540  1.45.4.4     joerg 		 */
   1541  1.45.4.4     joerg 		break;
   1542      1.18   xtraeme 	}
   1543      1.18   xtraeme 
   1544      1.18   xtraeme 	/* invalid target? return the error */
   1545      1.18   xtraeme 	if (!targetfound)
   1546      1.18   xtraeme 		error = EINVAL;
   1547      1.18   xtraeme 
   1548      1.18   xtraeme 	return error;
   1549       1.1   thorpej }
   1550