blob: b0a14bb836af5dfb25618a9c5e467f26ac81df07 [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;
Jack Wue561d032022-11-24 12:19:41 +080058using aidl::android::hardware::health::BatteryChargingPolicy;
59using aidl::android::hardware::health::BatteryChargingState;
Yifan Hongb99d15c2022-03-01 12:12:34 -080060using aidl::android::hardware::health::BatteryHealth;
Jack Wue561d032022-11-24 12:19:41 +080061using aidl::android::hardware::health::BatteryHealthData;
David Andersond5ed26a2023-12-08 15:12:24 -080062using aidl::android::hardware::health::BatteryPartStatus;
Yifan Hongb99d15c2022-03-01 12:12:34 -080063using aidl::android::hardware::health::BatteryStatus;
64using aidl::android::hardware::health::HealthInfo;
65
66namespace {
67
68// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
69// Skips storageInfo and diskStats.
70void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
71 ::android::hardware::health::V1_0::HealthInfo* out) {
72 out->chargerAcOnline = in.chargerAcOnline;
73 out->chargerUsbOnline = in.chargerUsbOnline;
74 out->chargerWirelessOnline = in.chargerWirelessOnline;
75 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
76 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
77 out->batteryStatus =
78 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
79 out->batteryHealth =
80 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
81 out->batteryPresent = in.batteryPresent;
82 out->batteryLevel = in.batteryLevel;
83 out->batteryVoltage = in.batteryVoltageMillivolts;
84 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
85 out->batteryCurrent = in.batteryCurrentMicroamps;
86 out->batteryCycleCount = in.batteryCycleCount;
87 out->batteryFullCharge = in.batteryFullChargeUah;
88 out->batteryChargeCounter = in.batteryChargeCounterUah;
89 out->batteryTechnology = in.batteryTechnology;
90}
91
92void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
93 ::android::hardware::health::V2_0::HealthInfo* out) {
94 translateToHidl(in, &out->legacy);
95 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
96 // Skip storageInfo and diskStats
97}
98
99void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
100 ::android::hardware::health::V2_1::HealthInfo* out) {
101 translateToHidl(in, &out->legacy);
102 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
103 in.batteryCapacityLevel);
104 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
105 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
106}
107
108} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700109
Todd Poynor752faf22013-06-12 13:25:59 -0700110namespace android {
111
Yifan Hong1d4368b2019-10-07 11:18:04 -0700112template <typename T>
113struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700114 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700115 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700116};
117
Yifan Hong1d4368b2019-10-07 11:18:04 -0700118template <typename T>
119static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700120 for (int i = 0; map[i].s; i++)
121 if (!strcmp(str, map[i].s))
122 return map[i].val;
123
Yifan Hong1d4368b2019-10-07 11:18:04 -0700124 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800125}
126
Yifan Hongb99d15c2022-03-01 12:12:34 -0800127static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000128 *health_info = {
129 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
130 .batteryChargeTimeToFullNowSeconds =
131 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
132 .batteryStatus = BatteryStatus::UNKNOWN,
133 .batteryHealth = BatteryHealth::UNKNOWN,
Andrei Ciubotariu515f5b52025-02-12 21:31:23 -0800134 .batteryHealthData = std::nullopt,
Bart Van Assche024e18f2022-02-24 21:22:07 +0000135 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800136}
137
Todd Poynore030a102018-01-19 14:03:59 -0800138BatteryMonitor::BatteryMonitor()
139 : mHealthdConfig(nullptr),
140 mBatteryDevicePresent(false),
141 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700142 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800143 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800144 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800145 initHealthInfo(mHealthInfo.get());
146}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700147
148BatteryMonitor::~BatteryMonitor() {}
149
Yifan Hongb99d15c2022-03-01 12:12:34 -0800150HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
151 HealthInfo_1_0 health_info_1_0;
152 translateToHidl(*mHealthInfo, &health_info_1_0);
153 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800154}
155
Yifan Hongb99d15c2022-03-01 12:12:34 -0800156HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
157 HealthInfo_2_0 health_info_2_0;
158 translateToHidl(*mHealthInfo, &health_info_2_0);
159 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800160}
161
Yifan Hongb99d15c2022-03-01 12:12:34 -0800162HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
163 HealthInfo_2_1 health_info_2_1;
164 translateToHidl(*mHealthInfo, &health_info_2_1);
165 return health_info_2_1;
166}
167
168const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700169 return *mHealthInfo;
170}
171
172BatteryStatus getBatteryStatus(const char* status) {
173 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
174 {"Unknown", BatteryStatus::UNKNOWN},
175 {"Charging", BatteryStatus::CHARGING},
176 {"Discharging", BatteryStatus::DISCHARGING},
177 {"Not charging", BatteryStatus::NOT_CHARGING},
178 {"Full", BatteryStatus::FULL},
179 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700180 };
181
Yifan Hong1d4368b2019-10-07 11:18:04 -0700182 auto ret = mapSysfsString(status, batteryStatusMap);
183 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700184 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700185 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700186 }
187
Yifan Hong1d4368b2019-10-07 11:18:04 -0700188 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700189}
190
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800191BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
192 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
193 {"Unknown", BatteryCapacityLevel::UNKNOWN},
194 {"Critical", BatteryCapacityLevel::CRITICAL},
195 {"Low", BatteryCapacityLevel::LOW},
196 {"Normal", BatteryCapacityLevel::NORMAL},
197 {"High", BatteryCapacityLevel::HIGH},
198 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800199 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800200 };
201
202 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
203 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800204 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
205 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800206 }
207
208 return *ret;
209}
210
Yifan Hong1d4368b2019-10-07 11:18:04 -0700211BatteryHealth getBatteryHealth(const char* status) {
212 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
213 {"Unknown", BatteryHealth::UNKNOWN},
214 {"Good", BatteryHealth::GOOD},
215 {"Overheat", BatteryHealth::OVERHEAT},
216 {"Dead", BatteryHealth::DEAD},
217 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
218 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
219 {"Cold", BatteryHealth::COLD},
220 // battery health values from JEITA spec
221 {"Warm", BatteryHealth::GOOD},
222 {"Cool", BatteryHealth::GOOD},
223 {"Hot", BatteryHealth::OVERHEAT},
David Anderson629a26b2023-12-11 15:25:13 -0800224 {"Calibration required", BatteryHealth::INCONSISTENT},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700225 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700226 };
227
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 auto ret = mapSysfsString(status, batteryHealthMap);
229 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700230 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700232 }
233
Yifan Hong1d4368b2019-10-07 11:18:04 -0700234 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700235}
236
Jack Wub57f68a2023-02-04 19:56:06 +0800237BatteryHealth getBatteryHealthStatus(int status) {
238 BatteryHealth value;
239
240 if (status == BatteryMonitor::BH_NOMINAL)
241 value = BatteryHealth::GOOD;
242 else if (status == BatteryMonitor::BH_MARGINAL)
243 value = BatteryHealth::FAIR;
244 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
245 value = BatteryHealth::DEAD;
246 else if (status == BatteryMonitor::BH_FAILED)
247 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800248 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
249 value = BatteryHealth::NOT_AVAILABLE;
Jack Wu8231c3f2023-05-19 14:31:53 +0800250 else if (status == BatteryMonitor::BH_INCONSISTENT)
251 value = BatteryHealth::INCONSISTENT;
Jack Wub57f68a2023-02-04 19:56:06 +0800252 else
253 value = BatteryHealth::UNKNOWN;
254
255 return value;
256}
257
Jack Wue561d032022-11-24 12:19:41 +0800258BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
259 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
260 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
261 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
262 {NULL, BatteryChargingPolicy::DEFAULT},
263 };
264
265 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
266 if (!ret) {
267 *ret = BatteryChargingPolicy::DEFAULT;
268 }
269
270 return *ret;
271}
272
273BatteryChargingState getBatteryChargingState(const char* chargingState) {
274 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
275 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
276 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
277 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
278 {NULL, BatteryChargingState::NORMAL},
279 };
280
281 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
282 if (!ret) {
283 *ret = BatteryChargingState::NORMAL;
284 }
285
286 return *ret;
287}
288
Bart Van Assche095c9442022-03-02 17:36:34 +0000289static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000290 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800291 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700292 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700293 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700294 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700295}
296
Jack Wue561d032022-11-24 12:19:41 +0800297static bool writeToFile(const String8& path, int32_t in_value) {
298 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
299}
300
Bart Van Assche095c9442022-03-02 17:36:34 +0000301static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700302 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000303 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
304 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
305 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
307 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
308 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
309 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
310 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
311 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
312 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
313 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
314 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
315 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
316 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700317 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700318 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700319 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700320
Bart Van Assche095c9442022-03-02 17:36:34 +0000321 if (readFromFile(path, &buf) <= 0) {
322 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
323 }
Todd Poynor752faf22013-06-12 13:25:59 -0700324
Yifan Hong1d4368b2019-10-07 11:18:04 -0700325 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000326 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700327 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000328 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100329 }
Todd Poynor752faf22013-06-12 13:25:59 -0700330
Yifan Hong1d4368b2019-10-07 11:18:04 -0700331 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700332}
333
Bart Van Assche095c9442022-03-02 17:36:34 +0000334static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700335 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700336 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700337
338 if (readFromFile(path, &buf) > 0)
339 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700340 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700341
342 return value;
343}
344
Bart Van Assche095c9442022-03-02 17:36:34 +0000345static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700346 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700347 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700348
349 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700350 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700351
Todd Poynor752faf22013-06-12 13:25:59 -0700352 return value;
353}
354
Bart Van Assche095c9442022-03-02 17:36:34 +0000355static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900356 constexpr char kScopeDevice[] = "Device";
357
358 String8 path;
359 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
360 std::string scope;
361 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
362}
363
Andrei Ciubotariu515f5b52025-02-12 21:31:23 -0800364static BatteryHealthData *ensureBatteryHealthData(HealthInfo *info) {
365 if (!info->batteryHealthData.has_value()) {
366 return &info->batteryHealthData.emplace();
367 }
368
369 return &info->batteryHealthData.value();
370}
371
Yifan Hong1353e702019-10-07 10:41:30 -0700372void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800373 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700374
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000375 if (!mHealthdConfig->batteryPresentPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800376 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700377 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800378 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700379
Yifan Hongb99d15c2022-03-01 12:12:34 -0800380 mHealthInfo->batteryLevel = mBatteryFixedCapacity
381 ? mBatteryFixedCapacity
382 : getIntField(mHealthdConfig->batteryCapacityPath);
383 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700384
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000385 if (!mHealthdConfig->batteryCurrentNowPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800386 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700387
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000388 if (!mHealthdConfig->batteryFullChargePath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800389 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700390
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000391 if (!mHealthdConfig->batteryCycleCountPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800392 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700393
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000394 if (!mHealthdConfig->batteryChargeCounterPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800395 mHealthInfo->batteryChargeCounterUah =
396 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700397
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000398 if (!mHealthdConfig->batteryCurrentAvgPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800399 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700400 getIntField(mHealthdConfig->batteryCurrentAvgPath);
401
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000402 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800403 mHealthInfo->batteryChargeTimeToFullNowSeconds =
404 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
405
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000406 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800407 mHealthInfo->batteryFullChargeDesignCapacityUah =
408 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700409
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000410 if (!mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +0800411 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
412
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000413 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
Andrei Ciubotariu515f5b52025-02-12 21:31:23 -0800414 ensureBatteryHealthData(mHealthInfo.get())->batteryStateOfHealth =
AleX Pelosiff708922023-02-17 01:39:21 +0000415 getIntField(mHealthdConfig->batteryStateOfHealthPath);
416
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000417 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Andrei Ciubotariu515f5b52025-02-12 21:31:23 -0800418 ensureBatteryHealthData(mHealthInfo.get())->batteryManufacturingDateSeconds =
Jack Wue561d032022-11-24 12:19:41 +0800419 getIntField(mHealthdConfig->batteryManufacturingDatePath);
420
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000421 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Andrei Ciubotariu515f5b52025-02-12 21:31:23 -0800422 ensureBatteryHealthData(mHealthInfo.get())->batteryFirstUsageSeconds =
Jack Wue561d032022-11-24 12:19:41 +0800423 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
424
Yifan Hongb99d15c2022-03-01 12:12:34 -0800425 mHealthInfo->batteryTemperatureTenthsCelsius =
426 mBatteryFixedTemperature ? mBatteryFixedTemperature
427 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700428
Michael Scott3217c5c2016-06-05 11:20:13 -0700429 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700430
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800431 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
432 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
433
Michael Scott3217c5c2016-06-05 11:20:13 -0700434 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800435 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700436
Jack Wub57f68a2023-02-04 19:56:06 +0800437 // Backward compatible with android.hardware.health V1
438 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
439 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
440 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
441 } else {
442 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
443 }
Todd Poynor752faf22013-06-12 13:25:59 -0700444
Michael Scott3217c5c2016-06-05 11:20:13 -0700445 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000446 mHealthInfo->batteryTechnology = buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700447
Jack Wue561d032022-11-24 12:19:41 +0800448 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
449 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
450
451 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
452 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
453
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700454 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700455
ShevT9d98a6a2018-07-26 11:47:47 +0300456 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700457 String8 path;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000458 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700459 if (getIntField(path)) {
460 path.clear();
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000461 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700462 switch(readPowerSupplyType(path)) {
463 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800464 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700465 break;
466 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800467 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700468 break;
469 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800470 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700471 break;
Jack Wu06b90412021-12-15 20:40:21 +0800472 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800473 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800474 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700475 default:
Jack Wu06b90412021-12-15 20:40:21 +0800476 path.clear();
477 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000478 mChargerNames[i].c_str());
479 if (access(path.c_str(), R_OK) == 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800480 mHealthInfo->chargerDockOnline = true;
481 else
Jack Wu06b90412021-12-15 20:40:21 +0800482 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000483 mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700484 }
485 path.clear();
486 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000487 mChargerNames[i].c_str());
488 int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700489
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700490 path.clear();
491 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000492 mChargerNames[i].c_str());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700493
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700494 int ChargingVoltage =
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000495 (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700496
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700497 double power = ((double)ChargingCurrent / MILLION) *
498 ((double)ChargingVoltage / MILLION);
499 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800500 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
501 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700502 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700503 }
504 }
505 }
Yifan Hong1353e702019-10-07 10:41:30 -0700506}
Todd Poynor752faf22013-06-12 13:25:59 -0700507
Bart Van Assche095c9442022-03-02 17:36:34 +0000508static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700509 char dmesgline[256];
510 size_t len;
511 if (props.batteryPresent) {
512 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800513 props.batteryLevel, props.batteryVoltageMillivolts,
514 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
515 abs(props.batteryTemperatureTenthsCelsius / 10),
516 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
517 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700518
Yifan Hong1353e702019-10-07 10:41:30 -0700519 len = strlen(dmesgline);
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000520 if (!healthd_config.batteryCurrentNowPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700521 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800522 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700523 }
524
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000525 if (!healthd_config.batteryFullChargePath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700526 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800527 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700528 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700529
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000530 if (!healthd_config.batteryCycleCountPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700531 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
532 props.batteryCycleCount);
533 }
534 } else {
535 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700536 }
537
Yifan Hongb99d15c2022-03-01 12:12:34 -0800538 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700539 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800540 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700541
AleX Pelosif08aede2024-02-15 18:42:11 +0000542 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Yifan Hong1353e702019-10-07 10:41:30 -0700543}
544
Bart Van Assche095c9442022-03-02 17:36:34 +0000545void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
546 const struct healthd_config& healthd_config) {
547 HealthInfo aidl_health_info;
548 (void)android::h2a::translate(health_info, &aidl_health_info);
549 doLogValues(aidl_health_info, healthd_config);
550}
551
552void BatteryMonitor::logValues(void) {
553 doLogValues(*mHealthInfo, *mHealthdConfig);
554}
555
Yifan Hong1353e702019-10-07 10:41:30 -0700556bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800557 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800558 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800559 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700560}
561
Yabin Cuiaedf6032016-02-19 18:03:23 -0800562int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700563 BatteryStatus result = BatteryStatus::UNKNOWN;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000564 if (!mHealthdConfig->batteryStatusPath.empty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700565 std::string buf;
566 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
567 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800568 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700569 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800570}
571
Jack Wue561d032022-11-24 12:19:41 +0800572status_t BatteryMonitor::setChargingPolicy(int value) {
573 status_t ret = NAME_NOT_FOUND;
574 bool result;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000575 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800576 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
577 if (!result) {
578 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
579 ret = BAD_VALUE;
580 } else {
581 ret = OK;
582 }
583 }
584 return ret;
585}
586
587int BatteryMonitor::getChargingPolicy() {
588 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000589 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800590 std::string buf;
591 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
592 result = getBatteryChargingPolicy(buf.c_str());
593 }
594 return static_cast<int>(result);
595}
596
597int BatteryMonitor::getBatteryHealthData(int id) {
598 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000599 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800600 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
601 }
602 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000603 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800604 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
605 }
AleX Pelosiff708922023-02-17 01:39:21 +0000606 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000607 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000608 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
609 }
David Andersond5ed26a2023-12-08 15:12:24 -0800610 if (id == BATTERY_PROP_PART_STATUS) {
611 return static_cast<int>(BatteryPartStatus::UNSUPPORTED);
612 }
Jack Wue561d032022-11-24 12:19:41 +0800613 return 0;
614}
615
Todd Poynorc133b712013-08-14 17:39:13 -0700616status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
617 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800618 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700619
Todd Poynor8f132af2014-05-08 17:15:45 -0700620 val->valueInt64 = LONG_MIN;
621
Todd Poynorc133b712013-08-14 17:39:13 -0700622 switch(id) {
623 case BATTERY_PROP_CHARGE_COUNTER:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000624 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700625 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700626 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700627 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700628 } else {
629 ret = NAME_NOT_FOUND;
630 }
631 break;
632
633 case BATTERY_PROP_CURRENT_NOW:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000634 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700635 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700636 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700637 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700638 } else {
639 ret = NAME_NOT_FOUND;
640 }
641 break;
642
Todd Poynorbc102112013-08-27 18:11:49 -0700643 case BATTERY_PROP_CURRENT_AVG:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000644 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700645 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700646 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700647 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700648 } else {
649 ret = NAME_NOT_FOUND;
650 }
651 break;
652
Paul Lawrence347c8de2014-03-19 15:04:40 -0700653 case BATTERY_PROP_CAPACITY:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000654 if (!mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700655 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700656 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700657 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700658 } else {
659 ret = NAME_NOT_FOUND;
660 }
661 break;
662
Todd Poynor8f132af2014-05-08 17:15:45 -0700663 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700664 if (mHealthdConfig->energyCounter) {
665 ret = mHealthdConfig->energyCounter(&val->valueInt64);
666 } else {
667 ret = NAME_NOT_FOUND;
668 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700669 break;
670
Jin Qian72adf112017-02-02 17:31:13 -0800671 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800672 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700673 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800674 break;
675
Jack Wue561d032022-11-24 12:19:41 +0800676 case BATTERY_PROP_CHARGING_POLICY:
677 val->valueInt64 = getChargingPolicy();
678 ret = OK;
679 break;
680
681 case BATTERY_PROP_MANUFACTURING_DATE:
682 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
683 ret = OK;
684 break;
685
686 case BATTERY_PROP_FIRST_USAGE_DATE:
687 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
688 ret = OK;
689 break;
690
AleX Pelosiff708922023-02-17 01:39:21 +0000691 case BATTERY_PROP_STATE_OF_HEALTH:
692 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
693 ret = OK;
694 break;
695
David Andersond5ed26a2023-12-08 15:12:24 -0800696 case BATTERY_PROP_PART_STATUS:
697 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_PART_STATUS);
698 ret = OK;
699 break;
700
Todd Poynorc133b712013-08-14 17:39:13 -0700701 default:
702 break;
703 }
704
Todd Poynorc133b712013-08-14 17:39:13 -0700705 return ret;
706}
707
David Andersond5ed26a2023-12-08 15:12:24 -0800708status_t BatteryMonitor::getSerialNumber(std::optional<std::string>* out) {
709 *out = std::nullopt;
710 return OK;
711}
712
Todd Poynor020369d2013-09-18 20:09:33 -0700713void BatteryMonitor::dumpState(int fd) {
714 int v;
715 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800716 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700717
Jack Wu06b90412021-12-15 20:40:21 +0800718 snprintf(vs, sizeof(vs),
719 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
720 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800721 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
722 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700723 write(fd, vs, strlen(vs));
724 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
725 props.batteryStatus, props.batteryHealth, props.batteryPresent);
726 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800727 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
728 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700729 write(fd, vs, strlen(vs));
730
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000731 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700732 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
733 snprintf(vs, sizeof(vs), "current now: %d\n", v);
734 write(fd, vs, strlen(vs));
735 }
736
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000737 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700738 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
739 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
740 write(fd, vs, strlen(vs));
741 }
742
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000743 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700744 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
745 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
746 write(fd, vs, strlen(vs));
747 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700748
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000749 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800750 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700751 write(fd, vs, strlen(vs));
752 }
753
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000754 if (!mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700755 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
756 write(fd, vs, strlen(vs));
757 }
758
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000759 if (!mHealthdConfig->batteryFullChargePath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800760 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700761 write(fd, vs, strlen(vs));
762 }
Todd Poynor020369d2013-09-18 20:09:33 -0700763}
764
Todd Poynorc7464c92013-09-10 12:40:00 -0700765void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700766 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700767 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700768
Todd Poynorf5d30122013-08-12 17:03:35 -0700769 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800770 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700771 if (dir == NULL) {
772 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
773 } else {
774 struct dirent* entry;
775
James Hawkins588a2ca2016-02-18 14:52:46 -0800776 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700777 const char* name = entry->d_name;
778
779 if (!strcmp(name, ".") || !strcmp(name, ".."))
780 continue;
781
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000782 std::vector<String8>::iterator itIgnoreName =
783 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
784 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800785 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
786 continue;
787
Todd Poynor752faf22013-06-12 13:25:59 -0700788 // Look for "type" file in each subdirectory
789 path.clear();
790 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
791 switch(readPowerSupplyType(path)) {
792 case ANDROID_POWER_SUPPLY_TYPE_AC:
793 case ANDROID_POWER_SUPPLY_TYPE_USB:
794 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800795 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700796 path.clear();
797 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000798 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Todd Poynor752faf22013-06-12 13:25:59 -0700799 break;
800
801 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900802 // Some devices expose the battery status of sub-component like
803 // stylus. Such a device-scoped battery info needs to be skipped
804 // in BatteryMonitor, which is intended to report the status of
805 // the battery supplying the power to the whole system.
806 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700807 mBatteryDevicePresent = true;
808
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000809 if (mHealthdConfig->batteryStatusPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700810 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700811 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
812 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000813 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700814 }
815
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000816 if (mHealthdConfig->batteryHealthPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700817 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700818 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
819 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000820 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700821 }
822
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000823 if (mHealthdConfig->batteryPresentPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700824 path.clear();
825 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
826 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000827 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700828 }
829
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000830 if (mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700831 path.clear();
832 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
833 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000834 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700835 }
836
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000837 if (mHealthdConfig->batteryVoltagePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700838 path.clear();
839 path.appendFormat("%s/%s/voltage_now",
840 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000841 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700842 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700843 }
844 }
845
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000846 if (mHealthdConfig->batteryFullChargePath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700847 path.clear();
848 path.appendFormat("%s/%s/charge_full",
849 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000850 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700851 mHealthdConfig->batteryFullChargePath = path;
852 }
853
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000854 if (mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700855 path.clear();
856 path.appendFormat("%s/%s/current_now",
857 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000858 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700859 mHealthdConfig->batteryCurrentNowPath = path;
860 }
861
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000862 if (mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700863 path.clear();
864 path.appendFormat("%s/%s/cycle_count",
865 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000866 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700867 mHealthdConfig->batteryCycleCountPath = path;
868 }
869
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000870 if (mHealthdConfig->batteryCapacityLevelPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800871 path.clear();
872 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000873 if (access(path.c_str(), R_OK) == 0) {
874 mHealthdConfig->batteryCapacityLevelPath = path;
875 }
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800876 }
877
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000878 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800879 path.clear();
880 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000881 if (access(path.c_str(), R_OK) == 0)
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800882 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
883 }
884
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000885 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) {
Stephane Lee1c108ed2020-02-10 18:23:57 -0800886 path.clear();
887 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000888 if (access(path.c_str(), R_OK) == 0)
Stephane Lee1c108ed2020-02-10 18:23:57 -0800889 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
890 }
891
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000892 if (mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynorbc102112013-08-27 18:11:49 -0700893 path.clear();
894 path.appendFormat("%s/%s/current_avg",
895 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000896 if (access(path.c_str(), R_OK) == 0)
Todd Poynorbc102112013-08-27 18:11:49 -0700897 mHealthdConfig->batteryCurrentAvgPath = path;
898 }
899
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000900 if (mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700901 path.clear();
902 path.appendFormat("%s/%s/charge_counter",
903 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000904 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700905 mHealthdConfig->batteryChargeCounterPath = path;
906 }
907
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000908 if (mHealthdConfig->batteryTemperaturePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700909 path.clear();
910 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
911 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000912 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700913 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700914 }
915 }
916
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000917 if (mHealthdConfig->batteryTechnologyPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700918 path.clear();
919 path.appendFormat("%s/%s/technology",
920 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000921 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700922 mHealthdConfig->batteryTechnologyPath = path;
923 }
924
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000925 if (mHealthdConfig->batteryStateOfHealthPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800926 path.clear();
927 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000928 if (access(path.c_str(), R_OK) == 0) {
Jack Wue561d032022-11-24 12:19:41 +0800929 mHealthdConfig->batteryStateOfHealthPath = path;
930 } else {
931 path.clear();
932 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000933 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800934 mHealthdConfig->batteryStateOfHealthPath = path;
935 }
936 }
937
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000938 if (mHealthdConfig->batteryHealthStatusPath.empty()) {
Jack Wub57f68a2023-02-04 19:56:06 +0800939 path.clear();
940 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000941 if (access(path.c_str(), R_OK) == 0) {
942 mHealthdConfig->batteryHealthStatusPath = path;
943 }
Jack Wub57f68a2023-02-04 19:56:06 +0800944 }
945
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000946 if (mHealthdConfig->batteryManufacturingDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800947 path.clear();
948 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000949 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800950 mHealthdConfig->batteryManufacturingDatePath = path;
951 }
952
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000953 if (mHealthdConfig->batteryFirstUsageDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800954 path.clear();
955 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000956 if (access(path.c_str(), R_OK) == 0) {
957 mHealthdConfig->batteryFirstUsageDatePath = path;
958 }
Jack Wue561d032022-11-24 12:19:41 +0800959 }
960
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000961 if (mHealthdConfig->chargingStatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800962 path.clear();
963 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000964 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path;
Jack Wue561d032022-11-24 12:19:41 +0800965 }
966
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000967 if (mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800968 path.clear();
969 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000970 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
Jack Wue561d032022-11-24 12:19:41 +0800971 }
972
Todd Poynor752faf22013-06-12 13:25:59 -0700973 break;
974
975 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
976 break;
977 }
Jack Wu06b90412021-12-15 20:40:21 +0800978
979 // Look for "is_dock" file
980 path.clear();
981 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000982 if (access(path.c_str(), R_OK) == 0) {
Jack Wu06b90412021-12-15 20:40:21 +0800983 path.clear();
984 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000985 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Jack Wu06b90412021-12-15 20:40:21 +0800986 }
Todd Poynor752faf22013-06-12 13:25:59 -0700987 }
Todd Poynor752faf22013-06-12 13:25:59 -0700988 }
989
Ian Pedowitz585ab652015-10-12 19:01:00 -0700990 // Typically the case for devices which do not have a battery and
991 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700992 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700993 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700994 hc->periodic_chores_interval_fast = -1;
995 hc->periodic_chores_interval_slow = -1;
996 } else {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000997 if (mHealthdConfig->batteryStatusPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700998 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000999 if (mHealthdConfig->batteryHealthPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001000 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001001 if (mHealthdConfig->batteryPresentPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001002 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001003 if (mHealthdConfig->batteryCapacityPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001004 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001005 if (mHealthdConfig->batteryVoltagePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001006 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001007 if (mHealthdConfig->batteryTemperaturePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001008 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001009 if (mHealthdConfig->batteryTechnologyPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001010 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001011 if (mHealthdConfig->batteryCurrentNowPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001012 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001013 if (mHealthdConfig->batteryFullChargePath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001014 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001015 if (mHealthdConfig->batteryCycleCountPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001016 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001017 if (mHealthdConfig->batteryCapacityLevelPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001018 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001019 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001020 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001021 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -08001022 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001023 if (mHealthdConfig->batteryStateOfHealthPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001024 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001025 if (mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +08001026 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001027 if (mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001028 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001029 if (mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001030 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001031 if (mHealthdConfig->chargingStatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001032 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001033 if (mHealthdConfig->chargingPolicyPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001034 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001035 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001036
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001037 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1038 && strtol(pval, NULL, 10) != 0) {
1039 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1040 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1041 }
Todd Poynor752faf22013-06-12 13:25:59 -07001042}
1043
1044}; // namespace android