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