1*1ca3442bSMatthias Ringwald#!/usr/bin/env python 2*1ca3442bSMatthias Ringwald 3*1ca3442bSMatthias Ringwaldcopyright = """/* 4*1ca3442bSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 5*1ca3442bSMatthias Ringwald * 6*1ca3442bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 7*1ca3442bSMatthias Ringwald * modification, are permitted provided that the following conditions 8*1ca3442bSMatthias Ringwald * are met: 9*1ca3442bSMatthias Ringwald * 10*1ca3442bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 11*1ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 12*1ca3442bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 13*1ca3442bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 14*1ca3442bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 15*1ca3442bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 16*1ca3442bSMatthias Ringwald * contributors may be used to endorse or promote products derived 17*1ca3442bSMatthias Ringwald * from this software without specific prior written permission. 18*1ca3442bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 19*1ca3442bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 20*1ca3442bSMatthias Ringwald * monetary gain. 21*1ca3442bSMatthias Ringwald * 22*1ca3442bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 23*1ca3442bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24*1ca3442bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25*1ca3442bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 26*1ca3442bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27*1ca3442bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28*1ca3442bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 29*1ca3442bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 30*1ca3442bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 31*1ca3442bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 32*1ca3442bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33*1ca3442bSMatthias Ringwald * SUCH DAMAGE. 34*1ca3442bSMatthias Ringwald * 35*1ca3442bSMatthias Ringwald * Please inquire about commercial licensing options at 36*1ca3442bSMatthias Ringwald * [email protected] 37*1ca3442bSMatthias Ringwald * 38*1ca3442bSMatthias Ringwald */ 39*1ca3442bSMatthias Ringwald""" 40*1ca3442bSMatthias Ringwald 41*1ca3442bSMatthias Ringwaldhfile_header_begin = """ 42*1ca3442bSMatthias Ringwald 43*1ca3442bSMatthias Ringwald/* 44*1ca3442bSMatthias Ringwald * btstack_memory.h 45*1ca3442bSMatthias Ringwald * 46*1ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 47*1ca3442bSMatthias Ringwald * 48*1ca3442bSMatthias Ringwald */ 49*1ca3442bSMatthias Ringwald 50*1ca3442bSMatthias Ringwald#ifndef __BTSTACK_MEMORY_H 51*1ca3442bSMatthias Ringwald#define __BTSTACK_MEMORY_H 52*1ca3442bSMatthias Ringwald 53*1ca3442bSMatthias Ringwald#if defined __cplusplus 54*1ca3442bSMatthias Ringwaldextern "C" { 55*1ca3442bSMatthias Ringwald#endif 56*1ca3442bSMatthias Ringwald 57*1ca3442bSMatthias Ringwald#include "btstack-config.h" 58*1ca3442bSMatthias Ringwald 59*1ca3442bSMatthias Ringwald#include "hci.h" 60*1ca3442bSMatthias Ringwald#include "l2cap.h" 61*1ca3442bSMatthias Ringwald#include "rfcomm.h" 62*1ca3442bSMatthias Ringwald#include "bnep.h" 63*1ca3442bSMatthias Ringwald#include "hfp.h" 64*1ca3442bSMatthias Ringwald#include "remote_device_db.h" 65*1ca3442bSMatthias Ringwald 66*1ca3442bSMatthias Ringwald#ifdef HAVE_BLE 67*1ca3442bSMatthias Ringwald#include "gatt_client.h" 68*1ca3442bSMatthias Ringwald#include "sm.h" 69*1ca3442bSMatthias Ringwald#endif 70*1ca3442bSMatthias Ringwald 71*1ca3442bSMatthias Ringwald/* API_START */ 72*1ca3442bSMatthias Ringwald 73*1ca3442bSMatthias Ringwald/** 74*1ca3442bSMatthias Ringwald * @brief Initializes BTstack memory pools. 75*1ca3442bSMatthias Ringwald */ 76*1ca3442bSMatthias Ringwaldvoid btstack_memory_init(void); 77*1ca3442bSMatthias Ringwald 78*1ca3442bSMatthias Ringwald/* API_END */ 79*1ca3442bSMatthias Ringwald""" 80*1ca3442bSMatthias Ringwald 81*1ca3442bSMatthias Ringwaldhfile_header_end = """ 82*1ca3442bSMatthias Ringwald#if defined __cplusplus 83*1ca3442bSMatthias Ringwald} 84*1ca3442bSMatthias Ringwald#endif 85*1ca3442bSMatthias Ringwald 86*1ca3442bSMatthias Ringwald#endif // __BTSTACK_MEMORY_H 87*1ca3442bSMatthias Ringwald""" 88*1ca3442bSMatthias Ringwald 89*1ca3442bSMatthias Ringwaldcfile_header_begin = """ 90*1ca3442bSMatthias Ringwald/* 91*1ca3442bSMatthias Ringwald * btstsack_memory.h 92*1ca3442bSMatthias Ringwald * 93*1ca3442bSMatthias Ringwald * @brief BTstack memory management via configurable memory pools 94*1ca3442bSMatthias Ringwald * 95*1ca3442bSMatthias Ringwald * @note code semi-atuomatically generated by tools/btstack_memory_generator.py 96*1ca3442bSMatthias Ringwald * 97*1ca3442bSMatthias Ringwald */ 98*1ca3442bSMatthias Ringwald 99*1ca3442bSMatthias Ringwald#include "btstack_memory.h" 100*1ca3442bSMatthias Ringwald#include "memory_pool.h" 101*1ca3442bSMatthias Ringwald 102*1ca3442bSMatthias Ringwald#include <stdlib.h> 103*1ca3442bSMatthias Ringwald 104*1ca3442bSMatthias Ringwald""" 105*1ca3442bSMatthias Ringwald 106*1ca3442bSMatthias Ringwaldheader_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 107*1ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 108*1ca3442bSMatthias Ringwald 109*1ca3442bSMatthias Ringwaldcode_template = """ 110*1ca3442bSMatthias Ringwald// MARK: STRUCT_TYPE 111*1ca3442bSMatthias Ringwald#ifdef POOL_COUNT 112*1ca3442bSMatthias Ringwald#if POOL_COUNT > 0 113*1ca3442bSMatthias Ringwaldstatic STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 114*1ca3442bSMatthias Ringwaldstatic memory_pool_t STRUCT_NAME_pool; 115*1ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 116*1ca3442bSMatthias Ringwald return (STRUCT_NAME_t *) memory_pool_get(&STRUCT_NAME_pool); 117*1ca3442bSMatthias Ringwald} 118*1ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 119*1ca3442bSMatthias Ringwald memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 120*1ca3442bSMatthias Ringwald} 121*1ca3442bSMatthias Ringwald#else 122*1ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 123*1ca3442bSMatthias Ringwald return NULL; 124*1ca3442bSMatthias Ringwald} 125*1ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 126*1ca3442bSMatthias Ringwald // silence compiler warning about unused parameter in a portable way 127*1ca3442bSMatthias Ringwald (void) STRUCT_NAME; 128*1ca3442bSMatthias Ringwald}; 129*1ca3442bSMatthias Ringwald#endif 130*1ca3442bSMatthias Ringwald#elif defined(HAVE_MALLOC) 131*1ca3442bSMatthias RingwaldSTRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 132*1ca3442bSMatthias Ringwald return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE)); 133*1ca3442bSMatthias Ringwald} 134*1ca3442bSMatthias Ringwaldvoid btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 135*1ca3442bSMatthias Ringwald free(STRUCT_NAME); 136*1ca3442bSMatthias Ringwald} 137*1ca3442bSMatthias Ringwald#else 138*1ca3442bSMatthias Ringwald#error "Neither HAVE_MALLOC nor POOL_COUNT for struct STRUCT_NAME is defined. Please, edit the config file." 139*1ca3442bSMatthias Ringwald#endif 140*1ca3442bSMatthias Ringwald""" 141*1ca3442bSMatthias Ringwald 142*1ca3442bSMatthias Ringwaldinit_template = """#if POOL_COUNT > 0 143*1ca3442bSMatthias Ringwald memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 144*1ca3442bSMatthias Ringwald#endif""" 145*1ca3442bSMatthias Ringwald 146*1ca3442bSMatthias Ringwalddef writeln(f, data): 147*1ca3442bSMatthias Ringwald f.write(data + "\n") 148*1ca3442bSMatthias Ringwald 149*1ca3442bSMatthias Ringwalddef replacePlaceholder(template, struct_name): 150*1ca3442bSMatthias Ringwald struct_type = struct_name + '_t' 151*1ca3442bSMatthias Ringwald if struct_name.endswith('try'): 152*1ca3442bSMatthias Ringwald pool_count = "MAX_NO_" + struct_name.upper()[:-3] + "TRIES" 153*1ca3442bSMatthias Ringwald else: 154*1ca3442bSMatthias Ringwald pool_count = "MAX_NO_" + struct_name.upper() + "S" 155*1ca3442bSMatthias Ringwald 156*1ca3442bSMatthias Ringwald snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count) 157*1ca3442bSMatthias Ringwald return snippet 158*1ca3442bSMatthias Ringwald 159*1ca3442bSMatthias Ringwaldlist_of_structs = [ ["hci_connection"], ["l2cap_service", "l2cap_channel"], ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], ["db_mem_device_name", "db_mem_device_link_key", "db_mem_service"], ["bnep_service", "bnep_channel"], ["hfp_connection"]] 160*1ca3442bSMatthias Ringwaldlist_of_le_structs = [["gatt_client", "gatt_subclient", "whitelist_entry", "sm_lookup_entry"]] 161*1ca3442bSMatthias Ringwald 162*1ca3442bSMatthias Ringwaldfile_name = "../src/btstack_memory" 163*1ca3442bSMatthias Ringwald 164*1ca3442bSMatthias Ringwald 165*1ca3442bSMatthias Ringwaldf = open(file_name+".h", "w") 166*1ca3442bSMatthias Ringwaldwriteln(f, copyright) 167*1ca3442bSMatthias Ringwaldwriteln(f, hfile_header_begin) 168*1ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 169*1ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 170*1ca3442bSMatthias Ringwald for struct_name in struct_names: 171*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 172*1ca3442bSMatthias Ringwald writeln(f, "") 173*1ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE") 174*1ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 175*1ca3442bSMatthias Ringwald writeln(f, "// "+ ", ".join(struct_names)) 176*1ca3442bSMatthias Ringwald for struct_name in struct_names: 177*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(header_template, struct_name)) 178*1ca3442bSMatthias Ringwaldwriteln(f, "#endif") 179*1ca3442bSMatthias Ringwaldwriteln(f, hfile_header_end) 180*1ca3442bSMatthias Ringwaldf.close(); 181*1ca3442bSMatthias Ringwald 182*1ca3442bSMatthias Ringwald 183*1ca3442bSMatthias Ringwaldf = open(file_name+".c", "w") 184*1ca3442bSMatthias Ringwaldwriteln(f, copyright) 185*1ca3442bSMatthias Ringwaldwriteln(f, cfile_header_begin) 186*1ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 187*1ca3442bSMatthias Ringwald for struct_name in struct_names: 188*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 189*1ca3442bSMatthias Ringwald writeln(f, "") 190*1ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE") 191*1ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 192*1ca3442bSMatthias Ringwald for struct_name in struct_names: 193*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(code_template, struct_name)) 194*1ca3442bSMatthias Ringwald writeln(f, "") 195*1ca3442bSMatthias Ringwaldwriteln(f, "#endif") 196*1ca3442bSMatthias Ringwald 197*1ca3442bSMatthias Ringwald 198*1ca3442bSMatthias Ringwaldwriteln(f, "// init") 199*1ca3442bSMatthias Ringwaldwriteln(f, "void btstack_memory_init(void){") 200*1ca3442bSMatthias Ringwaldfor struct_names in list_of_structs: 201*1ca3442bSMatthias Ringwald for struct_name in struct_names: 202*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 203*1ca3442bSMatthias Ringwaldwriteln(f, "#ifdef HAVE_BLE") 204*1ca3442bSMatthias Ringwaldfor struct_names in list_of_le_structs: 205*1ca3442bSMatthias Ringwald for struct_name in struct_names: 206*1ca3442bSMatthias Ringwald writeln(f, replacePlaceholder(init_template, struct_name)) 207*1ca3442bSMatthias Ringwaldwriteln(f, "#endif") 208*1ca3442bSMatthias Ringwaldwriteln(f, "}") 209*1ca3442bSMatthias Ringwaldf.close(); 210*1ca3442bSMatthias Ringwald 211