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