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