13b5c872aSMatthias Ringwald#!/usr/bin/env python 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 17*c6c20663SMatthias Ringwaldgatt_update_bat_template = '''python.exe BTSTACK_ROOT\\tool\\compile_gatt.py BTSTACK_ROOT\\example\\EXAMPLE.gatt PROJECT_SRC\\EXAMPLE.h 18*c6c20663SMatthias Ringwald''' 19*c6c20663SMatthias Ringwald 20*c6c20663SMatthias Ringwaldgatt_update_sh_template = '''#!/bin/sh 213b5c872aSMatthias RingwaldDIR=`dirname $0` 223b5c872aSMatthias RingwaldBTSTACK_ROOT=$DIR/../../../.. 233b5c872aSMatthias Ringwaldecho "Creating EXAMPLE.h from EXAMPLE.gatt" 24*c6c20663SMatthias 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): 38*c6c20663SMatthias Ringwald 39*c6c20663SMatthias Ringwald # btstack root 40*c6c20663SMatthias Ringwald btstack_root = os.path.abspath(port_folder + "/../..") 41*c6c20663SMatthias Ringwald 423b5c872aSMatthias Ringwald # path to examples 43*c6c20663SMatthias 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 51*c6c20663SMatthias 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 583b5c872aSMatthias Ringwald for file in os.listdir(examples_embedded): 593b5c872aSMatthias Ringwald if not file.endswith(".c"): 603b5c872aSMatthias Ringwald continue 613b5c872aSMatthias Ringwald if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']: 623b5c872aSMatthias Ringwald continue 633b5c872aSMatthias Ringwald 643b5c872aSMatthias Ringwald example = file[:-2] 653b5c872aSMatthias Ringwald gatt_path = examples_embedded + example + ".gatt" 663b5c872aSMatthias Ringwald 673b5c872aSMatthias Ringwald # create project folder 683b5c872aSMatthias Ringwald project_folder = example_folder + example + "/" 693b5c872aSMatthias Ringwald create_folder(project_folder) 703b5c872aSMatthias Ringwald 713b5c872aSMatthias Ringwald # copy some folders 723b5c872aSMatthias Ringwald for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']: 733b5c872aSMatthias Ringwald src = project_template + folder 743b5c872aSMatthias Ringwald dst = project_folder + folder 753b5c872aSMatthias Ringwald shutil.copytree(src, dst) 763b5c872aSMatthias Ringwald 773b5c872aSMatthias Ringwald # copy some files 783b5c872aSMatthias Ringwald for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']: 793b5c872aSMatthias Ringwald shutil.copy(project_template + file, project_folder) 803b5c872aSMatthias Ringwald 813b5c872aSMatthias Ringwald # create src folder 823b5c872aSMatthias Ringwald src_folder = project_folder + 'src/' 833b5c872aSMatthias Ringwald create_folder(src_folder) 843b5c872aSMatthias Ringwald 853b5c872aSMatthias Ringwald # copy files skipping example.c and gatt_streamer_server.h 863b5c872aSMatthias Ringwald for file in ['btstack_config.h', 'hal_entry.c']: 873b5c872aSMatthias Ringwald shutil.copy(project_template + "src/" + file, src_folder) 883b5c872aSMatthias Ringwald 893b5c872aSMatthias Ringwald # copy synergy_gen 903b5c872aSMatthias Ringwald src = project_template + 'src/synergy_gen' 913b5c872aSMatthias Ringwald dst = project_folder + 'src/synergy_gen' 923b5c872aSMatthias Ringwald shutil.copytree(src, dst) 933b5c872aSMatthias Ringwald 943b5c872aSMatthias Ringwald # copy example file 953b5c872aSMatthias Ringwald shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c") 963b5c872aSMatthias Ringwald 973b5c872aSMatthias Ringwald # copy init script 983b5c872aSMatthias Ringwald shutil.copy(port_folder + "/" + init_script, src_folder) 993b5c872aSMatthias Ringwald 1003b5c872aSMatthias Ringwald # add sco_demo_util.c for audio examples 1013b5c872aSMatthias Ringwald if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 1023b5c872aSMatthias Ringwald shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder) 1033b5c872aSMatthias Ringwald shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder) 1043b5c872aSMatthias Ringwald 1053b5c872aSMatthias Ringwald # update project files 1063b5c872aSMatthias Ringwald for file in ['.project', '.cproject']: 1073b5c872aSMatthias Ringwald with open(project_template + file, 'r') as fin: 1083b5c872aSMatthias Ringwald template = fin.read() 1093b5c872aSMatthias Ringwald with open(project_folder + file, 'wt') as fout: 1103b5c872aSMatthias Ringwald template = template.replace("btstack_example", example) 1113b5c872aSMatthias Ringwald fout.write(template) 1123b5c872aSMatthias Ringwald 1133b5c872aSMatthias Ringwald # copy jlink and launch files 1143b5c872aSMatthias Ringwald shutil.copy(project_template + 'btstack_port Debug.jlink', project_folder + example + ' Debug.jlink' ) 1153b5c872aSMatthias Ringwald shutil.copy(project_template + 'btstack_port Debug.launch', project_folder + example + ' Debug.launch') 1163b5c872aSMatthias Ringwald 1173b5c872aSMatthias Ringwald # generate .h from .gatt 1183b5c872aSMatthias Ringwald gatt_path = examples_embedded + example + ".gatt" 1193b5c872aSMatthias Ringwald if os.path.exists(gatt_path): 120*c6c20663SMatthias Ringwald update_gatt_script_sh = project_folder + "update_gatt_db.sh" 121*c6c20663SMatthias Ringwald update_gatt_script_bat = project_folder + "update_gatt_db.bat" 122*c6c20663SMatthias Ringwald with open(update_gatt_script_sh, "wt") as fout: 123*c6c20663SMatthias Ringwald fout.write(gatt_update_sh_template.replace("EXAMPLE", example)) 124*c6c20663SMatthias Ringwald os.chmod(update_gatt_script_sh, 0o755) 125*c6c20663SMatthias Ringwald with open(update_gatt_script_bat, "wt") as fout: 126*c6c20663SMatthias Ringwald fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder)) 127*c6c20663SMatthias Ringwald if os.name == "nt": 128*c6c20663SMatthias Ringwald subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True) 129*c6c20663SMatthias Ringwald else: 130*c6c20663SMatthias Ringwald subprocess.run(update_gatt_script.sh, shell=True, capture_output=True) 1313b5c872aSMatthias Ringwald print("- %s - converting GATT DB" % example) 1323b5c872aSMatthias Ringwald else: 1333b5c872aSMatthias Ringwald print("- %s" % example) 1343b5c872aSMatthias Ringwald 1353b5c872aSMatthias Ringwaldif __name__ == '__main__': 1363b5c872aSMatthias Ringwald # get script path 1373b5c872aSMatthias Ringwald port_folder = os.path.abspath(os.path.dirname(sys.argv[0])) 1383b5c872aSMatthias Ringwald suffix = '' 1393b5c872aSMatthias Ringwald if len(sys.argv) > 1: 1403b5c872aSMatthias Ringwald suffix = sys.argv[1] 1413b5c872aSMatthias Ringwald fetch_init_script() 1423b5c872aSMatthias Ringwald create_examples(port_folder, suffix) 1433b5c872aSMatthias Ringwald 1443b5c872aSMatthias Ringwald 145