#include #include #include "debug.h" #include "stringutils.h" // devlist for sp3 // devlist for sp3i, is lacking: COM1, COM5, DSK1 // // todo: use EnumDevices instead ( this returns a buffer with NUL separeted strings ) TCHAR *devices[]= { // devs found on all devices _T("ACM1:"), _T("ACM3:"), _T("ACM9:"), _T("BAT1:"), _T("BKL1:"), _T("BTD0:"), _T("COM2:"), _T("COM8:"), _T("DSK1:"), _T("IP60:"), _T("IPD0:"), _T("NDS0:"), _T("RIL1:"), _T("SIM1:"), _T("SMS1:"), _T("WAM1:"), _T("WAV1:"), // specific sp devs _T("WAP1:"), _T("PER1:"), _T("RDO1:"), _T("LOG1:"), _T("LOG2:"), _T("COM5:"), _T("COM1:"), // specific xda2 devs _T("AGW1:"), _T("BAT2:"), _T("BSP2:"), _T("DSK2:"), _T("SIP0:"), _T("SPI1:"), _T("UIO1:"), _T("UFN1:"), }; #define NR_DEVS (sizeof(devices)/sizeof(TCHAR*)) // only BKL1 has an actual status. BOOL ReadRegistryString(HKEY hRoot, LPTSTR devicekey, LPTSTR valname, TCHAR *strval, int maxlen) { HKEY hDevkey; DWORD rc= RegOpenKeyEx(hRoot, devicekey, 0, 0, &hDevkey); if (rc) { debug("RegOpenKeyEx(%ls): ERROR %08lx\n", devicekey, rc); return FALSE; } DWORD valtype=0; DWORD maxsize= sizeof(TCHAR)*(maxlen-1); rc= RegQueryValueEx(hDevkey, valname, NULL, &valtype, (LPBYTE)strval, &maxsize); if (rc) { //debug("RegQueryValueEx('%ls', '%ls', string(%d)): ERROR %08lx\n", devicekey, valname, maxlen, rc); RegCloseKey(hDevkey); return FALSE; } strval[maxlen-1]=0; RegCloseKey(hDevkey); return TRUE; } BOOL ReadRegistryDword(HKEY hRoot, LPTSTR devicekey, LPTSTR valname, DWORD *pvalue) { HKEY hDevkey; DWORD rc= RegOpenKeyEx(hRoot, devicekey, 0, 0, &hDevkey); if (rc) { debug("RegOpenKeyEx(%ls): ERROR %08lx\n", devicekey, rc); return FALSE; } DWORD valtype=0; DWORD maxsize= sizeof(DWORD); rc= RegQueryValueEx(hDevkey, valname, NULL, &valtype, (LPBYTE)pvalue, &maxsize); if (rc) { //debug("RegQueryValueEx('%ls', '%ls', DWORD): ERROR %08lx\n", devicekey, valname, rc); RegCloseKey(hDevkey); return FALSE; } RegCloseKey(hDevkey); return TRUE; } void DumpDevPower() { DWORD devhnd=0; HKEY hActive; LONG rc= RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Drivers\\Active"), 0, 0, &hActive); if (rc) { error("RegOpenKeyEx"); } StringList l; int i=0; while (true) { TCHAR keyname[16]; DWORD keylen=15; rc=RegEnumKeyEx(hActive, i++, keyname, &keylen, NULL, NULL, NULL, NULL); if (rc==ERROR_NO_MORE_ITEMS) break; else if (rc) { error("RegEnumKeyEx"); continue; } TCHAR regdev[16]; if (ReadRegistryString(hActive, keyname, _T("Name"), regdev, 16)) { CEDEVICE_POWER_STATE state= PwrDeviceUnspecified; GetDevicePower(regdev, POWER_NAME|POWER_FORCE, &state); if (state!=PwrDeviceUnspecified) l.push_back(stringformat(" %ls=%d", regdev, state)); } } RegCloseKey(hActive); debug("devstate: %s\n", JoinStringList(l, ", ").c_str()); } int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { DebugSetLogfile("Battery.log"); debug(" ticks idle acl bfl b% r1 blt bflt r2 bkf bk% r3 bklt bkfl vol cur acur ai mah tmp bkv bkc flag wv\n"); while (1) { WordVector wv; wv.resize(16); DWORD pflags= 0; DWORD res= 0; do { wv.resize(wv.size()*2); res= GetSystemPowerState((WCHAR*)vectorptr(wv), wv.size(), &pflags); } while (res==ERROR_INSUFFICIENT_BUFFER && wv.size()<65536); if (res!=ERROR_SUCCESS) { error(res, "GetSystemPowerState"); } SYSTEM_POWER_STATUS_EX2 stat2; if (!GetSystemPowerStatusEx2(&stat2, sizeof(stat2), false)) error("GetSystemPowerStatusEx2"); // ticks idle acl bfl b% r1 blt bflt r2 bkf bk% r3 bklt bkfl vol cur acur ai mah tmp bkv bkc flag wv debugt("%08lx %08lx 0x%02x 0x%02x %3d 0x%02x %7d %7d 0x%02x 0x%02x %3d 0x%02x %7d %7d %4d %5d %5d %5d %5d %5d %5d %5d %08lx %ls\n", GetTickCount(), GetIdleTime(), stat2.ACLineStatus, stat2.BatteryFlag, stat2.BatteryLifePercent, stat2.Reserved1, stat2.BatteryLifeTime, stat2.BatteryFullLifeTime, stat2.Reserved2, stat2.BackupBatteryFlag, stat2.BackupBatteryLifePercent, stat2.Reserved3, stat2.BackupBatteryLifeTime, stat2.BackupBatteryFullLifeTime, stat2.BatteryVoltage, stat2.BatteryCurrent, stat2.BatteryAverageCurrent, stat2.BatteryAverageInterval, stat2.BatterymAHourConsumed, stat2.BatteryTemperature, stat2.BackupBatteryVoltage, stat2.BatteryChemistry, pflags, vectorptr(wv)); DumpDevPower(); Sleep(60000); } return 0; }