- client (Android): change battery-status function so that it

keeps file descriptors open
    (slight simplification and performance enhancement).
This commit is contained in:
David Anderson 2013-02-09 21:02:11 -08:00 committed by Oliver Bock
parent 21f580d9ef
commit 3b18584cb3
2 changed files with 41 additions and 36 deletions

View File

@ -228,25 +228,25 @@ bool HOST_INFO::host_is_running_on_batteries() {
// using /sys/class/power_supply/*/online // using /sys/class/power_supply/*/online
// power supplies are both ac and usb! // power supplies are both ac and usb!
// //
char acpath[1024]; static bool first = true;
snprintf(acpath, sizeof(acpath), "/sys/class/power_supply/ac/online"); FILE *fsysac, *fsysusb;
char usbpath[1024]; if (first) {
snprintf(usbpath, sizeof(usbpath), "/sys/class/power_supply/usb/online"); first = false;
fsysac = fopen("/sys/class/power_supply/ac/online", "r");
FILE *fsysac = fopen(acpath, "r"); fsysusb = fopen("/sys/class/power_supply/usb/online", "r");
FILE *fsysusb = fopen(usbpath, "r"); }
int aconline = 0; int aconline = 0;
int usbonline = 0; int usbonline = 0;
bool power_supply_online = false; bool power_supply_online = false;
if(fsysac) { if (fsysac) {
rewind(fsysac);
(void) fscanf(fsysac, "%d", &aconline); (void) fscanf(fsysac, "%d", &aconline);
fclose(fsysac);
} }
if(fsysusb) { if (fsysusb) {
rewind(fsysusb);
(void) fscanf(fsysusb, "%d", &usbonline); (void) fscanf(fsysusb, "%d", &usbonline);
fclose(fsysusb);
} }
if ((aconline == 1) || (usbonline == 1)){ if ((aconline == 1) || (usbonline == 1)){
@ -451,17 +451,26 @@ bool HOST_INFO::host_is_running_on_batteries() {
void HOST_INFO::get_battery_status() { void HOST_INFO::get_battery_status() {
char msg[1024]; char msg[1024];
battery_charge_pct = -1; battery_charge_pct = -1;
static bool first = true;
static FILE *fcap, *fhealth, *fstatus, *ftemp;
FILE *f = fopen("/sys/class/power_supply/battery/capacity", "r"); if (first) {
if (f) { first = false;
fscanf(f, "%d", &battery_charge_pct); fcap = fopen("/sys/class/power_supply/battery/capacity", "r");
fclose(f); fhealth = fopen("/sys/class/power_supply/battery/health", "r");
fstatus = fopen("/sys/class/power_supply/battery/status", "r");
ftemp = fopen("/sys/class/power_supply/battery/batt_temp", "r");
if (!ftemp) {
ftemp = fopen("/sys/class/power_supply/battery/temp", "r");
}
} }
snprintf(msg, sizeof(msg), if (fcap) {
"battery capacity at: %d%% charge", rewind(fcap);
capacity fscanf(fcap, "%d", &battery_charge_pct);
); }
snprintf(msg, sizeof(msg), "battery capacity at: %d%% charge", capacity);
LOGD(msg); LOGD(msg);
char health[256]; char health[256];
@ -469,16 +478,14 @@ void HOST_INFO::get_battery_status() {
strcpy(health, ""); strcpy(health, "");
strcpy(status, ""); strcpy(status, "");
f = fopen("/sys/class/power_supply/battery/health", "r"); if (fhealth) {
if (f) { rewind(fhealth);
fgets(health, sizeof(health), f); fgets(health, sizeof(health), fhealth);
fclose(f);
} }
f = fopen("/sys/class/power_supply/battery/status", "r"); if (fstatus) {
if (f) { rewind(fstatus);
fgets(status, sizeof(status), f); fgets(status, sizeof(status), fstatus);
fclose(f);
} }
battery_state = BATTERY_STATE_UNKNOWN; battery_state = BATTERY_STATE_UNKNOWN;
@ -497,14 +504,12 @@ void HOST_INFO::get_battery_status() {
} }
battery_temperature_celsius = 0; battery_temperature_celsius = 0;
f = fopen("/sys/class/power_supply/battery/batt_temp", "r"); if (ftemp) {
if (!f) { rewind(ftemp);
f = fopen("/sys/class/power_supply/battery/temp", "r"); int x;
} if (fscanf(ftemp, "%d", &x) == 1) {
if (f) { battery_temperature_celsius = x/10.;
fscanf(f, "%d", &battery_temperature_celsius); }
battery_temperature_celsius /= 10;
fclose(f);
} }
} }
#endif #endif

View File

@ -70,7 +70,7 @@ public:
#ifdef ANDROID #ifdef ANDROID
int battery_charge_pct; int battery_charge_pct;
int battery_state; int battery_state;
int battery_temperature_celsius; double battery_temperature_celsius;
void get_battery_status(); void get_battery_status();
#endif #endif