📄 battif.c
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//
// This module contains a stub implementation of the battery PDD. OEMs
// that want to support the battery APIs on their platform can copy this
// file to their platform and link it into their own version of the power
// manager DLL.
//
// If the platform-specific power manager provides its own version of these
// entry points, this module will not be pulled into the link map from
// the pm_battapi library.
//
//--------------------------------------------------------------------------------------------------
// Includes
//--------------------------------------------------------------------------------------------------
#include <battimpl.h>
#include <ceddk.h>
#include <bsp.h>
typedef struct {
unsigned __int8 IsOnBattery; // 0 for on AC, nonzero for on battery
unsigned __int8 ChargePercent; // 0 = fully charged, 100 = fully discharged. Values greater than 100 are illegal
unsigned __int16 Temperature;
} DEVICEEMULATOR_BATTERY_REGS;
//-------------------------------------------------------------------------------------------------
// Global Variables
//--------------------------------------------------------------------------------------------------
volatile DEVICEEMULATOR_BATTERY_REGS *g_pBatteryRegs;
unsigned __int8 ChargePercent;
//------------------------------------------------------------------------------------------------------------
// Function: InitBatteryDriver
//
// Purpose: Initialises the battery driver and allocates the necessary memory.
// Returns: void
//
//-------------------------------------------------------------------------------------------------------------
void InitBatteryDriver()
{
if (g_pBatteryRegs == NULL)
{
PHYSICAL_ADDRESS ioPhysicalBase = { BSP_BASE_REG_PA_BATTERY, 0};
ULONG inIoSpace = 0;
if (HalTranslateBusAddress(Internal,0, ioPhysicalBase,&inIoSpace,&ioPhysicalBase))
{
// Map it if it is Memeory Mapped IO.
g_pBatteryRegs = (volatile DEVICEEMULATOR_BATTERY_REGS *)MmMapIoSpace(ioPhysicalBase, sizeof(DEVICEEMULATOR_BATTERY_REGS),FALSE);
}
}
}
//------------------------------------------------------------------------------------------------------------
// Function: IsACOnline
//
// Purpose: Simply indicates whether we are running out of AC or out of Battery.
// Returns: TRUE indicates we are running out of the AC wall power supply.
// FALSE indicates we are running out of Battery.
//
//-------------------------------------------------------------------------------------------------------------
BOOL WINAPI IsACOnline(void)
{
if (g_pBatteryRegs->IsOnBattery)
{
return FALSE;
}
return TRUE;
}
BOOL WINAPI
BatteryPDDInitialize(LPCTSTR pszRegistryContext)
{
BOOL fOk = TRUE;
SETFNAME(_T("BatteryPDDInitialize"));
UNREFERENCED_PARAMETER(pszRegistryContext);
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fOk));
return fOk;
}
void WINAPI
BatteryPDDDeinitialize(void)
{
SETFNAME(_T("BatteryPDDDeinitialize"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked\r\n"), pszFname));
if (g_pBatteryRegs)
{
MmUnmapIoSpace((PVOID)g_pBatteryRegs, sizeof(DEVICEEMULATOR_BATTERY_REGS));
}
}
void WINAPI
BatteryPDDResume(void)
{
SETFNAME(_T("BatteryPDDResume"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked\r\n"), pszFname));
}
void WINAPI
BatteryPDDPowerHandler(BOOL bOff)
{
SETFNAME(_T("BatteryPDDPowerHandler"));
UNREFERENCED_PARAMETER(bOff);
DEBUGMSG(ZONE_PDD | ZONE_RESUME, (_T("%s: invoked w/ bOff %d\r\n"), pszFname, bOff));
}
// This routine obtains the most current battery/power status available
// on the platform. It fills in the structures pointed to by its parameters
// and returns TRUE if successful. If there's an error, it returns FALSE.
BOOL WINAPI
BatteryPDDGetStatus(
PSYSTEM_POWER_STATUS_EX2 pstatus,
PBOOL pfBatteriesChangedSinceLastCall
)
{
// this function is used to report the battery status
// now, temperature, and terminal voltage
unsigned __int8 sample;
DWORD temperature ;
BOOL PowerStatus;
static BOOL first_time = TRUE;
BYTE BatteryFlag = 0;
if(first_time)
{
InitBatteryDriver();
ChargePercent = 100;
first_time=FALSE;
}
PowerStatus = IsACOnline();
sample = g_pBatteryRegs->ChargePercent;
if (sample > 100) {
sample = 100;
}
sample = 100-sample; // convert from the emulators "0=fully charged, 100=fully discharged" to "100=fully charged, 0=fully discharged"
if (PowerStatus)
{
pstatus->ACLineStatus = AC_LINE_ONLINE;
ChargePercent = sample;
if (sample > ChargePercent) // AC ON, Battery OFF
{
BatteryFlag |= BATTERY_FLAG_CHARGING;
}
}
else
{
pstatus->ACLineStatus = AC_LINE_OFFLINE;
ChargePercent = sample;
}
temperature = g_pBatteryRegs->Temperature;
// Level Indicator
if(ChargePercent >= 65)
BatteryFlag |= BATTERY_FLAG_HIGH;
else if ((ChargePercent < 65) && (ChargePercent >= 20))
BatteryFlag |= BATTERY_FLAG_LOW;
else
BatteryFlag |= BATTERY_FLAG_CRITICAL;
pstatus->BatteryFlag = BatteryFlag;
pstatus->BatteryLifePercent = ChargePercent;
pstatus->Reserved1 = 0;
pstatus->BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
pstatus->BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
pstatus->Reserved2 = 0;
pstatus->BackupBatteryFlag = BATTERY_FLAG_UNKNOWN;
pstatus->BackupBatteryLifePercent = 0;
pstatus->Reserved3 = 0;
pstatus->BackupBatteryLifeTime = BATTERY_LIFE_UNKNOWN;
pstatus->BackupBatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
pstatus->BatteryChemistry = BATTERY_CHEMISTRY_LION;
pstatus->BatteryVoltage = (((unsigned long)ChargePercent * 41)/1000);
pstatus->BatteryCurrent = 0;
pstatus->BatteryAverageCurrent = 0;
pstatus->BatteryAverageInterval = 0;
pstatus->BatterymAHourConsumed = 0;
pstatus->BatteryTemperature = temperature;
pstatus->BackupBatteryVoltage = 0;
*pfBatteriesChangedSinceLastCall = FALSE;
return (TRUE);
}
// This routine indicates how many battery levels will be reported
// in the BatteryFlag and BackupBatteryFlag fields of the PSYSTEM_POWER_STATUS_EX2
// filed in by BatteryPDDGetStatus(). This number ranges from 0 through 3 --
// see the Platform Builder documentation for details. The main battery
// level count is reported in the low word of the return value; the count
// for the backup battery is in the high word.
LONG
BatteryPDDGetLevels(
void
)
{
LONG lLevels = MAKELONG (3 /* main battery levels */,
3 /* backup battery levels */);
SETFNAME(_T("BatteryPDDPowerHandler"));
DEBUGMSG(ZONE_PDD, (_T("%s: returning %u (%d main levels, %d backup levels)\r\n"),
pszFname, LOWORD(lLevels), HIWORD(lLevels)));
return lLevels;
}
// This routine returns TRUE to indicate that the pfBatteriesChangedSinceLastCall
// value filled in by BatteryPDDGetStatus() is valid. If there is no way to
// tell that the platform's batteries have been changed this routine should
// return FALSE.
BOOL
BatteryPDDSupportsChangeNotification(
void
)
{
BOOL fSupportsChange = FALSE;
SETFNAME(_T("BatteryPDDPowerHandler"));
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fSupportsChange));
return fSupportsChange;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -