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