3 * SNMP thread synchronization implementation.
7 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
10 * Redistribution and use in source and binary forms, with or without modification,
11 * are permitted provided that the following conditions are met:
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.
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
32 * Author: Dirk Ziegelmeier <dziegel@gmx.de>
35 #include "lwip/apps/snmp_opts.h"
37 #if LWIP_SNMP && (NO_SYS == 0) /* don't build if not configured for use in lwipopts.h */
39 #include "lwip/apps/snmp_threadsync.h"
40 #include "lwip/apps/snmp_core.h"
45 call_synced_function(struct threadsync_data *call_data, snmp_threadsync_called_fn fn)
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);
54 threadsync_get_value_synced(void *ctx)
56 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
58 call_data->retval.s16 = call_data->proxy_instance.get_value(&call_data->proxy_instance, call_data->arg1.value);
60 sys_sem_signal(&call_data->threadsync_node->instance->sem);
64 threadsync_get_value(struct snmp_node_instance* instance, void* value)
66 struct threadsync_data *call_data = (struct threadsync_data*)instance->reference.ptr;
68 call_data->arg1.value = value;
69 call_synced_function(call_data, threadsync_get_value_synced);
71 return call_data->retval.s16;
75 threadsync_set_test_synced(void *ctx)
77 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
79 call_data->retval.err = call_data->proxy_instance.set_test(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value);
81 sys_sem_signal(&call_data->threadsync_node->instance->sem);
85 threadsync_set_test(struct snmp_node_instance* instance, u16_t len, void *value)
87 struct threadsync_data *call_data = (struct threadsync_data*)instance->reference.ptr;
89 call_data->arg1.value = value;
90 call_data->arg2.len = len;
91 call_synced_function(call_data, threadsync_set_test_synced);
93 return call_data->retval.err;
97 threadsync_set_value_synced(void *ctx)
99 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
101 call_data->retval.err = call_data->proxy_instance.set_value(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value);
103 sys_sem_signal(&call_data->threadsync_node->instance->sem);
107 threadsync_set_value(struct snmp_node_instance* instance, u16_t len, void *value)
109 struct threadsync_data *call_data = (struct threadsync_data*)instance->reference.ptr;
111 call_data->arg1.value = value;
112 call_data->arg2.len = len;
113 call_synced_function(call_data, threadsync_set_value_synced);
115 return call_data->retval.err;
119 threadsync_release_instance_synced(void* ctx)
121 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
123 call_data->proxy_instance.release_instance(&call_data->proxy_instance);
125 sys_sem_signal(&call_data->threadsync_node->instance->sem);
129 threadsync_release_instance(struct snmp_node_instance *instance)
131 struct threadsync_data *call_data = (struct threadsync_data*)instance->reference.ptr;
133 if (call_data->proxy_instance.release_instance != NULL) {
134 call_synced_function(call_data, threadsync_release_instance_synced);
139 get_instance_synced(void* ctx)
141 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
142 const struct snmp_leaf_node *leaf = (const struct snmp_leaf_node*)(const void*)call_data->proxy_instance.node;
144 call_data->retval.err = leaf->get_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance);
146 sys_sem_signal(&call_data->threadsync_node->instance->sem);
150 get_next_instance_synced(void* ctx)
152 struct threadsync_data *call_data = (struct threadsync_data*)ctx;
153 const struct snmp_leaf_node *leaf = (const struct snmp_leaf_node*)(const void*)call_data->proxy_instance.node;
155 call_data->retval.err = leaf->get_next_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance);
157 sys_sem_signal(&call_data->threadsync_node->instance->sem);
161 do_sync(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance, snmp_threadsync_called_fn fn)
163 const struct snmp_threadsync_node *threadsync_node = (const struct snmp_threadsync_node*)(const void*)instance->node;
164 struct threadsync_data *call_data = &threadsync_node->instance->data;
166 if (threadsync_node->node.node.oid != threadsync_node->target->node.oid) {
167 LWIP_DEBUGF(SNMP_DEBUG, ("Sync node OID does not match target node OID"));
168 return SNMP_ERR_NOSUCHINSTANCE;
171 memset(&call_data->proxy_instance, 0, sizeof(call_data->proxy_instance));
173 instance->reference.ptr = call_data;
174 snmp_oid_assign(&call_data->proxy_instance.instance_oid, instance->instance_oid.id, instance->instance_oid.len);
176 call_data->proxy_instance.node = &threadsync_node->target->node;
177 call_data->threadsync_node = threadsync_node;
179 call_data->arg1.root_oid = root_oid;
180 call_data->arg2.root_oid_len = root_oid_len;
181 call_synced_function(call_data, fn);
183 if (call_data->retval.err == SNMP_ERR_NOERROR) {
184 instance->access = call_data->proxy_instance.access;
185 instance->asn1_type = call_data->proxy_instance.asn1_type;
186 instance->release_instance = threadsync_release_instance;
187 instance->get_value = (call_data->proxy_instance.get_value != NULL)? threadsync_get_value : NULL;
188 instance->set_value = (call_data->proxy_instance.set_value != NULL)? threadsync_set_value : NULL;
189 instance->set_test = (call_data->proxy_instance.set_test != NULL)? threadsync_set_test : NULL;
190 snmp_oid_assign(&instance->instance_oid, call_data->proxy_instance.instance_oid.id, call_data->proxy_instance.instance_oid.len);
193 return call_data->retval.err;
197 snmp_threadsync_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
199 return do_sync(root_oid, root_oid_len, instance, get_instance_synced);
203 snmp_threadsync_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
205 return do_sync(root_oid, root_oid_len, instance, get_next_instance_synced);
208 /** Initializes thread synchronization instance */
209 void snmp_threadsync_init(struct snmp_threadsync_instance *instance, snmp_threadsync_synchronizer_fn sync_fn)
211 err_t err = sys_mutex_new(&instance->sem_usage_mutex);
212 LWIP_ASSERT("Failed to set up mutex", err == ERR_OK);
213 err = sys_sem_new(&instance->sem, 0);
214 LWIP_UNUSED_ARG(err); /* in case of LWIP_NOASSERT */
215 LWIP_ASSERT("Failed to set up semaphore", err == ERR_OK);
216 instance->sync_fn = sync_fn;
219 #endif /* LWIP_SNMP */