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