1#!/usr/bin/env python3 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 example_files = os.listdir(examples_embedded) 59 example_files.sort() 60 for file in example_files: 61 if not file.endswith(".c"): 62 continue 63 if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'a2dp_sink_demo.c', 'a2dp_source_demo.c']: 64 continue 65 66 example = file[:-2] 67 gatt_path = examples_embedded + example + ".gatt" 68 69 # create project folder 70 project_folder = example_folder + example + "/" 71 create_folder(project_folder) 72 73 # copy some folders 74 for folder in ['.settings', 'script', 'synergy', 'synergy_cfg']: 75 src = project_template + folder 76 dst = project_folder + folder 77 shutil.copytree(src, dst) 78 79 # copy some files 80 for file in ['configuration.xml', 'synergy_cfg.txt', 'TB_S1JA.pincfg']: 81 shutil.copy(project_template + file, project_folder) 82 83 # create src folder 84 src_folder = project_folder + 'src/' 85 create_folder(src_folder) 86 87 # copy files skipping example.c and gatt_streamer_server.h 88 for file in ['btstack_config.h', 'hal_entry.c']: 89 shutil.copy(project_template + "src/" + file, src_folder) 90 91 # copy synergy_gen 92 src = project_template + 'src/synergy_gen' 93 dst = project_folder + 'src/synergy_gen' 94 shutil.copytree(src, dst) 95 96 # copy example file 97 shutil.copyfile(examples_embedded + example + '.c', src_folder + example + ".c") 98 99 # copy init script 100 shutil.copy(port_folder + "/" + init_script, src_folder) 101 102 # add sco_demo_util.c for audio examples 103 if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 104 shutil.copy(examples_embedded + 'sco_demo_util.c', src_folder) 105 shutil.copy(examples_embedded + 'sco_demo_util.h', src_folder) 106 107 # update project files 108 for file in ['.project', '.cproject','btstack_example.jdebug']: 109 with open(project_template + file, 'r') as fin: 110 template = fin.read() 111 file = file.replace('btstack_example',example) 112 with open(project_folder + file, 'wt') as fout: 113 template = template.replace("btstack_example", example) 114 fout.write(template) 115 116 # copy jlink and launch files 117 shutil.copy(project_template + 'btstack_example Debug.jlink', project_folder + example + ' Debug.jlink' ) 118 shutil.copy(project_template + 'btstack_example Debug.launch', project_folder + example + ' Debug.launch') 119 120 # generate .h from .gatt 121 gatt_path = examples_embedded + example + ".gatt" 122 if os.path.exists(gatt_path): 123 update_gatt_script_sh = project_folder + "update_gatt_db.sh" 124 update_gatt_script_bat = project_folder + "update_gatt_db.bat" 125 with open(update_gatt_script_sh, "wt") as fout: 126 fout.write(gatt_update_sh_template.replace("EXAMPLE", example)) 127 os.chmod(update_gatt_script_sh, 0o755) 128 with open(update_gatt_script_bat, "wt") as fout: 129 fout.write(gatt_update_bat_template.replace("EXAMPLE", example).replace("BTSTACK_ROOT", btstack_root).replace("PROJECT_SRC", src_folder)) 130 if os.name == "nt": 131 subprocess.run(["python.exe", port_folder + "/../../tool/compile_gatt.py", gatt_path, src_folder + example + ".h" ], shell=True, capture_output=True) 132 else: 133 subprocess.run(update_gatt_script_sh, shell=True, capture_output=True) 134 print("- %s - converting GATT DB" % example) 135 else: 136 print("- %s" % example) 137 138if __name__ == '__main__': 139 # get script path 140 port_folder = os.path.abspath(os.path.dirname(sys.argv[0])) 141 suffix = '' 142 if len(sys.argv) > 1: 143 suffix = sys.argv[1] 144 fetch_init_script() 145 create_examples(port_folder, suffix) 146 147 148