xref: /btstack/port/archive/wiced-h5/create_examples.py (revision 50b47281dcd786f25b8e20527b26e5aaa0460809)
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-h5
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
74# UART API changes in 3.4
75additional_cflags = ""
76if wiced_version < "3.4.0":
77    print("Adding WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ for SDK < 3.4.0")
78    additional_cflags = "-DWICED_UART_READ_DOES_NOT_RETURN_BYTES_READ"
79
80# aes in wiced_security was replaced by mbedTLS in 5.2
81if wiced_version >= "5.2":
82    print("Add WICED_HAVE_MBEDTLS for SDK >= 5.2")
83    additional_cflags += " -DWICED_HAVE_MBEDTLS"
84
85# NOTE: it would be more robust to check for files on disk
86
87# bluetooth firmware image name changed in 5.2
88if wiced_version < "5.2":
89    bluetooth_firmware_file = 'bt_firmware_image.c'
90else:
91    bluetooth_firmware_file = 'bt_firmware_controller.c'
92print("Bluetooth Firmware name: %s" % bluetooth_firmware_file)
93
94# micro-ecc moved in 6.2 from libraries/crypto/micro-ecc to WICED/security/BESL/crypto_internal/micro-ecc
95if wiced_version < "6.2":
96    micro_ecc_component = "crypto/micro-ecc"
97else:
98    micro_ecc_component = "BESL/crypto_internal/micro-ecc"
99print("micro-ecc component: %s" % micro_ecc_component)
100
101# path to examples
102examples_embedded = script_path + "/../../example/"
103
104# path to WICED/apps/btstack
105apps_btstack = wiced_root + "/apps/btstack/"
106
107print("\nCreating examples in apps/btstack:")
108
109# iterate over btstack examples
110for file in os.listdir(examples_embedded):
111    if not file.endswith(".c"):
112        continue
113    if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'pan_lwip_http_server.c']:
114        continue
115    example = file[:-2]
116
117    # create folder
118    apps_folder = apps_btstack + example + "/"
119    if not os.path.exists(apps_folder):
120        os.makedirs(apps_folder)
121
122    # create .mk file
123    with open(apps_folder + example + ".mk", "wt") as fout:
124        fout.write(mk_template.replace("EXAMPLE", example)
125            .replace("TOOL", script_path)
126            .replace("ADDITIONAL_CFLAGS", additional_cflags)
127            .replace("DATE",time.strftime("%c"))
128            .replace('BLUETOOTH_FIRMWARE_FILE', bluetooth_firmware_file)
129            .replace('MICRO_ECC_COMPONENT', micro_ecc_component))
130
131    # create update_gatt.sh if .gatt file is present
132    gatt_path = examples_embedded + example + ".gatt"
133    if os.path.exists(gatt_path):
134        update_gatt_script = apps_folder + "update_gatt_db.sh"
135        with open(update_gatt_script, "wt") as fout:
136            fout.write(gatt_update_template.replace("EXAMPLE", example))
137        os.chmod(update_gatt_script, 0o755)
138        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
139        print("- %s including compiled GATT DB" % example)
140    else:
141        print("- %s" % example)
142
143