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