blob: f68d65a54d70dacaecb252d22e54f614fd76ec0e [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "healthd"
18
Yabin Cuie98e1772016-02-17 12:21:34 -080019#include <healthd/healthd.h>
20#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Thierry Strudelf73de6f2019-01-11 17:09:20 -080029
30#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Yifan Hong1d4368b2019-10-07 11:18:04 -070032#include <optional>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033
Yifan Hongb99d15c2022-03-01 12:12:34 -080034#include <aidl/android/hardware/health/HealthInfo.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070035#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070036#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070037#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070038#include <android/hardware/health/2.1/types.h>
Yifan Hongb99d15c2022-03-01 12:12:34 -080039#include <android/hardware/health/translate-ndk.h>
Todd Poynor752faf22013-06-12 13:25:59 -070040#include <batteryservice/BatteryService.h>
41#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070042#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070043#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070044#include <utils/String8.h>
45#include <utils/Vector.h>
46
47#define POWER_SUPPLY_SUBSYSTEM "power_supply"
48#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070049#define FAKE_BATTERY_CAPACITY 42
50#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080051#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070052#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070053
Yifan Hong1d4368b2019-10-07 11:18:04 -070054using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
55using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
56using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
Yifan Hongb99d15c2022-03-01 12:12:34 -080057using aidl::android::hardware::health::BatteryCapacityLevel;
Jack Wue561d032022-11-24 12:19:41 +080058using aidl::android::hardware::health::BatteryChargingPolicy;
59using aidl::android::hardware::health::BatteryChargingState;
Yifan Hongb99d15c2022-03-01 12:12:34 -080060using aidl::android::hardware::health::BatteryHealth;
Jack Wue561d032022-11-24 12:19:41 +080061using aidl::android::hardware::health::BatteryHealthData;
Yifan Hongb99d15c2022-03-01 12:12:34 -080062using aidl::android::hardware::health::BatteryStatus;
63using aidl::android::hardware::health::HealthInfo;
64
65namespace {
66
67// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
68// Skips storageInfo and diskStats.
69void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
70 ::android::hardware::health::V1_0::HealthInfo* out) {
71 out->chargerAcOnline = in.chargerAcOnline;
72 out->chargerUsbOnline = in.chargerUsbOnline;
73 out->chargerWirelessOnline = in.chargerWirelessOnline;
74 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
75 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
76 out->batteryStatus =
77 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
78 out->batteryHealth =
79 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
80 out->batteryPresent = in.batteryPresent;
81 out->batteryLevel = in.batteryLevel;
82 out->batteryVoltage = in.batteryVoltageMillivolts;
83 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
84 out->batteryCurrent = in.batteryCurrentMicroamps;
85 out->batteryCycleCount = in.batteryCycleCount;
86 out->batteryFullCharge = in.batteryFullChargeUah;
87 out->batteryChargeCounter = in.batteryChargeCounterUah;
88 out->batteryTechnology = in.batteryTechnology;
89}
90
91void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
92 ::android::hardware::health::V2_0::HealthInfo* out) {
93 translateToHidl(in, &out->legacy);
94 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
95 // Skip storageInfo and diskStats
96}
97
98void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
99 ::android::hardware::health::V2_1::HealthInfo* out) {
100 translateToHidl(in, &out->legacy);
101 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
102 in.batteryCapacityLevel);
103 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
104 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
105}
106
107} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700108
Todd Poynor752faf22013-06-12 13:25:59 -0700109namespace android {
110
Yifan Hong1d4368b2019-10-07 11:18:04 -0700111template <typename T>
112struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700113 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700114 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700115};
116
Yifan Hong1d4368b2019-10-07 11:18:04 -0700117template <typename T>
118static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700119 for (int i = 0; map[i].s; i++)
120 if (!strcmp(str, map[i].s))
121 return map[i].val;
122
Yifan Hong1d4368b2019-10-07 11:18:04 -0700123 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800124}
125
Yifan Hongb99d15c2022-03-01 12:12:34 -0800126static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000127 *health_info = {
128 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
129 .batteryChargeTimeToFullNowSeconds =
130 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
131 .batteryStatus = BatteryStatus::UNKNOWN,
132 .batteryHealth = BatteryHealth::UNKNOWN,
133 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800134}
135
Todd Poynore030a102018-01-19 14:03:59 -0800136BatteryMonitor::BatteryMonitor()
137 : mHealthdConfig(nullptr),
138 mBatteryDevicePresent(false),
139 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700140 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800141 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800142 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800143 initHealthInfo(mHealthInfo.get());
144}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145
146BatteryMonitor::~BatteryMonitor() {}
147
Yifan Hongb99d15c2022-03-01 12:12:34 -0800148HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
149 HealthInfo_1_0 health_info_1_0;
150 translateToHidl(*mHealthInfo, &health_info_1_0);
151 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800152}
153
Yifan Hongb99d15c2022-03-01 12:12:34 -0800154HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
155 HealthInfo_2_0 health_info_2_0;
156 translateToHidl(*mHealthInfo, &health_info_2_0);
157 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800158}
159
Yifan Hongb99d15c2022-03-01 12:12:34 -0800160HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
161 HealthInfo_2_1 health_info_2_1;
162 translateToHidl(*mHealthInfo, &health_info_2_1);
163 return health_info_2_1;
164}
165
166const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700167 return *mHealthInfo;
168}
169
170BatteryStatus getBatteryStatus(const char* status) {
171 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
172 {"Unknown", BatteryStatus::UNKNOWN},
173 {"Charging", BatteryStatus::CHARGING},
174 {"Discharging", BatteryStatus::DISCHARGING},
175 {"Not charging", BatteryStatus::NOT_CHARGING},
176 {"Full", BatteryStatus::FULL},
177 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700178 };
179
Yifan Hong1d4368b2019-10-07 11:18:04 -0700180 auto ret = mapSysfsString(status, batteryStatusMap);
181 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700182 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700183 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 }
185
Yifan Hong1d4368b2019-10-07 11:18:04 -0700186 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700187}
188
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800189BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
190 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
191 {"Unknown", BatteryCapacityLevel::UNKNOWN},
192 {"Critical", BatteryCapacityLevel::CRITICAL},
193 {"Low", BatteryCapacityLevel::LOW},
194 {"Normal", BatteryCapacityLevel::NORMAL},
195 {"High", BatteryCapacityLevel::HIGH},
196 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800197 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800198 };
199
200 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
201 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800202 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
203 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800204 }
205
206 return *ret;
207}
208
Yifan Hong1d4368b2019-10-07 11:18:04 -0700209BatteryHealth getBatteryHealth(const char* status) {
210 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
211 {"Unknown", BatteryHealth::UNKNOWN},
212 {"Good", BatteryHealth::GOOD},
213 {"Overheat", BatteryHealth::OVERHEAT},
214 {"Dead", BatteryHealth::DEAD},
215 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
216 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
217 {"Cold", BatteryHealth::COLD},
218 // battery health values from JEITA spec
219 {"Warm", BatteryHealth::GOOD},
220 {"Cool", BatteryHealth::GOOD},
221 {"Hot", BatteryHealth::OVERHEAT},
222 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700223 };
224
Yifan Hong1d4368b2019-10-07 11:18:04 -0700225 auto ret = mapSysfsString(status, batteryHealthMap);
226 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700227 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700229 }
230
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700232}
233
Jack Wub57f68a2023-02-04 19:56:06 +0800234BatteryHealth getBatteryHealthStatus(int status) {
235 BatteryHealth value;
236
237 if (status == BatteryMonitor::BH_NOMINAL)
238 value = BatteryHealth::GOOD;
239 else if (status == BatteryMonitor::BH_MARGINAL)
240 value = BatteryHealth::FAIR;
241 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
242 value = BatteryHealth::DEAD;
243 else if (status == BatteryMonitor::BH_FAILED)
244 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800245 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
246 value = BatteryHealth::NOT_AVAILABLE;
Jack Wub57f68a2023-02-04 19:56:06 +0800247 else
248 value = BatteryHealth::UNKNOWN;
249
250 return value;
251}
252
Jack Wue561d032022-11-24 12:19:41 +0800253BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
254 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
255 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
256 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
257 {NULL, BatteryChargingPolicy::DEFAULT},
258 };
259
260 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
261 if (!ret) {
262 *ret = BatteryChargingPolicy::DEFAULT;
263 }
264
265 return *ret;
266}
267
268BatteryChargingState getBatteryChargingState(const char* chargingState) {
269 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
270 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
271 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
272 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
273 {NULL, BatteryChargingState::NORMAL},
274 };
275
276 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
277 if (!ret) {
278 *ret = BatteryChargingState::NORMAL;
279 }
280
281 return *ret;
282}
283
Bart Van Assche095c9442022-03-02 17:36:34 +0000284static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000285 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800286 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700287 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700288 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700289 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700290}
291
Jack Wue561d032022-11-24 12:19:41 +0800292static bool writeToFile(const String8& path, int32_t in_value) {
293 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
294}
295
Bart Van Assche095c9442022-03-02 17:36:34 +0000296static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700297 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000298 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
299 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
300 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
301 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
302 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
303 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
304 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
305 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
307 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
308 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
309 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
310 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
311 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700312 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700313 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700314 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700315
Bart Van Assche095c9442022-03-02 17:36:34 +0000316 if (readFromFile(path, &buf) <= 0) {
317 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
318 }
Todd Poynor752faf22013-06-12 13:25:59 -0700319
Yifan Hong1d4368b2019-10-07 11:18:04 -0700320 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000321 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700322 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000323 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100324 }
Todd Poynor752faf22013-06-12 13:25:59 -0700325
Yifan Hong1d4368b2019-10-07 11:18:04 -0700326 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700327}
328
Bart Van Assche095c9442022-03-02 17:36:34 +0000329static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700330 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700331 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700332
333 if (readFromFile(path, &buf) > 0)
334 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700335 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700336
337 return value;
338}
339
Bart Van Assche095c9442022-03-02 17:36:34 +0000340static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700341 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700342 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700343
344 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700345 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700346
Todd Poynor752faf22013-06-12 13:25:59 -0700347 return value;
348}
349
Bart Van Assche095c9442022-03-02 17:36:34 +0000350static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900351 constexpr char kScopeDevice[] = "Device";
352
353 String8 path;
354 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
355 std::string scope;
356 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
357}
358
Yifan Hong1353e702019-10-07 10:41:30 -0700359void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800360 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700361
Todd Poynorf5d30122013-08-12 17:03:35 -0700362 if (!mHealthdConfig->batteryPresentPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800363 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700364 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800365 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700366
Yifan Hongb99d15c2022-03-01 12:12:34 -0800367 mHealthInfo->batteryLevel = mBatteryFixedCapacity
368 ? mBatteryFixedCapacity
369 : getIntField(mHealthdConfig->batteryCapacityPath);
370 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700371
Ruchi Kandoicc338802015-08-24 13:01:16 -0700372 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800373 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700374
375 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800376 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700377
378 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800379 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700380
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700381 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800382 mHealthInfo->batteryChargeCounterUah =
383 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700384
Yifan Hong35cb0832019-10-07 13:58:29 -0700385 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800386 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700387 getIntField(mHealthdConfig->batteryCurrentAvgPath);
388
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800389 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
390 mHealthInfo->batteryChargeTimeToFullNowSeconds =
391 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
392
Stephane Lee1c108ed2020-02-10 18:23:57 -0800393 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
394 mHealthInfo->batteryFullChargeDesignCapacityUah =
395 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700396
Jack Wub57f68a2023-02-04 19:56:06 +0800397 if (!mHealthdConfig->batteryHealthStatusPath.isEmpty())
398 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
399
AleX Pelosiff708922023-02-17 01:39:21 +0000400 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
401 mHealthInfo->batteryHealthData->batteryStateOfHealth =
402 getIntField(mHealthdConfig->batteryStateOfHealthPath);
403
Jack Wue561d032022-11-24 12:19:41 +0800404 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
405 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
406 getIntField(mHealthdConfig->batteryManufacturingDatePath);
407
408 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
409 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
410 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
411
Yifan Hongb99d15c2022-03-01 12:12:34 -0800412 mHealthInfo->batteryTemperatureTenthsCelsius =
413 mBatteryFixedTemperature ? mBatteryFixedTemperature
414 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700415
Michael Scott3217c5c2016-06-05 11:20:13 -0700416 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700417
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800418 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
419 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
420
Michael Scott3217c5c2016-06-05 11:20:13 -0700421 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800422 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700423
Jack Wub57f68a2023-02-04 19:56:06 +0800424 // Backward compatible with android.hardware.health V1
425 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
426 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
427 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
428 } else {
429 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
430 }
Todd Poynor752faf22013-06-12 13:25:59 -0700431
Michael Scott3217c5c2016-06-05 11:20:13 -0700432 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800433 mHealthInfo->batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700434
Jack Wue561d032022-11-24 12:19:41 +0800435 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
436 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
437
438 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
439 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
440
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700441 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700442
ShevT9d98a6a2018-07-26 11:47:47 +0300443 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700444 String8 path;
445 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
446 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700447 if (getIntField(path)) {
448 path.clear();
449 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
450 mChargerNames[i].string());
451 switch(readPowerSupplyType(path)) {
452 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800453 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700454 break;
455 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800456 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700457 break;
458 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800459 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700460 break;
Jack Wu06b90412021-12-15 20:40:21 +0800461 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800462 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800463 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700464 default:
Jack Wu06b90412021-12-15 20:40:21 +0800465 path.clear();
466 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
467 mChargerNames[i].string());
Yifan Hongb99d15c2022-03-01 12:12:34 -0800468 if (access(path.string(), R_OK) == 0)
469 mHealthInfo->chargerDockOnline = true;
470 else
Jack Wu06b90412021-12-15 20:40:21 +0800471 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
472 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700473 }
474 path.clear();
475 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
476 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700477 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700478 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
479
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700480 path.clear();
481 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
482 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700483
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700484 int ChargingVoltage =
485 (access(path.string(), R_OK) == 0) ? getIntField(path) :
486 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700487
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700488 double power = ((double)ChargingCurrent / MILLION) *
489 ((double)ChargingVoltage / MILLION);
490 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800491 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
492 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700493 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700494 }
495 }
496 }
Yifan Hong1353e702019-10-07 10:41:30 -0700497}
Todd Poynor752faf22013-06-12 13:25:59 -0700498
Bart Van Assche095c9442022-03-02 17:36:34 +0000499static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700500 char dmesgline[256];
501 size_t len;
502 if (props.batteryPresent) {
503 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800504 props.batteryLevel, props.batteryVoltageMillivolts,
505 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
506 abs(props.batteryTemperatureTenthsCelsius / 10),
507 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
508 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700509
Yifan Hong1353e702019-10-07 10:41:30 -0700510 len = strlen(dmesgline);
Yifan Hong605e7d22021-02-08 15:14:48 -0800511 if (!healthd_config.batteryCurrentNowPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700512 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800513 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700514 }
515
Yifan Hong605e7d22021-02-08 15:14:48 -0800516 if (!healthd_config.batteryFullChargePath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700517 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800518 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700519 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700520
Yifan Hong605e7d22021-02-08 15:14:48 -0800521 if (!healthd_config.batteryCycleCountPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700522 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
523 props.batteryCycleCount);
524 }
525 } else {
526 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700527 }
528
Yifan Hongb99d15c2022-03-01 12:12:34 -0800529 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700530 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800531 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700532
533 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
534}
535
Bart Van Assche095c9442022-03-02 17:36:34 +0000536void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
537 const struct healthd_config& healthd_config) {
538 HealthInfo aidl_health_info;
539 (void)android::h2a::translate(health_info, &aidl_health_info);
540 doLogValues(aidl_health_info, healthd_config);
541}
542
543void BatteryMonitor::logValues(void) {
544 doLogValues(*mHealthInfo, *mHealthdConfig);
545}
546
Yifan Hong1353e702019-10-07 10:41:30 -0700547bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800548 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800549 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800550 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700551}
552
Yabin Cuiaedf6032016-02-19 18:03:23 -0800553int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700554 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800555 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700556 std::string buf;
557 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
558 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800559 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700560 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800561}
562
Jack Wue561d032022-11-24 12:19:41 +0800563status_t BatteryMonitor::setChargingPolicy(int value) {
564 status_t ret = NAME_NOT_FOUND;
565 bool result;
566 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
567 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
568 if (!result) {
569 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
570 ret = BAD_VALUE;
571 } else {
572 ret = OK;
573 }
574 }
575 return ret;
576}
577
578int BatteryMonitor::getChargingPolicy() {
579 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
580 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
581 std::string buf;
582 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
583 result = getBatteryChargingPolicy(buf.c_str());
584 }
585 return static_cast<int>(result);
586}
587
588int BatteryMonitor::getBatteryHealthData(int id) {
589 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
590 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
591 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
592 }
593 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
594 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
595 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
596 }
AleX Pelosiff708922023-02-17 01:39:21 +0000597 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
598 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
599 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
600 }
Jack Wue561d032022-11-24 12:19:41 +0800601 return 0;
602}
603
Todd Poynorc133b712013-08-14 17:39:13 -0700604status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
605 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800606 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700607
Todd Poynor8f132af2014-05-08 17:15:45 -0700608 val->valueInt64 = LONG_MIN;
609
Todd Poynorc133b712013-08-14 17:39:13 -0700610 switch(id) {
611 case BATTERY_PROP_CHARGE_COUNTER:
612 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700613 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700614 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700615 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700616 } else {
617 ret = NAME_NOT_FOUND;
618 }
619 break;
620
621 case BATTERY_PROP_CURRENT_NOW:
622 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700623 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700624 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700625 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700626 } else {
627 ret = NAME_NOT_FOUND;
628 }
629 break;
630
Todd Poynorbc102112013-08-27 18:11:49 -0700631 case BATTERY_PROP_CURRENT_AVG:
632 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700633 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700634 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700635 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700636 } else {
637 ret = NAME_NOT_FOUND;
638 }
639 break;
640
Paul Lawrence347c8de2014-03-19 15:04:40 -0700641 case BATTERY_PROP_CAPACITY:
642 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700643 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700644 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700645 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700646 } else {
647 ret = NAME_NOT_FOUND;
648 }
649 break;
650
Todd Poynor8f132af2014-05-08 17:15:45 -0700651 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700652 if (mHealthdConfig->energyCounter) {
653 ret = mHealthdConfig->energyCounter(&val->valueInt64);
654 } else {
655 ret = NAME_NOT_FOUND;
656 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700657 break;
658
Jin Qian72adf112017-02-02 17:31:13 -0800659 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800660 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700661 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800662 break;
663
Jack Wue561d032022-11-24 12:19:41 +0800664 case BATTERY_PROP_CHARGING_POLICY:
665 val->valueInt64 = getChargingPolicy();
666 ret = OK;
667 break;
668
669 case BATTERY_PROP_MANUFACTURING_DATE:
670 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
671 ret = OK;
672 break;
673
674 case BATTERY_PROP_FIRST_USAGE_DATE:
675 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
676 ret = OK;
677 break;
678
AleX Pelosiff708922023-02-17 01:39:21 +0000679 case BATTERY_PROP_STATE_OF_HEALTH:
680 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
681 ret = OK;
682 break;
683
Todd Poynorc133b712013-08-14 17:39:13 -0700684 default:
685 break;
686 }
687
Todd Poynorc133b712013-08-14 17:39:13 -0700688 return ret;
689}
690
Todd Poynor020369d2013-09-18 20:09:33 -0700691void BatteryMonitor::dumpState(int fd) {
692 int v;
693 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800694 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700695
Jack Wu06b90412021-12-15 20:40:21 +0800696 snprintf(vs, sizeof(vs),
697 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
698 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800699 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
700 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700701 write(fd, vs, strlen(vs));
702 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
703 props.batteryStatus, props.batteryHealth, props.batteryPresent);
704 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800705 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
706 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700707 write(fd, vs, strlen(vs));
708
709 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
710 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
711 snprintf(vs, sizeof(vs), "current now: %d\n", v);
712 write(fd, vs, strlen(vs));
713 }
714
715 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
716 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
717 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
718 write(fd, vs, strlen(vs));
719 }
720
721 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
722 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
723 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
724 write(fd, vs, strlen(vs));
725 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700726
727 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800728 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700729 write(fd, vs, strlen(vs));
730 }
731
732 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
733 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
734 write(fd, vs, strlen(vs));
735 }
736
737 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800738 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700739 write(fd, vs, strlen(vs));
740 }
Todd Poynor020369d2013-09-18 20:09:33 -0700741}
742
Todd Poynorc7464c92013-09-10 12:40:00 -0700743void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700744 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700745 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700746
Todd Poynorf5d30122013-08-12 17:03:35 -0700747 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800748 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700749 if (dir == NULL) {
750 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
751 } else {
752 struct dirent* entry;
753
James Hawkins588a2ca2016-02-18 14:52:46 -0800754 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700755 const char* name = entry->d_name;
756
757 if (!strcmp(name, ".") || !strcmp(name, ".."))
758 continue;
759
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000760 std::vector<String8>::iterator itIgnoreName =
761 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
762 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800763 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
764 continue;
765
Todd Poynor752faf22013-06-12 13:25:59 -0700766 // Look for "type" file in each subdirectory
767 path.clear();
768 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
769 switch(readPowerSupplyType(path)) {
770 case ANDROID_POWER_SUPPLY_TYPE_AC:
771 case ANDROID_POWER_SUPPLY_TYPE_USB:
772 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800773 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700774 path.clear();
775 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
776 if (access(path.string(), R_OK) == 0)
777 mChargerNames.add(String8(name));
778 break;
779
780 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900781 // Some devices expose the battery status of sub-component like
782 // stylus. Such a device-scoped battery info needs to be skipped
783 // in BatteryMonitor, which is intended to report the status of
784 // the battery supplying the power to the whole system.
785 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700786 mBatteryDevicePresent = true;
787
Todd Poynorf5d30122013-08-12 17:03:35 -0700788 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700789 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700790 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
791 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700792 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700793 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700794 }
795
Todd Poynorf5d30122013-08-12 17:03:35 -0700796 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700797 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700798 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
799 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700800 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700801 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700802 }
803
Todd Poynorf5d30122013-08-12 17:03:35 -0700804 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
805 path.clear();
806 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
807 name);
808 if (access(path, R_OK) == 0)
809 mHealthdConfig->batteryPresentPath = path;
810 }
811
812 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
813 path.clear();
814 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
815 name);
816 if (access(path, R_OK) == 0)
817 mHealthdConfig->batteryCapacityPath = path;
818 }
819
820 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
821 path.clear();
822 path.appendFormat("%s/%s/voltage_now",
823 POWER_SUPPLY_SYSFS_PATH, name);
824 if (access(path, R_OK) == 0) {
825 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700826 }
827 }
828
Ruchi Kandoicc338802015-08-24 13:01:16 -0700829 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
830 path.clear();
831 path.appendFormat("%s/%s/charge_full",
832 POWER_SUPPLY_SYSFS_PATH, name);
833 if (access(path, R_OK) == 0)
834 mHealthdConfig->batteryFullChargePath = path;
835 }
836
Todd Poynorf5d30122013-08-12 17:03:35 -0700837 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
838 path.clear();
839 path.appendFormat("%s/%s/current_now",
840 POWER_SUPPLY_SYSFS_PATH, name);
841 if (access(path, R_OK) == 0)
842 mHealthdConfig->batteryCurrentNowPath = path;
843 }
844
Ruchi Kandoicc338802015-08-24 13:01:16 -0700845 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
846 path.clear();
847 path.appendFormat("%s/%s/cycle_count",
848 POWER_SUPPLY_SYSFS_PATH, name);
849 if (access(path, R_OK) == 0)
850 mHealthdConfig->batteryCycleCountPath = path;
851 }
852
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800853 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) {
854 path.clear();
855 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
856 if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path;
857 }
858
859 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) {
860 path.clear();
861 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
862 if (access(path, R_OK) == 0)
863 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
864 }
865
Stephane Lee1c108ed2020-02-10 18:23:57 -0800866 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) {
867 path.clear();
868 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
869 if (access(path, R_OK) == 0)
870 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
871 }
872
Todd Poynorbc102112013-08-27 18:11:49 -0700873 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
874 path.clear();
875 path.appendFormat("%s/%s/current_avg",
876 POWER_SUPPLY_SYSFS_PATH, name);
877 if (access(path, R_OK) == 0)
878 mHealthdConfig->batteryCurrentAvgPath = path;
879 }
880
Todd Poynorf5d30122013-08-12 17:03:35 -0700881 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
882 path.clear();
883 path.appendFormat("%s/%s/charge_counter",
884 POWER_SUPPLY_SYSFS_PATH, name);
885 if (access(path, R_OK) == 0)
886 mHealthdConfig->batteryChargeCounterPath = path;
887 }
888
889 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
890 path.clear();
891 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
892 name);
893 if (access(path, R_OK) == 0) {
894 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700895 }
896 }
897
898 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
899 path.clear();
900 path.appendFormat("%s/%s/technology",
901 POWER_SUPPLY_SYSFS_PATH, name);
902 if (access(path, R_OK) == 0)
903 mHealthdConfig->batteryTechnologyPath = path;
904 }
905
Jack Wue561d032022-11-24 12:19:41 +0800906 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty()) {
907 path.clear();
908 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
909 if (access(path, R_OK) == 0) {
910 mHealthdConfig->batteryStateOfHealthPath = path;
911 } else {
912 path.clear();
913 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
914 if (access(path, R_OK) == 0)
915 mHealthdConfig->batteryStateOfHealthPath = path;
916 }
917 }
918
Jack Wub57f68a2023-02-04 19:56:06 +0800919 if (mHealthdConfig->batteryHealthStatusPath.isEmpty()) {
920 path.clear();
921 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
922 if (access(path, R_OK) == 0) mHealthdConfig->batteryHealthStatusPath = path;
923 }
924
Jack Wue561d032022-11-24 12:19:41 +0800925 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty()) {
926 path.clear();
927 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
928 if (access(path, R_OK) == 0)
929 mHealthdConfig->batteryManufacturingDatePath = path;
930 }
931
932 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty()) {
933 path.clear();
934 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
935 if (access(path, R_OK) == 0) mHealthdConfig->batteryFirstUsageDatePath = path;
936 }
937
938 if (mHealthdConfig->chargingStatePath.isEmpty()) {
939 path.clear();
940 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
941 if (access(path, R_OK) == 0) mHealthdConfig->chargingStatePath = path;
942 }
943
944 if (mHealthdConfig->chargingPolicyPath.isEmpty()) {
945 path.clear();
946 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
947 if (access(path, R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
948 }
949
Todd Poynor752faf22013-06-12 13:25:59 -0700950 break;
951
952 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
953 break;
954 }
Jack Wu06b90412021-12-15 20:40:21 +0800955
956 // Look for "is_dock" file
957 path.clear();
958 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
959 if (access(path.string(), R_OK) == 0) {
960 path.clear();
961 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
962 if (access(path.string(), R_OK) == 0)
963 mChargerNames.add(String8(name));
964
965 }
Todd Poynor752faf22013-06-12 13:25:59 -0700966 }
Todd Poynor752faf22013-06-12 13:25:59 -0700967 }
968
Ian Pedowitz585ab652015-10-12 19:01:00 -0700969 // Typically the case for devices which do not have a battery and
970 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700971 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700972 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700973 hc->periodic_chores_interval_fast = -1;
974 hc->periodic_chores_interval_slow = -1;
975 } else {
976 if (mHealthdConfig->batteryStatusPath.isEmpty())
977 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
978 if (mHealthdConfig->batteryHealthPath.isEmpty())
979 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
980 if (mHealthdConfig->batteryPresentPath.isEmpty())
981 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
982 if (mHealthdConfig->batteryCapacityPath.isEmpty())
983 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
984 if (mHealthdConfig->batteryVoltagePath.isEmpty())
985 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
986 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
987 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
988 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
989 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700990 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700991 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
992 if (mHealthdConfig->batteryFullChargePath.isEmpty())
993 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
994 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
995 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800996 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty())
997 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
998 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
999 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Stephane Lee1c108ed2020-02-10 18:23:57 -08001000 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
1001 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001002 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty())
1003 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Jack Wub57f68a2023-02-04 19:56:06 +08001004 if (mHealthdConfig->batteryHealthStatusPath.isEmpty())
1005 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001006 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty())
1007 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
1008 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
1009 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
1010 if (mHealthdConfig->chargingStatePath.isEmpty())
1011 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
1012 if (mHealthdConfig->chargingPolicyPath.isEmpty())
1013 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001014 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001015
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001016 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1017 && strtol(pval, NULL, 10) != 0) {
1018 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1019 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1020 }
Todd Poynor752faf22013-06-12 13:25:59 -07001021}
1022
1023}; // namespace android