11dd2ed97SMatthias Ringwald#!/usr/bin/env python 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 42*6769b4c3SMatthias 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 541dd2ed97SMatthias Ringwald#ifndef __BTSTACK_RTOS_H 551dd2ed97SMatthias 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 76*6769b4c3SMatthias Ringwaldsingle_hfile_api_header = """ 771dd2ed97SMatthias Ringwald#include "API_NAME" 781dd2ed97SMatthias Ringwald""" 791dd2ed97SMatthias Ringwald 80*6769b4c3SMatthias Ringwaldsingle_hfile_header_end = """ 811dd2ed97SMatthias Ringwald 821dd2ed97SMatthias Ringwald/* API_END */ 831dd2ed97SMatthias Ringwald 841dd2ed97SMatthias Ringwald#if defined __cplusplus 851dd2ed97SMatthias Ringwald} 861dd2ed97SMatthias Ringwald#endif 871dd2ed97SMatthias Ringwald 881dd2ed97SMatthias Ringwald#endif // __BTSTACK_RTOS_H 891dd2ed97SMatthias Ringwald""" 901dd2ed97SMatthias Ringwald 91*6769b4c3SMatthias Ringwaldmultiple_header_begin = """ 92*6769b4c3SMatthias Ringwald 93*6769b4c3SMatthias Ringwald/* 94*6769b4c3SMatthias Ringwald * FILENAME 95*6769b4c3SMatthias Ringwald * 96*6769b4c3SMatthias Ringwald * @brief BTstack Wrapper for use with Real-Time OS 97*6769b4c3SMatthias Ringwald * Wraps each public BTstack function into a thread-safe version 98*6769b4c3SMatthias Ringwald * 99*6769b4c3SMatthias Ringwald * @note Don't edit - generated by tool/btstack_rtos_generator.py 100*6769b4c3SMatthias Ringwald * 101*6769b4c3SMatthias Ringwald */ 102*6769b4c3SMatthias Ringwald 103*6769b4c3SMatthias Ringwald#ifndef GUARD 104*6769b4c3SMatthias Ringwald#define GUARD 105*6769b4c3SMatthias Ringwald 106*6769b4c3SMatthias Ringwald#if defined __cplusplus 107*6769b4c3SMatthias Ringwaldextern "C" { 108*6769b4c3SMatthias Ringwald#endif 109*6769b4c3SMatthias Ringwald 110*6769b4c3SMatthias Ringwald#include "btstack_config.h" 111*6769b4c3SMatthias Ringwald#include "HEADER" 112*6769b4c3SMatthias Ringwald 113*6769b4c3SMatthias Ringwald#ifndef BTSTACK_RTOS_ENTER 114*6769b4c3SMatthias Ringwald#error Please define BTSTACK_RTOS_ENTER that locks a recursive mutex when using the RTOS wrapper btstack_rtos.h 115*6769b4c3SMatthias Ringwald#endif 116*6769b4c3SMatthias Ringwald 117*6769b4c3SMatthias Ringwald#ifndef BTSTACK_RTOS_EXIT 118*6769b4c3SMatthias Ringwald#error Please define BTSTACK_RTOS_EXIT that releases a recursive mutex when using the RTOS wrapper btstack_rtos.h 119*6769b4c3SMatthias Ringwald#endif 120*6769b4c3SMatthias Ringwald 121*6769b4c3SMatthias Ringwald/* API_START */ 122*6769b4c3SMatthias Ringwald 123*6769b4c3SMatthias Ringwald 124*6769b4c3SMatthias Ringwald""" 125*6769b4c3SMatthias Ringwald 126*6769b4c3SMatthias Ringwaldmultiple_header_end = """ 127*6769b4c3SMatthias Ringwald 128*6769b4c3SMatthias Ringwald/* API_END */ 129*6769b4c3SMatthias Ringwald 130*6769b4c3SMatthias Ringwald#if defined __cplusplus 131*6769b4c3SMatthias Ringwald} 132*6769b4c3SMatthias Ringwald#endif 133*6769b4c3SMatthias Ringwald 134*6769b4c3SMatthias Ringwald#endif // GUARD 135*6769b4c3SMatthias Ringwald""" 136*6769b4c3SMatthias 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 = [ 1471dd2ed97SMatthias Ringwald ["src/ble/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 167*6769b4c3SMatthias Ringwald ["src/ad_parser.h", "BLE Advertisements Parser", "advParser", False], 168*6769b4c3SMatthias Ringwald # ["src/btstack_chipset.h","BTstack Chipset","btMemory", True], 169*6769b4c3SMatthias Ringwald # ["src/btstack_control.h","BTstack Hardware Control","btControl", True], 1701dd2ed97SMatthias Ringwald ["src/btstack_event.h","HCI Event Getter","btEvent", False], 171*6769b4c3SMatthias Ringwald # ["src/btstack_memory.h","BTstack Memory Management","btMemory", True], 172*6769b4c3SMatthias Ringwald ["src/btstack_linked_list.h","BTstack Linked List","btList", False], 1731dd2ed97SMatthias Ringwald ["src/btstack_run_loop.h", "Run Loop", "runLoop", True], 174*6769b4c3SMatthias 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], 178*6769b4c3SMatthias 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): 2011dd2ed97SMatthias Ringwald function_pointer = re.match('[\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 263*6769b4c3SMatthias Ringwalddef write_wrappers_for_file(fout, file, header_name, need_lock): 264*6769b4c3SMatthias Ringwald with open(file, 'rb') 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: 2771dd2ed97SMatthias Ringwald parts = re.match('.*API_START.*',line) 2781dd2ed97SMatthias Ringwald if parts: 2791dd2ed97SMatthias Ringwald state = State.SearchEndAPI 2801dd2ed97SMatthias Ringwald continue 2811dd2ed97SMatthias Ringwald 2821dd2ed97SMatthias Ringwald if state == State.SearchEndAPI: 2831dd2ed97SMatthias Ringwald parts = re.match('.*API_END.*',line) 2841dd2ed97SMatthias Ringwald if parts: 2851dd2ed97SMatthias Ringwald state = State.DoneAPI 2861dd2ed97SMatthias Ringwald continue 2871dd2ed97SMatthias Ringwald 2881dd2ed97SMatthias Ringwald if inline_function: 2891dd2ed97SMatthias Ringwald function_end = re.match('.*}.*', 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 2961dd2ed97SMatthias Ringwald function_end = re.match('.*\)', line) 2971dd2ed97SMatthias Ringwald if function_end: 2981dd2ed97SMatthias Ringwald multiline_function_def = 0 2991dd2ed97SMatthias Ringwald function = re.match('([\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: 3071dd2ed97SMatthias Ringwald comment_end = re.match('.*\*/.*', 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: 3151dd2ed97SMatthias Ringwald typedef = re.match('}\s*(.*);\n', line) 3161dd2ed97SMatthias Ringwald if typedef: 3171dd2ed97SMatthias Ringwald typedefFound = 0 3181dd2ed97SMatthias Ringwald continue 3191dd2ed97SMatthias Ringwald 3201dd2ed97SMatthias Ringwald # search comment line 3211dd2ed97SMatthias Ringwald comment = re.match(".*/\*.*\*/.*", line) 3221dd2ed97SMatthias Ringwald if comment: 3231dd2ed97SMatthias Ringwald fout.write(line) 3241dd2ed97SMatthias Ringwald continue 3251dd2ed97SMatthias Ringwald 3261dd2ed97SMatthias Ringwald # search start of multi line comment 3271dd2ed97SMatthias Ringwald comment = re.match(".*/\*", 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 3341dd2ed97SMatthias Ringwald param = re.match(".*__attribute__", line) 3351dd2ed97SMatthias Ringwald if param: 3361dd2ed97SMatthias Ringwald continue 3371dd2ed97SMatthias Ringwald 3381dd2ed97SMatthias Ringwald # search typedef struct begin 3391dd2ed97SMatthias Ringwald typedef = re.match('.*typedef\s+struct.*', line) 3401dd2ed97SMatthias Ringwald if typedef: 3411dd2ed97SMatthias Ringwald typedefFound = 1 3421dd2ed97SMatthias Ringwald 3431dd2ed97SMatthias Ringwald # complete function declaration 3441dd2ed97SMatthias Ringwald function = re.match('([\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 3551dd2ed97SMatthias Ringwald function = re.match('([\w\s\*]*)\((.*).*', line) 3561dd2ed97SMatthias Ringwald if function: 3571dd2ed97SMatthias Ringwald multiline = line 3581dd2ed97SMatthias Ringwald multiline_function_def = 1 3591dd2ed97SMatthias Ringwald continue 3601dd2ed97SMatthias Ringwald 361*6769b4c3SMatthias Ringwald # fout.write(single_hfile_header_begin) 362*6769b4c3SMatthias Ringwald 363*6769b4c3SMatthias Ringwald 364*6769b4c3SMatthias Ringwalddef create_wrapper_file(btstack_root, apis, wrapper_file): 365*6769b4c3SMatthias Ringwald with open(wrapper_file, 'w') as fout: 366*6769b4c3SMatthias Ringwald fout.write(copyright) 367*6769b4c3SMatthias Ringwald fout.write(single_hfile_header_begin) 368*6769b4c3SMatthias Ringwald 369*6769b4c3SMatthias Ringwald for api_tuple in apis: 370*6769b4c3SMatthias Ringwald api_filename = btstack_root + "/" + api_tuple[0] 371*6769b4c3SMatthias Ringwald need_lock = api_tuple[3] 372*6769b4c3SMatthias Ringwald header_file = api_tuple[0].replace('src/','') 373*6769b4c3SMatthias Ringwald fout.write(single_hfile_api_header.replace("API_NAME", header_file)) 374*6769b4c3SMatthias Ringwald write_wrappers_for_file(fout, api_filename, header_file, need_lock) 375*6769b4c3SMatthias Ringwald # fout.write(single_hfile_header_begin) 376*6769b4c3SMatthias Ringwald fout.write(single_hfile_header_end) 377*6769b4c3SMatthias Ringwald 378*6769b4c3SMatthias Ringwalddef create_wrapper_files(btstack_root, rtos_folder, apis): 379*6769b4c3SMatthias Ringwald for api_tuple in apis: 380*6769b4c3SMatthias Ringwald api_filename = btstack_root + "/" + api_tuple[0] 381*6769b4c3SMatthias Ringwald need_lock = api_tuple[3] 382*6769b4c3SMatthias Ringwald header_file = api_tuple[0].replace('src/','') 383*6769b4c3SMatthias Ringwald path_parts = header_file.split('/') 384*6769b4c3SMatthias Ringwald path_parts[-1] = 'rtos_' + path_parts[-1] 385*6769b4c3SMatthias Ringwald rtos_file = '/'.join(path_parts) 386*6769b4c3SMatthias Ringwald wrapper_file = rtos_folder + '/' + rtos_file 387*6769b4c3SMatthias Ringwald # print('- %s' % wrapper_file) 388*6769b4c3SMatthias Ringwald with open(wrapper_file, 'w') as fout: 389*6769b4c3SMatthias Ringwald guard = '__' + rtos_file.replace('.','_').upper() 390*6769b4c3SMatthias Ringwald fout.write(copyright) 391*6769b4c3SMatthias Ringwald fout.write(multiple_header_begin.replace('FILENAME',rtos_file).replace('GUARD',guard).replace('HEADER',header_file)) 392*6769b4c3SMatthias Ringwald write_wrappers_for_file(fout, api_filename, header_file, need_lock) 393*6769b4c3SMatthias Ringwald fout.write(multiple_header_end.replace('GUARD',guard)) 394*6769b4c3SMatthias Ringwald 395*6769b4c3SMatthias Ringwalddef assert_dir_exists(path): 396*6769b4c3SMatthias Ringwald if not os.path.exists(path): 397*6769b4c3SMatthias 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) 402*6769b4c3SMatthias Ringwald 403*6769b4c3SMatthias Ringwald # single file 404*6769b4c3SMatthias Ringwald # gen_path = btstack_root + '/src/btstack_rtos.h' 405*6769b4c3SMatthias Ringwald # print ('Generating RTOS wrapper %s' % gen_path) 406*6769b4c3SMatthias Ringwald # create_wrapper_file(btstack_root, apis, gen_path) 407*6769b4c3SMatthias Ringwald 408*6769b4c3SMatthias Ringwald # individual files in platform/rtos 409*6769b4c3SMatthias Ringwald print ('Generating RTOS wrappers...') 410*6769b4c3SMatthias Ringwald rtos_folder = btstack_root + '/platform/rtos' 411*6769b4c3SMatthias Ringwald assert_dir_exists(rtos_folder) 412*6769b4c3SMatthias Ringwald assert_dir_exists(rtos_folder+'/ble') 413*6769b4c3SMatthias Ringwald assert_dir_exists(rtos_folder+'/classic') 414*6769b4c3SMatthias Ringwald create_wrapper_files(btstack_root, rtos_folder, apis) 415*6769b4c3SMatthias Ringwald 416*6769b4c3SMatthias Ringwald # summary 4171dd2ed97SMatthias Ringwald print ('Number wrapped headers: %u' % len(apis)) 4181dd2ed97SMatthias Ringwald print ('Number wrapped functions: %u' % num_functions) 419*6769b4c3SMatthias Ringwald 4201dd2ed97SMatthias Ringwaldif __name__ == "__main__": 4211dd2ed97SMatthias Ringwald main(sys.argv[1:]) 422