1 /*
2  * Copyright (C) 2020 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 #ifndef HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYEEPROMREPORTER_H
18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYEEPROMREPORTER_H
19 
20 #include <cstdint>
21 #include <string>
22 
23 #include <aidl/android/frameworks/stats/IStats.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace google {
28 namespace pixel {
29 
30 using aidl::android::frameworks::stats::IStats;
31 using aidl::android::frameworks::stats::VendorAtomValue;
32 
33 /**
34  * A class to upload battery EEPROM metrics
35  */
36 class BatteryEEPROMReporter {
37   public:
38     BatteryEEPROMReporter();
39     void checkAndReport(const std::shared_ptr<IStats> &stats_client, const std::string &path);
40     void checkAndReportGMSR(const std::shared_ptr<IStats> &stats_client, const std::vector<std::string> &paths);
41     void checkAndReportMaxfgHistory(const std::shared_ptr<IStats> &stats_client,
42                                     const std::string &path);
43     void checkAndReportFGLearning(const std::shared_ptr<IStats> &stats_client,
44                                   const std::vector<std::string> &paths);
45     void checkAndReportFGModelLoading(const std::shared_ptr<IStats> &stats_client,
46                                       const std::vector<std::string> &paths);
47     void checkAndReportValidation(const std::shared_ptr<IStats> &stats_client,
48                                   const std::vector<std::string> &paths);
49 
50   private:
51     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
52     // store everything in the values array at the index of the field number
53     // -2.
54     const int kVendorAtomOffset = 2;
55 
56     struct BatteryHistory {
57         /* The cycle count number; record of charge/discharge times */
58         uint16_t cycle_cnt;
59         /* The current full capacity of the battery under nominal conditions */
60         uint16_t full_cap;
61         /* The battery equivalent series resistance */
62         uint16_t esr;
63         /* Battery resistance related to temperature change */
64         uint16_t rslow;
65         /* Battery health indicator reflecting the battery age state */
66         uint8_t soh;
67         /* The battery temperature */
68         int8_t batt_temp;
69         /* Battery state of charge (SOC) shutdown point */
70         uint8_t cutoff_soc;
71         /* Raw battery state of charge (SOC), based on battery current (CC = Coulomb Counter) */
72         uint8_t cc_soc;
73         /* Estimated battery state of charge (SOC) from batt_soc with endpoint limiting
74          * (0% and 100%)
75          */
76         uint8_t sys_soc;
77         /* Filtered monotonic SOC, handles situations where the cutoff_soc is increased and
78          * then decreased from the battery physical properties
79          */
80         uint8_t msoc;
81         /* Estimated SOC derived from cc_soc that provides voltage loop feedback correction using
82          * battery voltage, current, and status values
83          */
84         uint8_t batt_soc;
85 
86         /* Field used for data padding in the EEPROM data */
87         uint8_t reserve;
88 
89         /* The maximum battery temperature ever seen */
90         int8_t max_temp;
91         /* The minimum battery temperature ever seen */
92         int8_t min_temp;
93         /* The maximum battery voltage ever seen */
94         uint16_t max_vbatt;
95         /* The minimum battery voltage ever seen */
96         uint16_t min_vbatt;
97         /* The maximum battery current ever seen */
98         int16_t max_ibatt;
99         /* The minimum battery current ever seen */
100         int16_t min_ibatt;
101         /* Field used to verify the integrity of the EEPROM data */
102         uint16_t checksum;
103         /* Extend data for P21 */
104         /* Temperature compensation information */
105         uint16_t tempco;
106         /* Learned characterization related to the voltage gauge */
107         uint16_t rcomp0;
108         /* For time to monitor the life of cell */
109         uint8_t timer_h;
110          /* The full capacity of the battery learning at the end of every charge cycle */
111         uint16_t full_rep;
112         /* The battery pairing state */
113         int16_t battery_pairing;
114     };
115     /* The number of elements for relaxation event */
116     const int kNumFGLearningFieldsV2 = 16;
117     /* with additional unix time field */
118     const int kNumFGLearningFieldsV3 = 17;
119     unsigned int last_lh_check_ = 0;
120     /* The number of elements for history validation event */
121     const int kNumValidationFields = 4;
122     unsigned int last_hv_check_ = 0;
123 
124     /* P21+ history format */
125     struct BatteryHistoryRawFormat {
126         uint16_t tempco;
127         uint16_t rcomp0;
128         uint8_t timer_h;
129         unsigned fullcapnom:10;
130         unsigned fullcaprep:10;
131         unsigned mixsoc:6;
132         unsigned vfsoc:6;
133         unsigned maxvolt:4;
134         unsigned minvolt:4;
135         unsigned maxtemp:4;
136         unsigned mintemp:4;
137         unsigned maxchgcurr:4;
138         unsigned maxdischgcurr:4;
139     };
140 
141     struct BatteryHistoryInt32 {
142         int32_t cycle_cnt;
143         int32_t full_cap;
144         int32_t esr;
145         int32_t rslow;
146         int32_t soh;
147         int32_t batt_temp;
148         int32_t cutoff_soc;
149         int32_t cc_soc;
150         int32_t sys_soc;
151         int32_t msoc;
152         int32_t batt_soc;
153         int32_t reserve;
154         int32_t max_temp;
155         int32_t min_temp;
156         int32_t max_vbatt;
157         int32_t min_vbatt;
158         int32_t max_ibatt;
159         int32_t min_ibatt;
160         int32_t checksum;
161         int32_t tempco;
162         int32_t rcomp0;
163         int32_t timer_h;
164         int32_t full_rep;
165     };
166 
167     int64_t report_time_ = 0;
168     int64_t getTimeSecs();
169 
170     bool checkLogEvent(struct BatteryHistory hist);
171     void reportEvent(const std::shared_ptr<IStats> &stats_client,
172                      const struct BatteryHistory &hist);
173     void reportEventInt32(const std::shared_ptr<IStats> &stats_client,
174                      const struct BatteryHistoryInt32 &hist);
175     void setAtomFieldValue(std::vector<VendorAtomValue> *values, int offset, int content);
176     bool ReadFileToInt(const std::string &path, int16_t *val);
177 
178     const int kNum77759GMSRFields = 11;
179     const int kNum77779GMSRFields = 9;
180     const int kNum17201HISTFields = 16;
181 
182     const std::string kBatteryPairingPath = "/sys/class/power_supply/battery/pairing_state";
183 };
184 
185 }  // namespace pixel
186 }  // namespace google
187 }  // namespace hardware
188 }  // namespace android
189 
190 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYEEPROMREPORTER_H
191