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
CheckPcaContentsAndAck(std::string * file_contents)35 bool PcaChargeStats::CheckPcaContentsAndAck(std::string *file_contents) {
36 std::string line;
37 std::istringstream ss;
38
39 if (!ReadFileToString(kPcaChargeMetricsPath.c_str(), file_contents)) {
40 return false;
41 }
42
43 ss.str(*file_contents);
44
45 if (!std::getline(ss, line)) {
46 ALOGE("Unable to read first line %s - %s", kPcaChargeMetricsPath.c_str(), strerror(errno));
47 return false;
48 }
49 if (!WriteStringToFile(std::to_string(0), kPcaChargeMetricsPath.c_str())) {
50 ALOGE("Couldn't clear %s - %s", kPcaChargeMetricsPath.c_str(), strerror(errno));
51 return false;
52 }
53 return true;
54 }
55
PcaChargeStats(const std::string pca_charge_metrics_path)56 PcaChargeStats::PcaChargeStats(const std::string pca_charge_metrics_path)
57 : kPcaChargeMetricsPath(pca_charge_metrics_path) {}
58
59 } // namespace pixel
60 } // namespace google
61 } // namespace hardware
62 } // namespace android
63