📄 battif.c
字号:
}
// get the number of backup levels
dwSize = sizeof(dwValue);
dwStatus = RegQueryValueEx(hk, _T("BackupLevels"), NULL, &dwType, (LPBYTE) &dwValue, &dwSize);
if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) {
wBackupLevels = (WORD) dwValue;
}
// does the battery support change?
dwSize = sizeof(dwValue);
dwStatus = RegQueryValueEx(hk, _T("SupportsChange"), NULL, &dwType, (LPBYTE) &dwValue, &dwSize);
if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) {
fSupportsChange = dwValue != 0 ? TRUE : FALSE;
}
// do we have an initial battery status?
dwSize = sizeof(spstemp);
dwStatus = RegQueryValueEx(hk, _T("InitialStatus"), NULL, &dwType, (LPBYTE) &spstemp, &dwSize);
if(dwStatus == ERROR_SUCCESS && dwType == REG_BINARY && dwSize == sizeof(spstemp)) {
memcpy(&sps, &spstemp, dwSize);
}
// close the key
RegCloseKey(hk);
}
if((gADCMutex = CreateMutex(NULL, FALSE, ADC_MUTEX_NAME)) == NULL) {
fOk = FALSE;
}
// Initialize the AC97 link.
//
if (!InitializeACLink(FALSE, DEV_BATTERY))
{
fOk = FALSE;
}
else
{
g_pfnWriteAc97=WriteAC97;
g_pfnReadAc97=ReadAC97;
g_pfnWriteAc97Mask(RT_PWR_MANAG_ADD1,PWR_MAIN_BIAS,PWR_MAIN_BIAS);//power on main BIAS
g_pfnWriteAc97Mask(RT_PWR_MANAG_ADD2,PWR_MIXER_VREF |PWR_TP_ADC ,PWR_MIXER_VREF |PWR_TP_ADC );//enable touch panel and ADC and VREF of all analog
#if BATTERY_ADC_USE_AUX3
g_pfnWriteAc97Mask(RT_MIC_ROUTING_CTRL,0,MIC1_DIFF_INPUT_CTRL); //Disable MIC1 Differential Input Control to enable AUX3
#endif
#if BATTERY_ADC_USE_AUX4
g_pfnWriteAc97Mask(RT_PWR_MANAG_ADD1,0,PWR_MIC_BIAS2); //Power down mic2 bias to enable AUX4
#endif
}
// allocate resources
if((ghMutex = CreateMutex(NULL, FALSE, BATTERY_FILE_MUTEX)) == NULL) {
DEBUGMSG(ZONE_ERROR || ZONE_PDD || ZONE_INIT,
(_T("%s: Could not aquire battery info file mutex handle\n"), pszFname));
fOk = FALSE;
} else {
HINSTANCE hiCoreDll = NULL;
BOOL fNewMapping = TRUE;
// get pointers to file-mapping functions
hiCoreDll = LoadLibrary(_T("coredll.dll"));
if(hiCoreDll != NULL) {
gpfnCreateFileMappingW = (PFN_CreateFileMappingW) GetProcAddress((HMODULE) hiCoreDll, _T("CreateFileMappingW"));
gpfnMapViewOfFile = (PFN_MapViewOfFile) GetProcAddress((HMODULE) hiCoreDll, _T("MapViewOfFile"));
gpfnUnmapViewOfFile = (PFN_UnmapViewOfFile) GetProcAddress((HMODULE) hiCoreDll, _T("UnmapViewOfFile"));
}
FreeLibrary(hiCoreDll); // we're already linked to coredll
// serialize access to the mapping file
LockBattery();
// create the mapping
if(gpfnCreateFileMappingW == NULL ) {
// no file mapping, use a global variable
static BATTERY_STATUS sBatteryStatus;
gpStatus = &sBatteryStatus;
} else if((ghFileMap = gpfnCreateFileMappingW((HANDLE)INVALID_HANDLE_VALUE, NULL,
PAGE_READWRITE, 0, sizeof(BATTERY_STATUS), BATTERY_STATUS_FILE)) == NULL) {
DEBUGMSG(ZONE_ERROR || ZONE_PDD || ZONE_INIT,
(_T("%s: Could not create file mapping for battery info file\n"), pszFname));
fOk = FALSE;
} else {
// is this a new mapping?
if(GetLastError() == ERROR_ALREADY_EXISTS) {
fNewMapping = FALSE;
}
// map the object into our address space
if(gpfnMapViewOfFile == NULL
|| (gpStatus = (PBATTERY_STATUS) gpfnMapViewOfFile(ghFileMap, FILE_MAP_ALL_ACCESS,
0, 0, sizeof(BATTERY_STATUS))) == NULL) {
DEBUGMSG(ZONE_ERROR || ZONE_PDD || ZONE_INIT,
(_T("Could not map view of battery info file into process address space\n"), pszFname));
fOk = FALSE;
}
}
// should we initialize our structure?
if(fOk && fNewMapping) {
// initialize the memory mapped object
memcpy(&gpStatus->sps, &sps, sizeof(gpStatus->sps));
gpStatus->fSupportsChange = fSupportsChange;
gpStatus->fChanged = FALSE;
gpStatus->wMainLevels = wMainLevels;
gpStatus->wBackupLevels = wBackupLevels;
}
// allow access to the battery buffer
UnlockBattery();
}
// clean up if necessary
if(!fOk) {
if(gpStatus != NULL && gpfnUnmapViewOfFile != NULL) gpfnUnmapViewOfFile(gpStatus);
if(ghFileMap != NULL) CloseHandle(ghFileMap);
if(ghMutex != NULL) CloseHandle(ghMutex);
if(gADCMutex != NULL) CloseHandle(gADCMutex);
gpStatus = NULL;
ghFileMap = NULL;
ghMutex = NULL;
}
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(gpStatus != NULL && gpfnUnmapViewOfFile != NULL) gpfnUnmapViewOfFile(gpStatus);
if(ghFileMap != NULL) CloseHandle(ghFileMap);
if(ghMutex != NULL) CloseHandle(ghMutex);
gpStatus = NULL;
ghFileMap = NULL;
ghMutex = NULL;
}
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
)
{
BOOL fOk = TRUE;
#if BATTERY_ADC_USE_AUX3
unsigned short int BatteryADCAux3Value;
#endif
#if BATTERY_ADC_USE_AUX4
unsigned short int BatteryADCAux4Value;
#endif
SETFNAME(_T("BatteryPDDGetStatus"));
DEBUGMSG(ZONE_PDD, (_T("%s: invoked w/ pstatus 0x%08x, pfChange 0x%08x\r\n"),
pszFname, pstatus, pfBatteriesChangedSinceLastCall));
PREFAST_DEBUGCHK(gpStatus != NULL);
PREFAST_DEBUGCHK(pstatus != NULL);
PREFAST_DEBUGCHK(pfBatteriesChangedSinceLastCall != NULL);
//Read ADC value from AUX3
#if BATTERY_ADC_USE_AUX3
g_pfnWriteAc97Mask(RT_MIC_ROUTING_CTRL,0,MIC1_DIFF_INPUT_CTRL); //Disable MIC1 Differential Input Control to enable AUX3
ADC_Lock();
Get_Battery_ADCValue(CB2_AUX_SEL_AUX3,&BatteryADCAux3Value);
ADC_Unlock();
#endif
//Read ADC value from AUX4
#if BATTERY_ADC_USE_AUX4
g_pfnWriteAc97Mask(RT_PWR_MANAG_ADD1,0,PWR_MIC_BIAS2); //Power down mic2 bias to enable AUX4
ADC_Lock();
Get_Battery_ADCValue(CB2_AUX_SEL_AUX4,&BatteryADCAux4Value);
ADC_Unlock();
#endif
// get battery status information from the shared structure
LockBattery();
memcpy(pstatus, &gpStatus->sps, sizeof(*pstatus));
*pfBatteriesChangedSinceLastCall = gpStatus->fChanged;
if(*pfBatteriesChangedSinceLastCall) {
gpStatus->fChanged = FALSE; // reset changed flag if it's set
}
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fOk));
return (fOk);
}
// 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;
SETFNAME(_T("BatteryPDDPowerHandler"));
PREFAST_DEBUGCHK(gpStatus != NULL);
LockBattery();
lLevels = MAKELONG (gpStatus->wMainLevels, gpStatus->wBackupLevels);
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %u (%d main levels, %d backup levels)\r\n"),
pszFname, lLevels, 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;
SETFNAME(_T("BatteryPDDPowerHandler"));
PREFAST_DEBUGCHK(gpStatus != NULL);
LockBattery();
fSupportsChange = gpStatus->fSupportsChange;
UnlockBattery();
DEBUGMSG(ZONE_PDD, (_T("%s: returning %d\r\n"), pszFname, fSupportsChange));
return fSupportsChange;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -