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