Beispiel #1
0
        private void WriteTemplate_EnumsHeader(string namespacePrefix, string dictionaryName)
        {
            // get datatypes.
            List <DataType> datatypes = GetDataTypeList(null, m_exportAll, false);

            if (datatypes.Count == 0)
            {
                return;
            }

            // sort types to ensure types are declared in the correct order.
            List <DataType> sortedTypes = new List <DataType>();

            foreach (DataType datatype in datatypes)
            {
                AddDataType(datatype, sortedTypes);
            }

            datatypes = sortedTypes;

            string fileName = String.Format(@"{0}\{1}_enumparser.h", OutputDirectory, namespacePrefix);

            fileName = fileName.ToLower();

            StreamWriter writer = new StreamWriter(fileName, false);

            writer.Write(string.Format(m_sHeader, "OpcUa Enum Type Parser", DateTime.Now));
            writer.Write("#ifdef HAVE_CONFIG_H\n" +
                         "# include \"config.h\"\n" +
                         "#endif\n" +
                         "\n" +
                         "#include <gmodule.h>\n" +
                         "#include <epan/packet.h>\n\n");
            try
            {
                foreach (DataType datatype in sortedTypes)
                {
                    EnumeratedType enumtype = datatype as EnumeratedType;

                    if (enumtype != null)
                    {
                        writer.Write("void parse{0}(proto_tree *tree, tvbuff_t *tvb, gint *pOffset);\n", enumtype.Name);
                    }
                }

                writer.Write("void registerEnumTypes(int proto);\n\n");
            }
            finally
            {
                writer.Close();
            }
        }
        /// <summary>
        /// Chooses the template for the data type.
        /// </summary>
        private string LoadTemplate_EnumeratedTypeValues(Template template, Context context)
        {
            EnumeratedType enumeratedType = context.Target as EnumeratedType;

            if (enumeratedType == null)
            {
                return(null);
            }

            foreach (EnumeratedValue value in enumeratedType.Value)
            {
                template.WriteLine(String.Empty);
                template.Write(context.Prefix);
                template.Write("<opc:Field Name=\"{0}\" Identifier=\"{1}\" />", value.Name, value.Value);
            }

            return(null);
        }
        /// <summary>
        /// Writes the
        /// </summary>
        private bool WriteTemplate_DataType(Template template, Context context)
        {
            DataType datatype = context.Target as DataType;

            if (datatype == null)
            {
                return(false);
            }

            template.AddReplacement("_TypeName_", datatype.QName.Name);
            CreateDescription(template, "_Description_", datatype.Documentation);

            ComplexType complexType = datatype as ComplexType;

            if (complexType != null)
            {
                List <FieldType> fields = new List <FieldType>();
                GetFields(complexType, fields);

                AddTemplate(
                    template,
                    "<!-- ListOfFields -->",
                    TemplatePath + "Field.xml",
                    fields,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            EnumeratedType enumeratedType = datatype as EnumeratedType;

            if (enumeratedType != null)
            {
                uint lengthInBits             = 32;
                bool isOptionSet              = false;
                List <EnumeratedValue> values = new List <EnumeratedValue>(enumeratedType.Value);

                if (enumeratedType.IsOptionSet)
                {
                    isOptionSet = true;

                    var baseType = Validator.ResolveType(enumeratedType.BaseType);

                    if (baseType != null)
                    {
                        switch (baseType.Name)
                        {
                        case "SByte": { lengthInBits = 8; break; }

                        case "Byte": { lengthInBits = 8; break; }

                        case "Int16": { lengthInBits = 16; break; }

                        case "UInt16": { lengthInBits = 16; break; }

                        case "Int32": { lengthInBits = 32; break; }

                        case "UInt32": { lengthInBits = 32; break; }

                        case "Int64": { lengthInBits = 64; break; }

                        case "UInt64": { lengthInBits = 64; break; }
                        }
                    }

                    values.Add(new EnumeratedValue()
                    {
                        Name           = "None",
                        Value          = 0,
                        ValueSpecified = true
                    });
                }

                template.AddReplacement("_LengthInBits_", lengthInBits);
                template.AddReplacement("_IsOptionSet_", (isOptionSet) ? " IsOptionSet=\"true\"" : "");

                AddTemplate(
                    template,
                    "<!-- ListOfValues -->",
                    TemplatePath + "EnumeratedValue.xml",
                    values,
                    new LoadTemplateEventHandler(LoadTemplate_EnumeratedValue),
                    null);
            }

            ServiceType serviceType = datatype as ServiceType;

            if (serviceType != null)
            {
                AddTemplate(
                    template,
                    "<!-- ListOfRequestParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Request,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);

                AddTemplate(
                    template,
                    "<!-- ListOfResponseParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Response,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            return(template.WriteTemplate(context));
        }
Beispiel #4
0
        /// <summary>
        /// Writes the
        /// </summary>
        private bool WriteTemplate_DataType(Template template, Context context)
        {
            DataType datatype = context.Target as DataType;

            if (datatype == null)
            {
                return(false);
            }

            template.AddReplacement("_TypeName_", datatype.QName.Name);
            CreateDescription(template, "_Description_", datatype.Documentation);

            ComplexType complexType = datatype as ComplexType;

            if (complexType != null)
            {
                List <FieldType> fields = new List <FieldType>();
                GetFields(complexType, fields);

                AddTemplate(
                    template,
                    "<!-- ListOfFields -->",
                    TemplatePath + "Field.xml",
                    fields,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            EnumeratedType enumeratedType = datatype as EnumeratedType;

            if (enumeratedType != null)
            {
                AddTemplate(
                    template,
                    "<!-- ListOfValues -->",
                    TemplatePath + "EnumeratedValue.xml",
                    enumeratedType.Value,
                    new LoadTemplateEventHandler(LoadTemplate_EnumeratedValue),
                    null);
            }

            ServiceType serviceType = datatype as ServiceType;

            if (serviceType != null)
            {
                AddTemplate(
                    template,
                    "<!-- ListOfRequestParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Request,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);

                AddTemplate(
                    template,
                    "<!-- ListOfResponseParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Response,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            return(template.WriteTemplate(context));
        }
Beispiel #5
0
        private void WriteTemplate_EnumsImplementation(string namespacePrefix, string dictionaryNam)
        {
            // get datatypes.
            List <DataType> datatypes = GetDataTypeList(null, m_exportAll, false);

            if (datatypes.Count == 0)
            {
                return;
            }

            // sort types to ensure types are declared in the correct order.
            List <DataType> sortedTypes = new List <DataType>();

            foreach (DataType datatype in datatypes)
            {
                AddDataType(datatype, sortedTypes);
            }

            datatypes = sortedTypes;

            string fileName = String.Format(@"{0}\{1}_enumparser.c", OutputDirectory, namespacePrefix);

            fileName = fileName.ToLower();

            StreamWriter writer = new StreamWriter(fileName, false);

            writer.Write(string.Format(m_sHeader, "OpcUa Enum Type Parser", DateTime.Now));
            writer.Write("#ifdef HAVE_CONFIG_H\n" +
                         "# include \"config.h\"\n" +
                         "#endif\n" +
                         "\n" +
                         "#include <gmodule.h>\n" +
                         "#include <epan/packet.h>\n\n" +
                         "#include \"opcua_enumparser.h\"\n\n");
            try
            {
                foreach (DataType datatype in sortedTypes)
                {
                    EnumeratedType enumtype = datatype as EnumeratedType;

                    if (enumtype != null)
                    {
                        Template template = new Template(writer, WiresharkTemplatePath + "enumparser.c", Assembly.GetExecutingAssembly());

                        template.AddReplacement("_NAME_", enumtype.Name);

                        AddTemplate(
                            template,
                            "// _ENTRY_",
                            WiresharkTemplatePath + "enumparser.c",
                            enumtype.Value,
                            null,
                            new WriteTemplateEventHandler(WriteTemplate_Enum));

                        template.WriteTemplate(null);
                    }
                }

                writer.Write("\n/** header field definitions */\n" +
                             "static hf_register_info hf[] =\n" +
                             "{");
                int index = 0;
                int count = 0;
                // count number of enums
                foreach (DataType datatype in sortedTypes)
                {
                    EnumeratedType enumtype = datatype as EnumeratedType;

                    if (enumtype != null)
                    {
                        count++;
                    }
                }
                // generate code
                foreach (DataType datatype in sortedTypes)
                {
                    EnumeratedType enumtype = datatype as EnumeratedType;

                    if (enumtype != null)
                    {
                        Template template = new Template(writer, WiresharkTemplatePath + "enumregisterinfo.c", Assembly.GetExecutingAssembly());

                        template.AddReplacement("_NAME_", enumtype.Name);
                        template.WriteTemplate(null);
                        if ((index + 1) < count)
                        {
                            writer.Write(",");
                        }
                        index++;
                    }
                }
                writer.Write("\n};\n\n");
                writer.Write("/** Register enum types. */\n");
                writer.Write("void registerEnumTypes(int proto)\n");
                writer.Write("{\n");
                writer.Write("    proto_register_field_array(proto, hf, array_length(hf));\n");
                writer.Write("}\n");
            }
            finally
            {
                writer.Close();
            }
        }
        /// <summary>
        /// Writes the nodes that describe a data type or method.
        /// </summary>
        private bool WriteTemplate_DataType(Template template, Context context)
        {
            DataType datatype = context.Target as DataType;

            if (datatype == null)
            {
                return(false);
            }

            template.AddReplacement("_TypeName_", datatype.QName.Name);
            template.AddReplacement("_DictionaryName_", m_dictionaryName);

            if (datatype.NotInAddressSpace)
            {
                template.AddReplacement(" NotInAddressSpace=\"false\"", " NotInAddressSpace=\"true\"");
            }
            else
            {
                template.AddReplacement(" NotInAddressSpace=\"false\"", "");
            }

            if (!datatype.AllowArrays)
            {
                template.AddReplacement(" NoArraysAllowed=\"false\"", " NoArraysAllowed=\"true\"");
            }
            else
            {
                template.AddReplacement(" NoArraysAllowed=\"false\"", "");
            }

            TypeDeclaration declaration = datatype as TypeDeclaration;

            if (declaration != null)
            {
                DataType sourceType = Validator.ResolveType(declaration.SourceType);

                if (sourceType != null)
                {
                    template.AddReplacement("_BaseType_", GetPrefixedName(sourceType.QName));
                }
            }

            ComplexType complexType = datatype as ComplexType;

            if (complexType != null)
            {
                if (!IsNull(complexType.BaseType))
                {
                    template.AddReplacement("_BaseType_", GetPrefixedName(complexType.BaseType));
                }
                else
                {
                    template.AddReplacement("_BaseType_", "ua:Structure");
                }

                AddTemplate(
                    template,
                    "<!-- ListOfFields -->",
                    TemplatePath + "DataType.xml",
                    new ComplexType[] { complexType },
                    new LoadTemplateEventHandler(LoadTemplate_ComplexTypeFields),
                    null);
            }

            EnumeratedType enumeratedType = datatype as EnumeratedType;

            if (enumeratedType != null)
            {
                if (enumeratedType != null)
                {
                    template.AddReplacement("_BaseType_", "ua:Enumeration");
                }
                else
                {
                    template.AddReplacement("_BaseType_", "ua:BaseDataType");
                }

                AddTemplate(
                    template,
                    "<!-- ListOfFields -->",
                    TemplatePath + "DataType.xml",
                    new EnumeratedType[] { enumeratedType },
                    new LoadTemplateEventHandler(LoadTemplate_EnumeratedTypeValues),
                    null);
            }

            ServiceType serviceType = datatype as ServiceType;

            if (serviceType != null)
            {
                ComplexType requestType = new ComplexType();

                requestType.Name              = String.Format("{0}Request", serviceType.Name);
                requestType.QName             = new XmlQualifiedName(requestType.Name, serviceType.QName.Namespace);
                requestType.Field             = serviceType.Request;
                requestType.NotInAddressSpace = true;

                AddTemplate(
                    template,
                    "<!-- Request -->",
                    TemplatePath + "DataType.xml",
                    new ComplexType[] { requestType },
                    new LoadTemplateEventHandler(LoadTemplate_DataType),
                    new WriteTemplateEventHandler(WriteTemplate_DataType));

                ComplexType responseType = new ComplexType();

                responseType.Name              = String.Format("{0}Response", serviceType.Name);
                responseType.QName             = new XmlQualifiedName(responseType.Name, serviceType.QName.Namespace);
                responseType.Field             = serviceType.Response;
                responseType.NotInAddressSpace = true;

                AddTemplate(
                    template,
                    "<!-- Response -->",
                    TemplatePath + "DataType.xml",
                    new ComplexType[] { responseType },
                    new LoadTemplateEventHandler(LoadTemplate_DataType),
                    new WriteTemplateEventHandler(WriteTemplate_DataType));
            }

            return(template.WriteTemplate(context));
        }
        /// <summary>
        /// Writes a datatype to the stream.
        /// </summary>
        private bool WriteTemplate_DataType(Template template, Context context)
        {
            DataType datatype = context.Target as DataType;

            if (datatype == null)
            {
                return(false);
            }

            template.AddReplacement("_TypeName_", datatype.QName.Name);
            CreateDescription(template, "_Description_", datatype.Documentation);

            AddTemplate(
                template,
                "<!-- ArrayDeclaration -->",
                TemplatePath + "Array.xml",
                new DataType[] { datatype },
                null,
                new WriteTemplateEventHandler(WriteTemplate_Array));

            ComplexType complexType = datatype as ComplexType;

            if (complexType != null)
            {
                if (!IsNull(complexType.BaseType))
                {
                    DataType basetype = Validator.ResolveType(complexType.BaseType);

                    if (basetype == null)
                    {
                        throw new ApplicationException(String.Format("Could not find base type '{0}' for complex type '{1}'.", complexType.BaseType, complexType.QName));
                    }

                    template.AddReplacement("_BaseType_", GetXmlSchemaTypeName(basetype.QName, -1));
                }

                AddTemplate(
                    template,
                    "<!-- ListOfFields -->",
                    TemplatePath + "Field.xml",
                    complexType.Field,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            EnumeratedType enumeratedType = datatype as EnumeratedType;

            if (enumeratedType != null)
            {
                AddTemplate(
                    template,
                    "<!-- ListOfValues -->",
                    TemplatePath + "EnumeratedValue.xml",
                    enumeratedType.Value,
                    new LoadTemplateEventHandler(LoadTemplate_EnumeratedValue),
                    null);
            }

            ServiceType serviceType = datatype as ServiceType;

            if (serviceType != null)
            {
                AddTemplate(
                    template,
                    "<!-- ListOfRequestParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Request,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);

                AddTemplate(
                    template,
                    "<!-- ListOfResponseParameters -->",
                    TemplatePath + "Field.xml",
                    serviceType.Response,
                    new LoadTemplateEventHandler(LoadTemplate_Field),
                    null);
            }

            return(template.WriteTemplate(context));
        }