diff --git a/client/hostinfo_unix.cpp b/client/hostinfo_unix.cpp index efcbf64572..f7aeee4b1d 100644 --- a/client/hostinfo_unix.cpp +++ b/client/hostinfo_unix.cpp @@ -100,6 +100,7 @@ #endif #include "error_numbers.h" +#include "common_defs.h" #include "filesys.h" #include "str_util.h" #include "str_replace.h" @@ -446,15 +447,12 @@ bool HOST_INFO::host_is_running_on_batteries() { // 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 HOST_INFO::get_host_battery_charge() { int capacity = 0; - snprintf(capacitypath, sizeof(capacitypath), "/sys/class/power_supply/battery/capacity"); +#if defined(ANDROID) - FILE *battery_capacity_file = fopen(acpath, "r"); + FILE *battery_capacity_file = fopen("/sys/class/power_supply/battery/capacity", "r"); if(battery_capacity_file) { fscanf(battery_capacity_file, "%d", &capacity); fclose(battery_capacity_file); @@ -467,13 +465,54 @@ int HOST_INFO::host_battery_charge() { capacity ); LOGD(msg); - return capacity; } + #endif - return 0; + + return capacity; } +// Returns the percent of which the battery is charged +// +int HOST_INFO::get_host_battery_state() { + int rc = BATTERY_STATE_UNKNOwN; +#if defined(ANDROID) + + char health[256]; + char status[256]; + + FILE *battery_health_file = fopen("/sys/class/power_supply/battery/health", "r"); + if(battery_health_file) { + fscanf(battery_health_file, "%s", &health); + fclose(battery_health_file); + } + + FILE *battery_status_file = fopen("/sys/class/power_supply/battery/status", "r"); + if(battery_status_file) { + fscanf(battery_status_file, "%s", &status); + fclose(battery_status_file); + } + + if (strlen(health) && (strcmp(health, "Overheat") == 0)) { + rc = BATTERY_STATE_OVERHEAT; + } else if (strlen(status) && (strcmp(status, "Not charging") == 0)) { + rc = BATTERY_STATE_DISCHARGING; + } else if (strlen(status) && (strcmp(status, "Charging") == 0)) { + rc = BATTERY_STATE_CHARGING; + } else if (strlen(status) && (strcmp(status, "Full") == 0)) { + rc = BATTERY_STATE_CHARGING; + } + + if (BATTERY_STATE_OVERHEAT == rc) LOGD("battery is overheating"); + if (BATTERY_STATE_DISCHARGING == rc) LOGD("battery is discharging"); + if (BATTERY_STATE_CHARGING == rc) LOGD("battery is charging"); + if (BATTERY_STATE_CHARGING == rc) LOGD("battery is full"); + +#endif + + return rc; +} #if LINUX_LIKE_SYSTEM static void parse_meminfo_linux(HOST_INFO& host) { diff --git a/client/hostinfo_win.cpp b/client/hostinfo_win.cpp index 73f27092dc..7a80901c86 100644 --- a/client/hostinfo_win.cpp +++ b/client/hostinfo_win.cpp @@ -28,6 +28,7 @@ #endif #include "error_numbers.h" +#include "common_defs.h" #include "filesys.h" #include "str_util.h" #include "str_replace.h" @@ -1296,7 +1297,7 @@ bool HOST_INFO::host_is_running_on_batteries() { return (bIsOnBatteryPower && !bIsBatteryCharging && !bIsBatteryMissing); } -int HOST_INFO::host_battery_charge() { +int HOST_INFO::get_host_battery_charge() { SYSTEM_POWER_STATUS Status; ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS)); if (!GetSystemPowerStatus(&Status)) { @@ -1307,6 +1308,30 @@ int HOST_INFO::host_battery_charge() { return ((int)Status.BatteryLifePercent); } +int HOST_INFO::get_host_battery_state() { + SYSTEM_POWER_STATUS Status; + ZeroMemory(&Status, sizeof(SYSTEM_POWER_STATUS)); + if (!GetSystemPowerStatus(&Status)) { + return false; + } + + // Sometimes the system reports the ACLineStatus as an + // undocumented value, so lets check to see if the + // battery is charging or missing and make that part + // of the decision. + bool bIsOnBatteryPower = (Status.ACLineStatus != 1); + bool bIsBatteryCharging = ((Status.BatteryFlag & 8) == 8); + + if (bIsOnBatteryPower && !bIsBatteryCharging) { + return BATTERY_STATE_DISCHARGING; + } else if (((int)Status.BatteryLifePercent) == 100) { + return BATTERY_STATE_FULL; + } else if (bIsBatteryCharging) { + return BATTERY_STATE_CHARGING; + } + return BATTERY_STATE_UNKNOwN; +} + 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/common_defs.h b/lib/common_defs.h index c38d834659..ea82f8b16b 100644 --- a/lib/common_defs.h +++ b/lib/common_defs.h @@ -109,6 +109,16 @@ enum SUSPEND_REASON { SUSPEND_REASON_WIFI_STATE = 8192 }; +// bitmap defs for battery_state +// +enum BATTERY_STATE { + BATTERY_STATE_DISCHARGING = 1, + BATTERY_STATE_CHARGING = 2, + BATTERY_STATE_FULL = 4, + BATTERY_STATE_OVERHEAT = 8, + BATTERY_STATE_UNKNOwN = 16 +}; + // Values of RESULT::state in client. // THESE MUST BE IN NUMERICAL ORDER // (because of the > comparison in RESULT::computing_done()) diff --git a/lib/hostinfo.h b/lib/hostinfo.h index 99913924c3..fbc0efaef1 100644 --- a/lib/hostinfo.h +++ b/lib/hostinfo.h @@ -75,7 +75,6 @@ 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 @@ -85,6 +84,8 @@ public: bool host_wifi_online(); #endif int get_host_info(); + int get_host_battery_charge(); + int get_host_battery_state(); int get_local_network_info(); int get_virtualbox_version(); void clear_host_info();