blob: d1c547df93e6b13dd4227a8ba08f7e776f5619b5 [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
Michael Scott3217c5c2016-06-05 11:20:13 -070031#include <android-base/file.h>
32#include <android-base/strings.h>
Todd Poynor752faf22013-06-12 13:25:59 -070033#include <batteryservice/BatteryService.h>
34#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070035#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070036#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070037#include <utils/String8.h>
38#include <utils/Vector.h>
39
40#define POWER_SUPPLY_SUBSYSTEM "power_supply"
41#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070042#define FAKE_BATTERY_CAPACITY 42
43#define FAKE_BATTERY_TEMPERATURE 424
Ruchi Kandoi42a981d2015-09-28 13:35:59 -070044#define ALWAYS_PLUGGED_CAPACITY 100
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;
67 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
68 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
69 props->batteryPresent = false;
70 props->batteryLevel = 0;
71 props->batteryVoltage = 0;
72 props->batteryTemperature = 0;
73 props->batteryCurrent = 0;
74 props->batteryCycleCount = 0;
75 props->batteryFullCharge = 0;
76 props->batteryTechnology.clear();
77}
78
79BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
80 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
81 initBatteryProperties(&props);
82}
83
Todd Poynor752faf22013-06-12 13:25:59 -070084int BatteryMonitor::getBatteryStatus(const char* status) {
85 int ret;
86 struct sysfsStringEnumMap batteryStatusMap[] = {
87 { "Unknown", BATTERY_STATUS_UNKNOWN },
88 { "Charging", BATTERY_STATUS_CHARGING },
89 { "Discharging", BATTERY_STATUS_DISCHARGING },
90 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
91 { "Full", BATTERY_STATUS_FULL },
92 { NULL, 0 },
93 };
94
95 ret = mapSysfsString(status, batteryStatusMap);
96 if (ret < 0) {
97 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
98 ret = BATTERY_STATUS_UNKNOWN;
99 }
100
101 return ret;
102}
103
104int BatteryMonitor::getBatteryHealth(const char* status) {
105 int ret;
106 struct sysfsStringEnumMap batteryHealthMap[] = {
107 { "Unknown", BATTERY_HEALTH_UNKNOWN },
108 { "Good", BATTERY_HEALTH_GOOD },
109 { "Overheat", BATTERY_HEALTH_OVERHEAT },
110 { "Dead", BATTERY_HEALTH_DEAD },
111 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
112 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
113 { "Cold", BATTERY_HEALTH_COLD },
114 { NULL, 0 },
115 };
116
117 ret = mapSysfsString(status, batteryHealthMap);
118 if (ret < 0) {
119 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
120 ret = BATTERY_HEALTH_UNKNOWN;
121 }
122
123 return ret;
124}
125
Michael Scott3217c5c2016-06-05 11:20:13 -0700126int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
127 if (android::base::ReadFileToString(String8::std_string(path), buf)) {
128 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700129 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700130 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700131}
132
133BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700134 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700135 BatteryMonitor::PowerSupplyType ret;
136 struct sysfsStringEnumMap supplyTypeMap[] = {
137 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
138 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
139 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
140 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
141 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
142 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100143 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700144 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
145 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700146 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
147 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
148 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700149 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
150 { NULL, 0 },
151 };
152
Michael Scott3217c5c2016-06-05 11:20:13 -0700153 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700154 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
155
Michael Scott3217c5c2016-06-05 11:20:13 -0700156 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100157 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700158 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700159 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100160 }
Todd Poynor752faf22013-06-12 13:25:59 -0700161
162 return ret;
163}
164
165bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700166 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700167 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700168
169 if (readFromFile(path, &buf) > 0)
170 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700171 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700172
173 return value;
174}
175
176int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700177 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700178 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700179
180 if (readFromFile(path, &buf) > 0)
181 value = std::stoi(buf.c_str(), NULL, 0);
182
Todd Poynor752faf22013-06-12 13:25:59 -0700183 return value;
184}
185
186bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700187 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700188
Yabin Cuidb04a492016-02-16 17:19:23 -0800189 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700190
Todd Poynorf5d30122013-08-12 17:03:35 -0700191 if (!mHealthdConfig->batteryPresentPath.isEmpty())
192 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700193 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700194 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700195
Todd Poynor3db03a52014-05-21 16:28:13 -0700196 props.batteryLevel = mBatteryFixedCapacity ?
197 mBatteryFixedCapacity :
198 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700199 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700200
Ruchi Kandoicc338802015-08-24 13:01:16 -0700201 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
202 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
203
204 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
205 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
206
207 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
208 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
209
Todd Poynor3db03a52014-05-21 16:28:13 -0700210 props.batteryTemperature = mBatteryFixedTemperature ?
211 mBatteryFixedTemperature :
212 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700213
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700214 // For devices which do not have battery and are always plugged
215 // into power souce.
216 if (mAlwaysPluggedDevice) {
217 props.chargerAcOnline = true;
218 props.batteryPresent = true;
219 props.batteryStatus = BATTERY_STATUS_CHARGING;
220 props.batteryHealth = BATTERY_HEALTH_GOOD;
221 }
222
Michael Scott3217c5c2016-06-05 11:20:13 -0700223 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700224
Michael Scott3217c5c2016-06-05 11:20:13 -0700225 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
226 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700227
Michael Scott3217c5c2016-06-05 11:20:13 -0700228 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
229 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700230
Michael Scott3217c5c2016-06-05 11:20:13 -0700231 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
232 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700233
234 unsigned int i;
235
236 for (i = 0; i < mChargerNames.size(); i++) {
237 String8 path;
238 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
239 mChargerNames[i].string());
240
Michael Scott3217c5c2016-06-05 11:20:13 -0700241 if (getIntField(path)) {
242 path.clear();
243 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
244 mChargerNames[i].string());
245 switch(readPowerSupplyType(path)) {
246 case ANDROID_POWER_SUPPLY_TYPE_AC:
247 props.chargerAcOnline = true;
248 break;
249 case ANDROID_POWER_SUPPLY_TYPE_USB:
250 props.chargerUsbOnline = true;
251 break;
252 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
253 props.chargerWirelessOnline = true;
254 break;
255 default:
256 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
257 mChargerNames[i].string());
258 }
259 path.clear();
260 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
261 mChargerNames[i].string());
262 if (access(path.string(), R_OK) == 0) {
263 int maxChargingCurrent = getIntField(path);
264 if (props.maxChargingCurrent < maxChargingCurrent) {
265 props.maxChargingCurrent = maxChargingCurrent;
Adrian Roosd5fe6672015-07-10 13:11:01 -0700266 }
Todd Poynor752faf22013-06-12 13:25:59 -0700267 }
268 }
269 }
270
Todd Poynor10b235e2013-08-07 15:25:14 -0700271 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700272
Todd Poynor10b235e2013-08-07 15:25:14 -0700273 if (logthis) {
274 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700275 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700276 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700277 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700278 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
279 props.batteryLevel, props.batteryVoltage,
280 props.batteryTemperature < 0 ? "-" : "",
281 abs(props.batteryTemperature / 10),
282 abs(props.batteryTemperature % 10), props.batteryHealth,
283 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700284
Ruchi Kandoicc338802015-08-24 13:01:16 -0700285 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700286 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700287 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
288 " c=%d", props.batteryCurrent);
289 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700290
Ruchi Kandoicc338802015-08-24 13:01:16 -0700291 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
292 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
293 " fc=%d", props.batteryFullCharge);
294 }
295
296 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
297 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
298 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700299 }
300 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800301 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700302 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700303 }
304
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700305 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
306 props.chargerAcOnline ? "a" : "",
307 props.chargerUsbOnline ? "u" : "",
308 props.chargerWirelessOnline ? "w" : "");
309
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700310 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700311 }
312
Todd Poynorc7464c92013-09-10 12:40:00 -0700313 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700314 return props.chargerAcOnline | props.chargerUsbOnline |
315 props.chargerWirelessOnline;
316}
317
Yabin Cuiaedf6032016-02-19 18:03:23 -0800318int BatteryMonitor::getChargeStatus() {
319 int result = BATTERY_STATUS_UNKNOWN;
320 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700321 std::string buf;
322 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
323 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800324 }
325 return result;
326}
327
Todd Poynorc133b712013-08-14 17:39:13 -0700328status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
329 status_t ret = BAD_VALUE;
330
Todd Poynor8f132af2014-05-08 17:15:45 -0700331 val->valueInt64 = LONG_MIN;
332
Todd Poynorc133b712013-08-14 17:39:13 -0700333 switch(id) {
334 case BATTERY_PROP_CHARGE_COUNTER:
335 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700336 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700337 getIntField(mHealthdConfig->batteryChargeCounterPath);
338 ret = NO_ERROR;
339 } else {
340 ret = NAME_NOT_FOUND;
341 }
342 break;
343
344 case BATTERY_PROP_CURRENT_NOW:
345 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700346 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700347 getIntField(mHealthdConfig->batteryCurrentNowPath);
348 ret = NO_ERROR;
349 } else {
350 ret = NAME_NOT_FOUND;
351 }
352 break;
353
Todd Poynorbc102112013-08-27 18:11:49 -0700354 case BATTERY_PROP_CURRENT_AVG:
355 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700356 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700357 getIntField(mHealthdConfig->batteryCurrentAvgPath);
358 ret = NO_ERROR;
359 } else {
360 ret = NAME_NOT_FOUND;
361 }
362 break;
363
Paul Lawrence347c8de2014-03-19 15:04:40 -0700364 case BATTERY_PROP_CAPACITY:
365 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700366 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700367 getIntField(mHealthdConfig->batteryCapacityPath);
368 ret = NO_ERROR;
369 } else {
370 ret = NAME_NOT_FOUND;
371 }
372 break;
373
Todd Poynor8f132af2014-05-08 17:15:45 -0700374 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700375 if (mHealthdConfig->energyCounter) {
376 ret = mHealthdConfig->energyCounter(&val->valueInt64);
377 } else {
378 ret = NAME_NOT_FOUND;
379 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700380 break;
381
Todd Poynorc133b712013-08-14 17:39:13 -0700382 default:
383 break;
384 }
385
Todd Poynorc133b712013-08-14 17:39:13 -0700386 return ret;
387}
388
Todd Poynor020369d2013-09-18 20:09:33 -0700389void BatteryMonitor::dumpState(int fd) {
390 int v;
391 char vs[128];
392
Adrian Roosd5fe6672015-07-10 13:11:01 -0700393 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700394 props.chargerAcOnline, props.chargerUsbOnline,
Adrian Roosd5fe6672015-07-10 13:11:01 -0700395 props.chargerWirelessOnline, props.maxChargingCurrent);
Todd Poynor020369d2013-09-18 20:09:33 -0700396 write(fd, vs, strlen(vs));
397 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
398 props.batteryStatus, props.batteryHealth, props.batteryPresent);
399 write(fd, vs, strlen(vs));
400 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
401 props.batteryLevel, props.batteryVoltage,
402 props.batteryTemperature);
403 write(fd, vs, strlen(vs));
404
405 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
406 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
407 snprintf(vs, sizeof(vs), "current now: %d\n", v);
408 write(fd, vs, strlen(vs));
409 }
410
411 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
412 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
413 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
414 write(fd, vs, strlen(vs));
415 }
416
417 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
418 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
419 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
420 write(fd, vs, strlen(vs));
421 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700422
423 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
424 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
425 write(fd, vs, strlen(vs));
426 }
427
428 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
429 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
430 write(fd, vs, strlen(vs));
431 }
432
433 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
434 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
435 write(fd, vs, strlen(vs));
436 }
Todd Poynor020369d2013-09-18 20:09:33 -0700437}
438
Todd Poynorc7464c92013-09-10 12:40:00 -0700439void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700440 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700441 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700442
Todd Poynorf5d30122013-08-12 17:03:35 -0700443 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800444 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700445 if (dir == NULL) {
446 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
447 } else {
448 struct dirent* entry;
449
James Hawkins588a2ca2016-02-18 14:52:46 -0800450 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700451 const char* name = entry->d_name;
452
453 if (!strcmp(name, ".") || !strcmp(name, ".."))
454 continue;
455
Todd Poynor752faf22013-06-12 13:25:59 -0700456 // Look for "type" file in each subdirectory
457 path.clear();
458 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
459 switch(readPowerSupplyType(path)) {
460 case ANDROID_POWER_SUPPLY_TYPE_AC:
461 case ANDROID_POWER_SUPPLY_TYPE_USB:
462 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
463 path.clear();
464 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
465 if (access(path.string(), R_OK) == 0)
466 mChargerNames.add(String8(name));
467 break;
468
469 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700470 mBatteryDevicePresent = true;
471
Todd Poynorf5d30122013-08-12 17:03:35 -0700472 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700473 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700474 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
475 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700476 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700477 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700478 }
479
Todd Poynorf5d30122013-08-12 17:03:35 -0700480 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700481 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700482 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
483 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700484 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700485 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700486 }
487
Todd Poynorf5d30122013-08-12 17:03:35 -0700488 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
489 path.clear();
490 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
491 name);
492 if (access(path, R_OK) == 0)
493 mHealthdConfig->batteryPresentPath = path;
494 }
495
496 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
497 path.clear();
498 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
499 name);
500 if (access(path, R_OK) == 0)
501 mHealthdConfig->batteryCapacityPath = path;
502 }
503
504 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
505 path.clear();
506 path.appendFormat("%s/%s/voltage_now",
507 POWER_SUPPLY_SYSFS_PATH, name);
508 if (access(path, R_OK) == 0) {
509 mHealthdConfig->batteryVoltagePath = path;
510 } else {
511 path.clear();
512 path.appendFormat("%s/%s/batt_vol",
513 POWER_SUPPLY_SYSFS_PATH, name);
514 if (access(path, R_OK) == 0)
515 mHealthdConfig->batteryVoltagePath = path;
516 }
517 }
518
Ruchi Kandoicc338802015-08-24 13:01:16 -0700519 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
520 path.clear();
521 path.appendFormat("%s/%s/charge_full",
522 POWER_SUPPLY_SYSFS_PATH, name);
523 if (access(path, R_OK) == 0)
524 mHealthdConfig->batteryFullChargePath = path;
525 }
526
Todd Poynorf5d30122013-08-12 17:03:35 -0700527 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
528 path.clear();
529 path.appendFormat("%s/%s/current_now",
530 POWER_SUPPLY_SYSFS_PATH, name);
531 if (access(path, R_OK) == 0)
532 mHealthdConfig->batteryCurrentNowPath = path;
533 }
534
Ruchi Kandoicc338802015-08-24 13:01:16 -0700535 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
536 path.clear();
537 path.appendFormat("%s/%s/cycle_count",
538 POWER_SUPPLY_SYSFS_PATH, name);
539 if (access(path, R_OK) == 0)
540 mHealthdConfig->batteryCycleCountPath = path;
541 }
542
Todd Poynorbc102112013-08-27 18:11:49 -0700543 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
544 path.clear();
545 path.appendFormat("%s/%s/current_avg",
546 POWER_SUPPLY_SYSFS_PATH, name);
547 if (access(path, R_OK) == 0)
548 mHealthdConfig->batteryCurrentAvgPath = path;
549 }
550
Todd Poynorf5d30122013-08-12 17:03:35 -0700551 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
552 path.clear();
553 path.appendFormat("%s/%s/charge_counter",
554 POWER_SUPPLY_SYSFS_PATH, name);
555 if (access(path, R_OK) == 0)
556 mHealthdConfig->batteryChargeCounterPath = path;
557 }
558
559 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
560 path.clear();
561 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
562 name);
563 if (access(path, R_OK) == 0) {
564 mHealthdConfig->batteryTemperaturePath = path;
565 } else {
566 path.clear();
567 path.appendFormat("%s/%s/batt_temp",
568 POWER_SUPPLY_SYSFS_PATH, name);
569 if (access(path, R_OK) == 0)
570 mHealthdConfig->batteryTemperaturePath = path;
571 }
572 }
573
574 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
575 path.clear();
576 path.appendFormat("%s/%s/technology",
577 POWER_SUPPLY_SYSFS_PATH, name);
578 if (access(path, R_OK) == 0)
579 mHealthdConfig->batteryTechnologyPath = path;
580 }
581
Todd Poynor752faf22013-06-12 13:25:59 -0700582 break;
583
584 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
585 break;
586 }
587 }
Todd Poynor752faf22013-06-12 13:25:59 -0700588 }
589
Ruchi Kandoi42a981d2015-09-28 13:35:59 -0700590 // Typically the case for devices which do not have a battery and
591 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700592 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700593 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700594 hc->periodic_chores_interval_fast = -1;
595 hc->periodic_chores_interval_slow = -1;
Ruchi Kandoifabd4902016-02-29 13:13:39 -0800596 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
597 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
598 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700599 } else {
600 if (mHealthdConfig->batteryStatusPath.isEmpty())
601 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
602 if (mHealthdConfig->batteryHealthPath.isEmpty())
603 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
604 if (mHealthdConfig->batteryPresentPath.isEmpty())
605 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
606 if (mHealthdConfig->batteryCapacityPath.isEmpty())
607 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
608 if (mHealthdConfig->batteryVoltagePath.isEmpty())
609 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
610 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
611 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
612 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
613 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoicc338802015-08-24 13:01:16 -0700614 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
615 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
616 if (mHealthdConfig->batteryFullChargePath.isEmpty())
617 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
618 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
619 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700620 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700621
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700622 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
623 && strtol(pval, NULL, 10) != 0) {
624 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
625 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
626 }
Todd Poynor752faf22013-06-12 13:25:59 -0700627}
628
629}; // namespace android