📄 intelmobilebatteryclassprovider.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.
*///==============================================================================// IntelMobileBatteryClassProvider.cpp//// Defines the class for the default battery provider for IntelMobile. This module// contains the methods for the public interfaces of the inherited classes.////==============================================================================//#include "stdafx.h"#include "IntelMobileBatteryClassProvider.h"#define S_FALSE 0//typedef wchar_t BSTR;typedef long HRESULT;BatteryClassProvider LocalClassProvider;//==============================================================================PROVIDER_API IClassProvider* CreateClassProvider( /*HWND& hWnd*/ ){// LocalClassProvider.SetWindowHandle( hWnd ); return &LocalClassProvider;}//==============================================================================BatteryClassProvider::~BatteryClassProvider(){ Unload(); try { DeleteCriticalSection( &m_DefaultProviderCS ); } catch(...) { THROWIMEXCEPTION("80043041"); }}bool BatteryClassProvider::Load(){ bool bRet = true; HRESULT hRet = S_FALSE; // printf("Battery instance load !\n"); bRet = Scan(); // Must do this after Scanned because we may get messages and notifications // before we have any devices in the system if ( bRet ) { // if ( InterlockedExchange ( &m_lWndNotificationCreated, 1 ) == 0 ) { RegisterForHwndDeviceNotifications(); } } return bRet;}bool BatteryClassProvider::Unload(){ return true;}bool BatteryClassProvider::Refresh(){ try { bool bRet = false; Lock(); /* int i; BATTERY_DEVICE_LIST DeviceList; DWORD ret; ULONG DevId; ret = GetBatteryInstances( &DeviceList ); int deviceCount = (int)DeviceList.Count; if (deviceCount >= 0) { wchar_t tags [100][10]; // IntelMobileString * tags = new IntelMobileString[(int)DeviceList.Count]; int * tagflags = new int[deviceCount+1]; for (i=0; i<deviceCount; i++) { if ( DeviceList.Device[i].Tag > 0 ) { tagflags[i] = 1; } else { tagflags[i] = 0; } DevId = (i+1)*10000000 + DeviceList.Device[i].Tag; //added 1 to have all key and ID being a large number // wsprintf(tags[i], _T("%d"),DevId); // wprintf(L"Battery[%d]:%ld, %s\n", i+1, DevId, tags[i]); } //ServerBatteryClassObject& MyBatterys = GetBatteryClass(); ServerInstanceCollection* pBatteryCollection = GetClassObject().GetInstances(); if ( pBatteryCollection != NULL ) { long iChangedIndex = 0; bool bHasNext = false; pBatteryCollection->Reset( &iChangedIndex ); int iEnumIndex = 0; if ( bHasNext = pBatteryCollection->HasNext( iEnumIndex, iChangedIndex ) ) { // remove all non-exist instance ServerInstanceObject* pInstance = NULL; while ( bHasNext ) { if ( !pBatteryCollection->Next( &pInstance, &iEnumIndex, iChangedIndex ) ) { break; } IntelMobileString sKey = pInstance->GetKey(); int j = 0; for (; j<deviceCount; j++) { if ( sKey.compare( tags[j] ) == 0 ) { tagflags[j] = 0; break; } } if (j == deviceCount) { // wcout<< "Remove Battery"; wcout << sKey.c_str() << endl; // wprintf(L"Remove Battery %s\n", sKey.c_str()); pBatteryCollection->Remove( pInstance ); pBatteryCollection->Reset( &iChangedIndex ); iEnumIndex --; } bHasNext = pBatteryCollection->HasNext( iEnumIndex, iChangedIndex ); } } for (i=0; i<deviceCount; i++) { if ( tagflags[i] ) { // wcout<< "Add Battery"; wcout << tags[i] << endl; // wprintf(L"Add Battery %s\n", tags[i] ); CreateInstanceBattery ( tags[i] ); } } bRet = true; } delete[] tagflags; } */ Unlock(); return bRet; } catch(ServerException se) { Unlock(); if (se.IfServerBaseObjectException()) { ServerException nse(IntelMobileText("80043042")); nse.SetParent( &se ); throw nse; } throw se; } catch(...) { Unlock(); THROWIMEXCEPTION("80043042"); }// return true;}bool BatteryClassProvider::Scan(){_Log(IntelMobileText("Initializing Batterys")); try { bool bRet = true; Lock();/* int i; FILE *fp; int max=1; for(int i=0;i<max;i++) { fp=fopen("/proc/acpi/battery/BAT0/info","r"); if(!fp) { fp=fopen("/proc/acpi/battery/BAT0/state","r"); } if(!fp) return false; CreateInstance(IntelMobileText("battery"),IntelMobileText("1")); } fclose(fp); */ char *prefix="/proc/acpi/battery/BAT"; char *name_info="/info"; char *name_state="/state"; int max=5; char num[5]=""; for(int i=0; i<max; i++) { char *pathName=new char[32]; memset(pathName, 0, sizeof(pathName)); //memcpy(pathName, prefix, sizeof(prefix)); strcpy(pathName, prefix); sprintf(num, "%d", i); strcat(pathName, num); strcat(pathName, name_info); printf("pathName is %s\n", pathName); if(access(pathName, R_OK)==0) CreateInstance(IntelMobileText("battery"),IntelMobileText("1")); delete[] pathName; }//CreateInstanceBattery(L"1");//CreateInstance(L"mainbattery",L"1"); //CreateInstance(L"mainbattery",L"mainbattery"); //CreateInstance(L"backupbattery",L"100000"); Unlock();_Log(IntelMobileText("Battery init complete")); return bRet; } catch(ServerException se) {//cout<<"create failed"<<endl; Unlock(); if (se.IfServerBaseObjectException()) { ServerException nse(IntelMobileText("80043043")); nse.SetParent( &se ); throw nse; } throw se; } catch(...) {//cout<<"create failed"<<endl; Unlock(); THROWIMEXCEPTION("80043043"); }}bool BatteryClassProvider::WindowNotification(HWND m_hWnd, UINT iMsg, WPARAM wParam, LPARAM lParam){ bool bRet = true; wchar_t wsMessage[1000]; //wsprintf( wsMessage, L"Trace HandleProcessorMessage(): param=0x%08X", lParam ); //_Log( LOG_DEBUG_DEVICE_DEVICE, wsMessage ); HRESULT hr = S_FALSE; TCHAR* regname = NULL; switch ( iMsg ) {// case WM_DEVICECHANGE :// PDEV_BROADCAST_HDR pDevBroadcastHdr;/// PDEV_BROADCAST_DEVICEINTERFACE pDevBroadcastInterface;// switch ( wParam )// {// case DBT_CONFIGCHANGECANCELED:// //_Log( LOG_DEBUG_DEVICE_DEVICE, L"Received DBT_CONFIGCHANGECANCELED" );// bRet = true;// hr = S_OK;// break;// case DBT_CONFIGCHANGED: //_Log( LOG_DEBUG_DEVICE_DEVICE, L"Received DBT_CONFIGCHANGED" );// bRet = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -