client: Basic detection of the battery state for a given host for Windows and Android. Useful for detecting if the battery is overheating.

This commit is contained in:
Rom Walton 2013-02-07 17:32:51 -05:00 committed by Oliver Bock
parent 759b3b8078
commit 9ce39485b5
4 changed files with 85 additions and 10 deletions

View File

@ -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) {

View File

@ -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;

View File

@ -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())

View File

@ -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();