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