2011-11-03 14:33:31 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-03 14:33:31 +04:00
|
|
|
|
2014-09-24 17:23:18 +04:00
|
|
|
#include "Hal.h"
|
|
|
|
#include "HalLog.h"
|
2011-11-03 14:33:31 +04:00
|
|
|
#include <dbus/dbus-glib.h>
|
|
|
|
#include <dbus/dbus-glib-lowlevel.h>
|
2016-01-21 08:34:41 +03:00
|
|
|
#include <mozilla/Attributes.h>
|
2011-11-03 14:33:31 +04:00
|
|
|
#include <mozilla/dom/battery/Constants.h>
|
|
|
|
#include "nsAutoRef.h"
|
2015-01-21 17:06:40 +03:00
|
|
|
#include <cmath>
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper that manages the destruction of glib objects as soon as they leave
|
|
|
|
* the current scope.
|
|
|
|
*
|
|
|
|
* We are specializing nsAutoRef class.
|
|
|
|
*/
|
|
|
|
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2011-11-03 14:33:31 +04:00
|
|
|
class nsAutoRefTraits<GHashTable> : public nsPointerRefTraits<GHashTable>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static void Release(GHashTable* ptr) { g_hash_table_unref(ptr); }
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace mozilla::dom::battery;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace hal_impl {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the declaration of UPowerClient class. This class is listening and
|
|
|
|
* communicating to upower daemon through DBus.
|
|
|
|
* There is no header file because this class shouldn't be public.
|
|
|
|
*/
|
|
|
|
class UPowerClient
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static UPowerClient* GetInstance();
|
|
|
|
|
|
|
|
void BeginListening();
|
|
|
|
void StopListening();
|
|
|
|
|
2011-11-09 12:56:22 +04:00
|
|
|
double GetLevel();
|
|
|
|
bool IsCharging();
|
2011-11-09 13:00:01 +04:00
|
|
|
double GetRemainingTime();
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
~UPowerClient();
|
|
|
|
|
|
|
|
private:
|
|
|
|
UPowerClient();
|
|
|
|
|
|
|
|
enum States {
|
|
|
|
eState_Unknown = 0,
|
|
|
|
eState_Charging,
|
|
|
|
eState_Discharging,
|
|
|
|
eState_Empty,
|
|
|
|
eState_FullyCharged,
|
|
|
|
eState_PendingCharge,
|
|
|
|
eState_PendingDischarge
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the currently tracked device.
|
|
|
|
* @return whether everything went ok.
|
|
|
|
*/
|
2012-11-03 00:11:50 +04:00
|
|
|
void UpdateTrackedDeviceSync();
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a hash table with the properties of aDevice.
|
|
|
|
* Note: the caller has to unref the hash table.
|
|
|
|
*/
|
2012-11-03 00:11:50 +04:00
|
|
|
GHashTable* GetDevicePropertiesSync(DBusGProxy* aProxy);
|
|
|
|
void GetDevicePropertiesAsync(DBusGProxy* aProxy);
|
|
|
|
static void GetDevicePropertiesCallback(DBusGProxy* aProxy,
|
|
|
|
DBusGProxyCall* aCall,
|
|
|
|
void* aData);
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Using the device properties (aHashTable), this method updates the member
|
|
|
|
* variable storing the values we care about.
|
|
|
|
*/
|
|
|
|
void UpdateSavedInfo(GHashTable* aHashTable);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback used by 'DeviceChanged' signal.
|
|
|
|
*/
|
|
|
|
static void DeviceChanged(DBusGProxy* aProxy, const gchar* aObjectPath,
|
|
|
|
UPowerClient* aListener);
|
|
|
|
|
2016-11-02 12:37:11 +03:00
|
|
|
/**
|
|
|
|
* Callback used by 'PropertiesChanged' signal.
|
|
|
|
* This method is called when the the battery level changes.
|
|
|
|
* (Only with upower >= 0.99)
|
|
|
|
*/
|
|
|
|
static void PropertiesChanged(DBusGProxy* aProxy, const gchar*,
|
|
|
|
GHashTable*, char**,
|
|
|
|
UPowerClient* aListener);
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
/**
|
|
|
|
* Callback called when mDBusConnection gets a signal.
|
|
|
|
*/
|
|
|
|
static DBusHandlerResult ConnectionSignalFilter(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage,
|
|
|
|
void* aData);
|
|
|
|
|
|
|
|
// The DBus connection object.
|
|
|
|
DBusGConnection* mDBusConnection;
|
|
|
|
|
|
|
|
// The DBus proxy object to upower.
|
|
|
|
DBusGProxy* mUPowerProxy;
|
|
|
|
|
|
|
|
// The path of the tracked device.
|
|
|
|
gchar* mTrackedDevice;
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
// The DBusGProxy for the tracked device.
|
|
|
|
DBusGProxy* mTrackedDeviceProxy;
|
|
|
|
|
2011-11-09 12:56:22 +04:00
|
|
|
double mLevel;
|
2011-11-03 14:33:31 +04:00
|
|
|
bool mCharging;
|
2011-11-09 13:00:01 +04:00
|
|
|
double mRemainingTime;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
static UPowerClient* sInstance;
|
|
|
|
|
|
|
|
static const guint sDeviceTypeBattery = 2;
|
2011-11-09 13:00:01 +04:00
|
|
|
static const guint64 kUPowerUnknownRemainingTime = 0;
|
2011-11-03 14:33:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implementation of mozilla::hal_impl::EnableBatteryNotifications,
|
|
|
|
* mozilla::hal_impl::DisableBatteryNotifications,
|
|
|
|
* and mozilla::hal_impl::GetCurrentBatteryInformation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
EnableBatteryNotifications()
|
|
|
|
{
|
|
|
|
UPowerClient::GetInstance()->BeginListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
DisableBatteryNotifications()
|
|
|
|
{
|
|
|
|
UPowerClient::GetInstance()->StopListening();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
|
|
|
|
{
|
|
|
|
UPowerClient* upowerClient = UPowerClient::GetInstance();
|
|
|
|
|
|
|
|
aBatteryInfo->level() = upowerClient->GetLevel();
|
|
|
|
aBatteryInfo->charging() = upowerClient->IsCharging();
|
2011-11-09 13:00:01 +04:00
|
|
|
aBatteryInfo->remainingTime() = upowerClient->GetRemainingTime();
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Following is the implementation of UPowerClient.
|
|
|
|
*/
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
UPowerClient* UPowerClient::sInstance = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
/* static */ UPowerClient*
|
|
|
|
UPowerClient::GetInstance()
|
|
|
|
{
|
|
|
|
if (!sInstance) {
|
|
|
|
sInstance = new UPowerClient();
|
|
|
|
}
|
|
|
|
|
|
|
|
return sInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
UPowerClient::UPowerClient()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mDBusConnection(nullptr)
|
|
|
|
, mUPowerProxy(nullptr)
|
|
|
|
, mTrackedDevice(nullptr)
|
2012-11-03 00:11:50 +04:00
|
|
|
, mTrackedDeviceProxy(nullptr)
|
2011-11-03 14:33:31 +04:00
|
|
|
, mLevel(kDefaultLevel)
|
|
|
|
, mCharging(kDefaultCharging)
|
2011-12-01 14:50:58 +04:00
|
|
|
, mRemainingTime(kDefaultRemainingTime)
|
2011-11-03 14:33:31 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UPowerClient::~UPowerClient()
|
|
|
|
{
|
2012-11-03 00:11:50 +04:00
|
|
|
NS_ASSERTION(!mDBusConnection && !mUPowerProxy && !mTrackedDevice && !mTrackedDeviceProxy,
|
2011-11-03 14:33:31 +04:00
|
|
|
"The observers have not been correctly removed! "
|
|
|
|
"(StopListening should have been called)");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UPowerClient::BeginListening()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
GError* error = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
mDBusConnection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
|
|
|
|
|
|
|
|
if (!mDBusConnection) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Failed to open connection to bus: %s\n", error->message);
|
2011-11-03 14:33:31 +04:00
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DBusConnection* dbusConnection =
|
|
|
|
dbus_g_connection_get_connection(mDBusConnection);
|
|
|
|
|
|
|
|
// Make sure we do not exit the entire program if DBus connection get lost.
|
|
|
|
dbus_connection_set_exit_on_disconnect(dbusConnection, false);
|
|
|
|
|
|
|
|
// Listening to signals the DBus connection is going to get so we will know
|
|
|
|
// when it is lost and we will be able to disconnect cleanly.
|
|
|
|
dbus_connection_add_filter(dbusConnection, ConnectionSignalFilter, this,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr);
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
mUPowerProxy = dbus_g_proxy_new_for_name(mDBusConnection,
|
|
|
|
"org.freedesktop.UPower",
|
|
|
|
"/org/freedesktop/UPower",
|
|
|
|
"org.freedesktop.UPower");
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
UpdateTrackedDeviceSync();
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: we should probably listen to DeviceAdded and DeviceRemoved signals.
|
|
|
|
* If we do that, we would have to disconnect from those in StopListening.
|
|
|
|
* It's not yet implemented because it requires testing hot plugging and
|
|
|
|
* removal of a battery.
|
|
|
|
*/
|
|
|
|
dbus_g_proxy_add_signal(mUPowerProxy, "DeviceChanged", G_TYPE_STRING,
|
|
|
|
G_TYPE_INVALID);
|
|
|
|
dbus_g_proxy_connect_signal(mUPowerProxy, "DeviceChanged",
|
2012-07-30 18:20:58 +04:00
|
|
|
G_CALLBACK (DeviceChanged), this, nullptr);
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UPowerClient::StopListening()
|
|
|
|
{
|
|
|
|
// If mDBusConnection isn't initialized, that means we are not really listening.
|
|
|
|
if (!mDBusConnection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbus_connection_remove_filter(
|
|
|
|
dbus_g_connection_get_connection(mDBusConnection),
|
|
|
|
ConnectionSignalFilter, this);
|
|
|
|
|
|
|
|
dbus_g_proxy_disconnect_signal(mUPowerProxy, "DeviceChanged",
|
|
|
|
G_CALLBACK (DeviceChanged), this);
|
|
|
|
|
|
|
|
g_free(mTrackedDevice);
|
2012-07-30 18:20:58 +04:00
|
|
|
mTrackedDevice = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
if (mTrackedDeviceProxy) {
|
2016-11-02 12:37:11 +03:00
|
|
|
dbus_g_proxy_disconnect_signal(mTrackedDeviceProxy, "PropertiesChanged",
|
|
|
|
G_CALLBACK (PropertiesChanged), this);
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
g_object_unref(mTrackedDeviceProxy);
|
|
|
|
mTrackedDeviceProxy = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
g_object_unref(mUPowerProxy);
|
2012-07-30 18:20:58 +04:00
|
|
|
mUPowerProxy = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
dbus_g_connection_unref(mDBusConnection);
|
2012-07-30 18:20:58 +04:00
|
|
|
mDBusConnection = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
// We should now show the default values, not the latest we got.
|
|
|
|
mLevel = kDefaultLevel;
|
|
|
|
mCharging = kDefaultCharging;
|
2011-12-01 14:50:58 +04:00
|
|
|
mRemainingTime = kDefaultRemainingTime;
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-03 00:11:50 +04:00
|
|
|
UPowerClient::UpdateTrackedDeviceSync()
|
2011-11-03 14:33:31 +04:00
|
|
|
{
|
|
|
|
GType typeGPtrArray = dbus_g_type_get_collection("GPtrArray",
|
|
|
|
DBUS_TYPE_G_OBJECT_PATH);
|
2012-07-30 18:20:58 +04:00
|
|
|
GPtrArray* devices = nullptr;
|
|
|
|
GError* error = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
// Reset the current tracked device:
|
|
|
|
g_free(mTrackedDevice);
|
|
|
|
mTrackedDevice = nullptr;
|
|
|
|
|
|
|
|
// Reset the current tracked device proxy:
|
|
|
|
if (mTrackedDeviceProxy) {
|
2016-11-02 12:37:11 +03:00
|
|
|
dbus_g_proxy_disconnect_signal(mTrackedDeviceProxy, "PropertiesChanged",
|
|
|
|
G_CALLBACK (PropertiesChanged), this);
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
g_object_unref(mTrackedDeviceProxy);
|
|
|
|
mTrackedDeviceProxy = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
// If that fails, that likely means upower isn't installed.
|
|
|
|
if (!dbus_g_proxy_call(mUPowerProxy, "EnumerateDevices", &error, G_TYPE_INVALID,
|
|
|
|
typeGPtrArray, &devices, G_TYPE_INVALID)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Error: %s\n", error->message);
|
2011-11-03 14:33:31 +04:00
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We are looking for the first device that is a battery.
|
|
|
|
* TODO: we could try to combine more than one battery.
|
|
|
|
*/
|
|
|
|
for (guint i=0; i<devices->len; ++i) {
|
|
|
|
gchar* devicePath = static_cast<gchar*>(g_ptr_array_index(devices, i));
|
2012-11-03 00:11:50 +04:00
|
|
|
|
|
|
|
DBusGProxy* proxy = dbus_g_proxy_new_from_proxy(mUPowerProxy,
|
|
|
|
"org.freedesktop.DBus.Properties",
|
|
|
|
devicePath);
|
|
|
|
|
|
|
|
nsAutoRef<GHashTable> hashTable(GetDevicePropertiesSync(proxy));
|
2011-11-03 14:33:31 +04:00
|
|
|
|
|
|
|
if (g_value_get_uint(static_cast<const GValue*>(g_hash_table_lookup(hashTable, "Type"))) == sDeviceTypeBattery) {
|
|
|
|
UpdateSavedInfo(hashTable);
|
|
|
|
mTrackedDevice = devicePath;
|
2012-11-03 00:11:50 +04:00
|
|
|
mTrackedDeviceProxy = proxy;
|
2011-11-03 14:33:31 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
g_object_unref(proxy);
|
2011-11-03 14:33:31 +04:00
|
|
|
g_free(devicePath);
|
|
|
|
}
|
|
|
|
|
2016-11-02 12:37:11 +03:00
|
|
|
if (mTrackedDeviceProxy) {
|
|
|
|
dbus_g_proxy_add_signal(mTrackedDeviceProxy, "PropertiesChanged",
|
|
|
|
G_TYPE_STRING,
|
|
|
|
dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
|
|
|
|
G_TYPE_VALUE),
|
|
|
|
G_TYPE_STRV, G_TYPE_INVALID);
|
|
|
|
dbus_g_proxy_connect_signal(mTrackedDeviceProxy, "PropertiesChanged",
|
|
|
|
G_CALLBACK (PropertiesChanged), this, nullptr);
|
|
|
|
}
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
g_ptr_array_free(devices, true);
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2016-11-02 12:37:11 +03:00
|
|
|
UPowerClient::DeviceChanged(DBusGProxy* aProxy, const gchar* aObjectPath,
|
|
|
|
UPowerClient* aListener)
|
2011-11-03 14:33:31 +04:00
|
|
|
{
|
2012-11-03 00:11:50 +04:00
|
|
|
if (!aListener->mTrackedDevice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
#if GLIB_MAJOR_VERSION >= 2 && GLIB_MINOR_VERSION >= 16
|
|
|
|
if (g_strcmp0(aObjectPath, aListener->mTrackedDevice)) {
|
|
|
|
#else
|
|
|
|
if (g_ascii_strcasecmp(aObjectPath, aListener->mTrackedDevice)) {
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
aListener->GetDevicePropertiesAsync(aListener->mTrackedDeviceProxy);
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 12:37:11 +03:00
|
|
|
/* static */ void
|
|
|
|
UPowerClient::PropertiesChanged(DBusGProxy* aProxy, const gchar*, GHashTable*,
|
|
|
|
char**, UPowerClient* aListener)
|
|
|
|
{
|
|
|
|
aListener->GetDevicePropertiesAsync(aListener->mTrackedDeviceProxy);
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
/* static */ DBusHandlerResult
|
|
|
|
UPowerClient::ConnectionSignalFilter(DBusConnection* aConnection,
|
|
|
|
DBusMessage* aMessage, void* aData)
|
|
|
|
{
|
|
|
|
if (dbus_message_is_signal(aMessage, DBUS_INTERFACE_LOCAL, "Disconnected")) {
|
|
|
|
static_cast<UPowerClient*>(aData)->StopListening();
|
|
|
|
// We do not return DBUS_HANDLER_RESULT_HANDLED here because the connection
|
|
|
|
// might be shared and some other filters might want to do something.
|
|
|
|
}
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
GHashTable*
|
2012-11-03 00:11:50 +04:00
|
|
|
UPowerClient::GetDevicePropertiesSync(DBusGProxy* aProxy)
|
2011-11-03 14:33:31 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
GError* error = nullptr;
|
|
|
|
GHashTable* hashTable = nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
GType typeGHashTable = dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
|
|
|
|
G_TYPE_VALUE);
|
2012-11-03 00:11:50 +04:00
|
|
|
if (!dbus_g_proxy_call(aProxy, "GetAll", &error, G_TYPE_STRING,
|
2011-11-03 14:33:31 +04:00
|
|
|
"org.freedesktop.UPower.Device", G_TYPE_INVALID,
|
|
|
|
typeGHashTable, &hashTable, G_TYPE_INVALID)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Error: %s\n", error->message);
|
2011-11-03 14:33:31 +04:00
|
|
|
g_error_free(error);
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return hashTable;
|
|
|
|
}
|
|
|
|
|
2012-11-03 00:11:50 +04:00
|
|
|
/* static */ void
|
|
|
|
UPowerClient::GetDevicePropertiesCallback(DBusGProxy* aProxy,
|
|
|
|
DBusGProxyCall* aCall, void* aData)
|
|
|
|
{
|
|
|
|
GError* error = nullptr;
|
|
|
|
GHashTable* hashTable = nullptr;
|
|
|
|
GType typeGHashTable = dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
|
|
|
|
G_TYPE_VALUE);
|
|
|
|
if (!dbus_g_proxy_end_call(aProxy, aCall, &error, typeGHashTable,
|
|
|
|
&hashTable, G_TYPE_INVALID)) {
|
2014-09-24 17:23:18 +04:00
|
|
|
HAL_LOG("Error: %s\n", error->message);
|
2012-11-03 00:11:50 +04:00
|
|
|
g_error_free(error);
|
|
|
|
} else {
|
|
|
|
sInstance->UpdateSavedInfo(hashTable);
|
|
|
|
hal::NotifyBatteryChange(hal::BatteryInformation(sInstance->mLevel,
|
|
|
|
sInstance->mCharging,
|
|
|
|
sInstance->mRemainingTime));
|
|
|
|
g_hash_table_unref(hashTable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
UPowerClient::GetDevicePropertiesAsync(DBusGProxy* aProxy)
|
|
|
|
{
|
|
|
|
dbus_g_proxy_begin_call(aProxy, "GetAll", GetDevicePropertiesCallback, nullptr,
|
|
|
|
nullptr, G_TYPE_STRING,
|
|
|
|
"org.freedesktop.UPower.Device", G_TYPE_INVALID);
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
void
|
|
|
|
UPowerClient::UpdateSavedInfo(GHashTable* aHashTable)
|
|
|
|
{
|
2011-11-09 13:00:01 +04:00
|
|
|
bool isFull = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* State values are confusing...
|
|
|
|
* First of all, after looking at upower sources (0.9.13), it seems that
|
|
|
|
* PendingDischarge and PendingCharge are not used.
|
|
|
|
* In addition, FullyCharged and Empty states are not clear because we do not
|
|
|
|
* know if the battery is actually charging or not. Those values come directly
|
|
|
|
* from sysfs (in the Linux kernel) which have four states: "Empty", "Full",
|
|
|
|
* "Charging" and "Discharging". In sysfs, "Empty" and "Full" are also only
|
|
|
|
* related to the level, not to the charging state.
|
|
|
|
* In this code, we are going to assume that Full means charging and Empty
|
|
|
|
* means discharging because if that is not the case, the state should not
|
|
|
|
* last a long time (actually, it should disappear at the following update).
|
|
|
|
* It might be even very hard to see real cases where the state is Empty and
|
|
|
|
* the battery is charging or the state is Full and the battery is discharging
|
|
|
|
* given that plugging/unplugging the battery should have an impact on the
|
|
|
|
* level.
|
|
|
|
*/
|
2011-11-03 14:33:31 +04:00
|
|
|
switch (g_value_get_uint(static_cast<const GValue*>(g_hash_table_lookup(aHashTable, "State")))) {
|
|
|
|
case eState_Unknown:
|
|
|
|
mCharging = kDefaultCharging;
|
|
|
|
break;
|
|
|
|
case eState_FullyCharged:
|
2011-11-09 13:00:01 +04:00
|
|
|
isFull = true;
|
2016-01-21 08:34:41 +03:00
|
|
|
MOZ_FALLTHROUGH;
|
2011-11-09 13:00:01 +04:00
|
|
|
case eState_Charging:
|
2011-11-03 14:33:31 +04:00
|
|
|
case eState_PendingCharge:
|
|
|
|
mCharging = true;
|
|
|
|
break;
|
|
|
|
case eState_Discharging:
|
|
|
|
case eState_Empty:
|
|
|
|
case eState_PendingDischarge:
|
|
|
|
mCharging = false;
|
|
|
|
break;
|
|
|
|
}
|
2011-11-09 13:00:01 +04:00
|
|
|
|
2011-11-22 13:36:22 +04:00
|
|
|
/*
|
2015-01-21 17:06:40 +03:00
|
|
|
* The battery level might be very close to 100% (like 99%) without
|
2011-11-22 13:36:22 +04:00
|
|
|
* increasing. It seems that upower sets the battery state as 'full' in that
|
|
|
|
* case so we should trust it and not even try to get the value.
|
|
|
|
*/
|
|
|
|
if (isFull) {
|
|
|
|
mLevel = 1.0;
|
|
|
|
} else {
|
2015-01-21 17:06:40 +03:00
|
|
|
mLevel = round(g_value_get_double(static_cast<const GValue*>(g_hash_table_lookup(aHashTable, "Percentage"))))*0.01;
|
2011-11-22 13:36:22 +04:00
|
|
|
}
|
|
|
|
|
2011-11-09 13:00:01 +04:00
|
|
|
if (isFull) {
|
|
|
|
mRemainingTime = 0;
|
|
|
|
} else {
|
|
|
|
mRemainingTime = mCharging ? g_value_get_int64(static_cast<const GValue*>(g_hash_table_lookup(aHashTable, "TimeToFull")))
|
|
|
|
: g_value_get_int64(static_cast<const GValue*>(g_hash_table_lookup(aHashTable, "TimeToEmpty")));
|
|
|
|
|
|
|
|
if (mRemainingTime == kUPowerUnknownRemainingTime) {
|
|
|
|
mRemainingTime = kUnknownRemainingTime;
|
|
|
|
}
|
|
|
|
}
|
2011-11-03 14:33:31 +04:00
|
|
|
}
|
|
|
|
|
2011-11-09 12:56:22 +04:00
|
|
|
double
|
2011-11-03 14:33:31 +04:00
|
|
|
UPowerClient::GetLevel()
|
|
|
|
{
|
|
|
|
return mLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
UPowerClient::IsCharging()
|
|
|
|
{
|
|
|
|
return mCharging;
|
|
|
|
}
|
|
|
|
|
2011-11-09 13:00:01 +04:00
|
|
|
double
|
|
|
|
UPowerClient::GetRemainingTime()
|
|
|
|
{
|
|
|
|
return mRemainingTime;
|
|
|
|
}
|
|
|
|
|
2011-11-03 14:33:31 +04:00
|
|
|
} // namespace hal_impl
|
|
|
|
} // namespace mozilla
|