mavgen.py 2.14 KB
Newer Older
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
#!/usr/bin/env python
'''
parse a MAVLink protocol XML file and generate a python implementation

Copyright Andrew Tridgell 2011
Released under GNU GPL version 3 or later
'''

import sys, textwrap, os
from optparse import OptionParser

# allow import from the parent directory, where mavutil.py is
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))

import mavparse
import mavgen_python
import mavgen_c

parser = OptionParser("mavgen.py [options] <XML files>")
parser.add_option("-o", "--output", dest="output", default="mavlink", help="output base name")
parser.add_option("--lang", dest="language", default="python", help="language to generate")
parser.add_option("--wire-protocol", dest="wire_protocol", default=mavparse.PROTOCOL_0_9, help="wire protocol version")
(opts, args) = parser.parse_args()

if len(args) < 1:
    parser.error("You must supply at least one MAVLink XML protocol definition")
    

xml = []

for fname in args:
    print("Parsing %s" % fname)
    xml.append(mavparse.MAVXML(fname, opts.wire_protocol))

# expand includes
for x in xml[:]:
    for i in x.include:
        fname = os.path.join(os.path.dirname(x.filename), i)
        print("Parsing %s" % fname)
        xml.append(mavparse.MAVXML(fname, opts.wire_protocol))

        # include message lengths and CRCs too
        for idx in range(0, 256):
            if x.message_lengths[idx] == 0:
                x.message_lengths[idx] = xml[-1].message_lengths[idx]
                x.message_crcs[idx] = xml[-1].message_crcs[idx]
                x.message_names[idx] = xml[-1].message_names[idx]

# work out max payload size across all includes
largest_payload = 0
for x in xml:
    if x.largest_payload > largest_payload:
        largest_payload = x.largest_payload
for x in xml:
    x.largest_payload = largest_payload

if mavparse.check_duplicates(xml):
    sys.exit(1)

print("Found %u MAVLink message types in %u XML files" % (
    mavparse.total_msgs(xml), len(xml)))

if opts.language == 'python':
    mavgen_python.generate(opts.output, xml)
elif opts.language == 'C':
    mavgen_c.generate(opts.output, xml)
else:
    print("Unsupported language %s" % opts.language)