1#!/usr/bin/env python3 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 22component_mk_gatt_add_on = ''' 23# app depends on compiled gatt db 24EXAMPLE.o: EXAMPLE.h 25 26# rule to compile gatt db 27EXAMPLE.h: $(COMPONENT_PATH)/EXAMPLE.gatt 28\t$(IDF_PATH)/components/btstack/tool/compile_gatt.py $^ $@ 29 30# remove compiled gatt db on clean 31COMPONENT_EXTRA_CLEAN = EXAMPLE.h 32''' 33 34example_cmake_template = ''' 35# BTstack example 'EXAMPLE' for ESP32 port 36# 37# Generated by TOOL 38# On DATE 39 40# The following lines of boilerplate have to be in your project's 41# CMakeLists in this exact order for cmake to work correctly 42cmake_minimum_required(VERSION 3.5) 43 44include($ENV{IDF_PATH}/tools/cmake/project.cmake) 45project(EXAMPLE) 46''' 47 48main_cmake_template = ''' 49idf_component_register( 50 SRCS MAIN_FILES 51 INCLUDE_DIRS "${CMAKE_CURRENT_BINARY_DIR}") 52''' 53 54main_cmake_gatt_add_on = ''' 55if(NOT CMAKE_BUILD_EARLY_EXPANSION) 56 add_custom_command( 57 OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h 58 COMMAND ${CMAKE_SOURCE_DIR}/../../../../tool/compile_gatt.py ${COMPONENT_DIR}/EXAMPLE.gatt ${CMAKE_CURRENT_BINARY_DIR}/EXAMPLE.h 59 DEPENDS EXAMPLE.gatt 60 VERBATIM 61 ) 62 add_custom_target(GATT_DB DEPENDS EXAMPLE.h) 63 add_dependencies(${COMPONENT_LIB} GATT_DB) 64endif() 65''' 66 67def create_examples(script_path, suffix): 68 # path to examples 69 examples_embedded = script_path + "/../../example/" 70 71 # path to samples 72 example_folder = script_path + "/example" + suffix + "/" 73 74 print("Creating examples folder") 75 if not os.path.exists(example_folder): 76 os.makedirs(example_folder) 77 78 print("Creating examples in examples folder") 79 80 # iterate over btstack examples 81 for file in os.listdir(examples_embedded): 82 if not file.endswith(".c"): 83 continue 84 if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c']: 85 continue 86 87 example = file[:-2] 88 gatt_path = examples_embedded + example + ".gatt" 89 90 # create folder 91 apps_folder = example_folder + example + "/" 92 if os.path.exists(apps_folder): 93 shutil.rmtree(apps_folder) 94 os.makedirs(apps_folder) 95 96 # copy files 97 for item in ['sdkconfig']: 98 src = script_path + '/template/' + item 99 if item == 'sdkconfig': 100 src = src + suffix 101 dst = apps_folder + '/' + item 102 shutil.copyfile(src, dst) 103 104 # create Makefile file 105 with open(apps_folder + "Makefile", "wt") as fout: 106 fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 107 108 # create CMakeLists.txt file 109 with open(apps_folder + "CMakeLists.txt", "wt") as fout: 110 fout.write(example_cmake_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("DATE",time.strftime("%c"))) 111 112 # create main folder 113 main_folder = apps_folder + "main/" 114 if not os.path.exists(main_folder): 115 os.makedirs(main_folder) 116 117 # copy main file 118 shutil.copyfile(script_path + '/template/main/main.c', apps_folder + "/main/main.c") 119 120 # copy example file 121 main_files = '"main.c" "' + example + '.c"' 122 shutil.copyfile(examples_embedded + file, apps_folder + "/main/" + example + ".c") 123 124 # add sco_demo_util.c for audio examples 125 if example in ['hfp_ag_demo','hfp_hf_demo', 'hsp_ag_demo', 'hsp_hs_demo']: 126 shutil.copy(examples_embedded + 'sco_demo_util.c', apps_folder + '/main/') 127 shutil.copy(examples_embedded + 'sco_demo_util.h', apps_folder + '/main/') 128 main_files += ' "sco_demo_util.c"' 129 130 # add component.mk file to main folder 131 main_component_mk = apps_folder + "/main/component.mk" 132 shutil.copyfile(script_path + '/template/main/component.mk', main_component_mk) 133 134 # create CMakeLists.txt file 135 main_cmake_file = apps_folder + "/main/CMakeLists.txt" 136 with open(main_cmake_file, "wt") as fout: 137 fout.write(main_cmake_template.replace("MAIN_FILES", main_files)) 138 139 # add rules to compile gatt db if .gatt file is present 140 gatt_path = examples_embedded + example + ".gatt" 141 if os.path.exists(gatt_path): 142 shutil.copy(gatt_path, apps_folder + "/main/" + example + ".gatt") 143 with open(main_component_mk, "a") as fout: 144 fout.write(component_mk_gatt_add_on.replace("EXAMPLE", example)) 145 with open(main_cmake_file, "a") as fout: 146 fout.write(main_cmake_gatt_add_on.replace("EXAMPLE", example)) 147 print("- %s including GATT DB compilation rules" % example) 148 else: 149 print("- %s" % example) 150 151if __name__ == '__main__': 152 # get script path 153 script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 154 suffix = '' 155 if len(sys.argv) > 1: 156 suffix = sys.argv[1] 157 create_examples(script_path, suffix) 158 159 160