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