simple_sensor: dfu, radio silence watchdog and get boot via shared NodeMaintenance

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-04 00:21:54 +01:00
1 parent 278c1b8c8a
commit 7159a00f05
4 files changed
+305 -1

No files matched your search

+69
View File
@@ -0,0 +1,69 @@
#pragma once
// CUSTOM (TeTeHacko): upkeep that belongs to the BOARD, not to the node's role --
// so every build with a text console gets it, not only the repeater.
//
// It grew in examples/simple_repeater/main.cpp, because every node of ours on the
// mesh is a repeater and each feature was written for the problem at hand on a
// mast. That left the sensor on the birdhouse (x4) without `dfu` (flashable only
// with the 1200-baud touch) and without the radio silence watchdog, although it
// was the first node seen deaf (31. 10. 2026). Living here, it costs a role one
// line per hook, and the upstream files it touches stay one added line each.
//
// dfu [uf2|serial|ota] reboot into the bootloader (local console only)
// set radio.silence <min> radio silence watchdog, 0/off = disabled (#6)
// get radio.silence threshold, backoff, quiet time, stage
// get radio.reboots reboots done by the watchdog, survives power loss
// get boot why the board last reset, and the hardfault record
//
// Wiring (see simple_sensor/main.cpp):
// setup(): maint.printBootDiag() early, maint.begin(fs, prefs, cb) after the
// mesh has loaded its prefs
// loop(): maint.loop()
// commands: if (maint.handleCommand(sender_timestamp, command, reply)) return;
#include <Arduino.h>
#include <Mesh.h>
#include <helpers/CommonCLI.h>
#include <helpers/RadioSilenceWatchdog.h>
#include <helpers/radiolib/RadioLibWrappers.h>
class NodeMaintenance {
public:
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio) { }
// 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 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);
private:
uint32_t radioActivity() const { return _radio.getPacketsRecv() + _radio.getPacketsRecvErrors(); }
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);
void dfuTick();
mesh::MainBoard& _board;
RadioLibWrapper& _radio;
FILESYSTEM* _fs = NULL;
NodePrefs* _prefs = NULL;
CommonCLICallbacks* _callbacks = NULL;
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
uint32_t _reset_reason = 0;
uint32_t _hardfault[3] = {0, 0, 0}; // pc, lr, psr; pc 0 = none
bool _hardfault_seen = false;
};