blob: 3e618c0b187edc77b3c13bcafb5d71af2ca87018 [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
Todd Poynor10b235e2013-08-07 15:25:14 -070019#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070020#include "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>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070029
Todd Poynor752faf22013-06-12 13:25:59 -070030#include <batteryservice/BatteryService.h>
31#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070032#include <cutils/properties.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033#include <log/log_read.h>
Todd Poynorc133b712013-08-14 17:39:13 -070034#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070035#include <utils/String8.h>
36#include <utils/Vector.h>
37
38#define POWER_SUPPLY_SUBSYSTEM "power_supply"
39#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070040#define FAKE_BATTERY_CAPACITY 42
41#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -070042#define ALWAYS_PLUGGED_CAPACITY 100
Todd Poynor752faf22013-06-12 13:25:59 -070043
44namespace android {
45
46struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070047 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070048 int val;
49};
50
51static int mapSysfsString(const char* str,
52 struct sysfsStringEnumMap map[]) {
53 for (int i = 0; map[i].s; i++)
54 if (!strcmp(str, map[i].s))
55 return map[i].val;
56
57 return -1;
58}
59
60int BatteryMonitor::getBatteryStatus(const char* status) {
61 int ret;
62 struct sysfsStringEnumMap batteryStatusMap[] = {
63 { "Unknown", BATTERY_STATUS_UNKNOWN },
64 { "Charging", BATTERY_STATUS_CHARGING },
65 { "Discharging", BATTERY_STATUS_DISCHARGING },
66 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
67 { "Full", BATTERY_STATUS_FULL },
68 { NULL, 0 },
69 };
70
71 ret = mapSysfsString(status, batteryStatusMap);
72 if (ret < 0) {
73 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
74 ret = BATTERY_STATUS_UNKNOWN;
75 }
76
77 return ret;
78}
79
80int BatteryMonitor::getBatteryHealth(const char* status) {
81 int ret;
82 struct sysfsStringEnumMap batteryHealthMap[] = {
83 { "Unknown", BATTERY_HEALTH_UNKNOWN },
84 { "Good", BATTERY_HEALTH_GOOD },
85 { "Overheat", BATTERY_HEALTH_OVERHEAT },
86 { "Dead", BATTERY_HEALTH_DEAD },
87 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
88 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
89 { "Cold", BATTERY_HEALTH_COLD },
90 { NULL, 0 },
91 };
92
93 ret = mapSysfsString(status, batteryHealthMap);
94 if (ret < 0) {
95 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
96 ret = BATTERY_HEALTH_UNKNOWN;
97 }
98
99 return ret;
100}
101
102int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
103 char *cp = NULL;
104
105 if (path.isEmpty())
106 return -1;
107 int fd = open(path.string(), O_RDONLY, 0);
108 if (fd == -1) {
109 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
110 return -1;
111 }
112
113 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
114 if (count > 0)
115 cp = (char *)memrchr(buf, '\n', count);
116
117 if (cp)
118 *cp = '\0';
119 else
120 buf[0] = '\0';
121
122 close(fd);
123 return count;
124}
125
126BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
127 const int SIZE = 128;
128 char buf[SIZE];
129 int length = readFromFile(path, buf, SIZE);
130 BatteryMonitor::PowerSupplyType ret;
131 struct sysfsStringEnumMap supplyTypeMap[] = {
132 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
133 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
134 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
135 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
136 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
137 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
138 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
139 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700140 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
141 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
142 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700143 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
144 { NULL, 0 },
145 };
146
147 if (length <= 0)
148 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
149
150 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
151 if (ret < 0)
152 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
153
154 return ret;
155}
156
157bool BatteryMonitor::getBooleanField(const String8& path) {
158 const int SIZE = 16;
159 char buf[SIZE];
160
161 bool value = false;
162 if (readFromFile(path, buf, SIZE) > 0) {
163 if (buf[0] != '0') {
164 value = true;
165 }
166 }
167
168 return value;
169}
170
171int BatteryMonitor::getIntField(const String8& path) {
172 const int SIZE = 128;
173 char buf[SIZE];
174
175 int value = 0;
176 if (readFromFile(path, buf, SIZE) > 0) {
177 value = strtol(buf, NULL, 0);
178 }
179 return value;
180}
181
182bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700183 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700184
185 props.chargerAcOnline = false;
186 props.chargerUsbOnline = false;
187 props.chargerWirelessOnline = false;
188 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
189 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700190 props.maxChargingCurrent = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700191
Todd Poynorf5d30122013-08-12 17:03:35 -0700192 if (!mHealthdConfig->batteryPresentPath.isEmpty())
193 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700194 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700195 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700196
Todd Poynor3db03a52014-05-21 16:28:13 -0700197 props.batteryLevel = mBatteryFixedCapacity ?
198 mBatteryFixedCapacity :
199 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700200 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700201
Ruchi Kandoicc338802015-08-24 13:01:16 -0700202 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
203 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
204
205 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
206 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
207
208 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
209 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
210
Todd Poynor3db03a52014-05-21 16:28:13 -0700211 props.batteryTemperature = mBatteryFixedTemperature ?
212 mBatteryFixedTemperature :
213 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700214
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700215 // For devices which do not have battery and are always plugged
216 // into power souce.
217 if (mAlwaysPluggedDevice) {
218 props.chargerAcOnline = true;
219 props.batteryPresent = true;
220 props.batteryStatus = BATTERY_STATUS_CHARGING;
221 props.batteryHealth = BATTERY_HEALTH_GOOD;
222 }
223
Todd Poynor752faf22013-06-12 13:25:59 -0700224 const int SIZE = 128;
225 char buf[SIZE];
226 String8 btech;
227
Todd Poynorf5d30122013-08-12 17:03:35 -0700228 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700229 props.batteryStatus = getBatteryStatus(buf);
230
Todd Poynorf5d30122013-08-12 17:03:35 -0700231 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700232 props.batteryHealth = getBatteryHealth(buf);
233
Todd Poynorf5d30122013-08-12 17:03:35 -0700234 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700235 props.batteryTechnology = String8(buf);
236
237 unsigned int i;
238
239 for (i = 0; i < mChargerNames.size(); i++) {
240 String8 path;
241 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
242 mChargerNames[i].string());
243
244 if (readFromFile(path, buf, SIZE) > 0) {
245 if (buf[0] != '0') {
246 path.clear();
247 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
248 mChargerNames[i].string());
249 switch(readPowerSupplyType(path)) {
250 case ANDROID_POWER_SUPPLY_TYPE_AC:
251 props.chargerAcOnline = true;
252 break;
253 case ANDROID_POWER_SUPPLY_TYPE_USB:
254 props.chargerUsbOnline = true;
255 break;
256 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
257 props.chargerWirelessOnline = true;
258 break;
259 default:
260 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
261 mChargerNames[i].string());
262 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700263 path.clear();
264 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
265 mChargerNames[i].string());
266 if (access(path.string(), R_OK) == 0) {
267 int maxChargingCurrent = getIntField(path);
268 if (props.maxChargingCurrent < maxChargingCurrent) {
269 props.maxChargingCurrent = maxChargingCurrent;
270 }
271 }
Todd Poynor752faf22013-06-12 13:25:59 -0700272 }
273 }
274 }
275
Todd Poynor10b235e2013-08-07 15:25:14 -0700276 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700277
Todd Poynor10b235e2013-08-07 15:25:14 -0700278 if (logthis) {
279 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700280 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700281 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700282 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700283 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
284 props.batteryLevel, props.batteryVoltage,
285 props.batteryTemperature < 0 ? "-" : "",
286 abs(props.batteryTemperature / 10),
287 abs(props.batteryTemperature % 10), props.batteryHealth,
288 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700289
Ruchi Kandoicc338802015-08-24 13:01:16 -0700290 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700291 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700292 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
293 " c=%d", props.batteryCurrent);
294 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700295
Ruchi Kandoicc338802015-08-24 13:01:16 -0700296 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
297 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
298 " fc=%d", props.batteryFullCharge);
299 }
300
301 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
302 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
303 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700304 }
305 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700306 snprintf(dmesgline, sizeof(dmesgline),
307 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700308 }
309
Ruchi Kandoicc338802015-08-24 13:01:16 -0700310 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700311 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
312 props.chargerAcOnline ? "a" : "",
313 props.chargerUsbOnline ? "u" : "",
314 props.chargerWirelessOnline ? "w" : "");
315
316 log_time realtime(CLOCK_REALTIME);
317 time_t t = realtime.tv_sec;
318 struct tm *tmp = gmtime(&t);
319 if (tmp) {
320 static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC";
321 len = strlen(dmesgline);
322 if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin
323 && strftime(dmesgline + len, sizeof(dmesgline) - len,
324 fmt, tmp)) {
325 char *usec = strchr(dmesgline + len, 'X');
326 if (usec) {
327 len = usec - dmesgline;
328 snprintf(dmesgline + len, sizeof(dmesgline) - len,
329 "%09u", realtime.tv_nsec);
330 usec[9] = ' ';
331 }
332 }
333 }
334
335 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700336 }
337
Todd Poynorc7464c92013-09-10 12:40:00 -0700338 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700339 return props.chargerAcOnline | props.chargerUsbOnline |
340 props.chargerWirelessOnline;
341}
342
Todd Poynorc133b712013-08-14 17:39:13 -0700343status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
344 status_t ret = BAD_VALUE;
345
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);
353 ret = NO_ERROR;
354 } 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);
363 ret = NO_ERROR;
364 } 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);
373 ret = NO_ERROR;
374 } 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);
383 ret = NO_ERROR;
384 } 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
Todd Poynorc133b712013-08-14 17:39:13 -0700397 default:
398 break;
399 }
400
Todd Poynorc133b712013-08-14 17:39:13 -0700401 return ret;
402}
403
Todd Poynor020369d2013-09-18 20:09:33 -0700404void BatteryMonitor::dumpState(int fd) {
405 int v;
406 char vs[128];
407
Adrian Roosd5fe6672015-07-10 13:11:01 -0700408 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700409 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700410 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700411 write(fd, vs, strlen(vs));
412 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
413 props.batteryStatus, props.batteryHealth, props.batteryPresent);
414 write(fd, vs, strlen(vs));
415 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
416 props.batteryLevel, props.batteryVoltage,
417 props.batteryTemperature);
418 write(fd, vs, strlen(vs));
419
420 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
421 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
422 snprintf(vs, sizeof(vs), "current now: %d\n", v);
423 write(fd, vs, strlen(vs));
424 }
425
426 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
427 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
428 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
429 write(fd, vs, strlen(vs));
430 }
431
432 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
433 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
434 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
435 write(fd, vs, strlen(vs));
436 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700437
438 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
439 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
440 write(fd, vs, strlen(vs));
441 }
442
443 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
444 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
445 write(fd, vs, strlen(vs));
446 }
447
448 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
449 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
450 write(fd, vs, strlen(vs));
451 }
Todd Poynor020369d2013-09-18 20:09:33 -0700452}
453
Todd Poynorc7464c92013-09-10 12:40:00 -0700454void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700455 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700456 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700457
Todd Poynorf5d30122013-08-12 17:03:35 -0700458 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700459 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
460 if (dir == NULL) {
461 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
462 } else {
463 struct dirent* entry;
464
465 while ((entry = readdir(dir))) {
466 const char* name = entry->d_name;
467
468 if (!strcmp(name, ".") || !strcmp(name, ".."))
469 continue;
470
Todd Poynor752faf22013-06-12 13:25:59 -0700471 // Look for "type" file in each subdirectory
472 path.clear();
473 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
474 switch(readPowerSupplyType(path)) {
475 case ANDROID_POWER_SUPPLY_TYPE_AC:
476 case ANDROID_POWER_SUPPLY_TYPE_USB:
477 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
478 path.clear();
479 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
480 if (access(path.string(), R_OK) == 0)
481 mChargerNames.add(String8(name));
482 break;
483
484 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700485 mBatteryDevicePresent = true;
486
Todd Poynorf5d30122013-08-12 17:03:35 -0700487 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700488 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700489 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
490 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700491 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700492 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700493 }
494
Todd Poynorf5d30122013-08-12 17:03:35 -0700495 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700496 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700497 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
498 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700499 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700500 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700501 }
502
Todd Poynorf5d30122013-08-12 17:03:35 -0700503 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
504 path.clear();
505 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
506 name);
507 if (access(path, R_OK) == 0)
508 mHealthdConfig->batteryPresentPath = path;
509 }
510
511 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
512 path.clear();
513 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
514 name);
515 if (access(path, R_OK) == 0)
516 mHealthdConfig->batteryCapacityPath = path;
517 }
518
519 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
520 path.clear();
521 path.appendFormat("%s/%s/voltage_now",
522 POWER_SUPPLY_SYSFS_PATH, name);
523 if (access(path, R_OK) == 0) {
524 mHealthdConfig->batteryVoltagePath = path;
525 } else {
526 path.clear();
527 path.appendFormat("%s/%s/batt_vol",
528 POWER_SUPPLY_SYSFS_PATH, name);
529 if (access(path, R_OK) == 0)
530 mHealthdConfig->batteryVoltagePath = path;
531 }
532 }
533
Ruchi Kandoicc338802015-08-24 13:01:16 -0700534 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
535 path.clear();
536 path.appendFormat("%s/%s/charge_full",
537 POWER_SUPPLY_SYSFS_PATH, name);
538 if (access(path, R_OK) == 0)
539 mHealthdConfig->batteryFullChargePath = path;
540 }
541
Todd Poynorf5d30122013-08-12 17:03:35 -0700542 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
543 path.clear();
544 path.appendFormat("%s/%s/current_now",
545 POWER_SUPPLY_SYSFS_PATH, name);
546 if (access(path, R_OK) == 0)
547 mHealthdConfig->batteryCurrentNowPath = path;
548 }
549
Ruchi Kandoicc338802015-08-24 13:01:16 -0700550 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
551 path.clear();
552 path.appendFormat("%s/%s/cycle_count",
553 POWER_SUPPLY_SYSFS_PATH, name);
554 if (access(path, R_OK) == 0)
555 mHealthdConfig->batteryCycleCountPath = path;
556 }
557
Todd Poynorbc102112013-08-27 18:11:49 -0700558 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
559 path.clear();
560 path.appendFormat("%s/%s/current_avg",
561 POWER_SUPPLY_SYSFS_PATH, name);
562 if (access(path, R_OK) == 0)
563 mHealthdConfig->batteryCurrentAvgPath = path;
564 }
565
Todd Poynorf5d30122013-08-12 17:03:35 -0700566 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
567 path.clear();
568 path.appendFormat("%s/%s/charge_counter",
569 POWER_SUPPLY_SYSFS_PATH, name);
570 if (access(path, R_OK) == 0)
571 mHealthdConfig->batteryChargeCounterPath = path;
572 }
573
574 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
575 path.clear();
576 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
577 name);
578 if (access(path, R_OK) == 0) {
579 mHealthdConfig->batteryTemperaturePath = path;
580 } else {
581 path.clear();
582 path.appendFormat("%s/%s/batt_temp",
583 POWER_SUPPLY_SYSFS_PATH, name);
584 if (access(path, R_OK) == 0)
585 mHealthdConfig->batteryTemperaturePath = path;
586 }
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 }
603 closedir(dir);
604 }
605
Ian Pedowitz585ab652015-10-12 19:01:00 -0700606 // This indicates that there is no charger driver registered.
607 // Typically the case for devices which do not have a battery and
608 // and are always plugged into AC mains.
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700609 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700610 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700611 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
612 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
613 mAlwaysPluggedDevice = true;
614 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700615 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700616 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700617 hc->periodic_chores_interval_fast = -1;
618 hc->periodic_chores_interval_slow = -1;
619 } else {
620 if (mHealthdConfig->batteryStatusPath.isEmpty())
621 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
622 if (mHealthdConfig->batteryHealthPath.isEmpty())
623 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
624 if (mHealthdConfig->batteryPresentPath.isEmpty())
625 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
626 if (mHealthdConfig->batteryCapacityPath.isEmpty())
627 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
628 if (mHealthdConfig->batteryVoltagePath.isEmpty())
629 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
630 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
631 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
632 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
633 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700634 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700635 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
636 if (mHealthdConfig->batteryFullChargePath.isEmpty())
637 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
638 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
639 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700640 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700641
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700642 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
643 && strtol(pval, NULL, 10) != 0) {
644 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
645 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
646 }
Todd Poynor752faf22013-06-12 13:25:59 -0700647}
648
649}; // namespace android