1#!/usr/bin/env python 2# 3# Create project files for all BTstack embedded examples in local port/esp32 folder 4 5import os 6import shutil 7import sys 8import time 9import subprocess 10 11mk_template = '''# 12# BTstack example 'EXAMPLE' for ESP32 port 13# 14# Generated by TOOL 15# On DATE 16 17PROJECT_NAME := EXAMPLE 18 19include $(IDF_PATH)/make/project.mk 20''' 21 22gatt_update_template = '''#!/usr/bin/env python 23# 24# Update EXAMPLE.h from EXAMPLE.gatt 25import os 26import sys 27 28script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 29btstack_root = script_path + '/../../../' 30compile_gatt = btstack_root + 'tool/compile_gatt.py' 31print("Creating src/EXAMPLE.h from EXAMPLE.gatt") 32sys.argv= [compile_gatt, btstack_root + "example/EXAMPLE.gatt", script_path + "/main/EXAMPLE.h"] 33exec(open(compile_gatt).read(), globals()) 34''' 35 36# get script path 37script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 38 39# path to examples 40examples_embedded = script_path + "/../../example/" 41 42# path to zephyr/samples/btstack 43apps_btstack = "" 44 45print("Creating examples in local folder") 46 47# iterate over btstack examples 48for file in os.listdir(examples_embedded): 49 if not file.endswith(".c"): 50 continue 51 if file in ['panu_demo.c', 'sco_demo_util.c']: 52 continue 53 54 example = file[:-2] 55 gatt_path = examples_embedded + example + ".gatt" 56 57 # create folder 58 apps_folder = apps_btstack + example + "/" 59 if os.path.exists(apps_folder): 60 shutil.rmtree(apps_folder) 61 os.makedirs(apps_folder) 62 63 # copy files 64 for item in ['sdkconfig', 'set_port.sh']: 65 shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item) 66 67 # mark set_port.sh as executable 68 os.chmod(apps_folder + '/set_port.sh', 0o755) 69 70 71 # create Makefile file 72 with open(apps_folder + "Makefile", "wt") as fout: 73 fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 74 75 # create main folder 76 main_folder = apps_folder + "main/" 77 if not os.path.exists(main_folder): 78 os.makedirs(main_folder) 79 80 # copy example file 81 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 82 83 # add sco_demo_util.c for audio examples 84 if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hf_demo']: 85 shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/') 86 shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/') 87 88 # add component.mk file to main folder 89 shutil.copyfile(script_path + '/template/main/component.mk', apps_folder + "/main/component.mk") 90 91 # create update_gatt.sh if .gatt file is present 92 gatt_path = examples_embedded + example + ".gatt" 93 if os.path.exists(gatt_path): 94 update_gatt_script = apps_folder + "update_gatt_db.py" 95 with open(update_gatt_script, "wt") as fout: 96 fout.write(gatt_update_template.replace("EXAMPLE", example)) 97 os.chmod(update_gatt_script, 0o755) 98 subprocess.call(update_gatt_script + "> /dev/null", shell=True) 99 print("- %s including compiled GATT DB" % example) 100 else: 101 print("- %s" % example) 102