health: Combine ChargingControl constructor

It is possible that a device might support multiple modes, combine
the constructor so that there is no need to write separate constructors
for each modes.

Change-Id: Ieaecd95e05d72d7212b44954f9801c55da061203
This commit is contained in:
Luofan Chen 2023-06-29 00:12:20 +08:00 committed by Fazil Sheik
parent 4a059ea05b
commit dd3b4c21f8
2 changed files with 29 additions and 26 deletions

View File

@ -40,10 +40,19 @@ static const std::vector<ChargingEnabledNode> kChargingEnabledNodes = {
static_cast<int>(ChargingControlSupportedMode::TOGGLE)},
#endif
};
#endif
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_DEADLINE
static const std::vector<std::string> kChargingDeadlineNodes = {
HEALTH_CHARGING_CONTROL_DEADLINE_PATH,
"/sys/class/power_supply/battery/charge_deadline",
};
#endif
#define OPEN_RETRY_COUNT 10
ChargingControl::ChargingControl() : mChargingEnabledNode(nullptr) {
ChargingControl::ChargingControl() : mChargingEnabledNode(nullptr), mChargingDeadlineNode(nullptr) {
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_TOGGLE
while (!mChargingEnabledNode) {
for (const auto& node : kChargingEnabledNodes) {
for (int retries = 0; retries < OPEN_RETRY_COUNT; retries++) {
@ -56,8 +65,25 @@ ChargingControl::ChargingControl() : mChargingEnabledNode(nullptr) {
}
}
}
#endif
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_DEADLINE
while (!mChargingDeadlineNode) {
for (const auto& node : kChargingDeadlineNodes) {
for (int retries = 0; retries < OPEN_RETRY_COUNT; retries++) {
if (access(node.c_str(), R_OK | W_OK) == 0) {
mChargingDeadlineNode = &node;
break;
}
PLOG(WARNING) << "Failed to access() file " << node;
usleep(100000);
}
}
}
#endif
}
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_TOGGLE
ndk::ScopedAStatus ChargingControl::getChargingEnabled(bool* _aidl_return) {
std::string content;
if (!android::base::ReadFileToString(mChargingEnabledNode->path, &content, true)) {
@ -100,24 +126,6 @@ ndk::ScopedAStatus ChargingControl::setChargingEnabled(bool /* enabled */) {
#endif
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_DEADLINE
static const std::vector<std::string> kChargingDeadlineNodes = {
HEALTH_CHARGING_CONTROL_DEADLINE_PATH,
"/sys/class/power_supply/battery/charge_deadline",
};
ChargingControl::ChargingControl() : mChargingDeadlineNode(nullptr) {
while (!mChargingDeadlineNode) {
for (const auto& node : kChargingDeadlineNodes) {
if (access(node.c_str(), R_OK | W_OK) == 0) {
mChargingDeadlineNode = &node;
break;
}
PLOG(WARNING) << "Failed to access() file " << node;
usleep(100000);
}
}
}
ndk::ScopedAStatus ChargingControl::setChargingDeadline(int64_t deadline) {
std::string content = std::to_string(deadline);
if (!android::base::WriteStringToFile(content, *mChargingDeadlineNode, true)) {

View File

@ -34,13 +34,8 @@ struct ChargingControl : public BnChargingControl {
binder_status_t dump(int fd, const char** args, uint32_t numArgs) override;
private:
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_TOGGLE
const ChargingEnabledNode* mChargingEnabledNode;
#endif
#ifdef HEALTH_CHARGING_CONTROL_SUPPORTS_DEADLINE
const std::string* mChargingDeadlineNode;
#endif
[[maybe_unused]] const ChargingEnabledNode* mChargingEnabledNode;
[[maybe_unused]] const std::string* mChargingDeadlineNode;
};
} // namespace health