Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #define LOG_TAG "pixelstats-uevent" |
| 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/parseint.h> |
| 22 | #include <android-base/strings.h> |
| 23 | #include <hardware/google/pixel/pixelstats/pixelatoms.pb.h> |
| 24 | #include <log/log.h> |
| 25 | #include <pixelstats/PcaChargeStats.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace google { |
| 30 | namespace pixel { |
| 31 | |
| 32 | using android::base::ReadFileToString; |
| 33 | using android::base::WriteStringToFile; |
| 34 | |
| 35 | bool PcaChargeStats::CheckPcaContentsAndAck(std::string *file_contents) { |
Jack Wu | 97c29d4 | 2022-06-14 17:42:58 +0800 | [diff] [blame] | 36 | std::string path = kPcaChargeMetricsPath; |
Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 37 | std::string line; |
| 38 | std::istringstream ss; |
| 39 | |
Jack Wu | 97c29d4 | 2022-06-14 17:42:58 +0800 | [diff] [blame] | 40 | if (!ReadFileToString(path.c_str(), file_contents)) { |
| 41 | path = kPca94xxChargeMetricsPath; |
| 42 | if (!ReadFileToString(path.c_str(), file_contents)) { |
| 43 | return false; |
| 44 | } |
Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | ss.str(*file_contents); |
| 48 | |
| 49 | if (!std::getline(ss, line)) { |
Jack Wu | 97c29d4 | 2022-06-14 17:42:58 +0800 | [diff] [blame] | 50 | ALOGE("Unable to read first line %s - %s", path.c_str(), strerror(errno)); |
Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
Jack Wu | 97c29d4 | 2022-06-14 17:42:58 +0800 | [diff] [blame] | 53 | if (!WriteStringToFile(std::to_string(0), path.c_str())) { |
| 54 | ALOGE("Couldn't clear %s - %s", path.c_str(), strerror(errno)); |
Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
Jack Wu | 97c29d4 | 2022-06-14 17:42:58 +0800 | [diff] [blame] | 60 | PcaChargeStats::PcaChargeStats(const std::string pca_charge_metrics_path, |
| 61 | const std::string pca94xx_charge_metrics_path) |
| 62 | : kPcaChargeMetricsPath(pca_charge_metrics_path), |
| 63 | kPca94xxChargeMetricsPath(pca94xx_charge_metrics_path) {} |
Jack Wu | 024f2f5 | 2021-07-23 13:20:03 +0800 | [diff] [blame] | 64 | |
| 65 | } // namespace pixel |
| 66 | } // namespace google |
| 67 | } // namespace hardware |
| 68 | } // namespace android |