mavtestgen.py 3.5 KB

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