SnmpScalarNodeOctetString.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.Text;
  34. using CCodeGeneration;
  35. namespace LwipSnmpCodeGeneration
  36. {
  37. public class SnmpScalarNodeOctetString : SnmpScalarNode
  38. {
  39. public SnmpScalarNodeOctetString(SnmpDataType dataType, SnmpTreeNode parentNode)
  40. : base(parentNode)
  41. {
  42. System.Diagnostics.Debug.Assert(
  43. (dataType == SnmpDataType.OctetString) ||
  44. (dataType == SnmpDataType.Opaque) ||
  45. (dataType == SnmpDataType.IpAddress));
  46. this.DataType = dataType;
  47. }
  48. protected override void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
  49. {
  50. if (this.Restrictions.Count > 0)
  51. {
  52. StringBuilder ifCond = new StringBuilder();
  53. foreach (IRestriction restriction in this.Restrictions)
  54. {
  55. ifCond.Append(restriction.GetCheckCodeValid(retLenVarName));
  56. ifCond.Append(" || ");
  57. }
  58. ifCond.Length -= 4;
  59. container.AddElement(new Comment("TODO: take care of len restrictions defined in MIB: " + ifCond, singleLine: true));
  60. }
  61. base.GenerateGetMethodCodeCore(container, localValueVarName, ref localValueVarUsed, retLenVarName);
  62. }
  63. protected override void GenerateTestMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
  64. {
  65. System.Diagnostics.Trace.Assert(this.Restrictions.Count > 0);
  66. // checks refer to length of octet string
  67. StringBuilder ifCond = new StringBuilder();
  68. foreach (IRestriction restriction in this.Restrictions)
  69. {
  70. ifCond.Append(restriction.GetCheckCodeValid(lenVarName));
  71. ifCond.Append(" || ");
  72. lenVarUsed = true;
  73. }
  74. ifCond.Length -= 4;
  75. IfThenElse ite = new IfThenElse(ifCond.ToString());
  76. ite.AddCode(String.Format("{0} = {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok));
  77. container.AddElement(ite);
  78. }
  79. public override int OidRepresentationLen
  80. {
  81. get
  82. {
  83. // check restrictions if we are set to one fixed length
  84. if ((this.Restrictions != null) && (this.Restrictions.Count > 0))
  85. {
  86. foreach (IRestriction restriction in this.Restrictions)
  87. {
  88. if (restriction is IsInRangeRestriction)
  89. {
  90. if ((restriction as IsInRangeRestriction).RangeStart == (restriction as IsInRangeRestriction).RangeEnd)
  91. {
  92. return (int)(restriction as IsInRangeRestriction).RangeStart;
  93. }
  94. }
  95. else if (restriction is IsEqualRestriction)
  96. {
  97. return (int)(restriction as IsEqualRestriction).Value;
  98. }
  99. }
  100. }
  101. return -1; // variable length
  102. }
  103. }
  104. }
  105. }