From 2fa48ee4fec4aca518270f64a534b88e7d6eadb3 Mon Sep 17 00:00:00 2001 From: Jack Wu Date: Mon, 7 May 2018 11:57:40 +0800 Subject: [PATCH] battery learned capacity: backup/restore to/from persist data Test: - delete file /persist/battery/qcom_charge_full - adb logcat to check code flow related health@2.0 - 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 --- health/Android.bp | 1 + health/HealthService.cpp | 8 +- health/LearnedCapacityBackupRestore.cpp | 114 ++++++++++++++++++++++++ health/LearnedCapacityBackupRestore.h | 52 +++++++++++ init.hardware.rc | 5 ++ 5 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 health/LearnedCapacityBackupRestore.cpp create mode 100644 health/LearnedCapacityBackupRestore.h diff --git a/health/Android.bp b/health/Android.bp index ba2414823..d78a7c045 100644 --- a/health/Android.bp +++ b/health/Android.bp @@ -21,6 +21,7 @@ cc_binary { srcs: [ "HealthService.cpp", "CycleCountBackupRestore.cpp", + "LearnedCapacityBackupRestore.cpp", ], cflags: [ diff --git a/health/HealthService.cpp b/health/HealthService.cpp index 45d18c302..677ca659c 100644 --- a/health/HealthService.cpp +++ b/health/HealthService.cpp @@ -28,13 +28,16 @@ #include #include "CycleCountBackupRestore.h" +#include "LearnedCapacityBackupRestore.h" using android::hardware::health::V2_0::StorageInfo; using android::hardware::health::V2_0::DiskStats; using ::device::google::wahoo::health::CycleCountBackupRestore; +using ::device::google::wahoo::health::LearnedCapacityBackupRestore; static constexpr int kBackupTrigger = 20; static CycleCountBackupRestore ccBackupRestore; +static LearnedCapacityBackupRestore lcBackupRestore; int cycle_count_backup(int battery_level) { @@ -66,11 +69,14 @@ int cycle_count_backup(int battery_level) void healthd_board_init(struct healthd_config*) { ccBackupRestore.Restore(); + lcBackupRestore.Restore(); } int healthd_board_battery_update(struct android::BatteryProperties *props) { - return cycle_count_backup(props->batteryLevel); + cycle_count_backup(props->batteryLevel); + lcBackupRestore.Backup(); + return 0; } const char kUFSHealthFile[] = "/sys/kernel/debug/ufshcd0/dump_health_desc"; diff --git a/health/LearnedCapacityBackupRestore.cpp b/health/LearnedCapacityBackupRestore.cpp new file mode 100644 index 000000000..48fe46109 --- /dev/null +++ b/health/LearnedCapacityBackupRestore.cpp @@ -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 diff --git a/health/LearnedCapacityBackupRestore.h b/health/LearnedCapacityBackupRestore.h new file mode 100644 index 000000000..13d38e25a --- /dev/null +++ b/health/LearnedCapacityBackupRestore.h @@ -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 +#include +#include +#include + +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 diff --git a/init.hardware.rc b/init.hardware.rc index fef62570f..ed6eb2023 100644 --- a/init.hardware.rc +++ b/init.hardware.rc @@ -437,6 +437,11 @@ on early-boot # HardwareInfo needs to be able to read CC bins chmod 644 /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/cycle_counts_bins + # dumpstate needs to read, vendor.health-hal needs to be able to RW + chown system system /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/power_supply/bms/charge_full + # HardwareInfo needs to be able to read charge_full + chmod 644 /sys/devices/soc/800f000.qcom,spmi/spmi-0/spmi0-02/800f000.qcom,spmi:qcom,pmi8998@2:qpnp,fg/power_supply/bms/charge_full + on boot mkdir /dev/socket/qmux_radio 0770 radio radio chmod 2770 /dev/socket/qmux_radio