mavtestgen.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 generate a MAVLink test suite
4 
5 Copyright Andrew Tridgell 2011
6 Released under GNU GPL version 3 or later
7 '''
8 from __future__ import print_function
9 from builtins import range
10 
11 import sys
12 from argparse import ArgumentParser
13 
14 # mavparse is up a directory level
15 from . import mavparse
16 
17 def gen_value(f, i, language):
18  '''generate a test value for the ith field of a message'''
19  type = f.type
20 
21  # could be an array
22  if type.find("[") != -1:
23  aidx = type.find("[")
24  basetype = type[0:aidx]
25  if basetype == "array":
26  basetype = "int8_t"
27  if language == 'C':
28  return '(const %s *)"%s%u"' % (basetype, f.name, i)
29  return '"%s%u"' % (f.name, i)
30 
31  if type == 'float':
32  return 17.0 + i*7
33  if type == 'char':
34  return 'A' + i
35  if type == 'int8_t':
36  return 5 + i
37  if type in ['int8_t', 'uint8_t']:
38  return 5 + i
39  if type in ['uint8_t_mavlink_version']:
40  return 2
41  if type in ['int16_t', 'uint16_t']:
42  return 17235 + i*52
43  if type in ['int32_t', 'uint32_t']:
44  v = 963497464 + i*52
45  if language == 'C':
46  return "%sL" % v
47  return v
48  if type in ['int64_t', 'uint64_t']:
49  v = 9223372036854775807 + i*63
50  if language == 'C':
51  return "%sLL" % v
52  return v
53 
54 
55 
56 def generate_methods_python(outf, msgs):
57  outf.write("""
58 '''
59 MAVLink protocol test implementation (auto-generated by mavtestgen.py)
60 
61 Generated from: %s
62 
63 Note: this file has been auto-generated. DO NOT EDIT
64 '''
65 
66 import mavlink
67 
68 def generate_outputs(mav):
69  '''generate all message types as outputs'''
70 """)
71  for m in msgs:
72  if m.name == "HEARTBEAT": continue
73  outf.write("\tmav.%s_send(" % m.name.lower())
74  for i in range(0, len(m.fields)):
75  f = m.fields[i]
76  outf.write("%s=%s" % (f.name, gen_value(f, i, 'py')))
77  if i != len(m.fields)-1:
78  outf.write(",")
79  outf.write(")\n")
80 
81 
82 def generate_methods_C(outf, msgs):
83  outf.write("""
84 /*
85 MAVLink protocol test implementation (auto-generated by mavtestgen.py)
86 
87 Generated from: %s
88 
89 Note: this file has been auto-generated. DO NOT EDIT
90 */
91 
92 static void mavtest_generate_outputs(mavlink_channel_t chan)
93 {
94 """)
95  for m in msgs:
96  if m.name == "HEARTBEAT": continue
97  outf.write("\tmavlink_msg_%s_send(chan," % m.name.lower())
98  for i in range(0, len(m.fields)):
99  f = m.fields[i]
100  outf.write("%s" % gen_value(f, i, 'C'))
101  if i != len(m.fields)-1:
102  outf.write(",")
103  outf.write(");\n")
104  outf.write("}\n")
105 
106 
107 
108 ######################################################################
109 '''main program'''
110 
111 parser = ArgumentParser(description="This tool generate MAVLink test suite")
112 parser.add_argument("-o", "--output", default="mavtest", help="output folder [default: %(default)s]")
113 parser.add_argument("definitions", metavar="XML", nargs="+", help="MAVLink definitions")
114 args = parser.parse_args()
115 
116 msgs = []
117 enums = []
118 
119 for fname in args.definitions:
120  (m, e) = mavparse.parse_mavlink_xml(fname)
121  msgs.extend(m)
122  enums.extend(e)
123 
124 
125 if mavparse.check_duplicates(msgs):
126  sys.exit(1)
127 
128 print("Found %u MAVLink message types" % len(msgs))
129 
130 print("Generating python %s" % (args.output+'.py'))
131 outf = open(args.output + '.py', "w")
132 generate_methods_python(outf, msgs)
133 outf.close()
134 
135 print("Generating C %s" % (args.output+'.h'))
136 outf = open(args.output + '.h', "w")
137 generate_methods_C(outf, msgs)
138 outf.close()
139 
140 print("Generated %s OK" % args.output)


mavlink
Author(s): Lorenz Meier
autogenerated on Sun Jul 7 2019 03:22:07