📄 instance_battery.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 "stdafx.h"#include "Instance_Battery.h"//#include "BatteryHelp.h"#include "inc/common/EnumBase.h"//#include "inc/framework/core_utils.h"#include <sys/time.h>#include <stdlib.h>long BATTERY_UNKNOWN_CAPACITY=0;long BATTERY_UNKNOWN_TIME=0;long BATTERY_UNKNOWN_RATE=0;long BATTERY_UNKNOWN_VOLTAGE=0;typedef time_t SYSTEMTIME;typedef IntelMobileChar * BSTR;typedef struct GLOBAL_POWER_POLICY{};typedef struct POWER_POLICY{};typedef struct GLOBAL_USER_POWER_POLICY{};typedef struct USER_POWER_POLICY{};typedef struct MACHINE_POWER_POLICY{};typedef struct SYSTEM_POWER_LEVEL{};#define NUM_DISCHARGE_POLICIES 2typedef struct GLOBAL_MACHINE_POWER_POLICY{};const IntelMobileString& DefaultBatteryInstanceObject::GetKey(){ Lock(); m_sKey = Id.GetValue();// + GetType(); Unlock(); return m_sKey;}/*HRESULT IntelMobileString DefaultBatteryInstanceObject::GetType( BSTR* sClass ){ *sClass = SysAllocString( TEXT("Battery") ); if ( *sClass == NULL ) return E_OUTOFMEMORY; return S_OK;}*/int x=0;bool DefaultBatteryInstanceObject::Initialize( /*in*/ const IntelMobileString& szDeviceID ){//cout<<"DefaultBatteryInstanceObject ::Initialize\t"<<x++<<endl; try { bool bRet = false; Lock(); if ( szDeviceID[0] != NULL ) { Battery_T *battery=(Battery_T *)malloc(sizeof(Battery_T)); memset(battery,0,sizeof(Battery_T)); BatteryInit(1,battery); __battery=battery; m_sKey=(szDeviceID);// Id.SetValue(L"1"); // Index.SetValue ( 2 ); // long tag = _wtol( szDeviceID.c_str() );// long tag = atol(( char*)(szDeviceID.c_str()));// cout<<"tag="<<tag<<endl;// long tag=10000000;// if (tag/1000000-1 >= 0) { Id.SetValue( szDeviceID ); // Id.SetValue(L"1" ); // Index.SetValueImpl ( 1 ); bRet = true;//sleep(1); } } //===== initialization of values for Thread ===== m_bThreadActive = false; m_hThreadHandle = NULL; m_InternalStatusUpdateInterval = 1; //===== end of initialization =====//Battery_T battery=INIT_VAL;//CPUINFO_T x=CPUINFO_T_INIT_VAL;//LinCPUID_Init(&x);//GetInfo(&battery.batteryinfo);//GetState(&battery.batterystate); Unlock(); bRet = RegisterEvents(); return bRet;//return true; } catch(...) { Unlock(); THROWIMEXCEPTION("80043049"); }}DefaultBatteryInstanceObject::~DefaultBatteryInstanceObject(){ _Log( LOG_DEBUG_DEVICE, TEXT("Trace: DefaultBatteryInstanceObject::~DefaultBatteryInstanceObject()") ); UnRegisterEvents();}bool DefaultBatteryInstanceObject::RegisterEvents(){ Lock(); if (m_bThreadActive == false) { if (m_hThreadHandle) { CloseHandle(m_hThreadHandle); m_hThreadHandle = NULL; } DWORD dwThreadId = 0;//MonitorCondition(); m_hThreadHandle = CreateThread(NULL, 0, ThreadProc, this, NULL, &dwThreadId); //m_hThreadHandle = CreateThread(NULL, 0, ThreadProc, this, NULL, NULL); if (m_hThreadHandle) m_bThreadActive = true; } Unlock(); return m_bThreadActive;}bool DefaultBatteryInstanceObject::UnRegisterEvents(){ Lock(); if (m_hThreadHandle) { CloseHandle(m_hThreadHandle); m_hThreadHandle = NULL; m_bThreadActive = false; } Unlock(); //cout << "unregisterEvents\n" << endl; return true;}DWORD __stdcall DefaultBatteryInstanceObject::ThreadProc(LPVOID pParam){//cout<<"thread proc"<<endl; DefaultBatteryInstanceObject *pThis = reinterpret_cast<DefaultBatteryInstanceObject *> (pParam); if ( pThis ) pThis->MonitorCondition(); return 0;}void DefaultBatteryInstanceObject::MonitorCondition(){ IntelMobileInt PreviousCondition = Condition.GetValue(), CurrentCondition; IntelMobileUInt64 PreviousCapacity = 0, CurrentCapacity; unsigned int total_times = 10, current_times = 0; DWORD dwStart, dwSpan;//cout<<"battery active:"<<m_bThreadActive<<endl; while (m_bThreadActive) { try { CurrentCondition = Condition.GetValue();// cout<<"precondition:\t"<<PreviousCondition<<", curcondition\t"<<CurrentCondition<<endl; if ( ( PreviousCondition != BatteryConditionEnum::Charged ) && ( CurrentCondition == BatteryConditionEnum::Charged ) ) { FireEvent(ServerEvent(ServerEvent::eFullyCharged, this)); printf("FullyCharged\n"); } if ( ( PreviousCondition == BatteryConditionEnum::Discharging ) && ( CurrentCondition != BatteryConditionEnum::Discharging ) ) { FireEvent(ServerEvent(ServerEvent::eGoingOffline, this)); printf("GoingOffline\n"); } if ( ( PreviousCondition != BatteryConditionEnum::Discharging ) && ( CurrentCondition == BatteryConditionEnum::Discharging ) ) { FireEvent(ServerEvent(ServerEvent::ePoweringSystem, this)); printf("PoweringSystem\n"); } if ( PreviousCondition != CurrentCondition ) PreviousCondition = CurrentCondition; if ( current_times < total_times ) { if (( CurrentCondition == BatteryConditionEnum::Charging ) || ( CurrentCondition == BatteryConditionEnum::Discharging )) { CurrentCapacity = Capacity.GetValue();//cout<<"precapacity:\t"<<PreviousCapacity<<", curcapacity\t"<<CurrentCapacity<<endl; if ( PreviousCapacity == 0 ) { PreviousCapacity = CurrentCapacity; // dwStart = GetTickCount(); dwStart = time((time_t *)NULL);//cout<<"dwStart1:\t"<<dwStart; } else if ( PreviousCapacity != CurrentCapacity ) { current_times ++; //dwSpan = GetTickCount()-dwStart; //dwStart = GetTickCount(); dwSpan = time((time_t*)NULL)-dwStart;//cout<<"dwSpan:\t"<<dwSpan; dwStart = time((time_t*)NULL);//cout<<"dwStart:\t"<<dwStart;// FireEvent(ServerEvent(ServerEvent::eChanged, this)); if ( m_InternalStatusUpdateInterval < dwSpan ) { m_InternalStatusUpdateInterval = dwSpan; } } } else { PreviousCapacity = 0; //reset ::Sleep( Condition.GetMinPollRate() ); } } else // Add this in order to prohibit from sleeping when probing InternalStatusUpdateInterval // After that, sleeping is allowable. // wonking 10/8/2004 {// current_times=0; ::Sleep( Condition.GetMinPollRate() ); } } catch(...) { _Log( LOG_FATAL_ERROR, L"BatteryInstanceObject::MonitorCondition"); } }}/*IntelMobileUInt64 DefaultBatteryInstanceObject::TimeRemainingAtRate(IntelMobileInt64 Rate){ if ( Rate >= 0 ) return 0; else return ( this->Capacity.GetValue()*3600/-Rate );}*////////////////////////////////////////////////////////////////////////////////////// Property Set ///////////////////////////////////////////////////////////////////////////////////////template for change//)// this->SetValueImpl( psValue );// else// {// this->SetNull( true );////}// ServerNetworkAdapterInstanceObject* pThis = (ServerNetworkAdapterInstanceObject*)this->_pThis;//end templatevoid BatteryIndexClass::GetValueImpl(){ // Static, will never have to update this since it was defined in constructor this->SetValueImpl(1);}void BatteryIdClass::GetValueImpl(){ // Static, will never have to update this since it was defined in constructor}void BatteryConditionClass::GetValueImpl(){ //TODO use an int property for now (1: Charging, 2: Discharging, 3: Charged, 4: Discharged, 5: PartiallyCharged) DefaultBatteryInstanceObject* pThis = (DefaultBatteryInstanceObject*)this->_pThis; int index; int value=-1; // LONG rate= 0; IntelMobileUInt lifepercentremaining = 0; if(!pThis->Index.IsNull()) { index = pThis->Index.GetValue(); } else { return; } // order is such, that charging, discharing have higher priority than charged and discharged status //first test to see if battery is at least partially charged if(!pThis->PercentRemaining.IsNull()) { // value = 5; // set to PartiallyCharged value = BatteryConditionEnum::Unknown; } //second check for fully charged or empty if(!pThis->PercentRemaining.IsNull()) { lifepercentremaining = pThis->PercentRemaining.GetValue(); if(lifepercentremaining > 98) { //above 98 is considered charged //charged value = BatteryConditionEnum::Charged; } if(lifepercentremaining < 5) { //below 5% is considered empty //discharged value = BatteryConditionEnum::Discharged; } } //finally check for charging or discharging if(!pThis->Rate.IsNull()) {// IntelMobileUInt64 rate = pThis->Rate.GetValue(); IntelMobileInt64 rate = pThis->Rate.GetValue(); if(rate > 0) { //charging value = BatteryConditionEnum::Charging; } if(rate <0) { //discharging value = BatteryConditionEnum::Discharging; } } //To resolve issue: battery is not full, however it is not being charged and not being discharged //NEW 03/29/04 PartiallyCharged is added as a value //currently this results in Condition being NULL if(value == -1){ //condition is not one of the 5 this->SetNull( true ); //needed since SetValue is setting Null to false } else{ this->SetValueImpl( value ); } return;}void BatteryCapacityClass::GetValueImpl(){ DefaultBatteryInstanceObject* pThis = (DefaultBatteryInstanceObject*)this->_pThis; ULONG value = 0; int index ; ULONG Capacity; DWORD ret; if(!pThis->Index.IsNull()) { index = pThis->Index.GetValue(); } else { return; } Battery_T battery; BatteryInit(index,&battery); ret = GetState(index,&battery.batterystate); if (ret == 0) { this->SetValueImpl(battery.batterystate.Capacity); } else { //raise error Battery rate unknows //TODO this->SetNull( true ); }}void BatteryChemistryClass::GetValueImpl (){ DefaultBatteryInstanceObject* pThis = (DefaultBatteryInstanceObject*)this->_pThis; if (pThis->__battery->isInit) { IntelMobileChar chemistry[256]; #ifdef _UNICODE mbstowcs(chemistry,pThis->__battery->batteryinfo.Chemistry,256); #else strncpy(chemistry,pThis->__battery->batteryinfo.Chemistry,256); #endif this->SetValueImpl( chemistry ); } else { //raise error //TODO this->SetNull( true ); }/* int index; BATTERY_DEVICE_LIST DeviceList; BATTERY_DEVICE * BatteryDevice; DWORD ret; char Chemistry[16]="bfsh"; // char *Chemistry; //char *Chemistry; // BSTR value = NULL; IntelMobileChar value[256]=L""; if(!pThis->Index.IsNull()) { index = pThis->Index.GetValue(); } else { return; } //Only first implementation to see that it works within framework ret = pThis->GetBatteryInstances( &DeviceList ); //TODO optimize code, pull Devicelist out as a global if (ret == 0){ if(DeviceList.Device[index].Tag !=0) { //we do have a battery installed in the battery slot BatteryDevice = &DeviceList.Device[index]; ret = pThis->GetBatteryChemistry( BatteryDevice, Chemistry ); if (ret == 0){ ///printf("value %s\n",Chemistry); used for debugging only //value = _com_util::ConvertStringToBSTR( Chemistry );//IntelMobileChar *bfsh; //cout<<"\tjjjjjjjjjjjjjj:"<<Chemistry<<"ff"<<endl; //strcpy(value,Chemistry);//value=(IntelMobileChar [256])strdup(Chemistry);//cout<<(IntelMobileChar *)strdup(Chemistry);//memcpy();//memcpy(value,Chemistry,256);//cout<<"bfsh:"<<bfsh<<endl;//value=(IntelMobileChar*)Chemistry;//this->SetValueImpl( static_cast<IntelMobileChar*>( value ) );mbstowcs(value,Chemistry,256);//this->SetValueImpl( static_cast<IntelMobileChar*>( L"value" ) ); //this->SetValueImpl((IntelMobileChar *)Chemistry); this->SetValueImpl(value); //::SysFreeString( value ); //Leak fixed. //cout<<"bfdsh chemistry:"<<value<<endl; } else{ //raise error //TODO this->SetNull( true ); } } else { //raise error //TODO this->SetNull( true ); } } else { //raise error //TODO this->SetNull( true ); }*/}void BatteryCriticalBiasClass::GetValueImpl (){ DefaultBatteryInstanceObject* pThis = (DefaultBatteryInstanceObject*)this->_pThis; if (pThis->__battery->isInit) { this->SetValueImpl( pThis->__battery->batteryinfo.CriticalBias ); } else { //raise error //TODO this->SetNull( true ); }/* ULONG value = 0; int index; BATTERY_DEVICE_LIST DeviceList; ULONG CriticalBias; BATTERY_DEVICE * BatteryDevice; DWORD ret; if(!pThis->Index.IsNull()) { index = pThis->Index.GetValue(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -