?? batterylog.cpp
字號(hào):
// BatteryLog.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "wirelessdevices.h"
#include "itc50.h"
#pragma comment (lib, "itc50.lib")
#include "BatteryLog.h"
#include "Log2File.h"
#include "nclog.h"
#define SEKUNDE 1000;
int BatCapacity = 4200; //battery capacity
bool bUseDLL=false;
bool bUseBCAST=false;
//global for arguments array
int argc;
TCHAR** argv;
TCHAR startPath[MAX_PATH];
TCHAR *szFlagFile = L"\\Windows\\_stop_battery_log.flag";
//thread stuff
HANDLE hTread=NULL;
BOOL bKeepRunning=false;
LONG idThread=0;
// Returns number of elements
#define dim(x) (sizeof(x) / sizeof(x[0]))
//===================================================================================
int getAppPath(TCHAR strPath[MAX_PATH])
{
//get the app dir
GetModuleFileName (NULL, strPath, MAX_PATH);
//find the last backslash
TCHAR* p;
if ( p = wcsrchr ( strPath, '\\'))
*++p = _T('\0'); // zero-terminate at the last backslash
wsprintf(startPath, strPath);
return 0;
}
int SplitArgs(LPTSTR lpCmdLine)
{
unsigned int i;
int j;
// parse a few of the command line arguments
// a space delimites an argument except when it is inside a quote
argc = 1;
int pos = 0;
for (i = 0; i < wcslen(lpCmdLine); i++)
{
while (lpCmdLine[i] == ' ' && i < wcslen(lpCmdLine))
{
i++;
}
if (lpCmdLine[i] == '\"')
{
i++;
while (lpCmdLine[i] != '\"' && i < wcslen(lpCmdLine))
{
i++;
pos++;
}
argc++;
pos = 0;
}
else
{
while (lpCmdLine[i] != ' ' && i < wcslen(lpCmdLine))
{
i++;
pos++;
}
argc++;
pos = 0;
}
}
argv = (TCHAR**)malloc(sizeof(TCHAR*)* (argc+1));
argv[0] = (TCHAR*)malloc(1024);
GetModuleFileName(0, argv[0],1024);
for(j=1; j<argc; j++)
{
argv[j] = (TCHAR*)malloc(wcslen(lpCmdLine)+10);
}
argv[argc] = 0;
argc = 1;
pos = 0;
for (i = 0; i < wcslen(lpCmdLine); i++)
{
while (lpCmdLine[i] == ' ' && i < wcslen(lpCmdLine))
{
i++;
}
if (lpCmdLine[i] == '\"')
{
i++;
while (lpCmdLine[i] != '\"' && i < wcslen(lpCmdLine))
{
argv[argc][pos] = lpCmdLine[i];
i++;
pos++;
}
argv[argc][pos] = '\0';
argc++;
pos = 0;
}
else
{
while (lpCmdLine[i] != ' ' && i < wcslen(lpCmdLine))
{
argv[argc][pos] = lpCmdLine[i];
i++;
pos++;
}
argv[argc][pos] = '\0';
argc++;
pos = 0;
}
}
argv[argc] = 0;
// Initialize the processes and start the application.
// retVal = MyMain(argc, argv);
// Delete arguments
TCHAR str[MAX_PATH+1];
for(j=0; j<argc; j++)
{
wsprintf(str, L"arg %i = %s\n", j, argv[j]);
OutputDebugString(str);
//free(argv[j]);
}
//free(argv);
return argc;
}
bool existFile(TCHAR f[MAX_PATH])
{
FILE *stream;
/* Open for read (will fail if file "data" does not exist) */
if( (stream = _wfopen( f, L"r" )) == NULL )
return false;
else
{
fclose(stream);
return true;
}
}
void dumpBattery()
{
SYSTEM_POWER_STATUS_EX2 pwrs;
int iRes = GetSystemPowerStatusEx2(&pwrs, sizeof(SYSTEM_POWER_STATUS_EX2), true);
/*
BYTE ACLineStatus;
BYTE BatteryFlag;
BYTE BatteryLifePercent;
BYTE Reserved1;
DWORD BatteryLifeTime;
DWORD BatteryFullLifeTime;
BYTE Reserved2;
BYTE BackupBatteryFlag;
BYTE BackupBatteryLifePercent;
BYTE Reserved3;
DWORD BackupBatteryLifeTime;
DWORD BackupBatteryFullLifeTime;
// Above here is old struct, below are new fields
DWORD BatteryVoltage; // Reports Reading of battery voltage in millivolts (0..65535 mV)
DWORD BatteryCurrent; // Reports Instantaneous current drain (mA). 0..32767 for charge, 0 to -32768 for discharge
DWORD BatteryAverageCurrent; // Reports short term average of device current drain (mA). 0..32767 for charge, 0 to -32768 for discharge
DWORD BatteryAverageInterval; // Reports time constant (mS) of integration used in reporting BatteryAverageCurrent
DWORD BatterymAHourConsumed; // Reports long-term cumulative average DISCHARGE (mAH). Reset by charging or changing the batteries. 0 to 32767 mAH
DWORD BatteryTemperature; // Reports Battery temp in 0.1 degree C (-3276.8 to 3276.7 degrees C)
DWORD BackupBatteryVoltage; // Reports Reading of backup battery voltage
BYTE BatteryChemistry; // See Chemistry defines above
*/
TCHAR str[1024];
TCHAR lpTimeStr[32];
TCHAR lpDateStr[32];
int res = GetTimeFormat(LOCALE_SYSTEM_DEFAULT,
TIME_FORCE24HOURFORMAT,
NULL,
L"hh:mm:ss",
lpTimeStr,
sizeof (lpTimeStr ) * sizeof(TCHAR));
if (res == 0)
{
wcscpy(lpTimeStr, L"00:00:00");
}
//Read the system date
res = GetDateFormat( LOCALE_SYSTEM_DEFAULT,
NULL,
NULL,
L"dd.MM.yyyy",
lpDateStr,
sizeof (lpDateStr) * sizeof(TCHAR));
if (res == 0)
{
wcscpy(lpDateStr, L"01.01.1961");
}
//convert to hours and minutes: ie 13.50 => 13 hours and 30 Minutes
double BatLife = (double)BatCapacity / (double)pwrs.BatteryCurrent;
int hhBatLife = (int) BatLife;
int mmBatLife = (int)(((BatLife - hhBatLife)*100) * 0.6);
DWORD dwWifi=-1;
DWORD dwPhone=-1;
DWORD dwBT=-1;
if (bUseDLL){
GetWDevState(&dwWifi, &dwPhone, &dwBT);
//if(dwWifi == 1)
// str=TEXT("ON");
//DeinitDLL();
}
DWORD dwBright=0;
ITCGetScreenBrightness(&dwBright);
int iBKLon=-1;
ITCGetScreenLight(&iBKLon);
wsprintf(str, L"%s \t%s \t%04u \t%04u \t%04u \t%04u \t%04u \t%u \t%u \t%u \t%u \t%u \t%u \t%u \t%02i:%02i \t%2u \t%2u \t%2u \t%2u \t%2u\r\n",
lpDateStr, lpTimeStr,
pwrs.ACLineStatus, pwrs.BatteryFlag, pwrs.BatteryLifePercent,
pwrs.BackupBatteryFlag, pwrs.BackupBatteryLifePercent,
pwrs.BatteryVoltage, pwrs.BatteryCurrent, pwrs.BatteryAverageCurrent, pwrs.BatteryAverageInterval, pwrs.BatterymAHourConsumed,
pwrs.BatteryTemperature, pwrs.BackupBatteryVoltage, hhBatLife, mmBatLife,
dwWifi, dwPhone, dwBT,
dwBright, iBKLon);
Add2Log(str, false);
if (bUseBCAST){
const int buffSize=24;
TCHAR number[buffSize-1];
if (ITC_ISSUCCESS(ITCGetSerialNumber(number, buffSize)))
nclog(L"%s:\t%s", number, str);
else
nclog(L"unknown:\t%s", str);
}
DEBUGMSG(true, (L"%s", str));
}
DWORD ThreadProc(LPVOID lpParameter)
{
do{
dumpBattery();
Sleep((ULONG) lpParameter);
}while (bKeepRunning);
return 0;
}
int WINAPI WinMain( HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
TCHAR strP[MAX_PATH];
int argc;
uint iTime=10;
TCHAR str[1024];
//use the ossvc.dll?
if (InitDLL())
bUseDLL=true;
else
bUseDLL=false;
argc = SplitArgs(lpCmdLine); //argc is count of arguments, argv[i] are the arguments
GetModuleFileName(NULL, strP, MAX_PATH);
getAppPath(strP);
TCHAR logFileName[MAX_PATH];
wsprintf(logFileName, L"%sbatterylog.txt", startPath);
if (wcslen(lpCmdLine)==0){
newfile(logFileName);
Add2Log(L"BatteryLog: invalid command line, missing argument\n", true);
Add2Log(L"usage: batterylog 10 \r\n\trun BatteryLog and log battery status every 10 minutes, estimation is done for 4000mAh capacity\n", false);
Add2Log(L"usage: batterylog 10 4200\r\n\trun BatteryLog and log battery status every 10 minutes, estimate for a 4200mAh capacity\n", false);
Add2Log(L"usage: batterylog kill\r\n\tkill all running BatteryLog processes\n", false);
MessageBeep(MB_ICONEXCLAMATION);
return -1;
}
DEBUGMSG(true, (L"argc=%i\r\n", argc));
if (argc>=2)
{
if (wcsicmp(L"kill", argv[1])==0)
{
FILE *stream;
stream = _wfopen ( szFlagFile, L"w" );
if (stream != NULL)
{
fwprintf(stream, L"file to stop battery logging");
fclose(stream);
return 1;
}
else
return -2;
}
if (argc==3){
DWORD d=_wtoi(argv[2]);
if (d!=0)
BatCapacity = d;
}
if (argc==4){
if (wcsicmp(L"NOBCAST", argv[3]) == 0)
bUseBCAST = false;
}
else
bUseBCAST = true;
int i=_wtoi(argv[1]);
if ( (i>0) && (i<60) )
{
newfile(logFileName);
//print header
wsprintf(str, L"Date \tTime \tACLineStatus \tBatteryFlag \tBatteryLifePercent \tBackupBatteryFlag \tBackupBatteryLifePercent \tBatteryVoltage \tBatteryCurrent \tBatteryAverageCurrent \tBatteryAverageInterval \tBatterymAHourConsumed \tBatteryTemperature \tBackupBatteryVoltage \testimated BattLife(for %i mAh) \twifi \tphone \tBT \tbkl \tBKL stat\r\n", BatCapacity);
DEBUGMSG(true, (L"%s", str));
Add2Log(str, false);
iTime=i * SEKUNDE; //use as minutes 60 * 1000mSeconds
//the main LOOP, iTime is 60000 to ?
//check all 1000ms (1 sec) for exit
//dumplog all iTime
DWORD dwCount=0;
do
{
if (dwCount == iTime){
dumpBattery();
dwCount=0;
}
dwCount += 1000;
Sleep(500);
}
while (!existFile(szFlagFile));
// or use a thread
//hTread = CreateThread(NULL, 0, ThreadProc, &iTime, 0, &idThread);
if(bUseDLL)
DeinitDLL();
DeleteFile(szFlagFile);
MessageBeep(MB_OK);
return 0;
}
}
return 0;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -