xref: /btstack/port/renesas-tb-s1ja-cc256x/create_examples.py (revision 84ede5298a0e78e13fa262ae22b02aea335c90d6)
1#!/usr/bin/env python
2#
3# Create project files for all BTstack embedded examples in local port/renesas-tb-s1ja-cc256x folder
4
5import os
6import shutil
7import sys
8import time
9import subprocess
10
11# use for CC2564B
12# init_script = 'bluetooth_init_cc2564B_1.8_BT_Spec_4.1.c'
13
14# use for CC2564C
15init_script = 'bluetooth_init_cc2564C_1.3.c'
16
17gatt_update_bat_template = '''python.exe BTSTACK_ROOT\\tool\\compile_gatt.py BTSTACK_ROOT\\example\\EXAMPLE.gatt PROJECT_SRC\\EXAMPLE.h
18'''
19
20gatt_update_sh_template = '''#!/bin/sh
21DIR=`dirname $0`
22BTSTACK_ROOT=$DIR/../../../..
23echo "Creating EXAMPLE.h from EXAMPLE.gatt"
24$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/src/EXAMPLE.h
25'''
26
27def fetch_init_script():
28    # print("Fetch CC256x initscript: " + init_script)
29    # subprocess.call("make -f ../../chipset/cc256x/Makefile.inc BTSTACK_ROOT=../.. " + init_script, shell=True)
30    print("Using local CC256x initscript: " + init_script)
31
32def create_folder(path):
33    if os.path.exists(path):
34        shutil.rmtree(path)
35    os.makedirs(path)
36
37def create_examples(port_folder, suffix):
38
39	# btstack root
40    btstack_root = os.path.abspath(port_folder + "/../..")
41
42    # path to examples
43    examples_embedded = btstack_root + "/example/"
44
45    # path to project template
46    project_template = port_folder + "/template/btstack_example/"
47
48    # path to example projects
49    example_folder = port_folder + "/example" + suffix + "/"
50
51
52    print("Creating example folder")
53    create_folder(example_folder)
54
55    print("Creating example projects in example folder")
56
57    # iterate over btstack examples
58    for file in os.listdir(examples_embedded):
59        if not file.endswith(".c"):
60            continue
61        if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']:
62            continue
63
64        example = file[:-2]
65        gatt_path = examples_embedded + example + ".gatt"
66
67        # create project folder
68        project_folder = example_folder + example + "/"
69        create_folder(project_folder)
70
71        # copy some folders
72        for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']:
73            src = project_template + folder
74            dst = project_folder   + folder
75            shutil.copytree(src, dst)
76
77        # copy some files
78        for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']:
79            shutil.copy(project_template + file, project_folder)
80
81        # create src folder
82        src_folder = project_folder + 'src/'
83        create_folder(src_folder)
84
85        # copy files skipping example.c and gatt_streamer_server.h
86        for file in ['btstack_config.h', 'hal_entry.c']:
87            shutil.copy(project_template + "src/" + file, src_folder)
88
89        # copy synergy_gen
90        src = project_template + 'src/synergy_gen'
91        dst = project_folder   + 'src/synergy_gen'
92        shutil.copytree(src, dst)
93
94        # copy example file
95        shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c")
96
97        # copy init script
98        shutil.copy(port_folder + "/" + init_script, src_folder)
99
100        # add sco_demo_util.c for audio examples
101        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
102            shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder)
103            shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder)
104
105        # update project files
106        for file in ['.project', '.cproject']:
107            with open(project_template + file, 'r') as fin:
108                template = fin.read()
109            with open(project_folder + file, 'wt') as fout:
110                template = template.replace("btstack_example", example)
111                fout.write(template)
112
113        # copy jlink and launch files
114        shutil.copy(project_template + 'btstack_port Debug.jlink',  project_folder + example + ' Debug.jlink' )
115        shutil.copy(project_template + 'btstack_port Debug.launch', project_folder + example + ' Debug.launch')
116
117        # generate .h from .gatt
118        gatt_path = examples_embedded + example + ".gatt"
119        if os.path.exists(gatt_path):
120            update_gatt_script_sh  = project_folder + "update_gatt_db.sh"
121            update_gatt_script_bat = project_folder + "update_gatt_db.bat"
122            with open(update_gatt_script_sh, "wt") as fout:
123                fout.write(gatt_update_sh_template.replace("EXAMPLE", example))
124            os.chmod(update_gatt_script_sh, 0o755)
125            with open(update_gatt_script_bat, "wt") as fout:
126                fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder))
127            if os.name == "nt":
128                subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True)
129            else:
130	            subprocess.run(update_gatt_script.sh, shell=True, capture_output=True)
131            print("- %s - converting GATT DB" % example)
132        else:
133            print("- %s" % example)
134
135if __name__ == '__main__':
136    # get script path
137    port_folder = os.path.abspath(os.path.dirname(sys.argv[0]))
138    suffix = ''
139    if len(sys.argv) > 1:
140        suffix = sys.argv[1]
141    fetch_init_script()
142    create_examples(port_folder, suffix)
143
144
145