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 if file in ['panu_demo.c', 'sco_demo_util.c']: 46 continue 47 48 example = file[:-2] 49 gatt_path = examples_embedded + example + ".gatt" 50 51 # create folder 52 apps_folder = apps_btstack + example + "/" 53 if os.path.exists(apps_folder): 54 shutil.rmtree(apps_folder) 55 os.makedirs(apps_folder) 56 57 # copy files 58 for item in ['sdkconfig']: 59 shutil.copyfile(script_path + '/template/' + item, apps_folder + '/' + item) 60 61 # create Makefile file 62 with open(apps_folder + "Makefile", "wt") as fout: 63 fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 64 65 # copy components folder 66 shutil.copytree(script_path + '/template/components', apps_folder + '/components') 67 68 # create main folder 69 main_folder = apps_folder + "main/" 70 if not os.path.exists(main_folder): 71 os.makedirs(main_folder) 72 73 # copy example file 74 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 75 76 # add component.mk file to main folder 77 shutil.copyfile(script_path + '/template/main/component.mk', apps_folder + "/main/component.mk") 78 79 # create update_gatt.sh if .gatt file is present 80 gatt_path = examples_embedded + example + ".gatt" 81 if os.path.exists(gatt_path): 82 update_gatt_script = apps_folder + "update_gatt_db.sh" 83 with open(update_gatt_script, "wt") as fout: 84 fout.write(gatt_update_template.replace("EXAMPLE", example)) 85 os.chmod(update_gatt_script, 0o755) 86 subprocess.call(update_gatt_script + "> /dev/null", shell=True) 87 print("- %s including compiled GATT DB" % example) 88 else: 89 print("- %s" % example) 90 91