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 <android-base/file.h> 22 #include <android-base/logging.h> 23 #include <android-base/strings.h> 24 #include <batteryservice/BatteryService.h> 25 #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h> 26 #include <math.h> 27 #include <time.h> 28 #include <utils/Timers.h> 29 30 #include <string> 31 32 namespace hardware { 33 namespace google { 34 namespace pixel { 35 namespace health { 36 37 using aidl::android::frameworks::stats::IStats; 38 using android::hardware::google::pixel::PixelAtoms::VendorBatteryHealthSnapshot; 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 VendorBatteryHealthSnapshot::BATTERY_SNAPSHOT_TYPE_MIN_CURRENT, 62 VendorBatteryHealthSnapshot::BATTERY_SNAPSHOT_TYPE_MIN_VOLTAGE, 63 VendorBatteryHealthSnapshot::BATTERY_SNAPSHOT_TYPE_MIN_TEMP, 64 VendorBatteryHealthSnapshot::BATTERY_SNAPSHOT_TYPE_MIN_BATT_LEVEL, 65 VendorBatteryHealthSnapshot::BATTERY_SNAPSHOT_TYPE_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(const std::shared_ptr<IStats> &stats_client, sampleType type); 92 bool uploadAverageBatteryResistance(const std::shared_ptr<IStats> &stats_client); 93 }; 94 95 } // namespace health 96 } // namespace pixel 97 } // namespace google 98 } // namespace hardware 99 100 #endif 101