xref: /netbsd-src/sys/dev/sysmon/sysmon_envsys_util.c (revision 814a779807a6d31c293aff50a5fc6824fcb72030)
1*814a7798Sthorpej /* $NetBSD: sysmon_envsys_util.c,v 1.7 2020/06/11 02:39:31 thorpej Exp $ */
27282589dSxtraeme 
37282589dSxtraeme /*-
46c4da82bSxtraeme  * Copyright (c) 2007 Juan Romero Pardines.
57282589dSxtraeme  * All rights reserved.
67282589dSxtraeme  *
77282589dSxtraeme  * Redistribution and use in source and binary forms, with or without
87282589dSxtraeme  * modification, are permitted provided that the following conditions
97282589dSxtraeme  * are met:
107282589dSxtraeme  * 1. Redistributions of source code must retain the above copyright
117282589dSxtraeme  *    notice, this list of conditions and the following disclaimer.
127282589dSxtraeme  * 2. Redistributions in binary form must reproduce the above copyright
137282589dSxtraeme  *    notice, this list of conditions and the following disclaimer in the
147282589dSxtraeme  *    documentation and/or other materials provided with the distribution.
157282589dSxtraeme  *
166c4da82bSxtraeme  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
176c4da82bSxtraeme  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
186c4da82bSxtraeme  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
196c4da82bSxtraeme  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
206c4da82bSxtraeme  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
216c4da82bSxtraeme  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
226c4da82bSxtraeme  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
236c4da82bSxtraeme  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
246c4da82bSxtraeme  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
256c4da82bSxtraeme  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
267282589dSxtraeme  */
277282589dSxtraeme 
287282589dSxtraeme #include <sys/cdefs.h>
29*814a7798Sthorpej __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_util.c,v 1.7 2020/06/11 02:39:31 thorpej Exp $");
307282589dSxtraeme 
317282589dSxtraeme #include <sys/param.h>
327282589dSxtraeme #include <sys/types.h>
337282589dSxtraeme #include <sys/conf.h>
347282589dSxtraeme #include <sys/kernel.h>
357282589dSxtraeme 
367282589dSxtraeme #include <dev/sysmon/sysmonvar.h>
377282589dSxtraeme #include <dev/sysmon/sysmon_envsysvar.h>
387282589dSxtraeme #include <prop/proplib.h>
397282589dSxtraeme 
407282589dSxtraeme /*
417282589dSxtraeme  * Functions to create objects in a dictionary if they do not exist, or
4231962fc6Sxtraeme  * for updating its value if it doesn't match with the value in dictionary.
437282589dSxtraeme  */
446c4da82bSxtraeme 
457282589dSxtraeme int
sme_sensor_upbool(prop_dictionary_t dict,const char * key,bool val)46b2537bf1Sxtraeme sme_sensor_upbool(prop_dictionary_t dict, const char *key, bool val)
477282589dSxtraeme {
48b2537bf1Sxtraeme 	prop_object_t obj;
49b2537bf1Sxtraeme 
507282589dSxtraeme 	KASSERT(dict != NULL);
517282589dSxtraeme 
527282589dSxtraeme 	obj = prop_dictionary_get(dict, key);
537282589dSxtraeme 	if (obj) {
547282589dSxtraeme 		if (prop_bool_true(obj) != val) {
557282589dSxtraeme 			if (!prop_dictionary_set_bool(dict, key, val)) {
567282589dSxtraeme 				DPRINTF(("%s: (up) set_bool %s:%d\n",
577282589dSxtraeme 				    __func__, key, val));
587282589dSxtraeme 				return EINVAL;
597282589dSxtraeme 			}
607282589dSxtraeme 		}
617282589dSxtraeme 	} else {
627282589dSxtraeme 		if (!prop_dictionary_set_bool(dict, key, val)) {
637282589dSxtraeme 			DPRINTF(("%s: (set) set_bool %s:%d\n",
647282589dSxtraeme 			    __func__, key, val));
657282589dSxtraeme 			return EINVAL;
667282589dSxtraeme 		}
677282589dSxtraeme 	}
687282589dSxtraeme 
697282589dSxtraeme 	return 0;
707282589dSxtraeme }
717282589dSxtraeme 
727282589dSxtraeme int
sme_sensor_upint32(prop_dictionary_t dict,const char * key,int32_t val)73b2537bf1Sxtraeme sme_sensor_upint32(prop_dictionary_t dict, const char *key, int32_t val)
747282589dSxtraeme {
75b2537bf1Sxtraeme 	prop_object_t obj;
76b2537bf1Sxtraeme 
777282589dSxtraeme 	KASSERT(dict != NULL);
787282589dSxtraeme 
797282589dSxtraeme 	obj = prop_dictionary_get(dict, key);
807282589dSxtraeme 	if (obj) {
81a2b798fdSthorpej 		if (!prop_number_equals_signed(obj, val)) {
827282589dSxtraeme 			if (!prop_dictionary_set_int32(dict, key, val)) {
837282589dSxtraeme 				DPRINTF(("%s: (up) set_int32 %s:%d\n",
847282589dSxtraeme 				    __func__, key, val));
857282589dSxtraeme 				return EINVAL;
867282589dSxtraeme 			}
877282589dSxtraeme 		}
887282589dSxtraeme 	} else {
897282589dSxtraeme 		if (!prop_dictionary_set_int32(dict, key, val)) {
907282589dSxtraeme 			DPRINTF(("%s: (set) set_int32 %s:%d\n",
917282589dSxtraeme 			    __func__, key, val));
927282589dSxtraeme 			return EINVAL;
937282589dSxtraeme 		}
947282589dSxtraeme 	}
957282589dSxtraeme 
967282589dSxtraeme 	return 0;
977282589dSxtraeme }
987282589dSxtraeme 
997282589dSxtraeme int
sme_sensor_upuint32(prop_dictionary_t dict,const char * key,uint32_t val)100b2537bf1Sxtraeme sme_sensor_upuint32(prop_dictionary_t dict, const char *key, uint32_t val)
1017282589dSxtraeme {
102b2537bf1Sxtraeme 	prop_object_t obj;
103b2537bf1Sxtraeme 
1047282589dSxtraeme 	KASSERT(dict != NULL);
1057282589dSxtraeme 
1067282589dSxtraeme 	obj = prop_dictionary_get(dict, key);
1077282589dSxtraeme 	if (obj) {
108a2b798fdSthorpej 		if (!prop_number_equals_unsigned(obj, val)) {
1097282589dSxtraeme 			if (!prop_dictionary_set_uint32(dict, key, val)) {
1107282589dSxtraeme 				DPRINTF(("%s: (up) set_uint32 %s:%d\n",
1117282589dSxtraeme 				    __func__, key, val));
1127282589dSxtraeme 				return EINVAL;
1137282589dSxtraeme 			}
1147282589dSxtraeme 		}
1157282589dSxtraeme 	} else {
1167282589dSxtraeme 		if (!prop_dictionary_set_uint32(dict, key, val)) {
1177282589dSxtraeme 			DPRINTF(("%s: (set) set_uint32 %s:%d\n",
1187282589dSxtraeme 			    __func__, key, val));
1197282589dSxtraeme 			return EINVAL;
1207282589dSxtraeme 		}
1217282589dSxtraeme 	}
1227282589dSxtraeme 
1237282589dSxtraeme 	return 0;
1247282589dSxtraeme }
1257282589dSxtraeme 
1267282589dSxtraeme int
sme_sensor_upstring(prop_dictionary_t dict,const char * key,const char * str)127b2537bf1Sxtraeme sme_sensor_upstring(prop_dictionary_t dict, const char *key, const char *str)
1287282589dSxtraeme {
129b2537bf1Sxtraeme 	prop_object_t obj;
130b2537bf1Sxtraeme 
1317282589dSxtraeme 	KASSERT(dict != NULL);
1327282589dSxtraeme 
1337282589dSxtraeme 	obj = prop_dictionary_get(dict, key);
1347282589dSxtraeme 	if (obj == NULL) {
135*814a7798Sthorpej 		if (!prop_dictionary_set_string(dict, key, str)) {
1367282589dSxtraeme 			DPRINTF(("%s: (up) set_cstring %s:%s\n",
1377282589dSxtraeme 			    __func__, key, str));
1387282589dSxtraeme 			return EINVAL;
1397282589dSxtraeme 		}
1407282589dSxtraeme 	} else {
141*814a7798Sthorpej 		if (!prop_string_equals_string(obj, str)) {
142*814a7798Sthorpej 			if (!prop_dictionary_set_string(dict, key, str)) {
1437282589dSxtraeme 				DPRINTF(("%s: (set) set_cstring %s:%s\n",
1447282589dSxtraeme 				    __func__, key, str));
1457282589dSxtraeme 				return EINVAL;
1467282589dSxtraeme 			}
1477282589dSxtraeme 		}
1487282589dSxtraeme 	}
1497282589dSxtraeme 
1507282589dSxtraeme 	return 0;
1517282589dSxtraeme }
152