📄 instance_power.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.
*///==============================================================================// Instance_Power.cpp//// Implement the Power APIs////// //==============================================================================//#include "stdafx.h"#include "Instance_Power.h"#include "inc/battery/battery_ClassObject.h"#include "inc/battery/battery_InstanceObject.h"#include "inc/common/EnumBase.h"#include <math.h>#include "inc/base/Layer_Manager.h"#include <sys/ioctl.h>#include <fcntl.h>//#include "WMIReader.h"//static class CWMIReader myWMI;#define LOG_DEGUG LOG_DEBUG_CONTEXT//==============================================================================//// PowerInstanceObject implementation////==============================================================================typedef struct { int level, voltage, raw; unsigned char ac; unsigned char battery;} BATTERY_RET;#define MZ_GET_BATTERY_STATUS _IOR(('h'), 0x85, BATTERY_RET)bool PowerInstanceObject::Initialize( const IntelMobileString& szKey ){ m_sKey = szKey; // TODO return true;}/*unsigned int PowerInstanceObject::SetPowerState( int PowerState, DATE When ){ return 0;}*///==============================================================================//==============================================================================//// Power properties////==============================================================================//==============================================================================//// These all return the true value. If there is an error, throw // an appropriate HRESULT. These need to return the true value// because the override the virtual GetValue() function in the // Property base class./*void PowerManagementSupportedClass::GetValueImpl(){ _Log( LOG_DEBUG_CONTEXT, TEXT("Trace: PowerManagementSupportedClass::GetValueImpl()")); IntelMobileBool value; myWMI.GetWmiAttribute(&value, L"PowerManagementSupported"); this->SetValueImpl( value);}void PowerManagementEnabledClass::GetValueImpl(){ this->SetNull ( true );}void PowerAutoManagedClass::GetValueImpl(){ this->SetNull ( true );}void PowerPowerStateSettableClass::GetValueImpl(){ this->SetNull ( true );}void PowerCyclePowerSupportClass::GetValueImpl(){ this->SetNull ( true );}void PowerTimedPowerOnSupportedClass::GetValueImpl(){ this->SetNull ( true );}void PowerStateClass::GetValueImpl(){ this->SetNull ( true );}*/void PowerSourceClass::GetValueImpl(){ try {/* _Log( LOG_DEBUG_CONTEXT, TEXT("Trace: PowerSourceClass::GetValueImpl()")); SYSTEM_POWER_STATUS *pSystemPowerStatus = new SYSTEM_POWER_STATUS(); BOOL bVal = GetSystemPowerStatus(pSystemPowerStatus);*/ int fd=open("/dev/misc/apm_bios",O_RDWR); BATTERY_RET *power=(BATTERY_RET *)malloc(sizeof(BATTERY_RET)); int res=ioctl(fd,MZ_GET_BATTERY_STATUS,power); if(res==0) { if(power->ac==0x00) { this->SetValueImpl( PowerSourceEnum::Internal ); //internal source DC } else if(power->ac==0x01) { this->SetValueImpl( PowerSourceEnum::External ); //external source AC } else this->SetNull ( true ); } else { this->SetNull ( true ); } // delete pSystemPowerStatus; free(power); close(fd); } catch (...) { // THROWIMEXCEPTION(8004321B); } /* if (bVal) { if (pSystemPowerStatus->ACLineStatus == 0) { this->SetValueImpl( Internal ); //internal source DC } else if (pSystemPowerStatus->ACLineStatus == 1) { this->SetValueImpl( External ); //external source AC } else { this->SetNull ( true ); } } else { this->SetNull ( true ); } delete pSystemPowerStatus; } catch (...) { THROWIMEXCEPTION(8004321B); }*/}void PowerCapacityClass::GetValueImpl(){ try { // _Log( LOG_DEBUG_CONTEXT, TEXT("Trace: PowerCapacityClass::GetValueImpl()")); ServerInstanceCollection *myBatteryCollection = NULL; ServerInstanceObject *myInstanceObject = NULL; IntelMobileUInt64 sumCapacity = 0; bool isnull = true; myBatteryCollection = ( GetLayer()->GetLayerClassObject(IntelMobileText("Battery")) ).GetInstances(); if ( myBatteryCollection ) { long iChangedIndex = 0; myBatteryCollection->Reset( &iChangedIndex ); int iEnumIndex = 0; while ( myBatteryCollection->HasNext(iEnumIndex, iChangedIndex) ) { if ( !myBatteryCollection->Next(&myInstanceObject, &iEnumIndex, iChangedIndex) ) break; if ( myInstanceObject ) { if ( myInstanceObject->GetType().compare(IntelMobileText("Battery")) == 0 ) { // ServerBatteryInstanceObject *myBatteryInstanceObject = dynamic_cast <ServerBatteryInstanceObject *> (myInstanceObject); ServerBatteryInstanceObject *myBatteryInstanceObject = reinterpret_cast <ServerBatteryInstanceObject *> (myInstanceObject); if ( !myBatteryInstanceObject->Capacity.IsNull() ) { sumCapacity += myBatteryInstanceObject->Capacity.GetValue(); if (isnull) isnull = false; } else { isnull = true; break; } } } myInstanceObject = NULL; } } if (isnull) this->SetNull( true ); else this->SetValueImpl( sumCapacity ); } catch (...) { // THROWIMEXCEPTION(8004321C); }}void PowerStateClass::GetValueImpl(){ try { // _Log( LOG_DEBUG_CONTEXT, TEXT("Trace: PowerStateClass::GetValueImpl()")); ServerInstanceCollection *myBatteryCollection = NULL; ServerInstanceObject *myInstanceObject = NULL; IntelMobileInternalPowerStateEnum value = InternalPowerStateEnum::Unknown; bool charged = true, discharged = true; myBatteryCollection = ( GetLayer()->GetLayerClassObject(/*L*/IntelMobileText("Battery")) ).GetInstances(); if ( myBatteryCollection ) { long iChangedIndex = 0; myBatteryCollection->Reset( &iChangedIndex ); int iEnumIndex = 0; while ( myBatteryCollection->HasNext(iEnumIndex, iChangedIndex) ) { if ( !myBatteryCollection->Next(&myInstanceObject, &iEnumIndex, iChangedIndex) ) break; if ( myInstanceObject ) { if ( myInstanceObject->GetType().compare(/*L*/IntelMobileText("Battery")) == 0 ) { //ServerBatteryInstanceObject *myBatteryInstanceObject = dynamic_cast <ServerBatteryInstanceObject *> (myInstanceObject); ServerBatteryInstanceObject *myBatteryInstanceObject = reinterpret_cast <ServerBatteryInstanceObject *> (myInstanceObject); if ( !myBatteryInstanceObject->Condition.IsNull() ) { if ( myBatteryInstanceObject->Condition.GetValue() == BatteryConditionEnum::Charging ) { value = InternalPowerStateEnum::Charging; charged = false; discharged = false; break; } else if ( myBatteryInstanceObject->Condition.GetValue() == BatteryConditionEnum::Discharging ) { value = InternalPowerStateEnum::Discharging; charged = false; discharged = false; break; } else if ( myBatteryInstanceObject->Condition.GetValue() == BatteryConditionEnum::Charged ) { value = InternalPowerStateEnum::Charged; discharged = false; } else if (myBatteryInstanceObject->Condition.GetValue() == BatteryConditionEnum::Discharged ) { value = InternalPowerStateEnum::Discharged; charged = false; } else { value = InternalPowerStateEnum::Unknown; break; } } else { value = InternalPowerStateEnum::Unknown; //Unknown break; } } } myInstanceObject = NULL; } } if ( value == InternalPowerStateEnum::Unknown ) { this->SetValueImpl( InternalPowerStateEnum::Unknown ); return; } if ( (charged == true) && (discharged == false) ) { this->SetValueImpl( InternalPowerStateEnum::Charged ); return; } else if ( (discharged==true) && (charged == false) ) { this->SetValueImpl( InternalPowerStateEnum::Discharged ); return; } else { /* if ( (discharged == false) && (charged == false) && ( (value == Discharging) || (value == Charging) ) ) { this->SetValueImpl( value ); } else { this->SetValueImpl( Undetermined ); }*/ if ( (discharged == false) && (charged == false) ) { if (value == InternalPowerStateEnum::Discharging) this->SetValueImpl( InternalPowerStateEnum::Discharging ); else if (value == InternalPowerStateEnum::Charging) this->SetValueImpl( InternalPowerStateEnum::Charging ); else this->SetValueImpl( InternalPowerStateEnum::Unknown ); } else { this->SetValueImpl( InternalPowerStateEnum::Unknown ); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -