blob: 57ed362b20b5ad1e95e7231da6c8a00823971a4e [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>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070032
Michael Scott3217c5c2016-06-05 11:20:13 -070033#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070034#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070035#include <android-base/strings.h>
Todd Poynor752faf22013-06-12 13:25:59 -070036#include <batteryservice/BatteryService.h>
37#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070038#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070039#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070040#include <utils/String8.h>
41#include <utils/Vector.h>
42
43#define POWER_SUPPLY_SUBSYSTEM "power_supply"
44#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070045#define FAKE_BATTERY_CAPACITY 42
46#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080047#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070048#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070049
50namespace android {
51
52struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070053 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070054 int val;
55};
56
57static int mapSysfsString(const char* str,
58 struct sysfsStringEnumMap map[]) {
59 for (int i = 0; map[i].s; i++)
60 if (!strcmp(str, map[i].s))
61 return map[i].val;
62
63 return -1;
64}
65
Yabin Cuidb04a492016-02-16 17:19:23 -080066static void initBatteryProperties(BatteryProperties* props) {
67 props->chargerAcOnline = false;
68 props->chargerUsbOnline = false;
69 props->chargerWirelessOnline = false;
70 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070071 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080072 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
73 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
74 props->batteryPresent = false;
75 props->batteryLevel = 0;
76 props->batteryVoltage = 0;
77 props->batteryTemperature = 0;
78 props->batteryCurrent = 0;
79 props->batteryCycleCount = 0;
80 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070081 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080082 props->batteryTechnology.clear();
83}
84
Todd Poynore030a102018-01-19 14:03:59 -080085BatteryMonitor::BatteryMonitor()
86 : mHealthdConfig(nullptr),
87 mBatteryDevicePresent(false),
88 mBatteryFixedCapacity(0),
89 mBatteryFixedTemperature(0) {
Yabin Cuidb04a492016-02-16 17:19:23 -080090 initBatteryProperties(&props);
91}
92
Hridya Valsaraju7fa72252018-01-12 17:44:33 -080093struct BatteryProperties getBatteryProperties(BatteryMonitor* batteryMonitor) {
94 return batteryMonitor->props;
95}
96
Todd Poynor752faf22013-06-12 13:25:59 -070097int BatteryMonitor::getBatteryStatus(const char* status) {
98 int ret;
99 struct sysfsStringEnumMap batteryStatusMap[] = {
100 { "Unknown", BATTERY_STATUS_UNKNOWN },
101 { "Charging", BATTERY_STATUS_CHARGING },
102 { "Discharging", BATTERY_STATUS_DISCHARGING },
103 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
104 { "Full", BATTERY_STATUS_FULL },
105 { NULL, 0 },
106 };
107
108 ret = mapSysfsString(status, batteryStatusMap);
109 if (ret < 0) {
110 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
111 ret = BATTERY_STATUS_UNKNOWN;
112 }
113
114 return ret;
115}
116
117int BatteryMonitor::getBatteryHealth(const char* status) {
118 int ret;
119 struct sysfsStringEnumMap batteryHealthMap[] = {
120 { "Unknown", BATTERY_HEALTH_UNKNOWN },
121 { "Good", BATTERY_HEALTH_GOOD },
122 { "Overheat", BATTERY_HEALTH_OVERHEAT },
123 { "Dead", BATTERY_HEALTH_DEAD },
124 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
125 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
126 { "Cold", BATTERY_HEALTH_COLD },
Yueyao Zhu9bbfbf72017-06-12 16:55:38 -0700127 // battery health values from JEITA spec
128 { "Warm", BATTERY_HEALTH_GOOD },
129 { "Cool", BATTERY_HEALTH_GOOD },
130 { "Hot", BATTERY_HEALTH_OVERHEAT },
Todd Poynor752faf22013-06-12 13:25:59 -0700131 { NULL, 0 },
132 };
133
134 ret = mapSysfsString(status, batteryHealthMap);
135 if (ret < 0) {
136 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
137 ret = BATTERY_HEALTH_UNKNOWN;
138 }
139
140 return ret;
141}
142
Michael Scott3217c5c2016-06-05 11:20:13 -0700143int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800144 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700145 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700146 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700147 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700148}
149
150BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700151 std::string buf;
Yi Kong808e57e2018-03-02 11:27:02 -0500152 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700153 struct sysfsStringEnumMap supplyTypeMap[] = {
154 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
155 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
156 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
157 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
158 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
159 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100160 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700161 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
162 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700163 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
164 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
165 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700166 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
167 { NULL, 0 },
168 };
169
Michael Scott3217c5c2016-06-05 11:20:13 -0700170 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700171 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
172
Yi Kong808e57e2018-03-02 11:27:02 -0500173 ret = mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100174 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700175 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700176 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100177 }
Todd Poynor752faf22013-06-12 13:25:59 -0700178
Yi Kong808e57e2018-03-02 11:27:02 -0500179 return static_cast<BatteryMonitor::PowerSupplyType>(ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700180}
181
182bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700183 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700185
186 if (readFromFile(path, &buf) > 0)
187 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700188 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700189
190 return value;
191}
192
193int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700194 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700195 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700196
197 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700198 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700199
Todd Poynor752faf22013-06-12 13:25:59 -0700200 return value;
201}
202
Yifan Hong1353e702019-10-07 10:41:30 -0700203void BatteryMonitor::updateValues(void) {
Yabin Cuidb04a492016-02-16 17:19:23 -0800204 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700205
Todd Poynorf5d30122013-08-12 17:03:35 -0700206 if (!mHealthdConfig->batteryPresentPath.isEmpty())
207 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700208 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700209 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700210
Todd Poynor3db03a52014-05-21 16:28:13 -0700211 props.batteryLevel = mBatteryFixedCapacity ?
212 mBatteryFixedCapacity :
213 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700214 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700215
Ruchi Kandoicc338802015-08-24 13:01:16 -0700216 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
217 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
218
219 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
220 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
221
222 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
223 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
224
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700225 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
226 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
227
Todd Poynor3db03a52014-05-21 16:28:13 -0700228 props.batteryTemperature = mBatteryFixedTemperature ?
229 mBatteryFixedTemperature :
230 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700231
Michael Scott3217c5c2016-06-05 11:20:13 -0700232 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700233
Michael Scott3217c5c2016-06-05 11:20:13 -0700234 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
235 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700236
Michael Scott3217c5c2016-06-05 11:20:13 -0700237 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
238 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700239
Michael Scott3217c5c2016-06-05 11:20:13 -0700240 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
241 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700242
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700243 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700244
ShevT9d98a6a2018-07-26 11:47:47 +0300245 for (size_t i = 0; i < mChargerNames.size(); i++) {
Todd Poynor752faf22013-06-12 13:25:59 -0700246 String8 path;
247 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
248 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700249 if (getIntField(path)) {
250 path.clear();
251 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
252 mChargerNames[i].string());
253 switch(readPowerSupplyType(path)) {
254 case ANDROID_POWER_SUPPLY_TYPE_AC:
255 props.chargerAcOnline = true;
256 break;
257 case ANDROID_POWER_SUPPLY_TYPE_USB:
258 props.chargerUsbOnline = true;
259 break;
260 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
261 props.chargerWirelessOnline = true;
262 break;
263 default:
264 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
265 mChargerNames[i].string());
266 }
267 path.clear();
268 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
269 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700270 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700271 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
272
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700273 path.clear();
274 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
275 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700276
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700277 int ChargingVoltage =
278 (access(path.string(), R_OK) == 0) ? getIntField(path) :
279 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700280
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700281 double power = ((double)ChargingCurrent / MILLION) *
282 ((double)ChargingVoltage / MILLION);
283 if (MaxPower < power) {
284 props.maxChargingCurrent = ChargingCurrent;
285 props.maxChargingVoltage = ChargingVoltage;
286 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700287 }
288 }
289 }
Yifan Hong1353e702019-10-07 10:41:30 -0700290}
Todd Poynor752faf22013-06-12 13:25:59 -0700291
Yifan Hong1353e702019-10-07 10:41:30 -0700292void BatteryMonitor::logValues(void) {
293 char dmesgline[256];
294 size_t len;
295 if (props.batteryPresent) {
296 snprintf(dmesgline, sizeof(dmesgline), "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
297 props.batteryLevel, props.batteryVoltage, props.batteryTemperature < 0 ? "-" : "",
298 abs(props.batteryTemperature / 10), abs(props.batteryTemperature % 10),
299 props.batteryHealth, props.batteryStatus);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700300
Yifan Hong1353e702019-10-07 10:41:30 -0700301 len = strlen(dmesgline);
302 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
303 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " c=%d",
304 props.batteryCurrent);
Todd Poynor10b235e2013-08-07 15:25:14 -0700305 }
306
Yifan Hong1353e702019-10-07 10:41:30 -0700307 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
308 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " fc=%d",
309 props.batteryFullCharge);
310 }
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700311
Yifan Hong1353e702019-10-07 10:41:30 -0700312 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
313 len += snprintf(dmesgline + len, sizeof(dmesgline) - len, " cc=%d",
314 props.batteryCycleCount);
315 }
316 } else {
317 len = snprintf(dmesgline, sizeof(dmesgline), "battery none");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700318 }
319
Yifan Hong1353e702019-10-07 10:41:30 -0700320 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
321 props.chargerAcOnline ? "a" : "", props.chargerUsbOnline ? "u" : "",
322 props.chargerWirelessOnline ? "w" : "");
323
324 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
325}
326
327bool BatteryMonitor::isChargerOnline() {
Todd Poynor752faf22013-06-12 13:25:59 -0700328 return props.chargerAcOnline | props.chargerUsbOnline |
329 props.chargerWirelessOnline;
330}
331
Yabin Cuiaedf6032016-02-19 18:03:23 -0800332int BatteryMonitor::getChargeStatus() {
333 int result = BATTERY_STATUS_UNKNOWN;
334 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700335 std::string buf;
336 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
337 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800338 }
339 return result;
340}
341
Todd Poynorc133b712013-08-14 17:39:13 -0700342status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
343 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800344 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700345
Todd Poynor8f132af2014-05-08 17:15:45 -0700346 val->valueInt64 = LONG_MIN;
347
Todd Poynorc133b712013-08-14 17:39:13 -0700348 switch(id) {
349 case BATTERY_PROP_CHARGE_COUNTER:
350 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700351 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700352 getIntField(mHealthdConfig->batteryChargeCounterPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700353 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700354 } else {
355 ret = NAME_NOT_FOUND;
356 }
357 break;
358
359 case BATTERY_PROP_CURRENT_NOW:
360 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700361 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700362 getIntField(mHealthdConfig->batteryCurrentNowPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700363 ret = OK;
Todd Poynorc133b712013-08-14 17:39:13 -0700364 } else {
365 ret = NAME_NOT_FOUND;
366 }
367 break;
368
Todd Poynorbc102112013-08-27 18:11:49 -0700369 case BATTERY_PROP_CURRENT_AVG:
370 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700371 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700372 getIntField(mHealthdConfig->batteryCurrentAvgPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700373 ret = OK;
Todd Poynorbc102112013-08-27 18:11:49 -0700374 } else {
375 ret = NAME_NOT_FOUND;
376 }
377 break;
378
Paul Lawrence347c8de2014-03-19 15:04:40 -0700379 case BATTERY_PROP_CAPACITY:
380 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700381 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700382 getIntField(mHealthdConfig->batteryCapacityPath);
Elliott Hughes643268f2018-10-08 11:10:11 -0700383 ret = OK;
Paul Lawrence347c8de2014-03-19 15:04:40 -0700384 } else {
385 ret = NAME_NOT_FOUND;
386 }
387 break;
388
Todd Poynor8f132af2014-05-08 17:15:45 -0700389 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700390 if (mHealthdConfig->energyCounter) {
391 ret = mHealthdConfig->energyCounter(&val->valueInt64);
392 } else {
393 ret = NAME_NOT_FOUND;
394 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700395 break;
396
Jin Qian72adf112017-02-02 17:31:13 -0800397 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynore030a102018-01-19 14:03:59 -0800398 val->valueInt64 = getChargeStatus();
Elliott Hughes643268f2018-10-08 11:10:11 -0700399 ret = OK;
Jin Qian72adf112017-02-02 17:31:13 -0800400 break;
401
Todd Poynorc133b712013-08-14 17:39:13 -0700402 default:
403 break;
404 }
405
Todd Poynorc133b712013-08-14 17:39:13 -0700406 return ret;
407}
408
Todd Poynor020369d2013-09-18 20:09:33 -0700409void BatteryMonitor::dumpState(int fd) {
410 int v;
411 char vs[128];
412
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700413 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700414 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700415 props.chargerWirelessOnline, props.maxChargingCurrent,
416 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700417 write(fd, vs, strlen(vs));
418 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
419 props.batteryStatus, props.batteryHealth, props.batteryPresent);
420 write(fd, vs, strlen(vs));
421 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
422 props.batteryLevel, props.batteryVoltage,
423 props.batteryTemperature);
424 write(fd, vs, strlen(vs));
425
426 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
427 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
428 snprintf(vs, sizeof(vs), "current now: %d\n", v);
429 write(fd, vs, strlen(vs));
430 }
431
432 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
433 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
434 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
435 write(fd, vs, strlen(vs));
436 }
437
438 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
439 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
440 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
441 write(fd, vs, strlen(vs));
442 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700443
444 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
445 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
446 write(fd, vs, strlen(vs));
447 }
448
449 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
450 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
451 write(fd, vs, strlen(vs));
452 }
453
454 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
455 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
456 write(fd, vs, strlen(vs));
457 }
Todd Poynor020369d2013-09-18 20:09:33 -0700458}
459
Todd Poynorc7464c92013-09-10 12:40:00 -0700460void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700461 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700462 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700463
Todd Poynorf5d30122013-08-12 17:03:35 -0700464 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800465 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700466 if (dir == NULL) {
467 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
468 } else {
469 struct dirent* entry;
470
James Hawkins588a2ca2016-02-18 14:52:46 -0800471 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700472 const char* name = entry->d_name;
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800473 std::vector<String8>::iterator itIgnoreName;
Todd Poynor752faf22013-06-12 13:25:59 -0700474
475 if (!strcmp(name, ".") || !strcmp(name, ".."))
476 continue;
477
Thierry Strudelf73de6f2019-01-11 17:09:20 -0800478 itIgnoreName = find(hc->ignorePowerSupplyNames.begin(),
479 hc->ignorePowerSupplyNames.end(), String8(name));
480 if (itIgnoreName != hc->ignorePowerSupplyNames.end())
481 continue;
482
Todd Poynor752faf22013-06-12 13:25:59 -0700483 // Look for "type" file in each subdirectory
484 path.clear();
485 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
486 switch(readPowerSupplyType(path)) {
487 case ANDROID_POWER_SUPPLY_TYPE_AC:
488 case ANDROID_POWER_SUPPLY_TYPE_USB:
489 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
490 path.clear();
491 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
492 if (access(path.string(), R_OK) == 0)
493 mChargerNames.add(String8(name));
494 break;
495
496 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700497 mBatteryDevicePresent = true;
498
Todd Poynorf5d30122013-08-12 17:03:35 -0700499 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700500 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700501 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
502 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700503 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700504 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700505 }
506
Todd Poynorf5d30122013-08-12 17:03:35 -0700507 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700508 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700509 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
510 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700511 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700512 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700513 }
514
Todd Poynorf5d30122013-08-12 17:03:35 -0700515 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
516 path.clear();
517 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
518 name);
519 if (access(path, R_OK) == 0)
520 mHealthdConfig->batteryPresentPath = path;
521 }
522
523 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
524 path.clear();
525 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
526 name);
527 if (access(path, R_OK) == 0)
528 mHealthdConfig->batteryCapacityPath = path;
529 }
530
531 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
532 path.clear();
533 path.appendFormat("%s/%s/voltage_now",
534 POWER_SUPPLY_SYSFS_PATH, name);
535 if (access(path, R_OK) == 0) {
536 mHealthdConfig->batteryVoltagePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700537 }
538 }
539
Ruchi Kandoicc338802015-08-24 13:01:16 -0700540 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
541 path.clear();
542 path.appendFormat("%s/%s/charge_full",
543 POWER_SUPPLY_SYSFS_PATH, name);
544 if (access(path, R_OK) == 0)
545 mHealthdConfig->batteryFullChargePath = path;
546 }
547
Todd Poynorf5d30122013-08-12 17:03:35 -0700548 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
549 path.clear();
550 path.appendFormat("%s/%s/current_now",
551 POWER_SUPPLY_SYSFS_PATH, name);
552 if (access(path, R_OK) == 0)
553 mHealthdConfig->batteryCurrentNowPath = path;
554 }
555
Ruchi Kandoicc338802015-08-24 13:01:16 -0700556 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
557 path.clear();
558 path.appendFormat("%s/%s/cycle_count",
559 POWER_SUPPLY_SYSFS_PATH, name);
560 if (access(path, R_OK) == 0)
561 mHealthdConfig->batteryCycleCountPath = path;
562 }
563
Todd Poynorbc102112013-08-27 18:11:49 -0700564 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
565 path.clear();
566 path.appendFormat("%s/%s/current_avg",
567 POWER_SUPPLY_SYSFS_PATH, name);
568 if (access(path, R_OK) == 0)
569 mHealthdConfig->batteryCurrentAvgPath = path;
570 }
571
Todd Poynorf5d30122013-08-12 17:03:35 -0700572 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
573 path.clear();
574 path.appendFormat("%s/%s/charge_counter",
575 POWER_SUPPLY_SYSFS_PATH, name);
576 if (access(path, R_OK) == 0)
577 mHealthdConfig->batteryChargeCounterPath = path;
578 }
579
580 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
581 path.clear();
582 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
583 name);
584 if (access(path, R_OK) == 0) {
585 mHealthdConfig->batteryTemperaturePath = path;
Todd Poynorf5d30122013-08-12 17:03:35 -0700586 }
587 }
588
589 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
590 path.clear();
591 path.appendFormat("%s/%s/technology",
592 POWER_SUPPLY_SYSFS_PATH, name);
593 if (access(path, R_OK) == 0)
594 mHealthdConfig->batteryTechnologyPath = path;
595 }
596
Todd Poynor752faf22013-06-12 13:25:59 -0700597 break;
598
599 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
600 break;
601 }
602 }
Todd Poynor752faf22013-06-12 13:25:59 -0700603 }
604
Ian Pedowitz585ab652015-10-12 19:01:00 -0700605 // Typically the case for devices which do not have a battery and
606 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700607 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700608 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700609 hc->periodic_chores_interval_fast = -1;
610 hc->periodic_chores_interval_slow = -1;
611 } else {
612 if (mHealthdConfig->batteryStatusPath.isEmpty())
613 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
614 if (mHealthdConfig->batteryHealthPath.isEmpty())
615 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
616 if (mHealthdConfig->batteryPresentPath.isEmpty())
617 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
618 if (mHealthdConfig->batteryCapacityPath.isEmpty())
619 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
620 if (mHealthdConfig->batteryVoltagePath.isEmpty())
621 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
622 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
623 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
624 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
625 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700626 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700627 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
628 if (mHealthdConfig->batteryFullChargePath.isEmpty())
629 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
630 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
631 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700632 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700633
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700634 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
635 && strtol(pval, NULL, 10) != 0) {
636 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
637 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
638 }
Todd Poynor752faf22013-06-12 13:25:59 -0700639}
640
641}; // namespace android