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