blob: a7571a260a6c106a88acf57e1a9b87d52e2dd6d4 [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 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 "healthd"
18
Yabin Cuie98e1772016-02-17 12:21:34 -080019#include <healthd/healthd.h>
20#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Thierry Strudelf73de6f2019-01-11 17:09:20 -080029
30#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Yifan Hong1d4368b2019-10-07 11:18:04 -070032#include <optional>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033
Yifan Hongb99d15c2022-03-01 12:12:34 -080034#include <aidl/android/hardware/health/HealthInfo.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070035#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070036#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070037#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070038#include <android/hardware/health/2.1/types.h>
Yifan Hongb99d15c2022-03-01 12:12:34 -080039#include <android/hardware/health/translate-ndk.h>
Todd Poynor752faf22013-06-12 13:25:59 -070040#include <batteryservice/BatteryService.h>
41#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070042#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070043#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070044#include <utils/String8.h>
45#include <utils/Vector.h>
46
47#define POWER_SUPPLY_SUBSYSTEM "power_supply"
48#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070049#define FAKE_BATTERY_CAPACITY 42
50#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080051#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070052#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070053
Yifan Hong1d4368b2019-10-07 11:18:04 -070054using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
55using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
56using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
Yifan Hongb99d15c2022-03-01 12:12:34 -080057using aidl::android::hardware::health::BatteryCapacityLevel;
58using aidl::android::hardware::health::BatteryHealth;
59using aidl::android::hardware::health::BatteryStatus;
60using aidl::android::hardware::health::HealthInfo;
61
62namespace {
63
64// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
65// Skips storageInfo and diskStats.
66void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
67 ::android::hardware::health::V1_0::HealthInfo* out) {
68 out->chargerAcOnline = in.chargerAcOnline;
69 out->chargerUsbOnline = in.chargerUsbOnline;
70 out->chargerWirelessOnline = in.chargerWirelessOnline;
71 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
72 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
73 out->batteryStatus =
74 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
75 out->batteryHealth =
76 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
77 out->batteryPresent = in.batteryPresent;
78 out->batteryLevel = in.batteryLevel;
79 out->batteryVoltage = in.batteryVoltageMillivolts;
80 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
81 out->batteryCurrent = in.batteryCurrentMicroamps;
82 out->batteryCycleCount = in.batteryCycleCount;
83 out->batteryFullCharge = in.batteryFullChargeUah;
84 out->batteryChargeCounter = in.batteryChargeCounterUah;
85 out->batteryTechnology = in.batteryTechnology;
86}
87
88void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
89 ::android::hardware::health::V2_0::HealthInfo* out) {
90 translateToHidl(in, &out->legacy);
91 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
92 // Skip storageInfo and diskStats
93}
94
95void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
96 ::android::hardware::health::V2_1::HealthInfo* out) {
97 translateToHidl(in, &out->legacy);
98 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
99 in.batteryCapacityLevel);
100 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
101 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
102}
103
104} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700105
Todd Poynor752faf22013-06-12 13:25:59 -0700106namespace android {
107
Yifan Hong1d4368b2019-10-07 11:18:04 -0700108template <typename T>
109struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700110 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700111 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700112};
113
Yifan Hong1d4368b2019-10-07 11:18:04 -0700114template <typename T>
115static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700116 for (int i = 0; map[i].s; i++)
117 if (!strcmp(str, map[i].s))
118 return map[i].val;
119
Yifan Hong1d4368b2019-10-07 11:18:04 -0700120 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800121}
122
Yifan Hongb99d15c2022-03-01 12:12:34 -0800123static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000124 *health_info = {
125 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
126 .batteryChargeTimeToFullNowSeconds =
127 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
128 .batteryStatus = BatteryStatus::UNKNOWN,
129 .batteryHealth = BatteryHealth::UNKNOWN,
130 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800131}
132
Todd Poynore030a102018-01-19 14:03:59 -0800133BatteryMonitor::BatteryMonitor()
134 : mHealthdConfig(nullptr),
135 mBatteryDevicePresent(false),
136 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700137 mBatteryFixedTemperature(0),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800138 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800139 initHealthInfo(mHealthInfo.get());
140}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700141
142BatteryMonitor::~BatteryMonitor() {}
143
Yifan Hongb99d15c2022-03-01 12:12:34 -0800144HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
145 HealthInfo_1_0 health_info_1_0;
146 translateToHidl(*mHealthInfo, &health_info_1_0);
147 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800148}
149
Yifan Hongb99d15c2022-03-01 12:12:34 -0800150HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
151 HealthInfo_2_0 health_info_2_0;
152 translateToHidl(*mHealthInfo, &health_info_2_0);
153 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800154}
155
Yifan Hongb99d15c2022-03-01 12:12:34 -0800156HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
157 HealthInfo_2_1 health_info_2_1;
158 translateToHidl(*mHealthInfo, &health_info_2_1);
159 return health_info_2_1;
160}
161
162const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700163 return *mHealthInfo;
164}
165
166BatteryStatus getBatteryStatus(const char* status) {
167 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
168 {"Unknown", BatteryStatus::UNKNOWN},
169 {"Charging", BatteryStatus::CHARGING},
170 {"Discharging", BatteryStatus::DISCHARGING},
171 {"Not charging", BatteryStatus::NOT_CHARGING},
172 {"Full", BatteryStatus::FULL},
173 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700174 };
175
Yifan Hong1d4368b2019-10-07 11:18:04 -0700176 auto ret = mapSysfsString(status, batteryStatusMap);
177 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700178 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700179 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700180 }
181
Yifan Hong1d4368b2019-10-07 11:18:04 -0700182 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700183}
184
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800185BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
186 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
187 {"Unknown", BatteryCapacityLevel::UNKNOWN},
188 {"Critical", BatteryCapacityLevel::CRITICAL},
189 {"Low", BatteryCapacityLevel::LOW},
190 {"Normal", BatteryCapacityLevel::NORMAL},
191 {"High", BatteryCapacityLevel::HIGH},
192 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800193 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800194 };
195
196 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
197 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800198 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
199 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800200 }
201
202 return *ret;
203}
204
Yifan Hong1d4368b2019-10-07 11:18:04 -0700205BatteryHealth getBatteryHealth(const char* status) {
206 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
207 {"Unknown", BatteryHealth::UNKNOWN},
208 {"Good", BatteryHealth::GOOD},
209 {"Overheat", BatteryHealth::OVERHEAT},
210 {"Dead", BatteryHealth::DEAD},
211 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
212 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
213 {"Cold", BatteryHealth::COLD},
214 // battery health values from JEITA spec
215 {"Warm", BatteryHealth::GOOD},
216 {"Cool", BatteryHealth::GOOD},
217 {"Hot", BatteryHealth::OVERHEAT},
218 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700219 };
220
Yifan Hong1d4368b2019-10-07 11:18:04 -0700221 auto ret = mapSysfsString(status, batteryHealthMap);
222 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700223 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700224 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700225 }
226
Yifan Hong1d4368b2019-10-07 11:18:04 -0700227 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700228}
229
Bart Van Assche095c9442022-03-02 17:36:34 +0000230static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000231 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800232 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700233 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700234 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700235 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700236}
237
Bart Van Assche095c9442022-03-02 17:36:34 +0000238static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700239 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000240 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
241 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
242 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
243 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
244 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
245 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
246 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
247 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
248 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
249 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
250 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
251 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
252 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
253 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700254 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700255 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700256 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700257
Bart Van Assche095c9442022-03-02 17:36:34 +0000258 if (readFromFile(path, &buf) <= 0) {
259 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
260 }
Todd Poynor752faf22013-06-12 13:25:59 -0700261
Yifan Hong1d4368b2019-10-07 11:18:04 -0700262 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000263 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700264 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000265 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100266 }
Todd Poynor752faf22013-06-12 13:25:59 -0700267
Yifan Hong1d4368b2019-10-07 11:18:04 -0700268 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700269}
270
Bart Van Assche095c9442022-03-02 17:36:34 +0000271static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700272 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700273 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700274
275 if (readFromFile(path, &buf) > 0)
276 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700277 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700278
279 return value;
280}
281
Bart Van Assche095c9442022-03-02 17:36:34 +0000282static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700283 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700284 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700285
286 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700287 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700288
Todd Poynor752faf22013-06-12 13:25:59 -0700289 return value;
290}
291
Bart Van Assche095c9442022-03-02 17:36:34 +0000292static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900293 constexpr char kScopeDevice[] = "Device";
294
295 String8 path;
296 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
297 std::string scope;
298 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
299}
300
Yifan Hong1353e702019-10-07 10:41:30 -0700301void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800302 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700303
Todd Poynorf5d30122013-08-12 17:03:35 -0700304 if (!mHealthdConfig->batteryPresentPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800305 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700306 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800307 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700308
Yifan Hongb99d15c2022-03-01 12:12:34 -0800309 mHealthInfo->batteryLevel = mBatteryFixedCapacity
310 ? mBatteryFixedCapacity
311 : getIntField(mHealthdConfig->batteryCapacityPath);
312 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700313
Ruchi Kandoicc338802015-08-24 13:01:16 -0700314 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800315 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700316
317 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800318 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700319
320 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800321 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700322
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700323 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800324 mHealthInfo->batteryChargeCounterUah =
325 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700326
Yifan Hong35cb0832019-10-07 13:58:29 -0700327 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800328 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700329 getIntField(mHealthdConfig->batteryCurrentAvgPath);
330
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800331 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
332 mHealthInfo->batteryChargeTimeToFullNowSeconds =
333 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
334
Stephane Lee1c108ed2020-02-10 18:23:57 -0800335 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
336 mHealthInfo->batteryFullChargeDesignCapacityUah =
337 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700338
Yifan Hongb99d15c2022-03-01 12:12:34 -0800339 mHealthInfo->batteryTemperatureTenthsCelsius =
340 mBatteryFixedTemperature ? mBatteryFixedTemperature
341 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700342
Michael Scott3217c5c2016-06-05 11:20:13 -0700343 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700344
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800345 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
346 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
347
Michael Scott3217c5c2016-06-05 11:20:13 -0700348 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800349 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700350
Michael Scott3217c5c2016-06-05 11:20:13 -0700351 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800352 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700353
Michael Scott3217c5c2016-06-05 11:20:13 -0700354 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800355 mHealthInfo->batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700356
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700357 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700358
ShevT9d98a6a2018-07-26 11:47:47 +0300359 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700360 String8 path;
361 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
362 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700363 if (getIntField(path)) {
364 path.clear();
365 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
366 mChargerNames[i].string());
367 switch(readPowerSupplyType(path)) {
368 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800369 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700370 break;
371 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800372 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700373 break;
374 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800375 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700376 break;
Jack Wu06b90412021-12-15 20:40:21 +0800377 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800378 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800379 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700380 default:
Jack Wu06b90412021-12-15 20:40:21 +0800381 path.clear();
382 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
383 mChargerNames[i].string());
Yifan Hongb99d15c2022-03-01 12:12:34 -0800384 if (access(path.string(), R_OK) == 0)
385 mHealthInfo->chargerDockOnline = true;
386 else
Jack Wu06b90412021-12-15 20:40:21 +0800387 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
388 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700389 }
390 path.clear();
391 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
392 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700393 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700394 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
395
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700396 path.clear();
397 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
398 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700399
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700400 int ChargingVoltage =
401 (access(path.string(), R_OK) == 0) ? getIntField(path) :
402 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700403
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700404 double power = ((double)ChargingCurrent / MILLION) *
405 ((double)ChargingVoltage / MILLION);
406 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800407 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
408 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700409 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700410 }
411 }
412 }
Yifan Hong1353e702019-10-07 10:41:30 -0700413}
Todd Poynor752faf22013-06-12 13:25:59 -0700414
Bart Van Assche095c9442022-03-02 17:36:34 +0000415static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700416 char dmesgline[256];
417 size_t len;
418 if (props.batteryPresent) {
419 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800420 props.batteryLevel, props.batteryVoltageMillivolts,
421 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
422 abs(props.batteryTemperatureTenthsCelsius / 10),
423 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
424 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700425
Yifan Hong1353e702019-10-07 10:41:30 -0700426 len = strlen(dmesgline);
Yifan Hong605e7d22021-02-08 15:14:48 -0800427 if (!healthd_config.batteryCurrentNowPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700428 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800429 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700430 }
431
Yifan Hong605e7d22021-02-08 15:14:48 -0800432 if (!healthd_config.batteryFullChargePath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700433 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800434 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700435 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700436
Yifan Hong605e7d22021-02-08 15:14:48 -0800437 if (!healthd_config.batteryCycleCountPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700438 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
439 props.batteryCycleCount);
440 }
441 } else {
442 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700443 }
444
Yifan Hongb99d15c2022-03-01 12:12:34 -0800445 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700446 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800447 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700448
449 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
450}
451
Bart Van Assche095c9442022-03-02 17:36:34 +0000452void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
453 const struct healthd_config& healthd_config) {
454 HealthInfo aidl_health_info;
455 (void)android::h2a::translate(health_info, &aidl_health_info);
456 doLogValues(aidl_health_info, healthd_config);
457}
458
459void BatteryMonitor::logValues(void) {
460 doLogValues(*mHealthInfo, *mHealthdConfig);
461}
462
Yifan Hong1353e702019-10-07 10:41:30 -0700463bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800464 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800465 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800466 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700467}
468
Yabin Cuiaedf6032016-02-19 18:03:23 -0800469int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700470 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800471 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700472 std::string buf;
473 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
474 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800475 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700476 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800477}
478
Todd Poynorc133b712013-08-14 17:39:13 -0700479status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
480 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800481 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700482
Todd Poynor8f132af2014-05-08 17:15:45 -0700483 val->valueInt64 = LONG_MIN;
484
Todd Poynorc133b712013-08-14 17:39:13 -0700485 switch(id) {
486 case BATTERY_PROP_CHARGE_COUNTER:
487 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700488 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700489 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700490 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700491 } else {
492 ret = NAME_NOT_FOUND;
493 }
494 break;
495
496 case BATTERY_PROP_CURRENT_NOW:
497 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700498 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700499 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700500 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700501 } else {
502 ret = NAME_NOT_FOUND;
503 }
504 break;
505
Todd Poynorbc102112013-08-27 18:11:49 -0700506 case BATTERY_PROP_CURRENT_AVG:
507 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700508 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700509 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700510 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700511 } else {
512 ret = NAME_NOT_FOUND;
513 }
514 break;
515
Paul Lawrence347c8de2014-03-19 15:04:40 -0700516 case BATTERY_PROP_CAPACITY:
517 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700518 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700519 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700520 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700521 } else {
522 ret = NAME_NOT_FOUND;
523 }
524 break;
525
Todd Poynor8f132af2014-05-08 17:15:45 -0700526 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700527 if (mHealthdConfig->energyCounter) {
528 ret = mHealthdConfig->energyCounter(&val->valueInt64);
529 } else {
530 ret = NAME_NOT_FOUND;
531 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700532 break;
533
Jin Qian72adf112017-02-02 17:31:13 -0800534 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800535 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700536 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800537 break;
538
Todd Poynorc133b712013-08-14 17:39:13 -0700539 default:
540 break;
541 }
542
Todd Poynorc133b712013-08-14 17:39:13 -0700543 return ret;
544}
545
Todd Poynor020369d2013-09-18 20:09:33 -0700546void BatteryMonitor::dumpState(int fd) {
547 int v;
548 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800549 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700550
Jack Wu06b90412021-12-15 20:40:21 +0800551 snprintf(vs, sizeof(vs),
552 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
553 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800554 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
555 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700556 write(fd, vs, strlen(vs));
557 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
558 props.batteryStatus, props.batteryHealth, props.batteryPresent);
559 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800560 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
561 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700562 write(fd, vs, strlen(vs));
563
564 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
565 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
566 snprintf(vs, sizeof(vs), "current now: %d\n", v);
567 write(fd, vs, strlen(vs));
568 }
569
570 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
571 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
572 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
573 write(fd, vs, strlen(vs));
574 }
575
576 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
577 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
578 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
579 write(fd, vs, strlen(vs));
580 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700581
582 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800583 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700584 write(fd, vs, strlen(vs));
585 }
586
587 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
588 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
589 write(fd, vs, strlen(vs));
590 }
591
592 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800593 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700594 write(fd, vs, strlen(vs));
595 }
Todd Poynor020369d2013-09-18 20:09:33 -0700596}
597
Todd Poynorc7464c92013-09-10 12:40:00 -0700598void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700599 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700600 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700601
Todd Poynorf5d30122013-08-12 17:03:35 -0700602 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800603 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700604 if (dir == NULL) {
605 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
606 } else {
607 struct dirent* entry;
608
James Hawkins588a2ca2016-02-18 14:52:46 -0800609 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700610 const char* name = entry->d_name;
611
612 if (!strcmp(name, ".") || !strcmp(name, ".."))
613 continue;
614
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000615 std::vector<String8>::iterator itIgnoreName =
616 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
617 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800618 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
619 continue;
620
Todd Poynor752faf22013-06-12 13:25:59 -0700621 // Look for "type" file in each subdirectory
622 path.clear();
623 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
624 switch(readPowerSupplyType(path)) {
625 case ANDROID_POWER_SUPPLY_TYPE_AC:
626 case ANDROID_POWER_SUPPLY_TYPE_USB:
627 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800628 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700629 path.clear();
630 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
631 if (access(path.string(), R_OK) == 0)
632 mChargerNames.add(String8(name));
633 break;
634
635 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900636 // Some devices expose the battery status of sub-component like
637 // stylus. Such a device-scoped battery info needs to be skipped
638 // in BatteryMonitor, which is intended to report the status of
639 // the battery supplying the power to the whole system.
640 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700641 mBatteryDevicePresent = true;
642
Todd Poynorf5d30122013-08-12 17:03:35 -0700643 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700644 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700645 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
646 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700647 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700648 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700649 }
650
Todd Poynorf5d30122013-08-12 17:03:35 -0700651 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700652 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700653 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
654 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700655 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700656 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700657 }
658
Todd Poynorf5d30122013-08-12 17:03:35 -0700659 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
660 path.clear();
661 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
662 name);
663 if (access(path, R_OK) == 0)
664 mHealthdConfig->batteryPresentPath = path;
665 }
666
667 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
668 path.clear();
669 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
670 name);
671 if (access(path, R_OK) == 0)
672 mHealthdConfig->batteryCapacityPath = path;
673 }
674
675 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
676 path.clear();
677 path.appendFormat("%s/%s/voltage_now",
678 POWER_SUPPLY_SYSFS_PATH, name);
679 if (access(path, R_OK) == 0) {
680 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700681 }
682 }
683
Ruchi Kandoicc338802015-08-24 13:01:16 -0700684 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
685 path.clear();
686 path.appendFormat("%s/%s/charge_full",
687 POWER_SUPPLY_SYSFS_PATH, name);
688 if (access(path, R_OK) == 0)
689 mHealthdConfig->batteryFullChargePath = path;
690 }
691
Todd Poynorf5d30122013-08-12 17:03:35 -0700692 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
693 path.clear();
694 path.appendFormat("%s/%s/current_now",
695 POWER_SUPPLY_SYSFS_PATH, name);
696 if (access(path, R_OK) == 0)
697 mHealthdConfig->batteryCurrentNowPath = path;
698 }
699
Ruchi Kandoicc338802015-08-24 13:01:16 -0700700 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
701 path.clear();
702 path.appendFormat("%s/%s/cycle_count",
703 POWER_SUPPLY_SYSFS_PATH, name);
704 if (access(path, R_OK) == 0)
705 mHealthdConfig->batteryCycleCountPath = path;
706 }
707
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800708 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) {
709 path.clear();
710 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
711 if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path;
712 }
713
714 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) {
715 path.clear();
716 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
717 if (access(path, R_OK) == 0)
718 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
719 }
720
Stephane Lee1c108ed2020-02-10 18:23:57 -0800721 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) {
722 path.clear();
723 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
724 if (access(path, R_OK) == 0)
725 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
726 }
727
Todd Poynorbc102112013-08-27 18:11:49 -0700728 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
729 path.clear();
730 path.appendFormat("%s/%s/current_avg",
731 POWER_SUPPLY_SYSFS_PATH, name);
732 if (access(path, R_OK) == 0)
733 mHealthdConfig->batteryCurrentAvgPath = path;
734 }
735
Todd Poynorf5d30122013-08-12 17:03:35 -0700736 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
737 path.clear();
738 path.appendFormat("%s/%s/charge_counter",
739 POWER_SUPPLY_SYSFS_PATH, name);
740 if (access(path, R_OK) == 0)
741 mHealthdConfig->batteryChargeCounterPath = path;
742 }
743
744 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
745 path.clear();
746 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
747 name);
748 if (access(path, R_OK) == 0) {
749 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700750 }
751 }
752
753 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
754 path.clear();
755 path.appendFormat("%s/%s/technology",
756 POWER_SUPPLY_SYSFS_PATH, name);
757 if (access(path, R_OK) == 0)
758 mHealthdConfig->batteryTechnologyPath = path;
759 }
760
Todd Poynor752faf22013-06-12 13:25:59 -0700761 break;
762
763 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
764 break;
765 }
Jack Wu06b90412021-12-15 20:40:21 +0800766
767 // Look for "is_dock" file
768 path.clear();
769 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
770 if (access(path.string(), R_OK) == 0) {
771 path.clear();
772 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
773 if (access(path.string(), R_OK) == 0)
774 mChargerNames.add(String8(name));
775
776 }
Todd Poynor752faf22013-06-12 13:25:59 -0700777 }
Todd Poynor752faf22013-06-12 13:25:59 -0700778 }
779
Ian Pedowitz585ab652015-10-12 19:01:00 -0700780 // Typically the case for devices which do not have a battery and
781 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700782 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700783 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700784 hc->periodic_chores_interval_fast = -1;
785 hc->periodic_chores_interval_slow = -1;
786 } else {
787 if (mHealthdConfig->batteryStatusPath.isEmpty())
788 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
789 if (mHealthdConfig->batteryHealthPath.isEmpty())
790 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
791 if (mHealthdConfig->batteryPresentPath.isEmpty())
792 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
793 if (mHealthdConfig->batteryCapacityPath.isEmpty())
794 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
795 if (mHealthdConfig->batteryVoltagePath.isEmpty())
796 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
797 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
798 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
799 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
800 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700801 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700802 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
803 if (mHealthdConfig->batteryFullChargePath.isEmpty())
804 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
805 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
806 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800807 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty())
808 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
809 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
810 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Stephane Lee1c108ed2020-02-10 18:23:57 -0800811 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
812 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700813 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700814
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700815 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
816 && strtol(pval, NULL, 10) != 0) {
817 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
818 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
819 }
Todd Poynor752faf22013-06-12 13:25:59 -0700820}
821
822}; // namespace android