171174eb4SMatthias 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 1501f72e3dSMatthias Ringwaldinit_script = 'bluetooth_init_cc2564C_1.5.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 5871174eb4SMatthias Ringwald example_files = os.listdir(examples_embedded) 5971174eb4SMatthias Ringwald example_files.sort() 6071174eb4SMatthias Ringwald for file in example_files: 613b5c872aSMatthias Ringwald if not file.endswith(".c"): 623b5c872aSMatthias Ringwald continue 63*b4d037a2SMatthias Ringwald if file in ['panu_demo.c', 'ant_test.c', 'audio_duplex.c', 'mod_player.c']: 64*b4d037a2SMatthias Ringwald continue 65*b4d037a2SMatthias Ringwald if 'demo_util' in file: 663035e4e5SMatthias Ringwald continue 673035e4e5SMatthias Ringwald if file in ['a2dp_sink_demo.c', 'a2dp_source_demo.c', 'hfp_hf_demo.c', 'hfp_ag_demo.c', 'hsp_hs_demo.c', 'hsp_ag_demo.c']: 683b5c872aSMatthias Ringwald continue 693b5c872aSMatthias Ringwald 703b5c872aSMatthias Ringwald example = file[:-2] 713b5c872aSMatthias Ringwald gatt_path = examples_embedded + example + ".gatt" 723b5c872aSMatthias Ringwald 733b5c872aSMatthias Ringwald # create project folder 743b5c872aSMatthias Ringwald project_folder = example_folder + example + "/" 753b5c872aSMatthias Ringwald create_folder(project_folder) 763b5c872aSMatthias Ringwald 773b5c872aSMatthias Ringwald # copy some folders 783b5c872aSMatthias Ringwald for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']: 793b5c872aSMatthias Ringwald src = project_template + folder 803b5c872aSMatthias Ringwald dst = project_folder + folder 813b5c872aSMatthias Ringwald shutil.copytree(src, dst) 823b5c872aSMatthias Ringwald 833b5c872aSMatthias Ringwald # copy some files 843b5c872aSMatthias Ringwald for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']: 853b5c872aSMatthias Ringwald shutil.copy(project_template + file, project_folder) 863b5c872aSMatthias Ringwald 873b5c872aSMatthias Ringwald # create src folder 883b5c872aSMatthias Ringwald src_folder = project_folder + 'src/' 893b5c872aSMatthias Ringwald create_folder(src_folder) 903b5c872aSMatthias Ringwald 913b5c872aSMatthias Ringwald # copy files skipping example.c and gatt_streamer_server.h 923035e4e5SMatthias Ringwald for file in ['btstack_config.h', 'hal_entry.c', 'hal_flash_bank_synergy.c', 'hal_flash_bank_synergy.h']: 933b5c872aSMatthias Ringwald shutil.copy(project_template + "src/" + file, src_folder) 943b5c872aSMatthias Ringwald 953b5c872aSMatthias Ringwald # copy synergy_gen 963b5c872aSMatthias Ringwald src = project_template + 'src/synergy_gen' 973b5c872aSMatthias Ringwald dst = project_folder + 'src/synergy_gen' 983b5c872aSMatthias Ringwald shutil.copytree(src, dst) 993b5c872aSMatthias Ringwald 1003b5c872aSMatthias Ringwald # copy example file 1013b5c872aSMatthias Ringwald shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c") 1023b5c872aSMatthias Ringwald 1033b5c872aSMatthias Ringwald # copy init script 1043b5c872aSMatthias Ringwald shutil.copy(port_folder + "/" + init_script, src_folder) 1053b5c872aSMatthias Ringwald 1063b5c872aSMatthias Ringwald # add sco_demo_util.c for audio examples 1073035e4e5SMatthias Ringwald # if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 1083035e4e5SMatthias Ringwald # shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder) 1093035e4e5SMatthias Ringwald # shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder) 1103b5c872aSMatthias Ringwald 1113b5c872aSMatthias Ringwald # update project files 11271174eb4SMatthias Ringwald for file in ['.project', '.cproject','btstack_example.jdebug']: 1133b5c872aSMatthias Ringwald with open(project_template + file, 'r') as fin: 1143b5c872aSMatthias Ringwald template = fin.read() 11571174eb4SMatthias Ringwald file = file.replace('btstack_example',example) 1163b5c872aSMatthias Ringwald with open(project_folder + file, 'wt') as fout: 1173b5c872aSMatthias Ringwald template = template.replace("btstack_example", example) 1183b5c872aSMatthias Ringwald fout.write(template) 1193b5c872aSMatthias Ringwald 1203b5c872aSMatthias Ringwald # copy jlink and launch files 12171174eb4SMatthias Ringwald shutil.copy(project_template + 'btstack_example Debug.jlink', project_folder + example + ' Debug.jlink' ) 12271174eb4SMatthias Ringwald shutil.copy(project_template + 'btstack_example Debug.launch', project_folder + example + ' Debug.launch') 1233b5c872aSMatthias Ringwald 1243b5c872aSMatthias Ringwald # generate .h from .gatt 1253b5c872aSMatthias Ringwald gatt_path = examples_embedded + example + ".gatt" 1263b5c872aSMatthias Ringwald if os.path.exists(gatt_path): 127c6c20663SMatthias Ringwald update_gatt_script_sh = project_folder + "update_gatt_db.sh" 128c6c20663SMatthias Ringwald update_gatt_script_bat = project_folder + "update_gatt_db.bat" 129c6c20663SMatthias Ringwald with open(update_gatt_script_sh, "wt") as fout: 130c6c20663SMatthias Ringwald fout.write(gatt_update_sh_template.replace("EXAMPLE", example)) 131c6c20663SMatthias Ringwald os.chmod(update_gatt_script_sh, 0o755) 132c6c20663SMatthias Ringwald with open(update_gatt_script_bat, "wt") as fout: 133c6c20663SMatthias Ringwald fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder)) 134c6c20663SMatthias Ringwald if os.name == "nt": 135c6c20663SMatthias Ringwald subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True) 136c6c20663SMatthias Ringwald else: 13771174eb4SMatthias Ringwald subprocess.run(update_gatt_script_sh, shell=True, capture_output=True) 1383b5c872aSMatthias Ringwald print("- %s - converting GATT DB" % example) 1393b5c872aSMatthias Ringwald else: 1403b5c872aSMatthias Ringwald print("- %s" % example) 1413b5c872aSMatthias Ringwald 1423b5c872aSMatthias Ringwaldif __name__ == '__main__': 1433b5c872aSMatthias Ringwald # get script path 1443b5c872aSMatthias Ringwald port_folder = os.path.abspath(os.path.dirname(sys.argv[0])) 1453b5c872aSMatthias Ringwald suffix = '' 1463b5c872aSMatthias Ringwald if len(sys.argv) > 1: 1473b5c872aSMatthias Ringwald suffix = sys.argv[1] 1483b5c872aSMatthias Ringwald fetch_init_script() 1493b5c872aSMatthias Ringwald create_examples(port_folder, suffix) 1503b5c872aSMatthias Ringwald 1513b5c872aSMatthias Ringwald 152