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/btstack_link_key_db.h" 67#include "classic/btstack_link_key_db_memory.h" 68#include "classic/rfcomm.h" 69#include "classic/sdp_server.h" 70 71// BLE 72#ifdef ENABLE_BLE 73#include "ble/gatt_client.h" 74#include "ble/sm.h" 75#endif 76 77/* API_START */ 78 79/** 80 * @brief Initializes BTstack memory pools. 81 */ 82void btstack_memory_init(void); 83 84/* API_END */ 85""" 86 87hfile_header_end = """ 88#if defined __cplusplus 89} 90#endif 91 92#endif // __BTSTACK_MEMORY_H 93""" 94 95cfile_header_begin = """ 96/* 97 * btstack_memory.h 98 * 99 * @brief BTstack memory management via configurable memory pools 100 * 101 * @note code generated by tool/btstack_memory_generator.py 102 * 103 */ 104 105#include "btstack_memory.h" 106#include "btstack_memory_pool.h" 107 108#include <stdlib.h> 109 110""" 111 112header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 113void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 114 115code_template = """ 116// MARK: STRUCT_TYPE 117#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT) 118 #if defined(POOL_COUNT_OLD_NO) 119 #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT." 120 #else 121 #define POOL_COUNT 0 122 #endif 123#endif 124 125#ifdef POOL_COUNT 126#if POOL_COUNT > 0 127static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 128static btstack_memory_pool_t STRUCT_NAME_pool; 129STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 130 return (STRUCT_NAME_t *) btstack_memory_pool_get(&STRUCT_NAME_pool); 131} 132void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 133 btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 134} 135#else 136STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 137 return NULL; 138} 139void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 140 // silence compiler warning about unused parameter in a portable way 141 (void) STRUCT_NAME; 142}; 143#endif 144#elif defined(HAVE_MALLOC) 145STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 146 return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE)); 147} 148void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 149 free(STRUCT_NAME); 150} 151#endif 152""" 153 154init_template = """#if POOL_COUNT > 0 155 btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 156#endif""" 157 158def writeln(f, data): 159 f.write(data + "\n") 160 161def replacePlaceholder(template, struct_name): 162 struct_type = struct_name + '_t' 163 if struct_name.endswith('try'): 164 pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES" 165 else: 166 pool_count = "MAX_NR_" + struct_name.upper() + "S" 167 pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_") 168 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) 169 return snippet 170 171list_of_structs = [ 172 ["hci_connection"], 173 ["l2cap_service", "l2cap_channel"], 174 ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"], 175 ["btstack_link_key_db_memory_entry"], 176 ["bnep_service", "bnep_channel"], 177 ["hfp_connection"], 178 ["service_record_item"] 179] 180list_of_le_structs = [["gatt_client", "whitelist_entry", "sm_lookup_entry"]] 181 182file_name = "../src/btstack_memory" 183 184 185f = open(file_name+".h", "w") 186writeln(f, copyright) 187writeln(f, hfile_header_begin) 188for struct_names in list_of_structs: 189 writeln(f, "// "+ ", ".join(struct_names)) 190 for struct_name in struct_names: 191 writeln(f, replacePlaceholder(header_template, struct_name)) 192 writeln(f, "") 193writeln(f, "#ifdef ENABLE_BLE") 194for struct_names in list_of_le_structs: 195 writeln(f, "// "+ ", ".join(struct_names)) 196 for struct_name in struct_names: 197 writeln(f, replacePlaceholder(header_template, struct_name)) 198writeln(f, "#endif") 199writeln(f, hfile_header_end) 200f.close(); 201 202 203f = open(file_name+".c", "w") 204writeln(f, copyright) 205writeln(f, cfile_header_begin) 206for struct_names in list_of_structs: 207 for struct_name in struct_names: 208 writeln(f, replacePlaceholder(code_template, struct_name)) 209 writeln(f, "") 210writeln(f, "#ifdef ENABLE_BLE") 211for struct_names in list_of_le_structs: 212 for struct_name in struct_names: 213 writeln(f, replacePlaceholder(code_template, struct_name)) 214 writeln(f, "") 215writeln(f, "#endif") 216 217 218writeln(f, "// init") 219writeln(f, "void btstack_memory_init(void){") 220for struct_names in list_of_structs: 221 for struct_name in struct_names: 222 writeln(f, replacePlaceholder(init_template, struct_name)) 223writeln(f, "#ifdef ENABLE_BLE") 224for struct_names in list_of_le_structs: 225 for struct_name in struct_names: 226 writeln(f, replacePlaceholder(init_template, struct_name)) 227writeln(f, "#endif") 228writeln(f, "}") 229f.close(); 230 231