blob: 8f98f17344c7cb943ac574904945fd4392070e25 [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>
Todd Poynorc133b712013-08-14 17:39:13 -070033#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070034#include <utils/String8.h>
35#include <utils/Vector.h>
36
37#define POWER_SUPPLY_SUBSYSTEM "power_supply"
38#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070039#define FAKE_BATTERY_CAPACITY 42
40#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi42a981d2015-09-28 13:35:59 -070041#define ALWAYS_PLUGGED_CAPACITY 100
Todd Poynor752faf22013-06-12 13:25:59 -070042
43namespace android {
44
45struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070046 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070047 int val;
48};
49
50static int mapSysfsString(const char* str,
51 struct sysfsStringEnumMap map[]) {
52 for (int i = 0; map[i].s; i++)
53 if (!strcmp(str, map[i].s))
54 return map[i].val;
55
56 return -1;
57}
58
59int BatteryMonitor::getBatteryStatus(const char* status) {
60 int ret;
61 struct sysfsStringEnumMap batteryStatusMap[] = {
62 { "Unknown", BATTERY_STATUS_UNKNOWN },
63 { "Charging", BATTERY_STATUS_CHARGING },
64 { "Discharging", BATTERY_STATUS_DISCHARGING },
65 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
66 { "Full", BATTERY_STATUS_FULL },
67 { NULL, 0 },
68 };
69
70 ret = mapSysfsString(status, batteryStatusMap);
71 if (ret < 0) {
72 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
73 ret = BATTERY_STATUS_UNKNOWN;
74 }
75
76 return ret;
77}
78
79int BatteryMonitor::getBatteryHealth(const char* status) {
80 int ret;
81 struct sysfsStringEnumMap batteryHealthMap[] = {
82 { "Unknown", BATTERY_HEALTH_UNKNOWN },
83 { "Good", BATTERY_HEALTH_GOOD },
84 { "Overheat", BATTERY_HEALTH_OVERHEAT },
85 { "Dead", BATTERY_HEALTH_DEAD },
86 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
87 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
88 { "Cold", BATTERY_HEALTH_COLD },
89 { NULL, 0 },
90 };
91
92 ret = mapSysfsString(status, batteryHealthMap);
93 if (ret < 0) {
94 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
95 ret = BATTERY_HEALTH_UNKNOWN;
96 }
97
98 return ret;
99}
100
101int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
102 char *cp = NULL;
103
104 if (path.isEmpty())
105 return -1;
106 int fd = open(path.string(), O_RDONLY, 0);
107 if (fd == -1) {
108 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
109 return -1;
110 }
111
112 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
113 if (count > 0)
114 cp = (char *)memrchr(buf, '\n', count);
115
116 if (cp)
117 *cp = '\0';
118 else
119 buf[0] = '\0';
120
121 close(fd);
122 return count;
123}
124
125BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
126 const int SIZE = 128;
127 char buf[SIZE];
128 int length = readFromFile(path, buf, SIZE);
129 BatteryMonitor::PowerSupplyType ret;
130 struct sysfsStringEnumMap supplyTypeMap[] = {
131 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
132 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
133 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
134 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
135 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
136 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100137 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700138 { "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);
Johan Redestig32828612016-02-03 13:45:54 +0100151 if (ret < 0) {
152 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700153 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100154 }
Todd Poynor752faf22013-06-12 13:25:59 -0700155
156 return ret;
157}
158
159bool BatteryMonitor::getBooleanField(const String8& path) {
160 const int SIZE = 16;
161 char buf[SIZE];
162
163 bool value = false;
164 if (readFromFile(path, buf, SIZE) > 0) {
165 if (buf[0] != '0') {
166 value = true;
167 }
168 }
169
170 return value;
171}
172
173int BatteryMonitor::getIntField(const String8& path) {
174 const int SIZE = 128;
175 char buf[SIZE];
176
177 int value = 0;
178 if (readFromFile(path, buf, SIZE) > 0) {
179 value = strtol(buf, NULL, 0);
180 }
181 return value;
182}
183
184bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700185 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700186
187 props.chargerAcOnline = false;
188 props.chargerUsbOnline = false;
189 props.chargerWirelessOnline = false;
190 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
191 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700192 props.maxChargingCurrent = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700193
Todd Poynorf5d30122013-08-12 17:03:35 -0700194 if (!mHealthdConfig->batteryPresentPath.isEmpty())
195 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700196 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700197 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700198
Todd Poynor3db03a52014-05-21 16:28:13 -0700199 props.batteryLevel = mBatteryFixedCapacity ?
200 mBatteryFixedCapacity :
201 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700202 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700203
Ruchi Kandoicc338802015-08-24 13:01:16 -0700204 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
205 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
206
207 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
208 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
209
210 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
211 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
212
Todd Poynor3db03a52014-05-21 16:28:13 -0700213 props.batteryTemperature = mBatteryFixedTemperature ?
214 mBatteryFixedTemperature :
215 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700216
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700217 // For devices which do not have battery and are always plugged
218 // into power souce.
219 if (mAlwaysPluggedDevice) {
220 props.chargerAcOnline = true;
221 props.batteryPresent = true;
222 props.batteryStatus = BATTERY_STATUS_CHARGING;
223 props.batteryHealth = BATTERY_HEALTH_GOOD;
224 }
225
Todd Poynor752faf22013-06-12 13:25:59 -0700226 const int SIZE = 128;
227 char buf[SIZE];
228 String8 btech;
229
Todd Poynorf5d30122013-08-12 17:03:35 -0700230 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700231 props.batteryStatus = getBatteryStatus(buf);
232
Todd Poynorf5d30122013-08-12 17:03:35 -0700233 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700234 props.batteryHealth = getBatteryHealth(buf);
235
Todd Poynorf5d30122013-08-12 17:03:35 -0700236 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700237 props.batteryTechnology = String8(buf);
238
239 unsigned int i;
240
241 for (i = 0; i < mChargerNames.size(); i++) {
242 String8 path;
243 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
244 mChargerNames[i].string());
245
246 if (readFromFile(path, buf, SIZE) > 0) {
247 if (buf[0] != '0') {
248 path.clear();
249 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
250 mChargerNames[i].string());
251 switch(readPowerSupplyType(path)) {
252 case ANDROID_POWER_SUPPLY_TYPE_AC:
253 props.chargerAcOnline = true;
254 break;
255 case ANDROID_POWER_SUPPLY_TYPE_USB:
256 props.chargerUsbOnline = true;
257 break;
258 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
259 props.chargerWirelessOnline = true;
260 break;
261 default:
262 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
263 mChargerNames[i].string());
264 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700265 path.clear();
266 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
267 mChargerNames[i].string());
268 if (access(path.string(), R_OK) == 0) {
269 int maxChargingCurrent = getIntField(path);
270 if (props.maxChargingCurrent < maxChargingCurrent) {
271 props.maxChargingCurrent = maxChargingCurrent;
272 }
273 }
Todd Poynor752faf22013-06-12 13:25:59 -0700274 }
275 }
276 }
277
Todd Poynor10b235e2013-08-07 15:25:14 -0700278 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700279
Todd Poynor10b235e2013-08-07 15:25:14 -0700280 if (logthis) {
281 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700282 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700283 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700284 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700285 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
286 props.batteryLevel, props.batteryVoltage,
287 props.batteryTemperature < 0 ? "-" : "",
288 abs(props.batteryTemperature / 10),
289 abs(props.batteryTemperature % 10), props.batteryHealth,
290 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700291
Ruchi Kandoicc338802015-08-24 13:01:16 -0700292 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700293 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700294 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
295 " c=%d", props.batteryCurrent);
296 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700297
Ruchi Kandoicc338802015-08-24 13:01:16 -0700298 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
299 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
300 " fc=%d", props.batteryFullCharge);
301 }
302
303 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
304 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
305 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700306 }
307 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700308 snprintf(dmesgline, sizeof(dmesgline),
309 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700310 }
311
Ruchi Kandoicc338802015-08-24 13:01:16 -0700312 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700313 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
314 props.chargerAcOnline ? "a" : "",
315 props.chargerUsbOnline ? "u" : "",
316 props.chargerWirelessOnline ? "w" : "");
317
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700318 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700319 }
320
Todd Poynorc7464c92013-09-10 12:40:00 -0700321 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700322 return props.chargerAcOnline | props.chargerUsbOnline |
323 props.chargerWirelessOnline;
324}
325
Todd Poynorc133b712013-08-14 17:39:13 -0700326status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
327 status_t ret = BAD_VALUE;
328
Todd Poynor8f132af2014-05-08 17:15:45 -0700329 val->valueInt64 = LONG_MIN;
330
Todd Poynorc133b712013-08-14 17:39:13 -0700331 switch(id) {
332 case BATTERY_PROP_CHARGE_COUNTER:
333 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700334 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700335 getIntField(mHealthdConfig->batteryChargeCounterPath);
336 ret = NO_ERROR;
337 } else {
338 ret = NAME_NOT_FOUND;
339 }
340 break;
341
342 case BATTERY_PROP_CURRENT_NOW:
343 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700344 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700345 getIntField(mHealthdConfig->batteryCurrentNowPath);
346 ret = NO_ERROR;
347 } else {
348 ret = NAME_NOT_FOUND;
349 }
350 break;
351
Todd Poynorbc102112013-08-27 18:11:49 -0700352 case BATTERY_PROP_CURRENT_AVG:
353 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700354 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700355 getIntField(mHealthdConfig->batteryCurrentAvgPath);
356 ret = NO_ERROR;
357 } else {
358 ret = NAME_NOT_FOUND;
359 }
360 break;
361
Paul Lawrence347c8de2014-03-19 15:04:40 -0700362 case BATTERY_PROP_CAPACITY:
363 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700364 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700365 getIntField(mHealthdConfig->batteryCapacityPath);
366 ret = NO_ERROR;
367 } else {
368 ret = NAME_NOT_FOUND;
369 }
370 break;
371
Todd Poynor8f132af2014-05-08 17:15:45 -0700372 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700373 if (mHealthdConfig->energyCounter) {
374 ret = mHealthdConfig->energyCounter(&val->valueInt64);
375 } else {
376 ret = NAME_NOT_FOUND;
377 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700378 break;
379
Todd Poynorc133b712013-08-14 17:39:13 -0700380 default:
381 break;
382 }
383
Todd Poynorc133b712013-08-14 17:39:13 -0700384 return ret;
385}
386
Todd Poynor020369d2013-09-18 20:09:33 -0700387void BatteryMonitor::dumpState(int fd) {
388 int v;
389 char vs[128];
390
Adrian Roosd5fe6672015-07-10 13:11:01 -0700391 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700392 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700393 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700394 write(fd, vs, strlen(vs));
395 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
396 props.batteryStatus, props.batteryHealth, props.batteryPresent);
397 write(fd, vs, strlen(vs));
398 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
399 props.batteryLevel, props.batteryVoltage,
400 props.batteryTemperature);
401 write(fd, vs, strlen(vs));
402
403 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
404 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
405 snprintf(vs, sizeof(vs), "current now: %d\n", v);
406 write(fd, vs, strlen(vs));
407 }
408
409 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
410 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
411 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
412 write(fd, vs, strlen(vs));
413 }
414
415 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
416 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
417 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
418 write(fd, vs, strlen(vs));
419 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700420
421 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
422 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
423 write(fd, vs, strlen(vs));
424 }
425
426 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
427 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
428 write(fd, vs, strlen(vs));
429 }
430
431 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
432 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
433 write(fd, vs, strlen(vs));
434 }
Todd Poynor020369d2013-09-18 20:09:33 -0700435}
436
Todd Poynorc7464c92013-09-10 12:40:00 -0700437void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700438 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700439 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700440
Todd Poynorf5d30122013-08-12 17:03:35 -0700441 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700442 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
443 if (dir == NULL) {
444 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
445 } else {
446 struct dirent* entry;
447
448 while ((entry = readdir(dir))) {
449 const char* name = entry->d_name;
450
451 if (!strcmp(name, ".") || !strcmp(name, ".."))
452 continue;
453
Todd Poynor752faf22013-06-12 13:25:59 -0700454 // Look for "type" file in each subdirectory
455 path.clear();
456 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
457 switch(readPowerSupplyType(path)) {
458 case ANDROID_POWER_SUPPLY_TYPE_AC:
459 case ANDROID_POWER_SUPPLY_TYPE_USB:
460 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
461 path.clear();
462 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
463 if (access(path.string(), R_OK) == 0)
464 mChargerNames.add(String8(name));
465 break;
466
467 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700468 mBatteryDevicePresent = true;
469
Todd Poynorf5d30122013-08-12 17:03:35 -0700470 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700471 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700472 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
473 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700474 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700475 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700476 }
477
Todd Poynorf5d30122013-08-12 17:03:35 -0700478 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700479 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700480 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
481 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700482 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700483 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700484 }
485
Todd Poynorf5d30122013-08-12 17:03:35 -0700486 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
487 path.clear();
488 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
489 name);
490 if (access(path, R_OK) == 0)
491 mHealthdConfig->batteryPresentPath = path;
492 }
493
494 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
495 path.clear();
496 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
497 name);
498 if (access(path, R_OK) == 0)
499 mHealthdConfig->batteryCapacityPath = path;
500 }
501
502 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
503 path.clear();
504 path.appendFormat("%s/%s/voltage_now",
505 POWER_SUPPLY_SYSFS_PATH, name);
506 if (access(path, R_OK) == 0) {
507 mHealthdConfig->batteryVoltagePath = path;
508 } else {
509 path.clear();
510 path.appendFormat("%s/%s/batt_vol",
511 POWER_SUPPLY_SYSFS_PATH, name);
512 if (access(path, R_OK) == 0)
513 mHealthdConfig->batteryVoltagePath = path;
514 }
515 }
516
Ruchi Kandoicc338802015-08-24 13:01:16 -0700517 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
518 path.clear();
519 path.appendFormat("%s/%s/charge_full",
520 POWER_SUPPLY_SYSFS_PATH, name);
521 if (access(path, R_OK) == 0)
522 mHealthdConfig->batteryFullChargePath = path;
523 }
524
Todd Poynorf5d30122013-08-12 17:03:35 -0700525 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
526 path.clear();
527 path.appendFormat("%s/%s/current_now",
528 POWER_SUPPLY_SYSFS_PATH, name);
529 if (access(path, R_OK) == 0)
530 mHealthdConfig->batteryCurrentNowPath = path;
531 }
532
Ruchi Kandoicc338802015-08-24 13:01:16 -0700533 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
534 path.clear();
535 path.appendFormat("%s/%s/cycle_count",
536 POWER_SUPPLY_SYSFS_PATH, name);
537 if (access(path, R_OK) == 0)
538 mHealthdConfig->batteryCycleCountPath = path;
539 }
540
Todd Poynorbc102112013-08-27 18:11:49 -0700541 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
542 path.clear();
543 path.appendFormat("%s/%s/current_avg",
544 POWER_SUPPLY_SYSFS_PATH, name);
545 if (access(path, R_OK) == 0)
546 mHealthdConfig->batteryCurrentAvgPath = path;
547 }
548
Todd Poynorf5d30122013-08-12 17:03:35 -0700549 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
550 path.clear();
551 path.appendFormat("%s/%s/charge_counter",
552 POWER_SUPPLY_SYSFS_PATH, name);
553 if (access(path, R_OK) == 0)
554 mHealthdConfig->batteryChargeCounterPath = path;
555 }
556
557 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
558 path.clear();
559 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
560 name);
561 if (access(path, R_OK) == 0) {
562 mHealthdConfig->batteryTemperaturePath = path;
563 } else {
564 path.clear();
565 path.appendFormat("%s/%s/batt_temp",
566 POWER_SUPPLY_SYSFS_PATH, name);
567 if (access(path, R_OK) == 0)
568 mHealthdConfig->batteryTemperaturePath = path;
569 }
570 }
571
572 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
573 path.clear();
574 path.appendFormat("%s/%s/technology",
575 POWER_SUPPLY_SYSFS_PATH, name);
576 if (access(path, R_OK) == 0)
577 mHealthdConfig->batteryTechnologyPath = path;
578 }
579
Todd Poynor752faf22013-06-12 13:25:59 -0700580 break;
581
582 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
583 break;
584 }
585 }
586 closedir(dir);
587 }
588
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700589 // This indicates that there is no charger driver registered.
590 // Typically the case for devices which do not have a battery and
591 // and are always plugged into AC mains.
592 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700593 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700594 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
595 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
596 mAlwaysPluggedDevice = true;
597 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700598 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700599 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700600 hc->periodic_chores_interval_fast = -1;
601 hc->periodic_chores_interval_slow = -1;
602 } else {
603 if (mHealthdConfig->batteryStatusPath.isEmpty())
604 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
605 if (mHealthdConfig->batteryHealthPath.isEmpty())
606 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
607 if (mHealthdConfig->batteryPresentPath.isEmpty())
608 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
609 if (mHealthdConfig->batteryCapacityPath.isEmpty())
610 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
611 if (mHealthdConfig->batteryVoltagePath.isEmpty())
612 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
613 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
614 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
615 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
616 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoicc338802015-08-24 13:01:16 -0700617 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
618 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
619 if (mHealthdConfig->batteryFullChargePath.isEmpty())
620 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
621 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
622 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700623 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700624
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700625 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
626 && strtol(pval, NULL, 10) != 0) {
627 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
628 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
629 }
Todd Poynor752faf22013-06-12 13:25:59 -0700630}
631
632}; // namespace android