blob: 561948cefa669798a3c9ef2211761218ec4d7a4b [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>
Elliott Hughesda46b392016-10-11 17:09:00 -070032#include <android-base/parseint.h>
Michael Scott3217c5c2016-06-05 11:20:13 -070033#include <android-base/strings.h>
Todd Poynor752faf22013-06-12 13:25:59 -070034#include <batteryservice/BatteryService.h>
35#include <cutils/klog.h>
Todd Poynor3db03a52014-05-21 16:28:13 -070036#include <cutils/properties.h>
Todd Poynorc133b712013-08-14 17:39:13 -070037#include <utils/Errors.h>
Todd Poynor752faf22013-06-12 13:25:59 -070038#include <utils/String8.h>
39#include <utils/Vector.h>
40
41#define POWER_SUPPLY_SUBSYSTEM "power_supply"
42#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
Ruchi Kandoia78fc232014-07-10 15:06:21 -070043#define FAKE_BATTERY_CAPACITY 42
44#define FAKE_BATTERY_TEMPERATURE 424
Todd Poynor1bc572d2017-12-15 06:51:04 +000045#define ALWAYS_PLUGGED_CAPACITY 100
Ruchi Kandoi5c09ec12016-02-25 16:19:30 -080046#define MILLION 1.0e6
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070047#define DEFAULT_VBUS_VOLTAGE 5000000
Todd Poynor752faf22013-06-12 13:25:59 -070048
49namespace android {
50
51struct sysfsStringEnumMap {
Mark Salyzyn6f5b47f2014-05-15 15:00:59 -070052 const char* s;
Todd Poynor752faf22013-06-12 13:25:59 -070053 int val;
54};
55
56static int mapSysfsString(const char* str,
57 struct sysfsStringEnumMap map[]) {
58 for (int i = 0; map[i].s; i++)
59 if (!strcmp(str, map[i].s))
60 return map[i].val;
61
62 return -1;
63}
64
Yabin Cuidb04a492016-02-16 17:19:23 -080065static void initBatteryProperties(BatteryProperties* props) {
66 props->chargerAcOnline = false;
67 props->chargerUsbOnline = false;
68 props->chargerWirelessOnline = false;
69 props->maxChargingCurrent = 0;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -070070 props->maxChargingVoltage = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080071 props->batteryStatus = BATTERY_STATUS_UNKNOWN;
72 props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
73 props->batteryPresent = false;
74 props->batteryLevel = 0;
75 props->batteryVoltage = 0;
76 props->batteryTemperature = 0;
77 props->batteryCurrent = 0;
78 props->batteryCycleCount = 0;
79 props->batteryFullCharge = 0;
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -070080 props->batteryChargeCounter = 0;
Yabin Cuidb04a492016-02-16 17:19:23 -080081 props->batteryTechnology.clear();
82}
83
Todd Poynor1bc572d2017-12-15 06:51:04 +000084BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
85 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
Yabin Cuidb04a492016-02-16 17:19:23 -080086 initBatteryProperties(&props);
87}
88
Hridya Valsaraju7fa72252018-01-12 17:44:33 -080089struct BatteryProperties getBatteryProperties(BatteryMonitor* batteryMonitor) {
90 return batteryMonitor->props;
91}
92
Todd Poynor752faf22013-06-12 13:25:59 -070093int BatteryMonitor::getBatteryStatus(const char* status) {
94 int ret;
95 struct sysfsStringEnumMap batteryStatusMap[] = {
96 { "Unknown", BATTERY_STATUS_UNKNOWN },
97 { "Charging", BATTERY_STATUS_CHARGING },
98 { "Discharging", BATTERY_STATUS_DISCHARGING },
99 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
100 { "Full", BATTERY_STATUS_FULL },
101 { NULL, 0 },
102 };
103
104 ret = mapSysfsString(status, batteryStatusMap);
105 if (ret < 0) {
106 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
107 ret = BATTERY_STATUS_UNKNOWN;
108 }
109
110 return ret;
111}
112
113int BatteryMonitor::getBatteryHealth(const char* status) {
114 int ret;
115 struct sysfsStringEnumMap batteryHealthMap[] = {
116 { "Unknown", BATTERY_HEALTH_UNKNOWN },
117 { "Good", BATTERY_HEALTH_GOOD },
118 { "Overheat", BATTERY_HEALTH_OVERHEAT },
119 { "Dead", BATTERY_HEALTH_DEAD },
120 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
121 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
122 { "Cold", BATTERY_HEALTH_COLD },
Yueyao Zhu9bbfbf72017-06-12 16:55:38 -0700123 // battery health values from JEITA spec
124 { "Warm", BATTERY_HEALTH_GOOD },
125 { "Cool", BATTERY_HEALTH_GOOD },
126 { "Hot", BATTERY_HEALTH_OVERHEAT },
Todd Poynor752faf22013-06-12 13:25:59 -0700127 { NULL, 0 },
128 };
129
130 ret = mapSysfsString(status, batteryHealthMap);
131 if (ret < 0) {
132 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
133 ret = BATTERY_HEALTH_UNKNOWN;
134 }
135
136 return ret;
137}
138
Michael Scott3217c5c2016-06-05 11:20:13 -0700139int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
Steven Moreland2aac3352017-03-10 22:31:08 -0800140 if (android::base::ReadFileToString(path.c_str(), buf)) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700141 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700142 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700143 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700144}
145
146BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700147 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700148 BatteryMonitor::PowerSupplyType ret;
149 struct sysfsStringEnumMap supplyTypeMap[] = {
150 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
151 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
152 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
153 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
154 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
155 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100156 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700157 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
158 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700159 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
160 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
161 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700162 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
163 { NULL, 0 },
164 };
165
Michael Scott3217c5c2016-06-05 11:20:13 -0700166 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700167 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
168
Michael Scott3217c5c2016-06-05 11:20:13 -0700169 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100170 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700171 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700172 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100173 }
Todd Poynor752faf22013-06-12 13:25:59 -0700174
175 return ret;
176}
177
178bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700179 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700180 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700181
182 if (readFromFile(path, &buf) > 0)
183 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700184 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700185
186 return value;
187}
188
189int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700190 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700191 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700192
193 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700194 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700195
Todd Poynor752faf22013-06-12 13:25:59 -0700196 return value;
197}
198
199bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700200 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700201
Yabin Cuidb04a492016-02-16 17:19:23 -0800202 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700203
Todd Poynorf5d30122013-08-12 17:03:35 -0700204 if (!mHealthdConfig->batteryPresentPath.isEmpty())
205 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700206 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700207 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700208
Todd Poynor3db03a52014-05-21 16:28:13 -0700209 props.batteryLevel = mBatteryFixedCapacity ?
210 mBatteryFixedCapacity :
211 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700212 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700213
Ruchi Kandoicc338802015-08-24 13:01:16 -0700214 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
215 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
216
217 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
218 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
219
220 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
221 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
222
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700223 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
224 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
225
Todd Poynor3db03a52014-05-21 16:28:13 -0700226 props.batteryTemperature = mBatteryFixedTemperature ?
227 mBatteryFixedTemperature :
228 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700229
Todd Poynor1bc572d2017-12-15 06:51:04 +0000230 // For devices which do not have battery and are always plugged
231 // into power souce.
232 if (mAlwaysPluggedDevice) {
233 props.chargerAcOnline = true;
234 props.batteryPresent = true;
235 props.batteryStatus = BATTERY_STATUS_CHARGING;
236 props.batteryHealth = BATTERY_HEALTH_GOOD;
237 }
238
Michael Scott3217c5c2016-06-05 11:20:13 -0700239 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700240
Michael Scott3217c5c2016-06-05 11:20:13 -0700241 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
242 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700243
Michael Scott3217c5c2016-06-05 11:20:13 -0700244 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
245 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700246
Michael Scott3217c5c2016-06-05 11:20:13 -0700247 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
248 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700249
250 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700251 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700252
253 for (i = 0; i < mChargerNames.size(); i++) {
254 String8 path;
255 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
256 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700257 if (getIntField(path)) {
258 path.clear();
259 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
260 mChargerNames[i].string());
261 switch(readPowerSupplyType(path)) {
262 case ANDROID_POWER_SUPPLY_TYPE_AC:
263 props.chargerAcOnline = true;
264 break;
265 case ANDROID_POWER_SUPPLY_TYPE_USB:
266 props.chargerUsbOnline = true;
267 break;
268 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
269 props.chargerWirelessOnline = true;
270 break;
271 default:
272 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
273 mChargerNames[i].string());
274 }
275 path.clear();
276 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
277 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700278 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700279 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
280
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700281 path.clear();
282 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
283 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700284
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700285 int ChargingVoltage =
286 (access(path.string(), R_OK) == 0) ? getIntField(path) :
287 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700288
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700289 double power = ((double)ChargingCurrent / MILLION) *
290 ((double)ChargingVoltage / MILLION);
291 if (MaxPower < power) {
292 props.maxChargingCurrent = ChargingCurrent;
293 props.maxChargingVoltage = ChargingVoltage;
294 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700295 }
296 }
297 }
298
Todd Poynor10b235e2013-08-07 15:25:14 -0700299 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700300
Todd Poynor10b235e2013-08-07 15:25:14 -0700301 if (logthis) {
302 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700303 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700304 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700305 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700306 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
307 props.batteryLevel, props.batteryVoltage,
308 props.batteryTemperature < 0 ? "-" : "",
309 abs(props.batteryTemperature / 10),
310 abs(props.batteryTemperature % 10), props.batteryHealth,
311 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700312
Ruchi Kandoicc338802015-08-24 13:01:16 -0700313 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700314 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700315 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
316 " c=%d", props.batteryCurrent);
317 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700318
Ruchi Kandoicc338802015-08-24 13:01:16 -0700319 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
320 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
321 " fc=%d", props.batteryFullCharge);
322 }
323
324 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
325 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
326 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700327 }
328 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800329 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700330 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700331 }
332
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700333 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
334 props.chargerAcOnline ? "a" : "",
335 props.chargerUsbOnline ? "u" : "",
336 props.chargerWirelessOnline ? "w" : "");
337
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700338 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700339 }
340
Todd Poynorc7464c92013-09-10 12:40:00 -0700341 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700342 return props.chargerAcOnline | props.chargerUsbOnline |
343 props.chargerWirelessOnline;
344}
345
Yabin Cuiaedf6032016-02-19 18:03:23 -0800346int BatteryMonitor::getChargeStatus() {
347 int result = BATTERY_STATUS_UNKNOWN;
348 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700349 std::string buf;
350 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
351 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800352 }
353 return result;
354}
355
Todd Poynorc133b712013-08-14 17:39:13 -0700356status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
357 status_t ret = BAD_VALUE;
Jin Qian72adf112017-02-02 17:31:13 -0800358 std::string buf;
Todd Poynorc133b712013-08-14 17:39:13 -0700359
Todd Poynor8f132af2014-05-08 17:15:45 -0700360 val->valueInt64 = LONG_MIN;
361
Todd Poynorc133b712013-08-14 17:39:13 -0700362 switch(id) {
363 case BATTERY_PROP_CHARGE_COUNTER:
364 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700365 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700366 getIntField(mHealthdConfig->batteryChargeCounterPath);
367 ret = NO_ERROR;
368 } else {
369 ret = NAME_NOT_FOUND;
370 }
371 break;
372
373 case BATTERY_PROP_CURRENT_NOW:
374 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700375 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700376 getIntField(mHealthdConfig->batteryCurrentNowPath);
377 ret = NO_ERROR;
378 } else {
379 ret = NAME_NOT_FOUND;
380 }
381 break;
382
Todd Poynorbc102112013-08-27 18:11:49 -0700383 case BATTERY_PROP_CURRENT_AVG:
384 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700385 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700386 getIntField(mHealthdConfig->batteryCurrentAvgPath);
387 ret = NO_ERROR;
388 } else {
389 ret = NAME_NOT_FOUND;
390 }
391 break;
392
Paul Lawrence347c8de2014-03-19 15:04:40 -0700393 case BATTERY_PROP_CAPACITY:
394 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700395 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700396 getIntField(mHealthdConfig->batteryCapacityPath);
397 ret = NO_ERROR;
398 } else {
399 ret = NAME_NOT_FOUND;
400 }
401 break;
402
Todd Poynor8f132af2014-05-08 17:15:45 -0700403 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700404 if (mHealthdConfig->energyCounter) {
405 ret = mHealthdConfig->energyCounter(&val->valueInt64);
406 } else {
407 ret = NAME_NOT_FOUND;
408 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700409 break;
410
Jin Qian72adf112017-02-02 17:31:13 -0800411 case BATTERY_PROP_BATTERY_STATUS:
Todd Poynor1bc572d2017-12-15 06:51:04 +0000412 if (mAlwaysPluggedDevice) {
413 val->valueInt64 = BATTERY_STATUS_CHARGING;
414 } else {
415 val->valueInt64 = getChargeStatus();
416 }
Jin Qian72adf112017-02-02 17:31:13 -0800417 ret = NO_ERROR;
418 break;
419
Todd Poynorc133b712013-08-14 17:39:13 -0700420 default:
421 break;
422 }
423
Todd Poynorc133b712013-08-14 17:39:13 -0700424 return ret;
425}
426
Todd Poynor020369d2013-09-18 20:09:33 -0700427void BatteryMonitor::dumpState(int fd) {
428 int v;
429 char vs[128];
430
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700431 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700432 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700433 props.chargerWirelessOnline, props.maxChargingCurrent,
434 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700435 write(fd, vs, strlen(vs));
436 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
437 props.batteryStatus, props.batteryHealth, props.batteryPresent);
438 write(fd, vs, strlen(vs));
439 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
440 props.batteryLevel, props.batteryVoltage,
441 props.batteryTemperature);
442 write(fd, vs, strlen(vs));
443
444 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
445 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
446 snprintf(vs, sizeof(vs), "current now: %d\n", v);
447 write(fd, vs, strlen(vs));
448 }
449
450 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
451 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
452 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
453 write(fd, vs, strlen(vs));
454 }
455
456 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
457 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
458 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
459 write(fd, vs, strlen(vs));
460 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700461
462 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
463 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
464 write(fd, vs, strlen(vs));
465 }
466
467 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
468 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
469 write(fd, vs, strlen(vs));
470 }
471
472 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
473 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
474 write(fd, vs, strlen(vs));
475 }
Todd Poynor020369d2013-09-18 20:09:33 -0700476}
477
Todd Poynorc7464c92013-09-10 12:40:00 -0700478void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700479 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700480 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700481
Todd Poynorf5d30122013-08-12 17:03:35 -0700482 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800483 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700484 if (dir == NULL) {
485 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
486 } else {
487 struct dirent* entry;
488
James Hawkins588a2ca2016-02-18 14:52:46 -0800489 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700490 const char* name = entry->d_name;
491
492 if (!strcmp(name, ".") || !strcmp(name, ".."))
493 continue;
494
Todd Poynor752faf22013-06-12 13:25:59 -0700495 // Look for "type" file in each subdirectory
496 path.clear();
497 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
498 switch(readPowerSupplyType(path)) {
499 case ANDROID_POWER_SUPPLY_TYPE_AC:
500 case ANDROID_POWER_SUPPLY_TYPE_USB:
501 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
502 path.clear();
503 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
504 if (access(path.string(), R_OK) == 0)
505 mChargerNames.add(String8(name));
506 break;
507
508 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700509 mBatteryDevicePresent = true;
510
Todd Poynorf5d30122013-08-12 17:03:35 -0700511 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700512 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700513 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
514 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700515 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700516 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700517 }
518
Todd Poynorf5d30122013-08-12 17:03:35 -0700519 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700520 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700521 path.appendFormat("%s/%s/health", 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->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700525 }
526
Todd Poynorf5d30122013-08-12 17:03:35 -0700527 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
528 path.clear();
529 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
530 name);
531 if (access(path, R_OK) == 0)
532 mHealthdConfig->batteryPresentPath = path;
533 }
534
535 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
536 path.clear();
537 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
538 name);
539 if (access(path, R_OK) == 0)
540 mHealthdConfig->batteryCapacityPath = path;
541 }
542
543 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
544 path.clear();
545 path.appendFormat("%s/%s/voltage_now",
546 POWER_SUPPLY_SYSFS_PATH, name);
547 if (access(path, R_OK) == 0) {
548 mHealthdConfig->batteryVoltagePath = path;
549 } else {
550 path.clear();
551 path.appendFormat("%s/%s/batt_vol",
552 POWER_SUPPLY_SYSFS_PATH, name);
553 if (access(path, R_OK) == 0)
554 mHealthdConfig->batteryVoltagePath = path;
555 }
556 }
557
Ruchi Kandoicc338802015-08-24 13:01:16 -0700558 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
559 path.clear();
560 path.appendFormat("%s/%s/charge_full",
561 POWER_SUPPLY_SYSFS_PATH, name);
562 if (access(path, R_OK) == 0)
563 mHealthdConfig->batteryFullChargePath = path;
564 }
565
Todd Poynorf5d30122013-08-12 17:03:35 -0700566 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
567 path.clear();
568 path.appendFormat("%s/%s/current_now",
569 POWER_SUPPLY_SYSFS_PATH, name);
570 if (access(path, R_OK) == 0)
571 mHealthdConfig->batteryCurrentNowPath = path;
572 }
573
Ruchi Kandoicc338802015-08-24 13:01:16 -0700574 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
575 path.clear();
576 path.appendFormat("%s/%s/cycle_count",
577 POWER_SUPPLY_SYSFS_PATH, name);
578 if (access(path, R_OK) == 0)
579 mHealthdConfig->batteryCycleCountPath = path;
580 }
581
Todd Poynorbc102112013-08-27 18:11:49 -0700582 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
583 path.clear();
584 path.appendFormat("%s/%s/current_avg",
585 POWER_SUPPLY_SYSFS_PATH, name);
586 if (access(path, R_OK) == 0)
587 mHealthdConfig->batteryCurrentAvgPath = path;
588 }
589
Todd Poynorf5d30122013-08-12 17:03:35 -0700590 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
591 path.clear();
592 path.appendFormat("%s/%s/charge_counter",
593 POWER_SUPPLY_SYSFS_PATH, name);
594 if (access(path, R_OK) == 0)
595 mHealthdConfig->batteryChargeCounterPath = path;
596 }
597
598 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
599 path.clear();
600 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
601 name);
602 if (access(path, R_OK) == 0) {
603 mHealthdConfig->batteryTemperaturePath = path;
604 } else {
605 path.clear();
606 path.appendFormat("%s/%s/batt_temp",
607 POWER_SUPPLY_SYSFS_PATH, name);
608 if (access(path, R_OK) == 0)
609 mHealthdConfig->batteryTemperaturePath = path;
610 }
611 }
612
613 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
614 path.clear();
615 path.appendFormat("%s/%s/technology",
616 POWER_SUPPLY_SYSFS_PATH, name);
617 if (access(path, R_OK) == 0)
618 mHealthdConfig->batteryTechnologyPath = path;
619 }
620
Todd Poynor752faf22013-06-12 13:25:59 -0700621 break;
622
623 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
624 break;
625 }
626 }
Todd Poynor752faf22013-06-12 13:25:59 -0700627 }
628
Ian Pedowitz585ab652015-10-12 19:01:00 -0700629 // Typically the case for devices which do not have a battery and
630 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700631 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700632 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700633 hc->periodic_chores_interval_fast = -1;
634 hc->periodic_chores_interval_slow = -1;
Todd Poynor1bc572d2017-12-15 06:51:04 +0000635 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
636 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
637 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700638 } else {
639 if (mHealthdConfig->batteryStatusPath.isEmpty())
640 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
641 if (mHealthdConfig->batteryHealthPath.isEmpty())
642 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
643 if (mHealthdConfig->batteryPresentPath.isEmpty())
644 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
645 if (mHealthdConfig->batteryCapacityPath.isEmpty())
646 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
647 if (mHealthdConfig->batteryVoltagePath.isEmpty())
648 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
649 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
650 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
651 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
652 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700653 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700654 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
655 if (mHealthdConfig->batteryFullChargePath.isEmpty())
656 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
657 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
658 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700659 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700660
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700661 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
662 && strtol(pval, NULL, 10) != 0) {
663 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
664 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
665 }
Todd Poynor752faf22013-06-12 13:25:59 -0700666}
667
668}; // namespace android