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