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

📄 pwsdef.cpp

📁 LX 800 WindowsCE 6.0 BSP
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    m_hSystemApiCalled = CreateEvent(NULL, FALSE, FALSE,NULL) ;

    m_pUserActivity = ActivityTimerFindByName(_T("UserActivity"));;
    m_pSystemActivity = ActivityTimerFindByName(_T("SystemActivity"));
}
DefaultPowerStateManager::~DefaultPowerStateManager()
{
    if (m_hevReloadActivityTimeouts)
        CloseHandle(m_hevReloadActivityTimeouts);
    if (m_hevBootPhase2)
        CloseHandle(m_hevBootPhase2) ;
        
    if (m_hevRestartTimers)
        CloseHandle(m_hevRestartTimers);
    if (m_hqNotify)
        PmPolicyCloseNotificationQueue(m_hqNotify);
    
    if (m_hSystemApiCalled)
        CloseHandle(m_hSystemApiCalled);

}
BOOL  DefaultPowerStateManager::Init()
{
    SETFNAME(_T("PowerStateManager::Init"));
    if(m_hevReloadActivityTimeouts == NULL 
        || m_hevRestartTimers == NULL
        || m_hqNotify == NULL ) {
        PMLOGMSG(ZONE_WARN, (_T("%s: CreateEvent() failed for system event\r\n"), pszFname));
        return FALSE;
    }
    if (!m_hSystemApiCalled) {
        PMLOGMSG(ZONE_WARN, (_T("%s:  PMSystemAPI::Init() failed\r\n"), pszFname));
        return FALSE;
    }
    // check that all of our activity events exist
    if(m_pUserActivity && (m_pUserActivity->hevActive == NULL || m_pUserActivity->hevInactive == NULL)) {
        PMLOGMSG(ZONE_WARN, (_T("%s: UserActivity timer events not found\r\n"), pszFname));
        return FALSE;
    }
    if(m_pSystemActivity && ( m_pSystemActivity->hevActive == NULL || m_pSystemActivity->hevInactive == NULL)) {
        // SystemActivity is optional. If it present. Both hevActive && hevInactive has to valid.
        PMLOGMSG(ZONE_WARN, (_T("%s: SystemActivity timer events not found\r\n"), pszFname));
        return FALSE;
    }
    if(m_hqNotify == NULL) {
        PMLOGMSG(ZONE_WARN, (_T("%s: PmPolicyCreateNotificationQueue() failed\r\n"), pszFname));
        return FALSE;
    }
    return TRUE;
        
    
};
HANDLE  DefaultPowerStateManager::GetEventHandle(DWORD dwIndex)
{
    switch (dwIndex) {
        case PM_SHUTDOWN_EVENT:
            return ghevPmShutdown;

        case PM_RELOAD_ACTIVITY_TIMEOUTS_EVENT:
            return m_hevReloadActivityTimeouts;

        case PM_MSGQUEUE_EVENT:
            return m_hqNotify;

        case PM_RESTART_TIMER_EVENT:
            return m_hevRestartTimers;

        case PM_SYSTEM_API_EVENT:
            return GetAPISignalHandle();

        case PM_BOOTPHASE2_EVENT:
            return m_hevBootPhase2;
    }
    return NULL;

}

void    DefaultPowerStateManager::PlatformResumeSystem(BOOL fSuspened)
{
    SETFNAME(_T("DefaultPowerStateManager::PlatformResumeSystem"));
    if (fSuspened) {
        // Unexpected resume -- go to the resuming state.  This should not happen unless
        // somebody is illegally calling PowerOffSystem() directly.
        PMLOGMSG(ZONE_WARN || ZONE_RESUME, (_T("%s: WARNING: unexpected resume!\r\n"), pszFname));
        
        // Go into the new state.  OEMs that choose to support unexpected resumes may want to
        // lock PM variables with PMLOCK(), then set the curDx and actualDx values for all
        // devices to PwrDeviceUnspecified before calling PmSetSystemPowerState_I().  This will
        // force an update IOCTL to all devices.
        DEBUGCHK(m_hevRestartTimers != NULL);
        SetEvent(m_hevRestartTimers);
    }
    PMLOGMSG(ZONE_RESUME, (_T("-%s\r\n"), pszFname));
}
DWORD CountBit(DWORD dwBit)
{
    DWORD dwReturn = 0;
    while (dwBit) {
        dwBit = ((dwBit - 1 ) & dwBit);
        dwReturn ++ ;
    }
    return dwReturn;
}
DWORD   DefaultPowerStateManager::PlatformMapPowerStateHint(DWORD dwHint, LPTSTR pszBuf, DWORD dwBufChars)
{
    DWORD dwStatus = ERROR_FILE_NOT_FOUND;
    LPTSTR pszMappedStateName = NULL;
    SETFNAME(_T("DefaultPowerStateManager::PlatformMapPowerStateHint"));
    if (dwHint && pszBuf && dwBufChars) {
        PowerState * bestState = NULL;
        DWORD bestMatchBit = 0;
        PowerState * curState = m_pPowerStateList ;
        while (curState) {
            PSYSTEM_POWER_STATE psps=NULL;
            if (curState->AppsCanRequestState() 
                    && RegReadSystemPowerState( curState->GetStateString(), &psps, NULL) == ERROR_SUCCESS
                    && psps!=NULL) { // Here we do not use state intial flags because someone can change it on live.
                DWORD dwFlagsBit = CountBit(psps->dwFlags & dwHint );
                if (dwFlagsBit > bestMatchBit ) {
                    bestMatchBit = dwFlagsBit;
                    bestState = curState;
                }
            }
            if (psps)
                SystemPowerStateDestroy(psps);
            curState = curState->GetNextPowerState();
        }

        // if we have one, copy the name back to the caller
        if(bestState != NULL) {
            if(dwBufChars < (_tcslen(bestState->GetStateString()) + 1)) {
                dwStatus = ERROR_INSUFFICIENT_BUFFER;
            } else {
                _tcscpy(pszBuf, bestState->GetStateString());
                dwStatus = ERROR_SUCCESS;
                PMLOGMSG(ZONE_PLATFORM,(_T("%s: returning state \"%s\"\r\n"), pszFname, pszBuf));
            }
        }
    }
    DEBUGCHK(dwStatus == ERROR_SUCCESS);
    PMLOGMSG(dwStatus != ERROR_SUCCESS && ZONE_WARN,(_T("%s: returning %d\r\n"), pszFname, dwStatus));
    return dwStatus;
}
PowerState * DefaultPowerStateManager::GetFirstPowerState()
{
    SETFNAME(_T("PowerStateManager::GetFirstPowerState"));
    PowerState * curState = NULL;
    HANDLE hEvents[2];
    hEvents[0] = ghevPmShutdown;
    hEvents[1] = GetAPISignalHandle();
    DWORD dwStatus = WaitForMultipleObjects(2, hEvents, FALSE, INFINITE);
    switch(dwStatus) {
    case (WAIT_OBJECT_0 + 0):
        PMLOGMSG(ZONE_INIT || ZONE_WARN, (_T("%s: shutdown event signaled, exiting\r\n"), pszFname));
        break;
    case (WAIT_OBJECT_0 + 1): {
        curState = m_pCurPowerState ;
        PMLOGMSG(ZONE_INIT, (_T("%s: initialization complete\r\n"), pszFname));
    }
        break;
    default:
        PMLOGMSG(ZONE_INIT || ZONE_WARN, (_T("%s: WaitForMultipleObjects() returned %d, exiting\r\n"),
            pszFname, dwStatus));
        break;
    }
    return curState;
}
PowerState * DefaultPowerStateManager::SetSystemState(PowerState * pCurPowerState )
{
    SETFNAME(_T("PowerStateManager::SetSystemState"));
    if (pCurPowerState!=NULL) {
        DWORD curState = pCurPowerState->GetState();
        DWORD newState = curState;
        do { //Switch to new stable state.
            newState = pCurPowerState->GetLastNewState();
            if (newState!= curState) {
                PowerState * pNewPowerState = GetStateObject(newState);
                if (pNewPowerState == NULL ) { // Wrong state all unspported state.
                    PMLOGMSG(ZONE_WARN || ZONE_PLATFORM,(_T("Unsported state %d \r\n"),newState));
                    ASSERT(FALSE);
                    newState = curState;
                }
                else if ( pNewPowerState!=pCurPowerState) {
                    PMLOGMSG(ZONE_INIT || ZONE_PLATFORM, (_T("%s: state change from \"%s\" to \"%s\" \r\n"  ), 
                        pszFname,pCurPowerState->GetStateString(),pNewPowerState->GetStateString())) ;
                    pCurPowerState = pNewPowerState;
                    pCurPowerState ->EnterState();
                    // Update to new state.
                    curState = newState;
                    newState = pCurPowerState->GetLastNewState();
                }
                else
                    newState = curState;
            }
        } while (newState!= curState) ; // Change to stable state.
        ASSERT(pCurPowerState!=NULL);
    }
    return pCurPowerState ;
}
DWORD   DefaultPowerStateManager::SendSystemPowerState(LPCWSTR pwsState, DWORD dwStateHint, DWORD dwOptions )
{
    TCHAR   szStateName[MAX_PATH];
    DWORD   dwStatus = ERROR_SUCCESS;
    szStateName[0] = 0;
    SETFNAME(_T("PowerStateManager::SendSystemPowerState"));
    PMLOGMSG(ZONE_API, (_T("+%s: name %s, hint 0x%08x, options 0x%08x, fInternal %d\r\n"),
            pszFname, pwsState != NULL ? pwsState : _T("<NULL>"), dwStateHint, dwOptions));
    if (pwsState) {
        __try {
            StringCchCopy(szStateName, MAX_PATH, pwsState) ;
        }
        __except(EXCEPTION_EXECUTE_HANDLER) {
            PMLOGMSG(ZONE_WARN, (_T("%s: exception copying state name\r\n"), 
                pszFname));
            szStateName[0] = 0 ;
            dwStatus = ERROR_INVALID_PARAMETER;
        }
    } 
    else {
        // try to match the hint flag to a system state
        dwStatus = PlatformMapPowerStateHint(dwStateHint, szStateName, dim(szStateName));
    }
    
    PMLOGMSG(ZONE_API, (_T("+%s: name \"%s\", hint 0x%08x, options 0x%08x, fInternal %d\r\n"),
            pszFname,szStateName, dwStateHint, dwOptions));

    // go ahead and do the update?
    if(dwStatus == ERROR_SUCCESS) {
        Lock();
        DWORD activeState = SystemStateToActivityState(szStateName );
        if (activeState == PM_UNKNOWN_POWER_STATE ) {
            dwStatus = ERROR_INVALID_PARAMETER;
        }
        else {
            PowerState * pNewPowerState = GetStateObject(activeState);
            if (pNewPowerState && pNewPowerState->AppsCanRequestState()) {
                if ((dwOptions & POWER_DUMPDW)!=0) {
                    CaptureDumpFileOnDevice (GetCurrentProcessId (), GetCurrentThreadId (), NULL);
                }
                pNewPowerState->EnterState();
                pNewPowerState = SetSystemState(pNewPowerState ) ;
                m_pCurPowerState = pNewPowerState;
                SetEvent(m_hSystemApiCalled);
            }
            else 
                dwStatus = ERROR_INVALID_PARAMETER; 
        }
        Unlock();
    }
    return dwStatus;

}
DWORD   DefaultPowerStateManager::SystemStateToActivityState(LPCTSTR lpState )
{
    if (lpState == NULL)
        return PM_UNKNOWN_POWER_STATE;
    PowerState * curState = m_pPowerStateList ;
    while (curState) {
        if (_tcsicmp(curState->GetStateString(),lpState)==0)
            return curState->GetState();
        else
            curState = curState->GetNextPowerState();
    }
    return PM_UNKNOWN_POWER_STATE;

}
LPCTSTR DefaultPowerStateManager::ActivityStateToSystemState (DWORD platActiveState)
{
    PowerState * curState = m_pPowerStateList ;
    while (curState) {
        if (curState->GetState() == platActiveState )
            return curState->GetStateString() ;
        else
            curState = curState->GetNextPowerState();
    }
    return NULL;

}
PowerState * DefaultPowerStateManager::GetStateObject(DWORD newState)
{
    if (m_pPowerStateList != NULL) {
        PowerState * pCurState = m_pPowerStateList;
        while (pCurState) {
            if (pCurState-> GetState() ==  newState ) {
                return pCurState ;
            }
            pCurState = pCurState->GetNextPowerState();
        }
    }
    return NULL;
}

⌨️ 快捷键说明

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