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