/* * ANODE Power Policy State Machine & Configuration Persistence * (shared across anode-powerd, anode-power, anode-display, and unit tests) */ #ifndef ANODE_POWER_POLICY_H #define ANODE_POWER_POLICY_H #include #include #include #include #include typedef enum { ANODE_PWR_ACTIVE = 0, ANODE_PWR_DIMMED, ANODE_PWR_LOCKED, ANODE_PWR_SUSPENDED } anode_pwr_state_t; typedef struct { uint32_t dim_ms; uint32_t lock_ms; uint32_t suspend_ms; int crit_batt_pct; bool suspend_on_ac; } anode_power_cfg_t; typedef struct { bool dim_now; bool restore_brightness; bool lock_now; bool suspend_now; bool warn_low_batt; } anode_power_actions_t; static inline anode_power_cfg_t anode_power_cfg_default(void) { anode_power_cfg_t c; c.dim_ms = 300000U; /* 5 min */ c.lock_ms = 600000U; /* 10 min */ c.suspend_ms = 1800000U; /* 30 min (battery only) */ c.crit_batt_pct = 5; c.suspend_on_ac = false; return c; } static inline void anode_power_policy_step(const anode_power_cfg_t *cfg, anode_pwr_state_t *state, uint32_t idle_ms, bool on_ac, int batt_pct, bool batt_present, bool screen_locked, anode_power_actions_t *out) { anode_power_actions_t a = {0}; if (!cfg || !state) { if (out) *out = a; return; } /* Critical battery check takes precedence on battery */ if (batt_present && !on_ac && batt_pct >= 0 && batt_pct <= cfg->crit_batt_pct) { a.warn_low_batt = true; if (batt_pct <= (cfg->crit_batt_pct > 2 ? cfg->crit_batt_pct - 2 : 1)) { if (*state != ANODE_PWR_SUSPENDED) { *state = ANODE_PWR_SUSPENDED; a.suspend_now = true; } if (out) *out = a; return; } } /* User activity resumes from dimmed/locked/suspended before the earliest enabled timeout */ uint32_t eff_susp_ms = (cfg->suspend_ms > 0U && (cfg->suspend_on_ac || !on_ac)) ? cfg->suspend_ms : 0U; uint32_t resume_thresh = 0U; uint32_t cands[3] = {cfg->dim_ms, cfg->lock_ms, eff_susp_ms}; for (int i = 0; i < 3; i++) { if (cands[i] > 0U && (resume_thresh == 0U || cands[i] < resume_thresh)) { resume_thresh = cands[i]; } } if (resume_thresh == 0U || idle_ms < resume_thresh) { if (*state == ANODE_PWR_DIMMED || *state == ANODE_PWR_LOCKED || *state == ANODE_PWR_SUSPENDED) { a.restore_brightness = true; } *state = screen_locked ? ANODE_PWR_LOCKED : ANODE_PWR_ACTIVE; if (out) *out = a; return; } if (cfg->suspend_ms > 0 && idle_ms >= cfg->suspend_ms && (cfg->suspend_on_ac || !on_ac)) { if (*state != ANODE_PWR_SUSPENDED) { if (!screen_locked) a.lock_now = true; a.suspend_now = true; *state = ANODE_PWR_SUSPENDED; } if (out) *out = a; return; } if (cfg->lock_ms > 0 && idle_ms >= cfg->lock_ms) { if (*state < ANODE_PWR_LOCKED) { if (!screen_locked) a.lock_now = true; *state = ANODE_PWR_LOCKED; } if (out) *out = a; return; } if (cfg->dim_ms > 0 && idle_ms >= cfg->dim_ms && *state == ANODE_PWR_ACTIVE) { *state = ANODE_PWR_DIMMED; a.dim_now = true; } if (out) *out = a; } static inline int anode_power_policy_next_wake_ms(const anode_power_cfg_t *cfg, uint32_t idle_ms, bool on_ac, bool is_dimmed) { int timeout_ms = 15000; if (cfg) { uint32_t eff_susp_ms = (cfg->suspend_ms > 0U && (cfg->suspend_on_ac || !on_ac)) ? cfg->suspend_ms : 0U; uint32_t cands[3] = {cfg->dim_ms, cfg->lock_ms, eff_susp_ms}; for (int i = 0; i < 3; i++) { if (cands[i] > 0U && idle_ms < cands[i]) { uint32_t rem = cands[i] - idle_ms; if (rem < (uint32_t)timeout_ms) timeout_ms = (int)rem; } } } if (timeout_ms < 1000) timeout_ms = 1000; if (is_dimmed && timeout_ms > 500) timeout_ms = 500; return timeout_ms; } static inline bool anode_power_cfg_load(const char *path, anode_power_cfg_t *cfg) { if (!cfg) return false; *cfg = anode_power_cfg_default(); if (!path || !path[0]) return false; FILE *f = fopen(path, "r"); if (!f) return false; char line[256]; while (fgets(line, sizeof(line), f)) { char *s = line; while (*s == ' ' || *s == '\t') s++; if (*s == '\0' || *s == '#' || *s == '\n' || *s == '\r') continue; char *eq = strchr(s, '='); if (!eq) continue; *eq = '\0'; char *kend = eq; while (kend > s && (kend[-1] == ' ' || kend[-1] == '\t')) { *--kend = '\0'; } const char *k = s; const char *v = eq + 1; while (*v == ' ' || *v == '\t') v++; if (!*v) continue; char *endp = NULL; long val = strtol(v, &endp, 10); if (endp == v) continue; if (strcmp(k, "DIM_SEC") == 0 || strcmp(k, "LOCK_SEC") == 0 || strcmp(k, "SUSPEND_SEC") == 0) { long clamped = val < 0 ? 0L : (val > 86400L ? 86400L : val); uint32_t ms = (uint32_t)clamped * 1000U; if (strcmp(k, "DIM_SEC") == 0) cfg->dim_ms = ms; else if (strcmp(k, "LOCK_SEC") == 0) cfg->lock_ms = ms; else cfg->suspend_ms = ms; } else if (strcmp(k, "SUSPEND_ON_AC") == 0) { cfg->suspend_on_ac = (val != 0); } else if (strcmp(k, "CRIT_BATT_PCT") == 0) { if (val >= 1 && val <= 30) cfg->crit_batt_pct = (int)val; } } fclose(f); return true; } static inline bool anode_power_cfg_save(const char *path, const anode_power_cfg_t *cfg) { if (!path || !path[0] || !cfg) return false; char tmp[1088]; int tn = snprintf(tmp, sizeof(tmp), "%s.tmp", path); if (tn <= 0 || (size_t)tn >= sizeof(tmp)) return false; FILE *f = fopen(tmp, "w"); if (!f) return false; int wn = fprintf( f, "# ANODE idle power policy (0 disables dim/lock/suspend)\n" "DIM_SEC=%u\n" "LOCK_SEC=%u\n" "SUSPEND_SEC=%u\n" "SUSPEND_ON_AC=%d\n" "CRIT_BATT_PCT=%d\n", (unsigned)(cfg->dim_ms / 1000U), (unsigned)(cfg->lock_ms / 1000U), (unsigned)(cfg->suspend_ms / 1000U), cfg->suspend_on_ac ? 1 : 0, cfg->crit_batt_pct); bool bad = (wn < 0) || ferror(f); if (fclose(f) != 0) bad = true; if (bad || rename(tmp, path) != 0) { remove(tmp); return false; } return true; } static inline uint32_t anode_power_step_presets(uint32_t cur_ms, int dir, const uint32_t *presets, size_t count) { if (!presets || count == 0) return cur_ms; uint32_t cur_sec = cur_ms / 1000U; if (dir > 0) { for (size_t i = 0; i < count; i++) { if (presets[i] > cur_sec) return presets[i] * 1000U; } return presets[count - 1] * 1000U; } if (dir < 0) { for (size_t i = count; i > 0; i--) { if (presets[i - 1] < cur_sec) return presets[i - 1] * 1000U; } return presets[0] * 1000U; } return cur_ms; } static inline uint32_t anode_power_step_screen_timeout_ms(uint32_t cur_ms, int dir) { static const uint32_t PRESETS_SEC[] = {0U, 30U, 60U, 120U, 180U, 300U, 600U, 900U, 1200U, 1800U, 3600U}; return anode_power_step_presets(cur_ms, dir, PRESETS_SEC, sizeof(PRESETS_SEC) / sizeof(PRESETS_SEC[0])); } static inline uint32_t anode_power_step_suspend_timeout_ms(uint32_t cur_ms, int dir) { static const uint32_t PRESETS_SEC[] = {0U, 300U, 600U, 900U, 1200U, 1800U, 2700U, 3600U, 7200U}; return anode_power_step_presets(cur_ms, dir, PRESETS_SEC, sizeof(PRESETS_SEC) / sizeof(PRESETS_SEC[0])); } static inline void anode_power_format_timeout(uint32_t ms, char *out, size_t sz) { if (!out || sz == 0) return; uint32_t sec = ms / 1000U; if (sec == 0U) { snprintf(out, sz, "Off"); } else if (sec >= 3600U && (sec % 3600U) == 0U) { snprintf(out, sz, "%uh", (unsigned)(sec / 3600U)); } else if (sec >= 60U && (sec % 60U) == 0U) { snprintf(out, sz, "%um", (unsigned)(sec / 60U)); } else { snprintf(out, sz, "%us", (unsigned)sec); } } #endif /* ANODE_POWER_POLICY_H */