firmware: config-integrity check, fault injection, verified recovery ladder; soak harness

This commit is contained in:
Lars Nolden
2026-09-01 11:20:29 +02:00
parent 26fab7202a
commit e131687264
7 changed files with 191 additions and 5 deletions
+6
View File
@@ -105,6 +105,12 @@ static void console_task(void *arg)
repeater_request_probe();
ack("probe", true, "queued");
}
else if (strncmp(line, "inject ", 7) == 0) {
const char *w = line + 7;
if (strcmp(w, "standby") == 0) { repeater_inject_fault(1); ack("inject standby", true, "queued"); }
else if (strcmp(w, "sleep") == 0) { repeater_inject_fault(2); ack("inject sleep", true, "queued"); }
else ack("inject", false, "use standby|sleep");
}
else if (strcmp(line, "test_burst") == 0) {
repeater_request_test_burst();
ack("test_burst", true, "queued");
+37 -5
View File
@@ -34,6 +34,7 @@ static volatile bool s_want_radio_reset;
static volatile bool s_want_test_tx;
static volatile bool s_want_probe;
static volatile bool s_want_test_burst;
static volatile int s_inject; // 1 = standby, 2 = sleep (config loss)
static uint8_t s_test_access_nr;
static uint8_t s_test_lfield = 19;
static uint32_t s_seq;
@@ -95,20 +96,29 @@ static bool recover(const char *reason, int level)
bool ok = false;
switch (level) {
case 1:
// Cheapest step: clear IRQ state and re-arm RX. It must only report
// success if the radio really is configured and receiving, otherwise
// the ladder would stop at a deaf radio.
sx126x_clear_irq(SX126X_IRQ_ALL);
sx126x_standby();
sx126x_restore_rx_payload_len();
ok = sx126x_set_rx();
ok = sx126x_set_rx() && sx126x_config_intact();
if (ok) {
vTaskDelay(pdMS_TO_TICKS(2));
ok = (((sx126x_get_status() >> 4) & 0x7) == 0x5);
}
break;
case 2:
g_stats.radio_reinit++;
ok = radio_full_init() && sx126x_set_rx();
ok = radio_full_init() && sx126x_set_rx() && sx126x_config_intact();
break;
case 3:
g_stats.fem_reinit++;
fem_power(false);
vTaskDelay(pdMS_TO_TICKS(20));
ok = radio_full_init() && sx126x_set_rx();
fem_power(true);
vTaskDelay(pdMS_TO_TICKS(5));
ok = radio_full_init() && sx126x_set_rx() && sx126x_config_intact();
break;
default:
LOGJ("\"event\":\"radio_recovery\",\"reason\":\"%s\",\"level\":%d,"
@@ -496,6 +506,19 @@ static void repeater_task(void *arg)
s_want_probe = false;
run_probe();
}
if (s_inject) {
int what = s_inject;
s_inject = 0;
// Deliberately break the radio to exercise detection + the
// recovery ladder. The heartbeat probe is expected to notice.
if (what == 1) {
sx126x_standby();
} else {
sx126x_inject_sleep();
}
LOGJ("\"event\":\"fault_injected\",\"fault\":\"%s\"",
what == 1 ? "standby" : "sleep");
}
if (ulTaskNotifyTake(pdTRUE, pdMS_TO_TICKS(1000)) > 0) {
uint16_t irq = sx126x_get_irq();
@@ -511,13 +534,21 @@ static void repeater_task(void *arg)
int64_t now = esp_timer_get_time();
if (now - last_heartbeat > 10000000LL) {
last_heartbeat = now;
// consistency probe: chip should be in RX (0x5)
// Consistency probe: the chip must be in RX (0x5) AND still
// hold its wM-Bus configuration. Checking the mode alone is not
// enough: a radio that lost its configuration (reset, sleep,
// glitch) can sit in RX looking healthy while deaf to C1.
uint8_t st = sx126x_get_status();
uint8_t mode = (st >> 4) & 0x7;
bool cfg_ok = sx126x_config_intact();
LOGJ("\"event\":\"heartbeat\",\"uptime_us\":%lld,\"chip_mode\":%u,"
"\"rssi_dbm\":%d", (long long)now, mode, sx126x_get_rssi_inst());
"\"config_intact\":%s,\"rssi_dbm\":%d",
(long long)now, mode, cfg_ok ? "true" : "false",
sx126x_get_rssi_inst());
if (mode != 0x5) {
recover_ladder("not_in_rx");
} else if (!cfg_ok) {
recover_ladder("config_lost");
}
}
}
@@ -533,6 +564,7 @@ void repeater_request_test_tx(void) { s_want_test_tx = true; }
void repeater_request_probe(void) { s_want_probe = true; }
void repeater_set_test_lfield(uint8_t l) { s_test_lfield = l; }
void repeater_request_test_burst(void) { s_want_test_burst = true; }
void repeater_inject_fault(int what) { s_inject = what; }
// ------------------------------------------------------------------ status
+1
View File
@@ -39,6 +39,7 @@ void repeater_request_test_tx(void);
void repeater_request_probe(void);
void repeater_set_test_lfield(uint8_t l);
void repeater_request_test_burst(void);
void repeater_inject_fault(int what); // 1 = standby, 2 = sleep
void repeater_print_status(void);
void repeater_print_stats(void);
void repeater_print_radio_status(void);
+19
View File
@@ -385,6 +385,25 @@ bool sx126x_set_tx_power(int8_t dbm)
uint8_t tp[2] = {(uint8_t)dbm, 0x02}; // ramp 40 us
return cmd(OP_SET_TX_PARAMS, tp, 2);
}
// Integrity check: the sync word registers must still hold the wM-Bus
// access code. A radio that was reset, slept, or glitched loses its
// configuration while still reporting a plausible chip mode, so checking
// the mode alone cannot detect that failure.
bool sx126x_config_intact(void)
{
uint8_t v[2] = {0, 0};
if (!read_regs(REG_SYNCWORD_0, v, 2)) return false;
return v[0] == 0x54 && v[1] == 0x3D;
}
// Fault injection for exercising the recovery ladder (see console `inject`).
bool sx126x_inject_sleep(void)
{
// cold start: configuration is lost on wake
uint8_t cfg = 0x00;
return cmd(OP_SET_SLEEP, &cfg, 1);
}
bool sx126x_standby(void)
{
+2
View File
@@ -34,6 +34,8 @@ bool sx126x_config_wmbus_c1(uint32_t freq_hz, uint8_t rx_payload_len);
bool sx126x_set_tx_power(int8_t dbm); // -9 .. +14 (PA config capped)
bool sx126x_set_rx(void); // continuous RX, FEM to RX path
bool sx126x_standby(void);
bool sx126x_config_intact(void); // sync-word readback still correct
bool sx126x_inject_sleep(void); // fault injection: SLEEP, config lost
// Transmit buf as GFSK payload (sync word prepended by radio).
// Handles FEM CTX switching. Blocking until commands issued; TX-done is