From 759b3b8078fea09232488fd822cdb5e8492bcae2 Mon Sep 17 00:00:00 2001 From: Rom Walton Date: Thu, 7 Feb 2013 16:06:47 -0500 Subject: [PATCH] client: Basic detection of how much capacity a host's battery has on Android and Windows. --- client/hostinfo_unix.cpp | 31 +++++++++++++++++++++++++++++++ client/hostinfo_win.cpp | 23 +++++++++++++++++------ lib/hostinfo.h | 1 + 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index cadbc6f48e..efcbf64572 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -444,6 +444,37 @@ bool HOST_INFO::host_is_running_on_batteries() { #endif } +// Returns the percent of which the battery is charged +// +int HOST_INFO::host_battery_charge() { +#if defined(ANDROID) + // using /sys/class/power_supply/battery/capacity + char capacitypath[256]; + int capacity = 0; + + snprintf(capacitypath, sizeof(capacitypath), "/sys/class/power_supply/battery/capacity"); + + FILE *battery_capacity_file = fopen(acpath, "r"); + if(battery_capacity_file) { + fscanf(battery_capacity_file, "%d", &capacity); + fclose(battery_capacity_file); + } + + if (capacity) { + char msg[1024]; + snprintf(msg, sizeof(msg), + "battery capacity at: %d%% charge", + capacity + ); + LOGD(msg); + return capacity; + } +#endif + return 0; +} + + + #if LINUX_LIKE_SYSTEM static void parse_meminfo_linux(HOST_INFO& host) { char buf[256]; diff --git a/client/hostinfo_win.cpp b/client/hostinfo_win.cpp index 19a8042f94..73f27092dc 100644 --- a/client/hostinfo_win.cpp +++ b/client/hostinfo_win.cpp @@ -1279,9 +1279,9 @@ int HOST_INFO::get_host_info() { } bool HOST_INFO::host_is_running_on_batteries() { - SYSTEM_POWER_STATUS pStatus; - ZeroMemory(&pStatus, sizeof(SYSTEM_POWER_STATUS)); - if (!GetSystemPowerStatus(&pStatus)) { + SYSTEM_POWER_STATUS Status; + ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS)); + if (!GetSystemPowerStatus(&Status)) { return false; } @@ -1289,13 +1289,24 @@ bool HOST_INFO::host_is_running_on_batteries() { // undocumented value, so lets check to see if the // battery is charging or missing and make that part // of the decision. - bool bIsOnBatteryPower = (pStatus.ACLineStatus != 1); - bool bIsBatteryCharging = ((pStatus.BatteryFlag & 8) == 8); - bool bIsBatteryMissing = ((pStatus.BatteryFlag & 128) == 128); + bool bIsOnBatteryPower = (Status.ACLineStatus != 1); + bool bIsBatteryCharging = ((Status.BatteryFlag & 8) == 8); + bool bIsBatteryMissing = ((Status.BatteryFlag & 128) == 128); return (bIsOnBatteryPower && !bIsBatteryCharging && !bIsBatteryMissing); } +int HOST_INFO::host_battery_charge() { + SYSTEM_POWER_STATUS Status; + ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS)); + if (!GetSystemPowerStatus(&Status)) { + return false; + } + + if (((int)Status.BatteryLifePercent) == 255) return 0; + return ((int)Status.BatteryLifePercent); +} + bool HOST_INFO::users_idle(bool /*check_all_logins*/, double idle_time_to_run) { double seconds_idle = get_idle_tick_count() / 1000; double seconds_time_to_run = 60 * idle_time_to_run; diff --git a/lib/hostinfo.h b/lib/hostinfo.h index ab811d9543..99913924c3 100644 --- a/lib/hostinfo.h +++ b/lib/hostinfo.h @@ -75,6 +75,7 @@ public: void print(); bool host_is_running_on_batteries(); + int host_battery_charge(); #ifdef __APPLE__ bool users_idle(bool check_all_logins, double idle_time_to_run, double *actual_idle_time=NULL); #else