107 lines
2.8 KiB
C
107 lines
2.8 KiB
C
/*
|
|||
|
|
* ANODE Power Policy State Machine (shared across anode-powerd and tests)
|
||
|
|
*/
|
||
|
|
#ifndef ANODE_POWER_POLICY_H
|
||
|
|
#define ANODE_POWER_POLICY_H
|
||
|
|
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdint.h>
|
||
|
|
|
||
|
|
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 = 60000U;
|
||
|
|
c.lock_ms = 180000U;
|
||
|
|
c.suspend_ms = 600000U;
|
||
|
|
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/suspended */
|
||
|
|
if (idle_ms < cfg->dim_ms) {
|
||
|
|
if (*state == ANODE_PWR_DIMMED || *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;
|
||
|
|
}
|
||
|
|
|
||
|
|
#endif /* ANODE_POWER_POLICY_H */
|