⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 platform.cpp

📁 此代码为WCE5.0下电源管理的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//

//
// This module implements a set of states defined by the MS Mobile Devices
// Division as a baseline for Pocket PC devices.  OEMs may choose to customize
// this file to support the hardware specific to their platform.
//

#include <pmimpl.h>
#include <nkintr.h>
#include <extfile.h>
#include <pmpolicy.h>

// This typedef describes the system activity states.  These are independent of
// factors such as AC power vs. battery, in cradle or not, etc.  OEMs may choose
// to add their own activity states if they customize this module.
typedef enum { 
    On,                     // system is running normally with UI enabled
    ScreenOff,              // system is running normally with minimal UI
    Unattended,             // system is working in unattended mode
    Resuming,               // system is determining what to do after a resume
    Suspend                 // system suspended, all devices off (or wake-enabled)
} PLATFORM_ACTIVITY_STATE, *PPLATFORM_ACTIVITY_STATE;

// This typedef describes activity events such as user activity or inactivity,
// power status changes, etc.  OEMs may choose to factor other events into their
// system power state transition decisions.
typedef enum {
    NoActivity,
    UserActivity,
    UserInactivity,
    SystemIdleTimerWasReset,
    EnterUnattendedModeRequest,
    LeaveUnattendedModeRequest,
    Timeout,
    RestartTimeouts,
    PowerSourceChange,
    Resume,
    SystemPowerStateChange
} PLATFORM_ACTIVITY_EVENT, *PPLATFORM_ACTIVITY_EVENT;

typedef BOOL (WINAPI * PFN_GwesPowerDown)(void);
typedef void (WINAPI * PFN_GwesPowerUp)(BOOL);
typedef BOOL (WINAPI * PFN_ShowStartupWindow)( void );


// platform-specific default values
#define DEF_ACSUSPENDTIMEOUT            600         // in seconds, 0 to disable
#define DEF_ACRESUMINGSUSPENDTIMEOUT    15          // in seconds, 0 to disable
#define DEF_BATTSUSPENDTIMEOUT          300         // in seconds, 0 to disable
#define DEF_BATTRESUMINGSUSPENDTIMEOUT  15          // in seconds, 0 to disable

#define MAXACTIVITYTIMEOUT              (0xFFFFFFFF / 1000)  // in seconds

// gwes suspend/resume functions
PFN_GwesPowerDown gpfnGwesPowerDown = NULL;
PFN_GwesPowerUp gpfnGwesPowerUp = NULL;
PFN_ShowStartupWindow gpfnShowStartupWindow = NULL;

// this variable is protected by the system power state critical section
BOOL gfSystemSuspended = FALSE;
BOOL gfFileSystemsAvailable = TRUE;
GUID idBlockDevices = {0x8DD679CE, 0x8AB4, 0x43c8, { 0xA1, 0x4A, 0xEA, 0x49, 0x63, 0xFA, 0xA7, 0x15 } };

// activity timer variables
HANDLE ghevReloadActivityTimeouts;
HANDLE ghevRestartTimers;
HANDLE ghevSignalUserActivity;
HANDLE ghevUserActive;
HANDLE ghevUserInactive;
HANDLE ghevSystemIdleTimerReset;
HANDLE ghevGwesReady;
BOOL gfGwesReady;
DWORD gdwACSuspendTimeout;                   // AC power, from any state other than "Resuming"
DWORD gdwACResumingSuspendTimeout;           // AC power, from "Resuming"
DWORD gdwBattSuspendTimeout;                 // battery, from any state other than "Resuming"
DWORD gdwBattResumingSuspendTimeout;         // battery, from "Resuming"
DWORD gdwStateTimeLeft = INFINITE;
PLATFORM_ACTIVITY_STATE gActivityState = On;
HANDLE ghevBootPhase2;
INT giPreSuspendPriority;
INT giSuspendPriority;
DWORD gdwUnattendedModeRequests;
BOOL gfSuspendToUnattendedMode;

BOOL gfPasswordOn = 0;
BOOL gfSupportPowerButtonRelease = FALSE;

// need "C" linkage for compatibility with C language PDD implementations
extern "C" {
POWER_BROADCAST_POWER_INFO gSystemPowerStatus;
};

// This routine is called to check the consistency of the system's power 
// management registry settings.  It is called during during power manager
// initialization.  If no registry settings are found, OEMs can use this
// routine to set them up.  The routine returns FALSE if some fatal error
// is discovered and the registry is unusable.  This will halt PM 
// initialization.  If the registry is OK (or can be initialized/repaired)
// this routine returns ERROR_SUCCESS, otherwise it returns an error code.
EXTERN_C DWORD WINAPI
PlatformValidatePMRegistry(VOID)
{
    HKEY hkPM = NULL, hkSubkey;
    LPTSTR pszSubKey;
    DWORD dwStatus, dwDisposition;
    SETFNAME(_T("PlatformValidatePMRegistry"));

    PMLOGMSG(ZONE_INIT, (_T("+%s\r\n"), pszFname));

    // open the PM registry key
    dwStatus = RegCreateKeyEx(HKEY_LOCAL_MACHINE, PWRMGR_REG_KEY, 0, NULL, 0, 0, NULL, 
        &hkPM, &dwDisposition);
    if(dwStatus != ERROR_SUCCESS) {
        PMLOGMSG(ZONE_ERROR, (_T("%s: can't open '%s', error is %d\r\n"), pszFname, 
            PWRMGR_REG_KEY, dwStatus));
    } 
    if (dwStatus== ERROR_SUCCESS  && dwDisposition != REG_CREATED_NEW_KEY ) { // Exit Key.
        DWORD dwValue =  0;
        DWORD dwSize = sizeof(DWORD);
        if (RegQueryTypedValue(hkPM, PM_SUPPORT_PB_RELEASE, &dwValue, &dwSize, REG_DWORD)==ERROR_SUCCESS) {
            gfSupportPowerButtonRelease = (dwValue!=0);
        }
    }

    // verify the On system state
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("State\\On");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                // allow devices to go to any power level
                DWORD dwValue = 0;      // D0
                dwStatus = RegSetValueEx(hkSubkey, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                
                // write the flags value
                if(dwStatus == ERROR_SUCCESS) {
                    dwValue = POWER_STATE_ON;
                    dwStatus = RegSetValueEx(hkSubkey, _T("Flags"), 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // verify the ScreenOff system state
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("State\\ScreenOff");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                // allow devices to go to any power level
                DWORD dwValue = 0;      // D0
                dwStatus = RegSetValueEx(hkSubkey, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                
                // write the flags value
                if(dwStatus == ERROR_SUCCESS) {
                    dwValue = POWER_STATE_IDLE;
                    dwStatus = RegSetValueEx(hkSubkey, _T("Flags"), 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                }

                // force displays off in this state
                if(dwStatus == ERROR_SUCCESS) {
                    HKEY hkDisplay;
                    dwStatus = RegCreateKeyEx(hkSubkey, PMCLASS_DISPLAY, 0, NULL, 0, 0, NULL, &hkDisplay, NULL);
                    if(dwStatus == ERROR_SUCCESS) {
                        dwValue = 4;    // D4
                        dwStatus = RegSetValueEx(hkDisplay, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                        RegCloseKey(hkDisplay);
                    }
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // verify the Unattended system state
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("State\\Unattended");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                // allow devices to go to any power level
                DWORD dwValue = 0;      // D0
                dwStatus = RegSetValueEx(hkSubkey, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                
                // write the flags value
                if(dwStatus == ERROR_SUCCESS) {
                    dwValue = 0;
                    dwStatus = RegSetValueEx(hkSubkey, _T("Flags"), 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                }

                // force displays off in this state
                if(dwStatus == ERROR_SUCCESS) {
                    HKEY hkDisplay;
                    dwStatus = RegCreateKeyEx(hkSubkey, PMCLASS_DISPLAY, 0, NULL, 0, 0, NULL, &hkDisplay, NULL);
                    if(dwStatus == ERROR_SUCCESS) {
                        dwValue = 4;    // D4
                        dwStatus = RegSetValueEx(hkDisplay, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                        RegCloseKey(hkDisplay);
                    }
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // verify the Resuming system state
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("State\\Resuming");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                // devices should be very low-power in this state, while we figure out whether
                // or not to fully resume.
                DWORD dwValue = 2;      // D2
                dwStatus = RegSetValueEx(hkSubkey, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                
                // write the flags value
                if(dwStatus == ERROR_SUCCESS) {
                    dwValue = 0;
                    dwStatus = RegSetValueEx(hkSubkey, _T("Flags"), 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                }

                // force displays off in this state
                if(dwStatus == ERROR_SUCCESS) {
                    HKEY hkDisplay;
                    dwStatus = RegCreateKeyEx(hkSubkey, PMCLASS_DISPLAY, 0, NULL, 0, 0, NULL, &hkDisplay, NULL);
                    if(dwStatus == ERROR_SUCCESS) {
                        dwValue = 4;    // D4
                        dwStatus = RegSetValueEx(hkDisplay, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                        RegCloseKey(hkDisplay);
                    }
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // verify the Suspend system state
    if(dwStatus == ERROR_SUCCESS) {
        pszSubKey = _T("State\\Suspend");
        dwStatus = RegCreateKeyEx(hkPM, pszSubKey, 0, NULL, 0, 0, NULL, &hkSubkey,
            &dwDisposition);
        if(dwStatus == ERROR_SUCCESS) {
            if(dwDisposition == REG_CREATED_NEW_KEY) {
                // enable all wake sources by default in this state
                DWORD dwValue = 3;      // D3
                dwStatus = RegSetValueEx(hkSubkey, NULL, 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                
                // write the flags value
                if(dwStatus == ERROR_SUCCESS) {
                    dwValue = POWER_STATE_SUSPEND;
                    dwStatus = RegSetValueEx(hkSubkey, _T("Flags"), 0, REG_DWORD, (LPBYTE) &dwValue, sizeof(dwValue));
                }
            }
            RegCloseKey(hkSubkey);
        }

        PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_ERROR, 
            (_T("%s: error %d while creating or writing values in '%s\\%s'\r\n"), pszFname, dwStatus,
            PWRMGR_REG_KEY, pszSubKey));
    }

    // verify interface guids

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -