blob: 230f29705805b146d0dc279c34aa05c8a3e38391 [file] [log] [blame]
George Leea1bea3e2022-10-24 19:21:53 -07001/*
2 * Copyright (C) 2022 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 "battery-mitigation"
18
19#include <battery_mitigation/BatteryMitigation.h>
Xiang Wangf060fe32023-02-21 15:14:42 -080020#include <android/binder_process.h>
George Leea1bea3e2022-10-24 19:21:53 -070021
22using android::hardware::google::pixel::BatteryMitigation;
23using android::hardware::google::pixel::MitigationConfig;
24
25android::sp<BatteryMitigation> bmSp;
26
27const struct MitigationConfig::Config cfg = {
28 .SystemPath = {
29 "/dev/thermal/tz-by-name/batoilo/temp",
30 "/dev/thermal/tz-by-name/smpl_gm/temp",
31 "/dev/thermal/tz-by-name/soc/temp",
32 "/dev/thermal/tz-by-name/vdroop1/temp",
33 "/dev/thermal/tz-by-name/vdroop2/temp",
34 "/dev/thermal/tz-by-name/ocp_gpu/temp",
35 "/dev/thermal/tz-by-name/ocp_tpu/temp",
36 "/dev/thermal/tz-by-name/soft_ocp_cpu2/temp",
37 "/dev/thermal/tz-by-name/soft_ocp_cpu1/temp",
38 "/dev/thermal/tz-by-name/battery/temp",
39 "/dev/thermal/tz-by-name/battery_cycle/temp",
40 "/sys/bus/iio/devices/iio:device0/lpf_power",
41 "/sys/bus/iio/devices/iio:device1/lpf_power",
42 "/dev/thermal/cdev-by-name/thermal-cpufreq-2/cur_state",
43 "/dev/thermal/cdev-by-name/thermal-cpufreq-1/cur_state",
44 "/dev/thermal/cdev-by-name/thermal-gpufreq-0/cur_state",
45 "/dev/thermal/cdev-by-name/tpu_cooling/cur_state",
46 "/dev/thermal/cdev-by-name/CAM/cur_state",
47 "/dev/thermal/cdev-by-name/DISP/cur_state",
48 "/dev/thermal/cdev-by-name/gxp-cooling/cur_state",
49 "/sys/class/power_supply/battery/voltage_now",
50 "/sys/class/power_supply/battery/current_now",
51 },
52 .FilteredZones = {
53 "batoilo",
54 "vdroop1",
55 "vdroop2",
56 "smpl_gm",
57 },
58 .SystemName = {
59 "batoilo", "smpl_gm", "soc", "vdroop1", "vdroop2", "ocp_gpu",
60 "ocp_tpu", "soft_ocp_cpu2", "soft_ocp_cpu1", "battery", "battery_cycle",
61 "main", "sub", "CPU2", "CPU1", "GPU", "TPU", "CAM", "DISP", "NPU",
62 "voltage_now", "current_now",
63 },
64 .LogFilePath = "/data/vendor/mitigation/thismeal.txt",
65 .TimestampFormat = "%Y-%m-%d %H:%M:%S",
66};
67
68const char kReadyFilePath[] = "/sys/devices/virtual/pmic/mitigation/instruction/ready";
69const char kReadyProperty[] = "vendor.brownout.mitigation.ready";
70const char kLastMealPath[] = "/data/vendor/mitigation/lastmeal.txt";
George Leefd2395f2022-12-02 00:09:30 +000071const char kBRRequestedProperty[] = "vendor.brownout_reason";
George Lee930ea072023-01-27 15:34:38 -080072const char kLastMealProperty[] = "vendor.brownout.br.feasible";
George Leea1bea3e2022-10-24 19:21:53 -070073const std::regex kTimestampRegex("^\\S+\\s[0-9]+:[0-9]+:[0-9]+\\S+$");
74
75int main(int /*argc*/, char ** /*argv*/) {
76 auto batteryMitigationStartTime = std::chrono::system_clock::now();
Xiang Wangf060fe32023-02-21 15:14:42 -080077 ABinderProcess_setThreadPoolMaxThreadCount(1);
78 ABinderProcess_startThreadPool();
George Leea1bea3e2022-10-24 19:21:53 -070079 bmSp = new BatteryMitigation(cfg);
80 if (!bmSp) {
81 return 0;
82 }
83 bool mitigationLogTimeValid = bmSp->isMitigationLogTimeValid(batteryMitigationStartTime,
84 cfg.LogFilePath,
85 cfg.TimestampFormat,
86 kTimestampRegex);
George Leefd2395f2022-12-02 00:09:30 +000087 std::string reason = android::base::GetProperty(kBRRequestedProperty, "");
88 if (!reason.empty() && mitigationLogTimeValid) {
George Leea1bea3e2022-10-24 19:21:53 -070089 std::ifstream src(cfg.LogFilePath, std::ios::in);
90 std::ofstream dst(kLastMealPath, std::ios::out);
91 dst << src.rdbuf();
George Lee930ea072023-01-27 15:34:38 -080092 android::base::SetProperty(kLastMealProperty, "1");
George Leea1bea3e2022-10-24 19:21:53 -070093 }
94 bool isBatteryMitigationReady = false;
95 std::string ready_str;
96 int val = 0;
97 while (!isBatteryMitigationReady) {
98 if (!android::base::ReadFileToString(kReadyFilePath, &ready_str)) {
99 continue;
100 }
101 ready_str = android::base::Trim(ready_str);
102 if (!android::base::ParseInt(ready_str, &val)) {
103 continue;
104 }
105 if (val == 1) {
106 isBatteryMitigationReady = true;
107 }
108 }
109 android::base::SetProperty(kReadyProperty, "1");
110 while (true) {
111 pause();
112 }
113 return 0;
114}