STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
SnmpTreeNode.cs
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 using System;
34 using System.Collections.Generic;
35 using System.Text;
36 using CCodeGeneration;
37 
38 namespace LwipSnmpCodeGeneration
39 {
41  {
42  private readonly List<SnmpNode> childNodes = new List<SnmpNode>();
43  private readonly List<SnmpScalarNode> childScalarNodes = new List<SnmpScalarNode>();
44  private string fullOid = "";
45 
46  public SnmpTreeNode(SnmpTreeNode parentNode)
47  : base(parentNode)
48  {
49  }
50 
51  public override string FullNodeName
52  {
53  get { return this.Name + "_treenode"; }
54  }
55 
56  public string FullOid
57  {
58  get { return this.fullOid; }
59  set { this.fullOid = value; }
60  }
61 
62  public List<SnmpNode> ChildNodes
63  {
64  get { return this.childNodes; }
65  }
66 
67  protected override IEnumerable<SnmpScalarNode> AggregatedScalarNodes
68  {
69  get { return this.childScalarNodes; }
70  }
71 
72  private void GenerateAggregatedCode(MibCFile mibFile, bool generateDeclarations, bool generateImplementations)
73  {
74  VariableType instanceType = new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*");
75  base.GenerateAggregatedCode(
76  mibFile,
77  instanceType,
78  String.Format("{0}->node->oid", instanceType.Name),
79  generateDeclarations,
80  generateImplementations);
81  }
82 
83  private void GenerateAggregateMethodDeclarations(MibCFile mibFile)
84  {
85  if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1))
86  {
87  GenerateAggregatedCode(mibFile, true, false);
88  }
89  }
90 
91  public override void GenerateCode(MibCFile mibFile)
92  {
93  string nodeInitialization;
94 
95  if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1))
96  {
97  GenerateAggregatedCode(mibFile, false, true);
98  }
99 
100  // create and add node declaration
101  if (this.childNodes.Count > 0)
102  {
103  StringBuilder subnodeArrayInitialization = new StringBuilder();
104 
105  for (int i=0; i<this.childNodes.Count; i++)
106  {
107  subnodeArrayInitialization.Append(" &");
108  subnodeArrayInitialization.Append(this.childNodes[i].FullNodeName);
109  subnodeArrayInitialization.Append(".node");
110  if (!(this.childNodes[i] is SnmpTreeNode))
111  {
112  subnodeArrayInitialization.Append(".node");
113  }
114 
115  if (i < (this.childNodes.Count - 1))
116  {
117  subnodeArrayInitialization.Append(",\n");
118  }
119  }
120 
121  VariableDeclaration subnodeArray = new VariableDeclaration(
122  new VariableType(this.Name + "_subnodes", LwipDefs.Vt_StNode, "*", ConstType.Value, String.Empty),
123  "{\n" + subnodeArrayInitialization + "\n}",
124  isStatic: true);
125 
126  mibFile.Declarations.Add(subnodeArray);
127 
128  nodeInitialization = String.Format("SNMP_CREATE_TREE_NODE({0}, {1})", this.Oid, subnodeArray.Type.Name);
129  }
130  else
131  {
132  nodeInitialization = String.Format("SNMP_CREATE_EMPTY_TREE_NODE({0})", this.Oid);
133  }
134 
135  mibFile.Declarations.Add(new VariableDeclaration(
136  new VariableType(this.FullNodeName, LwipDefs.Vt_StTreeNode, null, ConstType.Value),
137  nodeInitialization,
138  isStatic: true));
139  }
140 
141  public override void Analyze()
142  {
143  this.childScalarNodes.Clear();
144 
145  // delegate analyze (don't use enumerator because the child node may change our child collection by e.g. removing or replacing itself)
146  for (int i=this.ChildNodes.Count-1; i>=0; i--)
147  {
148  this.ChildNodes[i].Analyze();
149  }
150 
151  // collect scalar nodes
152  foreach (SnmpNode childNode in this.childNodes)
153  {
154  SnmpScalarNode scalarNode = childNode as SnmpScalarNode;
155  if (scalarNode != null)
156  {
157  this.childScalarNodes.Add(scalarNode);
158  }
159  }
160 
161  base.Analyze();
162 
163  // check if we can merge this node to a scalar array node (all childs need to be scalars)
164  if (this.childNodes.Count > 0)
165  {
166  if (LwipOpts.GenerateScalarArrays && (this.childScalarNodes.Count == this.childNodes.Count) && (this.ParentNode != null))
167  {
168  SnmpScalarArrayNode scalarArrayNode = new SnmpScalarArrayNode(this.childScalarNodes, this.ParentNode);
169  scalarArrayNode.Oid = this.Oid;
170  scalarArrayNode.Name = this.Name;
171 
172  for (int i=0; i<this.ParentNode.ChildNodes.Count; i++)
173  {
174  if (this.ParentNode.ChildNodes[i] == this)
175  {
176  this.ParentNode.ChildNodes.RemoveAt(i);
177  this.ParentNode.ChildNodes.Insert(i, scalarArrayNode);
178  break;
179  }
180  }
181  }
182  else if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1))
183  {
184  foreach (SnmpScalarNode scalarNode in this.childScalarNodes)
185  {
186  scalarNode.UseExternalMethods = true;
187  scalarNode.ExternalGetMethod = this.GetMethodName;
188  scalarNode.ExternalTestMethod = this.TestMethodName;
189  scalarNode.ExternalSetMethod = this.SetMethodName;
190  }
191  }
192  }
193  else // if (this.childNodes.Count == 0)
194  {
195  if (!LwipOpts.GenerateEmptyFolders && (this.ParentNode != null))
196  {
197  // do not generate this empty folder because it only waste (static) memory
198  for (int i=0; i<this.ParentNode.ChildNodes.Count; i++)
199  {
200  if (this.ParentNode.ChildNodes[i] == this)
201  {
202  this.ParentNode.ChildNodes.RemoveAt(i);
203  break;
204  }
205  }
206  }
207  }
208  }
209 
210  public override void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile)
211  {
212  // generate code of child nodes
213  foreach (SnmpNode childNode in this.childNodes)
214  {
215  if (childNode is SnmpTreeNode)
216  {
217  childNode.Generate(generatedFile, generatedHeaderFile);
218  }
219  }
220 
221  Comment dividerComment = new Comment(
222  String.Format("--- {0} {1} -----------------------------------------------------", this.Name, this.fullOid),
223  singleLine: true);
224 
225  generatedFile.Declarations.Add(dividerComment);
226  generatedFile.Implementation.Add(dividerComment);
227 
228  this.GenerateAggregateMethodDeclarations(generatedFile);
229 
230  foreach (SnmpNode childNode in this.childNodes)
231  {
232  if (!(childNode is SnmpTreeNode))
233  {
234  childNode.Generate(generatedFile, generatedHeaderFile);
235  }
236  }
237 
238  base.Generate(generatedFile, generatedHeaderFile);
239  }
240  }
241 }
virtual void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile)
Definition: SnmpNode.cs:81
override IEnumerable< SnmpScalarNode > AggregatedScalarNodes
Definition: SnmpTreeNode.cs:68
override void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile)
override void GenerateCode(MibCFile mibFile)
Definition: SnmpTreeNode.cs:91
override void Analyze()
Called after node structure creation is completed and before code is created. Offers the possibility ...
virtual void Analyze()
Called after node structure creation is completed and before code is created. Offers the possibility ...
Definition: SnmpNode.cs:115
List< CodeElement > Declarations
Definition: MibCFile.cs:76
SnmpTreeNode(SnmpTreeNode parentNode)
Definition: SnmpTreeNode.cs:46