[457] | 1 | /**
|
---|
| 2 | * @file
|
---|
| 3 | * SNMP thread synchronization implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
| 7 | * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
|
---|
| 8 | * All rights reserved.
|
---|
| 9 | *
|
---|
| 10 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
| 11 | * are permitted provided that the following conditions are met:
|
---|
| 12 | *
|
---|
| 13 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
| 14 | * this list of conditions and the following disclaimer.
|
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
| 16 | * this list of conditions and the following disclaimer in the documentation
|
---|
| 17 | * and/or other materials provided with the distribution.
|
---|
| 18 | * 3. The name of the author may not be used to endorse or promote products
|
---|
| 19 | * derived from this software without specific prior written permission.
|
---|
| 20 | *
|
---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
---|
| 24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
---|
| 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
| 29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
---|
| 30 | * OF SUCH DAMAGE.
|
---|
| 31 | *
|
---|
| 32 | * Author: Dirk Ziegelmeier <dziegel@gmx.de>
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include "lwip/apps/snmp_opts.h"
|
---|
| 36 |
|
---|
| 37 | #if LWIP_SNMP && (NO_SYS == 0) /* don't build if not configured for use in lwipopts.h */
|
---|
| 38 |
|
---|
| 39 | #include "lwip/apps/snmp_threadsync.h"
|
---|
| 40 | #include "lwip/apps/snmp_core.h"
|
---|
| 41 | #include "lwip/sys.h"
|
---|
| 42 | #include <string.h>
|
---|
| 43 |
|
---|
| 44 | static void
|
---|
| 45 | call_synced_function(struct threadsync_data *call_data, snmp_threadsync_called_fn fn)
|
---|
| 46 | {
|
---|
| 47 | sys_mutex_lock(&call_data->threadsync_node->instance->sem_usage_mutex);
|
---|
| 48 | call_data->threadsync_node->instance->sync_fn(fn, call_data);
|
---|
| 49 | sys_sem_wait(&call_data->threadsync_node->instance->sem);
|
---|
| 50 | sys_mutex_unlock(&call_data->threadsync_node->instance->sem_usage_mutex);
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | static void
|
---|
| 54 | threadsync_get_value_synced(void *ctx)
|
---|
| 55 | {
|
---|
| 56 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 57 |
|
---|
| 58 | if (call_data->proxy_instance.get_value != NULL) {
|
---|
| 59 | call_data->retval.s16 = call_data->proxy_instance.get_value(&call_data->proxy_instance, call_data->arg1.value);
|
---|
| 60 | } else {
|
---|
| 61 | call_data->retval.s16 = -1;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | static s16_t
|
---|
| 68 | threadsync_get_value(struct snmp_node_instance *instance, void *value)
|
---|
| 69 | {
|
---|
| 70 | struct threadsync_data *call_data = (struct threadsync_data *)instance->reference.ptr;
|
---|
| 71 |
|
---|
| 72 | call_data->arg1.value = value;
|
---|
| 73 | call_synced_function(call_data, threadsync_get_value_synced);
|
---|
| 74 |
|
---|
| 75 | return call_data->retval.s16;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | static void
|
---|
| 79 | threadsync_set_test_synced(void *ctx)
|
---|
| 80 | {
|
---|
| 81 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 82 |
|
---|
| 83 | if (call_data->proxy_instance.set_test != NULL) {
|
---|
| 84 | call_data->retval.err = call_data->proxy_instance.set_test(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value);
|
---|
| 85 | } else {
|
---|
| 86 | call_data->retval.err = SNMP_ERR_NOTWRITABLE;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | static snmp_err_t
|
---|
| 93 | threadsync_set_test(struct snmp_node_instance *instance, u16_t len, void *value)
|
---|
| 94 | {
|
---|
| 95 | struct threadsync_data *call_data = (struct threadsync_data *)instance->reference.ptr;
|
---|
| 96 |
|
---|
| 97 | call_data->arg1.value = value;
|
---|
| 98 | call_data->arg2.len = len;
|
---|
| 99 | call_synced_function(call_data, threadsync_set_test_synced);
|
---|
| 100 |
|
---|
| 101 | return call_data->retval.err;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | static void
|
---|
| 105 | threadsync_set_value_synced(void *ctx)
|
---|
| 106 | {
|
---|
| 107 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 108 |
|
---|
| 109 | if (call_data->proxy_instance.set_value != NULL) {
|
---|
| 110 | call_data->retval.err = call_data->proxy_instance.set_value(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value);
|
---|
| 111 | } else {
|
---|
| 112 | call_data->retval.err = SNMP_ERR_NOTWRITABLE;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | static snmp_err_t
|
---|
| 119 | threadsync_set_value(struct snmp_node_instance *instance, u16_t len, void *value)
|
---|
| 120 | {
|
---|
| 121 | struct threadsync_data *call_data = (struct threadsync_data *)instance->reference.ptr;
|
---|
| 122 |
|
---|
| 123 | call_data->arg1.value = value;
|
---|
| 124 | call_data->arg2.len = len;
|
---|
| 125 | call_synced_function(call_data, threadsync_set_value_synced);
|
---|
| 126 |
|
---|
| 127 | return call_data->retval.err;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | static void
|
---|
| 131 | threadsync_release_instance_synced(void *ctx)
|
---|
| 132 | {
|
---|
| 133 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 134 |
|
---|
| 135 | call_data->proxy_instance.release_instance(&call_data->proxy_instance);
|
---|
| 136 |
|
---|
| 137 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | static void
|
---|
| 141 | threadsync_release_instance(struct snmp_node_instance *instance)
|
---|
| 142 | {
|
---|
| 143 | struct threadsync_data *call_data = (struct threadsync_data *)instance->reference.ptr;
|
---|
| 144 |
|
---|
| 145 | if (call_data->proxy_instance.release_instance != NULL) {
|
---|
| 146 | call_synced_function(call_data, threadsync_release_instance_synced);
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | static void
|
---|
| 151 | get_instance_synced(void *ctx)
|
---|
| 152 | {
|
---|
| 153 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 154 | const struct snmp_leaf_node *leaf = (const struct snmp_leaf_node *)(const void *)call_data->proxy_instance.node;
|
---|
| 155 |
|
---|
| 156 | call_data->retval.err = leaf->get_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance);
|
---|
| 157 |
|
---|
| 158 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | static void
|
---|
| 162 | get_next_instance_synced(void *ctx)
|
---|
| 163 | {
|
---|
| 164 | struct threadsync_data *call_data = (struct threadsync_data *)ctx;
|
---|
| 165 | const struct snmp_leaf_node *leaf = (const struct snmp_leaf_node *)(const void *)call_data->proxy_instance.node;
|
---|
| 166 |
|
---|
| 167 | call_data->retval.err = leaf->get_next_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance);
|
---|
| 168 |
|
---|
| 169 | sys_sem_signal(&call_data->threadsync_node->instance->sem);
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | static snmp_err_t
|
---|
| 173 | do_sync(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance, snmp_threadsync_called_fn fn)
|
---|
| 174 | {
|
---|
| 175 | const struct snmp_threadsync_node *threadsync_node = (const struct snmp_threadsync_node *)(const void *)instance->node;
|
---|
| 176 | struct threadsync_data *call_data = &threadsync_node->instance->data;
|
---|
| 177 |
|
---|
| 178 | if (threadsync_node->node.node.oid != threadsync_node->target->node.oid) {
|
---|
| 179 | LWIP_DEBUGF(SNMP_DEBUG, ("Sync node OID does not match target node OID"));
|
---|
| 180 | return SNMP_ERR_NOSUCHINSTANCE;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | memset(&call_data->proxy_instance, 0, sizeof(call_data->proxy_instance));
|
---|
| 184 |
|
---|
| 185 | instance->reference.ptr = call_data;
|
---|
| 186 | snmp_oid_assign(&call_data->proxy_instance.instance_oid, instance->instance_oid.id, instance->instance_oid.len);
|
---|
| 187 |
|
---|
| 188 | call_data->proxy_instance.node = &threadsync_node->target->node;
|
---|
| 189 | call_data->threadsync_node = threadsync_node;
|
---|
| 190 |
|
---|
| 191 | call_data->arg1.root_oid = root_oid;
|
---|
| 192 | call_data->arg2.root_oid_len = root_oid_len;
|
---|
| 193 | call_synced_function(call_data, fn);
|
---|
| 194 |
|
---|
| 195 | if (call_data->retval.err == SNMP_ERR_NOERROR) {
|
---|
| 196 | instance->access = call_data->proxy_instance.access;
|
---|
| 197 | instance->asn1_type = call_data->proxy_instance.asn1_type;
|
---|
| 198 | instance->release_instance = threadsync_release_instance;
|
---|
| 199 | instance->get_value = (call_data->proxy_instance.get_value != NULL) ? threadsync_get_value : NULL;
|
---|
| 200 | instance->set_value = (call_data->proxy_instance.set_value != NULL) ? threadsync_set_value : NULL;
|
---|
| 201 | instance->set_test = (call_data->proxy_instance.set_test != NULL) ? threadsync_set_test : NULL;
|
---|
| 202 | snmp_oid_assign(&instance->instance_oid, call_data->proxy_instance.instance_oid.id, call_data->proxy_instance.instance_oid.len);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 | return call_data->retval.err;
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | snmp_err_t
|
---|
| 209 | snmp_threadsync_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
|
---|
| 210 | {
|
---|
| 211 | return do_sync(root_oid, root_oid_len, instance, get_instance_synced);
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | snmp_err_t
|
---|
| 215 | snmp_threadsync_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance *instance)
|
---|
| 216 | {
|
---|
| 217 | return do_sync(root_oid, root_oid_len, instance, get_next_instance_synced);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Initializes thread synchronization instance */
|
---|
| 221 | void snmp_threadsync_init(struct snmp_threadsync_instance *instance, snmp_threadsync_synchronizer_fn sync_fn)
|
---|
| 222 | {
|
---|
| 223 | err_t err = sys_mutex_new(&instance->sem_usage_mutex);
|
---|
| 224 | LWIP_ASSERT("Failed to set up mutex", err == ERR_OK);
|
---|
| 225 | err = sys_sem_new(&instance->sem, 0);
|
---|
| 226 | LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */
|
---|
| 227 | LWIP_ASSERT("Failed to set up semaphore", err == ERR_OK);
|
---|
| 228 | instance->sync_fn = sync_fn;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | #endif /* LWIP_SNMP */
|
---|