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