blob: e80f36855430e9fa0b316954582b5c1017be2015 [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
Michael Scott3217c5c2016-06-05 11:20:13 -070034#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070035#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070036#include <android-base/strings.h>
Yifan Hong1d4368b2019-10-07 11:18:04 -070037#include <android/hardware/health/2.1/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070038#include <batteryservice/BatteryService.h>
39#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070040#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070041#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070042#include <utils/String8.h>
43#include <utils/Vector.h>
44
45#define POWER_SUPPLY_SUBSYSTEM "power_supply"
46#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070047#define FAKE_BATTERY_CAPACITY 42
48#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080049#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070050#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070051
Yifan Hong1d4368b2019-10-07 11:18:04 -070052using HealthInfo_1_0 = android::hardware::health::V1_0::HealthInfo;
53using HealthInfo_2_0 = android::hardware::health::V2_0::HealthInfo;
54using HealthInfo_2_1 = android::hardware::health::V2_1::HealthInfo;
55using android::hardware::health::V1_0::BatteryHealth;
56using android::hardware::health::V1_0::BatteryStatus;
Yifan Hong35cb0832019-10-07 13:58:29 -070057using android::hardware::health::V2_1::BatteryCapacityLevel;
Yifan Hong1d4368b2019-10-07 11:18:04 -070058
Todd Poynor752faf22013-06-12 13:25:59 -070059namespace android {
60
Yifan Hong1d4368b2019-10-07 11:18:04 -070061template <typename T>
62struct SysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070063 const char* s;
Yifan Hong1d4368b2019-10-07 11:18:04 -070064 T val;
Todd Poynor752faf22013-06-12 13:25:59 -070065};
66
Yifan Hong1d4368b2019-10-07 11:18:04 -070067template <typename T>
68static std::optional<T> mapSysfsString(const char* str, SysfsStringEnumMap<T> map[]) {
Todd Poynor752faf22013-06-12 13:25:59 -070069 for (int i = 0; map[i].s; i++)
70 if (!strcmp(str, map[i].s))
71 return map[i].val;
72
Yifan Hong1d4368b2019-10-07 11:18:04 -070073 return std::nullopt;
Yabin Cuidb04a492016-02-16 17:19:23 -080074}
75
Yifan Hong6cabe9b2019-11-05 17:04:50 -080076static void initHealthInfo(HealthInfo_2_1* health_info_2_1) {
77 *health_info_2_1 = HealthInfo_2_1{};
78
79 // HIDL enum values are zero initialized, so they need to be initialized
80 // properly.
81 health_info_2_1->batteryCapacityLevel = BatteryCapacityLevel::UNKNOWN;
82 auto* props = &health_info_2_1->legacy.legacy;
83 props->batteryStatus = BatteryStatus::UNKNOWN;
84 props->batteryHealth = BatteryHealth::UNKNOWN;
85}
86
Todd Poynore030a102018-01-19 14:03:59 -080087BatteryMonitor::BatteryMonitor()
88 : mHealthdConfig(nullptr),
89 mBatteryDevicePresent(false),
90 mBatteryFixedCapacity(0),
Yifan Hong1d4368b2019-10-07 11:18:04 -070091 mBatteryFixedTemperature(0),
Yifan Hong6cabe9b2019-11-05 17:04:50 -080092 mHealthInfo(std::make_unique<HealthInfo_2_1>()) {
93 initHealthInfo(mHealthInfo.get());
94}
Yifan Hong1d4368b2019-10-07 11:18:04 -070095
96BatteryMonitor::~BatteryMonitor() {}
97
98const HealthInfo_1_0& BatteryMonitor::getHealthInfo_1_0() const {
99 return getHealthInfo_2_0().legacy;
Yabin Cuidb04a492016-02-16 17:19:23 -0800100}
101
Yifan Hong1d4368b2019-10-07 11:18:04 -0700102const HealthInfo_2_0& BatteryMonitor::getHealthInfo_2_0() const {
103 return getHealthInfo_2_1().legacy;
Hridya Valsaraju7fa72252018-01-12 17:44:33 -0800104}
105
Yifan Hong1d4368b2019-10-07 11:18:04 -0700106const HealthInfo_2_1& BatteryMonitor::getHealthInfo_2_1() const {
107 return *mHealthInfo;
108}
109
110BatteryStatus getBatteryStatus(const char* status) {
111 static SysfsStringEnumMap<BatteryStatus> batteryStatusMap[] = {
112 {"Unknown", BatteryStatus::UNKNOWN},
113 {"Charging", BatteryStatus::CHARGING},
114 {"Discharging", BatteryStatus::DISCHARGING},
115 {"Not charging", BatteryStatus::NOT_CHARGING},
116 {"Full", BatteryStatus::FULL},
117 {NULL, BatteryStatus::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700118 };
119
Yifan Hong1d4368b2019-10-07 11:18:04 -0700120 auto ret = mapSysfsString(status, batteryStatusMap);
121 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700122 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700123 *ret = BatteryStatus::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700124 }
125
Yifan Hong1d4368b2019-10-07 11:18:04 -0700126 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700127}
128
Yifan Hong1d4368b2019-10-07 11:18:04 -0700129BatteryHealth getBatteryHealth(const char* status) {
130 static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
131 {"Unknown", BatteryHealth::UNKNOWN},
132 {"Good", BatteryHealth::GOOD},
133 {"Overheat", BatteryHealth::OVERHEAT},
134 {"Dead", BatteryHealth::DEAD},
135 {"Over voltage", BatteryHealth::OVER_VOLTAGE},
136 {"Unspecified failure", BatteryHealth::UNSPECIFIED_FAILURE},
137 {"Cold", BatteryHealth::COLD},
138 // battery health values from JEITA spec
139 {"Warm", BatteryHealth::GOOD},
140 {"Cool", BatteryHealth::GOOD},
141 {"Hot", BatteryHealth::OVERHEAT},
142 {NULL, BatteryHealth::UNKNOWN},
Todd Poynor752faf22013-06-12 13:25:59 -0700143 };
144
Yifan Hong1d4368b2019-10-07 11:18:04 -0700145 auto ret = mapSysfsString(status, batteryHealthMap);
146 if (!ret) {
Todd Poynor752faf22013-06-12 13:25:59 -0700147 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
Yifan Hong1d4368b2019-10-07 11:18:04 -0700148 *ret = BatteryHealth::UNKNOWN;
Todd Poynor752faf22013-06-12 13:25:59 -0700149 }
150
Yifan Hong1d4368b2019-10-07 11:18:04 -0700151 return *ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700152}
153
Michael Scott3217c5c2016-06-05 11:20:13 -0700154int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800155 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700156 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700157 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700158 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700159}
160
161BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700162 static SysfsStringEnumMap<int> supplyTypeMap[] = {
163 {"Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN},
164 {"Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY},
165 {"UPS", ANDROID_POWER_SUPPLY_TYPE_AC},
166 {"Mains", ANDROID_POWER_SUPPLY_TYPE_AC},
167 {"USB", ANDROID_POWER_SUPPLY_TYPE_USB},
168 {"USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC},
169 {"USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC},
170 {"USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC},
171 {"USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC},
172 {"USB_C", ANDROID_POWER_SUPPLY_TYPE_AC},
173 {"USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC},
174 {"USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB},
175 {"Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS},
176 {NULL, 0},
Todd Poynor752faf22013-06-12 13:25:59 -0700177 };
Yifan Hong1d4368b2019-10-07 11:18:04 -0700178 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700179
Michael Scott3217c5c2016-06-05 11:20:13 -0700180 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700181 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
182
Yifan Hong1d4368b2019-10-07 11:18:04 -0700183 auto ret = mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100184 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700185 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700186 *ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100187 }
Todd Poynor752faf22013-06-12 13:25:59 -0700188
Yifan Hong1d4368b2019-10-07 11:18:04 -0700189 return static_cast<BatteryMonitor::PowerSupplyType>(*ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700190}
191
192bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700193 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700194 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700195
196 if (readFromFile(path, &buf) > 0)
197 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700198 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700199
200 return value;
201}
202
203int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700204 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700205 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700206
207 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700208 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700209
Todd Poynor752faf22013-06-12 13:25:59 -0700210 return value;
211}
212
Yifan Hong1353e702019-10-07 10:41:30 -0700213void BatteryMonitor::updateValues(void) {
Yifan Hong6cabe9b2019-11-05 17:04:50 -0800214 initHealthInfo(mHealthInfo.get());
Yifan Hong1d4368b2019-10-07 11:18:04 -0700215
216 HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor752faf22013-06-12 13:25:59 -0700217
Todd Poynorf5d30122013-08-12 17:03:35 -0700218 if (!mHealthdConfig->batteryPresentPath.isEmpty())
219 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700220 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700221 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700222
Todd Poynor3db03a52014-05-21 16:28:13 -0700223 props.batteryLevel = mBatteryFixedCapacity ?
224 mBatteryFixedCapacity :
225 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700226 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700227
Ruchi Kandoicc338802015-08-24 13:01:16 -0700228 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
229 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
230
231 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
232 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
233
234 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
235 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
236
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700237 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
238 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
239
Yifan Hong35cb0832019-10-07 13:58:29 -0700240 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty())
241 mHealthInfo->legacy.batteryCurrentAverage =
242 getIntField(mHealthdConfig->batteryCurrentAvgPath);
243
244 // TODO(b/142260281): Retrieve these values correctly.
245 mHealthInfo->batteryCapacityLevel = BatteryCapacityLevel::UNKNOWN;
246 mHealthInfo->batteryChargeTimeToFullNowSeconds = 0;
247 mHealthInfo->batteryFullCapacityUah = props.batteryFullCharge;
248
Todd Poynor3db03a52014-05-21 16:28:13 -0700249 props.batteryTemperature = mBatteryFixedTemperature ?
250 mBatteryFixedTemperature :
251 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700252
Michael Scott3217c5c2016-06-05 11:20:13 -0700253 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700254
Michael Scott3217c5c2016-06-05 11:20:13 -0700255 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
256 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700257
Michael Scott3217c5c2016-06-05 11:20:13 -0700258 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
259 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700260
Michael Scott3217c5c2016-06-05 11:20:13 -0700261 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
262 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700263
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700264 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700265
ShevT9d98a6a2018-07-26 11:47:47 +0300266 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700267 String8 path;
268 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
269 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700270 if (getIntField(path)) {
271 path.clear();
272 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
273 mChargerNames[i].string());
274 switch(readPowerSupplyType(path)) {
275 case ANDROID_POWER_SUPPLY_TYPE_AC:
276 props.chargerAcOnline = true;
277 break;
278 case ANDROID_POWER_SUPPLY_TYPE_USB:
279 props.chargerUsbOnline = true;
280 break;
281 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
282 props.chargerWirelessOnline = true;
283 break;
284 default:
285 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
286 mChargerNames[i].string());
287 }
288 path.clear();
289 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
290 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700291 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700292 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
293
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700294 path.clear();
295 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
296 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700297
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700298 int ChargingVoltage =
299 (access(path.string(), R_OK) == 0) ? getIntField(path) :
300 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700301
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700302 double power = ((double)ChargingCurrent / MILLION) *
303 ((double)ChargingVoltage / MILLION);
304 if (MaxPower < power) {
305 props.maxChargingCurrent = ChargingCurrent;
306 props.maxChargingVoltage = ChargingVoltage;
307 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700308 }
309 }
310 }
Yifan Hong1353e702019-10-07 10:41:30 -0700311}
Todd Poynor752faf22013-06-12 13:25:59 -0700312
Yifan Hong1353e702019-10-07 10:41:30 -0700313void BatteryMonitor::logValues(void) {
314 char dmesgline[256];
315 size_t len;
Yifan Hong1d4368b2019-10-07 11:18:04 -0700316 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Yifan Hong1353e702019-10-07 10:41:30 -0700317 if (props.batteryPresent) {
318 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
319 props.batteryLevel, props.batteryVoltage, props.batteryTemperature < 0 ? "-" : "",
320 abs(props.batteryTemperature / 10), abs(props.batteryTemperature % 10),
321 props.batteryHealth, props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700322
Yifan Hong1353e702019-10-07 10:41:30 -0700323 len = strlen(dmesgline);
324 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
325 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
326 props.batteryCurrent);
Todd Poynor10b235e2013-08-07 15:25:14 -0700327 }
328
Yifan Hong1353e702019-10-07 10:41:30 -0700329 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
330 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
331 props.batteryFullCharge);
332 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700333
Yifan Hong1353e702019-10-07 10:41:30 -0700334 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
335 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
336 props.batteryCycleCount);
337 }
338 } else {
339 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700340 }
341
Yifan Hong1353e702019-10-07 10:41:30 -0700342 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
343 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
344 props.chargerWirelessOnline ? "w" : "");
345
346 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
347}
348
349bool BatteryMonitor::isChargerOnline() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700350 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor752faf22013-06-12 13:25:59 -0700351 return props.chargerAcOnline | props.chargerUsbOnline |
352 props.chargerWirelessOnline;
353}
354
Yabin Cuiaedf6032016-02-19 18:03:23 -0800355int BatteryMonitor::getChargeStatus() {
Yifan Hong1d4368b2019-10-07 11:18:04 -0700356 BatteryStatus result = BatteryStatus::UNKNOWN;
Yabin Cuiaedf6032016-02-19 18:03:23 -0800357 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700358 std::string buf;
359 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
360 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800361 }
Yifan Hong1d4368b2019-10-07 11:18:04 -0700362 return static_cast<int>(result);
Yabin Cuiaedf6032016-02-19 18:03:23 -0800363}
364
Todd Poynorc133b712013-08-14 17:39:13 -0700365status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
366 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800367 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700368
Todd Poynor8f132af2014-05-08 17:15:45 -0700369 val->valueInt64 = LONG_MIN;
370
Todd Poynorc133b712013-08-14 17:39:13 -0700371 switch(id) {
372 case BATTERY_PROP_CHARGE_COUNTER:
373 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700374 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700375 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700376 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700377 } else {
378 ret = NAME_NOT_FOUND;
379 }
380 break;
381
382 case BATTERY_PROP_CURRENT_NOW:
383 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700384 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700385 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700386 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700387 } else {
388 ret = NAME_NOT_FOUND;
389 }
390 break;
391
Todd Poynorbc102112013-08-27 18:11:49 -0700392 case BATTERY_PROP_CURRENT_AVG:
393 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700394 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700395 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700396 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700397 } else {
398 ret = NAME_NOT_FOUND;
399 }
400 break;
401
Paul Lawrence347c8de2014-03-19 15:04:40 -0700402 case BATTERY_PROP_CAPACITY:
403 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700404 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700405 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700406 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700407 } else {
408 ret = NAME_NOT_FOUND;
409 }
410 break;
411
Todd Poynor8f132af2014-05-08 17:15:45 -0700412 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700413 if (mHealthdConfig->energyCounter) {
414 ret = mHealthdConfig->energyCounter(&val->valueInt64);
415 } else {
416 ret = NAME_NOT_FOUND;
417 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700418 break;
419
Jin Qian72adf112017-02-02 17:31:13 -0800420 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800421 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700422 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800423 break;
424
Todd Poynorc133b712013-08-14 17:39:13 -0700425 default:
426 break;
427 }
428
Todd Poynorc133b712013-08-14 17:39:13 -0700429 return ret;
430}
431
Todd Poynor020369d2013-09-18 20:09:33 -0700432void BatteryMonitor::dumpState(int fd) {
433 int v;
434 char vs[128];
Yifan Hong1d4368b2019-10-07 11:18:04 -0700435 const HealthInfo_1_0& props = mHealthInfo->legacy.legacy;
Todd Poynor020369d2013-09-18 20:09:33 -0700436
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700437 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700438 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700439 props.chargerWirelessOnline, props.maxChargingCurrent,
440 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700441 write(fd, vs, strlen(vs));
442 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
443 props.batteryStatus, props.batteryHealth, props.batteryPresent);
444 write(fd, vs, strlen(vs));
445 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
446 props.batteryLevel, props.batteryVoltage,
447 props.batteryTemperature);
448 write(fd, vs, strlen(vs));
449
450 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
451 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
452 snprintf(vs, sizeof(vs), "current now: %d\n", v);
453 write(fd, vs, strlen(vs));
454 }
455
456 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
457 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
458 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
459 write(fd, vs, strlen(vs));
460 }
461
462 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
463 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
464 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
465 write(fd, vs, strlen(vs));
466 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700467
468 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
469 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
470 write(fd, vs, strlen(vs));
471 }
472
473 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
474 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
475 write(fd, vs, strlen(vs));
476 }
477
478 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
479 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
480 write(fd, vs, strlen(vs));
481 }
Todd Poynor020369d2013-09-18 20:09:33 -0700482}
483
Todd Poynorc7464c92013-09-10 12:40:00 -0700484void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700485 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700486 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700487
Todd Poynorf5d30122013-08-12 17:03:35 -0700488 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800489 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700490 if (dir == NULL) {
491 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
492 } else {
493 struct dirent* entry;
494
James Hawkins588a2ca2016-02-18 14:52:46 -0800495 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700496 const char* name = entry->d_name;
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800497 std::vector<String8>::iterator itIgnoreName;
Todd Poynor752faf22013-06-12 13:25:59 -0700498
499 if (!strcmp(name, ".") || !strcmp(name, ".."))
500 continue;
501
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800502 itIgnoreName = find(hc->ignorePowerSupplyNames.begin(),
503 hc->ignorePowerSupplyNames.end(), String8(name));
504 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
505 continue;
506
Todd Poynor752faf22013-06-12 13:25:59 -0700507 // Look for "type" file in each subdirectory
508 path.clear();
509 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
510 switch(readPowerSupplyType(path)) {
511 case ANDROID_POWER_SUPPLY_TYPE_AC:
512 case ANDROID_POWER_SUPPLY_TYPE_USB:
513 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
514 path.clear();
515 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
516 if (access(path.string(), R_OK) == 0)
517 mChargerNames.add(String8(name));
518 break;
519
520 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700521 mBatteryDevicePresent = true;
522
Todd Poynorf5d30122013-08-12 17:03:35 -0700523 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700524 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700525 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
526 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700527 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700528 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700529 }
530
Todd Poynorf5d30122013-08-12 17:03:35 -0700531 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700532 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700533 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
534 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700535 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700536 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700537 }
538
Todd Poynorf5d30122013-08-12 17:03:35 -0700539 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
540 path.clear();
541 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
542 name);
543 if (access(path, R_OK) == 0)
544 mHealthdConfig->batteryPresentPath = path;
545 }
546
547 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
548 path.clear();
549 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
550 name);
551 if (access(path, R_OK) == 0)
552 mHealthdConfig->batteryCapacityPath = path;
553 }
554
555 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
556 path.clear();
557 path.appendFormat("%s/%s/voltage_now",
558 POWER_SUPPLY_SYSFS_PATH, name);
559 if (access(path, R_OK) == 0) {
560 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700561 }
562 }
563
Ruchi Kandoicc338802015-08-24 13:01:16 -0700564 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
565 path.clear();
566 path.appendFormat("%s/%s/charge_full",
567 POWER_SUPPLY_SYSFS_PATH, name);
568 if (access(path, R_OK) == 0)
569 mHealthdConfig->batteryFullChargePath = path;
570 }
571
Todd Poynorf5d30122013-08-12 17:03:35 -0700572 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
573 path.clear();
574 path.appendFormat("%s/%s/current_now",
575 POWER_SUPPLY_SYSFS_PATH, name);
576 if (access(path, R_OK) == 0)
577 mHealthdConfig->batteryCurrentNowPath = path;
578 }
579
Ruchi Kandoicc338802015-08-24 13:01:16 -0700580 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
581 path.clear();
582 path.appendFormat("%s/%s/cycle_count",
583 POWER_SUPPLY_SYSFS_PATH, name);
584 if (access(path, R_OK) == 0)
585 mHealthdConfig->batteryCycleCountPath = path;
586 }
587
Todd Poynorbc102112013-08-27 18:11:49 -0700588 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
589 path.clear();
590 path.appendFormat("%s/%s/current_avg",
591 POWER_SUPPLY_SYSFS_PATH, name);
592 if (access(path, R_OK) == 0)
593 mHealthdConfig->batteryCurrentAvgPath = path;
594 }
595
Todd Poynorf5d30122013-08-12 17:03:35 -0700596 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
597 path.clear();
598 path.appendFormat("%s/%s/charge_counter",
599 POWER_SUPPLY_SYSFS_PATH, name);
600 if (access(path, R_OK) == 0)
601 mHealthdConfig->batteryChargeCounterPath = path;
602 }
603
604 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
605 path.clear();
606 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
607 name);
608 if (access(path, R_OK) == 0) {
609 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700610 }
611 }
612
613 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
614 path.clear();
615 path.appendFormat("%s/%s/technology",
616 POWER_SUPPLY_SYSFS_PATH, name);
617 if (access(path, R_OK) == 0)
618 mHealthdConfig->batteryTechnologyPath = path;
619 }
620
Todd Poynor752faf22013-06-12 13:25:59 -0700621 break;
622
623 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
624 break;
625 }
626 }
Todd Poynor752faf22013-06-12 13:25:59 -0700627 }
628
Ian Pedowitz585ab652015-10-12 19:01:00 -0700629 // Typically the case for devices which do not have a battery and
630 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700631 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700632 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700633 hc->periodic_chores_interval_fast = -1;
634 hc->periodic_chores_interval_slow = -1;
635 } else {
636 if (mHealthdConfig->batteryStatusPath.isEmpty())
637 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
638 if (mHealthdConfig->batteryHealthPath.isEmpty())
639 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
640 if (mHealthdConfig->batteryPresentPath.isEmpty())
641 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
642 if (mHealthdConfig->batteryCapacityPath.isEmpty())
643 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
644 if (mHealthdConfig->batteryVoltagePath.isEmpty())
645 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
646 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
647 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
648 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
649 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700650 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700651 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
652 if (mHealthdConfig->batteryFullChargePath.isEmpty())
653 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
654 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
655 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700656 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700657
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700658 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
659 && strtol(pval, NULL, 10) != 0) {
660 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
661 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
662 }
Todd Poynor752faf22013-06-12 13:25:59 -0700663}
664
665}; // namespace android