1#!/usr/bin/env python3 2# 3# Create project files for all BTstack embedded examples in WICED/apps/btstack 4 5import os 6import shutil 7import sys 8import time 9import subprocess 10 11mk_template = '''# 12# BTstack example 'EXAMPLE' for WICED port 13# 14# Generated by TOOL 15# On DATE 16 17NAME := EXAMPLE 18 19GLOBAL_INCLUDES += . 20 21# Replace Linefeed with -> CRLF 22GLOBAL_DEFINES += CRLF_STDIO_REPLACEMENT 23 24$(NAME)_SOURCES := ../../../libraries/btstack/example/EXAMPLE.c 25$(NAME)_COMPONENTS += btstack/port/wiced-h4 26 27# micro-ecc of WICED tree used for SECP256R1 in LE Secure Connections 28MICRO_ECC := MICRO_ECC_COMPONENT 29 30# Additional CFLAGS for BTstack Component compilation 31BTSTACK_CFLAGS += ADDITIONAL_CFLAGS 32 33# Name of Firmware file 34BT_FIRMWARE_FILE := BLUETOOTH_FIRMWARE_FILE 35''' 36 37gatt_update_template = '''#!/bin/sh 38DIR=`dirname $0` 39BTSTACK_ROOT=$DIR/../../../libraries/btstack 40echo "Creating EXAMPLE.h from EXAMPLE.gatt" 41$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/EXAMPLE.h 42''' 43 44# get script path 45script_path = os.path.abspath(os.path.dirname(sys.argv[0])) 46 47# validate WICED root by reading version.txt 48wiced_root = script_path + "/../../../../" 49wiced_version_txt = "" 50try: 51 with open(wiced_root + 'version.txt', 'r') as fin: 52 wiced_version_txt = fin.read() # Read the contents of the file into memory. 53except: 54 pass 55if not "WICED Version" in wiced_version_txt: 56 print("Cannot find WICED root. Make sure BTstack is checked out in WICED-SDK-X/libraries") 57 sys.exit(1) 58 59# check for 5.2+ version syntax 60if 'Wiced_' in wiced_version_txt: 61 wiced_version_string = (wiced_version_txt.split()[2]).split('_')[1] 62 wiced_version_major = int(wiced_version_string.split('.')[0]) 63 wiced_version_minor = int(wiced_version_string.split('.')[1]) 64else: 65 wiced_version = wiced_version_txt.split()[2] 66 wiced_version_major = int(wiced_version.split('.')[0]) 67 wiced_version_minor = int(wiced_version.split('.')[1]) 68 69wiced_version = "%u.%u" % (wiced_version_major, wiced_version_minor) 70 71# show WICED version 72print("\nFound WICED SDK version: %s" % wiced_version) 73 74additional_cflags = "" 75if wiced_version < "3.4.0": 76 print("Adding WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ for SDK < 3.4.0") 77 additional_cflags = "-DWICED_UART_READ_DOES_NOT_RETURN_BYTES_READ" 78 79# NOTE: it would be more robust to check for files on disk 80 81# bluetooth firmware image name changed in 5.2 82if wiced_version < "5.2": 83 bluetooth_firmware_file = 'bt_firmware_image.c' 84else: 85 bluetooth_firmware_file = 'bt_firmware_controller.c' 86print("Bluetooth Firmware name: %s" % bluetooth_firmware_file) 87 88# micro-ecc moved in 6.2 from libraries/crypto/micro-ecc to WICED/security/BESL/crypto_internal/micro-ecc 89if wiced_version < "6.2": 90 micro_ecc_component = "crypto/micro-ecc" 91else: 92 micro_ecc_component = "BESL/crypto_internal/micro-ecc" 93print("micro-ecc component: %s" % micro_ecc_component) 94 95# path to examples 96examples_embedded = script_path + "/../../example/" 97 98# path to WICED/apps/btstack 99apps_btstack = wiced_root + "/apps/btstack/" 100 101print("\nCreating examples in apps/btstack:") 102 103# iterate over btstack examples 104for file in os.listdir(examples_embedded): 105 if not file.endswith(".c"): 106 continue 107 if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'pan_lwip_http_server.c']: 108 continue 109 example = file[:-2] 110 111 # create folder 112 apps_folder = apps_btstack + example + "/" 113 if not os.path.exists(apps_folder): 114 os.makedirs(apps_folder) 115 116 # create .mk file 117 with open(apps_folder + example + ".mk", "wt") as fout: 118 fout.write(mk_template.replace("EXAMPLE", example) 119 .replace("TOOL", script_path) 120 .replace("ADDITIONAL_CFLAGS", additional_cflags) 121 .replace("DATE",time.strftime("%c")) 122 .replace('BLUETOOTH_FIRMWARE_FILE', bluetooth_firmware_file) 123 .replace('MICRO_ECC_COMPONENT', micro_ecc_component)) 124 125 # create update_gatt.sh if .gatt file is present 126 gatt_path = examples_embedded + example + ".gatt" 127 if os.path.exists(gatt_path): 128 update_gatt_script = apps_folder + "update_gatt_db.sh" 129 with open(update_gatt_script, "wt") as fout: 130 fout.write(gatt_update_template.replace("EXAMPLE", example)) 131 os.chmod(update_gatt_script, 0o755) 132 subprocess.call(update_gatt_script + "> /dev/null", shell=True) 133 print("- %s including compiled GATT DB" % example) 134 else: 135 print("- %s" % example) 136 137