sysmon_envsys_util.c revision 1.6 1 1.6 thorpej /* $NetBSD: sysmon_envsys_util.c,v 1.6 2020/06/08 20:18:13 thorpej 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.6 thorpej __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_util.c,v 1.6 2020/06/08 20:18:13 thorpej 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.5 xtraeme * for updating its value if it doesn't match with the value in dictionary.
43 1.1 xtraeme */
44 1.4 xtraeme
45 1.1 xtraeme int
46 1.2 xtraeme sme_sensor_upbool(prop_dictionary_t dict, const char *key, bool val)
47 1.1 xtraeme {
48 1.2 xtraeme prop_object_t obj;
49 1.2 xtraeme
50 1.1 xtraeme KASSERT(dict != NULL);
51 1.1 xtraeme
52 1.1 xtraeme obj = prop_dictionary_get(dict, key);
53 1.1 xtraeme if (obj) {
54 1.1 xtraeme if (prop_bool_true(obj) != val) {
55 1.1 xtraeme if (!prop_dictionary_set_bool(dict, key, val)) {
56 1.1 xtraeme DPRINTF(("%s: (up) set_bool %s:%d\n",
57 1.1 xtraeme __func__, key, val));
58 1.1 xtraeme return EINVAL;
59 1.1 xtraeme }
60 1.1 xtraeme }
61 1.1 xtraeme } else {
62 1.1 xtraeme if (!prop_dictionary_set_bool(dict, key, val)) {
63 1.1 xtraeme DPRINTF(("%s: (set) set_bool %s:%d\n",
64 1.1 xtraeme __func__, key, val));
65 1.1 xtraeme return EINVAL;
66 1.1 xtraeme }
67 1.1 xtraeme }
68 1.1 xtraeme
69 1.1 xtraeme return 0;
70 1.1 xtraeme }
71 1.1 xtraeme
72 1.1 xtraeme int
73 1.2 xtraeme sme_sensor_upint32(prop_dictionary_t dict, const char *key, int32_t val)
74 1.1 xtraeme {
75 1.2 xtraeme prop_object_t obj;
76 1.2 xtraeme
77 1.1 xtraeme KASSERT(dict != NULL);
78 1.1 xtraeme
79 1.1 xtraeme obj = prop_dictionary_get(dict, key);
80 1.1 xtraeme if (obj) {
81 1.6 thorpej if (!prop_number_equals_signed(obj, val)) {
82 1.1 xtraeme if (!prop_dictionary_set_int32(dict, key, val)) {
83 1.1 xtraeme DPRINTF(("%s: (up) set_int32 %s:%d\n",
84 1.1 xtraeme __func__, key, val));
85 1.1 xtraeme return EINVAL;
86 1.1 xtraeme }
87 1.1 xtraeme }
88 1.1 xtraeme } else {
89 1.1 xtraeme if (!prop_dictionary_set_int32(dict, key, val)) {
90 1.1 xtraeme DPRINTF(("%s: (set) set_int32 %s:%d\n",
91 1.1 xtraeme __func__, key, val));
92 1.1 xtraeme return EINVAL;
93 1.1 xtraeme }
94 1.1 xtraeme }
95 1.1 xtraeme
96 1.1 xtraeme return 0;
97 1.1 xtraeme }
98 1.1 xtraeme
99 1.1 xtraeme int
100 1.2 xtraeme sme_sensor_upuint32(prop_dictionary_t dict, const char *key, uint32_t val)
101 1.1 xtraeme {
102 1.2 xtraeme prop_object_t obj;
103 1.2 xtraeme
104 1.1 xtraeme KASSERT(dict != NULL);
105 1.1 xtraeme
106 1.1 xtraeme obj = prop_dictionary_get(dict, key);
107 1.1 xtraeme if (obj) {
108 1.6 thorpej if (!prop_number_equals_unsigned(obj, val)) {
109 1.1 xtraeme if (!prop_dictionary_set_uint32(dict, key, val)) {
110 1.1 xtraeme DPRINTF(("%s: (up) set_uint32 %s:%d\n",
111 1.1 xtraeme __func__, key, val));
112 1.1 xtraeme return EINVAL;
113 1.1 xtraeme }
114 1.1 xtraeme }
115 1.1 xtraeme } else {
116 1.1 xtraeme if (!prop_dictionary_set_uint32(dict, key, val)) {
117 1.1 xtraeme DPRINTF(("%s: (set) set_uint32 %s:%d\n",
118 1.1 xtraeme __func__, key, val));
119 1.1 xtraeme return EINVAL;
120 1.1 xtraeme }
121 1.1 xtraeme }
122 1.1 xtraeme
123 1.1 xtraeme return 0;
124 1.1 xtraeme }
125 1.1 xtraeme
126 1.1 xtraeme int
127 1.2 xtraeme sme_sensor_upstring(prop_dictionary_t dict, const char *key, const char *str)
128 1.1 xtraeme {
129 1.2 xtraeme prop_object_t obj;
130 1.2 xtraeme
131 1.1 xtraeme KASSERT(dict != NULL);
132 1.1 xtraeme
133 1.1 xtraeme obj = prop_dictionary_get(dict, key);
134 1.1 xtraeme if (obj == NULL) {
135 1.3 plunky if (!prop_dictionary_set_cstring(dict, key, str)) {
136 1.1 xtraeme DPRINTF(("%s: (up) set_cstring %s:%s\n",
137 1.1 xtraeme __func__, key, str));
138 1.1 xtraeme return EINVAL;
139 1.1 xtraeme }
140 1.1 xtraeme } else {
141 1.1 xtraeme if (!prop_string_equals_cstring(obj, str)) {
142 1.3 plunky if (!prop_dictionary_set_cstring(dict, key, str)) {
143 1.1 xtraeme DPRINTF(("%s: (set) set_cstring %s:%s\n",
144 1.1 xtraeme __func__, key, str));
145 1.1 xtraeme return EINVAL;
146 1.1 xtraeme }
147 1.1 xtraeme }
148 1.1 xtraeme }
149 1.1 xtraeme
150 1.1 xtraeme return 0;
151 1.1 xtraeme }
152