blob: 369a022c90eb0ccbb3d60a283ce6dc88539ec058 [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
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -070045#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
84BatteryMonitor::BatteryMonitor() : mHealthdConfig(nullptr), mBatteryDevicePresent(false),
85 mAlwaysPluggedDevice(false), mBatteryFixedCapacity(0), mBatteryFixedTemperature(0) {
86 initBatteryProperties(&props);
87}
88
Todd Poynor752faf22013-06-12 13:25:59 -070089int BatteryMonitor::getBatteryStatus(const char* status) {
90 int ret;
91 struct sysfsStringEnumMap batteryStatusMap[] = {
92 { "Unknown", BATTERY_STATUS_UNKNOWN },
93 { "Charging", BATTERY_STATUS_CHARGING },
94 { "Discharging", BATTERY_STATUS_DISCHARGING },
95 { "Not charging", BATTERY_STATUS_NOT_CHARGING },
96 { "Full", BATTERY_STATUS_FULL },
97 { NULL, 0 },
98 };
99
100 ret = mapSysfsString(status, batteryStatusMap);
101 if (ret < 0) {
102 KLOG_WARNING(LOG_TAG, "Unknown battery status '%s'\n", status);
103 ret = BATTERY_STATUS_UNKNOWN;
104 }
105
106 return ret;
107}
108
109int BatteryMonitor::getBatteryHealth(const char* status) {
110 int ret;
111 struct sysfsStringEnumMap batteryHealthMap[] = {
112 { "Unknown", BATTERY_HEALTH_UNKNOWN },
113 { "Good", BATTERY_HEALTH_GOOD },
114 { "Overheat", BATTERY_HEALTH_OVERHEAT },
115 { "Dead", BATTERY_HEALTH_DEAD },
116 { "Over voltage", BATTERY_HEALTH_OVER_VOLTAGE },
117 { "Unspecified failure", BATTERY_HEALTH_UNSPECIFIED_FAILURE },
118 { "Cold", BATTERY_HEALTH_COLD },
119 { NULL, 0 },
120 };
121
122 ret = mapSysfsString(status, batteryHealthMap);
123 if (ret < 0) {
124 KLOG_WARNING(LOG_TAG, "Unknown battery health '%s'\n", status);
125 ret = BATTERY_HEALTH_UNKNOWN;
126 }
127
128 return ret;
129}
130
Michael Scott3217c5c2016-06-05 11:20:13 -0700131int BatteryMonitor::readFromFile(const String8& path, std::string* buf) {
132 if (android::base::ReadFileToString(String8::std_string(path), buf)) {
133 *buf = android::base::Trim(*buf);
Todd Poynor752faf22013-06-12 13:25:59 -0700134 }
Michael Scott3217c5c2016-06-05 11:20:13 -0700135 return buf->length();
Todd Poynor752faf22013-06-12 13:25:59 -0700136}
137
138BatteryMonitor::PowerSupplyType BatteryMonitor::readPowerSupplyType(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700139 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700140 BatteryMonitor::PowerSupplyType ret;
141 struct sysfsStringEnumMap supplyTypeMap[] = {
142 { "Unknown", ANDROID_POWER_SUPPLY_TYPE_UNKNOWN },
143 { "Battery", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
Adam Lesinskif13c6ff2016-12-09 19:23:46 -0800144 { "BMS", ANDROID_POWER_SUPPLY_TYPE_BATTERY },
Todd Poynor752faf22013-06-12 13:25:59 -0700145 { "UPS", ANDROID_POWER_SUPPLY_TYPE_AC },
146 { "Mains", ANDROID_POWER_SUPPLY_TYPE_AC },
147 { "USB", ANDROID_POWER_SUPPLY_TYPE_USB },
148 { "USB_DCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Johan Redestig32828612016-02-03 13:45:54 +0100149 { "USB_HVDCP", ANDROID_POWER_SUPPLY_TYPE_AC },
Todd Poynor752faf22013-06-12 13:25:59 -0700150 { "USB_CDP", ANDROID_POWER_SUPPLY_TYPE_AC },
151 { "USB_ACA", ANDROID_POWER_SUPPLY_TYPE_AC },
Benson Leung8a4eef62015-10-07 16:12:04 -0700152 { "USB_C", ANDROID_POWER_SUPPLY_TYPE_AC },
153 { "USB_PD", ANDROID_POWER_SUPPLY_TYPE_AC },
154 { "USB_PD_DRP", ANDROID_POWER_SUPPLY_TYPE_USB },
Todd Poynor752faf22013-06-12 13:25:59 -0700155 { "Wireless", ANDROID_POWER_SUPPLY_TYPE_WIRELESS },
156 { NULL, 0 },
157 };
158
Michael Scott3217c5c2016-06-05 11:20:13 -0700159 if (readFromFile(path, &buf) <= 0)
Todd Poynor752faf22013-06-12 13:25:59 -0700160 return ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
161
Michael Scott3217c5c2016-06-05 11:20:13 -0700162 ret = (BatteryMonitor::PowerSupplyType)mapSysfsString(buf.c_str(), supplyTypeMap);
Johan Redestig32828612016-02-03 13:45:54 +0100163 if (ret < 0) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700164 KLOG_WARNING(LOG_TAG, "Unknown power supply type '%s'\n", buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700165 ret = ANDROID_POWER_SUPPLY_TYPE_UNKNOWN;
Johan Redestig32828612016-02-03 13:45:54 +0100166 }
Todd Poynor752faf22013-06-12 13:25:59 -0700167
168 return ret;
169}
170
171bool BatteryMonitor::getBooleanField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700172 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700173 bool value = false;
Michael Scott3217c5c2016-06-05 11:20:13 -0700174
175 if (readFromFile(path, &buf) > 0)
176 if (buf[0] != '0')
Todd Poynor752faf22013-06-12 13:25:59 -0700177 value = true;
Todd Poynor752faf22013-06-12 13:25:59 -0700178
179 return value;
180}
181
182int BatteryMonitor::getIntField(const String8& path) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700183 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700184 int value = 0;
Michael Scott3217c5c2016-06-05 11:20:13 -0700185
186 if (readFromFile(path, &buf) > 0)
Elliott Hughesda46b392016-10-11 17:09:00 -0700187 android::base::ParseInt(buf, &value);
Michael Scott3217c5c2016-06-05 11:20:13 -0700188
Todd Poynor752faf22013-06-12 13:25:59 -0700189 return value;
190}
191
192bool BatteryMonitor::update(void) {
Todd Poynor10b235e2013-08-07 15:25:14 -0700193 bool logthis;
Todd Poynor752faf22013-06-12 13:25:59 -0700194
Yabin Cuidb04a492016-02-16 17:19:23 -0800195 initBatteryProperties(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700196
Todd Poynorf5d30122013-08-12 17:03:35 -0700197 if (!mHealthdConfig->batteryPresentPath.isEmpty())
198 props.batteryPresent = getBooleanField(mHealthdConfig->batteryPresentPath);
Todd Poynor752faf22013-06-12 13:25:59 -0700199 else
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700200 props.batteryPresent = mBatteryDevicePresent;
Todd Poynor752faf22013-06-12 13:25:59 -0700201
Todd Poynor3db03a52014-05-21 16:28:13 -0700202 props.batteryLevel = mBatteryFixedCapacity ?
203 mBatteryFixedCapacity :
204 getIntField(mHealthdConfig->batteryCapacityPath);
Todd Poynorf5d30122013-08-12 17:03:35 -0700205 props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
Todd Poynorb45f1f52013-07-30 18:57:16 -0700206
Ruchi Kandoicc338802015-08-24 13:01:16 -0700207 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())
208 props.batteryCurrent = getIntField(mHealthdConfig->batteryCurrentNowPath) / 1000;
209
210 if (!mHealthdConfig->batteryFullChargePath.isEmpty())
211 props.batteryFullCharge = getIntField(mHealthdConfig->batteryFullChargePath);
212
213 if (!mHealthdConfig->batteryCycleCountPath.isEmpty())
214 props.batteryCycleCount = getIntField(mHealthdConfig->batteryCycleCountPath);
215
Ruchi Kandoi3f9886b2016-04-07 12:34:40 -0700216 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty())
217 props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
218
Todd Poynor3db03a52014-05-21 16:28:13 -0700219 props.batteryTemperature = mBatteryFixedTemperature ?
220 mBatteryFixedTemperature :
221 getIntField(mHealthdConfig->batteryTemperaturePath);
Todd Poynor752faf22013-06-12 13:25:59 -0700222
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700223 // For devices which do not have battery and are always plugged
224 // into power souce.
225 if (mAlwaysPluggedDevice) {
226 props.chargerAcOnline = true;
227 props.batteryPresent = true;
228 props.batteryStatus = BATTERY_STATUS_CHARGING;
229 props.batteryHealth = BATTERY_HEALTH_GOOD;
230 }
231
Michael Scott3217c5c2016-06-05 11:20:13 -0700232 std::string buf;
Todd Poynor752faf22013-06-12 13:25:59 -0700233
Michael Scott3217c5c2016-06-05 11:20:13 -0700234 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
235 props.batteryStatus = getBatteryStatus(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700236
Michael Scott3217c5c2016-06-05 11:20:13 -0700237 if (readFromFile(mHealthdConfig->batteryHealthPath, &buf) > 0)
238 props.batteryHealth = getBatteryHealth(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700239
Michael Scott3217c5c2016-06-05 11:20:13 -0700240 if (readFromFile(mHealthdConfig->batteryTechnologyPath, &buf) > 0)
241 props.batteryTechnology = String8(buf.c_str());
Todd Poynor752faf22013-06-12 13:25:59 -0700242
243 unsigned int i;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700244 double MaxPower = 0;
Todd Poynor752faf22013-06-12 13:25:59 -0700245
246 for (i = 0; i < mChargerNames.size(); i++) {
247 String8 path;
248 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH,
249 mChargerNames[i].string());
Michael Scott3217c5c2016-06-05 11:20:13 -0700250 if (getIntField(path)) {
251 path.clear();
252 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH,
253 mChargerNames[i].string());
254 switch(readPowerSupplyType(path)) {
255 case ANDROID_POWER_SUPPLY_TYPE_AC:
256 props.chargerAcOnline = true;
257 break;
258 case ANDROID_POWER_SUPPLY_TYPE_USB:
259 props.chargerUsbOnline = true;
260 break;
261 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
262 props.chargerWirelessOnline = true;
263 break;
264 default:
265 KLOG_WARNING(LOG_TAG, "%s: Unknown power supply type\n",
266 mChargerNames[i].string());
267 }
268 path.clear();
269 path.appendFormat("%s/%s/current_max", POWER_SUPPLY_SYSFS_PATH,
270 mChargerNames[i].string());
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700271 int ChargingCurrent =
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700272 (access(path.string(), R_OK) == 0) ? getIntField(path) : 0;
273
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700274 path.clear();
275 path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,
276 mChargerNames[i].string());
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700277
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700278 int ChargingVoltage =
279 (access(path.string(), R_OK) == 0) ? getIntField(path) :
280 DEFAULT_VBUS_VOLTAGE;
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700281
Dmitry Shmidt9f6b80c2016-06-20 12:58:37 -0700282 double power = ((double)ChargingCurrent / MILLION) *
283 ((double)ChargingVoltage / MILLION);
284 if (MaxPower < power) {
285 props.maxChargingCurrent = ChargingCurrent;
286 props.maxChargingVoltage = ChargingVoltage;
287 MaxPower = power;
Todd Poynor752faf22013-06-12 13:25:59 -0700288 }
289 }
290 }
291
Todd Poynor10b235e2013-08-07 15:25:14 -0700292 logthis = !healthd_board_battery_update(&props);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700293
Todd Poynor10b235e2013-08-07 15:25:14 -0700294 if (logthis) {
295 char dmesgline[256];
Ruchi Kandoicc338802015-08-24 13:01:16 -0700296 size_t len;
Todd Poynorc15e9932013-10-22 18:21:27 -0700297 if (props.batteryPresent) {
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700298 snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor10b235e2013-08-07 15:25:14 -0700299 "battery l=%d v=%d t=%s%d.%d h=%d st=%d",
300 props.batteryLevel, props.batteryVoltage,
301 props.batteryTemperature < 0 ? "-" : "",
302 abs(props.batteryTemperature / 10),
303 abs(props.batteryTemperature % 10), props.batteryHealth,
304 props.batteryStatus);
Todd Poynorc15e9932013-10-22 18:21:27 -0700305
Ruchi Kandoicc338802015-08-24 13:01:16 -0700306 len = strlen(dmesgline);
Todd Poynorc15e9932013-10-22 18:21:27 -0700307 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Ruchi Kandoicc338802015-08-24 13:01:16 -0700308 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
309 " c=%d", props.batteryCurrent);
310 }
Todd Poynorc15e9932013-10-22 18:21:27 -0700311
Ruchi Kandoicc338802015-08-24 13:01:16 -0700312 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
313 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
314 " fc=%d", props.batteryFullCharge);
315 }
316
317 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
318 len += snprintf(dmesgline + len, sizeof(dmesgline) - len,
319 " cc=%d", props.batteryCycleCount);
Todd Poynorc15e9932013-10-22 18:21:27 -0700320 }
321 } else {
Ruchi Kandoie9320b72016-02-22 12:46:57 -0800322 len = snprintf(dmesgline, sizeof(dmesgline),
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700323 "battery none");
Todd Poynor10b235e2013-08-07 15:25:14 -0700324 }
325
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700326 snprintf(dmesgline + len, sizeof(dmesgline) - len, " chg=%s%s%s",
327 props.chargerAcOnline ? "a" : "",
328 props.chargerUsbOnline ? "u" : "",
329 props.chargerWirelessOnline ? "w" : "");
330
Mark Salyzynacb1ddf2015-07-23 09:22:50 -0700331 KLOG_WARNING(LOG_TAG, "%s\n", dmesgline);
Todd Poynorb45f1f52013-07-30 18:57:16 -0700332 }
333
Todd Poynorc7464c92013-09-10 12:40:00 -0700334 healthd_mode_ops->battery_update(&props);
Todd Poynor752faf22013-06-12 13:25:59 -0700335 return props.chargerAcOnline | props.chargerUsbOnline |
336 props.chargerWirelessOnline;
337}
338
Yabin Cuiaedf6032016-02-19 18:03:23 -0800339int BatteryMonitor::getChargeStatus() {
340 int result = BATTERY_STATUS_UNKNOWN;
341 if (!mHealthdConfig->batteryStatusPath.isEmpty()) {
Michael Scott3217c5c2016-06-05 11:20:13 -0700342 std::string buf;
343 if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
344 result = getBatteryStatus(buf.c_str());
Yabin Cuiaedf6032016-02-19 18:03:23 -0800345 }
346 return result;
347}
348
Todd Poynorc133b712013-08-14 17:39:13 -0700349status_t BatteryMonitor::getProperty(int id, struct BatteryProperty *val) {
350 status_t ret = BAD_VALUE;
351
Todd Poynor8f132af2014-05-08 17:15:45 -0700352 val->valueInt64 = LONG_MIN;
353
Todd Poynorc133b712013-08-14 17:39:13 -0700354 switch(id) {
355 case BATTERY_PROP_CHARGE_COUNTER:
356 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700357 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700358 getIntField(mHealthdConfig->batteryChargeCounterPath);
359 ret = NO_ERROR;
360 } else {
361 ret = NAME_NOT_FOUND;
362 }
363 break;
364
365 case BATTERY_PROP_CURRENT_NOW:
366 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700367 val->valueInt64 =
Todd Poynorc133b712013-08-14 17:39:13 -0700368 getIntField(mHealthdConfig->batteryCurrentNowPath);
369 ret = NO_ERROR;
370 } else {
371 ret = NAME_NOT_FOUND;
372 }
373 break;
374
Todd Poynorbc102112013-08-27 18:11:49 -0700375 case BATTERY_PROP_CURRENT_AVG:
376 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700377 val->valueInt64 =
Todd Poynorbc102112013-08-27 18:11:49 -0700378 getIntField(mHealthdConfig->batteryCurrentAvgPath);
379 ret = NO_ERROR;
380 } else {
381 ret = NAME_NOT_FOUND;
382 }
383 break;
384
Paul Lawrence347c8de2014-03-19 15:04:40 -0700385 case BATTERY_PROP_CAPACITY:
386 if (!mHealthdConfig->batteryCapacityPath.isEmpty()) {
Todd Poynor8f132af2014-05-08 17:15:45 -0700387 val->valueInt64 =
Paul Lawrence347c8de2014-03-19 15:04:40 -0700388 getIntField(mHealthdConfig->batteryCapacityPath);
389 ret = NO_ERROR;
390 } else {
391 ret = NAME_NOT_FOUND;
392 }
393 break;
394
Todd Poynor8f132af2014-05-08 17:15:45 -0700395 case BATTERY_PROP_ENERGY_COUNTER:
Todd Poynore14b37e2014-05-20 13:54:40 -0700396 if (mHealthdConfig->energyCounter) {
397 ret = mHealthdConfig->energyCounter(&val->valueInt64);
398 } else {
399 ret = NAME_NOT_FOUND;
400 }
Todd Poynor8f132af2014-05-08 17:15:45 -0700401 break;
402
Todd Poynorc133b712013-08-14 17:39:13 -0700403 default:
404 break;
405 }
406
Todd Poynorc133b712013-08-14 17:39:13 -0700407 return ret;
408}
409
Todd Poynor020369d2013-09-18 20:09:33 -0700410void BatteryMonitor::dumpState(int fd) {
411 int v;
412 char vs[128];
413
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700414 snprintf(vs, sizeof(vs), "ac: %d usb: %d wireless: %d current_max: %d voltage_max: %d\n",
Todd Poynor020369d2013-09-18 20:09:33 -0700415 props.chargerAcOnline, props.chargerUsbOnline,
Badhri Jagan Sridharan40e1df42015-10-27 10:43:53 -0700416 props.chargerWirelessOnline, props.maxChargingCurrent,
417 props.maxChargingVoltage);
Todd Poynor020369d2013-09-18 20:09:33 -0700418 write(fd, vs, strlen(vs));
419 snprintf(vs, sizeof(vs), "status: %d health: %d present: %d\n",
420 props.batteryStatus, props.batteryHealth, props.batteryPresent);
421 write(fd, vs, strlen(vs));
422 snprintf(vs, sizeof(vs), "level: %d voltage: %d temp: %d\n",
423 props.batteryLevel, props.batteryVoltage,
424 props.batteryTemperature);
425 write(fd, vs, strlen(vs));
426
427 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
428 v = getIntField(mHealthdConfig->batteryCurrentNowPath);
429 snprintf(vs, sizeof(vs), "current now: %d\n", v);
430 write(fd, vs, strlen(vs));
431 }
432
433 if (!mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
434 v = getIntField(mHealthdConfig->batteryCurrentAvgPath);
435 snprintf(vs, sizeof(vs), "current avg: %d\n", v);
436 write(fd, vs, strlen(vs));
437 }
438
439 if (!mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
440 v = getIntField(mHealthdConfig->batteryChargeCounterPath);
441 snprintf(vs, sizeof(vs), "charge counter: %d\n", v);
442 write(fd, vs, strlen(vs));
443 }
Ruchi Kandoicc338802015-08-24 13:01:16 -0700444
445 if (!mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
446 snprintf(vs, sizeof(vs), "current now: %d\n", props.batteryCurrent);
447 write(fd, vs, strlen(vs));
448 }
449
450 if (!mHealthdConfig->batteryCycleCountPath.isEmpty()) {
451 snprintf(vs, sizeof(vs), "cycle count: %d\n", props.batteryCycleCount);
452 write(fd, vs, strlen(vs));
453 }
454
455 if (!mHealthdConfig->batteryFullChargePath.isEmpty()) {
456 snprintf(vs, sizeof(vs), "Full charge: %d\n", props.batteryFullCharge);
457 write(fd, vs, strlen(vs));
458 }
Todd Poynor020369d2013-09-18 20:09:33 -0700459}
460
Todd Poynorc7464c92013-09-10 12:40:00 -0700461void BatteryMonitor::init(struct healthd_config *hc) {
Todd Poynor752faf22013-06-12 13:25:59 -0700462 String8 path;
Todd Poynor3db03a52014-05-21 16:28:13 -0700463 char pval[PROPERTY_VALUE_MAX];
Todd Poynor752faf22013-06-12 13:25:59 -0700464
Todd Poynorf5d30122013-08-12 17:03:35 -0700465 mHealthdConfig = hc;
James Hawkins588a2ca2016-02-18 14:52:46 -0800466 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(POWER_SUPPLY_SYSFS_PATH), closedir);
Todd Poynor752faf22013-06-12 13:25:59 -0700467 if (dir == NULL) {
468 KLOG_ERROR(LOG_TAG, "Could not open %s\n", POWER_SUPPLY_SYSFS_PATH);
469 } else {
470 struct dirent* entry;
471
James Hawkins588a2ca2016-02-18 14:52:46 -0800472 while ((entry = readdir(dir.get()))) {
Todd Poynor752faf22013-06-12 13:25:59 -0700473 const char* name = entry->d_name;
474
475 if (!strcmp(name, ".") || !strcmp(name, ".."))
476 continue;
477
Todd Poynor752faf22013-06-12 13:25:59 -0700478 // Look for "type" file in each subdirectory
479 path.clear();
480 path.appendFormat("%s/%s/type", POWER_SUPPLY_SYSFS_PATH, name);
481 switch(readPowerSupplyType(path)) {
482 case ANDROID_POWER_SUPPLY_TYPE_AC:
483 case ANDROID_POWER_SUPPLY_TYPE_USB:
484 case ANDROID_POWER_SUPPLY_TYPE_WIRELESS:
485 path.clear();
486 path.appendFormat("%s/%s/online", POWER_SUPPLY_SYSFS_PATH, name);
487 if (access(path.string(), R_OK) == 0)
488 mChargerNames.add(String8(name));
489 break;
490
491 case ANDROID_POWER_SUPPLY_TYPE_BATTERY:
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700492 mBatteryDevicePresent = true;
493
Todd Poynorf5d30122013-08-12 17:03:35 -0700494 if (mHealthdConfig->batteryStatusPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700495 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700496 path.appendFormat("%s/%s/status", POWER_SUPPLY_SYSFS_PATH,
497 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700498 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700499 mHealthdConfig->batteryStatusPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700500 }
501
Todd Poynorf5d30122013-08-12 17:03:35 -0700502 if (mHealthdConfig->batteryHealthPath.isEmpty()) {
Todd Poynor752faf22013-06-12 13:25:59 -0700503 path.clear();
Todd Poynorf5d30122013-08-12 17:03:35 -0700504 path.appendFormat("%s/%s/health", POWER_SUPPLY_SYSFS_PATH,
505 name);
Todd Poynor752faf22013-06-12 13:25:59 -0700506 if (access(path, R_OK) == 0)
Todd Poynorf5d30122013-08-12 17:03:35 -0700507 mHealthdConfig->batteryHealthPath = path;
Todd Poynor752faf22013-06-12 13:25:59 -0700508 }
509
Todd Poynorf5d30122013-08-12 17:03:35 -0700510 if (mHealthdConfig->batteryPresentPath.isEmpty()) {
511 path.clear();
512 path.appendFormat("%s/%s/present", POWER_SUPPLY_SYSFS_PATH,
513 name);
514 if (access(path, R_OK) == 0)
515 mHealthdConfig->batteryPresentPath = path;
516 }
517
518 if (mHealthdConfig->batteryCapacityPath.isEmpty()) {
519 path.clear();
520 path.appendFormat("%s/%s/capacity", POWER_SUPPLY_SYSFS_PATH,
521 name);
522 if (access(path, R_OK) == 0)
523 mHealthdConfig->batteryCapacityPath = path;
524 }
525
526 if (mHealthdConfig->batteryVoltagePath.isEmpty()) {
527 path.clear();
528 path.appendFormat("%s/%s/voltage_now",
529 POWER_SUPPLY_SYSFS_PATH, name);
530 if (access(path, R_OK) == 0) {
531 mHealthdConfig->batteryVoltagePath = path;
532 } else {
533 path.clear();
534 path.appendFormat("%s/%s/batt_vol",
535 POWER_SUPPLY_SYSFS_PATH, name);
536 if (access(path, R_OK) == 0)
537 mHealthdConfig->batteryVoltagePath = path;
538 }
539 }
540
Ruchi Kandoicc338802015-08-24 13:01:16 -0700541 if (mHealthdConfig->batteryFullChargePath.isEmpty()) {
542 path.clear();
543 path.appendFormat("%s/%s/charge_full",
544 POWER_SUPPLY_SYSFS_PATH, name);
545 if (access(path, R_OK) == 0)
546 mHealthdConfig->batteryFullChargePath = path;
547 }
548
Todd Poynorf5d30122013-08-12 17:03:35 -0700549 if (mHealthdConfig->batteryCurrentNowPath.isEmpty()) {
550 path.clear();
551 path.appendFormat("%s/%s/current_now",
552 POWER_SUPPLY_SYSFS_PATH, name);
553 if (access(path, R_OK) == 0)
554 mHealthdConfig->batteryCurrentNowPath = path;
555 }
556
Ruchi Kandoicc338802015-08-24 13:01:16 -0700557 if (mHealthdConfig->batteryCycleCountPath.isEmpty()) {
558 path.clear();
559 path.appendFormat("%s/%s/cycle_count",
560 POWER_SUPPLY_SYSFS_PATH, name);
561 if (access(path, R_OK) == 0)
562 mHealthdConfig->batteryCycleCountPath = path;
563 }
564
Todd Poynorbc102112013-08-27 18:11:49 -0700565 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
566 path.clear();
567 path.appendFormat("%s/%s/current_avg",
568 POWER_SUPPLY_SYSFS_PATH, name);
569 if (access(path, R_OK) == 0)
570 mHealthdConfig->batteryCurrentAvgPath = path;
571 }
572
Todd Poynorf5d30122013-08-12 17:03:35 -0700573 if (mHealthdConfig->batteryChargeCounterPath.isEmpty()) {
574 path.clear();
575 path.appendFormat("%s/%s/charge_counter",
576 POWER_SUPPLY_SYSFS_PATH, name);
577 if (access(path, R_OK) == 0)
578 mHealthdConfig->batteryChargeCounterPath = path;
579 }
580
581 if (mHealthdConfig->batteryTemperaturePath.isEmpty()) {
582 path.clear();
583 path.appendFormat("%s/%s/temp", POWER_SUPPLY_SYSFS_PATH,
584 name);
585 if (access(path, R_OK) == 0) {
586 mHealthdConfig->batteryTemperaturePath = path;
587 } else {
588 path.clear();
589 path.appendFormat("%s/%s/batt_temp",
590 POWER_SUPPLY_SYSFS_PATH, name);
591 if (access(path, R_OK) == 0)
592 mHealthdConfig->batteryTemperaturePath = path;
593 }
594 }
595
596 if (mHealthdConfig->batteryTechnologyPath.isEmpty()) {
597 path.clear();
598 path.appendFormat("%s/%s/technology",
599 POWER_SUPPLY_SYSFS_PATH, name);
600 if (access(path, R_OK) == 0)
601 mHealthdConfig->batteryTechnologyPath = path;
602 }
603
Todd Poynor752faf22013-06-12 13:25:59 -0700604 break;
605
606 case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
607 break;
608 }
609 }
Todd Poynor752faf22013-06-12 13:25:59 -0700610 }
611
Ian Pedowitz585ab652015-10-12 19:01:00 -0700612 // Typically the case for devices which do not have a battery and
613 // and are always plugged into AC mains.
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700614 if (!mBatteryDevicePresent) {
Todd Poynorebeb0c02014-09-23 14:54:24 -0700615 KLOG_WARNING(LOG_TAG, "No battery devices found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700616 hc->periodic_chores_interval_fast = -1;
617 hc->periodic_chores_interval_slow = -1;
Ruchi Kandoifabd4902016-02-29 13:13:39 -0800618 mBatteryFixedCapacity = ALWAYS_PLUGGED_CAPACITY;
619 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
620 mAlwaysPluggedDevice = true;
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700621 } else {
622 if (mHealthdConfig->batteryStatusPath.isEmpty())
623 KLOG_WARNING(LOG_TAG, "BatteryStatusPath not found\n");
624 if (mHealthdConfig->batteryHealthPath.isEmpty())
625 KLOG_WARNING(LOG_TAG, "BatteryHealthPath not found\n");
626 if (mHealthdConfig->batteryPresentPath.isEmpty())
627 KLOG_WARNING(LOG_TAG, "BatteryPresentPath not found\n");
628 if (mHealthdConfig->batteryCapacityPath.isEmpty())
629 KLOG_WARNING(LOG_TAG, "BatteryCapacityPath not found\n");
630 if (mHealthdConfig->batteryVoltagePath.isEmpty())
631 KLOG_WARNING(LOG_TAG, "BatteryVoltagePath not found\n");
632 if (mHealthdConfig->batteryTemperaturePath.isEmpty())
633 KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
634 if (mHealthdConfig->batteryTechnologyPath.isEmpty())
635 KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
Ruchi Kandoif18ec9f2015-09-28 13:35:59 -0700636 if (mHealthdConfig->batteryCurrentNowPath.isEmpty())
Ruchi Kandoicc338802015-08-24 13:01:16 -0700637 KLOG_WARNING(LOG_TAG, "BatteryCurrentNowPath not found\n");
638 if (mHealthdConfig->batteryFullChargePath.isEmpty())
639 KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
640 if (mHealthdConfig->batteryCycleCountPath.isEmpty())
641 KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
Todd Poynor6dcc45e2013-10-21 20:26:25 -0700642 }
Todd Poynor3db03a52014-05-21 16:28:13 -0700643
Ruchi Kandoia78fc232014-07-10 15:06:21 -0700644 if (property_get("ro.boot.fake_battery", pval, NULL) > 0
645 && strtol(pval, NULL, 10) != 0) {
646 mBatteryFixedCapacity = FAKE_BATTERY_CAPACITY;
647 mBatteryFixedTemperature = FAKE_BATTERY_TEMPERATURE;
648 }
Todd Poynor752faf22013-06-12 13:25:59 -0700649}
650
651}; // namespace android