248 lines
9.0 KiB
C++
248 lines
9.0 KiB
C++
#include "power_mgr.h"
|
|
|
|
#include <WiFi.h>
|
|
#include <esp_sleep.h>
|
|
|
|
#include "app_config.h"
|
|
#include "board_pins.h"
|
|
#include "camera_mgr.h"
|
|
#include "nightlight.h"
|
|
#include "stream_session.h"
|
|
|
|
// RTC slow memory survives deep sleep but not a power cycle — exactly the
|
|
// semantics SPEC C7 assumes for the boot counter.
|
|
RTC_DATA_ATTR static uint32_t s_boot_count = 0;
|
|
RTC_DATA_ATTR static uint32_t s_sleep_epoch = 0; // seconds slept, cumulative
|
|
RTC_DATA_ATTR static uint32_t s_last_sleep_s = 0; // duration of the last sleep
|
|
|
|
static WakeCause s_cause = WAKE_POWER_ON;
|
|
static uint8_t s_inhibit[INHIBIT_COUNT] = {0};
|
|
static uint32_t s_last_activity_ms = 0;
|
|
static uint32_t s_awake_since_ms = 0;
|
|
static uint32_t s_hold_until_ms = 0;
|
|
|
|
static const char* kInhibitNames[INHIBIT_COUNT] = {
|
|
"boot", "upload", "stream", "ota", "hold"
|
|
};
|
|
|
|
void powerBegin() {
|
|
s_boot_count++;
|
|
s_awake_since_ms = millis();
|
|
s_last_activity_ms = millis();
|
|
|
|
switch (esp_sleep_get_wakeup_cause()) {
|
|
case ESP_SLEEP_WAKEUP_EXT1: s_cause = WAKE_RADAR; break;
|
|
case ESP_SLEEP_WAKEUP_TIMER: s_cause = WAKE_TIMER; break;
|
|
case ESP_SLEEP_WAKEUP_UNDEFINED: s_cause = WAKE_POWER_ON; break;
|
|
default: s_cause = WAKE_OTHER; break;
|
|
}
|
|
|
|
// Hold the device awake until the first successful check-in. Without this
|
|
// a device configured with a bad endpoint would sleep before it could ever
|
|
// be told otherwise, and the only fix would be USB.
|
|
memset(s_inhibit, 0, sizeof(s_inhibit));
|
|
powerInhibit(INHIBIT_BOOT);
|
|
|
|
log_i("power: boot #%u, wake cause %s, %us slept so far",
|
|
(unsigned)s_boot_count, powerWakeCauseName(), (unsigned)s_sleep_epoch);
|
|
}
|
|
|
|
WakeCause powerWakeCause() { return s_cause; }
|
|
uint32_t powerBootCount() { return s_boot_count; }
|
|
uint32_t powerLastSleepSeconds() { return s_last_sleep_s; }
|
|
uint32_t powerTotalSleepSeconds() { return s_sleep_epoch; }
|
|
|
|
const char* powerWakeCauseName() {
|
|
switch (s_cause) {
|
|
case WAKE_RADAR: return "radar";
|
|
case WAKE_TIMER: return "timer";
|
|
case WAKE_POWER_ON: return "power-on";
|
|
default: return "other";
|
|
}
|
|
}
|
|
|
|
void powerInhibit(SleepInhibit r) {
|
|
if (r < INHIBIT_COUNT && s_inhibit[r] < 255) s_inhibit[r]++;
|
|
}
|
|
|
|
void powerRelease(SleepInhibit r) {
|
|
if (r < INHIBIT_COUNT && s_inhibit[r] > 0) s_inhibit[r]--;
|
|
}
|
|
|
|
bool powerIsInhibited() {
|
|
for (uint8_t i = 0; i < INHIBIT_COUNT; i++) {
|
|
if (s_inhibit[i]) return true;
|
|
}
|
|
return (int32_t)(s_hold_until_ms - millis()) > 0;
|
|
}
|
|
|
|
const char* powerInhibitSummary() {
|
|
static char buf[64];
|
|
buf[0] = '\0';
|
|
for (uint8_t i = 0; i < INHIBIT_COUNT; i++) {
|
|
if (!s_inhibit[i]) continue;
|
|
if (buf[0]) strncat(buf, ",", sizeof(buf) - strlen(buf) - 1);
|
|
strncat(buf, kInhibitNames[i], sizeof(buf) - strlen(buf) - 1);
|
|
}
|
|
if (!buf[0]) strncpy(buf, "none", sizeof(buf));
|
|
return buf;
|
|
}
|
|
|
|
void powerNoteActivity() { s_last_activity_ms = millis(); }
|
|
|
|
void powerHoldAwake(uint32_t seconds) {
|
|
uint32_t until = millis() + seconds * 1000UL;
|
|
if ((int32_t)(until - s_hold_until_ms) > 0) s_hold_until_ms = until;
|
|
powerNoteActivity();
|
|
log_i("power: holding awake for %us", (unsigned)seconds);
|
|
}
|
|
|
|
bool powerSleepModeActive() { return g_cfg.deep_sleep_enabled; }
|
|
|
|
const char* powerSleepBlockedReason() {
|
|
if (!g_cfg.deep_sleep_enabled) return "deep sleep switched off";
|
|
if (g_cfg.stream_enabled && g_cfg.stream_blocks_sleep)
|
|
return "streaming enabled (stream_blocks_sleep)";
|
|
if (powerIsInhibited()) return powerInhibitSummary();
|
|
return "";
|
|
}
|
|
|
|
bool powerSleepAllowed() { return powerSleepBlockedReason()[0] == '\0'; }
|
|
|
|
// Configure wake sources and go. Does not return.
|
|
// `wake_on_radar` is separate from the config flag so the night sleep can
|
|
// force it off without mutating config.
|
|
static void enterDeepSleep(uint32_t seconds, bool wake_on_radar) {
|
|
if (seconds == 0) seconds = g_cfg.deep_sleep_s;
|
|
|
|
log_i("power: sleeping %us (wake: timer%s)", (unsigned)seconds,
|
|
wake_on_radar ? " + radar" : "");
|
|
|
|
// Flush the camera and radio explicitly. Letting deep sleep cut power out
|
|
// from under an initialised camera has been a reliable source of failures
|
|
// to re-init on the next boot.
|
|
cameraEnd();
|
|
WiFi.disconnect(true, /*eraseap=*/false);
|
|
WiFi.mode(WIFI_OFF);
|
|
delay(20);
|
|
|
|
esp_sleep_enable_timer_wakeup((uint64_t)seconds * 1000000ULL);
|
|
|
|
if (wake_on_radar) {
|
|
// ESP32-S3 has no EXT0; EXT1 with ANY_HIGH is the supported way to
|
|
// wake on an active-high presence pin. PIN_RADAR_OUT must be an RTC
|
|
// GPIO (0-21 on the S3) — GPIO3 is.
|
|
esp_sleep_enable_ext1_wakeup(1ULL << PIN_RADAR_OUT, ESP_EXT1_WAKEUP_ANY_HIGH);
|
|
|
|
// If the radar is *already* asserting when we try to sleep we would
|
|
// wake instantly and spin. Wait briefly for it to clear; if it does
|
|
// not, drop to a timer-only sleep for this cycle.
|
|
uint32_t wait_until = millis() + 3000;
|
|
while (digitalRead(PIN_RADAR_OUT) == HIGH && (int32_t)(wait_until - millis()) > 0) {
|
|
delay(50);
|
|
}
|
|
if (digitalRead(PIN_RADAR_OUT) == HIGH) {
|
|
log_w("power: radar still asserted, sleeping on timer only this cycle");
|
|
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_EXT1);
|
|
}
|
|
}
|
|
|
|
s_sleep_epoch += seconds;
|
|
s_last_sleep_s = seconds;
|
|
Serial.flush();
|
|
esp_deep_sleep_start();
|
|
// unreachable
|
|
}
|
|
|
|
void powerSleepUntilMorning() {
|
|
const uint32_t secs = nightSecondsUntilMorning();
|
|
if (secs == 0) return; // not night, or clock unsynced
|
|
|
|
// Night sleep overrides the deep_sleep_enabled switch, but never an OTA.
|
|
if (s_inhibit[INHIBIT_OTA]) {
|
|
log_w("power: night sleep deferred, OTA in progress");
|
|
return;
|
|
}
|
|
if (streamActive()) {
|
|
log_i("power: night sleep deferred, a stream is running");
|
|
return;
|
|
}
|
|
|
|
log_i("power: night — sleeping %u min until morning", (unsigned)(secs / 60));
|
|
enterDeepSleep(secs, /*wake_on_radar=*/false);
|
|
}
|
|
|
|
bool powerWouldSleepNow() {
|
|
if (!powerSleepAllowed()) return false;
|
|
return (millis() - s_last_activity_ms) >= g_cfg.sleep_idle_timeout_s * 1000UL;
|
|
}
|
|
|
|
bool powerMaybeSleep() {
|
|
if (!g_cfg.deep_sleep_enabled) return false;
|
|
|
|
const uint32_t now = millis();
|
|
const uint32_t idle_ms = now - s_last_activity_ms;
|
|
const uint32_t awake_ms = now - s_awake_since_ms;
|
|
|
|
// The hard cap is a deadlock escape, not a normal path: if something holds
|
|
// an inhibit forever (a stream whose socket never closes, an OTA that
|
|
// stalled) the device would otherwise stay awake until the battery is flat.
|
|
// OTA is the one inhibit we never override — sleeping mid-flash bricks it.
|
|
if (awake_ms > g_cfg.sleep_max_awake_s * 1000UL) {
|
|
if (s_inhibit[INHIBIT_OTA]) {
|
|
log_w("power: past max_awake but OTA in progress, staying up");
|
|
return false;
|
|
}
|
|
log_w("power: max_awake %us exceeded (inhibits: %s), sleeping anyway",
|
|
g_cfg.sleep_max_awake_s, powerInhibitSummary());
|
|
enterDeepSleep(0, g_cfg.sleep_wake_on_radar);
|
|
}
|
|
|
|
// Streaming enabled means "keep me reachable", not just "a session is
|
|
// running" — a sleeping device cannot be told to start one (SPEC C1).
|
|
if (!powerSleepAllowed()) return false;
|
|
if (idle_ms < g_cfg.sleep_idle_timeout_s * 1000UL) return false;
|
|
|
|
enterDeepSleep(0, g_cfg.sleep_wake_on_radar);
|
|
return true; // unreachable
|
|
}
|
|
|
|
void powerSleepNow(uint32_t seconds) {
|
|
// Deliberately laxer than powerSleepAllowed(): an explicit sleep request
|
|
// overrides "streaming is enabled" and the boot hold, because something
|
|
// asked for it on purpose. It does NOT override an OTA (bricks the device)
|
|
// or a live stream (cutting a viewer off mid-frame is never what was meant
|
|
// — send stream_stop first).
|
|
if (!g_cfg.deep_sleep_enabled) {
|
|
log_w("power: sleep_now ignored, deep sleep is switched off");
|
|
return;
|
|
}
|
|
if (s_inhibit[INHIBIT_OTA]) {
|
|
log_w("power: sleep_now refused, OTA in progress");
|
|
return;
|
|
}
|
|
if (streamActive()) {
|
|
log_w("power: sleep_now refused, a stream is running");
|
|
return;
|
|
}
|
|
enterDeepSleep(seconds, g_cfg.sleep_wake_on_radar);
|
|
}
|
|
|
|
void powerOnConfigChanged(bool was_enabled) {
|
|
if (was_enabled == g_cfg.deep_sleep_enabled) return;
|
|
|
|
if (g_cfg.deep_sleep_enabled) {
|
|
// Do not slam the door on the way out: finish this wake so the ack for
|
|
// the config command reaches the Pi, and so an operator who flipped the
|
|
// switch by mistake still has sleep_idle_timeout_s to flip it back.
|
|
log_i("power: deep sleep switched ON, will sleep after %us idle",
|
|
g_cfg.sleep_idle_timeout_s);
|
|
powerNoteActivity();
|
|
} else {
|
|
log_i("power: deep sleep switched OFF, staying awake");
|
|
s_hold_until_ms = 0;
|
|
// The ring and camera are brought back up by the caller in main.cpp,
|
|
// which owns their lifecycle.
|
|
}
|
|
}
|