xref: /btstack/tool/btstack_rtos_generator.py (revision c3d71bb2b911bd599f3afcdec63599104f5ecfd3)
15c544019SMatthias Ringwald#!/usr/bin/env python3
21dd2ed97SMatthias Ringwaldimport os, sys, getopt, re, pickle
31dd2ed97SMatthias Ringwald
41dd2ed97SMatthias Ringwaldcopyright = """/*
51dd2ed97SMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH
61dd2ed97SMatthias Ringwald *
71dd2ed97SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
81dd2ed97SMatthias Ringwald * modification, are permitted provided that the following conditions
91dd2ed97SMatthias Ringwald * are met:
101dd2ed97SMatthias Ringwald *
111dd2ed97SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
121dd2ed97SMatthias Ringwald *    notice, this list of conditions and the following disclaimer.
131dd2ed97SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
141dd2ed97SMatthias Ringwald *    notice, this list of conditions and the following disclaimer in the
151dd2ed97SMatthias Ringwald *    documentation and/or other materials provided with the distribution.
161dd2ed97SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
171dd2ed97SMatthias Ringwald *    contributors may be used to endorse or promote products derived
181dd2ed97SMatthias Ringwald *    from this software without specific prior written permission.
191dd2ed97SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
201dd2ed97SMatthias Ringwald *    personal benefit and not for any commercial purpose or for
211dd2ed97SMatthias Ringwald *    monetary gain.
221dd2ed97SMatthias Ringwald *
231dd2ed97SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
241dd2ed97SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
251dd2ed97SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
261dd2ed97SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
271dd2ed97SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
281dd2ed97SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
291dd2ed97SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
301dd2ed97SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
311dd2ed97SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
321dd2ed97SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
331dd2ed97SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341dd2ed97SMatthias Ringwald * SUCH DAMAGE.
351dd2ed97SMatthias Ringwald *
361dd2ed97SMatthias Ringwald * Please inquire about commercial licensing options at
371dd2ed97SMatthias Ringwald * [email protected]
381dd2ed97SMatthias Ringwald *
391dd2ed97SMatthias Ringwald */
401dd2ed97SMatthias Ringwald"""
411dd2ed97SMatthias Ringwald
426769b4c3SMatthias Ringwaldsingle_hfile_header_begin = """
431dd2ed97SMatthias Ringwald
441dd2ed97SMatthias Ringwald/*
451dd2ed97SMatthias Ringwald *  btstack_rtos.h
461dd2ed97SMatthias Ringwald *
471dd2ed97SMatthias Ringwald *  @brief BTstack Wrapper for use with Real-Time OS
481dd2ed97SMatthias Ringwald *         Wraps each public BTstack function into a thread-safe version
491dd2ed97SMatthias Ringwald *
501dd2ed97SMatthias Ringwald *  @note  Don't edit - generated by tool/btstack_rtos_generator.py
511dd2ed97SMatthias Ringwald *
521dd2ed97SMatthias Ringwald */
531dd2ed97SMatthias Ringwald
5480e33422SMatthias Ringwald#ifndef BTSTACK_RTOS_H
5580e33422SMatthias Ringwald#define BTSTACK_RTOS_H
561dd2ed97SMatthias Ringwald
571dd2ed97SMatthias Ringwald#if defined __cplusplus
581dd2ed97SMatthias Ringwaldextern "C" {
591dd2ed97SMatthias Ringwald#endif
601dd2ed97SMatthias Ringwald
611dd2ed97SMatthias Ringwald#include "btstack_config.h"
621dd2ed97SMatthias Ringwald
631dd2ed97SMatthias Ringwald#ifndef BTSTACK_RTOS_ENTER
641dd2ed97SMatthias Ringwald#error Please define BTSTACK_RTOS_ENTER that locks a recursive mutex when using the RTOS wrapper btstack_rtos.h
651dd2ed97SMatthias Ringwald#endif
661dd2ed97SMatthias Ringwald
671dd2ed97SMatthias Ringwald#ifndef BTSTACK_RTOS_EXIT
681dd2ed97SMatthias Ringwald#error Please define BTSTACK_RTOS_EXIT that releases a recursive mutex when using the RTOS wrapper btstack_rtos.h
691dd2ed97SMatthias Ringwald#endif
701dd2ed97SMatthias Ringwald
711dd2ed97SMatthias Ringwald/* API_START */
721dd2ed97SMatthias Ringwald
731dd2ed97SMatthias Ringwald
741dd2ed97SMatthias Ringwald"""
751dd2ed97SMatthias Ringwald
766769b4c3SMatthias Ringwaldsingle_hfile_api_header = """
771dd2ed97SMatthias Ringwald#include "API_NAME"
781dd2ed97SMatthias Ringwald"""
791dd2ed97SMatthias Ringwald
806769b4c3SMatthias Ringwaldsingle_hfile_header_end = """
811dd2ed97SMatthias Ringwald
821dd2ed97SMatthias Ringwald/* API_END */
831dd2ed97SMatthias Ringwald
841dd2ed97SMatthias Ringwald#if defined __cplusplus
851dd2ed97SMatthias Ringwald}
861dd2ed97SMatthias Ringwald#endif
871dd2ed97SMatthias Ringwald
8880e33422SMatthias Ringwald#endif // BTSTACK_RTOS_H
891dd2ed97SMatthias Ringwald"""
901dd2ed97SMatthias Ringwald
916769b4c3SMatthias Ringwaldmultiple_header_begin = """
926769b4c3SMatthias Ringwald
936769b4c3SMatthias Ringwald/*
946769b4c3SMatthias Ringwald *  FILENAME
956769b4c3SMatthias Ringwald *
966769b4c3SMatthias Ringwald *  @brief BTstack Wrapper for use with Real-Time OS
976769b4c3SMatthias Ringwald *         Wraps each public BTstack function into a thread-safe version
986769b4c3SMatthias Ringwald *
996769b4c3SMatthias Ringwald *  @note  Don't edit - generated by tool/btstack_rtos_generator.py
1006769b4c3SMatthias Ringwald *
1016769b4c3SMatthias Ringwald */
1026769b4c3SMatthias Ringwald
1036769b4c3SMatthias Ringwald#ifndef GUARD
1046769b4c3SMatthias Ringwald#define GUARD
1056769b4c3SMatthias Ringwald
1066769b4c3SMatthias Ringwald#if defined __cplusplus
1076769b4c3SMatthias Ringwaldextern "C" {
1086769b4c3SMatthias Ringwald#endif
1096769b4c3SMatthias Ringwald
1106769b4c3SMatthias Ringwald#include "btstack_config.h"
1116769b4c3SMatthias Ringwald#include "HEADER"
1126769b4c3SMatthias Ringwald
1136769b4c3SMatthias Ringwald#ifndef BTSTACK_RTOS_ENTER
1146769b4c3SMatthias Ringwald#error Please define BTSTACK_RTOS_ENTER that locks a recursive mutex when using the RTOS wrapper btstack_rtos.h
1156769b4c3SMatthias Ringwald#endif
1166769b4c3SMatthias Ringwald
1176769b4c3SMatthias Ringwald#ifndef BTSTACK_RTOS_EXIT
1186769b4c3SMatthias Ringwald#error Please define BTSTACK_RTOS_EXIT that releases a recursive mutex when using the RTOS wrapper btstack_rtos.h
1196769b4c3SMatthias Ringwald#endif
1206769b4c3SMatthias Ringwald
1216769b4c3SMatthias Ringwald/* API_START */
1226769b4c3SMatthias Ringwald
1236769b4c3SMatthias Ringwald
1246769b4c3SMatthias Ringwald"""
1256769b4c3SMatthias Ringwald
1266769b4c3SMatthias Ringwaldmultiple_header_end = """
1276769b4c3SMatthias Ringwald
1286769b4c3SMatthias Ringwald/* API_END */
1296769b4c3SMatthias Ringwald
1306769b4c3SMatthias Ringwald#if defined __cplusplus
1316769b4c3SMatthias Ringwald}
1326769b4c3SMatthias Ringwald#endif
1336769b4c3SMatthias Ringwald
1346769b4c3SMatthias Ringwald#endif // GUARD
1356769b4c3SMatthias Ringwald"""
1366769b4c3SMatthias Ringwald
1371dd2ed97SMatthias Ringwaldclass State:
1381dd2ed97SMatthias Ringwald    SearchStartAPI = 0
1391dd2ed97SMatthias Ringwald    SearchEndAPI = 1
1401dd2ed97SMatthias Ringwald    DoneAPI = 2
1411dd2ed97SMatthias Ringwald
1421dd2ed97SMatthias Ringwald
1431dd2ed97SMatthias Ringwaldnum_functions = 0
1441dd2ed97SMatthias Ringwald
1451dd2ed97SMatthias Ringwald# [file_name, api_title, api_label]
1461dd2ed97SMatthias Ringwaldapis = [
147*c3d71bb2SMatthias Ringwald    ["src/ble/gatt-service/ancs_client.h", "BLE ANCS Client", "ancsClient", True],
1481dd2ed97SMatthias Ringwald    ["src/ble/att_db_util.h", "BLE ATT Database", "attDb", True],
1491dd2ed97SMatthias Ringwald    ["src/ble/att_server.h", "BLE ATT Server", "attServer", True],
1501dd2ed97SMatthias Ringwald    ["src/ble/gatt_client.h", "BLE GATT Client", "gattClient", True],
1511dd2ed97SMatthias Ringwald    ["src/ble/le_device_db.h", "BLE Device Database", "leDeviceDb", True],
1521dd2ed97SMatthias Ringwald    ["src/ble/sm.h", "BLE Security Manager", "sm", True],
1531dd2ed97SMatthias Ringwald
1541dd2ed97SMatthias Ringwald    ["src/classic/bnep.h", "BNEP", "bnep", True],
1551dd2ed97SMatthias Ringwald    ["src/classic/btstack_link_key_db.h","Link Key DB","lkDb", True],
1561dd2ed97SMatthias Ringwald    ["src/classic/hsp_hs.h","HSP Headset","hspHS", True],
1571dd2ed97SMatthias Ringwald    ["src/classic/hsp_ag.h","HSP Audio Gateway","hspAG", True],
1581dd2ed97SMatthias Ringwald    ["src/classic/hfp_hf.h","HFP Hands-Free","hfpHF", True],
1591dd2ed97SMatthias Ringwald    ["src/classic/hfp_ag.h","HFP Audio Gateway","hfpAG", True],
1601dd2ed97SMatthias Ringwald    ["src/classic/pan.h", "PAN", "pan", True],
1611dd2ed97SMatthias Ringwald    ["src/classic/rfcomm.h", "RFCOMM", "rfcomm", True],
1621dd2ed97SMatthias Ringwald    ["src/classic/sdp_client.h", "SDP Client", "sdpClient", True],
1631dd2ed97SMatthias Ringwald    ["src/classic/sdp_client_rfcomm.h", "SDP RFCOMM Query", "sdpQueries", True],
1641dd2ed97SMatthias Ringwald    ["src/classic/sdp_server.h", "SDP Server", "sdpSrv", True],
1651dd2ed97SMatthias Ringwald    ["src/classic/sdp_util.h","SDP Utils", "sdpUtil", True],
1661dd2ed97SMatthias Ringwald
1676769b4c3SMatthias Ringwald    ["src/ad_parser.h", "BLE Advertisements Parser", "advParser", False],
1686769b4c3SMatthias Ringwald    # ["src/btstack_chipset.h","BTstack Chipset","btMemory", True],
1696769b4c3SMatthias Ringwald    # ["src/btstack_control.h","BTstack Hardware Control","btControl", True],
1701dd2ed97SMatthias Ringwald    ["src/btstack_event.h","HCI Event Getter","btEvent", False],
1716769b4c3SMatthias Ringwald    # ["src/btstack_memory.h","BTstack Memory Management","btMemory", True],
1726769b4c3SMatthias Ringwald    ["src/btstack_linked_list.h","BTstack Linked List","btList", False],
1731dd2ed97SMatthias Ringwald    ["src/btstack_run_loop.h", "Run Loop", "runLoop", True],
1746769b4c3SMatthias Ringwald    ["src/btstack_util.h", "Common Utils", "btUtil", False],
1751dd2ed97SMatthias Ringwald    ["src/gap.h", "GAP", "gap", True],
1761dd2ed97SMatthias Ringwald    ["src/hci.h", "HCI", "hci", True],
1771dd2ed97SMatthias Ringwald    ["src/hci_dump.h","HCI Logging","hciTrace", True],
1786769b4c3SMatthias Ringwald    # ["src/hci_transport.h","HCI Transport","hciTransport", True],
1791dd2ed97SMatthias Ringwald    ["src/l2cap.h", "L2CAP", "l2cap", True],
1801dd2ed97SMatthias Ringwald]
1811dd2ed97SMatthias Ringwald
1821dd2ed97SMatthias Ringwalddef split_arguments(args_string):
1831dd2ed97SMatthias Ringwald    args = []
1841dd2ed97SMatthias Ringwald    brace_level = 0
1851dd2ed97SMatthias Ringwald    arg = ''
1861dd2ed97SMatthias Ringwald    for c in args_string:
1871dd2ed97SMatthias Ringwald        if c == '(':
1881dd2ed97SMatthias Ringwald            brace_level += 1
1891dd2ed97SMatthias Ringwald        if c == ')':
1901dd2ed97SMatthias Ringwald            brace_level -= 1
1911dd2ed97SMatthias Ringwald        if c == ',' and brace_level == 0:
1921dd2ed97SMatthias Ringwald            args.append(arg)
1931dd2ed97SMatthias Ringwald            arg = ''
1941dd2ed97SMatthias Ringwald            continue
1951dd2ed97SMatthias Ringwald        arg = arg + c
1961dd2ed97SMatthias Ringwald    if len(arg):
1971dd2ed97SMatthias Ringwald        args.append(arg)
1981dd2ed97SMatthias Ringwald    return args
1991dd2ed97SMatthias Ringwald
2001dd2ed97SMatthias Ringwalddef argument_name(parameter):
201*c3d71bb2SMatthias Ringwald    function_pointer = re.match(r'[\w\s\*]*\(\s*\*(\w*)\s*\)\(.*\)', parameter)
2021dd2ed97SMatthias Ringwald    if function_pointer:
2031dd2ed97SMatthias Ringwald        return function_pointer.group(1)
2041dd2ed97SMatthias Ringwald    parts = parameter.split(' ')
2051dd2ed97SMatthias Ringwald    filtered_parts = [part for part in parts if part not in ['']]
2061dd2ed97SMatthias Ringwald    arg = filtered_parts[len(filtered_parts)-1].replace('*','').replace('[]','')
2071dd2ed97SMatthias Ringwald    # le_device_db_encryption_set(index, ediv, rand[8], ltk, key_size, authenticated, authorized);
2081dd2ed97SMatthias Ringwald    if arg == 'rand[8]':
2091dd2ed97SMatthias Ringwald        arg = 'rand'
2101dd2ed97SMatthias Ringwald    return arg
2111dd2ed97SMatthias Ringwald
2121dd2ed97SMatthias Ringwalddef create_wrapper(fout, type_and_name, arg_string, need_lock):
2131dd2ed97SMatthias Ringwald    global num_functions
2141dd2ed97SMatthias Ringwald
2151dd2ed97SMatthias Ringwald    parts = type_and_name.split(' ')
2161dd2ed97SMatthias Ringwald    filtered_parts = [part for part in parts if part not in ['static','inline','']]
2171dd2ed97SMatthias Ringwald    name = filtered_parts[len(filtered_parts)-1]
2181dd2ed97SMatthias Ringwald    return_type = ' '.join(filtered_parts[:-1])
2191dd2ed97SMatthias Ringwald    # handle *function_name
2201dd2ed97SMatthias Ringwald    if name.startswith('*'):
2211dd2ed97SMatthias Ringwald        name = name[1:]
2221dd2ed97SMatthias Ringwald        return_type = return_type + ' *'
2231dd2ed97SMatthias Ringwald    rtos_name = "rtos_" + name
2241dd2ed97SMatthias Ringwald    is_void_function = len(filtered_parts) == 2 and filtered_parts[0] == "void"
2251dd2ed97SMatthias Ringwald    args = split_arguments(arg_string)
2261dd2ed97SMatthias Ringwald    call = []
2271dd2ed97SMatthias Ringwald    is_ellipse_function = False
2281dd2ed97SMatthias Ringwald    if len(args)!= 1 or args[0] != 'void':
2291dd2ed97SMatthias Ringwald        for arg in args:
2301dd2ed97SMatthias Ringwald            call_arg = argument_name(arg)
2311dd2ed97SMatthias Ringwald            if call_arg == '...':
2321dd2ed97SMatthias Ringwald                is_ellipse_function = True
2331dd2ed97SMatthias Ringwald                call.append('argptr')
2341dd2ed97SMatthias Ringwald                name += '_va_arg'
2351dd2ed97SMatthias Ringwald            else:
2361dd2ed97SMatthias Ringwald                call.append(argument_name(arg))
2371dd2ed97SMatthias Ringwald    call_args = ', '.join(call)
2381dd2ed97SMatthias Ringwald    fout.write('static inline ' + return_type + ' ' + rtos_name + '(' + ", ".join(args) + '){\n')
2391dd2ed97SMatthias Ringwald    orig_call = name + '(' + call_args + ')'
2401dd2ed97SMatthias Ringwald    if need_lock:
2411dd2ed97SMatthias Ringwald        fout.write('    BTSTACK_RTOS_ENTER();\n')
2421dd2ed97SMatthias Ringwald        if is_ellipse_function:
2431dd2ed97SMatthias Ringwald            fout.write('    va_list argptr;\n')
2441dd2ed97SMatthias Ringwald            fout.write('    va_start(argptr, %s);\n' % call[-2])
2451dd2ed97SMatthias Ringwald        if is_void_function:
2461dd2ed97SMatthias Ringwald            fout.write('    ' + orig_call+';\n')
2471dd2ed97SMatthias Ringwald        else:
2481dd2ed97SMatthias Ringwald            fout.write('    ' + return_type + ' res = ' + orig_call + ';\n')
2491dd2ed97SMatthias Ringwald        if is_ellipse_function:
2501dd2ed97SMatthias Ringwald            fout.write('    va_end(argptr);\n')
2511dd2ed97SMatthias Ringwald        fout.write('    BTSTACK_RTOS_EXIT();\n')
2521dd2ed97SMatthias Ringwald        if not is_void_function:
2531dd2ed97SMatthias Ringwald            fout.write('    return res;\n')
2541dd2ed97SMatthias Ringwald    else:
2551dd2ed97SMatthias Ringwald        if is_void_function:
2561dd2ed97SMatthias Ringwald            fout.write('    ' + orig_call+';\n')
2571dd2ed97SMatthias Ringwald        else:
2581dd2ed97SMatthias Ringwald            fout.write('    return ' + orig_call + ';\n')
2591dd2ed97SMatthias Ringwald    fout.write('}\n')
2601dd2ed97SMatthias Ringwald    fout.write('\n')
2611dd2ed97SMatthias Ringwald    num_functions += 1
2621dd2ed97SMatthias Ringwald
2636769b4c3SMatthias Ringwalddef write_wrappers_for_file(fout, file, header_name, need_lock):
2643e90877dSMilanka Ringwald    with open(file, 'r') as fin:
2651dd2ed97SMatthias Ringwald        typedefFound = 0
2661dd2ed97SMatthias Ringwald        multiline_function_def = 0
2671dd2ed97SMatthias Ringwald        multiline = ''
2681dd2ed97SMatthias Ringwald        multiline_comment = 0
2691dd2ed97SMatthias Ringwald        inline_function = 0
2701dd2ed97SMatthias Ringwald        state = State.SearchStartAPI
2711dd2ed97SMatthias Ringwald
2721dd2ed97SMatthias Ringwald        for line in fin:
2731dd2ed97SMatthias Ringwald            if state == State.DoneAPI:
2741dd2ed97SMatthias Ringwald                continue
2751dd2ed97SMatthias Ringwald
2761dd2ed97SMatthias Ringwald            if state == State.SearchStartAPI:
277*c3d71bb2SMatthias Ringwald                parts = re.match(r'.*API_START.*',line)
2781dd2ed97SMatthias Ringwald                if parts:
2791dd2ed97SMatthias Ringwald                    state = State.SearchEndAPI
2801dd2ed97SMatthias Ringwald                continue
2811dd2ed97SMatthias Ringwald
2821dd2ed97SMatthias Ringwald            if state == State.SearchEndAPI:
283*c3d71bb2SMatthias Ringwald                parts = re.match(r'.*API_END.*',line)
2841dd2ed97SMatthias Ringwald                if parts:
2851dd2ed97SMatthias Ringwald                    state = State.DoneAPI
2861dd2ed97SMatthias Ringwald                    continue
2871dd2ed97SMatthias Ringwald
2881dd2ed97SMatthias Ringwald            if inline_function:
289*c3d71bb2SMatthias Ringwald                function_end = re.match(r'.*}.*', line)
2901dd2ed97SMatthias Ringwald                if function_end:
2911dd2ed97SMatthias Ringwald                    inline_function = 0
2921dd2ed97SMatthias Ringwald                continue
2931dd2ed97SMatthias Ringwald
2941dd2ed97SMatthias Ringwald            if multiline_function_def:
2951dd2ed97SMatthias Ringwald                multiline += line
296*c3d71bb2SMatthias Ringwald                function_end = re.match(r'.*\)', line)
2971dd2ed97SMatthias Ringwald                if function_end:
2981dd2ed97SMatthias Ringwald                    multiline_function_def = 0
299*c3d71bb2SMatthias Ringwald                    function = re.match(r'([\w\s\*]*)\(([\w\s,\*]*)\).*', multiline)
3001dd2ed97SMatthias Ringwald                    if function:
3011dd2ed97SMatthias Ringwald                        type_and_name = function.group(1)
3021dd2ed97SMatthias Ringwald                        arg_string    = function.group(2)
3031dd2ed97SMatthias Ringwald                        create_wrapper(fout, type_and_name, arg_string, need_lock)
3041dd2ed97SMatthias Ringwald                continue
3051dd2ed97SMatthias Ringwald
3061dd2ed97SMatthias Ringwald            if multiline_comment:
307*c3d71bb2SMatthias Ringwald                comment_end = re.match(r'.*\*/.*', line)
3081dd2ed97SMatthias Ringwald                if comment_end:
3091dd2ed97SMatthias Ringwald                    multiline_comment = 0
3101dd2ed97SMatthias Ringwald                fout.write(line)
3111dd2ed97SMatthias Ringwald                continue
3121dd2ed97SMatthias Ringwald
3131dd2ed97SMatthias Ringwald            # search typedef struct end
3141dd2ed97SMatthias Ringwald            if typedefFound:
315*c3d71bb2SMatthias Ringwald                typedef = re.match(r'}\s*(.*);\n', line)
3161dd2ed97SMatthias Ringwald                if typedef:
3171dd2ed97SMatthias Ringwald                    typedefFound = 0
3181dd2ed97SMatthias Ringwald                continue
3191dd2ed97SMatthias Ringwald
3201dd2ed97SMatthias Ringwald            # search comment line
321*c3d71bb2SMatthias Ringwald            comment = re.match(r'.*/\*.*\*/.*', line)
3221dd2ed97SMatthias Ringwald            if comment:
3231dd2ed97SMatthias Ringwald                fout.write(line)
3241dd2ed97SMatthias Ringwald                continue
3251dd2ed97SMatthias Ringwald
3261dd2ed97SMatthias Ringwald            # search start of multi line comment
327*c3d71bb2SMatthias Ringwald            comment = re.match(r'.*/\*', line)
3281dd2ed97SMatthias Ringwald            if comment:
3291dd2ed97SMatthias Ringwald                fout.write(line)
3301dd2ed97SMatthias Ringwald                multiline_comment = 1
3311dd2ed97SMatthias Ringwald                continue
3321dd2ed97SMatthias Ringwald
3331dd2ed97SMatthias Ringwald            # ignore __attribute__ for hci_dump_log in src/hci_dump.h
334*c3d71bb2SMatthias Ringwald            param = re.match(r'.*__attribute__', line)
3351dd2ed97SMatthias Ringwald            if param:
3361dd2ed97SMatthias Ringwald                continue
3371dd2ed97SMatthias Ringwald
3381dd2ed97SMatthias Ringwald            # search typedef struct begin
339*c3d71bb2SMatthias Ringwald            typedef =  re.match(r'.*typedef\s+struct.*', line)
3401dd2ed97SMatthias Ringwald            if typedef:
3411dd2ed97SMatthias Ringwald                typedefFound = 1
3421dd2ed97SMatthias Ringwald
3431dd2ed97SMatthias Ringwald            # complete function declaration
344*c3d71bb2SMatthias Ringwald            function = re.match(r'([\w\s\*]*)\((.*)\).*', line)
3451dd2ed97SMatthias Ringwald            if function:
3461dd2ed97SMatthias Ringwald                if "return" in line:
3471dd2ed97SMatthias Ringwald                    continue
3481dd2ed97SMatthias Ringwald                type_and_name = function.group(1)
3491dd2ed97SMatthias Ringwald                arg_string    = function.group(2)
3501dd2ed97SMatthias Ringwald                create_wrapper(fout, type_and_name, arg_string, need_lock)
3511dd2ed97SMatthias Ringwald                inline_function = 'inline' in line;
3521dd2ed97SMatthias Ringwald                continue
3531dd2ed97SMatthias Ringwald
3541dd2ed97SMatthias Ringwald            # multi-line function declaration
355*c3d71bb2SMatthias Ringwald            function = re.match(r'([\w\s\*]*)\((.*).*', line)
3561dd2ed97SMatthias Ringwald            if function:
3571dd2ed97SMatthias Ringwald                multiline = line
3581dd2ed97SMatthias Ringwald                multiline_function_def = 1
3591dd2ed97SMatthias Ringwald                continue
3601dd2ed97SMatthias Ringwald
3616769b4c3SMatthias Ringwald    # fout.write(single_hfile_header_begin)
3626769b4c3SMatthias Ringwald
3636769b4c3SMatthias Ringwald
3646769b4c3SMatthias Ringwalddef create_wrapper_file(btstack_root, apis, wrapper_file):
3656769b4c3SMatthias Ringwald    with open(wrapper_file, 'w') as fout:
3666769b4c3SMatthias Ringwald        fout.write(copyright)
3676769b4c3SMatthias Ringwald        fout.write(single_hfile_header_begin)
3686769b4c3SMatthias Ringwald
3696769b4c3SMatthias Ringwald        for api_tuple in apis:
3706769b4c3SMatthias Ringwald            api_filename = btstack_root + "/" + api_tuple[0]
3716769b4c3SMatthias Ringwald            need_lock = api_tuple[3]
3726769b4c3SMatthias Ringwald            header_file = api_tuple[0].replace('src/','')
3736769b4c3SMatthias Ringwald            fout.write(single_hfile_api_header.replace("API_NAME", header_file))
3746769b4c3SMatthias Ringwald            write_wrappers_for_file(fout, api_filename, header_file, need_lock)
3756769b4c3SMatthias Ringwald            # fout.write(single_hfile_header_begin)
3766769b4c3SMatthias Ringwald        fout.write(single_hfile_header_end)
3776769b4c3SMatthias Ringwald
3786769b4c3SMatthias Ringwalddef create_wrapper_files(btstack_root, rtos_folder, apis):
3796769b4c3SMatthias Ringwald    for api_tuple in apis:
3806769b4c3SMatthias Ringwald        api_filename = btstack_root + "/" + api_tuple[0]
3816769b4c3SMatthias Ringwald        need_lock = api_tuple[3]
3826769b4c3SMatthias Ringwald        header_file = api_tuple[0].replace('src/','')
3836769b4c3SMatthias Ringwald        path_parts = header_file.split('/')
3846769b4c3SMatthias Ringwald        path_parts[-1] = 'rtos_' + path_parts[-1]
3856769b4c3SMatthias Ringwald        rtos_file = '/'.join(path_parts)
3866769b4c3SMatthias Ringwald        wrapper_file = rtos_folder + '/' + rtos_file
3876769b4c3SMatthias Ringwald        # print('- %s' % wrapper_file)
3886769b4c3SMatthias Ringwald        with open(wrapper_file, 'w') as fout:
3896769b4c3SMatthias Ringwald            guard = '__' + rtos_file.replace('.','_').upper()
3906769b4c3SMatthias Ringwald            fout.write(copyright)
3916769b4c3SMatthias Ringwald            fout.write(multiple_header_begin.replace('FILENAME',rtos_file).replace('GUARD',guard).replace('HEADER',header_file))
3926769b4c3SMatthias Ringwald            write_wrappers_for_file(fout, api_filename, header_file, need_lock)
3936769b4c3SMatthias Ringwald            fout.write(multiple_header_end.replace('GUARD',guard))
3946769b4c3SMatthias Ringwald
3956769b4c3SMatthias Ringwalddef assert_dir_exists(path):
3966769b4c3SMatthias Ringwald    if not os.path.exists(path):
3976769b4c3SMatthias Ringwald        os.makedirs(path)
3981dd2ed97SMatthias Ringwald
3991dd2ed97SMatthias Ringwalddef main(argv):
4001dd2ed97SMatthias Ringwald    btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
4011dd2ed97SMatthias Ringwald    print ('BTstack folder is: %s' % btstack_root)
4026769b4c3SMatthias Ringwald
4036769b4c3SMatthias Ringwald    # single file
4046769b4c3SMatthias Ringwald    # gen_path = btstack_root + '/src/btstack_rtos.h'
4056769b4c3SMatthias Ringwald    # print ('Generating RTOS wrapper %s' % gen_path)
4066769b4c3SMatthias Ringwald    # create_wrapper_file(btstack_root, apis, gen_path)
4076769b4c3SMatthias Ringwald
4086769b4c3SMatthias Ringwald    # individual files in platform/rtos
4096769b4c3SMatthias Ringwald    print ('Generating RTOS wrappers...')
4106769b4c3SMatthias Ringwald    rtos_folder = btstack_root + '/platform/rtos'
4116769b4c3SMatthias Ringwald    assert_dir_exists(rtos_folder)
4126769b4c3SMatthias Ringwald    assert_dir_exists(rtos_folder+'/ble')
413*c3d71bb2SMatthias Ringwald    assert_dir_exists(rtos_folder+'/ble/gatt-service')
4146769b4c3SMatthias Ringwald    assert_dir_exists(rtos_folder+'/classic')
4156769b4c3SMatthias Ringwald    create_wrapper_files(btstack_root, rtos_folder, apis)
4166769b4c3SMatthias Ringwald
4176769b4c3SMatthias Ringwald    # summary
4181dd2ed97SMatthias Ringwald    print ('Number wrapped headers: %u' % len(apis))
4191dd2ed97SMatthias Ringwald    print ('Number wrapped functions: %u' % num_functions)
4206769b4c3SMatthias Ringwald
4211dd2ed97SMatthias Ringwaldif __name__ == "__main__":
4221dd2ed97SMatthias Ringwald   main(sys.argv[1:])
423