Loading healthd/Android.mk +2 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ LOCAL_SRC_FILES := BatteryMonitor.cpp LOCAL_MODULE := libbatterymonitor LOCAL_C_INCLUDES := $(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 $(CLEAR_VARS) Loading Loading @@ -60,7 +60,7 @@ endif 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) LOCAL_STATIC_LIBRARIES += libminui libpng libz Loading healthd/BatteryMonitor.cpp +65 −92 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ #include <unistd.h> #include <memory> #include <android-base/file.h> #include <android-base/strings.h> #include <batteryservice/BatteryService.h> #include <cutils/klog.h> #include <cutils/properties.h> Loading Loading @@ -125,34 +127,15 @@ int BatteryMonitor::getBatteryHealth(const char* status) { return ret; } int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) { char *cp = NULL; 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; int BatteryMonitor::readFromFile(const String8& path, std::string* buf) { if (android::base::ReadFileToString(String8::std_string(path), buf)) { *buf = android::base::Trim(*buf); } 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; return buf->length(); } BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { const int SIZE = 128; char buf[SIZE]; int length = readFromFile(path, buf, SIZE); std::string buf; BatteryMonitor::PowerSupplyType ret; struct sysfsStringEnumMap supplyTypeMap[] = { { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN }, Loading @@ -171,12 +154,12 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String { NULL, 0 }, }; if (length <= 0) if (readFromFile(path, &buf) <= 0) return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap); ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap); 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; } Loading @@ -184,27 +167,23 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String } bool BatteryMonitor::getBooleanField(const String8& path) { const int SIZE = 16; char buf[SIZE]; std::string buf; bool value = false; if (readFromFile(path, buf, SIZE) > 0) { if (buf[0] != '0') { if (readFromFile(path, &buf) > 0) if (buf[0] != '0') value = true; } } return value; } int BatteryMonitor::getIntField(const String8& path) { const int SIZE = 128; char buf[SIZE]; std::string buf; 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; } Loading Loading @@ -248,18 +227,16 @@ bool BatteryMonitor::update(void) { props.batteryHealth = BATTERY_HEALTH_GOOD; } const int SIZE = 128; char buf[SIZE]; String8 btech; std::string buf; if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) props.batteryStatus = getBatteryStatus(buf); if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) props.batteryStatus = getBatteryStatus(buf.c_str()); if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) props.batteryHealth = getBatteryHealth(buf); if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0) props.batteryHealth = getBatteryHealth(buf.c_str()); if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) props.batteryTechnology = String8(buf); if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0) props.batteryTechnology = String8(buf.c_str()); unsigned int i; double MaxPower = 0; Loading @@ -268,9 +245,7 @@ bool BatteryMonitor::update(void) { String8 path; path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].string()); if (readFromFile(path, buf, SIZE) > 0) { if (buf[0] != '0') { if (getIntField(path)) { path.clear(); path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].string()); Loading Loading @@ -311,7 +286,6 @@ bool BatteryMonitor::update(void) { } } } } logthis = !healthd_board_battery_update(&props); Loading Loading @@ -363,10 +337,9 @@ bool BatteryMonitor::update(void) { int BatteryMonitor::getChargeStatus() { int result = BATTERY_STATUS_UNKNOWN; if (!mHealthdConfig->batteryStatusPath.isEmpty()) { char buf[128]; if (readFromFile(mHealthdConfig->batteryStatusPath, buf, sizeof(buf)) > 0) { result = getBatteryStatus(buf); } std::string buf; if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) result = getBatteryStatus(buf.c_str()); } return result; } Loading healthd/include/healthd/BatteryMonitor.h +1 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ class BatteryMonitor { int getBatteryStatus(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); bool getBooleanField(const String8& path); int getIntField(const String8& path); Loading Loading
healthd/Android.mk +2 −2 Original line number Diff line number Diff line Loading @@ -17,7 +17,7 @@ LOCAL_SRC_FILES := BatteryMonitor.cpp LOCAL_MODULE := libbatterymonitor LOCAL_C_INCLUDES := $(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 $(CLEAR_VARS) Loading Loading @@ -60,7 +60,7 @@ endif 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) LOCAL_STATIC_LIBRARIES += libminui libpng libz Loading
healthd/BatteryMonitor.cpp +65 −92 Original line number Diff line number Diff line Loading @@ -28,6 +28,8 @@ #include <unistd.h> #include <memory> #include <android-base/file.h> #include <android-base/strings.h> #include <batteryservice/BatteryService.h> #include <cutils/klog.h> #include <cutils/properties.h> Loading Loading @@ -125,34 +127,15 @@ int BatteryMonitor::getBatteryHealth(const char* status) { return ret; } int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) { char *cp = NULL; 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; int BatteryMonitor::readFromFile(const String8& path, std::string* buf) { if (android::base::ReadFileToString(String8::std_string(path), buf)) { *buf = android::base::Trim(*buf); } 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; return buf->length(); } BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { const int SIZE = 128; char buf[SIZE]; int length = readFromFile(path, buf, SIZE); std::string buf; BatteryMonitor::PowerSupplyType ret; struct sysfsStringEnumMap supplyTypeMap[] = { { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN }, Loading @@ -171,12 +154,12 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String { NULL, 0 }, }; if (length <= 0) if (readFromFile(path, &buf) <= 0) return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap); ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap); 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; } Loading @@ -184,27 +167,23 @@ BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String } bool BatteryMonitor::getBooleanField(const String8& path) { const int SIZE = 16; char buf[SIZE]; std::string buf; bool value = false; if (readFromFile(path, buf, SIZE) > 0) { if (buf[0] != '0') { if (readFromFile(path, &buf) > 0) if (buf[0] != '0') value = true; } } return value; } int BatteryMonitor::getIntField(const String8& path) { const int SIZE = 128; char buf[SIZE]; std::string buf; 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; } Loading Loading @@ -248,18 +227,16 @@ bool BatteryMonitor::update(void) { props.batteryHealth = BATTERY_HEALTH_GOOD; } const int SIZE = 128; char buf[SIZE]; String8 btech; std::string buf; if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0) props.batteryStatus = getBatteryStatus(buf); if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) props.batteryStatus = getBatteryStatus(buf.c_str()); if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0) props.batteryHealth = getBatteryHealth(buf); if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0) props.batteryHealth = getBatteryHealth(buf.c_str()); if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0) props.batteryTechnology = String8(buf); if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0) props.batteryTechnology = String8(buf.c_str()); unsigned int i; double MaxPower = 0; Loading @@ -268,9 +245,7 @@ bool BatteryMonitor::update(void) { String8 path; path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].string()); if (readFromFile(path, buf, SIZE) > 0) { if (buf[0] != '0') { if (getIntField(path)) { path.clear(); path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].string()); Loading Loading @@ -311,7 +286,6 @@ bool BatteryMonitor::update(void) { } } } } logthis = !healthd_board_battery_update(&props); Loading Loading @@ -363,10 +337,9 @@ bool BatteryMonitor::update(void) { int BatteryMonitor::getChargeStatus() { int result = BATTERY_STATUS_UNKNOWN; if (!mHealthdConfig->batteryStatusPath.isEmpty()) { char buf[128]; if (readFromFile(mHealthdConfig->batteryStatusPath, buf, sizeof(buf)) > 0) { result = getBatteryStatus(buf); } std::string buf; if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0) result = getBatteryStatus(buf.c_str()); } return result; } Loading
healthd/include/healthd/BatteryMonitor.h +1 −1 Original line number Diff line number Diff line Loading @@ -55,7 +55,7 @@ class BatteryMonitor { int getBatteryStatus(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); bool getBooleanField(const String8& path); int getIntField(const String8& path); Loading