blob: 676ee4151c2560db2e9ffb9959a32b3e283cb7d0 [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 Kandoif18ec9f2015-09-28 13:35:59 -070045#define ALWAYS_PLUGGED_CAPACITY 100
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080046#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070047#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070048
49namespace android {
50
51struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070052 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070053 int val;
54};
55
56static int mapSysfsString(const char* str,
57 struct sysfsStringEnumMap map[]) {
58 for (int i = 0; map[i].s; i++)
59 if (!strcmp(str, map[i].s))
60 return map[i].val;
61
62 return -1;
63}
64
Yabin Cuidb04a492016-02-16 17:19:23 -080065static void initBatteryProperties(BatteryProperties* props) {
66 props->chargerAcOnline = false;
67 props->chargerUsbOnline = false;
68 props->chargerWirelessOnline = false;
69 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070070 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080071 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
72 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
73 props->batteryPresent = false;
74 props->batteryLevel = 0;
75 props->batteryVoltage = 0;
76 props->batteryTemperature = 0;
77 props->batteryCurrent = 0;
78 props->batteryCycleCount = 0;
79 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070080 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080081 props->batteryTechnology.clear();
82}
83
84BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
85 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
86 initBatteryProperties(&props);
87}
88
Todd Poynor752faf22013-06-12 13:25:59 -070089int BatteryMonitor::getBatteryStatus(const char* status) {
90 int ret;
91 struct sysfsStringEnumMap batteryStatusMap[] = {
92 { "Unknown", BATTERY_STATUS_UNKNOWN },
93 { "Charging", BATTERY_STATUS_CHARGING },
94 { "Discharging", BATTERY_STATUS_DISCHARGING },
95 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
96 { "Full", BATTERY_STATUS_FULL },
97 { NULL, 0 },
98 };
99
100 ret = mapSysfsString(status, batteryStatusMap);
101 if (ret < 0) {
102 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
103 ret = BATTERY_STATUS_UNKNOWN;
104 }
105
106 return ret;
107}
108
109int BatteryMonitor::getBatteryHealth(const char* status) {
110 int ret;
111 struct sysfsStringEnumMap batteryHealthMap[] = {
112 { "Unknown", BATTERY_HEALTH_UNKNOWN },
113 { "Good", BATTERY_HEALTH_GOOD },
114 { "Overheat", BATTERY_HEALTH_OVERHEAT },
115 { "Dead", BATTERY_HEALTH_DEAD },
116 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
117 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
118 { "Cold", BATTERY_HEALTH_COLD },
Yueyao Zhu9bbfbf72017-06-12 16:55:38 -0700119 // battery health values from JEITA spec
120 { "Warm", BATTERY_HEALTH_GOOD },
121 { "Cool", BATTERY_HEALTH_GOOD },
122 { "Hot", BATTERY_HEALTH_OVERHEAT },
Todd Poynor752faf22013-06-12 13:25:59 -0700123 { NULL, 0 },
124 };
125
126 ret = mapSysfsString(status, batteryHealthMap);
127 if (ret < 0) {
128 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
129 ret = BATTERY_HEALTH_UNKNOWN;
130 }
131
132 return ret;
133}
134
Michael Scott3217c5c2016-06-05 11:20:13 -0700135int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800136 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700137 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700138 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700139 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700140}
141
142BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700143 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700144 BatteryMonitor::PowerSupplyType ret;
145 struct sysfsStringEnumMap supplyTypeMap[] = {
146 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
147 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
148 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
149 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
150 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
151 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100152 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700153 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
154 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700155 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
156 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
157 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700158 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
159 { NULL, 0 },
160 };
161
Michael Scott3217c5c2016-06-05 11:20:13 -0700162 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700163 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
164
Michael Scott3217c5c2016-06-05 11:20:13 -0700165 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100166 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700167 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700168 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100169 }
Todd Poynor752faf22013-06-12 13:25:59 -0700170
171 return ret;
172}
173
174bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700175 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700176 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700177
178 if (readFromFile(path, &buf) > 0)
179 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700180 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700181
182 return value;
183}
184
185int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700186 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700187 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700188
189 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700190 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700191
Todd Poynor752faf22013-06-12 13:25:59 -0700192 return value;
193}
194
195bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700196 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700197
Yabin Cuidb04a492016-02-16 17:19:23 -0800198 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700199
Todd Poynorf5d30122013-08-12 17:03:35 -0700200 if (!mHealthdConfig->batteryPresentPath.isEmpty())
201 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700202 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700203 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700204
Todd Poynor3db03a52014-05-21 16:28:13 -0700205 props.batteryLevel = mBatteryFixedCapacity ?
206 mBatteryFixedCapacity :
207 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700208 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700209
Ruchi Kandoicc338802015-08-24 13:01:16 -0700210 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
211 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
212
213 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
214 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
215
216 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
217 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
218
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700219 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
220 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
221
Todd Poynor3db03a52014-05-21 16:28:13 -0700222 props.batteryTemperature = mBatteryFixedTemperature ?
223 mBatteryFixedTemperature :
224 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700225
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700226 // For devices which do not have battery and are always plugged
227 // into power souce.
228 if (mAlwaysPluggedDevice) {
229 props.chargerAcOnline = true;
230 props.batteryPresent = true;
231 props.batteryStatus = BATTERY_STATUS_CHARGING;
232 props.batteryHealth = BATTERY_HEALTH_GOOD;
233 }
234
Michael Scott3217c5c2016-06-05 11:20:13 -0700235 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700236
Michael Scott3217c5c2016-06-05 11:20:13 -0700237 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
238 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700239
Michael Scott3217c5c2016-06-05 11:20:13 -0700240 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
241 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700242
Michael Scott3217c5c2016-06-05 11:20:13 -0700243 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
244 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700245
246 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700247 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700248
249 for (i = 0; i < mChargerNames.size(); i++) {
250 String8 path;
251 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
252 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700253 if (getIntField(path)) {
254 path.clear();
255 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
256 mChargerNames[i].string());
257 switch(readPowerSupplyType(path)) {
258 case ANDROID_POWER_SUPPLY_TYPE_AC:
259 props.chargerAcOnline = true;
260 break;
261 case ANDROID_POWER_SUPPLY_TYPE_USB:
262 props.chargerUsbOnline = true;
263 break;
264 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
265 props.chargerWirelessOnline = true;
266 break;
267 default:
268 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
269 mChargerNames[i].string());
270 }
271 path.clear();
272 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
273 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700274 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700275 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
276
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700277 path.clear();
278 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
279 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700280
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700281 int ChargingVoltage =
282 (access(path.string(), R_OK) == 0) ? getIntField(path) :
283 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700284
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700285 double power = ((double)ChargingCurrent / MILLION) *
286 ((double)ChargingVoltage / MILLION);
287 if (MaxPower < power) {
288 props.maxChargingCurrent = ChargingCurrent;
289 props.maxChargingVoltage = ChargingVoltage;
290 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700291 }
292 }
293 }
294
Todd Poynor10b235e2013-08-07 15:25:14 -0700295 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700296
Todd Poynor10b235e2013-08-07 15:25:14 -0700297 if (logthis) {
298 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700299 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700300 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700301 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700302 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
303 props.batteryLevel, props.batteryVoltage,
304 props.batteryTemperature < 0 ? "-" : "",
305 abs(props.batteryTemperature / 10),
306 abs(props.batteryTemperature % 10), props.batteryHealth,
307 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700308
Ruchi Kandoicc338802015-08-24 13:01:16 -0700309 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700310 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700311 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
312 " c=%d", props.batteryCurrent);
313 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700314
Ruchi Kandoicc338802015-08-24 13:01:16 -0700315 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
316 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
317 " fc=%d", props.batteryFullCharge);
318 }
319
320 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
321 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
322 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700323 }
324 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800325 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700326 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700327 }
328
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700329 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
330 props.chargerAcOnline ? "a" : "",
331 props.chargerUsbOnline ? "u" : "",
332 props.chargerWirelessOnline ? "w" : "");
333
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700334 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700335 }
336
Todd Poynorc7464c92013-09-10 12:40:00 -0700337 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700338 return props.chargerAcOnline | props.chargerUsbOnline |
339 props.chargerWirelessOnline;
340}
341
Yabin Cuiaedf6032016-02-19 18:03:23 -0800342int BatteryMonitor::getChargeStatus() {
343 int result = BATTERY_STATUS_UNKNOWN;
344 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700345 std::string buf;
346 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
347 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800348 }
349 return result;
350}
351
Todd Poynorc133b712013-08-14 17:39:13 -0700352status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
353 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800354 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700355
Todd Poynor8f132af2014-05-08 17:15:45 -0700356 val->valueInt64 = LONG_MIN;
357
Todd Poynorc133b712013-08-14 17:39:13 -0700358 switch(id) {
359 case BATTERY_PROP_CHARGE_COUNTER:
360 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700361 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700362 getIntField(mHealthdConfig->batteryChargeCounterPath);
363 ret = NO_ERROR;
364 } else {
365 ret = NAME_NOT_FOUND;
366 }
367 break;
368
369 case BATTERY_PROP_CURRENT_NOW:
370 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700371 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700372 getIntField(mHealthdConfig->batteryCurrentNowPath);
373 ret = NO_ERROR;
374 } else {
375 ret = NAME_NOT_FOUND;
376 }
377 break;
378
Todd Poynorbc102112013-08-27 18:11:49 -0700379 case BATTERY_PROP_CURRENT_AVG:
380 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700381 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700382 getIntField(mHealthdConfig->batteryCurrentAvgPath);
383 ret = NO_ERROR;
384 } else {
385 ret = NAME_NOT_FOUND;
386 }
387 break;
388
Paul Lawrence347c8de2014-03-19 15:04:40 -0700389 case BATTERY_PROP_CAPACITY:
390 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700391 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700392 getIntField(mHealthdConfig->batteryCapacityPath);
393 ret = NO_ERROR;
394 } else {
395 ret = NAME_NOT_FOUND;
396 }
397 break;
398
Todd Poynor8f132af2014-05-08 17:15:45 -0700399 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700400 if (mHealthdConfig->energyCounter) {
401 ret = mHealthdConfig->energyCounter(&val->valueInt64);
402 } else {
403 ret = NAME_NOT_FOUND;
404 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700405 break;
406
Jin Qian72adf112017-02-02 17:31:13 -0800407 case BATTERY_PROP_BATTERY_STATUS:
408 if (mAlwaysPluggedDevice) {
409 val->valueInt64 = BATTERY_STATUS_CHARGING;
410 } else {
411 val->valueInt64 = getChargeStatus();
412 }
413 ret = NO_ERROR;
414 break;
415
Todd Poynorc133b712013-08-14 17:39:13 -0700416 default:
417 break;
418 }
419
Todd Poynorc133b712013-08-14 17:39:13 -0700420 return ret;
421}
422
Todd Poynor020369d2013-09-18 20:09:33 -0700423void BatteryMonitor::dumpState(int fd) {
424 int v;
425 char vs[128];
426
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700427 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700428 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700429 props.chargerWirelessOnline, props.maxChargingCurrent,
430 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700431 write(fd, vs, strlen(vs));
432 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
433 props.batteryStatus, props.batteryHealth, props.batteryPresent);
434 write(fd, vs, strlen(vs));
435 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
436 props.batteryLevel, props.batteryVoltage,
437 props.batteryTemperature);
438 write(fd, vs, strlen(vs));
439
440 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
441 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
442 snprintf(vs, sizeof(vs), "current now: %d\n", v);
443 write(fd, vs, strlen(vs));
444 }
445
446 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
447 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
448 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
449 write(fd, vs, strlen(vs));
450 }
451
452 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
453 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
454 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
455 write(fd, vs, strlen(vs));
456 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700457
458 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
459 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
460 write(fd, vs, strlen(vs));
461 }
462
463 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
464 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
465 write(fd, vs, strlen(vs));
466 }
467
468 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
469 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
470 write(fd, vs, strlen(vs));
471 }
Todd Poynor020369d2013-09-18 20:09:33 -0700472}
473
Todd Poynorc7464c92013-09-10 12:40:00 -0700474void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700475 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700476 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700477
Todd Poynorf5d30122013-08-12 17:03:35 -0700478 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800479 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700480 if (dir == NULL) {
481 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
482 } else {
483 struct dirent* entry;
484
James Hawkins588a2ca2016-02-18 14:52:46 -0800485 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700486 const char* name = entry->d_name;
487
488 if (!strcmp(name, ".") || !strcmp(name, ".."))
489 continue;
490
Todd Poynor752faf22013-06-12 13:25:59 -0700491 // Look for "type" file in each subdirectory
492 path.clear();
493 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
494 switch(readPowerSupplyType(path)) {
495 case ANDROID_POWER_SUPPLY_TYPE_AC:
496 case ANDROID_POWER_SUPPLY_TYPE_USB:
497 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
498 path.clear();
499 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
500 if (access(path.string(), R_OK) == 0)
501 mChargerNames.add(String8(name));
502 break;
503
504 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700505 mBatteryDevicePresent = true;
506
Todd Poynorf5d30122013-08-12 17:03:35 -0700507 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700508 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700509 path.appendFormat("%s/%s/status", 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->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700513 }
514
Todd Poynorf5d30122013-08-12 17:03:35 -0700515 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700516 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700517 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
518 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700519 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700520 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700521 }
522
Todd Poynorf5d30122013-08-12 17:03:35 -0700523 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
524 path.clear();
525 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
526 name);
527 if (access(path, R_OK) == 0)
528 mHealthdConfig->batteryPresentPath = path;
529 }
530
531 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
532 path.clear();
533 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
534 name);
535 if (access(path, R_OK) == 0)
536 mHealthdConfig->batteryCapacityPath = path;
537 }
538
539 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
540 path.clear();
541 path.appendFormat("%s/%s/voltage_now",
542 POWER_SUPPLY_SYSFS_PATH, name);
543 if (access(path, R_OK) == 0) {
544 mHealthdConfig->batteryVoltagePath = path;
545 } else {
546 path.clear();
547 path.appendFormat("%s/%s/batt_vol",
548 POWER_SUPPLY_SYSFS_PATH, name);
549 if (access(path, R_OK) == 0)
550 mHealthdConfig->batteryVoltagePath = path;
551 }
552 }
553
Ruchi Kandoicc338802015-08-24 13:01:16 -0700554 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
555 path.clear();
556 path.appendFormat("%s/%s/charge_full",
557 POWER_SUPPLY_SYSFS_PATH, name);
558 if (access(path, R_OK) == 0)
559 mHealthdConfig->batteryFullChargePath = path;
560 }
561
Todd Poynorf5d30122013-08-12 17:03:35 -0700562 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
563 path.clear();
564 path.appendFormat("%s/%s/current_now",
565 POWER_SUPPLY_SYSFS_PATH, name);
566 if (access(path, R_OK) == 0)
567 mHealthdConfig->batteryCurrentNowPath = path;
568 }
569
Ruchi Kandoicc338802015-08-24 13:01:16 -0700570 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
571 path.clear();
572 path.appendFormat("%s/%s/cycle_count",
573 POWER_SUPPLY_SYSFS_PATH, name);
574 if (access(path, R_OK) == 0)
575 mHealthdConfig->batteryCycleCountPath = path;
576 }
577
Todd Poynorbc102112013-08-27 18:11:49 -0700578 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
579 path.clear();
580 path.appendFormat("%s/%s/current_avg",
581 POWER_SUPPLY_SYSFS_PATH, name);
582 if (access(path, R_OK) == 0)
583 mHealthdConfig->batteryCurrentAvgPath = path;
584 }
585
Todd Poynorf5d30122013-08-12 17:03:35 -0700586 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
587 path.clear();
588 path.appendFormat("%s/%s/charge_counter",
589 POWER_SUPPLY_SYSFS_PATH, name);
590 if (access(path, R_OK) == 0)
591 mHealthdConfig->batteryChargeCounterPath = path;
592 }
593
594 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
595 path.clear();
596 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
597 name);
598 if (access(path, R_OK) == 0) {
599 mHealthdConfig->batteryTemperaturePath = path;
600 } else {
601 path.clear();
602 path.appendFormat("%s/%s/batt_temp",
603 POWER_SUPPLY_SYSFS_PATH, name);
604 if (access(path, R_OK) == 0)
605 mHealthdConfig->batteryTemperaturePath = path;
606 }
607 }
608
609 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
610 path.clear();
611 path.appendFormat("%s/%s/technology",
612 POWER_SUPPLY_SYSFS_PATH, name);
613 if (access(path, R_OK) == 0)
614 mHealthdConfig->batteryTechnologyPath = path;
615 }
616
Todd Poynor752faf22013-06-12 13:25:59 -0700617 break;
618
619 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
620 break;
621 }
622 }
Todd Poynor752faf22013-06-12 13:25:59 -0700623 }
624
Ian Pedowitz585ab652015-10-12 19:01:00 -0700625 // Typically the case for devices which do not have a battery and
626 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700627 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700628 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700629 hc->periodic_chores_interval_fast = -1;
630 hc->periodic_chores_interval_slow = -1;
Ruchi Kandoifabd4902016-02-29 13:13:39 -0800631 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
632 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
633 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700634 } else {
635 if (mHealthdConfig->batteryStatusPath.isEmpty())
636 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
637 if (mHealthdConfig->batteryHealthPath.isEmpty())
638 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
639 if (mHealthdConfig->batteryPresentPath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
641 if (mHealthdConfig->batteryCapacityPath.isEmpty())
642 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
643 if (mHealthdConfig->batteryVoltagePath.isEmpty())
644 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
645 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
646 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
647 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
648 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700649 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700650 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
651 if (mHealthdConfig->batteryFullChargePath.isEmpty())
652 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
653 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
654 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700655 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700656
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700657 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
658 && strtol(pval, NULL, 10) != 0) {
659 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
660 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
661 }
Todd Poynor752faf22013-06-12 13:25:59 -0700662}
663
664}; // namespace android