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