Merge "batterymonitor: simplify readFromFile and use std::string buffers"
This commit is contained in:
commit
a617e336dc
3 changed files with 57 additions and 83 deletions
|
|
@ -17,7 +17,7 @@ LOCAL_SRC_FILES := BatteryMonitor.cpp
|
||||||
LOCAL_MODULE := libbatterymonitor
|
LOCAL_MODULE := libbatterymonitor
|
||||||
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
|
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
|
||||||
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
|
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
|
||||||
LOCAL_STATIC_LIBRARIES := libutils libbinder
|
LOCAL_STATIC_LIBRARIES := libutils libbase libbinder
|
||||||
include $(BUILD_STATIC_LIBRARY)
|
include $(BUILD_STATIC_LIBRARY)
|
||||||
|
|
||||||
include $(CLEAR_VARS)
|
include $(CLEAR_VARS)
|
||||||
|
|
@ -60,7 +60,7 @@ endif
|
||||||
|
|
||||||
LOCAL_C_INCLUDES := bootable/recovery $(LOCAL_PATH)/include
|
LOCAL_C_INCLUDES := bootable/recovery $(LOCAL_PATH)/include
|
||||||
|
|
||||||
LOCAL_STATIC_LIBRARIES := libbatterymonitor libbatteryservice libbinder
|
LOCAL_STATIC_LIBRARIES := libbatterymonitor libbatteryservice libbinder libbase
|
||||||
|
|
||||||
ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
|
ifneq ($(strip $(LOCAL_CHARGER_NO_UI)),true)
|
||||||
LOCAL_STATIC_LIBRARIES += libminui libpng libz
|
LOCAL_STATIC_LIBRARIES += libminui libpng libz
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,8 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
|
#include <android-base/file.h>
|
||||||
|
#include <android-base/strings.h>
|
||||||
#include <batteryservice/BatteryService.h>
|
#include <batteryservice/BatteryService.h>
|
||||||
#include <cutils/klog.h>
|
#include <cutils/klog.h>
|
||||||
#include <cutils/properties.h>
|
#include <cutils/properties.h>
|
||||||
|
|
@ -121,34 +123,15 @@ int BatteryMonitor::getBatteryHealth(const char* status) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
|
int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
|
||||||
char *cp = NULL;
|
if (android::base::ReadFileToString(String8::std_string(path), buf)) {
|
||||||
|
*buf = android::base::Trim(*buf);
|
||||||
if (path.isEmpty())
|
|
||||||
return -1;
|
|
||||||
int fd = open(path.string(), O_RDONLY, 0);
|
|
||||||
if (fd == -1) {
|
|
||||||
KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
|
return buf->length();
|
||||||
ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
|
|
||||||
if (count > 0)
|
|
||||||
cp = (char *)memrchr(buf, '\n', count);
|
|
||||||
|
|
||||||
if (cp)
|
|
||||||
*cp = '\0';
|
|
||||||
else
|
|
||||||
buf[0] = '\0';
|
|
||||||
|
|
||||||
close(fd);
|
|
||||||
return count;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
|
BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
|
||||||
const int SIZE = 128;
|
std::string buf;
|
||||||
char buf[SIZE];
|
|
||||||
int length = readFromFile(path, buf, SIZE);
|
|
||||||
BatteryMonitor::PowerSupplyType ret;
|
BatteryMonitor::PowerSupplyType ret;
|
||||||
struct sysfsStringEnumMap supplyTypeMap[] = {
|
struct sysfsStringEnumMap supplyTypeMap[] = {
|
||||||
{ "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
|
{ "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
|
||||||
|
|
@ -167,12 +150,12 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String
|
||||||
{ NULL, 0 },
|
{ NULL, 0 },
|
||||||
};
|
};
|
||||||
|
|
||||||
if (length <= 0)
|
if (readFromFile(path, &buf) <= 0)
|
||||||
return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
||||||
|
|
||||||
ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
|
ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
|
||||||
if (ret < 0) {
|
if (ret < 0) {
|
||||||
KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf);
|
KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
|
||||||
ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -180,27 +163,23 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BatteryMonitor::getBooleanField(const String8& path) {
|
bool BatteryMonitor::getBooleanField(const String8& path) {
|
||||||
const int SIZE = 16;
|
std::string buf;
|
||||||
char buf[SIZE];
|
|
||||||
|
|
||||||
bool value = false;
|
bool value = false;
|
||||||
if (readFromFile(path, buf, SIZE) > 0) {
|
|
||||||
if (buf[0] != '0') {
|
if (readFromFile(path, &buf) > 0)
|
||||||
|
if (buf[0] != '0')
|
||||||
value = true;
|
value = true;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BatteryMonitor::getIntField(const String8& path) {
|
int BatteryMonitor::getIntField(const String8& path) {
|
||||||
const int SIZE = 128;
|
std::string buf;
|
||||||
char buf[SIZE];
|
|
||||||
|
|
||||||
int value = 0;
|
int value = 0;
|
||||||
if (readFromFile(path, buf, SIZE) > 0) {
|
|
||||||
value = strtol(buf, NULL, 0);
|
if (readFromFile(path, &buf) > 0)
|
||||||
}
|
value = std::stoi(buf.c_str(), NULL, 0);
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -241,18 +220,16 @@ bool BatteryMonitor::update(void) {
|
||||||
props.batteryHealth = BATTERY_HEALTH_GOOD;
|
props.batteryHealth = BATTERY_HEALTH_GOOD;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int SIZE = 128;
|
std::string buf;
|
||||||
char buf[SIZE];
|
|
||||||
String8 btech;
|
|
||||||
|
|
||||||
if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
|
if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
|
||||||
props.batteryStatus = getBatteryStatus(buf);
|
props.batteryStatus = getBatteryStatus(buf.c_str());
|
||||||
|
|
||||||
if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
|
if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
|
||||||
props.batteryHealth = getBatteryHealth(buf);
|
props.batteryHealth = getBatteryHealth(buf.c_str());
|
||||||
|
|
||||||
if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
|
if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
|
||||||
props.batteryTechnology = String8(buf);
|
props.batteryTechnology = String8(buf.c_str());
|
||||||
|
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
|
|
@ -261,33 +238,31 @@ bool BatteryMonitor::update(void) {
|
||||||
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
|
path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
|
||||||
mChargerNames[i].string());
|
mChargerNames[i].string());
|
||||||
|
|
||||||
if (readFromFile(path, buf, SIZE) > 0) {
|
if (getIntField(path)) {
|
||||||
if (buf[0] != '0') {
|
path.clear();
|
||||||
path.clear();
|
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
|
||||||
path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
|
mChargerNames[i].string());
|
||||||
mChargerNames[i].string());
|
switch(readPowerSupplyType(path)) {
|
||||||
switch(readPowerSupplyType(path)) {
|
case ANDROID_POWER_SUPPLY_TYPE_AC:
|
||||||
case ANDROID_POWER_SUPPLY_TYPE_AC:
|
props.chargerAcOnline = true;
|
||||||
props.chargerAcOnline = true;
|
break;
|
||||||
break;
|
case ANDROID_POWER_SUPPLY_TYPE_USB:
|
||||||
case ANDROID_POWER_SUPPLY_TYPE_USB:
|
props.chargerUsbOnline = true;
|
||||||
props.chargerUsbOnline = true;
|
break;
|
||||||
break;
|
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
|
||||||
case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
|
props.chargerWirelessOnline = true;
|
||||||
props.chargerWirelessOnline = true;
|
break;
|
||||||
break;
|
default:
|
||||||
default:
|
KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
|
||||||
KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
|
mChargerNames[i].string());
|
||||||
mChargerNames[i].string());
|
}
|
||||||
}
|
path.clear();
|
||||||
path.clear();
|
path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
|
||||||
path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
|
mChargerNames[i].string());
|
||||||
mChargerNames[i].string());
|
if (access(path.string(), R_OK) == 0) {
|
||||||
if (access(path.string(), R_OK) == 0) {
|
int maxChargingCurrent = getIntField(path);
|
||||||
int maxChargingCurrent = getIntField(path);
|
if (props.maxChargingCurrent < maxChargingCurrent) {
|
||||||
if (props.maxChargingCurrent < maxChargingCurrent) {
|
props.maxChargingCurrent = maxChargingCurrent;
|
||||||
props.maxChargingCurrent = maxChargingCurrent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -343,10 +318,9 @@ bool BatteryMonitor::update(void) {
|
||||||
int BatteryMonitor::getChargeStatus() {
|
int BatteryMonitor::getChargeStatus() {
|
||||||
int result = BATTERY_STATUS_UNKNOWN;
|
int result = BATTERY_STATUS_UNKNOWN;
|
||||||
if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
|
if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
|
||||||
char buf[128];
|
std::string buf;
|
||||||
if (readFromFile(mHealthdConfig->batteryStatusPath, buf, sizeof(buf)) > 0) {
|
if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
|
||||||
result = getBatteryStatus(buf);
|
result = getBatteryStatus(buf.c_str());
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ class BatteryMonitor {
|
||||||
|
|
||||||
int getBatteryStatus(const char* status);
|
int getBatteryStatus(const char* status);
|
||||||
int getBatteryHealth(const char* status);
|
int getBatteryHealth(const char* status);
|
||||||
int readFromFile(const String8& path, char* buf, size_t size);
|
int readFromFile(const String8& path, std::string* buf);
|
||||||
PowerSupplyType readPowerSupplyType(const String8& path);
|
PowerSupplyType readPowerSupplyType(const String8& path);
|
||||||
bool getBooleanField(const String8& path);
|
bool getBooleanField(const String8& path);
|
||||||
int getIntField(const String8& path);
|
int getIntField(const String8& path);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue