blob: 34019fa677d3108349558cae15ad99e82d379fff [file] [log] [blame]
Todd Poynor752faf22013-06-12 13:25:59 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "healthd"
18
Todd Poynor10b235e2013-08-07 15:25:14 -070019#include "healthd.h"
Todd Poynor752faf22013-06-12 13:25:59 -070020#include "BatteryMonitor.h"
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070029
Todd Poynor752faf22013-06-12 13:25:59 -070030#include <batteryservice/BatteryService.h>
31#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070032#include <cutils/properties.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070033#include <log/log_read.h>
Todd Poynorc133b712013-08-14 17:39:13 -070034#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070035#include <utils/String8.h>
36#include <utils/Vector.h>
37
38#define POWER_SUPPLY_SUBSYSTEM "power_supply"
39#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070040#define FAKE_BATTERY_CAPACITY 42
41#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -070042#define ALWAYS_PLUGGED_CAPACITY 100
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -070043#define MILLION 10000000.0
44#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070045
46namespace android {
47
48struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070049 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070050 int val;
51};
52
53static int mapSysfsString(const char* str,
54 struct sysfsStringEnumMap map[]) {
55 for (int i = 0; map[i].s; i++)
56 if (!strcmp(str, map[i].s))
57 return map[i].val;
58
59 return -1;
60}
61
62int BatteryMonitor::getBatteryStatus(const char* status) {
63 int ret;
64 struct sysfsStringEnumMap batteryStatusMap[] = {
65 { "Unknown", BATTERY_STATUS_UNKNOWN },
66 { "Charging", BATTERY_STATUS_CHARGING },
67 { "Discharging", BATTERY_STATUS_DISCHARGING },
68 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
69 { "Full", BATTERY_STATUS_FULL },
70 { NULL, 0 },
71 };
72
73 ret = mapSysfsString(status, batteryStatusMap);
74 if (ret < 0) {
75 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
76 ret = BATTERY_STATUS_UNKNOWN;
77 }
78
79 return ret;
80}
81
82int BatteryMonitor::getBatteryHealth(const char* status) {
83 int ret;
84 struct sysfsStringEnumMap batteryHealthMap[] = {
85 { "Unknown", BATTERY_HEALTH_UNKNOWN },
86 { "Good", BATTERY_HEALTH_GOOD },
87 { "Overheat", BATTERY_HEALTH_OVERHEAT },
88 { "Dead", BATTERY_HEALTH_DEAD },
89 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
90 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
91 { "Cold", BATTERY_HEALTH_COLD },
92 { NULL, 0 },
93 };
94
95 ret = mapSysfsString(status, batteryHealthMap);
96 if (ret < 0) {
97 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
98 ret = BATTERY_HEALTH_UNKNOWN;
99 }
100
101 return ret;
102}
103
104int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
105 char *cp = NULL;
106
107 if (path.isEmpty())
108 return -1;
109 int fd = open(path.string(), O_RDONLY, 0);
110 if (fd == -1) {
111 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
112 return -1;
113 }
114
115 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
116 if (count > 0)
117 cp = (char *)memrchr(buf, '\n', count);
118
119 if (cp)
120 *cp = '\0';
121 else
122 buf[0] = '\0';
123
124 close(fd);
125 return count;
126}
127
128BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
129 const int SIZE = 128;
130 char buf[SIZE];
131 int length = readFromFile(path, buf, SIZE);
132 BatteryMonitor::PowerSupplyType ret;
133 struct sysfsStringEnumMap supplyTypeMap[] = {
134 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
135 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
136 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
137 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
138 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
139 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
140 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
141 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
142 { "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;
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700190 props.maxChargingVoltage = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700191
Todd Poynorf5d30122013-08-12 17:03:35 -0700192 if (!mHealthdConfig->batteryPresentPath.isEmpty())
193 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700194 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700195 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700196
Todd Poynor3db03a52014-05-21 16:28:13 -0700197 props.batteryLevel = mBatteryFixedCapacity ?
198 mBatteryFixedCapacity :
199 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700200 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700201
Ruchi Kandoicc338802015-08-24 13:01:16 -0700202 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
203 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
204
205 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
206 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
207
208 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
209 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
210
Todd Poynor3db03a52014-05-21 16:28:13 -0700211 props.batteryTemperature = mBatteryFixedTemperature ?
212 mBatteryFixedTemperature :
213 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700214
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700215 // For devices which do not have battery and are always plugged
216 // into power souce.
217 if (mAlwaysPluggedDevice) {
218 props.chargerAcOnline = true;
219 props.batteryPresent = true;
220 props.batteryStatus = BATTERY_STATUS_CHARGING;
221 props.batteryHealth = BATTERY_HEALTH_GOOD;
222 }
223
Todd Poynor752faf22013-06-12 13:25:59 -0700224 const int SIZE = 128;
225 char buf[SIZE];
226 String8 btech;
227
Todd Poynorf5d30122013-08-12 17:03:35 -0700228 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700229 props.batteryStatus = getBatteryStatus(buf);
230
Todd Poynorf5d30122013-08-12 17:03:35 -0700231 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700232 props.batteryHealth = getBatteryHealth(buf);
233
Todd Poynorf5d30122013-08-12 17:03:35 -0700234 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700235 props.batteryTechnology = String8(buf);
236
237 unsigned int i;
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700238 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700239
240 for (i = 0; i < mChargerNames.size(); i++) {
241 String8 path;
242 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
243 mChargerNames[i].string());
244
245 if (readFromFile(path, buf, SIZE) > 0) {
246 if (buf[0] != '0') {
247 path.clear();
248 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
249 mChargerNames[i].string());
250 switch(readPowerSupplyType(path)) {
251 case ANDROID_POWER_SUPPLY_TYPE_AC:
252 props.chargerAcOnline = true;
253 break;
254 case ANDROID_POWER_SUPPLY_TYPE_USB:
255 props.chargerUsbOnline = true;
256 break;
257 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
258 props.chargerWirelessOnline = true;
259 break;
260 default:
261 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
262 mChargerNames[i].string());
263 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700264 path.clear();
265 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
266 mChargerNames[i].string());
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700267 int ChargingCurrent =
268 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
269
270 path.clear();
271 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
272 mChargerNames[i].string());
273
274 int ChargingVoltage =
275 (access(path.string(), R_OK) == 0) ? getIntField(path) :
276 DEFAULT_VBUS_VOLTAGE;
277
278 double power = ((double)ChargingCurrent / MILLION) *
279 ((double)ChargingVoltage / MILLION);
280 if (MaxPower < power) {
281 props.maxChargingCurrent = ChargingCurrent;
282 props.maxChargingVoltage = ChargingVoltage;
283 MaxPower = power;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700284 }
Todd Poynor752faf22013-06-12 13:25:59 -0700285 }
286 }
287 }
288
Todd Poynor10b235e2013-08-07 15:25:14 -0700289 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700290
Todd Poynor10b235e2013-08-07 15:25:14 -0700291 if (logthis) {
292 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700293 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700294 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700295 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700296 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
297 props.batteryLevel, props.batteryVoltage,
298 props.batteryTemperature < 0 ? "-" : "",
299 abs(props.batteryTemperature / 10),
300 abs(props.batteryTemperature % 10), props.batteryHealth,
301 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700302
Ruchi Kandoicc338802015-08-24 13:01:16 -0700303 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700304 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700305 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
306 " c=%d", props.batteryCurrent);
307 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700308
Ruchi Kandoicc338802015-08-24 13:01:16 -0700309 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
310 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
311 " fc=%d", props.batteryFullCharge);
312 }
313
314 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
315 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
316 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700317 }
318 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700319 snprintf(dmesgline, sizeof(dmesgline),
320 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700321 }
322
Ruchi Kandoicc338802015-08-24 13:01:16 -0700323 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700324 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
325 props.chargerAcOnline ? "a" : "",
326 props.chargerUsbOnline ? "u" : "",
327 props.chargerWirelessOnline ? "w" : "");
328
329 log_time realtime(CLOCK_REALTIME);
330 time_t t = realtime.tv_sec;
331 struct tm *tmp = gmtime(&t);
332 if (tmp) {
333 static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC";
334 len = strlen(dmesgline);
335 if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin
336 && strftime(dmesgline + len, sizeof(dmesgline) - len,
337 fmt, tmp)) {
338 char *usec = strchr(dmesgline + len, 'X');
339 if (usec) {
340 len = usec - dmesgline;
341 snprintf(dmesgline + len, sizeof(dmesgline) - len,
342 "%09u", realtime.tv_nsec);
343 usec[9] = ' ';
344 }
345 }
346 }
347
348 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700349 }
350
Todd Poynorc7464c92013-09-10 12:40:00 -0700351 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700352 return props.chargerAcOnline | props.chargerUsbOnline |
353 props.chargerWirelessOnline;
354}
355
Todd Poynorc133b712013-08-14 17:39:13 -0700356status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
357 status_t ret = BAD_VALUE;
358
Todd Poynor8f132af2014-05-08 17:15:45 -0700359 val->valueInt64 = LONG_MIN;
360
Todd Poynorc133b712013-08-14 17:39:13 -0700361 switch(id) {
362 case BATTERY_PROP_CHARGE_COUNTER:
363 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700364 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700365 getIntField(mHealthdConfig->batteryChargeCounterPath);
366 ret = NO_ERROR;
367 } else {
368 ret = NAME_NOT_FOUND;
369 }
370 break;
371
372 case BATTERY_PROP_CURRENT_NOW:
373 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700374 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700375 getIntField(mHealthdConfig->batteryCurrentNowPath);
376 ret = NO_ERROR;
377 } else {
378 ret = NAME_NOT_FOUND;
379 }
380 break;
381
Todd Poynorbc102112013-08-27 18:11:49 -0700382 case BATTERY_PROP_CURRENT_AVG:
383 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700384 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700385 getIntField(mHealthdConfig->batteryCurrentAvgPath);
386 ret = NO_ERROR;
387 } else {
388 ret = NAME_NOT_FOUND;
389 }
390 break;
391
Paul Lawrence347c8de2014-03-19 15:04:40 -0700392 case BATTERY_PROP_CAPACITY:
393 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700394 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700395 getIntField(mHealthdConfig->batteryCapacityPath);
396 ret = NO_ERROR;
397 } else {
398 ret = NAME_NOT_FOUND;
399 }
400 break;
401
Todd Poynor8f132af2014-05-08 17:15:45 -0700402 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700403 if (mHealthdConfig->energyCounter) {
404 ret = mHealthdConfig->energyCounter(&val->valueInt64);
405 } else {
406 ret = NAME_NOT_FOUND;
407 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700408 break;
409
Todd Poynorc133b712013-08-14 17:39:13 -0700410 default:
411 break;
412 }
413
Todd Poynorc133b712013-08-14 17:39:13 -0700414 return ret;
415}
416
Todd Poynor020369d2013-09-18 20:09:33 -0700417void BatteryMonitor::dumpState(int fd) {
418 int v;
419 char vs[128];
420
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700421 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700422 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700423 props.chargerWirelessOnline, props.maxChargingCurrent,
424 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700425 write(fd, vs, strlen(vs));
426 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
427 props.batteryStatus, props.batteryHealth, props.batteryPresent);
428 write(fd, vs, strlen(vs));
429 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
430 props.batteryLevel, props.batteryVoltage,
431 props.batteryTemperature);
432 write(fd, vs, strlen(vs));
433
434 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
435 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
436 snprintf(vs, sizeof(vs), "current now: %d\n", v);
437 write(fd, vs, strlen(vs));
438 }
439
440 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
441 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
442 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
443 write(fd, vs, strlen(vs));
444 }
445
446 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
447 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
448 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
449 write(fd, vs, strlen(vs));
450 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700451
452 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
453 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
454 write(fd, vs, strlen(vs));
455 }
456
457 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
458 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
459 write(fd, vs, strlen(vs));
460 }
461
462 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
463 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
464 write(fd, vs, strlen(vs));
465 }
Todd Poynor020369d2013-09-18 20:09:33 -0700466}
467
Todd Poynorc7464c92013-09-10 12:40:00 -0700468void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700469 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700470 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700471
Todd Poynorf5d30122013-08-12 17:03:35 -0700472 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700473 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
474 if (dir == NULL) {
475 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
476 } else {
477 struct dirent* entry;
478
479 while ((entry = readdir(dir))) {
480 const char* name = entry->d_name;
481
482 if (!strcmp(name, ".") || !strcmp(name, ".."))
483 continue;
484
Todd Poynor752faf22013-06-12 13:25:59 -0700485 // Look for "type" file in each subdirectory
486 path.clear();
487 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
488 switch(readPowerSupplyType(path)) {
489 case ANDROID_POWER_SUPPLY_TYPE_AC:
490 case ANDROID_POWER_SUPPLY_TYPE_USB:
491 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
492 path.clear();
493 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
494 if (access(path.string(), R_OK) == 0)
495 mChargerNames.add(String8(name));
496 break;
497
498 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700499 mBatteryDevicePresent = true;
500
Todd Poynorf5d30122013-08-12 17:03:35 -0700501 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700502 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700503 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
504 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700505 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700506 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700507 }
508
Todd Poynorf5d30122013-08-12 17:03:35 -0700509 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700510 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700511 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
512 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700513 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700514 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700515 }
516
Todd Poynorf5d30122013-08-12 17:03:35 -0700517 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
518 path.clear();
519 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
520 name);
521 if (access(path, R_OK) == 0)
522 mHealthdConfig->batteryPresentPath = path;
523 }
524
525 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
526 path.clear();
527 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
528 name);
529 if (access(path, R_OK) == 0)
530 mHealthdConfig->batteryCapacityPath = path;
531 }
532
533 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
534 path.clear();
535 path.appendFormat("%s/%s/voltage_now",
536 POWER_SUPPLY_SYSFS_PATH, name);
537 if (access(path, R_OK) == 0) {
538 mHealthdConfig->batteryVoltagePath = path;
539 } else {
540 path.clear();
541 path.appendFormat("%s/%s/batt_vol",
542 POWER_SUPPLY_SYSFS_PATH, name);
543 if (access(path, R_OK) == 0)
544 mHealthdConfig->batteryVoltagePath = path;
545 }
546 }
547
Ruchi Kandoicc338802015-08-24 13:01:16 -0700548 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
549 path.clear();
550 path.appendFormat("%s/%s/charge_full",
551 POWER_SUPPLY_SYSFS_PATH, name);
552 if (access(path, R_OK) == 0)
553 mHealthdConfig->batteryFullChargePath = path;
554 }
555
Todd Poynorf5d30122013-08-12 17:03:35 -0700556 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
557 path.clear();
558 path.appendFormat("%s/%s/current_now",
559 POWER_SUPPLY_SYSFS_PATH, name);
560 if (access(path, R_OK) == 0)
561 mHealthdConfig->batteryCurrentNowPath = path;
562 }
563
Ruchi Kandoicc338802015-08-24 13:01:16 -0700564 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
565 path.clear();
566 path.appendFormat("%s/%s/cycle_count",
567 POWER_SUPPLY_SYSFS_PATH, name);
568 if (access(path, R_OK) == 0)
569 mHealthdConfig->batteryCycleCountPath = path;
570 }
571
Todd Poynorbc102112013-08-27 18:11:49 -0700572 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
573 path.clear();
574 path.appendFormat("%s/%s/current_avg",
575 POWER_SUPPLY_SYSFS_PATH, name);
576 if (access(path, R_OK) == 0)
577 mHealthdConfig->batteryCurrentAvgPath = path;
578 }
579
Todd Poynorf5d30122013-08-12 17:03:35 -0700580 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
581 path.clear();
582 path.appendFormat("%s/%s/charge_counter",
583 POWER_SUPPLY_SYSFS_PATH, name);
584 if (access(path, R_OK) == 0)
585 mHealthdConfig->batteryChargeCounterPath = path;
586 }
587
588 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
589 path.clear();
590 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
591 name);
592 if (access(path, R_OK) == 0) {
593 mHealthdConfig->batteryTemperaturePath = path;
594 } else {
595 path.clear();
596 path.appendFormat("%s/%s/batt_temp",
597 POWER_SUPPLY_SYSFS_PATH, name);
598 if (access(path, R_OK) == 0)
599 mHealthdConfig->batteryTemperaturePath = path;
600 }
601 }
602
603 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
604 path.clear();
605 path.appendFormat("%s/%s/technology",
606 POWER_SUPPLY_SYSFS_PATH, name);
607 if (access(path, R_OK) == 0)
608 mHealthdConfig->batteryTechnologyPath = path;
609 }
610
Todd Poynor752faf22013-06-12 13:25:59 -0700611 break;
612
613 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
614 break;
615 }
616 }
617 closedir(dir);
618 }
619
Ian Pedowitz585ab652015-10-12 19:01:00 -0700620 // This indicates that there is no charger driver registered.
621 // Typically the case for devices which do not have a battery and
622 // and are always plugged into AC mains.
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700623 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700624 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700625 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
626 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
627 mAlwaysPluggedDevice = true;
628 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700629 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700630 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700631 hc->periodic_chores_interval_fast = -1;
632 hc->periodic_chores_interval_slow = -1;
633 } else {
634 if (mHealthdConfig->batteryStatusPath.isEmpty())
635 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
636 if (mHealthdConfig->batteryHealthPath.isEmpty())
637 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
638 if (mHealthdConfig->batteryPresentPath.isEmpty())
639 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
640 if (mHealthdConfig->batteryCapacityPath.isEmpty())
641 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
642 if (mHealthdConfig->batteryVoltagePath.isEmpty())
643 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
644 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
645 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
646 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
647 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700648 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700649 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
650 if (mHealthdConfig->batteryFullChargePath.isEmpty())
651 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
652 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
653 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700654 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700655
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700656 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
657 && strtol(pval, NULL, 10) != 0) {
658 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
659 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
660 }
Todd Poynor752faf22013-06-12 13:25:59 -0700661}
662
663}; // namespace android