private void GenerateAggregateMethodDeclarations(MibCFile mibFile) { if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1)) { GenerateAggregatedCode(mibFile, true, false); } }
private void GenerateAggregatedCode(MibCFile mibFile, bool generateDeclarations, bool generateImplementations) { VariableType instanceType = new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*"); base.GenerateAggregatedCode( mibFile, instanceType, String.Format("{0}->node->oid", instanceType.Name), generateDeclarations, generateImplementations); }
private void GenerateAggregatedCode(MibCFile mibFile, bool generateDeclarations, bool generateImplementations) { VariableType instanceType = new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*"); base.GenerateAggregatedCode( mibFile, instanceType, String.Format("{0}->node->oid", instanceType.Name), generateDeclarations, generateImplementations); }
public override void GenerateCode(MibCFile mibFile) { string nodeInitialization; if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1)) { GenerateAggregatedCode(mibFile, false, true); } // create and add node declaration if (this.childNodes.Count > 0) { StringBuilder subnodeArrayInitialization = new StringBuilder(); for (int i = 0; i < this.childNodes.Count; i++) { subnodeArrayInitialization.Append(" &"); subnodeArrayInitialization.Append(this.childNodes[i].FullNodeName); subnodeArrayInitialization.Append(".node"); if (!(this.childNodes[i] is SnmpTreeNode)) { subnodeArrayInitialization.Append(".node"); } if (i < (this.childNodes.Count - 1)) { subnodeArrayInitialization.Append(",\n"); } } VariableDeclaration subnodeArray = new VariableDeclaration( new VariableType(this.Name.ToLowerInvariant() + "_subnodes", LwipDefs.Vt_StNode, "*", ConstType.Both, String.Empty), "{\n" + subnodeArrayInitialization + "\n}", isStatic: true); mibFile.Declarations.Add(subnodeArray); nodeInitialization = String.Format("SNMP_CREATE_TREE_NODE({0}, {1})", this.Oid, subnodeArray.Type.Name); } else { nodeInitialization = String.Format("SNMP_CREATE_EMPTY_TREE_NODE({0})", this.Oid); } mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StTreeNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public override void GenerateCode(MibCFile mibFile) { VariableType instanceType = new VariableType("node", LwipDefs.Vt_StScalarArrayNodeDef, "*", ConstType.Value); GenerateAggregatedCode( mibFile, instanceType, instanceType.Name + "->oid"); // create and add node definitions StringBuilder nodeDefs = new StringBuilder(); foreach (SnmpScalarNode scalarNode in this.scalarNodes) { nodeDefs.AppendFormat(" {{{0}, {1}, {2}}}, /* {3} */ \n", scalarNode.Oid, LwipDefs.GetAsn1DefForSnmpDataType(scalarNode.DataType), LwipDefs.GetLwipDefForSnmpAccessMode(scalarNode.AccessMode), scalarNode.Name); } if (nodeDefs.Length > 0) { nodeDefs.Length--; } VariableDeclaration nodeDefsDecl = new VariableDeclaration( new VariableType(this.FullNodeName + "_nodes", LwipDefs.Vt_StScalarArrayNodeDef, null, ConstType.Value, String.Empty), "{\n" + nodeDefs + "\n}", isStatic: true); mibFile.Declarations.Add(nodeDefsDecl); // create and add node declaration string nodeInitialization = String.Format("SNMP_SCALAR_CREATE_ARRAY_NODE({0}, {1}, {2}, {3}, {4})", this.Oid, nodeDefsDecl.Type.Name, (this.GetMethodRequired) ? this.GetMethodName : LwipDefs.Null, (this.TestMethodRequired) ? this.TestMethodName : LwipDefs.Null, (this.SetMethodRequired) ? this.SetMethodName : LwipDefs.Null ); mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StScalarArrayNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public virtual void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile) { int declCount = generatedFile.Declarations.Count; int implCount = generatedFile.Implementation.Count; this.GenerateHeaderCode(generatedHeaderFile); this.GenerateCode(generatedFile); if (generatedFile.Declarations.Count != declCount) { generatedFile.Declarations.Add(EmptyLine.SingleLine); } if (generatedFile.Implementation.Count != implCount) { generatedFile.Implementation.Add(EmptyLine.SingleLine); } }
public virtual void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile) { int declCount = generatedFile.Declarations.Count; int implCount = generatedFile.Implementation.Count; this.GenerateHeaderCode(generatedHeaderFile); this.GenerateCode(generatedFile); if (generatedFile.Declarations.Count != declCount) { generatedFile.Declarations.Add(EmptyLine.SingleLine); } if (generatedFile.Implementation.Count != implCount) { generatedFile.Implementation.Add(EmptyLine.SingleLine); } }
public override void GenerateCode(MibCFile mibFile) { VariableType instanceType = new VariableType("node", LwipDefs.Vt_StScalarArrayNodeDef, "*", ConstType.Value); GenerateAggregatedCode( mibFile, instanceType, instanceType.Name + "->oid"); // create and add node definitions StringBuilder nodeDefs = new StringBuilder(); foreach (SnmpScalarNode scalarNode in this.scalarNodes) { nodeDefs.AppendFormat(" {{{0}, {1}, {2}}}, /* {3} */ \n", scalarNode.Oid, LwipDefs.GetAsn1DefForSnmpDataType(scalarNode.DataType), LwipDefs.GetLwipDefForSnmpAccessMode(scalarNode.AccessMode), scalarNode.Name); } if (nodeDefs.Length > 0) nodeDefs.Length--; VariableDeclaration nodeDefsDecl = new VariableDeclaration( new VariableType(this.FullNodeName + "_nodes", LwipDefs.Vt_StScalarArrayNodeDef, null, ConstType.Value, String.Empty), "{\n" + nodeDefs + "\n}" , isStatic: true); mibFile.Declarations.Add(nodeDefsDecl); // create and add node declaration string nodeInitialization = String.Format("SNMP_SCALAR_CREATE_ARRAY_NODE({0}, {1}, {2}, {3}, {4})", this.Oid, nodeDefsDecl.Type.Name, (this.GetMethodRequired) ? this.GetMethodName : LwipDefs.Null, (this.TestMethodRequired) ? this.TestMethodName : LwipDefs.Null, (this.SetMethodRequired) ? this.SetMethodName : LwipDefs.Null ); mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StScalarArrayNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public override void GenerateCode(MibCFile mibFile) { base.GenerateCode(mibFile); System.Diagnostics.Debug.Assert((this.BaseOid != null) && (this.BaseOid.Length > 0)); // create and add BaseOID declarations StringBuilder boidInitialization = new StringBuilder("{"); foreach (uint t in this.BaseOid) { boidInitialization.Append(t); boidInitialization.Append(","); } boidInitialization.Length -= 1; boidInitialization.Append("}"); VariableDeclaration boidDecl = new VariableDeclaration( new VariableType(this.Name + "_base_oid", LwipDefs.Vt_U32, null, ConstType.Value, String.Empty), boidInitialization.ToString(), true); mibFile.Declarations.Add(boidDecl); mibFile.Declarations.Add(GetExportDeclaration()); }
public override void GenerateCode(MibCFile mibFile) { base.GenerateCode(mibFile); System.Diagnostics.Debug.Assert((this.BaseOid != null) && (this.BaseOid.Length > 0)); // create and add BaseOID declarations StringBuilder boidInitialization = new StringBuilder("{"); foreach (uint t in this.BaseOid) { boidInitialization.Append(t); boidInitialization.Append(","); } boidInitialization.Length -= 1; boidInitialization.Append("}"); VariableDeclaration boidDecl = new VariableDeclaration( new VariableType(this.Name.ToLowerInvariant() + "_base_oid", LwipDefs.Vt_U32, null, ConstType.Value, String.Empty), boidInitialization.ToString(), true); mibFile.Declarations.Add(boidDecl); mibFile.Declarations.Add(GetExportDeclaration()); }
public override void GenerateCode(MibCFile mibFile) { string getMethodName; string testMethodName; string setMethodName; if (this.useExternalMethods) { getMethodName = this.externalGetMethod; testMethodName = this.externalTestMethod; setMethodName = this.externalSetMethod; } else { getMethodName = LwipDefs.Null; testMethodName = LwipDefs.Null; setMethodName = LwipDefs.Null; if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.ReadOnly)) { FunctionDeclaration getMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_GetValue, isStatic: true); getMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); getMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*")); getMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_S16); mibFile.Declarations.Add(getMethodDecl); Function getMethod = Function.FromDeclaration(getMethodDecl); getMethodName = getMethod.Name; VariableDeclaration returnValue = new VariableDeclaration((VariableType)getMethod.ReturnType.Clone()); returnValue.Type.Name = "value_len"; getMethod.Declarations.Add(returnValue); getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[0].Name); bool valueVarUsed = false; GenerateGetMethodCode(getMethod, getMethod.Parameter[1].Name, ref valueVarUsed, returnValue.Type.Name); if (!valueVarUsed) { getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[1].Name); } getMethod.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(getMethod); } if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.WriteOnly)) { bool valueVarUsed; bool lenVarUsed; VariableDeclaration returnValue; if (this.restrictions.Count > 0) { FunctionDeclaration testMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetTest, isStatic: true); testMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); 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); mibFile.Declarations.Add(testMethodDecl); Function testMethod = Function.FromDeclaration(testMethodDecl); testMethodName = testMethod.Name; returnValue = new VariableDeclaration((VariableType)testMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_WrongValue); returnValue.Type.Name = "err"; testMethod.Declarations.Add(returnValue); testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[0].Name); valueVarUsed = false; lenVarUsed = false; GenerateTestMethodCode(testMethod, testMethod.Parameter[2].Name, ref valueVarUsed, testMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name); 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.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(testMethod); } else { testMethodName = LwipDefs.FnctName_SetTest_Ok; } FunctionDeclaration setMethodDecl = null; setMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetValue, isStatic: true); setMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); 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); mibFile.Declarations.Add(setMethodDecl); Function setMethod = Function.FromDeclaration(setMethodDecl); setMethodName = setMethod.Name; returnValue = new VariableDeclaration((VariableType)setMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_Ok); returnValue.Type.Name = "err"; setMethod.Declarations.Add(returnValue); setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[0].Name); valueVarUsed = false; lenVarUsed = false; GenerateSetMethodCode(setMethod, setMethod.Parameter[2].Name, ref valueVarUsed, setMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name); 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.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(setMethod); } } // create and add node declaration string nodeInitialization; if (this.accessMode == SnmpAccessMode.ReadOnly) { nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE_READONLY({0}, {1}, {2})", this.Oid, LwipDefs.GetAsn1DefForSnmpDataType(this.dataType), getMethodName); } else { nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE({0}, {1}, {2}, {3}, {4}, {5})", this.Oid, LwipDefs.GetLwipDefForSnmpAccessMode(this.accessMode), LwipDefs.GetAsn1DefForSnmpDataType(this.dataType), getMethodName, testMethodName, setMethodName); } mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StScalarNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public override void Generate(MibCFile generatedFile, MibHeaderFile generatedHeaderFile) { // generate code of child nodes foreach (SnmpNode childNode in this.childNodes) { if (childNode is SnmpTreeNode) { childNode.Generate(generatedFile, generatedHeaderFile); } } Comment dividerComment = new Comment( String.Format("--- {0} {1} -----------------------------------------------------", this.Name, this.fullOid), singleLine: true); generatedFile.Declarations.Add(dividerComment); generatedFile.Implementation.Add(dividerComment); this.GenerateAggregateMethodDeclarations(generatedFile); foreach (SnmpNode childNode in this.childNodes) { if (!(childNode is SnmpTreeNode)) { childNode.Generate(generatedFile, generatedHeaderFile); } } base.Generate(generatedFile, generatedHeaderFile); }
public override void GenerateCode(MibCFile mibFile) { string nodeInitialization; if (LwipOpts.GenerateSingleAccessMethodsForTreeNodeScalars && (this.childScalarNodes.Count > 1)) { GenerateAggregatedCode(mibFile, false, true); } // create and add node declaration if (this.childNodes.Count > 0) { StringBuilder subnodeArrayInitialization = new StringBuilder(); for (int i=0; i<this.childNodes.Count; i++) { subnodeArrayInitialization.Append(" &"); subnodeArrayInitialization.Append(this.childNodes[i].FullNodeName); subnodeArrayInitialization.Append(".node"); if (!(this.childNodes[i] is SnmpTreeNode)) { subnodeArrayInitialization.Append(".node"); } if (i < (this.childNodes.Count - 1)) { subnodeArrayInitialization.Append(",\n"); } } VariableDeclaration subnodeArray = new VariableDeclaration( new VariableType(this.Name + "_subnodes", LwipDefs.Vt_StNode, "*", ConstType.Value, String.Empty), "{\n" + subnodeArrayInitialization + "\n}", isStatic: true); mibFile.Declarations.Add(subnodeArray); nodeInitialization = String.Format("SNMP_CREATE_TREE_NODE({0}, {1})", this.Oid, subnodeArray.Type.Name); } else { nodeInitialization = String.Format("SNMP_CREATE_EMPTY_TREE_NODE({0})", this.Oid); } mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StTreeNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public abstract void GenerateCode(MibCFile mibFile);
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_U16); 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); } } }
public override void GenerateCode(MibCFile mibFile) { FunctionDeclaration getInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetInstance, isStatic: true); getInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value)); getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_U32, "*", ConstType.Value)); getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid_len", LwipDefs.Vt_U8, "")); getInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*")); getInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err); mibFile.Declarations.Add(getInstanceMethodDecl); Function getInstanceMethod = Function.FromDeclaration(getInstanceMethodDecl); GenerateGetInstanceMethodCode(getInstanceMethod); mibFile.Implementation.Add(getInstanceMethod); FunctionDeclaration getNextInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetNextInstance, isStatic: true); getNextInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value)); getNextInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_StObjectId, "*")); getNextInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*")); getNextInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err); mibFile.Declarations.Add(getNextInstanceMethodDecl); Function getNextInstanceMethod = Function.FromDeclaration(getNextInstanceMethodDecl); GenerateGetNextInstanceMethodCode(getNextInstanceMethod); mibFile.Implementation.Add(getNextInstanceMethod); VariableType instanceType = new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*"); GenerateAggregatedCode( mibFile, instanceType, String.Format("SNMP_TABLE_GET_COLUMN_FROM_OID({0}->instance_oid.id)", instanceType.Name)); #region create and add column/table definitions StringBuilder colDefs = new StringBuilder(); foreach (SnmpScalarNode colNode in this.cellNodes) { colDefs.AppendFormat(" {{{0}, {1}, {2}}}, /* {3} */ \n", colNode.Oid, LwipDefs.GetAsn1DefForSnmpDataType(colNode.DataType), LwipDefs.GetLwipDefForSnmpAccessMode(colNode.AccessMode), colNode.Name); } if (colDefs.Length > 0) { colDefs.Length--; } VariableDeclaration colDefsDecl = new VariableDeclaration( new VariableType(this.FullNodeName + "_columns", LwipDefs.Vt_StTableColumnDef, null, ConstType.Value, String.Empty), "{\n" + colDefs + "\n}", isStatic: true); mibFile.Declarations.Add(colDefsDecl); string nodeInitialization = String.Format("SNMP_TABLE_CREATE({0}, {1}, {2}, {3}, {4}, {5}, {6})", this.Oid, colDefsDecl.Type.Name, getInstanceMethodDecl.Name, getNextInstanceMethodDecl.Name, (this.GetMethodRequired) ? this.GetMethodName : LwipDefs.Null, (this.TestMethodRequired) ? this.TestMethodName : LwipDefs.Null, (this.SetMethodRequired) ? this.SetMethodName : LwipDefs.Null ); mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StTableNode, null, ConstType.Value), nodeInitialization, isStatic: true)); #endregion }
public override void GenerateCode(MibCFile mibFile) { FunctionDeclaration getInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetInstance, isStatic: true); getInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value)); getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_U32, "*", ConstType.Value)); getInstanceMethodDecl.Parameter.Add(new VariableType("row_oid_len", LwipDefs.Vt_U8, "")); getInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*")); getInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err); mibFile.Declarations.Add(getInstanceMethodDecl); Function getInstanceMethod = Function.FromDeclaration(getInstanceMethodDecl); GenerateGetInstanceMethodCode(getInstanceMethod); mibFile.Implementation.Add(getInstanceMethod); FunctionDeclaration getNextInstanceMethodDecl = new FunctionDeclaration(this.FullNodeName + LwipDefs.FnctSuffix_GetNextInstance, isStatic: true); getNextInstanceMethodDecl.Parameter.Add(new VariableType("column", LwipDefs.Vt_U32, "*", ConstType.Value)); getNextInstanceMethodDecl.Parameter.Add(new VariableType("row_oid", LwipDefs.Vt_StObjectId, "*")); getNextInstanceMethodDecl.Parameter.Add(new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*")); getNextInstanceMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_Snmp_err); mibFile.Declarations.Add(getNextInstanceMethodDecl); Function getNextInstanceMethod = Function.FromDeclaration(getNextInstanceMethodDecl); GenerateGetNextInstanceMethodCode(getNextInstanceMethod); mibFile.Implementation.Add(getNextInstanceMethod); VariableType instanceType = new VariableType("cell_instance", LwipDefs.Vt_StNodeInstance, "*"); GenerateAggregatedCode( mibFile, instanceType, String.Format("SNMP_TABLE_GET_COLUMN_FROM_OID({0}->instance_oid.id)", instanceType.Name)); #region create and add column/table definitions StringBuilder colDefs = new StringBuilder(); foreach (SnmpScalarNode colNode in this.cellNodes) { colDefs.AppendFormat(" {{{0}, {1}, {2}}}, /* {3} */ \n", colNode.Oid, LwipDefs.GetAsn1DefForSnmpDataType(colNode.DataType), LwipDefs.GetLwipDefForSnmpAccessMode(colNode.AccessMode), colNode.Name); } if (colDefs.Length > 0) { colDefs.Length--; } VariableDeclaration colDefsDecl = new VariableDeclaration( new VariableType(this.FullNodeName + "_columns", LwipDefs.Vt_StTableColumnDef, null, ConstType.Value, String.Empty), "{\n" + colDefs + "\n}", isStatic: true); mibFile.Declarations.Add(colDefsDecl); string nodeInitialization = String.Format("SNMP_TABLE_CREATE({0}, {1}, {2}, {3}, {4}, {5}, {6})", this.Oid, colDefsDecl.Type.Name, getInstanceMethodDecl.Name, getNextInstanceMethodDecl.Name, (this.GetMethodRequired) ? this.GetMethodName : LwipDefs.Null, (this.TestMethodRequired) ? this.TestMethodName : LwipDefs.Null, (this.SetMethodRequired) ? this.SetMethodName : LwipDefs.Null ); mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StTableNode, null, ConstType.Value), nodeInitialization, isStatic: true)); #endregion }
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); } } }
static void Main(string[] args) { Console.WriteLine("lwIP MIB Compiler"); Console.WriteLine(""); // check args if ((args.Length < 2) || String.IsNullOrWhiteSpace(args[0]) || String.IsNullOrWhiteSpace(args[1])) { PrintUsage(); return; } string mibFile = args[0]; if (!File.Exists(mibFile)) { Console.WriteLine(String.Format("Unable to find file '{0}'!", mibFile)); } string destFile = args[1]; string destHeaderFile; if (Directory.Exists(destFile)) { // only directory passed -> create dest filename from mib filename string mibFileName = Path.GetFileNameWithoutExtension(mibFile).ToLowerInvariant(); destFile = Path.Combine(destFile, mibFileName + ".c"); } string destFileExt = Path.GetExtension(destFile); if (!String.IsNullOrEmpty(destFileExt)) { destHeaderFile = destFile.Substring(0, destFile.Length - destFileExt.Length); } else { destHeaderFile = destFile; } destHeaderFile += ".h"; for (int i=2; i<args.Length; i++) { if (!String.IsNullOrWhiteSpace(args[i]) && Directory.Exists(args[i])) { MibTypesResolver.RegisterResolver(new FileSystemMibResolver(args[i], true)); } } // read and resolve MIB Console.WriteLine(" Reading MIB file..."); MibDocument md = new MibDocument(mibFile); MibTypesResolver.ResolveTypes(md.Modules[0]); MibTree mt = new MibTree(md.Modules[0] as MibModule); if (mt.Root.Count == 0) { Console.WriteLine("No root element found inside MIB!"); return; } MibCFile generatedFile = new MibCFile(); MibHeaderFile generatedHeaderFile = new MibHeaderFile(); foreach (MibTreeNode mibTreeNode in mt.Root) { // create LWIP object tree from MIB structure Console.WriteLine(" Creating lwIP object tree " + mibTreeNode.Entity.Name); SnmpMib snmpMib = new SnmpMib(); snmpMib.Oid = mibTreeNode.Entity.Value; snmpMib.BaseOid = MibTypesResolver.ResolveOid(mibTreeNode.Entity).GetOidValues(); snmpMib.Name = mibTreeNode.Entity.Name; ProcessMibTreeNode(mibTreeNode, snmpMib); // let the tree transform itself depending on node structure snmpMib.Analyze(); // generate code from LWIP object tree Console.WriteLine(" Generating code " + snmpMib.Name); snmpMib.Generate(generatedFile, generatedHeaderFile); } string preservedCode = MibCFile.GetPreservedCode(destFile); if (!string.IsNullOrEmpty(preservedCode)) { generatedFile.PreservedCode.Add(new PlainText(preservedCode)); } else { generatedFile.PreservedCode.AddRange(generatedFile.Implementation); } generatedFile.Implementation.Clear(); using (StreamWriter fileWriter = new StreamWriter(destHeaderFile)) { CGenerator cGenerator = new CGenerator(fileWriter, destHeaderFile, 3, " ", Environment.NewLine); generatedHeaderFile.Save(cGenerator); } using (StreamWriter fileWriter = new StreamWriter(destFile)) { CGenerator cGenerator = new CGenerator(fileWriter, destFile, 3, " ", Environment.NewLine); generatedFile.Save(cGenerator); } Console.WriteLine(" Done"); }
public override void GenerateCode(MibCFile mibFile) { string getMethodName; string testMethodName; string setMethodName; if (this.useExternalMethods) { getMethodName = this.externalGetMethod; testMethodName = this.externalTestMethod; setMethodName = this.externalSetMethod; } else { getMethodName = LwipDefs.Null; testMethodName = LwipDefs.Null; setMethodName = LwipDefs.Null; if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.ReadOnly)) { FunctionDeclaration getMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_GetValue, isStatic: true); getMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); getMethodDecl.Parameter.Add(new VariableType("value", VariableType.VoidString, "*")); getMethodDecl.ReturnType = new VariableType(null, LwipDefs.Vt_U16); mibFile.Declarations.Add(getMethodDecl); Function getMethod = Function.FromDeclaration(getMethodDecl); getMethodName = getMethod.Name; VariableDeclaration returnValue = new VariableDeclaration((VariableType)getMethod.ReturnType.Clone()); returnValue.Type.Name = "value_len"; getMethod.Declarations.Add(returnValue); getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[0].Name); bool valueVarUsed = false; GenerateGetMethodCode(getMethod, getMethod.Parameter[1].Name, ref valueVarUsed, returnValue.Type.Name); if (!valueVarUsed) { getMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", getMethod.Parameter[1].Name); } getMethod.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(getMethod); } if ((this.accessMode == SnmpAccessMode.ReadWrite) || (this.accessMode == SnmpAccessMode.WriteOnly)) { bool valueVarUsed; bool lenVarUsed; VariableDeclaration returnValue; if (this.restrictions.Count > 0) { FunctionDeclaration testMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetTest, isStatic: true); testMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); 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); mibFile.Declarations.Add(testMethodDecl); Function testMethod = Function.FromDeclaration(testMethodDecl); testMethodName = testMethod.Name; returnValue = new VariableDeclaration((VariableType)testMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_WrongValue); returnValue.Type.Name = "err"; testMethod.Declarations.Add(returnValue); testMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", testMethod.Parameter[0].Name); valueVarUsed = false; lenVarUsed = false; GenerateTestMethodCode(testMethod, testMethod.Parameter[2].Name, ref valueVarUsed, testMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name); 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.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(testMethod); } else { testMethodName = LwipDefs.FnctName_SetTest_Ok; } FunctionDeclaration setMethodDecl = null; setMethodDecl = new FunctionDeclaration(this.Name + LwipDefs.FnctSuffix_SetValue, isStatic: true); setMethodDecl.Parameter.Add(new VariableType("instance", LwipDefs.Vt_StNodeInstance, "*")); 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); mibFile.Declarations.Add(setMethodDecl); Function setMethod = Function.FromDeclaration(setMethodDecl); setMethodName = setMethod.Name; returnValue = new VariableDeclaration((VariableType)setMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_Ok); returnValue.Type.Name = "err"; setMethod.Declarations.Add(returnValue); setMethod.AddCodeFormat("LWIP_UNUSED_ARG({0});", setMethod.Parameter[0].Name); valueVarUsed = false; lenVarUsed = false; GenerateSetMethodCode(setMethod, setMethod.Parameter[2].Name, ref valueVarUsed, setMethod.Parameter[1].Name, ref lenVarUsed, returnValue.Type.Name); 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.AddCodeFormat("return {0};", returnValue.Type.Name); mibFile.Implementation.Add(setMethod); } } // create and add node declaration string nodeInitialization; if (this.accessMode == SnmpAccessMode.ReadOnly) { nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE_READONLY({0}, {1}, {2})", this.Oid, LwipDefs.GetAsn1DefForSnmpDataType(this.dataType), getMethodName); } else { nodeInitialization = String.Format("SNMP_SCALAR_CREATE_NODE({0}, {1}, {2}, {3}, {4}, {5})", this.Oid, LwipDefs.GetLwipDefForSnmpAccessMode(this.accessMode), LwipDefs.GetAsn1DefForSnmpDataType(this.dataType), getMethodName, testMethodName, setMethodName); } mibFile.Declarations.Add(new VariableDeclaration( new VariableType(this.FullNodeName, LwipDefs.Vt_StScalarNode, null, ConstType.Value), nodeInitialization, isStatic: true)); }
public abstract void GenerateCode(MibCFile mibFile);