blob: cdfe9c573bc02ec07461a6b524902375411071af [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 },
137 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
138 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700139 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
140 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
141 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700142 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
143 { NULL, 0 },
144 };
145
146 if (length <= 0)
147 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
148
149 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
150 if (ret < 0)
151 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
152
153 return ret;
154}
155
156bool BatteryMonitor::getBooleanField(const String8& path) {
157 const int SIZE = 16;
158 char buf[SIZE];
159
160 bool value = false;
161 if (readFromFile(path, buf, SIZE) > 0) {
162 if (buf[0] != '0') {
163 value = true;
164 }
165 }
166
167 return value;
168}
169
170int BatteryMonitor::getIntField(const String8& path) {
171 const int SIZE = 128;
172 char buf[SIZE];
173
174 int value = 0;
175 if (readFromFile(path, buf, SIZE) > 0) {
176 value = strtol(buf, NULL, 0);
177 }
178 return value;
179}
180
181bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700182 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700183
184 props.chargerAcOnline = false;
185 props.chargerUsbOnline = false;
186 props.chargerWirelessOnline = false;
187 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
188 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700189 props.maxChargingCurrent = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700190
Todd Poynorf5d30122013-08-12 17:03:35 -0700191 if (!mHealthdConfig->batteryPresentPath.isEmpty())
192 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700193 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700194 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700195
Todd Poynor3db03a52014-05-21 16:28:13 -0700196 props.batteryLevel = mBatteryFixedCapacity ?
197 mBatteryFixedCapacity :
198 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700199 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700200
Ruchi Kandoicc338802015-08-24 13:01:16 -0700201 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
202 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
203
204 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
205 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
206
207 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
208 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
209
Todd Poynor3db03a52014-05-21 16:28:13 -0700210 props.batteryTemperature = mBatteryFixedTemperature ?
211 mBatteryFixedTemperature :
212 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700213
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700214 // For devices which do not have battery and are always plugged
215 // into power souce.
216 if (mAlwaysPluggedDevice) {
217 props.chargerAcOnline = true;
218 props.batteryPresent = true;
219 props.batteryStatus = BATTERY_STATUS_CHARGING;
220 props.batteryHealth = BATTERY_HEALTH_GOOD;
221 }
222
Todd Poynor752faf22013-06-12 13:25:59 -0700223 const int SIZE = 128;
224 char buf[SIZE];
225 String8 btech;
226
Todd Poynorf5d30122013-08-12 17:03:35 -0700227 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700228 props.batteryStatus = getBatteryStatus(buf);
229
Todd Poynorf5d30122013-08-12 17:03:35 -0700230 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700231 props.batteryHealth = getBatteryHealth(buf);
232
Todd Poynorf5d30122013-08-12 17:03:35 -0700233 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700234 props.batteryTechnology = String8(buf);
235
236 unsigned int i;
237
238 for (i = 0; i < mChargerNames.size(); i++) {
239 String8 path;
240 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
241 mChargerNames[i].string());
242
243 if (readFromFile(path, buf, SIZE) > 0) {
244 if (buf[0] != '0') {
245 path.clear();
246 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
247 mChargerNames[i].string());
248 switch(readPowerSupplyType(path)) {
249 case ANDROID_POWER_SUPPLY_TYPE_AC:
250 props.chargerAcOnline = true;
251 break;
252 case ANDROID_POWER_SUPPLY_TYPE_USB:
253 props.chargerUsbOnline = true;
254 break;
255 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
256 props.chargerWirelessOnline = true;
257 break;
258 default:
259 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
260 mChargerNames[i].string());
261 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700262 path.clear();
263 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
264 mChargerNames[i].string());
265 if (access(path.string(), R_OK) == 0) {
266 int maxChargingCurrent = getIntField(path);
267 if (props.maxChargingCurrent < maxChargingCurrent) {
268 props.maxChargingCurrent = maxChargingCurrent;
269 }
270 }
Todd Poynor752faf22013-06-12 13:25:59 -0700271 }
272 }
273 }
274
Todd Poynor10b235e2013-08-07 15:25:14 -0700275 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700276
Todd Poynor10b235e2013-08-07 15:25:14 -0700277 if (logthis) {
278 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700279 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700280 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700281 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700282 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
283 props.batteryLevel, props.batteryVoltage,
284 props.batteryTemperature < 0 ? "-" : "",
285 abs(props.batteryTemperature / 10),
286 abs(props.batteryTemperature % 10), props.batteryHealth,
287 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700288
Ruchi Kandoicc338802015-08-24 13:01:16 -0700289 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700290 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700291 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
292 " c=%d", props.batteryCurrent);
293 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700294
Ruchi Kandoicc338802015-08-24 13:01:16 -0700295 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
296 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
297 " fc=%d", props.batteryFullCharge);
298 }
299
300 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
301 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
302 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700303 }
304 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700305 snprintf(dmesgline, sizeof(dmesgline),
306 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700307 }
308
Ruchi Kandoicc338802015-08-24 13:01:16 -0700309 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700310 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
311 props.chargerAcOnline ? "a" : "",
312 props.chargerUsbOnline ? "u" : "",
313 props.chargerWirelessOnline ? "w" : "");
314
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700315 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700316 }
317
Todd Poynorc7464c92013-09-10 12:40:00 -0700318 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700319 return props.chargerAcOnline | props.chargerUsbOnline |
320 props.chargerWirelessOnline;
321}
322
Todd Poynorc133b712013-08-14 17:39:13 -0700323status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
324 status_t ret = BAD_VALUE;
325
Todd Poynor8f132af2014-05-08 17:15:45 -0700326 val->valueInt64 = LONG_MIN;
327
Todd Poynorc133b712013-08-14 17:39:13 -0700328 switch(id) {
329 case BATTERY_PROP_CHARGE_COUNTER:
330 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700331 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700332 getIntField(mHealthdConfig->batteryChargeCounterPath);
333 ret = NO_ERROR;
334 } else {
335 ret = NAME_NOT_FOUND;
336 }
337 break;
338
339 case BATTERY_PROP_CURRENT_NOW:
340 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700341 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700342 getIntField(mHealthdConfig->batteryCurrentNowPath);
343 ret = NO_ERROR;
344 } else {
345 ret = NAME_NOT_FOUND;
346 }
347 break;
348
Todd Poynorbc102112013-08-27 18:11:49 -0700349 case BATTERY_PROP_CURRENT_AVG:
350 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700351 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700352 getIntField(mHealthdConfig->batteryCurrentAvgPath);
353 ret = NO_ERROR;
354 } else {
355 ret = NAME_NOT_FOUND;
356 }
357 break;
358
Paul Lawrence347c8de2014-03-19 15:04:40 -0700359 case BATTERY_PROP_CAPACITY:
360 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700361 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700362 getIntField(mHealthdConfig->batteryCapacityPath);
363 ret = NO_ERROR;
364 } else {
365 ret = NAME_NOT_FOUND;
366 }
367 break;
368
Todd Poynor8f132af2014-05-08 17:15:45 -0700369 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700370 if (mHealthdConfig->energyCounter) {
371 ret = mHealthdConfig->energyCounter(&val->valueInt64);
372 } else {
373 ret = NAME_NOT_FOUND;
374 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700375 break;
376
Todd Poynorc133b712013-08-14 17:39:13 -0700377 default:
378 break;
379 }
380
Todd Poynorc133b712013-08-14 17:39:13 -0700381 return ret;
382}
383
Todd Poynor020369d2013-09-18 20:09:33 -0700384void BatteryMonitor::dumpState(int fd) {
385 int v;
386 char vs[128];
387
Adrian Roosd5fe6672015-07-10 13:11:01 -0700388 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700389 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700390 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700391 write(fd, vs, strlen(vs));
392 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
393 props.batteryStatus, props.batteryHealth, props.batteryPresent);
394 write(fd, vs, strlen(vs));
395 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
396 props.batteryLevel, props.batteryVoltage,
397 props.batteryTemperature);
398 write(fd, vs, strlen(vs));
399
400 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
401 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
402 snprintf(vs, sizeof(vs), "current now: %d\n", v);
403 write(fd, vs, strlen(vs));
404 }
405
406 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
407 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
408 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
409 write(fd, vs, strlen(vs));
410 }
411
412 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
413 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
414 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
415 write(fd, vs, strlen(vs));
416 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700417
418 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
419 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
420 write(fd, vs, strlen(vs));
421 }
422
423 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
424 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
425 write(fd, vs, strlen(vs));
426 }
427
428 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
429 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
430 write(fd, vs, strlen(vs));
431 }
Todd Poynor020369d2013-09-18 20:09:33 -0700432}
433
Todd Poynorc7464c92013-09-10 12:40:00 -0700434void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700435 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700436 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700437
Todd Poynorf5d30122013-08-12 17:03:35 -0700438 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700439 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
440 if (dir == NULL) {
441 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
442 } else {
443 struct dirent* entry;
444
445 while ((entry = readdir(dir))) {
446 const char* name = entry->d_name;
447
448 if (!strcmp(name, ".") || !strcmp(name, ".."))
449 continue;
450
Todd Poynor752faf22013-06-12 13:25:59 -0700451 // Look for "type" file in each subdirectory
452 path.clear();
453 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
454 switch(readPowerSupplyType(path)) {
455 case ANDROID_POWER_SUPPLY_TYPE_AC:
456 case ANDROID_POWER_SUPPLY_TYPE_USB:
457 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
458 path.clear();
459 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
460 if (access(path.string(), R_OK) == 0)
461 mChargerNames.add(String8(name));
462 break;
463
464 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700465 mBatteryDevicePresent = true;
466
Todd Poynorf5d30122013-08-12 17:03:35 -0700467 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700468 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700469 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
470 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700471 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700472 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700473 }
474
Todd Poynorf5d30122013-08-12 17:03:35 -0700475 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700476 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700477 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
478 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700479 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700480 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700481 }
482
Todd Poynorf5d30122013-08-12 17:03:35 -0700483 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
484 path.clear();
485 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
486 name);
487 if (access(path, R_OK) == 0)
488 mHealthdConfig->batteryPresentPath = path;
489 }
490
491 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
492 path.clear();
493 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
494 name);
495 if (access(path, R_OK) == 0)
496 mHealthdConfig->batteryCapacityPath = path;
497 }
498
499 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
500 path.clear();
501 path.appendFormat("%s/%s/voltage_now",
502 POWER_SUPPLY_SYSFS_PATH, name);
503 if (access(path, R_OK) == 0) {
504 mHealthdConfig->batteryVoltagePath = path;
505 } else {
506 path.clear();
507 path.appendFormat("%s/%s/batt_vol",
508 POWER_SUPPLY_SYSFS_PATH, name);
509 if (access(path, R_OK) == 0)
510 mHealthdConfig->batteryVoltagePath = path;
511 }
512 }
513
Ruchi Kandoicc338802015-08-24 13:01:16 -0700514 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
515 path.clear();
516 path.appendFormat("%s/%s/charge_full",
517 POWER_SUPPLY_SYSFS_PATH, name);
518 if (access(path, R_OK) == 0)
519 mHealthdConfig->batteryFullChargePath = path;
520 }
521
Todd Poynorf5d30122013-08-12 17:03:35 -0700522 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
523 path.clear();
524 path.appendFormat("%s/%s/current_now",
525 POWER_SUPPLY_SYSFS_PATH, name);
526 if (access(path, R_OK) == 0)
527 mHealthdConfig->batteryCurrentNowPath = path;
528 }
529
Ruchi Kandoicc338802015-08-24 13:01:16 -0700530 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
531 path.clear();
532 path.appendFormat("%s/%s/cycle_count",
533 POWER_SUPPLY_SYSFS_PATH, name);
534 if (access(path, R_OK) == 0)
535 mHealthdConfig->batteryCycleCountPath = path;
536 }
537
Todd Poynorbc102112013-08-27 18:11:49 -0700538 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
539 path.clear();
540 path.appendFormat("%s/%s/current_avg",
541 POWER_SUPPLY_SYSFS_PATH, name);
542 if (access(path, R_OK) == 0)
543 mHealthdConfig->batteryCurrentAvgPath = path;
544 }
545
Todd Poynorf5d30122013-08-12 17:03:35 -0700546 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
547 path.clear();
548 path.appendFormat("%s/%s/charge_counter",
549 POWER_SUPPLY_SYSFS_PATH, name);
550 if (access(path, R_OK) == 0)
551 mHealthdConfig->batteryChargeCounterPath = path;
552 }
553
554 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
555 path.clear();
556 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
557 name);
558 if (access(path, R_OK) == 0) {
559 mHealthdConfig->batteryTemperaturePath = path;
560 } else {
561 path.clear();
562 path.appendFormat("%s/%s/batt_temp",
563 POWER_SUPPLY_SYSFS_PATH, name);
564 if (access(path, R_OK) == 0)
565 mHealthdConfig->batteryTemperaturePath = path;
566 }
567 }
568
569 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
570 path.clear();
571 path.appendFormat("%s/%s/technology",
572 POWER_SUPPLY_SYSFS_PATH, name);
573 if (access(path, R_OK) == 0)
574 mHealthdConfig->batteryTechnologyPath = path;
575 }
576
Todd Poynor752faf22013-06-12 13:25:59 -0700577 break;
578
579 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
580 break;
581 }
582 }
583 closedir(dir);
584 }
585
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700586 // This indicates that there is no charger driver registered.
587 // Typically the case for devices which do not have a battery and
588 // and are always plugged into AC mains.
589 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700590 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700591 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
592 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
593 mAlwaysPluggedDevice = true;
594 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700595 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700596 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700597 hc->periodic_chores_interval_fast = -1;
598 hc->periodic_chores_interval_slow = -1;
599 } else {
600 if (mHealthdConfig->batteryStatusPath.isEmpty())
601 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
602 if (mHealthdConfig->batteryHealthPath.isEmpty())
603 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
604 if (mHealthdConfig->batteryPresentPath.isEmpty())
605 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
606 if (mHealthdConfig->batteryCapacityPath.isEmpty())
607 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
608 if (mHealthdConfig->batteryVoltagePath.isEmpty())
609 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
610 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
611 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
612 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
613 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoicc338802015-08-24 13:01:16 -0700614 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
615 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
616 if (mHealthdConfig->batteryFullChargePath.isEmpty())
617 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
618 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
619 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700620 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700621
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700622 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
623 && strtol(pval, NULL, 10) != 0) {
624 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
625 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
626 }
Todd Poynor752faf22013-06-12 13:25:59 -0700627}
628
629}; // namespace android