• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYMETRICSLOGGER_H
18 #define HARDWARE_GOOGLE_PIXEL_HEALTH_BATTERYMETRICSLOGGER_H
19 
20 #include <android-base/file.h>
21 #include <android-base/logging.h>
22 #include <android-base/strings.h>
23 #include <batteryservice/BatteryService.h>
24 #include <math.h>
25 #include <time.h>
26 #include <utils/Timers.h>
27 #include <string>
28 
29 #include <android/frameworks/stats/1.0/IStats.h>
30 
31 namespace hardware {
32 namespace google {
33 namespace pixel {
34 namespace health {
35 
36 using android::sp;
37 using android::frameworks::stats::V1_0::BatteryHealthSnapshotArgs;
38 using android::frameworks::stats::V1_0::IStats;
39 
40 class BatteryMetricsLogger {
41   public:
42     BatteryMetricsLogger(const char *const batt_res, const char *const batt_ocv,
43                          int sample_period = TEN_MINUTES_SEC, int upload_period = ONE_DAY_SEC);
44     void logBatteryProperties(struct android::BatteryProperties *props);
45 
46   private:
47     enum sampleType {
48         TIME,        // time in seconds
49         CURR,        // current in mA
50         VOLT,        // voltage in mV
51         TEMP,        // temp in deci-degC
52         SOC,         // SoC in % battery level
53         RES,         // resistance in milli-ohms
54         OCV,         // open-circuit voltage in mV
55         NUM_FIELDS,  // do not reference
56     };
57 
58     const int kStatsSnapshotType[NUM_FIELDS] = {
59             -1,
60             (int)BatteryHealthSnapshotArgs::BatterySnapshotType::MIN_CURRENT,
61             (int)BatteryHealthSnapshotArgs::BatterySnapshotType::MIN_VOLTAGE,
62             (int)BatteryHealthSnapshotArgs::BatterySnapshotType::MIN_TEMP,
63             (int)BatteryHealthSnapshotArgs::BatterySnapshotType::MIN_BATT_LEVEL,
64             (int)BatteryHealthSnapshotArgs::BatterySnapshotType::MIN_RESISTANCE,
65             -1,
66     };
67 
68     const char *const kBatteryResistance;
69     const char *const kBatteryOCV;
70     const int kSamplePeriod;
71     const int kUploadPeriod;
72     const int kMaxSamples;
73     static constexpr int TEN_MINUTES_SEC = 10 * 60;
74     static constexpr int ONE_DAY_SEC = 24 * 60 * 60;
75 
76     // min and max are referenced by type in both the X and Y axes
77     // i.e. min[TYPE] is the event where the minimum of that type occurred, and
78     // min[TYPE][TYPE] is the reading of that type at that minimum event
79     int32_t min_[NUM_FIELDS][NUM_FIELDS];
80     int32_t max_[NUM_FIELDS][NUM_FIELDS];
81     int32_t num_res_samples_;   // number of res samples since last upload
82     int32_t num_samples_;       // number of min/max samples since last upload
83     int64_t accum_resistance_;  // accumulative resistance
84     int64_t last_sample_;       // time in seconds since boot of last sample
85     int64_t last_upload_;       // time in seconds since boot of last upload
86 
87     int64_t getTime();
88     bool recordSample(struct android::BatteryProperties *props);
89     bool uploadMetrics();
90     bool uploadOutlierMetric(sp<IStats> stats_client, sampleType type);
91 };
92 
93 }  // namespace health
94 }  // namespace pixel
95 }  // namespace google
96 }  // namespace hardware
97 
98 #endif
99