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.Text; 35 using CCodeGeneration; 36 37 namespace LwipSnmpCodeGeneration 38 { 39 public class SnmpScalarNodeBits : SnmpScalarNode 40 { 41 private readonly uint bitCount; 42 SnmpScalarNodeBits(SnmpTreeNode parentNode, uint bitCount)43 public SnmpScalarNodeBits(SnmpTreeNode parentNode, uint bitCount) 44 : base(parentNode) 45 { 46 this.DataType = SnmpDataType.Bits; 47 this.bitCount = bitCount; 48 } 49 GenerateGetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string retLenVarName)50 public override void GenerateGetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string retLenVarName) 51 { 52 container.AddCode(String.Format( 53 "{0} = snmp_encode_bits(({1} *){2}, SNMP_MAX_VALUE_SIZE, 0 /* TODO: pass real value here */, {3});", 54 retLenVarName, 55 LwipDefs.Vt_U8, 56 valueVarName, 57 this.bitCount)); 58 59 valueVarUsed = true; 60 } 61 GenerateTestMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)62 public override void GenerateTestMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName) 63 { 64 if (this.Restrictions.Count > 0) 65 { 66 const string bitVarName = "bits"; 67 68 container.Declarations.Add(new VariableDeclaration(new VariableType(bitVarName, LwipDefs.Vt_U32))); 69 70 IfThenElse ite = new IfThenElse(String.Format( 71 "snmp_decode_bits(({0} *){1}, {2}, &{3}) == ERR_OK", 72 LwipDefs.Vt_U8, 73 valueVarName, 74 lenVarName, 75 bitVarName)); 76 77 valueVarUsed = true; 78 lenVarUsed = true; 79 80 StringBuilder innerIfCond = new StringBuilder(); 81 foreach (IRestriction restriction in this.Restrictions) 82 { 83 innerIfCond.Append(restriction.GetCheckCodeValid(bitVarName)); 84 innerIfCond.Append(" || "); 85 } 86 87 innerIfCond.Length -= 4; 88 89 IfThenElse innerIte = new IfThenElse(innerIfCond.ToString()); 90 innerIte.AddCode(String.Format("{0} = {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok)); 91 ite.AddElement(innerIte); 92 container.AddElement(ite); 93 } 94 else 95 { 96 base.GenerateTestMethodCode(container, valueVarName, ref valueVarUsed, lenVarName, ref lenVarUsed, retErrVarName); 97 } 98 } 99 GenerateSetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)100 public override void GenerateSetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName) 101 { 102 const string bitVarName = "bits"; 103 104 container.Declarations.Add(new VariableDeclaration(new VariableType(bitVarName, LwipDefs.Vt_U32))); 105 106 IfThenElse ite = new IfThenElse(String.Format( 107 "snmp_decode_bits(({0} *){1}, {2}, &{3}) == ERR_OK", 108 LwipDefs.Vt_U8, 109 valueVarName, 110 lenVarName, 111 bitVarName)); 112 113 valueVarUsed = true; 114 lenVarUsed = true; 115 116 ite.AddElement(new Comment(String.Format("TODO: store new value contained in '{0}' here", bitVarName), singleLine: true)); 117 118 container.AddElement(ite); 119 } 120 } 121 } 122