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