📄 batteryhelp.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include <fstream>#include <iostream>#include <strstream>//#include "CoreBatteryImpl.h"#include "BatteryHelp.h"using namespace std;#define BUFSIZE 180#define WordMatch(a, b) ( (*(a) == *(b)) && (strcmp(a, b) == 0) )char *BatteryInfo_FileName="/proc/acpi/battery/BAT0/info";//char *BatteryInfo_FileName="info";char *BatteryState_FileName="/proc/acpi/battery/BAT0/state";//char *BatteryState_FileName="state";char *AC_FileName="/proc/acpi/ac_adapter/AC/state";char *Temperature_FileName="/proc/acpi/thermal_zone/THRM/temperature";int GetInfo(int id,BatteryInfo_T *battery_info){ char pcReadLine[BUFSIZE]; char pcKeyword[BUFSIZE]; char pcKeyword2[BUFSIZE]; char pcKeyword3[BUFSIZE];// int iDone = 0; /* Flag to determine end of loop */ memset(pcReadLine,0,BUFSIZE); memset(pcKeyword,0,BUFSIZE); memset(pcKeyword2,0,BUFSIZE); memset(pcKeyword3,0,BUFSIZE); fstream fpBatteryInfo(BatteryInfo_FileName, ios::in);//cout<<"want to read"<<endl; if(fpBatteryInfo) {//cout<<"have read"<<endl;//int i=1; /* Read a line into the buffer */ while ((fpBatteryInfo.getline(pcReadLine, BUFSIZE))!=NULL)// && (iDone == 0)) {//cout<<i<<endl;//i++; /* * Instantiate istrmInput to translate input line into a stream object */ istrstream istrmInput(pcReadLine); /* * Get first 2 word from the input line and build the keyword */ istrmInput >> pcKeyword; istrmInput >> pcKeyword2; // istrmInput >> pcKeyword3; strcat(pcKeyword, " "); strcat(pcKeyword, pcKeyword2); // strcat(pcKeyword, " "); // strcat(pcKeyword, pcKeyword3); //cout<<pcKeyword<<endl; if (WordMatch(pcKeyword, "design capacity:")) { //istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_info->DesignedCapacity=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "last full"))// capacity")) { //istrmInput >> pcKeyword; istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_info->FullChargedCapacity=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "battery technology:")) { // istrmInput >> pcKeyword; istrmInput >> pcKeyword; if(WordMatch(pcKeyword,"rechargeable")) battery_info->Rechargeable=1; else battery_info->Rechargeable=0; } else if (WordMatch(pcKeyword, "design voltage:")) { //istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_info->DesignedVoltage=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "design capacity"))// warning")) { istrmInput >> pcKeyword; if(WordMatch(pcKeyword,"warning:")) { istrmInput >> pcKeyword; battery_info->CriticalAlert=(unsigned long)(atof(pcKeyword)); } if(WordMatch(pcKeyword,"low:")) { istrmInput >> pcKeyword; battery_info->CriticalBias=(unsigned long)(atof(pcKeyword)); } }/* else if (WordMatch(pcKeyword, "design capacity"))// low")) { istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_info->CriticalBias=(unsigned long)(atof(pcKeyword)); }*/ else if (WordMatch(pcKeyword, "capacity granularity"))// 1")) { istrmInput >> pcKeyword; if(WordMatch(pcKeyword,"1:")){ istrmInput >> pcKeyword; battery_info->SCriticalAlert=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword,"2:")) { istrmInput >> pcKeyword; battery_info->SCriticalBias=(unsigned long)(atof(pcKeyword)); } } else if (WordMatch(pcKeyword, "model number:")) { istrmInput >> pcKeyword; battery_info->Name=strdup(pcKeyword); } else if (WordMatch(pcKeyword, "serial number:")) { istrmInput >> pcKeyword; //battery_info->SerialNumber=(unsigned long)(atof(pcKeyword)); battery_info->SerialNumber=strdup(pcKeyword);//(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "battery type:")) { istrmInput >> pcKeyword; battery_info->Chemistry=strdup(pcKeyword); } else if (WordMatch(pcKeyword, "OEM info:")) { istrmInput >> pcKeyword; battery_info->Manufacturer=strdup(pcKeyword); } } return 0; } else { return 1; }}int GetState(int id,BatteryState_T *battery_state){ char pcReadLine[BUFSIZE]; char pcKeyword[BUFSIZE]; char pcKeyword2[BUFSIZE]; //char pcKeyword3[BUFSIZE]; int iDone = 0; /* Flag to determine end of loop */ memset(pcReadLine,0,BUFSIZE); memset(pcKeyword,0,BUFSIZE); memset(pcKeyword2,0,BUFSIZE); //memset(pcKeyword3,0,BUFSIZE); fstream fpBatteryState(BatteryState_FileName, ios::in);//cout<<"want to read state"<<endl; if(fpBatteryState) {//cout<<"read now"<<endl;int i=1; /* Read a line into the buffer */ while ((fpBatteryState.getline(pcReadLine, BUFSIZE))&& (iDone == 0)) {//cout<<i<<endl;i++; //sleep(100);//fpBatteryState.getline(pcReadLine,BUFSIZE); /* * Instantiate istrmInput to translate input line into a stream object */ istrstream istrmInput(pcReadLine); /* * Get first 2 word from the input line and build the keyword */ istrmInput >> pcKeyword; istrmInput >> pcKeyword2; //istrmInput >> pcKeyword3; strcat(pcKeyword, " "); strcat(pcKeyword, pcKeyword2); //strcat(pcKeyword, " "); //strcat(pcKeyword, pcKeyword3);//cout<<pcKeyword<<endl;//cout<<"while"<<endl; if (WordMatch(pcKeyword, "charging state:")) { // istrmInput >> pcKeyword; istrmInput >> pcKeyword; //cout<<pcKeyword<<endl;battery_state->Condition=strdup(pcKeyword); /* if(WordMatch(pcKeyword,"charging")) battery_state->Condition=1;//1 charging.... else battery_state->Condition=0; */ } if (WordMatch(pcKeyword, "present rate:")) { // istrmInput >> pcKeyword; istrmInput >> pcKeyword;// cout<<pcKeyword<<endl; battery_state->Rate=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "remaining capacity:")) { //istrmInput >> pcKeyword; // istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_state->Capacity=(unsigned long)(atof(pcKeyword)); } else if (WordMatch(pcKeyword, "present voltage:")) { // istrmInput >> pcKeyword; istrmInput >> pcKeyword; battery_state->CurrentVoltage=(unsigned long)(atof(pcKeyword));iDone=1; } } return 0; } else return 1;}int BatteryInit(int id,Battery_T *battery){ int info=GetInfo(id,&battery->batteryinfo); int state=GetState(id,&battery->batterystate); if(!info && !state) { battery->isInit=1; return 0; } return 1;}int GetSystemBattery(bool *isAC){ char pcReadLine[BUFSIZE]; char pcKeyword[BUFSIZE]; char pcKeyword2[BUFSIZE]; memset(pcReadLine,0,BUFSIZE); memset(pcKeyword,0,BUFSIZE); memset(pcKeyword2,0,BUFSIZE); fstream fpSystemBattery(AC_FileName,ios::in); if(fpSystemBattery) { fpSystemBattery.getline(pcReadLine,BUFSIZE); istrstream istrmInput(pcReadLine); istrmInput >> pcKeyword; // istrmInput >> pcKeyword2; // strcat(pcKeyword," "); // strcat(pcKeyword,pcKeyword2); if(WordMatch(pcKeyword,"state:")){istrmInput>>pcKeyword;if(WordMatch(pcKeyword,"on-line")) *isAC=1; else *isAC=0; return 0;} } else return 1;}/*LINUXBATTERY_API */int GetTemperature(DWORD *tem){//cout<<"want \t\t\t\t\t\tbfsh"<<endl; char pcReadLine[BUFSIZE]; char pcKeyword[BUFSIZE]; char pcKeyword2[BUFSIZE]; memset(pcReadLine,0,BUFSIZE); memset(pcKeyword,0,BUFSIZE); memset(pcKeyword2,0,BUFSIZE); fstream fpTemperature(Temperature_FileName,ios::in); if(fpTemperature) { fpTemperature.getline(pcReadLine,BUFSIZE); istrstream istrmInput(pcReadLine); istrmInput >> pcKeyword; // istrmInput >> pcKeyword2; // strcat(pcKeyword," "); // strcat(pcKeyword,pcKeyword2); if(WordMatch(pcKeyword,"temperature:")) { istrmInput>>pcKeyword;//cout<<"tempreateifjaksj :\t\t\t\t"<<pcKeyword<<endl; *tem=(unsigned long)atof(pcKeyword); return 0; } } else return 1;}/*LINUXBATTERY_API*/ int GetEstimatedTimeRemaining(Battery_T *battery,DWORD *time){// if(abs(battery->batterystate.Rate-0.00)>0.001) if(abs((int)(battery->batterystate.Rate-0.00)*1000)>1) {//cout<<battery->batterystate.Capacity<<"\t"<<battery->batterystate.Rate<<endl; *time=battery->batterystate.Capacity/battery->batterystate.Rate; return 0; } else return 1;}/*DWORD GetTimeRemainingAtCurrentRate(int capacity,double rate){ if(abs(rate-0)<0.001) return capacity/rate; else return 0;}*//*LINUXBATTERY_API */int GetLifePercentRemaining(Battery_T *battery, LONG *lifePercent){ //if(abs(battery->batterystate.Capacity-0.00)>0.000001) if(abs(((int)(battery->batterystate.Capacity-0.00)*1000000))>1) { *lifePercent=(battery->batteryinfo.FullChargedCapacity/battery->batterystate.Capacity); return 0; } return 1;}char *precondition;void BatteryListing(){system("acpid");Battery_T *battery=(Battery_T*)malloc(sizeof(Battery_T));//INIT_VAL;GetInfo(1,&(battery->batteryinfo));GetState(1,&(battery->batterystate));if(precondition!=battery->batterystate.Condition){//cout<<"event:\t"<<precondition<<"change to"<<battery.batterystate.Condition<<endl;precondition=strdup(battery->batterystate.Condition);}}/*int GetXXX(char *path,DWORD *var){ char pcReadLine[BUFSIZE]; char pcKeyword[BUFSIZE]; char pcKeyword2[BUFSIZE]; memset(pcReadLine,0,BUFSIZE); memset(pcKeyword,0,BUFSIZE); memset(pcKeyword2,0,BUFSIZE); fstream fpXXX(path,ios::in); if(fpXXX) { fpXXX.getline(pcReadLine,BUFSIZE);if(pcReadLine){}else{} istrstream istrmInput(pcReadLine); istrmInput >> pcKeyword; // istrmInput >> pcKeyword2; // strcat(pcKeyword," "); // strcat(pcKeyword,pcKeyword2); if(WordMatch(pcKeyword,"state:")){istrmInput>>pcKeyword;if(WordMatch(pcKeyword,"on-line")) *isAC=1; else *isAC=0; return 0;} } else return 1;}*//*int main(int argc, char* argv[]){precondition=NULL; Battery_T battery=INIT_VAL; GetInfo(&(battery.batteryinfo)); GetState(&(battery.batterystate)); cout<<"**********************************battery information**************************************"<<endl;BatteryListing(); cout<<"Manufacturer: \t"<<battery.batteryinfo.Manufacturer<<endl; cout<<"Name: \t"<<battery.batteryinfo.Name<<endl; cout<<"SerialNumber: \t"<<battery.batteryinfo.SerialNumber<<endl; cout<<"Chemistry: \t"<<battery.batteryinfo.Chemistry<<endl; cout<<"DesignedCapacity: \t"<<battery.batteryinfo.DesignedCapacity<<endl; cout<<"FullChargedCapacity: \t"<<battery.batteryinfo.FullChargedCapacity<<endl; cout<<"CurrentCapacity: "<<battery.batterystate.Capacity<<endl; cout<<"DesignedVoltage: "<<battery.batteryinfo.DesignedVoltage<<endl; cout<<"CriticalAlert: \t"<<battery.batteryinfo.CriticalAlert<<endl; cout<<"CriticalBias: \t"<<battery.batteryinfo.CriticalBias<<endl; cout<<"InternalStatusUpdateInterval: \t"<<battery.batteryinfo.InternalStatusUpdateInterval<<endl; cout<<"Rechargeable: \t"<<battery.batteryinfo.Rechargeable<<endl; cout<<"Rate: \t"<<battery.batterystate.Rate<<endl; cout<<"Condition: \t"<<battery.batterystate.Condition<<endl; cout<<"CurrentVoltage: \t"<<battery.batterystate.CurrentVoltage<<endl; BOOL isAC; GetSystemBattery(&isAC);// char *str=((isAC==1)?"AC online":"AC offline"); cout<<"SystemBattery:isAC? \t"<<isAC<<endl; DWORD tem=0; GetTemperature(&tem); cout<<"Temperature: \t"<<tem<<endl;tem=0; GetEstimatedTimeRemaining(&battery,&tem); cout<<"EstimatedTimeRemaining: \t"<<tem<<endl; DWORD life; GetLifePercentRemaining(&battery,&life); cout<<"LifePercentRemaining: \t"<<life<<endl; return 0;}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -