Skip to content

Commit 8cf8c9e

Browse files
committed
Refactor generator
1 parent 408896c commit 8cf8c9e

File tree

5 files changed

+109
-75
lines changed

5 files changed

+109
-75
lines changed

firmware/generate.sh

-75
This file was deleted.

firmware/generator.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#!/usr/bin/env python3
2+
3+
import os
4+
import shutil
5+
import json
6+
import hashlib
7+
import sys
8+
from pathlib import Path
9+
import argparse
10+
import subprocess
11+
12+
DOWNLOAD_URL = "https://cloud-downloads.oniudra.cc"
13+
14+
PROVISION_BINARY_PATHS = {
15+
"lora": "binaries/provision/lora",
16+
"crypto": "binaries/provision/crypto",
17+
}
18+
19+
SKETCH_NAMES = {
20+
"lora": "LoraProvision",
21+
"crypto": "CryptoProvision"
22+
}
23+
24+
INDEX_PATH = "binaries/index.json"
25+
26+
BOARDS = [
27+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:nano_33_iot"},
28+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrwifi1010"},
29+
{"type": "crypto", "ext": ".elf", "fqbn": "arduino:mbed_nano:nanorp2040connect"},
30+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:mbed_portenta:envie_m7"},
31+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkr1000"},
32+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrgsm1400"},
33+
{"type": "crypto", "ext": ".bin", "fqbn": "arduino:samd:mkrnb1500"},
34+
{"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1300"},
35+
{"type": "lora", "ext": ".bin", "fqbn": "arduino:samd:mkrwan1310"},
36+
]
37+
38+
# Generates file SHA256
39+
def sha2(file_path):
40+
with open(file_path, "rb") as f:
41+
return hashlib.sha256(f.read()).hexdigest()
42+
43+
# Runs arduino-cli
44+
def arduino_cli(cli_path, args=[]):
45+
res = subprocess.run([cli_path, *args], capture_output=True, text=True)
46+
return res.stdout, res.stderr
47+
48+
def provision_binary_details(board):
49+
bin_path = PROVISION_BINARY_PATHS[board["type"]]
50+
simple_fqbn = board["fqbn"].replace(":", ".")
51+
sketch_dir = Path(__file__).parent / bin_path / simple_fqbn
52+
sketch_files = list(sketch_dir.iterdir())
53+
# there should be only one binary file
54+
if len(sketch_files) != 1:
55+
print(f"Invalid binaries found in {sketch_dir}")
56+
sys.exit(1)
57+
sketch_file = sketch_files[0]
58+
59+
sketch_dest = f"{bin_path}/{simple_fqbn}/{sketch_file.name}"
60+
file_hash = sha2(sketch_file)
61+
62+
return {
63+
"url": f"{DOWNLOAD_URL}/{sketch_dest}",
64+
"checksum": f"SHA-256:{file_hash}",
65+
"size": f"{sketch_file.stat().st_size}",
66+
}
67+
68+
def generate_index(boards):
69+
index_json = []
70+
for board in boards:
71+
index_board = {"fqbn": board["fqbn"]}
72+
index_board["provision"] = provision_binary_details(board)
73+
index_json.append(index_board)
74+
75+
p = Path(__file__).parent / INDEX_PATH
76+
with open(p, "w") as f:
77+
json.dump(index_json, f, indent=2)
78+
79+
def generate_binaries(arduino_cli_path, boards):
80+
for board in boards:
81+
sketch_path = Path(__file__).parent / "provision" / SKETCH_NAMES[board["type"]]
82+
print(f"Compiling for {board['fqbn']}")
83+
res, err = arduino_cli(arduino_cli_path, args=[
84+
"compile",
85+
"-e",
86+
"-b", board["fqbn"],
87+
sketch_path,
88+
])
89+
print(res, err)
90+
simple_fqbn = board["fqbn"].replace(":", ".")
91+
# Make output directory
92+
out = Path(__file__).parent / PROVISION_BINARY_PATHS[board["type"]] / simple_fqbn
93+
os.makedirs(out, exist_ok=True)
94+
# Copy the new binary file in the correct output directory
95+
compiled_bin = sketch_path / "build" / simple_fqbn / (SKETCH_NAMES[board["type"]] + ".ino" + board["ext"])
96+
shutil.copy2(compiled_bin, out / ("provision" + board["ext"]))
97+
98+
if __name__ == "__main__":
99+
parser = argparse.ArgumentParser(prog="generator.py")
100+
parser.add_argument(
101+
"-a",
102+
"--arduino-cli",
103+
default="arduino-cli",
104+
help="Path to arduino-cli executable",
105+
required=False,
106+
)
107+
args = parser.parse_args(sys.argv[1:])
108+
generate_binaries(args.arduino_cli, BOARDS)
109+
generate_index(BOARDS)

0 commit comments

Comments
 (0)