1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "LearnedCapacityBackupRestore.h" 18 19 namespace device { 20 namespace google { 21 namespace wahoo { 22 namespace health { 23 24 static constexpr char kChgFullFile[] = "sys/class/power_supply/bms/charge_full"; 25 static constexpr char kSysCFPersistFile[] = "/persist/battery/qcom_charge_full"; 26 static constexpr int kBuffSize = 256; 27 28 LearnedCapacityBackupRestore::LearnedCapacityBackupRestore() : sw_cap_(0), hw_cap_(0) {} 29 30 void LearnedCapacityBackupRestore::Restore() { 31 ReadFromStorage(); 32 ReadFromSRAM(); 33 if (sw_cap_ == 0) { 34 // First backup 35 sw_cap_ = hw_cap_; 36 SaveToStorage(); 37 } else { 38 // Always restore backup value 39 SaveToSRAM(); 40 } 41 } 42 43 void LearnedCapacityBackupRestore::Backup() { 44 ReadFromSRAM(); 45 if (sw_cap_ != hw_cap_) { 46 // Always backup the new FG computed learned capacity 47 sw_cap_ = hw_cap_; 48 SaveToStorage(); 49 } 50 } 51 52 void LearnedCapacityBackupRestore::ReadFromStorage() { 53 std::string buffer; 54 55 if (!android::base::ReadFileToString(std::string(kSysCFPersistFile), &buffer)) { 56 LOG(ERROR) << "Cannot read the storage file"; 57 return; 58 } 59 60 if (sscanf(buffer.c_str(), "%d", &sw_cap_) < 1) 61 LOG(ERROR) << "data format is wrong in the storage file: " << buffer; 62 else 63 LOG(INFO) << "Storage data: " << buffer; 64 } 65 66 void LearnedCapacityBackupRestore::SaveToStorage() { 67 char strData[kBuffSize]; 68 69 snprintf(strData, kBuffSize, "%d", sw_cap_); 70 71 LOG(INFO) << "Save to Storage: " << strData; 72 73 if (!android::base::WriteStringToFile(strData, std::string(kSysCFPersistFile))) 74 LOG(ERROR) << "Write file error: " << strerror(errno); 75 } 76 77 void LearnedCapacityBackupRestore::ReadFromSRAM() { 78 std::string buffer; 79 80 if (!android::base::ReadFileToString(std::string(kChgFullFile), &buffer)) { 81 LOG(ERROR) << "Read cycle counter error: " << strerror(errno); 82 return; 83 } 84 85 buffer = android::base::Trim(buffer); 86 87 if (sscanf(buffer.c_str(), "%d", &hw_cap_) < 1) 88 LOG(ERROR) << "Failed to parse SRAM bins: " << buffer; 89 else 90 LOG(INFO) << "SRAM data: " << buffer; 91 } 92 93 void LearnedCapacityBackupRestore::SaveToSRAM() { 94 char strData[kBuffSize]; 95 96 snprintf(strData, kBuffSize, "%d", hw_cap_); 97 98 LOG(INFO) << "Save to SRAM: " << strData; 99 100 if (!android::base::WriteStringToFile(strData, std::string(kChgFullFile))) 101 LOG(ERROR) << "Write data error: " << strerror(errno); 102 } 103 104 } // namespace health 105 } // namespace wahoo 106 } // namespace google 107 } // namespace device 108