xref: /btstack/tool/btstack_rtos_generator.py (revision 1dd2ed97dc83220636e5a39c9b81638e62ff2141)
1*1dd2ed97SMatthias Ringwald#!/usr/bin/env python
2*1dd2ed97SMatthias Ringwaldimport os, sys, getopt, re, pickle
3*1dd2ed97SMatthias Ringwald
4*1dd2ed97SMatthias Ringwaldcopyright = """/*
5*1dd2ed97SMatthias Ringwald * Copyright (C) 2016 BlueKitchen GmbH
6*1dd2ed97SMatthias Ringwald *
7*1dd2ed97SMatthias Ringwald * Redistribution and use in source and binary forms, with or without
8*1dd2ed97SMatthias Ringwald * modification, are permitted provided that the following conditions
9*1dd2ed97SMatthias Ringwald * are met:
10*1dd2ed97SMatthias Ringwald *
11*1dd2ed97SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
12*1dd2ed97SMatthias Ringwald *    notice, this list of conditions and the following disclaimer.
13*1dd2ed97SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
14*1dd2ed97SMatthias Ringwald *    notice, this list of conditions and the following disclaimer in the
15*1dd2ed97SMatthias Ringwald *    documentation and/or other materials provided with the distribution.
16*1dd2ed97SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
17*1dd2ed97SMatthias Ringwald *    contributors may be used to endorse or promote products derived
18*1dd2ed97SMatthias Ringwald *    from this software without specific prior written permission.
19*1dd2ed97SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
20*1dd2ed97SMatthias Ringwald *    personal benefit and not for any commercial purpose or for
21*1dd2ed97SMatthias Ringwald *    monetary gain.
22*1dd2ed97SMatthias Ringwald *
23*1dd2ed97SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
24*1dd2ed97SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*1dd2ed97SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26*1dd2ed97SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
27*1dd2ed97SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28*1dd2ed97SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29*1dd2ed97SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30*1dd2ed97SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31*1dd2ed97SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32*1dd2ed97SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
33*1dd2ed97SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34*1dd2ed97SMatthias Ringwald * SUCH DAMAGE.
35*1dd2ed97SMatthias Ringwald *
36*1dd2ed97SMatthias Ringwald * Please inquire about commercial licensing options at
37*1dd2ed97SMatthias Ringwald * [email protected]
38*1dd2ed97SMatthias Ringwald *
39*1dd2ed97SMatthias Ringwald */
40*1dd2ed97SMatthias Ringwald"""
41*1dd2ed97SMatthias Ringwald
42*1dd2ed97SMatthias Ringwaldhfile_header_begin = """
43*1dd2ed97SMatthias Ringwald
44*1dd2ed97SMatthias Ringwald/*
45*1dd2ed97SMatthias Ringwald *  btstack_rtos.h
46*1dd2ed97SMatthias Ringwald *
47*1dd2ed97SMatthias Ringwald *  @brief BTstack Wrapper for use with Real-Time OS
48*1dd2ed97SMatthias Ringwald *         Wraps each public BTstack function into a thread-safe version
49*1dd2ed97SMatthias Ringwald *
50*1dd2ed97SMatthias Ringwald *  @note  Don't edit - generated by tool/btstack_rtos_generator.py
51*1dd2ed97SMatthias Ringwald *
52*1dd2ed97SMatthias Ringwald */
53*1dd2ed97SMatthias Ringwald
54*1dd2ed97SMatthias Ringwald#ifndef __BTSTACK_RTOS_H
55*1dd2ed97SMatthias Ringwald#define __BTSTACK_RTOS_H
56*1dd2ed97SMatthias Ringwald
57*1dd2ed97SMatthias Ringwald#if defined __cplusplus
58*1dd2ed97SMatthias Ringwaldextern "C" {
59*1dd2ed97SMatthias Ringwald#endif
60*1dd2ed97SMatthias Ringwald
61*1dd2ed97SMatthias Ringwald#include "btstack_config.h"
62*1dd2ed97SMatthias Ringwald
63*1dd2ed97SMatthias Ringwald#ifndef BTSTACK_RTOS_ENTER
64*1dd2ed97SMatthias Ringwald#error Please define BTSTACK_RTOS_ENTER that locks a recursive mutex when using the RTOS wrapper btstack_rtos.h
65*1dd2ed97SMatthias Ringwald#endif
66*1dd2ed97SMatthias Ringwald
67*1dd2ed97SMatthias Ringwald#ifndef BTSTACK_RTOS_EXIT
68*1dd2ed97SMatthias Ringwald#error Please define BTSTACK_RTOS_EXIT that releases a recursive mutex when using the RTOS wrapper btstack_rtos.h
69*1dd2ed97SMatthias Ringwald#endif
70*1dd2ed97SMatthias Ringwald
71*1dd2ed97SMatthias Ringwald/* API_START */
72*1dd2ed97SMatthias Ringwald
73*1dd2ed97SMatthias Ringwald
74*1dd2ed97SMatthias Ringwald"""
75*1dd2ed97SMatthias Ringwald
76*1dd2ed97SMatthias Ringwaldhfile_api_header = """
77*1dd2ed97SMatthias Ringwald#include "API_NAME"
78*1dd2ed97SMatthias Ringwald"""
79*1dd2ed97SMatthias Ringwald
80*1dd2ed97SMatthias Ringwaldhfile_header_end = """
81*1dd2ed97SMatthias Ringwald
82*1dd2ed97SMatthias Ringwald/* API_END */
83*1dd2ed97SMatthias Ringwald
84*1dd2ed97SMatthias Ringwald#if defined __cplusplus
85*1dd2ed97SMatthias Ringwald}
86*1dd2ed97SMatthias Ringwald#endif
87*1dd2ed97SMatthias Ringwald
88*1dd2ed97SMatthias Ringwald#endif // __BTSTACK_RTOS_H
89*1dd2ed97SMatthias Ringwald"""
90*1dd2ed97SMatthias Ringwald
91*1dd2ed97SMatthias Ringwaldclass State:
92*1dd2ed97SMatthias Ringwald    SearchStartAPI = 0
93*1dd2ed97SMatthias Ringwald    SearchEndAPI = 1
94*1dd2ed97SMatthias Ringwald    DoneAPI = 2
95*1dd2ed97SMatthias Ringwald
96*1dd2ed97SMatthias Ringwald
97*1dd2ed97SMatthias Ringwaldnum_functions = 0
98*1dd2ed97SMatthias Ringwald
99*1dd2ed97SMatthias Ringwald# [file_name, api_title, api_label]
100*1dd2ed97SMatthias Ringwaldapis = [
101*1dd2ed97SMatthias Ringwald    ["src/ble/ancs_client.h", "BLE ANCS Client", "ancsClient", True],
102*1dd2ed97SMatthias Ringwald    ["src/ble/att_db_util.h", "BLE ATT Database", "attDb", True],
103*1dd2ed97SMatthias Ringwald    ["src/ble/att_server.h", "BLE ATT Server", "attServer", True],
104*1dd2ed97SMatthias Ringwald    ["src/ble/gatt_client.h", "BLE GATT Client", "gattClient", True],
105*1dd2ed97SMatthias Ringwald    ["src/ble/le_device_db.h", "BLE Device Database", "leDeviceDb", True],
106*1dd2ed97SMatthias Ringwald    ["src/ble/sm.h", "BLE Security Manager", "sm", True],
107*1dd2ed97SMatthias Ringwald
108*1dd2ed97SMatthias Ringwald    ["src/classic/bnep.h", "BNEP", "bnep", True],
109*1dd2ed97SMatthias Ringwald    ["src/classic/btstack_link_key_db.h","Link Key DB","lkDb", True],
110*1dd2ed97SMatthias Ringwald    ["src/classic/hsp_hs.h","HSP Headset","hspHS", True],
111*1dd2ed97SMatthias Ringwald    ["src/classic/hsp_ag.h","HSP Audio Gateway","hspAG", True],
112*1dd2ed97SMatthias Ringwald    ["src/classic/hfp_hf.h","HFP Hands-Free","hfpHF", True],
113*1dd2ed97SMatthias Ringwald    ["src/classic/hfp_ag.h","HFP Audio Gateway","hfpAG", True],
114*1dd2ed97SMatthias Ringwald    ["src/classic/pan.h", "PAN", "pan", True],
115*1dd2ed97SMatthias Ringwald    ["src/classic/rfcomm.h", "RFCOMM", "rfcomm", True],
116*1dd2ed97SMatthias Ringwald    ["src/classic/sdp_client.h", "SDP Client", "sdpClient", True],
117*1dd2ed97SMatthias Ringwald    ["src/classic/sdp_client_rfcomm.h", "SDP RFCOMM Query", "sdpQueries", True],
118*1dd2ed97SMatthias Ringwald    ["src/classic/sdp_server.h", "SDP Server", "sdpSrv", True],
119*1dd2ed97SMatthias Ringwald    ["src/classic/sdp_util.h","SDP Utils", "sdpUtil", True],
120*1dd2ed97SMatthias Ringwald
121*1dd2ed97SMatthias Ringwald    ["src/ad_parser.h", "BLE Advertisements Parser", "advParser", True],
122*1dd2ed97SMatthias Ringwald    ["src/btstack_chipset.h","BTstack Chipset","btMemory", True],
123*1dd2ed97SMatthias Ringwald    ["src/btstack_control.h","BTstack Hardware Control","btControl", True],
124*1dd2ed97SMatthias Ringwald    ["src/btstack_event.h","HCI Event Getter","btEvent", False],
125*1dd2ed97SMatthias Ringwald    ["src/btstack_memory.h","BTstack Memory Management","btMemory", True],
126*1dd2ed97SMatthias Ringwald    ["src/btstack_linked_list.h","BTstack Linked List","btList", True],
127*1dd2ed97SMatthias Ringwald    ["src/btstack_run_loop.h", "Run Loop", "runLoop", True],
128*1dd2ed97SMatthias Ringwald    ["src/btstack_util.h", "Common Utils", "btUtil", True],
129*1dd2ed97SMatthias Ringwald    ["src/gap.h", "GAP", "gap", True],
130*1dd2ed97SMatthias Ringwald    ["src/hci.h", "HCI", "hci", True],
131*1dd2ed97SMatthias Ringwald    ["src/hci_dump.h","HCI Logging","hciTrace", True],
132*1dd2ed97SMatthias Ringwald    ["src/hci_transport.h","HCI Transport","hciTransport", True],
133*1dd2ed97SMatthias Ringwald    ["src/l2cap.h", "L2CAP", "l2cap", True],
134*1dd2ed97SMatthias Ringwald]
135*1dd2ed97SMatthias Ringwald
136*1dd2ed97SMatthias Ringwald
137*1dd2ed97SMatthias Ringwalddef codeReference(fname, filepath, linenr):
138*1dd2ed97SMatthias Ringwald    return fname
139*1dd2ed97SMatthias Ringwald
140*1dd2ed97SMatthias Ringwalddef split_arguments(args_string):
141*1dd2ed97SMatthias Ringwald    args = []
142*1dd2ed97SMatthias Ringwald    brace_level = 0
143*1dd2ed97SMatthias Ringwald    arg = ''
144*1dd2ed97SMatthias Ringwald    for c in args_string:
145*1dd2ed97SMatthias Ringwald        if c == '(':
146*1dd2ed97SMatthias Ringwald            brace_level += 1
147*1dd2ed97SMatthias Ringwald        if c == ')':
148*1dd2ed97SMatthias Ringwald            brace_level -= 1
149*1dd2ed97SMatthias Ringwald        if c == ',' and brace_level == 0:
150*1dd2ed97SMatthias Ringwald            args.append(arg)
151*1dd2ed97SMatthias Ringwald            arg = ''
152*1dd2ed97SMatthias Ringwald            continue
153*1dd2ed97SMatthias Ringwald        arg = arg + c
154*1dd2ed97SMatthias Ringwald    if len(arg):
155*1dd2ed97SMatthias Ringwald        args.append(arg)
156*1dd2ed97SMatthias Ringwald    return args
157*1dd2ed97SMatthias Ringwald
158*1dd2ed97SMatthias Ringwalddef argument_name(parameter):
159*1dd2ed97SMatthias Ringwald    function_pointer = re.match('[\w\s\*]*\(\s*\*(\w*)\s*\)\(.*\)', parameter)
160*1dd2ed97SMatthias Ringwald    if function_pointer:
161*1dd2ed97SMatthias Ringwald        return function_pointer.group(1)
162*1dd2ed97SMatthias Ringwald    parts = parameter.split(' ')
163*1dd2ed97SMatthias Ringwald    filtered_parts = [part for part in parts if part not in ['']]
164*1dd2ed97SMatthias Ringwald    arg = filtered_parts[len(filtered_parts)-1].replace('*','').replace('[]','')
165*1dd2ed97SMatthias Ringwald    # le_device_db_encryption_set(index, ediv, rand[8], ltk, key_size, authenticated, authorized);
166*1dd2ed97SMatthias Ringwald    if arg == 'rand[8]':
167*1dd2ed97SMatthias Ringwald        arg = 'rand'
168*1dd2ed97SMatthias Ringwald    return arg
169*1dd2ed97SMatthias Ringwald
170*1dd2ed97SMatthias Ringwalddef create_wrapper(fout, type_and_name, arg_string, need_lock):
171*1dd2ed97SMatthias Ringwald    global num_functions
172*1dd2ed97SMatthias Ringwald
173*1dd2ed97SMatthias Ringwald    parts = type_and_name.split(' ')
174*1dd2ed97SMatthias Ringwald    filtered_parts = [part for part in parts if part not in ['static','inline','']]
175*1dd2ed97SMatthias Ringwald    name = filtered_parts[len(filtered_parts)-1]
176*1dd2ed97SMatthias Ringwald    return_type = ' '.join(filtered_parts[:-1])
177*1dd2ed97SMatthias Ringwald    # handle *function_name
178*1dd2ed97SMatthias Ringwald    if name.startswith('*'):
179*1dd2ed97SMatthias Ringwald        name = name[1:]
180*1dd2ed97SMatthias Ringwald        return_type = return_type + ' *'
181*1dd2ed97SMatthias Ringwald    rtos_name = "rtos_" + name
182*1dd2ed97SMatthias Ringwald    is_void_function = len(filtered_parts) == 2 and filtered_parts[0] == "void"
183*1dd2ed97SMatthias Ringwald    args = split_arguments(arg_string)
184*1dd2ed97SMatthias Ringwald    call = []
185*1dd2ed97SMatthias Ringwald    is_ellipse_function = False
186*1dd2ed97SMatthias Ringwald    if len(args)!= 1 or args[0] != 'void':
187*1dd2ed97SMatthias Ringwald        for arg in args:
188*1dd2ed97SMatthias Ringwald            call_arg = argument_name(arg)
189*1dd2ed97SMatthias Ringwald            if call_arg == '...':
190*1dd2ed97SMatthias Ringwald                is_ellipse_function = True
191*1dd2ed97SMatthias Ringwald                call.append('argptr')
192*1dd2ed97SMatthias Ringwald                name += '_va_arg'
193*1dd2ed97SMatthias Ringwald            else:
194*1dd2ed97SMatthias Ringwald                call.append(argument_name(arg))
195*1dd2ed97SMatthias Ringwald    call_args = ', '.join(call)
196*1dd2ed97SMatthias Ringwald    fout.write('static inline ' + return_type + ' ' + rtos_name + '(' + ", ".join(args) + '){\n')
197*1dd2ed97SMatthias Ringwald    orig_call = name + '(' + call_args + ')'
198*1dd2ed97SMatthias Ringwald    if need_lock:
199*1dd2ed97SMatthias Ringwald        fout.write('    BTSTACK_RTOS_ENTER();\n')
200*1dd2ed97SMatthias Ringwald        if is_ellipse_function:
201*1dd2ed97SMatthias Ringwald            fout.write('    va_list argptr;\n')
202*1dd2ed97SMatthias Ringwald            fout.write('    va_start(argptr, %s);\n' % call[-2])
203*1dd2ed97SMatthias Ringwald        if is_void_function:
204*1dd2ed97SMatthias Ringwald            fout.write('    ' + orig_call+';\n')
205*1dd2ed97SMatthias Ringwald        else:
206*1dd2ed97SMatthias Ringwald            fout.write('    ' + return_type + ' res = ' + orig_call + ';\n')
207*1dd2ed97SMatthias Ringwald        if is_ellipse_function:
208*1dd2ed97SMatthias Ringwald            fout.write('    va_end(argptr);\n')
209*1dd2ed97SMatthias Ringwald        fout.write('    BTSTACK_RTOS_EXIT();\n')
210*1dd2ed97SMatthias Ringwald        if not is_void_function:
211*1dd2ed97SMatthias Ringwald            fout.write('    return res;\n')
212*1dd2ed97SMatthias Ringwald    else:
213*1dd2ed97SMatthias Ringwald        if is_void_function:
214*1dd2ed97SMatthias Ringwald            fout.write('    ' + orig_call+';\n')
215*1dd2ed97SMatthias Ringwald        else:
216*1dd2ed97SMatthias Ringwald            fout.write('    return ' + orig_call + ';\n')
217*1dd2ed97SMatthias Ringwald
218*1dd2ed97SMatthias Ringwald    fout.write('}\n')
219*1dd2ed97SMatthias Ringwald    fout.write('\n')
220*1dd2ed97SMatthias Ringwald    num_functions += 1
221*1dd2ed97SMatthias Ringwald
222*1dd2ed97SMatthias Ringwalddef create_wrapper_file(btstackfolder, apis, wrapper_file):
223*1dd2ed97SMatthias Ringwald    with open(wrapper_file, 'w') as fout:
224*1dd2ed97SMatthias Ringwald        fout.write(copyright)
225*1dd2ed97SMatthias Ringwald        fout.write(hfile_header_begin)
226*1dd2ed97SMatthias Ringwald
227*1dd2ed97SMatthias Ringwald        for api_tuple in apis:
228*1dd2ed97SMatthias Ringwald            api_filename = btstackfolder + "/" + api_tuple[0]
229*1dd2ed97SMatthias Ringwald            api_title = api_tuple[1]
230*1dd2ed97SMatthias Ringwald            api_lable = api_tuple[2]
231*1dd2ed97SMatthias Ringwald            need_lock = api_tuple[3]
232*1dd2ed97SMatthias Ringwald
233*1dd2ed97SMatthias Ringwald            header_file = api_tuple[0].replace('src/','')
234*1dd2ed97SMatthias Ringwald            fout.write(hfile_api_header.replace("API_NAME", header_file))
235*1dd2ed97SMatthias Ringwald
236*1dd2ed97SMatthias Ringwald            with open(api_filename, 'rb') as fin:
237*1dd2ed97SMatthias Ringwald                typedefFound = 0
238*1dd2ed97SMatthias Ringwald                multiline_function_def = 0
239*1dd2ed97SMatthias Ringwald                multiline = ''
240*1dd2ed97SMatthias Ringwald                multiline_comment = 0
241*1dd2ed97SMatthias Ringwald                inline_function = 0
242*1dd2ed97SMatthias Ringwald                state = State.SearchStartAPI
243*1dd2ed97SMatthias Ringwald
244*1dd2ed97SMatthias Ringwald                for line in fin:
245*1dd2ed97SMatthias Ringwald                    if state == State.DoneAPI:
246*1dd2ed97SMatthias Ringwald                        continue
247*1dd2ed97SMatthias Ringwald
248*1dd2ed97SMatthias Ringwald                    if state == State.SearchStartAPI:
249*1dd2ed97SMatthias Ringwald                        parts = re.match('.*API_START.*',line)
250*1dd2ed97SMatthias Ringwald                        if parts:
251*1dd2ed97SMatthias Ringwald                            state = State.SearchEndAPI
252*1dd2ed97SMatthias Ringwald                        continue
253*1dd2ed97SMatthias Ringwald
254*1dd2ed97SMatthias Ringwald                    if state == State.SearchEndAPI:
255*1dd2ed97SMatthias Ringwald                        parts = re.match('.*API_END.*',line)
256*1dd2ed97SMatthias Ringwald                        if parts:
257*1dd2ed97SMatthias Ringwald                            state = State.DoneAPI
258*1dd2ed97SMatthias Ringwald                            continue
259*1dd2ed97SMatthias Ringwald
260*1dd2ed97SMatthias Ringwald                    if inline_function:
261*1dd2ed97SMatthias Ringwald                        function_end = re.match('.*}.*', line)
262*1dd2ed97SMatthias Ringwald                        if function_end:
263*1dd2ed97SMatthias Ringwald                            inline_function = 0
264*1dd2ed97SMatthias Ringwald                        continue
265*1dd2ed97SMatthias Ringwald
266*1dd2ed97SMatthias Ringwald                    if multiline_function_def:
267*1dd2ed97SMatthias Ringwald                        multiline += line
268*1dd2ed97SMatthias Ringwald                        function_end = re.match('.*\)', line)
269*1dd2ed97SMatthias Ringwald                        if function_end:
270*1dd2ed97SMatthias Ringwald                            multiline_function_def = 0
271*1dd2ed97SMatthias Ringwald                            function = re.match('([\w\s\*]*)\(([\w\s,\*]*)\).*', multiline)
272*1dd2ed97SMatthias Ringwald                            if function:
273*1dd2ed97SMatthias Ringwald                                type_and_name = function.group(1)
274*1dd2ed97SMatthias Ringwald                                arg_string    = function.group(2)
275*1dd2ed97SMatthias Ringwald                                create_wrapper(fout, type_and_name, arg_string, need_lock)
276*1dd2ed97SMatthias Ringwald                        continue
277*1dd2ed97SMatthias Ringwald
278*1dd2ed97SMatthias Ringwald                    if multiline_comment:
279*1dd2ed97SMatthias Ringwald                        comment_end = re.match('.*\*/.*', line)
280*1dd2ed97SMatthias Ringwald                        if comment_end:
281*1dd2ed97SMatthias Ringwald                            multiline_comment = 0
282*1dd2ed97SMatthias Ringwald                        fout.write(line)
283*1dd2ed97SMatthias Ringwald                        continue
284*1dd2ed97SMatthias Ringwald
285*1dd2ed97SMatthias Ringwald                    # search typedef struct end
286*1dd2ed97SMatthias Ringwald                    if typedefFound:
287*1dd2ed97SMatthias Ringwald                        typedef = re.match('}\s*(.*);\n', line)
288*1dd2ed97SMatthias Ringwald                        if typedef:
289*1dd2ed97SMatthias Ringwald                            typedefFound = 0
290*1dd2ed97SMatthias Ringwald                        continue
291*1dd2ed97SMatthias Ringwald
292*1dd2ed97SMatthias Ringwald                    # search comment line
293*1dd2ed97SMatthias Ringwald                    comment = re.match(".*/\*.*\*/.*", line)
294*1dd2ed97SMatthias Ringwald                    if comment:
295*1dd2ed97SMatthias Ringwald                        fout.write(line)
296*1dd2ed97SMatthias Ringwald                        continue
297*1dd2ed97SMatthias Ringwald
298*1dd2ed97SMatthias Ringwald                    # search start of multi line comment
299*1dd2ed97SMatthias Ringwald                    comment = re.match(".*/\*", line)
300*1dd2ed97SMatthias Ringwald                    if comment:
301*1dd2ed97SMatthias Ringwald                        fout.write(line)
302*1dd2ed97SMatthias Ringwald                        multiline_comment = 1
303*1dd2ed97SMatthias Ringwald                        continue
304*1dd2ed97SMatthias Ringwald
305*1dd2ed97SMatthias Ringwald                    # ignore __attribute__ for hci_dump_log in src/hci_dump.h
306*1dd2ed97SMatthias Ringwald                    param = re.match(".*__attribute__", line)
307*1dd2ed97SMatthias Ringwald                    if param:
308*1dd2ed97SMatthias Ringwald                        continue
309*1dd2ed97SMatthias Ringwald
310*1dd2ed97SMatthias Ringwald                    # search typedef struct begin
311*1dd2ed97SMatthias Ringwald                    typedef =  re.match('.*typedef\s+struct.*', line)
312*1dd2ed97SMatthias Ringwald                    if typedef:
313*1dd2ed97SMatthias Ringwald                        typedefFound = 1
314*1dd2ed97SMatthias Ringwald
315*1dd2ed97SMatthias Ringwald                    # complete function declaration
316*1dd2ed97SMatthias Ringwald                    function = re.match('([\w\s\*]*)\((.*)\).*', line)
317*1dd2ed97SMatthias Ringwald                    if function:
318*1dd2ed97SMatthias Ringwald                        if "return" in line:
319*1dd2ed97SMatthias Ringwald                            continue
320*1dd2ed97SMatthias Ringwald                        type_and_name = function.group(1)
321*1dd2ed97SMatthias Ringwald                        arg_string    = function.group(2)
322*1dd2ed97SMatthias Ringwald                        create_wrapper(fout, type_and_name, arg_string, need_lock)
323*1dd2ed97SMatthias Ringwald                        inline_function = 'inline' in line;
324*1dd2ed97SMatthias Ringwald                        continue
325*1dd2ed97SMatthias Ringwald
326*1dd2ed97SMatthias Ringwald                    # multi-line function declaration
327*1dd2ed97SMatthias Ringwald                    function = re.match('([\w\s\*]*)\((.*).*', line)
328*1dd2ed97SMatthias Ringwald                    if function:
329*1dd2ed97SMatthias Ringwald                        multiline = line
330*1dd2ed97SMatthias Ringwald                        multiline_function_def = 1
331*1dd2ed97SMatthias Ringwald                        continue
332*1dd2ed97SMatthias Ringwald
333*1dd2ed97SMatthias Ringwald            # fout.write(hfile_header_begin)
334*1dd2ed97SMatthias Ringwald        fout.write(hfile_header_end)
335*1dd2ed97SMatthias Ringwald
336*1dd2ed97SMatthias Ringwalddef main(argv):
337*1dd2ed97SMatthias Ringwald    btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
338*1dd2ed97SMatthias Ringwald    gen_path = btstack_root + '/src/btstack_rtos.h'
339*1dd2ed97SMatthias Ringwald    print ('BTstack folder is: %s' % btstack_root)
340*1dd2ed97SMatthias Ringwald    print ('Generating RTOS wrapper %s' % gen_path)
341*1dd2ed97SMatthias Ringwald    create_wrapper_file(btstack_root, apis, gen_path)
342*1dd2ed97SMatthias Ringwald    print ('Number wrapped headers: %u' % len(apis))
343*1dd2ed97SMatthias Ringwald    print ('Number wrapped functions: %u' % num_functions)
344*1dd2ed97SMatthias Ringwaldif __name__ == "__main__":
345*1dd2ed97SMatthias Ringwald   main(sys.argv[1:])
346