blob: 582821d2194452c1aed7839cfbacdfcc55e45f32 [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 },
Benson Leung8a4eef62015-10-07 16:12:04 -0700142 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
143 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
144 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700145 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
146 { NULL, 0 },
147 };
148
149 if (length <= 0)
150 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
151
152 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
153 if (ret < 0)
154 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
155
156 return ret;
157}
158
159bool BatteryMonitor::getBooleanField(const String8& path) {
160 const int SIZE = 16;
161 char buf[SIZE];
162
163 bool value = false;
164 if (readFromFile(path, buf, SIZE) > 0) {
165 if (buf[0] != '0') {
166 value = true;
167 }
168 }
169
170 return value;
171}
172
173int BatteryMonitor::getIntField(const String8& path) {
174 const int SIZE = 128;
175 char buf[SIZE];
176
177 int value = 0;
178 if (readFromFile(path, buf, SIZE) > 0) {
179 value = strtol(buf, NULL, 0);
180 }
181 return value;
182}
183
184bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700185 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700186
187 props.chargerAcOnline = false;
188 props.chargerUsbOnline = false;
189 props.chargerWirelessOnline = false;
190 props.batteryStatus = BATTERY_STATUS_UNKNOWN;
191 props.batteryHealth = BATTERY_HEALTH_UNKNOWN;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700192 props.maxChargingCurrent = 0;
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700193 props.maxChargingVoltage = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700194
Todd Poynorf5d30122013-08-12 17:03:35 -0700195 if (!mHealthdConfig->batteryPresentPath.isEmpty())
196 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700197 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700198 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700199
Todd Poynor3db03a52014-05-21 16:28:13 -0700200 props.batteryLevel = mBatteryFixedCapacity ?
201 mBatteryFixedCapacity :
202 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700203 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700204
Ruchi Kandoicc338802015-08-24 13:01:16 -0700205 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
206 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
207
208 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
209 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
210
211 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
212 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
213
Todd Poynor3db03a52014-05-21 16:28:13 -0700214 props.batteryTemperature = mBatteryFixedTemperature ?
215 mBatteryFixedTemperature :
216 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700217
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700218 // For devices which do not have battery and are always plugged
219 // into power souce.
220 if (mAlwaysPluggedDevice) {
221 props.chargerAcOnline = true;
222 props.batteryPresent = true;
223 props.batteryStatus = BATTERY_STATUS_CHARGING;
224 props.batteryHealth = BATTERY_HEALTH_GOOD;
225 }
226
Todd Poynor752faf22013-06-12 13:25:59 -0700227 const int SIZE = 128;
228 char buf[SIZE];
229 String8 btech;
230
Todd Poynorf5d30122013-08-12 17:03:35 -0700231 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700232 props.batteryStatus = getBatteryStatus(buf);
233
Todd Poynorf5d30122013-08-12 17:03:35 -0700234 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700235 props.batteryHealth = getBatteryHealth(buf);
236
Todd Poynorf5d30122013-08-12 17:03:35 -0700237 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700238 props.batteryTechnology = String8(buf);
239
240 unsigned int i;
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700241 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700242
243 for (i = 0; i < mChargerNames.size(); i++) {
244 String8 path;
245 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
246 mChargerNames[i].string());
247
248 if (readFromFile(path, buf, SIZE) > 0) {
249 if (buf[0] != '0') {
250 path.clear();
251 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
252 mChargerNames[i].string());
253 switch(readPowerSupplyType(path)) {
254 case ANDROID_POWER_SUPPLY_TYPE_AC:
255 props.chargerAcOnline = true;
256 break;
257 case ANDROID_POWER_SUPPLY_TYPE_USB:
258 props.chargerUsbOnline = true;
259 break;
260 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
261 props.chargerWirelessOnline = true;
262 break;
263 default:
264 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
265 mChargerNames[i].string());
266 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700267 path.clear();
268 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
269 mChargerNames[i].string());
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700270 int ChargingCurrent =
271 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
272
273 path.clear();
274 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
275 mChargerNames[i].string());
276
277 int ChargingVoltage =
278 (access(path.string(), R_OK) == 0) ? getIntField(path) :
279 DEFAULT_VBUS_VOLTAGE;
280
281 double power = ((double)ChargingCurrent / MILLION) *
282 ((double)ChargingVoltage / MILLION);
283 if (MaxPower < power) {
284 props.maxChargingCurrent = ChargingCurrent;
285 props.maxChargingVoltage = ChargingVoltage;
286 MaxPower = power;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700287 }
Todd Poynor752faf22013-06-12 13:25:59 -0700288 }
289 }
290 }
291
Todd Poynor10b235e2013-08-07 15:25:14 -0700292 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700293
Todd Poynor10b235e2013-08-07 15:25:14 -0700294 if (logthis) {
295 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700296 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700297 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700298 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700299 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
300 props.batteryLevel, props.batteryVoltage,
301 props.batteryTemperature < 0 ? "-" : "",
302 abs(props.batteryTemperature / 10),
303 abs(props.batteryTemperature % 10), props.batteryHealth,
304 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700305
Ruchi Kandoicc338802015-08-24 13:01:16 -0700306 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700307 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700308 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
309 " c=%d", props.batteryCurrent);
310 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700311
Ruchi Kandoicc338802015-08-24 13:01:16 -0700312 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
313 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
314 " fc=%d", props.batteryFullCharge);
315 }
316
317 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
318 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
319 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700320 }
321 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700322 snprintf(dmesgline, sizeof(dmesgline),
323 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700324 }
325
Ruchi Kandoicc338802015-08-24 13:01:16 -0700326 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700327 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
328 props.chargerAcOnline ? "a" : "",
329 props.chargerUsbOnline ? "u" : "",
330 props.chargerWirelessOnline ? "w" : "");
331
332 log_time realtime(CLOCK_REALTIME);
333 time_t t = realtime.tv_sec;
334 struct tm *tmp = gmtime(&t);
335 if (tmp) {
336 static const char fmt[] = " %Y-%m-%d %H:%M:%S.XXXXXXXXX UTC";
337 len = strlen(dmesgline);
338 if ((len < (sizeof(dmesgline) - sizeof(fmt) - 8)) // margin
339 && strftime(dmesgline + len, sizeof(dmesgline) - len,
340 fmt, tmp)) {
341 char *usec = strchr(dmesgline + len, 'X');
342 if (usec) {
343 len = usec - dmesgline;
344 snprintf(dmesgline + len, sizeof(dmesgline) - len,
345 "%09u", realtime.tv_nsec);
346 usec[9] = ' ';
347 }
348 }
349 }
350
351 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700352 }
353
Todd Poynorc7464c92013-09-10 12:40:00 -0700354 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700355 return props.chargerAcOnline | props.chargerUsbOnline |
356 props.chargerWirelessOnline;
357}
358
Todd Poynorc133b712013-08-14 17:39:13 -0700359status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
360 status_t ret = BAD_VALUE;
361
Todd Poynor8f132af2014-05-08 17:15:45 -0700362 val->valueInt64 = LONG_MIN;
363
Todd Poynorc133b712013-08-14 17:39:13 -0700364 switch(id) {
365 case BATTERY_PROP_CHARGE_COUNTER:
366 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700367 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700368 getIntField(mHealthdConfig->batteryChargeCounterPath);
369 ret = NO_ERROR;
370 } else {
371 ret = NAME_NOT_FOUND;
372 }
373 break;
374
375 case BATTERY_PROP_CURRENT_NOW:
376 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700377 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700378 getIntField(mHealthdConfig->batteryCurrentNowPath);
379 ret = NO_ERROR;
380 } else {
381 ret = NAME_NOT_FOUND;
382 }
383 break;
384
Todd Poynorbc102112013-08-27 18:11:49 -0700385 case BATTERY_PROP_CURRENT_AVG:
386 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700387 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700388 getIntField(mHealthdConfig->batteryCurrentAvgPath);
389 ret = NO_ERROR;
390 } else {
391 ret = NAME_NOT_FOUND;
392 }
393 break;
394
Paul Lawrence347c8de2014-03-19 15:04:40 -0700395 case BATTERY_PROP_CAPACITY:
396 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700397 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700398 getIntField(mHealthdConfig->batteryCapacityPath);
399 ret = NO_ERROR;
400 } else {
401 ret = NAME_NOT_FOUND;
402 }
403 break;
404
Todd Poynor8f132af2014-05-08 17:15:45 -0700405 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700406 if (mHealthdConfig->energyCounter) {
407 ret = mHealthdConfig->energyCounter(&val->valueInt64);
408 } else {
409 ret = NAME_NOT_FOUND;
410 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700411 break;
412
Todd Poynorc133b712013-08-14 17:39:13 -0700413 default:
414 break;
415 }
416
Todd Poynorc133b712013-08-14 17:39:13 -0700417 return ret;
418}
419
Todd Poynor020369d2013-09-18 20:09:33 -0700420void BatteryMonitor::dumpState(int fd) {
421 int v;
422 char vs[128];
423
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700424 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700425 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan42d91332015-10-27 10:43:53 -0700426 props.chargerWirelessOnline, props.maxChargingCurrent,
427 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700428 write(fd, vs, strlen(vs));
429 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
430 props.batteryStatus, props.batteryHealth, props.batteryPresent);
431 write(fd, vs, strlen(vs));
432 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
433 props.batteryLevel, props.batteryVoltage,
434 props.batteryTemperature);
435 write(fd, vs, strlen(vs));
436
437 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
438 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
439 snprintf(vs, sizeof(vs), "current now: %d\n", v);
440 write(fd, vs, strlen(vs));
441 }
442
443 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
444 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
445 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
446 write(fd, vs, strlen(vs));
447 }
448
449 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
450 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
451 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
452 write(fd, vs, strlen(vs));
453 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700454
455 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
456 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
457 write(fd, vs, strlen(vs));
458 }
459
460 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
461 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
462 write(fd, vs, strlen(vs));
463 }
464
465 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
466 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
467 write(fd, vs, strlen(vs));
468 }
Todd Poynor020369d2013-09-18 20:09:33 -0700469}
470
Todd Poynorc7464c92013-09-10 12:40:00 -0700471void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700472 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700473 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700474
Todd Poynorf5d30122013-08-12 17:03:35 -0700475 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700476 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
477 if (dir == NULL) {
478 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
479 } else {
480 struct dirent* entry;
481
482 while ((entry = readdir(dir))) {
483 const char* name = entry->d_name;
484
485 if (!strcmp(name, ".") || !strcmp(name, ".."))
486 continue;
487
Todd Poynor752faf22013-06-12 13:25:59 -0700488 // Look for "type" file in each subdirectory
489 path.clear();
490 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
491 switch(readPowerSupplyType(path)) {
492 case ANDROID_POWER_SUPPLY_TYPE_AC:
493 case ANDROID_POWER_SUPPLY_TYPE_USB:
494 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
495 path.clear();
496 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
497 if (access(path.string(), R_OK) == 0)
498 mChargerNames.add(String8(name));
499 break;
500
501 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700502 mBatteryDevicePresent = true;
503
Todd Poynorf5d30122013-08-12 17:03:35 -0700504 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700505 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700506 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
507 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700508 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700509 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700510 }
511
Todd Poynorf5d30122013-08-12 17:03:35 -0700512 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700513 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700514 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
515 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700516 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700517 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700518 }
519
Todd Poynorf5d30122013-08-12 17:03:35 -0700520 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
521 path.clear();
522 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
523 name);
524 if (access(path, R_OK) == 0)
525 mHealthdConfig->batteryPresentPath = path;
526 }
527
528 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
529 path.clear();
530 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
531 name);
532 if (access(path, R_OK) == 0)
533 mHealthdConfig->batteryCapacityPath = path;
534 }
535
536 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
537 path.clear();
538 path.appendFormat("%s/%s/voltage_now",
539 POWER_SUPPLY_SYSFS_PATH, name);
540 if (access(path, R_OK) == 0) {
541 mHealthdConfig->batteryVoltagePath = path;
542 } else {
543 path.clear();
544 path.appendFormat("%s/%s/batt_vol",
545 POWER_SUPPLY_SYSFS_PATH, name);
546 if (access(path, R_OK) == 0)
547 mHealthdConfig->batteryVoltagePath = path;
548 }
549 }
550
Ruchi Kandoicc338802015-08-24 13:01:16 -0700551 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
552 path.clear();
553 path.appendFormat("%s/%s/charge_full",
554 POWER_SUPPLY_SYSFS_PATH, name);
555 if (access(path, R_OK) == 0)
556 mHealthdConfig->batteryFullChargePath = path;
557 }
558
Todd Poynorf5d30122013-08-12 17:03:35 -0700559 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
560 path.clear();
561 path.appendFormat("%s/%s/current_now",
562 POWER_SUPPLY_SYSFS_PATH, name);
563 if (access(path, R_OK) == 0)
564 mHealthdConfig->batteryCurrentNowPath = path;
565 }
566
Ruchi Kandoicc338802015-08-24 13:01:16 -0700567 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
568 path.clear();
569 path.appendFormat("%s/%s/cycle_count",
570 POWER_SUPPLY_SYSFS_PATH, name);
571 if (access(path, R_OK) == 0)
572 mHealthdConfig->batteryCycleCountPath = path;
573 }
574
Todd Poynorbc102112013-08-27 18:11:49 -0700575 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
576 path.clear();
577 path.appendFormat("%s/%s/current_avg",
578 POWER_SUPPLY_SYSFS_PATH, name);
579 if (access(path, R_OK) == 0)
580 mHealthdConfig->batteryCurrentAvgPath = path;
581 }
582
Todd Poynorf5d30122013-08-12 17:03:35 -0700583 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
584 path.clear();
585 path.appendFormat("%s/%s/charge_counter",
586 POWER_SUPPLY_SYSFS_PATH, name);
587 if (access(path, R_OK) == 0)
588 mHealthdConfig->batteryChargeCounterPath = path;
589 }
590
591 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
592 path.clear();
593 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
594 name);
595 if (access(path, R_OK) == 0) {
596 mHealthdConfig->batteryTemperaturePath = path;
597 } else {
598 path.clear();
599 path.appendFormat("%s/%s/batt_temp",
600 POWER_SUPPLY_SYSFS_PATH, name);
601 if (access(path, R_OK) == 0)
602 mHealthdConfig->batteryTemperaturePath = path;
603 }
604 }
605
606 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
607 path.clear();
608 path.appendFormat("%s/%s/technology",
609 POWER_SUPPLY_SYSFS_PATH, name);
610 if (access(path, R_OK) == 0)
611 mHealthdConfig->batteryTechnologyPath = path;
612 }
613
Todd Poynor752faf22013-06-12 13:25:59 -0700614 break;
615
616 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
617 break;
618 }
619 }
620 closedir(dir);
621 }
622
Ian Pedowitz585ab652015-10-12 19:01:00 -0700623 // This indicates that there is no charger driver registered.
624 // Typically the case for devices which do not have a battery and
625 // and are always plugged into AC mains.
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700626 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700627 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700628 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
629 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
630 mAlwaysPluggedDevice = true;
631 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700632 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700633 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700634 hc->periodic_chores_interval_fast = -1;
635 hc->periodic_chores_interval_slow = -1;
636 } else {
637 if (mHealthdConfig->batteryStatusPath.isEmpty())
638 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
639 if (mHealthdConfig->batteryHealthPath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
641 if (mHealthdConfig->batteryPresentPath.isEmpty())
642 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
643 if (mHealthdConfig->batteryCapacityPath.isEmpty())
644 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
645 if (mHealthdConfig->batteryVoltagePath.isEmpty())
646 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
647 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
648 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
649 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
650 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700651 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700652 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
653 if (mHealthdConfig->batteryFullChargePath.isEmpty())
654 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
655 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
656 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700657 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700658
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700659 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
660 && strtol(pval, NULL, 10) != 0) {
661 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
662 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
663 }
Todd Poynor752faf22013-06-12 13:25:59 -0700664}
665
666}; // namespace android