blob: 15b9ed2a96258f736b6e1ab49a3ec55384c3c17f [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
Stefan Berger06c6f502019-04-06 11:05:19 +020053#ifdef HEALTHD_USE_BATTERY_INFO
54#define SYSFS_BATTERY_CURRENT "/sys/class/power_supply/battery/current_now"
55#define SYSFS_BATTERY_VOLTAGE "/sys/class/power_supply/battery/voltage_now"
56#endif
Todd Poynor752faf22013-06-12 13:25:59 -070057
Yifan Hong1d4368b2019-10-07 11:18:04 -070058using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
59using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
60using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
Yifan Hongb99d15c2022-03-01 12:12:34 -080061using aidl::android::hardware::health::BatteryCapacityLevel;
Jack Wue561d032022-11-24 12:19:41 +080062using aidl::android::hardware::health::BatteryChargingPolicy;
63using aidl::android::hardware::health::BatteryChargingState;
Yifan Hongb99d15c2022-03-01 12:12:34 -080064using aidl::android::hardware::health::BatteryHealth;
Jack Wue561d032022-11-24 12:19:41 +080065using aidl::android::hardware::health::BatteryHealthData;
David Andersond5ed26a2023-12-08 15:12:24 -080066using aidl::android::hardware::health::BatteryPartStatus;
Yifan Hongb99d15c2022-03-01 12:12:34 -080067using aidl::android::hardware::health::BatteryStatus;
68using aidl::android::hardware::health::HealthInfo;
69
70namespace {
71
72// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
73// Skips storageInfo and diskStats.
74void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
75 ::android::hardware::health::V1_0::HealthInfo* out) {
76 out->chargerAcOnline = in.chargerAcOnline;
77 out->chargerUsbOnline = in.chargerUsbOnline;
78 out->chargerWirelessOnline = in.chargerWirelessOnline;
79 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
80 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
81 out->batteryStatus =
82 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
83 out->batteryHealth =
84 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
85 out->batteryPresent = in.batteryPresent;
86 out->batteryLevel = in.batteryLevel;
87 out->batteryVoltage = in.batteryVoltageMillivolts;
88 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
89 out->batteryCurrent = in.batteryCurrentMicroamps;
90 out->batteryCycleCount = in.batteryCycleCount;
91 out->batteryFullCharge = in.batteryFullChargeUah;
92 out->batteryChargeCounter = in.batteryChargeCounterUah;
93 out->batteryTechnology = in.batteryTechnology;
94}
95
96void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
97 ::android::hardware::health::V2_0::HealthInfo* out) {
98 translateToHidl(in, &out->legacy);
99 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
100 // Skip storageInfo and diskStats
101}
102
103void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
104 ::android::hardware::health::V2_1::HealthInfo* out) {
105 translateToHidl(in, &out->legacy);
106 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
107 in.batteryCapacityLevel);
108 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
109 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
110}
111
112} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700113
Todd Poynor752faf22013-06-12 13:25:59 -0700114namespace android {
115
Yifan Hong1d4368b2019-10-07 11:18:04 -0700116template <typename T>
117struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700118 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700119 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700120};
121
Yifan Hong1d4368b2019-10-07 11:18:04 -0700122template <typename T>
123static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700124 for (int i = 0; map[i].s; i++)
125 if (!strcmp(str, map[i].s))
126 return map[i].val;
127
Yifan Hong1d4368b2019-10-07 11:18:04 -0700128 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800129}
130
Yifan Hongb99d15c2022-03-01 12:12:34 -0800131static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000132 *health_info = {
133 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
134 .batteryChargeTimeToFullNowSeconds =
135 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
136 .batteryStatus = BatteryStatus::UNKNOWN,
137 .batteryHealth = BatteryHealth::UNKNOWN,
138 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800139}
140
Todd Poynore030a102018-01-19 14:03:59 -0800141BatteryMonitor::BatteryMonitor()
142 : mHealthdConfig(nullptr),
143 mBatteryDevicePresent(false),
144 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800146 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800147 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800148 initHealthInfo(mHealthInfo.get());
149}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700150
151BatteryMonitor::~BatteryMonitor() {}
152
Yifan Hongb99d15c2022-03-01 12:12:34 -0800153HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
154 HealthInfo_1_0 health_info_1_0;
155 translateToHidl(*mHealthInfo, &health_info_1_0);
156 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800157}
158
Yifan Hongb99d15c2022-03-01 12:12:34 -0800159HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
160 HealthInfo_2_0 health_info_2_0;
161 translateToHidl(*mHealthInfo, &health_info_2_0);
162 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800163}
164
Yifan Hongb99d15c2022-03-01 12:12:34 -0800165HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
166 HealthInfo_2_1 health_info_2_1;
167 translateToHidl(*mHealthInfo, &health_info_2_1);
168 return health_info_2_1;
169}
170
171const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700172 return *mHealthInfo;
173}
174
175BatteryStatus getBatteryStatus(const char* status) {
176 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
177 {"Unknown", BatteryStatus::UNKNOWN},
178 {"Charging", BatteryStatus::CHARGING},
179 {"Discharging", BatteryStatus::DISCHARGING},
180 {"Not charging", BatteryStatus::NOT_CHARGING},
181 {"Full", BatteryStatus::FULL},
182 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700183 };
184
Yifan Hong1d4368b2019-10-07 11:18:04 -0700185 auto ret = mapSysfsString(status, batteryStatusMap);
186 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700187 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700188 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700189 }
190
Yifan Hong1d4368b2019-10-07 11:18:04 -0700191 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700192}
193
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800194BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
195 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
196 {"Unknown", BatteryCapacityLevel::UNKNOWN},
197 {"Critical", BatteryCapacityLevel::CRITICAL},
198 {"Low", BatteryCapacityLevel::LOW},
199 {"Normal", BatteryCapacityLevel::NORMAL},
200 {"High", BatteryCapacityLevel::HIGH},
201 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800202 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800203 };
204
205 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
206 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800207 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
208 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800209 }
210
211 return *ret;
212}
213
Yifan Hong1d4368b2019-10-07 11:18:04 -0700214BatteryHealth getBatteryHealth(const char* status) {
215 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
216 {"Unknown", BatteryHealth::UNKNOWN},
217 {"Good", BatteryHealth::GOOD},
218 {"Overheat", BatteryHealth::OVERHEAT},
219 {"Dead", BatteryHealth::DEAD},
220 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
221 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
222 {"Cold", BatteryHealth::COLD},
223 // battery health values from JEITA spec
224 {"Warm", BatteryHealth::GOOD},
225 {"Cool", BatteryHealth::GOOD},
226 {"Hot", BatteryHealth::OVERHEAT},
David Anderson629a26b2023-12-11 15:25:13 -0800227 {"Calibration required", BatteryHealth::INCONSISTENT},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700229 };
230
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 auto ret = mapSysfsString(status, batteryHealthMap);
232 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700233 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700234 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700235 }
236
Yifan Hong1d4368b2019-10-07 11:18:04 -0700237 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700238}
239
Jack Wub57f68a2023-02-04 19:56:06 +0800240BatteryHealth getBatteryHealthStatus(int status) {
241 BatteryHealth value;
242
243 if (status == BatteryMonitor::BH_NOMINAL)
244 value = BatteryHealth::GOOD;
245 else if (status == BatteryMonitor::BH_MARGINAL)
246 value = BatteryHealth::FAIR;
247 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
248 value = BatteryHealth::DEAD;
249 else if (status == BatteryMonitor::BH_FAILED)
250 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800251 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
252 value = BatteryHealth::NOT_AVAILABLE;
Jack Wu8231c3f2023-05-19 14:31:53 +0800253 else if (status == BatteryMonitor::BH_INCONSISTENT)
254 value = BatteryHealth::INCONSISTENT;
Jack Wub57f68a2023-02-04 19:56:06 +0800255 else
256 value = BatteryHealth::UNKNOWN;
257
258 return value;
259}
260
Jack Wue561d032022-11-24 12:19:41 +0800261BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
262 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
263 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
264 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
265 {NULL, BatteryChargingPolicy::DEFAULT},
266 };
267
268 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
269 if (!ret) {
270 *ret = BatteryChargingPolicy::DEFAULT;
271 }
272
273 return *ret;
274}
275
276BatteryChargingState getBatteryChargingState(const char* chargingState) {
277 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
278 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
279 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
280 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
281 {NULL, BatteryChargingState::NORMAL},
282 };
283
284 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
285 if (!ret) {
286 *ret = BatteryChargingState::NORMAL;
287 }
288
289 return *ret;
290}
291
Bart Van Assche095c9442022-03-02 17:36:34 +0000292static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000293 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800294 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700295 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700296 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700297 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700298}
299
Jack Wue561d032022-11-24 12:19:41 +0800300static bool writeToFile(const String8& path, int32_t in_value) {
301 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
302}
303
Bart Van Assche095c9442022-03-02 17:36:34 +0000304static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700305 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000306 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
307 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
308 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
309 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
310 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
311 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
312 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Abhijeet Dharmapurikaree19f9f2015-09-14 16:35:26 -0700313 {"USB_HVDCP_3", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Bart Van Assche095c9442022-03-02 17:36:34 +0000314 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
315 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
316 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
317 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
318 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
319 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
320 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Abhijeet Dharmapurikaree19f9f2015-09-14 16:35:26 -0700321 {"DASH", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700322 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700323 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700324 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700325
Bart Van Assche095c9442022-03-02 17:36:34 +0000326 if (readFromFile(path, &buf) <= 0) {
327 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
328 }
Todd Poynor752faf22013-06-12 13:25:59 -0700329
Yifan Hong1d4368b2019-10-07 11:18:04 -0700330 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000331 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700332 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000333 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100334 }
Todd Poynor752faf22013-06-12 13:25:59 -0700335
Yifan Hong1d4368b2019-10-07 11:18:04 -0700336 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700337}
338
Bart Van Assche095c9442022-03-02 17:36:34 +0000339static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700340 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700341 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700342
343 if (readFromFile(path, &buf) > 0)
344 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700345 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700346
347 return value;
348}
349
Bart Van Assche095c9442022-03-02 17:36:34 +0000350static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700351 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700352 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700353
354 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700355 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700356
Todd Poynor752faf22013-06-12 13:25:59 -0700357 return value;
358}
359
Bart Van Assche095c9442022-03-02 17:36:34 +0000360static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900361 constexpr char kScopeDevice[] = "Device";
362
363 String8 path;
364 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
365 std::string scope;
366 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
367}
368
Yifan Hong1353e702019-10-07 10:41:30 -0700369void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800370 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700371
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000372 if (!mHealthdConfig->batteryPresentPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800373 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700374 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800375 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700376
Yifan Hongb99d15c2022-03-01 12:12:34 -0800377 mHealthInfo->batteryLevel = mBatteryFixedCapacity
378 ? mBatteryFixedCapacity
379 : getIntField(mHealthdConfig->batteryCapacityPath);
380 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700381
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000382 if (!mHealthdConfig->batteryCurrentNowPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800383 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700384
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000385 if (!mHealthdConfig->batteryFullChargePath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800386 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700387
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000388 if (!mHealthdConfig->batteryCycleCountPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800389 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700390
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000391 if (!mHealthdConfig->batteryChargeCounterPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800392 mHealthInfo->batteryChargeCounterUah =
393 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700394
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000395 if (!mHealthdConfig->batteryCurrentAvgPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800396 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700397 getIntField(mHealthdConfig->batteryCurrentAvgPath);
398
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000399 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800400 mHealthInfo->batteryChargeTimeToFullNowSeconds =
401 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
402
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000403 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800404 mHealthInfo->batteryFullChargeDesignCapacityUah =
405 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700406
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000407 if (!mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +0800408 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
409
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000410 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000411 mHealthInfo->batteryHealthData->batteryStateOfHealth =
412 getIntField(mHealthdConfig->batteryStateOfHealthPath);
413
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000414 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800415 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
416 getIntField(mHealthdConfig->batteryManufacturingDatePath);
417
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000418 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800419 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
420 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
421
Yifan Hongb99d15c2022-03-01 12:12:34 -0800422 mHealthInfo->batteryTemperatureTenthsCelsius =
423 mBatteryFixedTemperature ? mBatteryFixedTemperature
424 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700425
Michael Scott3217c5c2016-06-05 11:20:13 -0700426 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700427
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800428 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
429 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
430
Michael Scott3217c5c2016-06-05 11:20:13 -0700431 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800432 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700433
Jack Wub57f68a2023-02-04 19:56:06 +0800434 // Backward compatible with android.hardware.health V1
435 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
436 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
437 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
438 } else {
439 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
440 }
Todd Poynor752faf22013-06-12 13:25:59 -0700441
Michael Scott3217c5c2016-06-05 11:20:13 -0700442 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000443 mHealthInfo->batteryTechnology = buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700444
Jack Wue561d032022-11-24 12:19:41 +0800445 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
446 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
447
448 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
449 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
450
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700451 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700452
ShevT9d98a6a2018-07-26 11:47:47 +0300453 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700454 String8 path;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000455 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700456 if (getIntField(path)) {
457 path.clear();
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000458 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700459 switch(readPowerSupplyType(path)) {
460 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800461 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700462 break;
463 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800464 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700465 break;
466 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800467 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700468 break;
Jack Wu06b90412021-12-15 20:40:21 +0800469 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800470 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800471 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700472 default:
Jack Wu06b90412021-12-15 20:40:21 +0800473 path.clear();
474 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000475 mChargerNames[i].c_str());
476 if (access(path.c_str(), R_OK) == 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800477 mHealthInfo->chargerDockOnline = true;
478 else
Jack Wu06b90412021-12-15 20:40:21 +0800479 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000480 mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700481 }
Stefan Berger06c6f502019-04-06 11:05:19 +0200482
483#ifdef HEALTHD_USE_BATTERY_INFO
484 int ChargingCurrent = (access(SYSFS_BATTERY_CURRENT, R_OK) == 0) ?
485 abs(getIntField(String8(SYSFS_BATTERY_CURRENT))) : 0;
486
487 int ChargingVoltage = (access(SYSFS_BATTERY_VOLTAGE, R_OK) == 0) ?
488 getIntField(String8(SYSFS_BATTERY_VOLTAGE)) : DEFAULT_VBUS_VOLTAGE;
489#else
Michael Scott3217c5c2016-06-05 11:20:13 -0700490 path.clear();
491 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000492 mChargerNames[i].c_str());
493 int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700494
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700495 path.clear();
496 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000497 mChargerNames[i].c_str());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700498
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700499 int ChargingVoltage =
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000500 (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE;
Stefan Berger06c6f502019-04-06 11:05:19 +0200501#endif
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700502
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700503 double power = ((double)ChargingCurrent / MILLION) *
504 ((double)ChargingVoltage / MILLION);
505 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800506 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
507 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700508 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700509 }
510 }
511 }
Yifan Hong1353e702019-10-07 10:41:30 -0700512}
Todd Poynor752faf22013-06-12 13:25:59 -0700513
Bart Van Assche095c9442022-03-02 17:36:34 +0000514static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700515 char dmesgline[256];
516 size_t len;
517 if (props.batteryPresent) {
518 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800519 props.batteryLevel, props.batteryVoltageMillivolts,
520 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
521 abs(props.batteryTemperatureTenthsCelsius / 10),
522 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
523 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700524
Yifan Hong1353e702019-10-07 10:41:30 -0700525 len = strlen(dmesgline);
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000526 if (!healthd_config.batteryCurrentNowPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700527 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800528 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700529 }
530
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000531 if (!healthd_config.batteryFullChargePath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700532 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800533 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700534 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700535
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000536 if (!healthd_config.batteryCycleCountPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700537 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
538 props.batteryCycleCount);
539 }
540 } else {
541 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700542 }
543
Yifan Hongb99d15c2022-03-01 12:12:34 -0800544 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700545 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800546 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700547
AleX Pelosif08aede2024-02-15 18:42:11 +0000548 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Yifan Hong1353e702019-10-07 10:41:30 -0700549}
550
Bart Van Assche095c9442022-03-02 17:36:34 +0000551void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
552 const struct healthd_config& healthd_config) {
553 HealthInfo aidl_health_info;
554 (void)android::h2a::translate(health_info, &aidl_health_info);
555 doLogValues(aidl_health_info, healthd_config);
556}
557
558void BatteryMonitor::logValues(void) {
559 doLogValues(*mHealthInfo, *mHealthdConfig);
560}
561
Yifan Hong1353e702019-10-07 10:41:30 -0700562bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800563 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800564 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800565 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700566}
567
Yabin Cuiaedf6032016-02-19 18:03:23 -0800568int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700569 BatteryStatus result = BatteryStatus::UNKNOWN;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000570 if (!mHealthdConfig->batteryStatusPath.empty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700571 std::string buf;
572 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
573 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800574 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700575 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800576}
577
Jack Wue561d032022-11-24 12:19:41 +0800578status_t BatteryMonitor::setChargingPolicy(int value) {
579 status_t ret = NAME_NOT_FOUND;
580 bool result;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000581 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800582 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
583 if (!result) {
584 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
585 ret = BAD_VALUE;
586 } else {
587 ret = OK;
588 }
589 }
590 return ret;
591}
592
593int BatteryMonitor::getChargingPolicy() {
594 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000595 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800596 std::string buf;
597 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
598 result = getBatteryChargingPolicy(buf.c_str());
599 }
600 return static_cast<int>(result);
601}
602
603int BatteryMonitor::getBatteryHealthData(int id) {
604 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000605 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800606 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
607 }
608 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000609 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800610 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
611 }
AleX Pelosiff708922023-02-17 01:39:21 +0000612 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000613 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000614 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
615 }
David Andersond5ed26a2023-12-08 15:12:24 -0800616 if (id == BATTERY_PROP_PART_STATUS) {
617 return static_cast<int>(BatteryPartStatus::UNSUPPORTED);
618 }
Jack Wue561d032022-11-24 12:19:41 +0800619 return 0;
620}
621
Todd Poynorc133b712013-08-14 17:39:13 -0700622status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
623 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800624 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700625
Todd Poynor8f132af2014-05-08 17:15:45 -0700626 val->valueInt64 = LONG_MIN;
627
Todd Poynorc133b712013-08-14 17:39:13 -0700628 switch(id) {
629 case BATTERY_PROP_CHARGE_COUNTER:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000630 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700631 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700632 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700633 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700634 } else {
635 ret = NAME_NOT_FOUND;
636 }
637 break;
638
639 case BATTERY_PROP_CURRENT_NOW:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000640 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700641 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700642 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700643 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700644 } else {
645 ret = NAME_NOT_FOUND;
646 }
647 break;
648
Todd Poynorbc102112013-08-27 18:11:49 -0700649 case BATTERY_PROP_CURRENT_AVG:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000650 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700651 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700652 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700653 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700654 } else {
655 ret = NAME_NOT_FOUND;
656 }
657 break;
658
Paul Lawrence347c8de2014-03-19 15:04:40 -0700659 case BATTERY_PROP_CAPACITY:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000660 if (!mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700661 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700662 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700663 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700664 } else {
665 ret = NAME_NOT_FOUND;
666 }
667 break;
668
Todd Poynor8f132af2014-05-08 17:15:45 -0700669 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700670 if (mHealthdConfig->energyCounter) {
671 ret = mHealthdConfig->energyCounter(&val->valueInt64);
672 } else {
673 ret = NAME_NOT_FOUND;
674 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700675 break;
676
Jin Qian72adf112017-02-02 17:31:13 -0800677 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800678 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700679 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800680 break;
681
Jack Wue561d032022-11-24 12:19:41 +0800682 case BATTERY_PROP_CHARGING_POLICY:
683 val->valueInt64 = getChargingPolicy();
684 ret = OK;
685 break;
686
687 case BATTERY_PROP_MANUFACTURING_DATE:
688 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
689 ret = OK;
690 break;
691
692 case BATTERY_PROP_FIRST_USAGE_DATE:
693 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
694 ret = OK;
695 break;
696
AleX Pelosiff708922023-02-17 01:39:21 +0000697 case BATTERY_PROP_STATE_OF_HEALTH:
698 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
699 ret = OK;
700 break;
701
David Andersond5ed26a2023-12-08 15:12:24 -0800702 case BATTERY_PROP_PART_STATUS:
703 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_PART_STATUS);
704 ret = OK;
705 break;
706
Todd Poynorc133b712013-08-14 17:39:13 -0700707 default:
708 break;
709 }
710
Todd Poynorc133b712013-08-14 17:39:13 -0700711 return ret;
712}
713
David Andersond5ed26a2023-12-08 15:12:24 -0800714status_t BatteryMonitor::getSerialNumber(std::optional<std::string>* out) {
715 *out = std::nullopt;
716 return OK;
717}
718
Todd Poynor020369d2013-09-18 20:09:33 -0700719void BatteryMonitor::dumpState(int fd) {
720 int v;
721 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800722 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700723
Jack Wu06b90412021-12-15 20:40:21 +0800724 snprintf(vs, sizeof(vs),
725 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
726 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800727 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
728 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700729 write(fd, vs, strlen(vs));
730 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
731 props.batteryStatus, props.batteryHealth, props.batteryPresent);
732 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800733 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
734 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700735 write(fd, vs, strlen(vs));
736
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000737 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700738 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
739 snprintf(vs, sizeof(vs), "current now: %d\n", v);
740 write(fd, vs, strlen(vs));
741 }
742
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000743 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700744 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
745 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
746 write(fd, vs, strlen(vs));
747 }
748
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000749 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700750 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
751 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
752 write(fd, vs, strlen(vs));
753 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700754
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000755 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800756 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700757 write(fd, vs, strlen(vs));
758 }
759
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000760 if (!mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700761 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
762 write(fd, vs, strlen(vs));
763 }
764
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000765 if (!mHealthdConfig->batteryFullChargePath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800766 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700767 write(fd, vs, strlen(vs));
768 }
Todd Poynor020369d2013-09-18 20:09:33 -0700769}
770
Todd Poynorc7464c92013-09-10 12:40:00 -0700771void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700772 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700773 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700774
Todd Poynorf5d30122013-08-12 17:03:35 -0700775 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800776 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700777 if (dir == NULL) {
778 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
779 } else {
780 struct dirent* entry;
781
James Hawkins588a2ca2016-02-18 14:52:46 -0800782 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700783 const char* name = entry->d_name;
784
785 if (!strcmp(name, ".") || !strcmp(name, ".."))
786 continue;
787
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000788 std::vector<String8>::iterator itIgnoreName =
789 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
790 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800791 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
792 continue;
793
Todd Poynor752faf22013-06-12 13:25:59 -0700794 // Look for "type" file in each subdirectory
795 path.clear();
796 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
797 switch(readPowerSupplyType(path)) {
798 case ANDROID_POWER_SUPPLY_TYPE_AC:
799 case ANDROID_POWER_SUPPLY_TYPE_USB:
800 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800801 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700802 path.clear();
803 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000804 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Todd Poynor752faf22013-06-12 13:25:59 -0700805 break;
806
807 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900808 // Some devices expose the battery status of sub-component like
809 // stylus. Such a device-scoped battery info needs to be skipped
810 // in BatteryMonitor, which is intended to report the status of
811 // the battery supplying the power to the whole system.
812 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700813 mBatteryDevicePresent = true;
814
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000815 if (mHealthdConfig->batteryStatusPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700816 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700817 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
818 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000819 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700820 }
821
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000822 if (mHealthdConfig->batteryHealthPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700823 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700824 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
825 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000826 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700827 }
828
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000829 if (mHealthdConfig->batteryPresentPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700830 path.clear();
831 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
832 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000833 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700834 }
835
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000836 if (mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700837 path.clear();
838 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
839 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000840 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700841 }
842
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000843 if (mHealthdConfig->batteryVoltagePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700844 path.clear();
845 path.appendFormat("%s/%s/voltage_now",
846 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000847 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700848 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700849 }
850 }
851
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000852 if (mHealthdConfig->batteryFullChargePath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700853 path.clear();
854 path.appendFormat("%s/%s/charge_full",
855 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000856 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700857 mHealthdConfig->batteryFullChargePath = path;
858 }
859
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000860 if (mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700861 path.clear();
862 path.appendFormat("%s/%s/current_now",
863 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000864 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700865 mHealthdConfig->batteryCurrentNowPath = path;
866 }
867
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000868 if (mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700869 path.clear();
870 path.appendFormat("%s/%s/cycle_count",
871 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000872 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700873 mHealthdConfig->batteryCycleCountPath = path;
874 }
875
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000876 if (mHealthdConfig->batteryCapacityLevelPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800877 path.clear();
878 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000879 if (access(path.c_str(), R_OK) == 0) {
880 mHealthdConfig->batteryCapacityLevelPath = path;
881 }
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800882 }
883
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000884 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800885 path.clear();
886 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000887 if (access(path.c_str(), R_OK) == 0)
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800888 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
889 }
890
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000891 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) {
Stephane Lee1c108ed2020-02-10 18:23:57 -0800892 path.clear();
893 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000894 if (access(path.c_str(), R_OK) == 0)
Stephane Lee1c108ed2020-02-10 18:23:57 -0800895 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
896 }
897
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000898 if (mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynorbc102112013-08-27 18:11:49 -0700899 path.clear();
900 path.appendFormat("%s/%s/current_avg",
901 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000902 if (access(path.c_str(), R_OK) == 0)
Todd Poynorbc102112013-08-27 18:11:49 -0700903 mHealthdConfig->batteryCurrentAvgPath = path;
904 }
905
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000906 if (mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700907 path.clear();
908 path.appendFormat("%s/%s/charge_counter",
909 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000910 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700911 mHealthdConfig->batteryChargeCounterPath = path;
912 }
913
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000914 if (mHealthdConfig->batteryTemperaturePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700915 path.clear();
916 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
917 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000918 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700919 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700920 }
921 }
922
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000923 if (mHealthdConfig->batteryTechnologyPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700924 path.clear();
925 path.appendFormat("%s/%s/technology",
926 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000927 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700928 mHealthdConfig->batteryTechnologyPath = path;
929 }
930
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000931 if (mHealthdConfig->batteryStateOfHealthPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800932 path.clear();
933 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000934 if (access(path.c_str(), R_OK) == 0) {
Jack Wue561d032022-11-24 12:19:41 +0800935 mHealthdConfig->batteryStateOfHealthPath = path;
936 } else {
937 path.clear();
938 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000939 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800940 mHealthdConfig->batteryStateOfHealthPath = path;
941 }
942 }
943
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000944 if (mHealthdConfig->batteryHealthStatusPath.empty()) {
Jack Wub57f68a2023-02-04 19:56:06 +0800945 path.clear();
946 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000947 if (access(path.c_str(), R_OK) == 0) {
948 mHealthdConfig->batteryHealthStatusPath = path;
949 }
Jack Wub57f68a2023-02-04 19:56:06 +0800950 }
951
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000952 if (mHealthdConfig->batteryManufacturingDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800953 path.clear();
954 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000955 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800956 mHealthdConfig->batteryManufacturingDatePath = path;
957 }
958
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000959 if (mHealthdConfig->batteryFirstUsageDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800960 path.clear();
961 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000962 if (access(path.c_str(), R_OK) == 0) {
963 mHealthdConfig->batteryFirstUsageDatePath = path;
964 }
Jack Wue561d032022-11-24 12:19:41 +0800965 }
966
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000967 if (mHealthdConfig->chargingStatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800968 path.clear();
969 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000970 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path;
Jack Wue561d032022-11-24 12:19:41 +0800971 }
972
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000973 if (mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800974 path.clear();
975 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000976 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
Jack Wue561d032022-11-24 12:19:41 +0800977 }
978
Todd Poynor752faf22013-06-12 13:25:59 -0700979 break;
980
981 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
982 break;
983 }
Jack Wu06b90412021-12-15 20:40:21 +0800984
985 // Look for "is_dock" file
986 path.clear();
987 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000988 if (access(path.c_str(), R_OK) == 0) {
Jack Wu06b90412021-12-15 20:40:21 +0800989 path.clear();
990 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000991 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Jack Wu06b90412021-12-15 20:40:21 +0800992 }
Todd Poynor752faf22013-06-12 13:25:59 -0700993 }
Todd Poynor752faf22013-06-12 13:25:59 -0700994 }
995
Ian Pedowitz585ab652015-10-12 19:01:00 -0700996 // Typically the case for devices which do not have a battery and
997 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700998 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700999 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001000 hc->periodic_chores_interval_fast = -1;
1001 hc->periodic_chores_interval_slow = -1;
1002 } else {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001003 if (mHealthdConfig->batteryStatusPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001004 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001005 if (mHealthdConfig->batteryHealthPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001006 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001007 if (mHealthdConfig->batteryPresentPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001008 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001009 if (mHealthdConfig->batteryCapacityPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001010 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001011 if (mHealthdConfig->batteryVoltagePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001012 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001013 if (mHealthdConfig->batteryTemperaturePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001014 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001015 if (mHealthdConfig->batteryTechnologyPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001016 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001017 if (mHealthdConfig->batteryCurrentNowPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001018 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001019 if (mHealthdConfig->batteryFullChargePath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001020 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001021 if (mHealthdConfig->batteryCycleCountPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001022 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001023 if (mHealthdConfig->batteryCapacityLevelPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001024 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001025 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001026 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001027 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -08001028 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001029 if (mHealthdConfig->batteryStateOfHealthPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001030 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001031 if (mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +08001032 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001033 if (mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001034 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001035 if (mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001036 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001037 if (mHealthdConfig->chargingStatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001038 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001039 if (mHealthdConfig->chargingPolicyPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001040 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001041 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001042
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001043 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1044 && strtol(pval, NULL, 10) != 0) {
1045 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1046 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1047 }
Todd Poynor752faf22013-06-12 13:25:59 -07001048}
1049
1050}; // namespace android