SnmpScalarAggregationNode.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. using System.Collections.Generic;
  33. using System.Globalization;
  34. using CCodeGeneration;
  35. namespace LwipSnmpCodeGeneration
  36. {
  37. public abstract class SnmpScalarAggregationNode: SnmpNode
  38. {
  39. private bool getMethodRequired = false;
  40. private bool testMethodRequired = false;
  41. private bool setMethodRequired = false;
  42. protected SnmpScalarAggregationNode(SnmpTreeNode parentNode)
  43. : base(parentNode)
  44. {
  45. }
  46. protected virtual string GetMethodName
  47. {
  48. get { return this.FullNodeName + LwipDefs.FnctSuffix_GetValue; }
  49. }
  50. protected bool GetMethodRequired
  51. {
  52. get { return this.getMethodRequired; }
  53. }
  54. protected virtual string TestMethodName
  55. {
  56. get { return this.FullNodeName + LwipDefs.FnctSuffix_SetTest; }
  57. }
  58. protected bool TestMethodRequired
  59. {
  60. get { return this.testMethodRequired; }
  61. }
  62. protected virtual string SetMethodName
  63. {
  64. get { return this.FullNodeName + LwipDefs.FnctSuffix_SetValue; }
  65. }
  66. protected bool SetMethodRequired
  67. {
  68. get { return this.setMethodRequired; }
  69. }
  70. protected abstract IEnumerable<SnmpScalarNode> AggregatedScalarNodes
  71. {
  72. get;
  73. }
  74. public override void Analyze()
  75. {
  76. base.Analyze();
  77. this.getMethodRequired = false;
  78. this.testMethodRequired = false;
  79. this.setMethodRequired = false;
  80. foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
  81. {
  82. if ((scalarNode.AccessMode == SnmpAccessMode.ReadOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
  83. {
  84. this.getMethodRequired = true;
  85. }
  86. if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
  87. {
  88. this.testMethodRequired = true;
  89. this.setMethodRequired = true;
  90. }
  91. if (this.getMethodRequired && this.setMethodRequired)
  92. {
  93. break;
  94. }
  95. }
  96. }
  97. protected void GenerateAggregatedCode(MibCFile mibFile, VariableType instanceType, string switchSelector, bool generateDeclarations = true, bool generateImplementations = true)
  98. {
  99. if (this.getMethodRequired)
  100. {
  101. FunctionDeclaration getMethodDecl = new FunctionDeclaration(this.GetMethodName, isStatic: true);
  102. getMethodDecl.Parameter.Add(instanceType);
  103. getMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  104. getMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_S16);
  105. if (generateDeclarations)
  106. {
  107. mibFile.Declarations.Add(getMethodDecl);
  108. }
  109. if (generateImplementations)
  110. {
  111. Function getMethod = Function.FromDeclaration(getMethodDecl);
  112. GenerateGetMethodCode(getMethod, switchSelector);
  113. mibFile.Implementation.Add(getMethod);
  114. }
  115. }
  116. if (this.testMethodRequired)
  117. {
  118. FunctionDeclaration testMethodDecl = new FunctionDeclaration(this.TestMethodName, isStatic: true);
  119. testMethodDecl.Parameter.Add(instanceType);
  120. testMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
  121. testMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  122. testMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  123. if (generateDeclarations)
  124. {
  125. mibFile.Declarations.Add(testMethodDecl);
  126. }
  127. if (generateImplementations)
  128. {
  129. Function testMethod = Function.FromDeclaration(testMethodDecl);
  130. GenerateTestMethodCode(testMethod, switchSelector);
  131. mibFile.Implementation.Add(testMethod);
  132. }
  133. }
  134. if (this.setMethodRequired)
  135. {
  136. FunctionDeclaration setMethodDecl = new FunctionDeclaration(this.SetMethodName, isStatic: true);
  137. setMethodDecl.Parameter.Add(instanceType);
  138. setMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
  139. setMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  140. setMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  141. if (generateDeclarations)
  142. {
  143. mibFile.Declarations.Add(setMethodDecl);
  144. }
  145. if (generateImplementations)
  146. {
  147. Function setMethod = Function.FromDeclaration(setMethodDecl);
  148. GenerateSetMethodCode(setMethod, switchSelector);
  149. mibFile.Implementation.Add(setMethod);
  150. }
  151. }
  152. }
  153. protected virtual void GenerateGetMethodCode(Function getMethod, string switchSelector)
  154. {
  155. VariableDeclaration returnValue = new VariableDeclaration((VariableType)getMethod.ReturnType.Clone());
  156. returnValue.Type.Name = "value_len";
  157. getMethod.Declarations.Add(returnValue);
  158. Switch sw = new Switch(switchSelector);
  159. bool valueVarUsed = false;
  160. foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
  161. {
  162. if ((scalarNode.AccessMode == SnmpAccessMode.ReadOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
  163. {
  164. SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
  165. sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
  166. scalarNode.GenerateGetMethodCode(sc, getMethod.Parameter[1].Name, ref valueVarUsed, returnValue.Type.Name);
  167. sw.Switches.Add(sc);
  168. }
  169. }
  170. SwitchCase scd = SwitchCase.GenerateDefault();
  171. scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", getMethod.Name, switchSelector);
  172. scd.AddCodeFormat("{0} = 0;", returnValue.Type.Name);
  173. sw.Switches.Add(scd);
  174. if (!valueVarUsed)
  175. {
  176. getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[1].Name);
  177. }
  178. getMethod.AddElement(sw);
  179. getMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  180. }
  181. protected virtual void GenerateTestMethodCode(Function testMethod, string switchSelector)
  182. {
  183. VariableDeclaration returnValue = new VariableDeclaration((VariableType)testMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_WrongValue);
  184. returnValue.Type.Name = "err";
  185. testMethod.Declarations.Add(returnValue);
  186. Switch sw = new Switch(switchSelector);
  187. bool valueVarUsed = false;
  188. bool lenVarUsed = false;
  189. foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
  190. {
  191. if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
  192. {
  193. SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
  194. sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
  195. scalarNode.GenerateTestMethodCode(sc, testMethod.Parameter[2].Name, ref valueVarUsed, testMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
  196. sw.Switches.Add(sc);
  197. }
  198. }
  199. SwitchCase scd = SwitchCase.GenerateDefault();
  200. scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", testMethod.Name, switchSelector);
  201. sw.Switches.Add(scd);
  202. if (!valueVarUsed)
  203. {
  204. testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[2].Name);
  205. }
  206. if (!lenVarUsed)
  207. {
  208. testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[1].Name);
  209. }
  210. testMethod.AddElement(sw);
  211. testMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  212. }
  213. protected virtual void GenerateSetMethodCode(Function setMethod, string switchSelector)
  214. {
  215. VariableDeclaration returnValue = new VariableDeclaration((VariableType)setMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_Ok);
  216. returnValue.Type.Name = "err";
  217. setMethod.Declarations.Add(returnValue);
  218. Switch sw = new Switch(switchSelector);
  219. bool valueVarUsed = false;
  220. bool lenVarUsed = false;
  221. foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
  222. {
  223. if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
  224. {
  225. SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
  226. sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
  227. scalarNode.GenerateSetMethodCode(sc, setMethod.Parameter[2].Name, ref valueVarUsed, setMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
  228. sw.Switches.Add(sc);
  229. }
  230. }
  231. SwitchCase scd = SwitchCase.GenerateDefault();
  232. scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", setMethod.Name, switchSelector);
  233. sw.Switches.Add(scd);
  234. if (!valueVarUsed)
  235. {
  236. setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[2].Name);
  237. }
  238. if (!lenVarUsed)
  239. {
  240. setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[1].Name);
  241. }
  242. setMethod.AddElement(sw);
  243. setMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  244. }
  245. }
  246. }