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