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