def make_method_declarations():
    func_version = """
/**
 * \ingroup {2}{1}
 *
 * Returns the API version (major, minor, release) of the bindings for this
 * device.
 */
int {0}_get_api_version({1} *{0}, uint8_t ret_api_version[3]);
"""
    func = """
/**
 * \ingroup {5}{2}
 *
 * {4}
 */
int {0}_{1}({2} *{0}{3});
"""

    a = device.get_underscore_name()
    c = device.get_camel_case_name()

    funcs = ''
    for packet in device.get_packets('function'):
        b = packet.get_underscore_name()
        d = c_common.make_parameter_list(packet)
        doc = format_doc(packet)

        funcs += func.format(a, b, c, d, doc, device.get_category())

    return func_version.format(a, c, device.get_category()) + funcs
def make_callbacks():
    param_format = {
    'en': """
 .. code-block:: c

  void callback({0})
""",
    'de': """
 .. code-block:: c

  void callback({0})
"""
    }

    cbs = ''
    func_start = '.. c:var:: '
    for packet in device.get_packets('callback'):
        plist = c_common.make_parameter_list(packet)[2:].replace('*ret_', '')
        if not plist:
            plist = 'void *user_data'
        else:
            plist += ', void *user_data'
        params = common.select_lang(param_format).format(plist)
        desc = format_doc(packet)
        name = '{0}_{1}'.format(device.get_upper_case_name(),
                                packet.get_upper_case_name())

        func = '{0}{1}_CALLBACK_{2}\n{3}\n{4}'.format(func_start,
                                                      device.get_upper_case_name(),
                                                      packet.get_upper_case_name(),
                                                      params,
                                                      desc)
        cbs += func + '\n'

    return cbs
def format_doc(packet):
    text = common.select_lang(packet.get_doc()[1])
    link = '{{@link {0}_{1}}}'
    link_c = '{{@link {0}_CALLBACK_{1}}}'

    # handle tables
    lines = text.split('\n')
    replaced_lines = []
    in_table_head = False
    in_table_body = False

    for line in lines:
        if line.strip() == '.. csv-table::':
            in_table_head = True
            replaced_lines.append('\\verbatim')
        elif line.strip().startswith(':header: ') and in_table_head:
            replaced_lines.append(line[len(':header: '):])
        elif line.strip().startswith(':widths:') and in_table_head:
            pass
        elif len(line.strip()) == 0 and in_table_head:
            in_table_head = False
            in_table_body = True

            replaced_lines.append('')
        elif len(line.strip()) == 0 and in_table_body:
            in_table_body = False

            replaced_lines.append('\\endverbatim')
            replaced_lines.append('')
        else:
            replaced_lines.append(line)

    text = '\n'.join(replaced_lines)

    for other_packet in device.get_packets():
        name_false = ':func:`{0}`'.format(other_packet.get_camel_case_name())
        if other_packet.get_type() == 'callback':
            name = other_packet.get_upper_case_name()
            name_right = link_c.format(device.get_upper_case_name(), name)
        else:
            name = other_packet.get_underscore_name()
            name_right = link.format(device.get_underscore_name(), name)

        text = text.replace(name_false, name_right)

    if packet.get_type() == 'callback':
        plist = c_common.make_parameter_list(packet)[2:].replace('*ret_', '')
        text = 'Signature: \code void callback({0}) \endcode\n'.format(plist) + text

    text = text.replace(":word:`parameter`", "parameter")
    text = text.replace(":word:`parameters`", "parameters")
    text = text.replace('.. note::', '\\note')
    text = text.replace('.. warning::', '\\warning')

    text = common.handle_rst_if(text, device)
    text = common.handle_since_firmware(text, device, packet)

    return '\n * '.join(text.strip().split('\n'))
def make_methods(typ):
    methods = ''
    func_start = '.. c:function:: int '
    for packet in device.get_packets('function'):
        if packet.get_doc()[0] != typ:
            continue
        name = '{0}_{1}'.format(device.get_underscore_name(), packet.get_underscore_name())
        plist = c_common.make_parameter_list(packet)
        params = '{0} *{1}{2}'.format(device.get_camel_case_name(), device.get_underscore_name(), plist)
        desc = format_doc(packet)
        func = '{0}{1}({2})\n{3}'.format(func_start, name, params, desc)
        methods += func + '\n'

    return methods
def make_method_funcs():
    def make_struct_list(packet):
        struct_list = ''
        needs_i = False
        for element in packet.get_elements('in'):
            sf = 'request'
            if element[1] == 'string':
                temp = '\n\tstrncpy({0}.{1}, {1}, {2});\n'
                struct_list += temp.format(sf, element[0], element[2])
            elif element[2] > 1:
                if common.get_type_size(element[1]) > 1:
                    needs_i = True
                    struct_list += '\n\tfor (i = 0; i < {3}; i++) {0}.{1}[i] = leconvert_{2}_to({1}[i]);' \
                                   .format(sf, element[0], element[1], element[2])
                else:
                    temp = '\n\tmemcpy({0}.{1}, {1}, {2} * sizeof({3}));'
                    struct_list += temp.format(sf,
                                               element[0],
                                               element[2],
                                               c_common.get_c_type(element[1], 'in', False))
            elif common.get_type_size(element[1]) > 1:
                struct_list += '\n\t{0}.{1} = leconvert_{2}_to({1});'.format(sf, element[0], element[1])
            else:
                struct_list += '\n\t{0}.{1} = {1};'.format(sf, element[0])
        return struct_list, needs_i

    def make_return_list(packet):
        return_list = ''
        needs_i = False
        for element in packet.get_elements('out'):
            sf = 'response'
            if element[1] == 'string':
                temp = '\tstrncpy(ret_{0}, {1}.{0}, {2});\n'
                return_list += temp.format(element[0], sf, element[2])
            elif element[2] > 1:
                if common.get_type_size(element[1]) > 1:
                    needs_i = True
                    return_list += '\tfor (i = 0; i < {3}; i++) ret_{0}[i] = leconvert_{2}_from({1}.{0}[i]);\n' \
                                   .format(element[0], sf, element[1], element[2])
                else:
                    temp = '\tmemcpy(ret_{0}, {1}.{0}, {2} * sizeof({3}));\n'
                    return_list += temp.format(element[0],
                                               sf,
                                               element[2],
                                               c_common.get_c_type(element[1], 'out', False))
            elif common.get_type_size(element[1]) > 1:
                return_list += '\t*ret_{0} = leconvert_{2}_from({1}.{0});\n'.format(element[0], sf, element[1])
            else:
                return_list += '\t*ret_{0} = {1}.{0};\n'.format(element[0], sf)
        return return_list, needs_i

    func_version = """
int {0}_get_api_version({1} *{0}, uint8_t ret_api_version[3]) {{
\treturn device_get_api_version({0}, ret_api_version);
}}
"""

    func = """
int {0}_{1}({2} *{0}{3}) {{
\t{5}_ request;{6}
\tint ret;{9}

\tret = packet_header_create(&request.header, sizeof(request), {4}, {0}->ipcon, {0});

\tif (ret < 0) {{
\t\treturn ret;
\t}}
{7}

\tret = device_send_request({0}, (Packet *)&request, {10});
{8}

\treturn ret;
}}
"""

    func_ret = """
\tif (ret < 0) {{
\t\treturn ret;
\t}}
{2}
"""

    device_name = device.get_underscore_name()
    c = device.get_camel_case_name()

    funcs = ''
    for packet in device.get_packets('function'):
        packet_name = packet.get_underscore_name()
        params = c_common.make_parameter_list(packet)
        fid = '{0}_FUNCTION_{1}'.format(device.get_upper_case_name(),
                                        packet.get_upper_case_name())
        f = packet.get_camel_case_name()
        h, needs_i = make_struct_list(packet)
        if len(packet.get_elements('out')) > 0:
            g = '\n\t' + f + 'Response_ response;'
            rl, needs_i2 = make_return_list(packet)
            i = func_ret.format(f, device_name, rl)
            r = '(Packet *)&response'
        else:
            g = ''
            i = ''
            needs_i2 = False
            r = 'NULL'
        if needs_i or needs_i2:
            k = '\n\tint i;'
        else:
            k = ''

        funcs += func.format(device_name, packet_name, c, params, fid, f, g, h, i, k, r)

    return func_version.format(device_name, c) + funcs