mavtestgen.py 3.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#!/usr/bin/env python
'''
generate a MAVLink test suite

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

import sys, textwrap
from optparse import OptionParser
LM's avatar
LM committed
11 12 13

# mavparse is up a directory level
sys.path.append('..')
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
import mavparse

def gen_value(f, i, language):
    '''generate a test value for the ith field of a message'''
    type = f.type

    # could be an array
    if type.find("[") != -1:
        aidx = type.find("[")
        basetype = type[0:aidx]
        if basetype == "array":
            basetype = "int8_t"
        if language == 'C':
            return '(const %s *)"%s%u"' % (basetype, f.name, i)
        return '"%s%u"' % (f.name, i)

    if type == 'float':
        return 17.0 + i*7
    if type == 'char':
        return 'A' + i
    if type == 'int8_t':
        return 5 + i
    if type in ['int8_t', 'uint8_t']:
        return 5 + i
    if type in ['uint8_t_mavlink_version']:
        return 2
    if type in ['int16_t', 'uint16_t']:
        return 17235 + i*52
    if type in ['int32_t', 'uint32_t']:
        v = 963497464 + i*52
        if language == 'C':
            return "%sL" % v
        return v
    if type in ['int64_t', 'uint64_t']:
        v = 9223372036854775807 + i*63
        if language == 'C':
            return "%sLL" % v
        return v



def generate_methods_python(outf, msgs):
    outf.write("""
'''
MAVLink protocol test implementation (auto-generated by mavtestgen.py)

Generated from: %s

Note: this file has been auto-generated. DO NOT EDIT
'''

import mavlink

def generate_outputs(mav):
LM's avatar
LM committed
68
    '''generate all message types as outputs'''
69 70 71
""")
    for m in msgs:
        if m.name == "HEARTBEAT": continue
LM's avatar
LM committed
72
        outf.write("\tmav.%s_send(" % m.name.lower())
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
        for i in range(0, len(m.fields)):
            f = m.fields[i]
            outf.write("%s=%s" % (f.name, gen_value(f, i, 'py')))
            if i != len(m.fields)-1:
                outf.write(",")
        outf.write(")\n")


def generate_methods_C(outf, msgs):
    outf.write("""
/*
MAVLink protocol test implementation (auto-generated by mavtestgen.py)

Generated from: %s

Note: this file has been auto-generated. DO NOT EDIT
*/

static void mavtest_generate_outputs(mavlink_channel_t chan)
{
""")
    for m in msgs:
        if m.name == "HEARTBEAT": continue
LM's avatar
LM committed
96
        outf.write("\tmavlink_msg_%s_send(chan," % m.name.lower())
97 98 99 100 101 102 103 104 105 106 107 108 109
        for i in range(0, len(m.fields)):
            f = m.fields[i]
            outf.write("%s" % gen_value(f, i, 'C'))
            if i != len(m.fields)-1:
                outf.write(",")
        outf.write(");\n")
    outf.write("}\n")



######################################################################
'''main program'''

LM's avatar
LM committed
110 111
parser = OptionParser("%prog [options] <XML files>")
parser.add_option("-o", "--output", dest="output", default="mavtest", help="output folder [default: %default]")
112 113 114 115 116 117 118 119 120 121
(opts, args) = parser.parse_args()

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

msgs = []
enums = []

for fname in args:
LM's avatar
LM committed
122 123 124
    (m, e) = mavparse.parse_mavlink_xml(fname)
    msgs.extend(m)
    enums.extend(e)
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142


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

print("Found %u MAVLink message types" % len(msgs))

print("Generating python %s" % (opts.output+'.py'))
outf = open(opts.output + '.py', "w")
generate_methods_python(outf, msgs)
outf.close()

print("Generating C %s" % (opts.output+'.h'))
outf = open(opts.output + '.h', "w")
generate_methods_C(outf, msgs)
outf.close()

print("Generated %s OK" % opts.output)