xref: /btstack/port/wiced-h4/create_examples.py (revision a06bcae0f7f63d9d69b2f846d04300ea7058ea66)
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'''
25
26gatt_update_template = '''#!/bin/sh
27DIR=`dirname $0`
28BTSTACK_ROOT=$DIR/../../../libraries/btstack
29echo "Creating EXAMPLE.h from EXAMPLE.gatt"
30$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/EXAMPLE.h
31'''
32
33# get script path
34script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
35
36# validate WICED root by reading version.txt
37wiced_root = script_path + "/../../../../"
38wiced_version = ""
39try:
40    with open(wiced_root + 'version.txt', 'r') as fin:
41        wiced_version = fin.read()  # Read the contents of the file into memory.
42except:
43    pass
44if not "WICED Version" in wiced_version:
45    print("Cannot find WICED root. Make sure BTstack is checked out in WICED-SDK-X/libraries")
46    sys.exit(1)
47
48# show WICED version
49wiced_version = wiced_version.split()[2]
50print("Found WICED SDK version: %s" % wiced_version)
51
52additional_cflags = ""
53if wiced_version < "3.4.0":
54    print("Adding WICED_UART_READ_DOES_NOT_RETURN_BYTES_READ for SDK < 3.4.0")
55    additional_cflags = "-DWICED_UART_READ_DOES_NOT_RETURN_BYTES_READ"
56
57
58# path to examples
59examples_embedded = script_path + "/../../example/"
60
61# path to WICED/apps/btstack
62apps_btstack = wiced_root + "/apps/btstack/"
63
64print("Creating examples in apps/btstack:")
65
66# iterate over btstack examples
67for file in os.listdir(examples_embedded):
68    if not file.endswith(".c"):
69        continue
70    example = file[:-2]
71
72    # create folder
73    apps_folder = apps_btstack + example + "/"
74    if not os.path.exists(apps_folder):
75        os.makedirs(apps_folder)
76
77    # create .mk file
78    with open(apps_folder + example + ".mk", "wt") as fout:
79        fout.write(mk_template.replace("EXAMPLE", example).replace("TOOL", script_path).replace("ADDITIONAL_CFLAGS", additional_cflags).replace("DATE",time.strftime("%c")))
80
81    # create update_gatt.sh if .gatt file is present
82    gatt_path = examples_embedded + example + ".gatt"
83    if os.path.exists(gatt_path):
84        update_gatt_script = apps_folder + "update_gatt_db.sh"
85        with open(update_gatt_script, "wt") as fout:
86            fout.write(gatt_update_template.replace("EXAMPLE", example))
87        os.chmod(update_gatt_script, 0o755)
88        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
89        print("- %s including compiled GATT DB" % example)
90    else:
91        print("- %s" % example)
92
93