예제 #1
0
def generate(output, xml):
    '''generate complete PPRZLink C implemenation from a XML messages file'''

    directory, name = os.path.split(output)


    print("Generating C implementation in %s" % output)
    if directory != '':
        pprz_parse.mkdir_p(directory)

    # add some extra field attributes for convenience with arrays
    for m in xml.message:
        offset = 2 # 2 bytes initial offset (sender id, message id)
        for f in m.fields:
            if f.array_type == 'VariableArray':
                f.attrib_macro = 'nb_%s, %s' % (f.field_name, f.field_name)
                f.attrib_fun = 'uint8_t nb_%s, %s *_%s' % (f.field_name, f.type, f.field_name)
                f.attrib_fun_unused = 'uint8_t nb_%s __attribute__((unused)), %s *_%s __attribute__((unused))' % (f.field_name, f.type, f.field_name)
                f.array_byte = 'trans->put_bytes(trans->impl, dev, _FD, DL_TYPE_ARRAY_LENGTH, DL_FORMAT_SCALAR, (void *) &nb_%s, 1);\n    ' % f.field_name
                f.read_type = f.type+'_array'
                if (offset + 1) % min(4, int(f.type_length)) == 0: # data are aligned
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_uint8_t(_payload, %d)\n' % (m.msg_name, f.field_name, offset)
                else: # rely on arch capability to read or not
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_len_aligned(_payload, %d)\n' % (m.msg_name, f.field_name, offset)
                offset += 1
                # variable arrays are last (for now)
                f.offset = offset
                f.dl_format = 'DL_FORMAT_ARRAY'
            elif f.array_type == 'FixedArray':
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type+'_array'
                if offset % min(4, int(f.type_length)) == 0: # data are aligned
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) (%d)\n' % (m.msg_name, f.field_name, int(f.array_length))
                else: # rely on arch capability to read or not
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_fixed_len_aligned(%d)\n' % (m.msg_name, f.field_name, int(f.array_length))
                f.offset = offset
                offset += int(f.length)
                f.dl_format = 'DL_FORMAT_ARRAY'
            else:
                f.offset = offset
                offset += int(f.length)
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type
                f.read_array_byte = ''
                f.dl_format = 'DL_FORMAT_SCALAR'

    generate_messages_h(directory, name, xml)
    copy_fixed_headers(directory, xml.protocol_version)
예제 #2
0
def generate(output, xml):
    '''generate complete PPRZLink C implemenation from a XML messages file'''

    directory, name = os.path.split(output)


    print("Generating C implementation in %s" % output)
    if directory != '':
        pprz_parse.mkdir_p(directory)

    # add some extra field attributes for convenience with arrays
    for m in xml.message:
        offset = 2 # 2 bytes initial offset (sender id, message id)
        for f in m.fields:
            if f.array_type == 'VariableArray':
                f.attrib_macro = 'nb_%s, %s' % (f.field_name, f.field_name)
                f.attrib_fun = 'uint8_t nb_%s, %s *_%s' % (f.field_name, f.type, f.field_name)
                f.attrib_fun_unused = 'uint8_t nb_%s __attribute__((unused)), %s *_%s __attribute__((unused))' % (f.field_name, f.type, f.field_name)
                f.array_byte = 'trans->put_bytes(trans->impl, dev, _FD, DL_TYPE_ARRAY_LENGTH, DL_FORMAT_SCALAR, (void *) &nb_%s, 1);\n    ' % f.field_name
                f.read_type = f.type+'_array'
                if (offset + 1) % min(4, int(f.type_length)) == 0: # data are aligned
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_uint8_t(_payload, %d)\n' % (m.msg_name, f.field_name, offset)
                else: # rely on arch capability to read or not
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_len_aligned(_payload, %d)\n' % (m.msg_name, f.field_name, offset)
                offset += 1
                # variable arrays are last (for now)
                f.offset = offset
                f.dl_format = 'DL_FORMAT_ARRAY'
            elif f.array_type == 'FixedArray':
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type+'_array'
                if offset % min(4, int(f.type_length)) == 0: # data are aligned
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) (%d)\n' % (m.msg_name, f.field_name, int(f.array_length))
                else: # rely on arch capability to read or not
                    f.read_array_byte = '#define DL_%s_%s_length(_payload) _PPRZ_VAL_fixed_len_aligned(%d)\n' % (m.msg_name, f.field_name, int(f.array_length))
                f.offset = offset
                offset += int(eval(f.length))
                f.dl_format = 'DL_FORMAT_ARRAY'
            else:
                f.offset = offset
                offset += int(f.length)
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type
                f.read_array_byte = ''
                f.dl_format = 'DL_FORMAT_SCALAR'

    generate_messages_h(directory, name, xml)
    copy_fixed_headers(directory, xml.protocol_version)
예제 #3
0
def generate(output, xml, msg_list):
    '''
    generate PPRZLINK C standalone implemenation

    single header to send / read a limited set of messages
    '''
    # filter messages
    msg_filtered = []
    for m in xml.message:
        if m.msg_name in msg_list.split(','):
            msg_filtered.append(m)
    xml.message = msg_filtered
    print(xml)

    directory, name = os.path.split(output)
    print("Generating C standalone implementation in %s" % output)
    if directory != '':
        pprz_parse.mkdir_p(directory)

    # add some extra field attributes for convenience with arrays
    for m in xml.message:
        offset = 4  # 4 bytes initial offset (sender id, receiver id, component and class id, message id)
        for f in m.fields:
            if f.array_type == 'VariableArray':
                f.attrib_fun = 'uint8_t nb_%s, %s *_%s' % (
                    f.field_name, f.type, f.field_name)
                f.attrib_fun_unused = 'uint8_t nb_%s __attribute__((unused)), %s *_%s __attribute__((unused))' % (
                    f.field_name, f.type, f.field_name)
                f.array_byte = 'pprzlink_put_bytes(dev, (uint8_t *) &nb_%s, 1);\n    ' % f.field_name
                f.read_type = f.type + '_array'
                f.return_type = f.type + ' *'
                if (offset + 1) % min(4, int(
                        f.type_length)) == 0:  # data are aligned
                    f.fun_read_array_byte = 'static inline uint8_t pprzlink_get_%s_%s_length(void* _payload) {\n  return _PPRZ_VAL_uint8_t(_payload, %d);\n}\n' \
                                        % (m.msg_name, f.field_name ,offset)
                else:  # rely on arch capability to read or not
                    f.fun_read_array_byte = 'static inline uint8_t pprzlink_get_%s_%s_length(void * _payload __attribute__ ((unused)))  {\n  return _PPRZ_VAL_len_aligned(_payload, %d);\n}\n' \
                                        % (m.msg_name, f.field_name ,offset)
                offset += 1
                # variable arrays are last (for now)
                f.offset = offset
            elif f.array_type == 'FixedArray':
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (
                    f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type + '_array'
                f.return_type = f.type + ' *'
                if offset % min(4, int(
                        f.type_length)) == 0:  # data are aligned
                    f.fun_read_array_byte = 'static inline uint8_t pprzlink_get_%s_%s_length(void* _payload __attribute__ ((unused))) {\n  return %d;\n}\n' \
                                            % (m.msg_name, f.field_name ,int(f.array_length))
                else:  # rely on arch capability to read or not
                    f.fun_read_array_byte = 'static inline uint8_t pprzlink_get_%s_%s_length(void* _payload __attribute__ ((unused))) {\n  return _PPRZ_VAL_fixed_len_aligned(%d);\n}\n' \
                                            % (m.msg_name, f.field_name ,int(f.array_length))
                f.offset = offset
                offset += int(eval(f.length))
            else:
                f.offset = offset
                offset += int(f.length)
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (
                    f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type
                f.return_type = f.type
                f.fun_read_array_byte = ''

    generate_main_h(directory, name, xml)
    copy_fixed_headers(directory, xml.protocol_version)
예제 #4
0
def generate(output, xml):
    '''generate complete MAVLink C implemenation'''

    directory, name = os.path.split(output)
    print("Generating C implementation in %s" % output)
    if directory != '':
        pprz_parse.mkdir_p(directory)
        pprz_parse.mkdir_p(os.path.join(directory, xml.class_name))

    # add some extra field attributes for convenience with arrays
    for m in xml.message:
        offset = 4  # 4 bytes initial offset (sender id, receiver id, component and class id, message id)
        for f in m.fields:
            if f.array_type == 'VariableArray':
                f.attrib_macro = 'nb_%s, %s' % (f.field_name, f.field_name)
                f.attrib_param = 'nb_%s,_%s' % (f.field_name, f.field_name)
                f.attrib_fun = 'uint8_t nb_%s, %s *_%s' % (
                    f.field_name, f.type, f.field_name)
                f.attrib_fun_unused = 'uint8_t nb_%s __attribute__((unused)), %s *_%s __attribute__((unused))' % (
                    f.field_name, f.type, f.field_name)
                f.array_byte = 'msg->trans->put_bytes(msg, _FD, DL_TYPE_ARRAY_LENGTH, DL_FORMAT_SCALAR, (void *) &nb_%s, 1);\n    ' % f.field_name
                f.read_type = f.type + '_array'
                f.return_type = f.type + ' *'
                if (offset + 1) % min(4, int(
                        f.type_length)) == 0:  # data are aligned
                    f.fun_read_array_byte = '/** Getter for length of array %s in message %s\n *\n * @return %s : %s\n */\n static inline uint8_t pprzlink_get_%s_%s_length(void* _payload) {\n    return _PPRZ_VAL_uint8_t(_payload, %d);\n}\n' \
                                        % (f.field_name,m.msg_name, f.field_name, f.description, m.msg_name, f.field_name ,offset)
                else:  # rely on arch capability to read or not
                    f.fun_read_array_byte = '/** Getter for length of array %s in message %s\n *\n * @return %s : %s\n */\n static inline uint8_t pprzlink_get_%s_%s_length(__attribute__ ((unused)) void* _payload) {\n    return _PPRZ_VAL_len_aligned(_payload, %d);\n}\n' \
                                        % (f.field_name,m.msg_name, f.field_name, f.description, m.msg_name, f.field_name ,offset)
                f.read_array_byte = '#define DL_%s_%s_length(_payload) pprzlink_get_%s_%s_length(_payload)\n' % (
                    m.msg_name, f.field_name, m.msg_name, f.field_name)
                offset += 1
                # variable arrays are last (for now)
                f.offset = offset
                f.dl_format = 'DL_FORMAT_ARRAY'
            elif f.array_type == 'FixedArray':
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_param = '_%s' % (f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (
                    f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type + '_array'
                f.return_type = f.type + ' *'
                if offset % min(4, int(
                        f.type_length)) == 0:  # data are aligned
                    f.fun_read_array_byte = '/** Getter for length of array %s in message %s\n *\n * @return %s : %s\n */\n static inline uint8_t pprzlink_get_%s_%s_length(void* _payload __attribute__ ((unused))) {\n    return %d;\n}\n' \
                                            % (f.field_name,m.msg_name, f.field_name, f.description, m.msg_name, f.field_name ,int(f.array_length))
                else:  # rely on arch capability to read or not
                    f.fun_read_array_byte = '/** Getter for length of array %s in message %s\n *\n * @return %s : %s\n */\n static inline uint8_t pprzlink_get_%s_%s_length(void* _payload __attribute__ ((unused))) {\n    return _PPRZ_VAL_fixed_len_aligned(%d);\n}\n' \
                                            % (f.field_name,m.msg_name, f.field_name, f.description, m.msg_name, f.field_name ,int(f.array_length))
                f.read_array_byte = '#define DL_%s_%s_length(_payload) pprzlink_get_%s_%s_length(_payload)\n' % (
                    m.msg_name, f.field_name, m.msg_name, f.field_name)
                f.offset = offset
                offset += int(eval(f.length))
                f.dl_format = 'DL_FORMAT_ARRAY'
            else:
                f.offset = offset
                offset += int(f.length)
                f.attrib_macro = '%s' % f.field_name
                f.attrib_fun = '%s *_%s' % (f.type, f.field_name)
                f.attrib_param = '_%s' % (f.field_name)
                f.attrib_fun_unused = '%s *_%s __attribute__((unused))' % (
                    f.type, f.field_name)
                f.array_byte = ''
                f.read_type = f.type
                f.return_type = f.type
                f.read_array_byte = ''
                f.fun_read_array_byte = ''
                f.dl_format = 'DL_FORMAT_SCALAR'
        generate_one(directory, xml, m)

    generate_main_h(directory, name, xml)
    copy_fixed_headers(directory, xml.protocol_version)