blob: 53ba6cad52182a451042553de1f5d332325a6dd3 [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);
Abhijeet Dharmapurikar69332482016-05-24 15:12:11 -0700331 if (!ret)
Bart Van Assche095c9442022-03-02 17:36:34 +0000332 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700333
Yifan Hong1d4368b2019-10-07 11:18:04 -0700334 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700335}
336
Bart Van Assche095c9442022-03-02 17:36:34 +0000337static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700338 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700339 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700340
341 if (readFromFile(path, &buf) > 0)
342 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700343 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700344
345 return value;
346}
347
Bart Van Assche095c9442022-03-02 17:36:34 +0000348static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700349 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700350 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700351
352 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700353 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700354
Todd Poynor752faf22013-06-12 13:25:59 -0700355 return value;
356}
357
Bart Van Assche095c9442022-03-02 17:36:34 +0000358static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900359 constexpr char kScopeDevice[] = "Device";
360
361 String8 path;
362 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
363 std::string scope;
364 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
365}
366
Yifan Hong1353e702019-10-07 10:41:30 -0700367void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800368 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700369
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000370 if (!mHealthdConfig->batteryPresentPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800371 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700372 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800373 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700374
Yifan Hongb99d15c2022-03-01 12:12:34 -0800375 mHealthInfo->batteryLevel = mBatteryFixedCapacity
376 ? mBatteryFixedCapacity
377 : getIntField(mHealthdConfig->batteryCapacityPath);
378 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700379
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000380 if (!mHealthdConfig->batteryCurrentNowPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800381 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700382
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000383 if (!mHealthdConfig->batteryFullChargePath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800384 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700385
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000386 if (!mHealthdConfig->batteryCycleCountPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800387 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700388
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000389 if (!mHealthdConfig->batteryChargeCounterPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800390 mHealthInfo->batteryChargeCounterUah =
391 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700392
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000393 if (!mHealthdConfig->batteryCurrentAvgPath.empty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800394 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700395 getIntField(mHealthdConfig->batteryCurrentAvgPath);
396
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000397 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800398 mHealthInfo->batteryChargeTimeToFullNowSeconds =
399 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
400
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000401 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -0800402 mHealthInfo->batteryFullChargeDesignCapacityUah =
403 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700404
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000405 if (!mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +0800406 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
407
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000408 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000409 mHealthInfo->batteryHealthData->batteryStateOfHealth =
410 getIntField(mHealthdConfig->batteryStateOfHealthPath);
411
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000412 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800413 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
414 getIntField(mHealthdConfig->batteryManufacturingDatePath);
415
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000416 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800417 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
418 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
419
Yifan Hongb99d15c2022-03-01 12:12:34 -0800420 mHealthInfo->batteryTemperatureTenthsCelsius =
421 mBatteryFixedTemperature ? mBatteryFixedTemperature
422 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700423
Michael Scott3217c5c2016-06-05 11:20:13 -0700424 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700425
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800426 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
427 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
428
Michael Scott3217c5c2016-06-05 11:20:13 -0700429 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800430 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700431
Jack Wub57f68a2023-02-04 19:56:06 +0800432 // Backward compatible with android.hardware.health V1
433 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
434 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
435 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
436 } else {
437 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
438 }
Todd Poynor752faf22013-06-12 13:25:59 -0700439
Michael Scott3217c5c2016-06-05 11:20:13 -0700440 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000441 mHealthInfo->batteryTechnology = buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700442
Jack Wue561d032022-11-24 12:19:41 +0800443 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
444 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
445
446 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
447 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
448
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700449 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700450
Abhijeet Dharmapurikar69332482016-05-24 15:12:11 -0700451 // Rescan for the available charger types
452 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
453 if (dir == NULL) {
454 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
455 } else {
456 struct dirent* entry;
457 String8 path;
458
459 mChargerNames.clear();
460
461 while ((entry = readdir(dir.get()))) {
462 const char* name = entry->d_name;
463
464 if (!strcmp(name, ".") || !strcmp(name, ".."))
465 continue;
466
467 // Look for "type" file in each subdirectory
468 path.clear();
469 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
470 switch(readPowerSupplyType(path)) {
471 case ANDROID_POWER_SUPPLY_TYPE_AC:
472 case ANDROID_POWER_SUPPLY_TYPE_USB:
473 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
474 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
475 path.clear();
476 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
477 if (access(path.string(), R_OK) == 0)
478 mChargerNames.add(String8(name));
479 break;
480 default:
481 break;
482 }
483
484 // Look for "is_dock" file
485 path.clear();
486 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
487 if (access(path.string(), R_OK) == 0) {
488 path.clear();
489 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
490 if (access(path.string(), R_OK) == 0)
491 mChargerNames.add(String8(name));
492
493 }
494 }
495 }
496
ShevT9d98a6a2018-07-26 11:47:47 +0300497 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700498 String8 path;
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000499 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700500 if (getIntField(path)) {
501 path.clear();
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000502 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700503 switch(readPowerSupplyType(path)) {
504 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800505 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700506 break;
507 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800508 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700509 break;
510 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800511 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700512 break;
Jack Wu06b90412021-12-15 20:40:21 +0800513 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800514 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800515 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700516 default:
Jack Wu06b90412021-12-15 20:40:21 +0800517 path.clear();
518 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000519 mChargerNames[i].c_str());
520 if (access(path.c_str(), R_OK) == 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800521 mHealthInfo->chargerDockOnline = true;
522 else
Jack Wu06b90412021-12-15 20:40:21 +0800523 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000524 mChargerNames[i].c_str());
Michael Scott3217c5c2016-06-05 11:20:13 -0700525 }
Stefan Berger06c6f502019-04-06 11:05:19 +0200526
527#ifdef HEALTHD_USE_BATTERY_INFO
528 int ChargingCurrent = (access(SYSFS_BATTERY_CURRENT, R_OK) == 0) ?
529 abs(getIntField(String8(SYSFS_BATTERY_CURRENT))) : 0;
530
531 int ChargingVoltage = (access(SYSFS_BATTERY_VOLTAGE, R_OK) == 0) ?
532 getIntField(String8(SYSFS_BATTERY_VOLTAGE)) : DEFAULT_VBUS_VOLTAGE;
533#else
Michael Scott3217c5c2016-06-05 11:20:13 -0700534 path.clear();
535 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000536 mChargerNames[i].c_str());
537 int ChargingCurrent = (access(path.c_str(), R_OK) == 0) ? getIntField(path) : 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700538
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700539 path.clear();
540 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000541 mChargerNames[i].c_str());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700542
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700543 int ChargingVoltage =
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000544 (access(path.c_str(), R_OK) == 0) ? getIntField(path) : DEFAULT_VBUS_VOLTAGE;
Stefan Berger06c6f502019-04-06 11:05:19 +0200545#endif
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700546
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700547 double power = ((double)ChargingCurrent / MILLION) *
548 ((double)ChargingVoltage / MILLION);
549 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800550 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
551 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700552 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700553 }
554 }
555 }
Yifan Hong1353e702019-10-07 10:41:30 -0700556}
Todd Poynor752faf22013-06-12 13:25:59 -0700557
Bart Van Assche095c9442022-03-02 17:36:34 +0000558static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700559 char dmesgline[256];
560 size_t len;
561 if (props.batteryPresent) {
562 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800563 props.batteryLevel, props.batteryVoltageMillivolts,
564 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
565 abs(props.batteryTemperatureTenthsCelsius / 10),
566 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
567 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700568
Yifan Hong1353e702019-10-07 10:41:30 -0700569 len = strlen(dmesgline);
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000570 if (!healthd_config.batteryCurrentNowPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700571 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800572 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700573 }
574
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000575 if (!healthd_config.batteryFullChargePath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700576 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800577 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700578 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700579
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000580 if (!healthd_config.batteryCycleCountPath.empty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700581 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
582 props.batteryCycleCount);
583 }
584 } else {
585 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700586 }
587
Yifan Hongb99d15c2022-03-01 12:12:34 -0800588 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700589 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800590 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700591
AleX Pelosif08aede2024-02-15 18:42:11 +0000592 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Yifan Hong1353e702019-10-07 10:41:30 -0700593}
594
Bart Van Assche095c9442022-03-02 17:36:34 +0000595void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
596 const struct healthd_config& healthd_config) {
597 HealthInfo aidl_health_info;
598 (void)android::h2a::translate(health_info, &aidl_health_info);
599 doLogValues(aidl_health_info, healthd_config);
600}
601
602void BatteryMonitor::logValues(void) {
603 doLogValues(*mHealthInfo, *mHealthdConfig);
604}
605
Yifan Hong1353e702019-10-07 10:41:30 -0700606bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800607 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800608 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800609 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700610}
611
Yabin Cuiaedf6032016-02-19 18:03:23 -0800612int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700613 BatteryStatus result = BatteryStatus::UNKNOWN;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000614 if (!mHealthdConfig->batteryStatusPath.empty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700615 std::string buf;
616 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
617 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800618 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700619 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800620}
621
Jack Wue561d032022-11-24 12:19:41 +0800622status_t BatteryMonitor::setChargingPolicy(int value) {
623 status_t ret = NAME_NOT_FOUND;
624 bool result;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000625 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800626 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
627 if (!result) {
628 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
629 ret = BAD_VALUE;
630 } else {
631 ret = OK;
632 }
633 }
634 return ret;
635}
636
637int BatteryMonitor::getChargingPolicy() {
638 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000639 if (!mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800640 std::string buf;
641 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
642 result = getBatteryChargingPolicy(buf.c_str());
643 }
644 return static_cast<int>(result);
645}
646
647int BatteryMonitor::getBatteryHealthData(int id) {
648 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000649 if (!mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800650 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
651 }
652 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000653 if (!mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +0800654 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
655 }
AleX Pelosiff708922023-02-17 01:39:21 +0000656 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000657 if (!mHealthdConfig->batteryStateOfHealthPath.empty())
AleX Pelosiff708922023-02-17 01:39:21 +0000658 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
659 }
David Andersond5ed26a2023-12-08 15:12:24 -0800660 if (id == BATTERY_PROP_PART_STATUS) {
661 return static_cast<int>(BatteryPartStatus::UNSUPPORTED);
662 }
Jack Wue561d032022-11-24 12:19:41 +0800663 return 0;
664}
665
Todd Poynorc133b712013-08-14 17:39:13 -0700666status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
667 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800668 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700669
Todd Poynor8f132af2014-05-08 17:15:45 -0700670 val->valueInt64 = LONG_MIN;
671
Todd Poynorc133b712013-08-14 17:39:13 -0700672 switch(id) {
673 case BATTERY_PROP_CHARGE_COUNTER:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000674 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700675 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700676 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700677 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700678 } else {
679 ret = NAME_NOT_FOUND;
680 }
681 break;
682
683 case BATTERY_PROP_CURRENT_NOW:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000684 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700685 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700686 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700687 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700688 } else {
689 ret = NAME_NOT_FOUND;
690 }
691 break;
692
Todd Poynorbc102112013-08-27 18:11:49 -0700693 case BATTERY_PROP_CURRENT_AVG:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000694 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700695 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700696 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700697 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700698 } else {
699 ret = NAME_NOT_FOUND;
700 }
701 break;
702
Paul Lawrence347c8de2014-03-19 15:04:40 -0700703 case BATTERY_PROP_CAPACITY:
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000704 if (!mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700705 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700706 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700707 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700708 } else {
709 ret = NAME_NOT_FOUND;
710 }
711 break;
712
Todd Poynor8f132af2014-05-08 17:15:45 -0700713 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700714 if (mHealthdConfig->energyCounter) {
715 ret = mHealthdConfig->energyCounter(&val->valueInt64);
716 } else {
717 ret = NAME_NOT_FOUND;
718 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700719 break;
720
Jin Qian72adf112017-02-02 17:31:13 -0800721 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800722 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700723 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800724 break;
725
Jack Wue561d032022-11-24 12:19:41 +0800726 case BATTERY_PROP_CHARGING_POLICY:
727 val->valueInt64 = getChargingPolicy();
728 ret = OK;
729 break;
730
731 case BATTERY_PROP_MANUFACTURING_DATE:
732 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
733 ret = OK;
734 break;
735
736 case BATTERY_PROP_FIRST_USAGE_DATE:
737 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
738 ret = OK;
739 break;
740
AleX Pelosiff708922023-02-17 01:39:21 +0000741 case BATTERY_PROP_STATE_OF_HEALTH:
742 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
743 ret = OK;
744 break;
745
David Andersond5ed26a2023-12-08 15:12:24 -0800746 case BATTERY_PROP_PART_STATUS:
747 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_PART_STATUS);
748 ret = OK;
749 break;
750
Todd Poynorc133b712013-08-14 17:39:13 -0700751 default:
752 break;
753 }
754
Todd Poynorc133b712013-08-14 17:39:13 -0700755 return ret;
756}
757
David Andersond5ed26a2023-12-08 15:12:24 -0800758status_t BatteryMonitor::getSerialNumber(std::optional<std::string>* out) {
759 *out = std::nullopt;
760 return OK;
761}
762
Todd Poynor020369d2013-09-18 20:09:33 -0700763void BatteryMonitor::dumpState(int fd) {
764 int v;
765 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800766 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700767
Jack Wu06b90412021-12-15 20:40:21 +0800768 snprintf(vs, sizeof(vs),
769 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
770 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800771 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
772 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700773 write(fd, vs, strlen(vs));
774 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
775 props.batteryStatus, props.batteryHealth, props.batteryPresent);
776 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800777 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
778 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700779 write(fd, vs, strlen(vs));
780
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000781 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700782 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
783 snprintf(vs, sizeof(vs), "current now: %d\n", v);
784 write(fd, vs, strlen(vs));
785 }
786
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000787 if (!mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700788 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
789 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
790 write(fd, vs, strlen(vs));
791 }
792
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000793 if (!mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynor020369d2013-09-18 20:09:33 -0700794 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
795 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
796 write(fd, vs, strlen(vs));
797 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700798
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000799 if (!mHealthdConfig->batteryCurrentNowPath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800800 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700801 write(fd, vs, strlen(vs));
802 }
803
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000804 if (!mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700805 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
806 write(fd, vs, strlen(vs));
807 }
808
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000809 if (!mHealthdConfig->batteryFullChargePath.empty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800810 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700811 write(fd, vs, strlen(vs));
812 }
Todd Poynor020369d2013-09-18 20:09:33 -0700813}
814
Todd Poynorc7464c92013-09-10 12:40:00 -0700815void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700816 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700817 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700818
Todd Poynorf5d30122013-08-12 17:03:35 -0700819 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800820 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700821 if (dir == NULL) {
822 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
823 } else {
824 struct dirent* entry;
825
James Hawkins588a2ca2016-02-18 14:52:46 -0800826 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700827 const char* name = entry->d_name;
828
829 if (!strcmp(name, ".") || !strcmp(name, ".."))
830 continue;
831
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000832 std::vector<String8>::iterator itIgnoreName =
833 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
834 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800835 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
836 continue;
837
Todd Poynor752faf22013-06-12 13:25:59 -0700838 // Look for "type" file in each subdirectory
839 path.clear();
840 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
841 switch(readPowerSupplyType(path)) {
842 case ANDROID_POWER_SUPPLY_TYPE_AC:
843 case ANDROID_POWER_SUPPLY_TYPE_USB:
844 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800845 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700846 path.clear();
847 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +0000848 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Todd Poynor752faf22013-06-12 13:25:59 -0700849 break;
850
851 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900852 // Some devices expose the battery status of sub-component like
853 // stylus. Such a device-scoped battery info needs to be skipped
854 // in BatteryMonitor, which is intended to report the status of
855 // the battery supplying the power to the whole system.
856 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700857 mBatteryDevicePresent = true;
858
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000859 if (mHealthdConfig->batteryStatusPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700860 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700861 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
862 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000863 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700864 }
865
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000866 if (mHealthdConfig->batteryHealthPath.empty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700867 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700868 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
869 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000870 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700871 }
872
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000873 if (mHealthdConfig->batteryPresentPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700874 path.clear();
875 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
876 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000877 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryPresentPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700878 }
879
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000880 if (mHealthdConfig->batteryCapacityPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700881 path.clear();
882 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
883 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000884 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->batteryCapacityPath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700885 }
886
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000887 if (mHealthdConfig->batteryVoltagePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700888 path.clear();
889 path.appendFormat("%s/%s/voltage_now",
890 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000891 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700892 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700893 }
894 }
895
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000896 if (mHealthdConfig->batteryFullChargePath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700897 path.clear();
898 path.appendFormat("%s/%s/charge_full",
899 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000900 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700901 mHealthdConfig->batteryFullChargePath = path;
902 }
903
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000904 if (mHealthdConfig->batteryCurrentNowPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700905 path.clear();
906 path.appendFormat("%s/%s/current_now",
907 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000908 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700909 mHealthdConfig->batteryCurrentNowPath = path;
910 }
911
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000912 if (mHealthdConfig->batteryCycleCountPath.empty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700913 path.clear();
914 path.appendFormat("%s/%s/cycle_count",
915 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000916 if (access(path.c_str(), R_OK) == 0)
Ruchi Kandoicc338802015-08-24 13:01:16 -0700917 mHealthdConfig->batteryCycleCountPath = path;
918 }
919
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000920 if (mHealthdConfig->batteryCapacityLevelPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800921 path.clear();
922 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000923 if (access(path.c_str(), R_OK) == 0) {
924 mHealthdConfig->batteryCapacityLevelPath = path;
925 }
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800926 }
927
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000928 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty()) {
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800929 path.clear();
930 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000931 if (access(path.c_str(), R_OK) == 0)
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800932 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
933 }
934
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000935 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty()) {
Stephane Lee1c108ed2020-02-10 18:23:57 -0800936 path.clear();
937 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000938 if (access(path.c_str(), R_OK) == 0)
Stephane Lee1c108ed2020-02-10 18:23:57 -0800939 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
940 }
941
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000942 if (mHealthdConfig->batteryCurrentAvgPath.empty()) {
Todd Poynorbc102112013-08-27 18:11:49 -0700943 path.clear();
944 path.appendFormat("%s/%s/current_avg",
945 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000946 if (access(path.c_str(), R_OK) == 0)
Todd Poynorbc102112013-08-27 18:11:49 -0700947 mHealthdConfig->batteryCurrentAvgPath = path;
948 }
949
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000950 if (mHealthdConfig->batteryChargeCounterPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700951 path.clear();
952 path.appendFormat("%s/%s/charge_counter",
953 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000954 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700955 mHealthdConfig->batteryChargeCounterPath = path;
956 }
957
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000958 if (mHealthdConfig->batteryTemperaturePath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700959 path.clear();
960 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
961 name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000962 if (access(path.c_str(), R_OK) == 0) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700963 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700964 }
965 }
966
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000967 if (mHealthdConfig->batteryTechnologyPath.empty()) {
Todd Poynorf5d30122013-08-12 17:03:35 -0700968 path.clear();
969 path.appendFormat("%s/%s/technology",
970 POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000971 if (access(path.c_str(), R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700972 mHealthdConfig->batteryTechnologyPath = path;
973 }
974
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000975 if (mHealthdConfig->batteryStateOfHealthPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800976 path.clear();
977 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000978 if (access(path.c_str(), R_OK) == 0) {
Jack Wue561d032022-11-24 12:19:41 +0800979 mHealthdConfig->batteryStateOfHealthPath = path;
980 } else {
981 path.clear();
982 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000983 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +0800984 mHealthdConfig->batteryStateOfHealthPath = path;
985 }
986 }
987
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000988 if (mHealthdConfig->batteryHealthStatusPath.empty()) {
Jack Wub57f68a2023-02-04 19:56:06 +0800989 path.clear();
990 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000991 if (access(path.c_str(), R_OK) == 0) {
992 mHealthdConfig->batteryHealthStatusPath = path;
993 }
Jack Wub57f68a2023-02-04 19:56:06 +0800994 }
995
Tomasz Wasilczykf5971292023-08-14 18:18:26 +0000996 if (mHealthdConfig->batteryManufacturingDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +0800997 path.clear();
998 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +0000999 if (access(path.c_str(), R_OK) == 0)
Jack Wue561d032022-11-24 12:19:41 +08001000 mHealthdConfig->batteryManufacturingDatePath = path;
1001 }
1002
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001003 if (mHealthdConfig->batteryFirstUsageDatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +08001004 path.clear();
1005 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +00001006 if (access(path.c_str(), R_OK) == 0) {
1007 mHealthdConfig->batteryFirstUsageDatePath = path;
1008 }
Jack Wue561d032022-11-24 12:19:41 +08001009 }
1010
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001011 if (mHealthdConfig->chargingStatePath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +08001012 path.clear();
1013 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +00001014 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingStatePath = path;
Jack Wue561d032022-11-24 12:19:41 +08001015 }
1016
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001017 if (mHealthdConfig->chargingPolicyPath.empty()) {
Jack Wue561d032022-11-24 12:19:41 +08001018 path.clear();
1019 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk2b1a0592023-09-12 15:26:15 +00001020 if (access(path.c_str(), R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
Jack Wue561d032022-11-24 12:19:41 +08001021 }
1022
Todd Poynor752faf22013-06-12 13:25:59 -07001023 break;
1024
1025 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
1026 break;
1027 }
Jack Wu06b90412021-12-15 20:40:21 +08001028
1029 // Look for "is_dock" file
1030 path.clear();
1031 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +00001032 if (access(path.c_str(), R_OK) == 0) {
Jack Wu06b90412021-12-15 20:40:21 +08001033 path.clear();
1034 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
Tomasz Wasilczyk18b74612023-08-10 23:29:50 +00001035 if (access(path.c_str(), R_OK) == 0) mChargerNames.add(String8(name));
Jack Wu06b90412021-12-15 20:40:21 +08001036 }
Todd Poynor752faf22013-06-12 13:25:59 -07001037 }
Todd Poynor752faf22013-06-12 13:25:59 -07001038 }
1039
Ian Pedowitz585ab652015-10-12 19:01:00 -07001040 // Typically the case for devices which do not have a battery and
1041 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001042 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -07001043 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001044 hc->periodic_chores_interval_fast = -1;
1045 hc->periodic_chores_interval_slow = -1;
1046 } else {
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001047 if (mHealthdConfig->batteryStatusPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001048 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001049 if (mHealthdConfig->batteryHealthPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001050 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001051 if (mHealthdConfig->batteryPresentPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001052 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001053 if (mHealthdConfig->batteryCapacityPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001054 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001055 if (mHealthdConfig->batteryVoltagePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001056 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001057 if (mHealthdConfig->batteryTemperaturePath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001058 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001059 if (mHealthdConfig->batteryTechnologyPath.empty())
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001060 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001061 if (mHealthdConfig->batteryCurrentNowPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001062 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001063 if (mHealthdConfig->batteryFullChargePath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001064 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001065 if (mHealthdConfig->batteryCycleCountPath.empty())
Ruchi Kandoicc338802015-08-24 13:01:16 -07001066 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001067 if (mHealthdConfig->batteryCapacityLevelPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001068 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001069 if (mHealthdConfig->batteryChargeTimeToFullNowPath.empty())
Stephane Lee86f9f6a2019-12-19 15:09:41 -08001070 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001071 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.empty())
Stephane Lee1c108ed2020-02-10 18:23:57 -08001072 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001073 if (mHealthdConfig->batteryStateOfHealthPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001074 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001075 if (mHealthdConfig->batteryHealthStatusPath.empty())
Jack Wub57f68a2023-02-04 19:56:06 +08001076 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001077 if (mHealthdConfig->batteryManufacturingDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001078 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001079 if (mHealthdConfig->batteryFirstUsageDatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001080 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001081 if (mHealthdConfig->chargingStatePath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001082 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
Tomasz Wasilczykf5971292023-08-14 18:18:26 +00001083 if (mHealthdConfig->chargingPolicyPath.empty())
Jack Wue561d032022-11-24 12:19:41 +08001084 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001085 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001086
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001087 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1088 && strtol(pval, NULL, 10) != 0) {
1089 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1090 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1091 }
Todd Poynor752faf22013-06-12 13:25:59 -07001092}
1093
1094}; // namespace android