Ejemplo n.º 1
0
        public MavLink Parse(string xmlFile)
        {
            using (var fs = new FileStream(xmlFile, FileMode.Open, FileAccess.Read))
            {
                XmlSerializer s       = new XmlSerializer(typeof(MavLink));
                MavLink       mavlink = (MavLink)s.Deserialize(fs);

                foreach (var mavMess in mavlink.messages)
                {
                    // convert names to lower case.
                    mavMess.name = mavMess.name.ToLowerInvariant();
                    foreach (var field in mavMess.fields)
                    {
                        field.name = field.name.ToLowerInvariant();
                    }

                    //compute the correct types
                    ComputePtype(mavMess);
                    mavMess.fields = mavMess.fields.OrderByDescending(x => typeSize[x.type]).ToList();

                    //compute offset for all the fields
                    ComputeOffsetForFields(mavMess);
                }

                return(mavlink);
            }
        }
Ejemplo n.º 2
0
        public void GeneratePTypes(MavLink mavlink, string outputFile)
        {
            using (StreamWriter sw = new StreamWriter(outputFile, false))
            {
                foreach (var message in mavlink.messages)
                {
                    string fieldsStr = NewLine;

                    if (message.name == "setup_signing")
                    {
                        // something odd is going on with this one, the Python "C" generator is not generating it.
                        continue;
                    }

                    int count = 0;
                    while (count < message.fields.Count())
                    {
                        if (count < message.fields.Count() - 1)
                        {
                            var f = message.fields.ElementAt(count);
                            fieldsStr = fieldsStr + String.Format(AllTemplates.fieldTypeFormat, f.name, f.ptype, ", ", f.description) + NewLine;
                        }
                        else
                        {
                            var f = message.fields.ElementAt(count);
                            fieldsStr = fieldsStr + String.Format(AllTemplates.fieldTypeFormat, f.name, f.ptype, " ", f.description) + NewLine;
                        }
                        count++;
                    }

                    string messagePType = String.Format(AllTemplates.typeFormat, message.description, message.name, fieldsStr);
                    sw.Write(messagePType + NewLine);
                }
            }
        }
Ejemplo n.º 3
0
        public void GeneratePTypes(MavLink mavlink, string outputFile)
        {
            using (StreamWriter sw = new StreamWriter(outputFile, false))
            {

                foreach (var message in mavlink.messages)
                {
                    string fieldsStr = NewLine;

                    if (message.name == "setup_signing")
                    {
                        // something odd is going on with this one, the Python "C" generator is not generating it.
                        continue;
                    }

                    int count = 0;
                    while (count < message.fields.Count())
                    {
                        if (count < message.fields.Count() - 1)
                        {
                            var f = message.fields.ElementAt(count);
                            fieldsStr = fieldsStr + String.Format(AllTemplates.fieldTypeFormat, f.name, f.ptype, ", ", f.description) + NewLine;
                        }
                        else
                        {
                            var f = message.fields.ElementAt(count);
                            fieldsStr = fieldsStr + String.Format(AllTemplates.fieldTypeFormat, f.name, f.ptype, " ", f.description) + NewLine;
                        }
                        count++;

                    }

                    string messagePType = String.Format(AllTemplates.typeFormat, message.description, message.name, fieldsStr);
                    sw.Write(messagePType + NewLine);
                }
            }
        }
Ejemplo n.º 4
0
        public void GeneratePEnums(MavLink mavlink, string outputFile, string tablesFile)
        {
            using (StreamWriter sw = new StreamWriter(outputFile, false))
            {
                sw.Write("// MAVLINK message ids");
                sw.Write(NewLine);

                sw.Write("enum mavlink_messages {");
                sw.Write(NewLine);
                bool first = true;
                // generate enum for message ids.
                foreach (var en in mavlink.messages)
                {
                    if (!first)
                    {
                        sw.Write(',');
                        sw.Write(NewLine);
                    }
                    sw.Write("    msg_" + en.name.ToLowerInvariant() + " = " + en.id);
                    first = false;
                }
                sw.Write(NewLine);
                sw.Write("}");
                sw.Write(NewLine);

                foreach (var en in mavlink.enums)
                {
                    sw.Write(NewLine);
                    sw.Write("// " + ("" + en.description).Replace('\n', ' ').Replace("\r", ""));
                    sw.Write(NewLine);
                    sw.Write("enum {0} {{", en.name.ToLowerInvariant());
                    sw.Write(NewLine);
                    first = true;

                    bool hasZero = false;
                    bool allZero = true;
                    foreach (var e in en.entries)
                    {
                        if (e.value != 0)
                        {
                            allZero = false;
                            break;
                        }
                        else
                        {
                            hasZero = true;
                        }
                    }
                    if (!hasZero && !allZero)
                    {
                        // P enums require one item to be equal to zero
                        first = false;
                        sw.Write("    " + en.name.ToLowerInvariant() + "_none = 0");
                    }

                    foreach (var e in en.entries)
                    {
                        if (!first)
                        {
                            sw.Write(',');
                            sw.Write(NewLine);
                        }
                        first = false;
                        sw.Write("    " + e.name.ToLowerInvariant());
                        if (!allZero)
                        {
                            sw.Write(" = " + e.value);
                        }
                    }
                    sw.Write(NewLine);
                    sw.Write("}");
                }
                sw.Write(NewLine);
            }
        }
Ejemplo n.º 5
0
        public void GeneratePEnums(MavLink mavlink, string outputFile, string tablesFile)
        {
            using (StreamWriter sw = new StreamWriter(outputFile, false))
            {
                sw.Write("// MAVLINK message ids");
                sw.Write(NewLine);

                sw.Write("enum mavlink_messages {");
                sw.Write(NewLine);
                bool first = true;
                // generate enum for message ids.
                foreach (var en in mavlink.messages)
                {
                    if (!first)
                    {
                        sw.Write(',');
                        sw.Write(NewLine);
                    }
                    sw.Write("    msg_" + en.name.ToLowerInvariant() + " = " + en.id);
                    first = false;
                }
                sw.Write(NewLine);
                sw.Write("}");
                sw.Write(NewLine);

                foreach (var en in mavlink.enums)
                {
                    sw.Write(NewLine);
                    sw.Write("// " + ("" + en.description).Replace('\n', ' ').Replace("\r", ""));
                    sw.Write(NewLine);
                    sw.Write("enum {0} {{", en.name.ToLowerInvariant());
                    sw.Write(NewLine);
                    first = true;

                    bool hasZero = false;
                    bool allZero = true;
                    foreach (var e in en.entries)
                    {
                        if (e.value != 0)
                        {
                            allZero = false;
                            break;
                        }
                        else
                        {
                            hasZero = true;
                        }
                    }
                    if (!hasZero && !allZero)
                    {
                        // P enums require one item to be equal to zero
                        first = false;
                        sw.Write("    " + en.name.ToLowerInvariant() + "_none = 0");
                    }

                    foreach (var e in en.entries)
                    {
                        if (!first)
                        {
                            sw.Write(',');
                            sw.Write(NewLine);
                        }
                        first = false;
                        sw.Write("    " + e.name.ToLowerInvariant());
                        if (!allZero)
                        {
                            sw.Write(" = " + e.value);
                        }
                    }
                    sw.Write(NewLine);
                    sw.Write("}");
                }
                sw.Write(NewLine);
            }
        }
Ejemplo n.º 6
0
        static void Main(string[] args)
        {
            string xmlInput     = null;
            string outputFolder = null;

            if (args.Length == 0)
            {
                Console.WriteLine("USAGE: -xml:<pathToXML> -out:<pathToOutDir>");
                return;
            }

            for (int i = 0; i < args.Length; i++)
            {
                string arg      = args[i];
                string colonArg = null;
                if (arg.StartsWith("/") || arg.StartsWith("-"))
                {
                    var colonIndex = arg.IndexOf(':');
                    if (colonIndex >= 0)
                    {
                        arg      = args[i].Substring(1, colonIndex - 1);
                        colonArg = args[i].Substring(colonIndex + 1);
                    }

                    switch (arg)
                    {
                    case "xml":
                        xmlInput = colonArg;
                        break;

                    case "out":
                        outputFolder = colonArg;
                        break;

                    default:
                        Console.WriteLine("USAGE: -xml:<pathToXML> -out:<pathToOutDir>");
                        return;
                    }
                }
            }
            if (!File.Exists(xmlInput))
            {
                Console.WriteLine("File not found: {0}", xmlInput);
                return;
            }
            if (!Directory.Exists(outputFolder))
            {
                Console.WriteLine("Output directory not found: {0}", outputFolder);
                return;
            }

            string pTypeFileName          = String.Format("\\P_{0}_MessageTypes.p", Path.GetFileNameWithoutExtension(xmlInput));
            string pTypeFunctionsFileName = String.Format("\\P_{0}_MessageTypeFunctions.h", Path.GetFileNameWithoutExtension(xmlInput));
            string pEnumsFileName         = String.Format("\\P_{0}_Enums.p", Path.GetFileNameWithoutExtension(xmlInput));

            //parse the XML
            var     parser  = new MavXMLParser();
            MavLink mavlink = parser.Parse(xmlInput);

            //generate the P type for each message
            var generator = new PCodeGenerator();

            generator.GeneratePTypes(mavlink, outputFolder + pTypeFileName);

            generator.GeneratePEnums(mavlink, outputFolder + pEnumsFileName, outputFolder);

            //generate the two functions for encoding and decoding P Message types
            generator.GeneratePTypeHelperFun(mavlink.messages, outputFolder + pTypeFunctionsFileName);
        }