blob: cb77a8e41b746e455ae4dcfcfcbcc0805a625895 [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
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 },
139 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
140 { NULL, 0 },
141 };
142
143 if (length <= 0)
144 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
145
146 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
147 if (ret < 0)
148 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
149
150 return ret;
151}
152
153bool BatteryMonitor::getBooleanField(const String8& path) {
154 const int SIZE = 16;
155 char buf[SIZE];
156
157 bool value = false;
158 if (readFromFile(path, buf, SIZE) > 0) {
159 if (buf[0] != '0') {
160 value = true;
161 }
162 }
163
164 return value;
165}
166
167int BatteryMonitor::getIntField(const String8& path) {
168 const int SIZE = 128;
169 char buf[SIZE];
170
171 int value = 0;
172 if (readFromFile(path, buf, SIZE) > 0) {
173 value = strtol(buf, NULL, 0);
174 }
175 return value;
176}
177
178bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700179 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700180
181 props.chargerAcOnline = false;
182 props.chargerUsbOnline = false;
183 props.chargerWirelessOnline = false;
184 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
185 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700186 props.maxChargingCurrent = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700187
Todd Poynorf5d30122013-08-12 17:03:35 -0700188 if (!mHealthdConfig->batteryPresentPath.isEmpty())
189 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700190 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700191 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700192
Todd Poynor3db03a52014-05-21 16:28:13 -0700193 props.batteryLevel = mBatteryFixedCapacity ?
194 mBatteryFixedCapacity :
195 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700196 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700197
Ruchi Kandoicc338802015-08-24 13:01:16 -0700198 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
199 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
200
201 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
202 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
203
204 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
205 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
206
Todd Poynor3db03a52014-05-21 16:28:13 -0700207 props.batteryTemperature = mBatteryFixedTemperature ?
208 mBatteryFixedTemperature :
209 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700210
211 const int SIZE = 128;
212 char buf[SIZE];
213 String8 btech;
214
Todd Poynorf5d30122013-08-12 17:03:35 -0700215 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700216 props.batteryStatus = getBatteryStatus(buf);
217
Todd Poynorf5d30122013-08-12 17:03:35 -0700218 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700219 props.batteryHealth = getBatteryHealth(buf);
220
Todd Poynorf5d30122013-08-12 17:03:35 -0700221 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700222 props.batteryTechnology = String8(buf);
223
224 unsigned int i;
225
226 for (i = 0; i < mChargerNames.size(); i++) {
227 String8 path;
228 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
229 mChargerNames[i].string());
230
231 if (readFromFile(path, buf, SIZE) > 0) {
232 if (buf[0] != '0') {
233 path.clear();
234 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
235 mChargerNames[i].string());
236 switch(readPowerSupplyType(path)) {
237 case ANDROID_POWER_SUPPLY_TYPE_AC:
238 props.chargerAcOnline = true;
239 break;
240 case ANDROID_POWER_SUPPLY_TYPE_USB:
241 props.chargerUsbOnline = true;
242 break;
243 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
244 props.chargerWirelessOnline = true;
245 break;
246 default:
247 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
248 mChargerNames[i].string());
249 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700250 path.clear();
251 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
252 mChargerNames[i].string());
253 if (access(path.string(), R_OK) == 0) {
254 int maxChargingCurrent = getIntField(path);
255 if (props.maxChargingCurrent < maxChargingCurrent) {
256 props.maxChargingCurrent = maxChargingCurrent;
257 }
258 }
Todd Poynor752faf22013-06-12 13:25:59 -0700259 }
260 }
261 }
262
Todd Poynor10b235e2013-08-07 15:25:14 -0700263 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700264
Todd Poynor10b235e2013-08-07 15:25:14 -0700265 if (logthis) {
266 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700267 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700268 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700269 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700270 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
271 props.batteryLevel, props.batteryVoltage,
272 props.batteryTemperature < 0 ? "-" : "",
273 abs(props.batteryTemperature / 10),
274 abs(props.batteryTemperature % 10), props.batteryHealth,
275 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700276
Ruchi Kandoicc338802015-08-24 13:01:16 -0700277 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700278 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700279 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
280 " c=%d", props.batteryCurrent);
281 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700282
Ruchi Kandoicc338802015-08-24 13:01:16 -0700283 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
284 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
285 " fc=%d", props.batteryFullCharge);
286 }
287
288 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
289 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
290 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700291 }
292 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700293 snprintf(dmesgline, sizeof(dmesgline),
294 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700295 }
296
Ruchi Kandoicc338802015-08-24 13:01:16 -0700297 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700298 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
299 props.chargerAcOnline ? "a" : "",
300 props.chargerUsbOnline ? "u" : "",
301 props.chargerWirelessOnline ? "w" : "");
302
303 log_time realtime(CLOCK_REALTIME);
304 time_t t = realtime.tv_sec;
305 struct tm *tmp = gmtime(&t);
306 if (tmp) {
307 static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC";
308 len = strlen(dmesgline);
309 if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin
310 && strftime(dmesgline + len, sizeof(dmesgline) - len,
311 fmt, tmp)) {
312 char *usec = strchr(dmesgline + len, 'X');
313 if (usec) {
314 len = usec - dmesgline;
315 snprintf(dmesgline + len, sizeof(dmesgline) - len,
316 "%09u", realtime.tv_nsec);
317 usec[9] = ' ';
318 }
319 }
320 }
321
322 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700323 }
324
Todd Poynorc7464c92013-09-10 12:40:00 -0700325 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700326 return props.chargerAcOnline | props.chargerUsbOnline |
327 props.chargerWirelessOnline;
328}
329
Todd Poynorc133b712013-08-14 17:39:13 -0700330status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
331 status_t ret = BAD_VALUE;
332
Todd Poynor8f132af2014-05-08 17:15:45 -0700333 val->valueInt64 = LONG_MIN;
334
Todd Poynorc133b712013-08-14 17:39:13 -0700335 switch(id) {
336 case BATTERY_PROP_CHARGE_COUNTER:
337 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700338 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700339 getIntField(mHealthdConfig->batteryChargeCounterPath);
340 ret = NO_ERROR;
341 } else {
342 ret = NAME_NOT_FOUND;
343 }
344 break;
345
346 case BATTERY_PROP_CURRENT_NOW:
347 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700348 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700349 getIntField(mHealthdConfig->batteryCurrentNowPath);
350 ret = NO_ERROR;
351 } else {
352 ret = NAME_NOT_FOUND;
353 }
354 break;
355
Todd Poynorbc102112013-08-27 18:11:49 -0700356 case BATTERY_PROP_CURRENT_AVG:
357 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700358 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700359 getIntField(mHealthdConfig->batteryCurrentAvgPath);
360 ret = NO_ERROR;
361 } else {
362 ret = NAME_NOT_FOUND;
363 }
364 break;
365
Paul Lawrence347c8de2014-03-19 15:04:40 -0700366 case BATTERY_PROP_CAPACITY:
367 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700368 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700369 getIntField(mHealthdConfig->batteryCapacityPath);
370 ret = NO_ERROR;
371 } else {
372 ret = NAME_NOT_FOUND;
373 }
374 break;
375
Todd Poynor8f132af2014-05-08 17:15:45 -0700376 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700377 if (mHealthdConfig->energyCounter) {
378 ret = mHealthdConfig->energyCounter(&val->valueInt64);
379 } else {
380 ret = NAME_NOT_FOUND;
381 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700382 break;
383
Todd Poynorc133b712013-08-14 17:39:13 -0700384 default:
385 break;
386 }
387
Todd Poynorc133b712013-08-14 17:39:13 -0700388 return ret;
389}
390
Todd Poynor020369d2013-09-18 20:09:33 -0700391void BatteryMonitor::dumpState(int fd) {
392 int v;
393 char vs[128];
394
Adrian Roosd5fe6672015-07-10 13:11:01 -0700395 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700396 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700397 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700398 write(fd, vs, strlen(vs));
399 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
400 props.batteryStatus, props.batteryHealth, props.batteryPresent);
401 write(fd, vs, strlen(vs));
402 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
403 props.batteryLevel, props.batteryVoltage,
404 props.batteryTemperature);
405 write(fd, vs, strlen(vs));
406
407 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
408 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
409 snprintf(vs, sizeof(vs), "current now: %d\n", v);
410 write(fd, vs, strlen(vs));
411 }
412
413 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
414 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
415 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
416 write(fd, vs, strlen(vs));
417 }
418
419 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
420 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
421 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
422 write(fd, vs, strlen(vs));
423 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700424
425 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
426 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
427 write(fd, vs, strlen(vs));
428 }
429
430 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
431 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
432 write(fd, vs, strlen(vs));
433 }
434
435 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
436 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
437 write(fd, vs, strlen(vs));
438 }
Todd Poynor020369d2013-09-18 20:09:33 -0700439}
440
Todd Poynorc7464c92013-09-10 12:40:00 -0700441void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700442 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700443 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700444
Todd Poynorf5d30122013-08-12 17:03:35 -0700445 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700446 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
447 if (dir == NULL) {
448 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
449 } else {
450 struct dirent* entry;
451
452 while ((entry = readdir(dir))) {
453 const char* name = entry->d_name;
454
455 if (!strcmp(name, ".") || !strcmp(name, ".."))
456 continue;
457
Todd Poynor752faf22013-06-12 13:25:59 -0700458 // Look for "type" file in each subdirectory
459 path.clear();
460 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
461 switch(readPowerSupplyType(path)) {
462 case ANDROID_POWER_SUPPLY_TYPE_AC:
463 case ANDROID_POWER_SUPPLY_TYPE_USB:
464 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
465 path.clear();
466 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
467 if (access(path.string(), R_OK) == 0)
468 mChargerNames.add(String8(name));
469 break;
470
471 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700472 mBatteryDevicePresent = true;
473
Todd Poynorf5d30122013-08-12 17:03:35 -0700474 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700475 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700476 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
477 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700478 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700479 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700480 }
481
Todd Poynorf5d30122013-08-12 17:03:35 -0700482 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700483 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700484 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
485 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700486 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700487 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700488 }
489
Todd Poynorf5d30122013-08-12 17:03:35 -0700490 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
491 path.clear();
492 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
493 name);
494 if (access(path, R_OK) == 0)
495 mHealthdConfig->batteryPresentPath = path;
496 }
497
498 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
499 path.clear();
500 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
501 name);
502 if (access(path, R_OK) == 0)
503 mHealthdConfig->batteryCapacityPath = path;
504 }
505
506 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
507 path.clear();
508 path.appendFormat("%s/%s/voltage_now",
509 POWER_SUPPLY_SYSFS_PATH, name);
510 if (access(path, R_OK) == 0) {
511 mHealthdConfig->batteryVoltagePath = path;
512 } else {
513 path.clear();
514 path.appendFormat("%s/%s/batt_vol",
515 POWER_SUPPLY_SYSFS_PATH, name);
516 if (access(path, R_OK) == 0)
517 mHealthdConfig->batteryVoltagePath = path;
518 }
519 }
520
Ruchi Kandoicc338802015-08-24 13:01:16 -0700521 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
522 path.clear();
523 path.appendFormat("%s/%s/charge_full",
524 POWER_SUPPLY_SYSFS_PATH, name);
525 if (access(path, R_OK) == 0)
526 mHealthdConfig->batteryFullChargePath = path;
527 }
528
Todd Poynorf5d30122013-08-12 17:03:35 -0700529 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
530 path.clear();
531 path.appendFormat("%s/%s/current_now",
532 POWER_SUPPLY_SYSFS_PATH, name);
533 if (access(path, R_OK) == 0)
534 mHealthdConfig->batteryCurrentNowPath = path;
535 }
536
Ruchi Kandoicc338802015-08-24 13:01:16 -0700537 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
538 path.clear();
539 path.appendFormat("%s/%s/cycle_count",
540 POWER_SUPPLY_SYSFS_PATH, name);
541 if (access(path, R_OK) == 0)
542 mHealthdConfig->batteryCycleCountPath = path;
543 }
544
Todd Poynorbc102112013-08-27 18:11:49 -0700545 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
546 path.clear();
547 path.appendFormat("%s/%s/current_avg",
548 POWER_SUPPLY_SYSFS_PATH, name);
549 if (access(path, R_OK) == 0)
550 mHealthdConfig->batteryCurrentAvgPath = path;
551 }
552
Todd Poynorf5d30122013-08-12 17:03:35 -0700553 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
554 path.clear();
555 path.appendFormat("%s/%s/charge_counter",
556 POWER_SUPPLY_SYSFS_PATH, name);
557 if (access(path, R_OK) == 0)
558 mHealthdConfig->batteryChargeCounterPath = path;
559 }
560
561 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
562 path.clear();
563 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
564 name);
565 if (access(path, R_OK) == 0) {
566 mHealthdConfig->batteryTemperaturePath = path;
567 } else {
568 path.clear();
569 path.appendFormat("%s/%s/batt_temp",
570 POWER_SUPPLY_SYSFS_PATH, name);
571 if (access(path, R_OK) == 0)
572 mHealthdConfig->batteryTemperaturePath = path;
573 }
574 }
575
576 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
577 path.clear();
578 path.appendFormat("%s/%s/technology",
579 POWER_SUPPLY_SYSFS_PATH, name);
580 if (access(path, R_OK) == 0)
581 mHealthdConfig->batteryTechnologyPath = path;
582 }
583
Todd Poynor752faf22013-06-12 13:25:59 -0700584 break;
585
586 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
587 break;
588 }
589 }
590 closedir(dir);
591 }
592
593 if (!mChargerNames.size())
594 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
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