def generate_shared(basename, xml_list): # Create a dictionary to hold all the values we want to use in the templates template_dict = {} template_dict['parse_time'] = xml_list[0].parse_time template_dict['message'] = [] template_dict['message_definition_files'] = [] print("Generating Objective-C implementation in directory %s" % basename) mavparse.mkdir_p(basename) for xml in xml_list: template_dict['message'].extend(xml.message) basename_camel_case = camel_case_from_underscores(xml.basename) template_dict['message_definition_files'].append( {'name_camel_case': basename_camel_case}) if not template_dict.get('basename', None): template_dict['basename'] = xml.basename else: template_dict[ 'basename'] = template_dict['basename'] + ', ' + xml.basename # Sort messages by ID template_dict['message'] = sorted(template_dict['message'], key=lambda message: message.id) # Add name_camel_case to each message object for message in template_dict['message']: message.name_camel_case = camel_case_from_underscores( message.name_lower) generate_mavlink(basename, template_dict) generate_base_message(basename, template_dict)
def generate_shared(basename, xml_list): # Create a dictionary to hold all the values we want to use in the templates template_dict = {} template_dict['parse_time'] = xml_list[0].parse_time template_dict['message'] = [] template_dict['message_definition_files'] = [] print("Generating Objective-C implementation in directory %s" % basename) mavparse.mkdir_p(basename) for xml in xml_list: template_dict['message'].extend(xml.message) basename_camel_case = camel_case_from_underscores(xml.basename) template_dict['message_definition_files'].append({'name_camel_case': basename_camel_case}) if not template_dict.get('basename', None): template_dict['basename'] = xml.basename else: template_dict['basename'] = template_dict['basename'] + ', ' + xml.basename # Sort messages by ID template_dict['message'] = sorted(template_dict['message'], key = lambda message : message.id) # Add name_camel_case to each message object for message in template_dict['message']: message.name_camel_case = camel_case_from_underscores(message.name_lower) generate_mavlink(basename, template_dict) generate_base_message(basename, template_dict)
def generate_messages(basename, xml_list): messages = [] print("Generating SMACCMPilot.Mavlink.Messages modules in directory %s" % basename) mavparse.mkdir_p(basename) for xml in xml_list: process_xml(basename, xml); for m in xml.message: generate_message_ivory(basename, m) messages.append(m) generate_messages_hs(basename, messages)
def generate_enums(basename, xml): '''generate main header per XML file''' directory = os.path.join(basename, '''enums''') mavparse.mkdir_p(directory) for en in xml.enum: f = open(os.path.join(directory, en.name+".java"), mode='w') t.write(f, ''' /** ${description} */ package com.MAVLink.Messages.enums; public class ${name} { ${{entry: public static final int ${name} = ${value}; /* ${description} |${{param:${description}| }} */ }} } ''', en) f.close()
def generate(xml_list, outputdirectory): mavparse.mkdir_p(outputdirectory) # I've really punted on doing dependencies right - # it would take some significant work. # We'll go back to it later. common = None for xml in xml_list: mavgen_process_xml.process_xml(xml) if xml.basename == "common": common = xml for xml in xml_list: if xml.basename == "common": r = generate_mavlink_thrift(xml, None) else: r = generate_mavlink_thrift(xml, common) writethrift(r,outputdirectory,xml.basename)
def generate_one(basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating C implementation in directory %s" % directory) mavparse.mkdir_p(directory) if xml.little_endian: xml.mavlink_endian = "MAVLINK_LITTLE_ENDIAN" else: xml.mavlink_endian = "MAVLINK_BIG_ENDIAN" if xml.crc_extra: xml.crc_extra_define = "1" else: xml.crc_extra_define = "0" if xml.sort_fields: xml.aligned_fields_define = "1" else: xml.aligned_fields_define = "0" # work out the included headers xml.include_list = [] for i in xml.include: base = i[:-4] xml.include_list.append(mav_include(base)) # form message lengths array xml.message_lengths_array = '' for mlen in xml.message_lengths: xml.message_lengths_array += '%u, ' % mlen xml.message_lengths_array = xml.message_lengths_array[:-2] # and message CRCs array xml.message_crcs_array = '' for crc in xml.message_crcs: xml.message_crcs_array += '%u, ' % crc xml.message_crcs_array = xml.message_crcs_array[:-2] # form message info array xml.message_info_array = '' for name in xml.message_names: if name is not None: xml.message_info_array += 'MAVLINK_MESSAGE_INFO_%s, ' % name else: # Several C compilers don't accept {NULL} for # multi-dimensional arrays and structs # feed the compiler a "filled" empty message xml.message_info_array += '{"EMPTY",0,{{"","",MAVLINK_TYPE_CHAR,0,0,0}}}, ' xml.message_info_array = xml.message_info_array[:-2] # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" for f in m.fields: if f.print_format is None: f.c_print_format = 'NULL' else: f.c_print_format = '"%s"' % f.print_format if f.array_length != 0: f.array_suffix = '[%u]' % f.array_length f.array_prefix = '*' f.array_tag = '_array' f.array_arg = ', %u' % f.array_length f.array_return_arg = '%s, %u, ' % (f.name, f.array_length) f.array_const = 'const ' f.decode_left = '' f.decode_right = ', %s->%s' % (m.name_lower, f.name) f.return_type = 'uint16_t' f.get_arg = ', %s *%s' % (f.type, f.name) if f.type == 'char': f.c_test_value = '"%s"' % f.test_value else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) else: f.array_suffix = '' f.array_prefix = '' f.array_tag = '' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = "%s->%s = " % (m.name_lower, f.name) f.decode_right = '' f.get_arg = '' f.return_type = f.type if f.type == 'char': f.c_test_value = "'%s'" % f.test_value elif f.type == 'uint64_t': f.c_test_value = "%sULL" % f.test_value elif f.type == 'int64_t': f.c_test_value = "%sLL" % f.test_value else: f.c_test_value = f.test_value # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value generate_mavlink_h(directory, xml) generate_version_h(directory, xml) generate_main_h(directory, xml) for m in xml.message: generate_message_h(directory, m) generate_testsuite_h(directory, xml)
def generate_one(basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating CSharp implementation in directory %s" % directory) mavparse.mkdir_p(directory) if xml.little_endian: xml.mavlink_endian = "MAVLINK_LITTLE_ENDIAN" else: xml.mavlink_endian = "MAVLINK_BIG_ENDIAN" if xml.crc_extra: xml.crc_extra_define = "1" else: xml.crc_extra_define = "0" if xml.sort_fields: xml.aligned_fields_define = "1" else: xml.aligned_fields_define = "0" # work out the included headers xml.include_list = [] for i in xml.include: base = i[:-4] xml.include_list.append(mav_include(base)) # form message lengths array xml.message_lengths_array = '' for mlen in xml.message_lengths: xml.message_lengths_array += '%u, ' % mlen xml.message_lengths_array = xml.message_lengths_array[:-2] # and message CRCs array xml.message_crcs_array = '' for crc in xml.message_crcs: xml.message_crcs_array += '%u, ' % crc xml.message_crcs_array = xml.message_crcs_array[:-2] # form message info array xml.message_info_array = '' for name in xml.message_names: if name is not None: xml.message_info_array += 'typeof( mavlink_%s_t ), ' % name.lower() else: #xml.message_info_array += '{"EMPTY",0,{}}, ' xml.message_info_array += 'null, ' xml.message_info_array = xml.message_info_array[:-2] # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" for f in m.fields: # if f.print_format is None: # f.c_print_format = 'null' # else: # f.c_print_format = '"%s"' % f.print_format if f.array_length != 0: f.array_suffix = '' f.array_prefix = '[MarshalAs(UnmanagedType.ByValArray,SizeConst=%u)]\n public' % f.array_length f.array_arg = ', %u' % f.array_length f.array_return_arg = '%u, ' % (f.array_length) f.array_tag = '' f.array_const = 'const ' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.return_type = 'void' f.return_value = 'void' if f.type == 'char': f.type = "string" f.array_tag = 'System.Text.ASCIIEncoding.ASCII.GetString(msg,%u,%u); //' % ( f.wire_offset, f.array_length) f.return_type = 'string' f.c_test_value = ".ToCharArray()" elif f.type == 'uint8_t': f.type = "byte[]" f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int8_t': f.type = "byte[]" f.array_tag = 'getBytes' f.return_type = 'byte[]' else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) f.array_tag = '!!!%s' % f.type f.get_arg = ', %s %s' % (f.type, f.name) else: if f.type == 'char': f.type = "byte" elif f.type == 'uint8_t': f.type = "byte" elif f.type == 'int8_t': f.type = "byte" elif f.type == 'uint16_t': f.type = "UInt16" elif f.type == 'uint32_t': f.type = "UInt32" elif f.type == 'int16_t': f.type = "Int16" elif f.type == 'int32_t': f.type = "Int32" elif f.type == 'uint64_t': f.type = "UInt64" elif f.type == 'int64_t': f.type = "Int64" elif f.type == 'float': f.type = "Single" else: f.c_test_value = f.test_value f.array_suffix = '' f.array_prefix = 'public ' f.array_tag = 'BitConverter.To%s' % f.type if f.type == 'byte': f.array_tag = 'getByte' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.get_arg = '' f.c_test_value = f.test_value f.return_type = f.type # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value # generate_mavlink_h(directory, xml) # generate_version_h(directory, xml) # generate_main_h(directory, xml) for m in xml.message: generate_message_h(directory, m)
def generate_one(basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, '''ardupilotmega''') print("Generating Java implementation in directory %s" % directory) mavparse.mkdir_p(directory) if xml.little_endian: xml.mavlink_endian = "MAVLINK_LITTLE_ENDIAN" else: xml.mavlink_endian = "MAVLINK_BIG_ENDIAN" if xml.crc_extra: xml.crc_extra_define = "1" else: xml.crc_extra_define = "0" if xml.sort_fields: xml.aligned_fields_define = "1" else: xml.aligned_fields_define = "0" # work out the included headers xml.include_list = [] for i in xml.include: base = i[:-4] xml.include_list.append(mav_include(base)) # form message lengths array xml.message_lengths_array = '' for mlen in xml.message_lengths: xml.message_lengths_array += '%u, ' % mlen xml.message_lengths_array = xml.message_lengths_array[:-2] # form message info array xml.message_info_array = '' for name in xml.message_names: if name is not None: xml.message_info_array += 'MAVLINK_MESSAGE_INFO_%s, ' % name else: # Several C compilers don't accept {NULL} for # multi-dimensional arrays and structs # feed the compiler a "filled" empty message xml.message_info_array += '{"EMPTY",0,{{"","",MAVLINK_TYPE_CHAR,0,0,0}}}, ' xml.message_info_array = xml.message_info_array[:-2] # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" for f in m.fields: if f.print_format is None: f.c_print_format = 'NULL' else: f.c_print_format = '"%s"' % f.print_format f.getText = '' if f.array_length != 0: f.array_suffix = '[] = new %s[%u]' % (mavfmt(f), f.array_length) f.array_prefix = '*' f.array_tag = '_array' f.array_arg = ', %u' % f.array_length f.array_return_arg = '%s, %u, ' % (f.name, f.array_length) f.array_const = 'const ' f.decode_left = '' f.decode_right = 'm.%s' % (f.name) f.unpackField = ''' for (int i = 0; i < %s.length; i++) { %s[i] = payload.get%s(); }''' % (f.name, f.name, mavfmt(f).title()) f.packField = ''' for (int i = 0; i < %s.length; i++) { packet.payload.put%s(%s[i]); }''' % (f.name, mavfmt(f).title(), f.name) f.return_type = 'uint16_t' f.get_arg = ', %s *%s' % (f.type, f.name) if f.type == 'char': f.c_test_value = '"%s"' % f.test_value f.getText = '''/** * Sets the buffer of this message with a string, adds the necessary padding */ public void set%s(String str) { int len = Math.min(str.length(), %d); for (int i=0; i<len; i++) { %s[i] = (byte) str.charAt(i); } for (int i=len; i<%d; i++) { // padding for the rest of the buffer %s[i] = 0; } } /** * Gets the message, formated as a string */ public String get%s() { String result = ""; for (int i = 0; i < %d; i++) { if (%s[i] != 0) result = result + (char) %s[i]; else break; } return result; }''' % (f.name.title(), f.array_length, f.name, f.array_length, f.name, f.name.title(), f.array_length, f.name, f.name) else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) else: f.array_suffix = '' f.array_prefix = '' f.array_tag = '' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = '%s' % (f.name) f.decode_right = '' f.unpackField = '%s = payload.get%s();' % (f.name, mavfmt(f).title()) f.packField = 'packet.payload.put%s(%s);' % (mavfmt(f).title(), f.name) f.get_arg = '' f.return_type = f.type if f.type == 'char': f.c_test_value = "'%s'" % f.test_value elif f.type == 'uint64_t': f.c_test_value = "%sULL" % f.test_value elif f.type == 'int64_t': f.c_test_value = "%sLL" % f.test_value else: f.c_test_value = f.test_value # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value # fix types to java for m in xml.message: for f in m.ordered_fields: f.type = mavfmt(f) #generate_mavlink_h(directory, xml) #generate_version_h(directory, xml) for m in xml.message: generate_message_h(directory, m)
def generate_one(basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, '''ardupilotmega''') print("Generating Java implementation in directory %s" % directory) mavparse.mkdir_p(directory) if xml.little_endian: xml.mavlink_endian = "MAVLINK_LITTLE_ENDIAN" else: xml.mavlink_endian = "MAVLINK_BIG_ENDIAN" if xml.crc_extra: xml.crc_extra_define = "1" else: xml.crc_extra_define = "0" if xml.sort_fields: xml.aligned_fields_define = "1" else: xml.aligned_fields_define = "0" # work out the included headers xml.include_list = [] for i in xml.include: base = i[:-4] xml.include_list.append(mav_include(base)) # form message lengths array xml.message_lengths_array = '' for mlen in xml.message_lengths: xml.message_lengths_array += '%u, ' % mlen xml.message_lengths_array = xml.message_lengths_array[:-2] # form message info array xml.message_info_array = '' for name in xml.message_names: if name is not None: xml.message_info_array += 'MAVLINK_MESSAGE_INFO_%s, ' % name else: # Several C compilers don't accept {NULL} for # multi-dimensional arrays and structs # feed the compiler a "filled" empty message xml.message_info_array += '{"EMPTY",0,{{"","",MAVLINK_TYPE_CHAR,0,0,0}}}, ' xml.message_info_array = xml.message_info_array[:-2] # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" for f in m.fields: if f.print_format is None: f.c_print_format = 'NULL' else: f.c_print_format = '"%s"' % f.print_format f.getText = '' if f.array_length != 0: f.array_suffix = '[] = new %s[%u]' % (mavfmt(f),f.array_length) f.array_prefix = '*' f.array_tag = '_array' f.array_arg = ', %u' % f.array_length f.array_return_arg = '%s, %u, ' % (f.name, f.array_length) f.array_const = 'const ' f.decode_left = '' f.decode_right = 'm.%s' % (f.name) f.unpackField = ''' for (int i = 0; i < %s.length; i++) { %s[i] = payload.get%s(); }''' % (f.name, f.name, mavfmt(f).title() ) f.packField = ''' for (int i = 0; i < %s.length; i++) { packet.payload.put%s(%s[i]); }''' % (f.name, mavfmt(f).title(),f.name) f.return_type = 'uint16_t' f.get_arg = ', %s *%s' % (f.type, f.name) if f.type == 'char': f.c_test_value = '"%s"' % f.test_value f.getText = '''/** * Sets the buffer of this message with a string, adds the necessary padding */ public void set%s(String str) { int len = Math.min(str.length(), %d); for (int i=0; i<len; i++) { %s[i] = (byte) str.charAt(i); } for (int i=len; i<%d; i++) { // padding for the rest of the buffer %s[i] = 0; } } /** * Gets the message, formated as a string */ public String get%s() { String result = ""; for (int i = 0; i < %d; i++) { if (%s[i] != 0) result = result + (char) %s[i]; else break; } return result; }''' % (f.name.title(),f.array_length,f.name,f.array_length,f.name,f.name.title(),f.array_length,f.name,f.name) else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) else: f.array_suffix = '' f.array_prefix = '' f.array_tag = '' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = '%s' % (f.name) f.decode_right = '' f.unpackField = '%s = payload.get%s();' % (f.name, mavfmt(f).title()) f.packField = 'packet.payload.put%s(%s);' % (mavfmt(f).title(),f.name) f.get_arg = '' f.return_type = f.type if f.type == 'char': f.c_test_value = "'%s'" % f.test_value elif f.type == 'uint64_t': f.c_test_value = "%sULL" % f.test_value elif f.type == 'int64_t': f.c_test_value = "%sLL" % f.test_value else: f.c_test_value = f.test_value # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value # fix types to java for m in xml.message: for f in m.ordered_fields: f.type = mavfmt(f) #generate_mavlink_h(directory, xml) #generate_version_h(directory, xml) for m in xml.message: generate_message_h(directory, m)
def generate_one(fh, basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating CSharp implementation in directory %s" % directory) mavparse.mkdir_p(directory) # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" m.msg_nameid = "MAVLINK_MSG_ID_${name} = ${id}" for f in m.fields: f.description = f.description.replace("\n"," ") f.description = f.description.replace("\r","") if f.array_length != 0: f.array_suffix = '' f.array_prefix = '[MarshalAs(UnmanagedType.ByValArray,SizeConst=%u)]\n\t\tpublic' % f.array_length f.array_arg = ', %u' % f.array_length f.array_return_arg = '%u, ' % (f.array_length) f.array_tag = '' f.array_const = 'const ' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.return_type = 'void' f.return_value = 'void' if f.type == 'char': f.type = "byte[]" f.array_tag = 'System.Text.ASCIIEncoding.ASCII.GetString(msg,%u,%u); //' % (f.wire_offset, f.array_length) f.return_type = 'byte[]' f.c_test_value = ".ToCharArray()"; elif f.type == 'uint8_t': f.type = "byte[]"; f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int8_t': f.type = "byte[]"; f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int16_t': f.type = "Int16[]"; f.array_tag = 'getBytes' f.return_type = 'Int16[]' elif f.type == 'uint16_t': f.type = "UInt16[]"; f.array_tag = 'getBytes' f.return_type = 'UInt16[]' else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) f.array_tag = '!!!%s' % f.type f.get_arg = ', %s %s' % (f.type, f.name) else: if f.type == 'char': f.type = "byte"; elif f.type == 'uint8_t': f.type = "byte"; elif f.type == 'int8_t': f.type = "byte"; elif f.type == 'int16_t': f.type = "Int16"; elif f.type == 'uint16_t': f.type = "UInt16"; elif f.type == 'uint32_t': f.type = "UInt32"; elif f.type == 'int16_t': f.type = "Int16"; elif f.type == 'int32_t': f.type = "Int32"; elif f.type == 'uint64_t': f.type = "UInt64"; elif f.type == 'int64_t': f.type = "Int64"; elif f.type == 'float': f.type = "Single"; else: f.c_test_value = f.test_value f.array_suffix = '' f.array_prefix = 'public ' f.array_tag = 'BitConverter.To%s' % f.type if f.type == 'byte': f.array_tag = 'getByte' if f.name == 'fixed': # this is a keyword f.name = '@fixed' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.get_arg = '' f.c_test_value = f.test_value f.return_type = f.type # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value for m in xml.message: generate_message_h(fh, directory, m)
def generate_message_definitions(basename, xml): '''generate files for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating Objective-C implementation in directory %s" % directory) mavparse.mkdir_p(directory) xml.basename_camel_case = camel_case_from_underscores(xml.basename) # Add some extra field attributes for convenience for m in xml.message: m.basename = xml.basename m.parse_time = xml.parse_time m.name_camel_case = camel_case_from_underscores(m.name_lower) for f in m.fields: f.name_lower_camel_case = lower_camel_case_from_underscores(f.name); f.get_message = "[self %s]" % f.name_lower_camel_case f.return_method_implementation = '' f.array_prefix = '' f.array_return_arg = '' f.get_arg = '' f.get_arg_objc = '' if f.enum: f.return_type = f.enum f.arg_type = f.enum else: f.return_type = f.type f.arg_type = f.type if f.print_format is None: if f.array_length != 0: f.print_format = "%@" elif f.type.startswith('uint64_t'): f.print_format = "%lld" elif f.type.startswith('uint') or f.type.startswith('int'): f.print_format = "%d" elif f.type.startswith('float'): f.print_format = "%f" elif f.type.startswith('char'): f.print_format = "%c" else: print ("print_format unsupported for type %s" % f.type) if f.array_length != 0: f.get_message = '@"[array of %s[%d]]"' % (f.type, f.array_length) f.array_prefix = ' *' f.array_return_arg = '%s, %u, ' % (f.name, f.array_length) f.return_type = 'uint16_t' f.get_arg = ', %s' % (f.name) f.get_arg_objc = ':(%s *)%s' % (f.type, f.name) if f.type == 'char': # Special handling for strings (assumes all char arrays are strings) f.return_type = 'NSString *' f.get_arg_objc = '' f.get_message = "[self %s]" % f.name_lower_camel_case f.return_method_implementation = \ """char string[%(array_length)d]; mavlink_msg_%(message_name_lower)s_get_%(name)s(&(self->_message), (char *)&string); return [[NSString alloc] initWithBytes:string length:%(array_length)d encoding:NSASCIIStringEncoding];""" % {'array_length': f.array_length, 'message_name_lower': m.name_lower, 'name': f.name} if not f.return_method_implementation: f.return_method_implementation = \ """return mavlink_msg_%(message_name_lower)s_get_%(name)s(&(self->_message)%(get_arg)s);""" % {'message_name_lower': m.name_lower, 'name': f.name, 'get_arg': f.get_arg} for m in xml.message: m.arg_fields = [] for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) generate_message_definitions_h(directory, xml) for m in xml.message: generate_message(directory, m)
def generate_one(fh, basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating CSharp implementation in directory %s" % directory) mavparse.mkdir_p(directory) # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" m.msg_nameid = "MAVLINK_MSG_ID_${name} = ${id}" for f in m.fields: f.description = f.description.replace("\n", " ") f.description = f.description.replace("\r", "") if f.array_length != 0: f.array_suffix = '' f.array_prefix = '[MarshalAs(UnmanagedType.ByValArray,SizeConst=%u)]\n\t\tpublic' % f.array_length f.array_arg = ', %u' % f.array_length f.array_return_arg = '%u, ' % (f.array_length) f.array_tag = '' f.array_const = 'const ' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.return_type = 'void' f.return_value = 'void' if f.type == 'char': f.type = "byte[]" f.array_tag = 'System.Text.ASCIIEncoding.ASCII.GetString(msg,%u,%u); //' % ( f.wire_offset, f.array_length) f.return_type = 'byte[]' f.c_test_value = ".ToCharArray()" elif f.type == 'uint8_t': f.type = "byte[]" f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int8_t': f.type = "byte[]" f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int16_t': f.type = "Int16[]" f.array_tag = 'getBytes' f.return_type = 'Int16[]' elif f.type == 'uint16_t': f.type = "UInt16[]" f.array_tag = 'getBytes' f.return_type = 'UInt16[]' else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) f.array_tag = '!!!%s' % f.type f.get_arg = ', %s %s' % (f.type, f.name) else: if f.type == 'char': f.type = "byte" elif f.type == 'uint8_t': f.type = "byte" elif f.type == 'int8_t': f.type = "byte" elif f.type == 'int16_t': f.type = "Int16" elif f.type == 'uint16_t': f.type = "UInt16" elif f.type == 'uint32_t': f.type = "UInt32" elif f.type == 'int16_t': f.type = "Int16" elif f.type == 'int32_t': f.type = "Int32" elif f.type == 'uint64_t': f.type = "UInt64" elif f.type == 'int64_t': f.type = "Int64" elif f.type == 'float': f.type = "Single" else: f.c_test_value = f.test_value f.array_suffix = '' f.array_prefix = 'public ' f.array_tag = 'BitConverter.To%s' % f.type if f.type == 'byte': f.array_tag = 'getByte' if f.name == 'fixed': # this is a keyword f.name = '@fixed' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.get_arg = '' f.c_test_value = f.test_value f.return_type = f.type # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value for m in xml.message: generate_message_h(fh, directory, m)
def generate_message_definitions(basename, xml): '''generate files for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating Objective-C implementation in directory %s" % directory) mavparse.mkdir_p(directory) xml.basename_camel_case = camel_case_from_underscores(xml.basename) # Add some extra field attributes for convenience for m in xml.message: m.basename = xml.basename m.parse_time = xml.parse_time m.name_camel_case = camel_case_from_underscores(m.name_lower) for f in m.fields: f.name_lower_camel_case = lower_camel_case_from_underscores(f.name) f.get_message = "[self %s]" % f.name_lower_camel_case f.return_method_implementation = '' f.array_prefix = '' f.array_return_arg = '' f.get_arg = '' f.get_arg_objc = '' if f.enum: f.return_type = f.enum f.arg_type = f.enum else: f.return_type = f.type f.arg_type = f.type if f.print_format is None: if f.array_length != 0: f.print_format = "%@" elif f.type.startswith('uint64_t'): f.print_format = "%lld" elif f.type.startswith('uint') or f.type.startswith('int'): f.print_format = "%d" elif f.type.startswith('float'): f.print_format = "%f" elif f.type.startswith('char'): f.print_format = "%c" else: print "print_format unsupported for type %s" % f.type if f.array_length != 0: f.get_message = '@"[array of %s[%d]]"' % (f.type, f.array_length) f.array_prefix = ' *' f.array_return_arg = '%s, %u, ' % (f.name, f.array_length) f.return_type = 'uint16_t' f.get_arg = ', %s' % (f.name) f.get_arg_objc = ':(%s *)%s' % (f.type, f.name) if f.type == 'char': # Special handling for strings (assumes all char arrays are strings) f.return_type = 'NSString *' f.get_arg_objc = '' f.get_message = "[self %s]" % f.name_lower_camel_case f.return_method_implementation = \ """char string[%(array_length)d]; mavlink_msg_%(message_name_lower)s_get_%(name)s(&(self->_message), (char *)&string); return [[NSString alloc] initWithBytes:string length:%(array_length)d encoding:NSASCIIStringEncoding];""" % {'array_length': f.array_length, 'message_name_lower': m.name_lower, 'name': f.name} if not f.return_method_implementation: f.return_method_implementation = \ """return mavlink_msg_%(message_name_lower)s_get_%(name)s(&(self->_message)%(get_arg)s);""" % {'message_name_lower': m.name_lower, 'name': f.name, 'get_arg': f.get_arg} for m in xml.message: m.arg_fields = [] for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) generate_message_definitions_h(directory, xml) for m in xml.message: generate_message(directory, m)
def generate_one(basename, xml): '''generate headers for one XML file''' directory = os.path.join(basename, xml.basename) print("Generating CSharp implementation in directory %s" % directory) mavparse.mkdir_p(directory) if xml.little_endian: xml.mavlink_endian = "MAVLINK_LITTLE_ENDIAN" else: xml.mavlink_endian = "MAVLINK_BIG_ENDIAN" if xml.crc_extra: xml.crc_extra_define = "1" else: xml.crc_extra_define = "0" if xml.sort_fields: xml.aligned_fields_define = "1" else: xml.aligned_fields_define = "0" # work out the included headers xml.include_list = [] for i in xml.include: base = i[:-4] xml.include_list.append(mav_include(base)) # form message lengths array xml.message_lengths_array = '' for mlen in xml.message_lengths: xml.message_lengths_array += '%u, ' % mlen xml.message_lengths_array = xml.message_lengths_array[:-2] # and message CRCs array xml.message_crcs_array = '' for crc in xml.message_crcs: xml.message_crcs_array += '%u, ' % crc xml.message_crcs_array = xml.message_crcs_array[:-2] # form message info array xml.message_info_array = '' for name in xml.message_names: if name is not None: xml.message_info_array += 'typeof( mavlink_%s_t ), ' % name.lower() else: #xml.message_info_array += '{"EMPTY",0,{}}, ' xml.message_info_array += 'null, ' xml.message_info_array = xml.message_info_array[:-2] # add some extra field attributes for convenience with arrays for m in xml.message: m.msg_name = m.name if xml.crc_extra: m.crc_extra_arg = ", %s" % m.crc_extra else: m.crc_extra_arg = "" for f in m.fields: # if f.print_format is None: # f.c_print_format = 'null' # else: # f.c_print_format = '"%s"' % f.print_format if f.array_length != 0: f.array_suffix = '' f.array_prefix = '[MarshalAs(UnmanagedType.ByValArray,SizeConst=%u)]\n public' % f.array_length f.array_arg = ', %u' % f.array_length f.array_return_arg = '%u, ' % (f.array_length) f.array_tag = '' f.array_const = 'const ' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.return_type = 'void' f.return_value = 'void' if f.type == 'char': f.type = "string" f.array_tag = 'System.Text.ASCIIEncoding.ASCII.GetString(msg,%u,%u); //' % (f.wire_offset, f.array_length) f.return_type = 'string' f.c_test_value = ".ToCharArray()"; elif f.type == 'uint8_t': f.type = "byte[]"; f.array_tag = 'getBytes' f.return_type = 'byte[]' elif f.type == 'int8_t': f.type = "byte[]"; f.array_tag = 'getBytes' f.return_type = 'byte[]' else: test_strings = [] for v in f.test_value: test_strings.append(str(v)) f.c_test_value = '{ %s }' % ', '.join(test_strings) f.array_tag = '!!!%s' % f.type f.get_arg = ', %s %s' % (f.type, f.name) else: if f.type == 'char': f.type = "byte"; elif f.type == 'uint8_t': f.type = "byte"; elif f.type == 'int8_t': f.type = "byte"; elif f.type == 'uint16_t': f.type = "UInt16"; elif f.type == 'uint32_t': f.type = "UInt32"; elif f.type == 'int16_t': f.type = "Int16"; elif f.type == 'int32_t': f.type = "Int32"; elif f.type == 'uint64_t': f.type = "UInt64"; elif f.type == 'int64_t': f.type = "Int64"; elif f.type == 'float': f.type = "Single"; else: f.c_test_value = f.test_value f.array_suffix = '' f.array_prefix = 'public ' f.array_tag = 'BitConverter.To%s' % f.type if f.type == 'byte': f.array_tag = 'getByte' f.array_arg = '' f.array_return_arg = '' f.array_const = '' f.decode_left = "%s.%s = " % (m.name_lower, f.name) f.decode_right = '' f.get_arg = '' f.c_test_value = f.test_value f.return_type = f.type # cope with uint8_t_mavlink_version for m in xml.message: m.arg_fields = [] m.array_fields = [] m.scalar_fields = [] for f in m.ordered_fields: if f.array_length != 0: m.array_fields.append(f) else: m.scalar_fields.append(f) for f in m.fields: if not f.omit_arg: m.arg_fields.append(f) f.putname = f.name else: f.putname = f.const_value # generate_mavlink_h(directory, xml) # generate_version_h(directory, xml) # generate_main_h(directory, xml) for m in xml.message: generate_message_h(directory, m)