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