Exemplo n.º 1
0
        public virtual void GenerateTestMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            if (this.Restrictions.Count > 0)
            {
                bool localVarUsed;
                if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
                {
                    valueVarUsed = true;
                    localVarUsed = false;
                }
                else
                {
                    localVarUsed = true;                      // do not generate UNUSED_ARG code
                }

                if (!String.IsNullOrWhiteSpace(this.FixedValueLength))
                {
                    // check for fixed value
                    container.AddCodeFormat("LWIP_ASSERT(\"Invalid length for datatype\", ({0} == {1}));", lenVarName, this.FixedValueLength);
                    lenVarUsed = true;
                }

                GenerateTestMethodCodeCore(container, LocalValueName, ref localVarUsed, lenVarName, ref lenVarUsed, retErrVarName);

                if (!localVarUsed)
                {
                    container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
                }
            }
            else
            {
                container.AddCodeFormat("{0} == {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok);
            }
        }
Exemplo n.º 2
0
        public virtual void GenerateGetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string retLenVarName)
        {
            bool localValueVarUsed;

            if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
            {
                valueVarUsed      = true;
                localValueVarUsed = false;
            }
            else
            {
                localValueVarUsed = true;                  // do not generate UNUSED_ARG code
            }

            if (this.FixedValueLength == null)
            {
                // check that value with variable length fits into buffer
                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));
            }

            GenerateGetMethodCodeCore(container, LocalValueName, ref localValueVarUsed, retLenVarName);
            if (!localValueVarUsed)
            {
                container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
            }
        }
Exemplo n.º 3
0
 protected virtual void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
 {
     container.AddElement(new Comment(String.Format("TODO: put requested value to '*{0}' here", localValueVarName), singleLine: true));
     container.AddCodeFormat("{0} = {1};",
                             retLenVarName,
                             (!String.IsNullOrWhiteSpace(this.FixedValueLength)) ? this.FixedValueLength : "0");
 }
        protected override bool GenerateValueDeclaration(CodeContainerBase container, string variableName, string sourceName)
        {
            container.AddDeclaration(new VariableDeclaration(
                                         new VariableType(variableName, LwipDefs.Vt_S32, "*"),
                                         "(" + new VariableType(null, LwipDefs.Vt_S32, "*") + ")" + sourceName));

            return(true);
        }
        protected override void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
        {
            container.AddCodeFormat("snmp_encode_truthvalue({0}, /* TODO: put requested bool value here */ 0);", localValueVarName);
            localValueVarUsed = true;

            container.AddCode(String.Format("{0} = {1};",
                                            retLenVarName,
                                            (!String.IsNullOrWhiteSpace(this.FixedValueLength)) ? this.FixedValueLength : "0"));
        }
        protected override void GenerateSetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            VariableType truthVar = new VariableType("bool_value", LwipDefs.Vt_U8);

            container.Declarations.Add(new VariableDeclaration(truthVar));

            container.AddCodeFormat("snmp_decode_truthvalue({0}, &{1});", localValueVarName, truthVar.Name);
            localValueVarUsed = true;

            container.AddElement(new Comment(String.Format("TODO: store new value contained in '{0}' here", truthVar.Name), singleLine: true));
        }
Exemplo n.º 7
0
        public override void GenerateGetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string retLenVarName)
        {
            container.AddCode(String.Format(
                                  "{0} = snmp_encode_bits(({1} *){2}, SNMP_MAX_VALUE_SIZE, 0 /* TODO: pass real value here */, {3});",
                                  retLenVarName,
                                  LwipDefs.Vt_U8,
                                  valueVarName,
                                  this.bitCount));

            valueVarUsed = true;
        }
        protected override void GenerateSetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            VariableDeclaration objIdLenVar = new VariableDeclaration(
                new VariableType(localValueVarName + "_len", LwipDefs.Vt_U8),
                String.Format("{0} / sizeof({1})", lenVarName, LwipDefs.Vt_U32));

            lenVarUsed = true;

            container.Declarations.Add(objIdLenVar);

            base.GenerateSetMethodCodeCore(container, localValueVarName, ref localValueVarUsed, lenVarName, ref lenVarUsed, retErrVarName);

            container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", objIdLenVar.Type.Name));
        }
Exemplo n.º 9
0
        protected override bool GenerateValueDeclaration(CodeContainerBase container, string variableName, string sourceName)
        {
            container.AddDeclaration(new VariableDeclaration(
                                         new VariableType(variableName + "_high", LwipDefs.Vt_U32, "*"),
                                         "(" + new VariableType(null, LwipDefs.Vt_U32, "*").ToString() + ")" + sourceName));
            container.AddDeclaration(new VariableDeclaration(
                                         new VariableType(variableName + "_low", LwipDefs.Vt_U32, "*"),
                                         variableName + "_high + 1"));

            container.AddCode(String.Format("LWIP_UNUSED_ARG({0}_high);", variableName));
            container.AddCode(String.Format("LWIP_UNUSED_ARG({0}_low);", variableName));

            return(false);
        }
        public void Save(CGenerator cGenerator)
        {
            CFile cFile = new CFile();

            cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
            cFile.AddElement(EmptyLine.SingleLine);

            string headerDefine = cGenerator.FileName;

            headerDefine = new Regex("[^a-zA-Z0-9]").Replace(headerDefine, "_");
            headerDefine = headerDefine.ToUpperInvariant();

            CodeContainerBase e = cFile.AddElement(new PP_Ifdef(headerDefine, inverted: true)) as CodeContainerBase;

            e.AddElement(new PP_Macro(headerDefine, headerDefine));
            e.AddElement(EmptyLine.SingleLine);

            e.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
            e = e.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;
            e.AddElement(EmptyLine.SingleLine);

            CodeContainerBase cplusplusopen = e.AddElement(new PP_Ifdef("__cplusplus")) as CodeContainerBase;

            cplusplusopen.AddElement(new Code("extern \"C\" {"));
            e.AddElement(EmptyLine.SingleLine);

            if (this.includes.Count > 0)
            {
                e.AddElements(this.includes);
                e.AddElement(EmptyLine.SingleLine);
            }

            if (this.defines.Count > 0)
            {
                e.AddElements(this.defines);
                e.AddElement(EmptyLine.SingleLine);
            }

            e.AddElements(this.functionDeclarations, EmptyLine.SingleLine);
            e.AddElements(this.variableDeclarations, EmptyLine.SingleLine);

            e.AddElement(EmptyLine.SingleLine);
            CodeContainerBase cplusplusclose = e.AddElement(new PP_Ifdef("__cplusplus")) as CodeContainerBase;

            cplusplusclose.AddElement(new Code("}"));

            e.AddElement(EmptyLine.SingleLine);
            cFile.Save(cGenerator);
        }
Exemplo n.º 11
0
        protected override void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
        {
            if (this.Restrictions.Count > 0)
            {
                StringBuilder ifCond = new StringBuilder();
                foreach (IRestriction restriction in this.Restrictions)
                {
                    ifCond.Append(restriction.GetCheckCodeValid(retLenVarName));
                    ifCond.Append(" || ");
                }

                ifCond.Length -= 4;
                container.AddElement(new Comment("TODO: take care of len restrictions defined in MIB: " + ifCond, singleLine: true));
            }
            base.GenerateGetMethodCodeCore(container, localValueVarName, ref localValueVarUsed, retLenVarName);
        }
Exemplo n.º 12
0
        public override void GenerateSetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            const string bitVarName = "bits";

            container.Declarations.Add(new VariableDeclaration(new VariableType(bitVarName, LwipDefs.Vt_U32)));

            IfThenElse ite = new IfThenElse(String.Format(
                                                "snmp_decode_bits(({0} *){1}, {2}, &{3}) == ERR_OK",
                                                LwipDefs.Vt_U8,
                                                valueVarName,
                                                lenVarName,
                                                bitVarName));

            valueVarUsed = true;
            lenVarUsed   = true;

            ite.AddElement(new Comment(String.Format("TODO: store new value contained in '{0}' here", bitVarName), singleLine: true));

            container.AddElement(ite);
        }
        protected override void GenerateTestMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            System.Diagnostics.Trace.Assert(this.Restrictions.Count > 0);

            StringBuilder ifCond = new StringBuilder();

            foreach (IRestriction restriction in this.Restrictions)
            {
                ifCond.Append(restriction.GetCheckCodeValid("*" + localValueVarName));
                ifCond.Append(" || ");

                localValueVarUsed = true;
            }

            ifCond.Length -= 4;

            IfThenElse ite = new IfThenElse(ifCond.ToString());

            ite.AddCode(String.Format("{0} = {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok));
            container.AddElement(ite);
        }
Exemplo n.º 14
0
        public virtual void GenerateSetMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            bool localVarUsed;

            if (GenerateValueDeclaration(container, LocalValueName, valueVarName))
            {
                valueVarUsed = true;
                localVarUsed = false;
            }
            else
            {
                localVarUsed = true;                 // do not generate UNUSED_ARG code
            }

            GenerateSetMethodCodeCore(container, LocalValueName, ref localVarUsed, lenVarName, ref lenVarUsed, retErrVarName);

            if (!localVarUsed)
            {
                container.AddCode(String.Format("LWIP_UNUSED_ARG({0});", LocalValueName));
            }
        }
Exemplo n.º 15
0
        public override void GenerateTestMethodCode(CodeContainerBase container, string valueVarName, ref bool valueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
        {
            if (this.Restrictions.Count > 0)
            {
                const string bitVarName = "bits";

                container.Declarations.Add(new VariableDeclaration(new VariableType(bitVarName, LwipDefs.Vt_U32)));

                IfThenElse ite = new IfThenElse(String.Format(
                                                    "snmp_decode_bits(({0} *){1}, {2}, &{3}) == ERR_OK",
                                                    LwipDefs.Vt_U8,
                                                    valueVarName,
                                                    lenVarName,
                                                    bitVarName));

                valueVarUsed = true;
                lenVarUsed   = true;

                StringBuilder innerIfCond = new StringBuilder();
                foreach (IRestriction restriction in this.Restrictions)
                {
                    innerIfCond.Append(restriction.GetCheckCodeValid(bitVarName));
                    innerIfCond.Append(" || ");
                }

                innerIfCond.Length -= 4;

                IfThenElse innerIte = new IfThenElse(innerIfCond.ToString());
                innerIte.AddCode(String.Format("{0} = {1};", retErrVarName, LwipDefs.Def_ErrorCode_Ok));
                ite.AddElement(innerIte);
                container.AddElement(ite);
            }
            else
            {
                base.GenerateTestMethodCode(container, valueVarName, ref valueVarUsed, lenVarName, ref lenVarUsed, retErrVarName);
            }
        }
Exemplo n.º 16
0
        protected virtual void GenerateGetInstanceMethodCode(Function getInstanceMethod)
        {
            VariableDeclaration returnValue = new VariableDeclaration((VariableType)getInstanceMethod.ReturnType.Clone(), LwipDefs.Def_ErrorCode_NoSuchInstance);

            returnValue.Type.Name = "err";
            getInstanceMethod.Declarations.Add(returnValue);

            int           instanceOidLength = 0;
            StringBuilder indexColumns      = new StringBuilder();

            foreach (SnmpScalarNode indexNode in this.indexNodes)
            {
                if (instanceOidLength >= 0)
                {
                    if (indexNode.OidRepresentationLen >= 0)
                    {
                        instanceOidLength += indexNode.OidRepresentationLen;
                    }
                    else
                    {
                        // at least one index column has a variable length -> we cannot perform a static check
                        instanceOidLength = -1;
                    }
                }

                indexColumns.AppendFormat(
                    " {0} ({1}, OID length = {2})\n",
                    indexNode.Name,
                    indexNode.DataType,
                    (indexNode.OidRepresentationLen >= 0) ? indexNode.OidRepresentationLen.ToString() : "variable");
            }
            if (indexColumns.Length > 0)
            {
                indexColumns.Length--;

                getInstanceMethod.Declarations.Insert(0, new Comment(String.Format(
                                                                         "The instance OID of this table consists of following (index) column(s):\n{0}",
                                                                         indexColumns)));
            }

            string augmentsHint = "";

            if (!String.IsNullOrWhiteSpace(this.augmentedTableRow))
            {
                augmentsHint = String.Format(
                    "This table augments table '{0}'! Index columns therefore belong to table '{0}'!\n" +
                    "You may simply call the '*{1}' method of this table.\n\n",
                    (this.augmentedTableRow.ToLowerInvariant().EndsWith("entry")) ? this.augmentedTableRow.Substring(0, this.augmentedTableRow.Length - 5) : this.augmentedTableRow,
                    LwipDefs.FnctSuffix_GetInstance);
            }

            CodeContainerBase ccb = getInstanceMethod;

            if (instanceOidLength > 0)
            {
                IfThenElse ite = new IfThenElse(String.Format("{0} == {1}", getInstanceMethod.Parameter[2].Name, instanceOidLength));
                getInstanceMethod.AddElement(ite);
                ccb = ite;
            }

            ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[0].Name);
            ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[1].Name);
            if (instanceOidLength <= 0)
            {
                ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[2].Name);
            }
            ccb.AddCodeFormat("LWIP_UNUSED_ARG({0});", getInstanceMethod.Parameter[3].Name);

            ccb.AddElement(new Comment(String.Format(
                                           "TODO: check if '{0}'/'{1}' params contain a valid instance oid for a row\n" +
                                           "If so, set '{2} = {3};'\n\n" +
                                           "snmp_oid_* methods may be used for easier processing of oid\n\n" +
                                           "{4}" +
                                           "In order to avoid decoding OID a second time in subsequent get_value/set_test/set_value methods,\n" +
                                           "you may store an arbitrary value (like a pointer to target value object) in '{5}->reference'/'{5}->reference_len'.\n" +
                                           "But be aware that not always a subsequent method is called -> Do NOT allocate memory here and try to release it in subsequent methods!\n\n" +
                                           "You also may replace function pointers in '{5}' param for get/test/set methods which contain the default values from table definition,\n" +
                                           "in order to provide special methods, for the currently processed cell. Changed pointers are only valid for current request.",
                                           getInstanceMethod.Parameter[1].Name,
                                           getInstanceMethod.Parameter[2].Name,
                                           returnValue.Type.Name,
                                           LwipDefs.Def_ErrorCode_Ok,
                                           augmentsHint,
                                           getInstanceMethod.Parameter[3].Name
                                           )));

            getInstanceMethod.AddCodeFormat("return {0};", returnValue.Type.Name);
        }
 protected override void GenerateGetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string retLenVarName)
 {
     container.AddElement(new Comment(String.Format("TODO: put requested value to '*{0}' here. '{0}' has to be interpreted as {1}[]", localValueVarName, LwipDefs.Vt_U32), singleLine: true));
     container.AddElement(EmptyLine.SingleLine);
     container.AddCode(String.Format("{0} = 0; // TODO: return real value length here (should be 'numOfElements * sizeof({1})')", retLenVarName, LwipDefs.Vt_U32));
 }
Exemplo n.º 18
0
 protected virtual void GenerateSetMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
 {
     container.AddElement(new Comment(String.Format("TODO: store new value contained in '*{0}' here", localValueVarName), singleLine: true));
 }
Exemplo n.º 19
0
 protected virtual void GenerateTestMethodCodeCore(CodeContainerBase container, string localValueVarName, ref bool localValueVarUsed, string lenVarName, ref bool lenVarUsed, string retErrVarName)
 {
     container.AddElement(new Comment(String.Format("TODO: test new value here:\nif (*{0} == ) {1} = {2};", localValueVarName, retErrVarName, LwipDefs.Def_ErrorCode_Ok)));
 }
Exemplo n.º 20
0
        public void Save(CGenerator cGenerator)
        {
            CFile cFile = new CFile();

            cFile.AddElement(new Comment("Generated by LwipMibCompiler"));
            cFile.AddElement(EmptyLine.SingleLine);

            cFile.AddElement(new PP_Include(LwipDefs.Incl_SnmpOpts));
            CodeContainerBase e = cFile.AddElement(new PP_If(LwipDefs.Opt_SnmpEnabled)) as CodeContainerBase;

            e.AddElement(EmptyLine.SingleLine);

            // include header file
            string file = cGenerator.FileName;

            if (!String.IsNullOrWhiteSpace(file))
            {
                string ext = System.IO.Path.GetExtension(file);

                string headerFile = !String.IsNullOrEmpty(ext) ? file.Substring(0, file.Length - ext.Length) : file;
                headerFile += ".h";

                e.AddElement(new PP_Include(headerFile));
            }

            // include common snmp files
            e.AddElement(new PP_Include("lwip/apps/snmp.h"));
            e.AddElement(new PP_Include("lwip/apps/snmp_core.h"));
            e.AddElement(new PP_Include("lwip/apps/snmp_scalar.h"));
            e.AddElement(new PP_Include("lwip/apps/snmp_table.h"));

            if (this.includes.Count > 0)
            {
                e.AddElement(EmptyLine.SingleLine);
                e.AddElements(this.includes);
            }

            if (this.defines.Count > 0)
            {
                e.AddElement(EmptyLine.SingleLine);
                e.AddElements(this.defines);
            }

            if (this.declarations.Count > 0)
            {
                e.AddElement(EmptyLine.TwoLines);
                e.AddElements(this.declarations);
            }

            if (this.implementation.Count > 0)
            {
                e.AddElement(EmptyLine.TwoLines);
                e.AddElements(this.implementation);
            }

            if (this.preservedCode.Count > 0)
            {
                e.AddElement(EmptyLine.TwoLines);
                e.AddElement(new Comment(PreservedSectionHeader));
                e.AddElement(EmptyLine.SingleLine);
                e.AddElements(this.preservedCode);
            }

            cFile.Save(cGenerator);
        }