...
  View open merge request
Commits (3)
......@@ -43,23 +43,31 @@
#include "sysfs-utils.h"
double
sysfs_get_double_with_error (const char *dir, const char *attribute)
gboolean
sysfs_get_double_with_error (const char *dir,
const char *attribute,
double *value)
{
double result;
char *contents;
char *filename;
gboolean ret = FALSE;
double parsed;
g_return_val_if_fail (value != NULL, FALSE);
filename = g_build_filename (dir, attribute, NULL);
if (g_file_get_contents (filename, &contents, NULL, NULL)) {
result = g_ascii_strtod (contents, NULL);
parsed = g_ascii_strtod (contents, NULL);
if (errno == 0)
ret = TRUE;
g_free (contents);
} else {
result = -1.0;
}
g_free (filename);
return result;
if (ret)
*value = parsed;
return ret;
}
double
......@@ -77,7 +85,6 @@ sysfs_get_double (const char *dir, const char *attribute)
}
g_free (filename);
return result;
}
......
......@@ -28,6 +28,8 @@ char *sysfs_get_string (const char *dir, const char *attribute);
int sysfs_get_int (const char *dir, const char *attribute);
gboolean sysfs_get_bool (const char *dir, const char *attribute);
gboolean sysfs_file_exists (const char *dir, const char *attribute);
double sysfs_get_double_with_error (const char *dir, const char *attribute);
gboolean sysfs_get_double_with_error (const char *dir,
const char *attribute,
double *value);
#endif /* __SYSFS_UTILS_H__ */
......@@ -674,8 +674,7 @@ 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) {
if (!sysfs_get_double_with_error (native_path, "power_now", &energy_rate)) {
gdouble charge_full;
/* convert charge to energy */
......@@ -696,6 +695,8 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply,
energy_rate = fabs (sysfs_get_double (native_path, "current_now") / 1000000.0);
if (charge_full != 0)
energy_rate *= voltage_design;
} else {
energy_rate = fabs (energy_rate / 1000000.0);
}
/* some batteries don't update last_full attribute */
......@@ -959,8 +960,7 @@ up_device_supply_refresh_device (UpDeviceSupply *supply,
}
/* get a precise percentage */
percentage = sysfs_get_double_with_error (native_path, "capacity");
if (percentage < 0.0)
if (!sysfs_get_double_with_error (native_path, "capacity", &percentage))
percentage = sysfs_get_capacity_level (native_path, &level);
if (percentage < 0.0) {
......