blob: 66e1e63ef62e5f9b4853c6c95ab99d629cf842ac [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "healthd"
18
Yabin Cuie98e1772016-02-17 12:21:34 -080019#include <healthd/healthd.h>
20#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Thierry Strudelf73de6f2019-01-11 17:09:20 -080029
30#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Yifan Hong1d4368b2019-10-07 11:18:04 -070032#include <optional>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033
Yifan Hongb99d15c2022-03-01 12:12:34 -080034#include <aidl/android/hardware/health/HealthInfo.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070035#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070036#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070037#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070038#include <android/hardware/health/2.1/types.h>
Yifan Hongb99d15c2022-03-01 12:12:34 -080039#include <android/hardware/health/translate-ndk.h>
Todd Poynor752faf22013-06-12 13:25:59 -070040#include <batteryservice/BatteryService.h>
41#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070042#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070043#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070044#include <utils/String8.h>
45#include <utils/Vector.h>
46
47#define POWER_SUPPLY_SUBSYSTEM "power_supply"
48#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070049#define FAKE_BATTERY_CAPACITY 42
50#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080051#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070052#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070053
Yifan Hong1d4368b2019-10-07 11:18:04 -070054using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
55using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
56using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
Yifan Hongb99d15c2022-03-01 12:12:34 -080057using aidl::android::hardware::health::BatteryCapacityLevel;
Jack Wue561d032022-11-24 12:19:41 +080058using aidl::android::hardware::health::BatteryChargingPolicy;
59using aidl::android::hardware::health::BatteryChargingState;
Yifan Hongb99d15c2022-03-01 12:12:34 -080060using aidl::android::hardware::health::BatteryHealth;
Jack Wue561d032022-11-24 12:19:41 +080061using aidl::android::hardware::health::BatteryHealthData;
Yifan Hongb99d15c2022-03-01 12:12:34 -080062using aidl::android::hardware::health::BatteryStatus;
63using aidl::android::hardware::health::HealthInfo;
64
65namespace {
66
67// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
68// Skips storageInfo and diskStats.
69void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
70 ::android::hardware::health::V1_0::HealthInfo* out) {
71 out->chargerAcOnline = in.chargerAcOnline;
72 out->chargerUsbOnline = in.chargerUsbOnline;
73 out->chargerWirelessOnline = in.chargerWirelessOnline;
74 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
75 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
76 out->batteryStatus =
77 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
78 out->batteryHealth =
79 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
80 out->batteryPresent = in.batteryPresent;
81 out->batteryLevel = in.batteryLevel;
82 out->batteryVoltage = in.batteryVoltageMillivolts;
83 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
84 out->batteryCurrent = in.batteryCurrentMicroamps;
85 out->batteryCycleCount = in.batteryCycleCount;
86 out->batteryFullCharge = in.batteryFullChargeUah;
87 out->batteryChargeCounter = in.batteryChargeCounterUah;
88 out->batteryTechnology = in.batteryTechnology;
89}
90
91void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
92 ::android::hardware::health::V2_0::HealthInfo* out) {
93 translateToHidl(in, &out->legacy);
94 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
95 // Skip storageInfo and diskStats
96}
97
98void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
99 ::android::hardware::health::V2_1::HealthInfo* out) {
100 translateToHidl(in, &out->legacy);
101 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
102 in.batteryCapacityLevel);
103 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
104 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
105}
106
107} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700108
Todd Poynor752faf22013-06-12 13:25:59 -0700109namespace android {
110
Yifan Hong1d4368b2019-10-07 11:18:04 -0700111template <typename T>
112struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700113 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700114 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700115};
116
Yifan Hong1d4368b2019-10-07 11:18:04 -0700117template <typename T>
118static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700119 for (int i = 0; map[i].s; i++)
120 if (!strcmp(str, map[i].s))
121 return map[i].val;
122
Yifan Hong1d4368b2019-10-07 11:18:04 -0700123 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800124}
125
Yifan Hongb99d15c2022-03-01 12:12:34 -0800126static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000127 *health_info = {
128 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
129 .batteryChargeTimeToFullNowSeconds =
130 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
131 .batteryStatus = BatteryStatus::UNKNOWN,
132 .batteryHealth = BatteryHealth::UNKNOWN,
133 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800134}
135
Todd Poynore030a102018-01-19 14:03:59 -0800136BatteryMonitor::BatteryMonitor()
137 : mHealthdConfig(nullptr),
138 mBatteryDevicePresent(false),
139 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700140 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800141 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800142 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800143 initHealthInfo(mHealthInfo.get());
144}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145
146BatteryMonitor::~BatteryMonitor() {}
147
Yifan Hongb99d15c2022-03-01 12:12:34 -0800148HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
149 HealthInfo_1_0 health_info_1_0;
150 translateToHidl(*mHealthInfo, &health_info_1_0);
151 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800152}
153
Yifan Hongb99d15c2022-03-01 12:12:34 -0800154HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
155 HealthInfo_2_0 health_info_2_0;
156 translateToHidl(*mHealthInfo, &health_info_2_0);
157 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800158}
159
Yifan Hongb99d15c2022-03-01 12:12:34 -0800160HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
161 HealthInfo_2_1 health_info_2_1;
162 translateToHidl(*mHealthInfo, &health_info_2_1);
163 return health_info_2_1;
164}
165
166const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700167 return *mHealthInfo;
168}
169
170BatteryStatus getBatteryStatus(const char* status) {
171 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
172 {"Unknown", BatteryStatus::UNKNOWN},
173 {"Charging", BatteryStatus::CHARGING},
174 {"Discharging", BatteryStatus::DISCHARGING},
175 {"Not charging", BatteryStatus::NOT_CHARGING},
176 {"Full", BatteryStatus::FULL},
177 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700178 };
179
Yifan Hong1d4368b2019-10-07 11:18:04 -0700180 auto ret = mapSysfsString(status, batteryStatusMap);
181 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700182 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700183 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 }
185
Yifan Hong1d4368b2019-10-07 11:18:04 -0700186 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700187}
188
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800189BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
190 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
191 {"Unknown", BatteryCapacityLevel::UNKNOWN},
192 {"Critical", BatteryCapacityLevel::CRITICAL},
193 {"Low", BatteryCapacityLevel::LOW},
194 {"Normal", BatteryCapacityLevel::NORMAL},
195 {"High", BatteryCapacityLevel::HIGH},
196 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800197 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800198 };
199
200 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
201 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800202 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
203 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800204 }
205
206 return *ret;
207}
208
Yifan Hong1d4368b2019-10-07 11:18:04 -0700209BatteryHealth getBatteryHealth(const char* status) {
210 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
211 {"Unknown", BatteryHealth::UNKNOWN},
212 {"Good", BatteryHealth::GOOD},
213 {"Overheat", BatteryHealth::OVERHEAT},
214 {"Dead", BatteryHealth::DEAD},
215 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
216 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
217 {"Cold", BatteryHealth::COLD},
218 // battery health values from JEITA spec
219 {"Warm", BatteryHealth::GOOD},
220 {"Cool", BatteryHealth::GOOD},
221 {"Hot", BatteryHealth::OVERHEAT},
222 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700223 };
224
Yifan Hong1d4368b2019-10-07 11:18:04 -0700225 auto ret = mapSysfsString(status, batteryHealthMap);
226 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700227 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700229 }
230
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700232}
233
Jack Wub57f68a2023-02-04 19:56:06 +0800234BatteryHealth getBatteryHealthStatus(int status) {
235 BatteryHealth value;
236
237 if (status == BatteryMonitor::BH_NOMINAL)
238 value = BatteryHealth::GOOD;
239 else if (status == BatteryMonitor::BH_MARGINAL)
240 value = BatteryHealth::FAIR;
241 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
242 value = BatteryHealth::DEAD;
243 else if (status == BatteryMonitor::BH_FAILED)
244 value = BatteryHealth::UNSPECIFIED_FAILURE;
245 else
246 value = BatteryHealth::UNKNOWN;
247
248 return value;
249}
250
Jack Wue561d032022-11-24 12:19:41 +0800251BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
252 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
253 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
254 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
255 {NULL, BatteryChargingPolicy::DEFAULT},
256 };
257
258 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
259 if (!ret) {
260 *ret = BatteryChargingPolicy::DEFAULT;
261 }
262
263 return *ret;
264}
265
266BatteryChargingState getBatteryChargingState(const char* chargingState) {
267 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
268 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
269 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
270 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
271 {NULL, BatteryChargingState::NORMAL},
272 };
273
274 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
275 if (!ret) {
276 *ret = BatteryChargingState::NORMAL;
277 }
278
279 return *ret;
280}
281
Bart Van Assche095c9442022-03-02 17:36:34 +0000282static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000283 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800284 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700285 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700286 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700287 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700288}
289
Jack Wue561d032022-11-24 12:19:41 +0800290static bool writeToFile(const String8& path, int32_t in_value) {
291 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
292}
293
Bart Van Assche095c9442022-03-02 17:36:34 +0000294static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700295 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000296 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
297 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
298 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
299 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
300 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
301 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
302 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
303 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
304 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
305 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
307 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
308 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
309 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700310 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700311 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700312 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700313
Bart Van Assche095c9442022-03-02 17:36:34 +0000314 if (readFromFile(path, &buf) <= 0) {
315 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
316 }
Todd Poynor752faf22013-06-12 13:25:59 -0700317
Yifan Hong1d4368b2019-10-07 11:18:04 -0700318 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000319 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700320 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000321 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100322 }
Todd Poynor752faf22013-06-12 13:25:59 -0700323
Yifan Hong1d4368b2019-10-07 11:18:04 -0700324 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700325}
326
Bart Van Assche095c9442022-03-02 17:36:34 +0000327static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700328 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700329 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700330
331 if (readFromFile(path, &buf) > 0)
332 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700333 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700334
335 return value;
336}
337
Bart Van Assche095c9442022-03-02 17:36:34 +0000338static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700339 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700340 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700341
342 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700343 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700344
Todd Poynor752faf22013-06-12 13:25:59 -0700345 return value;
346}
347
Bart Van Assche095c9442022-03-02 17:36:34 +0000348static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900349 constexpr char kScopeDevice[] = "Device";
350
351 String8 path;
352 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
353 std::string scope;
354 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
355}
356
Yifan Hong1353e702019-10-07 10:41:30 -0700357void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800358 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700359
Todd Poynorf5d30122013-08-12 17:03:35 -0700360 if (!mHealthdConfig->batteryPresentPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800361 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700362 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800363 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700364
Yifan Hongb99d15c2022-03-01 12:12:34 -0800365 mHealthInfo->batteryLevel = mBatteryFixedCapacity
366 ? mBatteryFixedCapacity
367 : getIntField(mHealthdConfig->batteryCapacityPath);
368 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700369
Ruchi Kandoicc338802015-08-24 13:01:16 -0700370 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800371 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700372
373 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800374 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700375
376 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800377 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700378
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700379 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800380 mHealthInfo->batteryChargeCounterUah =
381 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700382
Yifan Hong35cb0832019-10-07 13:58:29 -0700383 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800384 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700385 getIntField(mHealthdConfig->batteryCurrentAvgPath);
386
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800387 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
388 mHealthInfo->batteryChargeTimeToFullNowSeconds =
389 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
390
Stephane Lee1c108ed2020-02-10 18:23:57 -0800391 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
392 mHealthInfo->batteryFullChargeDesignCapacityUah =
393 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700394
Jack Wub57f68a2023-02-04 19:56:06 +0800395 if (!mHealthdConfig->batteryHealthStatusPath.isEmpty())
396 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
397
AleX Pelosiff708922023-02-17 01:39:21 +0000398 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
399 mHealthInfo->batteryHealthData->batteryStateOfHealth =
400 getIntField(mHealthdConfig->batteryStateOfHealthPath);
401
Jack Wue561d032022-11-24 12:19:41 +0800402 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
403 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
404 getIntField(mHealthdConfig->batteryManufacturingDatePath);
405
406 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
407 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
408 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
409
Yifan Hongb99d15c2022-03-01 12:12:34 -0800410 mHealthInfo->batteryTemperatureTenthsCelsius =
411 mBatteryFixedTemperature ? mBatteryFixedTemperature
412 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700413
Michael Scott3217c5c2016-06-05 11:20:13 -0700414 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700415
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800416 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
417 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
418
Michael Scott3217c5c2016-06-05 11:20:13 -0700419 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800420 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700421
Jack Wub57f68a2023-02-04 19:56:06 +0800422 // Backward compatible with android.hardware.health V1
423 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
424 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
425 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
426 } else {
427 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
428 }
Todd Poynor752faf22013-06-12 13:25:59 -0700429
Michael Scott3217c5c2016-06-05 11:20:13 -0700430 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800431 mHealthInfo->batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700432
Jack Wue561d032022-11-24 12:19:41 +0800433 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
434 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
435
436 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
437 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
438
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700439 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700440
ShevT9d98a6a2018-07-26 11:47:47 +0300441 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700442 String8 path;
443 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
444 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700445 if (getIntField(path)) {
446 path.clear();
447 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
448 mChargerNames[i].string());
449 switch(readPowerSupplyType(path)) {
450 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800451 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700452 break;
453 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800454 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700455 break;
456 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800457 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700458 break;
Jack Wu06b90412021-12-15 20:40:21 +0800459 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800460 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800461 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700462 default:
Jack Wu06b90412021-12-15 20:40:21 +0800463 path.clear();
464 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
465 mChargerNames[i].string());
Yifan Hongb99d15c2022-03-01 12:12:34 -0800466 if (access(path.string(), R_OK) == 0)
467 mHealthInfo->chargerDockOnline = true;
468 else
Jack Wu06b90412021-12-15 20:40:21 +0800469 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
470 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700471 }
472 path.clear();
473 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
474 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700475 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700476 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
477
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700478 path.clear();
479 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
480 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700481
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700482 int ChargingVoltage =
483 (access(path.string(), R_OK) == 0) ? getIntField(path) :
484 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700485
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700486 double power = ((double)ChargingCurrent / MILLION) *
487 ((double)ChargingVoltage / MILLION);
488 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800489 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
490 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700491 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700492 }
493 }
494 }
Yifan Hong1353e702019-10-07 10:41:30 -0700495}
Todd Poynor752faf22013-06-12 13:25:59 -0700496
Bart Van Assche095c9442022-03-02 17:36:34 +0000497static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700498 char dmesgline[256];
499 size_t len;
500 if (props.batteryPresent) {
501 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800502 props.batteryLevel, props.batteryVoltageMillivolts,
503 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
504 abs(props.batteryTemperatureTenthsCelsius / 10),
505 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
506 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700507
Yifan Hong1353e702019-10-07 10:41:30 -0700508 len = strlen(dmesgline);
Yifan Hong605e7d22021-02-08 15:14:48 -0800509 if (!healthd_config.batteryCurrentNowPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700510 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800511 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700512 }
513
Yifan Hong605e7d22021-02-08 15:14:48 -0800514 if (!healthd_config.batteryFullChargePath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700515 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800516 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700517 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700518
Yifan Hong605e7d22021-02-08 15:14:48 -0800519 if (!healthd_config.batteryCycleCountPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700520 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
521 props.batteryCycleCount);
522 }
523 } else {
524 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700525 }
526
Yifan Hongb99d15c2022-03-01 12:12:34 -0800527 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700528 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800529 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700530
531 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
532}
533
Bart Van Assche095c9442022-03-02 17:36:34 +0000534void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
535 const struct healthd_config& healthd_config) {
536 HealthInfo aidl_health_info;
537 (void)android::h2a::translate(health_info, &aidl_health_info);
538 doLogValues(aidl_health_info, healthd_config);
539}
540
541void BatteryMonitor::logValues(void) {
542 doLogValues(*mHealthInfo, *mHealthdConfig);
543}
544
Yifan Hong1353e702019-10-07 10:41:30 -0700545bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800546 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800547 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800548 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700549}
550
Yabin Cuiaedf6032016-02-19 18:03:23 -0800551int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700552 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800553 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700554 std::string buf;
555 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
556 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800557 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700558 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800559}
560
Jack Wue561d032022-11-24 12:19:41 +0800561status_t BatteryMonitor::setChargingPolicy(int value) {
562 status_t ret = NAME_NOT_FOUND;
563 bool result;
564 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
565 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
566 if (!result) {
567 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
568 ret = BAD_VALUE;
569 } else {
570 ret = OK;
571 }
572 }
573 return ret;
574}
575
576int BatteryMonitor::getChargingPolicy() {
577 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
578 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
579 std::string buf;
580 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
581 result = getBatteryChargingPolicy(buf.c_str());
582 }
583 return static_cast<int>(result);
584}
585
586int BatteryMonitor::getBatteryHealthData(int id) {
587 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
588 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
589 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
590 }
591 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
592 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
593 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
594 }
AleX Pelosiff708922023-02-17 01:39:21 +0000595 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
596 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
597 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
598 }
Jack Wue561d032022-11-24 12:19:41 +0800599 return 0;
600}
601
Todd Poynorc133b712013-08-14 17:39:13 -0700602status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
603 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800604 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700605
Todd Poynor8f132af2014-05-08 17:15:45 -0700606 val->valueInt64 = LONG_MIN;
607
Todd Poynorc133b712013-08-14 17:39:13 -0700608 switch(id) {
609 case BATTERY_PROP_CHARGE_COUNTER:
610 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700611 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700612 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700613 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700614 } else {
615 ret = NAME_NOT_FOUND;
616 }
617 break;
618
619 case BATTERY_PROP_CURRENT_NOW:
620 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700621 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700622 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700623 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700624 } else {
625 ret = NAME_NOT_FOUND;
626 }
627 break;
628
Todd Poynorbc102112013-08-27 18:11:49 -0700629 case BATTERY_PROP_CURRENT_AVG:
630 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700631 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700632 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700633 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700634 } else {
635 ret = NAME_NOT_FOUND;
636 }
637 break;
638
Paul Lawrence347c8de2014-03-19 15:04:40 -0700639 case BATTERY_PROP_CAPACITY:
640 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700641 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700642 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700643 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700644 } else {
645 ret = NAME_NOT_FOUND;
646 }
647 break;
648
Todd Poynor8f132af2014-05-08 17:15:45 -0700649 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700650 if (mHealthdConfig->energyCounter) {
651 ret = mHealthdConfig->energyCounter(&val->valueInt64);
652 } else {
653 ret = NAME_NOT_FOUND;
654 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700655 break;
656
Jin Qian72adf112017-02-02 17:31:13 -0800657 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800658 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700659 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800660 break;
661
Jack Wue561d032022-11-24 12:19:41 +0800662 case BATTERY_PROP_CHARGING_POLICY:
663 val->valueInt64 = getChargingPolicy();
664 ret = OK;
665 break;
666
667 case BATTERY_PROP_MANUFACTURING_DATE:
668 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
669 ret = OK;
670 break;
671
672 case BATTERY_PROP_FIRST_USAGE_DATE:
673 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
674 ret = OK;
675 break;
676
AleX Pelosiff708922023-02-17 01:39:21 +0000677 case BATTERY_PROP_STATE_OF_HEALTH:
678 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
679 ret = OK;
680 break;
681
Todd Poynorc133b712013-08-14 17:39:13 -0700682 default:
683 break;
684 }
685
Todd Poynorc133b712013-08-14 17:39:13 -0700686 return ret;
687}
688
Todd Poynor020369d2013-09-18 20:09:33 -0700689void BatteryMonitor::dumpState(int fd) {
690 int v;
691 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800692 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700693
Jack Wu06b90412021-12-15 20:40:21 +0800694 snprintf(vs, sizeof(vs),
695 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
696 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800697 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
698 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700699 write(fd, vs, strlen(vs));
700 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
701 props.batteryStatus, props.batteryHealth, props.batteryPresent);
702 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800703 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
704 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700705 write(fd, vs, strlen(vs));
706
707 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
708 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
709 snprintf(vs, sizeof(vs), "current now: %d\n", v);
710 write(fd, vs, strlen(vs));
711 }
712
713 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
714 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
715 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
716 write(fd, vs, strlen(vs));
717 }
718
719 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
720 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
721 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
722 write(fd, vs, strlen(vs));
723 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700724
725 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800726 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700727 write(fd, vs, strlen(vs));
728 }
729
730 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
731 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
732 write(fd, vs, strlen(vs));
733 }
734
735 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800736 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700737 write(fd, vs, strlen(vs));
738 }
Todd Poynor020369d2013-09-18 20:09:33 -0700739}
740
Todd Poynorc7464c92013-09-10 12:40:00 -0700741void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700742 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700743 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700744
Todd Poynorf5d30122013-08-12 17:03:35 -0700745 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800746 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700747 if (dir == NULL) {
748 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
749 } else {
750 struct dirent* entry;
751
James Hawkins588a2ca2016-02-18 14:52:46 -0800752 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700753 const char* name = entry->d_name;
754
755 if (!strcmp(name, ".") || !strcmp(name, ".."))
756 continue;
757
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000758 std::vector<String8>::iterator itIgnoreName =
759 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
760 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800761 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
762 continue;
763
Todd Poynor752faf22013-06-12 13:25:59 -0700764 // Look for "type" file in each subdirectory
765 path.clear();
766 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
767 switch(readPowerSupplyType(path)) {
768 case ANDROID_POWER_SUPPLY_TYPE_AC:
769 case ANDROID_POWER_SUPPLY_TYPE_USB:
770 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800771 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700772 path.clear();
773 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
774 if (access(path.string(), R_OK) == 0)
775 mChargerNames.add(String8(name));
776 break;
777
778 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900779 // Some devices expose the battery status of sub-component like
780 // stylus. Such a device-scoped battery info needs to be skipped
781 // in BatteryMonitor, which is intended to report the status of
782 // the battery supplying the power to the whole system.
783 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700784 mBatteryDevicePresent = true;
785
Todd Poynorf5d30122013-08-12 17:03:35 -0700786 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700787 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700788 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
789 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700790 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700791 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700792 }
793
Todd Poynorf5d30122013-08-12 17:03:35 -0700794 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700795 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700796 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
797 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700798 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700799 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700800 }
801
Todd Poynorf5d30122013-08-12 17:03:35 -0700802 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
803 path.clear();
804 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
805 name);
806 if (access(path, R_OK) == 0)
807 mHealthdConfig->batteryPresentPath = path;
808 }
809
810 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
811 path.clear();
812 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
813 name);
814 if (access(path, R_OK) == 0)
815 mHealthdConfig->batteryCapacityPath = path;
816 }
817
818 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
819 path.clear();
820 path.appendFormat("%s/%s/voltage_now",
821 POWER_SUPPLY_SYSFS_PATH, name);
822 if (access(path, R_OK) == 0) {
823 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700824 }
825 }
826
Ruchi Kandoicc338802015-08-24 13:01:16 -0700827 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
828 path.clear();
829 path.appendFormat("%s/%s/charge_full",
830 POWER_SUPPLY_SYSFS_PATH, name);
831 if (access(path, R_OK) == 0)
832 mHealthdConfig->batteryFullChargePath = path;
833 }
834
Todd Poynorf5d30122013-08-12 17:03:35 -0700835 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
836 path.clear();
837 path.appendFormat("%s/%s/current_now",
838 POWER_SUPPLY_SYSFS_PATH, name);
839 if (access(path, R_OK) == 0)
840 mHealthdConfig->batteryCurrentNowPath = path;
841 }
842
Ruchi Kandoicc338802015-08-24 13:01:16 -0700843 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
844 path.clear();
845 path.appendFormat("%s/%s/cycle_count",
846 POWER_SUPPLY_SYSFS_PATH, name);
847 if (access(path, R_OK) == 0)
848 mHealthdConfig->batteryCycleCountPath = path;
849 }
850
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800851 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) {
852 path.clear();
853 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
854 if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path;
855 }
856
857 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) {
858 path.clear();
859 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
860 if (access(path, R_OK) == 0)
861 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
862 }
863
Stephane Lee1c108ed2020-02-10 18:23:57 -0800864 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) {
865 path.clear();
866 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
867 if (access(path, R_OK) == 0)
868 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
869 }
870
Todd Poynorbc102112013-08-27 18:11:49 -0700871 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
872 path.clear();
873 path.appendFormat("%s/%s/current_avg",
874 POWER_SUPPLY_SYSFS_PATH, name);
875 if (access(path, R_OK) == 0)
876 mHealthdConfig->batteryCurrentAvgPath = path;
877 }
878
Todd Poynorf5d30122013-08-12 17:03:35 -0700879 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
880 path.clear();
881 path.appendFormat("%s/%s/charge_counter",
882 POWER_SUPPLY_SYSFS_PATH, name);
883 if (access(path, R_OK) == 0)
884 mHealthdConfig->batteryChargeCounterPath = path;
885 }
886
887 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
888 path.clear();
889 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
890 name);
891 if (access(path, R_OK) == 0) {
892 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700893 }
894 }
895
896 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
897 path.clear();
898 path.appendFormat("%s/%s/technology",
899 POWER_SUPPLY_SYSFS_PATH, name);
900 if (access(path, R_OK) == 0)
901 mHealthdConfig->batteryTechnologyPath = path;
902 }
903
Jack Wue561d032022-11-24 12:19:41 +0800904 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty()) {
905 path.clear();
906 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
907 if (access(path, R_OK) == 0) {
908 mHealthdConfig->batteryStateOfHealthPath = path;
909 } else {
910 path.clear();
911 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
912 if (access(path, R_OK) == 0)
913 mHealthdConfig->batteryStateOfHealthPath = path;
914 }
915 }
916
Jack Wub57f68a2023-02-04 19:56:06 +0800917 if (mHealthdConfig->batteryHealthStatusPath.isEmpty()) {
918 path.clear();
919 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
920 if (access(path, R_OK) == 0) mHealthdConfig->batteryHealthStatusPath = path;
921 }
922
Jack Wue561d032022-11-24 12:19:41 +0800923 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty()) {
924 path.clear();
925 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
926 if (access(path, R_OK) == 0)
927 mHealthdConfig->batteryManufacturingDatePath = path;
928 }
929
930 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty()) {
931 path.clear();
932 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
933 if (access(path, R_OK) == 0) mHealthdConfig->batteryFirstUsageDatePath = path;
934 }
935
936 if (mHealthdConfig->chargingStatePath.isEmpty()) {
937 path.clear();
938 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
939 if (access(path, R_OK) == 0) mHealthdConfig->chargingStatePath = path;
940 }
941
942 if (mHealthdConfig->chargingPolicyPath.isEmpty()) {
943 path.clear();
944 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
945 if (access(path, R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
946 }
947
Todd Poynor752faf22013-06-12 13:25:59 -0700948 break;
949
950 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
951 break;
952 }
Jack Wu06b90412021-12-15 20:40:21 +0800953
954 // Look for "is_dock" file
955 path.clear();
956 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
957 if (access(path.string(), R_OK) == 0) {
958 path.clear();
959 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
960 if (access(path.string(), R_OK) == 0)
961 mChargerNames.add(String8(name));
962
963 }
Todd Poynor752faf22013-06-12 13:25:59 -0700964 }
Todd Poynor752faf22013-06-12 13:25:59 -0700965 }
966
Ian Pedowitz585ab652015-10-12 19:01:00 -0700967 // Typically the case for devices which do not have a battery and
968 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700969 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700970 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700971 hc->periodic_chores_interval_fast = -1;
972 hc->periodic_chores_interval_slow = -1;
973 } else {
974 if (mHealthdConfig->batteryStatusPath.isEmpty())
975 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
976 if (mHealthdConfig->batteryHealthPath.isEmpty())
977 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
978 if (mHealthdConfig->batteryPresentPath.isEmpty())
979 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
980 if (mHealthdConfig->batteryCapacityPath.isEmpty())
981 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
982 if (mHealthdConfig->batteryVoltagePath.isEmpty())
983 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
984 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
985 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
986 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
987 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700988 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700989 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
990 if (mHealthdConfig->batteryFullChargePath.isEmpty())
991 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
992 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
993 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800994 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty())
995 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
996 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
997 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Stephane Lee1c108ed2020-02-10 18:23:57 -0800998 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
999 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001000 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty())
1001 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Jack Wub57f68a2023-02-04 19:56:06 +08001002 if (mHealthdConfig->batteryHealthStatusPath.isEmpty())
1003 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001004 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty())
1005 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
1006 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
1007 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
1008 if (mHealthdConfig->chargingStatePath.isEmpty())
1009 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
1010 if (mHealthdConfig->chargingPolicyPath.isEmpty())
1011 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001012 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001013
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001014 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1015 && strtol(pval, NULL, 10) != 0) {
1016 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1017 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1018 }
Todd Poynor752faf22013-06-12 13:25:59 -07001019}
1020
1021}; // namespace android