• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
32 // The storage for save whole history is 928 byte
33 // each history contains 19 items with total size 28 byte
34 // hence the history number is 928/28~33
35 #define BATT_HIST_NUM_MAX 33
36 
37 // New history layout total size is 924 or 900 byte
38 // each history data size is 12 bytes: 900/12=75
39 #define BATT_HIST_NUM_MAX_V2 75
40 
41 /**
42  * A class to upload battery EEPROM metrics
43  */
44 class BatteryEEPROMReporter {
45   public:
46     BatteryEEPROMReporter();
47     void checkAndReport(const std::shared_ptr<IStats> &stats_client, const std::string &path);
48 
49   private:
50     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
51     // store everything in the values array at the index of the field number
52     // -2.
53     const int kVendorAtomOffset = 2;
54 
55     struct BatteryHistory {
56         /* The cycle count number; record of charge/discharge times */
57         uint16_t cycle_cnt;
58         /* The current full capacity of the battery under nominal conditions */
59         uint16_t full_cap;
60         /* The battery equivalent series resistance */
61         uint16_t esr;
62         /* Battery resistance related to temperature change */
63         uint16_t rslow;
64         /* Battery health indicator reflecting the battery age state */
65         uint8_t soh;
66         /* The battery temperature */
67         int8_t batt_temp;
68         /* Battery state of charge (SOC) shutdown point */
69         uint8_t cutoff_soc;
70         /* Raw battery state of charge (SOC), based on battery current (CC = Coulomb Counter) */
71         uint8_t cc_soc;
72         /* Estimated battery state of charge (SOC) from batt_soc with endpoint limiting
73          * (0% and 100%)
74          */
75         uint8_t sys_soc;
76         /* Filtered monotonic SOC, handles situations where the cutoff_soc is increased and
77          * then decreased from the battery physical properties
78          */
79         uint8_t msoc;
80         /* Estimated SOC derived from cc_soc that provides voltage loop feedback correction using
81          * battery voltage, current, and status values
82          */
83         uint8_t batt_soc;
84 
85         /* Field used for data padding in the EEPROM data */
86         uint8_t reserve;
87 
88         /* The maximum battery temperature ever seen */
89         int8_t max_temp;
90         /* The minimum battery temperature ever seen */
91         int8_t min_temp;
92         /* The maximum battery voltage ever seen */
93         uint16_t max_vbatt;
94         /* The minimum battery voltage ever seen */
95         uint16_t min_vbatt;
96         /* The maximum battery current ever seen */
97         int16_t max_ibatt;
98         /* The minimum battery current ever seen */
99         int16_t min_ibatt;
100         /* Field used to verify the integrity of the EEPROM data */
101         uint16_t checksum;
102         /* Extend data for P21 */
103         /* Temperature compensation information */
104         uint16_t tempco;
105         /* Learned characterization related to the voltage gauge */
106         uint16_t rcomp0;
107         /* For time to monitor the life of cell */
108         uint8_t timer_h;
109          /* The full capacity of the battery learning at the end of every charge cycle */
110         uint16_t full_rep;
111     };
112     /* The number of elements in struct BatteryHistory for P20 series */
113     const int kNumBatteryHistoryFields = 19;
114 
115     /* P21 history format */
116     struct BatteryHistoryExtend {
117         uint16_t tempco;
118         uint16_t rcomp0;
119         uint8_t timer_h;
120         unsigned fullcapnom:10;
121         unsigned fullcaprep:10;
122         unsigned mixsoc:6;
123         unsigned vfsoc:6;
124         unsigned maxvolt:4;
125         unsigned minvolt:4;
126         unsigned maxtemp:4;
127         unsigned mintemp:4;
128         unsigned maxchgcurr:4;
129         unsigned maxdischgcurr:4;
130     };
131 
132     int64_t report_time_ = 0;
133     int64_t getTimeSecs();
134 
135     bool checkLogEvent(struct BatteryHistory hist);
136     void reportEvent(const std::shared_ptr<IStats> &stats_client,
137                      const struct BatteryHistory &hist);
138 };
139 
140 }  // namespace pixel
141 }  // namespace google
142 }  // namespace hardware
143 }  // namespace android
144 
145 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYEEPROMREPORTER_H
146