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