Merge changes I4b9b76c9,I6f329491,I9c783193,I0798cc94

* changes:
  healthd: Convert multiple private methods into static nonmember functions
  healthd: Fix readFromFile()
  healthd: Make initHealthInfo() easier to read
  healthd: Improve readability of BatteryMonitor::init()
This commit is contained in:
Bart Van Assche 2022-03-03 21:18:17 +00:00 committed by Gerrit Code Review
commit 40080208c1
2 changed files with 46 additions and 55 deletions

View file

@ -121,14 +121,13 @@ static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> ma
} }
static void initHealthInfo(HealthInfo* health_info) { static void initHealthInfo(HealthInfo* health_info) {
*health_info = HealthInfo{}; *health_info = {
.batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
// Enum values may be zero initialized, so they need to be initialized properly. .batteryChargeTimeToFullNowSeconds =
health_info->batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED; (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
health_info->batteryChargeTimeToFullNowSeconds = .batteryStatus = BatteryStatus::UNKNOWN,
(int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED; .batteryHealth = BatteryHealth::UNKNOWN,
health_info->batteryStatus = BatteryStatus::UNKNOWN; };
health_info->batteryHealth = BatteryHealth::UNKNOWN;
} }
BatteryMonitor::BatteryMonitor() BatteryMonitor::BatteryMonitor()
@ -228,46 +227,48 @@ BatteryHealth getBatteryHealth(const char* status) {
return *ret; return *ret;
} }
int BatteryMonitor::readFromFile(const String8& path, std::string* buf) { static int readFromFile(const String8& path, std::string* buf) {
buf->clear();
if (android::base::ReadFileToString(path.c_str(), buf)) { if (android::base::ReadFileToString(path.c_str(), buf)) {
*buf = android::base::Trim(*buf); *buf = android::base::Trim(*buf);
} }
return buf->length(); return buf->length();
} }
BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) { static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
static SysfsStringEnumMap<int> supplyTypeMap[] = { static SysfsStringEnumMap<int> supplyTypeMap[] = {
{"Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN}, {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
{"Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY}, {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
{"UPS", ANDROID_POWER_SUPPLY_TYPE_AC}, {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"Mains", ANDROID_POWER_SUPPLY_TYPE_AC}, {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB", ANDROID_POWER_SUPPLY_TYPE_USB}, {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
{"USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_C", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC}, {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
{"USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB}, {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
{"Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS}, {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
{"Dock", ANDROID_POWER_SUPPLY_TYPE_DOCK}, {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
{NULL, 0}, {NULL, 0},
}; };
std::string buf; std::string buf;
if (readFromFile(path, &buf) <= 0) if (readFromFile(path, &buf) <= 0) {
return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
}
auto ret = mapSysfsString(buf.c_str(), supplyTypeMap); auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
if (!ret) { if (!ret) {
KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str()); KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
*ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN; *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
} }
return static_cast<BatteryMonitor::PowerSupplyType>(*ret); return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
} }
bool BatteryMonitor::getBooleanField(const String8& path) { static bool getBooleanField(const String8& path) {
std::string buf; std::string buf;
bool value = false; bool value = false;
@ -278,7 +279,7 @@ bool BatteryMonitor::getBooleanField(const String8& path) {
return value; return value;
} }
int BatteryMonitor::getIntField(const String8& path) { static int getIntField(const String8& path) {
std::string buf; std::string buf;
int value = 0; int value = 0;
@ -288,7 +289,7 @@ int BatteryMonitor::getIntField(const String8& path) {
return value; return value;
} }
bool BatteryMonitor::isScopedPowerSupply(const char* name) { static bool isScopedPowerSupply(const char* name) {
constexpr char kScopeDevice[] = "Device"; constexpr char kScopeDevice[] = "Device";
String8 path; String8 path;
@ -411,19 +412,7 @@ void BatteryMonitor::updateValues(void) {
} }
} }
void BatteryMonitor::logValues(void) { static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
logValues(*mHealthInfo, *mHealthdConfig);
}
void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
const struct healthd_config& healthd_config) {
HealthInfo aidl_health_info;
(void)android::h2a::translate(health_info, &aidl_health_info);
logValues(aidl_health_info, healthd_config);
}
void BatteryMonitor::logValues(const HealthInfo& props,
const struct healthd_config& healthd_config) {
char dmesgline[256]; char dmesgline[256];
size_t len; size_t len;
if (props.batteryPresent) { if (props.batteryPresent) {
@ -460,6 +449,17 @@ void BatteryMonitor::logValues(const HealthInfo& props,
KLOG_WARNING(LOG_TAG, "%s\n", dmesgline); KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
} }
void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
const struct healthd_config& healthd_config) {
HealthInfo aidl_health_info;
(void)android::h2a::translate(health_info, &aidl_health_info);
doLogValues(aidl_health_info, healthd_config);
}
void BatteryMonitor::logValues(void) {
doLogValues(*mHealthInfo, *mHealthdConfig);
}
bool BatteryMonitor::isChargerOnline() { bool BatteryMonitor::isChargerOnline() {
const HealthInfo& props = *mHealthInfo; const HealthInfo& props = *mHealthInfo;
return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline | return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
@ -608,13 +608,13 @@ void BatteryMonitor::init(struct healthd_config *hc) {
while ((entry = readdir(dir.get()))) { while ((entry = readdir(dir.get()))) {
const char* name = entry->d_name; const char* name = entry->d_name;
std::vector<String8>::iterator itIgnoreName;
if (!strcmp(name, ".") || !strcmp(name, "..")) if (!strcmp(name, ".") || !strcmp(name, ".."))
continue; continue;
itIgnoreName = find(hc->ignorePowerSupplyNames.begin(), std::vector<String8>::iterator itIgnoreName =
hc->ignorePowerSupplyNames.end(), String8(name)); find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
String8(name));
if (itIgnoreName != hc->ignorePowerSupplyNames.end()) if (itIgnoreName != hc->ignorePowerSupplyNames.end())
continue; continue;

View file

@ -82,15 +82,6 @@ class BatteryMonitor {
int mBatteryFixedCapacity; int mBatteryFixedCapacity;
int mBatteryFixedTemperature; int mBatteryFixedTemperature;
std::unique_ptr<aidl::android::hardware::health::HealthInfo> mHealthInfo; std::unique_ptr<aidl::android::hardware::health::HealthInfo> mHealthInfo;
int readFromFile(const String8& path, std::string* buf);
PowerSupplyType readPowerSupplyType(const String8& path);
bool getBooleanField(const String8& path);
int getIntField(const String8& path);
bool isScopedPowerSupply(const char* name);
static void logValues(const aidl::android::hardware::health::HealthInfo& health_info,
const struct healthd_config& healthd_config);
}; };
}; // namespace android }; // namespace android