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 /**
38  * A class to upload battery EEPROM metrics
39  */
40 class BatteryEEPROMReporter {
41   public:
42     BatteryEEPROMReporter();
43     void checkAndReport(const std::shared_ptr<IStats> &stats_client, const std::string &path);
44 
45   private:
46     // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so
47     // store everything in the values array at the index of the field number
48     // -2.
49     const int kVendorAtomOffset = 2;
50 
51     struct BatteryHistory {
52         /* The cycle count number; record of charge/discharge times */
53         uint16_t cycle_cnt;
54         /* The current full capacity of the battery under nominal conditions */
55         uint16_t full_cap;
56         /* The battery equivalent series resistance */
57         uint16_t esr;
58         /* Battery resistance related to temperature change */
59         uint16_t rslow;
60         /* Battery health indicator reflecting the battery age state */
61         uint8_t soh;
62         /* The battery temperature */
63         int8_t batt_temp;
64         /* Battery state of charge (SOC) shutdown point */
65         uint8_t cutoff_soc;
66         /* Raw battery state of charge (SOC), based on battery current (CC = Coulomb Counter) */
67         uint8_t cc_soc;
68         /* Estimated battery state of charge (SOC) from batt_soc with endpoint limiting
69          * (0% and 100%)
70          */
71         uint8_t sys_soc;
72         /* Filtered monotonic SOC, handles situations where the cutoff_soc is increased and
73          * then decreased from the battery physical properties
74          */
75         uint8_t msoc;
76         /* Estimated SOC derived from cc_soc that provides voltage loop feedback correction using
77          * battery voltage, current, and status values
78          */
79         uint8_t batt_soc;
80 
81         /* Field used for data padding in the EEPROM data */
82         uint8_t reserve;
83 
84         /* The maximum battery temperature ever seen */
85         int8_t max_temp;
86         /* The minimum battery temperature ever seen */
87         int8_t min_temp;
88         /* The maximum battery voltage ever seen */
89         uint16_t max_vbatt;
90         /* The minimum battery voltage ever seen */
91         uint16_t min_vbatt;
92         /* The maximum battery current ever seen */
93         int16_t max_ibatt;
94         /* The minimum battery current ever seen */
95         int16_t min_ibatt;
96         /* Field used to verify the integrity of the EEPROM data */
97         uint16_t checksum;
98     };
99     /* The number of elements in struct BatteryHistory */
100     const int kNumBatteryHistoryFields = 19;
101 
102     int64_t report_time_ = 0;
103     int64_t getTimeSecs();
104 
105     bool checkLogEvent(struct BatteryHistory hist);
106     void reportEvent(const std::shared_ptr<IStats> &stats_client,
107                      const struct BatteryHistory &hist);
108 };
109 
110 }  // namespace pixel
111 }  // namespace google
112 }  // namespace hardware
113 }  // namespace android
114 
115 #endif  // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYEEPROMREPORTER_H
116