STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
snmp_scalar.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25  * OF SUCH DAMAGE.
26  *
27  * This file is part of the lwIP TCP/IP stack.
28  *
29  * Author: Martin Hentschel <info@cl-soft.de>
30  *
31  */
32 
33 #include "lwip/apps/snmp_opts.h"
34 
35 #if LWIP_SNMP /* don't build if not configured for use in lwipopts.h */
36 
37 #include "lwip/apps/snmp_scalar.h"
38 #include "lwip/apps/snmp_core.h"
39 
40 static u16_t snmp_scalar_array_get_value(struct snmp_node_instance* instance, void* value);
41 static snmp_err_t snmp_scalar_array_set_test(struct snmp_node_instance* instance, u16_t value_len, void* value);
42 static snmp_err_t snmp_scalar_array_set_value(struct snmp_node_instance* instance, u16_t value_len, void* value);
43 
44 snmp_err_t
45 snmp_scalar_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
46 {
47  const struct snmp_scalar_node* scalar_node = (const struct snmp_scalar_node*)instance->node;
48 
49  LWIP_UNUSED_ARG(root_oid);
50  LWIP_UNUSED_ARG(root_oid_len);
51 
52  /* scalar only has one dedicated instance: .0 */
53  if ((instance->instance_oid.len != 1) || (instance->instance_oid.id[0] != 0))
54  {
55  return SNMP_ERR_NOSUCHINSTANCE;
56  }
57 
58  instance->access = scalar_node->access;
59  instance->asn1_type = scalar_node->asn1_type;
60  instance->get_value = scalar_node->get_value;
61  instance->set_test = scalar_node->set_test;
62  instance->set_value = scalar_node->set_value;
63  return SNMP_ERR_NOERROR;
64 }
65 
66 snmp_err_t
67 snmp_scalar_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
68 {
69  /* because our only instance is .0 we can only return a next instance if no instance oid is passed */
70  if (instance->instance_oid.len == 0)
71  {
72  instance->instance_oid.len = 1;
73  instance->instance_oid.id[0] = 0;
74 
75  return snmp_scalar_get_instance(root_oid, root_oid_len, instance);
76  }
77 
78  return SNMP_ERR_NOSUCHINSTANCE;
79 }
80 
81 
82 snmp_err_t
83 snmp_scalar_array_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
84 {
85  LWIP_UNUSED_ARG(root_oid);
86  LWIP_UNUSED_ARG(root_oid_len);
87 
88  if ((instance->instance_oid.len == 2) && (instance->instance_oid.id[1] == 0))
89  {
90  const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)instance->node;
91  const struct snmp_scalar_array_node_def* array_node_def = array_node->array_nodes;
92  u32_t i = 0;
93 
94  while (i < array_node->array_node_count)
95  {
96  if (array_node_def->oid == instance->instance_oid.id[0])
97  {
98  break;
99  }
100 
101  array_node_def++;
102  i++;
103  }
104 
105  if (i < array_node->array_node_count)
106  {
107  instance->access = array_node_def->access;
108  instance->asn1_type = array_node_def->asn1_type;
109  instance->get_value = snmp_scalar_array_get_value;
110  instance->set_test = snmp_scalar_array_set_test;
111  instance->set_value = snmp_scalar_array_set_value;
112  instance->reference.const_ptr = array_node_def;
113 
114  return SNMP_ERR_NOERROR;
115  }
116  }
117 
118  return SNMP_ERR_NOSUCHINSTANCE;
119 }
120 
121 snmp_err_t
122 snmp_scalar_array_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance)
123 {
124  const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)instance->node;
125  const struct snmp_scalar_array_node_def* array_node_def = array_node->array_nodes;
126  const struct snmp_scalar_array_node_def* result = NULL;
127 
128  LWIP_UNUSED_ARG(root_oid);
129  LWIP_UNUSED_ARG(root_oid_len);
130 
131  if ((instance->instance_oid.len == 0) && (array_node->array_node_count > 0))
132  {
133  /* return node with lowest OID */
134  u16_t i = 0;
135 
136  result = array_node_def;
137  array_node_def++;
138 
139  for (i = 1; i < array_node->array_node_count; i++)
140  {
141  if (array_node_def->oid < result->oid)
142  {
143  result = array_node_def;
144  }
145  array_node_def++;
146  }
147  }
148  else if (instance->instance_oid.len >= 1)
149  {
150  if (instance->instance_oid.len == 1)
151  {
152  /* if we have the requested OID we return its instance, otherwise we search for the next available */
153  u16_t i = 0;
154  while (i < array_node->array_node_count)
155  {
156  if (array_node_def->oid == instance->instance_oid.id[0])
157  {
158  result = array_node_def;
159  break;
160  }
161 
162  array_node_def++;
163  i++;
164  }
165  }
166  if (result == NULL)
167  {
168  u32_t oid_dist = 0xFFFFFFFFUL;
169  u16_t i = 0;
170  array_node_def = array_node->array_nodes; /* may be already at the end when if case before was executed without result -> reinitialize to start */
171  while (i < array_node->array_node_count)
172  {
173  if ((array_node_def->oid > instance->instance_oid.id[0]) && ((u32_t)(array_node_def->oid - instance->instance_oid.id[0]) < oid_dist))
174  {
175  result = array_node_def;
176  oid_dist = array_node_def->oid - instance->instance_oid.id[0];
177  }
178 
179  array_node_def++;
180  i++;
181  }
182  }
183  }
184 
185  if (result == NULL)
186  {
187  /* nothing to return */
188  return SNMP_ERR_NOSUCHINSTANCE;
189  }
190 
191  instance->instance_oid.len = 2;
192  instance->instance_oid.id[0] = result->oid;
193  instance->instance_oid.id[1] = 0;
194 
195  instance->access = result->access;
196  instance->asn1_type = result->asn1_type;
197  instance->get_value = snmp_scalar_array_get_value;
198  instance->set_test = snmp_scalar_array_set_test;
199  instance->set_value = snmp_scalar_array_set_value;
200  instance->reference.const_ptr = result;
201 
202  return SNMP_ERR_NOERROR;
203 }
204 
205 static u16_t
206 snmp_scalar_array_get_value(struct snmp_node_instance* instance, void* value)
207 {
208  const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)instance->node;
209  const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
210 
211  return array_node->get_value(array_node_def, value);
212 }
213 
214 static snmp_err_t
215 snmp_scalar_array_set_test(struct snmp_node_instance* instance, u16_t value_len, void* value)
216 {
217  const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)instance->node;
218  const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
219 
220  return array_node->set_test(array_node_def, value_len, value);
221 }
222 
223 static snmp_err_t
224 snmp_scalar_array_set_value(struct snmp_node_instance* instance, u16_t value_len, void* value)
225 {
226  const struct snmp_scalar_array_node* array_node = (const struct snmp_scalar_array_node*)instance->node;
227  const struct snmp_scalar_array_node_def* array_node_def = (const struct snmp_scalar_array_node_def*)instance->reference.const_ptr;
228 
229  return array_node->set_value(array_node_def, value_len, value);
230 }
231 
232 #endif /* LWIP_SNMP */
#define NULL
Definition: usbd_def.h:53
unsigned long u32_t
Definition: cc.h:42
unsigned char u8_t
Definition: cc.h:38
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
unsigned short u16_t
Definition: cc.h:40