forked from LineageOS/android_device_google_wahoo
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
battery learned capacity: backup/restore to/from persist data
Test: - delete file /persist/battery/qcom_charge_full - adb logcat to check code flow related [email protected] - adb bugreport - no "avc: denied" on health vendor service - check learned capacity in both /sys/class/power_supply/bms/charge_full /persist/battery/qcom_charge_full -- decrease /sys/class/power_supply/bms/charge_full to check backup to /persist/battery/qcom_charge_full -- increase /sys/class/power_supply/bms/charge_full to check restore from /persist/battery/qcom_charge_full (simluate the reset condition) Bug: 78883741 Change-Id: Ib17bc0938d3a4b78376140a8a1008b4fc82220af Signed-off-by: Jack Wu <[email protected]>
- Loading branch information
Jack Wu
authored and
Thierry Strudel
committed
May 10, 2018
1 parent
4eb8b24
commit 2fa48ee
Showing
5 changed files
with
179 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "LearnedCapacityBackupRestore.h" | ||
|
||
namespace device { | ||
namespace google { | ||
namespace wahoo { | ||
namespace health { | ||
|
||
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; | ||
|
||
LearnedCapacityBackupRestore::LearnedCapacityBackupRestore() {} | ||
|
||
void LearnedCapacityBackupRestore::Restore() { | ||
ReadFromStorage(); | ||
ReadFromSRAM(); | ||
UpdateAndSave(); | ||
} | ||
|
||
void LearnedCapacityBackupRestore::Backup() { | ||
ReadFromSRAM(); | ||
UpdateAndSave(); | ||
} | ||
|
||
void LearnedCapacityBackupRestore::ReadFromStorage() { | ||
std::string buffer; | ||
|
||
if (!android::base::ReadFileToString(std::string(kSysCFPersistFile), &buffer)) { | ||
LOG(ERROR) << "Cannot read the storage file"; | ||
return; | ||
} | ||
|
||
if (sscanf(buffer.c_str(), "%d", &sw_cap_) < 1) | ||
LOG(ERROR) << "data format is wrong in the storage file: " << buffer; | ||
else | ||
LOG(INFO) << "Storage data: " << buffer; | ||
} | ||
|
||
void LearnedCapacityBackupRestore::SaveToStorage() { | ||
char strData[kBuffSize]; | ||
|
||
snprintf(strData, kBuffSize, "%d", sw_cap_); | ||
|
||
LOG(INFO) << "Save to Storage: " << strData; | ||
|
||
if (!android::base::WriteStringToFile(strData, std::string(kSysCFPersistFile))) | ||
LOG(ERROR) << "Write file error: " << strerror(errno); | ||
} | ||
|
||
void LearnedCapacityBackupRestore::ReadFromSRAM() { | ||
std::string buffer; | ||
|
||
if (!android::base::ReadFileToString(std::string(kChgFullFile), &buffer)) { | ||
LOG(ERROR) << "Read cycle counter error: " << strerror(errno); | ||
return; | ||
} | ||
|
||
buffer = android::base::Trim(buffer); | ||
|
||
if (sscanf(buffer.c_str(), "%d", &hw_cap_) < 1) | ||
LOG(ERROR) << "Failed to parse SRAM bins: " << buffer; | ||
else | ||
LOG(INFO) << "SRAM data: " << buffer; | ||
} | ||
|
||
void LearnedCapacityBackupRestore::SaveToSRAM() { | ||
char strData[kBuffSize]; | ||
|
||
snprintf(strData, kBuffSize, "%d", hw_cap_); | ||
|
||
LOG(INFO) << "Save to SRAM: " << strData; | ||
|
||
if (!android::base::WriteStringToFile(strData, std::string(kChgFullFile))) | ||
LOG(ERROR) << "Write data error: " << strerror(errno); | ||
} | ||
|
||
void LearnedCapacityBackupRestore::UpdateAndSave() { | ||
bool backup = false; | ||
bool restore = false; | ||
if (hw_cap_) { | ||
if ((hw_cap_ < sw_cap_) || (sw_cap_ == 0)) { | ||
sw_cap_ = hw_cap_; | ||
backup = true; | ||
} else if (hw_cap_ > sw_cap_) { | ||
hw_cap_ = sw_cap_; | ||
restore = true; | ||
} | ||
} | ||
if (restore) | ||
SaveToSRAM(); | ||
if (backup) | ||
SaveToStorage(); | ||
} | ||
|
||
} // namespace health | ||
} // namespace wahoo | ||
} // namespace google | ||
} // namespace device |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2018 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H | ||
#define DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H | ||
|
||
#include <android-base/file.h> | ||
#include <android-base/logging.h> | ||
#include <android-base/strings.h> | ||
#include <string> | ||
|
||
namespace device { | ||
namespace google { | ||
namespace wahoo { | ||
namespace health { | ||
|
||
class LearnedCapacityBackupRestore { | ||
public: | ||
LearnedCapacityBackupRestore(); | ||
void Restore(); | ||
void Backup(); | ||
|
||
private: | ||
int sw_cap_; | ||
int hw_cap_; | ||
|
||
void ReadFromStorage(); | ||
void SaveToStorage(); | ||
void ReadFromSRAM(); | ||
void SaveToSRAM(); | ||
void UpdateAndSave(); | ||
}; | ||
|
||
} // namespace health | ||
} // namespace wahoo | ||
} // namespace google | ||
} // namespace device | ||
|
||
#endif // #ifndef DEVICE_GOOGLE_WAHOO_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters