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 path = kPcaChargeMetricsPath;
37     std::string line;
38     std::istringstream ss;
39 
40     if (!ReadFileToString(path.c_str(), file_contents)) {
41         if (!ReadFileToString(kPca94xxChargeMetricsPath.c_str(), file_contents)) {
42             path = kDcChargeMetricsPath;
43         } else {
44             path = kPca94xxChargeMetricsPath;
45         }
46         if (!ReadFileToString(path.c_str(), file_contents)) {
47             return false;
48         }
49     }
50 
51     ss.str(*file_contents);
52 
53     if (!std::getline(ss, line)) {
54         ALOGE("Unable to read first line %s - %s", path.c_str(), strerror(errno));
55         return false;
56     }
57     if (!WriteStringToFile(std::to_string(0), path.c_str())) {
58         ALOGE("Couldn't clear %s - %s", path.c_str(), strerror(errno));
59         return false;
60     }
61     return true;
62 }
63 
PcaChargeStats(const std::string pca_charge_metrics_path,const std::string pca94xx_charge_metrics_path,const std::string dc_charge_metrics_path)64 PcaChargeStats::PcaChargeStats(const std::string pca_charge_metrics_path,
65                                const std::string pca94xx_charge_metrics_path,
66                                const std::string dc_charge_metrics_path)
67     : kPcaChargeMetricsPath(pca_charge_metrics_path),
68       kPca94xxChargeMetricsPath(pca94xx_charge_metrics_path),
69       kDcChargeMetricsPath(dc_charge_metrics_path) {}
70 
71 }  // namespace pixel
72 }  // namespace google
73 }  // namespace hardware
74 }  // namespace android
75