Fix SENSOR power saving deep sleep truncating TX and skipping sleep on failed reads (#10939)

* Fix SENSOR power saving deep sleep behavior

Deep sleep could be entered while a telemetry packet was still queued
or on air, truncating the transmission. canSleep() gained a deepSleep
parameter and now vetoes in that case; light sleep is unchanged.
Telemetry modules defer a pending deep sleep (bounded to 30s) until the
radio is idle and no longer let the sensor polling interval override
the 5s pre-sleep grace period. A failed sensor read still arms deep
sleep instead of leaving the node awake for a full telemetry interval.

Fixes #10890
Fixes #10932

* Deduplicate telemetry deep sleep deferral logic

Move the radio-busy deferral and its counter into BaseTelemetryModule
and add an isPowerSavingSensor() helper. Removes the telemetry-specific
counter from OSThread. The sleep arming block stays per module because
it needs protected OSThread members not visible to the base class.
This commit is contained in:
Thomas Göttgens
2026-07-08 07:30:46 -05:00
committed by GitHub
co-authored by GitHub
parent ba473bf529
commit 38074f584f
11 changed files with 162 additions and 48 deletions
+23 -9
View File
@@ -39,6 +39,8 @@ static constexpr uint16_t TX_HISTORY_KEY_HEALTH_TELEMETRY = 0x8003;
int32_t HealthTelemetryModule::runOnce()
{
if (sleepOnNextExecution == true) {
if (shouldDeferDeepSleep())
return PREFLIGHT_SLEEP_RETRY_MS;
sleepOnNextExecution = false;
uint32_t nightyNightMs = Default::getConfiguredOrDefaultMs(moduleConfig.telemetry.health_update_interval,
default_telemetry_broadcast_interval_secs);
@@ -91,6 +93,12 @@ int32_t HealthTelemetryModule::runOnce()
lastSentToPhone = millis();
}
}
if (sleepOnNextExecution) {
// Honor the pre-sleep grace period armed in sendTelemetry(): OSThread reschedules with
// this return value, which would otherwise override setIntervalFromNow() with the
// sensor polling interval and trigger deep sleep while the TX is still on air
return FIVE_SECONDS_MS;
}
return min(sendToPhoneIntervalMs, result);
}
@@ -232,7 +240,8 @@ bool HealthTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
meshtastic_Telemetry m = meshtastic_Telemetry_init_zero;
m.which_variant = meshtastic_Telemetry_health_metrics_tag;
m.time = getTime();
if (getHealthTelemetry(&m)) {
bool validTelemetry = getHealthTelemetry(&m);
if (validTelemetry) {
LOG_INFO("Send: temperature=%f, heart_bpm=%d, spO2=%d", m.variant.health_metrics.temperature,
m.variant.health_metrics.heart_bpm, m.variant.health_metrics.spO2);
@@ -256,16 +265,21 @@ bool HealthTelemetryModule::sendTelemetry(NodeNum dest, bool phoneOnly)
} else {
LOG_INFO("Send packet to mesh");
service->sendToMesh(p, RX_SRC_LOCAL, true);
if (config.device.role == meshtastic_Config_DeviceConfig_Role_SENSOR && config.power.is_power_saving) {
LOG_DEBUG("Start next execution in 5s, then sleep");
sleepOnNextExecution = true;
setIntervalFromNow(5000);
}
}
return true;
}
return false;
// Arm the pre-sleep sequence even when no valid reading was available this cycle: a
// power-saving SENSOR node must still return to deep sleep, otherwise it stays awake
// until the next telemetry interval and drains its battery
if (!phoneOnly && isPowerSavingSensor()) {
if (!validTelemetry)
LOG_WARN("Health telemetry unavailable this cycle, sleep without sending");
sleepOnNextExecution = true;
preflightSleepDeferrals = 0;
LOG_DEBUG("Start next execution in 5s, then sleep");
setIntervalFromNow(FIVE_SECONDS_MS);
}
return validTelemetry;
}
#endif