Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_util.c revision 1.2
      1  1.2  xtraeme /* $NetBSD: sysmon_envsys_util.c,v 1.2 2007/07/21 12:11:27 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.2  xtraeme __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_util.c,v 1.2 2007/07/21 12:11:27 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.2  xtraeme sme_sensor_upbool(prop_dictionary_t dict, const char *key, bool val)
     58  1.1  xtraeme {
     59  1.2  xtraeme 	prop_object_t obj;
     60  1.2  xtraeme 
     61  1.1  xtraeme 	KASSERT(dict != NULL);
     62  1.1  xtraeme 
     63  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
     64  1.1  xtraeme 	if (obj) {
     65  1.1  xtraeme 		if (prop_bool_true(obj) != val) {
     66  1.1  xtraeme 			if (!prop_dictionary_set_bool(dict, key, val)) {
     67  1.1  xtraeme 				DPRINTF(("%s: (up) set_bool %s:%d\n",
     68  1.1  xtraeme 				    __func__, key, val));
     69  1.1  xtraeme 				return EINVAL;
     70  1.1  xtraeme 			}
     71  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
     72  1.1  xtraeme 		}
     73  1.1  xtraeme 	} else {
     74  1.1  xtraeme 		if (!prop_dictionary_set_bool(dict, key, val)) {
     75  1.1  xtraeme 			DPRINTF(("%s: (set) set_bool %s:%d\n",
     76  1.1  xtraeme 			    __func__, key, val));
     77  1.1  xtraeme 			return EINVAL;
     78  1.1  xtraeme 		}
     79  1.1  xtraeme 	}
     80  1.1  xtraeme 
     81  1.1  xtraeme 	return 0;
     82  1.1  xtraeme }
     83  1.1  xtraeme 
     84  1.1  xtraeme int
     85  1.2  xtraeme sme_sensor_upint32(prop_dictionary_t dict, const char *key, int32_t val)
     86  1.1  xtraeme {
     87  1.2  xtraeme 	prop_object_t obj;
     88  1.2  xtraeme 
     89  1.1  xtraeme 	KASSERT(dict != NULL);
     90  1.1  xtraeme 
     91  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
     92  1.1  xtraeme 	if (obj) {
     93  1.1  xtraeme 		if (!prop_number_equals_integer(obj, val)) {
     94  1.1  xtraeme 			if (!prop_dictionary_set_int32(dict, key, val)) {
     95  1.1  xtraeme 				DPRINTF(("%s: (up) set_int32 %s:%d\n",
     96  1.1  xtraeme 				    __func__, key, val));
     97  1.1  xtraeme 				return EINVAL;
     98  1.1  xtraeme 			}
     99  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
    100  1.1  xtraeme 		}
    101  1.1  xtraeme 	} else {
    102  1.1  xtraeme 		if (!prop_dictionary_set_int32(dict, key, val)) {
    103  1.1  xtraeme 			DPRINTF(("%s: (set) set_int32 %s:%d\n",
    104  1.1  xtraeme 			    __func__, key, val));
    105  1.1  xtraeme 			return EINVAL;
    106  1.1  xtraeme 		}
    107  1.1  xtraeme 	}
    108  1.1  xtraeme 
    109  1.1  xtraeme 	return 0;
    110  1.1  xtraeme }
    111  1.1  xtraeme 
    112  1.1  xtraeme int
    113  1.2  xtraeme sme_sensor_upuint32(prop_dictionary_t dict, const char *key, uint32_t val)
    114  1.1  xtraeme {
    115  1.2  xtraeme 	prop_object_t obj;
    116  1.2  xtraeme 
    117  1.1  xtraeme 	KASSERT(dict != NULL);
    118  1.1  xtraeme 
    119  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
    120  1.1  xtraeme 	if (obj) {
    121  1.1  xtraeme 		if (!prop_number_equals_unsigned_integer(obj, val)) {
    122  1.1  xtraeme 			if (!prop_dictionary_set_uint32(dict, key, val)) {
    123  1.1  xtraeme 				DPRINTF(("%s: (up) set_uint32 %s:%d\n",
    124  1.1  xtraeme 				    __func__, key, val));
    125  1.1  xtraeme 				return EINVAL;
    126  1.1  xtraeme 			}
    127  1.1  xtraeme 			SENSOR_OBJUPDATED(key, val);
    128  1.1  xtraeme 		}
    129  1.1  xtraeme 	} else {
    130  1.1  xtraeme 		if (!prop_dictionary_set_uint32(dict, key, val)) {
    131  1.1  xtraeme 			DPRINTF(("%s: (set) set_uint32 %s:%d\n",
    132  1.1  xtraeme 			    __func__, key, val));
    133  1.1  xtraeme 			return EINVAL;
    134  1.1  xtraeme 		}
    135  1.1  xtraeme 	}
    136  1.1  xtraeme 
    137  1.1  xtraeme 	return 0;
    138  1.1  xtraeme }
    139  1.1  xtraeme 
    140  1.1  xtraeme int
    141  1.2  xtraeme sme_sensor_upstring(prop_dictionary_t dict, const char *key, const char *str)
    142  1.1  xtraeme {
    143  1.2  xtraeme 	prop_object_t obj;
    144  1.2  xtraeme 
    145  1.1  xtraeme 	KASSERT(dict != NULL);
    146  1.1  xtraeme 
    147  1.1  xtraeme 	obj = prop_dictionary_get(dict, key);
    148  1.1  xtraeme 	if (obj == NULL) {
    149  1.1  xtraeme 		if (!prop_dictionary_set_cstring_nocopy(dict, key, str)) {
    150  1.1  xtraeme 			DPRINTF(("%s: (up) set_cstring %s:%s\n",
    151  1.1  xtraeme 			    __func__, key, str));
    152  1.1  xtraeme 			return EINVAL;
    153  1.1  xtraeme 		}
    154  1.1  xtraeme 	} else {
    155  1.1  xtraeme 		if (!prop_string_equals_cstring(obj, str)) {
    156  1.1  xtraeme 			if (!prop_dictionary_set_cstring_nocopy(dict,
    157  1.1  xtraeme 								key,
    158  1.1  xtraeme 								str)) {
    159  1.1  xtraeme 				DPRINTF(("%s: (set) set_cstring %s:%s\n",
    160  1.1  xtraeme 				    __func__, key, str));
    161  1.1  xtraeme 				return EINVAL;
    162  1.1  xtraeme 			}
    163  1.1  xtraeme 		}
    164  1.1  xtraeme 	}
    165  1.1  xtraeme 
    166  1.1  xtraeme 	return 0;
    167  1.1  xtraeme }
    168