blob: 0c976322d2cd9232791c9a58bfaff66596b24dd0 [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;
Yifan Hongb99d15c2022-03-01 12:12:34 -080062using aidl::android::hardware::health::BatteryStatus;
63using aidl::android::hardware::health::HealthInfo;
64
65namespace {
66
67// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
68// Skips storageInfo and diskStats.
69void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
70 ::android::hardware::health::V1_0::HealthInfo* out) {
71 out->chargerAcOnline = in.chargerAcOnline;
72 out->chargerUsbOnline = in.chargerUsbOnline;
73 out->chargerWirelessOnline = in.chargerWirelessOnline;
74 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
75 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
76 out->batteryStatus =
77 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
78 out->batteryHealth =
79 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
80 out->batteryPresent = in.batteryPresent;
81 out->batteryLevel = in.batteryLevel;
82 out->batteryVoltage = in.batteryVoltageMillivolts;
83 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
84 out->batteryCurrent = in.batteryCurrentMicroamps;
85 out->batteryCycleCount = in.batteryCycleCount;
86 out->batteryFullCharge = in.batteryFullChargeUah;
87 out->batteryChargeCounter = in.batteryChargeCounterUah;
88 out->batteryTechnology = in.batteryTechnology;
89}
90
91void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
92 ::android::hardware::health::V2_0::HealthInfo* out) {
93 translateToHidl(in, &out->legacy);
94 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
95 // Skip storageInfo and diskStats
96}
97
98void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
99 ::android::hardware::health::V2_1::HealthInfo* out) {
100 translateToHidl(in, &out->legacy);
101 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
102 in.batteryCapacityLevel);
103 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
104 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
105}
106
107} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700108
Todd Poynor752faf22013-06-12 13:25:59 -0700109namespace android {
110
Yifan Hong1d4368b2019-10-07 11:18:04 -0700111template <typename T>
112struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700113 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700114 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700115};
116
Yifan Hong1d4368b2019-10-07 11:18:04 -0700117template <typename T>
118static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700119 for (int i = 0; map[i].s; i++)
120 if (!strcmp(str, map[i].s))
121 return map[i].val;
122
Yifan Hong1d4368b2019-10-07 11:18:04 -0700123 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800124}
125
Yifan Hongb99d15c2022-03-01 12:12:34 -0800126static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000127 *health_info = {
128 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
129 .batteryChargeTimeToFullNowSeconds =
130 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
131 .batteryStatus = BatteryStatus::UNKNOWN,
132 .batteryHealth = BatteryHealth::UNKNOWN,
133 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800134}
135
Todd Poynore030a102018-01-19 14:03:59 -0800136BatteryMonitor::BatteryMonitor()
137 : mHealthdConfig(nullptr),
138 mBatteryDevicePresent(false),
139 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700140 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800141 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800142 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800143 initHealthInfo(mHealthInfo.get());
144}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145
146BatteryMonitor::~BatteryMonitor() {}
147
Yifan Hongb99d15c2022-03-01 12:12:34 -0800148HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
149 HealthInfo_1_0 health_info_1_0;
150 translateToHidl(*mHealthInfo, &health_info_1_0);
151 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800152}
153
Yifan Hongb99d15c2022-03-01 12:12:34 -0800154HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
155 HealthInfo_2_0 health_info_2_0;
156 translateToHidl(*mHealthInfo, &health_info_2_0);
157 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800158}
159
Yifan Hongb99d15c2022-03-01 12:12:34 -0800160HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
161 HealthInfo_2_1 health_info_2_1;
162 translateToHidl(*mHealthInfo, &health_info_2_1);
163 return health_info_2_1;
164}
165
166const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700167 return *mHealthInfo;
168}
169
170BatteryStatus getBatteryStatus(const char* status) {
171 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
172 {"Unknown", BatteryStatus::UNKNOWN},
173 {"Charging", BatteryStatus::CHARGING},
174 {"Discharging", BatteryStatus::DISCHARGING},
175 {"Not charging", BatteryStatus::NOT_CHARGING},
176 {"Full", BatteryStatus::FULL},
177 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700178 };
179
Yifan Hong1d4368b2019-10-07 11:18:04 -0700180 auto ret = mapSysfsString(status, batteryStatusMap);
181 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700182 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700183 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 }
185
Yifan Hong1d4368b2019-10-07 11:18:04 -0700186 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700187}
188
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800189BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
190 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
191 {"Unknown", BatteryCapacityLevel::UNKNOWN},
192 {"Critical", BatteryCapacityLevel::CRITICAL},
193 {"Low", BatteryCapacityLevel::LOW},
194 {"Normal", BatteryCapacityLevel::NORMAL},
195 {"High", BatteryCapacityLevel::HIGH},
196 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800197 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800198 };
199
200 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
201 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800202 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
203 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800204 }
205
206 return *ret;
207}
208
Yifan Hong1d4368b2019-10-07 11:18:04 -0700209BatteryHealth getBatteryHealth(const char* status) {
210 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
211 {"Unknown", BatteryHealth::UNKNOWN},
212 {"Good", BatteryHealth::GOOD},
213 {"Overheat", BatteryHealth::OVERHEAT},
214 {"Dead", BatteryHealth::DEAD},
215 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
216 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
217 {"Cold", BatteryHealth::COLD},
218 // battery health values from JEITA spec
219 {"Warm", BatteryHealth::GOOD},
220 {"Cool", BatteryHealth::GOOD},
221 {"Hot", BatteryHealth::OVERHEAT},
222 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700223 };
224
Yifan Hong1d4368b2019-10-07 11:18:04 -0700225 auto ret = mapSysfsString(status, batteryHealthMap);
226 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700227 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700229 }
230
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700232}
233
Jack Wub57f68a2023-02-04 19:56:06 +0800234BatteryHealth getBatteryHealthStatus(int status) {
235 BatteryHealth value;
236
237 if (status == BatteryMonitor::BH_NOMINAL)
238 value = BatteryHealth::GOOD;
239 else if (status == BatteryMonitor::BH_MARGINAL)
240 value = BatteryHealth::FAIR;
241 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
242 value = BatteryHealth::DEAD;
243 else if (status == BatteryMonitor::BH_FAILED)
244 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800245 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
246 value = BatteryHealth::NOT_AVAILABLE;
Jack Wu8231c3f2023-05-19 14:31:53 +0800247 else if (status == BatteryMonitor::BH_INCONSISTENT)
248 value = BatteryHealth::INCONSISTENT;
Jack Wub57f68a2023-02-04 19:56:06 +0800249 else
250 value = BatteryHealth::UNKNOWN;
251
252 return value;
253}
254
Jack Wue561d032022-11-24 12:19:41 +0800255BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
256 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
257 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
258 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
259 {NULL, BatteryChargingPolicy::DEFAULT},
260 };
261
262 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
263 if (!ret) {
264 *ret = BatteryChargingPolicy::DEFAULT;
265 }
266
267 return *ret;
268}
269
270BatteryChargingState getBatteryChargingState(const char* chargingState) {
271 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
272 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
273 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
274 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
275 {NULL, BatteryChargingState::NORMAL},
276 };
277
278 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
279 if (!ret) {
280 *ret = BatteryChargingState::NORMAL;
281 }
282
283 return *ret;
284}
285
Bart Van Assche095c9442022-03-02 17:36:34 +0000286static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000287 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800288 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700289 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700290 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700291 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700292}
293
Jack Wue561d032022-11-24 12:19:41 +0800294static bool writeToFile(const String8& path, int32_t in_value) {
295 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
296}
297
Bart Van Assche095c9442022-03-02 17:36:34 +0000298static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700299 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000300 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
301 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
302 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
303 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
304 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
305 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
307 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
308 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
309 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
310 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
311 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
312 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
313 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700314 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700315 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700316 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700317
Bart Van Assche095c9442022-03-02 17:36:34 +0000318 if (readFromFile(path, &buf) <= 0) {
319 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
320 }
Todd Poynor752faf22013-06-12 13:25:59 -0700321
Yifan Hong1d4368b2019-10-07 11:18:04 -0700322 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000323 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700324 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000325 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100326 }
Todd Poynor752faf22013-06-12 13:25:59 -0700327
Yifan Hong1d4368b2019-10-07 11:18:04 -0700328 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700329}
330
Bart Van Assche095c9442022-03-02 17:36:34 +0000331static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700332 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700333 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700334
335 if (readFromFile(path, &buf) > 0)
336 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700337 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700338
339 return value;
340}
341
Bart Van Assche095c9442022-03-02 17:36:34 +0000342static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700343 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700344 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700345
346 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700347 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700348
Todd Poynor752faf22013-06-12 13:25:59 -0700349 return value;
350}
351
Bart Van Assche095c9442022-03-02 17:36:34 +0000352static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900353 constexpr char kScopeDevice[] = "Device";
354
355 String8 path;
356 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
357 std::string scope;
358 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
359}
360
Yifan Hong1353e702019-10-07 10:41:30 -0700361void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800362 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700363
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000364 if (!mHealthdConfig->batteryPresentPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800365 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700366 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800367 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700368
Yifan Hongb99d15c2022-03-01 12:12:34 -0800369 mHealthInfo->batteryLevel = mBatteryFixedCapacity
370 ? mBatteryFixedCapacity
371 : getIntField(mHealthdConfig->batteryCapacityPath);
372 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700373
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000374 if (!mHealthdConfig->batteryCurrentNowPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800375 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700376
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000377 if (!mHealthdConfig->batteryFullChargePath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800378 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700379
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000380 if (!mHealthdConfig->batteryCycleCountPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800381 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700382
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000383 if (!mHealthdConfig->batteryChargeCounterPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800384 mHealthInfo->batteryChargeCounterUah =
385 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700386
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000387 if (!mHealthdConfig->batteryCurrentAvgPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800388 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700389 getIntField(mHealthdConfig->batteryCurrentAvgPath);
390
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000391 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800392 mHealthInfo->batteryChargeTimeToFullNowSeconds =
393 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
394
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000395 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800396 mHealthInfo->batteryFullChargeDesignCapacityUah =
397 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700398
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000399 if (!mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +0800400 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
401
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000402 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000403 mHealthInfo->batteryHealthData->batteryStateOfHealth =
404 getIntField(mHealthdConfig->batteryStateOfHealthPath);
405
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000406 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800407 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
408 getIntField(mHealthdConfig->batteryManufacturingDatePath);
409
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000410 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800411 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
412 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
413
Yifan Hongb99d15c2022-03-01 12:12:34 -0800414 mHealthInfo->batteryTemperatureTenthsCelsius =
415 mBatteryFixedTemperature ? mBatteryFixedTemperature
416 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700417
Michael Scott3217c5c2016-06-05 11:20:13 -0700418 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700419
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800420 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
421 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
422
Michael Scott3217c5c2016-06-05 11:20:13 -0700423 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800424 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700425
Jack Wub57f68a2023-02-04 19:56:06 +0800426 // Backward compatible with android.hardware.health V1
427 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
428 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
429 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
430 } else {
431 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
432 }
Todd Poynor752faf22013-06-12 13:25:59 -0700433
Michael Scott3217c5c2016-06-05 11:20:13 -0700434 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000435 mHealthInfo->batteryTechnology = buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700436
Jack Wue561d032022-11-24 12:19:41 +0800437 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
438 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
439
440 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
441 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
442
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700443 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700444
ShevT9d98a6a2018-07-26 11:47:47 +0300445 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700446 String8 path;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000447 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700448 if (getIntField(path)) {
449 path.clear();
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000450 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700451 switch(readPowerSupplyType(path)) {
452 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800453 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700454 break;
455 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800456 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700457 break;
458 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800459 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700460 break;
Jack Wu06b90412021-12-15 20:40:21 +0800461 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800462 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800463 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700464 default:
Jack Wu06b90412021-12-15 20:40:21 +0800465 path.clear();
466 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000467 mChargerNames[i].c_str());
468 if (access(path.c_str(), R_OK) == 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800469 mHealthInfo->chargerDockOnline = true;
470 else
Jack Wu06b90412021-12-15 20:40:21 +0800471 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000472 mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700473 }
474 path.clear();
475 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000476 mChargerNames[i].c_str());
477 int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700478
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700479 path.clear();
480 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000481 mChargerNames[i].c_str());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700482
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700483 int ChargingVoltage =
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000484 (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700485
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700486 double power = ((double)ChargingCurrent / MILLION) *
487 ((double)ChargingVoltage / MILLION);
488 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800489 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
490 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700491 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700492 }
493 }
494 }
Yifan Hong1353e702019-10-07 10:41:30 -0700495}
Todd Poynor752faf22013-06-12 13:25:59 -0700496
Bart Van Assche095c9442022-03-02 17:36:34 +0000497static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700498 char dmesgline[256];
499 size_t len;
500 if (props.batteryPresent) {
501 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800502 props.batteryLevel, props.batteryVoltageMillivolts,
503 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
504 abs(props.batteryTemperatureTenthsCelsius / 10),
505 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
506 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700507
Yifan Hong1353e702019-10-07 10:41:30 -0700508 len = strlen(dmesgline);
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000509 if (!healthd_config.batteryCurrentNowPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700510 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800511 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700512 }
513
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000514 if (!healthd_config.batteryFullChargePath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700515 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800516 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700517 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700518
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000519 if (!healthd_config.batteryCycleCountPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700520 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
521 props.batteryCycleCount);
522 }
523 } else {
524 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700525 }
526
Yifan Hongb99d15c2022-03-01 12:12:34 -0800527 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700528 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800529 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700530
531 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
532}
533
Bart Van Assche095c9442022-03-02 17:36:34 +0000534void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
535 const struct healthd_config& healthd_config) {
536 HealthInfo aidl_health_info;
537 (void)android::h2a::translate(health_info, &aidl_health_info);
538 doLogValues(aidl_health_info, healthd_config);
539}
540
541void BatteryMonitor::logValues(void) {
542 doLogValues(*mHealthInfo, *mHealthdConfig);
543}
544
Yifan Hong1353e702019-10-07 10:41:30 -0700545bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800546 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800547 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800548 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700549}
550
Yabin Cuiaedf6032016-02-19 18:03:23 -0800551int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700552 BatteryStatus result = BatteryStatus::UNKNOWN;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000553 if (!mHealthdConfig->batteryStatusPath.empty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700554 std::string buf;
555 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
556 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800557 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700558 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800559}
560
Jack Wue561d032022-11-24 12:19:41 +0800561status_t BatteryMonitor::setChargingPolicy(int value) {
562 status_t ret = NAME_NOT_FOUND;
563 bool result;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000564 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800565 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
566 if (!result) {
567 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
568 ret = BAD_VALUE;
569 } else {
570 ret = OK;
571 }
572 }
573 return ret;
574}
575
576int BatteryMonitor::getChargingPolicy() {
577 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000578 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800579 std::string buf;
580 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
581 result = getBatteryChargingPolicy(buf.c_str());
582 }
583 return static_cast<int>(result);
584}
585
586int BatteryMonitor::getBatteryHealthData(int id) {
587 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000588 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800589 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
590 }
591 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000592 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800593 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
594 }
AleX Pelosiff708922023-02-17 01:39:21 +0000595 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000596 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000597 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
598 }
Jack Wue561d032022-11-24 12:19:41 +0800599 return 0;
600}
601
Todd Poynorc133b712013-08-14 17:39:13 -0700602status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
603 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800604 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700605
Todd Poynor8f132af2014-05-08 17:15:45 -0700606 val->valueInt64 = LONG_MIN;
607
Todd Poynorc133b712013-08-14 17:39:13 -0700608 switch(id) {
609 case BATTERY_PROP_CHARGE_COUNTER:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000610 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700611 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700612 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700613 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700614 } else {
615 ret = NAME_NOT_FOUND;
616 }
617 break;
618
619 case BATTERY_PROP_CURRENT_NOW:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000620 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700621 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700622 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700623 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700624 } else {
625 ret = NAME_NOT_FOUND;
626 }
627 break;
628
Todd Poynorbc102112013-08-27 18:11:49 -0700629 case BATTERY_PROP_CURRENT_AVG:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000630 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700631 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700632 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700633 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700634 } else {
635 ret = NAME_NOT_FOUND;
636 }
637 break;
638
Paul Lawrence347c8de2014-03-19 15:04:40 -0700639 case BATTERY_PROP_CAPACITY:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000640 if (!mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700641 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700642 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700643 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700644 } else {
645 ret = NAME_NOT_FOUND;
646 }
647 break;
648
Todd Poynor8f132af2014-05-08 17:15:45 -0700649 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700650 if (mHealthdConfig->energyCounter) {
651 ret = mHealthdConfig->energyCounter(&val->valueInt64);
652 } else {
653 ret = NAME_NOT_FOUND;
654 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700655 break;
656
Jin Qian72adf112017-02-02 17:31:13 -0800657 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800658 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700659 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800660 break;
661
Jack Wue561d032022-11-24 12:19:41 +0800662 case BATTERY_PROP_CHARGING_POLICY:
663 val->valueInt64 = getChargingPolicy();
664 ret = OK;
665 break;
666
667 case BATTERY_PROP_MANUFACTURING_DATE:
668 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
669 ret = OK;
670 break;
671
672 case BATTERY_PROP_FIRST_USAGE_DATE:
673 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
674 ret = OK;
675 break;
676
AleX Pelosiff708922023-02-17 01:39:21 +0000677 case BATTERY_PROP_STATE_OF_HEALTH:
678 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
679 ret = OK;
680 break;
681
Todd Poynorc133b712013-08-14 17:39:13 -0700682 default:
683 break;
684 }
685
Todd Poynorc133b712013-08-14 17:39:13 -0700686 return ret;
687}
688
Todd Poynor020369d2013-09-18 20:09:33 -0700689void BatteryMonitor::dumpState(int fd) {
690 int v;
691 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800692 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700693
Jack Wu06b90412021-12-15 20:40:21 +0800694 snprintf(vs, sizeof(vs),
695 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
696 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800697 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
698 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700699 write(fd, vs, strlen(vs));
700 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
701 props.batteryStatus, props.batteryHealth, props.batteryPresent);
702 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800703 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
704 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700705 write(fd, vs, strlen(vs));
706
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000707 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700708 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
709 snprintf(vs, sizeof(vs), "current now: %d\n", v);
710 write(fd, vs, strlen(vs));
711 }
712
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000713 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700714 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
715 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
716 write(fd, vs, strlen(vs));
717 }
718
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000719 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700720 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
721 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
722 write(fd, vs, strlen(vs));
723 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700724
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000725 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800726 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700727 write(fd, vs, strlen(vs));
728 }
729
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000730 if (!mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700731 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
732 write(fd, vs, strlen(vs));
733 }
734
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000735 if (!mHealthdConfig->batteryFullChargePath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800736 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700737 write(fd, vs, strlen(vs));
738 }
Todd Poynor020369d2013-09-18 20:09:33 -0700739}
740
Todd Poynorc7464c92013-09-10 12:40:00 -0700741void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700742 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700743 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700744
Todd Poynorf5d30122013-08-12 17:03:35 -0700745 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800746 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700747 if (dir == NULL) {
748 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
749 } else {
750 struct dirent* entry;
751
James Hawkins588a2ca2016-02-18 14:52:46 -0800752 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700753 const char* name = entry->d_name;
754
755 if (!strcmp(name, ".") || !strcmp(name, ".."))
756 continue;
757
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000758 std::vector<String8>::iterator itIgnoreName =
759 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
760 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800761 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
762 continue;
763
Todd Poynor752faf22013-06-12 13:25:59 -0700764 // Look for "type" file in each subdirectory
765 path.clear();
766 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
767 switch(readPowerSupplyType(path)) {
768 case ANDROID_POWER_SUPPLY_TYPE_AC:
769 case ANDROID_POWER_SUPPLY_TYPE_USB:
770 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800771 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700772 path.clear();
773 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000774 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Todd Poynor752faf22013-06-12 13:25:59 -0700775 break;
776
777 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900778 // Some devices expose the battery status of sub-component like
779 // stylus. Such a device-scoped battery info needs to be skipped
780 // in BatteryMonitor, which is intended to report the status of
781 // the battery supplying the power to the whole system.
782 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700783 mBatteryDevicePresent = true;
784
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000785 if (mHealthdConfig->batteryStatusPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700786 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700787 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
788 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000789 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700790 }
791
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000792 if (mHealthdConfig->batteryHealthPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700793 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700794 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
795 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000796 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700797 }
798
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000799 if (mHealthdConfig->batteryPresentPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700800 path.clear();
801 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
802 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000803 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700804 }
805
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000806 if (mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700807 path.clear();
808 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
809 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000810 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700811 }
812
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000813 if (mHealthdConfig->batteryVoltagePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700814 path.clear();
815 path.appendFormat("%s/%s/voltage_now",
816 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000817 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700818 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700819 }
820 }
821
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000822 if (mHealthdConfig->batteryFullChargePath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700823 path.clear();
824 path.appendFormat("%s/%s/charge_full",
825 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000826 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700827 mHealthdConfig->batteryFullChargePath = path;
828 }
829
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000830 if (mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700831 path.clear();
832 path.appendFormat("%s/%s/current_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->batteryCurrentNowPath = path;
836 }
837
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000838 if (mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700839 path.clear();
840 path.appendFormat("%s/%s/cycle_count",
841 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000842 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700843 mHealthdConfig->batteryCycleCountPath = path;
844 }
845
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000846 if (mHealthdConfig->batteryCapacityLevelPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800847 path.clear();
848 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000849 if (access(path.c_str(), R_OK) == 0) {
850 mHealthdConfig->batteryCapacityLevelPath = path;
851 }
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800852 }
853
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000854 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800855 path.clear();
856 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000857 if (access(path.c_str(), R_OK) == 0)
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800858 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
859 }
860
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000861 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) {
Stephane Lee1c108ed2020-02-10 18:23:57 -0800862 path.clear();
863 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000864 if (access(path.c_str(), R_OK) == 0)
Stephane Lee1c108ed2020-02-10 18:23:57 -0800865 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
866 }
867
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000868 if (mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynorbc102112013-08-27 18:11:49 -0700869 path.clear();
870 path.appendFormat("%s/%s/current_avg",
871 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000872 if (access(path.c_str(), R_OK) == 0)
Todd Poynorbc102112013-08-27 18:11:49 -0700873 mHealthdConfig->batteryCurrentAvgPath = path;
874 }
875
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000876 if (mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700877 path.clear();
878 path.appendFormat("%s/%s/charge_counter",
879 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000880 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700881 mHealthdConfig->batteryChargeCounterPath = path;
882 }
883
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000884 if (mHealthdConfig->batteryTemperaturePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700885 path.clear();
886 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
887 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000888 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700889 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700890 }
891 }
892
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000893 if (mHealthdConfig->batteryTechnologyPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700894 path.clear();
895 path.appendFormat("%s/%s/technology",
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->batteryTechnologyPath = path;
899 }
900
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000901 if (mHealthdConfig->batteryStateOfHealthPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800902 path.clear();
903 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000904 if (access(path.c_str(), R_OK) == 0) {
Jack Wue561d032022-11-24 12:19:41 +0800905 mHealthdConfig->batteryStateOfHealthPath = path;
906 } else {
907 path.clear();
908 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000909 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800910 mHealthdConfig->batteryStateOfHealthPath = path;
911 }
912 }
913
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000914 if (mHealthdConfig->batteryHealthStatusPath.empty()) {
Jack Wub57f68a2023-02-04 19:56:06 +0800915 path.clear();
916 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000917 if (access(path.c_str(), R_OK) == 0) {
918 mHealthdConfig->batteryHealthStatusPath = path;
919 }
Jack Wub57f68a2023-02-04 19:56:06 +0800920 }
921
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000922 if (mHealthdConfig->batteryManufacturingDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800923 path.clear();
924 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000925 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800926 mHealthdConfig->batteryManufacturingDatePath = path;
927 }
928
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000929 if (mHealthdConfig->batteryFirstUsageDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800930 path.clear();
931 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000932 if (access(path.c_str(), R_OK) == 0) {
933 mHealthdConfig->batteryFirstUsageDatePath = path;
934 }
Jack Wue561d032022-11-24 12:19:41 +0800935 }
936
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000937 if (mHealthdConfig->chargingStatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800938 path.clear();
939 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000940 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path;
Jack Wue561d032022-11-24 12:19:41 +0800941 }
942
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000943 if (mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800944 path.clear();
945 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000946 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
Jack Wue561d032022-11-24 12:19:41 +0800947 }
948
Todd Poynor752faf22013-06-12 13:25:59 -0700949 break;
950
951 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
952 break;
953 }
Jack Wu06b90412021-12-15 20:40:21 +0800954
955 // Look for "is_dock" file
956 path.clear();
957 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000958 if (access(path.c_str(), R_OK) == 0) {
Jack Wu06b90412021-12-15 20:40:21 +0800959 path.clear();
960 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000961 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Jack Wu06b90412021-12-15 20:40:21 +0800962 }
Todd Poynor752faf22013-06-12 13:25:59 -0700963 }
Todd Poynor752faf22013-06-12 13:25:59 -0700964 }
965
Ian Pedowitz585ab652015-10-12 19:01:00 -0700966 // Typically the case for devices which do not have a battery and
967 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700968 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700969 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700970 hc->periodic_chores_interval_fast = -1;
971 hc->periodic_chores_interval_slow = -1;
972 } else {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000973 if (mHealthdConfig->batteryStatusPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700974 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000975 if (mHealthdConfig->batteryHealthPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700976 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000977 if (mHealthdConfig->batteryPresentPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700978 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000979 if (mHealthdConfig->batteryCapacityPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700980 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000981 if (mHealthdConfig->batteryVoltagePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700982 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000983 if (mHealthdConfig->batteryTemperaturePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700984 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000985 if (mHealthdConfig->batteryTechnologyPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700986 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000987 if (mHealthdConfig->batteryCurrentNowPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700988 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000989 if (mHealthdConfig->batteryFullChargePath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700990 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000991 if (mHealthdConfig->batteryCycleCountPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700992 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000993 if (mHealthdConfig->batteryCapacityLevelPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800994 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000995 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800996 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000997 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800998 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000999 if (mHealthdConfig->batteryStateOfHealthPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001000 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001001 if (mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +08001002 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001003 if (mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001004 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001005 if (mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001006 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001007 if (mHealthdConfig->chargingStatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001008 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001009 if (mHealthdConfig->chargingPolicyPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001010 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001011 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001012
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001013 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1014 && strtol(pval, NULL, 10) != 0) {
1015 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1016 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1017 }
Todd Poynor752faf22013-06-12 13:25:59 -07001018}
1019
1020}; // namespace android