Integrate Your System Power State Class (Compact 2013)

3/26/2014

After you define your system power state class and its methods, you must integrate it with the PowerStateManager class so that your system power state class can be instantiated by Power Manager. The PowerStateManager class contains the main Power Manager event loop. This loop implements Power Manager's response to external events such as being docked in a cradle, running low on battery power, or going on or off AC power. To integrate your class, you make a simple modification to PowerStateManager to instantiate your system power state class and add the resulting object to Power Manager’s list of system power state objects. After you have added your system power state object to this list, you can modify Power Manager to map power state hints to your system power state.

Instantiate the PowerState Class

The PowerStateManager::CreatePowerStateList method creates a linked list of system power state objects. This method is defined in the source file Pwstatemgr.cpp. To integrate your system power state class with PowerStateManager, you must instantiate your system power state class in this method and add the resulting object to the power state list in PowerStateManager. Notice that the default constructor for the PowerState class accepts a pointer to PowerState as its second argument. PowerStateManager::CreatePowerStateList uses nested constructor calls to generate a linked list of system power state objects, passing the previously-created object to the second parameter in each constructor call. You insert your system power class object by adding it to the second parameter of the last instantiation in the list. When you add your instantiation to the end of the list, you do not need to specify the second parameter because the constructor defaults the second parameter to NULL (the end of the list) if it is not specified.

For example, to add the new PowerStateOff class described earlier:

BOOLPowerStateManager::CreatePowerStateList (){
    if (m_pPowerStateList == NULL ) {        m_pPowerStateList =             new PowerStateOn (this,             new PowerStateUserIdle (this,             new PowerStateBacklightOff (this,             new PowerStateScreenOff (this,             new PowerStateUnattended (this,             new PowerStateResuming (this,             new PowerStateSuspended (this,             new PowerStateColdReboot (this,             new PowerStateReboot (this,             new PowerStateOff (this))))))))));     }

When the Platform Manager model device driver (MDD) instantiates PowerStateManager, it calls the PowerStateManager::Init method. The PowerStateManager::Init method calls PowerStateManager::CreatePowerStateList after initializing various notification queues and timers. After the Platform Manager MDD calls the PowerStateManager::Init method (see the PlatformManageSystemPower function in Platform.cpp), it calls the PowerStateManager::ThreadRun method, which starts the Power Manager event loop. You do not have to create a thread for PowerStateManager::ThreadRun because the Power Manager MDD creates this thread for you (see Pminit.cpp in the Power Manager MDD source code). Because PowerStateManager::ThreadRun iterates through the linked list of system power state objects, your new system power state object must be part of this list before the event loop thread starts.

Map Power State Hints

If you want Power Manager to map one or more power state hint flags to your new power state, you must modify the PlatformPowerStateHint function found in Platform.cpp. To add your system power state to this function, set the variable pszMappedStateName to the string name of your system power state. To implement the mapping, set this variable in the case statement associated with the power state hint flag that it maps to. For example, to cause Power Manager to transition directly to the Off system power state when applications pass the POWER_STATE OFF hint flag, modify the POWER_STATE_OFF case statement to explicitly map the POWER_STATE_OFF hint flag to the Off state rather than defaulting to the Suspend state:

case POWER_STATE_OFF:     pszMappedStateName = _T ("off");    break;

After you make this change, Power Manager will transition to the Off state when it receives a POWER_STATE_OFF hint. For example, when an application calls the Power Manager SetSystemPowerState function with the power state hint flag POWER_STATE_OFF, Power Manager transitions your platform directly to the Off system power state and calls your PowerStateOff::EnterState method.

See Also

Concepts

Define New System Power States