healthd: fix the over estimated battery capacity am: 282b3e3c9b

Original change: https://googleplex-android-review.googlesource.com/c/device/google/wahoo/+/11865856

Change-Id: I6992a19e53597e01e2c67e943967ccd4fdc82246
This commit is contained in:
Jack Wu
2020-06-17 02:23:59 +00:00
committed by Automerger Merge Worker
2 changed files with 24 additions and 3 deletions

View File

@@ -21,6 +21,7 @@ namespace google {
namespace wahoo {
namespace health {
static constexpr char kChgFullDesignFile[] = "sys/class/power_supply/bms/charge_full_design";
static constexpr char kChgFullFile[] = "sys/class/power_supply/bms/charge_full";
static constexpr char kSysCFPersistFile[] = "/persist/battery/qcom_charge_full";
static constexpr int kBuffSize = 256;
@@ -29,13 +30,15 @@ LearnedCapacityBackupRestore::LearnedCapacityBackupRestore() : sw_cap_(0), hw_ca
void LearnedCapacityBackupRestore::Restore() {
ReadFromStorage();
ReadNominalCapacity();
ReadFromSRAM();
if (sw_cap_ == 0) {
// First backup
sw_cap_ = hw_cap_;
SaveToStorage();
} else {
// Always restore backup value
} else if (hw_cap_ == nom_cap_) {
// Restore backup value when capacity is reset to nominal
hw_cap_ = sw_cap_;
SaveToSRAM();
}
}
@@ -74,11 +77,27 @@ void LearnedCapacityBackupRestore::SaveToStorage() {
LOG(ERROR) << "Write file error: " << strerror(errno);
}
void LearnedCapacityBackupRestore::ReadNominalCapacity() {
std::string buffer;
if (!android::base::ReadFileToString(std::string(kChgFullDesignFile), &buffer)) {
LOG(ERROR) << "Read nominal capacity error: " << strerror(errno);
return;
}
buffer = android::base::Trim(buffer);
if (sscanf(buffer.c_str(), "%d", &nom_cap_) < 1)
LOG(ERROR) << "Failed to parse nominal capacity: " << buffer;
else
LOG(INFO) << "nominal capacity: " << buffer;
}
void LearnedCapacityBackupRestore::ReadFromSRAM() {
std::string buffer;
if (!android::base::ReadFileToString(std::string(kChgFullFile), &buffer)) {
LOG(ERROR) << "Read cycle counter error: " << strerror(errno);
LOG(ERROR) << "Read capacity error: " << strerror(errno);
return;
}

View File

@@ -36,9 +36,11 @@ class LearnedCapacityBackupRestore {
private:
int sw_cap_;
int hw_cap_;
int nom_cap_;
void ReadFromStorage();
void SaveToStorage();
void ReadNominalCapacity();
void ReadFromSRAM();
void SaveToSRAM();
};