mavtestgen.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 '''
00003 generate a MAVLink test suite
00004 
00005 Copyright Andrew Tridgell 2011
00006 Released under GNU GPL version 3 or later
00007 '''
00008 
00009 import sys, textwrap
00010 from argparse import ArgumentParser
00011 
00012 # mavparse is up a directory level
00013 from . import mavparse
00014 
00015 def gen_value(f, i, language):
00016     '''generate a test value for the ith field of a message'''
00017     type = f.type
00018 
00019     # could be an array
00020     if type.find("[") != -1:
00021         aidx = type.find("[")
00022         basetype = type[0:aidx]
00023         if basetype == "array":
00024             basetype = "int8_t"
00025         if language == 'C':
00026             return '(const %s *)"%s%u"' % (basetype, f.name, i)
00027         return '"%s%u"' % (f.name, i)
00028 
00029     if type == 'float':
00030         return 17.0 + i*7
00031     if type == 'char':
00032         return 'A' + i
00033     if type == 'int8_t':
00034         return 5 + i
00035     if type in ['int8_t', 'uint8_t']:
00036         return 5 + i
00037     if type in ['uint8_t_mavlink_version']:
00038         return 2
00039     if type in ['int16_t', 'uint16_t']:
00040         return 17235 + i*52
00041     if type in ['int32_t', 'uint32_t']:
00042         v = 963497464 + i*52
00043         if language == 'C':
00044             return "%sL" % v
00045         return v
00046     if type in ['int64_t', 'uint64_t']:
00047         v = 9223372036854775807 + i*63
00048         if language == 'C':
00049             return "%sLL" % v
00050         return v
00051 
00052 
00053 
00054 def generate_methods_python(outf, msgs):
00055     outf.write("""
00056 '''
00057 MAVLink protocol test implementation (auto-generated by mavtestgen.py)
00058 
00059 Generated from: %s
00060 
00061 Note: this file has been auto-generated. DO NOT EDIT
00062 '''
00063 
00064 import mavlink
00065 
00066 def generate_outputs(mav):
00067     '''generate all message types as outputs'''
00068 """)
00069     for m in msgs:
00070         if m.name == "HEARTBEAT": continue
00071         outf.write("\tmav.%s_send(" % m.name.lower())
00072         for i in range(0, len(m.fields)):
00073             f = m.fields[i]
00074             outf.write("%s=%s" % (f.name, gen_value(f, i, 'py')))
00075             if i != len(m.fields)-1:
00076                 outf.write(",")
00077         outf.write(")\n")
00078 
00079 
00080 def generate_methods_C(outf, msgs):
00081     outf.write("""
00082 /*
00083 MAVLink protocol test implementation (auto-generated by mavtestgen.py)
00084 
00085 Generated from: %s
00086 
00087 Note: this file has been auto-generated. DO NOT EDIT
00088 */
00089 
00090 static void mavtest_generate_outputs(mavlink_channel_t chan)
00091 {
00092 """)
00093     for m in msgs:
00094         if m.name == "HEARTBEAT": continue
00095         outf.write("\tmavlink_msg_%s_send(chan," % m.name.lower())
00096         for i in range(0, len(m.fields)):
00097             f = m.fields[i]
00098             outf.write("%s" % gen_value(f, i, 'C'))
00099             if i != len(m.fields)-1:
00100                 outf.write(",")
00101         outf.write(");\n")
00102     outf.write("}\n")
00103 
00104 
00105 
00106 ######################################################################
00107 '''main program'''
00108 
00109 parser = ArgumentParser(description="This tool generate MAVLink test suite")
00110 parser.add_argument("-o", "--output", default="mavtest", help="output folder [default: %(default)s]")
00111 parser.add_argument("definitions", metavar="XML", nargs="+", help="MAVLink definitions")
00112 args = parser.parse_args()
00113 
00114 msgs = []
00115 enums = []
00116 
00117 for fname in args.definitions:
00118     (m, e) = mavparse.parse_mavlink_xml(fname)
00119     msgs.extend(m)
00120     enums.extend(e)
00121 
00122 
00123 if mavparse.check_duplicates(msgs):
00124     sys.exit(1)
00125 
00126 print("Found %u MAVLink message types" % len(msgs))
00127 
00128 print("Generating python %s" % (args.output+'.py'))
00129 outf = open(args.output + '.py', "w")
00130 generate_methods_python(outf, msgs)
00131 outf.close()
00132 
00133 print("Generating C %s" % (args.output+'.h'))
00134 outf = open(args.output + '.h', "w")
00135 generate_methods_C(outf, msgs)
00136 outf.close()
00137 
00138 print("Generated %s OK" % args.output)


mavlink
Author(s): Lorenz Meier
autogenerated on Sun May 22 2016 04:05:43