Skip to content

Commit

Permalink
Add charge properties for battery devices
Browse files Browse the repository at this point in the history
  • Loading branch information
spinal84 committed Jan 23, 2020
1 parent ac052c5 commit 3fe5cbc
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 13 deletions.
57 changes: 57 additions & 0 deletions dbus/org.freedesktop.UPower.Device.xml
Expand Up @@ -69,6 +69,18 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
string "online"
variant boolean false
)
dict entry(
string "charge"
variant double 4.415
)
dict entry(
string "charge-full"
variant double 4.541
)
dict entry(
string "charge-full-design"
variant double 4.561
)
dict entry(
string "energy"
variant double 72.85
Expand Down Expand Up @@ -385,6 +397,51 @@ method return sender=:1.386 -> dest=:1.477 reply_serial=2
</doc:doc>
</property>

<property name="Charge" type="d" access="read">
<doc:doc>
<doc:description>
<doc:para>
Amount of charge (measured in Ah) 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>
</property>

<property name="ChargeFull" type="d" access="read">
<doc:doc>
<doc:description>
<doc:para>
Amount of charge (measured in Ah) 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>
</property>

<property name="ChargeFullDesign" type="d" access="read">
<doc:doc>
<doc:description>
<doc:para>
Amount of charge (measured in Ah) 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>
</property>

<property name="Energy" type="d" access="read">
<doc:doc>
<doc:description>
Expand Down
6 changes: 6 additions & 0 deletions debian/libupower-glib3.symbols
Expand Up @@ -80,6 +80,9 @@ libupower-glib.so.3 libupower-glib3 #MINVER#
(optional)up_exported_device_dup_vendor@Base 0.99.4
(optional)up_exported_device_get_battery_level@Base 0.99.5
(optional)up_exported_device_get_capacity@Base 0.99.4
(optional)up_exported_device_get_charge@Base 1:0.99.7.3-2
(optional)up_exported_device_get_charge_full@Base 1:0.99.7.3-2
(optional)up_exported_device_get_charge_full_design@Base 1:0.99.7.3-2
(optional)up_exported_device_get_energy@Base 0.99.4
(optional)up_exported_device_get_energy_empty@Base 0.99.4
(optional)up_exported_device_get_energy_full@Base 0.99.4
Expand Down Expand Up @@ -119,6 +122,9 @@ libupower-glib.so.3 libupower-glib3 #MINVER#
(optional)up_exported_device_proxy_new_sync@Base 0.99.4
(optional)up_exported_device_set_battery_level@Base 0.99.5
(optional)up_exported_device_set_capacity@Base 0.99.4
(optional)up_exported_device_set_charge@Base 1:0.99.7.3-2
(optional)up_exported_device_set_charge_full@Base 1:0.99.7.3-2
(optional)up_exported_device_set_charge_full_design@Base 1:0.99.7.3-2
(optional)up_exported_device_set_energy@Base 0.99.4
(optional)up_exported_device_set_energy_empty@Base 0.99.4
(optional)up_exported_device_set_energy_full@Base 0.99.4
Expand Down
62 changes: 62 additions & 0 deletions libupower-glib/up-device.c
Expand Up @@ -77,6 +77,9 @@ enum {
PROP_STATE,
PROP_TECHNOLOGY,
PROP_CAPACITY,
PROP_CHARGE,
PROP_CHARGE_FULL,
PROP_CHARGE_FULL_DESIGN,
PROP_ENERGY,
PROP_ENERGY_EMPTY,
PROP_ENERGY_FULL,
Expand Down Expand Up @@ -328,6 +331,11 @@ up_device_to_text (UpDevice *device)
if (battery_level != UP_DEVICE_LEVEL_NONE)
g_string_append_printf (string, " battery-level: %s\n", up_device_level_to_string (battery_level));
if (kind == UP_DEVICE_KIND_BATTERY) {
if (!is_display) {
g_string_append_printf (string, " charge: %g Ah\n", up_exported_device_get_charge (priv->proxy_device));
g_string_append_printf (string, " charge-full: %g Ah\n", up_exported_device_get_charge_full (priv->proxy_device));
g_string_append_printf (string, " charge-full-design: %g Ah\n", up_exported_device_get_charge_full_design (priv->proxy_device));
}
g_string_append_printf (string, " energy: %g Wh\n", up_exported_device_get_energy (priv->proxy_device));
if (!is_display)
g_string_append_printf (string, " energy-empty: %g Wh\n", up_exported_device_get_energy_empty (priv->proxy_device));
Expand Down Expand Up @@ -641,6 +649,15 @@ up_device_set_property (GObject *object, guint prop_id, const GValue *value, GPa
case PROP_CAPACITY:
up_exported_device_set_capacity (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_CHARGE:
up_exported_device_set_charge (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_CHARGE_FULL:
up_exported_device_set_charge_full (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_CHARGE_FULL_DESIGN:
up_exported_device_set_charge_full_design (device->priv->proxy_device, g_value_get_double (value));
break;
case PROP_ENERGY:
up_exported_device_set_energy (device->priv->proxy_device, g_value_get_double (value));
break;
Expand Down Expand Up @@ -758,6 +775,15 @@ up_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpe
case PROP_CAPACITY:
g_value_set_double (value, up_exported_device_get_capacity (device->priv->proxy_device));
break;
case PROP_CHARGE:
g_value_set_double (value, up_exported_device_get_charge (device->priv->proxy_device));
break;
case PROP_CHARGE_FULL:
g_value_set_double (value, up_exported_device_get_charge_full (device->priv->proxy_device));
break;
case PROP_CHARGE_FULL_DESIGN:
g_value_set_double (value, up_exported_device_get_charge_full_design (device->priv->proxy_device));
break;
case PROP_ENERGY:
g_value_set_double (value, up_exported_device_get_energy (device->priv->proxy_device));
break;
Expand Down Expand Up @@ -1020,6 +1046,42 @@ up_device_class_init (UpDeviceClass *klass)
g_param_spec_double ("capacity", NULL, NULL,
0.0, 100.f, 100.0,
G_PARAM_READWRITE));
/**
* UpDevice:charge:
*
* The charge left in the device. Measured in Ah.
*
* Since: 1:0.99.7.3-2
**/
g_object_class_install_property (object_class,
PROP_CHARGE,
g_param_spec_double ("charge", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* UpDevice:charge-full:
*
* The amount of charge when the device is fully charged. Measured in Ah.
*
* Since: 1:0.99.7.3-2
**/
g_object_class_install_property (object_class,
PROP_CHARGE_FULL,
g_param_spec_double ("charge-full", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* UpDevice:charge-full-design:
*
* The amount of charge when the device was brand new. Measured in Ah.
*
* Since: 1:0.99.7.3-2
**/
g_object_class_install_property (object_class,
PROP_CHARGE_FULL_DESIGN,
g_param_spec_double ("charge-full-design", NULL, NULL,
0.0, G_MAXDOUBLE, 0.0,
G_PARAM_READWRITE));
/**
* UpDevice:energy:
*
Expand Down
31 changes: 18 additions & 13 deletions src/linux/up-device-supply.c
Expand Up @@ -131,6 +131,9 @@ up_device_supply_reset_values (UpDeviceSupply *supply)
"has-statistics", FALSE,
"state", UP_DEVICE_STATE_UNKNOWN,
"capacity", (gdouble) 0.0,
"charge", (gdouble) 0.0,
"charge-full", (gdouble) 0.0,
"charge-full-design", (gdouble) 0.0,
"energy-empty", (gdouble) 0.0,
"energy-full", (gdouble) 0.0,
"energy-full-design", (gdouble) 0.0,
Expand Down Expand Up @@ -546,6 +549,9 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
const gchar *native_path;
GUdevDevice *native;
gboolean is_present;
gdouble charge;
gdouble charge_full;
gdouble charge_full_design;
gdouble energy;
gdouble energy_full;
gdouble energy_full_design;
Expand Down Expand Up @@ -585,6 +591,7 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
}

/* get the current charge */
charge = sysfs_get_double (native_path, "charge_now") / 1000000.0;
energy = sysfs_get_double (native_path, "energy_now") / 1000000.0;
if (energy < 0.01)
energy = sysfs_get_double (native_path, "energy_avg") / 1000000.0;
Expand Down Expand Up @@ -624,15 +631,15 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
NULL);

/* these don't change at runtime */
charge_full = sysfs_get_double (native_path, "charge_full") / 1000000.0;
energy_full = sysfs_get_double (native_path, "energy_full") / 1000000.0;
charge_full_design = sysfs_get_double (native_path, "charge_full_design") / 1000000.0;
energy_full_design = sysfs_get_double (native_path, "energy_full_design") / 1000000.0;

/* convert charge to energy */
if (energy_full < 0.01) {
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;
energy_full = charge_full * voltage_design;
energy_full_design = charge_full_design * voltage_design;
supply->priv->coldplug_units = UP_DEVICE_SUPPLY_COLDPLUG_UNITS_CHARGE;
}

Expand Down Expand Up @@ -663,7 +670,9 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
} else {
/* get the old full */
g_object_get (device,
"charge-full", &charge_full,
"energy-full", &energy_full,
"charge-full-design", &charge_full_design,
"energy-full-design", &energy_full_design,
NULL);
}
Expand All @@ -673,25 +682,18 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
/* this is the new value in uW */
energy_rate = fabs (sysfs_get_double (native_path, "power_now") / 1000000.0);
if (energy_rate < 0.01) {
gdouble charge_full;

/* convert charge to energy */
if (energy < 0.01) {
energy = sysfs_get_double (native_path, "charge_now") / 1000000.0;
if (energy < 0.01)
if ((energy = charge) < 0.01)
energy = sysfs_get_double (native_path, "charge_avg") / 1000000.0;
energy *= voltage_design;
}

charge_full = sysfs_get_double (native_path, "charge_full") / 1000000.0;
if (charge_full < 0.01)
charge_full = sysfs_get_double (native_path, "charge_full_design") / 1000000.0;

/* If charge_full exists, then current_now is always reported in uA.
* In the legacy case, where energy only units exist, and power_now isn't present
* current_now is power in uW. */
energy_rate = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
if (charge_full != 0)
if (charge_full != 0 || charge_full_design != 0)
energy_rate *= voltage_design;
}

Expand Down Expand Up @@ -850,6 +852,9 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
*out_state = state;

g_object_set (device,
"charge", charge,
"charge-full", charge_full,
"charge-full-design", charge_full_design,
"energy", energy,
"energy-full", energy_full,
"energy-full-design", energy_full_design,
Expand Down

0 comments on commit 3fe5cbc

Please sign in to comment.