Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_util.c revision 1.1
      1  1.1  xtraeme /* $NetBSD: sysmon_envsys_util.c,v 1.1 2007/07/20 14:10:22 xtraeme Exp $ */
      2  1.1  xtraeme 
      3  1.1  xtraeme /*-
      4  1.1  xtraeme  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  1.1  xtraeme  * All rights reserved.
      6  1.1  xtraeme  *
      7  1.1  xtraeme  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  xtraeme  * by Juan Romero Pardines.
      9  1.1  xtraeme  *
     10  1.1  xtraeme  * Redistribution and use in source and binary forms, with or without
     11  1.1  xtraeme  * modification, are permitted provided that the following conditions
     12  1.1  xtraeme  * are met:
     13  1.1  xtraeme  * 1. Redistributions of source code must retain the above copyright
     14  1.1  xtraeme  *    notice, this list of conditions and the following disclaimer.
     15  1.1  xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  xtraeme  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  xtraeme  *    documentation and/or other materials provided with the distribution.
     18  1.1  xtraeme  * 3. All advertising materials mentioning features or use of this software
     19  1.1  xtraeme  *    must display the following acknowledgement:
     20  1.1  xtraeme  *      This product includes software developed by Juan Romero Pardines
     21  1.1  xtraeme  *      for the NetBSD Foundation, Inc. and its contributors.
     22  1.1  xtraeme  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  xtraeme  *    contributors may be used to endorse or promote products derived
     24  1.1  xtraeme  *    from this software without specific prior written permission.
     25  1.1  xtraeme  *
     26  1.1  xtraeme  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  xtraeme  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  xtraeme  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  xtraeme  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  xtraeme  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  xtraeme  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  xtraeme  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  xtraeme  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  xtraeme  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  xtraeme  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  xtraeme  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  xtraeme  */
     38  1.1  xtraeme 
     39  1.1  xtraeme #include <sys/cdefs.h>
     40  1.1  xtraeme __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_util.c,v 1.1 2007/07/20 14:10:22 xtraeme Exp $");
     41  1.1  xtraeme 
     42  1.1  xtraeme #include <sys/param.h>
     43  1.1  xtraeme #include <sys/types.h>
     44  1.1  xtraeme #include <sys/conf.h>
     45  1.1  xtraeme #include <sys/kernel.h>
     46  1.1  xtraeme 
     47  1.1  xtraeme #include <dev/sysmon/sysmonvar.h>
     48  1.1  xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     49  1.1  xtraeme #include <prop/proplib.h>
     50  1.1  xtraeme 
     51  1.1  xtraeme /*
     52  1.1  xtraeme  * Functions to create objects in a dictionary if they do not exist, or
     53  1.1  xtraeme  * for updating its value it value provided doesn't match with the value
     54  1.1  xtraeme  * in dictionary.
     55  1.1  xtraeme  */
     56  1.1  xtraeme int
     57  1.1  xtraeme sme_sensor_upbool(prop_object_t obj, prop_dictionary_t dict,
     58  1.1  xtraeme 		  const char *key, bool val)
     59  1.1  xtraeme {
     60  1.1  xtraeme 	KASSERT(dict != NULL);
     61  1.1  xtraeme 
     62  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
     63  1.1  xtraeme 	if (obj) {
     64  1.1  xtraeme 		if (prop_bool_true(obj) != val) {
     65  1.1  xtraeme 			if (!prop_dictionary_set_bool(dict, key, val)) {
     66  1.1  xtraeme 				DPRINTF(("%s: (up) set_bool %s:%d\n",
     67  1.1  xtraeme 				    __func__, key, val));
     68  1.1  xtraeme 				return EINVAL;
     69  1.1  xtraeme 			}
     70  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
     71  1.1  xtraeme 		}
     72  1.1  xtraeme 	} else {
     73  1.1  xtraeme 		if (!prop_dictionary_set_bool(dict, key, val)) {
     74  1.1  xtraeme 			DPRINTF(("%s: (set) set_bool %s:%d\n",
     75  1.1  xtraeme 			    __func__, key, val));
     76  1.1  xtraeme 			return EINVAL;
     77  1.1  xtraeme 		}
     78  1.1  xtraeme 	}
     79  1.1  xtraeme 
     80  1.1  xtraeme 	return 0;
     81  1.1  xtraeme }
     82  1.1  xtraeme 
     83  1.1  xtraeme int
     84  1.1  xtraeme sme_sensor_upint32(prop_object_t obj, prop_dictionary_t dict,
     85  1.1  xtraeme 		   const char *key, int32_t val)
     86  1.1  xtraeme {
     87  1.1  xtraeme 	KASSERT(dict != NULL);
     88  1.1  xtraeme 
     89  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
     90  1.1  xtraeme 	if (obj) {
     91  1.1  xtraeme 		if (!prop_number_equals_integer(obj, val)) {
     92  1.1  xtraeme 			if (!prop_dictionary_set_int32(dict, key, val)) {
     93  1.1  xtraeme 				DPRINTF(("%s: (up) set_int32 %s:%d\n",
     94  1.1  xtraeme 				    __func__, key, val));
     95  1.1  xtraeme 				return EINVAL;
     96  1.1  xtraeme 			}
     97  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
     98  1.1  xtraeme 		}
     99  1.1  xtraeme 	} else {
    100  1.1  xtraeme 		if (!prop_dictionary_set_int32(dict, key, val)) {
    101  1.1  xtraeme 			DPRINTF(("%s: (set) set_int32 %s:%d\n",
    102  1.1  xtraeme 			    __func__, key, val));
    103  1.1  xtraeme 			return EINVAL;
    104  1.1  xtraeme 		}
    105  1.1  xtraeme 	}
    106  1.1  xtraeme 
    107  1.1  xtraeme 	return 0;
    108  1.1  xtraeme }
    109  1.1  xtraeme 
    110  1.1  xtraeme int
    111  1.1  xtraeme sme_sensor_upuint32(prop_object_t obj, prop_dictionary_t dict,
    112  1.1  xtraeme 		    const char *key, uint32_t val)
    113  1.1  xtraeme {
    114  1.1  xtraeme 	KASSERT(dict != NULL);
    115  1.1  xtraeme 
    116  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
    117  1.1  xtraeme 	if (obj) {
    118  1.1  xtraeme 		if (!prop_number_equals_unsigned_integer(obj, val)) {
    119  1.1  xtraeme 			if (!prop_dictionary_set_uint32(dict, key, val)) {
    120  1.1  xtraeme 				DPRINTF(("%s: (up) set_uint32 %s:%d\n",
    121  1.1  xtraeme 				    __func__, key, val));
    122  1.1  xtraeme 				return EINVAL;
    123  1.1  xtraeme 			}
    124  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
    125  1.1  xtraeme 		}
    126  1.1  xtraeme 	} else {
    127  1.1  xtraeme 		if (!prop_dictionary_set_uint32(dict, key, val)) {
    128  1.1  xtraeme 			DPRINTF(("%s: (set) set_uint32 %s:%d\n",
    129  1.1  xtraeme 			    __func__, key, val));
    130  1.1  xtraeme 			return EINVAL;
    131  1.1  xtraeme 		}
    132  1.1  xtraeme 	}
    133  1.1  xtraeme 
    134  1.1  xtraeme 	return 0;
    135  1.1  xtraeme }
    136  1.1  xtraeme 
    137  1.1  xtraeme int
    138  1.1  xtraeme sme_sensor_upstring(prop_object_t obj, prop_dictionary_t dict,
    139  1.1  xtraeme 		    const char *key, const char *str)
    140  1.1  xtraeme {
    141  1.1  xtraeme 	KASSERT(dict != NULL);
    142  1.1  xtraeme 
    143  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
    144  1.1  xtraeme 	if (obj == NULL) {
    145  1.1  xtraeme 		if (!prop_dictionary_set_cstring_nocopy(dict, key, str)) {
    146  1.1  xtraeme 			DPRINTF(("%s: (up) set_cstring %s:%s\n",
    147  1.1  xtraeme 			    __func__, key, str));
    148  1.1  xtraeme 			return EINVAL;
    149  1.1  xtraeme 		}
    150  1.1  xtraeme 	} else {
    151  1.1  xtraeme 		if (!prop_string_equals_cstring(obj, str)) {
    152  1.1  xtraeme 			if (!prop_dictionary_set_cstring_nocopy(dict,
    153  1.1  xtraeme 								key,
    154  1.1  xtraeme 								str)) {
    155  1.1  xtraeme 				DPRINTF(("%s: (set) set_cstring %s:%s\n",
    156  1.1  xtraeme 				    __func__, key, str));
    157  1.1  xtraeme 				return EINVAL;
    158  1.1  xtraeme 			}
    159  1.1  xtraeme 		}
    160  1.1  xtraeme 	}
    161  1.1  xtraeme 
    162  1.1  xtraeme 	return 0;
    163  1.1  xtraeme }
    164