blob: 48af70ea7204d0303dfe2ae74591a56b6119ab52 [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
Yabin Cuif6dbc6d2016-02-17 12:21:34 -080019#include <healthd/healthd.h>
20#include <healthd/BatteryMonitor.h>
Todd Poynor752faf22013-06-12 13:25:59 -070021
22#include <dirent.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <stdlib.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070027#include <sys/types.h>
Todd Poynor752faf22013-06-12 13:25:59 -070028#include <unistd.h>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070029
Todd Poynor752faf22013-06-12 13:25:59 -070030#include <batteryservice/BatteryService.h>
31#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070032#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070033#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070034#include <utils/String8.h>
35#include <utils/Vector.h>
36
37#define POWER_SUPPLY_SUBSYSTEM "power_supply"
38#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070039#define FAKE_BATTERY_CAPACITY 42
40#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -070041#define ALWAYS_PLUGGED_CAPACITY 100
Todd Poynor752faf22013-06-12 13:25:59 -070042
43namespace android {
44
45struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070046 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070047 int val;
48};
49
50static int mapSysfsString(const char* str,
51 struct sysfsStringEnumMap map[]) {
52 for (int i = 0; map[i].s; i++)
53 if (!strcmp(str, map[i].s))
54 return map[i].val;
55
56 return -1;
57}
58
Yabin Cuiac3387b2016-02-16 17:19:23 -080059static void initBatteryProperties(BatteryProperties* props) {
60 props->chargerAcOnline = false;
61 props->chargerUsbOnline = false;
62 props->chargerWirelessOnline = false;
63 props->maxChargingCurrent = 0;
64 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
65 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
66 props->batteryPresent = false;
67 props->batteryLevel = 0;
68 props->batteryVoltage = 0;
69 props->batteryTemperature = 0;
70 props->batteryCurrent = 0;
71 props->batteryCycleCount = 0;
72 props->batteryFullCharge = 0;
73 props->batteryTechnology.clear();
74}
75
76BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
77 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
78 initBatteryProperties(&props);
79}
80
Todd Poynor752faf22013-06-12 13:25:59 -070081int BatteryMonitor::getBatteryStatus(const char* status) {
82 int ret;
83 struct sysfsStringEnumMap batteryStatusMap[] = {
84 { "Unknown", BATTERY_STATUS_UNKNOWN },
85 { "Charging", BATTERY_STATUS_CHARGING },
86 { "Discharging", BATTERY_STATUS_DISCHARGING },
87 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
88 { "Full", BATTERY_STATUS_FULL },
89 { NULL, 0 },
90 };
91
92 ret = mapSysfsString(status, batteryStatusMap);
93 if (ret < 0) {
94 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
95 ret = BATTERY_STATUS_UNKNOWN;
96 }
97
98 return ret;
99}
100
101int BatteryMonitor::getBatteryHealth(const char* status) {
102 int ret;
103 struct sysfsStringEnumMap batteryHealthMap[] = {
104 { "Unknown", BATTERY_HEALTH_UNKNOWN },
105 { "Good", BATTERY_HEALTH_GOOD },
106 { "Overheat", BATTERY_HEALTH_OVERHEAT },
107 { "Dead", BATTERY_HEALTH_DEAD },
108 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
109 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
110 { "Cold", BATTERY_HEALTH_COLD },
111 { NULL, 0 },
112 };
113
114 ret = mapSysfsString(status, batteryHealthMap);
115 if (ret < 0) {
116 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
117 ret = BATTERY_HEALTH_UNKNOWN;
118 }
119
120 return ret;
121}
122
123int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
124 char *cp = NULL;
125
126 if (path.isEmpty())
127 return -1;
128 int fd = open(path.string(), O_RDONLY, 0);
129 if (fd == -1) {
130 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
131 return -1;
132 }
133
134 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
135 if (count > 0)
136 cp = (char *)memrchr(buf, '\n', count);
137
138 if (cp)
139 *cp = '\0';
140 else
141 buf[0] = '\0';
142
143 close(fd);
144 return count;
145}
146
147BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
148 const int SIZE = 128;
149 char buf[SIZE];
150 int length = readFromFile(path, buf, SIZE);
151 BatteryMonitor::PowerSupplyType ret;
152 struct sysfsStringEnumMap supplyTypeMap[] = {
153 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
154 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
155 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
156 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
157 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
158 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100159 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700160 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
161 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700162 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
163 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
164 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700165 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
166 { NULL, 0 },
167 };
168
169 if (length <= 0)
170 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
171
172 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100173 if (ret < 0) {
174 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700175 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100176 }
Todd Poynor752faf22013-06-12 13:25:59 -0700177
178 return ret;
179}
180
181bool BatteryMonitor::getBooleanField(const String8& path) {
182 const int SIZE = 16;
183 char buf[SIZE];
184
185 bool value = false;
186 if (readFromFile(path, buf, SIZE) > 0) {
187 if (buf[0] != '0') {
188 value = true;
189 }
190 }
191
192 return value;
193}
194
195int BatteryMonitor::getIntField(const String8& path) {
196 const int SIZE = 128;
197 char buf[SIZE];
198
199 int value = 0;
200 if (readFromFile(path, buf, SIZE) > 0) {
201 value = strtol(buf, NULL, 0);
202 }
203 return value;
204}
205
206bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700207 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700208
Yabin Cuiac3387b2016-02-16 17:19:23 -0800209 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700210
Todd Poynorf5d30122013-08-12 17:03:35 -0700211 if (!mHealthdConfig->batteryPresentPath.isEmpty())
212 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700213 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700214 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700215
Todd Poynor3db03a52014-05-21 16:28:13 -0700216 props.batteryLevel = mBatteryFixedCapacity ?
217 mBatteryFixedCapacity :
218 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700219 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700220
Ruchi Kandoicc338802015-08-24 13:01:16 -0700221 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
222 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
223
224 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
225 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
226
227 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
228 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
229
Todd Poynor3db03a52014-05-21 16:28:13 -0700230 props.batteryTemperature = mBatteryFixedTemperature ?
231 mBatteryFixedTemperature :
232 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700233
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700234 // For devices which do not have battery and are always plugged
235 // into power souce.
236 if (mAlwaysPluggedDevice) {
237 props.chargerAcOnline = true;
238 props.batteryPresent = true;
239 props.batteryStatus = BATTERY_STATUS_CHARGING;
240 props.batteryHealth = BATTERY_HEALTH_GOOD;
241 }
242
Todd Poynor752faf22013-06-12 13:25:59 -0700243 const int SIZE = 128;
244 char buf[SIZE];
245 String8 btech;
246
Todd Poynorf5d30122013-08-12 17:03:35 -0700247 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700248 props.batteryStatus = getBatteryStatus(buf);
249
Todd Poynorf5d30122013-08-12 17:03:35 -0700250 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700251 props.batteryHealth = getBatteryHealth(buf);
252
Todd Poynorf5d30122013-08-12 17:03:35 -0700253 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700254 props.batteryTechnology = String8(buf);
255
256 unsigned int i;
257
258 for (i = 0; i < mChargerNames.size(); i++) {
259 String8 path;
260 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
261 mChargerNames[i].string());
262
263 if (readFromFile(path, buf, SIZE) > 0) {
264 if (buf[0] != '0') {
265 path.clear();
266 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
267 mChargerNames[i].string());
268 switch(readPowerSupplyType(path)) {
269 case ANDROID_POWER_SUPPLY_TYPE_AC:
270 props.chargerAcOnline = true;
271 break;
272 case ANDROID_POWER_SUPPLY_TYPE_USB:
273 props.chargerUsbOnline = true;
274 break;
275 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
276 props.chargerWirelessOnline = true;
277 break;
278 default:
279 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
280 mChargerNames[i].string());
281 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700282 path.clear();
283 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
284 mChargerNames[i].string());
285 if (access(path.string(), R_OK) == 0) {
286 int maxChargingCurrent = getIntField(path);
287 if (props.maxChargingCurrent < maxChargingCurrent) {
288 props.maxChargingCurrent = maxChargingCurrent;
289 }
290 }
Todd Poynor752faf22013-06-12 13:25:59 -0700291 }
292 }
293 }
294
Todd Poynor10b235e2013-08-07 15:25:14 -0700295 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700296
Todd Poynor10b235e2013-08-07 15:25:14 -0700297 if (logthis) {
298 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700299 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700300 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700301 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700302 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
303 props.batteryLevel, props.batteryVoltage,
304 props.batteryTemperature < 0 ? "-" : "",
305 abs(props.batteryTemperature / 10),
306 abs(props.batteryTemperature % 10), props.batteryHealth,
307 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700308
Ruchi Kandoicc338802015-08-24 13:01:16 -0700309 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700310 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700311 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
312 " c=%d", props.batteryCurrent);
313 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700314
Ruchi Kandoicc338802015-08-24 13:01:16 -0700315 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
316 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
317 " fc=%d", props.batteryFullCharge);
318 }
319
320 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
321 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
322 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700323 }
324 } else {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700325 snprintf(dmesgline, sizeof(dmesgline),
326 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700327 }
328
Ruchi Kandoicc338802015-08-24 13:01:16 -0700329 len = strlen(dmesgline);
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700330 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
331 props.chargerAcOnline ? "a" : "",
332 props.chargerUsbOnline ? "u" : "",
333 props.chargerWirelessOnline ? "w" : "");
334
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700335 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700336 }
337
Todd Poynorc7464c92013-09-10 12:40:00 -0700338 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700339 return props.chargerAcOnline | props.chargerUsbOnline |
340 props.chargerWirelessOnline;
341}
342
Yabin Cuib0580d72016-02-19 18:03:23 -0800343int BatteryMonitor::getChargeStatus() {
344 int result = BATTERY_STATUS_UNKNOWN;
345 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
346 char buf[128];
347 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, sizeof(buf)) > 0) {
348 result = getBatteryStatus(buf);
349 }
350 }
351 return result;
352}
353
Todd Poynorc133b712013-08-14 17:39:13 -0700354status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
355 status_t ret = BAD_VALUE;
356
Todd Poynor8f132af2014-05-08 17:15:45 -0700357 val->valueInt64 = LONG_MIN;
358
Todd Poynorc133b712013-08-14 17:39:13 -0700359 switch(id) {
360 case BATTERY_PROP_CHARGE_COUNTER:
361 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700362 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700363 getIntField(mHealthdConfig->batteryChargeCounterPath);
364 ret = NO_ERROR;
365 } else {
366 ret = NAME_NOT_FOUND;
367 }
368 break;
369
370 case BATTERY_PROP_CURRENT_NOW:
371 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700372 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700373 getIntField(mHealthdConfig->batteryCurrentNowPath);
374 ret = NO_ERROR;
375 } else {
376 ret = NAME_NOT_FOUND;
377 }
378 break;
379
Todd Poynorbc102112013-08-27 18:11:49 -0700380 case BATTERY_PROP_CURRENT_AVG:
381 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700382 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700383 getIntField(mHealthdConfig->batteryCurrentAvgPath);
384 ret = NO_ERROR;
385 } else {
386 ret = NAME_NOT_FOUND;
387 }
388 break;
389
Paul Lawrence347c8de2014-03-19 15:04:40 -0700390 case BATTERY_PROP_CAPACITY:
391 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700392 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700393 getIntField(mHealthdConfig->batteryCapacityPath);
394 ret = NO_ERROR;
395 } else {
396 ret = NAME_NOT_FOUND;
397 }
398 break;
399
Todd Poynor8f132af2014-05-08 17:15:45 -0700400 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700401 if (mHealthdConfig->energyCounter) {
402 ret = mHealthdConfig->energyCounter(&val->valueInt64);
403 } else {
404 ret = NAME_NOT_FOUND;
405 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700406 break;
407
Todd Poynorc133b712013-08-14 17:39:13 -0700408 default:
409 break;
410 }
411
Todd Poynorc133b712013-08-14 17:39:13 -0700412 return ret;
413}
414
Todd Poynor020369d2013-09-18 20:09:33 -0700415void BatteryMonitor::dumpState(int fd) {
416 int v;
417 char vs[128];
418
Adrian Roosd5fe6672015-07-10 13:11:01 -0700419 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700420 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700421 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700422 write(fd, vs, strlen(vs));
423 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
424 props.batteryStatus, props.batteryHealth, props.batteryPresent);
425 write(fd, vs, strlen(vs));
426 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
427 props.batteryLevel, props.batteryVoltage,
428 props.batteryTemperature);
429 write(fd, vs, strlen(vs));
430
431 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
432 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
433 snprintf(vs, sizeof(vs), "current now: %d\n", v);
434 write(fd, vs, strlen(vs));
435 }
436
437 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
438 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
439 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
440 write(fd, vs, strlen(vs));
441 }
442
443 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
444 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
445 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
446 write(fd, vs, strlen(vs));
447 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700448
449 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
450 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
451 write(fd, vs, strlen(vs));
452 }
453
454 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
455 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
456 write(fd, vs, strlen(vs));
457 }
458
459 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
460 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
461 write(fd, vs, strlen(vs));
462 }
Todd Poynor020369d2013-09-18 20:09:33 -0700463}
464
Todd Poynorc7464c92013-09-10 12:40:00 -0700465void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700466 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700467 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700468
Todd Poynorf5d30122013-08-12 17:03:35 -0700469 mHealthdConfig = hc;
Todd Poynor752faf22013-06-12 13:25:59 -0700470 DIR* dir = opendir(POWER_SUPPLY_SYSFS_PATH);
471 if (dir == NULL) {
472 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
473 } else {
474 struct dirent* entry;
475
476 while ((entry = readdir(dir))) {
477 const char* name = entry->d_name;
478
479 if (!strcmp(name, ".") || !strcmp(name, ".."))
480 continue;
481
Todd Poynor752faf22013-06-12 13:25:59 -0700482 // Look for "type" file in each subdirectory
483 path.clear();
484 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
485 switch(readPowerSupplyType(path)) {
486 case ANDROID_POWER_SUPPLY_TYPE_AC:
487 case ANDROID_POWER_SUPPLY_TYPE_USB:
488 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
489 path.clear();
490 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
491 if (access(path.string(), R_OK) == 0)
492 mChargerNames.add(String8(name));
493 break;
494
495 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700496 mBatteryDevicePresent = true;
497
Todd Poynorf5d30122013-08-12 17:03:35 -0700498 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700499 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700500 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
501 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700502 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700503 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700504 }
505
Todd Poynorf5d30122013-08-12 17:03:35 -0700506 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700507 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700508 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
509 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700510 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700511 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700512 }
513
Todd Poynorf5d30122013-08-12 17:03:35 -0700514 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
515 path.clear();
516 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
517 name);
518 if (access(path, R_OK) == 0)
519 mHealthdConfig->batteryPresentPath = path;
520 }
521
522 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
523 path.clear();
524 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
525 name);
526 if (access(path, R_OK) == 0)
527 mHealthdConfig->batteryCapacityPath = path;
528 }
529
530 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
531 path.clear();
532 path.appendFormat("%s/%s/voltage_now",
533 POWER_SUPPLY_SYSFS_PATH, name);
534 if (access(path, R_OK) == 0) {
535 mHealthdConfig->batteryVoltagePath = path;
536 } else {
537 path.clear();
538 path.appendFormat("%s/%s/batt_vol",
539 POWER_SUPPLY_SYSFS_PATH, name);
540 if (access(path, R_OK) == 0)
541 mHealthdConfig->batteryVoltagePath = path;
542 }
543 }
544
Ruchi Kandoicc338802015-08-24 13:01:16 -0700545 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
546 path.clear();
547 path.appendFormat("%s/%s/charge_full",
548 POWER_SUPPLY_SYSFS_PATH, name);
549 if (access(path, R_OK) == 0)
550 mHealthdConfig->batteryFullChargePath = path;
551 }
552
Todd Poynorf5d30122013-08-12 17:03:35 -0700553 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
554 path.clear();
555 path.appendFormat("%s/%s/current_now",
556 POWER_SUPPLY_SYSFS_PATH, name);
557 if (access(path, R_OK) == 0)
558 mHealthdConfig->batteryCurrentNowPath = path;
559 }
560
Ruchi Kandoicc338802015-08-24 13:01:16 -0700561 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
562 path.clear();
563 path.appendFormat("%s/%s/cycle_count",
564 POWER_SUPPLY_SYSFS_PATH, name);
565 if (access(path, R_OK) == 0)
566 mHealthdConfig->batteryCycleCountPath = path;
567 }
568
Todd Poynorbc102112013-08-27 18:11:49 -0700569 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
570 path.clear();
571 path.appendFormat("%s/%s/current_avg",
572 POWER_SUPPLY_SYSFS_PATH, name);
573 if (access(path, R_OK) == 0)
574 mHealthdConfig->batteryCurrentAvgPath = path;
575 }
576
Todd Poynorf5d30122013-08-12 17:03:35 -0700577 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
578 path.clear();
579 path.appendFormat("%s/%s/charge_counter",
580 POWER_SUPPLY_SYSFS_PATH, name);
581 if (access(path, R_OK) == 0)
582 mHealthdConfig->batteryChargeCounterPath = path;
583 }
584
585 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
586 path.clear();
587 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
588 name);
589 if (access(path, R_OK) == 0) {
590 mHealthdConfig->batteryTemperaturePath = path;
591 } else {
592 path.clear();
593 path.appendFormat("%s/%s/batt_temp",
594 POWER_SUPPLY_SYSFS_PATH, name);
595 if (access(path, R_OK) == 0)
596 mHealthdConfig->batteryTemperaturePath = path;
597 }
598 }
599
600 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
601 path.clear();
602 path.appendFormat("%s/%s/technology",
603 POWER_SUPPLY_SYSFS_PATH, name);
604 if (access(path, R_OK) == 0)
605 mHealthdConfig->batteryTechnologyPath = path;
606 }
607
Todd Poynor752faf22013-06-12 13:25:59 -0700608 break;
609
610 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
611 break;
612 }
613 }
614 closedir(dir);
615 }
616
Ian Pedowitz585ab652015-10-12 19:01:00 -0700617 // This indicates that there is no charger driver registered.
618 // Typically the case for devices which do not have a battery and
619 // and are always plugged into AC mains.
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700620 if (!mChargerNames.size()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700621 KLOG_ERROR(LOG_TAG, "No charger supplies found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700622 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
623 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
624 mAlwaysPluggedDevice = true;
625 }
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700626 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700627 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700628 hc->periodic_chores_interval_fast = -1;
629 hc->periodic_chores_interval_slow = -1;
630 } else {
631 if (mHealthdConfig->batteryStatusPath.isEmpty())
632 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
633 if (mHealthdConfig->batteryHealthPath.isEmpty())
634 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
635 if (mHealthdConfig->batteryPresentPath.isEmpty())
636 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
637 if (mHealthdConfig->batteryCapacityPath.isEmpty())
638 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
639 if (mHealthdConfig->batteryVoltagePath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
641 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
642 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
643 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
644 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700645 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700646 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
647 if (mHealthdConfig->batteryFullChargePath.isEmpty())
648 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
649 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
650 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700651 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700652
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700653 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
654 && strtol(pval, NULL, 10) != 0) {
655 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
656 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
657 }
Todd Poynor752faf22013-06-12 13:25:59 -0700658}
659
660}; // namespace android