blob: fa79d0bb2093887db709f8dae0f5bfce97fd03a9 [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>
James Hawkins588a2ca2016-02-18 14:52:46 -080029#include <memory>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070030
Michael Scott3217c5c2016-06-05 11:20:13 -070031#include <android-base/file.h>
Elliott Hughesda46b392016-10-11 17:09:00 -070032#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070033#include <android-base/strings.h>
Todd Poynor752faf22013-06-12 13:25:59 -070034#include <batteryservice/BatteryService.h>
35#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070036#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070037#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070038#include <utils/String8.h>
39#include <utils/Vector.h>
40
41#define POWER_SUPPLY_SUBSYSTEM "power_supply"
42#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070043#define FAKE_BATTERY_CAPACITY 42
44#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080045#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070046#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070047
48namespace android {
49
50struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070051 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070052 int val;
53};
54
55static int mapSysfsString(const char* str,
56 struct sysfsStringEnumMap map[]) {
57 for (int i = 0; map[i].s; i++)
58 if (!strcmp(str, map[i].s))
59 return map[i].val;
60
61 return -1;
62}
63
Yabin Cuidb04a492016-02-16 17:19:23 -080064static void initBatteryProperties(BatteryProperties* props) {
65 props->chargerAcOnline = false;
66 props->chargerUsbOnline = false;
67 props->chargerWirelessOnline = false;
68 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070069 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080070 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
71 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
72 props->batteryPresent = false;
73 props->batteryLevel = 0;
74 props->batteryVoltage = 0;
75 props->batteryTemperature = 0;
76 props->batteryCurrent = 0;
77 props->batteryCycleCount = 0;
78 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070079 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080080 props->batteryTechnology.clear();
81}
82
Todd Poynor4d7ee2e2017-11-06 18:14:25 -080083BatteryMonitor::BatteryMonitor()
84 : mHealthdConfig(nullptr),
85 mBatteryDevicePresent(false),
86 mBatteryFixedCapacity(0),
87 mBatteryFixedTemperature(0) {
Yabin Cuidb04a492016-02-16 17:19:23 -080088 initBatteryProperties(&props);
89}
90
Todd Poynor752faf22013-06-12 13:25:59 -070091int BatteryMonitor::getBatteryStatus(const char* status) {
92 int ret;
93 struct sysfsStringEnumMap batteryStatusMap[] = {
94 { "Unknown", BATTERY_STATUS_UNKNOWN },
95 { "Charging", BATTERY_STATUS_CHARGING },
96 { "Discharging", BATTERY_STATUS_DISCHARGING },
97 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
98 { "Full", BATTERY_STATUS_FULL },
99 { NULL, 0 },
100 };
101
102 ret = mapSysfsString(status, batteryStatusMap);
103 if (ret < 0) {
104 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
105 ret = BATTERY_STATUS_UNKNOWN;
106 }
107
108 return ret;
109}
110
111int BatteryMonitor::getBatteryHealth(const char* status) {
112 int ret;
113 struct sysfsStringEnumMap batteryHealthMap[] = {
114 { "Unknown", BATTERY_HEALTH_UNKNOWN },
115 { "Good", BATTERY_HEALTH_GOOD },
116 { "Overheat", BATTERY_HEALTH_OVERHEAT },
117 { "Dead", BATTERY_HEALTH_DEAD },
118 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
119 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
120 { "Cold", BATTERY_HEALTH_COLD },
Yueyao Zhu9bbfbf72017-06-12 16:55:38 -0700121 // battery health values from JEITA spec
122 { "Warm", BATTERY_HEALTH_GOOD },
123 { "Cool", BATTERY_HEALTH_GOOD },
124 { "Hot", BATTERY_HEALTH_OVERHEAT },
Todd Poynor752faf22013-06-12 13:25:59 -0700125 { NULL, 0 },
126 };
127
128 ret = mapSysfsString(status, batteryHealthMap);
129 if (ret < 0) {
130 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
131 ret = BATTERY_HEALTH_UNKNOWN;
132 }
133
134 return ret;
135}
136
Michael Scott3217c5c2016-06-05 11:20:13 -0700137int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800138 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700139 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700140 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700141 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700142}
143
144BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700145 std::string buf;
Yi Kong808e57e2018-03-02 11:27:02 -0500146 int ret;
Todd Poynor752faf22013-06-12 13:25:59 -0700147 struct sysfsStringEnumMap supplyTypeMap[] = {
148 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
149 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
150 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
151 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
152 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
153 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100154 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700155 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
156 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700157 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
158 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
159 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700160 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
161 { NULL, 0 },
162 };
163
Michael Scott3217c5c2016-06-05 11:20:13 -0700164 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700165 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
166
Yi Kong808e57e2018-03-02 11:27:02 -0500167 ret = mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100168 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700169 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700170 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100171 }
Todd Poynor752faf22013-06-12 13:25:59 -0700172
Yi Kong808e57e2018-03-02 11:27:02 -0500173 return static_cast<BatteryMonitor::PowerSupplyType>(ret);
Todd Poynor752faf22013-06-12 13:25:59 -0700174}
175
176bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700177 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700178 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700179
180 if (readFromFile(path, &buf) > 0)
181 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700182 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700183
184 return value;
185}
186
187int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700188 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700189 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700190
191 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700192 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700193
Todd Poynor752faf22013-06-12 13:25:59 -0700194 return value;
195}
196
197bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700198 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700199
Yabin Cuidb04a492016-02-16 17:19:23 -0800200 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700201
Todd Poynorf5d30122013-08-12 17:03:35 -0700202 if (!mHealthdConfig->batteryPresentPath.isEmpty())
203 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700204 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700205 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700206
Todd Poynor3db03a52014-05-21 16:28:13 -0700207 props.batteryLevel = mBatteryFixedCapacity ?
208 mBatteryFixedCapacity :
209 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700210 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700211
Ruchi Kandoicc338802015-08-24 13:01:16 -0700212 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
213 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
214
215 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
216 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
217
218 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
219 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
220
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700221 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
222 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
223
Todd Poynor3db03a52014-05-21 16:28:13 -0700224 props.batteryTemperature = mBatteryFixedTemperature ?
225 mBatteryFixedTemperature :
226 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700227
Michael Scott3217c5c2016-06-05 11:20:13 -0700228 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700229
Michael Scott3217c5c2016-06-05 11:20:13 -0700230 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
231 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700232
Michael Scott3217c5c2016-06-05 11:20:13 -0700233 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
234 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700235
Michael Scott3217c5c2016-06-05 11:20:13 -0700236 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
237 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700238
239 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700240 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700241
242 for (i = 0; i < mChargerNames.size(); i++) {
243 String8 path;
244 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
245 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700246 if (getIntField(path)) {
247 path.clear();
248 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
249 mChargerNames[i].string());
250 switch(readPowerSupplyType(path)) {
251 case ANDROID_POWER_SUPPLY_TYPE_AC:
252 props.chargerAcOnline = true;
253 break;
254 case ANDROID_POWER_SUPPLY_TYPE_USB:
255 props.chargerUsbOnline = true;
256 break;
257 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
258 props.chargerWirelessOnline = true;
259 break;
260 default:
261 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
262 mChargerNames[i].string());
263 }
264 path.clear();
265 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
266 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700267 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700268 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
269
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700270 path.clear();
271 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
272 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700273
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700274 int ChargingVoltage =
275 (access(path.string(), R_OK) == 0) ? getIntField(path) :
276 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700277
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700278 double power = ((double)ChargingCurrent / MILLION) *
279 ((double)ChargingVoltage / MILLION);
280 if (MaxPower < power) {
281 props.maxChargingCurrent = ChargingCurrent;
282 props.maxChargingVoltage = ChargingVoltage;
283 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700284 }
285 }
286 }
287
Todd Poynor10b235e2013-08-07 15:25:14 -0700288 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700289
Todd Poynor10b235e2013-08-07 15:25:14 -0700290 if (logthis) {
291 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700292 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700293 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700294 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700295 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
296 props.batteryLevel, props.batteryVoltage,
297 props.batteryTemperature < 0 ? "-" : "",
298 abs(props.batteryTemperature / 10),
299 abs(props.batteryTemperature % 10), props.batteryHealth,
300 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700301
Ruchi Kandoicc338802015-08-24 13:01:16 -0700302 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700303 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700304 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
305 " c=%d", props.batteryCurrent);
306 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700307
Ruchi Kandoicc338802015-08-24 13:01:16 -0700308 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
309 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
310 " fc=%d", props.batteryFullCharge);
311 }
312
313 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
314 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
315 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700316 }
317 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800318 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700319 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700320 }
321
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700322 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
323 props.chargerAcOnline ? "a" : "",
324 props.chargerUsbOnline ? "u" : "",
325 props.chargerWirelessOnline ? "w" : "");
326
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700327 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700328 }
329
Todd Poynorc7464c92013-09-10 12:40:00 -0700330 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700331 return props.chargerAcOnline | props.chargerUsbOnline |
332 props.chargerWirelessOnline;
333}
334
Yabin Cuiaedf6032016-02-19 18:03:23 -0800335int BatteryMonitor::getChargeStatus() {
336 int result = BATTERY_STATUS_UNKNOWN;
337 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700338 std::string buf;
339 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
340 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800341 }
342 return result;
343}
344
Todd Poynorc133b712013-08-14 17:39:13 -0700345status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
346 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800347 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700348
Todd Poynor8f132af2014-05-08 17:15:45 -0700349 val->valueInt64 = LONG_MIN;
350
Todd Poynorc133b712013-08-14 17:39:13 -0700351 switch(id) {
352 case BATTERY_PROP_CHARGE_COUNTER:
353 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700354 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700355 getIntField(mHealthdConfig->batteryChargeCounterPath);
356 ret = NO_ERROR;
357 } else {
358 ret = NAME_NOT_FOUND;
359 }
360 break;
361
362 case BATTERY_PROP_CURRENT_NOW:
363 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700364 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700365 getIntField(mHealthdConfig->batteryCurrentNowPath);
366 ret = NO_ERROR;
367 } else {
368 ret = NAME_NOT_FOUND;
369 }
370 break;
371
Todd Poynorbc102112013-08-27 18:11:49 -0700372 case BATTERY_PROP_CURRENT_AVG:
373 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700374 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700375 getIntField(mHealthdConfig->batteryCurrentAvgPath);
376 ret = NO_ERROR;
377 } else {
378 ret = NAME_NOT_FOUND;
379 }
380 break;
381
Paul Lawrence347c8de2014-03-19 15:04:40 -0700382 case BATTERY_PROP_CAPACITY:
383 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700384 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700385 getIntField(mHealthdConfig->batteryCapacityPath);
386 ret = NO_ERROR;
387 } else {
388 ret = NAME_NOT_FOUND;
389 }
390 break;
391
Todd Poynor8f132af2014-05-08 17:15:45 -0700392 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700393 if (mHealthdConfig->energyCounter) {
394 ret = mHealthdConfig->energyCounter(&val->valueInt64);
395 } else {
396 ret = NAME_NOT_FOUND;
397 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700398 break;
399
Jin Qian72adf112017-02-02 17:31:13 -0800400 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynor4d7ee2e2017-11-06 18:14:25 -0800401 val->valueInt64 = getChargeStatus();
Jin Qian72adf112017-02-02 17:31:13 -0800402 ret = NO_ERROR;
403 break;
404
Todd Poynorc133b712013-08-14 17:39:13 -0700405 default:
406 break;
407 }
408
Todd Poynorc133b712013-08-14 17:39:13 -0700409 return ret;
410}
411
Todd Poynor020369d2013-09-18 20:09:33 -0700412void BatteryMonitor::dumpState(int fd) {
413 int v;
414 char vs[128];
415
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700416 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700417 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700418 props.chargerWirelessOnline, props.maxChargingCurrent,
419 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700420 write(fd, vs, strlen(vs));
421 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
422 props.batteryStatus, props.batteryHealth, props.batteryPresent);
423 write(fd, vs, strlen(vs));
424 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
425 props.batteryLevel, props.batteryVoltage,
426 props.batteryTemperature);
427 write(fd, vs, strlen(vs));
428
429 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
430 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
431 snprintf(vs, sizeof(vs), "current now: %d\n", v);
432 write(fd, vs, strlen(vs));
433 }
434
435 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
436 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
437 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
438 write(fd, vs, strlen(vs));
439 }
440
441 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
442 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
443 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
444 write(fd, vs, strlen(vs));
445 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700446
447 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
448 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
449 write(fd, vs, strlen(vs));
450 }
451
452 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
453 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
454 write(fd, vs, strlen(vs));
455 }
456
457 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
458 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
459 write(fd, vs, strlen(vs));
460 }
Todd Poynor020369d2013-09-18 20:09:33 -0700461}
462
Todd Poynorc7464c92013-09-10 12:40:00 -0700463void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700464 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700465 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700466
Todd Poynorf5d30122013-08-12 17:03:35 -0700467 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800468 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700469 if (dir == NULL) {
470 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
471 } else {
472 struct dirent* entry;
473
James Hawkins588a2ca2016-02-18 14:52:46 -0800474 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700475 const char* name = entry->d_name;
476
477 if (!strcmp(name, ".") || !strcmp(name, ".."))
478 continue;
479
Todd Poynor752faf22013-06-12 13:25:59 -0700480 // Look for "type" file in each subdirectory
481 path.clear();
482 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
483 switch(readPowerSupplyType(path)) {
484 case ANDROID_POWER_SUPPLY_TYPE_AC:
485 case ANDROID_POWER_SUPPLY_TYPE_USB:
486 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
487 path.clear();
488 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
489 if (access(path.string(), R_OK) == 0)
490 mChargerNames.add(String8(name));
491 break;
492
493 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700494 mBatteryDevicePresent = true;
495
Todd Poynorf5d30122013-08-12 17:03:35 -0700496 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700497 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700498 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
499 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700500 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700501 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700502 }
503
Todd Poynorf5d30122013-08-12 17:03:35 -0700504 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700505 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700506 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
507 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700508 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700509 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700510 }
511
Todd Poynorf5d30122013-08-12 17:03:35 -0700512 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
513 path.clear();
514 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
515 name);
516 if (access(path, R_OK) == 0)
517 mHealthdConfig->batteryPresentPath = path;
518 }
519
520 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
521 path.clear();
522 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
523 name);
524 if (access(path, R_OK) == 0)
525 mHealthdConfig->batteryCapacityPath = path;
526 }
527
528 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
529 path.clear();
530 path.appendFormat("%s/%s/voltage_now",
531 POWER_SUPPLY_SYSFS_PATH, name);
532 if (access(path, R_OK) == 0) {
533 mHealthdConfig->batteryVoltagePath = path;
534 } else {
535 path.clear();
536 path.appendFormat("%s/%s/batt_vol",
537 POWER_SUPPLY_SYSFS_PATH, name);
538 if (access(path, R_OK) == 0)
539 mHealthdConfig->batteryVoltagePath = path;
540 }
541 }
542
Ruchi Kandoicc338802015-08-24 13:01:16 -0700543 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
544 path.clear();
545 path.appendFormat("%s/%s/charge_full",
546 POWER_SUPPLY_SYSFS_PATH, name);
547 if (access(path, R_OK) == 0)
548 mHealthdConfig->batteryFullChargePath = path;
549 }
550
Todd Poynorf5d30122013-08-12 17:03:35 -0700551 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
552 path.clear();
553 path.appendFormat("%s/%s/current_now",
554 POWER_SUPPLY_SYSFS_PATH, name);
555 if (access(path, R_OK) == 0)
556 mHealthdConfig->batteryCurrentNowPath = path;
557 }
558
Ruchi Kandoicc338802015-08-24 13:01:16 -0700559 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
560 path.clear();
561 path.appendFormat("%s/%s/cycle_count",
562 POWER_SUPPLY_SYSFS_PATH, name);
563 if (access(path, R_OK) == 0)
564 mHealthdConfig->batteryCycleCountPath = path;
565 }
566
Todd Poynorbc102112013-08-27 18:11:49 -0700567 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
568 path.clear();
569 path.appendFormat("%s/%s/current_avg",
570 POWER_SUPPLY_SYSFS_PATH, name);
571 if (access(path, R_OK) == 0)
572 mHealthdConfig->batteryCurrentAvgPath = path;
573 }
574
Todd Poynorf5d30122013-08-12 17:03:35 -0700575 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
576 path.clear();
577 path.appendFormat("%s/%s/charge_counter",
578 POWER_SUPPLY_SYSFS_PATH, name);
579 if (access(path, R_OK) == 0)
580 mHealthdConfig->batteryChargeCounterPath = path;
581 }
582
583 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
584 path.clear();
585 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
586 name);
587 if (access(path, R_OK) == 0) {
588 mHealthdConfig->batteryTemperaturePath = path;
589 } else {
590 path.clear();
591 path.appendFormat("%s/%s/batt_temp",
592 POWER_SUPPLY_SYSFS_PATH, name);
593 if (access(path, R_OK) == 0)
594 mHealthdConfig->batteryTemperaturePath = path;
595 }
596 }
597
598 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
599 path.clear();
600 path.appendFormat("%s/%s/technology",
601 POWER_SUPPLY_SYSFS_PATH, name);
602 if (access(path, R_OK) == 0)
603 mHealthdConfig->batteryTechnologyPath = path;
604 }
605
Todd Poynor752faf22013-06-12 13:25:59 -0700606 break;
607
608 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
609 break;
610 }
611 }
Todd Poynor752faf22013-06-12 13:25:59 -0700612 }
613
Ian Pedowitz585ab652015-10-12 19:01:00 -0700614 // Typically the case for devices which do not have a battery and
615 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700616 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700617 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700618 hc->periodic_chores_interval_fast = -1;
619 hc->periodic_chores_interval_slow = -1;
620 } else {
621 if (mHealthdConfig->batteryStatusPath.isEmpty())
622 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
623 if (mHealthdConfig->batteryHealthPath.isEmpty())
624 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
625 if (mHealthdConfig->batteryPresentPath.isEmpty())
626 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
627 if (mHealthdConfig->batteryCapacityPath.isEmpty())
628 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
629 if (mHealthdConfig->batteryVoltagePath.isEmpty())
630 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
631 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
632 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
633 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
634 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700635 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700636 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
637 if (mHealthdConfig->batteryFullChargePath.isEmpty())
638 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
639 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700641 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700642
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700643 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
644 && strtol(pval, NULL, 10) != 0) {
645 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
646 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
647 }
Todd Poynor752faf22013-06-12 13:25:59 -0700648}
649
650}; // namespace android