diff options
author | Bastien Nocera <hadess@hadess.net> | 2013-10-18 15:52:32 +0200 |
---|---|---|
committer | Bastien Nocera <hadess@hadess.net> | 2013-10-18 16:16:54 +0200 |
commit | 2e4ed0c64445885d6842b64078d922036d9b7a43 (patch) | |
tree | 56cae9fb977def5c4bf80e61c39c082cd764ce3e | |
parent | 8aaa59243c920bd99a76253ce270462cb94c58fe (diff) | |
download | upower-2e4ed0c64445885d6842b64078d922036d9b7a43.tar.gz |
linux: Split off device state retrieval
We're going to be reusing this elsewhere.
-rw-r--r-- | src/linux/up-device-supply.c | 51 |
1 files changed, 31 insertions, 20 deletions
diff --git a/src/linux/up-device-supply.c b/src/linux/up-device-supply.c index a180a7b..c4553cd 100644 --- a/src/linux/up-device-supply.c +++ b/src/linux/up-device-supply.c @@ -444,6 +444,36 @@ up_device_supply_units_changed (UpDeviceSupply *supply, const gchar *native_path return TRUE; } +static UpDeviceState +up_device_supply_get_state (const gchar *native_path) +{ + UpDeviceState state; + gchar *status; + + status = g_strstrip (sysfs_get_string (native_path, "status")); + if (g_ascii_strcasecmp (status, "charging") == 0) + state = UP_DEVICE_STATE_CHARGING; + else if (g_ascii_strcasecmp (status, "discharging") == 0) + state = UP_DEVICE_STATE_DISCHARGING; + else if (g_ascii_strcasecmp (status, "full") == 0) + state = UP_DEVICE_STATE_FULLY_CHARGED; + else if (g_ascii_strcasecmp (status, "empty") == 0) + state = UP_DEVICE_STATE_EMPTY; + else if (g_ascii_strcasecmp (status, "unknown") == 0 || + *status == '\0') + state = UP_DEVICE_STATE_UNKNOWN; + else if (g_ascii_strcasecmp (status, "not charging") == 0) + state = UP_DEVICE_STATE_PENDING_CHARGE; + else { + g_warning ("unknown status string: %s", status); + state = UP_DEVICE_STATE_UNKNOWN; + } + + g_free (status); + + return state; +} + /** * up_device_supply_refresh_battery: * @@ -452,7 +482,6 @@ up_device_supply_units_changed (UpDeviceSupply *supply, const gchar *native_path static gboolean up_device_supply_refresh_battery (UpDeviceSupply *supply) { - gchar *status = NULL; gchar *technology_native = NULL; gboolean ret = TRUE; gdouble voltage_design; @@ -582,24 +611,7 @@ up_device_supply_refresh_battery (UpDeviceSupply *supply) NULL); } - status = g_strstrip (sysfs_get_string (native_path, "status")); - if (g_ascii_strcasecmp (status, "charging") == 0) - state = UP_DEVICE_STATE_CHARGING; - else if (g_ascii_strcasecmp (status, "discharging") == 0) - state = UP_DEVICE_STATE_DISCHARGING; - else if (g_ascii_strcasecmp (status, "full") == 0) - state = UP_DEVICE_STATE_FULLY_CHARGED; - else if (g_ascii_strcasecmp (status, "empty") == 0) - state = UP_DEVICE_STATE_EMPTY; - else if (g_ascii_strcasecmp (status, "unknown") == 0 || - *status == '\0') - state = UP_DEVICE_STATE_UNKNOWN; - else if (g_ascii_strcasecmp (status, "not charging") == 0) - state = UP_DEVICE_STATE_PENDING_CHARGE; - else { - g_warning ("unknown status string: %s", status); - state = UP_DEVICE_STATE_UNKNOWN; - } + state = up_device_supply_get_state (native_path); /* only disable the polling if the kernel tells us we're fully charged, not if we've guessed the state to be fully charged */ @@ -791,7 +803,6 @@ out: g_free (manufacturer); g_free (model_name); g_free (serial_number); - g_free (status); return ret; } |