xref: /btstack/tool/btstack_memory_generator.py (revision 50dc57fcb6fcdc2cb4d5858b8dcb1d7772d49f11)
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 "ble/mesh/mesh_network.h"
87#include "mesh_keys.h"
88#endif
89
90/* API_START */
91
92/**
93 * @brief Initializes BTstack memory pools.
94 */
95void btstack_memory_init(void);
96
97/* API_END */
98"""
99
100hfile_header_end = """
101#if defined __cplusplus
102}
103#endif
104
105#endif // BTSTACK_MEMORY_H
106"""
107
108cfile_header_begin = """
109/*
110 *  btstack_memory.h
111 *
112 *  @brief BTstack memory management via configurable memory pools
113 *
114 *  @note code generated by tool/btstack_memory_generator.py
115 *  @note returnes buffers are initialized with 0
116 *
117 */
118
119#include "btstack_memory.h"
120#include "btstack_memory_pool.h"
121
122#include <stdlib.h>
123
124"""
125
126header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
127void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
128
129code_template = """
130// MARK: STRUCT_TYPE
131#if !defined(HAVE_MALLOC) && !defined(POOL_COUNT)
132    #if defined(POOL_COUNT_OLD_NO)
133        #error "Deprecated POOL_COUNT_OLD_NO defined instead of POOL_COUNT. Please update your btstack_config.h to use POOL_COUNT."
134    #else
135        #define POOL_COUNT 0
136    #endif
137#endif
138
139#ifdef POOL_COUNT
140#if POOL_COUNT > 0
141static STRUCT_TYPE STRUCT_NAME_storage[POOL_COUNT];
142static btstack_memory_pool_t STRUCT_NAME_pool;
143STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
144    void * buffer = btstack_memory_pool_get(&STRUCT_NAME_pool);
145    if (buffer){
146        memset(buffer, 0, sizeof(STRUCT_TYPE));
147    }
148    return (STRUCT_NAME_t *) buffer;
149}
150void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
151    btstack_memory_pool_free(&STRUCT_NAME_pool, STRUCT_NAME);
152}
153#else
154STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
155    return NULL;
156}
157void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
158    // silence compiler warning about unused parameter in a portable way
159    (void) STRUCT_NAME;
160};
161#endif
162#elif defined(HAVE_MALLOC)
163STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void){
164    void * buffer = malloc(sizeof(STRUCT_TYPE));
165    if (buffer){
166        memset(buffer, 0, sizeof(STRUCT_TYPE));
167    }
168    return (STRUCT_NAME_t *) buffer;
169}
170void btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME){
171    free(STRUCT_NAME);
172}
173#endif
174"""
175
176init_template = """#if POOL_COUNT > 0
177    btstack_memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
178#endif"""
179
180def writeln(f, data):
181    f.write(data + "\n")
182
183def replacePlaceholder(template, struct_name):
184    struct_type = struct_name + '_t'
185    if struct_name.endswith('try'):
186        pool_count = "MAX_NR_" + struct_name.upper()[:-3] + "TRIES"
187    else:
188        pool_count = "MAX_NR_" + struct_name.upper() + "S"
189    pool_count_old_no = pool_count.replace("MAX_NR_", "MAX_NO_")
190    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)
191    return snippet
192
193list_of_structs = [
194    ["hci_connection"],
195    ["l2cap_service", "l2cap_channel"],
196]
197list_of_classic_structs = [
198    ["rfcomm_multiplexer", "rfcomm_service", "rfcomm_channel"],
199    ["btstack_link_key_db_memory_entry"],
200    ["bnep_service", "bnep_channel"],
201    ["hfp_connection"],
202    ["service_record_item"],
203    ["avdtp_stream_endpoint"],
204    ["avdtp_connection"],
205    ["avrcp_connection"],
206    ["avrcp_browsing_connection"],
207]
208list_of_le_structs = [
209    ["gatt_client", "whitelist_entry", "sm_lookup_entry"],
210]
211list_of_mesh_structs = [
212    ['mesh_network_pdu', 'mesh_transport_pdu', 'mesh_network_key']
213]
214
215btstack_root = os.path.abspath(os.path.dirname(sys.argv[0]) + '/..')
216file_name = btstack_root + "/src/btstack_memory"
217print ('Generating %s.[h|c]' % file_name)
218
219f = open(file_name+".h", "w")
220writeln(f, copyright)
221writeln(f, hfile_header_begin)
222for struct_names in list_of_structs:
223    writeln(f, "// "+ ", ".join(struct_names))
224    for struct_name in struct_names:
225        writeln(f, replacePlaceholder(header_template, struct_name))
226    writeln(f, "")
227writeln(f, "#ifdef ENABLE_CLASSIC")
228for struct_names in list_of_classic_structs:
229    writeln(f, "// "+ ", ".join(struct_names))
230    for struct_name in struct_names:
231        writeln(f, replacePlaceholder(header_template, struct_name))
232    writeln(f, "")
233writeln(f, "#endif")
234writeln(f, "#ifdef ENABLE_BLE")
235for struct_names in list_of_le_structs:
236    writeln(f, "// "+ ", ".join(struct_names))
237    for struct_name in struct_names:
238        writeln(f, replacePlaceholder(header_template, struct_name))
239writeln(f, "#endif")
240writeln(f, "#ifdef ENABLE_MESH")
241for struct_names in list_of_mesh_structs:
242    writeln(f, "// "+ ", ".join(struct_names))
243    for struct_name in struct_names:
244        writeln(f, replacePlaceholder(header_template, struct_name))
245writeln(f, "#endif")
246writeln(f, hfile_header_end)
247f.close();
248
249
250f = open(file_name+".c", "w")
251writeln(f, copyright)
252writeln(f, cfile_header_begin)
253for struct_names in list_of_structs:
254    for struct_name in struct_names:
255        writeln(f, replacePlaceholder(code_template, struct_name))
256    writeln(f, "")
257writeln(f, "#ifdef ENABLE_CLASSIC")
258for struct_names in list_of_classic_structs:
259    for struct_name in struct_names:
260        writeln(f, replacePlaceholder(code_template, struct_name))
261    writeln(f, "")
262writeln(f, "#endif")
263writeln(f, "#ifdef ENABLE_BLE")
264for struct_names in list_of_le_structs:
265    for struct_name in struct_names:
266        writeln(f, replacePlaceholder(code_template, struct_name))
267    writeln(f, "")
268writeln(f, "#endif")
269writeln(f, "#ifdef ENABLE_MESH")
270for struct_names in list_of_mesh_structs:
271    for struct_name in struct_names:
272        writeln(f, replacePlaceholder(code_template, struct_name))
273    writeln(f, "")
274writeln(f, "#endif")
275
276
277writeln(f, "// init")
278writeln(f, "void btstack_memory_init(void){")
279for struct_names in list_of_structs:
280    for struct_name in struct_names:
281        writeln(f, replacePlaceholder(init_template, struct_name))
282writeln(f, "#ifdef ENABLE_CLASSIC")
283for struct_names in list_of_classic_structs:
284    for struct_name in struct_names:
285        writeln(f, replacePlaceholder(init_template, struct_name))
286writeln(f, "#endif")
287writeln(f, "#ifdef ENABLE_BLE")
288for struct_names in list_of_le_structs:
289    for struct_name in struct_names:
290        writeln(f, replacePlaceholder(init_template, struct_name))
291writeln(f, "#endif")
292writeln(f, "#ifdef ENABLE_MESH")
293for struct_names in list_of_mesh_structs:
294    for struct_name in struct_names:
295        writeln(f, replacePlaceholder(init_template, struct_name))
296writeln(f, "#endif")
297writeln(f, "}")
298f.close();
299
300