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#include "hci.h" 60#include "l2cap.h" 61#include "rfcomm.h" 62#include "bnep.h" 63#include "hfp.h" 64#include "remote_device_db.h" 65 66#ifdef HAVE_BLE 67#include "gatt_client.h" 68#include "sm.h" 69#endif 70 71/* API_START */ 72 73/** 74 * @brief Initializes BTstack memory pools. 75 */ 76void btstack_memory_init(void); 77 78/* API_END */ 79""" 80 81hfile_header_end = """ 82#if defined __cplusplus 83} 84#endif 85 86#endif // __BTSTACK_MEMORY_H 87""" 88 89cfile_header_begin = """ 90/* 91 * btstsack_memory.h 92 * 93 * @brief BTstack memory management via configurable memory pools 94 * 95 * @note code semi-atuomatically generated by tools/btstack_memory_generator.py 96 * 97 */ 98 99#include "btstack_memory.h" 100#include "memory_pool.h" 101 102#include <stdlib.h> 103 104""" 105 106header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void); 107void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);""" 108 109code_template = """ 110// MARK: STRUCT_TYPE 111#ifdef POOL_COUNT 112#if POOL_COUNT > 0 113static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT]; 114static memory_pool_t STRUCT_NAME_pool; 115STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 116 return (STRUCT_NAME_t *) memory_pool_get(&STRUCT_NAME_pool); 117} 118void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 119 memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME); 120} 121#else 122STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 123 return NULL; 124} 125void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 126 // silence compiler warning about unused parameter in a portable way 127 (void) STRUCT_NAME; 128}; 129#endif 130#elif defined(HAVE_MALLOC) 131STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){ 132 return (STRUCT_NAME_t*) malloc(sizeof(STRUCT_TYPE)); 133} 134void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){ 135 free(STRUCT_NAME); 136} 137#else 138#error "Neither HAVE_MALLOC nor POOL_COUNT for struct STRUCT_NAME is defined. Please, edit the config file." 139#endif 140""" 141 142init_template = """#if POOL_COUNT > 0 143 memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE)); 144#endif""" 145 146def writeln(f, data): 147 f.write(data + "\n") 148 149def replacePlaceholder(template, struct_name): 150 struct_type = struct_name + '_t' 151 if struct_name.endswith('try'): 152 pool_count = "MAX_NO_" + struct_name.upper()[:-3] + "TRIES" 153 else: 154 pool_count = "MAX_NO_" + struct_name.upper() + "S" 155 156 snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count) 157 return snippet 158 159list_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"]] 160list_of_le_structs = [["gatt_client", "gatt_subclient", "whitelist_entry", "sm_lookup_entry"]] 161 162file_name = "../src/btstack_memory" 163 164 165f = open(file_name+".h", "w") 166writeln(f, copyright) 167writeln(f, hfile_header_begin) 168for struct_names in list_of_structs: 169 writeln(f, "// "+ ", ".join(struct_names)) 170 for struct_name in struct_names: 171 writeln(f, replacePlaceholder(header_template, struct_name)) 172 writeln(f, "") 173writeln(f, "#ifdef HAVE_BLE") 174for struct_names in list_of_le_structs: 175 writeln(f, "// "+ ", ".join(struct_names)) 176 for struct_name in struct_names: 177 writeln(f, replacePlaceholder(header_template, struct_name)) 178writeln(f, "#endif") 179writeln(f, hfile_header_end) 180f.close(); 181 182 183f = open(file_name+".c", "w") 184writeln(f, copyright) 185writeln(f, cfile_header_begin) 186for struct_names in list_of_structs: 187 for struct_name in struct_names: 188 writeln(f, replacePlaceholder(code_template, struct_name)) 189 writeln(f, "") 190writeln(f, "#ifdef HAVE_BLE") 191for struct_names in list_of_le_structs: 192 for struct_name in struct_names: 193 writeln(f, replacePlaceholder(code_template, struct_name)) 194 writeln(f, "") 195writeln(f, "#endif") 196 197 198writeln(f, "// init") 199writeln(f, "void btstack_memory_init(void){") 200for struct_names in list_of_structs: 201 for struct_name in struct_names: 202 writeln(f, replacePlaceholder(init_template, struct_name)) 203writeln(f, "#ifdef HAVE_BLE") 204for struct_names in list_of_le_structs: 205 for struct_name in struct_names: 206 writeln(f, replacePlaceholder(init_template, struct_name)) 207writeln(f, "#endif") 208writeln(f, "}") 209f.close(); 210 211