blob: d287d1032a0de7db9f13ce6970b374ffdbb34cc8 [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 Cuie98e1772016-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>
James Hawkins588a2ca2016-02-18 14:52:46 -080029#include <memory>
Mark Salyzynacb1ddf2015-07-23 09:22:50 -070030
Todd Poynor752faf22013-06-12 13:25:59 -070031#include <batteryservice/BatteryService.h>
32#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070033#include <cutils/properties.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
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080043#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070044#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
Yabin Cuidb04a492016-02-16 17:19:23 -080062static void initBatteryProperties(BatteryProperties* props) {
63 props->chargerAcOnline = false;
64 props->chargerUsbOnline = false;
65 props->chargerWirelessOnline = false;
66 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070067 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080068 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
69 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
70 props->batteryPresent = false;
71 props->batteryLevel = 0;
72 props->batteryVoltage = 0;
73 props->batteryTemperature = 0;
74 props->batteryCurrent = 0;
75 props->batteryCycleCount = 0;
76 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070077 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080078 props->batteryTechnology.clear();
79}
80
81BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
82 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
83 initBatteryProperties(&props);
84}
85
Todd Poynor752faf22013-06-12 13:25:59 -070086int BatteryMonitor::getBatteryStatus(const char* status) {
87 int ret;
88 struct sysfsStringEnumMap batteryStatusMap[] = {
89 { "Unknown", BATTERY_STATUS_UNKNOWN },
90 { "Charging", BATTERY_STATUS_CHARGING },
91 { "Discharging", BATTERY_STATUS_DISCHARGING },
92 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
93 { "Full", BATTERY_STATUS_FULL },
94 { NULL, 0 },
95 };
96
97 ret = mapSysfsString(status, batteryStatusMap);
98 if (ret < 0) {
99 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
100 ret = BATTERY_STATUS_UNKNOWN;
101 }
102
103 return ret;
104}
105
106int BatteryMonitor::getBatteryHealth(const char* status) {
107 int ret;
108 struct sysfsStringEnumMap batteryHealthMap[] = {
109 { "Unknown", BATTERY_HEALTH_UNKNOWN },
110 { "Good", BATTERY_HEALTH_GOOD },
111 { "Overheat", BATTERY_HEALTH_OVERHEAT },
112 { "Dead", BATTERY_HEALTH_DEAD },
113 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
114 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
115 { "Cold", BATTERY_HEALTH_COLD },
116 { NULL, 0 },
117 };
118
119 ret = mapSysfsString(status, batteryHealthMap);
120 if (ret < 0) {
121 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
122 ret = BATTERY_HEALTH_UNKNOWN;
123 }
124
125 return ret;
126}
127
128int BatteryMonitor::readFromFile(const String8& path, char* buf, size_t size) {
129 char *cp = NULL;
130
131 if (path.isEmpty())
132 return -1;
133 int fd = open(path.string(), O_RDONLY, 0);
134 if (fd == -1) {
135 KLOG_ERROR(LOG_TAG, "Could not open '%s'\n", path.string());
136 return -1;
137 }
138
139 ssize_t count = TEMP_FAILURE_RETRY(read(fd, buf, size));
140 if (count > 0)
141 cp = (char *)memrchr(buf, '\n', count);
142
143 if (cp)
144 *cp = '\0';
145 else
146 buf[0] = '\0';
147
148 close(fd);
149 return count;
150}
151
152BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
153 const int SIZE = 128;
154 char buf[SIZE];
155 int length = readFromFile(path, buf, SIZE);
156 BatteryMonitor::PowerSupplyType ret;
157 struct sysfsStringEnumMap supplyTypeMap[] = {
158 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
159 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
160 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
161 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
162 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
163 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100164 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700165 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
166 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700167 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
168 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
169 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700170 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
171 { NULL, 0 },
172 };
173
174 if (length <= 0)
175 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
176
177 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf, supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100178 if (ret < 0) {
179 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700180 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100181 }
Todd Poynor752faf22013-06-12 13:25:59 -0700182
183 return ret;
184}
185
186bool BatteryMonitor::getBooleanField(const String8& path) {
187 const int SIZE = 16;
188 char buf[SIZE];
189
190 bool value = false;
191 if (readFromFile(path, buf, SIZE) > 0) {
192 if (buf[0] != '0') {
193 value = true;
194 }
195 }
196
197 return value;
198}
199
200int BatteryMonitor::getIntField(const String8& path) {
201 const int SIZE = 128;
202 char buf[SIZE];
203
204 int value = 0;
205 if (readFromFile(path, buf, SIZE) > 0) {
206 value = strtol(buf, NULL, 0);
207 }
208 return value;
209}
210
211bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700212 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700213
Yabin Cuidb04a492016-02-16 17:19:23 -0800214 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700215
Todd Poynorf5d30122013-08-12 17:03:35 -0700216 if (!mHealthdConfig->batteryPresentPath.isEmpty())
217 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700218 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700219 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700220
Todd Poynor3db03a52014-05-21 16:28:13 -0700221 props.batteryLevel = mBatteryFixedCapacity ?
222 mBatteryFixedCapacity :
223 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700224 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700225
Ruchi Kandoicc338802015-08-24 13:01:16 -0700226 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
227 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
228
229 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
230 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
231
232 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
233 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
234
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700235 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
236 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
237
Todd Poynor3db03a52014-05-21 16:28:13 -0700238 props.batteryTemperature = mBatteryFixedTemperature ?
239 mBatteryFixedTemperature :
240 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700241
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700242 // For devices which do not have battery and are always plugged
243 // into power souce.
244 if (mAlwaysPluggedDevice) {
245 props.chargerAcOnline = true;
246 props.batteryPresent = true;
247 props.batteryStatus = BATTERY_STATUS_CHARGING;
248 props.batteryHealth = BATTERY_HEALTH_GOOD;
249 }
250
Todd Poynor752faf22013-06-12 13:25:59 -0700251 const int SIZE = 128;
252 char buf[SIZE];
253 String8 btech;
254
Todd Poynorf5d30122013-08-12 17:03:35 -0700255 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700256 props.batteryStatus = getBatteryStatus(buf);
257
Todd Poynorf5d30122013-08-12 17:03:35 -0700258 if (readFromFile(mHealthdConfig->batteryHealthPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700259 props.batteryHealth = getBatteryHealth(buf);
260
Todd Poynorf5d30122013-08-12 17:03:35 -0700261 if (readFromFile(mHealthdConfig->batteryTechnologyPath, buf, SIZE) > 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700262 props.batteryTechnology = String8(buf);
263
264 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700265 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700266
267 for (i = 0; i < mChargerNames.size(); i++) {
268 String8 path;
269 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
270 mChargerNames[i].string());
271
272 if (readFromFile(path, buf, SIZE) > 0) {
273 if (buf[0] != '0') {
274 path.clear();
275 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
276 mChargerNames[i].string());
277 switch(readPowerSupplyType(path)) {
278 case ANDROID_POWER_SUPPLY_TYPE_AC:
279 props.chargerAcOnline = true;
280 break;
281 case ANDROID_POWER_SUPPLY_TYPE_USB:
282 props.chargerUsbOnline = true;
283 break;
284 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
285 props.chargerWirelessOnline = true;
286 break;
287 default:
288 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
289 mChargerNames[i].string());
290 }
Adrian Roosd5fe6672015-07-10 13:11:01 -0700291 path.clear();
292 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
293 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700294 int ChargingCurrent =
295 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
296
297 path.clear();
298 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
299 mChargerNames[i].string());
300
301 int ChargingVoltage =
302 (access(path.string(), R_OK) == 0) ? getIntField(path) :
303 DEFAULT_VBUS_VOLTAGE;
304
305 double power = ((double)ChargingCurrent / MILLION) *
306 ((double)ChargingVoltage / MILLION);
307 if (MaxPower < power) {
308 props.maxChargingCurrent = ChargingCurrent;
309 props.maxChargingVoltage = ChargingVoltage;
310 MaxPower = power;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700311 }
Todd Poynor752faf22013-06-12 13:25:59 -0700312 }
313 }
314 }
315
Todd Poynor10b235e2013-08-07 15:25:14 -0700316 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700317
Todd Poynor10b235e2013-08-07 15:25:14 -0700318 if (logthis) {
319 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700320 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700321 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700322 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700323 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
324 props.batteryLevel, props.batteryVoltage,
325 props.batteryTemperature < 0 ? "-" : "",
326 abs(props.batteryTemperature / 10),
327 abs(props.batteryTemperature % 10), props.batteryHealth,
328 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700329
Ruchi Kandoicc338802015-08-24 13:01:16 -0700330 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700331 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700332 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
333 " c=%d", props.batteryCurrent);
334 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700335
Ruchi Kandoicc338802015-08-24 13:01:16 -0700336 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
337 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
338 " fc=%d", props.batteryFullCharge);
339 }
340
341 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
342 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
343 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700344 }
345 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800346 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700347 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700348 }
349
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700350 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
351 props.chargerAcOnline ? "a" : "",
352 props.chargerUsbOnline ? "u" : "",
353 props.chargerWirelessOnline ? "w" : "");
354
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700355 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700356 }
357
Todd Poynorc7464c92013-09-10 12:40:00 -0700358 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700359 return props.chargerAcOnline | props.chargerUsbOnline |
360 props.chargerWirelessOnline;
361}
362
Yabin Cuiaedf6032016-02-19 18:03:23 -0800363int BatteryMonitor::getChargeStatus() {
364 int result = BATTERY_STATUS_UNKNOWN;
365 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
366 char buf[128];
367 if (readFromFile(mHealthdConfig->batteryStatusPath, buf, sizeof(buf)) > 0) {
368 result = getBatteryStatus(buf);
369 }
370 }
371 return result;
372}
373
Todd Poynorc133b712013-08-14 17:39:13 -0700374status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
375 status_t ret = BAD_VALUE;
376
Todd Poynor8f132af2014-05-08 17:15:45 -0700377 val->valueInt64 = LONG_MIN;
378
Todd Poynorc133b712013-08-14 17:39:13 -0700379 switch(id) {
380 case BATTERY_PROP_CHARGE_COUNTER:
381 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700382 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700383 getIntField(mHealthdConfig->batteryChargeCounterPath);
384 ret = NO_ERROR;
385 } else {
386 ret = NAME_NOT_FOUND;
387 }
388 break;
389
390 case BATTERY_PROP_CURRENT_NOW:
391 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700392 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700393 getIntField(mHealthdConfig->batteryCurrentNowPath);
394 ret = NO_ERROR;
395 } else {
396 ret = NAME_NOT_FOUND;
397 }
398 break;
399
Todd Poynorbc102112013-08-27 18:11:49 -0700400 case BATTERY_PROP_CURRENT_AVG:
401 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700402 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700403 getIntField(mHealthdConfig->batteryCurrentAvgPath);
404 ret = NO_ERROR;
405 } else {
406 ret = NAME_NOT_FOUND;
407 }
408 break;
409
Paul Lawrence347c8de2014-03-19 15:04:40 -0700410 case BATTERY_PROP_CAPACITY:
411 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700412 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700413 getIntField(mHealthdConfig->batteryCapacityPath);
414 ret = NO_ERROR;
415 } else {
416 ret = NAME_NOT_FOUND;
417 }
418 break;
419
Todd Poynor8f132af2014-05-08 17:15:45 -0700420 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700421 if (mHealthdConfig->energyCounter) {
422 ret = mHealthdConfig->energyCounter(&val->valueInt64);
423 } else {
424 ret = NAME_NOT_FOUND;
425 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700426 break;
427
Todd Poynorc133b712013-08-14 17:39:13 -0700428 default:
429 break;
430 }
431
Todd Poynorc133b712013-08-14 17:39:13 -0700432 return ret;
433}
434
Todd Poynor020369d2013-09-18 20:09:33 -0700435void BatteryMonitor::dumpState(int fd) {
436 int v;
437 char vs[128];
438
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700439 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700440 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700441 props.chargerWirelessOnline, props.maxChargingCurrent,
442 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700443 write(fd, vs, strlen(vs));
444 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
445 props.batteryStatus, props.batteryHealth, props.batteryPresent);
446 write(fd, vs, strlen(vs));
447 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
448 props.batteryLevel, props.batteryVoltage,
449 props.batteryTemperature);
450 write(fd, vs, strlen(vs));
451
452 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
453 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
454 snprintf(vs, sizeof(vs), "current now: %d\n", v);
455 write(fd, vs, strlen(vs));
456 }
457
458 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
459 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
460 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
461 write(fd, vs, strlen(vs));
462 }
463
464 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
465 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
466 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
467 write(fd, vs, strlen(vs));
468 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700469
470 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
471 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
472 write(fd, vs, strlen(vs));
473 }
474
475 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
476 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
477 write(fd, vs, strlen(vs));
478 }
479
480 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
481 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
482 write(fd, vs, strlen(vs));
483 }
Todd Poynor020369d2013-09-18 20:09:33 -0700484}
485
Todd Poynorc7464c92013-09-10 12:40:00 -0700486void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700487 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700488 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700489
Todd Poynorf5d30122013-08-12 17:03:35 -0700490 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800491 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700492 if (dir == NULL) {
493 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
494 } else {
495 struct dirent* entry;
496
James Hawkins588a2ca2016-02-18 14:52:46 -0800497 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700498 const char* name = entry->d_name;
499
500 if (!strcmp(name, ".") || !strcmp(name, ".."))
501 continue;
502
Todd Poynor752faf22013-06-12 13:25:59 -0700503 // Look for "type" file in each subdirectory
504 path.clear();
505 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
506 switch(readPowerSupplyType(path)) {
507 case ANDROID_POWER_SUPPLY_TYPE_AC:
508 case ANDROID_POWER_SUPPLY_TYPE_USB:
509 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
510 path.clear();
511 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
512 if (access(path.string(), R_OK) == 0)
513 mChargerNames.add(String8(name));
514 break;
515
516 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700517 mBatteryDevicePresent = true;
518
Todd Poynorf5d30122013-08-12 17:03:35 -0700519 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700520 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700521 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
522 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700523 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700524 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700525 }
526
Todd Poynorf5d30122013-08-12 17:03:35 -0700527 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700528 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700529 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
530 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700531 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700532 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700533 }
534
Todd Poynorf5d30122013-08-12 17:03:35 -0700535 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
536 path.clear();
537 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
538 name);
539 if (access(path, R_OK) == 0)
540 mHealthdConfig->batteryPresentPath = path;
541 }
542
543 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
544 path.clear();
545 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
546 name);
547 if (access(path, R_OK) == 0)
548 mHealthdConfig->batteryCapacityPath = path;
549 }
550
551 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
552 path.clear();
553 path.appendFormat("%s/%s/voltage_now",
554 POWER_SUPPLY_SYSFS_PATH, name);
555 if (access(path, R_OK) == 0) {
556 mHealthdConfig->batteryVoltagePath = path;
557 } else {
558 path.clear();
559 path.appendFormat("%s/%s/batt_vol",
560 POWER_SUPPLY_SYSFS_PATH, name);
561 if (access(path, R_OK) == 0)
562 mHealthdConfig->batteryVoltagePath = path;
563 }
564 }
565
Ruchi Kandoicc338802015-08-24 13:01:16 -0700566 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
567 path.clear();
568 path.appendFormat("%s/%s/charge_full",
569 POWER_SUPPLY_SYSFS_PATH, name);
570 if (access(path, R_OK) == 0)
571 mHealthdConfig->batteryFullChargePath = path;
572 }
573
Todd Poynorf5d30122013-08-12 17:03:35 -0700574 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
575 path.clear();
576 path.appendFormat("%s/%s/current_now",
577 POWER_SUPPLY_SYSFS_PATH, name);
578 if (access(path, R_OK) == 0)
579 mHealthdConfig->batteryCurrentNowPath = path;
580 }
581
Ruchi Kandoicc338802015-08-24 13:01:16 -0700582 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
583 path.clear();
584 path.appendFormat("%s/%s/cycle_count",
585 POWER_SUPPLY_SYSFS_PATH, name);
586 if (access(path, R_OK) == 0)
587 mHealthdConfig->batteryCycleCountPath = path;
588 }
589
Todd Poynorbc102112013-08-27 18:11:49 -0700590 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
591 path.clear();
592 path.appendFormat("%s/%s/current_avg",
593 POWER_SUPPLY_SYSFS_PATH, name);
594 if (access(path, R_OK) == 0)
595 mHealthdConfig->batteryCurrentAvgPath = path;
596 }
597
Todd Poynorf5d30122013-08-12 17:03:35 -0700598 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
599 path.clear();
600 path.appendFormat("%s/%s/charge_counter",
601 POWER_SUPPLY_SYSFS_PATH, name);
602 if (access(path, R_OK) == 0)
603 mHealthdConfig->batteryChargeCounterPath = path;
604 }
605
606 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
607 path.clear();
608 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
609 name);
610 if (access(path, R_OK) == 0) {
611 mHealthdConfig->batteryTemperaturePath = path;
612 } else {
613 path.clear();
614 path.appendFormat("%s/%s/batt_temp",
615 POWER_SUPPLY_SYSFS_PATH, name);
616 if (access(path, R_OK) == 0)
617 mHealthdConfig->batteryTemperaturePath = path;
618 }
619 }
620
621 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
622 path.clear();
623 path.appendFormat("%s/%s/technology",
624 POWER_SUPPLY_SYSFS_PATH, name);
625 if (access(path, R_OK) == 0)
626 mHealthdConfig->batteryTechnologyPath = path;
627 }
628
Todd Poynor752faf22013-06-12 13:25:59 -0700629 break;
630
631 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
632 break;
633 }
634 }
Todd Poynor752faf22013-06-12 13:25:59 -0700635 }
636
Ian Pedowitz585ab652015-10-12 19:01:00 -0700637 // Typically the case for devices which do not have a battery and
638 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700639 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700640 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700641 hc->periodic_chores_interval_fast = -1;
642 hc->periodic_chores_interval_slow = -1;
Ruchi Kandoifabd4902016-02-29 13:13:39 -0800643 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
644 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
645 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700646 } else {
647 if (mHealthdConfig->batteryStatusPath.isEmpty())
648 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
649 if (mHealthdConfig->batteryHealthPath.isEmpty())
650 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
651 if (mHealthdConfig->batteryPresentPath.isEmpty())
652 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
653 if (mHealthdConfig->batteryCapacityPath.isEmpty())
654 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
655 if (mHealthdConfig->batteryVoltagePath.isEmpty())
656 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
657 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
658 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
659 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
660 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700661 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700662 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
663 if (mHealthdConfig->batteryFullChargePath.isEmpty())
664 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
665 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
666 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700667 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700668
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700669 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
670 && strtol(pval, NULL, 10) != 0) {
671 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
672 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
673 }
Todd Poynor752faf22013-06-12 13:25:59 -0700674}
675
676}; // namespace android