1 /* 2 * Copyright (C) 2020 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_PIXELSTATS_BATTERYCAPACITYREPORTER_H 18 #define HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYCAPACITYREPORTER_H 19 20 #include <aidl/android/frameworks/stats/IStats.h> 21 22 namespace android { 23 namespace hardware { 24 namespace google { 25 namespace pixel { 26 27 using aidl::android::frameworks::stats::IStats; 28 29 #define MAX_LOG_EVENTS_PER_HOUR 4 30 31 /** 32 * A class to upload battery capacity metrics 33 */ 34 class BatteryCapacityReporter { 35 public: 36 BatteryCapacityReporter(); 37 void checkAndReport(const std::shared_ptr<IStats> &stats_client, const std::string &path); 38 39 private: 40 int64_t getTimeSecs(); 41 42 bool parse(const std::string &path); 43 bool checkLogEvent(void); 44 bool shouldReportEvent(void); 45 void reportEvent(const std::shared_ptr<IStats> &stats_client); 46 47 /** 48 * SOC status translation from sysfs node 49 */ 50 enum SOCStatus { 51 SOC_STATUS_UNKNOWN = 0, 52 SOC_STATUS_CONNECTED = 1, 53 SOC_STATUS_DISCONNECTED = 2, 54 SOC_STATUS_FULL = 3, 55 }; 56 57 enum LogReason { 58 LOG_REASON_UNKNOWN = 0, 59 LOG_REASON_CONNECTED = 1, 60 LOG_REASON_DISCONNECTED = 2, 61 LOG_REASON_FULL_CHARGE = 3, 62 LOG_REASON_PERCENT_SKIP = 4, 63 LOG_REASON_DIVERGING_FG = 5, 64 }; 65 66 SOCStatus status_ = SOC_STATUS_UNKNOWN; 67 SOCStatus status_previous_ = SOC_STATUS_UNKNOWN; 68 float gdf_ = 0.0; 69 float ssoc_ = 0.0f; 70 float gdf_curve_ = 0.0f; 71 float ssoc_curve_ = 0.0f; 72 float ssoc_previous_ = -1.0f; 73 float ssoc_gdf_diff_previous_ = 0.0f; 74 LogReason log_reason_ = LOG_REASON_UNKNOWN; 75 76 int num_events_in_last_hour_ = 0; 77 int64_t log_event_time_secs_[MAX_LOG_EVENTS_PER_HOUR] = {0}; 78 79 // Proto messages are 1-indexed and VendorAtom field numbers start at 2, so 80 // store everything in the values array at the index of the field number 81 // -2. 82 const int kVendorAtomOffset = 2; 83 }; 84 85 } // namespace pixel 86 } // namespace google 87 } // namespace hardware 88 } // namespace android 89 90 #endif // HARDWARE_GOOGLE_PIXEL_PIXELSTATS_BATTERYCAPACITYREPORTER_H 91