From 2e97c1499bddf39c62914b277ea50009d9d455fe Mon Sep 17 00:00:00 2001
From: "mila@ringwald.ch"
 <mila@ringwald.ch@1a0a8af8-31b5-11de-8e0c-53a27eea117e>
Date: Fri, 31 Oct 2014 11:42:29 +0000
Subject: [PATCH] python script for btstack memory template writes directly to
 btstack_memory.h and .c files

---
 src/btstack_memory.c              | 190 +++++++++++++++++++-----------
 src/btstack_memory.h              |  33 ++++--
 tools/btstack_memory_generator.py | 161 +++++++++++++++++++++++--
 3 files changed, 295 insertions(+), 89 deletions(-)

diff --git a/src/btstack_memory.c b/src/btstack_memory.c
index 61b1fa83a..91267c022 100644
--- a/src/btstack_memory.c
+++ b/src/btstack_memory.c
@@ -1,5 +1,6 @@
+
 /*
- * Copyright (C) 2009-2012 by Matthias Ringwald
+ * Copyright (C) 2009 by BlueKitchen GmbH
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -17,7 +18,7 @@
  *    personal benefit and not for any commercial purpose or for
  *    monetary gain.
  *
- * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
+ * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
@@ -30,10 +31,12 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * Please inquire about commercial licensing options at btstack@ringwald.ch
+ * Please inquire about commercial licensing options at 
+ * contact@bluekitchen-gmbh.com
  *
  */
 
+
 /*
  *  btstsack_memory.h
  *
@@ -53,6 +56,7 @@
 #include "l2cap.h"
 #include "rfcomm.h"
 
+
 // MARK: hci_connection_t
 #ifdef MAX_NO_HCI_CONNECTIONS
 #if MAX_NO_HCI_CONNECTIONS > 0
@@ -70,7 +74,7 @@ hci_connection_t * btstack_memory_hci_connection_get(void){
 }
 void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
     // silence compiler warning about unused parameter in a portable way
-    hci_connection;
+    (void) hci_connection;
 };
 #endif
 #elif defined(HAVE_MALLOC)
@@ -85,6 +89,7 @@ void btstack_memory_hci_connection_free(hci_connection_t *hci_connection){
 #endif
 
 
+
 // MARK: l2cap_service_t
 #ifdef MAX_NO_L2CAP_SERVICES
 #if MAX_NO_L2CAP_SERVICES > 0
@@ -102,7 +107,7 @@ l2cap_service_t * btstack_memory_l2cap_service_get(void){
 }
 void btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service){
     // silence compiler warning about unused parameter in a portable way
-    (void) l2cap_service;    
+    (void) l2cap_service;
 };
 #endif
 #elif defined(HAVE_MALLOC)
@@ -134,7 +139,7 @@ l2cap_channel_t * btstack_memory_l2cap_channel_get(void){
 }
 void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
     // silence compiler warning about unused parameter in a portable way
-    (void) l2cap_channel;    
+    (void) l2cap_channel;
 };
 #endif
 #elif defined(HAVE_MALLOC)
@@ -149,6 +154,7 @@ void btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel){
 #endif
 
 
+
 // MARK: rfcomm_multiplexer_t
 #ifdef MAX_NO_RFCOMM_MULTIPLEXERS
 #if MAX_NO_RFCOMM_MULTIPLEXERS > 0
@@ -244,64 +250,6 @@ void btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel){
 #error "Neither HAVE_MALLOC nor MAX_NO_RFCOMM_CHANNELS for struct rfcomm_channel is defined. Please, edit the config file."
 #endif
 
-// MARK: bnepservice_t
-#ifdef MAX_NO_BNEP_SERVICES
-#if MAX_NO_BNEP_SERVICES > 0
-static bnep_service_t bnep_service_storage[MAX_NO_BNEP_SERVICES];
-static memory_pool_t bnep_service_pool;
-bnep_service_t * btstack_memory_bnep_service_get(void){
-    return memory_pool_get(&bnep_service_pool);
-}
-void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
-    memory_pool_free(&bnep_service_pool, bnep_service);
-}
-#else
-bnep_service_t * btstack_memory_bnep_service_get(void){
-    return NULL;
-}
-void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
-};
-#endif
-#elif defined(HAVE_MALLOC)
-bnep_service_t * btstack_memory_bnep_service_get(void){
-    return (bnep_service_t*) malloc(sizeof(bnep_service_t));
-}
-void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
-    free(bnep_service);
-}
-#else
-#error "Neither HAVE_MALLOC nor MAX_NO_BNEP_SERVICES for struct bnep_service is defined. Please, edit the config file."
-#endif
-
-
-// MARK: bnep_channel_t
-#ifdef MAX_NO_BNEP_CHANNELS
-#if MAX_NO_BNEP_CHANNELS > 0
-static bnep_channel_t bnep_channel_storage[MAX_NO_BNEP_CHANNELS];
-static memory_pool_t bnep_channel_pool;
-bnep_channel_t * btstack_memory_bnep_channel_get(void){
-    return memory_pool_get(&bnep_channel_pool);
-}
-void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
-    memory_pool_free(&bnep_channel_pool, bnep_channel);
-}
-#else
-bnep_channel_t * btstack_memory_bnep_channel_get(void){
-    return NULL;
-}
-void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
-};
-#endif
-#elif defined(HAVE_MALLOC)
-bnep_channel_t * btstack_memory_bnep_channel_get(void){
-    return (bnep_channel_t*) malloc(sizeof(bnep_channel_t));
-}
-void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
-    free(bnep_channel);
-}
-#else
-#error "Neither HAVE_MALLOC nor MAX_NO_BNEP_CHANNELS for struct bnep_channel is defined. Please, edit the config file."
-#endif
 
 
 // MARK: db_mem_device_name_t
@@ -319,7 +267,7 @@ void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_
 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void){
     return NULL;
 }
-void btstack_memory_db_mem_device_name_free(db_mem_device_name_t * db_mem_device_name){
+void btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name){
     // silence compiler warning about unused parameter in a portable way
     (void) db_mem_device_name;
 };
@@ -399,8 +347,75 @@ void btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service){
 #error "Neither HAVE_MALLOC nor MAX_NO_DB_MEM_SERVICES for struct db_mem_service is defined. Please, edit the config file."
 #endif
 
-// MARK: gatt_client_t
+
+
+// MARK: bnep_service_t
+#ifdef MAX_NO_BNEP_SERVICES
+#if MAX_NO_BNEP_SERVICES > 0
+static bnep_service_t bnep_service_storage[MAX_NO_BNEP_SERVICES];
+static memory_pool_t bnep_service_pool;
+bnep_service_t * btstack_memory_bnep_service_get(void){
+    return memory_pool_get(&bnep_service_pool);
+}
+void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
+    memory_pool_free(&bnep_service_pool, bnep_service);
+}
+#else
+bnep_service_t * btstack_memory_bnep_service_get(void){
+    return NULL;
+}
+void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
+    // silence compiler warning about unused parameter in a portable way
+    (void) bnep_service;
+};
+#endif
+#elif defined(HAVE_MALLOC)
+bnep_service_t * btstack_memory_bnep_service_get(void){
+    return (bnep_service_t*) malloc(sizeof(bnep_service_t));
+}
+void btstack_memory_bnep_service_free(bnep_service_t *bnep_service){
+    free(bnep_service);
+}
+#else
+#error "Neither HAVE_MALLOC nor MAX_NO_BNEP_SERVICES for struct bnep_service is defined. Please, edit the config file."
+#endif
+
+
+// MARK: bnep_channel_t
+#ifdef MAX_NO_BNEP_CHANNELS
+#if MAX_NO_BNEP_CHANNELS > 0
+static bnep_channel_t bnep_channel_storage[MAX_NO_BNEP_CHANNELS];
+static memory_pool_t bnep_channel_pool;
+bnep_channel_t * btstack_memory_bnep_channel_get(void){
+    return memory_pool_get(&bnep_channel_pool);
+}
+void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
+    memory_pool_free(&bnep_channel_pool, bnep_channel);
+}
+#else
+bnep_channel_t * btstack_memory_bnep_channel_get(void){
+    return NULL;
+}
+void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
+    // silence compiler warning about unused parameter in a portable way
+    (void) bnep_channel;
+};
+#endif
+#elif defined(HAVE_MALLOC)
+bnep_channel_t * btstack_memory_bnep_channel_get(void){
+    return (bnep_channel_t*) malloc(sizeof(bnep_channel_t));
+}
+void btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel){
+    free(bnep_channel);
+}
+#else
+#error "Neither HAVE_MALLOC nor MAX_NO_BNEP_CHANNELS for struct bnep_channel is defined. Please, edit the config file."
+#endif
+
+
 #ifdef HAVE_BLE
+
+// MARK: gatt_client_t
 #ifdef MAX_NO_GATT_CLIENTS
 #if MAX_NO_GATT_CLIENTS > 0
 static gatt_client_t gatt_client_storage[MAX_NO_GATT_CLIENTS];
@@ -430,8 +445,41 @@ void btstack_memory_gatt_client_free(gatt_client_t *gatt_client){
 #else
 #error "Neither HAVE_MALLOC nor MAX_NO_GATT_CLIENTS for struct gatt_client is defined. Please, edit the config file."
 #endif
+
+
+// MARK: gatt_subclient_t
+#ifdef MAX_NO_GATT_SUBCLIENTS
+#if MAX_NO_GATT_SUBCLIENTS > 0
+static gatt_subclient_t gatt_subclient_storage[MAX_NO_GATT_SUBCLIENTS];
+static memory_pool_t gatt_subclient_pool;
+gatt_subclient_t * btstack_memory_gatt_subclient_get(void){
+    return memory_pool_get(&gatt_subclient_pool);
+}
+void btstack_memory_gatt_subclient_free(gatt_subclient_t *gatt_subclient){
+    memory_pool_free(&gatt_subclient_pool, gatt_subclient);
+}
+#else
+gatt_subclient_t * btstack_memory_gatt_subclient_get(void){
+    return NULL;
+}
+void btstack_memory_gatt_subclient_free(gatt_subclient_t *gatt_subclient){
+    // silence compiler warning about unused parameter in a portable way
+    (void) gatt_subclient;
+};
+#endif
+#elif defined(HAVE_MALLOC)
+gatt_subclient_t * btstack_memory_gatt_subclient_get(void){
+    return (gatt_subclient_t*) malloc(sizeof(gatt_subclient_t));
+}
+void btstack_memory_gatt_subclient_free(gatt_subclient_t *gatt_subclient){
+    free(gatt_subclient);
+}
+#else
+#error "Neither HAVE_MALLOC nor MAX_NO_GATT_SUBCLIENTS for struct gatt_subclient is defined. Please, edit the config file."
 #endif
 
+
+#endif
 // init
 void btstack_memory_init(void){
 #if MAX_NO_HCI_CONNECTIONS > 0
@@ -461,10 +509,18 @@ void btstack_memory_init(void){
 #if MAX_NO_DB_MEM_SERVICES > 0
     memory_pool_create(&db_mem_service_pool, db_mem_service_storage, MAX_NO_DB_MEM_SERVICES, sizeof(db_mem_service_t));
 #endif
+#if MAX_NO_BNEP_SERVICES > 0
+    memory_pool_create(&bnep_service_pool, bnep_service_storage, MAX_NO_BNEP_SERVICES, sizeof(bnep_service_t));
+#endif
+#if MAX_NO_BNEP_CHANNELS > 0
+    memory_pool_create(&bnep_channel_pool, bnep_channel_storage, MAX_NO_BNEP_CHANNELS, sizeof(bnep_channel_t));
+#endif
 #ifdef HAVE_BLE
 #if MAX_NO_GATT_CLIENTS > 0
     memory_pool_create(&gatt_client_pool, gatt_client_storage, MAX_NO_GATT_CLIENTS, sizeof(gatt_client_t));
 #endif
+#if MAX_NO_GATT_SUBCLIENTS > 0
+    memory_pool_create(&gatt_subclient_pool, gatt_subclient_storage, MAX_NO_GATT_SUBCLIENTS, sizeof(gatt_subclient_t));
+#endif
 #endif
 }
-
diff --git a/src/btstack_memory.h b/src/btstack_memory.h
index b82fd338b..a05c1d97b 100644
--- a/src/btstack_memory.h
+++ b/src/btstack_memory.h
@@ -1,5 +1,6 @@
+
 /*
- * Copyright (C) 2009-2012 by Matthias Ringwald
+ * Copyright (C) 2009 by BlueKitchen GmbH
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -17,7 +18,7 @@
  *    personal benefit and not for any commercial purpose or for
  *    monetary gain.
  *
- * THIS SOFTWARE IS PROVIDED BY MATTHIAS RINGWALD AND CONTRIBUTORS
+ * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
@@ -30,10 +31,13 @@
  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * Please inquire about commercial licensing options at btstack@ringwald.ch
+ * Please inquire about commercial licensing options at 
+ * contact@bluekitchen-gmbh.com
  *
  */
 
+
+
 /*
  *  btstsack_memory.h
  *
@@ -62,14 +66,17 @@ extern "C" {
 
 void btstack_memory_init(void);
 
+// hci_connection
 hci_connection_t * btstack_memory_hci_connection_get(void);
 void   btstack_memory_hci_connection_free(hci_connection_t *hci_connection);
-    
+
+// l2cap_service, l2cap_channel
 l2cap_service_t * btstack_memory_l2cap_service_get(void);
 void   btstack_memory_l2cap_service_free(l2cap_service_t *l2cap_service);
 l2cap_channel_t * btstack_memory_l2cap_channel_get(void);
 void   btstack_memory_l2cap_channel_free(l2cap_channel_t *l2cap_channel);
 
+// rfcomm_multiplexer, rfcomm_service, rfcomm_channel
 rfcomm_multiplexer_t * btstack_memory_rfcomm_multiplexer_get(void);
 void   btstack_memory_rfcomm_multiplexer_free(rfcomm_multiplexer_t *rfcomm_multiplexer);
 rfcomm_service_t * btstack_memory_rfcomm_service_get(void);
@@ -77,21 +84,26 @@ void   btstack_memory_rfcomm_service_free(rfcomm_service_t *rfcomm_service);
 rfcomm_channel_t * btstack_memory_rfcomm_channel_get(void);
 void   btstack_memory_rfcomm_channel_free(rfcomm_channel_t *rfcomm_channel);
 
-bnep_service_t * btstack_memory_bnep_service_get(void);
-void   btstack_memory_bnep_service_free(bnep_service_t *bnep_service);
-bnep_channel_t * btstack_memory_bnep_channel_get(void);
-void   btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel);    
-
+// db_mem_device_name, db_mem_device_link_key, db_mem_service
 db_mem_device_name_t * btstack_memory_db_mem_device_name_get(void);
 void   btstack_memory_db_mem_device_name_free(db_mem_device_name_t *db_mem_device_name);
 db_mem_device_link_key_t * btstack_memory_db_mem_device_link_key_get(void);
 void   btstack_memory_db_mem_device_link_key_free(db_mem_device_link_key_t *db_mem_device_link_key);
 db_mem_service_t * btstack_memory_db_mem_service_get(void);
 void   btstack_memory_db_mem_service_free(db_mem_service_t *db_mem_service);
-    
+
+// bnep_service, bnep_channel
+bnep_service_t * btstack_memory_bnep_service_get(void);
+void   btstack_memory_bnep_service_free(bnep_service_t *bnep_service);
+bnep_channel_t * btstack_memory_bnep_channel_get(void);
+void   btstack_memory_bnep_channel_free(bnep_channel_t *bnep_channel);
+
 #ifdef HAVE_BLE
+// gatt_client, gatt_subclient
 gatt_client_t * btstack_memory_gatt_client_get(void);
 void   btstack_memory_gatt_client_free(gatt_client_t *gatt_client);
+gatt_subclient_t * btstack_memory_gatt_subclient_get(void);
+void   btstack_memory_gatt_subclient_free(gatt_subclient_t *gatt_subclient);
 #endif
 
 #if defined __cplusplus
@@ -99,3 +111,4 @@ void   btstack_memory_gatt_client_free(gatt_client_t *gatt_client);
 #endif
 
 #endif // __BTSTACK_MEMORY_H
+
diff --git a/tools/btstack_memory_generator.py b/tools/btstack_memory_generator.py
index 45116c386..4efaaa8c7 100755
--- a/tools/btstack_memory_generator.py
+++ b/tools/btstack_memory_generator.py
@@ -1,5 +1,104 @@
 #!/usr/bin/env python
 
+copyright = """
+/*
+ * Copyright (C) 2009 by BlueKitchen GmbH
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holders nor the names of
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
+ * 4. Any redistribution, use, or modification is done solely for
+ *    personal benefit and not for any commercial purpose or for
+ *    monetary gain.
+ *
+ * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
+ * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
+ * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Please inquire about commercial licensing options at 
+ * contact@bluekitchen-gmbh.com
+ *
+ */
+"""
+
+hfile_header_begin = """
+
+/*
+ *  btstsack_memory.h
+ *
+ *  @brief BTstack memory management via configurable memory pools
+ *
+ */
+
+#ifndef __BTSTACK_MEMORY_H
+#define __BTSTACK_MEMORY_H
+
+#if defined __cplusplus
+extern "C" {
+#endif
+
+#include "btstack-config.h"
+    
+#include "hci.h"
+#include "l2cap.h"
+#include "rfcomm.h"
+#include "bnep.h"
+#include "remote_device_db.h"
+
+#ifdef HAVE_BLE
+#include "gatt_client.h"
+#endif
+
+void btstack_memory_init(void);
+"""
+
+hfile_header_end = """
+#if defined __cplusplus
+}
+#endif
+
+#endif // __BTSTACK_MEMORY_H
+"""
+
+cfile_header_begin = """
+/*
+ *  btstsack_memory.h
+ *
+ *  @brief BTstack memory management via configurable memory pools
+ *
+ *  @note code semi-atuomatically generated by tools/btstack_memory_generator.py
+ *
+ */
+
+#include "btstack_memory.h"
+#include <btstack/memory_pool.h>
+
+#include <stdlib.h>
+
+#include "btstack-config.h"
+#include "hci.h"
+#include "l2cap.h"
+#include "rfcomm.h"
+"""
+
 header_template = """STRUCT_NAME_t * btstack_memory_STRUCT_NAME_get(void);
 void   btstack_memory_STRUCT_NAME_free(STRUCT_NAME_t *STRUCT_NAME);"""
 
@@ -40,6 +139,9 @@ init_template = """#if POOL_COUNT > 0
     memory_pool_create(&STRUCT_NAME_pool, STRUCT_NAME_storage, POOL_COUNT, sizeof(STRUCT_TYPE));
 #endif"""
 
+def writeln(f, data):
+    f.write(data + "\n")
+
 def replacePlaceholder(template, struct_name):
     struct_type = struct_name + '_t'
     pool_count = "MAX_NO_" + struct_name.upper() + "S"
@@ -47,20 +149,55 @@ def replacePlaceholder(template, struct_name):
     snippet = template.replace("STRUCT_TYPE", struct_type).replace("STRUCT_NAME", struct_name).replace("POOL_COUNT", pool_count)
     return snippet
     
-list_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", "gatt_client", "bnep_service", "bnep_channel"]
+list_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"]]
+list_of_le_structs = [["gatt_client", "gatt_subclient"]]
 
-print "// header file"
-for struct_name in list_of_structs:
-    print replacePlaceholder(header_template, struct_name)
+file_name = "../src/btstack_memory"
 
-print "// template code"
-for struct_name in list_of_structs:
-    print replacePlaceholder(code_template, struct_name)
 
-print "// init"
-print "void btstack_memory_init(void){"
-for struct_name in list_of_structs:
-    print replacePlaceholder(init_template, struct_name)
-print "}"
+f = open(file_name+".h", "w")
+writeln(f, copyright)
+writeln(f, hfile_header_begin)
+for struct_names in list_of_structs:
+    writeln(f, "// "+ ", ".join(struct_names))
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(header_template, struct_name))
+    writeln(f, "")
+writeln(f, "#ifdef HAVE_BLE")
+for struct_names in list_of_le_structs:
+    writeln(f, "// "+ ", ".join(struct_names))
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(header_template, struct_name))
+writeln(f, "#endif")
+writeln(f, hfile_header_end)
+f.close();
 
+
+f = open(file_name+".c", "w")
+writeln(f, copyright)
+writeln(f, cfile_header_begin)
+for struct_names in list_of_structs:
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(code_template, struct_name))
+    writeln(f, "")
+writeln(f, "#ifdef HAVE_BLE")
+for struct_names in list_of_le_structs:
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(code_template, struct_name))
+    writeln(f, "")
+writeln(f, "#endif")
+
+
+writeln(f, "// init")
+writeln(f, "void btstack_memory_init(void){")
+for struct_names in list_of_structs:
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(init_template, struct_name))
+writeln(f, "#ifdef HAVE_BLE")
+for struct_names in list_of_le_structs:
+    for struct_name in struct_names:
+        writeln(f, replacePlaceholder(init_template, struct_name))
+writeln(f, "#endif")
+writeln(f, "}")
+f.close();