1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/*
* Copyright (c) 2001-2004 Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* This file is part of the lwIP TCP/IP stack.
*
* Author: Martin Hentschel <info@cl-soft.de>
*
*/
using System.Collections.Generic;
using System.Globalization;
using CCodeGeneration;
namespace LwipSnmpCodeGeneration
{
public abstract class SnmpScalarAggregationNode: SnmpNode
{
private bool getMethodRequired = false;
private bool testMethodRequired = false;
private bool setMethodRequired = false;
protected SnmpScalarAggregationNode(SnmpTreeNode parentNode)
: base(parentNode)
{
}
protected virtual string GetMethodName
{
get { return this.FullNodeName + LwipDefs.FnctSuffix_GetValue; }
}
protected bool GetMethodRequired
{
get { return this.getMethodRequired; }
}
protected virtual string TestMethodName
{
get { return this.FullNodeName + LwipDefs.FnctSuffix_SetTest; }
}
protected bool TestMethodRequired
{
get { return this.testMethodRequired; }
}
protected virtual string SetMethodName
{
get { return this.FullNodeName + LwipDefs.FnctSuffix_SetValue; }
}
protected bool SetMethodRequired
{
get { return this.setMethodRequired; }
}
protected abstract IEnumerable<SnmpScalarNode> AggregatedScalarNodes
{
get;
}
public override void Analyze()
{
base.Analyze();
this.getMethodRequired = false;
this.testMethodRequired = false;
this.setMethodRequired = false;
foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
{
if ((scalarNode.AccessMode == SnmpAccessMode.ReadOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
{
this.getMethodRequired = true;
}
if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
{
this.testMethodRequired = true;
this.setMethodRequired = true;
}
if (this.getMethodRequired && this.setMethodRequired)
{
break;
}
}
}
protected void GenerateAggregatedCode(MibCFile mibFile, VariableType instanceType, string switchSelector, bool generateDeclarations = true, bool generateImplementations = true)
{
if (this.getMethodRequired)
{
FunctionDeclaration getMethodDecl = new FunctionDeclaration(this.GetMethodName, isStatic: true);
getMethodDecl.Parameter.Add(instanceType);
getMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
getMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_S16);
if (generateDeclarations)
{
mibFile.Declarations.Add(getMethodDecl);
}
if (generateImplementations)
{
Function getMethod = Function.FromDeclaration(getMethodDecl);
GenerateGetMethodCode(getMethod, switchSelector);
mibFile.Implementation.Add(getMethod);
}
}
if (this.testMethodRequired)
{
FunctionDeclaration testMethodDecl = new FunctionDeclaration(this.TestMethodName, isStatic: true);
testMethodDecl.Parameter.Add(instanceType);
testMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
testMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
testMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
if (generateDeclarations)
{
mibFile.Declarations.Add(testMethodDecl);
}
if (generateImplementations)
{
Function testMethod = Function.FromDeclaration(testMethodDecl);
GenerateTestMethodCode(testMethod, switchSelector);
mibFile.Implementation.Add(testMethod);
}
}
if (this.setMethodRequired)
{
FunctionDeclaration setMethodDecl = new FunctionDeclaration(this.SetMethodName, isStatic: true);
setMethodDecl.Parameter.Add(instanceType);
setMethodDecl.Parameter.Add(new VariableType("len", LwipDefs.Vt_U16));
setMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*"));
setMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err);
if (generateDeclarations)
{
mibFile.Declarations.Add(setMethodDecl);
}
if (generateImplementations)
{
Function setMethod = Function.FromDeclaration(setMethodDecl);
GenerateSetMethodCode(setMethod, switchSelector);
mibFile.Implementation.Add(setMethod);
}
}
}
protected virtual void GenerateGetMethodCode(Function getMethod, string switchSelector)
{
VariableDeclaration returnValue = new VariableDeclaration((VariableType)getMethod.ReturnType.Clone());
returnValue.Type.Name = "value_len";
getMethod.Declarations.Add(returnValue);
Switch sw = new Switch(switchSelector);
bool valueVarUsed = false;
foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
{
if ((scalarNode.AccessMode == SnmpAccessMode.ReadOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
{
SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
scalarNode.GenerateGetMethodCode(sc, getMethod.Parameter[1].Name, ref valueVarUsed, returnValue.Type.Name);
sw.Switches.Add(sc);
}
}
SwitchCase scd = SwitchCase.GenerateDefault();
scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", getMethod.Name, switchSelector);
scd.AddCodeFormat("{0} = 0;", returnValue.Type.Name);
sw.Switches.Add(scd);
if (!valueVarUsed)
{
getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[1].Name);
}
getMethod.AddElement(sw);
getMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
}
protected virtual void GenerateTestMethodCode(Function testMethod, string switchSelector)
{
VariableDeclaration returnValue = new VariableDeclaration((VariableType)testMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_WrongValue);
returnValue.Type.Name = "err";
testMethod.Declarations.Add(returnValue);
Switch sw = new Switch(switchSelector);
bool valueVarUsed = false;
bool lenVarUsed = false;
foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
{
if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
{
SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
scalarNode.GenerateTestMethodCode(sc, testMethod.Parameter[2].Name, ref valueVarUsed, testMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
sw.Switches.Add(sc);
}
}
SwitchCase scd = SwitchCase.GenerateDefault();
scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", testMethod.Name, switchSelector);
sw.Switches.Add(scd);
if (!valueVarUsed)
{
testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[2].Name);
}
if (!lenVarUsed)
{
testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[1].Name);
}
testMethod.AddElement(sw);
testMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
}
protected virtual void GenerateSetMethodCode(Function setMethod, string switchSelector)
{
VariableDeclaration returnValue = new VariableDeclaration((VariableType)setMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_Ok);
returnValue.Type.Name = "err";
setMethod.Declarations.Add(returnValue);
Switch sw = new Switch(switchSelector);
bool valueVarUsed = false;
bool lenVarUsed = false;
foreach (SnmpScalarNode scalarNode in this.AggregatedScalarNodes)
{
if ((scalarNode.AccessMode == SnmpAccessMode.WriteOnly) || (scalarNode.AccessMode == SnmpAccessMode.ReadWrite))
{
SwitchCase sc = new SwitchCase(scalarNode.Oid.ToString(CultureInfo.InvariantCulture));
sc.Declarations.Add(new Comment(scalarNode.Name, singleLine: true));
scalarNode.GenerateSetMethodCode(sc, setMethod.Parameter[2].Name, ref valueVarUsed, setMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name);
sw.Switches.Add(sc);
}
}
SwitchCase scd = SwitchCase.GenerateDefault();
scd.AddCodeFormat("LWIP_DEBUGF(SNMP_MIB_DEBUG,(\"{0}(): unknown id: %\"S32_F\"\\n\", {1}));", setMethod.Name, switchSelector);
sw.Switches.Add(scd);
if (!valueVarUsed)
{
setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[2].Name);
}
if (!lenVarUsed)
{
setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[1].Name);
}
setMethod.AddElement(sw);
setMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
}
}
}
|