11ca3442bSMatthias Ringwald#!/usr/bin/env python 21ca3442bSMatthias Ringwald 3*a2673d88SMatthias Ringwaldimport os 4*a2673d88SMatthias Ringwaldimport sys 5*a2673d88SMatthias Ringwald 61ca3442bSMatthias Ringwaldcopyright = """/* 71ca3442bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 81ca3442bSMatthias Ringwald * 91ca3442bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 101ca3442bSMatthias Ringwald * modification, are permitted provided that the following conditions 111ca3442bSMatthias Ringwald * are met: 121ca3442bSMatthias Ringwald * 131ca3442bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 141ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 151ca3442bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 161ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 171ca3442bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 181ca3442bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 191ca3442bSMatthias Ringwald * contributors may be used to endorse or promote products derived 201ca3442bSMatthias Ringwald * from this software without specific prior written permission. 211ca3442bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 221ca3442bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 231ca3442bSMatthias Ringwald * monetary gain. 241ca3442bSMatthias Ringwald * 251ca3442bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 261ca3442bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 271ca3442bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 281ca3442bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 291ca3442bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 301ca3442bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 311ca3442bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 321ca3442bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 331ca3442bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 341ca3442bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 351ca3442bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 361ca3442bSMatthias Ringwald * SUCH DAMAGE. 371ca3442bSMatthias Ringwald * 381ca3442bSMatthias Ringwald * Please inquire about commercial licensing options at 391ca3442bSMatthias Ringwald * [email protected] 401ca3442bSMatthias Ringwald * 411ca3442bSMatthias Ringwald */ 421ca3442bSMatthias Ringwald""" 431ca3442bSMatthias Ringwald 441ca3442bSMatthias Ringwaldhfile_header_begin = """ 451ca3442bSMatthias Ringwald 461ca3442bSMatthias Ringwald/* 471ca3442bSMatthias Ringwald * btstack_memory.h 481ca3442bSMatthias Ringwald * 491ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 501ca3442bSMatthias Ringwald * 511ca3442bSMatthias Ringwald */ 521ca3442bSMatthias Ringwald 531ca3442bSMatthias Ringwald#ifndef __BTSTACK_MEMORY_H 541ca3442bSMatthias Ringwald#define __BTSTACK_MEMORY_H 551ca3442bSMatthias Ringwald 561ca3442bSMatthias Ringwald#if defined __cplusplus 571ca3442bSMatthias Ringwaldextern "C" { 581ca3442bSMatthias Ringwald#endif 591ca3442bSMatthias Ringwald 607907f069SMatthias Ringwald#include "btstack_config.h" 611ca3442bSMatthias Ringwald 623edc84c5SMatthias Ringwald// Core 631ca3442bSMatthias Ringwald#include "hci.h" 641ca3442bSMatthias Ringwald#include "l2cap.h" 651ca3442bSMatthias Ringwald 663edc84c5SMatthias Ringwald// Classic 673edc84c5SMatthias Ringwald#include "classic/bnep.h" 683edc84c5SMatthias Ringwald#include "classic/hfp.h" 69a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db.h" 70a98592bcSMatthias Ringwald#include "classic/btstack_link_key_db_memory.h" 71b3401248SMatthias Ringwald#include "classic/rfcomm.h" 72746ccb7eSMatthias Ringwald#include "classic/sdp_server.h" 73208d3378SMilanka Ringwald#include "classic/avdtp_sink.h" 74208d3378SMilanka Ringwald#include "classic/avdtp_source.h" 75be32e7f1SMilanka Ringwald#include "classic/avrcp.h" 763edc84c5SMatthias Ringwald 773edc84c5SMatthias Ringwald// BLE 78a9a4c409SMatthias Ringwald#ifdef ENABLE_BLE 793edc84c5SMatthias Ringwald#include "ble/gatt_client.h" 803edc84c5SMatthias Ringwald#include "ble/sm.h" 811ca3442bSMatthias Ringwald#endif 821ca3442bSMatthias Ringwald 831ca3442bSMatthias Ringwald/* API_START */ 841ca3442bSMatthias Ringwald 851ca3442bSMatthias Ringwald/** 861ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools. 871ca3442bSMatthias Ringwald */ 881ca3442bSMatthias Ringwaldvoid btstack_memory_init(void); 891ca3442bSMatthias Ringwald 901ca3442bSMatthias Ringwald/* API_END */ 911ca3442bSMatthias Ringwald""" 921ca3442bSMatthias Ringwald 931ca3442bSMatthias Ringwaldhfile_header_end = """ 941ca3442bSMatthias Ringwald#if defined __cplusplus 951ca3442bSMatthias Ringwald} 961ca3442bSMatthias Ringwald#endif 971ca3442bSMatthias Ringwald 981ca3442bSMatthias Ringwald#endif // __BTSTACK_MEMORY_H 991ca3442bSMatthias Ringwald""" 1001ca3442bSMatthias Ringwald 1011ca3442bSMatthias Ringwaldcfile_header_begin = """ 1021ca3442bSMatthias Ringwald/* 103a98592bcSMatthias Ringwald * btstack_memory.h 1041ca3442bSMatthias Ringwald * 1051ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 1061ca3442bSMatthias Ringwald * 107a98592bcSMatthias Ringwald * @note code generated by tool/btstack_memory_generator.py 108*a2673d88SMatthias Ringwald * @note returnes buffers are initialized with 0 1091ca3442bSMatthias Ringwald * 1101ca3442bSMatthias Ringwald */ 1111ca3442bSMatthias Ringwald 1121ca3442bSMatthias Ringwald#include "btstack_memory.h" 113d2e6c4b7SMatthias Ringwald#include "btstack_memory_pool.h" 1141ca3442bSMatthias Ringwald 1151ca3442bSMatthias Ringwald#include <stdlib.h> 1161ca3442bSMatthias Ringwald 1171ca3442bSMatthias Ringwald""" 1181ca3442bSMatthias Ringwald 1191ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 1201ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 1211ca3442bSMatthias Ringwald 1221ca3442bSMatthias Ringwaldcode_template = """ 1231ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE 124a265b909SMatthias Ringwald#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT) 125a265b909SMatthias Ringwald #if defined(POOL_COUNT_OLD_NO) 126a265b909SMatthias Ringwald #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT." 127a265b909SMatthias Ringwald #else 128a265b909SMatthias Ringwald #define POOL_COUNT 0 129a265b909SMatthias Ringwald #endif 130a265b909SMatthias Ringwald#endif 131a265b909SMatthias Ringwald 1321ca3442bSMatthias Ringwald#ifdef POOL_COUNT 1331ca3442bSMatthias Ringwald#if POOL_COUNT > 0 1341ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 13529d0c4f7SMatthias Ringwaldstatic btstack_memory_pool_t STRUCT_NAME_pool; 1361ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 137*a2673d88SMatthias Ringwald void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool); 138*a2673d88SMatthias Ringwald if (buffer){ 139*a2673d88SMatthias Ringwald memset(buffer, 0, sizeof(STRUCT_TYPE)); 140*a2673d88SMatthias Ringwald } 141*a2673d88SMatthias Ringwald return (STRUCT_NAME_t *) buffer; 1421ca3442bSMatthias Ringwald} 1431ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 14429d0c4f7SMatthias Ringwald btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 1451ca3442bSMatthias Ringwald} 1461ca3442bSMatthias Ringwald#else 1471ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 1481ca3442bSMatthias Ringwald return NULL; 1491ca3442bSMatthias Ringwald} 1501ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 1511ca3442bSMatthias Ringwald // silence compiler warning about unused parameter in a portable way 1521ca3442bSMatthias Ringwald (void) STRUCT_NAME; 1531ca3442bSMatthias Ringwald}; 1541ca3442bSMatthias Ringwald#endif 1551ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC) 1561ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 157*a2673d88SMatthias Ringwald void * buffer = malloc(sizeof(STRUCT_TYPE)); 158*a2673d88SMatthias Ringwald if (buffer){ 159*a2673d88SMatthias Ringwald memset(buffer, 0, sizeof(STRUCT_TYPE)); 160*a2673d88SMatthias Ringwald } 161*a2673d88SMatthias Ringwald return (STRUCT_NAME_t *) buffer; 1621ca3442bSMatthias Ringwald} 1631ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 1641ca3442bSMatthias Ringwald free(STRUCT_NAME); 1651ca3442bSMatthias Ringwald} 1661ca3442bSMatthias Ringwald#endif 1671ca3442bSMatthias Ringwald""" 1681ca3442bSMatthias Ringwald 1691ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0 17029d0c4f7SMatthias Ringwald btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 1711ca3442bSMatthias Ringwald#endif""" 1721ca3442bSMatthias Ringwald 1731ca3442bSMatthias Ringwalddef writeln(f, data): 1741ca3442bSMatthias Ringwald f.write(data + "\n") 1751ca3442bSMatthias Ringwald 1761ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name): 1771ca3442bSMatthias Ringwald struct_type = struct_name + '_t' 1781ca3442bSMatthias Ringwald if struct_name.endswith('try'): 179a265b909SMatthias Ringwald pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES" 1801ca3442bSMatthias Ringwald else: 181a265b909SMatthias Ringwald pool_count = "MAX_NR_" + struct_name.upper() + "S" 182a265b909SMatthias Ringwald pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_") 183a265b909SMatthias 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) 1841ca3442bSMatthias Ringwald return snippet 1851ca3442bSMatthias Ringwald 186b3401248SMatthias Ringwaldlist_of_structs = [ 187b3401248SMatthias Ringwald ["hci_connection"], 188b3401248SMatthias Ringwald ["l2cap_service", "l2cap_channel"], 189b3401248SMatthias Ringwald ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], 1902c455dadSMatthias Ringwald ["btstack_link_key_db_memory_entry"], 191b3401248SMatthias Ringwald ["bnep_service", "bnep_channel"], 192b3401248SMatthias Ringwald ["hfp_connection"], 19327faf85aSMilanka Ringwald ["service_record_item"], 19412e7f38cSMilanka Ringwald ["avdtp_stream_endpoint"], 19591451a2bSMilanka Ringwald ["avdtp_connection"], 196f12a3722SMilanka Ringwald ["avrcp_connection"], 197f12a3722SMilanka Ringwald ["avrcp_browsing_connection"] 198b3401248SMatthias Ringwald] 199*a2673d88SMatthias Ringwald 2009c662c9bSMatthias Ringwaldlist_of_le_structs = [["gatt_client", "whitelist_entry", "sm_lookup_entry"]] 2011ca3442bSMatthias Ringwald 202*a2673d88SMatthias Ringwald""" 203*a2673d88SMatthias Ringwald""" 2041ca3442bSMatthias Ringwald 205*a2673d88SMatthias Ringwaldbtstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..') 206*a2673d88SMatthias Ringwaldfile_name = btstack_root + "/src/btstack_memory" 207*a2673d88SMatthias Ringwaldprint ('Generating %s.[h|c]' % file_name) 2081ca3442bSMatthias Ringwald 2091ca3442bSMatthias Ringwaldf = open(file_name+".h", "w") 2101ca3442bSMatthias Ringwaldwriteln(f, copyright) 2111ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin) 2121ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2131ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 2141ca3442bSMatthias Ringwald for struct_name in struct_names: 2151ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 2161ca3442bSMatthias Ringwald writeln(f, "") 217a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2181ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2191ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 2201ca3442bSMatthias Ringwald for struct_name in struct_names: 2211ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 2221ca3442bSMatthias Ringwaldwriteln(f, "#endif") 2231ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end) 2241ca3442bSMatthias Ringwaldf.close(); 2251ca3442bSMatthias Ringwald 2261ca3442bSMatthias Ringwald 2271ca3442bSMatthias Ringwaldf = open(file_name+".c", "w") 2281ca3442bSMatthias Ringwaldwriteln(f, copyright) 2291ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin) 2301ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2311ca3442bSMatthias Ringwald for struct_name in struct_names: 2321ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 2331ca3442bSMatthias Ringwald writeln(f, "") 234a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2351ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2361ca3442bSMatthias Ringwald for struct_name in struct_names: 2371ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 2381ca3442bSMatthias Ringwald writeln(f, "") 2391ca3442bSMatthias Ringwaldwriteln(f, "#endif") 2401ca3442bSMatthias Ringwald 2411ca3442bSMatthias Ringwald 2421ca3442bSMatthias Ringwaldwriteln(f, "// init") 2431ca3442bSMatthias Ringwaldwriteln(f, "void btstack_memory_init(void){") 2441ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 2451ca3442bSMatthias Ringwald for struct_name in struct_names: 2461ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 247a9a4c409SMatthias Ringwaldwriteln(f, "#ifdef ENABLE_BLE") 2481ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 2491ca3442bSMatthias Ringwald for struct_name in struct_names: 2501ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 2511ca3442bSMatthias Ringwaldwriteln(f, "#endif") 2521ca3442bSMatthias Ringwaldwriteln(f, "}") 2531ca3442bSMatthias Ringwaldf.close(); 2541ca3442bSMatthias Ringwald 255