blob: 1d05d1eb0013741ccc3726307c1d0fa3a6ef3910 [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,
134 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800135}
136
Todd Poynore030a102018-01-19 14:03:59 -0800137BatteryMonitor::BatteryMonitor()
138 : mHealthdConfig(nullptr),
139 mBatteryDevicePresent(false),
140 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700141 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800142 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800143 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800144 initHealthInfo(mHealthInfo.get());
145}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700146
147BatteryMonitor::~BatteryMonitor() {}
148
Yifan Hongb99d15c2022-03-01 12:12:34 -0800149HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
150 HealthInfo_1_0 health_info_1_0;
151 translateToHidl(*mHealthInfo, &health_info_1_0);
152 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800153}
154
Yifan Hongb99d15c2022-03-01 12:12:34 -0800155HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
156 HealthInfo_2_0 health_info_2_0;
157 translateToHidl(*mHealthInfo, &health_info_2_0);
158 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800159}
160
Yifan Hongb99d15c2022-03-01 12:12:34 -0800161HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
162 HealthInfo_2_1 health_info_2_1;
163 translateToHidl(*mHealthInfo, &health_info_2_1);
164 return health_info_2_1;
165}
166
167const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700168 return *mHealthInfo;
169}
170
171BatteryStatus getBatteryStatus(const char* status) {
172 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
173 {"Unknown", BatteryStatus::UNKNOWN},
174 {"Charging", BatteryStatus::CHARGING},
175 {"Discharging", BatteryStatus::DISCHARGING},
176 {"Not charging", BatteryStatus::NOT_CHARGING},
177 {"Full", BatteryStatus::FULL},
178 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700179 };
180
Yifan Hong1d4368b2019-10-07 11:18:04 -0700181 auto ret = mapSysfsString(status, batteryStatusMap);
182 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700183 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700184 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700185 }
186
Yifan Hong1d4368b2019-10-07 11:18:04 -0700187 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700188}
189
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800190BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
191 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
192 {"Unknown", BatteryCapacityLevel::UNKNOWN},
193 {"Critical", BatteryCapacityLevel::CRITICAL},
194 {"Low", BatteryCapacityLevel::LOW},
195 {"Normal", BatteryCapacityLevel::NORMAL},
196 {"High", BatteryCapacityLevel::HIGH},
197 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800198 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800199 };
200
201 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
202 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800203 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
204 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800205 }
206
207 return *ret;
208}
209
Yifan Hong1d4368b2019-10-07 11:18:04 -0700210BatteryHealth getBatteryHealth(const char* status) {
211 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
212 {"Unknown", BatteryHealth::UNKNOWN},
213 {"Good", BatteryHealth::GOOD},
214 {"Overheat", BatteryHealth::OVERHEAT},
215 {"Dead", BatteryHealth::DEAD},
216 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
217 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
218 {"Cold", BatteryHealth::COLD},
219 // battery health values from JEITA spec
220 {"Warm", BatteryHealth::GOOD},
221 {"Cool", BatteryHealth::GOOD},
222 {"Hot", BatteryHealth::OVERHEAT},
David Anderson629a26b2023-12-11 15:25:13 -0800223 {"Calibration required", BatteryHealth::INCONSISTENT},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700224 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700225 };
226
Yifan Hong1d4368b2019-10-07 11:18:04 -0700227 auto ret = mapSysfsString(status, batteryHealthMap);
228 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700229 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700230 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700231 }
232
Yifan Hong1d4368b2019-10-07 11:18:04 -0700233 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700234}
235
Jack Wub57f68a2023-02-04 19:56:06 +0800236BatteryHealth getBatteryHealthStatus(int status) {
237 BatteryHealth value;
238
239 if (status == BatteryMonitor::BH_NOMINAL)
240 value = BatteryHealth::GOOD;
241 else if (status == BatteryMonitor::BH_MARGINAL)
242 value = BatteryHealth::FAIR;
243 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
244 value = BatteryHealth::DEAD;
245 else if (status == BatteryMonitor::BH_FAILED)
246 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800247 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
248 value = BatteryHealth::NOT_AVAILABLE;
Jack Wu8231c3f2023-05-19 14:31:53 +0800249 else if (status == BatteryMonitor::BH_INCONSISTENT)
250 value = BatteryHealth::INCONSISTENT;
Jack Wub57f68a2023-02-04 19:56:06 +0800251 else
252 value = BatteryHealth::UNKNOWN;
253
254 return value;
255}
256
Jack Wue561d032022-11-24 12:19:41 +0800257BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
258 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
259 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
260 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
261 {NULL, BatteryChargingPolicy::DEFAULT},
262 };
263
264 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
265 if (!ret) {
266 *ret = BatteryChargingPolicy::DEFAULT;
267 }
268
269 return *ret;
270}
271
272BatteryChargingState getBatteryChargingState(const char* chargingState) {
273 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
274 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
275 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
276 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
277 {NULL, BatteryChargingState::NORMAL},
278 };
279
280 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
281 if (!ret) {
282 *ret = BatteryChargingState::NORMAL;
283 }
284
285 return *ret;
286}
287
Bart Van Assche095c9442022-03-02 17:36:34 +0000288static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000289 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800290 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700291 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700292 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700293 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700294}
295
Jack Wue561d032022-11-24 12:19:41 +0800296static bool writeToFile(const String8& path, int32_t in_value) {
297 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
298}
299
Bart Van Assche095c9442022-03-02 17:36:34 +0000300static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700301 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000302 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
303 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
304 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
305 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
307 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
308 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Abhijeet Dharmapurikaree19f9f2015-09-14 16:35:26 -0700309 {"USB_HVDCP_3", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Bart Van Assche095c9442022-03-02 17:36:34 +0000310 {"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},
Abhijeet Dharmapurikaree19f9f2015-09-14 16:35:26 -0700317 {"DASH", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700318 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700319 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700320 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700321
Bart Van Assche095c9442022-03-02 17:36:34 +0000322 if (readFromFile(path, &buf) <= 0) {
323 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
324 }
Todd Poynor752faf22013-06-12 13:25:59 -0700325
Yifan Hong1d4368b2019-10-07 11:18:04 -0700326 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000327 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700328 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000329 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100330 }
Todd Poynor752faf22013-06-12 13:25:59 -0700331
Yifan Hong1d4368b2019-10-07 11:18:04 -0700332 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700333}
334
Bart Van Assche095c9442022-03-02 17:36:34 +0000335static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700336 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700337 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700338
339 if (readFromFile(path, &buf) > 0)
340 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700341 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700342
343 return value;
344}
345
Bart Van Assche095c9442022-03-02 17:36:34 +0000346static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700347 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700348 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700349
350 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700351 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700352
Todd Poynor752faf22013-06-12 13:25:59 -0700353 return value;
354}
355
Bart Van Assche095c9442022-03-02 17:36:34 +0000356static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900357 constexpr char kScopeDevice[] = "Device";
358
359 String8 path;
360 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
361 std::string scope;
362 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
363}
364
Yifan Hong1353e702019-10-07 10:41:30 -0700365void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800366 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700367
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000368 if (!mHealthdConfig->batteryPresentPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800369 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700370 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800371 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700372
Yifan Hongb99d15c2022-03-01 12:12:34 -0800373 mHealthInfo->batteryLevel = mBatteryFixedCapacity
374 ? mBatteryFixedCapacity
375 : getIntField(mHealthdConfig->batteryCapacityPath);
376 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700377
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000378 if (!mHealthdConfig->batteryCurrentNowPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800379 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700380
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000381 if (!mHealthdConfig->batteryFullChargePath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800382 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700383
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000384 if (!mHealthdConfig->batteryCycleCountPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800385 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700386
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000387 if (!mHealthdConfig->batteryChargeCounterPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800388 mHealthInfo->batteryChargeCounterUah =
389 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700390
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000391 if (!mHealthdConfig->batteryCurrentAvgPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800392 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700393 getIntField(mHealthdConfig->batteryCurrentAvgPath);
394
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000395 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800396 mHealthInfo->batteryChargeTimeToFullNowSeconds =
397 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
398
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000399 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800400 mHealthInfo->batteryFullChargeDesignCapacityUah =
401 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700402
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000403 if (!mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +0800404 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
405
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000406 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000407 mHealthInfo->batteryHealthData->batteryStateOfHealth =
408 getIntField(mHealthdConfig->batteryStateOfHealthPath);
409
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000410 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800411 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
412 getIntField(mHealthdConfig->batteryManufacturingDatePath);
413
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000414 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800415 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
416 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
417
Yifan Hongb99d15c2022-03-01 12:12:34 -0800418 mHealthInfo->batteryTemperatureTenthsCelsius =
419 mBatteryFixedTemperature ? mBatteryFixedTemperature
420 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700421
Michael Scott3217c5c2016-06-05 11:20:13 -0700422 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700423
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800424 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
425 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
426
Michael Scott3217c5c2016-06-05 11:20:13 -0700427 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800428 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700429
Jack Wub57f68a2023-02-04 19:56:06 +0800430 // Backward compatible with android.hardware.health V1
431 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
432 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
433 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
434 } else {
435 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
436 }
Todd Poynor752faf22013-06-12 13:25:59 -0700437
Michael Scott3217c5c2016-06-05 11:20:13 -0700438 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000439 mHealthInfo->batteryTechnology = buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700440
Jack Wue561d032022-11-24 12:19:41 +0800441 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
442 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
443
444 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
445 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
446
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700447 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700448
ShevT9d98a6a2018-07-26 11:47:47 +0300449 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700450 String8 path;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000451 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700452 if (getIntField(path)) {
453 path.clear();
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000454 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700455 switch(readPowerSupplyType(path)) {
456 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800457 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700458 break;
459 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800460 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700461 break;
462 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800463 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700464 break;
Jack Wu06b90412021-12-15 20:40:21 +0800465 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800466 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800467 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700468 default:
Jack Wu06b90412021-12-15 20:40:21 +0800469 path.clear();
470 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000471 mChargerNames[i].c_str());
472 if (access(path.c_str(), R_OK) == 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800473 mHealthInfo->chargerDockOnline = true;
474 else
Jack Wu06b90412021-12-15 20:40:21 +0800475 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000476 mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700477 }
478 path.clear();
479 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000480 mChargerNames[i].c_str());
481 int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700482
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700483 path.clear();
484 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000485 mChargerNames[i].c_str());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700486
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700487 int ChargingVoltage =
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000488 (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700489
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700490 double power = ((double)ChargingCurrent / MILLION) *
491 ((double)ChargingVoltage / MILLION);
492 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800493 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
494 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700495 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700496 }
497 }
498 }
Yifan Hong1353e702019-10-07 10:41:30 -0700499}
Todd Poynor752faf22013-06-12 13:25:59 -0700500
Bart Van Assche095c9442022-03-02 17:36:34 +0000501static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700502 char dmesgline[256];
503 size_t len;
504 if (props.batteryPresent) {
505 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800506 props.batteryLevel, props.batteryVoltageMillivolts,
507 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
508 abs(props.batteryTemperatureTenthsCelsius / 10),
509 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
510 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700511
Yifan Hong1353e702019-10-07 10:41:30 -0700512 len = strlen(dmesgline);
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000513 if (!healthd_config.batteryCurrentNowPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700514 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800515 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700516 }
517
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000518 if (!healthd_config.batteryFullChargePath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700519 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800520 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700521 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700522
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000523 if (!healthd_config.batteryCycleCountPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700524 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
525 props.batteryCycleCount);
526 }
527 } else {
528 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700529 }
530
Yifan Hongb99d15c2022-03-01 12:12:34 -0800531 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700532 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800533 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700534
AleX Pelosif08aede2024-02-15 18:42:11 +0000535 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Yifan Hong1353e702019-10-07 10:41:30 -0700536}
537
Bart Van Assche095c9442022-03-02 17:36:34 +0000538void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
539 const struct healthd_config& healthd_config) {
540 HealthInfo aidl_health_info;
541 (void)android::h2a::translate(health_info, &aidl_health_info);
542 doLogValues(aidl_health_info, healthd_config);
543}
544
545void BatteryMonitor::logValues(void) {
546 doLogValues(*mHealthInfo, *mHealthdConfig);
547}
548
Yifan Hong1353e702019-10-07 10:41:30 -0700549bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800550 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800551 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800552 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700553}
554
Yabin Cuiaedf6032016-02-19 18:03:23 -0800555int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700556 BatteryStatus result = BatteryStatus::UNKNOWN;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000557 if (!mHealthdConfig->batteryStatusPath.empty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700558 std::string buf;
559 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
560 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800561 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700562 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800563}
564
Jack Wue561d032022-11-24 12:19:41 +0800565status_t BatteryMonitor::setChargingPolicy(int value) {
566 status_t ret = NAME_NOT_FOUND;
567 bool result;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000568 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800569 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
570 if (!result) {
571 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
572 ret = BAD_VALUE;
573 } else {
574 ret = OK;
575 }
576 }
577 return ret;
578}
579
580int BatteryMonitor::getChargingPolicy() {
581 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000582 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800583 std::string buf;
584 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
585 result = getBatteryChargingPolicy(buf.c_str());
586 }
587 return static_cast<int>(result);
588}
589
590int BatteryMonitor::getBatteryHealthData(int id) {
591 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000592 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800593 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
594 }
595 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000596 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800597 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
598 }
AleX Pelosiff708922023-02-17 01:39:21 +0000599 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000600 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000601 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
602 }
David Andersond5ed26a2023-12-08 15:12:24 -0800603 if (id == BATTERY_PROP_PART_STATUS) {
604 return static_cast<int>(BatteryPartStatus::UNSUPPORTED);
605 }
Jack Wue561d032022-11-24 12:19:41 +0800606 return 0;
607}
608
Todd Poynorc133b712013-08-14 17:39:13 -0700609status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
610 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800611 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700612
Todd Poynor8f132af2014-05-08 17:15:45 -0700613 val->valueInt64 = LONG_MIN;
614
Todd Poynorc133b712013-08-14 17:39:13 -0700615 switch(id) {
616 case BATTERY_PROP_CHARGE_COUNTER:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000617 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700618 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700619 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700620 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700621 } else {
622 ret = NAME_NOT_FOUND;
623 }
624 break;
625
626 case BATTERY_PROP_CURRENT_NOW:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000627 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700628 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700629 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700630 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700631 } else {
632 ret = NAME_NOT_FOUND;
633 }
634 break;
635
Todd Poynorbc102112013-08-27 18:11:49 -0700636 case BATTERY_PROP_CURRENT_AVG:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000637 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700638 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700639 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700640 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700641 } else {
642 ret = NAME_NOT_FOUND;
643 }
644 break;
645
Paul Lawrence347c8de2014-03-19 15:04:40 -0700646 case BATTERY_PROP_CAPACITY:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000647 if (!mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700648 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700649 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700650 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700651 } else {
652 ret = NAME_NOT_FOUND;
653 }
654 break;
655
Todd Poynor8f132af2014-05-08 17:15:45 -0700656 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700657 if (mHealthdConfig->energyCounter) {
658 ret = mHealthdConfig->energyCounter(&val->valueInt64);
659 } else {
660 ret = NAME_NOT_FOUND;
661 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700662 break;
663
Jin Qian72adf112017-02-02 17:31:13 -0800664 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800665 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700666 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800667 break;
668
Jack Wue561d032022-11-24 12:19:41 +0800669 case BATTERY_PROP_CHARGING_POLICY:
670 val->valueInt64 = getChargingPolicy();
671 ret = OK;
672 break;
673
674 case BATTERY_PROP_MANUFACTURING_DATE:
675 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
676 ret = OK;
677 break;
678
679 case BATTERY_PROP_FIRST_USAGE_DATE:
680 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
681 ret = OK;
682 break;
683
AleX Pelosiff708922023-02-17 01:39:21 +0000684 case BATTERY_PROP_STATE_OF_HEALTH:
685 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
686 ret = OK;
687 break;
688
David Andersond5ed26a2023-12-08 15:12:24 -0800689 case BATTERY_PROP_PART_STATUS:
690 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_PART_STATUS);
691 ret = OK;
692 break;
693
Todd Poynorc133b712013-08-14 17:39:13 -0700694 default:
695 break;
696 }
697
Todd Poynorc133b712013-08-14 17:39:13 -0700698 return ret;
699}
700
David Andersond5ed26a2023-12-08 15:12:24 -0800701status_t BatteryMonitor::getSerialNumber(std::optional<std::string>* out) {
702 *out = std::nullopt;
703 return OK;
704}
705
Todd Poynor020369d2013-09-18 20:09:33 -0700706void BatteryMonitor::dumpState(int fd) {
707 int v;
708 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800709 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700710
Jack Wu06b90412021-12-15 20:40:21 +0800711 snprintf(vs, sizeof(vs),
712 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
713 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800714 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
715 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700716 write(fd, vs, strlen(vs));
717 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
718 props.batteryStatus, props.batteryHealth, props.batteryPresent);
719 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800720 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
721 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700722 write(fd, vs, strlen(vs));
723
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000724 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700725 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
726 snprintf(vs, sizeof(vs), "current now: %d\n", v);
727 write(fd, vs, strlen(vs));
728 }
729
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000730 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700731 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
732 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
733 write(fd, vs, strlen(vs));
734 }
735
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000736 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700737 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
738 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
739 write(fd, vs, strlen(vs));
740 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700741
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000742 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800743 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700744 write(fd, vs, strlen(vs));
745 }
746
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000747 if (!mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700748 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
749 write(fd, vs, strlen(vs));
750 }
751
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000752 if (!mHealthdConfig->batteryFullChargePath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800753 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700754 write(fd, vs, strlen(vs));
755 }
Todd Poynor020369d2013-09-18 20:09:33 -0700756}
757
Todd Poynorc7464c92013-09-10 12:40:00 -0700758void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700759 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700760 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700761
Todd Poynorf5d30122013-08-12 17:03:35 -0700762 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800763 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700764 if (dir == NULL) {
765 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
766 } else {
767 struct dirent* entry;
768
James Hawkins588a2ca2016-02-18 14:52:46 -0800769 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700770 const char* name = entry->d_name;
771
772 if (!strcmp(name, ".") || !strcmp(name, ".."))
773 continue;
774
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000775 std::vector<String8>::iterator itIgnoreName =
776 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
777 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800778 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
779 continue;
780
Todd Poynor752faf22013-06-12 13:25:59 -0700781 // Look for "type" file in each subdirectory
782 path.clear();
783 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
784 switch(readPowerSupplyType(path)) {
785 case ANDROID_POWER_SUPPLY_TYPE_AC:
786 case ANDROID_POWER_SUPPLY_TYPE_USB:
787 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800788 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700789 path.clear();
790 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000791 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Todd Poynor752faf22013-06-12 13:25:59 -0700792 break;
793
794 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900795 // Some devices expose the battery status of sub-component like
796 // stylus. Such a device-scoped battery info needs to be skipped
797 // in BatteryMonitor, which is intended to report the status of
798 // the battery supplying the power to the whole system.
799 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700800 mBatteryDevicePresent = true;
801
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000802 if (mHealthdConfig->batteryStatusPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700803 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700804 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
805 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000806 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700807 }
808
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000809 if (mHealthdConfig->batteryHealthPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700810 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700811 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
812 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000813 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700814 }
815
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000816 if (mHealthdConfig->batteryPresentPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700817 path.clear();
818 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
819 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000820 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700821 }
822
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000823 if (mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700824 path.clear();
825 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
826 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000827 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700828 }
829
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000830 if (mHealthdConfig->batteryVoltagePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700831 path.clear();
832 path.appendFormat("%s/%s/voltage_now",
833 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000834 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700835 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700836 }
837 }
838
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000839 if (mHealthdConfig->batteryFullChargePath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700840 path.clear();
841 path.appendFormat("%s/%s/charge_full",
842 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000843 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700844 mHealthdConfig->batteryFullChargePath = path;
845 }
846
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000847 if (mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700848 path.clear();
849 path.appendFormat("%s/%s/current_now",
850 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000851 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700852 mHealthdConfig->batteryCurrentNowPath = path;
853 }
854
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000855 if (mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700856 path.clear();
857 path.appendFormat("%s/%s/cycle_count",
858 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000859 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700860 mHealthdConfig->batteryCycleCountPath = path;
861 }
862
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000863 if (mHealthdConfig->batteryCapacityLevelPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800864 path.clear();
865 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000866 if (access(path.c_str(), R_OK) == 0) {
867 mHealthdConfig->batteryCapacityLevelPath = path;
868 }
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800869 }
870
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000871 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800872 path.clear();
873 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000874 if (access(path.c_str(), R_OK) == 0)
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800875 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
876 }
877
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000878 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) {
Stephane Lee1c108ed2020-02-10 18:23:57 -0800879 path.clear();
880 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000881 if (access(path.c_str(), R_OK) == 0)
Stephane Lee1c108ed2020-02-10 18:23:57 -0800882 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
883 }
884
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000885 if (mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynorbc102112013-08-27 18:11:49 -0700886 path.clear();
887 path.appendFormat("%s/%s/current_avg",
888 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000889 if (access(path.c_str(), R_OK) == 0)
Todd Poynorbc102112013-08-27 18:11:49 -0700890 mHealthdConfig->batteryCurrentAvgPath = path;
891 }
892
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000893 if (mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700894 path.clear();
895 path.appendFormat("%s/%s/charge_counter",
896 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000897 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700898 mHealthdConfig->batteryChargeCounterPath = path;
899 }
900
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000901 if (mHealthdConfig->batteryTemperaturePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700902 path.clear();
903 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
904 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000905 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700906 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700907 }
908 }
909
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000910 if (mHealthdConfig->batteryTechnologyPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700911 path.clear();
912 path.appendFormat("%s/%s/technology",
913 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000914 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700915 mHealthdConfig->batteryTechnologyPath = path;
916 }
917
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000918 if (mHealthdConfig->batteryStateOfHealthPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800919 path.clear();
920 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000921 if (access(path.c_str(), R_OK) == 0) {
Jack Wue561d032022-11-24 12:19:41 +0800922 mHealthdConfig->batteryStateOfHealthPath = path;
923 } else {
924 path.clear();
925 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000926 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800927 mHealthdConfig->batteryStateOfHealthPath = path;
928 }
929 }
930
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000931 if (mHealthdConfig->batteryHealthStatusPath.empty()) {
Jack Wub57f68a2023-02-04 19:56:06 +0800932 path.clear();
933 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000934 if (access(path.c_str(), R_OK) == 0) {
935 mHealthdConfig->batteryHealthStatusPath = path;
936 }
Jack Wub57f68a2023-02-04 19:56:06 +0800937 }
938
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000939 if (mHealthdConfig->batteryManufacturingDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800940 path.clear();
941 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000942 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800943 mHealthdConfig->batteryManufacturingDatePath = path;
944 }
945
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000946 if (mHealthdConfig->batteryFirstUsageDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800947 path.clear();
948 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000949 if (access(path.c_str(), R_OK) == 0) {
950 mHealthdConfig->batteryFirstUsageDatePath = path;
951 }
Jack Wue561d032022-11-24 12:19:41 +0800952 }
953
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000954 if (mHealthdConfig->chargingStatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800955 path.clear();
956 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000957 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path;
Jack Wue561d032022-11-24 12:19:41 +0800958 }
959
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000960 if (mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800961 path.clear();
962 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000963 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
Jack Wue561d032022-11-24 12:19:41 +0800964 }
965
Todd Poynor752faf22013-06-12 13:25:59 -0700966 break;
967
968 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
969 break;
970 }
Jack Wu06b90412021-12-15 20:40:21 +0800971
972 // Look for "is_dock" file
973 path.clear();
974 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000975 if (access(path.c_str(), R_OK) == 0) {
Jack Wu06b90412021-12-15 20:40:21 +0800976 path.clear();
977 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000978 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Jack Wu06b90412021-12-15 20:40:21 +0800979 }
Todd Poynor752faf22013-06-12 13:25:59 -0700980 }
Todd Poynor752faf22013-06-12 13:25:59 -0700981 }
982
Ian Pedowitz585ab652015-10-12 19:01:00 -0700983 // Typically the case for devices which do not have a battery and
984 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700985 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700986 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700987 hc->periodic_chores_interval_fast = -1;
988 hc->periodic_chores_interval_slow = -1;
989 } else {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000990 if (mHealthdConfig->batteryStatusPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700991 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000992 if (mHealthdConfig->batteryHealthPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700993 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000994 if (mHealthdConfig->batteryPresentPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700995 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000996 if (mHealthdConfig->batteryCapacityPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700997 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000998 if (mHealthdConfig->batteryVoltagePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700999 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001000 if (mHealthdConfig->batteryTemperaturePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001001 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001002 if (mHealthdConfig->batteryTechnologyPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001003 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001004 if (mHealthdConfig->batteryCurrentNowPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001005 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001006 if (mHealthdConfig->batteryFullChargePath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001007 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001008 if (mHealthdConfig->batteryCycleCountPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001009 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001010 if (mHealthdConfig->batteryCapacityLevelPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001011 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001012 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001013 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001014 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -08001015 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001016 if (mHealthdConfig->batteryStateOfHealthPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001017 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001018 if (mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +08001019 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001020 if (mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001021 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001022 if (mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001023 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001024 if (mHealthdConfig->chargingStatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001025 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001026 if (mHealthdConfig->chargingPolicyPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001027 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001028 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001029
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001030 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1031 && strtol(pval, NULL, 10) != 0) {
1032 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1033 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1034 }
Todd Poynor752faf22013-06-12 13:25:59 -07001035}
1036
1037}; // namespace android