xref: /btstack/port/renesas-tb-s1ja-cc256x/create_examples.py (revision 71174eb4bf0e56892ce2f123b9ca470a97a23224)
1*71174eb4SMatthias Ringwald#!/usr/bin/env python3
23b5c872aSMatthias Ringwald#
33b5c872aSMatthias Ringwald# Create project files for all BTstack embedded examples in local port/renesas-tb-s1ja-cc256x folder
43b5c872aSMatthias Ringwald
53b5c872aSMatthias Ringwaldimport os
63b5c872aSMatthias Ringwaldimport shutil
73b5c872aSMatthias Ringwaldimport sys
83b5c872aSMatthias Ringwaldimport time
93b5c872aSMatthias Ringwaldimport subprocess
103b5c872aSMatthias Ringwald
113b5c872aSMatthias Ringwald# use for CC2564B
123b5c872aSMatthias Ringwald# init_script = 'bluetooth_init_cc2564B_1.8_BT_Spec_4.1.c'
133b5c872aSMatthias Ringwald
143b5c872aSMatthias Ringwald# use for CC2564C
153b5c872aSMatthias Ringwaldinit_script = 'bluetooth_init_cc2564C_1.3.c'
163b5c872aSMatthias Ringwald
17c6c20663SMatthias Ringwaldgatt_update_bat_template = '''python.exe BTSTACK_ROOT\\tool\\compile_gatt.py BTSTACK_ROOT\\example\\EXAMPLE.gatt PROJECT_SRC\\EXAMPLE.h
18c6c20663SMatthias Ringwald'''
19c6c20663SMatthias Ringwald
20c6c20663SMatthias Ringwaldgatt_update_sh_template = '''#!/bin/sh
213b5c872aSMatthias RingwaldDIR=`dirname $0`
223b5c872aSMatthias RingwaldBTSTACK_ROOT=$DIR/../../../..
233b5c872aSMatthias Ringwaldecho "Creating EXAMPLE.h from EXAMPLE.gatt"
24c6c20663SMatthias Ringwald$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/src/EXAMPLE.h
253b5c872aSMatthias Ringwald'''
263b5c872aSMatthias Ringwald
273b5c872aSMatthias Ringwalddef fetch_init_script():
283b5c872aSMatthias Ringwald    # print("Fetch CC256x initscript: " + init_script)
293b5c872aSMatthias Ringwald    # subprocess.call("make -f ../../chipset/cc256x/Makefile.inc BTSTACK_ROOT=../.. " + init_script, shell=True)
303b5c872aSMatthias Ringwald    print("Using local CC256x initscript: " + init_script)
313b5c872aSMatthias Ringwald
323b5c872aSMatthias Ringwalddef create_folder(path):
333b5c872aSMatthias Ringwald    if os.path.exists(path):
343b5c872aSMatthias Ringwald        shutil.rmtree(path)
353b5c872aSMatthias Ringwald    os.makedirs(path)
363b5c872aSMatthias Ringwald
373b5c872aSMatthias Ringwalddef create_examples(port_folder, suffix):
38c6c20663SMatthias Ringwald
39c6c20663SMatthias Ringwald	# btstack root
40c6c20663SMatthias Ringwald    btstack_root = os.path.abspath(port_folder + "/../..")
41c6c20663SMatthias Ringwald
423b5c872aSMatthias Ringwald    # path to examples
43c6c20663SMatthias Ringwald    examples_embedded = btstack_root + "/example/"
443b5c872aSMatthias Ringwald
453b5c872aSMatthias Ringwald    # path to project template
463b5c872aSMatthias Ringwald    project_template = port_folder + "/template/btstack_example/"
473b5c872aSMatthias Ringwald
483b5c872aSMatthias Ringwald    # path to example projects
493b5c872aSMatthias Ringwald    example_folder = port_folder + "/example" + suffix + "/"
503b5c872aSMatthias Ringwald
51c6c20663SMatthias Ringwald
523b5c872aSMatthias Ringwald    print("Creating example folder")
533b5c872aSMatthias Ringwald    create_folder(example_folder)
543b5c872aSMatthias Ringwald
553b5c872aSMatthias Ringwald    print("Creating example projects in example folder")
563b5c872aSMatthias Ringwald
573b5c872aSMatthias Ringwald    # iterate over btstack examples
58*71174eb4SMatthias Ringwald    example_files = os.listdir(examples_embedded)
59*71174eb4SMatthias Ringwald    example_files.sort()
60*71174eb4SMatthias Ringwald    for file in example_files:
613b5c872aSMatthias Ringwald        if not file.endswith(".c"):
623b5c872aSMatthias Ringwald            continue
63*71174eb4SMatthias Ringwald        if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'a2dp_sink_demo.c', 'a2dp_source_demo.c']:
643b5c872aSMatthias Ringwald            continue
653b5c872aSMatthias Ringwald
663b5c872aSMatthias Ringwald        example = file[:-2]
673b5c872aSMatthias Ringwald        gatt_path = examples_embedded + example + ".gatt"
683b5c872aSMatthias Ringwald
693b5c872aSMatthias Ringwald        # create project folder
703b5c872aSMatthias Ringwald        project_folder = example_folder + example + "/"
713b5c872aSMatthias Ringwald        create_folder(project_folder)
723b5c872aSMatthias Ringwald
733b5c872aSMatthias Ringwald        # copy some folders
743b5c872aSMatthias Ringwald        for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']:
753b5c872aSMatthias Ringwald            src = project_template + folder
763b5c872aSMatthias Ringwald            dst = project_folder   + folder
773b5c872aSMatthias Ringwald            shutil.copytree(src, dst)
783b5c872aSMatthias Ringwald
793b5c872aSMatthias Ringwald        # copy some files
803b5c872aSMatthias Ringwald        for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']:
813b5c872aSMatthias Ringwald            shutil.copy(project_template + file, project_folder)
823b5c872aSMatthias Ringwald
833b5c872aSMatthias Ringwald        # create src folder
843b5c872aSMatthias Ringwald        src_folder = project_folder + 'src/'
853b5c872aSMatthias Ringwald        create_folder(src_folder)
863b5c872aSMatthias Ringwald
873b5c872aSMatthias Ringwald        # copy files skipping example.c and gatt_streamer_server.h
883b5c872aSMatthias Ringwald        for file in ['btstack_config.h', 'hal_entry.c']:
893b5c872aSMatthias Ringwald            shutil.copy(project_template + "src/" + file, src_folder)
903b5c872aSMatthias Ringwald
913b5c872aSMatthias Ringwald        # copy synergy_gen
923b5c872aSMatthias Ringwald        src = project_template + 'src/synergy_gen'
933b5c872aSMatthias Ringwald        dst = project_folder   + 'src/synergy_gen'
943b5c872aSMatthias Ringwald        shutil.copytree(src, dst)
953b5c872aSMatthias Ringwald
963b5c872aSMatthias Ringwald        # copy example file
973b5c872aSMatthias Ringwald        shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c")
983b5c872aSMatthias Ringwald
993b5c872aSMatthias Ringwald        # copy init script
1003b5c872aSMatthias Ringwald        shutil.copy(port_folder + "/" + init_script, src_folder)
1013b5c872aSMatthias Ringwald
1023b5c872aSMatthias Ringwald        # add sco_demo_util.c for audio examples
1033b5c872aSMatthias Ringwald        if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']:
1043b5c872aSMatthias Ringwald            shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder)
1053b5c872aSMatthias Ringwald            shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder)
1063b5c872aSMatthias Ringwald
1073b5c872aSMatthias Ringwald        # update project files
108*71174eb4SMatthias Ringwald        for file in ['.project', '.cproject','btstack_example.jdebug']:
1093b5c872aSMatthias Ringwald            with open(project_template + file, 'r') as fin:
1103b5c872aSMatthias Ringwald                template = fin.read()
111*71174eb4SMatthias Ringwald            file = file.replace('btstack_example',example)
1123b5c872aSMatthias Ringwald            with open(project_folder + file, 'wt') as fout:
1133b5c872aSMatthias Ringwald                template = template.replace("btstack_example", example)
1143b5c872aSMatthias Ringwald                fout.write(template)
1153b5c872aSMatthias Ringwald
1163b5c872aSMatthias Ringwald        # copy jlink and launch files
117*71174eb4SMatthias Ringwald        shutil.copy(project_template + 'btstack_example Debug.jlink',  project_folder + example + ' Debug.jlink' )
118*71174eb4SMatthias Ringwald        shutil.copy(project_template + 'btstack_example Debug.launch', project_folder + example + ' Debug.launch')
1193b5c872aSMatthias Ringwald
1203b5c872aSMatthias Ringwald        # generate .h from .gatt
1213b5c872aSMatthias Ringwald        gatt_path = examples_embedded + example + ".gatt"
1223b5c872aSMatthias Ringwald        if os.path.exists(gatt_path):
123c6c20663SMatthias Ringwald            update_gatt_script_sh  = project_folder + "update_gatt_db.sh"
124c6c20663SMatthias Ringwald            update_gatt_script_bat = project_folder + "update_gatt_db.bat"
125c6c20663SMatthias Ringwald            with open(update_gatt_script_sh, "wt") as fout:
126c6c20663SMatthias Ringwald                fout.write(gatt_update_sh_template.replace("EXAMPLE", example))
127c6c20663SMatthias Ringwald            os.chmod(update_gatt_script_sh, 0o755)
128c6c20663SMatthias Ringwald            with open(update_gatt_script_bat, "wt") as fout:
129c6c20663SMatthias Ringwald                fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder))
130c6c20663SMatthias Ringwald            if os.name == "nt":
131c6c20663SMatthias Ringwald                subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True)
132c6c20663SMatthias Ringwald            else:
133*71174eb4SMatthias Ringwald	            subprocess.run(update_gatt_script_sh, shell=True, capture_output=True)
1343b5c872aSMatthias Ringwald            print("- %s - converting GATT DB" % example)
1353b5c872aSMatthias Ringwald        else:
1363b5c872aSMatthias Ringwald            print("- %s" % example)
1373b5c872aSMatthias Ringwald
1383b5c872aSMatthias Ringwaldif __name__ == '__main__':
1393b5c872aSMatthias Ringwald    # get script path
1403b5c872aSMatthias Ringwald    port_folder = os.path.abspath(os.path.dirname(sys.argv[0]))
1413b5c872aSMatthias Ringwald    suffix = ''
1423b5c872aSMatthias Ringwald    if len(sys.argv) > 1:
1433b5c872aSMatthias Ringwald        suffix = sys.argv[1]
1443b5c872aSMatthias Ringwald    fetch_init_script()
1453b5c872aSMatthias Ringwald    create_examples(port_folder, suffix)
1463b5c872aSMatthias Ringwald
1473b5c872aSMatthias Ringwald
148