xref: /btstack/port/archive/pic32-harmony/create_examples.py (revision 50b47281dcd786f25b8e20527b26e5aaa0460809)
1#!/usr/bin/env python3
2#
3# Create project files for all BTstack embedded examples in harmony/apps/btstack
4
5import os
6import shutil
7import sys
8import time
9import subprocess
10import re
11
12gatt_update_template = '''#!/bin/sh
13DIR=`dirname $0`
14BTSTACK_ROOT=$DIR/../../../framework/btstack
15echo "Creating EXAMPLE.h from EXAMPLE.gatt"
16$BTSTACK_ROOT/tool/compile_gatt.py $BTSTACK_ROOT/example/EXAMPLE.gatt $DIR/EXAMPLE.h
17'''
18
19# get script path
20script_path = os.path.abspath(os.path.dirname(sys.argv[0]))
21
22# validate Harmony root by reading version.txt
23harmony_root = script_path + "/../../../../"
24print(harmony_root)
25harmony_version = ""
26try:
27    with open(harmony_root + 'config/harmony.hconfig', 'r') as fin:
28        for line in fin:
29            m = re.search('default \"(.*)\"', line)
30            if m and len(m.groups()) == 1:
31                harmony_version = m.groups(1)
32                break
33except:
34    pass
35
36if len(harmony_version) == 0:
37    print("Cannot find Harmony root. Make sure BTstack is checked out as harmony/vx_xx/frameworks/btstack")
38    sys.exit(1)
39
40# show Harmony version
41print("Found Harmony version %s" % harmony_version)
42
43# path to examples
44examples_embedded = script_path + "/../../example/"
45
46# path to WICED/apps/btstack
47apps_btstack = harmony_root + "/apps/btstack/"
48
49print("Creating examples in apps/btstack")
50
51# iterate over btstack examples
52for file in os.listdir(examples_embedded):
53    if not file.endswith(".c"):
54        continue
55    if file in ['panu_demo.c', 'sco_demo_util.c', 'ant_test.c', 'pan_lwip_http_server.c']:
56        continue
57    example = file[:-2]
58
59    # recreate folder
60    apps_folder = apps_btstack + example + "/"
61    shutil.rmtree(apps_folder, ignore_errors=True)
62    os.makedirs(apps_folder)
63
64    # create update_gatt.sh if .gatt file is present
65    gatt_path = examples_embedded + example + ".gatt"
66    if os.path.exists(gatt_path):
67        update_gatt_script = apps_folder + "update_gatt_db.sh"
68        with open(update_gatt_script, "wt") as fout:
69            fout.write(gatt_update_template.replace("EXAMPLE", example))
70        os.chmod(update_gatt_script, 0o755)
71        subprocess.call(update_gatt_script + "> /dev/null", shell=True)
72        print("- %s including compiled GATT DB" % example)
73    else:
74        print("- %s" % example)
75
76
77    # create $example.X
78    appX_folder = apps_folder + example + ".X/"
79    os.makedirs(appX_folder)
80
81    # create makefife
82    shutil.copyfile(script_path + "/app.X/Makefile", appX_folder + "Makefile")
83
84    nbproject_folder = appX_folder = appX_folder + "nbproject/"
85    os.makedirs(nbproject_folder)
86
87    template_path = script_path + "/app.X/nbproject/"
88    for file in os.listdir(template_path):
89        src = template_path + file
90        dst = nbproject_folder + file
91        # copy private folder
92        if file == "private":
93            shutil.copytree(src, dst)
94            continue
95        # replace app.X and spp_counter.c
96        with open(src, 'r') as fin:
97            template = fin.read()
98        with open(dst, 'wt') as fout:
99            # template = template.replace('app', example)
100            template = template.replace("<itemPath>../../../example/spp_counter.c", "<itemPath>../../../../framework/btstack/example/" + example + ".c")
101            template = template.replace(">../../../../driver", ">../../../../framework/driver")
102            template = template.replace(">../../../../system", ">../../../../framework/system")
103            template = template.replace(">../../../../../bin/framework/peripheral", ">../../../../bin/framework/peripheral")
104            template = template.replace(">../../../chipset",   ">../../../../framework/btstack/chipset")
105            template = template.replace(">../../../platform",  ">../../../../framework/btstack/platform")
106            template = template.replace(">../../../3rd-party", ">../../../../framework/btstack/3rd-party")
107            template = template.replace(">../../../src",       ">../../../../framework/btstack/src")
108            template = template.replace(">../src",             ">../../../../framework/btstack/port/pic32-harmony/src")
109            template = template.replace("app.X", example+".X")
110            template = template.replace(";../../../..", ";../../../../framework")
111            template = template.replace(";../../../chipset",   ";../../../../framework/btstack/chipset")
112            template = template.replace(";../../../platform",  ";../../../../framework/btstack/platform")
113            template = template.replace(";../../../src",       ";../../../../framework/btstack/src")
114            template = template.replace(";../../../3rd-party", ";../../../../framework/btstack/3rd-party")
115            template = template.replace(";../src",             ";../../../../framework/btstack/port/pic32-harmony/src")
116            template = template.replace('value=".;', 'value="..;')
117            # more or less the same for the command line build
118            template = template.replace('-I"../../../chipset/csr"', '-I"../../../../framework/btstack/chipset/csr"')
119            template = template.replace('-I"../../../src"',         '-I"../../../../framework/btstack/src"')
120            template = template.replace('-I"../../../platform',     '-I"../../../../framework/btstack/platform')
121            template = template.replace('-I"../src"',               '-I"../../../../framework/btstack/port/pic32-harmony/src"')
122            template = template.replace('-I"../../../.."',          '-I"../../../../framework"')
123            template = template.replace('-I"."',                    '-I".."')
124            template = template.replace("../../../../driver",       "../../../../framework/driver")
125            template = template.replace("../../../../system",       "../../../../framework/system")
126            template = template.replace("../../../../../bin/framework/peripheral", "../../../../bin/framework/peripheral")
127            template = template.replace("../../../3rd-party",       "../../../../framework/btstack/3rd-party")
128            template = template.replace("../../../chipset",         "../../../../framework/btstack/chipset")
129            template = template.replace("../../../platform",        "../../../../framework/btstack/platform")
130            template = template.replace("../../../src",             "../../../../framework/btstack/src")
131            template = template.replace("../src/app_debug.c",       "../../../../framework/btstack/port/pic32-harmony/src/app_debug.c")
132            template = template.replace("../src/app.c",             "../../../../framework/btstack/port/pic32-harmony/src/app.c")
133            template = template.replace("../src/btstack_port.c",    "../../../../framework/btstack/port/pic32-harmony/src/btstack_port.c")
134            template = template.replace("../src/main.c",            "../../../../framework/btstack/port/pic32-harmony/src/main.c")
135            template = template.replace("../src/system_config",     "../../../../framework/btstack/port/pic32-harmony/src/system_config")
136            template = template.replace("../../../example/spp_counter.c", "../../../../framework/btstack/example/" + example + ".c")
137            fout.write(template)
138