Skip to content

Commit

Permalink
added battery reading for UPower.
Browse files Browse the repository at this point in the history
We read all power sources and assume that IsRechargeable repesents a
battery in the system. All Percentage values of that devices are counted
together and divided by the number of found sources.
  • Loading branch information
huceke committed Jul 13, 2011
1 parent 07b733d commit 671de9d
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
75 changes: 74 additions & 1 deletion xbmc/powermanagement/linux/ConsoleUPowerSyscall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,38 @@
#ifdef HAS_DBUS
#include "Application.h"

CUPowerSource::CUPowerSource(const char *powerSource)
{
if(powerSource == NULL)
m_powerSource = "";
else
m_powerSource = powerSource;

CVariant properties = CDBusUtil::GetAll("org.freedesktop.UPower", m_powerSource.c_str(), "org.freedesktop.UPower.Device");
m_isRechargeable = properties["IsRechargeable"].asBoolean();
Update();
}

CUPowerSource::~CUPowerSource()
{
}

void CUPowerSource::Update()
{
CVariant properties = CDBusUtil::GetAll("org.freedesktop.UPower", m_powerSource.c_str(), "org.freedesktop.UPower.Device");
m_batteryLevel = properties["Percentage"].asDouble();
}

bool CUPowerSource::IsRechargeable()
{
return m_isRechargeable;
}

double CUPowerSource::BatteryLevel()
{
return m_batteryLevel;
}

CConsoleUPowerSyscall::CConsoleUPowerSyscall()
{
CLog::Log(LOGINFO, "Selected UPower and ConsoleKit as PowerSyscall");
Expand Down Expand Up @@ -57,6 +89,8 @@ CConsoleUPowerSyscall::CConsoleUPowerSyscall()
m_CanReboot = ConsoleKitMethodCall("CanRestart");

UpdateUPower();

EnumeratePowerSources();
}

CConsoleUPowerSyscall::~CConsoleUPowerSyscall()
Expand Down Expand Up @@ -122,7 +156,46 @@ bool CConsoleUPowerSyscall::CanReboot()

int CConsoleUPowerSyscall::BatteryLevel()
{
return 0;
unsigned int nBatteryCount = 0;
double subCapacity = 0;
double batteryLevel = 0;

std::list<CUPowerSource>::iterator itr;
for (itr = m_powerSources.begin(); itr != m_powerSources.end(); ++itr)
{
itr->Update();
if(itr->IsRechargeable())
{
nBatteryCount++;
subCapacity += itr->BatteryLevel();
}
}

if(nBatteryCount)
batteryLevel = subCapacity / (double)nBatteryCount;

return (int) batteryLevel;
}

void CConsoleUPowerSyscall::EnumeratePowerSources()
{
CDBusMessage message("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", "EnumerateDevices");
DBusMessage *reply = message.SendSystem();
if (reply)
{
char** source = NULL;
int length = 0;

if (dbus_message_get_args (reply, NULL, DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &source, &length, DBUS_TYPE_INVALID))
{
for (int i = 0; i < length; i++)
{
m_powerSources.push_back(CUPowerSource(source[i]));
}

dbus_free_string_array(source);
}
}
}

bool CConsoleUPowerSyscall::HasDeviceConsoleKit()
Expand Down
22 changes: 22 additions & 0 deletions xbmc/powermanagement/linux/ConsoleUPowerSyscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,25 @@
#ifdef HAS_DBUS
#include "powermanagement/IPowerSyscall.h"
#include "DBusUtil.h"
#include "utils/StdString.h"

#include <list>

class CUPowerSource
{
public:
CUPowerSource(const char *powerSource);
~CUPowerSource();

void Update();
bool IsRechargeable();
double BatteryLevel();

private:
CStdString m_powerSource;
bool m_isRechargeable;
double m_batteryLevel;
};

class CConsoleUPowerSyscall : public IPowerSyscall
{
Expand Down Expand Up @@ -54,6 +73,9 @@ class CConsoleUPowerSyscall : public IPowerSyscall

bool m_lowBattery;

void EnumeratePowerSources();
std::list<CUPowerSource> m_powerSources;

DBusConnection *m_connection;
DBusError m_error;
};
Expand Down

0 comments on commit 671de9d

Please sign in to comment.