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