xref: /btstack/port/wiced-h4/create_examples.py (revision e0ba407f1b814b3d91b90a2bc0e0762487184852)
1#!/usr/bin/env python
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# Additional CFLAGS for BTstack Component compilation
28BTSTACK_CFLAGS += ADDITIONAL_CFLAGS
29
30# Name of Firmware file
31BT_FIRMWARE_FILE := BLUETOOTH_FIRMWARE_FILE
32'''
33
34gatt_update_template = '''#!/bin/sh
35DIR=`dirname $0`
36BTSTACK_ROOT=$DIR/../../../libraries/btstack
37echo "Creating EXAMPLE.h from EXAMPLE.gatt"
38$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/EXAMPLE.h
39'''
40
41# get script path
42script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
43
44# validate WICED root by reading version.txt
45wiced_root = script_path + "/../../../../"
46wiced_version_txt = ""
47try:
48    with open(wiced_root + 'version.txt', 'r') as fin:
49        wiced_version_txt = fin.read()  # Read the contents of the file into memory.
50except:
51    pass
52if not "WICED Version" in wiced_version_txt:
53    print("Cannot find WICED root. Make sure BTstack is checked out in WICED-SDK-X/libraries")
54    sys.exit(1)
55
56# check for 5.2+ version syntax
57if 'Wiced_' in wiced_version_txt:
58    wiced_version_string = (wiced_version_txt.split()[2]).split('_')[1]
59    wiced_version_major = int(wiced_version_string.split('.')[0])
60    wiced_version_minor = int(wiced_version_string.split('.')[1])
61else:
62    wiced_version = wiced_version_txt.split()[2]
63    wiced_version_major = int(wiced_version.split('.')[0])
64    wiced_version_minor = int(wiced_version.split('.')[1])
65
66wiced_version = "%u.%u" % (wiced_version_major, wiced_version_minor)
67
68# show WICED version
69print("Found WICED SDK version: %s" % wiced_version)
70
71additional_cflags = ""
72if wiced_version < "3.4.0":
73    print("Adding WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ for SDK < 3.4.0")
74    additional_cflags = "-DWICED_UART_READ_DOES_NOT_RETURN_BYTES_READ"
75
76# NOTE: it would be more robust to check for files on disk
77
78# bluetooth firmware image name changed in 5.2
79if wiced_version < "5.2":
80    bluetooth_firmware_file = 'bt_firmware_image.c'
81else:
82    bluetooth_firmware_file = 'bt_firmware_controller.c'
83print("Bluetooth Firmware name: %s" % bluetooth_firmware_file)
84
85# path to examples
86examples_embedded = script_path + "/../../example/"
87
88# path to WICED/apps/btstack
89apps_btstack = wiced_root + "/apps/btstack/"
90
91print("Creating examples in apps/btstack:")
92
93# iterate over btstack examples
94for file in os.listdir(examples_embedded):
95    if not file.endswith(".c"):
96        continue
97    example = file[:-2]
98
99    # create folder
100    apps_folder = apps_btstack + example + "/"
101    if not os.path.exists(apps_folder):
102        os.makedirs(apps_folder)
103
104    # create .mk file
105    with open(apps_folder + example + ".mk", "wt") as fout:
106        fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("ADDITIONAL_CFLAGS", additional_cflags).replace("DATE",time.strftime("%c")).replace('BLUETOOTH_FIRMWARE_FILE', bluetooth_firmware_file))
107
108    # create update_gatt.sh if .gatt file is present
109    gatt_path = examples_embedded + example + ".gatt"
110    if os.path.exists(gatt_path):
111        update_gatt_script = apps_folder + "update_gatt_db.sh"
112        with open(update_gatt_script, "wt") as fout:
113            fout.write(gatt_update_template.replace("EXAMPLE", example))
114        os.chmod(update_gatt_script, 0o755)
115        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
116        print("- %s including compiled GATT DB" % example)
117    else:
118        print("- %s" % example)
119
120