📄 sbattif.c
字号:
//
// 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 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.
//
// To simplify testing the behavior of battery-sensitive applications on
// platforms (like CEPC or EMULATOR) that don't include physical batteries,
// this driver uses a memory-mapped file to store power information. This
// file is shared with the SETSTUBBAT test program in
// public\common\oak\drivers\battdrvr\test\setstubbat, which can update
// arbitrary battery values. Note that memory-mapped files require that
// the OS image include the "nkmapfile" component.
//
//#define CHN
#define PM_As3603
#ifdef PM_As3603
#include <windows.h>
#include <pm.h>
#include <ddkreg.h>
#include <ceddk.h>
#include <keybdpdd.h>
#include <laymgr.h>
#include <battimpl.h>
#include <devload.h>
#include <stubbattery.h>
#include <Nkintr.h>
#include ".\inc\power.h"
#include "hw_lib.h"
#include "platform.h"
#include "gpiodrv.h"
#include "oalintr.h"
#include ".\inc\charge.h"
#include "userconfig.h"
#ifdef CHN
#define POWER_OFF_USB TEXT("请拔出USB插头!")
#define POWER_OFF_TITLE TEXT("关机")
#define POWER_WARN TEXT("您要关机吗?")
#define LOW_POWER_WARN TEXT("电池电量低,请及时充电,目前请不要运行浏览器和插入U盘!")
#define LOW_POWER_TITLE TEXT("低电")
#else
#define POWER_OFF_USB TEXT("Please pull out the usb plug!")
#define POWER_OFF_TITLE TEXT("POWER OFF")
#define POWER_WARN TEXT("Are you sure to power off?")
#define LOW_POWER_WARN TEXT("Battery power is very low,please charge in time,and not using IE and u_disk!")
#define LOW_POWER_TITLE TEXT("Low Power")
#endif
//#define DEBUG_DISCHARGE
#define SHOW_DEBUG 0
#define SCREEN_BYTES 480*272*2
#endif
#ifdef PM_As3603
//2006-12-16 qzsu
#define FULLQ 2200000 //uAh
#define TOO_LOWQ 44000 //uAh
DWORD g_currentQ;
void LoadQAcc(void);
void SaveQAcc(void);
void GetBatteryStatus(PBATTERY_STATUS pStatus);
#define QUEUE_ENTRIES 3
#define MAX_NAMELEN 200
#define QUEUE_SIZE (QUEUE_ENTRIES * (sizeof(POWER_BROADCAST) + MAX_NAMELEN))
// global variables
unsigned char gInterrupt = 1;
int WINAPI ChargeThreadProc(LPVOID pvParam);
//powerkey
HANDLE ghPowerKeyThread = NULL;
HANDLE gPowerKeyEvent = NULL;
unsigned char gPowerKeyFlag = 0;
int WINAPI PowerKeyThreadProc(LPVOID pvParam);
unsigned char gClockFlag = 0;
int WINAPI ClockThreadProc(LPVOID pvParam);
//背光线程
HANDLE ghMonBkLightThread = NULL;
int WINAPI MonBkLightThreadProc(LPVOID pvParam);
//低电倒计时线程
HANDLE ghLowQAccThread = NULL;
int WINAPI LowQAccThreadProc(LPVOID pvParam);
//等待POWEROFF事件
HANDLE ghPowerOffThread = NULL;
int WINAPI PowerOffThreadProc(LPVOID pvParam);
void ShowPicture(void);
#endif
// typedefs for APIs that require the "nkmapfile" component
typedef WINBASEAPI HANDLE (WINAPI *PFN_CreateFileMappingW) (
HANDLE hFile,
LPSECURITY_ATTRIBUTES lpFileMappingAttributes,
DWORD flProtect,
DWORD dwMaximumSizeHigh,
DWORD dwMaximumSizeLow,
LPCWSTR lpName
);
typedef LPVOID (WINAPI *PFN_MapViewOfFile) (
HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
DWORD dwNumberOfBytesToMap
);
typedef BOOL (WINAPI *PFN_UnmapViewOfFile) (
LPCVOID lpBaseAddress
);
typedef BOOL (WINAPI * PFN_GwesPowerDown)(void);
#define MUTEX_TIMEOUT 5000
static PBATTERY_STATUS gpStatus;
static HANDLE ghMutex;
static HANDLE ghFileMap;
static PFN_CreateFileMappingW gpfnCreateFileMappingW;
static PFN_MapViewOfFile gpfnMapViewOfFile;
static PFN_UnmapViewOfFile gpfnUnmapViewOfFile;
// this routine takes the battery mutex
DWORD
LockBattery(void)
{
DWORD dwStatus;
SETFNAME(_T("LockBattery"));
DEBUGCHK(ghMutex != NULL);
dwStatus = WaitForSingleObject(ghMutex, MUTEX_TIMEOUT);
if(dwStatus == WAIT_OBJECT_0) {
dwStatus = ERROR_SUCCESS;
} else {
dwStatus = GetLastError();
DEBUGCHK(dwStatus != ERROR_SUCCESS);
}
DEBUGMSG(dwStatus != ERROR_SUCCESS && ZONE_WARN,
(_T("%s: WaitForSingleObject() failed %d\r\n"), pszFname,
GetLastError()));
DEBUGCHK(dwStatus == ERROR_SUCCESS);
return dwStatus;
}
// this routine releases the battery mutex
DWORD
UnlockBattery(void)
{
DWORD dwStatus = ERROR_SUCCESS;
BOOL fOk;
SETFNAME(_T("UnlockBattery"));
DEBUGCHK(ghMutex != NULL);
fOk = ReleaseMutex(ghMutex);
if(!fOk) {
dwStatus = GetLastError();
DEBUGCHK(dwStatus != ERROR_SUCCESS);
}
DEBUGMSG(dwStatus != ERROR_SUCCESS && ZONE_WARN,
(_T("%s: ReleaseMutex() failed %d\r\n"), pszFname, GetLastError()));
DEBUGCHK(dwStatus == ERROR_SUCCESS);
return dwStatus;
}
BOOL WINAPI
BatteryPDDInitialize(LPCTSTR pszRegistryContext)
{
BOOL fOk = TRUE;
HKEY hk;
SYSTEM_POWER_STATUS_EX2 sps;
WORD wMainLevels = 3, wBackupLevels = 3;
BOOL fSupportsChange = FALSE;
SETFNAME(_T("BatteryPDDInitialize"));
DEBUGCHK(ghMutex == NULL);
DEBUGCHK(ghFileMap == NULL);
DEBUGCHK(gpStatus == NULL);
DEBUGCHK(pszRegistryContext != NULL);
RETAILMSG(1, (_T("BatteryPDDInitialize\r\n")));
#ifdef PM_As3603
PM_InitI2C();
//充电初始化
PM_ChargeInitial();
if(!InitCharge())
{
RETAILMSG(SHOW_DEBUG, (_T("InitCharge error\r\n")));
BatteryPDDDeinitialize();
return FALSE;
}
//加载NORFLASH中保存的电量
FLA_Init();
LoadQAcc();
//WIFI上电
// PM_WiFi_1v8(1.8);
// PM_WiFi_3v3(3.3);
//线程
//powerkey
gPowerKeyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if(gPowerKeyEvent)
{
if(!(InterruptInitialize(SYSINTR_EDBG, gPowerKeyEvent, NULL, 0)))
{
RETAILMSG(SHOW_DEBUG, (TEXT("g_PowerKeyEvent: Error cann't initialize interrupt PowerKey!\r\n")));
}
else
{
gPowerKeyFlag = 1;
ghPowerKeyThread = CreateThread(NULL, 0, PowerKeyThreadProc, NULL, 0, NULL);
}
}
//end of powerkey
//poweroff菜单线程
ghPowerOffThread = CreateThread(NULL, 0, PowerOffThreadProc, NULL, 0, NULL);
// 创建监测背光事件的线程
ghMonBkLightThread = CreateThread(NULL, 0, MonBkLightThreadProc, NULL, 0, NULL);
if(!ghMonBkLightThread)
{
RETAILMSG(SHOW_DEBUG, (_T("ghMonBkLightThread error\r\n")));
}
#endif
// intialize the battery status structure -- assume AC power, no battery info
sps.ACLineStatus = AC_LINE_ONLINE;
sps.BatteryFlag = BATTERY_FLAG_HIGH;
sps.BatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
sps.Reserved1 = 0;
sps.BatteryLifeTime = BATTERY_LIFE_UNKNOWN;
sps.BatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
sps.Reserved2 = 0;
sps.BackupBatteryFlag = BATTERY_FLAG_UNKNOWN;//BATTERY_FLAG_HIGH;
sps.BackupBatteryLifePercent = BATTERY_PERCENTAGE_UNKNOWN;
sps.Reserved3 = 0;
sps.BackupBatteryLifeTime = BATTERY_LIFE_UNKNOWN;
sps.BackupBatteryFullLifeTime = BATTERY_LIFE_UNKNOWN;
sps.BatteryChemistry = BATTERY_CHEMISTRY_LION;//BATTERY_CHEMISTRY_UNKNOWN;
sps.BatteryVoltage = 0;
sps.BatteryCurrent = 0;
sps.BatteryAverageCurrent = 0;
sps.BatteryAverageInterval = 0;
sps.BatterymAHourConsumed = 0;
sps.BatteryTemperature = 0;
sps.BackupBatteryVoltage = 0;
// get registry values, if present
hk = OpenDeviceKey(pszRegistryContext);
if(hk != NULL) {
DWORD dwSize, dwStatus, dwType, dwValue;
SYSTEM_POWER_STATUS_EX2 spstemp;
// get the number of main levels
dwSize = sizeof(dwValue);
dwStatus = RegQueryValueEx(hk, _T("MainLevels"), NULL, &dwType, (LPBYTE) &dwValue, &dwSize);
if(dwStatus == ERROR_SUCCESS && dwType == REG_DWORD) {
wMainLevels = (WORD) dwValue;
}
// 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);
}
// 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?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -