SnmpTableNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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;
  33. using System.Collections.Generic;
  34. using System.Text;
  35. using CCodeGeneration;
  36. namespace LwipSnmpCodeGeneration
  37. {
  38. public class SnmpTableNode: SnmpScalarAggregationNode
  39. {
  40. private readonly List<SnmpScalarNode> cellNodes = new List<SnmpScalarNode>();
  41. private readonly List<SnmpScalarNode> indexNodes = new List<SnmpScalarNode>();
  42. private string augmentedTableRow = null;
  43. public SnmpTableNode(SnmpTreeNode parentNode)
  44. : base(parentNode)
  45. {
  46. }
  47. public List<SnmpScalarNode> CellNodes
  48. {
  49. get { return cellNodes; }
  50. }
  51. public List<SnmpScalarNode> IndexNodes
  52. {
  53. get { return indexNodes; }
  54. }
  55. public string AugmentedTableRow
  56. {
  57. get { return this.augmentedTableRow; }
  58. set { this.augmentedTableRow = value; }
  59. }
  60. public override string FullNodeName
  61. {
  62. get
  63. {
  64. string result = this.Name.ToLowerInvariant();
  65. if (!result.Contains("table"))
  66. {
  67. result += "_table";
  68. }
  69. return result;
  70. }
  71. }
  72. protected override IEnumerable<SnmpScalarNode> AggregatedScalarNodes
  73. {
  74. get { return this.cellNodes; }
  75. }
  76. public override void GenerateCode(MibCFile mibFile)
  77. {
  78. FunctionDeclaration getInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetInstance, isStatic: true);
  79. getInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value));
  80. getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_U32, "*", ConstType.Value));
  81. getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid_len", LwipDefs.Vt_U8, ""));
  82. getInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*"));
  83. getInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  84. mibFile.Declarations.Add(getInstanceMethodDecl);
  85. Function getInstanceMethod = Function.FromDeclaration(getInstanceMethodDecl);
  86. GenerateGetInstanceMethodCode(getInstanceMethod);
  87. mibFile.Implementation.Add(getInstanceMethod);
  88. FunctionDeclaration getNextInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetNextInstance, isStatic: true);
  89. getNextInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value));
  90. getNextInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_StObjectId, "*"));
  91. getNextInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*"));
  92. getNextInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  93. mibFile.Declarations.Add(getNextInstanceMethodDecl);
  94. Function getNextInstanceMethod = Function.FromDeclaration(getNextInstanceMethodDecl);
  95. GenerateGetNextInstanceMethodCode(getNextInstanceMethod);
  96. mibFile.Implementation.Add(getNextInstanceMethod);
  97. VariableType instanceType = new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*");
  98. GenerateAggregatedCode(
  99. mibFile,
  100. instanceType,
  101. String.Format("SNMP_TABLE_GET_COLUMN_FROM_OID({0}->instance_oid.id)", instanceType.Name));
  102. #region create and add column/table definitions
  103. StringBuilder colDefs = new StringBuilder();
  104. foreach (SnmpScalarNode colNode in this.cellNodes)
  105. {
  106. colDefs.AppendFormat(" {{{0}, {1}, {2}}}, /* {3} */ \n",
  107. colNode.Oid,
  108. LwipDefs.GetAsn1DefForSnmpDataType(colNode.DataType),
  109. LwipDefs.GetLwipDefForSnmpAccessMode(colNode.AccessMode),
  110. colNode.Name);
  111. }
  112. if (colDefs.Length > 0)
  113. {
  114. colDefs.Length--;
  115. }
  116. VariableDeclaration colDefsDecl = new VariableDeclaration(
  117. new VariableType(this.FullNodeName + "_columns", LwipDefs.Vt_StTableColumnDef, null, ConstType.Value, String.Empty),
  118. "{\n" + colDefs + "\n}",
  119. isStatic: true);
  120. mibFile.Declarations.Add(colDefsDecl);
  121. string nodeInitialization = String.Format("SNMP_TABLE_CREATE({0}, {1}, {2}, {3}, {4}, {5}, {6})",
  122. this.Oid,
  123. colDefsDecl.Type.Name,
  124. getInstanceMethodDecl.Name, getNextInstanceMethodDecl.Name,
  125. (this.GetMethodRequired) ? this.GetMethodName : LwipDefs.Null,
  126. (this.TestMethodRequired) ? this.TestMethodName : LwipDefs.Null,
  127. (this.SetMethodRequired) ? this.SetMethodName : LwipDefs.Null
  128. );
  129. mibFile.Declarations.Add(new VariableDeclaration(
  130. new VariableType(this.FullNodeName, LwipDefs.Vt_StTableNode, null, ConstType.Value),
  131. nodeInitialization,
  132. isStatic: true));
  133. #endregion
  134. }
  135. protected virtual void GenerateGetInstanceMethodCode(Function getInstanceMethod)
  136. {
  137. VariableDeclaration returnValue = new VariableDeclaration((VariableType)getInstanceMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_NoSuchInstance);
  138. returnValue.Type.Name = "err";
  139. getInstanceMethod.Declarations.Add(returnValue);
  140. int instanceOidLength = 0;
  141. StringBuilder indexColumns = new StringBuilder();
  142. foreach (SnmpScalarNode indexNode in this.indexNodes)
  143. {
  144. if (instanceOidLength >= 0)
  145. {
  146. if (indexNode.OidRepresentationLen >= 0)
  147. {
  148. instanceOidLength += indexNode.OidRepresentationLen;
  149. }
  150. else
  151. {
  152. // at least one index column has a variable length -> we cannot perform a static check
  153. instanceOidLength = -1;
  154. }
  155. }
  156. indexColumns.AppendFormat(
  157. " {0} ({1}, OID length = {2})\n",
  158. indexNode.Name,
  159. indexNode.DataType,
  160. (indexNode.OidRepresentationLen >= 0) ? indexNode.OidRepresentationLen.ToString() : "variable");
  161. }
  162. if (indexColumns.Length > 0)
  163. {
  164. indexColumns.Length--;
  165. getInstanceMethod.Declarations.Insert(0, new Comment(String.Format(
  166. "The instance OID of this table consists of following (index) column(s):\n{0}",
  167. indexColumns)));
  168. }
  169. string augmentsHint = "";
  170. if (!String.IsNullOrWhiteSpace(this.augmentedTableRow))
  171. {
  172. augmentsHint = String.Format(
  173. "This table augments table '{0}'! Index columns therefore belong to table '{0}'!\n" +
  174. "You may simply call the '*{1}' method of this table.\n\n",
  175. (this.augmentedTableRow.ToLowerInvariant().EndsWith("entry")) ? this.augmentedTableRow.Substring(0, this.augmentedTableRow.Length-5) : this.augmentedTableRow,
  176. LwipDefs.FnctSuffix_GetInstance);
  177. }
  178. CodeContainerBase ccb = getInstanceMethod;
  179. if (instanceOidLength > 0)
  180. {
  181. IfThenElse ite = new IfThenElse(String.Format("{0} == {1}", getInstanceMethod.Parameter[2].Name, instanceOidLength));
  182. getInstanceMethod.AddElement(ite);
  183. ccb = ite;
  184. }
  185. ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[0].Name);
  186. ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[1].Name);
  187. if (instanceOidLength <= 0)
  188. {
  189. ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[2].Name);
  190. }
  191. ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[3].Name);
  192. ccb.AddElement(new Comment(String.Format(
  193. "TODO: check if '{0}'/'{1}' params contain a valid instance oid for a row\n" +
  194. "If so, set '{2} = {3};'\n\n" +
  195. "snmp_oid_* methods may be used for easier processing of oid\n\n" +
  196. "{4}" +
  197. "In order to avoid decoding OID a second time in subsequent get_value/set_test/set_value methods,\n" +
  198. "you may store an arbitrary value (like a pointer to target value object) in '{5}->reference'/'{5}->reference_len'.\n" +
  199. "But be aware that not always a subsequent method is called -> Do NOT allocate memory here and try to release it in subsequent methods!\n\n" +
  200. "You also may replace function pointers in '{5}' param for get/test/set methods which contain the default values from table definition,\n" +
  201. "in order to provide special methods, for the currently processed cell. Changed pointers are only valid for current request.",
  202. getInstanceMethod.Parameter[1].Name,
  203. getInstanceMethod.Parameter[2].Name,
  204. returnValue.Type.Name,
  205. LwipDefs.Def_ErrorCode_Ok,
  206. augmentsHint,
  207. getInstanceMethod.Parameter[3].Name
  208. )));
  209. getInstanceMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  210. }
  211. protected virtual void GenerateGetNextInstanceMethodCode(Function getNextInstanceMethod)
  212. {
  213. getNextInstanceMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getNextInstanceMethod.Parameter[0].Name);
  214. getNextInstanceMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getNextInstanceMethod.Parameter[1].Name);
  215. getNextInstanceMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getNextInstanceMethod.Parameter[2].Name);
  216. VariableDeclaration returnValue = new VariableDeclaration((VariableType)getNextInstanceMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_NoSuchInstance);
  217. returnValue.Type.Name = "err";
  218. getNextInstanceMethod.Declarations.Add(returnValue);
  219. StringBuilder indexColumns = new StringBuilder();
  220. foreach (SnmpScalarNode indexNode in this.indexNodes)
  221. {
  222. indexColumns.AppendFormat(
  223. " {0} ({1}, OID length = {2})\n",
  224. indexNode.Name,
  225. indexNode.DataType,
  226. (indexNode.OidRepresentationLen >= 0) ? indexNode.OidRepresentationLen.ToString() : "variable");
  227. }
  228. if (indexColumns.Length > 0)
  229. {
  230. indexColumns.Length--;
  231. getNextInstanceMethod.Declarations.Insert(0, new Comment(String.Format(
  232. "The instance OID of this table consists of following (index) column(s):\n{0}",
  233. indexColumns)));
  234. }
  235. string augmentsHint = "";
  236. if (!String.IsNullOrWhiteSpace(this.augmentedTableRow))
  237. {
  238. augmentsHint = String.Format(
  239. "This table augments table '{0}'! Index columns therefore belong to table '{0}'!\n" +
  240. "You may simply call the '*{1}' method of this table.\n\n",
  241. (this.augmentedTableRow.ToLowerInvariant().EndsWith("entry")) ? this.augmentedTableRow.Substring(0, this.augmentedTableRow.Length-5) : this.augmentedTableRow,
  242. LwipDefs.FnctSuffix_GetNextInstance);
  243. }
  244. getNextInstanceMethod.AddElement(new Comment(String.Format(
  245. "TODO: analyze '{0}->id'/'{0}->len' and return the subsequent row instance\n" +
  246. "Be aware that '{0}->id'/'{0}->len' must not point to a valid instance or have correct instance length.\n" +
  247. "If '{0}->len' is 0, return the first instance. If '{0}->len' is longer than expected, cut superfluous OID parts.\n" +
  248. "If a valid next instance is found, store it in '{0}->id'/'{0}->len' and set '{1} = {2};'\n\n" +
  249. "snmp_oid_* methods may be used for easier processing of oid\n\n" +
  250. "{3}" +
  251. "In order to avoid decoding OID a second time in subsequent get_value/set_test/set_value methods,\n" +
  252. "you may store an arbitrary value (like a pointer to target value object) in '{4}->reference'/'{4}->reference_len'.\n" +
  253. "But be aware that not always a subsequent method is called -> Do NOT allocate memory here and try to release it in subsequent methods!\n\n" +
  254. "You also may replace function pointers in '{4}' param for get/test/set methods which contain the default values from table definition,\n" +
  255. "in order to provide special methods, for the currently processed cell. Changed pointers are only valid for current request.",
  256. getNextInstanceMethod.Parameter[1].Name,
  257. returnValue.Type.Name,
  258. LwipDefs.Def_ErrorCode_Ok,
  259. augmentsHint,
  260. getNextInstanceMethod.Parameter[2].Name
  261. )));
  262. getNextInstanceMethod.AddElement(new Comment(String.Format(
  263. "For easier processing and getting the next instance, you may use the 'snmp_next_oid_*' enumerator.\n" +
  264. "Simply pass all known instance OID's to it and it returns the next valid one:\n\n" +
  265. "{0} state;\n" +
  266. "{1} result_buf;\n" +
  267. "snmp_next_oid_init(&state, {2}->id, {2}->len, result_buf.id, SNMP_MAX_OBJ_ID_LEN);\n" +
  268. "while ({{not all instances passed}}) {{\n" +
  269. " {1} test_oid;\n" +
  270. " {{fill test_oid to create instance oid for next instance}}\n" +
  271. " snmp_next_oid_check(&state, test_oid.id, test_oid.len, {{target_data_ptr}});\n" +
  272. "}}\n" +
  273. "if(state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {{\n" +
  274. " snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);\n" +
  275. " {3}->reference.ptr = state.reference; //==target_data_ptr, for usage in subsequent get/test/set\n" +
  276. " {4} = {5};\n" +
  277. "}}"
  278. ,
  279. LwipDefs.Vt_StNextOidState,
  280. LwipDefs.Vt_StObjectId,
  281. getNextInstanceMethod.Parameter[1].Name,
  282. getNextInstanceMethod.Parameter[2].Name,
  283. returnValue.Type.Name,
  284. LwipDefs.Def_ErrorCode_Ok
  285. )));
  286. getNextInstanceMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  287. }
  288. }
  289. }