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