diff options
-rw-r--r-- | health/Android.bp | 1 | ||||
-rw-r--r-- | health/HealthService.cpp | 8 | ||||
-rw-r--r-- | health/LearnedCapacityBackupRestore.cpp | 114 | ||||
-rw-r--r-- | health/LearnedCapacityBackupRestore.h | 52 | ||||
-rw-r--r-- | init.common.rc | 5 |
5 files changed, 179 insertions, 1 deletions
diff --git a/health/Android.bp b/health/Android.bp index 1d2bc5aa..2a6c4b5c 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 93f159e0..b26efe2d 100644 --- a/health/HealthService.cpp +++ b/health/HealthService.cpp @@ -30,10 +30,12 @@ #include <sys/stat.h> #include "CycleCountBackupRestore.h" +#include "LearnedCapacityBackupRestore.h" using android::hardware::health::V2_0::StorageInfo; using android::hardware::health::V2_0::DiskStats; using ::device::google::marlin::health::CycleCountBackupRestore; +using ::device::google::marlin::health::LearnedCapacityBackupRestore; static constexpr int kBackupTrigger = 20; static constexpr size_t kDiskStatsSize = 11; @@ -43,6 +45,7 @@ static constexpr char kUFSName[] = "UFS0"; static constexpr char kDiskStatsFile[] = "/sys/block/sda/stat"; static CycleCountBackupRestore ccBackupRestore; +static LearnedCapacityBackupRestore lcBackupRestore; int cycle_count_backup(int battery_level) { @@ -74,11 +77,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; } void get_storage_info(std::vector<StorageInfo>& vec_storage_info) { diff --git a/health/LearnedCapacityBackupRestore.cpp b/health/LearnedCapacityBackupRestore.cpp new file mode 100644 index 00000000..f34b9096 --- /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 marlin { +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 marlin +} // namespace google +} // namespace device diff --git a/health/LearnedCapacityBackupRestore.h b/health/LearnedCapacityBackupRestore.h new file mode 100644 index 00000000..4e40ab5b --- /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_MARLIN_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H +#define DEVICE_GOOGLE_MARLIN_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 marlin { +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 marlin +} // namespace google +} // namespace device + +#endif // #ifndef DEVICE_GOOGLE_MARLIN_HEALTH_LEARNEDCAPACITYBACKUPRESTORE_H diff --git a/init.common.rc b/init.common.rc index 83408c86..1a9f8e47 100644 --- a/init.common.rc +++ b/init.common.rc @@ -380,6 +380,11 @@ on early-boot # HardwareInfo needs to be able to read CC bins chmod 644 /sys/devices/soc/qpnp-fg-17/cycle_counts_bins + # dumpstate needs to read, vendor.health-hal needs to be able to RW + chown system system /sys/devices/soc/qpnp-fg-17/power_supply/bms/charge_full + # HardwareInfo needs to be able to read charge_full + chmod 644 /sys/devices/soc/qpnp-fg-17/power_supply/bms/charge_full + on boot # from init.power.sh # disable thermal hotplug to switch governor |