The RDKB Power Manager is a lightweight system service responsible for managing power state transitions and coordinating orderly shutdown and startup of RDKB CCSP components based on power source changes and thermal conditions. This component monitors power transition events from the system layer and orchestrates component lifecycle management during power state changes. Power Manager handles transitions between AC power and battery operation on supported platforms, as well as thermal management states to protect hardware from overheating conditions.

The component operates as a daemon process that listens to sysevent notifications for power state changes, validates transitions, invokes management scripts to control RDKB component lifecycles, and publishes the current power state for other components to consume. Power Manager maintains minimal resource footprint for power event handling and component coordination across different power modes.

```mermaid
graph LR
    subgraph "External Systems"
        SYSD[System Power Events]
        THERM[Thermal Monitoring]
    end

    subgraph "RDK-B Platform"
        PWR["Power Manager"]
        PANDM["CCSP P&M"]
        MTA["MTA Agent"]
        WIFI["WiFi Manager"]
        MOCA["MoCA Agent"]
        LM["LMLite"]
        HARV["Harvester"]

        subgraph "System Layer"
            SYSEVT["Sysevent Daemon"]
            SYSD_SVC["Systemd Services"]
        end

        subgraph "HAL Layer"
            MTAHAL["MTA HAL"]
        end
    end

    SYSD -->|Power Events| SYSEVT
    THERM -->|Thermal Events| SYSEVT
    SYSEVT <-->|rdkb-power-transition<br />rdkb-power-state| PWR
    PWR -->|Script Execution| SYSD_SVC
    PWR -->|Battery Query| MTAHAL
    SYSD_SVC -.->|Shutdown Order| HARV
    SYSD_SVC -.->|Shutdown Order| LM
    SYSD_SVC -.->|Shutdown Order| WIFI
    SYSD_SVC -.->|Shutdown Order| MOCA
    SYSD_SVC -.->|Keep Running| PANDM
    SYSD_SVC -.->|Keep Running| MTA

    classDef external fill:#fff3e0,stroke:#ef6c00,stroke-width:2px;
    classDef pwrMgr fill:#e3f2fd,stroke:#1976d2,stroke-width:3px;
    classDef rdkbComponent fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px;
    classDef system fill:#fce4ec,stroke:#c2185b,stroke-width:2px;

    class SYSD,THERM external;
    class PWR pwrMgr;
    class PANDM,MTA,WIFI,MOCA,LM,HARV rdkbComponent;
    class SYSEVT,SYSD_SVC,MTAHAL system;
```

Key Features & Responsibilities:

Design

Power Manager implements a lightweight event-driven architecture focused on power state monitoring and component coordination with minimal system overhead. The design separates power event detection, state transition validation, and component lifecycle management. The component operates as a standalone daemon process with dedicated threading for asynchronous event handling without blocking critical operations.

The architecture maintains a simple state machine tracking current power mode and validating transition requests to prevent invalid state changes. Power Manager delegates actual component shutdown and startup operations to a companion shell script that encapsulates platform-specific systemd service management commands. This separation allows platform-specific customization while maintaining consistent core power management logic.

Sysevent serves as the primary IPC mechanism for both receiving power transition notifications and publishing current state information. The component establishes two sysevent connections - one dedicated to asynchronous event notifications and another for synchronous state publication operations. On startup, Power Manager queries battery status through MTA HAL on battery-capable platforms to initialize with correct power state before beginning event processing.

```mermaid
flowchart LR
    subgraph External ["External Services"]
        SYSE[Sysevent Daemon]
        HAL[MTA HAL]
        SYSD[Systemd Services]
    end

    subgraph PowerMgr ["Power Manager"]
        MAIN[Main Process]
        INIT[Initialization Module]
        STATE[State Machine]
        SYSE_HAND[Sysevent Handler Thread]
        TRANS[Transition Handler]
        SCRIPT[Script Executor]
        SHELL[Management Script<br/>rdkb_power_manager.sh]
    end

    MAIN -->|Initialize| INIT
    INIT -->|Register Events| SYSE
    INIT -->|Query Battery Status| HAL
    INIT -->|Set Initial State| STATE
    INIT -->|Spawn Thread| SYSE_HAND
    SYSE_HAND <-->|Get Notifications| SYSE
    SYSE_HAND -->|Validate & Process| TRANS
    TRANS -->|Check Current State| STATE
    TRANS -->|Execute Script| SCRIPT
    SCRIPT -->|Invoke| SHELL
    SHELL -->|systemctl commands| SYSD
    TRANS -->|Update State| STATE
    TRANS -->|Publish State| SYSE

    classDef component fill:#e1f5fe,stroke:#0277bd,stroke-width:2px
    classDef external fill:#e8f5e8,stroke:#2e7d32,stroke-width:2px

    class MAIN,INIT,STATE,SYSE_HAND,TRANS,SCRIPT,SHELL component
    class SYSE,HAL,SYSD external
```

Prerequisites and Dependencies

Build-Time Flags and Configuration:

Configure OptionDISTRO FeatureBuild FlagPurposeDefault
--enable-gtestappN/AGTEST_ENABLEEnable GTest support for unit testingDisabled
N/ARDK loggingFEATURE_SUPPORT_RDKLOGEnable RDK logger integration for standardized loggingEnabled
N/ACrash reportingINCLUDE_BREAKPADEnable breakpad crash handler for minidump generationEnabled
N/ADevelopment_DEBUGEnable debug output redirection to consoleEnabled in source


RDK-B Platform and Integration Requirements:


Threading Model:

Power Manager implements a multi-threaded architecture with a main process thread and a dedicated worker thread for asynchronous sysevent notification handling.

Component State Flow

Initialization to Active State

Power Manager follows a sequential initialization process: establish sysevent connections, query initial power state, spawn event handler thread, then transition to active monitoring mode.

```mermaid
sequenceDiagram
    autonumber
    participant System as System Startup
    participant PowerMgr as Power Manager
    participant Sysevent as Sysevent Daemon
    participant HAL as MTA HAL
    participant Thread as Sysevent Handler Thread

    System->>PowerMgr: Start rdkbPowerMgr Process
    Note over PowerMgr: State: Initializing<br/>Fork and daemonize process

    PowerMgr->>PowerMgr: Check PID File /tmp/.rdkbPowerMgr.pid
    Note over PowerMgr: Prevent duplicate instances

    PowerMgr->>Sysevent: sysevent_open(rdkb_power_manger)
    Sysevent-->>PowerMgr: sysevent_fd and token
    PowerMgr->>Sysevent: sysevent_open(rdkb_power_manger-gs)
    Sysevent-->>PowerMgr: sysevent_fd_gs and token_gs
    Note over PowerMgr: State: Initializing → LoadingDefaults

    PowerMgr->>HAL: mta_hal_BatteryGetPowerStatus()
    Note over PowerMgr: Query battery status on XBB1/CBR2
    HAL-->>PowerMgr: Battery Status or AC
    PowerMgr->>PowerMgr: Set gCurPowerState
    Note over PowerMgr: State: LoadingDefaults → RegisteringEvents

    PowerMgr->>Sysevent: sysevent_set(rdkb-power-state)
    PowerMgr->>Thread: pthread_create(PwrMgr_sysevent_handler)
    Thread->>Sysevent: sysevent_setnotification(rdkb-power-transition)
    Sysevent-->>Thread: async_id registered
    Note over PowerMgr: State: RegisteringEvents → Active

    PowerMgr->>System: Initialization Complete
    Note over Thread: State: Active<br/>Event monitoring loop

    loop Power State Monitoring
        Thread->>Sysevent: sysevent_getnotification()
        Sysevent-->>Thread: Power Transition Event
        Thread->>Thread: PwrMgr_StateTranstion()
    end

    System->>PowerMgr: Stop Signal
    PowerMgr->>Thread: pthread_join()
    Thread-->>PowerMgr: Thread Terminated
    Note over PowerMgr: State: Active → Shutdown
    PowerMgr->>System: Process Exit
```

Runtime State Changes and Context Switching

During active operation, Power Manager responds to power transition events by validating state and managing component lifecycle.

State Change Triggers:

Context Switching Scenarios:

Call Flow

Initialization Call Flow:

```mermaid
sequenceDiagram
    participant Main as Main Process
    participant Init as PwrMgr_Init
    participant Reg as PwrMgr_Register_sysevent
    participant Def as PwrMgr_SetDefaults
    participant HAL as MTA HAL
    participant Sysevent as Sysevent Daemon

    Main->>Main: Daemonize Process
    Main->>Main: checkIfAlreadyRunning()
    Main->>Init: PwrMgr_Init()
    Init->>Reg: PwrMgr_Register_sysevent()
    Reg->>Sysevent: sysevent_open(rdkb_power_manger)
    Sysevent-->>Reg: sysevent_fd and token
    Reg->>Sysevent: sysevent_open(rdkb_power_manger-gs)
    Sysevent-->>Reg: sysevent_fd_gs and token_gs
    Reg->>Def: PwrMgr_SetDefaults()
    Def->>HAL: mta_hal_BatteryGetPowerStatus()
    HAL-->>Def: Battery Status AC or Battery
    Def->>Def: Set gCurPowerState
    Def->>Def: sleep(5)
    Def->>Sysevent: sysevent_set(rdkb-power-state)
    Note over Sysevent: Publish initial power state
    Def-->>Reg: Initialization Complete
    Reg-->>Init: Registration Complete
    Init->>Init: pthread_create(PwrMgr_sysevent_handler)
    Init-->>Main: Initialization Complete
```

Power Transition Processing Call Flow:

```mermaid
sequenceDiagram
    participant External as External Event Source
    participant Sysevent as Sysevent Daemon
    participant Thread as Sysevent Handler Thread
    participant Trans as PwrMgr_StateTranstion
    participant Script as rdkb_power_manager.sh
    participant Systemd as Systemd

    External->>Sysevent: sysevent set rdkb-power-transition POWER_TRANS_*
    Sysevent->>Thread: Notification with event name and value
    Thread->>Thread: sysevent_getnotification(rdkb-power-transition)
    Thread->>Thread: Parse Event Name and Value
    Thread->>Trans: PwrMgr_StateTranstion(POWER_TRANS_*)
    Trans->>Trans: Convert String to Power State Enum
    Trans->>Trans: Validate Current vs New State

    alt Valid Transition AC/COOLED
        Trans->>Script: v_secure_system(rdkb_power_manager.sh POWER_TRANS_AC/COOLED)
        Script->>Systemd: systemctl start CcspMoca.service
        Script->>Systemd: systemctl start ccspwifiagent/onewifi.service
        Script->>Systemd: systemctl start CcspLMLite.service
        Script->>Systemd: systemctl start harvester.service
        Systemd-->>Script: Services Started
        Script-->>Trans: Exit Code 0 Success
        Trans->>Trans: Update gCurPowerState
        Trans->>Sysevent: sysevent_set(rdkb-power-state, AC/ThermalCooled)
    else Valid Transition BATTERY/HOT
        Trans->>Script: v_secure_system(rdkb_power_manager.sh POWER_TRANS_BATTERY/HOT)
        Script->>Systemd: systemctl stop harvester.service
        Script->>Systemd: systemctl stop CcspLMLite.service
        Script->>Systemd: systemctl stop ccspwifiagent/onewifi.service
        Script->>Systemd: systemctl stop CcspMoca.service
        Systemd-->>Script: Services Stopped
        Script-->>Trans: Exit Code 0 Success
        Trans->>Trans: Update gCurPowerState
        Trans->>Sysevent: sysevent_set(rdkb-power-state, Battery/ThermalHot)
    else Invalid or Same State
        Trans->>Trans: Log Warning and Ignore
    end

    Trans-->>Thread: Transition Result
    Thread->>Thread: Continue Event Loop
```

Internal Modules

Power Manager consists of a single monolithic process with functional separation through internal functions rather than separate module files.

Module/ClassDescriptionKey Files
Main ProcessProcess entry point handling daemonization, PID file management, initialization orchestration, and breakpad crash handler setuppwrMgr.c main function
Initialization HandlerComponent initialization logic including sysevent registration, default state setup, and thread creationpwrMgr.c PwrMgr_Init, PwrMgr_Register_sysevent, PwrMgr_SetDefaults
Sysevent Handler ThreadAsynchronous event monitoring loop receiving and dispatching power transition notificationspwrMgr.c PwrMgr_sysevent_handler
State Transition EnginePower state validation and transition execution including script invocation and state publicationpwrMgr.c PwrMgr_StateTranstion
Component Lifecycle Management ScriptShell script performing orderly shutdown and startup of RDKB components via systemd service management invoked by State Transition Enginescripts/rdkb_power_manager.sh PwrMgr_TearDownComponents, PwrMgr_StartupComponents
Configuration ModulePower state definitions and transition string mappingspwrMgr.h PWRMGR_PwrState, PWRMGR_PwrStateItem

Component Interactions

Power Manager maintains focused interactions with system services for event monitoring and component lifecycle management through sysevent notifications and shell script execution for orderly component shutdown and startup sequences.

Configuration Persistence: Power Manager does not persist power state configuration across reboots. The component queries MTA HAL on startup to determine initial power state on battery-capable platforms, defaulting to AC mode otherwise. Runtime power state changes are handled via sysevent notifications and are not stored in syscfg or PSM.

Events Published by Power Manager:

Event NameEvent Topic/PathTrigger ConditionSubscriber Components
Power State Updaterdkb-power-state syseventSuccessful power state transition completionAll RDKB components monitoring power state for operational adjustments

Events Subscribed by Power Manager:

Event NameEvent Topic/PathAction Taken
Power Transition Requestrdkb-power-transition syseventValidate and execute power state transition invoking management script

IPC Flow Patterns

Primary IPC Flow - Power State Transition:

```mermaid
sequenceDiagram
    participant External as External Event Source
    participant Sysevent as Sysevent Daemon
    participant PowerMgr as Power Manager
    participant Script as rdkb_power_manager.sh
    participant Systemd as Systemd

    External->>Sysevent: sysevent set rdkb-power-transition POWER_TRANS_BATTERY
    Note over Sysevent: Event: rdkb-power-transition<br/>Value: POWER_TRANS_BATTERY
    Sysevent->>PowerMgr: sysevent_getnotification() Callback
    Note over PowerMgr: PwrMgr_StateTranstion() validates request
    PowerMgr->>Script: v_secure_system(rdkb_power_manager.sh POWER_TRANS_BATTERY)
    Script->>Systemd: systemctl stop harvester.service
    Script->>Systemd: systemctl stop CcspLMLite.service
    Script->>Systemd: systemctl stop ccspwifiagent/onewifi.service
    Script->>Systemd: systemctl stop CcspMoca.service
    Note over Systemd: Non-essential services stopped
    Systemd-->>Script: Services Stopped Successfully
    Script-->>PowerMgr: Exit Status 0

    alt Success
        PowerMgr->>PowerMgr: Update gCurPowerState to BATTERY
        PowerMgr->>Sysevent: sysevent_set(rdkb-power-state, Battery)
        Note over Sysevent: State: Battery published
    else Failure
        PowerMgr->>PowerMgr: PWRMGRLOG(ERROR) Keep Current State
    end
```

Event Notification Flow:

```mermaid
sequenceDiagram
    participant Sysevent as Sysevent Daemon
    participant PowerMgr as Power Manager Sysevent Thread
    participant Components as RDKB Components

    loop Active Monitoring
        PowerMgr->>Sysevent: sysevent_getnotification()
        Note over PowerMgr: Blocking call waiting for rdkb-power-transition
        Sysevent-->>PowerMgr: Event: rdkb-power-transition<br/>Value: POWER_TRANS_AC
        PowerMgr->>PowerMgr: PwrMgr_StateTranstion(POWER_TRANS_AC)
        PowerMgr->>PowerMgr: v_secure_system(rdkb_power_manager.sh POWER_TRANS_AC)
        Note over PowerMgr: Start CcspMoca, WiFi, LMLite, Harvester
        PowerMgr->>Sysevent: sysevent_set(rdkb-power-state, AC)
    end

    Note over Sysevent: Power state change published:<br/>rdkb-power-state = AC
    Sysevent->>Components: Notify subscribed RDKB components
    Components->>Components: Adjust behavior for AC power mode
```

Implementation Details

Major HAL APIs Integration

Power Manager integrates with MTA HAL on battery-capable platforms to query initial battery power status during component initialization.

Core HAL APIs:

HAL APIPurposeImplementation File
mta_hal_BatteryGetPowerStatus()Query current battery power status returning AC Battery or Unknown for initial state determinationpwrMgr.c PwrMgr_SetDefaults function

Key Implementation Logic

Key Configuration Files

Configuration FilePurposeOverride Mechanisms
/tmp/.rdkbPowerMgr.pidProcess ID file preventing duplicate daemon instancesN/A system managed
/etc/device.propertiesPlatform configuration properties used by management scriptPlatform build configuration
/etc/debug.iniRDK logger initialization configurationManual configuration file edit
/usr/ccsp/pwrMgr/rdkb_power_manager.shComponent lifecycle management scriptPlatform-specific customization