feature: get rid of DkpObject and instead use the GObject property system correctly

This commit is contained in:
Richard Hughes 2009-02-27 11:59:27 +00:00
parent 6f0dc6a530
commit 5259367a6b
16 changed files with 1616 additions and 1083 deletions

View file

@ -35,9 +35,7 @@ libdevkit_power_la_SOURCES = \
dkp-wakeups-obj.h \
dkp-wakeups-obj.c \
dkp-enum.c \
dkp-enum.h \
dkp-object.c \
dkp-object.h
dkp-enum.h
libdevkit_power_la_LIBADD = $(INTLLIBS) $(GLIB_LIBS) $(DBUS_GLIB_LIBS)

View file

@ -59,6 +59,16 @@ typedef struct
void (*device_removed) (DkpClient *client,
const DkpDevice *device);
void (*changed) (DkpClient *client);
/*< private >*/
/* Padding for future expansion */
void (*_dkp_client_reserved1) (void);
void (*_dkp_client_reserved2) (void);
void (*_dkp_client_reserved3) (void);
void (*_dkp_client_reserved4) (void);
void (*_dkp_client_reserved5) (void);
void (*_dkp_client_reserved6) (void);
void (*_dkp_client_reserved7) (void);
void (*_dkp_client_reserved8) (void);
} DkpClientClass;
GType dkp_client_get_type (void);

View file

@ -25,11 +25,12 @@
#include <stdio.h>
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <string.h>
#include "egg-debug.h"
#include "egg-string.h"
#include "dkp-device.h"
#include "dkp-object.h"
#include "dkp-stats-obj.h"
#include "dkp-history-obj.h"
@ -42,10 +43,63 @@ static void dkp_device_finalize (GObject *object);
struct DkpDevicePrivate
{
gchar *object_path;
DkpObject *obj;
DBusGConnection *bus;
DBusGProxy *proxy_device;
DBusGProxy *proxy_props;
/* properties */
guint64 update_time;
gchar *vendor;
gchar *model;
gchar *serial;
gchar *native_path;
gboolean power_supply;
gboolean online;
gboolean is_present;
gboolean is_rechargeable;
gboolean has_history;
gboolean has_statistics;
DkpDeviceType type;
DkpDeviceState state;
DkpDeviceTechnology technology;
gdouble capacity; /* percent */
gdouble energy; /* Watt Hours */
gdouble energy_empty; /* Watt Hours */
gdouble energy_full; /* Watt Hours */
gdouble energy_full_design; /* Watt Hours */
gdouble energy_rate; /* Watts */
gdouble voltage; /* Volts */
gint64 time_to_empty; /* seconds */
gint64 time_to_full; /* seconds */
gdouble percentage; /* percent */
};
enum {
PROP_0,
PROP_UPDATE_TIME,
PROP_VENDOR,
PROP_MODEL,
PROP_SERIAL,
PROP_NATIVE_PATH,
PROP_POWER_SUPPLY,
PROP_ONLINE,
PROP_IS_PRESENT,
PROP_IS_RECHARGEABLE,
PROP_HAS_HISTORY,
PROP_HAS_STATISTICS,
PROP_TYPE,
PROP_STATE,
PROP_TECHNOLOGY,
PROP_CAPACITY,
PROP_ENERGY,
PROP_ENERGY_EMPTY,
PROP_ENERGY_FULL,
PROP_ENERGY_FULL_DESIGN,
PROP_ENERGY_RATE,
PROP_VOLTAGE,
PROP_TIME_TO_EMPTY,
PROP_TIME_TO_FULL,
PROP_PERCENTAGE
};
enum {
@ -82,6 +136,69 @@ out:
return hash_table;
}
/**
* dkp_device_collect_props_cb:
**/
static void
dkp_device_collect_props_cb (const char *key, const GValue *value, DkpDevice *device)
{
gboolean handled = TRUE;
if (g_strcmp0 (key, "native-path") == 0)
device->priv->native_path = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "vendor") == 0)
device->priv->vendor = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "model") == 0)
device->priv->model = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "serial") == 0)
device->priv->serial = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "update-time") == 0)
device->priv->update_time = g_value_get_uint64 (value);
else if (g_strcmp0 (key, "type") == 0)
device->priv->type = g_value_get_uint (value);
else if (g_strcmp0 (key, "online") == 0)
device->priv->online = g_value_get_boolean (value);
else if (g_strcmp0 (key, "has-history") == 0)
device->priv->has_history = g_value_get_boolean (value);
else if (g_strcmp0 (key, "has-statistics") == 0)
device->priv->has_statistics = g_value_get_boolean (value);
else if (g_strcmp0 (key, "energy") == 0)
device->priv->energy = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-empty") == 0)
device->priv->energy_empty = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-full") == 0)
device->priv->energy_full = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-full-design") == 0)
device->priv->energy_full_design = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-rate") == 0)
device->priv->energy_rate = g_value_get_double (value);
else if (g_strcmp0 (key, "voltage") == 0)
device->priv->voltage = g_value_get_double (value);
else if (g_strcmp0 (key, "time-to-full") == 0)
device->priv->time_to_full = g_value_get_int64 (value);
else if (g_strcmp0 (key, "time-to-empty") == 0)
device->priv->time_to_empty = g_value_get_int64 (value);
else if (g_strcmp0 (key, "percentage") == 0)
device->priv->percentage = g_value_get_double (value);
else if (g_strcmp0 (key, "technology") == 0)
device->priv->technology = g_value_get_uint (value);
else if (g_strcmp0 (key, "is-present") == 0)
device->priv->is_present = g_value_get_boolean (value);
else if (g_strcmp0 (key, "is-rechargeable") == 0)
device->priv->is_rechargeable = g_value_get_boolean (value);
else if (g_strcmp0 (key, "power-supply") == 0)
device->priv->power_supply = g_value_get_boolean (value);
else if (g_strcmp0 (key, "capacity") == 0)
device->priv->capacity = g_value_get_double (value);
else if (g_strcmp0 (key, "state") == 0)
device->priv->state = g_value_get_uint (value);
else
handled = FALSE;
if (!handled)
egg_warning ("unhandled property '%s'", key);
}
/**
* dkp_device_refresh_internal:
**/
@ -96,7 +213,7 @@ dkp_device_refresh_internal (DkpDevice *device)
egg_warning ("Cannot get device properties for %s", device->priv->object_path);
return FALSE;
}
dkp_object_set_from_map (device->priv->obj, hash);
g_hash_table_foreach (hash, (GHFunc) dkp_device_collect_props_cb, device);
g_hash_table_unref (hash);
return TRUE;
}
@ -109,7 +226,7 @@ dkp_device_changed_cb (DBusGProxy *proxy, DkpDevice *device)
{
g_return_if_fail (DKP_IS_DEVICE (device));
dkp_device_refresh_internal (device);
g_signal_emit (device, signals [DKP_DEVICE_CHANGED], 0, device->priv->obj);
g_signal_emit (device, signals [DKP_DEVICE_CHANGED], 0, NULL); //TODO
}
/**
@ -183,16 +300,6 @@ dkp_device_get_object_path (const DkpDevice *device)
return device->priv->object_path;
}
/**
* dkp_device_get_object:
**/
const DkpObject *
dkp_device_get_object (const DkpDevice *device)
{
g_return_val_if_fail (DKP_IS_DEVICE (device), NULL);
return device->priv->obj;
}
/**
* dkp_device_print_history:
**/
@ -222,16 +329,118 @@ out:
return ret;
}
/**
* dkp_device_print_bool_to_text:
**/
static const gchar *
dkp_device_print_bool_to_text (gboolean ret)
{
return ret ? "yes" : "no";
}
/**
* dkp_device_print_time_to_text:
**/
static gchar *
dkp_device_print_time_to_text (gint seconds)
{
gfloat value = seconds;
if (value < 0)
return g_strdup ("unknown");
if (value < 60)
return g_strdup_printf ("%.0f seconds", value);
value /= 60.0;
if (value < 60)
return g_strdup_printf ("%.1f minutes", value);
value /= 60.0;
if (value < 60)
return g_strdup_printf ("%.1f hours", value);
value /= 24.0;
return g_strdup_printf ("%.1f days", value);
}
/**
* dkp_device_print:
**/
gboolean
dkp_device_print (const DkpDevice *device)
{
struct tm *time_tm;
time_t t;
gchar time_buf[256];
gchar *time_str;
g_return_val_if_fail (DKP_IS_DEVICE (device), FALSE);
/* print to screen */
dkp_object_print (device->priv->obj);
/* get a human readable time */
t = (time_t) device->priv->update_time;
time_tm = localtime (&t);
strftime (time_buf, sizeof time_buf, "%c", time_tm);
g_print (" native-path: %s\n", device->priv->native_path);
if (!egg_strzero (device->priv->vendor))
g_print (" vendor: %s\n", device->priv->vendor);
if (!egg_strzero (device->priv->model))
g_print (" model: %s\n", device->priv->model);
if (!egg_strzero (device->priv->serial))
g_print (" serial: %s\n", device->priv->serial);
g_print (" power supply: %s\n", dkp_device_print_bool_to_text (device->priv->power_supply));
g_print (" updated: %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - device->priv->update_time));
g_print (" has history: %s\n", dkp_device_print_bool_to_text (device->priv->has_history));
g_print (" has statistics: %s\n", dkp_device_print_bool_to_text (device->priv->has_statistics));
g_print (" %s\n", dkp_device_type_to_text (device->priv->type));
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
device->priv->type == DKP_DEVICE_TYPE_KEYBOARD ||
device->priv->type == DKP_DEVICE_TYPE_UPS)
g_print (" present: %s\n", dkp_device_print_bool_to_text (device->priv->is_present));
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
device->priv->type == DKP_DEVICE_TYPE_KEYBOARD)
g_print (" rechargeable: %s\n", dkp_device_print_bool_to_text (device->priv->is_rechargeable));
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
device->priv->type == DKP_DEVICE_TYPE_KEYBOARD)
g_print (" state: %s\n", dkp_device_state_to_text (device->priv->state));
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
g_print (" energy: %g Wh\n", device->priv->energy);
g_print (" energy-empty: %g Wh\n", device->priv->energy_empty);
g_print (" energy-full: %g Wh\n", device->priv->energy_full);
g_print (" energy-full-design: %g Wh\n", device->priv->energy_full_design);
}
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MONITOR)
g_print (" energy-rate: %g W\n", device->priv->energy_rate);
if (device->priv->type == DKP_DEVICE_TYPE_UPS ||
device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MONITOR)
g_print (" voltage: %g V\n", device->priv->voltage);
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_UPS) {
if (device->priv->time_to_full >= 0) {
time_str = dkp_device_print_time_to_text (device->priv->time_to_full);
g_print (" time to full: %s\n", time_str);
g_free (time_str);
}
if (device->priv->time_to_empty >= 0) {
time_str = dkp_device_print_time_to_text (device->priv->time_to_empty);
g_print (" time to empty: %s\n", time_str);
g_free (time_str);
}
}
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY ||
device->priv->type == DKP_DEVICE_TYPE_MOUSE ||
device->priv->type == DKP_DEVICE_TYPE_KEYBOARD ||
device->priv->type == DKP_DEVICE_TYPE_UPS)
g_print (" percentage: %g%%\n", device->priv->percentage);
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY)
g_print (" capacity: %g%%\n", device->priv->capacity);
if (device->priv->type == DKP_DEVICE_TYPE_BATTERY)
g_print (" technology: %s\n", dkp_device_technology_to_text (device->priv->technology));
if (device->priv->type == DKP_DEVICE_TYPE_LINE_POWER)
g_print (" online: %s\n", dkp_device_print_bool_to_text (device->priv->online));
/* if we can, get history */
dkp_device_print_history (device, "charge");
@ -289,7 +498,7 @@ dkp_device_get_history (const DkpDevice *device, const gchar *type, guint timesp
dbus_g_type_get_struct("GValueArray",
G_TYPE_UINT,
G_TYPE_DOUBLE,
G_TYPE_STRING,
G_TYPE_UINT,
G_TYPE_INVALID));
/* get compound data */
@ -327,7 +536,7 @@ dkp_device_get_history (const DkpDevice *device, const gchar *type, guint timesp
g_value_unset (gv);
/* 2 */
gv = g_value_array_get_nth (gva, 2);
obj->state = dkp_device_state_from_text (g_value_get_string (gv));
obj->state = g_value_get_uint (gv);
g_value_unset (gv);
g_ptr_array_add (array, obj);
g_value_array_free (gva);
@ -407,6 +616,114 @@ out:
return array;
}
/**
* dkp_device_set_property:
**/
static void
dkp_device_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
// DkpDevice *device = DKP_DEVICE (object);
switch (prop_id) {
// case PROP_SUBSYSTEMS:
// if (device->priv->subsystems != NULL)
// g_strfreev (device->priv->subsystems);
// device->priv->subsystems = g_strdupv (g_value_get_boxed (value));
// break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* dkp_device_get_property:
**/
static void
dkp_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
DkpDevice *device = DKP_DEVICE (object);
switch (prop_id) {
case PROP_UPDATE_TIME:
g_value_set_uint64 (value, device->priv->update_time);
break;
case PROP_VENDOR:
g_value_set_string (value, device->priv->vendor);
break;
case PROP_MODEL:
g_value_set_string (value, device->priv->model);
break;
case PROP_SERIAL:
g_value_set_string (value, device->priv->serial);
break;
case PROP_NATIVE_PATH:
g_value_set_string (value, device->priv->native_path);
break;
case PROP_POWER_SUPPLY:
g_value_set_boolean (value, device->priv->power_supply);
break;
case PROP_ONLINE:
g_value_set_boolean (value, device->priv->online);
break;
case PROP_IS_PRESENT:
g_value_set_boolean (value, device->priv->is_present);
break;
case PROP_IS_RECHARGEABLE:
g_value_set_boolean (value, device->priv->is_rechargeable);
break;
case PROP_HAS_HISTORY:
g_value_set_boolean (value, device->priv->has_history);
break;
case PROP_HAS_STATISTICS:
g_value_set_boolean (value, device->priv->has_statistics);
break;
case PROP_TYPE:
g_value_set_uint (value, device->priv->type);
break;
case PROP_STATE:
g_value_set_uint (value, device->priv->state);
break;
case PROP_TECHNOLOGY:
g_value_set_uint (value, device->priv->technology);
break;
case PROP_CAPACITY:
g_value_set_double (value, device->priv->capacity);
break;
case PROP_ENERGY:
g_value_set_double (value, device->priv->energy);
break;
case PROP_ENERGY_EMPTY:
g_value_set_double (value, device->priv->energy_empty);
break;
case PROP_ENERGY_FULL:
g_value_set_double (value, device->priv->energy_full);
break;
case PROP_ENERGY_FULL_DESIGN:
g_value_set_double (value, device->priv->energy_full_design);
break;
case PROP_ENERGY_RATE:
g_value_set_double (value, device->priv->energy_rate);
break;
case PROP_VOLTAGE:
g_value_set_double (value, device->priv->voltage);
break;
case PROP_TIME_TO_EMPTY:
g_value_set_int64 (value, device->priv->time_to_empty);
break;
case PROP_TIME_TO_FULL:
g_value_set_int64 (value, device->priv->time_to_full);
break;
case PROP_PERCENTAGE:
g_value_set_double (value, device->priv->percentage);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* dkp_device_class_init:
* @klass: The DkpDeviceClass
@ -416,11 +733,13 @@ dkp_device_class_init (DkpDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->finalize = dkp_device_finalize;
object_class->set_property = dkp_device_set_property;
object_class->get_property = dkp_device_get_property;
/**
* PkClient::changed:
* @device: the #DkpDevice instance that emitted the signal
* @obj: the #DkpObject that has changed
* @obj: the #DkpObject that has changed //TODO
*
* The ::changed signal is emitted when the device data has changed.
**/
@ -431,6 +750,219 @@ dkp_device_class_init (DkpDeviceClass *klass)
NULL, NULL, g_cclosure_marshal_VOID__POINTER,
G_TYPE_NONE, 1, G_TYPE_POINTER);
/**
* DkpDevice:update-time:
*/
g_object_class_install_property (object_class,
PROP_UPDATE_TIME,
g_param_spec_uint64 ("update-time",
NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READABLE));
/**
* DkpDevice:vendor:
*/
g_object_class_install_property (object_class,
PROP_VENDOR,
g_param_spec_string ("vendor",
NULL, NULL,
NULL,
G_PARAM_READABLE));
/**
* DkpDevice:model:
*/
g_object_class_install_property (object_class,
PROP_MODEL,
g_param_spec_string ("model",
NULL, NULL,
NULL,
G_PARAM_READABLE));
/**
* DkpDevice:serial:
*/
g_object_class_install_property (object_class,
PROP_SERIAL,
g_param_spec_string ("serial",
NULL, NULL,
NULL,
G_PARAM_READABLE));
/**
* DkpDevice:native-path:
*/
g_object_class_install_property (object_class,
PROP_NATIVE_PATH,
g_param_spec_string ("native-path",
NULL, NULL,
NULL,
G_PARAM_READABLE));
/**
* DkpDevice:power-supply:
*/
g_object_class_install_property (object_class,
PROP_POWER_SUPPLY,
g_param_spec_boolean ("power-supply",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:online:
*/
g_object_class_install_property (object_class,
PROP_ONLINE,
g_param_spec_boolean ("online",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:is-present:
*/
g_object_class_install_property (object_class,
PROP_IS_PRESENT,
g_param_spec_boolean ("is-present",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:is-rechargeable:
*/
g_object_class_install_property (object_class,
PROP_IS_RECHARGEABLE,
g_param_spec_boolean ("is-rechargeable",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:has-history:
*/
g_object_class_install_property (object_class,
PROP_HAS_HISTORY,
g_param_spec_boolean ("has-history",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:has-statistics:
*/
g_object_class_install_property (object_class,
PROP_HAS_STATISTICS,
g_param_spec_boolean ("has-statistics",
NULL, NULL,
FALSE,
G_PARAM_READABLE));
/**
* DkpDevice:type:
*/
g_object_class_install_property (object_class,
PROP_TYPE,
g_param_spec_uint ("type",
NULL, NULL,
0,
DKP_DEVICE_TYPE_UNKNOWN,
DKP_DEVICE_TYPE_UNKNOWN,
G_PARAM_READABLE));
/**
* DkpDevice:state:
*/
g_object_class_install_property (object_class,
PROP_STATE,
g_param_spec_uint ("state",
NULL, NULL,
0,
DKP_DEVICE_STATE_UNKNOWN,
DKP_DEVICE_STATE_UNKNOWN,
G_PARAM_READABLE));
/**
* DkpDevice:technology:
*/
g_object_class_install_property (object_class,
PROP_TECHNOLOGY,
g_param_spec_uint ("technology",
NULL, NULL,
0,
DKP_DEVICE_TECHNOLOGY_UNKNOWN,
DKP_DEVICE_TECHNOLOGY_UNKNOWN,
G_PARAM_READABLE));
/**
* DkpDevice:capacity:
*/
g_object_class_install_property (object_class,
PROP_CAPACITY,
g_param_spec_double ("capacity", NULL, NULL,
0.0, 100.f, 100.0,
G_PARAM_READABLE));
/**
* DkpDevice:energy:
*/
g_object_class_install_property (object_class,
PROP_ENERGY,
g_param_spec_double ("energy", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:energy-empty:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_EMPTY,
g_param_spec_double ("energy-empty", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:energy-full:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_FULL,
g_param_spec_double ("energy-full", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:energy-full-design:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_FULL_DESIGN,
g_param_spec_double ("energy-full-design", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:energy-rate:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_RATE,
g_param_spec_double ("energy-rate", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:voltage:
*/
g_object_class_install_property (object_class,
PROP_VOLTAGE,
g_param_spec_double ("voltage", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READABLE));
/**
* DkpDevice:time-to-empty:
*/
g_object_class_install_property (object_class,
PROP_TIME_TO_EMPTY,
g_param_spec_int64 ("time-to-empty", NULL, NULL,
0, G_MAXINT64, 0,
G_PARAM_READABLE));
/**
* DkpDevice:time-to-full:
*/
g_object_class_install_property (object_class,
PROP_TIME_TO_FULL,
g_param_spec_int64 ("time-to-full", NULL, NULL,
0, G_MAXINT64, 0,
G_PARAM_READABLE));
/**
* DkpDevice:percentage:
*/
g_object_class_install_property (object_class,
PROP_PERCENTAGE,
g_param_spec_double ("percentage", NULL, NULL,
0.0, 100.f, 100.0,
G_PARAM_READABLE));
g_type_class_add_private (klass, sizeof (DkpDevicePrivate));
}
@ -445,7 +977,6 @@ dkp_device_init (DkpDevice *device)
device->priv->object_path = NULL;
device->priv->proxy_device = NULL;
device->priv->proxy_props = NULL;
device->priv->obj = dkp_object_new ();
}
/**
@ -462,7 +993,10 @@ dkp_device_finalize (GObject *object)
device = DKP_DEVICE (object);
g_free (device->priv->object_path);
dkp_object_free (device->priv->obj);
g_free (device->priv->vendor);
g_free (device->priv->model);
g_free (device->priv->serial);
g_free (device->priv->native_path);
if (device->priv->proxy_device != NULL)
g_object_unref (device->priv->proxy_device);
if (device->priv->proxy_props != NULL)

View file

@ -28,7 +28,6 @@
#include <glib-object.h>
#include <libdevkit-power/dkp-enum.h>
#include <libdevkit-power/dkp-object.h>
G_BEGIN_DECLS
@ -53,13 +52,22 @@ typedef struct
{
GObjectClass parent_class;
void (*changed) (DkpDevice *device,
const DkpObject *obj);
gpointer *obj);
/*< private >*/
/* Padding for future expansion */
void (*_dkp_device_reserved1) (void);
void (*_dkp_device_reserved2) (void);
void (*_dkp_device_reserved3) (void);
void (*_dkp_device_reserved4) (void);
void (*_dkp_device_reserved5) (void);
void (*_dkp_device_reserved6) (void);
void (*_dkp_device_reserved7) (void);
void (*_dkp_device_reserved8) (void);
} DkpDeviceClass;
GType dkp_device_get_type (void);
DkpDevice *dkp_device_new (void);
const DkpObject *dkp_device_get_object (const DkpDevice *device);
const gchar *dkp_device_get_object_path (const DkpDevice *device);
gboolean dkp_device_set_object_path (DkpDevice *device,
const gchar *object_path);

View file

@ -152,25 +152,25 @@ dkp_device_technology_to_text (DkpDeviceTechnology technology_enum)
{
const gchar *technology = NULL;
switch (technology_enum) {
case DKP_DEVICE_TECHNOLGY_LITHIUM_ION:
case DKP_DEVICE_TECHNOLOGY_LITHIUM_ION:
technology = "lithium-ion";
break;
case DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER:
case DKP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER:
technology = "lithium-polymer";
break;
case DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE:
case DKP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE:
technology = "lithium-iron-phosphate";
break;
case DKP_DEVICE_TECHNOLGY_LEAD_ACID:
case DKP_DEVICE_TECHNOLOGY_LEAD_ACID:
technology = "lead-acid";
break;
case DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM:
case DKP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM:
technology = "nickel-cadmium";
break;
case DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE:
case DKP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE:
technology = "nickel-metal-hydride";
break;
case DKP_DEVICE_TECHNOLGY_UNKNOWN:
case DKP_DEVICE_TECHNOLOGY_UNKNOWN:
technology = "unknown";
break;
default:
@ -187,20 +187,20 @@ DkpDeviceTechnology
dkp_device_technology_from_text (const gchar *technology)
{
if (technology == NULL)
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
if (egg_strequal (technology, "lithium-ion"))
return DKP_DEVICE_TECHNOLGY_LITHIUM_ION;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_ION;
if (egg_strequal (technology, "lithium-polymer"))
return DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER;
if (egg_strequal (technology, "lithium-iron-phosphate"))
return DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE;
if (egg_strequal (technology, "lead-acid"))
return DKP_DEVICE_TECHNOLGY_LEAD_ACID;
return DKP_DEVICE_TECHNOLOGY_LEAD_ACID;
if (egg_strequal (technology, "nickel-cadmium"))
return DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM;
return DKP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM;
if (egg_strequal (technology, "nickel-metal-hydride"))
return DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE;
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
}
/**

View file

@ -31,6 +31,7 @@
G_BEGIN_DECLS
typedef enum {
DKP_DEVICE_TYPE_UNKNOWN,
DKP_DEVICE_TYPE_LINE_POWER,
DKP_DEVICE_TYPE_BATTERY,
DKP_DEVICE_TYPE_UPS,
@ -39,31 +40,34 @@ typedef enum {
DKP_DEVICE_TYPE_KEYBOARD,
DKP_DEVICE_TYPE_PDA,
DKP_DEVICE_TYPE_PHONE,
DKP_DEVICE_TYPE_UNKNOWN
DKP_DEVICE_TYPE_LAST
} DkpDeviceType;
typedef enum {
DKP_DEVICE_STATE_UNKNOWN,
DKP_DEVICE_STATE_CHARGING,
DKP_DEVICE_STATE_DISCHARGING,
DKP_DEVICE_STATE_EMPTY,
DKP_DEVICE_STATE_FULLY_CHARGED,
DKP_DEVICE_STATE_UNKNOWN
DKP_DEVICE_STATE_LAST
} DkpDeviceState;
typedef enum {
DKP_DEVICE_TECHNOLGY_LITHIUM_ION,
DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER,
DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE,
DKP_DEVICE_TECHNOLGY_LEAD_ACID,
DKP_DEVICE_TECHNOLGY_NICKEL_CADMIUM,
DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE,
DKP_DEVICE_TECHNOLGY_UNKNOWN
DKP_DEVICE_TECHNOLOGY_UNKNOWN,
DKP_DEVICE_TECHNOLOGY_LITHIUM_ION,
DKP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER,
DKP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE,
DKP_DEVICE_TECHNOLOGY_LEAD_ACID,
DKP_DEVICE_TECHNOLOGY_NICKEL_CADMIUM,
DKP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE,
DKP_DEVICE_TECHNOLOGY_LAST
} DkpDeviceTechnology;
typedef enum {
DKP_QOS_TYPE_UNKNOWN,
DKP_QOS_TYPE_NETWORK,
DKP_QOS_TYPE_CPU_DMA,
DKP_QOS_TYPE_UNKNOWN
DKP_QOS_TYPE_LAST
} DkpQosType;
const gchar *dkp_device_type_to_text (DkpDeviceType type_enum);

View file

@ -1,544 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <glib.h>
#include <glib-object.h>
#include <string.h>
#include "egg-debug.h"
#include "egg-string.h"
#include "dkp-enum.h"
#include "dkp-object.h"
/**
* dkp_object_clear_internal:
**/
static void
dkp_object_clear_internal (DkpObject *obj)
{
obj->type = DKP_DEVICE_TYPE_UNKNOWN;
obj->update_time = 0;
obj->energy = 0;
obj->energy_full = 0;
obj->energy_full_design = 0;
obj->energy_rate = 0;
obj->voltage = 0;
obj->percentage = 0;
obj->capacity = 0;
obj->time_to_empty = 0;
obj->time_to_full = 0;
obj->state = DKP_DEVICE_STATE_UNKNOWN;
obj->technology = DKP_DEVICE_TECHNOLGY_UNKNOWN;
obj->vendor = NULL;
obj->model = NULL;
obj->serial = NULL;
obj->native_path = NULL;
obj->online = FALSE;
obj->is_present = FALSE;
obj->power_supply = FALSE;
obj->is_rechargeable = FALSE;
obj->has_history = FALSE;
obj->has_statistics = FALSE;
}
/**
* dkp_object_collect_props:
**/
static void
dkp_object_collect_props (const char *key, const GValue *value, DkpObject *obj)
{
gboolean handled = TRUE;
if (g_strcmp0 (key, "native-path") == 0)
obj->native_path = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "vendor") == 0)
obj->vendor = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "model") == 0)
obj->model = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "serial") == 0)
obj->serial = g_strdup (g_value_get_string (value));
else if (g_strcmp0 (key, "update-time") == 0)
obj->update_time = g_value_get_uint64 (value);
else if (g_strcmp0 (key, "type") == 0)
obj->type = dkp_device_type_from_text (g_value_get_string (value));
else if (g_strcmp0 (key, "online") == 0)
obj->online = g_value_get_boolean (value);
else if (g_strcmp0 (key, "has-history") == 0)
obj->has_history = g_value_get_boolean (value);
else if (g_strcmp0 (key, "has-statistics") == 0)
obj->has_statistics = g_value_get_boolean (value);
else if (g_strcmp0 (key, "energy") == 0)
obj->energy = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-empty") == 0)
obj->energy_empty = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-full") == 0)
obj->energy_full = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-full-design") == 0)
obj->energy_full_design = g_value_get_double (value);
else if (g_strcmp0 (key, "energy-rate") == 0)
obj->energy_rate = g_value_get_double (value);
else if (g_strcmp0 (key, "voltage") == 0)
obj->voltage = g_value_get_double (value);
else if (g_strcmp0 (key, "time-to-full") == 0)
obj->time_to_full = g_value_get_int64 (value);
else if (g_strcmp0 (key, "time-to-empty") == 0)
obj->time_to_empty = g_value_get_int64 (value);
else if (g_strcmp0 (key, "percentage") == 0)
obj->percentage = g_value_get_double (value);
else if (g_strcmp0 (key, "technology") == 0)
obj->technology = dkp_device_technology_from_text (g_value_get_string (value));
else if (g_strcmp0 (key, "is-present") == 0)
obj->is_present = g_value_get_boolean (value);
else if (g_strcmp0 (key, "is-rechargeable") == 0)
obj->is_rechargeable = g_value_get_boolean (value);
else if (g_strcmp0 (key, "power-supply") == 0)
obj->power_supply = g_value_get_boolean (value);
else if (g_strcmp0 (key, "capacity") == 0)
obj->capacity = g_value_get_double (value);
else if (g_strcmp0 (key, "state") == 0)
obj->state = dkp_device_state_from_text (g_value_get_string (value));
else
handled = FALSE;
if (!handled)
egg_warning ("unhandled property '%s'", key);
}
/**
* dkp_object_set_from_map:
**/
gboolean
dkp_object_set_from_map (DkpObject *obj, GHashTable *hash_table)
{
g_hash_table_foreach (hash_table, (GHFunc) dkp_object_collect_props, obj);
return TRUE;
}
/**
* dkp_object_copy:
**/
DkpObject *
dkp_object_copy (const DkpObject *cobj)
{
DkpObject *obj;
obj = g_new0 (DkpObject, 1);
obj->type = cobj->type;
obj->update_time = cobj->update_time;
obj->energy = cobj->energy;
obj->energy_full = cobj->energy_full;
obj->energy_full_design = cobj->energy_full_design;
obj->energy_rate = cobj->energy_rate;
obj->voltage = cobj->voltage;
obj->percentage = cobj->percentage;
obj->capacity = cobj->capacity;
obj->time_to_empty = cobj->time_to_empty;
obj->time_to_full = cobj->time_to_full;
obj->state = cobj->state;
obj->technology = cobj->technology;
obj->vendor = g_strdup (cobj->vendor);
obj->model = g_strdup (cobj->model);
obj->serial = g_strdup (cobj->serial);
obj->native_path = g_strdup (cobj->native_path);
obj->online = cobj->online;
obj->is_present = cobj->is_present;
obj->power_supply = cobj->power_supply;
obj->is_rechargeable = cobj->is_rechargeable;
obj->has_history = cobj->has_history;
obj->has_statistics = cobj->has_statistics;
return obj;
}
/**
* dkp_object_equal:
**/
gboolean
dkp_object_equal (const DkpObject *obj1, const DkpObject *obj2)
{
if (obj1->type == obj2->type &&
obj1->update_time == obj2->update_time &&
obj1->energy == obj2->energy &&
obj1->energy_full == obj2->energy_full &&
obj1->energy_full_design == obj2->energy_full_design &&
obj1->voltage == obj2->voltage &&
obj1->energy_rate == obj2->energy_rate &&
obj1->percentage == obj2->percentage &&
obj1->has_history == obj2->has_history &&
obj1->has_statistics == obj2->has_statistics &&
obj1->capacity == obj2->capacity &&
obj1->time_to_empty == obj2->time_to_empty &&
obj1->time_to_full == obj2->time_to_full &&
obj1->state == obj2->state &&
obj1->technology == obj2->technology &&
g_strcmp0 (obj1->vendor, obj2->vendor) == 0 &&
g_strcmp0 (obj1->model, obj2->model) == 0 &&
g_strcmp0 (obj1->serial, obj2->serial) == 0 &&
g_strcmp0 (obj1->native_path, obj2->native_path) == 0 &&
obj1->online == obj2->online &&
obj1->is_present == obj2->is_present &&
obj1->power_supply == obj2->power_supply &&
obj1->is_rechargeable == obj2->is_rechargeable)
return TRUE;
return FALSE;
}
/**
* dkp_object_time_to_text:
**/
static gchar *
dkp_object_time_to_text (gint seconds)
{
gfloat value = seconds;
if (value < 0)
return g_strdup ("unknown");
if (value < 60)
return g_strdup_printf ("%.0f seconds", value);
value /= 60.0;
if (value < 60)
return g_strdup_printf ("%.1f minutes", value);
value /= 60.0;
if (value < 60)
return g_strdup_printf ("%.1f hours", value);
value /= 24.0;
return g_strdup_printf ("%.1f days", value);
}
/**
* dkp_object_bool_to_text:
**/
static const gchar *
dkp_object_bool_to_text (gboolean ret)
{
return ret ? "yes" : "no";
}
/**
* dkp_object_print:
**/
gboolean
dkp_object_print (const DkpObject *obj)
{
gboolean ret = TRUE;
struct tm *time_tm;
time_t t;
gchar time_buf[256];
gchar *time_str;
/* get a human readable time */
t = (time_t) obj->update_time;
time_tm = localtime (&t);
strftime (time_buf, sizeof time_buf, "%c", time_tm);
g_print (" native-path: %s\n", obj->native_path);
if (!egg_strzero (obj->vendor))
g_print (" vendor: %s\n", obj->vendor);
if (!egg_strzero (obj->model))
g_print (" model: %s\n", obj->model);
if (!egg_strzero (obj->serial))
g_print (" serial: %s\n", obj->serial);
g_print (" power supply: %s\n", dkp_object_bool_to_text (obj->power_supply));
g_print (" updated: %s (%d seconds ago)\n", time_buf, (int) (time (NULL) - obj->update_time));
g_print (" has history: %s\n", dkp_object_bool_to_text (obj->has_history));
g_print (" has statistics: %s\n", dkp_object_bool_to_text (obj->has_statistics));
g_print (" %s\n", dkp_device_type_to_text (obj->type));
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
obj->type == DKP_DEVICE_TYPE_UPS)
g_print (" present: %s\n", dkp_object_bool_to_text (obj->is_present));
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD)
g_print (" rechargeable: %s\n", dkp_object_bool_to_text (obj->is_rechargeable));
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD)
g_print (" state: %s\n", dkp_device_state_to_text (obj->state));
if (obj->type == DKP_DEVICE_TYPE_BATTERY) {
g_print (" energy: %g Wh\n", obj->energy);
g_print (" energy-empty: %g Wh\n", obj->energy_empty);
g_print (" energy-full: %g Wh\n", obj->energy_full);
g_print (" energy-full-design: %g Wh\n", obj->energy_full_design);
}
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MONITOR)
g_print (" energy-rate: %g W\n", obj->energy_rate);
if (obj->type == DKP_DEVICE_TYPE_UPS ||
obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MONITOR)
g_print (" voltage: %g V\n", obj->voltage);
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_UPS) {
if (obj->time_to_full >= 0) {
time_str = dkp_object_time_to_text (obj->time_to_full);
g_print (" time to full: %s\n", time_str);
g_free (time_str);
}
if (obj->time_to_empty >= 0) {
time_str = dkp_object_time_to_text (obj->time_to_empty);
g_print (" time to empty: %s\n", time_str);
g_free (time_str);
}
}
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
obj->type == DKP_DEVICE_TYPE_UPS)
g_print (" percentage: %g%%\n", obj->percentage);
if (obj->type == DKP_DEVICE_TYPE_BATTERY)
g_print (" capacity: %g%%\n", obj->capacity);
if (obj->type == DKP_DEVICE_TYPE_BATTERY)
g_print (" technology: %s\n", dkp_device_technology_to_text (obj->technology));
if (obj->type == DKP_DEVICE_TYPE_LINE_POWER)
g_print (" online: %s\n", dkp_object_bool_to_text (obj->online));
return ret;
}
/**
* dkp_object_diff:
**/
gboolean
dkp_object_diff (const DkpObject *old, const DkpObject *obj)
{
gchar *time_str;
gchar *time_str_old;
g_print (" native-path: %s\n", obj->native_path);
if (g_strcmp0 (obj->vendor, old->vendor) != 0)
g_print (" vendor: %s -> %s\n", old->vendor, obj->vendor);
if (g_strcmp0 (obj->model, old->model) != 0)
g_print (" model: %s -> %s\n", old->model, obj->model);
if (g_strcmp0 (obj->serial, old->serial) != 0)
g_print (" serial: %s -> %s\n", old->serial, obj->serial);
if (obj->has_history != old->has_history)
g_print (" has history: %s -> %s\n",
dkp_object_bool_to_text (old->has_history),
dkp_object_bool_to_text (obj->has_history));
if (obj->has_statistics != old->has_statistics)
g_print (" has statistics: %s -> %s\n",
dkp_object_bool_to_text (old->has_statistics),
dkp_object_bool_to_text (obj->has_statistics));
g_print (" %s\n", dkp_device_type_to_text (obj->type));
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD)
if (old->is_present != obj->is_present)
g_print (" present: %s -> %s\n",
dkp_object_bool_to_text (old->is_present),
dkp_object_bool_to_text (obj->is_present));
if (obj->type == DKP_DEVICE_TYPE_BATTERY)
if (old->is_rechargeable != obj->is_rechargeable)
g_print (" rechargeable: %s -> %s\n",
dkp_object_bool_to_text (old->is_rechargeable),
dkp_object_bool_to_text (obj->is_rechargeable));
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD ||
obj->type == DKP_DEVICE_TYPE_UPS)
if (old->state != obj->state)
g_print (" state: %s -> %s\n",
dkp_device_state_to_text (old->state),
dkp_device_state_to_text (obj->state));
if (obj->type == DKP_DEVICE_TYPE_BATTERY) {
if (old->energy != obj->energy)
g_print (" energy: %g -> %g Wh\n",
old->energy,
obj->energy);
if (old->energy_empty != obj->energy_empty)
g_print (" energy-empty: %g -> %g Wh\n",
old->energy_empty,
obj->energy_empty);
if (old->energy_full != obj->energy_full)
g_print (" energy-full: %g -> %g Wh\n",
old->energy_full,
obj->energy_full);
if (old->energy_full_design != obj->energy_full_design)
g_print (" energy-full-design: %g -> %g Wh\n",
old->energy_full_design,
obj->energy_full_design);
}
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MONITOR)
if (old->energy_rate != obj->energy_rate)
g_print (" energy-rate: %g -> %g W\n",
old->energy_rate, obj->energy_rate);
if (obj->type == DKP_DEVICE_TYPE_UPS ||
obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_MONITOR)
if (old->voltage != obj->voltage)
g_print (" voltage: %g -> %g V\n",
old->voltage, obj->voltage);
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_UPS) {
if (old->time_to_full != obj->time_to_full) {
time_str_old = dkp_object_time_to_text (old->time_to_full);
time_str = dkp_object_time_to_text (obj->time_to_full);
g_print (" time to full: %s -> %s\n", time_str_old, time_str);
g_free (time_str_old);
g_free (time_str);
}
if (old->time_to_empty != obj->time_to_empty) {
time_str_old = dkp_object_time_to_text (old->time_to_empty);
time_str = dkp_object_time_to_text (obj->time_to_empty);
g_print (" time to empty: %s -> %s\n", time_str_old, time_str);
g_free (time_str_old);
g_free (time_str);
}
}
if (obj->type == DKP_DEVICE_TYPE_BATTERY ||
obj->type == DKP_DEVICE_TYPE_UPS ||
obj->type == DKP_DEVICE_TYPE_MOUSE ||
obj->type == DKP_DEVICE_TYPE_KEYBOARD)
if (old->percentage != obj->percentage)
g_print (" percentage: %g%% -> %g%%\n",
old->percentage, obj->percentage);
if (obj->type == DKP_DEVICE_TYPE_BATTERY)
if (old->capacity != obj->capacity)
g_print (" capacity: %g%% -> %g%%\n",
old->capacity, obj->capacity);
if (obj->type == DKP_DEVICE_TYPE_BATTERY)
if (old->technology != obj->technology)
g_print (" technology: %s -> %s\n",
dkp_device_technology_to_text (old->technology),
dkp_device_technology_to_text (obj->technology));
if (obj->type == DKP_DEVICE_TYPE_LINE_POWER)
if (old->online != obj->online)
g_print (" online: %s -> %s\n",
dkp_object_bool_to_text (old->online),
dkp_object_bool_to_text (obj->online));
return TRUE;
}
/**
* dkp_object_new:
**/
DkpObject *
dkp_object_new (void)
{
DkpObject *obj;
obj = g_new0 (DkpObject, 1);
dkp_object_clear_internal (obj);
return obj;
}
/**
* dkp_object_free_internal:
**/
static gboolean
dkp_object_free_internal (DkpObject *obj)
{
g_free (obj->vendor);
g_free (obj->model);
g_free (obj->serial);
g_free (obj->native_path);
return TRUE;
}
/**
* dkp_object_free:
**/
gboolean
dkp_object_free (DkpObject *obj)
{
if (obj == NULL)
return FALSE;
dkp_object_free_internal (obj);
g_free (obj);
return TRUE;
}
/**
* dkp_object_clear:
**/
gboolean
dkp_object_clear (DkpObject *obj)
{
if (obj == NULL)
return FALSE;
dkp_object_free_internal (obj);
dkp_object_clear_internal (obj);
return TRUE;
}
/**
* dkp_object_get_id:
**/
gchar *
dkp_object_get_id (DkpObject *obj)
{
GString *string;
gchar *id = NULL;
/* line power */
if (obj->type == DKP_DEVICE_TYPE_LINE_POWER) {
goto out;
/* batteries */
} else if (obj->type == DKP_DEVICE_TYPE_BATTERY) {
/* we don't have an ID if we are not present */
if (!obj->is_present)
goto out;
string = g_string_new ("");
/* in an ideal world, model-capacity-serial */
if (obj->model != NULL && strlen (obj->model) > 2) {
g_string_append (string, obj->model);
g_string_append_c (string, '-');
}
if (obj->energy_full_design > 0) {
/* FIXME: this may not be stable if we are using voltage_now */
g_string_append_printf (string, "%i", (guint) obj->energy_full_design);
g_string_append_c (string, '-');
}
if (obj->serial != NULL && strlen (obj->serial) > 2) {
g_string_append (string, obj->serial);
g_string_append_c (string, '-');
}
/* make sure we are sane */
if (string->len == 0) {
/* just use something generic */
g_string_append (string, "generic_id");
} else {
/* remove trailing '-' */
g_string_set_size (string, string->len - 1);
}
/* the id may have invalid chars that need to be replaced */
id = g_string_free (string, FALSE);
} else {
/* generic fallback */
id = g_strdup_printf ("%s-%s-%s", obj->vendor, obj->model, obj->serial);
}
g_strdelimit (id, "\\\t\"?' /,.", '_');
out:
return id;
}

View file

@ -1,76 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#if !defined (__DEVICEKIT_POWER_H_INSIDE__) && !defined (DKP_COMPILATION)
#error "Only <devicekit-power.h> can be included directly."
#endif
#ifndef __DKP_OBJECT_H__
#define __DKP_OBJECT_H__
#include <glib.h>
#include <libdevkit-power/dkp-enum.h>
G_BEGIN_DECLS
typedef struct {
guint64 update_time;
gchar *vendor;
gchar *model;
gchar *serial;
gchar *native_path;
gboolean power_supply;
gboolean online;
gboolean is_present;
gboolean is_rechargeable;
gboolean has_history;
gboolean has_statistics;
DkpDeviceType type;
DkpDeviceState state;
DkpDeviceTechnology technology;
gdouble capacity; /* percent */
gdouble energy; /* Watt Hours */
gdouble energy_empty; /* Watt Hours */
gdouble energy_full; /* Watt Hours */
gdouble energy_full_design; /* Watt Hours */
gdouble energy_rate; /* Watts */
gdouble voltage; /* Volts */
gint64 time_to_empty; /* seconds */
gint64 time_to_full; /* seconds */
gdouble percentage; /* percent */
} DkpObject;
DkpObject *dkp_object_new (void);
gboolean dkp_object_clear (DkpObject *obj);
gboolean dkp_object_free (DkpObject *obj);
gchar *dkp_object_get_id (DkpObject *obj);
DkpObject *dkp_object_copy (const DkpObject *cobj);
gboolean dkp_object_print (const DkpObject *obj);
gboolean dkp_object_diff (const DkpObject *old,
const DkpObject *obj);
gboolean dkp_object_equal (const DkpObject *obj1,
const DkpObject *obj2);
gboolean dkp_object_set_from_map (DkpObject *obj,
GHashTable *hash_table);
G_END_DECLS
#endif /* __DKP_OBJECT_H__ */

View file

@ -38,7 +38,6 @@
#include "egg-string.h"
#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-csr.h"
#define DKP_CSR_REFRESH_TIMEOUT 30L
@ -78,9 +77,8 @@ dkp_csr_poll_cb (DkpCsr *csr)
{
gboolean ret;
DkpDevice *device = DKP_DEVICE (csr);
DkpObject *obj = dkp_device_get_obj (device);
egg_debug ("Polling: %s", obj->native_path);
egg_debug ("Polling: %s", dkp_device_get_object_path (device));
ret = dkp_csr_refresh (device);
if (ret)
dkp_device_emit_changed (device);
@ -137,7 +135,7 @@ dkp_csr_coldplug (DkpDevice *device)
DevkitDevice *d;
gboolean ret = FALSE;
const gchar *type;
DkpObject *obj = dkp_device_get_obj (device);
const gchar *native_path;
/* detect what kind of device we are */
d = dkp_device_get_d (device);
@ -151,19 +149,18 @@ dkp_csr_coldplug (DkpDevice *device)
/* which one? */
if (egg_strequal (type, "mouse"))
obj->type = DKP_DEVICE_TYPE_MOUSE;
if (egg_strequal (type, "keyboard"))
obj->type = DKP_DEVICE_TYPE_KEYBOARD;
/* nothing known */
if (obj->type == DKP_DEVICE_TYPE_UNKNOWN) {
g_object_set (device, "type", DKP_DEVICE_TYPE_MOUSE, NULL);
else if (egg_strequal (type, "keyboard"))
g_object_set (device, "type", DKP_DEVICE_TYPE_KEYBOARD, NULL);
else {
egg_debug ("not a recognised csr device");
goto out;
}
/* get what USB device we are */
csr->priv->bus_num = sysfs_get_int (obj->native_path, "busnum");
csr->priv->dev_num = sysfs_get_int (obj->native_path, "devnum");
native_path = devkit_device_get_native_path (d);
csr->priv->bus_num = sysfs_get_int (native_path, "busnum");
csr->priv->dev_num = sysfs_get_int (native_path, "devnum");
/* get correct bus numbers? */
if (csr->priv->bus_num == 0 || csr->priv->dev_num == 0) {
@ -184,13 +181,15 @@ dkp_csr_coldplug (DkpDevice *device)
csr->priv->is_dual = devkit_device_get_property_as_boolean (d, "DKP_CSR_DUAL");
egg_debug ("is_dual=%i", csr->priv->is_dual);
obj->vendor = g_strdup (devkit_device_get_property (d, "ID_VENDOR"));
obj->model = g_strdup (devkit_device_get_property (d, "ID_PRODUCT"));
obj->power_supply = FALSE;
obj->is_present = TRUE;
obj->is_rechargeable = TRUE;
obj->state = DKP_DEVICE_STATE_DISCHARGING;
obj->has_history = TRUE;
g_object_set (device,
"vendor", devkit_device_get_property (d, "ID_VENDOR"),
"model", devkit_device_get_property (d, "ID_PRODUCT"),
"power-supply", FALSE,
"is-present", TRUE,
"is-rechargeable", TRUE,
"state", DKP_DEVICE_STATE_DISCHARGING,
"has-history", TRUE,
NULL);
/* coldplug */
ret = dkp_csr_refresh (device);
@ -214,14 +213,14 @@ dkp_csr_refresh (DkpDevice *device)
gboolean ret = TRUE;
GTimeVal time;
DkpCsr *csr = DKP_CSR (device);
DkpObject *obj = dkp_device_get_obj (device);
usb_dev_handle *handle;
char buf[80];
unsigned int addr;
gdouble percentage;
guint written;
g_get_current_time (&time);
obj->update_time = time.tv_sec;
g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
/* For dual receivers C502, C504 and C505, the mouse is the
* second device and uses an addr of 1 in the value and index
@ -257,9 +256,10 @@ dkp_csr_refresh (DkpDevice *device)
/* get battery status */
csr->priv->raw_value = CSR_P5 & 0x07;
egg_debug ("charge level: %d", csr->priv->raw_value);
if (csr->priv->raw_value != 0) {
obj->percentage = (100.0 / 7.0) * csr->priv->raw_value;
egg_debug ("percentage=%f", obj->percentage);
if (csr->priv->raw_value != 0) {
percentage = (100.0 / 7.0) * csr->priv->raw_value;
g_object_set (device, "percentage", percentage, NULL);
egg_debug ("percentage=%f", percentage);
}
out:

View file

@ -54,8 +54,33 @@ struct DkpDevicePrivate
DkpDaemon *daemon;
DkpHistory *history;
DevkitDevice *d;
DkpObject *obj;
gboolean has_ever_refresh;
/* properties */
guint64 update_time;
gchar *vendor;
gchar *model;
gchar *serial;
gchar *native_path;
gboolean power_supply;
gboolean online;
gboolean is_present;
gboolean is_rechargeable;
gboolean has_history;
gboolean has_statistics;
DkpDeviceType type;
DkpDeviceState state;
DkpDeviceTechnology technology;
gdouble capacity; /* percent */
gdouble energy; /* Watt Hours */
gdouble energy_empty; /* Watt Hours */
gdouble energy_full; /* Watt Hours */
gdouble energy_full_design; /* Watt Hours */
gdouble energy_rate; /* Watts */
gdouble voltage; /* Volts */
gint64 time_to_empty; /* seconds */
gint64 time_to_full; /* seconds */
gdouble percentage; /* percent */
};
static void dkp_device_class_init (DkpDeviceClass *klass);
@ -72,24 +97,24 @@ enum
PROP_SERIAL,
PROP_UPDATE_TIME,
PROP_TYPE,
PROP_LINE_POWER_ONLINE,
PROP_ONLINE,
PROP_POWER_SUPPLY,
PROP_BATTERY_CAPACITY,
PROP_BATTERY_IS_PRESENT,
PROP_BATTERY_IS_RECHARGEABLE,
PROP_BATTERY_HAS_HISTORY,
PROP_BATTERY_HAS_STATISTICS,
PROP_BATTERY_STATE,
PROP_BATTERY_ENERGY,
PROP_BATTERY_ENERGY_EMPTY,
PROP_BATTERY_ENERGY_FULL,
PROP_BATTERY_ENERGY_FULL_DESIGN,
PROP_BATTERY_ENERGY_RATE,
PROP_BATTERY_VOLTAGE,
PROP_BATTERY_TIME_TO_EMPTY,
PROP_BATTERY_TIME_TO_FULL,
PROP_BATTERY_PERCENTAGE,
PROP_BATTERY_TECHNOLOGY,
PROP_CAPACITY,
PROP_IS_PRESENT,
PROP_IS_RECHARGEABLE,
PROP_HAS_HISTORY,
PROP_HAS_STATISTICS,
PROP_STATE,
PROP_ENERGY,
PROP_ENERGY_EMPTY,
PROP_ENERGY_FULL,
PROP_ENERGY_FULL_DESIGN,
PROP_ENERGY_RATE,
PROP_VOLTAGE,
PROP_TIME_TO_EMPTY,
PROP_TIME_TO_FULL,
PROP_PERCENTAGE,
PROP_TECHNOLOGY,
};
enum
@ -102,8 +127,8 @@ static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE (DkpDevice, dkp_device, G_TYPE_OBJECT)
#define DKP_DEVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DKP_TYPE_DEVICE, DkpDevicePrivate))
#define DKP_DBUS_STRUCT_UINT_DOUBLE_STRING (dbus_g_type_get_struct ("GValueArray", \
G_TYPE_UINT, G_TYPE_DOUBLE, G_TYPE_STRING, G_TYPE_INVALID))
#define DKP_DBUS_STRUCT_UINT_DOUBLE_UINT (dbus_g_type_get_struct ("GValueArray", \
G_TYPE_UINT, G_TYPE_DOUBLE, G_TYPE_UINT, G_TYPE_INVALID))
#define DKP_DBUS_STRUCT_DOUBLE_DOUBLE (dbus_g_type_get_struct ("GValueArray", \
G_TYPE_DOUBLE, G_TYPE_DOUBLE, G_TYPE_INVALID))
@ -152,82 +177,170 @@ static void
dkp_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
{
DkpDevice *device = DKP_DEVICE (object);
DkpObject *obj = device->priv->obj;
switch (prop_id) {
case PROP_NATIVE_PATH:
g_value_set_string (value, obj->native_path);
g_value_set_string (value, device->priv->native_path);
break;
case PROP_VENDOR:
g_value_set_string (value, obj->vendor);
g_value_set_string (value, device->priv->vendor);
break;
case PROP_MODEL:
g_value_set_string (value, obj->model);
g_value_set_string (value, device->priv->model);
break;
case PROP_SERIAL:
g_value_set_string (value, obj->serial);
g_value_set_string (value, device->priv->serial);
break;
case PROP_UPDATE_TIME:
g_value_set_uint64 (value, obj->update_time);
g_value_set_uint64 (value, device->priv->update_time);
break;
case PROP_TYPE:
g_value_set_string (value, dkp_device_type_to_text (obj->type));
g_value_set_uint (value, device->priv->type);
break;
case PROP_POWER_SUPPLY:
g_value_set_boolean (value, obj->power_supply);
g_value_set_boolean (value, device->priv->power_supply);
break;
case PROP_LINE_POWER_ONLINE:
g_value_set_boolean (value, obj->online);
case PROP_ONLINE:
g_value_set_boolean (value, device->priv->online);
break;
case PROP_BATTERY_IS_PRESENT:
g_value_set_boolean (value, obj->is_present);
case PROP_IS_PRESENT:
g_value_set_boolean (value, device->priv->is_present);
break;
case PROP_BATTERY_IS_RECHARGEABLE:
g_value_set_boolean (value, obj->is_rechargeable);
case PROP_IS_RECHARGEABLE:
g_value_set_boolean (value, device->priv->is_rechargeable);
break;
case PROP_BATTERY_HAS_HISTORY:
g_value_set_boolean (value, obj->has_history);
case PROP_HAS_HISTORY:
g_value_set_boolean (value, device->priv->has_history);
break;
case PROP_BATTERY_HAS_STATISTICS:
g_value_set_boolean (value, obj->has_statistics);
case PROP_HAS_STATISTICS:
g_value_set_boolean (value, device->priv->has_statistics);
break;
case PROP_BATTERY_STATE:
g_value_set_string (value, dkp_device_state_to_text (obj->state));
case PROP_STATE:
g_value_set_uint (value, device->priv->state);
break;
case PROP_BATTERY_CAPACITY:
g_value_set_double (value, obj->capacity);
case PROP_CAPACITY:
g_value_set_double (value, device->priv->capacity);
break;
case PROP_BATTERY_ENERGY:
g_value_set_double (value, obj->energy);
case PROP_ENERGY:
g_value_set_double (value, device->priv->energy);
break;
case PROP_BATTERY_ENERGY_EMPTY:
g_value_set_double (value, obj->energy_empty);
case PROP_ENERGY_EMPTY:
g_value_set_double (value, device->priv->energy_empty);
break;
case PROP_BATTERY_ENERGY_FULL:
g_value_set_double (value, obj->energy_full);
case PROP_ENERGY_FULL:
g_value_set_double (value, device->priv->energy_full);
break;
case PROP_BATTERY_ENERGY_FULL_DESIGN:
g_value_set_double (value, obj->energy_full_design);
case PROP_ENERGY_FULL_DESIGN:
g_value_set_double (value, device->priv->energy_full_design);
break;
case PROP_BATTERY_ENERGY_RATE:
g_value_set_double (value, obj->energy_rate);
case PROP_ENERGY_RATE:
g_value_set_double (value, device->priv->energy_rate);
break;
case PROP_BATTERY_VOLTAGE:
g_value_set_double (value, obj->voltage);
case PROP_VOLTAGE:
g_value_set_double (value, device->priv->voltage);
break;
case PROP_BATTERY_TIME_TO_EMPTY:
g_value_set_int64 (value, obj->time_to_empty);
case PROP_TIME_TO_EMPTY:
g_value_set_int64 (value, device->priv->time_to_empty);
break;
case PROP_BATTERY_TIME_TO_FULL:
g_value_set_int64 (value, obj->time_to_full);
case PROP_TIME_TO_FULL:
g_value_set_int64 (value, device->priv->time_to_full);
break;
case PROP_BATTERY_PERCENTAGE:
g_value_set_double (value, obj->percentage);
case PROP_PERCENTAGE:
g_value_set_double (value, device->priv->percentage);
break;
case PROP_BATTERY_TECHNOLOGY:
g_value_set_string (value, dkp_device_technology_to_text (obj->technology));
case PROP_TECHNOLOGY:
g_value_set_uint (value, device->priv->technology);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
/**
* dkp_device_set_property:
**/
static void
dkp_device_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
DkpDevice *device = DKP_DEVICE (object);
switch (prop_id) {
// case PROP_NATIVE_PATH:
// g_free (device->priv->native_path);
// device->priv->native_path = g_strdup (g_value_get_string (value));
// break;
case PROP_VENDOR:
g_free (device->priv->vendor);
device->priv->vendor = g_strdup (g_value_get_string (value));
break;
case PROP_MODEL:
g_free (device->priv->model);
device->priv->model = g_strdup (g_value_get_string (value));
break;
case PROP_SERIAL:
g_free (device->priv->serial);
device->priv->serial = g_strdup (g_value_get_string (value));
break;
case PROP_UPDATE_TIME:
device->priv->update_time = g_value_get_uint64 (value);
break;
case PROP_TYPE:
device->priv->type = g_value_get_uint (value);
break;
case PROP_POWER_SUPPLY:
device->priv->power_supply = g_value_get_boolean (value);
break;
case PROP_ONLINE:
device->priv->online = g_value_get_boolean (value);
break;
case PROP_IS_PRESENT:
device->priv->is_present = g_value_get_boolean (value);
break;
case PROP_IS_RECHARGEABLE:
device->priv->is_rechargeable = g_value_get_boolean (value);
break;
case PROP_HAS_HISTORY:
device->priv->has_history = g_value_get_boolean (value);
break;
case PROP_HAS_STATISTICS:
device->priv->has_statistics = g_value_get_boolean (value);
break;
case PROP_STATE:
device->priv->state = g_value_get_uint (value);
break;
case PROP_CAPACITY:
device->priv->capacity = g_value_get_double (value);
break;
case PROP_ENERGY:
device->priv->energy = g_value_get_double (value);
break;
case PROP_ENERGY_EMPTY:
device->priv->energy_empty = g_value_get_double (value);
break;
case PROP_ENERGY_FULL:
device->priv->energy_full = g_value_get_double (value);
break;
case PROP_ENERGY_FULL_DESIGN:
device->priv->energy_full_design = g_value_get_double (value);
break;
case PROP_ENERGY_RATE:
device->priv->energy_rate = g_value_get_double (value);
break;
case PROP_VOLTAGE:
device->priv->voltage = g_value_get_double (value);
break;
case PROP_TIME_TO_EMPTY:
device->priv->time_to_empty = g_value_get_int64 (value);
break;
case PROP_TIME_TO_FULL:
device->priv->time_to_full = g_value_get_int64 (value);
break;
case PROP_PERCENTAGE:
device->priv->percentage = g_value_get_double (value);
break;
case PROP_TECHNOLOGY:
device->priv->technology = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@ -284,6 +397,65 @@ dkp_device_get_low_battery (DkpDevice *device, gboolean *low_battery)
return klass->get_low_battery (device, low_battery);
}
/**
* dkp_device_get_id:
**/
static gchar *
dkp_device_get_id (DkpDevice *device)
{
GString *string;
gchar *id = NULL;
/* line power */
if (device->priv->type == DKP_DEVICE_TYPE_LINE_POWER) {
goto out;
/* batteries */
} else if (device->priv->type == DKP_DEVICE_TYPE_BATTERY) {
/* we don't have an ID if we are not present */
if (!device->priv->is_present)
goto out;
string = g_string_new ("");
/* in an ideal world, model-capacity-serial */
if (device->priv->model != NULL && strlen (device->priv->model) > 2) {
g_string_append (string, device->priv->model);
g_string_append_c (string, '-');
}
if (device->priv->energy_full_design > 0) {
/* FIXME: this may not be stable if we are using voltage_now */
g_string_append_printf (string, "%i", (guint) device->priv->energy_full_design);
g_string_append_c (string, '-');
}
if (device->priv->serial != NULL && strlen (device->priv->serial) > 2) {
g_string_append (string, device->priv->serial);
g_string_append_c (string, '-');
}
/* make sure we are sane */
if (string->len == 0) {
/* just use something generic */
g_string_append (string, "generic_id");
} else {
/* remove trailing '-' */
g_string_set_size (string, string->len - 1);
}
/* the id may have invalid chars that need to be replaced */
id = g_string_free (string, FALSE);
} else {
/* generic fallback */
id = g_strdup_printf ("%s-%s-%s", device->priv->vendor, device->priv->model, device->priv->serial);
}
g_strdelimit (id, "\\\t\"?' /,.", '_');
out:
return id;
}
/**
* dkp_device_coldplug:
**/
@ -302,7 +474,7 @@ dkp_device_coldplug (DkpDevice *device, DkpDaemon *daemon, DevkitDevice *d)
device->priv->daemon = g_object_ref (daemon);
native_path = devkit_device_get_native_path (d);
device->priv->obj->native_path = g_strdup (native_path);
device->priv->native_path = g_strdup (native_path);
/* coldplug source */
ret = klass->coldplug (device);
@ -320,7 +492,7 @@ dkp_device_coldplug (DkpDevice *device, DkpDaemon *daemon, DevkitDevice *d)
goto out;
/* get the id so we can load the old history */
id = dkp_object_get_id (device->priv->obj);
id = dkp_device_get_id (device);
if (id != NULL)
dkp_history_set_id (device->priv->history, id);
g_free (id);
@ -346,7 +518,7 @@ dkp_device_get_statistics (DkpDevice *device, const gchar *type, DBusGMethodInvo
g_return_val_if_fail (type != NULL, FALSE);
/* doesn't even try to support this */
if (!device->priv->obj->has_statistics) {
if (!device->priv->has_statistics) {
error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "device does not support getting stats");
dbus_g_method_return_error (context, error);
goto out;
@ -411,7 +583,7 @@ dkp_device_get_history (DkpDevice *device, const gchar *type_string, guint times
g_return_val_if_fail (type_string != NULL, FALSE);
/* doesn't even try to support this */
if (!device->priv->obj->has_history) {
if (!device->priv->has_history) {
error = g_error_new (DKP_DAEMON_ERROR, DKP_DAEMON_ERROR_GENERAL, "device does not support getting history");
dbus_g_method_return_error (context, error);
goto out;
@ -443,9 +615,9 @@ dkp_device_get_history (DkpDevice *device, const gchar *type_string, guint times
for (i=0; i<array->len; i++) {
obj = (const DkpHistoryObj *) g_ptr_array_index (array, i);
value = g_new0 (GValue, 1);
g_value_init (value, DKP_DBUS_STRUCT_UINT_DOUBLE_STRING);
g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_UINT_DOUBLE_STRING));
dbus_g_type_struct_set (value, 0, obj->time, 1, obj->value, 2, dkp_device_state_to_text (obj->state), -1);
g_value_init (value, DKP_DBUS_STRUCT_UINT_DOUBLE_UINT);
g_value_take_boxed (value, dbus_g_type_specialized_construct (DKP_DBUS_STRUCT_UINT_DOUBLE_UINT));
dbus_g_type_struct_set (value, 0, obj->time, 1, obj->value, 2, obj->state, -1);
g_ptr_array_add (complex, g_value_get_boxed (value));
g_free (value);
}
@ -465,38 +637,24 @@ out:
static gboolean
dkp_device_refresh_internal (DkpDevice *device)
{
DkpObject *obj = dkp_device_get_obj (device);
DkpObject *obj_old;
gboolean ret;
gboolean success;
DkpDeviceClass *klass = DKP_DEVICE_GET_CLASS (device);
/* make a copy so we can see if anything changed */
obj_old = dkp_object_copy (obj);
/* do the refresh */
success = klass->refresh (device);
if (!success) {
egg_warning ("failed to reresh");
ret = klass->refresh (device);
if (!ret) {
egg_debug ("no changes");
goto out;
}
/* the first time, print all properties */
if (!device->priv->has_ever_refresh) {
egg_debug ("iniital fillup");
dkp_object_print (obj);
egg_debug ("added native-path: %s\n", device->priv->native_path);
device->priv->has_ever_refresh = TRUE;
goto out;
}
/* print difference */
ret = !dkp_object_equal (obj, obj_old);
if (ret)
dkp_object_diff (obj_old, obj);
out:
dkp_object_free (obj_old);
return success;
return ret;
}
/**
@ -551,13 +709,6 @@ dkp_device_get_object_path (DkpDevice *device)
return device->priv->object_path;
}
DkpObject *
dkp_device_get_obj (DkpDevice *device)
{
g_return_val_if_fail (DKP_IS_DEVICE (device), NULL);
return device->priv->obj;
}
DevkitDevice *
dkp_device_get_d (DkpDevice *device)
{
@ -571,18 +722,16 @@ dkp_device_get_d (DkpDevice *device)
void
dkp_device_emit_changed (DkpDevice *device)
{
DkpObject *obj = dkp_device_get_obj (device);
g_return_if_fail (DKP_IS_DEVICE (device));
/* save new history */
dkp_history_set_state (device->priv->history, obj->state);
dkp_history_set_charge_data (device->priv->history, obj->percentage);
dkp_history_set_rate_data (device->priv->history, obj->energy_rate);
dkp_history_set_time_full_data (device->priv->history, obj->time_to_full);
dkp_history_set_time_empty_data (device->priv->history, obj->time_to_empty);
dkp_history_set_state (device->priv->history, device->priv->state);
dkp_history_set_charge_data (device->priv->history, device->priv->percentage);
dkp_history_set_rate_data (device->priv->history, device->priv->energy_rate);
dkp_history_set_time_full_data (device->priv->history, device->priv->time_to_full);
dkp_history_set_time_empty_data (device->priv->history, device->priv->time_to_empty);
egg_debug ("emitting changed on %s", device->priv->obj->native_path);
egg_debug ("emitting changed on %s", device->priv->native_path);
/* The order here matters; we want Device::Changed() before
* the DeviceChanged() signal on the main object; otherwise
@ -607,8 +756,8 @@ dkp_device_compute_object_path (DkpDevice *device)
const gchar *type;
guint i;
type = dkp_device_type_to_text (device->priv->obj->type);
native_path = device->priv->obj->native_path;
type = dkp_device_type_to_text (device->priv->type);
native_path = device->priv->native_path;
basename = g_path_get_basename (native_path);
id = g_strjoin ("_", type, basename, NULL);
@ -665,7 +814,6 @@ dkp_device_init (DkpDevice *device)
device->priv->daemon = NULL;
device->priv->d = NULL;
device->priv->has_ever_refresh = FALSE;
device->priv->obj = dkp_object_new ();
device->priv->history = dkp_history_new ();
device->priv->system_bus_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
@ -691,7 +839,11 @@ dkp_device_finalize (GObject *object)
g_object_unref (device->priv->d);
g_object_unref (device->priv->daemon);
g_object_unref (device->priv->history);
dkp_object_free (device->priv->obj);
g_free (device->priv->object_path);
g_free (device->priv->vendor);
g_free (device->priv->model);
g_free (device->priv->serial);
g_free (device->priv->native_path);
G_OBJECT_CLASS (dkp_device_parent_class)->finalize (object);
}
@ -704,6 +856,7 @@ dkp_device_class_init (DkpDeviceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
object_class->get_property = dkp_device_get_property;
object_class->set_property = dkp_device_set_property;
object_class->finalize = dkp_device_finalize;
g_type_class_add_private (klass, sizeof (DkpDevicePrivate));
@ -718,102 +871,317 @@ dkp_device_class_init (DkpDeviceClass *klass)
dbus_g_object_type_install_info (DKP_TYPE_DEVICE, &dbus_glib_dkp_device_object_info);
/**
* DkpDevice:update-time:
*/
g_object_class_install_property (object_class,
PROP_UPDATE_TIME,
g_param_spec_uint64 ("update-time",
NULL, NULL,
0, G_MAXUINT64, 0,
G_PARAM_READWRITE));
/**
* DkpDevice:vendor:
*/
g_object_class_install_property (object_class,
PROP_VENDOR,
g_param_spec_string ("vendor",
NULL, NULL,
NULL,
G_PARAM_READWRITE));
/**
* DkpDevice:model:
*/
g_object_class_install_property (object_class,
PROP_MODEL,
g_param_spec_string ("model",
NULL, NULL,
NULL,
G_PARAM_READWRITE));
/**
* DkpDevice:serial:
*/
g_object_class_install_property (object_class,
PROP_SERIAL,
g_param_spec_string ("serial",
NULL, NULL,
NULL,
G_PARAM_READWRITE));
/**
* DkpDevice:native-path:
*/
g_object_class_install_property (object_class,
PROP_NATIVE_PATH,
g_param_spec_string ("native-path",
NULL, NULL,
NULL,
G_PARAM_READWRITE));
/**
* DkpDevice:power-supply:
*/
g_object_class_install_property (object_class,
PROP_POWER_SUPPLY,
g_param_spec_boolean ("power-supply",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:online:
*/
g_object_class_install_property (object_class,
PROP_ONLINE,
g_param_spec_boolean ("online",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:is-present:
*/
g_object_class_install_property (object_class,
PROP_IS_PRESENT,
g_param_spec_boolean ("is-present",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:is-rechargeable:
*/
g_object_class_install_property (object_class,
PROP_IS_RECHARGEABLE,
g_param_spec_boolean ("is-rechargeable",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:has-history:
*/
g_object_class_install_property (object_class,
PROP_HAS_HISTORY,
g_param_spec_boolean ("has-history",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:has-statistics:
*/
g_object_class_install_property (object_class,
PROP_HAS_STATISTICS,
g_param_spec_boolean ("has-statistics",
NULL, NULL,
FALSE,
G_PARAM_READWRITE));
/**
* DkpDevice:type:
*/
g_object_class_install_property (object_class,
PROP_TYPE,
g_param_spec_uint ("type",
NULL, NULL,
0,
DKP_DEVICE_TYPE_LAST,
DKP_DEVICE_TYPE_UNKNOWN,
G_PARAM_READWRITE));
/**
* DkpDevice:state:
*/
g_object_class_install_property (object_class,
PROP_STATE,
g_param_spec_uint ("state",
NULL, NULL,
0,
DKP_DEVICE_STATE_LAST,
DKP_DEVICE_STATE_UNKNOWN,
G_PARAM_READWRITE));
/**
* DkpDevice:technology:
*/
g_object_class_install_property (object_class,
PROP_TECHNOLOGY,
g_param_spec_uint ("technology",
NULL, NULL,
0,
DKP_DEVICE_TECHNOLOGY_LAST,
DKP_DEVICE_TECHNOLOGY_UNKNOWN,
G_PARAM_READWRITE));
/**
* DkpDevice:capacity:
*/
g_object_class_install_property (object_class,
PROP_CAPACITY,
g_param_spec_double ("capacity", NULL, NULL,
0.0, 100.f, 100.0,
G_PARAM_READWRITE));
/**
* DkpDevice:energy:
*/
g_object_class_install_property (object_class,
PROP_ENERGY,
g_param_spec_double ("energy", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:energy-empty:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_EMPTY,
g_param_spec_double ("energy-empty", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:energy-full:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_FULL,
g_param_spec_double ("energy-full", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:energy-full-design:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_FULL_DESIGN,
g_param_spec_double ("energy-full-design", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:energy-rate:
*/
g_object_class_install_property (object_class,
PROP_ENERGY_RATE,
g_param_spec_double ("energy-rate", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:voltage:
*/
g_object_class_install_property (object_class,
PROP_VOLTAGE,
g_param_spec_double ("voltage", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* DkpDevice:time-to-empty:
*/
g_object_class_install_property (object_class,
PROP_TIME_TO_EMPTY,
g_param_spec_int64 ("time-to-empty", NULL, NULL,
0, G_MAXINT64, 0,
G_PARAM_READWRITE));
/**
* DkpDevice:time-to-full:
*/
g_object_class_install_property (object_class,
PROP_TIME_TO_FULL,
g_param_spec_int64 ("time-to-full", NULL, NULL,
0, G_MAXINT64, 0,
G_PARAM_READWRITE));
/**
* DkpDevice:percentage:
*/
g_object_class_install_property (object_class,
PROP_PERCENTAGE,
g_param_spec_double ("percentage", NULL, NULL,
0.0, 100.f, 100.0,
G_PARAM_READWRITE));
#if 0
g_object_class_install_property (
object_class,
PROP_NATIVE_PATH,
g_param_spec_string ("native-path", NULL, NULL, NULL, G_PARAM_READABLE));
g_param_spec_string ("native-path", NULL, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_VENDOR,
g_param_spec_string ("vendor", NULL, NULL, NULL, G_PARAM_READABLE));
g_param_spec_string ("vendor", NULL, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_MODEL,
g_param_spec_string ("model", NULL, NULL, NULL, G_PARAM_READABLE));
g_param_spec_string ("model", NULL, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_SERIAL,
g_param_spec_string ("serial", NULL, NULL, NULL, G_PARAM_READABLE));
g_param_spec_string ("serial", NULL, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_UPDATE_TIME,
g_param_spec_uint64 ("update-time", NULL, NULL, 0, G_MAXUINT64, 0, G_PARAM_READABLE));
g_param_spec_uint64 ("update-time", NULL, NULL, 0, G_MAXUINT64, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_TYPE,
g_param_spec_string ("type", NULL, NULL, NULL, G_PARAM_READABLE));
g_param_spec_string ("type", NULL, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_POWER_SUPPLY,
g_param_spec_boolean ("power-supply", NULL, NULL, FALSE, G_PARAM_READABLE));
g_param_spec_boolean ("power-supply", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_LINE_POWER_ONLINE,
g_param_spec_boolean ("online", NULL, NULL, FALSE, G_PARAM_READABLE));
PROP_ONLINE,
g_param_spec_boolean ("online", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_ENERGY,
g_param_spec_double ("energy", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_ENERGY,
g_param_spec_double ("energy", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_IS_PRESENT,
g_param_spec_boolean ("is-present", NULL, NULL, FALSE, G_PARAM_READABLE));
PROP_IS_PRESENT,
g_param_spec_boolean ("is-present", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_IS_RECHARGEABLE,
g_param_spec_boolean ("is-rechargeable", NULL, NULL, FALSE, G_PARAM_READABLE));
PROP_IS_RECHARGEABLE,
g_param_spec_boolean ("is-rechargeable", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_HAS_HISTORY,
g_param_spec_boolean ("has-history", NULL, NULL, FALSE, G_PARAM_READABLE));
PROP_HAS_HISTORY,
g_param_spec_boolean ("has-history", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_HAS_STATISTICS,
g_param_spec_boolean ("has-statistics", NULL, NULL, FALSE, G_PARAM_READABLE));
PROP_HAS_STATISTICS,
g_param_spec_boolean ("has-statistics", NULL, NULL, FALSE, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_STATE,
g_param_spec_string ("state", NULL, NULL, NULL, G_PARAM_READABLE));
PROP_STATE,
g_param_spec_uint ("state", 0, NULL, NULL, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_CAPACITY,
g_param_spec_double ("capacity", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_CAPACITY,
g_param_spec_double ("capacity", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_ENERGY_EMPTY,
g_param_spec_double ("energy-empty", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_ENERGY_EMPTY,
g_param_spec_double ("energy-empty", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_ENERGY_FULL,
g_param_spec_double ("energy-full", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_ENERGY_FULL,
g_param_spec_double ("energy-full", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_ENERGY_FULL_DESIGN,
g_param_spec_double ("energy-full-design", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_ENERGY_FULL_DESIGN,
g_param_spec_double ("energy-full-design", NULL, NULL, 0, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_ENERGY_RATE,
g_param_spec_double ("energy-rate", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_ENERGY_RATE,
g_param_spec_double ("energy-rate", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_VOLTAGE,
g_param_spec_double ("voltage", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READABLE));
PROP_VOLTAGE,
g_param_spec_double ("voltage", NULL, NULL, -G_MAXDOUBLE, G_MAXDOUBLE, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_TIME_TO_EMPTY,
g_param_spec_int64 ("time-to-empty", NULL, NULL, 0, G_MAXINT64, 0, G_PARAM_READABLE));
PROP_TIME_TO_EMPTY,
g_param_spec_int64 ("time-to-empty", NULL, NULL, 0, G_MAXINT64, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_TIME_TO_FULL,
g_param_spec_int64 ("time-to-full", NULL, NULL, 0, G_MAXINT64, 0, G_PARAM_READABLE));
PROP_TIME_TO_FULL,
g_param_spec_int64 ("time-to-full", NULL, NULL, 0, G_MAXINT64, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_PERCENTAGE,
g_param_spec_double ("percentage", NULL, NULL, 0, 100, 0, G_PARAM_READABLE));
PROP_PERCENTAGE,
g_param_spec_double ("percentage", NULL, NULL, 0, 100, 0, G_PARAM_READWRITE));
g_object_class_install_property (
object_class,
PROP_BATTERY_TECHNOLOGY,
g_param_spec_string ("technology", NULL, NULL, NULL, G_PARAM_READABLE));
PROP_TECHNOLOGY,
g_param_spec_string ("technology", NULL, NULL, NULL, G_PARAM_READWRITE));
#endif
dbus_g_error_domain_register (DKP_DEVICE_ERROR, NULL, DKP_DEVICE_TYPE_ERROR);
}

View file

@ -27,12 +27,11 @@
#include <devkit-gobject/devkit-gobject.h>
#include <dbus/dbus-glib.h>
#include "dkp-object.h"
#include "dkp-daemon.h"
G_BEGIN_DECLS
#define DKP_TYPE_DEVICE (dkp_device_get_type ())
#define DKP_TYPE_DEVICE (dkp_device_get_type ())
#define DKP_DEVICE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), DKP_TYPE_DEVICE, DkpDevice))
#define DKP_DEVICE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), DKP_TYPE_DEVICE, DkpDeviceClass))
#define DKP_IS_DEVICE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), DKP_TYPE_DEVICE))
@ -80,7 +79,6 @@ gboolean dkp_device_changed (DkpDevice *device,
DevkitDevice *d,
gboolean synthesized);
void dkp_device_removed (DkpDevice *device);
DkpObject *dkp_device_get_obj (DkpDevice *device);
DevkitDevice *dkp_device_get_d (DkpDevice *device);
const gchar *dkp_device_get_object_path (DkpDevice *device);
gboolean dkp_device_get_on_battery (DkpDevice *device,

View file

@ -51,7 +51,6 @@
#include "egg-string.h"
#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-hid.h"
#define DKP_HID_REFRESH_TIMEOUT 30l
@ -137,9 +136,8 @@ dkp_hid_poll (DkpHid *hid)
{
gboolean ret;
DkpDevice *device = DKP_DEVICE (hid);
DkpObject *obj = dkp_device_get_obj (device);
egg_debug ("Polling: %s", obj->native_path);
egg_debug ("Polling: %s", dkp_device_get_object_path (device));
ret = dkp_hid_refresh (device);
if (ret)
dkp_device_emit_changed (device);
@ -175,11 +173,11 @@ static DkpDeviceTechnology
dkp_hid_convert_device_technology (const gchar *type)
{
if (type == NULL)
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
if (strcasecmp (type, "pb") == 0 ||
strcasecmp (type, "pbac") == 0)
return DKP_DEVICE_TECHNOLGY_LEAD_ACID;
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_LEAD_ACID;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
}
/**
@ -191,47 +189,46 @@ dkp_hid_set_obj (DkpHid *hid, int code, int value)
const gchar *type;
gboolean ret = TRUE;
DkpDevice *device = DKP_DEVICE (hid);
DkpObject *obj = dkp_device_get_obj (device);
switch (code) {
case DKP_HID_REMAINING_CAPACITY:
obj->percentage = value;
g_object_set (device, "percentage", value, NULL);
break;
case DKP_HID_RUNTIME_TO_EMPTY:
obj->time_to_empty = value;
g_object_set (device, "time-to-empty", value, NULL);
break;
case DKP_HID_CHARGING:
if (value != 0)
obj->state = DKP_DEVICE_STATE_CHARGING;
g_object_set (device, "state", DKP_DEVICE_STATE_CHARGING, NULL);
break;
case DKP_HID_DISCHARGING:
if (value != 0)
obj->state = DKP_DEVICE_STATE_DISCHARGING;
g_object_set (device, "state", DKP_DEVICE_STATE_DISCHARGING, NULL);
break;
case DKP_HID_BATTERY_PRESENT:
obj->is_present = (value != 0);
g_object_set (device, "is-present", (value != 0), NULL);
break;
case DKP_HID_DEVICE_NAME:
//obj->device_name = dkp_hid_get_string (hid, value);
g_object_set (device, "device-name", dkp_hid_get_string (hid, value), NULL);
break;
case DKP_HID_CHEMISTRY:
type = dkp_hid_get_string (hid, value);
obj->technology = dkp_hid_convert_device_technology (type);
g_object_set (device, "technology", dkp_hid_convert_device_technology (type), NULL);
break;
case DKP_HID_RECHARGEABLE:
obj->is_rechargeable = (value != 0);
g_object_set (device, "is-rechargeable", (value != 0), NULL);
break;
case DKP_HID_OEM_INFORMATION:
obj->vendor = g_strdup (dkp_hid_get_string (hid, value));
g_object_set (device, "vendor", dkp_hid_get_string (hid, value), NULL);
break;
case DKP_HID_PRODUCT:
obj->model = g_strdup (dkp_hid_get_string (hid, value));
g_object_set (device, "model", dkp_hid_get_string (hid, value), NULL);
break;
case DKP_HID_SERIAL_NUMBER:
obj->serial = g_strdup (dkp_hid_get_string (hid, value));
g_object_set (device, "serial", dkp_hid_get_string (hid, value), NULL);
break;
case DKP_HID_DESIGN_CAPACITY:
obj->energy_full_design = value;
g_object_set (device, "energy-full-design", value, NULL);
break;
default:
ret = FALSE;
@ -294,7 +291,6 @@ dkp_hid_coldplug (DkpDevice *device)
gboolean ret = FALSE;
const gchar *device_file;
const gchar *type;
DkpObject *obj = dkp_device_get_obj (device);
/* detect what kind of device we are */
d = dkp_device_get_d (device);
@ -328,16 +324,15 @@ dkp_hid_coldplug (DkpDevice *device)
}
/* hardcode some values */
obj->type = DKP_DEVICE_TYPE_UPS;
obj->is_rechargeable = TRUE;
obj->power_supply = TRUE;
obj->is_present = TRUE;
obj->has_history = TRUE;
obj->has_statistics = TRUE;
/* try and get from udev if UPS is being difficult */
if (obj->vendor == NULL)
obj->vendor = g_strdup (devkit_device_get_property (d, "ID_VENDOR"));
g_object_set (device,
"type", DKP_DEVICE_TYPE_UPS,
"is-rechargeable", TRUE,
"power-supply", TRUE,
"is-present", TRUE,
"vendor", devkit_device_get_property (d, "ID_VENDOR"),
"has-history", TRUE,
"has-statistics", TRUE,
NULL);
/* coldplug everything */
dkp_hid_get_all_data (hid);
@ -362,11 +357,10 @@ dkp_hid_refresh (DkpDevice *device)
struct hiddev_event ev[64];
int rd;
DkpHid *hid = DKP_HID (device);
DkpObject *obj = dkp_device_get_obj (device);
/* reset time */
g_get_current_time (&time);
obj->update_time = time.tv_sec;
g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
/* read any data -- it's okay if there's nothing as we are non-blocking */
rd = read (hid->priv->fd, ev, sizeof (ev));

View file

@ -63,6 +63,15 @@ enum {
G_DEFINE_TYPE (DkpHistory, dkp_history, G_TYPE_OBJECT)
#define DKP_HISTORY_FILE_HEADER "PackageKit Profile"
/**
* dkp_history_array_copy_cb:
**/
static void
dkp_history_array_copy_cb (const DkpHistoryObj *obj, GPtrArray *dest)
{
g_ptr_array_add (dest, dkp_history_obj_copy (obj));
}
/**
* dkp_history_array_limit_resolution:
* @array: The data we have for a specific graph
@ -114,16 +123,17 @@ dkp_history_array_limit_resolution (GPtrArray *array, guint max_num)
guint step = 1;
gfloat preset;
new = g_ptr_array_new ();
egg_debug ("length of array (before) %i", array->len);
/* check length */
length = array->len;
if (length < max_num) {
new = g_object_ref (array);
/* need to copy array */
g_ptr_array_foreach (array, (GFunc) dkp_history_array_copy_cb, new);
goto out;
}
new = g_ptr_array_new ();
egg_debug ("length of array (before) %i", array->len);
/* last element */
obj = (const DkpHistoryObj *) g_ptr_array_index (array, length-1);
last = obj->time;

View file

@ -37,7 +37,6 @@
#include "egg-string.h"
#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-supply.h"
#define DKP_SUPPLY_REFRESH_TIMEOUT 10L
@ -64,13 +63,19 @@ static gboolean
dkp_supply_refresh_line_power (DkpSupply *supply)
{
DkpDevice *device = DKP_DEVICE (supply);
DkpObject *obj = dkp_device_get_obj (device);
DevkitDevice *d;
const gchar *native_path;
d = dkp_device_get_d (device);
if (d == NULL)
egg_error ("could not get device");
/* force true */
obj->power_supply = TRUE;
g_object_set (device, "power-supply", TRUE, NULL);
/* get new AC value */
obj->online = sysfs_get_int (obj->native_path, "online");
native_path = devkit_device_get_native_path (d);
g_object_set (device, "online", sysfs_get_int (native_path, "online"), NULL);
return TRUE;
}
@ -81,23 +86,37 @@ dkp_supply_refresh_line_power (DkpSupply *supply)
static void
dkp_supply_reset_values (DkpSupply *supply)
{
gchar *native_path;
DkpDeviceType type;
DkpDevice *device = DKP_DEVICE (supply);
DkpObject *obj = dkp_device_get_obj (device);
/* some stuff we copy */
type = obj->type;
native_path = g_strdup (obj->native_path);
supply->priv->has_coldplug_values = FALSE;
supply->priv->energy_old = 0;
supply->priv->energy_old_timespec.tv_sec = 0;
dkp_object_clear (obj);
/* restore the saved stuff */
obj->type = type;
obj->native_path = native_path;
/* reset to default */
g_object_set (device,
"vendor", NULL,
"model", NULL,
"serial", NULL,
"update-time", 0,
"power-supply", FALSE,
"online", FALSE,
"energy", 0.0,
"is-present", FALSE,
"is-rechargeable", FALSE,
"has-history", FALSE,
"has-statistics", FALSE,
"state", NULL,
"capacity", 0.0,
"energy-empty", 0.0,
"energy-full", 0.0,
"energy-full-design", 0.0,
"energy-rate", 0.0,
"voltage", 0.0,
"time-to-empty", 0,
"time-to-full", 0,
"percentage", 0,
"technology", NULL,
NULL);
}
/**
@ -107,17 +126,25 @@ static gboolean
dkp_supply_get_on_battery (DkpDevice *device, gboolean *on_battery)
{
DkpSupply *supply = DKP_SUPPLY (device);
DkpObject *obj = dkp_device_get_obj (device);
DkpDeviceType type;
DkpDeviceState state;
gboolean is_present;
g_return_val_if_fail (DKP_IS_SUPPLY (supply), FALSE);
g_return_val_if_fail (on_battery != NULL, FALSE);
if (obj->type != DKP_DEVICE_TYPE_BATTERY)
g_object_get (device,
"type", &type,
"state", &state,
"is-present", &is_present,
NULL);
if (type != DKP_DEVICE_TYPE_BATTERY)
return FALSE;
if (!obj->is_present)
if (!is_present)
return FALSE;
*on_battery = (obj->state == DKP_DEVICE_STATE_DISCHARGING);
*on_battery = (state == DKP_DEVICE_STATE_DISCHARGING);
return TRUE;
}
@ -130,7 +157,7 @@ dkp_supply_get_low_battery (DkpDevice *device, gboolean *low_battery)
gboolean ret;
gboolean on_battery;
DkpSupply *supply = DKP_SUPPLY (device);
DkpObject *obj = dkp_device_get_obj (device);
guint percentage;
g_return_val_if_fail (DKP_IS_SUPPLY (supply), FALSE);
g_return_val_if_fail (low_battery != NULL, FALSE);
@ -146,7 +173,8 @@ dkp_supply_get_low_battery (DkpDevice *device, gboolean *low_battery)
return TRUE;
}
*low_battery = (obj->percentage < 10);
g_object_get (device, "percentage", &percentage, NULL);
*low_battery = (percentage < 10);
return TRUE;
}
@ -158,17 +186,19 @@ dkp_supply_calculate_rate (DkpSupply *supply)
{
guint time;
gdouble energy;
gdouble energy_rate;
GTimeVal now;
DkpDevice *device = DKP_DEVICE (supply);
DkpObject *obj = dkp_device_get_obj (device);
if (obj->energy < 0)
g_object_get (device, "energy", &energy, NULL);
if (energy < 0)
return;
if (supply->priv->energy_old < 0)
return;
if (supply->priv->energy_old == obj->energy)
if (supply->priv->energy_old == energy)
return;
/* get the time difference */
@ -179,12 +209,13 @@ dkp_supply_calculate_rate (DkpSupply *supply)
return;
/* get the difference in charge */
energy = supply->priv->energy_old - obj->energy;
energy = supply->priv->energy_old - energy;
if (energy < 0.1)
return;
/* probably okay */
obj->energy_rate = energy * 3600 / time;
energy_rate = energy * 3600 / time;
g_object_set (device, "energy-rate", energy_rate, NULL);
}
/**
@ -194,22 +225,22 @@ static DkpDeviceTechnology
dkp_supply_convert_device_technology (const gchar *type)
{
if (type == NULL)
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
/* every case combination of Li-Ion is commonly used.. */
if (strcasecmp (type, "li-ion") == 0 ||
strcasecmp (type, "lion") == 0)
return DKP_DEVICE_TECHNOLGY_LITHIUM_ION;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_ION;
if (strcasecmp (type, "pb") == 0 ||
strcasecmp (type, "pbac") == 0)
return DKP_DEVICE_TECHNOLGY_LEAD_ACID;
return DKP_DEVICE_TECHNOLOGY_LEAD_ACID;
if (strcasecmp (type, "lip") == 0 ||
strcasecmp (type, "lipo") == 0)
return DKP_DEVICE_TECHNOLGY_LITHIUM_POLYMER;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_POLYMER;
if (strcasecmp (type, "nimh") == 0)
return DKP_DEVICE_TECHNOLGY_NICKEL_METAL_HYDRIDE;
return DKP_DEVICE_TECHNOLOGY_NICKEL_METAL_HYDRIDE;
if (strcasecmp (type, "lifo") == 0)
return DKP_DEVICE_TECHNOLGY_LITHIUM_IRON_PHOSPHATE;
return DKP_DEVICE_TECHNOLGY_UNKNOWN;
return DKP_DEVICE_TECHNOLOGY_LITHIUM_IRON_PHOSPHATE;
return DKP_DEVICE_TECHNOLOGY_UNKNOWN;
}
/**
@ -224,30 +255,48 @@ dkp_supply_refresh_battery (DkpSupply *supply)
gchar *technology_native;
gboolean ret = TRUE;
gdouble voltage_design;
DkpDeviceState old_state;
DkpDeviceState state;
DkpDevice *device = DKP_DEVICE (supply);
DkpObject *obj = dkp_device_get_obj (device);
const gchar *native_path;
DevkitDevice *d;
gboolean is_present;
gdouble energy;
gdouble energy_full;
gdouble energy_full_design;
gdouble energy_rate;
gdouble capacity;
gdouble percentage;
gdouble voltage;
guint64 time_to_empty;
guint64 time_to_full;
d = dkp_device_get_d (device);
if (d == NULL)
egg_error ("could not get device");
native_path = devkit_device_get_native_path (d);
/* have we just been removed? */
obj->is_present = sysfs_get_bool (obj->native_path, "present");
if (!obj->is_present) {
is_present = sysfs_get_bool (native_path, "present");
g_object_set (device, "is-present", is_present, NULL);
if (!is_present) {
dkp_supply_reset_values (supply);
obj->type = DKP_DEVICE_TYPE_BATTERY;
goto out;
}
/* get the currect charge */
obj->energy = sysfs_get_double (obj->native_path, "energy_now") / 1000000.0;
if (obj->energy == 0)
obj->energy = sysfs_get_double (obj->native_path, "energy_avg") / 1000000.0;
energy = sysfs_get_double (native_path, "energy_now") / 1000000.0;
if (energy == 0)
energy = sysfs_get_double (native_path, "energy_avg") / 1000000.0;
/* used to convert A to W later */
voltage_design = sysfs_get_double (obj->native_path, "voltage_max_design") / 1000000.0;
voltage_design = sysfs_get_double (native_path, "voltage_max_design") / 1000000.0;
if (voltage_design < 1.00) {
voltage_design = sysfs_get_double (obj->native_path, "voltage_min_design") / 1000000.0;
voltage_design = sysfs_get_double (native_path, "voltage_min_design") / 1000000.0;
if (voltage_design < 1.00) {
egg_debug ("using present voltage as design voltage");
voltage_design = sysfs_get_double (obj->native_path, "voltage_present") / 1000000.0;
voltage_design = sysfs_get_double (native_path, "voltage_present") / 1000000.0;
}
}
@ -255,60 +304,61 @@ dkp_supply_refresh_battery (DkpSupply *supply)
if (!supply->priv->has_coldplug_values) {
/* when we add via sysfs power_supply class then we know this is true */
obj->power_supply = TRUE;
g_object_set (device, "power-supply", TRUE, NULL);
/* the ACPI spec is bad at defining battery type constants */
technology_native = g_strstrip (sysfs_get_string (obj->native_path, "technology"));
obj->technology = dkp_supply_convert_device_technology (technology_native);
technology_native = g_strstrip (sysfs_get_string (native_path, "technology"));
g_object_set (device, "technology", dkp_supply_convert_device_technology (technology_native), NULL);
g_free (technology_native);
obj->vendor = g_strstrip (sysfs_get_string (obj->native_path, "manufacturer"));
obj->model = g_strstrip (sysfs_get_string (obj->native_path, "model_name"));
obj->serial = g_strstrip (sysfs_get_string (obj->native_path, "serial_number"));
/* assume true for laptops */
obj->is_rechargeable = TRUE;
obj->has_history = TRUE;
obj->has_statistics = TRUE;
g_object_set (device,
"vendor", g_strstrip (sysfs_get_string (native_path, "manufacturer")),
"model", g_strstrip (sysfs_get_string (native_path, "model_name")),
"serial", g_strstrip (sysfs_get_string (native_path, "serial_number")),
"is-rechargeable", TRUE, /* assume true for laptops */
"has-history", TRUE,
"has-statistics", TRUE,
NULL);
/* these don't change at runtime */
obj->energy_full = sysfs_get_double (obj->native_path, "energy_full") / 1000000.0;
obj->energy_full_design = sysfs_get_double (obj->native_path, "energy_full_design") / 1000000.0;
energy_full = sysfs_get_double (native_path, "energy_full") / 1000000.0;
energy_full_design = sysfs_get_double (native_path, "energy_full_design") / 1000000.0;
/* convert charge to energy */
if (obj->energy == 0) {
obj->energy_full = sysfs_get_double (obj->native_path, "charge_full") / 1000000.0;
obj->energy_full_design = sysfs_get_double (obj->native_path, "charge_full_design") / 1000000.0;
obj->energy_full *= voltage_design;
obj->energy_full_design *= voltage_design;
if (energy == 0) {
energy_full = sysfs_get_double (native_path, "charge_full") / 1000000.0;
energy_full_design = sysfs_get_double (native_path, "charge_full_design") / 1000000.0;
energy_full *= voltage_design;
energy_full_design *= voltage_design;
}
/* the last full should not be bigger than the design */
if (obj->energy_full > obj->energy_full_design)
if (energy_full > energy_full_design)
egg_warning ("energy_full (%f) is greater than energy_full_design (%f)",
obj->energy_full, obj->energy_full_design);
energy_full, energy_full_design);
/* some systems don't have this */
if (obj->energy_full < 0.01) {
if (energy_full < 0.01) {
egg_warning ("correcting energy_full (%f) using energy_full_design (%f)",
obj->energy_full, obj->energy_full_design);
obj->energy_full = obj->energy_full_design;
energy_full, energy_full_design);
energy_full = energy_full_design;
}
/* calculate how broken our battery is */
if (obj->energy_full > 0) {
obj->capacity = (obj->energy_full / obj->energy_full_design) * 100.0f;
if (obj->capacity < 0)
obj->capacity = 0;
if (obj->capacity > 100.0)
obj->capacity = 100.0;
if (energy_full > 0) {
capacity = (energy_full / energy_full_design) * 100.0f;
if (capacity < 0)
capacity = 0.0;
if (capacity > 100.0)
capacity = 100.0;
}
g_object_set (device, "capacity", capacity, NULL);
/* we only coldplug once, as these values will never change */
supply->priv->has_coldplug_values = TRUE;
}
status = g_strstrip (sysfs_get_string (obj->native_path, "status"));
status = g_strstrip (sysfs_get_string (native_path, "status"));
if (strcasecmp (status, "charging") == 0)
state = DKP_DEVICE_STATE_CHARGING;
else if (strcasecmp (status, "discharging") == 0)
@ -323,74 +373,86 @@ dkp_supply_refresh_battery (DkpSupply *supply)
}
/* get rate; it seems odd as it's either in uVh or uWh */
obj->energy_rate = fabs (sysfs_get_double (obj->native_path, "current_now") / 1000000.0);
energy_rate = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
/* convert charge to energy */
if (obj->energy == 0) {
obj->energy = sysfs_get_double (obj->native_path, "charge_now") / 1000000.0;
if (obj->energy == 0)
obj->energy = sysfs_get_double (obj->native_path, "charge_avg") / 1000000.0;
obj->energy *= voltage_design;
obj->energy_rate *= voltage_design;
if (energy == 0) {
energy = sysfs_get_double (native_path, "charge_now") / 1000000.0;
if (energy == 0)
energy = sysfs_get_double (native_path, "charge_avg") / 1000000.0;
energy *= voltage_design;
energy_rate *= voltage_design;
}
/* some batteries don't update last_full attribute */
if (obj->energy > obj->energy_full)
obj->energy_full = obj->energy;
if (energy > energy_full)
energy_full = energy;
/* present voltage */
obj->voltage = sysfs_get_double (obj->native_path, "voltage_now") / 1000000.0;
if (obj->voltage == 0)
obj->voltage = sysfs_get_double (obj->native_path, "voltage_avg") / 1000000.0;
voltage = sysfs_get_double (native_path, "voltage_now") / 1000000.0;
if (voltage == 0)
voltage = sysfs_get_double (native_path, "voltage_avg") / 1000000.0;
/* ACPI gives out the special 'Ones' value for rate when it's unable
* to calculate the true rate. We should set the rate zero, and wait
* for the BIOS to stabilise. */
if (obj->energy_rate == 0xffff)
obj->energy_rate = 0;
if (energy_rate == 0xffff)
energy_rate = 0;
/* sanity check to less than 100W */
if (obj->energy_rate > 100*1000)
obj->energy_rate = 0;
if (energy_rate > 100*1000)
energy_rate = 0;
/* the hardware reporting failed -- try to calculate this */
if (obj->energy_rate < 0)
if (energy_rate < 0)
dkp_supply_calculate_rate (supply);
/* get a precise percentage */
obj->percentage = 100.0 * obj->energy / obj->energy_full;
if (obj->percentage < 0)
obj->percentage = 0;
if (obj->percentage > 100.0)
obj->percentage = 100.0;
percentage = 100.0 * energy / energy_full;
if (percentage < 0)
percentage = 0;
if (percentage > 100.0)
percentage = 100.0;
/* calculate a quick and dirty time remaining value */
obj->time_to_empty = 0;
obj->time_to_full = 0;
if (obj->energy_rate > 0) {
time_to_empty = 0;
time_to_full = 0;
if (energy_rate > 0) {
if (state == DKP_DEVICE_STATE_DISCHARGING)
obj->time_to_empty = 3600 * (obj->energy / obj->energy_rate);
time_to_empty = 3600 * (energy / energy_rate);
else if (state == DKP_DEVICE_STATE_CHARGING)
obj->time_to_full = 3600 * ((obj->energy_full - obj->energy) / obj->energy_rate);
time_to_full = 3600 * ((energy_full - energy) / energy_rate);
/* TODO: need to factor in battery charge metrics */
}
/* check the remaining time is under a set limit, to deal with broken
primary batteries rate */
if (obj->time_to_empty > (20 * 60 * 60))
obj->time_to_empty = 0;
if (obj->time_to_full > (20 * 60 * 60))
obj->time_to_full = 0;
if (time_to_empty > (20 * 60 * 60))
time_to_empty = 0;
if (time_to_full > (20 * 60 * 60))
time_to_full = 0;
/* set the old status */
supply->priv->energy_old = obj->energy;
supply->priv->energy_old = energy;
g_get_current_time (&supply->priv->energy_old_timespec);
/* we changed state */
if (obj->state != state) {
g_object_get (device, "state", &old_state, NULL);
if (old_state != state)
supply->priv->energy_old = 0;
obj->state = state;
}
g_object_set (device,
"energy", energy,
"energy-full", energy_full,
"energy-full-design", energy_full_design,
"energy-rate", energy_rate,
"percentage", percentage,
"state", state,
"voltage", voltage,
"time-to-empty", time_to_empty,
"time-to-full", time_to_full,
NULL);
out:
g_free (status);
return ret;
@ -404,9 +466,8 @@ dkp_supply_poll_battery (DkpSupply *supply)
{
gboolean ret;
DkpDevice *device = DKP_DEVICE (supply);
DkpObject *obj = dkp_device_get_obj (device);
egg_debug ("No updates on supply %s for 30 seconds; forcing update", obj->native_path);
egg_debug ("No updates on supply %s for 30 seconds; forcing update", dkp_device_get_object_path (device));
supply->priv->poll_timer_id = 0;
ret = dkp_supply_refresh (device);
if (ret)
@ -423,7 +484,6 @@ dkp_supply_coldplug (DkpDevice *device)
DkpSupply *supply = DKP_SUPPLY (device);
DevkitDevice *d;
const gchar *native_path;
DkpObject *obj = dkp_device_get_obj (device);
dkp_supply_reset_values (supply);
@ -437,10 +497,10 @@ dkp_supply_coldplug (DkpDevice *device)
egg_error ("could not get native path");
if (sysfs_file_exists (native_path, "online")) {
obj->type = DKP_DEVICE_TYPE_LINE_POWER;
g_object_set (device, "type", DKP_DEVICE_TYPE_LINE_POWER, NULL);
} else {
/* this is correct, UPS and CSR are not in the kernel */
obj->type = DKP_DEVICE_TYPE_BATTERY;
g_object_set (device, "type", DKP_DEVICE_TYPE_BATTERY, NULL);
}
/* coldplug values */
@ -458,7 +518,8 @@ dkp_supply_refresh (DkpDevice *device)
gboolean ret;
GTimeVal time;
DkpSupply *supply = DKP_SUPPLY (device);
DkpObject *obj = dkp_device_get_obj (device);
DkpDeviceType type;
DkpDeviceState state;
if (supply->priv->poll_timer_id > 0) {
g_source_remove (supply->priv->poll_timer_id);
@ -466,9 +527,12 @@ dkp_supply_refresh (DkpDevice *device)
}
g_get_current_time (&time);
obj->update_time = time.tv_sec;
switch (obj->type) {
g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
g_object_get (device,
"type", &type,
"state", &state,
NULL);
switch (type) {
case DKP_DEVICE_TYPE_LINE_POWER:
ret = dkp_supply_refresh_line_power (supply);
break;
@ -477,8 +541,8 @@ dkp_supply_refresh (DkpDevice *device)
/* Seems that we don't get change uevents from the
* kernel on some BIOS types; set up a timer to poll
* if we are charging or discharging */
if (obj->state == DKP_DEVICE_STATE_CHARGING ||
obj->state == DKP_DEVICE_STATE_DISCHARGING)
if (state == DKP_DEVICE_STATE_CHARGING ||
state == DKP_DEVICE_STATE_DISCHARGING)
supply->priv->poll_timer_id =
g_timeout_add_seconds (DKP_SUPPLY_REFRESH_TIMEOUT,
(GSourceFunc) dkp_supply_poll_battery, supply);

View file

@ -45,7 +45,6 @@
#include "egg-string.h"
#include "dkp-enum.h"
#include "dkp-object.h"
#include "dkp-wup.h"
#define DKP_WUP_REFRESH_TIMEOUT 10 /* seconds */
@ -90,9 +89,8 @@ dkp_wup_poll_cb (DkpWup *wup)
{
gboolean ret;
DkpDevice *device = DKP_DEVICE (wup);
DkpObject *obj = dkp_device_get_obj (device);
egg_debug ("Polling: %s", obj->native_path);
egg_debug ("Polling: %s", dkp_device_get_object_path (device));
ret = dkp_wup_refresh (device);
if (ret)
dkp_device_emit_changed (device);
@ -188,7 +186,6 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data)
guint length;
guint number_tokens;
DkpDevice *device = DKP_DEVICE (wup);
DkpObject *obj = dkp_device_get_obj (device);
const guint offset = 3;
/* invalid */
@ -277,8 +274,10 @@ dkp_wup_parse_command (DkpWup *wup, const gchar *data)
/* update the command fields */
if (command == 'd' && subcommand == '-' && number_tokens - offset == 18) {
obj->energy_rate = strtod (tokens[offset+DKP_WUP_RESPONSE_OFFSET_WATTS], NULL) / 10.0f;
obj->voltage = strtod (tokens[offset+DKP_WUP_RESPONSE_OFFSET_VOLTS], NULL) / 10.0f;
g_object_set (device,
"energy-rate", strtod (tokens[offset+DKP_WUP_RESPONSE_OFFSET_WATTS], NULL) / 10.0f,
"voltage", strtod (tokens[offset+DKP_WUP_RESPONSE_OFFSET_VOLTS], NULL) / 10.0f,
NULL);
ret = TRUE;
} else {
egg_debug ("ignoring command '%c'", command);
@ -301,8 +300,8 @@ dkp_wup_coldplug (DkpDevice *device)
gboolean ret = FALSE;
const gchar *device_file;
const gchar *type;
const gchar *native_path;
gchar *data;
DkpObject *obj = dkp_device_get_obj (device);
/* detect what kind of device we are */
d = dkp_device_get_d (device);
@ -353,15 +352,18 @@ dkp_wup_coldplug (DkpDevice *device)
g_free (data);
/* hardcode some values */
obj->type = DKP_DEVICE_TYPE_MONITOR;
obj->is_rechargeable = FALSE;
obj->power_supply = FALSE;
obj->is_present = FALSE;
obj->vendor = g_strdup (devkit_device_get_property (d, "ID_VENDOR"));
obj->model = g_strdup (devkit_device_get_property (d, "ID_PRODUCT"));
obj->serial = g_strstrip (sysfs_get_string (obj->native_path, "serial"));
obj->has_history = TRUE;
obj->state = DKP_DEVICE_STATE_DISCHARGING;
native_path = devkit_device_get_native_path (d);
g_object_set (device,
"type", DKP_DEVICE_TYPE_MONITOR,
"is-rechargeable", FALSE,
"power-supply", FALSE,
"is-present", FALSE,
"vendor", devkit_device_get_property (d, "ID_VENDOR"),
"model", devkit_device_get_property (d, "ID_PRODUCT"),
"serial", g_strstrip (sysfs_get_string (native_path, "serial")),
"has-history", TRUE,
"state", DKP_DEVICE_STATE_DISCHARGING,
NULL);
/* coldplug */
egg_debug ("coldplug");
@ -382,11 +384,10 @@ dkp_wup_refresh (DkpDevice *device)
GTimeVal time;
gchar *data = NULL;
DkpWup *wup = DKP_WUP (device);
DkpObject *obj = dkp_device_get_obj (device);
/* reset time */
g_get_current_time (&time);
obj->update_time = time.tv_sec;
g_object_set (device, "update-time", (guint64) time.tv_sec, NULL);
/* get data */
data = dkp_wup_read_command (wup);

View file

@ -51,7 +51,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
)
dict entry(
string "type"
variant string "battery"
variant uint 2
)
dict entry(
string "power-supply"
@ -111,7 +111,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
)
dict entry(
string "state"
variant string "fully-charged"
variant uint 3
)
dict entry(
string "is-rechargeable"
@ -123,7 +123,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
)
dict entry(
string "technology"
variant string "lithium-ion"
variant uint 1
)
]
</doc:code>
@ -182,7 +182,7 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:summary>
</doc:doc>
</arg>
<arg name="data" direction="out" type="a(uds)">
<arg name="data" direction="out" type="a(udu)">
<doc:doc><doc:summary>
The history data for the power device, if the device supports history.
Data is ordered from the earliest in time, to the newest data point.
@ -258,121 +258,208 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
<!-- ************************************************************ -->
<property name="native-path" type="s" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
OS specific native path of the power source. On Linux this
is the sysfs path, for
example <doc:tt>/sys/devices/LNXSYSTM:00/device:00/PNP0C0A:00/power_supply/BAT0</doc:tt>. Is
blank if the device is being driven by a user space
driver.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="vendor" type="s" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Name of the vendor of the battery.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="model" type="s" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Name of the model of this battery.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="serial" type="s" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Unique serial number of the battery.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="update-time" type="t" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
The point in time (seconds since the Epoch Jan 1, 1970
0:00 UTC) that data was read from the power source.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="type" type="s" access="read">
<doc:doc><doc:description><doc:para>
Type of power source. Known values are "battery" and "line-power".
</doc:para></doc:description></doc:doc>
<property name="type" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>
Type of power source.
</doc:para>
<doc:list>
<doc:item>
<doc:term>0</doc:term><doc:definition>Unknown</doc:definition>
</doc:item>
<doc:item>
<doc:term>1</doc:term><doc:definition>Line Power</doc:definition>
</doc:item>
<doc:item>
<doc:term>2</doc:term><doc:definition>Battery</doc:definition>
</doc:item>
<doc:item>
<doc:term>3</doc:term><doc:definition>Ups</doc:definition>
</doc:item>
<doc:item>
<doc:term>4</doc:term><doc:definition>Monitor</doc:definition>
</doc:item>
<doc:item>
<doc:term>5</doc:term><doc:definition>Mouse</doc:definition>
</doc:item>
<doc:item>
<doc:term>6</doc:term><doc:definition>Keyboard</doc:definition>
</doc:item>
<doc:item>
<doc:term>7</doc:term><doc:definition>Pda</doc:definition>
</doc:item>
<doc:item>
<doc:term>8</doc:term><doc:definition>Phone</doc:definition>
</doc:item>
</doc:list>
</doc:description>
</doc:doc>
</property>
<property name="power-supply" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
If the power device is used to supply the system.
This would be set TRUE for laptop batteries and UPS devices,
but set FALSE for wireless mice or PDAs.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="has-history" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
If the power device has history.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="has-statistics" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
If the power device has statistics.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="online" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Whether power is currently being provided through line power.
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "line-power".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="energy" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Amount of energy (measured in Wh) currently available in
the power source.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="energy-empty" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Amount of energy (measured in Wh) in the power source when
it's considered to be empty.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="energy-full" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Amount of energy (measured in Wh) in the power source when
it's considered full.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="energy-full-design" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Amount of energy (measured in Wh) the power source is
designed to hold when it's considered full.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="energy-rate" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Amount of energy being drained from the source, measured
in W. If positive, the source is being discharged, if
negative it's being charged.
@ -380,39 +467,55 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="voltage" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Voltage in the Cell or being recorded by the meter.
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="time-to-empty" type="x" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Number of seconds until the power source is considered empty.
Is set to 0 if unknown.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="time-to-full" type="x" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
Number of seconds until the power source is considered full.
Is set to 0 if unknown.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="percentage" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
The amount of energy left in the power source expressed as
a percentage between 0 and 100. Typically this is the same as
(<doc:ref type="property" to="Source:energy">energy</doc:ref> -
@ -426,11 +529,15 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="is-present" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
If the power source is present in the bay.
This field is required as some batteries are hot-removable, for example
expensive UPS and most laptop batteries.
@ -438,33 +545,61 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="state" type="s" access="read">
<doc:doc><doc:description><doc:para>
<property name="state" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>
The battery power state.
Known values are "fully-charged", "empty", "charging",
"discharging" or "unknown".
</doc:para><doc:para>
</doc:para>
<doc:list>
<doc:item>
<doc:term>0</doc:term><doc:definition>Unknown</doc:definition>
</doc:item>
<doc:item>
<doc:term>1</doc:term><doc:definition>Charging</doc:definition>
</doc:item>
<doc:item>
<doc:term>2</doc:term><doc:definition>Discharging</doc:definition>
</doc:item>
<doc:item>
<doc:term>3</doc:term><doc:definition>Empty</doc:definition>
</doc:item>
<doc:item>
<doc:term>4</doc:term><doc:definition>Fully charged</doc:definition>
</doc:item>
</doc:list>
<doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="is-rechargeable" type="b" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
If the power source is rechargeable.
</doc:para><doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="capacity" type="d" access="read">
<doc:doc><doc:description><doc:para>
<doc:doc>
<doc:description>
<doc:para>
The capacity of the power source expressed as a percentage between 0 and 100.
The capacity of the battery will reduce with age.
A capacity value less than 75% is usually a sign that you should renew your battery.
@ -477,18 +612,47 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
<property name="technology" type="s" access="read">
<doc:doc><doc:description><doc:para>
Technology used in the battery; known values are "lithium-ion", "lead-acid",
"lithium-polymer", "nickel-metal-hydride", "lithium-iron-phosphate" or "unknown".
</doc:para><doc:para>
<property name="technology" type="u" access="read">
<doc:doc>
<doc:description>
<doc:para>
Technology used in the battery:
</doc:para>
<doc:list>
<doc:item>
<doc:term>0</doc:term><doc:definition>Unknown</doc:definition>
</doc:item>
<doc:item>
<doc:term>1</doc:term><doc:definition>Lithium ion</doc:definition>
</doc:item>
<doc:item>
<doc:term>2</doc:term><doc:definition>Lithium polymer</doc:definition>
</doc:item>
<doc:item>
<doc:term>3</doc:term><doc:definition>Lithium iron phosphate</doc:definition>
</doc:item>
<doc:item>
<doc:term>4</doc:term><doc:definition>Lead acid</doc:definition>
</doc:item>
<doc:item>
<doc:term>5</doc:term><doc:definition>Nickel cadmium</doc:definition>
</doc:item>
<doc:item>
<doc:term>6</doc:term><doc:definition>Nickel metal hydride</doc:definition>
</doc:item>
</doc:list>
<doc:para>
This property is only valid if the property
<doc:ref type="property" to="Source:type">type</doc:ref>
has the value "battery".
</doc:para></doc:description></doc:doc>
</doc:para>
</doc:description>
</doc:doc>
</property>
</interface>