SnmpScalarNode.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 CCodeGeneration;
  35. namespace LwipSnmpCodeGeneration
  36. {
  37. public class SnmpScalarNode: SnmpNode
  38. {
  39. protected const string LocalValueName = "v"; // name of (casted) local value variable
  40. private SnmpDataType dataType;
  41. private SnmpAccessMode accessMode;
  42. private readonly List<IRestriction> restrictions = new List<IRestriction>();
  43. private bool useExternalMethods = false;
  44. private string externalGetMethod;
  45. private string externalTestMethod;
  46. private string externalSetMethod;
  47. public SnmpScalarNode(SnmpTreeNode parentNode)
  48. : base(parentNode)
  49. {
  50. }
  51. public override string FullNodeName
  52. {
  53. get { return this.Name.ToLowerInvariant() + "_scalar"; }
  54. }
  55. public SnmpDataType DataType
  56. {
  57. get { return this.dataType; }
  58. set { this.dataType = value; }
  59. }
  60. public List<IRestriction> Restrictions
  61. {
  62. get { return this.restrictions; }
  63. }
  64. public SnmpAccessMode AccessMode
  65. {
  66. get { return this.accessMode; }
  67. set { this.accessMode = value; }
  68. }
  69. public virtual string FixedValueLength
  70. {
  71. get { return null; }
  72. }
  73. /// <summary>
  74. /// If scalar is used as a table index its value becomes part of the OID. This value returns how many OID parts are required to represent this value.
  75. /// </summary>
  76. public virtual int OidRepresentationLen
  77. {
  78. get { return -1; }
  79. }
  80. public bool UseExternalMethods
  81. {
  82. get { return this.useExternalMethods; }
  83. set { this.useExternalMethods = value; }
  84. }
  85. public string ExternalGetMethod
  86. {
  87. get { return this.externalGetMethod; }
  88. set { this.externalGetMethod = value; }
  89. }
  90. public string ExternalTestMethod
  91. {
  92. get { return this.externalTestMethod; }
  93. set { this.externalTestMethod = value; }
  94. }
  95. public string ExternalSetMethod
  96. {
  97. get { return this.externalSetMethod; }
  98. set { this.externalSetMethod = value; }
  99. }
  100. public override void GenerateCode(MibCFile mibFile)
  101. {
  102. string getMethodName;
  103. string testMethodName;
  104. string setMethodName;
  105. if (this.useExternalMethods)
  106. {
  107. getMethodName = this.externalGetMethod;
  108. testMethodName = this.externalTestMethod;
  109. setMethodName = this.externalSetMethod;
  110. }
  111. else
  112. {
  113. getMethodName = LwipDefs.Null;
  114. testMethodName = LwipDefs.Null;
  115. setMethodName = LwipDefs.Null;
  116. if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.ReadOnly))
  117. {
  118. FunctionDeclaration getMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_GetValue, isStatic: true);
  119. getMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*"));
  120. getMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  121. getMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_S16);
  122. mibFile.Declarations.Add(getMethodDecl);
  123. Function getMethod = Function.FromDeclaration(getMethodDecl);
  124. getMethodName = getMethod.Name;
  125. VariableDeclaration returnValue = new VariableDeclaration((VariableType)getMethod.ReturnType.Clone());
  126. returnValue.Type.Name = "value_len";
  127. getMethod.Declarations.Add(returnValue);
  128. getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[0].Name);
  129. bool valueVarUsed = false;
  130. GenerateGetMethodCode(getMethod, getMethod.Parameter[1].Name, ref valueVarUsed, returnValue.Type.Name);
  131. if (!valueVarUsed)
  132. {
  133. getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[1].Name);
  134. }
  135. getMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  136. mibFile.Implementation.Add(getMethod);
  137. }
  138. if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.WriteOnly))
  139. {
  140. bool valueVarUsed;
  141. bool lenVarUsed;
  142. VariableDeclaration returnValue;
  143. if (this.restrictions.Count > 0)
  144. {
  145. FunctionDeclaration testMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetTest, isStatic: true);
  146. testMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*"));
  147. testMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
  148. testMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  149. testMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  150. mibFile.Declarations.Add(testMethodDecl);
  151. Function testMethod = Function.FromDeclaration(testMethodDecl);
  152. testMethodName = testMethod.Name;
  153. returnValue = new VariableDeclaration((VariableType)testMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_WrongValue);
  154. returnValue.Type.Name = "err";
  155. testMethod.Declarations.Add(returnValue);
  156. testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[0].Name);
  157. valueVarUsed = false;
  158. lenVarUsed = false;
  159. GenerateTestMethodCode(testMethod, testMethod.Parameter[2].Name, ref valueVarUsed, testMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
  160. if (!valueVarUsed)
  161. {
  162. testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[2].Name);
  163. }
  164. if (!lenVarUsed)
  165. {
  166. testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[1].Name);
  167. }
  168. testMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  169. mibFile.Implementation.Add(testMethod);
  170. }
  171. else
  172. {
  173. testMethodName = LwipDefs.FnctName_SetTest_Ok;
  174. }
  175. FunctionDeclaration setMethodDecl = null;
  176. setMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetValue, isStatic: true);
  177. setMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*"));
  178. setMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
  179. setMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
  180. setMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
  181. mibFile.Declarations.Add(setMethodDecl);
  182. Function setMethod = Function.FromDeclaration(setMethodDecl);
  183. setMethodName = setMethod.Name;
  184. returnValue = new VariableDeclaration((VariableType)setMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_Ok);
  185. returnValue.Type.Name = "err";
  186. setMethod.Declarations.Add(returnValue);
  187. setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[0].Name);
  188. valueVarUsed = false;
  189. lenVarUsed = false;
  190. GenerateSetMethodCode(setMethod, setMethod.Parameter[2].Name, ref valueVarUsed, setMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
  191. if (!valueVarUsed)
  192. {
  193. setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[2].Name);
  194. }
  195. if (!lenVarUsed)
  196. {
  197. setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[1].Name);
  198. }
  199. setMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
  200. mibFile.Implementation.Add(setMethod);
  201. }
  202. }
  203. // create and add node declaration
  204. string nodeInitialization;
  205. if (this.accessMode == SnmpAccessMode.ReadOnly)
  206. {
  207. nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE_READONLY({0}, {1}, {2})",
  208. this.Oid,
  209. LwipDefs.GetAsn1DefForSnmpDataType(this.dataType),
  210. getMethodName);
  211. }
  212. else
  213. {
  214. nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE({0}, {1}, {2}, {3}, {4}, {5})",
  215. this.Oid,
  216. LwipDefs.GetLwipDefForSnmpAccessMode(this.accessMode),
  217. LwipDefs.GetAsn1DefForSnmpDataType(this.dataType),
  218. getMethodName,
  219. testMethodName,
  220. setMethodName);
  221. }
  222. mibFile.Declarations.Add(new VariableDeclaration(
  223. new VariableType(this.FullNodeName, LwipDefs.Vt_StScalarNode, null, ConstType.Value),
  224. nodeInitialization, isStatic: true));
  225. }
  226. public virtual void GenerateGetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string retLenVarName)
  227. {
  228. bool localValueVarUsed;
  229. if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
  230. {
  231. valueVarUsed = true;
  232. localValueVarUsed = false;
  233. }
  234. else
  235. {
  236. localValueVarUsed = true; // do not generate UNUSED_ARG code
  237. }
  238. if (this.FixedValueLength == null)
  239. {
  240. // check that value with variable length fits into buffer
  241. container.AddElement(new Comment(String.Format("TODO: take care that value with variable length fits into buffer: ({0} <= SNMP_MAX_VALUE_SIZE)", retLenVarName), singleLine: true));
  242. }
  243. GenerateGetMethodCodeCore(container, LocalValueName, ref localValueVarUsed, retLenVarName);
  244. if (!localValueVarUsed)
  245. {
  246. container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
  247. }
  248. }
  249. protected virtual void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
  250. {
  251. container.AddElement(new Comment(String.Format("TODO: put requested value to '*{0}' here", localValueVarName), singleLine: true));
  252. container.AddCodeFormat("{0} = {1};",
  253. retLenVarName,
  254. (!String.IsNullOrWhiteSpace(this.FixedValueLength)) ? this.FixedValueLength : "0");
  255. }
  256. public virtual void GenerateTestMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
  257. {
  258. if (this.Restrictions.Count > 0)
  259. {
  260. bool localVarUsed;
  261. if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
  262. {
  263. valueVarUsed = true;
  264. localVarUsed = false;
  265. }
  266. else
  267. {
  268. localVarUsed = true; // do not generate UNUSED_ARG code
  269. }
  270. if (!String.IsNullOrWhiteSpace(this.FixedValueLength))
  271. {
  272. // check for fixed value
  273. container.AddCodeFormat("LWIP_ASSERT(\"Invalid length for datatype\", ({0} == {1}));", lenVarName, this.FixedValueLength);
  274. lenVarUsed = true;
  275. }
  276. GenerateTestMethodCodeCore(container, LocalValueName, ref localVarUsed, lenVarName, ref lenVarUsed, retErrVarName);
  277. if (!localVarUsed)
  278. {
  279. container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
  280. }
  281. }
  282. else
  283. {
  284. container.AddCodeFormat("{0} = {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok);
  285. }
  286. }
  287. protected virtual void GenerateTestMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
  288. {
  289. container.AddElement(new Comment(String.Format("TODO: test new value here:\nif (*{0} == ) {1} = {2};", localValueVarName, retErrVarName, LwipDefs.Def_ErrorCode_Ok)));
  290. }
  291. public virtual void GenerateSetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
  292. {
  293. bool localVarUsed;
  294. if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
  295. {
  296. valueVarUsed = true;
  297. localVarUsed = false;
  298. }
  299. else
  300. {
  301. localVarUsed = true; // do not generate UNUSED_ARG code
  302. }
  303. GenerateSetMethodCodeCore(container, LocalValueName, ref localVarUsed, lenVarName, ref lenVarUsed, retErrVarName);
  304. if (!localVarUsed)
  305. {
  306. container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
  307. }
  308. }
  309. protected virtual void GenerateSetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
  310. {
  311. container.AddElement(new Comment(String.Format("TODO: store new value contained in '*{0}' here", localValueVarName), singleLine: true));
  312. }
  313. protected virtual bool GenerateValueDeclaration(CodeContainerBase container, string variableName, string sourceName)
  314. {
  315. container.AddDeclaration(new VariableDeclaration(
  316. new VariableType(variableName, LwipDefs.Vt_U8, "*"),
  317. "(" + new VariableType(null, LwipDefs.Vt_U8, "*") + ")" + sourceName));
  318. return true;
  319. }
  320. public static SnmpScalarNode CreateFromDatatype(SnmpDataType dataType, SnmpTreeNode parentNode)
  321. {
  322. switch (dataType)
  323. {
  324. case SnmpDataType.Integer:
  325. return new SnmpScalarNodeInt(parentNode);
  326. case SnmpDataType.Gauge:
  327. case SnmpDataType.Counter:
  328. case SnmpDataType.TimeTicks:
  329. return new SnmpScalarNodeUint(dataType, parentNode);
  330. }
  331. return new SnmpScalarNode(parentNode);
  332. }
  333. }
  334. }