Skip to content

Commit 5ca10f3

Browse files
committed
Process metadata only in metadata.cpp
Improves cache utilization as fewer things are passed via CFLAGS to all files. In the event that no metadata is available, let the cpp file handle warning about default usage.
1 parent a073bf3 commit 5ca10f3

File tree

6 files changed

+74
-40
lines changed

6 files changed

+74
-40
lines changed
Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Import('env')
22
import subprocess
3+
import json
34
import re
45

56
def get_github_repo():
@@ -42,7 +43,7 @@ def get_github_repo():
4243

4344
# Check if it's a GitHub URL
4445
if 'github.com' not in remote_url.lower():
45-
return 'unknown'
46+
return None
4647

4748
# Parse GitHub URL patterns:
4849
# https://github.com/owner/repo.git
@@ -63,17 +64,56 @@ def get_github_repo():
6364
if ssh_match:
6465
return ssh_match.group(1)
6566

66-
return 'unknown'
67+
return None
6768

6869
except FileNotFoundError:
6970
# Git CLI is not installed or not in PATH
70-
return 'unknown'
71+
return None
7172
except subprocess.CalledProcessError:
7273
# Git command failed (e.g., not a git repo, no remote, etc.)
73-
return 'unknown'
74+
return None
7475
except Exception:
7576
# Any other unexpected error
76-
return 'unknown'
77+
return None
7778

78-
repo = get_github_repo()
79-
env.Append(BUILD_FLAGS=[f'-DWLED_REPO=\\"{repo}\\"'])
79+
PACKAGE_FILE = "package.json"
80+
81+
def get_version():
82+
with open(PACKAGE_FILE, "r") as package:
83+
return json.load(package)["version"]
84+
return None
85+
86+
87+
def has_def(cppdefs, name):
88+
""" Returns true if a given name is set in a CPPDEFINES collection """
89+
for f in cppdefs:
90+
if isinstance(f, tuple):
91+
f = f[0]
92+
if f == name:
93+
return True
94+
return False
95+
96+
97+
def add_wled_metadata_flags(env, node):
98+
cdefs = env["CPPDEFINES"].copy()
99+
100+
if not has_def(cdefs, "WLED_REPO"):
101+
repo = get_github_repo()
102+
if repo:
103+
cdefs.append(("WLED_REPO", f"\\\"{repo}\\\""))
104+
105+
if not has_def(cdefs, "WLED_VERSION"):
106+
version = get_version()
107+
if version:
108+
cdefs.append(("WLED_VERSION", get_version()))
109+
110+
# This transforms the node in to a Builder; it cannot be modified again
111+
return env.Object(
112+
node,
113+
CPPDEFINES=cdefs
114+
)
115+
116+
env.AddBuildMiddleware(
117+
add_wled_metadata_flags,
118+
"*/wled_metadata.cpp"
119+
)

pio-scripts/set_version.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

platformio.ini

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ ldscript_4m1m = eagle.flash.4m1m.ld
110110

111111
[scripts_defaults]
112112
extra_scripts =
113-
pre:pio-scripts/set_version.py
114-
pre:pio-scripts/set_repo.py
113+
pre:pio-scripts/set_metadata.py
115114
post:pio-scripts/output_bins.py
116115
post:pio-scripts/strip-floats.py
117116
pre:pio-scripts/user_config_copy.py

wled00/ota_update.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static bool validateOTA(const uint8_t* binaryData, size_t dataSize, char* errorM
3333
}
3434

3535
// Try to extract WLED structure directly from binary data
36-
wled_custom_desc_t extractedDesc;
36+
wled_metadata_t extractedDesc;
3737
bool hasDesc = findWledMetadata(binaryData, dataSize, &extractedDesc);
3838

3939
if (hasDesc) {

wled00/wled_metadata.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
#include "ota_update.h"
22
#include "wled.h"
33

4+
#ifndef WLED_VERSION
5+
#warning WLED_VERSION was not set - using default value of 'dev'
6+
#define WLED_VERSION dev
7+
#endif
8+
#ifndef WLED_RELEASE_NAME
9+
#warning WLED_RELEASE_NAME was not set - using default value of 'Custom'
10+
#define WLED_RELEASE_NAME "Custom"
11+
#endif
12+
#ifndef WLED_REPO
13+
// No warning for this one: integrators are not always on GitHub
14+
#define WLED_REPO "unknown"
15+
#endif
16+
417
#define WLED_CUSTOM_DESC_MAGIC 0x57535453 // "WSTS" (WLED System Tag Structure)
518
#define WLED_CUSTOM_DESC_VERSION 1
619
#define WLED_RELEASE_NAME_MAX_LEN 48
@@ -40,7 +53,7 @@ inline uint32_t djb2_hash_runtime(const char* str) {
4053
// GLOBAL VARIABLES
4154
// ------------------------------------
4255
// Structure instantiation for this build
43-
const wled_custom_desc_t __attribute__((section(BUILD_METADATA_SECTION))) WLED_BUILD_DESCRIPTION = {
56+
const wled_metadata_t __attribute__((section(BUILD_METADATA_SECTION))) WLED_BUILD_DESCRIPTION = {
4457
WLED_CUSTOM_DESC_MAGIC, // magic
4558
WLED_CUSTOM_DESC_VERSION, // version
4659
TOSTRING(WLED_VERSION),
@@ -66,20 +79,20 @@ const __FlashStringHelper* brandString = FPSTR(brandString_s);
6679
* @param extractedDesc Buffer to store extracted custom description structure
6780
* @return true if structure was found and extracted, false otherwise
6881
*/
69-
bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_custom_desc_t* extractedDesc) {
70-
if (!binaryData || !extractedDesc || dataSize < sizeof(wled_custom_desc_t)) {
82+
bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_metadata_t* extractedDesc) {
83+
if (!binaryData || !extractedDesc || dataSize < sizeof(wled_metadata_t)) {
7184
return false;
7285
}
7386

74-
for (size_t offset = 0; offset <= dataSize - sizeof(wled_custom_desc_t); offset++) {
75-
const wled_custom_desc_t* custom_desc = (const wled_custom_desc_t*)(binaryData + offset);
87+
for (size_t offset = 0; offset <= dataSize - sizeof(wled_metadata_t); offset++) {
88+
const wled_metadata_t* custom_desc = (const wled_metadata_t*)(binaryData + offset);
7689

7790
// Check for magic number
7891
if (custom_desc->magic == WLED_CUSTOM_DESC_MAGIC) {
7992
// Found potential match, validate version
8093
if (custom_desc->desc_version != WLED_CUSTOM_DESC_VERSION) {
8194
DEBUG_PRINTF_P(PSTR("Found WLED structure at offset %u but version mismatch: %u\n"),
82-
offset, custom_desc->version);
95+
offset, custom_desc->desc_version);
8396
continue;
8497
}
8598

@@ -91,7 +104,7 @@ bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_custom_de
91104
}
92105

93106
// Valid structure found - copy entire structure
94-
memcpy(extractedDesc, custom_desc, sizeof(wled_custom_desc_t));
107+
memcpy(extractedDesc, custom_desc, sizeof(wled_metadata_t));
95108

96109
DEBUG_PRINTF_P(PSTR("Extracted WLED structure at offset %u: '%s'\n"),
97110
offset, extractedDesc->release_name);
@@ -113,7 +126,7 @@ bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_custom_de
113126
* @return true if OTA should proceed, false if it should be blocked
114127
*/
115128

116-
bool shouldAllowOTA(const wled_custom_desc_t& firmwareDescription, char* errorMessage, size_t errorMessageLen) {
129+
bool shouldAllowOTA(const wled_metadata_t& firmwareDescription, char* errorMessage, size_t errorMessageLen) {
117130
// Clear error message
118131
if (errorMessage && errorMessageLen > 0) {
119132
errorMessage[0] = '\0';

wled00/wled_metadata.h

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,6 @@
1111
#include <string.h>
1212
#include <WString.h>
1313

14-
#ifndef WLED_VERSION
15-
#define WLED_VERSION dev
16-
#endif
17-
#ifndef WLED_RELEASE_NAME
18-
#define WLED_RELEASE_NAME "Custom"
19-
#endif
20-
#ifndef WLED_REPO
21-
#define WLED_REPO "unknown"
22-
#endif
23-
2414
#define WLED_VERSION_MAX_LEN 48
2515
#define WLED_RELEASE_NAME_MAX_LEN 48
2616

@@ -36,11 +26,11 @@ typedef struct {
3626
char wled_version[WLED_VERSION_MAX_LEN];
3727
char release_name[WLED_RELEASE_NAME_MAX_LEN]; // Release name (null-terminated)
3828
uint32_t hash; // Structure sanity check
39-
} __attribute__((packed)) wled_custom_desc_t;
29+
} __attribute__((packed)) wled_metadata_t;
4030

4131

4232
// Global build description
43-
extern const wled_custom_desc_t WLED_BUILD_DESCRIPTION;
33+
extern const wled_metadata_t WLED_BUILD_DESCRIPTION;
4434

4535
// Convenient metdata pointers
4636
#define versionString (WLED_BUILD_DESCRIPTION.wled_version) // Build version, WLED_VERSION
@@ -59,7 +49,7 @@ extern const __FlashStringHelper* brandString ; // Brand
5949
* @param extractedDesc Buffer to store extracted custom description structure
6050
* @return true if structure was found and extracted, false otherwise
6151
*/
62-
bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_custom_desc_t* extractedDesc);
52+
bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_metadata_t* extractedDesc);
6353

6454
/**
6555
* Check if OTA should be allowed based on release compatibility
@@ -68,4 +58,4 @@ bool findWledMetadata(const uint8_t* binaryData, size_t dataSize, wled_custom_de
6858
* @param errorMessageLen Maximum length of error message buffer
6959
* @return true if OTA should proceed, false if it should be blocked
7060
*/
71-
bool shouldAllowOTA(const wled_custom_desc_t& firmwareDescription, char* errorMessage, size_t errorMessageLen);
61+
bool shouldAllowOTA(const wled_metadata_t& firmwareDescription, char* errorMessage, size_t errorMessageLen);

0 commit comments

Comments
 (0)