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