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