Skip to content

Commit 8222ed3

Browse files
committed
Feat: Added version to SafeBoot image
1 parent 81ec182 commit 8222ed3

File tree

4 files changed

+95
-4
lines changed

4 files changed

+95
-4
lines changed

platformio.ini

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ build_unflags =
5858
-fno-lto
5959
build_type = release
6060
board_build.partitions = partitions.csv
61-
extra_scripts = post:tools/safeboot.py
61+
extra_scripts =
62+
pre:tools/version.py
63+
post:tools/safeboot.py
6264

6365
; --------------------------------------------------------------------
6466
; ENVIRONMENTs

src/HTTPUpdateServer.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
#include <Update.h>
55
#include <WebServer.h>
66

7-
static const char serverIndex[] PROGMEM =
7+
static String serverIndex =
88
R"(<!DOCTYPE html>
99
<html lang='en'>
1010
<head>
1111
<meta charset='utf-8'>
1212
<meta name='viewport' content='width=device-width,initial-scale=1'/>
1313
</head>
1414
<body>
15+
<h1>SafeBoot ${V}</h1>
1516
<form method='POST' action='' enctype='multipart/form-data'>
1617
<label for='firmware'><strong>Firmware:</strong></label>
1718
<br>
@@ -26,11 +27,15 @@ static const char serverIndex[] PROGMEM =
2627
</form>
2728
</body>
2829
</html>)";
29-
static const char successResponse[] PROGMEM = "<META http-equiv=\"refresh\" content=\"10;URL=/\">Update Success! Rebooting...";
30-
static const char cancelResponse[] PROGMEM = "<META http-equiv=\"refresh\" content=\"10;URL=/\">Rebooting...";
30+
static const char* successResponse = "<META http-equiv=\"refresh\" content=\"10;URL=/\">Update Success! Rebooting...";
31+
static const char* cancelResponse = "<META http-equiv=\"refresh\" content=\"10;URL=/\">Rebooting...";
3132

3233
class HTTPUpdateServer {
3334
public:
35+
static void setVersion(const char* version) {
36+
serverIndex.replace("${V}", version);
37+
}
38+
3439
void setup(WebServer* server) {
3540
setup(server, "/update");
3641
}

src/main.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Mycila::Logger logger;
3131

3232
#define TAG "SafeBoot"
3333

34+
extern const char* __COMPILED_APP_VERSION__;
35+
3436
static WebServer webServer(80);
3537
static HTTPUpdateServer httpUpdater;
3638
static Mycila::ESPConnect espConnect;
@@ -64,6 +66,8 @@ void setup() {
6466

6567
// Init hostname
6668
hostname = "SafeBoot-" + getChipIDStr();
69+
LOGI(TAG, "SafeBoot Version: %s", __COMPILED_APP_VERSION__);
70+
LOGI(TAG, "Chip ID: %s", getChipIDStr().c_str());
6771
LOGI(TAG, "Hostname: %s", hostname.c_str());
6872

6973
// Set next boot partition
@@ -72,6 +76,7 @@ void setup() {
7276
esp_ota_set_boot_partition(partition);
7377
}
7478
// setup routes
79+
HTTPUpdateServer::setVersion(__COMPILED_APP_VERSION__);
7580
httpUpdater.setup(&webServer, "/");
7681
webServer.onNotFound([]() {
7782
webServer.sendHeader("Location", "/");

tools/version.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import subprocess
2+
import os
3+
import re
4+
import sys
5+
from datetime import datetime, timezone
6+
7+
Import("env")
8+
9+
10+
def do_main():
11+
# hash
12+
ret = subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, text=True, check=False) # Uses any tags
13+
full_hash = ret.stdout.strip()
14+
short_hash = full_hash[:7]
15+
16+
# branch
17+
ref_name = os.environ.get("REF_NAME")
18+
if ref_name:
19+
branch = ref_name
20+
else:
21+
ret = subprocess.run(
22+
["git", "symbolic-ref", "--short", "HEAD"],
23+
stdout=subprocess.PIPE,
24+
text=True,
25+
check=False,
26+
) # retrieve branch name
27+
branch = ret.stdout.strip()
28+
branch = branch.replace("/", "")
29+
branch = branch.replace("-", "")
30+
branch = branch.replace("_", "")
31+
32+
if branch == "":
33+
raise Exception("No branch name found")
34+
35+
# is_tag ?
36+
tagPattern = re.compile("^v[0-9]+.[0-9]+.[0-9]+([_-][a-zA-Z0-9]+)?$")
37+
is_tag = branch.startswith("v") and len(branch) >= 6 and tagPattern.match(branch)
38+
39+
version = branch
40+
if not is_tag:
41+
version += "_" + short_hash
42+
43+
# local modifications ?
44+
has_local_modifications = False
45+
if not ref_name:
46+
# Check if the source has been modified since the last commit
47+
ret = subprocess.run(
48+
["git", "diff-index", "--quiet", "HEAD", "--"],
49+
stdout=subprocess.PIPE,
50+
text=True,
51+
check=False,
52+
)
53+
has_local_modifications = ret.returncode != 0
54+
55+
if has_local_modifications:
56+
version += "_modified"
57+
58+
# version = "v2.40.2-rc1"
59+
constantFile = os.path.join(env.subst("$BUILD_DIR"), "__compiled_constants.c")
60+
with open(constantFile, "w") as f:
61+
f.write(
62+
f'const char* __COMPILED_APP_VERSION__ = "{version[1:] if tagPattern.match(version) else version}";\n'
63+
f'const char* __COMPILED_BUILD_BRANCH__ = "{branch}";\n'
64+
f'const char* __COMPILED_BUILD_HASH__ = "{short_hash}";\n'
65+
f'const char* __COMPILED_BUILD_NAME__ = "{env["PIOENV"]}";\n'
66+
f'const char* __COMPILED_BUILD_TIMESTAMP__ = "{datetime.now(timezone.utc).isoformat()}";\n'
67+
f'const char* __COMPILED_BUILD_BOARD__ = "{env.get("BOARD")}";\n'
68+
)
69+
sys.stderr.write(f"version.py: APP_VERSION: {version[1:] if tagPattern.match(version) else version}\n")
70+
sys.stderr.write(f"version.py: BUILD_BRANCH: {branch}\n")
71+
sys.stderr.write(f"version.py: BUILD_HASH: {short_hash}\n")
72+
sys.stderr.write(f"version.py: BUILD_NAME: {env['PIOENV']}\n")
73+
sys.stderr.write(f"version.py: BUILD_TIMESTAMP: {datetime.now(timezone.utc).isoformat()}\n")
74+
sys.stderr.write(f"version.py: BUILD_BOARD: {env.get('BOARD')}\n")
75+
76+
env.AppendUnique(PIOBUILDFILES=[constantFile])
77+
78+
79+
do_main()

0 commit comments

Comments
 (0)