1*5c544019SMatthias Ringwald#!/usr/bin/env python3 2c0a711d9SMatthias Ringwaldimport os 3c0a711d9SMatthias Ringwaldimport sys 41ca3442bSMatthias Ringwald 5a2673d88SMatthias Ringwaldimport os 6a2673d88SMatthias Ringwaldimport sys 7a2673d88SMatthias Ringwald 81ca3442bSMatthias Ringwaldcopyright = """/* 91ca3442bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 101ca3442bSMatthias Ringwald * 111ca3442bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 121ca3442bSMatthias Ringwald * modification, are permitted provided that the following conditions 131ca3442bSMatthias Ringwald * are met: 141ca3442bSMatthias Ringwald * 151ca3442bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 161ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 171ca3442bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 181ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 191ca3442bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 201ca3442bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 211ca3442bSMatthias Ringwald * contributors may be used to endorse or promote products derived 221ca3442bSMatthias Ringwald * from this software without specific prior written permission. 231ca3442bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 241ca3442bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 251ca3442bSMatthias Ringwald * monetary gain. 261ca3442bSMatthias Ringwald * 271ca3442bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 281ca3442bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 291ca3442bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 301ca3442bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 311ca3442bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 321ca3442bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 331ca3442bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 341ca3442bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 351ca3442bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 361ca3442bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 371ca3442bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 381ca3442bSMatthias Ringwald * SUCH DAMAGE. 391ca3442bSMatthias Ringwald * 401ca3442bSMatthias Ringwald * Please inquire about commercial licensing options at 411ca3442bSMatthias Ringwald * [email protected] 421ca3442bSMatthias Ringwald * 431ca3442bSMatthias Ringwald */ 441ca3442bSMatthias Ringwald""" 451ca3442bSMatthias Ringwald 461ca3442bSMatthias Ringwaldhfile_header_begin = """ 471ca3442bSMatthias Ringwald 481ca3442bSMatthias Ringwald/* 491ca3442bSMatthias Ringwald * btstack_memory.h 501ca3442bSMatthias Ringwald * 511ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 521ca3442bSMatthias Ringwald * 531ca3442bSMatthias Ringwald */ 541ca3442bSMatthias Ringwald 5580e33422SMatthias Ringwald#ifndef BTSTACK_MEMORY_H 5680e33422SMatthias Ringwald#define BTSTACK_MEMORY_H 571ca3442bSMatthias Ringwald 581ca3442bSMatthias Ringwald#if defined __cplusplus 591ca3442bSMatthias Ringwaldextern "C" { 601ca3442bSMatthias Ringwald#endif 611ca3442bSMatthias Ringwald 627907f069SMatthias Ringwald#include "btstack_config.h" 631ca3442bSMatthias Ringwald 643edc84c5SMatthias Ringwald// Core 651ca3442bSMatthias Ringwald#include "hci.h" 661ca3442bSMatthias Ringwald#include "l2cap.h" 671ca3442bSMatthias Ringwald 683edc84c5SMatthias Ringwald// Classic 693edc84c5SMatthias Ringwald#include "classic/bnep.h" 703edc84c5SMatthias Ringwald#include "classic/hfp.h" 71a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db.h" 72a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db_memory.h" 73b3401248SMatthias Ringwald#include "classic/rfcomm.h" 74746ccb7eSMatthias Ringwald#include "classic/sdp_server.h" 75208d3378SMilanka Ringwald#include "classic/avdtp_sink.h" 76208d3378SMilanka Ringwald#include "classic/avdtp_source.h" 77be32e7f1SMilanka Ringwald#include "classic/avrcp.h" 783edc84c5SMatthias Ringwald 793edc84c5SMatthias Ringwald// BLE 80a9a4c409SMatthias Ringwald#ifdef ENABLE_BLE 813edc84c5SMatthias Ringwald#include "ble/gatt_client.h" 823edc84c5SMatthias Ringwald#include "ble/sm.h" 8344c5d856SMatthias Ringwald#endif 8444c5d856SMatthias Ringwald 8544c5d856SMatthias Ringwald#ifdef ENABLE_MESH 8677ba3d3fSMatthias Ringwald#include "mesh/mesh_network.h" 87a5a7b6daSMatthias Ringwald#include "mesh/mesh_keys.h" 88a5a7b6daSMatthias Ringwald#include "mesh/mesh_virtual_addresses.h" 891ca3442bSMatthias Ringwald#endif 901ca3442bSMatthias Ringwald 911ca3442bSMatthias Ringwald/* API_START */ 921ca3442bSMatthias Ringwald 931ca3442bSMatthias Ringwald/** 941ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools. 951ca3442bSMatthias Ringwald */ 961ca3442bSMatthias Ringwaldvoid btstack_memory_init(void); 971ca3442bSMatthias Ringwald 981ca3442bSMatthias Ringwald/* API_END */ 991ca3442bSMatthias Ringwald""" 1001ca3442bSMatthias Ringwald 1011ca3442bSMatthias Ringwaldhfile_header_end = """ 1021ca3442bSMatthias Ringwald#if defined __cplusplus 1031ca3442bSMatthias Ringwald} 1041ca3442bSMatthias Ringwald#endif 1051ca3442bSMatthias Ringwald 10680e33422SMatthias Ringwald#endif // BTSTACK_MEMORY_H 1071ca3442bSMatthias Ringwald""" 1081ca3442bSMatthias Ringwald 1091ca3442bSMatthias Ringwaldcfile_header_begin = """ 110*5c544019SMatthias Ringwald#define BTSTACK_FILE__ "btstack_memory.c" 111*5c544019SMatthias Ringwald 112*5c544019SMatthias Ringwald 1131ca3442bSMatthias Ringwald/* 114*5c544019SMatthias Ringwald * btstack_memory.c 1151ca3442bSMatthias Ringwald * 1161ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 1171ca3442bSMatthias Ringwald * 118a98592bcSMatthias Ringwald * @note code generated by tool/btstack_memory_generator.py 119a2673d88SMatthias Ringwald * @note returnes buffers are initialized with 0 1201ca3442bSMatthias Ringwald * 1211ca3442bSMatthias Ringwald */ 1221ca3442bSMatthias Ringwald 1231ca3442bSMatthias Ringwald#include "btstack_memory.h" 124d2e6c4b7SMatthias Ringwald#include "btstack_memory_pool.h" 1251ca3442bSMatthias Ringwald 1261ca3442bSMatthias Ringwald#include <stdlib.h> 1271ca3442bSMatthias Ringwald 1281ca3442bSMatthias Ringwald""" 1291ca3442bSMatthias Ringwald 1301ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 1311ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 1321ca3442bSMatthias Ringwald 1331ca3442bSMatthias Ringwaldcode_template = """ 1341ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE 135a265b909SMatthias Ringwald#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT) 136a265b909SMatthias Ringwald #if defined(POOL_COUNT_OLD_NO) 137a265b909SMatthias Ringwald #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT." 138a265b909SMatthias Ringwald #else 139a265b909SMatthias Ringwald #define POOL_COUNT 0 140a265b909SMatthias Ringwald #endif 141a265b909SMatthias Ringwald#endif 142a265b909SMatthias Ringwald 1431ca3442bSMatthias Ringwald#ifdef POOL_COUNT 1441ca3442bSMatthias Ringwald#if POOL_COUNT > 0 1451ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 14629d0c4f7SMatthias Ringwaldstatic btstack_memory_pool_t STRUCT_NAME_pool; 1471ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 148a2673d88SMatthias Ringwald void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool); 149a2673d88SMatthias Ringwald if (buffer){ 150a2673d88SMatthias Ringwald memset(buffer, 0, sizeof(STRUCT_TYPE)); 151a2673d88SMatthias Ringwald } 152a2673d88SMatthias Ringwald return (STRUCT_NAME_t *) buffer; 1531ca3442bSMatthias Ringwald} 1541ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 15529d0c4f7SMatthias Ringwald btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 1561ca3442bSMatthias Ringwald} 1571ca3442bSMatthias Ringwald#else 1581ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 1591ca3442bSMatthias Ringwald return NULL; 1601ca3442bSMatthias Ringwald} 1611ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 1621ca3442bSMatthias Ringwald // silence compiler warning about unused parameter in a portable way 1631ca3442bSMatthias Ringwald (void) STRUCT_NAME; 1641ca3442bSMatthias Ringwald}; 1651ca3442bSMatthias Ringwald#endif 1661ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC) 1671ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 168a2673d88SMatthias Ringwald void * buffer = malloc(sizeof(STRUCT_TYPE)); 169a2673d88SMatthias Ringwald if (buffer){ 170a2673d88SMatthias Ringwald memset(buffer, 0, sizeof(STRUCT_TYPE)); 171a2673d88SMatthias Ringwald } 172a2673d88SMatthias Ringwald return (STRUCT_NAME_t *) buffer; 1731ca3442bSMatthias Ringwald} 1741ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 1751ca3442bSMatthias Ringwald free(STRUCT_NAME); 1761ca3442bSMatthias Ringwald} 1771ca3442bSMatthias Ringwald#endif 1781ca3442bSMatthias Ringwald""" 1791ca3442bSMatthias Ringwald 1801ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0 18129d0c4f7SMatthias Ringwald btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 1821ca3442bSMatthias Ringwald#endif""" 1831ca3442bSMatthias Ringwald 1841ca3442bSMatthias Ringwalddef writeln(f, data): 1851ca3442bSMatthias Ringwald f.write(data + "\n") 1861ca3442bSMatthias Ringwald 1871ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name): 1881ca3442bSMatthias Ringwald struct_type = struct_name + '_t' 1891ca3442bSMatthias Ringwald if struct_name.endswith('try'): 190a265b909SMatthias Ringwald pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES" 1911ca3442bSMatthias Ringwald else: 192a265b909SMatthias Ringwald pool_count = "MAX_NR_" + struct_name.upper() + "S" 193a265b909SMatthias Ringwald pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_") 194a265b909SMatthias Ringwald snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT_OLD_NO", pool_count_old_no).replace("POOL_COUNT", pool_count) 1951ca3442bSMatthias Ringwald return snippet 1961ca3442bSMatthias Ringwald 197b3401248SMatthias Ringwaldlist_of_structs = [ 198b3401248SMatthias Ringwald ["hci_connection"], 199b3401248SMatthias Ringwald ["l2cap_service", "l2cap_channel"], 20044c5d856SMatthias Ringwald] 20144c5d856SMatthias Ringwaldlist_of_classic_structs = [ 202b3401248SMatthias Ringwald ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], 2032c455dadSMatthias Ringwald ["btstack_link_key_db_memory_entry"], 204b3401248SMatthias Ringwald ["bnep_service", "bnep_channel"], 205b3401248SMatthias Ringwald ["hfp_connection"], 20627faf85aSMilanka Ringwald ["service_record_item"], 20712e7f38cSMilanka Ringwald ["avdtp_stream_endpoint"], 20891451a2bSMilanka Ringwald ["avdtp_connection"], 209f12a3722SMilanka Ringwald ["avrcp_connection"], 210ebb73e1fSMatthias Ringwald ["avrcp_browsing_connection"], 211b3401248SMatthias Ringwald] 212ebb73e1fSMatthias Ringwaldlist_of_le_structs = [ 213ebb73e1fSMatthias Ringwald ["gatt_client", "whitelist_entry", "sm_lookup_entry"], 21444c5d856SMatthias Ringwald] 21544c5d856SMatthias Ringwaldlist_of_mesh_structs = [ 21601122b73SMatthias Ringwald ['mesh_network_pdu', 'mesh_transport_pdu', 'mesh_network_key', 'mesh_transport_key', 'mesh_virtual_address', 'mesh_subnet'] 217ebb73e1fSMatthias Ringwald] 2181ca3442bSMatthias Ringwald 219a2673d88SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 220a2673d88SMatthias Ringwaldfile_name = btstack_root + "/src/btstack_memory" 221a2673d88SMatthias Ringwaldprint ('Generating %s.[h|c]' % file_name) 2221ca3442bSMatthias Ringwald 2231ca3442bSMatthias Ringwaldf = open(file_name+".h", "w") 2241ca3442bSMatthias Ringwaldwriteln(f, copyright) 2251ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin) 2261ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2271ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 2281ca3442bSMatthias Ringwald for struct_name in struct_names: 2291ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 2301ca3442bSMatthias Ringwald writeln(f, "") 23144c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC") 23244c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs: 23344c5d856SMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 23444c5d856SMatthias Ringwald for struct_name in struct_names: 23544c5d856SMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 23644c5d856SMatthias Ringwald writeln(f, "") 23744c5d856SMatthias Ringwaldwriteln(f, "#endif") 238a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2391ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2401ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 2411ca3442bSMatthias Ringwald for struct_name in struct_names: 2421ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 2431ca3442bSMatthias Ringwaldwriteln(f, "#endif") 24444c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH") 24544c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs: 24644c5d856SMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 24744c5d856SMatthias Ringwald for struct_name in struct_names: 24844c5d856SMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 24944c5d856SMatthias Ringwaldwriteln(f, "#endif") 2501ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end) 2511ca3442bSMatthias Ringwaldf.close(); 2521ca3442bSMatthias Ringwald 2531ca3442bSMatthias Ringwald 2541ca3442bSMatthias Ringwaldf = open(file_name+".c", "w") 2551ca3442bSMatthias Ringwaldwriteln(f, copyright) 2561ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin) 2571ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2581ca3442bSMatthias Ringwald for struct_name in struct_names: 2591ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 2601ca3442bSMatthias Ringwald writeln(f, "") 26144c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC") 26244c5d856SMatthias Ringwaldfor struct_names in list_of_classic_structs: 26344c5d856SMatthias Ringwald for struct_name in struct_names: 26444c5d856SMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 26544c5d856SMatthias Ringwald writeln(f, "") 26644c5d856SMatthias Ringwaldwriteln(f, "#endif") 267a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2681ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2691ca3442bSMatthias Ringwald for struct_name in struct_names: 2701ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 2711ca3442bSMatthias Ringwald writeln(f, "") 2721ca3442bSMatthias Ringwaldwriteln(f, "#endif") 27344c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH") 27444c5d856SMatthias Ringwaldfor struct_names in list_of_mesh_structs: 27544c5d856SMatthias Ringwald for struct_name in struct_names: 27644c5d856SMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 27744c5d856SMatthias Ringwald writeln(f, "") 27844c5d856SMatthias Ringwaldwriteln(f, "#endif") 2791ca3442bSMatthias Ringwald 2801ca3442bSMatthias Ringwald 2811ca3442bSMatthias Ringwaldwriteln(f, "// init") 2821ca3442bSMatthias Ringwaldwriteln(f, "void btstack_memory_init(void){") 2831ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2841ca3442bSMatthias Ringwald for struct_name in struct_names: 2851ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 28644c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_CLASSIC") 28750dc57fcSMatthias Ringwaldfor struct_names in list_of_classic_structs: 28844c5d856SMatthias Ringwald for struct_name in struct_names: 28944c5d856SMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 29044c5d856SMatthias Ringwaldwriteln(f, "#endif") 291a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2921ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2931ca3442bSMatthias Ringwald for struct_name in struct_names: 2941ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 2951ca3442bSMatthias Ringwaldwriteln(f, "#endif") 29644c5d856SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_MESH") 29750dc57fcSMatthias Ringwaldfor struct_names in list_of_mesh_structs: 29844c5d856SMatthias Ringwald for struct_name in struct_names: 29944c5d856SMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 30044c5d856SMatthias Ringwaldwriteln(f, "#endif") 3011ca3442bSMatthias Ringwaldwriteln(f, "}") 3021ca3442bSMatthias Ringwaldf.close(); 3031ca3442bSMatthias Ringwald 304