blob: c75ed1394ea5443e1f5ad467aec0492ffa416eb2 [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>
27#include <unistd.h>
28#include <batteryservice/BatteryService.h>
29#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070030#include <cutils/properties.h>
Todd Poynore14b37e2014-05-20 13:54:40 -070031#include <sys/types.h>
Todd Poynorc133b712013-08-14 17:39:13 -070032#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070033#include <utils/String8.h>
34#include <utils/Vector.h>
35
36#define POWER_SUPPLY_SUBSYSTEM "power_supply"
37#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070038#define FAKE_BATTERY_CAPACITY 42
39#define FAKE_BATTERY_TEMPERATURE 424
Todd Poynor752faf22013-06-12 13:25:59 -070040
41namespace android {
42
43struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070044 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070045 int val;
46};
47
48static int mapSysfsString(const char* str,
49 struct sysfsStringEnumMap map[]) {
50 for (int i = 0; map[i].s; i++)
51 if (!strcmp(str, map[i].s))
52 return map[i].val;
53
54 return -1;
55}
56
57int BatteryMonitor::getBatteryStatus(const char* status) {
58 int ret;
59 struct sysfsStringEnumMap batteryStatusMap[] = {
60 { "Unknown", BATTERY_STATUS_UNKNOWN },
61 { "Charging", BATTERY_STATUS_CHARGING },
62 { "Discharging", BATTERY_STATUS_DISCHARGING },
63 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
64 { "Full", BATTERY_STATUS_FULL },
65 { NULL, 0 },
66 };
67
68 ret = mapSysfsString(status, batteryStatusMap);
69 if (ret < 0) {
70 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
71 ret = BATTERY_STATUS_UNKNOWN;
72 }
73
74 return ret;
75}
76
77int BatteryMonitor::getBatteryHealth(const char* status) {
78 int ret;
79 struct sysfsStringEnumMap batteryHealthMap[] = {
80 { "Unknown", BATTERY_HEALTH_UNKNOWN },
81 { "Good", BATTERY_HEALTH_GOOD },
82 { "Overheat", BATTERY_HEALTH_OVERHEAT },
83 { "Dead", BATTERY_HEALTH_DEAD },
84 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
85 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
86 { "Cold", BATTERY_HEALTH_COLD },
87 { NULL, 0 },
88 };
89
90 ret = mapSysfsString(status, batteryHealthMap);
91 if (ret < 0) {
92 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
93 ret = BATTERY_HEALTH_UNKNOWN;
94 }
95
96 return ret;
97}
98
99int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
100 char *cp = NULL;
101
102 if (path.isEmpty())
103 return -1;
104 int fd = open(path.string(), O_RDONLY, 0);
105 if (fd == -1) {
106 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
107 return -1;
108 }
109
110 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
111 if (count > 0)
112 cp = (char *)memrchr(buf, '\n', count);
113
114 if (cp)
115 *cp = '\0';
116 else
117 buf[0] = '\0';
118
119 close(fd);
120 return count;
121}
122
123BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
124 const int SIZE = 128;
125 char buf[SIZE];
126 int length = readFromFile(path, buf, SIZE);
127 BatteryMonitor::PowerSupplyType ret;
128 struct sysfsStringEnumMap supplyTypeMap[] = {
129 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
130 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
131 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
132 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
133 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
134 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
135 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
136 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
137 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
138 { NULL, 0 },
139 };
140
141 if (length <= 0)
142 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
143
144 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
145 if (ret < 0)
146 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
147
148 return ret;
149}
150
151bool BatteryMonitor::getBooleanField(const String8& path) {
152 const int SIZE = 16;
153 char buf[SIZE];
154
155 bool value = false;
156 if (readFromFile(path, buf, SIZE) > 0) {
157 if (buf[0] != '0') {
158 value = true;
159 }
160 }
161
162 return value;
163}
164
165int BatteryMonitor::getIntField(const String8& path) {
166 const int SIZE = 128;
167 char buf[SIZE];
168
169 int value = 0;
170 if (readFromFile(path, buf, SIZE) > 0) {
171 value = strtol(buf, NULL, 0);
172 }
173 return value;
174}
175
176bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700177 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700178
179 props.chargerAcOnline = false;
180 props.chargerUsbOnline = false;
181 props.chargerWirelessOnline = false;
182 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
183 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700184 props.maxChargingCurrent = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700185
Todd Poynorf5d30122013-08-12 17:03:35 -0700186 if (!mHealthdConfig->batteryPresentPath.isEmpty())
187 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700188 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700189 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700190
Todd Poynor3db03a52014-05-21 16:28:13 -0700191 props.batteryLevel = mBatteryFixedCapacity ?
192 mBatteryFixedCapacity :
193 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700194 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700195
Todd Poynor3db03a52014-05-21 16:28:13 -0700196 props.batteryTemperature = mBatteryFixedTemperature ?
197 mBatteryFixedTemperature :
198 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700199
200 const int SIZE = 128;
201 char buf[SIZE];
202 String8 btech;
203
Todd Poynorf5d30122013-08-12 17:03:35 -0700204 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700205 props.batteryStatus = getBatteryStatus(buf);
206
Todd Poynorf5d30122013-08-12 17:03:35 -0700207 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700208 props.batteryHealth = getBatteryHealth(buf);
209
Todd Poynorf5d30122013-08-12 17:03:35 -0700210 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700211 props.batteryTechnology = String8(buf);
212
213 unsigned int i;
214
215 for (i = 0; i < mChargerNames.size(); i++) {
216 String8 path;
217 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
218 mChargerNames[i].string());
219
220 if (readFromFile(path, buf, SIZE) > 0) {
221 if (buf[0] != '0') {
222 path.clear();
223 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
224 mChargerNames[i].string());
225 switch(readPowerSupplyType(path)) {
226 case ANDROID_POWER_SUPPLY_TYPE_AC:
227 props.chargerAcOnline = true;
228 break;
229 case ANDROID_POWER_SUPPLY_TYPE_USB:
230 props.chargerUsbOnline = true;
231 break;
232 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
233 props.chargerWirelessOnline = true;
234 break;
235 default:
236 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
237 mChargerNames[i].string());
238 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700239 path.clear();
240 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
241 mChargerNames[i].string());
242 if (access(path.string(), R_OK) == 0) {
243 int maxChargingCurrent = getIntField(path);
244 if (props.maxChargingCurrent < maxChargingCurrent) {
245 props.maxChargingCurrent = maxChargingCurrent;
246 }
247 }
Todd Poynor752faf22013-06-12 13:25:59 -0700248 }
249 }
250 }
251
Todd Poynor10b235e2013-08-07 15:25:14 -0700252 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700253
Todd Poynor10b235e2013-08-07 15:25:14 -0700254 if (logthis) {
255 char dmesgline[256];
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700256
Todd Poynorc15e9932013-10-22 18:21:27 -0700257 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700258 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700259 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
260 props.batteryLevel, props.batteryVoltage,
261 props.batteryTemperature < 0 ? "-" : "",
262 abs(props.batteryTemperature / 10),
263 abs(props.batteryTemperature % 10), props.batteryHealth,
264 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700265
266 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
267 int c = getIntField(mHealthdConfig->batteryCurrentNowPath);
268 char b[20];
269
270 snprintf(b, sizeof(b), " c=%d", c / 1000);
271 strlcat(dmesgline, b, sizeof(dmesgline));
272 }
273 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700274 snprintf(dmesgline, sizeof(dmesgline),
275 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700276 }
277
Todd Poynorebeb0c02014-09-23 14:54:24 -0700278 KLOG_WARNING(LOG_TAG, "%s chg=%s%s%s\n", dmesgline,
279 props.chargerAcOnline ? "a" : "",
280 props.chargerUsbOnline ? "u" : "",
281 props.chargerWirelessOnline ? "w" : "");
Todd Poynorb45f1f52013-07-30 18:57:16 -0700282 }
283
Todd Poynorc7464c92013-09-10 12:40:00 -0700284 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700285 return props.chargerAcOnline | props.chargerUsbOnline |
286 props.chargerWirelessOnline;
287}
288
Todd Poynorc133b712013-08-14 17:39:13 -0700289status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
290 status_t ret = BAD_VALUE;
291
Todd Poynor8f132af2014-05-08 17:15:45 -0700292 val->valueInt64 = LONG_MIN;
293
Todd Poynorc133b712013-08-14 17:39:13 -0700294 switch(id) {
295 case BATTERY_PROP_CHARGE_COUNTER:
296 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700297 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700298 getIntField(mHealthdConfig->batteryChargeCounterPath);
299 ret = NO_ERROR;
300 } else {
301 ret = NAME_NOT_FOUND;
302 }
303 break;
304
305 case BATTERY_PROP_CURRENT_NOW:
306 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700307 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700308 getIntField(mHealthdConfig->batteryCurrentNowPath);
309 ret = NO_ERROR;
310 } else {
311 ret = NAME_NOT_FOUND;
312 }
313 break;
314
Todd Poynorbc102112013-08-27 18:11:49 -0700315 case BATTERY_PROP_CURRENT_AVG:
316 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700317 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700318 getIntField(mHealthdConfig->batteryCurrentAvgPath);
319 ret = NO_ERROR;
320 } else {
321 ret = NAME_NOT_FOUND;
322 }
323 break;
324
Paul Lawrence347c8de2014-03-19 15:04:40 -0700325 case BATTERY_PROP_CAPACITY:
326 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700327 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700328 getIntField(mHealthdConfig->batteryCapacityPath);
329 ret = NO_ERROR;
330 } else {
331 ret = NAME_NOT_FOUND;
332 }
333 break;
334
Todd Poynor8f132af2014-05-08 17:15:45 -0700335 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700336 if (mHealthdConfig->energyCounter) {
337 ret = mHealthdConfig->energyCounter(&val->valueInt64);
338 } else {
339 ret = NAME_NOT_FOUND;
340 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700341 break;
342
Todd Poynorc133b712013-08-14 17:39:13 -0700343 default:
344 break;
345 }
346
Todd Poynorc133b712013-08-14 17:39:13 -0700347 return ret;
348}
349
Todd Poynor020369d2013-09-18 20:09:33 -0700350void BatteryMonitor::dumpState(int fd) {
351 int v;
352 char vs[128];
353
Adrian Roosd5fe6672015-07-10 13:11:01 -0700354 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700355 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700356 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700357 write(fd, vs, strlen(vs));
358 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
359 props.batteryStatus, props.batteryHealth, props.batteryPresent);
360 write(fd, vs, strlen(vs));
361 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
362 props.batteryLevel, props.batteryVoltage,
363 props.batteryTemperature);
364 write(fd, vs, strlen(vs));
365
366 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
367 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
368 snprintf(vs, sizeof(vs), "current now: %d\n", v);
369 write(fd, vs, strlen(vs));
370 }
371
372 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
373 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
374 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
375 write(fd, vs, strlen(vs));
376 }
377
378 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
379 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
380 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
381 write(fd, vs, strlen(vs));
382 }
383}
384
Todd Poynorc7464c92013-09-10 12:40:00 -0700385void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700386 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700387 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700388
Todd Poynorf5d30122013-08-12 17:03:35 -0700389 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700390 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
391 if (dir == NULL) {
392 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
393 } else {
394 struct dirent* entry;
395
396 while ((entry = readdir(dir))) {
397 const char* name = entry->d_name;
398
399 if (!strcmp(name, ".") || !strcmp(name, ".."))
400 continue;
401
Todd Poynor752faf22013-06-12 13:25:59 -0700402 // Look for "type" file in each subdirectory
403 path.clear();
404 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
405 switch(readPowerSupplyType(path)) {
406 case ANDROID_POWER_SUPPLY_TYPE_AC:
407 case ANDROID_POWER_SUPPLY_TYPE_USB:
408 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
409 path.clear();
410 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
411 if (access(path.string(), R_OK) == 0)
412 mChargerNames.add(String8(name));
413 break;
414
415 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700416 mBatteryDevicePresent = true;
417
Todd Poynorf5d30122013-08-12 17:03:35 -0700418 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700419 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700420 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
421 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700422 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700423 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700424 }
425
Todd Poynorf5d30122013-08-12 17:03:35 -0700426 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700427 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700428 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
429 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700430 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700431 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700432 }
433
Todd Poynorf5d30122013-08-12 17:03:35 -0700434 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
435 path.clear();
436 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
437 name);
438 if (access(path, R_OK) == 0)
439 mHealthdConfig->batteryPresentPath = path;
440 }
441
442 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
443 path.clear();
444 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
445 name);
446 if (access(path, R_OK) == 0)
447 mHealthdConfig->batteryCapacityPath = path;
448 }
449
450 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
451 path.clear();
452 path.appendFormat("%s/%s/voltage_now",
453 POWER_SUPPLY_SYSFS_PATH, name);
454 if (access(path, R_OK) == 0) {
455 mHealthdConfig->batteryVoltagePath = path;
456 } else {
457 path.clear();
458 path.appendFormat("%s/%s/batt_vol",
459 POWER_SUPPLY_SYSFS_PATH, name);
460 if (access(path, R_OK) == 0)
461 mHealthdConfig->batteryVoltagePath = path;
462 }
463 }
464
465 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
466 path.clear();
467 path.appendFormat("%s/%s/current_now",
468 POWER_SUPPLY_SYSFS_PATH, name);
469 if (access(path, R_OK) == 0)
470 mHealthdConfig->batteryCurrentNowPath = path;
471 }
472
Todd Poynorbc102112013-08-27 18:11:49 -0700473 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
474 path.clear();
475 path.appendFormat("%s/%s/current_avg",
476 POWER_SUPPLY_SYSFS_PATH, name);
477 if (access(path, R_OK) == 0)
478 mHealthdConfig->batteryCurrentAvgPath = path;
479 }
480
Todd Poynorf5d30122013-08-12 17:03:35 -0700481 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
482 path.clear();
483 path.appendFormat("%s/%s/charge_counter",
484 POWER_SUPPLY_SYSFS_PATH, name);
485 if (access(path, R_OK) == 0)
486 mHealthdConfig->batteryChargeCounterPath = path;
487 }
488
489 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
490 path.clear();
491 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
492 name);
493 if (access(path, R_OK) == 0) {
494 mHealthdConfig->batteryTemperaturePath = path;
495 } else {
496 path.clear();
497 path.appendFormat("%s/%s/batt_temp",
498 POWER_SUPPLY_SYSFS_PATH, name);
499 if (access(path, R_OK) == 0)
500 mHealthdConfig->batteryTemperaturePath = path;
501 }
502 }
503
504 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
505 path.clear();
506 path.appendFormat("%s/%s/technology",
507 POWER_SUPPLY_SYSFS_PATH, name);
508 if (access(path, R_OK) == 0)
509 mHealthdConfig->batteryTechnologyPath = path;
510 }
511
Todd Poynor752faf22013-06-12 13:25:59 -0700512 break;
513
514 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
515 break;
516 }
517 }
518 closedir(dir);
519 }
520
521 if (!mChargerNames.size())
522 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700523 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700524 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700525 hc->periodic_chores_interval_fast = -1;
526 hc->periodic_chores_interval_slow = -1;
527 } else {
528 if (mHealthdConfig->batteryStatusPath.isEmpty())
529 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
530 if (mHealthdConfig->batteryHealthPath.isEmpty())
531 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
532 if (mHealthdConfig->batteryPresentPath.isEmpty())
533 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
534 if (mHealthdConfig->batteryCapacityPath.isEmpty())
535 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
536 if (mHealthdConfig->batteryVoltagePath.isEmpty())
537 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
538 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
539 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
540 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
541 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
542 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700543
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700544 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
545 && strtol(pval, NULL, 10) != 0) {
546 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
547 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
548 }
Todd Poynor752faf22013-06-12 13:25:59 -0700549}
550
551}; // namespace android