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