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