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
LearnedCapacityBackupRestore()28 LearnedCapacityBackupRestore::LearnedCapacityBackupRestore() {}
29
Restore()30 void LearnedCapacityBackupRestore::Restore() {
31 ReadFromStorage();
32 ReadFromSRAM();
33 UpdateAndSave();
34 }
35
Backup()36 void LearnedCapacityBackupRestore::Backup() {
37 ReadFromSRAM();
38 UpdateAndSave();
39 }
40
ReadFromStorage()41 void LearnedCapacityBackupRestore::ReadFromStorage() {
42 std::string buffer;
43
44 if (!android::base::ReadFileToString(std::string(kSysCFPersistFile), &buffer)) {
45 LOG(ERROR) << "Cannot read the storage file";
46 return;
47 }
48
49 if (sscanf(buffer.c_str(), "%d", &sw_cap_) < 1)
50 LOG(ERROR) << "data format is wrong in the storage file: " << buffer;
51 else
52 LOG(INFO) << "Storage data: " << buffer;
53 }
54
SaveToStorage()55 void LearnedCapacityBackupRestore::SaveToStorage() {
56 char strData[kBuffSize];
57
58 snprintf(strData, kBuffSize, "%d", sw_cap_);
59
60 LOG(INFO) << "Save to Storage: " << strData;
61
62 if (!android::base::WriteStringToFile(strData, std::string(kSysCFPersistFile)))
63 LOG(ERROR) << "Write file error: " << strerror(errno);
64 }
65
ReadFromSRAM()66 void LearnedCapacityBackupRestore::ReadFromSRAM() {
67 std::string buffer;
68
69 if (!android::base::ReadFileToString(std::string(kChgFullFile), &buffer)) {
70 LOG(ERROR) << "Read cycle counter error: " << strerror(errno);
71 return;
72 }
73
74 buffer = android::base::Trim(buffer);
75
76 if (sscanf(buffer.c_str(), "%d", &hw_cap_) < 1)
77 LOG(ERROR) << "Failed to parse SRAM bins: " << buffer;
78 else
79 LOG(INFO) << "SRAM data: " << buffer;
80 }
81
SaveToSRAM()82 void LearnedCapacityBackupRestore::SaveToSRAM() {
83 char strData[kBuffSize];
84
85 snprintf(strData, kBuffSize, "%d", hw_cap_);
86
87 LOG(INFO) << "Save to SRAM: " << strData;
88
89 if (!android::base::WriteStringToFile(strData, std::string(kChgFullFile)))
90 LOG(ERROR) << "Write data error: " << strerror(errno);
91 }
92
UpdateAndSave()93 void LearnedCapacityBackupRestore::UpdateAndSave() {
94 bool backup = false;
95 bool restore = false;
96 if (hw_cap_) {
97 if ((hw_cap_ < sw_cap_) || (sw_cap_ == 0)) {
98 sw_cap_ = hw_cap_;
99 backup = true;
100 } else if (hw_cap_ > sw_cap_) {
101 hw_cap_ = sw_cap_;
102 restore = true;
103 }
104 }
105 if (restore)
106 SaveToSRAM();
107 if (backup)
108 SaveToStorage();
109 }
110
111 } // namespace health
112 } // namespace wahoo
113 } // namespace google
114 } // namespace device
115