Ersa Wearable Platform
Embedded smartwatch firmware and application framework
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
ersa::services::PowerManager Class Reference

#include <power_manager.h>

Collaboration diagram for ersa::services::PowerManager:
Collaboration graph
[legend]

Public Member Functions

 PowerManager (hal::IBattery &battery, events::EventBus &bus=events::EventBus::instance())
 
Result< void > init ()
 
void tick (uint32_t currentUptimeMs)
 
uint32_t nextBatterySampleDelayMs (uint32_t currentUptimeMs) const
 
WakeLock acquireWakeLock (const char *tag)
 
bool acquireWakeLockRaw (const char *tag)
 
bool releaseWakeLockRaw (const char *tag)
 
size_t getActiveWakeLockCount () const
 Return the number of active wake locks.
 
bool hasWakeLocks () const
 Return whether at least one wake lock blocks sleep.
 
void noteActivity (uint32_t currentUptimeMs)
 
uint32_t getIdleTimeMs (uint32_t currentUptimeMs) const
 
uint16_t getBatteryMv () const
 Return the latest battery voltage in millivolts.
 
uint32_t getBatterySampleAgeMs (uint32_t nowMs) const
 Return the age of the most recent battery sample in milliseconds.
 
bool hasBatterySample () const
 Return whether at least one battery sample has been recorded.
 
uint8_t getBatteryPercent () const
 Return estimated battery charge as a percentage.
 
bool isBatteryConnected () const
 Return whether a usable battery is connected.
 
bool isCharging () const
 Return whether external charging power is present.
 
BatteryPowerLevel getBatteryPowerLevel () const
 
PowerState getState () const
 Return the current service-level power state.
 
void requestState (PowerState state)
 Set the service-level power state.
 
bool canSleep () const
 
void enterDeepSleep (uint64_t sleepTimeUs=0)
 

Static Public Member Functions

static PowerManager & instance ()
 Return the installed process-wide power manager.
 
static void setInstance (PowerManager *instance)
 Install the process-wide power manager.
 

Static Public Attributes

static constexpr size_t MAX_WAKE_LOCKS = 16
 
static constexpr uint32_t DEEP_SLEEP_TIMEOUT_MS = 60000
 
static constexpr uint8_t LOW_BATTERY_PERCENT = 20
 
static constexpr uint16_t LOW_BATTERY_MV = 3600
 
static constexpr uint8_t CRITICAL_BATTERY_PERCENT = 3
 
static constexpr uint16_t CRITICAL_BATTERY_MV = 3350
 

Private Member Functions

uint32_t batterySampleIntervalMs () const
 

Private Attributes

hal::IBattery & battery_
 
events::EventBus & bus_
 
PowerState state_ {PowerState::Active}
 
uint32_t lastActivityMs_ {0}
 
uint32_t lastBatterySampleMs_ {0}
 
uint16_t cachedMv_ {0}
 
uint8_t cachedPercent_ {100}
 
bool cachedConnected_ {false}
 
bool cachedCharging_ {false}
 
BatteryPowerLevel batteryPowerLevel_ {BatteryPowerLevel::Normal}
 
uint8_t criticalSampleCount_ {0}
 
const char * wakeLockTags_ [MAX_WAKE_LOCKS]
 
uint16_t wakeLockReferences_ [MAX_WAKE_LOCKS]
 
size_t activeWakeLocks_ {0}
 

Detailed Description

Applies battery qualification, wake-lock accounting, and sleep eligibility. Product-level policy is kept here while IPowerManagement owns clocks, GPIO wake sources, and chip sleep APIs. This separation makes thresholds and activity decisions host-testable and keeps app code from bypassing radio coordination in the platform layer.

Constructor & Destructor Documentation

◆ PowerManager()

ersa::services::PowerManager::PowerManager ( hal::IBattery &  battery,
events::EventBus &  bus = events::EventBus::instance() 
)
explicit

Bind the battery HAL and event bus; neither is owned by the manager.

Member Function Documentation

◆ acquireWakeLock()

WakeLock ersa::services::PowerManager::acquireWakeLock ( const char *  tag)

Prefer this scoped lease when the protected operation has lexical lifetime.

◆ acquireWakeLockRaw()

bool ersa::services::PowerManager::acquireWakeLockRaw ( const char *  tag)

Manually acquire a tagged lease for state spanning multiple callbacks.

◆ batterySampleIntervalMs()

uint32_t ersa::services::PowerManager::batterySampleIntervalMs ( ) const
private

◆ canSleep()

bool ersa::services::PowerManager::canSleep ( ) const

True only when no active feature lease blocks automatic sleep.

◆ enterDeepSleep()

void ersa::services::PowerManager::enterDeepSleep ( uint64_t  sleepTimeUs = 0)

Delegate deep sleep; zero timer leaves configured GPIO wakes as sources.

◆ getActiveWakeLockCount()

size_t ersa::services::PowerManager::getActiveWakeLockCount ( ) const

Return the number of active wake locks.

◆ getBatteryMv()

uint16_t ersa::services::PowerManager::getBatteryMv ( ) const

Return the latest battery voltage in millivolts.

◆ getBatteryPercent()

uint8_t ersa::services::PowerManager::getBatteryPercent ( ) const

Return estimated battery charge as a percentage.

◆ getBatteryPowerLevel()

BatteryPowerLevel ersa::services::PowerManager::getBatteryPowerLevel ( ) const

Return the filtered battery band used by user alerts and sleep policy.

◆ getBatterySampleAgeMs()

uint32_t ersa::services::PowerManager::getBatterySampleAgeMs ( uint32_t  nowMs) const
inline

Return the age of the most recent battery sample in milliseconds.

◆ getIdleTimeMs()

uint32_t ersa::services::PowerManager::getIdleTimeMs ( uint32_t  currentUptimeMs) const

Compute wrap-safe idle duration from the last recorded activity.

◆ getState()

PowerState ersa::services::PowerManager::getState ( ) const

Return the current service-level power state.

◆ hasBatterySample()

bool ersa::services::PowerManager::hasBatterySample ( ) const
inline

Return whether at least one battery sample has been recorded.

◆ hasWakeLocks()

bool ersa::services::PowerManager::hasWakeLocks ( ) const

Return whether at least one wake lock blocks sleep.

◆ init()

Result< void > ersa::services::PowerManager::init ( )

Sample once, seed cached telemetry, and establish initial severity.

◆ instance()

PowerManager & ersa::services::PowerManager::instance ( )
static

Return the installed process-wide power manager.

◆ isBatteryConnected()

bool ersa::services::PowerManager::isBatteryConnected ( ) const

Return whether a usable battery is connected.

◆ isCharging()

bool ersa::services::PowerManager::isCharging ( ) const

Return whether external charging power is present.

◆ nextBatterySampleDelayMs()

uint32_t ersa::services::PowerManager::nextBatterySampleDelayMs ( uint32_t  currentUptimeMs) const

Return the sample deadline so the UI can sleep until useful work is due.

◆ noteActivity()

void ersa::services::PowerManager::noteActivity ( uint32_t  currentUptimeMs)

Reset the idle reference point after interaction or foreground work.

◆ releaseWakeLockRaw()

bool ersa::services::PowerManager::releaseWakeLockRaw ( const char *  tag)

Release one matching manual lease; false means the tag was not active.

◆ requestState()

void ersa::services::PowerManager::requestState ( PowerState  state)

Set the service-level power state.

◆ setInstance()

void ersa::services::PowerManager::setInstance ( PowerManager *  instance)
static

Install the process-wide power manager.

◆ tick()

void ersa::services::PowerManager::tick ( uint32_t  currentUptimeMs)

Sample when due, apply hysteresis/critical qualification, and publish state transitions. The explicit uptime makes interval behavior testable without a hardware clock.

Member Data Documentation

◆ activeWakeLocks_

size_t ersa::services::PowerManager::activeWakeLocks_ {0}
private

◆ battery_

hal::IBattery& ersa::services::PowerManager::battery_
private

◆ batteryPowerLevel_

BatteryPowerLevel ersa::services::PowerManager::batteryPowerLevel_ {BatteryPowerLevel::Normal}
private

◆ bus_

events::EventBus& ersa::services::PowerManager::bus_
private

◆ cachedCharging_

bool ersa::services::PowerManager::cachedCharging_ {false}
private

◆ cachedConnected_

bool ersa::services::PowerManager::cachedConnected_ {false}
private

◆ cachedMv_

uint16_t ersa::services::PowerManager::cachedMv_ {0}
private

◆ cachedPercent_

uint8_t ersa::services::PowerManager::cachedPercent_ {100}
private

◆ CRITICAL_BATTERY_MV

constexpr uint16_t ersa::services::PowerManager::CRITICAL_BATTERY_MV = 3350
staticconstexpr

Voltage threshold used to enter the critical battery band.

◆ CRITICAL_BATTERY_PERCENT

constexpr uint8_t ersa::services::PowerManager::CRITICAL_BATTERY_PERCENT = 3
staticconstexpr

Percentage threshold used to enter the critical battery band.

◆ criticalSampleCount_

uint8_t ersa::services::PowerManager::criticalSampleCount_ {0}
private

◆ DEEP_SLEEP_TIMEOUT_MS

constexpr uint32_t ersa::services::PowerManager::DEEP_SLEEP_TIMEOUT_MS = 60000
staticconstexpr

Idle interval relevant to legacy service-level deep-sleep transitions.

◆ lastActivityMs_

uint32_t ersa::services::PowerManager::lastActivityMs_ {0}
private

◆ lastBatterySampleMs_

uint32_t ersa::services::PowerManager::lastBatterySampleMs_ {0}
private

◆ LOW_BATTERY_MV

constexpr uint16_t ersa::services::PowerManager::LOW_BATTERY_MV = 3600
staticconstexpr

Voltage threshold used to enter the low battery band.

◆ LOW_BATTERY_PERCENT

constexpr uint8_t ersa::services::PowerManager::LOW_BATTERY_PERCENT = 20
staticconstexpr

Percentage threshold used to enter the low battery band.

◆ MAX_WAKE_LOCKS

constexpr size_t ersa::services::PowerManager::MAX_WAKE_LOCKS = 16
staticconstexpr

Maximum active lock tags tracked without heap allocation.

◆ state_

PowerState ersa::services::PowerManager::state_ {PowerState::Active}
private

◆ wakeLockReferences_

uint16_t ersa::services::PowerManager::wakeLockReferences_[MAX_WAKE_LOCKS]
private

◆ wakeLockTags_

const char* ersa::services::PowerManager::wakeLockTags_[MAX_WAKE_LOCKS]
private

The documentation for this class was generated from the following files: