blob: bd7955a7f8b45a657f506168703982a92915b4d9 [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "healthd"
18
Yabin Cuie98e1772016-02-17 12:21:34 -080019#include <healthd/healthd.h>
20#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Thierry Strudelf73de6f2019-01-11 17:09:20 -080029
30#include <algorithm>
James Hawkins588a2ca2016-02-18 14:52:46 -080031#include <memory>
Yifan Hong1d4368b2019-10-07 11:18:04 -070032#include <optional>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033
Yifan Hongb99d15c2022-03-01 12:12:34 -080034#include <aidl/android/hardware/health/HealthInfo.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070035#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070036#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070037#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070038#include <android/hardware/health/2.1/types.h>
Yifan Hongb99d15c2022-03-01 12:12:34 -080039#include <android/hardware/health/translate-ndk.h>
Todd Poynor752faf22013-06-12 13:25:59 -070040#include <batteryservice/BatteryService.h>
41#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070042#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070043#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070044#include <utils/String8.h>
45#include <utils/Vector.h>
46
47#define POWER_SUPPLY_SUBSYSTEM "power_supply"
48#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070049#define FAKE_BATTERY_CAPACITY 42
50#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080051#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070052#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070053
Yifan Hong1d4368b2019-10-07 11:18:04 -070054using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
55using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
56using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
Yifan Hongb99d15c2022-03-01 12:12:34 -080057using aidl::android::hardware::health::BatteryCapacityLevel;
Jack Wue561d032022-11-24 12:19:41 +080058using aidl::android::hardware::health::BatteryChargingPolicy;
59using aidl::android::hardware::health::BatteryChargingState;
Yifan Hongb99d15c2022-03-01 12:12:34 -080060using aidl::android::hardware::health::BatteryHealth;
Jack Wue561d032022-11-24 12:19:41 +080061using aidl::android::hardware::health::BatteryHealthData;
Yifan Hongb99d15c2022-03-01 12:12:34 -080062using aidl::android::hardware::health::BatteryStatus;
63using aidl::android::hardware::health::HealthInfo;
64
65namespace {
66
67// Translate from AIDL back to HIDL definition for getHealthInfo_*_* calls.
68// Skips storageInfo and diskStats.
69void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
70 ::android::hardware::health::V1_0::HealthInfo* out) {
71 out->chargerAcOnline = in.chargerAcOnline;
72 out->chargerUsbOnline = in.chargerUsbOnline;
73 out->chargerWirelessOnline = in.chargerWirelessOnline;
74 out->maxChargingCurrent = in.maxChargingCurrentMicroamps;
75 out->maxChargingVoltage = in.maxChargingVoltageMicrovolts;
76 out->batteryStatus =
77 static_cast<::android::hardware::health::V1_0::BatteryStatus>(in.batteryStatus);
78 out->batteryHealth =
79 static_cast<::android::hardware::health::V1_0::BatteryHealth>(in.batteryHealth);
80 out->batteryPresent = in.batteryPresent;
81 out->batteryLevel = in.batteryLevel;
82 out->batteryVoltage = in.batteryVoltageMillivolts;
83 out->batteryTemperature = in.batteryTemperatureTenthsCelsius;
84 out->batteryCurrent = in.batteryCurrentMicroamps;
85 out->batteryCycleCount = in.batteryCycleCount;
86 out->batteryFullCharge = in.batteryFullChargeUah;
87 out->batteryChargeCounter = in.batteryChargeCounterUah;
88 out->batteryTechnology = in.batteryTechnology;
89}
90
91void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
92 ::android::hardware::health::V2_0::HealthInfo* out) {
93 translateToHidl(in, &out->legacy);
94 out->batteryCurrentAverage = in.batteryCurrentAverageMicroamps;
95 // Skip storageInfo and diskStats
96}
97
98void translateToHidl(const ::aidl::android::hardware::health::HealthInfo& in,
99 ::android::hardware::health::V2_1::HealthInfo* out) {
100 translateToHidl(in, &out->legacy);
101 out->batteryCapacityLevel = static_cast<android::hardware::health::V2_1::BatteryCapacityLevel>(
102 in.batteryCapacityLevel);
103 out->batteryChargeTimeToFullNowSeconds = in.batteryChargeTimeToFullNowSeconds;
104 out->batteryFullChargeDesignCapacityUah = in.batteryFullChargeDesignCapacityUah;
105}
106
107} // namespace
Yifan Hong1d4368b2019-10-07 11:18:04 -0700108
Todd Poynor752faf22013-06-12 13:25:59 -0700109namespace android {
110
Yifan Hong1d4368b2019-10-07 11:18:04 -0700111template <typename T>
112struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -0700113 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700114 T val;
Todd Poynor752faf22013-06-12 13:25:59 -0700115};
116
Yifan Hong1d4368b2019-10-07 11:18:04 -0700117template <typename T>
118static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -0700119 for (int i = 0; map[i].s; i++)
120 if (!strcmp(str, map[i].s))
121 return map[i].val;
122
Yifan Hong1d4368b2019-10-07 11:18:04 -0700123 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -0800124}
125
Yifan Hongb99d15c2022-03-01 12:12:34 -0800126static void initHealthInfo(HealthInfo* health_info) {
Bart Van Assche024e18f2022-02-24 21:22:07 +0000127 *health_info = {
128 .batteryCapacityLevel = BatteryCapacityLevel::UNSUPPORTED,
129 .batteryChargeTimeToFullNowSeconds =
130 (int64_t)HealthInfo::BATTERY_CHARGE_TIME_TO_FULL_NOW_SECONDS_UNSUPPORTED,
131 .batteryStatus = BatteryStatus::UNKNOWN,
132 .batteryHealth = BatteryHealth::UNKNOWN,
133 };
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800134}
135
Todd Poynore030a102018-01-19 14:03:59 -0800136BatteryMonitor::BatteryMonitor()
137 : mHealthdConfig(nullptr),
138 mBatteryDevicePresent(false),
139 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -0700140 mBatteryFixedTemperature(0),
Jack Wub57f68a2023-02-04 19:56:06 +0800141 mBatteryHealthStatus(BatteryMonitor::BH_UNKNOWN),
Yifan Hongb99d15c2022-03-01 12:12:34 -0800142 mHealthInfo(std::make_unique<HealthInfo>()) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800143 initHealthInfo(mHealthInfo.get());
144}
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145
146BatteryMonitor::~BatteryMonitor() {}
147
Yifan Hongb99d15c2022-03-01 12:12:34 -0800148HealthInfo_1_0 BatteryMonitor::getHealthInfo_1_0() const {
149 HealthInfo_1_0 health_info_1_0;
150 translateToHidl(*mHealthInfo, &health_info_1_0);
151 return health_info_1_0;
Yabin Cuidb04a492016-02-16 17:19:23 -0800152}
153
Yifan Hongb99d15c2022-03-01 12:12:34 -0800154HealthInfo_2_0 BatteryMonitor::getHealthInfo_2_0() const {
155 HealthInfo_2_0 health_info_2_0;
156 translateToHidl(*mHealthInfo, &health_info_2_0);
157 return health_info_2_0;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800158}
159
Yifan Hongb99d15c2022-03-01 12:12:34 -0800160HealthInfo_2_1 BatteryMonitor::getHealthInfo_2_1() const {
161 HealthInfo_2_1 health_info_2_1;
162 translateToHidl(*mHealthInfo, &health_info_2_1);
163 return health_info_2_1;
164}
165
166const HealthInfo& BatteryMonitor::getHealthInfo() const {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700167 return *mHealthInfo;
168}
169
170BatteryStatus getBatteryStatus(const char* status) {
171 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
172 {"Unknown", BatteryStatus::UNKNOWN},
173 {"Charging", BatteryStatus::CHARGING},
174 {"Discharging", BatteryStatus::DISCHARGING},
175 {"Not charging", BatteryStatus::NOT_CHARGING},
176 {"Full", BatteryStatus::FULL},
177 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700178 };
179
Yifan Hong1d4368b2019-10-07 11:18:04 -0700180 auto ret = mapSysfsString(status, batteryStatusMap);
181 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700182 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700183 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 }
185
Yifan Hong1d4368b2019-10-07 11:18:04 -0700186 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700187}
188
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800189BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
190 static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
191 {"Unknown", BatteryCapacityLevel::UNKNOWN},
192 {"Critical", BatteryCapacityLevel::CRITICAL},
193 {"Low", BatteryCapacityLevel::LOW},
194 {"Normal", BatteryCapacityLevel::NORMAL},
195 {"High", BatteryCapacityLevel::HIGH},
196 {"Full", BatteryCapacityLevel::FULL},
Stephane Lee06846042020-02-12 17:00:24 -0800197 {NULL, BatteryCapacityLevel::UNSUPPORTED},
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800198 };
199
200 auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
201 if (!ret) {
Stephane Lee06846042020-02-12 17:00:24 -0800202 KLOG_WARNING(LOG_TAG, "Unsupported battery capacity level '%s'\n", capacityLevel);
203 *ret = BatteryCapacityLevel::UNSUPPORTED;
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800204 }
205
206 return *ret;
207}
208
Yifan Hong1d4368b2019-10-07 11:18:04 -0700209BatteryHealth getBatteryHealth(const char* status) {
210 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
211 {"Unknown", BatteryHealth::UNKNOWN},
212 {"Good", BatteryHealth::GOOD},
213 {"Overheat", BatteryHealth::OVERHEAT},
214 {"Dead", BatteryHealth::DEAD},
215 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
216 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
217 {"Cold", BatteryHealth::COLD},
218 // battery health values from JEITA spec
219 {"Warm", BatteryHealth::GOOD},
220 {"Cool", BatteryHealth::GOOD},
221 {"Hot", BatteryHealth::OVERHEAT},
222 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700223 };
224
Yifan Hong1d4368b2019-10-07 11:18:04 -0700225 auto ret = mapSysfsString(status, batteryHealthMap);
226 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700227 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700228 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700229 }
230
Yifan Hong1d4368b2019-10-07 11:18:04 -0700231 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700232}
233
Jack Wub57f68a2023-02-04 19:56:06 +0800234BatteryHealth getBatteryHealthStatus(int status) {
235 BatteryHealth value;
236
237 if (status == BatteryMonitor::BH_NOMINAL)
238 value = BatteryHealth::GOOD;
239 else if (status == BatteryMonitor::BH_MARGINAL)
240 value = BatteryHealth::FAIR;
241 else if (status == BatteryMonitor::BH_NEEDS_REPLACEMENT)
242 value = BatteryHealth::DEAD;
243 else if (status == BatteryMonitor::BH_FAILED)
244 value = BatteryHealth::UNSPECIFIED_FAILURE;
Jack Wucf996f32023-04-13 19:37:46 +0800245 else if (status == BatteryMonitor::BH_NOT_AVAILABLE)
246 value = BatteryHealth::NOT_AVAILABLE;
Jack Wu8231c3f2023-05-19 14:31:53 +0800247 else if (status == BatteryMonitor::BH_INCONSISTENT)
248 value = BatteryHealth::INCONSISTENT;
Jack Wub57f68a2023-02-04 19:56:06 +0800249 else
250 value = BatteryHealth::UNKNOWN;
251
252 return value;
253}
254
Jack Wue561d032022-11-24 12:19:41 +0800255BatteryChargingPolicy getBatteryChargingPolicy(const char* chargingPolicy) {
256 static SysfsStringEnumMap<BatteryChargingPolicy> batteryChargingPolicyMap[] = {
257 {"0", BatteryChargingPolicy::INVALID}, {"1", BatteryChargingPolicy::DEFAULT},
258 {"2", BatteryChargingPolicy::LONG_LIFE}, {"3", BatteryChargingPolicy::ADAPTIVE},
259 {NULL, BatteryChargingPolicy::DEFAULT},
260 };
261
262 auto ret = mapSysfsString(chargingPolicy, batteryChargingPolicyMap);
263 if (!ret) {
264 *ret = BatteryChargingPolicy::DEFAULT;
265 }
266
267 return *ret;
268}
269
270BatteryChargingState getBatteryChargingState(const char* chargingState) {
271 static SysfsStringEnumMap<BatteryChargingState> batteryChargingStateMap[] = {
272 {"0", BatteryChargingState::INVALID}, {"1", BatteryChargingState::NORMAL},
273 {"2", BatteryChargingState::TOO_COLD}, {"3", BatteryChargingState::TOO_HOT},
274 {"4", BatteryChargingState::LONG_LIFE}, {"5", BatteryChargingState::ADAPTIVE},
275 {NULL, BatteryChargingState::NORMAL},
276 };
277
278 auto ret = mapSysfsString(chargingState, batteryChargingStateMap);
279 if (!ret) {
280 *ret = BatteryChargingState::NORMAL;
281 }
282
283 return *ret;
284}
285
Bart Van Assche095c9442022-03-02 17:36:34 +0000286static int readFromFile(const String8& path, std::string* buf) {
Bart Van Assche5a7e5082022-02-24 21:40:15 +0000287 buf->clear();
Steven Moreland2aac3352017-03-10 22:31:08 -0800288 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700289 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700290 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700291 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700292}
293
Jack Wue561d032022-11-24 12:19:41 +0800294static bool writeToFile(const String8& path, int32_t in_value) {
295 return android::base::WriteStringToFile(std::to_string(in_value), path.c_str());
296}
297
Bart Van Assche095c9442022-03-02 17:36:34 +0000298static BatteryMonitor::PowerSupplyType readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700299 static SysfsStringEnumMap<int> supplyTypeMap[] = {
Bart Van Assche095c9442022-03-02 17:36:34 +0000300 {"Unknown", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
301 {"Battery", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_BATTERY},
302 {"UPS", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
303 {"Mains", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
304 {"USB", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
305 {"USB_DCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
306 {"USB_HVDCP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
307 {"USB_CDP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
308 {"USB_ACA", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
309 {"USB_C", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
310 {"USB_PD", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_AC},
311 {"USB_PD_DRP", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_USB},
312 {"Wireless", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
313 {"Dock", BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_DOCK},
Yifan Hong1d4368b2019-10-07 11:18:04 -0700314 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700315 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700316 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700317
Bart Van Assche095c9442022-03-02 17:36:34 +0000318 if (readFromFile(path, &buf) <= 0) {
319 return BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
320 }
Todd Poynor752faf22013-06-12 13:25:59 -0700321
Yifan Hong1d4368b2019-10-07 11:18:04 -0700322 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
John Stultz47a6bf02019-11-06 00:23:34 +0000323 if (!ret) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700324 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Bart Van Assche095c9442022-03-02 17:36:34 +0000325 *ret = BatteryMonitor::ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100326 }
Todd Poynor752faf22013-06-12 13:25:59 -0700327
Yifan Hong1d4368b2019-10-07 11:18:04 -0700328 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700329}
330
Bart Van Assche095c9442022-03-02 17:36:34 +0000331static bool getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700332 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700333 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700334
335 if (readFromFile(path, &buf) > 0)
336 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700337 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700338
339 return value;
340}
341
Bart Van Assche095c9442022-03-02 17:36:34 +0000342static int getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700343 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700344 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700345
346 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700347 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700348
Todd Poynor752faf22013-06-12 13:25:59 -0700349 return value;
350}
351
Bart Van Assche095c9442022-03-02 17:36:34 +0000352static bool isScopedPowerSupply(const char* name) {
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900353 constexpr char kScopeDevice[] = "Device";
354
355 String8 path;
356 path.appendFormat("%s/%s/scope", POWER_SUPPLY_SYSFS_PATH, name);
357 std::string scope;
358 return (readFromFile(path, &scope) > 0 && scope == kScopeDevice);
359}
360
Yifan Hong1353e702019-10-07 10:41:30 -0700361void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800362 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700363
Todd Poynorf5d30122013-08-12 17:03:35 -0700364 if (!mHealthdConfig->batteryPresentPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800365 mHealthInfo->batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700366 else
Yifan Hongb99d15c2022-03-01 12:12:34 -0800367 mHealthInfo->batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700368
Yifan Hongb99d15c2022-03-01 12:12:34 -0800369 mHealthInfo->batteryLevel = mBatteryFixedCapacity
370 ? mBatteryFixedCapacity
371 : getIntField(mHealthdConfig->batteryCapacityPath);
372 mHealthInfo->batteryVoltageMillivolts = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700373
Ruchi Kandoicc338802015-08-24 13:01:16 -0700374 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800375 mHealthInfo->batteryCurrentMicroamps = getIntField(mHealthdConfig->batteryCurrentNowPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700376
377 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800378 mHealthInfo->batteryFullChargeUah = getIntField(mHealthdConfig->batteryFullChargePath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700379
380 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800381 mHealthInfo->batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700382
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700383 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800384 mHealthInfo->batteryChargeCounterUah =
385 getIntField(mHealthdConfig->batteryChargeCounterPath);
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700386
Yifan Hong35cb0832019-10-07 13:58:29 -0700387 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
Yifan Hongb99d15c2022-03-01 12:12:34 -0800388 mHealthInfo->batteryCurrentAverageMicroamps =
Yifan Hong35cb0832019-10-07 13:58:29 -0700389 getIntField(mHealthdConfig->batteryCurrentAvgPath);
390
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800391 if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
392 mHealthInfo->batteryChargeTimeToFullNowSeconds =
393 getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
394
Stephane Lee1c108ed2020-02-10 18:23:57 -0800395 if (!mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
396 mHealthInfo->batteryFullChargeDesignCapacityUah =
397 getIntField(mHealthdConfig->batteryFullChargeDesignCapacityUahPath);
Yifan Hong35cb0832019-10-07 13:58:29 -0700398
Jack Wub57f68a2023-02-04 19:56:06 +0800399 if (!mHealthdConfig->batteryHealthStatusPath.isEmpty())
400 mBatteryHealthStatus = getIntField(mHealthdConfig->batteryHealthStatusPath);
401
AleX Pelosiff708922023-02-17 01:39:21 +0000402 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
403 mHealthInfo->batteryHealthData->batteryStateOfHealth =
404 getIntField(mHealthdConfig->batteryStateOfHealthPath);
405
Jack Wue561d032022-11-24 12:19:41 +0800406 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
407 mHealthInfo->batteryHealthData->batteryManufacturingDateSeconds =
408 getIntField(mHealthdConfig->batteryManufacturingDatePath);
409
410 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
411 mHealthInfo->batteryHealthData->batteryFirstUsageSeconds =
412 getIntField(mHealthdConfig->batteryFirstUsageDatePath);
413
Yifan Hongb99d15c2022-03-01 12:12:34 -0800414 mHealthInfo->batteryTemperatureTenthsCelsius =
415 mBatteryFixedTemperature ? mBatteryFixedTemperature
416 : getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700417
Michael Scott3217c5c2016-06-05 11:20:13 -0700418 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700419
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800420 if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
421 mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
422
Michael Scott3217c5c2016-06-05 11:20:13 -0700423 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800424 mHealthInfo->batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700425
Jack Wub57f68a2023-02-04 19:56:06 +0800426 // Backward compatible with android.hardware.health V1
427 if (mBatteryHealthStatus < BatteryMonitor::BH_MARGINAL) {
428 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
429 mHealthInfo->batteryHealth = getBatteryHealth(buf.c_str());
430 } else {
431 mHealthInfo->batteryHealth = getBatteryHealthStatus(mBatteryHealthStatus);
432 }
Todd Poynor752faf22013-06-12 13:25:59 -0700433
Michael Scott3217c5c2016-06-05 11:20:13 -0700434 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
Yifan Hongb99d15c2022-03-01 12:12:34 -0800435 mHealthInfo->batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700436
Jack Wue561d032022-11-24 12:19:41 +0800437 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
438 mHealthInfo->chargingPolicy = getBatteryChargingPolicy(buf.c_str());
439
440 if (readFromFile(mHealthdConfig->chargingStatePath, &buf) > 0)
441 mHealthInfo->chargingState = getBatteryChargingState(buf.c_str());
442
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700443 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700444
ShevT9d98a6a2018-07-26 11:47:47 +0300445 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700446 String8 path;
447 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
448 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700449 if (getIntField(path)) {
450 path.clear();
451 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
452 mChargerNames[i].string());
453 switch(readPowerSupplyType(path)) {
454 case ANDROID_POWER_SUPPLY_TYPE_AC:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800455 mHealthInfo->chargerAcOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700456 break;
457 case ANDROID_POWER_SUPPLY_TYPE_USB:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800458 mHealthInfo->chargerUsbOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700459 break;
460 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800461 mHealthInfo->chargerWirelessOnline = true;
Michael Scott3217c5c2016-06-05 11:20:13 -0700462 break;
Jack Wu06b90412021-12-15 20:40:21 +0800463 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Yifan Hongb99d15c2022-03-01 12:12:34 -0800464 mHealthInfo->chargerDockOnline = true;
Jack Wu06b90412021-12-15 20:40:21 +0800465 break;
Michael Scott3217c5c2016-06-05 11:20:13 -0700466 default:
Jack Wu06b90412021-12-15 20:40:21 +0800467 path.clear();
468 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH,
469 mChargerNames[i].string());
Yifan Hongb99d15c2022-03-01 12:12:34 -0800470 if (access(path.string(), R_OK) == 0)
471 mHealthInfo->chargerDockOnline = true;
472 else
Jack Wu06b90412021-12-15 20:40:21 +0800473 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
474 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700475 }
476 path.clear();
477 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
478 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700479 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700480 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
481
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700482 path.clear();
483 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
484 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700485
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700486 int ChargingVoltage =
487 (access(path.string(), R_OK) == 0) ? getIntField(path) :
488 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700489
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700490 double power = ((double)ChargingCurrent / MILLION) *
491 ((double)ChargingVoltage / MILLION);
492 if (MaxPower < power) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800493 mHealthInfo->maxChargingCurrentMicroamps = ChargingCurrent;
494 mHealthInfo->maxChargingVoltageMicrovolts = ChargingVoltage;
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700495 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700496 }
497 }
498 }
Yifan Hong1353e702019-10-07 10:41:30 -0700499}
Todd Poynor752faf22013-06-12 13:25:59 -0700500
Bart Van Assche095c9442022-03-02 17:36:34 +0000501static void doLogValues(const HealthInfo& props, const struct healthd_config& healthd_config) {
Yifan Hong1353e702019-10-07 10:41:30 -0700502 char dmesgline[256];
503 size_t len;
504 if (props.batteryPresent) {
505 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800506 props.batteryLevel, props.batteryVoltageMillivolts,
507 props.batteryTemperatureTenthsCelsius < 0 ? "-" : "",
508 abs(props.batteryTemperatureTenthsCelsius / 10),
509 abs(props.batteryTemperatureTenthsCelsius % 10), props.batteryHealth,
510 props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700511
Yifan Hong1353e702019-10-07 10:41:30 -0700512 len = strlen(dmesgline);
Yifan Hong605e7d22021-02-08 15:14:48 -0800513 if (!healthd_config.batteryCurrentNowPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700514 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800515 props.batteryCurrentMicroamps);
Todd Poynor10b235e2013-08-07 15:25:14 -0700516 }
517
Yifan Hong605e7d22021-02-08 15:14:48 -0800518 if (!healthd_config.batteryFullChargePath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700519 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800520 props.batteryFullChargeUah);
Yifan Hong1353e702019-10-07 10:41:30 -0700521 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700522
Yifan Hong605e7d22021-02-08 15:14:48 -0800523 if (!healthd_config.batteryCycleCountPath.isEmpty()) {
Yifan Hong1353e702019-10-07 10:41:30 -0700524 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
525 props.batteryCycleCount);
526 }
527 } else {
528 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700529 }
530
Yifan Hongb99d15c2022-03-01 12:12:34 -0800531 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s%s",
Yifan Hong1353e702019-10-07 10:41:30 -0700532 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
Yifan Hongb99d15c2022-03-01 12:12:34 -0800533 props.chargerWirelessOnline ? "w" : "", props.chargerDockOnline ? "d" : "");
Yifan Hong1353e702019-10-07 10:41:30 -0700534
535 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
536}
537
Bart Van Assche095c9442022-03-02 17:36:34 +0000538void BatteryMonitor::logValues(const HealthInfo_2_1& health_info,
539 const struct healthd_config& healthd_config) {
540 HealthInfo aidl_health_info;
541 (void)android::h2a::translate(health_info, &aidl_health_info);
542 doLogValues(aidl_health_info, healthd_config);
543}
544
545void BatteryMonitor::logValues(void) {
546 doLogValues(*mHealthInfo, *mHealthdConfig);
547}
548
Yifan Hong1353e702019-10-07 10:41:30 -0700549bool BatteryMonitor::isChargerOnline() {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800550 const HealthInfo& props = *mHealthInfo;
Jack Wu06b90412021-12-15 20:40:21 +0800551 return props.chargerAcOnline | props.chargerUsbOnline | props.chargerWirelessOnline |
Yifan Hongb99d15c2022-03-01 12:12:34 -0800552 props.chargerDockOnline;
Todd Poynor752faf22013-06-12 13:25:59 -0700553}
554
Yabin Cuiaedf6032016-02-19 18:03:23 -0800555int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700556 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800557 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700558 std::string buf;
559 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
560 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800561 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700562 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800563}
564
Jack Wue561d032022-11-24 12:19:41 +0800565status_t BatteryMonitor::setChargingPolicy(int value) {
566 status_t ret = NAME_NOT_FOUND;
567 bool result;
568 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
569 result = writeToFile(mHealthdConfig->chargingPolicyPath, value);
570 if (!result) {
571 KLOG_WARNING(LOG_TAG, "setChargingPolicy fail\n");
572 ret = BAD_VALUE;
573 } else {
574 ret = OK;
575 }
576 }
577 return ret;
578}
579
580int BatteryMonitor::getChargingPolicy() {
581 BatteryChargingPolicy result = BatteryChargingPolicy::DEFAULT;
582 if (!mHealthdConfig->chargingPolicyPath.isEmpty()) {
583 std::string buf;
584 if (readFromFile(mHealthdConfig->chargingPolicyPath, &buf) > 0)
585 result = getBatteryChargingPolicy(buf.c_str());
586 }
587 return static_cast<int>(result);
588}
589
590int BatteryMonitor::getBatteryHealthData(int id) {
591 if (id == BATTERY_PROP_MANUFACTURING_DATE) {
592 if (!mHealthdConfig->batteryManufacturingDatePath.isEmpty())
593 return getIntField(mHealthdConfig->batteryManufacturingDatePath);
594 }
595 if (id == BATTERY_PROP_FIRST_USAGE_DATE) {
596 if (!mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
597 return getIntField(mHealthdConfig->batteryFirstUsageDatePath);
598 }
AleX Pelosiff708922023-02-17 01:39:21 +0000599 if (id == BATTERY_PROP_STATE_OF_HEALTH) {
600 if (!mHealthdConfig->batteryStateOfHealthPath.isEmpty())
601 return getIntField(mHealthdConfig->batteryStateOfHealthPath);
602 }
Jack Wue561d032022-11-24 12:19:41 +0800603 return 0;
604}
605
Todd Poynorc133b712013-08-14 17:39:13 -0700606status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
607 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800608 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700609
Todd Poynor8f132af2014-05-08 17:15:45 -0700610 val->valueInt64 = LONG_MIN;
611
Todd Poynorc133b712013-08-14 17:39:13 -0700612 switch(id) {
613 case BATTERY_PROP_CHARGE_COUNTER:
614 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700615 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700616 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700617 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700618 } else {
619 ret = NAME_NOT_FOUND;
620 }
621 break;
622
623 case BATTERY_PROP_CURRENT_NOW:
624 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700625 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700626 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700627 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700628 } else {
629 ret = NAME_NOT_FOUND;
630 }
631 break;
632
Todd Poynorbc102112013-08-27 18:11:49 -0700633 case BATTERY_PROP_CURRENT_AVG:
634 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700635 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700636 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700637 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700638 } else {
639 ret = NAME_NOT_FOUND;
640 }
641 break;
642
Paul Lawrence347c8de2014-03-19 15:04:40 -0700643 case BATTERY_PROP_CAPACITY:
644 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700645 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700646 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700647 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700648 } else {
649 ret = NAME_NOT_FOUND;
650 }
651 break;
652
Todd Poynor8f132af2014-05-08 17:15:45 -0700653 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700654 if (mHealthdConfig->energyCounter) {
655 ret = mHealthdConfig->energyCounter(&val->valueInt64);
656 } else {
657 ret = NAME_NOT_FOUND;
658 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700659 break;
660
Jin Qian72adf112017-02-02 17:31:13 -0800661 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800662 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700663 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800664 break;
665
Jack Wue561d032022-11-24 12:19:41 +0800666 case BATTERY_PROP_CHARGING_POLICY:
667 val->valueInt64 = getChargingPolicy();
668 ret = OK;
669 break;
670
671 case BATTERY_PROP_MANUFACTURING_DATE:
672 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_MANUFACTURING_DATE);
673 ret = OK;
674 break;
675
676 case BATTERY_PROP_FIRST_USAGE_DATE:
677 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_FIRST_USAGE_DATE);
678 ret = OK;
679 break;
680
AleX Pelosiff708922023-02-17 01:39:21 +0000681 case BATTERY_PROP_STATE_OF_HEALTH:
682 val->valueInt64 = getBatteryHealthData(BATTERY_PROP_STATE_OF_HEALTH);
683 ret = OK;
684 break;
685
Todd Poynorc133b712013-08-14 17:39:13 -0700686 default:
687 break;
688 }
689
Todd Poynorc133b712013-08-14 17:39:13 -0700690 return ret;
691}
692
Todd Poynor020369d2013-09-18 20:09:33 -0700693void BatteryMonitor::dumpState(int fd) {
694 int v;
695 char vs[128];
Yifan Hongb99d15c2022-03-01 12:12:34 -0800696 const HealthInfo& props = *mHealthInfo;
Todd Poynor020369d2013-09-18 20:09:33 -0700697
Jack Wu06b90412021-12-15 20:40:21 +0800698 snprintf(vs, sizeof(vs),
699 "ac: %d usb: %d wireless: %d dock: %d current_max: %d voltage_max: %d\n",
700 props.chargerAcOnline, props.chargerUsbOnline, props.chargerWirelessOnline,
Yifan Hongb99d15c2022-03-01 12:12:34 -0800701 props.chargerDockOnline, props.maxChargingCurrentMicroamps,
702 props.maxChargingVoltageMicrovolts);
Todd Poynor020369d2013-09-18 20:09:33 -0700703 write(fd, vs, strlen(vs));
704 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
705 props.batteryStatus, props.batteryHealth, props.batteryPresent);
706 write(fd, vs, strlen(vs));
Yifan Hongb99d15c2022-03-01 12:12:34 -0800707 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n", props.batteryLevel,
708 props.batteryVoltageMillivolts, props.batteryTemperatureTenthsCelsius);
Todd Poynor020369d2013-09-18 20:09:33 -0700709 write(fd, vs, strlen(vs));
710
711 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
712 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
713 snprintf(vs, sizeof(vs), "current now: %d\n", v);
714 write(fd, vs, strlen(vs));
715 }
716
717 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
718 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
719 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
720 write(fd, vs, strlen(vs));
721 }
722
723 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
724 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
725 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
726 write(fd, vs, strlen(vs));
727 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700728
729 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800730 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrentMicroamps);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700731 write(fd, vs, strlen(vs));
732 }
733
734 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
735 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
736 write(fd, vs, strlen(vs));
737 }
738
739 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
Yifan Hongb99d15c2022-03-01 12:12:34 -0800740 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullChargeUah);
Ruchi Kandoicc338802015-08-24 13:01:16 -0700741 write(fd, vs, strlen(vs));
742 }
Todd Poynor020369d2013-09-18 20:09:33 -0700743}
744
Todd Poynorc7464c92013-09-10 12:40:00 -0700745void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700746 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700747 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700748
Todd Poynorf5d30122013-08-12 17:03:35 -0700749 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800750 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700751 if (dir == NULL) {
752 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
753 } else {
754 struct dirent* entry;
755
James Hawkins588a2ca2016-02-18 14:52:46 -0800756 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700757 const char* name = entry->d_name;
758
759 if (!strcmp(name, ".") || !strcmp(name, ".."))
760 continue;
761
Bart Van Assche25b2a8d2022-02-24 21:51:34 +0000762 std::vector<String8>::iterator itIgnoreName =
763 find(hc->ignorePowerSupplyNames.begin(), hc->ignorePowerSupplyNames.end(),
764 String8(name));
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800765 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
766 continue;
767
Todd Poynor752faf22013-06-12 13:25:59 -0700768 // Look for "type" file in each subdirectory
769 path.clear();
770 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
771 switch(readPowerSupplyType(path)) {
772 case ANDROID_POWER_SUPPLY_TYPE_AC:
773 case ANDROID_POWER_SUPPLY_TYPE_USB:
774 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
Jack Wu06b90412021-12-15 20:40:21 +0800775 case ANDROID_POWER_SUPPLY_TYPE_DOCK:
Todd Poynor752faf22013-06-12 13:25:59 -0700776 path.clear();
777 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
778 if (access(path.string(), R_OK) == 0)
779 mChargerNames.add(String8(name));
780 break;
781
782 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Kazuhiro Inaba8e4d9822019-06-12 13:46:08 +0900783 // Some devices expose the battery status of sub-component like
784 // stylus. Such a device-scoped battery info needs to be skipped
785 // in BatteryMonitor, which is intended to report the status of
786 // the battery supplying the power to the whole system.
787 if (isScopedPowerSupply(name)) continue;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700788 mBatteryDevicePresent = true;
789
Todd Poynorf5d30122013-08-12 17:03:35 -0700790 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700791 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700792 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
793 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700794 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700795 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700796 }
797
Todd Poynorf5d30122013-08-12 17:03:35 -0700798 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700799 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700800 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
801 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700802 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700803 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700804 }
805
Todd Poynorf5d30122013-08-12 17:03:35 -0700806 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
807 path.clear();
808 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
809 name);
810 if (access(path, R_OK) == 0)
811 mHealthdConfig->batteryPresentPath = path;
812 }
813
814 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
815 path.clear();
816 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
817 name);
818 if (access(path, R_OK) == 0)
819 mHealthdConfig->batteryCapacityPath = path;
820 }
821
822 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
823 path.clear();
824 path.appendFormat("%s/%s/voltage_now",
825 POWER_SUPPLY_SYSFS_PATH, name);
826 if (access(path, R_OK) == 0) {
827 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700828 }
829 }
830
Ruchi Kandoicc338802015-08-24 13:01:16 -0700831 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
832 path.clear();
833 path.appendFormat("%s/%s/charge_full",
834 POWER_SUPPLY_SYSFS_PATH, name);
835 if (access(path, R_OK) == 0)
836 mHealthdConfig->batteryFullChargePath = path;
837 }
838
Todd Poynorf5d30122013-08-12 17:03:35 -0700839 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
840 path.clear();
841 path.appendFormat("%s/%s/current_now",
842 POWER_SUPPLY_SYSFS_PATH, name);
843 if (access(path, R_OK) == 0)
844 mHealthdConfig->batteryCurrentNowPath = path;
845 }
846
Ruchi Kandoicc338802015-08-24 13:01:16 -0700847 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
848 path.clear();
849 path.appendFormat("%s/%s/cycle_count",
850 POWER_SUPPLY_SYSFS_PATH, name);
851 if (access(path, R_OK) == 0)
852 mHealthdConfig->batteryCycleCountPath = path;
853 }
854
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800855 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) {
856 path.clear();
857 path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
858 if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path;
859 }
860
861 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) {
862 path.clear();
863 path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
864 if (access(path, R_OK) == 0)
865 mHealthdConfig->batteryChargeTimeToFullNowPath = path;
866 }
867
Stephane Lee1c108ed2020-02-10 18:23:57 -0800868 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty()) {
869 path.clear();
870 path.appendFormat("%s/%s/charge_full_design", POWER_SUPPLY_SYSFS_PATH, name);
871 if (access(path, R_OK) == 0)
872 mHealthdConfig->batteryFullChargeDesignCapacityUahPath = path;
873 }
874
Todd Poynorbc102112013-08-27 18:11:49 -0700875 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
876 path.clear();
877 path.appendFormat("%s/%s/current_avg",
878 POWER_SUPPLY_SYSFS_PATH, name);
879 if (access(path, R_OK) == 0)
880 mHealthdConfig->batteryCurrentAvgPath = path;
881 }
882
Todd Poynorf5d30122013-08-12 17:03:35 -0700883 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
884 path.clear();
885 path.appendFormat("%s/%s/charge_counter",
886 POWER_SUPPLY_SYSFS_PATH, name);
887 if (access(path, R_OK) == 0)
888 mHealthdConfig->batteryChargeCounterPath = path;
889 }
890
891 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
892 path.clear();
893 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
894 name);
895 if (access(path, R_OK) == 0) {
896 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700897 }
898 }
899
900 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
901 path.clear();
902 path.appendFormat("%s/%s/technology",
903 POWER_SUPPLY_SYSFS_PATH, name);
904 if (access(path, R_OK) == 0)
905 mHealthdConfig->batteryTechnologyPath = path;
906 }
907
Jack Wue561d032022-11-24 12:19:41 +0800908 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty()) {
909 path.clear();
910 path.appendFormat("%s/%s/state_of_health", POWER_SUPPLY_SYSFS_PATH, name);
911 if (access(path, R_OK) == 0) {
912 mHealthdConfig->batteryStateOfHealthPath = path;
913 } else {
914 path.clear();
915 path.appendFormat("%s/%s/health_index", POWER_SUPPLY_SYSFS_PATH, name);
916 if (access(path, R_OK) == 0)
917 mHealthdConfig->batteryStateOfHealthPath = path;
918 }
919 }
920
Jack Wub57f68a2023-02-04 19:56:06 +0800921 if (mHealthdConfig->batteryHealthStatusPath.isEmpty()) {
922 path.clear();
923 path.appendFormat("%s/%s/health_status", POWER_SUPPLY_SYSFS_PATH, name);
924 if (access(path, R_OK) == 0) mHealthdConfig->batteryHealthStatusPath = path;
925 }
926
Jack Wue561d032022-11-24 12:19:41 +0800927 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty()) {
928 path.clear();
929 path.appendFormat("%s/%s/manufacturing_date", POWER_SUPPLY_SYSFS_PATH, name);
930 if (access(path, R_OK) == 0)
931 mHealthdConfig->batteryManufacturingDatePath = path;
932 }
933
934 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty()) {
935 path.clear();
936 path.appendFormat("%s/%s/first_usage_date", POWER_SUPPLY_SYSFS_PATH, name);
937 if (access(path, R_OK) == 0) mHealthdConfig->batteryFirstUsageDatePath = path;
938 }
939
940 if (mHealthdConfig->chargingStatePath.isEmpty()) {
941 path.clear();
942 path.appendFormat("%s/%s/charging_state", POWER_SUPPLY_SYSFS_PATH, name);
943 if (access(path, R_OK) == 0) mHealthdConfig->chargingStatePath = path;
944 }
945
946 if (mHealthdConfig->chargingPolicyPath.isEmpty()) {
947 path.clear();
948 path.appendFormat("%s/%s/charging_policy", POWER_SUPPLY_SYSFS_PATH, name);
949 if (access(path, R_OK) == 0) mHealthdConfig->chargingPolicyPath = path;
950 }
951
Todd Poynor752faf22013-06-12 13:25:59 -0700952 break;
953
954 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
955 break;
956 }
Jack Wu06b90412021-12-15 20:40:21 +0800957
958 // Look for "is_dock" file
959 path.clear();
960 path.appendFormat("%s/%s/is_dock", POWER_SUPPLY_SYSFS_PATH, name);
961 if (access(path.string(), R_OK) == 0) {
962 path.clear();
963 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
964 if (access(path.string(), R_OK) == 0)
965 mChargerNames.add(String8(name));
966
967 }
Todd Poynor752faf22013-06-12 13:25:59 -0700968 }
Todd Poynor752faf22013-06-12 13:25:59 -0700969 }
970
Ian Pedowitz585ab652015-10-12 19:01:00 -0700971 // Typically the case for devices which do not have a battery and
972 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700973 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700974 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700975 hc->periodic_chores_interval_fast = -1;
976 hc->periodic_chores_interval_slow = -1;
977 } else {
978 if (mHealthdConfig->batteryStatusPath.isEmpty())
979 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
980 if (mHealthdConfig->batteryHealthPath.isEmpty())
981 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
982 if (mHealthdConfig->batteryPresentPath.isEmpty())
983 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
984 if (mHealthdConfig->batteryCapacityPath.isEmpty())
985 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
986 if (mHealthdConfig->batteryVoltagePath.isEmpty())
987 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
988 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
989 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
990 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
991 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700992 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700993 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
994 if (mHealthdConfig->batteryFullChargePath.isEmpty())
995 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
996 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
997 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Stephane Lee86f9f6a2019-12-19 15:09:41 -0800998 if (mHealthdConfig->batteryCapacityLevelPath.isEmpty())
999 KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
1000 if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
1001 KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
Stephane Lee1c108ed2020-02-10 18:23:57 -08001002 if (mHealthdConfig->batteryFullChargeDesignCapacityUahPath.isEmpty())
1003 KLOG_WARNING(LOG_TAG, "batteryFullChargeDesignCapacityUahPath. not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001004 if (mHealthdConfig->batteryStateOfHealthPath.isEmpty())
1005 KLOG_WARNING(LOG_TAG, "batteryStateOfHealthPath not found\n");
Jack Wub57f68a2023-02-04 19:56:06 +08001006 if (mHealthdConfig->batteryHealthStatusPath.isEmpty())
1007 KLOG_WARNING(LOG_TAG, "batteryHealthStatusPath not found\n");
Jack Wue561d032022-11-24 12:19:41 +08001008 if (mHealthdConfig->batteryManufacturingDatePath.isEmpty())
1009 KLOG_WARNING(LOG_TAG, "batteryManufacturingDatePath not found\n");
1010 if (mHealthdConfig->batteryFirstUsageDatePath.isEmpty())
1011 KLOG_WARNING(LOG_TAG, "batteryFirstUsageDatePath not found\n");
1012 if (mHealthdConfig->chargingStatePath.isEmpty())
1013 KLOG_WARNING(LOG_TAG, "chargingStatePath not found\n");
1014 if (mHealthdConfig->chargingPolicyPath.isEmpty())
1015 KLOG_WARNING(LOG_TAG, "chargingPolicyPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -07001016 }
Todd Poynor3db03a52014-05-21 16:28:13 -07001017
Ruchi Kandoia78fc232014-07-10 15:06:21 -07001018 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
1019 && strtol(pval, NULL, 10) != 0) {
1020 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
1021 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
1022 }
Todd Poynor752faf22013-06-12 13:25:59 -07001023}
1024
1025}; // namespace android