Files
meshcore-tth/src/helpers/NodeMaintenance.h
T

122 lines
5.2 KiB
C++

#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)
// fw ... admin-pushed firmware update, see helpers/FwUpdate.h
// (only with -D FW_OTA=1: the experimental -ota images)
// 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 radio.txstall TX stall watchdog: threshold, queue, time without TX
// set radio.txstall <min> 0/off = disabled; RAM only, RADIO_TXSTALL_MIN at boot
// get boot why the board last reset, and the hardfault record
// get pwr battery thresholds, battery now and at boot, last shutdown
// set pwr.bootlock <mV> don't start below it, sleep until the battery recovers
// set pwr.cutoff <mV> switch a running node off below it; 0/off = disabled
// (both only with -D PWRMGT_RUNTIME, nRF52 power management,
// see helpers/LowVoltageGuard.h)
//
// 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(), or maint.loop(queued, due) to arm the TX stall
// watchdog (outbound packets in total / due now)
// commands: if (maint.handleCommand(sender_timestamp, command, reply)) return;
#include <Arduino.h>
#include <Mesh.h>
#include <helpers/CommonCLI.h>
#ifdef FW_OTA
#include <helpers/FwUpdate.h>
#endif
#include <helpers/RadioSilenceWatchdog.h>
#include <helpers/LowVoltageGuard.h>
#if defined(NRF52_PLATFORM) && defined(NRF52_POWER_MANAGEMENT) && defined(PWRMGT_RUNTIME)
#include <helpers/NRF52Board.h>
#define NODE_PWR_GUARD 1
#endif
#include <helpers/radiolib/RadioLibWrappers.h>
class NodeMaintenance {
public:
#ifdef FW_OTA
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio), _fw(board) { }
#else
NodeMaintenance(mesh::MainBoard& board, RadioLibWrapper& radio) : _board(board), _radio(radio) { }
#endif
// 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);
// tx_queued < 0: the role does not report its queue, no TX stall watchdog.
void loop(int tx_queued = -1, int tx_due = -1);
// 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);
// 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) {
#ifdef FW_OTA
if (!admin || len < 1 || req[0] != REQ_TYPE_FW_OTA) return 0;
return _fw.handleBinary(req, len, reply, reply_max);
#else
(void)admin; (void)req; (void)len; (void)reply; (void)reply_max;
return 0; // a classic image: no firmware update, the request is dropped
#endif
}
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 txStallTick(int tx_queued, int tx_due);
bool txStallCommand(const char* command, char* reply);
void dfuTick();
#ifdef NODE_PWR_GUARD
void pwrBegin();
void pwrTick();
void pwrSave();
bool pwrCommand(const char* command, char* reply);
NRF52Board& nrf() { return static_cast<NRF52Board&>(_board); }
#endif
mesh::MainBoard& _board;
RadioLibWrapper& _radio;
FILESYSTEM* _fs = NULL;
NodePrefs* _prefs = NULL;
CommonCLICallbacks* _callbacks = NULL;
#ifdef FW_OTA
FwUpdate _fw;
#endif
RadioSilenceWatchdog _silence;
uint32_t _silence_reboots = 0;
RadioSilenceWatchdog _txstall; // same staging, `activity` = packets sent
#ifdef NODE_PWR_GUARD
LowVoltageGuard _pwr;
#endif
int _tx_queued = -1, _tx_due = -1;
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;
};