1 /*
2 * Copyright (C) 2019 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 #include "pixelhealth/BatteryThermalControl.h"
18
19 using aidl::android::hardware::health::BatteryStatus;
20 using aidl::android::hardware::health::HealthInfo;
21
22 namespace hardware {
23 namespace google {
24 namespace pixel {
25 namespace health {
26
27 using android::base::GetIntProperty;
28
BatteryThermalControl(const std::string & path)29 BatteryThermalControl::BatteryThermalControl(const std::string &path) : mThermalSocMode(path) {
30 mStatus = true;
31 }
32
setThermalMode(bool isEnable,bool isWeakCharger)33 void BatteryThermalControl::setThermalMode(bool isEnable, bool isWeakCharger) {
34 std::string action = (isEnable) ? "enabled" : "disabled";
35
36 if (mStatus != isEnable) {
37 if (!isEnable && isWeakCharger)
38 return;
39 if (android::base::WriteStringToFile(action, mThermalSocMode)) {
40 mStatus = isEnable;
41 } else {
42 LOG(ERROR) << "Error Write: \"" << action << "\" to " << mThermalSocMode
43 << " error:" << strerror(errno);
44 }
45 }
46 }
47
updateThermalState(const struct android::BatteryProperties * props)48 void BatteryThermalControl::updateThermalState(const struct android::BatteryProperties *props) {
49 int bcl_disable = GetIntProperty("persist.vendor.disable.bcl.control", 0);
50
51 if (bcl_disable)
52 setThermalMode(false, true);
53 else
54 setThermalMode(props->batteryStatus != android::BATTERY_STATUS_CHARGING &&
55 props->batteryStatus != android::BATTERY_STATUS_FULL,
56 props->maxChargingCurrent * props->maxChargingVoltage < 37500000);
57 }
58
updateThermalState(const HealthInfo & health_info)59 void BatteryThermalControl::updateThermalState(const HealthInfo &health_info) {
60 int bcl_disable = GetIntProperty("persist.vendor.disable.bcl.control", 0);
61
62 if (bcl_disable)
63 setThermalMode(false, true);
64 else
65 setThermalMode(
66 health_info.batteryStatus != BatteryStatus::CHARGING &&
67 health_info.batteryStatus != BatteryStatus::FULL,
68 health_info.maxChargingCurrentMicroamps * health_info.maxChargingVoltageMicrovolts <
69 37500000);
70 }
71
72 } // namespace health
73 } // namespace pixel
74 } // namespace google
75 } // namespace hardware
76