summaryrefslogtreecommitdiff
path: root/pixelstats/BatteryHealthReporter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pixelstats/BatteryHealthReporter.cpp')
-rw-r--r--pixelstats/BatteryHealthReporter.cpp97
1 files changed, 84 insertions, 13 deletions
diff --git a/pixelstats/BatteryHealthReporter.cpp b/pixelstats/BatteryHealthReporter.cpp
index 027f7c4..66d6002 100644
--- a/pixelstats/BatteryHealthReporter.cpp
+++ b/pixelstats/BatteryHealthReporter.cpp
@@ -37,6 +37,7 @@ using aidl::android::frameworks::stats::VendorAtomValue;
using android::base::ReadFileToString;
using android::base::WriteStringToFile;
using android::hardware::google::pixel::PixelAtoms::BatteryHealthStatus;
+using android::hardware::google::pixel::PixelAtoms::BatteryHealthUsage;
const int SECONDS_PER_MONTH = 60 * 60 * 24 * 30;
@@ -46,8 +47,27 @@ int64_t BatteryHealthReporter::getTimeSecs(void) {
return nanoseconds_to_seconds(systemTime(SYSTEM_TIME_BOOTTIME));
}
-void BatteryHealthReporter::reportBatteryHealthStatus(const std::shared_ptr<IStats> &stats_client,
- const char *line) {
+bool BatteryHealthReporter::reportBatteryHealthStatus(const std::shared_ptr<IStats> &stats_client) {
+ std::string path = kBatteryHealthStatusPath;
+ std::string file_contents, line;
+ std::istringstream ss;
+
+ if (!ReadFileToString(path.c_str(), &file_contents)) {
+ ALOGD("Unsupported path %s - %s", path.c_str(), strerror(errno));
+ return false;
+ }
+
+ ss.str(file_contents);
+
+ while (std::getline(ss, line)) {
+ reportBatteryHealthStatusEvent(stats_client, line.c_str());
+ }
+
+ return true;
+}
+
+void BatteryHealthReporter::reportBatteryHealthStatusEvent(
+ const std::shared_ptr<IStats> &stats_client, const char *line) {
int health_status_stats_fields[] = {
BatteryHealthStatus::kHealthAlgorithmFieldNumber,
BatteryHealthStatus::kHealthStatusFieldNumber,
@@ -89,27 +109,78 @@ void BatteryHealthReporter::reportBatteryHealthStatus(const std::shared_ptr<ISta
ALOGE("Unable to report BatteryHealthStatus to Stats service");
}
-void BatteryHealthReporter::checkAndReportStatus(const std::shared_ptr<IStats> &stats_client) {
+bool BatteryHealthReporter::reportBatteryHealthUsage(const std::shared_ptr<IStats> &stats_client) {
+ std::string path = kBatteryHealthUsagePath;
std::string file_contents, line;
std::istringstream ss;
- std::string path = kBatteryHealthStatusPath;
- int64_t now = getTimeSecs();
- if ((report_time_ != 0) && (now - report_time_ < SECONDS_PER_MONTH)) {
- ALOGD("Do not upload yet. now: %" PRId64 ", pre: %" PRId64, now, report_time_);
+ if (!ReadFileToString(path.c_str(), &file_contents)) {
+ ALOGD("Unsupported path %s - %s", path.c_str(), strerror(errno));
+ return false;
+ }
+
+ ss.str(file_contents);
+
+ // skip first title line
+ if (!std::getline(ss, line)) {
+ ALOGE("Unable to read first line of: %s", path.c_str());
+ return false;
+ }
+
+ while (std::getline(ss, line)) {
+ reportBatteryHealthUsageEvent(stats_client, line.c_str());
+ }
+
+ return true;
+}
+
+void BatteryHealthReporter::reportBatteryHealthUsageEvent(
+ const std::shared_ptr<IStats> &stats_client, const char *line) {
+ int health_status_stats_fields[] = {
+ BatteryHealthUsage::kTemperatureLimitDeciCFieldNumber,
+ BatteryHealthUsage::kSocLimitFieldNumber,
+ BatteryHealthUsage::kChargeTimeSecsFieldNumber,
+ BatteryHealthUsage::kDischargeTimeSecsFieldNumber,
+ };
+
+ const int32_t vtier_fields_size = std::size(health_status_stats_fields);
+ static_assert(vtier_fields_size == 4, "Unexpected battery health status fields size");
+ std::vector<VendorAtomValue> values(vtier_fields_size);
+ VendorAtomValue val;
+ int32_t i = 0, tmp[vtier_fields_size] = {0};
+
+ // temp/soc charge(s) discharge(s)
+ if (sscanf(line, "%d/%d\t%d\t%d", &tmp[0], &tmp[1], &tmp[2], &tmp[3]) != vtier_fields_size) {
+ /* If format isn't as expected, then ignore line on purpose */
return;
}
- if (!ReadFileToString(path.c_str(), &file_contents)) {
- ALOGE("Unable to read %s - %s", path.c_str(), strerror(errno));
+ ALOGD("BatteryHealthUsage: processed %s", line);
+ for (i = 0; i < vtier_fields_size; i++) {
+ val.set<VendorAtomValue::intValue>(tmp[i]);
+ values[health_status_stats_fields[i] - kVendorAtomOffset] = val;
+ }
+
+ VendorAtom event = {.reverseDomainName = "",
+ .atomId = PixelAtoms::Atom::kBatteryHealthUsage,
+ .values = std::move(values)};
+ const ndk::ScopedAStatus ret = stats_client->reportVendorAtom(event);
+ if (!ret.isOk())
+ ALOGE("Unable to report BatteryHealthStatus to Stats service");
+}
+
+void BatteryHealthReporter::checkAndReportStatus(const std::shared_ptr<IStats> &stats_client) {
+ int64_t now = getTimeSecs();
+ if ((report_time_ != 0) && (now - report_time_ < SECONDS_PER_MONTH)) {
+ ALOGD("Do not upload yet. now: %" PRId64 ", pre: %" PRId64, now, report_time_);
return;
}
- ss.str(file_contents);
+ bool successStatus = reportBatteryHealthStatus(stats_client);
+ bool successUsage = reportBatteryHealthUsage(stats_client);
- report_time_ = now;
- while (std::getline(ss, line)) {
- reportBatteryHealthStatus(stats_client, line.c_str());
+ if (successStatus && successUsage) {
+ report_time_ = now;
}
}