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 18EXTRA_COMPONENT_DIRS := components 19 20include $(IDF_PATH)/make/project.mk 21''' 22 23gatt_update_template = '''#!/bin/sh 24DIR=`dirname $0` 25BTSTACK_ROOT=$DIR/../../../../btstack 26echo "Creating src/EXAMPLE.h from EXAMPLE.gatt" 27$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/main/EXAMPLE.h 28''' 29 30# get script path 31script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 32 33# path to examples 34examples_embedded = script_path + "/../../example/" 35 36# path to zephyr/samples/btstack 37apps_btstack = "" 38 39print("Creating examples in local folder") 40 41# iterate over btstack examples 42for file in os.listdir(examples_embedded): 43 if not file.endswith(".c"): 44 continue 45 46 example = file[:-2] 47 gatt_path = examples_embedded + example + ".gatt" 48 49 # create folder 50 apps_folder = apps_btstack + example + "/" 51 if os.path.exists(apps_folder): 52 shutil.rmtree(apps_folder) 53 os.makedirs(apps_folder) 54 55 # copy files 56 for item in ['sdkconfig']: 57 shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item) 58 59 # create Makefile file 60 with open(apps_folder + "Makefile", "wt") as fout: 61 fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 62 63 # copy components folder 64 shutil.copytree(script_path + '/template/components', apps_folder + '/components') 65 66 # create main folder 67 main_folder = apps_folder + "main/" 68 if not os.path.exists(main_folder): 69 os.makedirs(main_folder) 70 71 # copy example file 72 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 73 74 # add component.mk file to main folder 75 shutil.copyfile(script_path + '/template/main/component.mk', apps_folder + "/main/component.mk") 76 77 # create update_gatt.sh if .gatt file is present 78 gatt_path = examples_embedded + example + ".gatt" 79 if os.path.exists(gatt_path): 80 update_gatt_script = apps_folder + "update_gatt_db.sh" 81 with open(update_gatt_script, "wt") as fout: 82 fout.write(gatt_update_template.replace("EXAMPLE", example)) 83 os.chmod(update_gatt_script, 0o755) 84 subprocess.call(update_gatt_script + "> /dev/null", shell=True) 85 print("- %s including compiled GATT DB" % example) 86 else: 87 print("- %s" % example) 88 89