NodeMaintenance, build_version.py: back to the original formatting

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-13 14:30:19 +01:00
1 parent 46d33bd2b0
commit 73c1a7beb2
3 files changed
+86 -107

No files matched your search

+55 -59
View File
@@ -7,10 +7,10 @@
#if defined(NRF52_PLATFORM)
extern "C" {
void enterUf2Dfu(void);
void enterSerialDfu(void);
void enterOTADfu(void);
extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr
void enterUf2Dfu(void);
void enterSerialDfu(void);
void enterOTADfu(void);
extern uint32_t g_hardfault_record[4]; // NRF52Board.cpp, .noinit: magic, pc, lr, psr
}
#endif
@@ -30,21 +30,21 @@ void NodeMaintenance::printBootDiag() {
// the build epoch" from "restored something" -- and comparing against the
// epoch of a DIFFERENT build of the same tree invents discrepancies that are
// not there. Cost one wrong conclusion here.
Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long)FIRMWARE_BUILD_EPOCH);
Serial.printf("boot: FIRMWARE_BUILD_EPOCH=%lu\n", (unsigned long) FIRMWARE_BUILD_EPOCH);
Serial.printf("boot: RESETREAS=0x%08lX%s%s%s%s%s\n", (unsigned long)rr,
rr == 0 ? " (none set = power-on/brownout: power was removed)" : "",
(rr & 0x01) ? " RESETPIN" : "", (rr & 0x02) ? " WATCHDOG" : "",
(rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "", (rr & 0x08) ? " LOCKUP(!)" : "");
rr == 0 ? " (none set = power-on/brownout: power was removed)" : "",
(rr & 0x01) ? " RESETPIN" : "",
(rr & 0x02) ? " WATCHDOG" : "",
(rr & 0x04) ? " SREQ(sw reset, e.g. `dfu`/`reboot`)" : "",
(rr & 0x08) ? " LOCKUP(!)" : "");
volatile uint32_t *hf = g_hardfault_record;
volatile uint32_t* hf = g_hardfault_record;
if (hf[0] == 0xFA010DEB) {
_hardfault[0] = hf[1];
_hardfault[1] = hf[2];
_hardfault[2] = hf[3];
_hardfault[0] = hf[1]; _hardfault[1] = hf[2]; _hardfault[2] = hf[3];
_hardfault_seen = true;
Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n", (unsigned long)hf[1],
(unsigned long)hf[2], (unsigned long)hf[3]);
hf[0] = 0; // printed once; `get boot` keeps it until the next reset
Serial.printf("!!! HARDFAULT before last reboot: PC=%08lX LR=%08lX PSR=%08lX\n",
(unsigned long)hf[1], (unsigned long)hf[2], (unsigned long)hf[3]);
hf[0] = 0; // printed once; `get boot` keeps it until the next reset
}
#endif
}
@@ -53,7 +53,7 @@ void NodeMaintenance::printBootDiag() {
// Reboots it caused and the threshold backoff survive power loss in a small file,
// written only right before such a reboot and once when the radio proves itself.
void NodeMaintenance::silenceLoad(uint8_t *backoff) {
void NodeMaintenance::silenceLoad(uint8_t* backoff) {
*backoff = 0;
if (!_fs || !_fs->exists(RADIO_SILENCE_FILE)) return;
#if defined(RP2040_PLATFORM)
@@ -62,7 +62,7 @@ void NodeMaintenance::silenceLoad(uint8_t *backoff) {
File f = _fs->open(RADIO_SILENCE_FILE);
#endif
if (!f) return;
if (f.read((uint8_t *)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) {
if (f.read((uint8_t*)&_silence_reboots, sizeof(_silence_reboots)) != sizeof(_silence_reboots)) {
_silence_reboots = 0;
}
if (f.read(backoff, 1) != 1) *backoff = 0;
@@ -80,12 +80,12 @@ void NodeMaintenance::silenceSave(uint8_t backoff) {
File f = _fs->open(RADIO_SILENCE_FILE, "w", true);
#endif
if (!f) return;
f.write((const uint8_t *)&_silence_reboots, sizeof(_silence_reboots));
f.write((const uint8_t*)&_silence_reboots, sizeof(_silence_reboots));
f.write(&backoff, 1);
f.close();
}
void NodeMaintenance::begin(FILESYSTEM *fs, NodePrefs *prefs, CommonCLICallbacks *callbacks) {
void NodeMaintenance::begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks) {
_fs = fs;
_prefs = prefs;
_callbacks = callbacks;
@@ -94,59 +94,59 @@ void NodeMaintenance::begin(FILESYSTEM *fs, NodePrefs *prefs, CommonCLICallbacks
_silence.begin(millis(), radioActivity(), backoff);
_silence.setMinutes(_prefs->radio_silence_min);
Serial.printf("boot: radio silence watchdog %u min, reboots %lu, backoff x%u\n",
(unsigned)_silence.minutes(), (unsigned long)_silence_reboots,
(unsigned)(1u << _silence.backoff()));
(unsigned) _silence.minutes(), (unsigned long) _silence_reboots,
(unsigned) (1u << _silence.backoff()));
}
void NodeMaintenance::loop() {
dfuTick();
_fw.loop();
switch (_silence.poll(millis(), radioActivity())) {
case RadioSilenceWatchdog::KICK_RX:
Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n",
(unsigned long)(_silence.quietMs(millis()) / 60000));
_radio.kickRx();
break;
case RadioSilenceWatchdog::REBOOT:
Serial.printf("radio silence: still nothing after %lu min, rebooting\n",
(unsigned long)(_silence.quietMs(millis()) / 60000));
_silence_reboots++;
silenceSave(_silence.nextBackoff());
delay(100); // let the line out
_board.reboot();
break;
default:
break;
case RadioSilenceWatchdog::KICK_RX:
Serial.printf("radio silence: nothing received for %lu min, re-arming RX\n",
(unsigned long) (_silence.quietMs(millis()) / 60000));
_radio.kickRx();
break;
case RadioSilenceWatchdog::REBOOT:
Serial.printf("radio silence: still nothing after %lu min, rebooting\n",
(unsigned long) (_silence.quietMs(millis()) / 60000));
_silence_reboots++;
silenceSave(_silence.nextBackoff());
delay(100); // let the line out
_board.reboot();
break;
default:
break;
}
if (_silence.takeBackoffCleared()) silenceSave(0);
}
bool NodeMaintenance::silenceCommand(const char *command, char *reply) {
bool NodeMaintenance::silenceCommand(const char* command, char* reply) {
if (strcmp(command, "get radio.reboots") == 0) {
sprintf(reply, "> %lu", (unsigned long)_silence_reboots);
sprintf(reply, "> %lu", (unsigned long) _silence_reboots);
return true;
}
if (strcmp(command, "get radio.silence") == 0) {
if (_silence.minutes() == 0) {
strcpy(reply, "> off");
} else {
sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u", (unsigned)_silence.minutes(),
(unsigned)(1u << _silence.backoff()), (unsigned long)(_silence.quietMs(millis()) / 60000),
(unsigned)_silence.stage());
sprintf(reply, "> %u min (now x%u), quiet %lu min, stage %u",
(unsigned) _silence.minutes(), (unsigned) (1u << _silence.backoff()),
(unsigned long) (_silence.quietMs(millis()) / 60000), (unsigned) _silence.stage());
}
return true;
}
if (memcmp(command, "set radio.silence ", 18) == 0) {
const char *arg = &command[18];
const char* arg = &command[18];
bool numeric = *arg != 0 && strspn(arg, "0123456789") == strlen(arg);
int m = strcmp(arg, "off") == 0 ? 0 : (numeric ? atoi(arg) : -1);
if (m < 0 || m > RadioSilenceWatchdog::MAX_MINUTES) {
sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned)RadioSilenceWatchdog::MAX_MINUTES);
sprintf(reply, "ERR - minutes 0..%u (0/off = disabled)", (unsigned) RadioSilenceWatchdog::MAX_MINUTES);
return true;
}
_prefs->radio_silence_min = (uint16_t)m;
_prefs->radio_silence_min = (uint16_t) m;
_callbacks->savePrefs();
_silence.setMinutes((uint16_t)m);
_silence.setMinutes((uint16_t) m);
strcpy(reply, "OK");
return true;
}
@@ -214,25 +214,21 @@ void NodeMaintenance::dfuTick() {
// from here, and it is exactly what detach() does on this chip.
NRF_USBD->USBPULLUP = 0;
delay(150);
if (_dfu_mode == 1)
enterSerialDfu();
else if (_dfu_mode == 2)
enterOTADfu();
else
enterUf2Dfu();
if (_dfu_mode == 1) enterSerialDfu();
else if (_dfu_mode == 2) enterOTADfu();
else enterUf2Dfu();
#endif
}
bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char *command, char *reply) {
bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char* command, char* reply) {
if (memcmp(command, "dfu", 3) != 0 || (command[3] != 0 && command[3] != ' ')) return false;
#if defined(NRF52_PLATFORM)
if (sender_timestamp != 0) {
strcpy(reply, "ERR: dfu is local-only (would strand the node in the bootloader)");
return true;
}
const char *arg = (command[3] == ' ') ? &command[4] : "";
while (*arg == ' ')
arg++;
const char* arg = (command[3] == ' ') ? &command[4] : "";
while (*arg == ' ') arg++;
if (strcmp(arg, "serial") == 0) {
strcpy(reply, "OK - rebooting into serial DFU (CDC only, no drive)");
} else if (strcmp(arg, "ota") == 0) {
@@ -251,7 +247,7 @@ bool NodeMaintenance::dfuCommand(uint32_t sender_timestamp, const char *command,
return true;
}
bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char *command, char *reply) {
bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char* command, char* reply) {
if (dfuCommand(sender_timestamp, command, reply)) return true;
if (_fw.handleCommand(sender_timestamp, command, reply)) return true;
if (silenceCommand(command, reply)) return true;
@@ -259,10 +255,10 @@ bool NodeMaintenance::handleCommand(uint32_t sender_timestamp, const char *comma
#if defined(NRF52_PLATFORM)
if (_hardfault_seen) {
sprintf(reply, "> RESETREAS=0x%08lX, HARDFAULT before it: PC=%08lX LR=%08lX PSR=%08lX",
(unsigned long)_reset_reason, (unsigned long)_hardfault[0], (unsigned long)_hardfault[1],
(unsigned long)_hardfault[2]);
(unsigned long) _reset_reason, (unsigned long) _hardfault[0],
(unsigned long) _hardfault[1], (unsigned long) _hardfault[2]);
} else {
sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long)_reset_reason);
sprintf(reply, "> RESETREAS=0x%08lX, no hardfault", (unsigned long) _reset_reason);
}
#else
strcpy(reply, "> not recorded on this platform");
+15 -16
View File
@@ -32,49 +32,48 @@
class NodeMaintenance {
public:
NodeMaintenance(mesh::MainBoard &board, RadioLibWrapper &radio)
: _board(board), _radio(radio), _fw(board) {}
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio), _fw(board) { }
// Prints (and latches for `get boot`) the reset reason and the hardfault record.
// The record is cleared once read, so call it once, as early as Serial is up.
void printBootDiag();
void begin(FILESYSTEM *fs, NodePrefs *prefs, CommonCLICallbacks *callbacks);
void begin(FILESYSTEM* fs, NodePrefs* prefs, CommonCLICallbacks* callbacks);
void loop();
// sender_timestamp == 0 means the local console (the same test CommonCLI uses
// for `erase`); `dfu` refuses anything else.
bool handleCommand(uint32_t sender_timestamp, const char *command, char *reply);
bool handleCommand(uint32_t sender_timestamp, const char* command, char* reply);
// Binary request from a logged-in client (REQ_TYPE_FW_OTA, firmware chunks).
// Returns the reply length, 0 = no reply. Admins only.
int handleRequest(bool admin, const uint8_t *req, int len, uint8_t *reply, int reply_max) {
int handleRequest(bool admin, const uint8_t* req, int len, uint8_t* reply, int reply_max) {
if (!admin || len < 1 || req[0] != REQ_TYPE_FW_OTA) return 0;
return _fw.handleBinary(req, len, reply, reply_max);
}
private:
uint32_t radioActivity() const { return _radio.getPacketsRecv() + _radio.getPacketsRecvErrors(); }
void silenceLoad(uint8_t *backoff);
void silenceLoad(uint8_t* backoff);
void silenceSave(uint8_t backoff);
bool dfuCommand(uint32_t sender_timestamp, const char *command, char *reply);
bool silenceCommand(const char *command, char *reply);
bool dfuCommand(uint32_t sender_timestamp, const char* command, char* reply);
bool silenceCommand(const char* command, char* reply);
void dfuTick();
mesh::MainBoard &_board;
RadioLibWrapper &_radio;
FILESYSTEM *_fs = NULL;
NodePrefs *_prefs = NULL;
CommonCLICallbacks *_callbacks = NULL;
mesh::MainBoard& _board;
RadioLibWrapper& _radio;
FILESYSTEM* _fs = NULL;
NodePrefs* _prefs = NULL;
CommonCLICallbacks* _callbacks = NULL;
FwUpdate _fw;
RadioSilenceWatchdog _silence;
uint32_t _silence_reboots = 0;
unsigned long _dfu_at = 0; // 0 = nothing armed
uint8_t _dfu_mode = 0; // 0=uf2 1=serial 2=ota
unsigned long _dfu_at = 0; // 0 = nothing armed
uint8_t _dfu_mode = 0; // 0=uf2 1=serial 2=ota
uint32_t _reset_reason = 0;
uint32_t _hardfault[3] = { 0, 0, 0 }; // pc, lr, psr; pc 0 = none
uint32_t _hardfault[3] = {0, 0, 0}; // pc, lr, psr; pc 0 = none
bool _hardfault_seen = false;
};
+16 -32
View File
@@ -25,11 +25,8 @@ from datetime import datetime
def git(cwd, *args):
return (
subprocess.check_output(("git",) + args, cwd=cwd, stderr=subprocess.DEVNULL)
.decode()
.strip()
)
return subprocess.check_output(("git",) + args, cwd=cwd,
stderr=subprocess.DEVNULL).decode().strip()
cwd = env.subst("$PROJECT_DIR")
@@ -47,24 +44,15 @@ cwd = env.subst("$PROJECT_DIR")
# version string of the thing it was protecting.
base = "nover"
try:
tag = git(
cwd,
"describe",
"--tags",
"--abbrev=0",
"--match",
"*v[0-9]*",
"--exclude",
"*+tth*",
"--exclude",
"backup/*",
)
tag = git(cwd, "describe", "--tags", "--abbrev=0",
"--match", "*v[0-9]*", "--exclude", "*+tth*",
"--exclude", "backup/*")
# Cut at the 'v' that STARTS THE VERSION, not at the first 'v' in the tag:
# "room-server-v1.17.0" has one in "server", and find("v") turned the base
# into "ver-v1.17.0". Every earlier upstream tag we saw (companion-, repeater-)
# happened to have no 'v' in its prefix, so this hid until v1.17.0.
m = re.search(r"v[0-9]", tag)
base = tag[m.start() :] if m else tag
base = tag[m.start():] if m else tag
except Exception:
pass
@@ -91,12 +79,10 @@ VER_LIMIT, DATE_LIMIT = 19, 11
# so this only bites if upstream's version string grows unusually long.
marker = "-tth"
room = VER_LIMIT - len(base) - len(marker) - len(dirty)
version = "%s%s%s%s" % (base, marker, sha[: max(4, room)], dirty)
version = "%s%s%s%s" % (base, marker, sha[:max(4, room)], dirty)
if len(version) > VER_LIMIT:
print(
"build_version.py: WARNING: %r is %d chars, will truncate to %d on "
"the companion protocol" % (version, len(version), VER_LIMIT)
)
print("build_version.py: WARNING: %r is %d chars, will truncate to %d on "
"the companion protocol" % (version, len(version), VER_LIMIT))
# yymmdd hhmm = exactly 11. Not pretty, but complete and sortable; the readable
# "%d %b %Y %H:%M" is 17 and lost its time to the 12-byte field.
@@ -121,13 +107,11 @@ build_date = now.strftime("%y%m%d %H%M")[:DATE_LIMIT]
# the tree is already fully recompiled each time.
build_epoch = int(now.timestamp())
env.Append(
CPPDEFINES=[
("FIRMWARE_VERSION", env.StringifyMacro(version)),
("FIRMWARE_BUILD_DATE", env.StringifyMacro(build_date)),
("FIRMWARE_BUILD_EPOCH", str(build_epoch)),
# src/helpers/FwUpdate refuses a package built for another env
("FW_OTA_TARGET", env.StringifyMacro(env["PIOENV"])),
]
)
env.Append(CPPDEFINES=[
("FIRMWARE_VERSION", env.StringifyMacro(version)),
("FIRMWARE_BUILD_DATE", env.StringifyMacro(build_date)),
("FIRMWARE_BUILD_EPOCH", str(build_epoch)),
# src/helpers/FwUpdate refuses a package built for another env
("FW_OTA_TARGET", env.StringifyMacro(env["PIOENV"])),
])
print("build_version.py: %s (Build: %s, epoch %d)" % (version, build_date, build_epoch))